]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/ide/ide-floppy.c
ide-floppy: rename end_request handler properly
[linux-2.6-omap-h63xx.git] / drivers / ide / ide-floppy.c
1 /*
2  * IDE ATAPI floppy driver.
3  *
4  * Copyright (C) 1996-1999  Gadi Oxman <gadio@netvision.net.il>
5  * Copyright (C) 2000-2002  Paul Bristow <paul@paulbristow.net>
6  * Copyright (C) 2005       Bartlomiej Zolnierkiewicz
7  *
8  * This driver supports the following IDE floppy drives:
9  *
10  * LS-120/240 SuperDisk
11  * Iomega Zip 100/250
12  * Iomega PC Card Clik!/PocketZip
13  *
14  * For a historical changelog see
15  * Documentation/ide/ChangeLog.ide-floppy.1996-2002
16  */
17
18 #define IDEFLOPPY_VERSION "1.00"
19
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/kernel.h>
24 #include <linux/delay.h>
25 #include <linux/timer.h>
26 #include <linux/mm.h>
27 #include <linux/interrupt.h>
28 #include <linux/major.h>
29 #include <linux/errno.h>
30 #include <linux/genhd.h>
31 #include <linux/slab.h>
32 #include <linux/cdrom.h>
33 #include <linux/ide.h>
34 #include <linux/bitops.h>
35 #include <linux/mutex.h>
36
37 #include <scsi/scsi_ioctl.h>
38
39 #include <asm/byteorder.h>
40 #include <linux/irq.h>
41 #include <linux/uaccess.h>
42 #include <linux/io.h>
43 #include <asm/unaligned.h>
44
45 /* define to see debug info */
46 #define IDEFLOPPY_DEBUG_LOG             0
47
48 /* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
49 #define IDEFLOPPY_DEBUG(fmt, args...)
50
51 #if IDEFLOPPY_DEBUG_LOG
52 #define debug_log(fmt, args...) \
53         printk(KERN_INFO "ide-floppy: " fmt, ## args)
54 #else
55 #define debug_log(fmt, args...) do {} while (0)
56 #endif
57
58
59 /* Some drives require a longer irq timeout. */
60 #define IDEFLOPPY_WAIT_CMD              (5 * WAIT_CMD)
61
62 /*
63  * After each failed packet command we issue a request sense command and retry
64  * the packet command IDEFLOPPY_MAX_PC_RETRIES times.
65  */
66 #define IDEFLOPPY_MAX_PC_RETRIES        3
67
68 /*
69  * With each packet command, we allocate a buffer of IDEFLOPPY_PC_BUFFER_SIZE
70  * bytes.
71  */
72 #define IDEFLOPPY_PC_BUFFER_SIZE        256
73
74 /*
75  * In various places in the driver, we need to allocate storage for packet
76  * commands and requests, which will remain valid while we leave the driver to
77  * wait for an interrupt or a timeout event.
78  */
79 #define IDEFLOPPY_PC_STACK              (10 + IDEFLOPPY_MAX_PC_RETRIES)
80
81 /* Packet command flag bits. */
82 enum {
83         /* 1 when we prefer to use DMA if possible */
84         PC_FLAG_DMA_RECOMMENDED = (1 << 0),
85         /* 1 while DMA in progress */
86         PC_FLAG_DMA_IN_PROGRESS = (1 << 1),
87         /* 1 when encountered problem during DMA */
88         PC_FLAG_DMA_ERROR       = (1 << 2),
89         /* Data direction */
90         PC_FLAG_WRITING         = (1 << 3),
91         /* Suppress error reporting */
92         PC_FLAG_SUPPRESS_ERROR  = (1 << 4),
93 };
94
95 /* format capacities descriptor codes */
96 #define CAPACITY_INVALID        0x00
97 #define CAPACITY_UNFORMATTED    0x01
98 #define CAPACITY_CURRENT        0x02
99 #define CAPACITY_NO_CARTRIDGE   0x03
100
101 /*
102  * Most of our global data which we need to save even as we leave the driver
103  * due to an interrupt or a timer event is stored in a variable of type
104  * idefloppy_floppy_t, defined below.
105  */
106 typedef struct ide_floppy_obj {
107         ide_drive_t     *drive;
108         ide_driver_t    *driver;
109         struct gendisk  *disk;
110         struct kref     kref;
111         unsigned int    openers;        /* protected by BKL for now */
112
113         /* Current packet command */
114         struct ide_atapi_pc *pc;
115         /* Last failed packet command */
116         struct ide_atapi_pc *failed_pc;
117         /* Packet command stack */
118         struct ide_atapi_pc pc_stack[IDEFLOPPY_PC_STACK];
119         /* Next free packet command storage space */
120         int pc_stack_index;
121         struct request rq_stack[IDEFLOPPY_PC_STACK];
122         /* We implement a circular array */
123         int rq_stack_index;
124
125         /* Last error information */
126         u8 sense_key, asc, ascq;
127         /* delay this long before sending packet command */
128         u8 ticks;
129         int progress_indication;
130
131         /* Device information */
132         /* Current format */
133         int blocks, block_size, bs_factor;
134         /* Last format capacity descriptor */
135         u8 cap_desc[8];
136         /* Copy of the flexible disk page */
137         u8 flexible_disk_page[32];
138         /* Write protect */
139         int wp;
140         /* Supports format progress report */
141         int srfp;
142         /* Status/Action flags */
143         unsigned long flags;
144 } idefloppy_floppy_t;
145
146 #define IDEFLOPPY_TICKS_DELAY   HZ/20   /* default delay for ZIP 100 (50ms) */
147
148 /* Floppy flag bits values. */
149 enum {
150         /* DRQ interrupt device */
151         IDEFLOPPY_FLAG_DRQ_INTERRUPT            = (1 << 0),
152         /* Media may have changed */
153         IDEFLOPPY_FLAG_MEDIA_CHANGED            = (1 << 1),
154         /* Format in progress */
155         IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS       = (1 << 2),
156         /* Avoid commands not supported in Clik drive */
157         IDEFLOPPY_FLAG_CLIK_DRIVE               = (1 << 3),
158         /* Requires BH algorithm for packets */
159         IDEFLOPPY_FLAG_ZIP_DRIVE                = (1 << 4),
160 };
161
162 /* Defines for the MODE SENSE command */
163 #define MODE_SENSE_CURRENT              0x00
164 #define MODE_SENSE_CHANGEABLE           0x01
165 #define MODE_SENSE_DEFAULT              0x02
166 #define MODE_SENSE_SAVED                0x03
167
168 /* IOCTLs used in low-level formatting. */
169 #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED        0x4600
170 #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY     0x4601
171 #define IDEFLOPPY_IOCTL_FORMAT_START            0x4602
172 #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS     0x4603
173
174 /* Error code returned in rq->errors to the higher part of the driver. */
175 #define IDEFLOPPY_ERROR_GENERAL         101
176
177 /*
178  * Pages of the SELECT SENSE / MODE SENSE packet commands.
179  * See SFF-8070i spec.
180  */
181 #define IDEFLOPPY_CAPABILITIES_PAGE     0x1b
182 #define IDEFLOPPY_FLEXIBLE_DISK_PAGE    0x05
183
184 static DEFINE_MUTEX(idefloppy_ref_mutex);
185
186 #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
187
188 #define ide_floppy_g(disk) \
189         container_of((disk)->private_data, struct ide_floppy_obj, driver)
190
191 static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
192 {
193         struct ide_floppy_obj *floppy = NULL;
194
195         mutex_lock(&idefloppy_ref_mutex);
196         floppy = ide_floppy_g(disk);
197         if (floppy)
198                 kref_get(&floppy->kref);
199         mutex_unlock(&idefloppy_ref_mutex);
200         return floppy;
201 }
202
203 static void idefloppy_cleanup_obj(struct kref *);
204
205 static void ide_floppy_put(struct ide_floppy_obj *floppy)
206 {
207         mutex_lock(&idefloppy_ref_mutex);
208         kref_put(&floppy->kref, idefloppy_cleanup_obj);
209         mutex_unlock(&idefloppy_ref_mutex);
210 }
211
212 /*
213  * Used to finish servicing a request. For read/write requests, we will call
214  * ide_end_request to pass to the next buffer.
215  */
216 static int idefloppy_end_request(ide_drive_t *drive, int uptodate, int nsecs)
217 {
218         idefloppy_floppy_t *floppy = drive->driver_data;
219         struct request *rq = HWGROUP(drive)->rq;
220         int error;
221
222         debug_log("Reached %s\n", __func__);
223
224         switch (uptodate) {
225         case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
226         case 1: error = 0; break;
227         default: error = uptodate;
228         }
229         if (error)
230                 floppy->failed_pc = NULL;
231         /* Why does this happen? */
232         if (!rq)
233                 return 0;
234         if (!blk_special_request(rq)) {
235                 /* our real local end request function */
236                 ide_end_request(drive, uptodate, nsecs);
237                 return 0;
238         }
239         rq->errors = error;
240         /* fixme: need to move this local also */
241         ide_end_drive_cmd(drive, 0, 0);
242         return 0;
243 }
244
245 static void ide_floppy_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
246                                   unsigned int bcount, int direction)
247 {
248         struct request *rq = pc->rq;
249         struct req_iterator iter;
250         struct bio_vec *bvec;
251         unsigned long flags;
252         int count, done = 0;
253         char *data;
254
255         rq_for_each_segment(bvec, rq, iter) {
256                 if (!bcount)
257                         break;
258
259                 count = min(bvec->bv_len, bcount);
260
261                 data = bvec_kmap_irq(bvec, &flags);
262                 if (direction)
263                         drive->hwif->atapi_output_bytes(drive, data, count);
264                 else
265                         drive->hwif->atapi_input_bytes(drive, data, count);
266                 bvec_kunmap_irq(data, &flags);
267
268                 bcount -= count;
269                 pc->b_count += count;
270                 done += count;
271         }
272
273         idefloppy_end_request(drive, 1, done >> 9);
274
275         if (bcount) {
276                 printk(KERN_ERR "%s: leftover data in %s, bcount == %d\n",
277                                 drive->name, __func__, bcount);
278                 if (direction)
279                         ide_atapi_write_zeros(drive, bcount);
280                 else
281                         ide_atapi_discard_data(drive, bcount);
282         }
283 }
284
285 static void idefloppy_update_buffers(ide_drive_t *drive,
286                                 struct ide_atapi_pc *pc)
287 {
288         struct request *rq = pc->rq;
289         struct bio *bio = rq->bio;
290
291         while ((bio = rq->bio) != NULL)
292                 idefloppy_end_request(drive, 1, 0);
293 }
294
295 /*
296  * Generate a new packet command request in front of the request queue, before
297  * the current request so that it will be processed immediately, on the next
298  * pass through the driver.
299  */
300 static void idefloppy_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
301                 struct request *rq)
302 {
303         struct ide_floppy_obj *floppy = drive->driver_data;
304
305         ide_init_drive_cmd(rq);
306         rq->buffer = (char *) pc;
307         rq->cmd_type = REQ_TYPE_SPECIAL;
308         rq->rq_disk = floppy->disk;
309         (void) ide_do_drive_cmd(drive, rq, ide_preempt);
310 }
311
312 static struct ide_atapi_pc *idefloppy_next_pc_storage(ide_drive_t *drive)
313 {
314         idefloppy_floppy_t *floppy = drive->driver_data;
315
316         if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
317                 floppy->pc_stack_index = 0;
318         return (&floppy->pc_stack[floppy->pc_stack_index++]);
319 }
320
321 static struct request *idefloppy_next_rq_storage(ide_drive_t *drive)
322 {
323         idefloppy_floppy_t *floppy = drive->driver_data;
324
325         if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
326                 floppy->rq_stack_index = 0;
327         return (&floppy->rq_stack[floppy->rq_stack_index++]);
328 }
329
330 static void idefloppy_request_sense_callback(ide_drive_t *drive)
331 {
332         idefloppy_floppy_t *floppy = drive->driver_data;
333         u8 *buf = floppy->pc->buf;
334
335         debug_log("Reached %s\n", __func__);
336
337         if (!floppy->pc->error) {
338                 floppy->sense_key = buf[2] & 0x0F;
339                 floppy->asc = buf[12];
340                 floppy->ascq = buf[13];
341                 floppy->progress_indication = buf[15] & 0x80 ?
342                         (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
343
344                 if (floppy->failed_pc)
345                         debug_log("pc = %x, sense key = %x, asc = %x,"
346                                         " ascq = %x\n",
347                                         floppy->failed_pc->c[0],
348                                         floppy->sense_key,
349                                         floppy->asc,
350                                         floppy->ascq);
351                 else
352                         debug_log("sense key = %x, asc = %x, ascq = %x\n",
353                                         floppy->sense_key,
354                                         floppy->asc,
355                                         floppy->ascq);
356
357
358                 idefloppy_end_request(drive, 1, 0);
359         } else {
360                 printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting"
361                                 " request!\n");
362                 idefloppy_end_request(drive, 0, 0);
363         }
364 }
365
366 /* General packet command callback function. */
367 static void idefloppy_pc_callback(ide_drive_t *drive)
368 {
369         idefloppy_floppy_t *floppy = drive->driver_data;
370
371         debug_log("Reached %s\n", __func__);
372
373         idefloppy_end_request(drive, floppy->pc->error ? 0 : 1, 0);
374 }
375
376 static void idefloppy_init_pc(struct ide_atapi_pc *pc)
377 {
378         memset(pc->c, 0, 12);
379         pc->retries = 0;
380         pc->flags = 0;
381         pc->req_xfer = 0;
382         pc->buf = pc->pc_buf;
383         pc->buf_size = IDEFLOPPY_PC_BUFFER_SIZE;
384         pc->idefloppy_callback = &idefloppy_pc_callback;
385 }
386
387 static void idefloppy_create_request_sense_cmd(struct ide_atapi_pc *pc)
388 {
389         idefloppy_init_pc(pc);
390         pc->c[0] = GPCMD_REQUEST_SENSE;
391         pc->c[4] = 255;
392         pc->req_xfer = 18;
393         pc->idefloppy_callback = &idefloppy_request_sense_callback;
394 }
395
396 /*
397  * Called when an error was detected during the last packet command. We queue a
398  * request sense packet command in the head of the request list.
399  */
400 static void idefloppy_retry_pc(ide_drive_t *drive)
401 {
402         struct ide_atapi_pc *pc;
403         struct request *rq;
404
405         (void)ide_read_error(drive);
406         pc = idefloppy_next_pc_storage(drive);
407         rq = idefloppy_next_rq_storage(drive);
408         idefloppy_create_request_sense_cmd(pc);
409         idefloppy_queue_pc_head(drive, pc, rq);
410 }
411
412 /* The usual interrupt handler called during a packet command. */
413 static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
414 {
415         idefloppy_floppy_t *floppy = drive->driver_data;
416         ide_hwif_t *hwif = drive->hwif;
417         struct ide_atapi_pc *pc = floppy->pc;
418         struct request *rq = pc->rq;
419         xfer_func_t *xferfunc;
420         unsigned int temp;
421         int dma_error = 0;
422         u16 bcount;
423         u8 stat, ireason;
424
425         debug_log("Reached %s interrupt handler\n", __func__);
426
427         if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
428                 dma_error = hwif->ide_dma_end(drive);
429                 if (dma_error) {
430                         printk(KERN_ERR "%s: DMA %s error\n", drive->name,
431                                         rq_data_dir(rq) ? "write" : "read");
432                         pc->flags |= PC_FLAG_DMA_ERROR;
433                 } else {
434                         pc->xferred = pc->req_xfer;
435                         idefloppy_update_buffers(drive, pc);
436                 }
437                 debug_log("DMA finished\n");
438         }
439
440         /* Clear the interrupt */
441         stat = ide_read_status(drive);
442
443         /* No more interrupts */
444         if ((stat & DRQ_STAT) == 0) {
445                 debug_log("Packet command completed, %d bytes transferred\n",
446                                 pc->xferred);
447                 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
448
449                 local_irq_enable_in_hardirq();
450
451                 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
452                         /* Error detected */
453                         debug_log("%s: I/O error\n", drive->name);
454                         rq->errors++;
455                         if (pc->c[0] == GPCMD_REQUEST_SENSE) {
456                                 printk(KERN_ERR "ide-floppy: I/O error in "
457                                         "request sense command\n");
458                                 return ide_do_reset(drive);
459                         }
460                         /* Retry operation */
461                         idefloppy_retry_pc(drive);
462                         /* queued, but not started */
463                         return ide_stopped;
464                 }
465                 pc->error = 0;
466                 if (floppy->failed_pc == pc)
467                         floppy->failed_pc = NULL;
468                 /* Command finished - Call the callback function */
469                 pc->idefloppy_callback(drive);
470                 return ide_stopped;
471         }
472
473         if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
474                 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
475                 printk(KERN_ERR "ide-floppy: The floppy wants to issue "
476                         "more interrupts in DMA mode\n");
477                 ide_dma_off(drive);
478                 return ide_do_reset(drive);
479         }
480
481         /* Get the number of bytes to transfer */
482         bcount = (hwif->INB(hwif->io_ports[IDE_BCOUNTH_OFFSET]) << 8) |
483                   hwif->INB(hwif->io_ports[IDE_BCOUNTL_OFFSET]);
484         /* on this interrupt */
485         ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
486
487         if (ireason & CD) {
488                 printk(KERN_ERR "ide-floppy: CoD != 0 in %s\n", __func__);
489                 return ide_do_reset(drive);
490         }
491         if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
492                 /* Hopefully, we will never get here */
493                 printk(KERN_ERR "ide-floppy: We wanted to %s, ",
494                                 (ireason & IO) ? "Write" : "Read");
495                 printk(KERN_ERR "but the floppy wants us to %s !\n",
496                                 (ireason & IO) ? "Read" : "Write");
497                 return ide_do_reset(drive);
498         }
499         if (!(pc->flags & PC_FLAG_WRITING)) {
500                 /* Reading - Check that we have enough space */
501                 temp = pc->xferred + bcount;
502                 if (temp > pc->req_xfer) {
503                         if (temp > pc->buf_size) {
504                                 printk(KERN_ERR "ide-floppy: The floppy wants "
505                                         "to send us more data than expected "
506                                         "- discarding data\n");
507                                 ide_atapi_discard_data(drive, bcount);
508
509                                 ide_set_handler(drive,
510                                                 &idefloppy_pc_intr,
511                                                 IDEFLOPPY_WAIT_CMD,
512                                                 NULL);
513                                 return ide_started;
514                         }
515                         debug_log("The floppy wants to send us more data than"
516                                         " expected - allowing transfer\n");
517                 }
518         }
519         if (pc->flags & PC_FLAG_WRITING)
520                 xferfunc = hwif->atapi_output_bytes;
521         else
522                 xferfunc = hwif->atapi_input_bytes;
523
524         if (pc->buf)
525                 xferfunc(drive, pc->cur_pos, bcount);
526         else
527                 ide_floppy_io_buffers(drive, pc, bcount,
528                                       !!(pc->flags & PC_FLAG_WRITING));
529
530         /* Update the current position */
531         pc->xferred += bcount;
532         pc->cur_pos += bcount;
533
534         /* And set the interrupt handler again */
535         ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
536         return ide_started;
537 }
538
539 /*
540  * This is the original routine that did the packet transfer.
541  * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
542  * for that drive below. The algorithm is chosen based on drive type
543  */
544 static ide_startstop_t idefloppy_transfer_pc(ide_drive_t *drive)
545 {
546         ide_hwif_t *hwif = drive->hwif;
547         ide_startstop_t startstop;
548         idefloppy_floppy_t *floppy = drive->driver_data;
549         u8 ireason;
550
551         if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
552                 printk(KERN_ERR "ide-floppy: Strange, packet command "
553                                 "initiated yet DRQ isn't asserted\n");
554                 return startstop;
555         }
556         ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
557         if ((ireason & CD) == 0 || (ireason & IO)) {
558                 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
559                                 "issuing a packet command\n");
560                 return ide_do_reset(drive);
561         }
562
563         /* Set the interrupt routine */
564         ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
565         /* Send the actual packet */
566         HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
567         return ide_started;
568 }
569
570
571 /*
572  * What we have here is a classic case of a top half / bottom half interrupt
573  * service routine. In interrupt mode, the device sends an interrupt to signal
574  * that it is ready to receive a packet. However, we need to delay about 2-3
575  * ticks before issuing the packet or we gets in trouble.
576  *
577  * So, follow carefully. transfer_pc1 is called as an interrupt (or directly).
578  * In either case, when the device says it's ready for a packet, we schedule
579  * the packet transfer to occur about 2-3 ticks later in transfer_pc2.
580  */
581 static int idefloppy_transfer_pc2(ide_drive_t *drive)
582 {
583         idefloppy_floppy_t *floppy = drive->driver_data;
584
585         /* Send the actual packet */
586         HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
587         /* Timeout for the packet command */
588         return IDEFLOPPY_WAIT_CMD;
589 }
590
591 static ide_startstop_t idefloppy_transfer_pc1(ide_drive_t *drive)
592 {
593         ide_hwif_t *hwif = drive->hwif;
594         idefloppy_floppy_t *floppy = drive->driver_data;
595         ide_startstop_t startstop;
596         u8 ireason;
597
598         if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
599                 printk(KERN_ERR "ide-floppy: Strange, packet command "
600                                 "initiated yet DRQ isn't asserted\n");
601                 return startstop;
602         }
603         ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]);
604         if ((ireason & CD) == 0 || (ireason & IO)) {
605                 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
606                                 "while issuing a packet command\n");
607                 return ide_do_reset(drive);
608         }
609         /*
610          * The following delay solves a problem with ATAPI Zip 100 drives
611          * where the Busy flag was apparently being deasserted before the
612          * unit was ready to receive data. This was happening on a
613          * 1200 MHz Athlon system. 10/26/01 25msec is too short,
614          * 40 and 50msec work well. idefloppy_pc_intr will not be actually
615          * used until after the packet is moved in about 50 msec.
616          */
617
618         ide_set_handler(drive, &idefloppy_pc_intr, floppy->ticks,
619                         &idefloppy_transfer_pc2);
620         return ide_started;
621 }
622
623 static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
624                                     struct ide_atapi_pc *pc)
625 {
626         /* supress error messages resulting from Medium not present */
627         if (floppy->sense_key == 0x02 &&
628             floppy->asc       == 0x3a &&
629             floppy->ascq      == 0x00)
630                 return;
631
632         printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
633                         "asc = %2x, ascq = %2x\n",
634                         floppy->drive->name, pc->c[0], floppy->sense_key,
635                         floppy->asc, floppy->ascq);
636
637 }
638
639 static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive,
640                 struct ide_atapi_pc *pc)
641 {
642         idefloppy_floppy_t *floppy = drive->driver_data;
643         ide_hwif_t *hwif = drive->hwif;
644         ide_handler_t *pkt_xfer_routine;
645         u16 bcount;
646         u8 dma;
647
648         if (floppy->failed_pc == NULL &&
649             pc->c[0] != GPCMD_REQUEST_SENSE)
650                 floppy->failed_pc = pc;
651         /* Set the current packet command */
652         floppy->pc = pc;
653
654         if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
655                 if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
656                         ide_floppy_report_error(floppy, pc);
657                 /* Giving up */
658                 pc->error = IDEFLOPPY_ERROR_GENERAL;
659
660                 floppy->failed_pc = NULL;
661                 pc->idefloppy_callback(drive);
662                 return ide_stopped;
663         }
664
665         debug_log("Retry number - %d\n", pc->retries);
666
667         pc->retries++;
668         /* We haven't transferred any data yet */
669         pc->xferred = 0;
670         pc->cur_pos = pc->buf;
671         bcount = min(pc->req_xfer, 63 * 1024);
672
673         if (pc->flags & PC_FLAG_DMA_ERROR) {
674                 pc->flags &= ~PC_FLAG_DMA_ERROR;
675                 ide_dma_off(drive);
676         }
677         dma = 0;
678
679         if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
680                 dma = !hwif->dma_setup(drive);
681
682         ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
683                            IDE_TFLAG_OUT_DEVICE, bcount, dma);
684
685         if (dma) {
686                 /* Begin DMA, if necessary */
687                 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
688                 hwif->dma_start(drive);
689         }
690
691         /* Can we transfer the packet when we get the interrupt or wait? */
692         if (floppy->flags & IDEFLOPPY_FLAG_ZIP_DRIVE) {
693                 /* wait */
694                 pkt_xfer_routine = &idefloppy_transfer_pc1;
695         } else {
696                 /* immediate */
697                 pkt_xfer_routine = &idefloppy_transfer_pc;
698         }
699
700         if (floppy->flags & IDEFLOPPY_FLAG_DRQ_INTERRUPT) {
701                 /* Issue the packet command */
702                 ide_execute_command(drive, WIN_PACKETCMD,
703                                 pkt_xfer_routine,
704                                 IDEFLOPPY_WAIT_CMD,
705                                 NULL);
706                 return ide_started;
707         } else {
708                 /* Issue the packet command */
709                 hwif->OUTB(WIN_PACKETCMD, hwif->io_ports[IDE_COMMAND_OFFSET]);
710                 return (*pkt_xfer_routine) (drive);
711         }
712 }
713
714 static void idefloppy_rw_callback(ide_drive_t *drive)
715 {
716         debug_log("Reached %s\n", __func__);
717
718         idefloppy_end_request(drive, 1, 0);
719         return;
720 }
721
722 static void idefloppy_create_prevent_cmd(struct ide_atapi_pc *pc, int prevent)
723 {
724         debug_log("creating prevent removal command, prevent = %d\n", prevent);
725
726         idefloppy_init_pc(pc);
727         pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
728         pc->c[4] = prevent;
729 }
730
731 static void idefloppy_create_read_capacity_cmd(struct ide_atapi_pc *pc)
732 {
733         idefloppy_init_pc(pc);
734         pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
735         pc->c[7] = 255;
736         pc->c[8] = 255;
737         pc->req_xfer = 255;
738 }
739
740 static void idefloppy_create_format_unit_cmd(struct ide_atapi_pc *pc, int b,
741                 int l, int flags)
742 {
743         idefloppy_init_pc(pc);
744         pc->c[0] = GPCMD_FORMAT_UNIT;
745         pc->c[1] = 0x17;
746
747         memset(pc->buf, 0, 12);
748         pc->buf[1] = 0xA2;
749         /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
750
751         if (flags & 1)                          /* Verify bit on... */
752                 pc->buf[1] ^= 0x20;             /* ... turn off DCRT bit */
753         pc->buf[3] = 8;
754
755         put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buf[4]));
756         put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buf[8]));
757         pc->buf_size = 12;
758         pc->flags |= PC_FLAG_WRITING;
759 }
760
761 /* A mode sense command is used to "sense" floppy parameters. */
762 static void idefloppy_create_mode_sense_cmd(struct ide_atapi_pc *pc,
763                 u8 page_code, u8 type)
764 {
765         u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
766
767         idefloppy_init_pc(pc);
768         pc->c[0] = GPCMD_MODE_SENSE_10;
769         pc->c[1] = 0;
770         pc->c[2] = page_code + (type << 6);
771
772         switch (page_code) {
773         case IDEFLOPPY_CAPABILITIES_PAGE:
774                 length += 12;
775                 break;
776         case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
777                 length += 32;
778                 break;
779         default:
780                 printk(KERN_ERR "ide-floppy: unsupported page code "
781                                 "in create_mode_sense_cmd\n");
782         }
783         put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
784         pc->req_xfer = length;
785 }
786
787 static void idefloppy_create_start_stop_cmd(struct ide_atapi_pc *pc, int start)
788 {
789         idefloppy_init_pc(pc);
790         pc->c[0] = GPCMD_START_STOP_UNIT;
791         pc->c[4] = start;
792 }
793
794 static void idefloppy_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
795 {
796         idefloppy_init_pc(pc);
797         pc->c[0] = GPCMD_TEST_UNIT_READY;
798 }
799
800 static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
801                                     struct ide_atapi_pc *pc, struct request *rq,
802                                     unsigned long sector)
803 {
804         int block = sector / floppy->bs_factor;
805         int blocks = rq->nr_sectors / floppy->bs_factor;
806         int cmd = rq_data_dir(rq);
807
808         debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
809                 block, blocks);
810
811         idefloppy_init_pc(pc);
812         pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
813         put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
814         put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
815
816         pc->idefloppy_callback = &idefloppy_rw_callback;
817         pc->rq = rq;
818         pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
819         if (rq->cmd_flags & REQ_RW)
820                 pc->flags |= PC_FLAG_WRITING;
821         pc->buf = NULL;
822         pc->req_xfer = pc->buf_size = blocks * floppy->block_size;
823         pc->flags |= PC_FLAG_DMA_RECOMMENDED;
824 }
825
826 static void idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy,
827                 struct ide_atapi_pc *pc, struct request *rq)
828 {
829         idefloppy_init_pc(pc);
830         pc->idefloppy_callback = &idefloppy_rw_callback;
831         memcpy(pc->c, rq->cmd, sizeof(pc->c));
832         pc->rq = rq;
833         pc->b_count = rq->data_len;
834         if (rq->data_len && rq_data_dir(rq) == WRITE)
835                 pc->flags |= PC_FLAG_WRITING;
836         pc->buf = rq->data;
837         if (rq->bio)
838                 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
839         /*
840          * possibly problematic, doesn't look like ide-floppy correctly
841          * handled scattered requests if dma fails...
842          */
843         pc->req_xfer = pc->buf_size = rq->data_len;
844 }
845
846 static ide_startstop_t idefloppy_do_request(ide_drive_t *drive,
847                 struct request *rq, sector_t block_s)
848 {
849         idefloppy_floppy_t *floppy = drive->driver_data;
850         struct ide_atapi_pc *pc;
851         unsigned long block = (unsigned long)block_s;
852
853         debug_log("dev: %s, cmd_type: %x, errors: %d\n",
854                         rq->rq_disk ? rq->rq_disk->disk_name : "?",
855                         rq->cmd_type, rq->errors);
856         debug_log("sector: %ld, nr_sectors: %ld, "
857                         "current_nr_sectors: %d\n", (long)rq->sector,
858                         rq->nr_sectors, rq->current_nr_sectors);
859
860         if (rq->errors >= ERROR_MAX) {
861                 if (floppy->failed_pc)
862                         ide_floppy_report_error(floppy, floppy->failed_pc);
863                 else
864                         printk(KERN_ERR "ide-floppy: %s: I/O error\n",
865                                 drive->name);
866                 idefloppy_end_request(drive, 0, 0);
867                 return ide_stopped;
868         }
869         if (blk_fs_request(rq)) {
870                 if (((long)rq->sector % floppy->bs_factor) ||
871                     (rq->nr_sectors % floppy->bs_factor)) {
872                         printk(KERN_ERR "%s: unsupported r/w request size\n",
873                                         drive->name);
874                         idefloppy_end_request(drive, 0, 0);
875                         return ide_stopped;
876                 }
877                 pc = idefloppy_next_pc_storage(drive);
878                 idefloppy_create_rw_cmd(floppy, pc, rq, block);
879         } else if (blk_special_request(rq)) {
880                 pc = (struct ide_atapi_pc *) rq->buffer;
881         } else if (blk_pc_request(rq)) {
882                 pc = idefloppy_next_pc_storage(drive);
883                 idefloppy_blockpc_cmd(floppy, pc, rq);
884         } else {
885                 blk_dump_rq_flags(rq,
886                         "ide-floppy: unsupported command in queue");
887                 idefloppy_end_request(drive, 0, 0);
888                 return ide_stopped;
889         }
890
891         pc->rq = rq;
892         return idefloppy_issue_pc(drive, pc);
893 }
894
895 /*
896  * Add a special packet command request to the tail of the request queue,
897  * and wait for it to be serviced.
898  */
899 static int idefloppy_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
900 {
901         struct ide_floppy_obj *floppy = drive->driver_data;
902         struct request rq;
903
904         ide_init_drive_cmd(&rq);
905         rq.buffer = (char *) pc;
906         rq.cmd_type = REQ_TYPE_SPECIAL;
907         rq.rq_disk = floppy->disk;
908
909         return ide_do_drive_cmd(drive, &rq, ide_wait);
910 }
911
912 /*
913  * Look at the flexible disk page parameters. We ignore the CHS capacity
914  * parameters and use the LBA parameters instead.
915  */
916 static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
917 {
918         idefloppy_floppy_t *floppy = drive->driver_data;
919         struct ide_atapi_pc pc;
920         u8 *page;
921         int capacity, lba_capacity;
922         u16 transfer_rate, sector_size, cyls, rpm;
923         u8 heads, sectors;
924
925         idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE,
926                                         MODE_SENSE_CURRENT);
927
928         if (idefloppy_queue_pc_tail(drive, &pc)) {
929                 printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
930                                 " parameters\n");
931                 return 1;
932         }
933         floppy->wp = !!(pc.buf[3] & 0x80);
934         set_disk_ro(floppy->disk, floppy->wp);
935         page = &pc.buf[8];
936
937         transfer_rate = be16_to_cpu(*(u16 *)&pc.buf[8 + 2]);
938         sector_size   = be16_to_cpu(*(u16 *)&pc.buf[8 + 6]);
939         cyls          = be16_to_cpu(*(u16 *)&pc.buf[8 + 8]);
940         rpm           = be16_to_cpu(*(u16 *)&pc.buf[8 + 28]);
941         heads         = pc.buf[8 + 4];
942         sectors       = pc.buf[8 + 5];
943
944         capacity = cyls * heads * sectors * sector_size;
945
946         if (memcmp(page, &floppy->flexible_disk_page, 32))
947                 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
948                                 "%d sector size, %d rpm\n",
949                                 drive->name, capacity / 1024, cyls, heads,
950                                 sectors, transfer_rate / 8, sector_size, rpm);
951
952         memcpy(&floppy->flexible_disk_page, page, 32);
953         drive->bios_cyl = cyls;
954         drive->bios_head = heads;
955         drive->bios_sect = sectors;
956         lba_capacity = floppy->blocks * floppy->block_size;
957
958         if (capacity < lba_capacity) {
959                 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
960                         "bytes, but the drive only handles %d\n",
961                         drive->name, lba_capacity, capacity);
962                 floppy->blocks = floppy->block_size ?
963                         capacity / floppy->block_size : 0;
964         }
965         return 0;
966 }
967
968 static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
969 {
970         idefloppy_floppy_t *floppy = drive->driver_data;
971         struct ide_atapi_pc pc;
972
973         floppy->srfp = 0;
974         idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
975                                                  MODE_SENSE_CURRENT);
976
977         pc.flags |= PC_FLAG_SUPPRESS_ERROR;
978         if (idefloppy_queue_pc_tail(drive, &pc))
979                 return 1;
980
981         floppy->srfp = pc.buf[8 + 2] & 0x40;
982         return (0);
983 }
984
985 /*
986  * Determine if a media is present in the floppy drive, and if so, its LBA
987  * capacity.
988  */
989 static int ide_floppy_get_capacity(ide_drive_t *drive)
990 {
991         idefloppy_floppy_t *floppy = drive->driver_data;
992         struct ide_atapi_pc pc;
993         u8 *cap_desc;
994         u8 header_len, desc_cnt;
995         int i, rc = 1, blocks, length;
996
997         drive->bios_cyl = 0;
998         drive->bios_head = drive->bios_sect = 0;
999         floppy->blocks = 0;
1000         floppy->bs_factor = 1;
1001         set_capacity(floppy->disk, 0);
1002
1003         idefloppy_create_read_capacity_cmd(&pc);
1004         if (idefloppy_queue_pc_tail(drive, &pc)) {
1005                 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1006                 return 1;
1007         }
1008         header_len = pc.buf[3];
1009         cap_desc = &pc.buf[4];
1010         desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
1011
1012         for (i = 0; i < desc_cnt; i++) {
1013                 unsigned int desc_start = 4 + i*8;
1014
1015                 blocks = be32_to_cpu(*(u32 *)&pc.buf[desc_start]);
1016                 length = be16_to_cpu(*(u16 *)&pc.buf[desc_start + 6]);
1017
1018                 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
1019                                 i, blocks * length / 1024, blocks, length);
1020
1021                 if (i)
1022                         continue;
1023                 /*
1024                  * the code below is valid only for the 1st descriptor, ie i=0
1025                  */
1026
1027                 switch (pc.buf[desc_start + 4] & 0x03) {
1028                 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1029                 case CAPACITY_UNFORMATTED:
1030                         if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE))
1031                                 /*
1032                                  * If it is not a clik drive, break out
1033                                  * (maintains previous driver behaviour)
1034                                  */
1035                                 break;
1036                 case CAPACITY_CURRENT:
1037                         /* Normal Zip/LS-120 disks */
1038                         if (memcmp(cap_desc, &floppy->cap_desc, 8))
1039                                 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
1040                                         "sector size\n", drive->name,
1041                                         blocks * length / 1024, blocks, length);
1042                         memcpy(&floppy->cap_desc, cap_desc, 8);
1043
1044                         if (!length || length % 512) {
1045                                 printk(KERN_NOTICE "%s: %d bytes block size "
1046                                         "not supported\n", drive->name, length);
1047                         } else {
1048                                 floppy->blocks = blocks;
1049                                 floppy->block_size = length;
1050                                 floppy->bs_factor = length / 512;
1051                                 if (floppy->bs_factor != 1)
1052                                         printk(KERN_NOTICE "%s: warning: non "
1053                                                 "512 bytes block size not "
1054                                                 "fully supported\n",
1055                                                 drive->name);
1056                                 rc = 0;
1057                         }
1058                         break;
1059                 case CAPACITY_NO_CARTRIDGE:
1060                         /*
1061                          * This is a KERN_ERR so it appears on screen
1062                          * for the user to see
1063                          */
1064                         printk(KERN_ERR "%s: No disk in drive\n", drive->name);
1065                         break;
1066                 case CAPACITY_INVALID:
1067                         printk(KERN_ERR "%s: Invalid capacity for disk "
1068                                 "in drive\n", drive->name);
1069                         break;
1070                 }
1071                 debug_log("Descriptor 0 Code: %d\n",
1072                           pc.buf[desc_start + 4] & 0x03);
1073         }
1074
1075         /* Clik! disk does not support get_flexible_disk_page */
1076         if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE))
1077                 (void) ide_floppy_get_flexible_disk_page(drive);
1078
1079         set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
1080         return rc;
1081 }
1082
1083 /*
1084  * Obtain the list of formattable capacities.
1085  * Very similar to ide_floppy_get_capacity, except that we push the capacity
1086  * descriptors to userland, instead of our own structures.
1087  *
1088  * Userland gives us the following structure:
1089  *
1090  * struct idefloppy_format_capacities {
1091  *      int nformats;
1092  *      struct {
1093  *              int nblocks;
1094  *              int blocksize;
1095  *      } formats[];
1096  * };
1097  *
1098  * userland initializes nformats to the number of allocated formats[] records.
1099  * On exit we set nformats to the number of records we've actually initialized.
1100  */
1101
1102 static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
1103 {
1104         struct ide_atapi_pc pc;
1105         u8 header_len, desc_cnt;
1106         int i, blocks, length, u_array_size, u_index;
1107         int __user *argp;
1108
1109         if (get_user(u_array_size, arg))
1110                 return (-EFAULT);
1111
1112         if (u_array_size <= 0)
1113                 return (-EINVAL);
1114
1115         idefloppy_create_read_capacity_cmd(&pc);
1116         if (idefloppy_queue_pc_tail(drive, &pc)) {
1117                 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1118                 return (-EIO);
1119         }
1120         header_len = pc.buf[3];
1121         desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
1122
1123         u_index = 0;
1124         argp = arg + 1;
1125
1126         /*
1127          * We always skip the first capacity descriptor.  That's the current
1128          * capacity.  We are interested in the remaining descriptors, the
1129          * formattable capacities.
1130          */
1131         for (i = 1; i < desc_cnt; i++) {
1132                 unsigned int desc_start = 4 + i*8;
1133
1134                 if (u_index >= u_array_size)
1135                         break;  /* User-supplied buffer too small */
1136
1137                 blocks = be32_to_cpu(*(u32 *)&pc.buf[desc_start]);
1138                 length = be16_to_cpu(*(u16 *)&pc.buf[desc_start + 6]);
1139
1140                 if (put_user(blocks, argp))
1141                         return(-EFAULT);
1142                 ++argp;
1143
1144                 if (put_user(length, argp))
1145                         return (-EFAULT);
1146                 ++argp;
1147
1148                 ++u_index;
1149         }
1150
1151         if (put_user(u_index, arg))
1152                 return (-EFAULT);
1153         return (0);
1154 }
1155
1156 /*
1157  * Get ATAPI_FORMAT_UNIT progress indication.
1158  *
1159  * Userland gives a pointer to an int.  The int is set to a progress
1160  * indicator 0-65536, with 65536=100%.
1161  *
1162  * If the drive does not support format progress indication, we just check
1163  * the dsc bit, and return either 0 or 65536.
1164  */
1165
1166 static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
1167 {
1168         idefloppy_floppy_t *floppy = drive->driver_data;
1169         struct ide_atapi_pc pc;
1170         int progress_indication = 0x10000;
1171
1172         if (floppy->srfp) {
1173                 idefloppy_create_request_sense_cmd(&pc);
1174                 if (idefloppy_queue_pc_tail(drive, &pc))
1175                         return (-EIO);
1176
1177                 if (floppy->sense_key == 2 &&
1178                     floppy->asc == 4 &&
1179                     floppy->ascq == 4)
1180                         progress_indication = floppy->progress_indication;
1181
1182                 /* Else assume format_unit has finished, and we're at 0x10000 */
1183         } else {
1184                 unsigned long flags;
1185                 u8 stat;
1186
1187                 local_irq_save(flags);
1188                 stat = ide_read_status(drive);
1189                 local_irq_restore(flags);
1190
1191                 progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
1192         }
1193         if (put_user(progress_indication, arg))
1194                 return (-EFAULT);
1195
1196         return (0);
1197 }
1198
1199 static sector_t idefloppy_capacity(ide_drive_t *drive)
1200 {
1201         idefloppy_floppy_t *floppy = drive->driver_data;
1202         unsigned long capacity = floppy->blocks * floppy->bs_factor;
1203
1204         return capacity;
1205 }
1206
1207 /*
1208  * Check whether we can support a drive, based on the ATAPI IDENTIFY command
1209  * results.
1210  */
1211 static int idefloppy_identify_device(ide_drive_t *drive, struct hd_driveid *id)
1212 {
1213         u8 gcw[2];
1214         u8 device_type, protocol, removable, drq_type, packet_size;
1215
1216         *((u16 *) &gcw) = id->config;
1217
1218         device_type =  gcw[1] & 0x1F;
1219         removable   = (gcw[0] & 0x80) >> 7;
1220         protocol    = (gcw[1] & 0xC0) >> 6;
1221         drq_type    = (gcw[0] & 0x60) >> 5;
1222         packet_size =  gcw[0] & 0x03;
1223
1224 #ifdef CONFIG_PPC
1225         /* kludge for Apple PowerBook internal zip */
1226         if (device_type == 5 &&
1227             !strstr(id->model, "CD-ROM") && strstr(id->model, "ZIP"))
1228                 device_type = 0;
1229 #endif
1230
1231         if (protocol != 2)
1232                 printk(KERN_ERR "ide-floppy: Protocol (0x%02x) is not ATAPI\n",
1233                         protocol);
1234         else if (device_type != 0)
1235                 printk(KERN_ERR "ide-floppy: Device type (0x%02x) is not set "
1236                                 "to floppy\n", device_type);
1237         else if (!removable)
1238                 printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
1239         else if (drq_type == 3)
1240                 printk(KERN_ERR "ide-floppy: Sorry, DRQ type (0x%02x) not "
1241                                 "supported\n", drq_type);
1242         else if (packet_size != 0)
1243                 printk(KERN_ERR "ide-floppy: Packet size (0x%02x) is not 12 "
1244                                 "bytes\n", packet_size);
1245         else
1246                 return 1;
1247         return 0;
1248 }
1249
1250 #ifdef CONFIG_IDE_PROC_FS
1251 static void idefloppy_add_settings(ide_drive_t *drive)
1252 {
1253         idefloppy_floppy_t *floppy = drive->driver_data;
1254
1255         ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1,
1256                         &drive->bios_cyl, NULL);
1257         ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1,
1258                         &drive->bios_head, NULL);
1259         ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0,  63, 1, 1,
1260                         &drive->bios_sect, NULL);
1261         ide_add_setting(drive, "ticks",    SETTING_RW, TYPE_BYTE, 0, 255, 1, 1,
1262                         &floppy->ticks,  NULL);
1263 }
1264 #else
1265 static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
1266 #endif
1267
1268 static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
1269 {
1270         u8 gcw[2];
1271
1272         *((u16 *) &gcw) = drive->id->config;
1273         floppy->pc = floppy->pc_stack;
1274
1275         if (((gcw[0] & 0x60) >> 5) == 1)
1276                 floppy->flags |= IDEFLOPPY_FLAG_DRQ_INTERRUPT;
1277         /*
1278          * We used to check revisions here. At this point however I'm giving up.
1279          * Just assume they are all broken, its easier.
1280          *
1281          * The actual reason for the workarounds was likely a driver bug after
1282          * all rather than a firmware bug, and the workaround below used to hide
1283          * it. It should be fixed as of version 1.9, but to be on the safe side
1284          * we'll leave the limitation below for the 2.2.x tree.
1285          */
1286         if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
1287                 floppy->flags |= IDEFLOPPY_FLAG_ZIP_DRIVE;
1288                 /* This value will be visible in the /proc/ide/hdx/settings */
1289                 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1290                 blk_queue_max_sectors(drive->queue, 64);
1291         }
1292
1293         /*
1294          * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
1295          * nasty clicking noises without it, so please don't remove this.
1296          */
1297         if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
1298                 blk_queue_max_sectors(drive->queue, 64);
1299                 floppy->flags |= IDEFLOPPY_FLAG_CLIK_DRIVE;
1300         }
1301
1302         (void) ide_floppy_get_capacity(drive);
1303         idefloppy_add_settings(drive);
1304 }
1305
1306 static void ide_floppy_remove(ide_drive_t *drive)
1307 {
1308         idefloppy_floppy_t *floppy = drive->driver_data;
1309         struct gendisk *g = floppy->disk;
1310
1311         ide_proc_unregister_driver(drive, floppy->driver);
1312
1313         del_gendisk(g);
1314
1315         ide_floppy_put(floppy);
1316 }
1317
1318 static void idefloppy_cleanup_obj(struct kref *kref)
1319 {
1320         struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1321         ide_drive_t *drive = floppy->drive;
1322         struct gendisk *g = floppy->disk;
1323
1324         drive->driver_data = NULL;
1325         g->private_data = NULL;
1326         put_disk(g);
1327         kfree(floppy);
1328 }
1329
1330 #ifdef CONFIG_IDE_PROC_FS
1331 static int proc_idefloppy_read_capacity(char *page, char **start, off_t off,
1332                 int count, int *eof, void *data)
1333 {
1334         ide_drive_t*drive = (ide_drive_t *)data;
1335         int len;
1336
1337         len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive));
1338         PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
1339 }
1340
1341 static ide_proc_entry_t idefloppy_proc[] = {
1342         { "capacity",   S_IFREG|S_IRUGO, proc_idefloppy_read_capacity,  NULL },
1343         { "geometry",   S_IFREG|S_IRUGO, proc_ide_read_geometry,        NULL },
1344         { NULL, 0, NULL, NULL }
1345 };
1346 #endif  /* CONFIG_IDE_PROC_FS */
1347
1348 static int ide_floppy_probe(ide_drive_t *);
1349
1350 static ide_driver_t idefloppy_driver = {
1351         .gen_driver = {
1352                 .owner          = THIS_MODULE,
1353                 .name           = "ide-floppy",
1354                 .bus            = &ide_bus_type,
1355         },
1356         .probe                  = ide_floppy_probe,
1357         .remove                 = ide_floppy_remove,
1358         .version                = IDEFLOPPY_VERSION,
1359         .media                  = ide_floppy,
1360         .supports_dsc_overlap   = 0,
1361         .do_request             = idefloppy_do_request,
1362         .end_request            = idefloppy_end_request,
1363         .error                  = __ide_error,
1364         .abort                  = __ide_abort,
1365 #ifdef CONFIG_IDE_PROC_FS
1366         .proc                   = idefloppy_proc,
1367 #endif
1368 };
1369
1370 static int idefloppy_open(struct inode *inode, struct file *filp)
1371 {
1372         struct gendisk *disk = inode->i_bdev->bd_disk;
1373         struct ide_floppy_obj *floppy;
1374         ide_drive_t *drive;
1375         struct ide_atapi_pc pc;
1376         int ret = 0;
1377
1378         debug_log("Reached %s\n", __func__);
1379
1380         floppy = ide_floppy_get(disk);
1381         if (!floppy)
1382                 return -ENXIO;
1383
1384         drive = floppy->drive;
1385
1386         floppy->openers++;
1387
1388         if (floppy->openers == 1) {
1389                 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
1390                 /* Just in case */
1391
1392                 idefloppy_create_test_unit_ready_cmd(&pc);
1393                 if (idefloppy_queue_pc_tail(drive, &pc)) {
1394                         idefloppy_create_start_stop_cmd(&pc, 1);
1395                         (void) idefloppy_queue_pc_tail(drive, &pc);
1396                 }
1397
1398                 if (ide_floppy_get_capacity(drive)
1399                    && (filp->f_flags & O_NDELAY) == 0
1400                     /*
1401                      * Allow O_NDELAY to open a drive without a disk, or with an
1402                      * unreadable disk, so that we can get the format capacity
1403                      * of the drive or begin the format - Sam
1404                      */
1405                     ) {
1406                         ret = -EIO;
1407                         goto out_put_floppy;
1408                 }
1409
1410                 if (floppy->wp && (filp->f_mode & 2)) {
1411                         ret = -EROFS;
1412                         goto out_put_floppy;
1413                 }
1414                 floppy->flags |= IDEFLOPPY_FLAG_MEDIA_CHANGED;
1415                 /* IOMEGA Clik! drives do not support lock/unlock commands */
1416                 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
1417                         idefloppy_create_prevent_cmd(&pc, 1);
1418                         (void) idefloppy_queue_pc_tail(drive, &pc);
1419                 }
1420                 check_disk_change(inode->i_bdev);
1421         } else if (floppy->flags & IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS) {
1422                 ret = -EBUSY;
1423                 goto out_put_floppy;
1424         }
1425         return 0;
1426
1427 out_put_floppy:
1428         floppy->openers--;
1429         ide_floppy_put(floppy);
1430         return ret;
1431 }
1432
1433 static int idefloppy_release(struct inode *inode, struct file *filp)
1434 {
1435         struct gendisk *disk = inode->i_bdev->bd_disk;
1436         struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1437         ide_drive_t *drive = floppy->drive;
1438         struct ide_atapi_pc pc;
1439
1440         debug_log("Reached %s\n", __func__);
1441
1442         if (floppy->openers == 1) {
1443                 /* IOMEGA Clik! drives do not support lock/unlock commands */
1444                 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
1445                         idefloppy_create_prevent_cmd(&pc, 0);
1446                         (void) idefloppy_queue_pc_tail(drive, &pc);
1447                 }
1448
1449                 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
1450         }
1451
1452         floppy->openers--;
1453
1454         ide_floppy_put(floppy);
1455
1456         return 0;
1457 }
1458
1459 static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1460 {
1461         struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1462         ide_drive_t *drive = floppy->drive;
1463
1464         geo->heads = drive->bios_head;
1465         geo->sectors = drive->bios_sect;
1466         geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1467         return 0;
1468 }
1469
1470 static int ide_floppy_lockdoor(idefloppy_floppy_t *floppy,
1471                 struct ide_atapi_pc *pc, unsigned long arg, unsigned int cmd)
1472 {
1473         if (floppy->openers > 1)
1474                 return -EBUSY;
1475
1476         /* The IOMEGA Clik! Drive doesn't support this command -
1477          * no room for an eject mechanism */
1478         if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
1479                 int prevent = arg ? 1 : 0;
1480
1481                 if (cmd == CDROMEJECT)
1482                         prevent = 0;
1483
1484                 idefloppy_create_prevent_cmd(pc, prevent);
1485                 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1486         }
1487
1488         if (cmd == CDROMEJECT) {
1489                 idefloppy_create_start_stop_cmd(pc, 2);
1490                 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1491         }
1492
1493         return 0;
1494 }
1495
1496 static int ide_floppy_format_unit(idefloppy_floppy_t *floppy,
1497                                   int __user *arg)
1498 {
1499         int blocks, length, flags, err = 0;
1500         struct ide_atapi_pc pc;
1501
1502         if (floppy->openers > 1) {
1503                 /* Don't format if someone is using the disk */
1504                 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
1505                 return -EBUSY;
1506         }
1507
1508         floppy->flags |= IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
1509
1510         /*
1511          * Send ATAPI_FORMAT_UNIT to the drive.
1512          *
1513          * Userland gives us the following structure:
1514          *
1515          * struct idefloppy_format_command {
1516          *        int nblocks;
1517          *        int blocksize;
1518          *        int flags;
1519          *        } ;
1520          *
1521          * flags is a bitmask, currently, the only defined flag is:
1522          *
1523          *        0x01 - verify media after format.
1524          */
1525         if (get_user(blocks, arg) ||
1526                         get_user(length, arg+1) ||
1527                         get_user(flags, arg+2)) {
1528                 err = -EFAULT;
1529                 goto out;
1530         }
1531
1532         (void) idefloppy_get_sfrp_bit(floppy->drive);
1533         idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1534
1535         if (idefloppy_queue_pc_tail(floppy->drive, &pc))
1536                 err = -EIO;
1537
1538 out:
1539         if (err)
1540                 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
1541         return err;
1542 }
1543
1544
1545 static int idefloppy_ioctl(struct inode *inode, struct file *file,
1546                         unsigned int cmd, unsigned long arg)
1547 {
1548         struct block_device *bdev = inode->i_bdev;
1549         struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1550         ide_drive_t *drive = floppy->drive;
1551         struct ide_atapi_pc pc;
1552         void __user *argp = (void __user *)arg;
1553         int err;
1554
1555         switch (cmd) {
1556         case CDROMEJECT:
1557                 /* fall through */
1558         case CDROM_LOCKDOOR:
1559                 return ide_floppy_lockdoor(floppy, &pc, arg, cmd);
1560         case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1561                 return 0;
1562         case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
1563                 return ide_floppy_get_format_capacities(drive, argp);
1564         case IDEFLOPPY_IOCTL_FORMAT_START:
1565                 if (!(file->f_mode & 2))
1566                         return -EPERM;
1567
1568                 return ide_floppy_format_unit(floppy, (int __user *)arg);
1569         case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1570                 return idefloppy_get_format_progress(drive, argp);
1571         }
1572
1573         /*
1574          * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1575          * and CDROM_SEND_PACKET (legacy) ioctls
1576          */
1577         if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1578                 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1579                                         bdev->bd_disk, cmd, argp);
1580         else
1581                 err = -ENOTTY;
1582
1583         if (err == -ENOTTY)
1584                 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1585
1586         return err;
1587 }
1588
1589 static int idefloppy_media_changed(struct gendisk *disk)
1590 {
1591         struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1592         ide_drive_t *drive = floppy->drive;
1593         int ret;
1594
1595         /* do not scan partitions twice if this is a removable device */
1596         if (drive->attach) {
1597                 drive->attach = 0;
1598                 return 0;
1599         }
1600         ret = !!(floppy->flags & IDEFLOPPY_FLAG_MEDIA_CHANGED);
1601         floppy->flags &= ~IDEFLOPPY_FLAG_MEDIA_CHANGED;
1602         return ret;
1603 }
1604
1605 static int idefloppy_revalidate_disk(struct gendisk *disk)
1606 {
1607         struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1608         set_capacity(disk, idefloppy_capacity(floppy->drive));
1609         return 0;
1610 }
1611
1612 static struct block_device_operations idefloppy_ops = {
1613         .owner          = THIS_MODULE,
1614         .open           = idefloppy_open,
1615         .release        = idefloppy_release,
1616         .ioctl          = idefloppy_ioctl,
1617         .getgeo         = idefloppy_getgeo,
1618         .media_changed  = idefloppy_media_changed,
1619         .revalidate_disk= idefloppy_revalidate_disk
1620 };
1621
1622 static int ide_floppy_probe(ide_drive_t *drive)
1623 {
1624         idefloppy_floppy_t *floppy;
1625         struct gendisk *g;
1626
1627         if (!strstr("ide-floppy", drive->driver_req))
1628                 goto failed;
1629         if (!drive->present)
1630                 goto failed;
1631         if (drive->media != ide_floppy)
1632                 goto failed;
1633         if (!idefloppy_identify_device(drive, drive->id)) {
1634                 printk(KERN_ERR "ide-floppy: %s: not supported by this version"
1635                                 " of ide-floppy\n", drive->name);
1636                 goto failed;
1637         }
1638         if (drive->scsi) {
1639                 printk(KERN_INFO "ide-floppy: passing drive %s to ide-scsi"
1640                                 " emulation.\n", drive->name);
1641                 goto failed;
1642         }
1643         floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
1644         if (!floppy) {
1645                 printk(KERN_ERR "ide-floppy: %s: Can't allocate a floppy"
1646                                 " structure\n", drive->name);
1647                 goto failed;
1648         }
1649
1650         g = alloc_disk(1 << PARTN_BITS);
1651         if (!g)
1652                 goto out_free_floppy;
1653
1654         ide_init_disk(g, drive);
1655
1656         ide_proc_register_driver(drive, &idefloppy_driver);
1657
1658         kref_init(&floppy->kref);
1659
1660         floppy->drive = drive;
1661         floppy->driver = &idefloppy_driver;
1662         floppy->disk = g;
1663
1664         g->private_data = &floppy->driver;
1665
1666         drive->driver_data = floppy;
1667
1668         idefloppy_setup(drive, floppy);
1669
1670         g->minors = 1 << PARTN_BITS;
1671         g->driverfs_dev = &drive->gendev;
1672         g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1673         g->fops = &idefloppy_ops;
1674         drive->attach = 1;
1675         add_disk(g);
1676         return 0;
1677
1678 out_free_floppy:
1679         kfree(floppy);
1680 failed:
1681         return -ENODEV;
1682 }
1683
1684 static void __exit idefloppy_exit(void)
1685 {
1686         driver_unregister(&idefloppy_driver.gen_driver);
1687 }
1688
1689 static int __init idefloppy_init(void)
1690 {
1691         printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
1692         return driver_register(&idefloppy_driver.gen_driver);
1693 }
1694
1695 MODULE_ALIAS("ide:*m-floppy*");
1696 module_init(idefloppy_init);
1697 module_exit(idefloppy_exit);
1698 MODULE_LICENSE("GPL");
1699 MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
1700