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