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