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