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