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