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