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