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