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