]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/ide/ide-floppy.c
ide-{floppy,tape}: remove request stack
[linux-2.6-omap-h63xx.git] / drivers / ide / ide-floppy.c
1 /*
2  * IDE ATAPI floppy driver.
3  *
4  * Copyright (C) 1996-1999  Gadi Oxman <gadio@netvision.net.il>
5  * Copyright (C) 2000-2002  Paul Bristow <paul@paulbristow.net>
6  * Copyright (C) 2005       Bartlomiej Zolnierkiewicz
7  *
8  * This driver supports the following IDE floppy drives:
9  *
10  * LS-120/240 SuperDisk
11  * Iomega Zip 100/250
12  * Iomega PC Card Clik!/PocketZip
13  *
14  * For a historical changelog see
15  * Documentation/ide/ChangeLog.ide-floppy.1996-2002
16  */
17
18 #define DRV_NAME "ide-floppy"
19
20 #define IDEFLOPPY_VERSION "1.00"
21
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/string.h>
25 #include <linux/kernel.h>
26 #include <linux/delay.h>
27 #include <linux/timer.h>
28 #include <linux/mm.h>
29 #include <linux/interrupt.h>
30 #include <linux/major.h>
31 #include <linux/errno.h>
32 #include <linux/genhd.h>
33 #include <linux/slab.h>
34 #include <linux/cdrom.h>
35 #include <linux/ide.h>
36 #include <linux/hdreg.h>
37 #include <linux/bitops.h>
38 #include <linux/mutex.h>
39 #include <linux/scatterlist.h>
40
41 #include <scsi/scsi_ioctl.h>
42
43 #include <asm/byteorder.h>
44 #include <linux/irq.h>
45 #include <linux/uaccess.h>
46 #include <linux/io.h>
47 #include <asm/unaligned.h>
48
49 /* define to see debug info */
50 #define IDEFLOPPY_DEBUG_LOG             0
51
52 /* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
53 #define IDEFLOPPY_DEBUG(fmt, args...)
54
55 #if IDEFLOPPY_DEBUG_LOG
56 #define debug_log(fmt, args...) \
57         printk(KERN_INFO "ide-floppy: " fmt, ## args)
58 #else
59 #define debug_log(fmt, args...) do {} while (0)
60 #endif
61
62
63 /* Some drives require a longer irq timeout. */
64 #define IDEFLOPPY_WAIT_CMD              (5 * WAIT_CMD)
65
66 /*
67  * After each failed packet command we issue a request sense command and retry
68  * the packet command IDEFLOPPY_MAX_PC_RETRIES times.
69  */
70 #define IDEFLOPPY_MAX_PC_RETRIES        3
71
72 /*
73  * With each packet command, we allocate a buffer of IDEFLOPPY_PC_BUFFER_SIZE
74  * bytes.
75  */
76 #define IDEFLOPPY_PC_BUFFER_SIZE        256
77
78 /*
79  * In various places in the driver, we need to allocate storage for packet
80  * commands, which will remain valid while we leave the driver to wait for
81  * an interrupt or a timeout event.
82  */
83 #define IDEFLOPPY_PC_STACK              (10 + IDEFLOPPY_MAX_PC_RETRIES)
84
85 /* format capacities descriptor codes */
86 #define CAPACITY_INVALID        0x00
87 #define CAPACITY_UNFORMATTED    0x01
88 #define CAPACITY_CURRENT        0x02
89 #define CAPACITY_NO_CARTRIDGE   0x03
90
91 /*
92  * Most of our global data which we need to save even as we leave the driver
93  * due to an interrupt or a timer event is stored in a variable of type
94  * idefloppy_floppy_t, defined below.
95  */
96 typedef struct ide_floppy_obj {
97         ide_drive_t     *drive;
98         ide_driver_t    *driver;
99         struct gendisk  *disk;
100         struct kref     kref;
101         unsigned int    openers;        /* protected by BKL for now */
102
103         /* Current packet command */
104         struct ide_atapi_pc *pc;
105         /* Last failed packet command */
106         struct ide_atapi_pc *failed_pc;
107         /* Packet command stack */
108         struct ide_atapi_pc pc_stack[IDEFLOPPY_PC_STACK];
109         /* Next free packet command storage space */
110         int pc_stack_index;
111
112         struct request request_sense_rq;
113
114         /* Last error information */
115         u8 sense_key, asc, ascq;
116         /* delay this long before sending packet command */
117         u8 ticks;
118         int progress_indication;
119
120         /* Device information */
121         /* Current format */
122         int blocks, block_size, bs_factor;
123         /* Last format capacity descriptor */
124         u8 cap_desc[8];
125         /* Copy of the flexible disk page */
126         u8 flexible_disk_page[32];
127         /* Write protect */
128         int wp;
129         /* Supports format progress report */
130         int srfp;
131 } idefloppy_floppy_t;
132
133 #define IDEFLOPPY_TICKS_DELAY   HZ/20   /* default delay for ZIP 100 (50ms) */
134
135 /* Defines for the MODE SENSE command */
136 #define MODE_SENSE_CURRENT              0x00
137 #define MODE_SENSE_CHANGEABLE           0x01
138 #define MODE_SENSE_DEFAULT              0x02
139 #define MODE_SENSE_SAVED                0x03
140
141 /* IOCTLs used in low-level formatting. */
142 #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED        0x4600
143 #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY     0x4601
144 #define IDEFLOPPY_IOCTL_FORMAT_START            0x4602
145 #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS     0x4603
146
147 /* Error code returned in rq->errors to the higher part of the driver. */
148 #define IDEFLOPPY_ERROR_GENERAL         101
149
150 /*
151  * Pages of the SELECT SENSE / MODE SENSE packet commands.
152  * See SFF-8070i spec.
153  */
154 #define IDEFLOPPY_CAPABILITIES_PAGE     0x1b
155 #define IDEFLOPPY_FLEXIBLE_DISK_PAGE    0x05
156
157 static DEFINE_MUTEX(idefloppy_ref_mutex);
158
159 #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
160
161 #define ide_floppy_g(disk) \
162         container_of((disk)->private_data, struct ide_floppy_obj, driver)
163
164 static void idefloppy_cleanup_obj(struct kref *);
165
166 static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
167 {
168         struct ide_floppy_obj *floppy = NULL;
169
170         mutex_lock(&idefloppy_ref_mutex);
171         floppy = ide_floppy_g(disk);
172         if (floppy) {
173                 if (ide_device_get(floppy->drive))
174                         floppy = NULL;
175                 else
176                         kref_get(&floppy->kref);
177         }
178         mutex_unlock(&idefloppy_ref_mutex);
179         return floppy;
180 }
181
182 static void ide_floppy_put(struct ide_floppy_obj *floppy)
183 {
184         ide_drive_t *drive = floppy->drive;
185
186         mutex_lock(&idefloppy_ref_mutex);
187         kref_put(&floppy->kref, idefloppy_cleanup_obj);
188         ide_device_put(drive);
189         mutex_unlock(&idefloppy_ref_mutex);
190 }
191
192 /*
193  * Used to finish servicing a request. For read/write requests, we will call
194  * ide_end_request to pass to the next buffer.
195  */
196 static int idefloppy_end_request(ide_drive_t *drive, int uptodate, int nsecs)
197 {
198         idefloppy_floppy_t *floppy = drive->driver_data;
199         struct request *rq = HWGROUP(drive)->rq;
200         int error;
201
202         debug_log("Reached %s\n", __func__);
203
204         switch (uptodate) {
205         case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
206         case 1: error = 0; break;
207         default: error = uptodate;
208         }
209         if (error)
210                 floppy->failed_pc = NULL;
211         /* Why does this happen? */
212         if (!rq)
213                 return 0;
214         if (!blk_special_request(rq)) {
215                 /* our real local end request function */
216                 ide_end_request(drive, uptodate, nsecs);
217                 return 0;
218         }
219         rq->errors = error;
220         /* fixme: need to move this local also */
221         ide_end_drive_cmd(drive, 0, 0);
222         return 0;
223 }
224
225 static void ide_floppy_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
226                                   unsigned int bcount, int direction)
227 {
228         ide_hwif_t *hwif = drive->hwif;
229         const struct ide_tp_ops *tp_ops = hwif->tp_ops;
230         xfer_func_t *xf = direction ? tp_ops->output_data : tp_ops->input_data;
231         struct scatterlist *sg = pc->sg;
232         char *buf;
233         int count, done = 0;
234
235         while (bcount) {
236                 count = min(sg->length - pc->b_count, bcount);
237                 if (PageHighMem(sg_page(sg))) {
238                         unsigned long flags;
239
240                         local_irq_save(flags);
241                         buf = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
242                         xf(drive, NULL, buf + pc->b_count, count);
243                         kunmap_atomic(buf - sg->offset, KM_IRQ0);
244                         local_irq_restore(flags);
245                 } else {
246                         buf = sg_virt(sg);
247                         xf(drive, NULL, buf + pc->b_count, count);
248                 }
249                 bcount -= count;
250                 pc->b_count += count;
251                 done += count;
252
253                 if (pc->b_count == sg->length) {
254                         if (!--pc->sg_cnt)
255                                 break;
256                         pc->sg = sg = sg_next(sg);
257                         pc->b_count = 0;
258                 }
259         }
260
261         idefloppy_end_request(drive, 1, done >> 9);
262
263         if (bcount) {
264                 printk(KERN_ERR "%s: leftover data in %s, bcount == %d\n",
265                                 drive->name, __func__, bcount);
266                 ide_pad_transfer(drive, direction, bcount);
267         }
268 }
269
270 static void idefloppy_update_buffers(ide_drive_t *drive,
271                                 struct ide_atapi_pc *pc)
272 {
273         struct request *rq = pc->rq;
274         struct bio *bio = rq->bio;
275
276         while ((bio = rq->bio) != NULL)
277                 idefloppy_end_request(drive, 1, 0);
278 }
279
280 /*
281  * Generate a new packet command request in front of the request queue, before
282  * the current request so that it will be processed immediately, on the next
283  * pass through the driver.
284  */
285 static void idefloppy_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
286                 struct request *rq)
287 {
288         struct ide_floppy_obj *floppy = drive->driver_data;
289
290         blk_rq_init(NULL, rq);
291         rq->buffer = (char *) pc;
292         rq->cmd_type = REQ_TYPE_SPECIAL;
293         rq->cmd_flags |= REQ_PREEMPT;
294         rq->rq_disk = floppy->disk;
295         memcpy(rq->cmd, pc->c, 12);
296         ide_do_drive_cmd(drive, rq);
297 }
298
299 static struct ide_atapi_pc *idefloppy_next_pc_storage(ide_drive_t *drive)
300 {
301         idefloppy_floppy_t *floppy = drive->driver_data;
302
303         if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
304                 floppy->pc_stack_index = 0;
305         return (&floppy->pc_stack[floppy->pc_stack_index++]);
306 }
307
308 static void ide_floppy_callback(ide_drive_t *drive)
309 {
310         idefloppy_floppy_t *floppy = drive->driver_data;
311         struct ide_atapi_pc *pc = floppy->pc;
312         int uptodate = pc->error ? 0 : 1;
313
314         debug_log("Reached %s\n", __func__);
315
316         if (floppy->failed_pc == pc)
317                 floppy->failed_pc = NULL;
318
319         if (pc->c[0] == GPCMD_READ_10 || pc->c[0] == GPCMD_WRITE_10 ||
320             (pc->rq && blk_pc_request(pc->rq)))
321                 uptodate = 1; /* FIXME */
322         else if (pc->c[0] == GPCMD_REQUEST_SENSE) {
323                 u8 *buf = floppy->pc->buf;
324
325                 if (!pc->error) {
326                         floppy->sense_key = buf[2] & 0x0F;
327                         floppy->asc = buf[12];
328                         floppy->ascq = buf[13];
329                         floppy->progress_indication = buf[15] & 0x80 ?
330                                 (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
331
332                         if (floppy->failed_pc)
333                                 debug_log("pc = %x, ", floppy->failed_pc->c[0]);
334
335                         debug_log("sense key = %x, asc = %x, ascq = %x\n",
336                                   floppy->sense_key, floppy->asc, floppy->ascq);
337                 } else
338                         printk(KERN_ERR "Error in REQUEST SENSE itself - "
339                                         "Aborting request!\n");
340         }
341
342         idefloppy_end_request(drive, uptodate, 0);
343 }
344
345 static void idefloppy_init_pc(struct ide_atapi_pc *pc)
346 {
347         memset(pc, 0, sizeof(*pc));
348         pc->buf = pc->pc_buf;
349         pc->buf_size = IDEFLOPPY_PC_BUFFER_SIZE;
350 }
351
352 static void idefloppy_create_request_sense_cmd(struct ide_atapi_pc *pc)
353 {
354         idefloppy_init_pc(pc);
355         pc->c[0] = GPCMD_REQUEST_SENSE;
356         pc->c[4] = 255;
357         pc->req_xfer = 18;
358 }
359
360 /*
361  * Called when an error was detected during the last packet command. We queue a
362  * request sense packet command in the head of the request list.
363  */
364 static void idefloppy_retry_pc(ide_drive_t *drive)
365 {
366         struct ide_floppy_obj *floppy = drive->driver_data;
367         struct request *rq = &floppy->request_sense_rq;
368         struct ide_atapi_pc *pc;
369
370         (void)ide_read_error(drive);
371         pc = idefloppy_next_pc_storage(drive);
372         idefloppy_create_request_sense_cmd(pc);
373         idefloppy_queue_pc_head(drive, pc, rq);
374 }
375
376 /* The usual interrupt handler called during a packet command. */
377 static ide_startstop_t idefloppy_pc_intr(ide_drive_t *drive)
378 {
379         idefloppy_floppy_t *floppy = drive->driver_data;
380
381         return ide_pc_intr(drive, floppy->pc, idefloppy_pc_intr,
382                            IDEFLOPPY_WAIT_CMD, NULL, idefloppy_update_buffers,
383                            idefloppy_retry_pc, NULL, ide_floppy_io_buffers);
384 }
385
386 /*
387  * What we have here is a classic case of a top half / bottom half interrupt
388  * service routine. In interrupt mode, the device sends an interrupt to signal
389  * that it is ready to receive a packet. However, we need to delay about 2-3
390  * ticks before issuing the packet or we gets in trouble.
391  */
392 static int idefloppy_transfer_pc(ide_drive_t *drive)
393 {
394         idefloppy_floppy_t *floppy = drive->driver_data;
395
396         /* Send the actual packet */
397         drive->hwif->tp_ops->output_data(drive, NULL, floppy->pc->c, 12);
398
399         /* Timeout for the packet command */
400         return IDEFLOPPY_WAIT_CMD;
401 }
402
403
404 /*
405  * Called as an interrupt (or directly). When the device says it's ready for a
406  * packet, we schedule the packet transfer to occur about 2-3 ticks later in
407  * transfer_pc.
408  */
409 static ide_startstop_t idefloppy_start_pc_transfer(ide_drive_t *drive)
410 {
411         idefloppy_floppy_t *floppy = drive->driver_data;
412         struct ide_atapi_pc *pc = floppy->pc;
413         ide_expiry_t *expiry;
414         unsigned int timeout;
415
416         /*
417          * The following delay solves a problem with ATAPI Zip 100 drives
418          * where the Busy flag was apparently being deasserted before the
419          * unit was ready to receive data. This was happening on a
420          * 1200 MHz Athlon system. 10/26/01 25msec is too short,
421          * 40 and 50msec work well. idefloppy_pc_intr will not be actually
422          * used until after the packet is moved in about 50 msec.
423          */
424         if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
425                 timeout = floppy->ticks;
426                 expiry = &idefloppy_transfer_pc;
427         } else {
428                 timeout = IDEFLOPPY_WAIT_CMD;
429                 expiry = NULL;
430         }
431
432         return ide_transfer_pc(drive, pc, idefloppy_pc_intr, timeout, expiry);
433 }
434
435 static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
436                                     struct ide_atapi_pc *pc)
437 {
438         /* supress error messages resulting from Medium not present */
439         if (floppy->sense_key == 0x02 &&
440             floppy->asc       == 0x3a &&
441             floppy->ascq      == 0x00)
442                 return;
443
444         printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
445                         "asc = %2x, ascq = %2x\n",
446                         floppy->drive->name, pc->c[0], floppy->sense_key,
447                         floppy->asc, floppy->ascq);
448
449 }
450
451 static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive,
452                 struct ide_atapi_pc *pc)
453 {
454         idefloppy_floppy_t *floppy = drive->driver_data;
455
456         if (floppy->failed_pc == NULL &&
457             pc->c[0] != GPCMD_REQUEST_SENSE)
458                 floppy->failed_pc = pc;
459         /* Set the current packet command */
460         floppy->pc = pc;
461
462         if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
463                 if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
464                         ide_floppy_report_error(floppy, pc);
465                 /* Giving up */
466                 pc->error = IDEFLOPPY_ERROR_GENERAL;
467
468                 floppy->failed_pc = NULL;
469                 drive->pc_callback(drive);
470                 return ide_stopped;
471         }
472
473         debug_log("Retry number - %d\n", pc->retries);
474
475         pc->retries++;
476
477         return ide_issue_pc(drive, pc, idefloppy_start_pc_transfer,
478                             IDEFLOPPY_WAIT_CMD, NULL);
479 }
480
481 static void idefloppy_create_prevent_cmd(struct ide_atapi_pc *pc, int prevent)
482 {
483         debug_log("creating prevent removal command, prevent = %d\n", prevent);
484
485         idefloppy_init_pc(pc);
486         pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
487         pc->c[4] = prevent;
488 }
489
490 static void idefloppy_create_read_capacity_cmd(struct ide_atapi_pc *pc)
491 {
492         idefloppy_init_pc(pc);
493         pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
494         pc->c[7] = 255;
495         pc->c[8] = 255;
496         pc->req_xfer = 255;
497 }
498
499 static void idefloppy_create_format_unit_cmd(struct ide_atapi_pc *pc, int b,
500                 int l, int flags)
501 {
502         idefloppy_init_pc(pc);
503         pc->c[0] = GPCMD_FORMAT_UNIT;
504         pc->c[1] = 0x17;
505
506         memset(pc->buf, 0, 12);
507         pc->buf[1] = 0xA2;
508         /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
509
510         if (flags & 1)                          /* Verify bit on... */
511                 pc->buf[1] ^= 0x20;             /* ... turn off DCRT bit */
512         pc->buf[3] = 8;
513
514         put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buf[4]));
515         put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buf[8]));
516         pc->buf_size = 12;
517         pc->flags |= PC_FLAG_WRITING;
518 }
519
520 /* A mode sense command is used to "sense" floppy parameters. */
521 static void idefloppy_create_mode_sense_cmd(struct ide_atapi_pc *pc,
522                 u8 page_code, u8 type)
523 {
524         u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
525
526         idefloppy_init_pc(pc);
527         pc->c[0] = GPCMD_MODE_SENSE_10;
528         pc->c[1] = 0;
529         pc->c[2] = page_code + (type << 6);
530
531         switch (page_code) {
532         case IDEFLOPPY_CAPABILITIES_PAGE:
533                 length += 12;
534                 break;
535         case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
536                 length += 32;
537                 break;
538         default:
539                 printk(KERN_ERR "ide-floppy: unsupported page code "
540                                 "in create_mode_sense_cmd\n");
541         }
542         put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
543         pc->req_xfer = length;
544 }
545
546 static void idefloppy_create_start_stop_cmd(struct ide_atapi_pc *pc, int start)
547 {
548         idefloppy_init_pc(pc);
549         pc->c[0] = GPCMD_START_STOP_UNIT;
550         pc->c[4] = start;
551 }
552
553 static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
554                                     struct ide_atapi_pc *pc, struct request *rq,
555                                     unsigned long sector)
556 {
557         int block = sector / floppy->bs_factor;
558         int blocks = rq->nr_sectors / floppy->bs_factor;
559         int cmd = rq_data_dir(rq);
560
561         debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
562                 block, blocks);
563
564         idefloppy_init_pc(pc);
565         pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
566         put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
567         put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
568
569         memcpy(rq->cmd, pc->c, 12);
570
571         pc->rq = rq;
572         pc->b_count = 0;
573         if (rq->cmd_flags & REQ_RW)
574                 pc->flags |= PC_FLAG_WRITING;
575         pc->buf = NULL;
576         pc->req_xfer = pc->buf_size = blocks * floppy->block_size;
577         pc->flags |= PC_FLAG_DMA_OK;
578 }
579
580 static void idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy,
581                 struct ide_atapi_pc *pc, struct request *rq)
582 {
583         idefloppy_init_pc(pc);
584         memcpy(pc->c, rq->cmd, sizeof(pc->c));
585         pc->rq = rq;
586         pc->b_count = 0;
587         if (rq->data_len && rq_data_dir(rq) == WRITE)
588                 pc->flags |= PC_FLAG_WRITING;
589         pc->buf = rq->data;
590         if (rq->bio)
591                 pc->flags |= PC_FLAG_DMA_OK;
592         /*
593          * possibly problematic, doesn't look like ide-floppy correctly
594          * handled scattered requests if dma fails...
595          */
596         pc->req_xfer = pc->buf_size = rq->data_len;
597 }
598
599 static ide_startstop_t idefloppy_do_request(ide_drive_t *drive,
600                 struct request *rq, sector_t block_s)
601 {
602         idefloppy_floppy_t *floppy = drive->driver_data;
603         ide_hwif_t *hwif = drive->hwif;
604         struct ide_atapi_pc *pc;
605         unsigned long block = (unsigned long)block_s;
606
607         debug_log("%s: dev: %s, cmd: 0x%x, cmd_type: %x, errors: %d\n",
608                   __func__, rq->rq_disk ? rq->rq_disk->disk_name : "?",
609                   rq->cmd[0], rq->cmd_type, rq->errors);
610
611         debug_log("%s: sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",
612                   __func__, (long)rq->sector, rq->nr_sectors,
613                   rq->current_nr_sectors);
614
615         if (rq->errors >= ERROR_MAX) {
616                 if (floppy->failed_pc)
617                         ide_floppy_report_error(floppy, floppy->failed_pc);
618                 else
619                         printk(KERN_ERR "ide-floppy: %s: I/O error\n",
620                                 drive->name);
621                 idefloppy_end_request(drive, 0, 0);
622                 return ide_stopped;
623         }
624         if (blk_fs_request(rq)) {
625                 if (((long)rq->sector % floppy->bs_factor) ||
626                     (rq->nr_sectors % floppy->bs_factor)) {
627                         printk(KERN_ERR "%s: unsupported r/w request size\n",
628                                         drive->name);
629                         idefloppy_end_request(drive, 0, 0);
630                         return ide_stopped;
631                 }
632                 pc = idefloppy_next_pc_storage(drive);
633                 idefloppy_create_rw_cmd(floppy, pc, rq, block);
634         } else if (blk_special_request(rq)) {
635                 pc = (struct ide_atapi_pc *) rq->buffer;
636         } else if (blk_pc_request(rq)) {
637                 pc = idefloppy_next_pc_storage(drive);
638                 idefloppy_blockpc_cmd(floppy, pc, rq);
639         } else {
640                 blk_dump_rq_flags(rq,
641                         "ide-floppy: unsupported command in queue");
642                 idefloppy_end_request(drive, 0, 0);
643                 return ide_stopped;
644         }
645
646         ide_init_sg_cmd(drive, rq);
647         ide_map_sg(drive, rq);
648
649         pc->sg = hwif->sg_table;
650         pc->sg_cnt = hwif->sg_nents;
651
652         pc->rq = rq;
653
654         return idefloppy_issue_pc(drive, pc);
655 }
656
657 /*
658  * Add a special packet command request to the tail of the request queue,
659  * and wait for it to be serviced.
660  */
661 static int idefloppy_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
662 {
663         struct ide_floppy_obj *floppy = drive->driver_data;
664         struct request *rq;
665         int error;
666
667         rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
668         rq->buffer = (char *) pc;
669         rq->cmd_type = REQ_TYPE_SPECIAL;
670         memcpy(rq->cmd, pc->c, 12);
671         error = blk_execute_rq(drive->queue, floppy->disk, rq, 0);
672         blk_put_request(rq);
673
674         return error;
675 }
676
677 /*
678  * Look at the flexible disk page parameters. We ignore the CHS capacity
679  * parameters and use the LBA parameters instead.
680  */
681 static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
682 {
683         idefloppy_floppy_t *floppy = drive->driver_data;
684         struct ide_atapi_pc pc;
685         u8 *page;
686         int capacity, lba_capacity;
687         u16 transfer_rate, sector_size, cyls, rpm;
688         u8 heads, sectors;
689
690         idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE,
691                                         MODE_SENSE_CURRENT);
692
693         if (idefloppy_queue_pc_tail(drive, &pc)) {
694                 printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
695                                 " parameters\n");
696                 return 1;
697         }
698         floppy->wp = !!(pc.buf[3] & 0x80);
699         set_disk_ro(floppy->disk, floppy->wp);
700         page = &pc.buf[8];
701
702         transfer_rate = be16_to_cpup((__be16 *)&pc.buf[8 + 2]);
703         sector_size   = be16_to_cpup((__be16 *)&pc.buf[8 + 6]);
704         cyls          = be16_to_cpup((__be16 *)&pc.buf[8 + 8]);
705         rpm           = be16_to_cpup((__be16 *)&pc.buf[8 + 28]);
706         heads         = pc.buf[8 + 4];
707         sectors       = pc.buf[8 + 5];
708
709         capacity = cyls * heads * sectors * sector_size;
710
711         if (memcmp(page, &floppy->flexible_disk_page, 32))
712                 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
713                                 "%d sector size, %d rpm\n",
714                                 drive->name, capacity / 1024, cyls, heads,
715                                 sectors, transfer_rate / 8, sector_size, rpm);
716
717         memcpy(&floppy->flexible_disk_page, page, 32);
718         drive->bios_cyl = cyls;
719         drive->bios_head = heads;
720         drive->bios_sect = sectors;
721         lba_capacity = floppy->blocks * floppy->block_size;
722
723         if (capacity < lba_capacity) {
724                 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
725                         "bytes, but the drive only handles %d\n",
726                         drive->name, lba_capacity, capacity);
727                 floppy->blocks = floppy->block_size ?
728                         capacity / floppy->block_size : 0;
729         }
730         return 0;
731 }
732
733 static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
734 {
735         idefloppy_floppy_t *floppy = drive->driver_data;
736         struct ide_atapi_pc pc;
737
738         floppy->srfp = 0;
739         idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
740                                                  MODE_SENSE_CURRENT);
741
742         pc.flags |= PC_FLAG_SUPPRESS_ERROR;
743         if (idefloppy_queue_pc_tail(drive, &pc))
744                 return 1;
745
746         floppy->srfp = pc.buf[8 + 2] & 0x40;
747         return 0;
748 }
749
750 /*
751  * Determine if a media is present in the floppy drive, and if so, its LBA
752  * capacity.
753  */
754 static int ide_floppy_get_capacity(ide_drive_t *drive)
755 {
756         idefloppy_floppy_t *floppy = drive->driver_data;
757         struct ide_atapi_pc pc;
758         u8 *cap_desc;
759         u8 header_len, desc_cnt;
760         int i, rc = 1, blocks, length;
761
762         drive->bios_cyl = 0;
763         drive->bios_head = drive->bios_sect = 0;
764         floppy->blocks = 0;
765         floppy->bs_factor = 1;
766         set_capacity(floppy->disk, 0);
767
768         idefloppy_create_read_capacity_cmd(&pc);
769         if (idefloppy_queue_pc_tail(drive, &pc)) {
770                 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
771                 return 1;
772         }
773         header_len = pc.buf[3];
774         cap_desc = &pc.buf[4];
775         desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
776
777         for (i = 0; i < desc_cnt; i++) {
778                 unsigned int desc_start = 4 + i*8;
779
780                 blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
781                 length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
782
783                 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
784                                 i, blocks * length / 1024, blocks, length);
785
786                 if (i)
787                         continue;
788                 /*
789                  * the code below is valid only for the 1st descriptor, ie i=0
790                  */
791
792                 switch (pc.buf[desc_start + 4] & 0x03) {
793                 /* Clik! drive returns this instead of CAPACITY_CURRENT */
794                 case CAPACITY_UNFORMATTED:
795                         if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
796                                 /*
797                                  * If it is not a clik drive, break out
798                                  * (maintains previous driver behaviour)
799                                  */
800                                 break;
801                 case CAPACITY_CURRENT:
802                         /* Normal Zip/LS-120 disks */
803                         if (memcmp(cap_desc, &floppy->cap_desc, 8))
804                                 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
805                                         "sector size\n", drive->name,
806                                         blocks * length / 1024, blocks, length);
807                         memcpy(&floppy->cap_desc, cap_desc, 8);
808
809                         if (!length || length % 512) {
810                                 printk(KERN_NOTICE "%s: %d bytes block size "
811                                         "not supported\n", drive->name, length);
812                         } else {
813                                 floppy->blocks = blocks;
814                                 floppy->block_size = length;
815                                 floppy->bs_factor = length / 512;
816                                 if (floppy->bs_factor != 1)
817                                         printk(KERN_NOTICE "%s: warning: non "
818                                                 "512 bytes block size not "
819                                                 "fully supported\n",
820                                                 drive->name);
821                                 rc = 0;
822                         }
823                         break;
824                 case CAPACITY_NO_CARTRIDGE:
825                         /*
826                          * This is a KERN_ERR so it appears on screen
827                          * for the user to see
828                          */
829                         printk(KERN_ERR "%s: No disk in drive\n", drive->name);
830                         break;
831                 case CAPACITY_INVALID:
832                         printk(KERN_ERR "%s: Invalid capacity for disk "
833                                 "in drive\n", drive->name);
834                         break;
835                 }
836                 debug_log("Descriptor 0 Code: %d\n",
837                           pc.buf[desc_start + 4] & 0x03);
838         }
839
840         /* Clik! disk does not support get_flexible_disk_page */
841         if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
842                 (void) ide_floppy_get_flexible_disk_page(drive);
843
844         set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
845         return rc;
846 }
847
848 /*
849  * Obtain the list of formattable capacities.
850  * Very similar to ide_floppy_get_capacity, except that we push the capacity
851  * descriptors to userland, instead of our own structures.
852  *
853  * Userland gives us the following structure:
854  *
855  * struct idefloppy_format_capacities {
856  *      int nformats;
857  *      struct {
858  *              int nblocks;
859  *              int blocksize;
860  *      } formats[];
861  * };
862  *
863  * userland initializes nformats to the number of allocated formats[] records.
864  * On exit we set nformats to the number of records we've actually initialized.
865  */
866
867 static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
868 {
869         struct ide_atapi_pc pc;
870         u8 header_len, desc_cnt;
871         int i, blocks, length, u_array_size, u_index;
872         int __user *argp;
873
874         if (get_user(u_array_size, arg))
875                 return -EFAULT;
876
877         if (u_array_size <= 0)
878                 return -EINVAL;
879
880         idefloppy_create_read_capacity_cmd(&pc);
881         if (idefloppy_queue_pc_tail(drive, &pc)) {
882                 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
883                 return -EIO;
884         }
885
886         header_len = pc.buf[3];
887         desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
888
889         u_index = 0;
890         argp = arg + 1;
891
892         /*
893          * We always skip the first capacity descriptor.  That's the current
894          * capacity.  We are interested in the remaining descriptors, the
895          * formattable capacities.
896          */
897         for (i = 1; i < desc_cnt; i++) {
898                 unsigned int desc_start = 4 + i*8;
899
900                 if (u_index >= u_array_size)
901                         break;  /* User-supplied buffer too small */
902
903                 blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
904                 length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
905
906                 if (put_user(blocks, argp))
907                         return -EFAULT;
908
909                 ++argp;
910
911                 if (put_user(length, argp))
912                         return -EFAULT;
913
914                 ++argp;
915
916                 ++u_index;
917         }
918
919         if (put_user(u_index, arg))
920                 return -EFAULT;
921
922         return 0;
923 }
924
925 /*
926  * Get ATAPI_FORMAT_UNIT progress indication.
927  *
928  * Userland gives a pointer to an int.  The int is set to a progress
929  * indicator 0-65536, with 65536=100%.
930  *
931  * If the drive does not support format progress indication, we just check
932  * the dsc bit, and return either 0 or 65536.
933  */
934
935 static int ide_floppy_get_format_progress(ide_drive_t *drive, int __user *arg)
936 {
937         idefloppy_floppy_t *floppy = drive->driver_data;
938         struct ide_atapi_pc pc;
939         int progress_indication = 0x10000;
940
941         if (floppy->srfp) {
942                 idefloppy_create_request_sense_cmd(&pc);
943                 if (idefloppy_queue_pc_tail(drive, &pc))
944                         return -EIO;
945
946                 if (floppy->sense_key == 2 &&
947                     floppy->asc == 4 &&
948                     floppy->ascq == 4)
949                         progress_indication = floppy->progress_indication;
950
951                 /* Else assume format_unit has finished, and we're at 0x10000 */
952         } else {
953                 ide_hwif_t *hwif = drive->hwif;
954                 unsigned long flags;
955                 u8 stat;
956
957                 local_irq_save(flags);
958                 stat = hwif->tp_ops->read_status(hwif);
959                 local_irq_restore(flags);
960
961                 progress_indication = ((stat & ATA_DSC) == 0) ? 0 : 0x10000;
962         }
963
964         if (put_user(progress_indication, arg))
965                 return -EFAULT;
966
967         return 0;
968 }
969
970 static sector_t idefloppy_capacity(ide_drive_t *drive)
971 {
972         idefloppy_floppy_t *floppy = drive->driver_data;
973         unsigned long capacity = floppy->blocks * floppy->bs_factor;
974
975         return capacity;
976 }
977
978 #ifdef CONFIG_IDE_PROC_FS
979 ide_devset_rw(bios_cyl,  0, 1023, bios_cyl);
980 ide_devset_rw(bios_head, 0,  255, bios_head);
981 ide_devset_rw(bios_sect, 0,   63, bios_sect);
982
983 static int get_ticks(ide_drive_t *drive)
984 {
985         idefloppy_floppy_t *floppy = drive->driver_data;
986         return floppy->ticks;
987 }
988
989 static int set_ticks(ide_drive_t *drive, int arg)
990 {
991         idefloppy_floppy_t *floppy = drive->driver_data;
992         floppy->ticks = arg;
993         return 0;
994 }
995
996 IDE_DEVSET(ticks, S_RW, 0, 255, get_ticks, set_ticks);
997
998 static const struct ide_devset *idefloppy_settings[] = {
999         &ide_devset_bios_cyl,
1000         &ide_devset_bios_head,
1001         &ide_devset_bios_sect,
1002         &ide_devset_ticks,
1003         NULL
1004 };
1005 #endif
1006
1007 static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
1008 {
1009         u16 *id = drive->id;
1010         u8 gcw[2];
1011
1012         *((u16 *)&gcw) = id[ATA_ID_CONFIG];
1013         floppy->pc = floppy->pc_stack;
1014         drive->pc_callback = ide_floppy_callback;
1015
1016         if (((gcw[0] & 0x60) >> 5) == 1)
1017                 drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
1018         /*
1019          * We used to check revisions here. At this point however I'm giving up.
1020          * Just assume they are all broken, its easier.
1021          *
1022          * The actual reason for the workarounds was likely a driver bug after
1023          * all rather than a firmware bug, and the workaround below used to hide
1024          * it. It should be fixed as of version 1.9, but to be on the safe side
1025          * we'll leave the limitation below for the 2.2.x tree.
1026          */
1027         if (!strncmp((char *)&id[ATA_ID_PROD], "IOMEGA ZIP 100 ATAPI", 20)) {
1028                 drive->atapi_flags |= IDE_AFLAG_ZIP_DRIVE;
1029                 /* This value will be visible in the /proc/ide/hdx/settings */
1030                 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1031                 blk_queue_max_sectors(drive->queue, 64);
1032         }
1033
1034         /*
1035          * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
1036          * nasty clicking noises without it, so please don't remove this.
1037          */
1038         if (strncmp((char *)&id[ATA_ID_PROD], "IOMEGA Clik!", 11) == 0) {
1039                 blk_queue_max_sectors(drive->queue, 64);
1040                 drive->atapi_flags |= IDE_AFLAG_CLIK_DRIVE;
1041         }
1042
1043         (void) ide_floppy_get_capacity(drive);
1044
1045         ide_proc_register_driver(drive, floppy->driver);
1046 }
1047
1048 static void ide_floppy_remove(ide_drive_t *drive)
1049 {
1050         idefloppy_floppy_t *floppy = drive->driver_data;
1051         struct gendisk *g = floppy->disk;
1052
1053         ide_proc_unregister_driver(drive, floppy->driver);
1054
1055         del_gendisk(g);
1056
1057         ide_floppy_put(floppy);
1058 }
1059
1060 static void idefloppy_cleanup_obj(struct kref *kref)
1061 {
1062         struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1063         ide_drive_t *drive = floppy->drive;
1064         struct gendisk *g = floppy->disk;
1065
1066         drive->driver_data = NULL;
1067         g->private_data = NULL;
1068         put_disk(g);
1069         kfree(floppy);
1070 }
1071
1072 #ifdef CONFIG_IDE_PROC_FS
1073 static int proc_idefloppy_read_capacity(char *page, char **start, off_t off,
1074                 int count, int *eof, void *data)
1075 {
1076         ide_drive_t*drive = (ide_drive_t *)data;
1077         int len;
1078
1079         len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive));
1080         PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
1081 }
1082
1083 static ide_proc_entry_t idefloppy_proc[] = {
1084         { "capacity",   S_IFREG|S_IRUGO, proc_idefloppy_read_capacity,  NULL },
1085         { "geometry",   S_IFREG|S_IRUGO, proc_ide_read_geometry,        NULL },
1086         { NULL, 0, NULL, NULL }
1087 };
1088 #endif  /* CONFIG_IDE_PROC_FS */
1089
1090 static int ide_floppy_probe(ide_drive_t *);
1091
1092 static ide_driver_t idefloppy_driver = {
1093         .gen_driver = {
1094                 .owner          = THIS_MODULE,
1095                 .name           = "ide-floppy",
1096                 .bus            = &ide_bus_type,
1097         },
1098         .probe                  = ide_floppy_probe,
1099         .remove                 = ide_floppy_remove,
1100         .version                = IDEFLOPPY_VERSION,
1101         .media                  = ide_floppy,
1102         .do_request             = idefloppy_do_request,
1103         .end_request            = idefloppy_end_request,
1104         .error                  = __ide_error,
1105 #ifdef CONFIG_IDE_PROC_FS
1106         .proc                   = idefloppy_proc,
1107         .settings               = idefloppy_settings,
1108 #endif
1109 };
1110
1111 static int idefloppy_open(struct inode *inode, struct file *filp)
1112 {
1113         struct gendisk *disk = inode->i_bdev->bd_disk;
1114         struct ide_floppy_obj *floppy;
1115         ide_drive_t *drive;
1116         struct ide_atapi_pc pc;
1117         int ret = 0;
1118
1119         debug_log("Reached %s\n", __func__);
1120
1121         floppy = ide_floppy_get(disk);
1122         if (!floppy)
1123                 return -ENXIO;
1124
1125         drive = floppy->drive;
1126
1127         floppy->openers++;
1128
1129         if (floppy->openers == 1) {
1130                 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1131                 /* Just in case */
1132
1133                 idefloppy_init_pc(&pc);
1134                 pc.c[0] = GPCMD_TEST_UNIT_READY;
1135
1136                 if (idefloppy_queue_pc_tail(drive, &pc)) {
1137                         idefloppy_create_start_stop_cmd(&pc, 1);
1138                         (void) idefloppy_queue_pc_tail(drive, &pc);
1139                 }
1140
1141                 if (ide_floppy_get_capacity(drive)
1142                    && (filp->f_flags & O_NDELAY) == 0
1143                     /*
1144                      * Allow O_NDELAY to open a drive without a disk, or with an
1145                      * unreadable disk, so that we can get the format capacity
1146                      * of the drive or begin the format - Sam
1147                      */
1148                     ) {
1149                         ret = -EIO;
1150                         goto out_put_floppy;
1151                 }
1152
1153                 if (floppy->wp && (filp->f_mode & 2)) {
1154                         ret = -EROFS;
1155                         goto out_put_floppy;
1156                 }
1157                 drive->atapi_flags |= IDE_AFLAG_MEDIA_CHANGED;
1158                 /* IOMEGA Clik! drives do not support lock/unlock commands */
1159                 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
1160                         idefloppy_create_prevent_cmd(&pc, 1);
1161                         (void) idefloppy_queue_pc_tail(drive, &pc);
1162                 }
1163                 check_disk_change(inode->i_bdev);
1164         } else if (drive->atapi_flags & IDE_AFLAG_FORMAT_IN_PROGRESS) {
1165                 ret = -EBUSY;
1166                 goto out_put_floppy;
1167         }
1168         return 0;
1169
1170 out_put_floppy:
1171         floppy->openers--;
1172         ide_floppy_put(floppy);
1173         return ret;
1174 }
1175
1176 static int idefloppy_release(struct inode *inode, struct file *filp)
1177 {
1178         struct gendisk *disk = inode->i_bdev->bd_disk;
1179         struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1180         ide_drive_t *drive = floppy->drive;
1181         struct ide_atapi_pc pc;
1182
1183         debug_log("Reached %s\n", __func__);
1184
1185         if (floppy->openers == 1) {
1186                 /* IOMEGA Clik! drives do not support lock/unlock commands */
1187                 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
1188                         idefloppy_create_prevent_cmd(&pc, 0);
1189                         (void) idefloppy_queue_pc_tail(drive, &pc);
1190                 }
1191
1192                 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1193         }
1194
1195         floppy->openers--;
1196
1197         ide_floppy_put(floppy);
1198
1199         return 0;
1200 }
1201
1202 static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1203 {
1204         struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1205         ide_drive_t *drive = floppy->drive;
1206
1207         geo->heads = drive->bios_head;
1208         geo->sectors = drive->bios_sect;
1209         geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1210         return 0;
1211 }
1212
1213 static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
1214                                unsigned long arg, unsigned int cmd)
1215 {
1216         idefloppy_floppy_t *floppy = drive->driver_data;
1217
1218         if (floppy->openers > 1)
1219                 return -EBUSY;
1220
1221         /* The IOMEGA Clik! Drive doesn't support this command -
1222          * no room for an eject mechanism */
1223         if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
1224                 int prevent = arg ? 1 : 0;
1225
1226                 if (cmd == CDROMEJECT)
1227                         prevent = 0;
1228
1229                 idefloppy_create_prevent_cmd(pc, prevent);
1230                 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1231         }
1232
1233         if (cmd == CDROMEJECT) {
1234                 idefloppy_create_start_stop_cmd(pc, 2);
1235                 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1236         }
1237
1238         return 0;
1239 }
1240
1241 static int ide_floppy_format_unit(ide_drive_t *drive, int __user *arg)
1242 {
1243         idefloppy_floppy_t *floppy = drive->driver_data;
1244         struct ide_atapi_pc pc;
1245         int blocks, length, flags, err = 0;
1246
1247         if (floppy->openers > 1) {
1248                 /* Don't format if someone is using the disk */
1249                 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1250                 return -EBUSY;
1251         }
1252
1253         drive->atapi_flags |= IDE_AFLAG_FORMAT_IN_PROGRESS;
1254
1255         /*
1256          * Send ATAPI_FORMAT_UNIT to the drive.
1257          *
1258          * Userland gives us the following structure:
1259          *
1260          * struct idefloppy_format_command {
1261          *        int nblocks;
1262          *        int blocksize;
1263          *        int flags;
1264          *        } ;
1265          *
1266          * flags is a bitmask, currently, the only defined flag is:
1267          *
1268          *        0x01 - verify media after format.
1269          */
1270         if (get_user(blocks, arg) ||
1271                         get_user(length, arg+1) ||
1272                         get_user(flags, arg+2)) {
1273                 err = -EFAULT;
1274                 goto out;
1275         }
1276
1277         (void) idefloppy_get_sfrp_bit(drive);
1278         idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1279
1280         if (idefloppy_queue_pc_tail(drive, &pc))
1281                 err = -EIO;
1282
1283 out:
1284         if (err)
1285                 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1286         return err;
1287 }
1288
1289 static int ide_floppy_format_ioctl(ide_drive_t *drive, struct file *file,
1290                                    unsigned int cmd, void __user *argp)
1291 {
1292         switch (cmd) {
1293         case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1294                 return 0;
1295         case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
1296                 return ide_floppy_get_format_capacities(drive, argp);
1297         case IDEFLOPPY_IOCTL_FORMAT_START:
1298                 if (!(file->f_mode & 2))
1299                         return -EPERM;
1300                 return ide_floppy_format_unit(drive, (int __user *)argp);
1301         case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1302                 return ide_floppy_get_format_progress(drive, argp);
1303         default:
1304                 return -ENOTTY;
1305         }
1306 }
1307
1308 static int idefloppy_ioctl(struct inode *inode, struct file *file,
1309                         unsigned int cmd, unsigned long arg)
1310 {
1311         struct block_device *bdev = inode->i_bdev;
1312         struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1313         ide_drive_t *drive = floppy->drive;
1314         struct ide_atapi_pc pc;
1315         void __user *argp = (void __user *)arg;
1316         int err;
1317
1318         if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR)
1319                 return ide_floppy_lockdoor(drive, &pc, arg, cmd);
1320
1321         err = ide_floppy_format_ioctl(drive, file, cmd, argp);
1322         if (err != -ENOTTY)
1323                 return err;
1324
1325         /*
1326          * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1327          * and CDROM_SEND_PACKET (legacy) ioctls
1328          */
1329         if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1330                 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1331                                         bdev->bd_disk, cmd, argp);
1332
1333         if (err == -ENOTTY)
1334                 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1335
1336         return err;
1337 }
1338
1339 static int idefloppy_media_changed(struct gendisk *disk)
1340 {
1341         struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1342         ide_drive_t *drive = floppy->drive;
1343         int ret;
1344
1345         /* do not scan partitions twice if this is a removable device */
1346         if (drive->attach) {
1347                 drive->attach = 0;
1348                 return 0;
1349         }
1350         ret = !!(drive->atapi_flags & IDE_AFLAG_MEDIA_CHANGED);
1351         drive->atapi_flags &= ~IDE_AFLAG_MEDIA_CHANGED;
1352         return ret;
1353 }
1354
1355 static int idefloppy_revalidate_disk(struct gendisk *disk)
1356 {
1357         struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1358         set_capacity(disk, idefloppy_capacity(floppy->drive));
1359         return 0;
1360 }
1361
1362 static struct block_device_operations idefloppy_ops = {
1363         .owner                  = THIS_MODULE,
1364         .open                   = idefloppy_open,
1365         .release                = idefloppy_release,
1366         .ioctl                  = idefloppy_ioctl,
1367         .getgeo                 = idefloppy_getgeo,
1368         .media_changed          = idefloppy_media_changed,
1369         .revalidate_disk        = idefloppy_revalidate_disk
1370 };
1371
1372 static int ide_floppy_probe(ide_drive_t *drive)
1373 {
1374         idefloppy_floppy_t *floppy;
1375         struct gendisk *g;
1376
1377         if (!strstr("ide-floppy", drive->driver_req))
1378                 goto failed;
1379
1380         if (drive->media != ide_floppy)
1381                 goto failed;
1382
1383         if (!ide_check_atapi_device(drive, DRV_NAME)) {
1384                 printk(KERN_ERR "ide-floppy: %s: not supported by this version"
1385                                 " of ide-floppy\n", drive->name);
1386                 goto failed;
1387         }
1388         floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
1389         if (!floppy) {
1390                 printk(KERN_ERR "ide-floppy: %s: Can't allocate a floppy"
1391                                 " structure\n", drive->name);
1392                 goto failed;
1393         }
1394
1395         g = alloc_disk(1 << PARTN_BITS);
1396         if (!g)
1397                 goto out_free_floppy;
1398
1399         ide_init_disk(g, drive);
1400
1401         kref_init(&floppy->kref);
1402
1403         floppy->drive = drive;
1404         floppy->driver = &idefloppy_driver;
1405         floppy->disk = g;
1406
1407         g->private_data = &floppy->driver;
1408
1409         drive->driver_data = floppy;
1410
1411         idefloppy_setup(drive, floppy);
1412
1413         g->minors = 1 << PARTN_BITS;
1414         g->driverfs_dev = &drive->gendev;
1415         g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1416         g->fops = &idefloppy_ops;
1417         drive->attach = 1;
1418         add_disk(g);
1419         return 0;
1420
1421 out_free_floppy:
1422         kfree(floppy);
1423 failed:
1424         return -ENODEV;
1425 }
1426
1427 static void __exit idefloppy_exit(void)
1428 {
1429         driver_unregister(&idefloppy_driver.gen_driver);
1430 }
1431
1432 static int __init idefloppy_init(void)
1433 {
1434         printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
1435         return driver_register(&idefloppy_driver.gen_driver);
1436 }
1437
1438 MODULE_ALIAS("ide:*m-floppy*");
1439 module_init(idefloppy_init);
1440 module_exit(idefloppy_exit);
1441 MODULE_LICENSE("GPL");
1442 MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
1443