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