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