ide: /proc/ide/hd*/settings rework
[safe/jmp/linux-2.6] / drivers / ide / ide-floppy.c
1 /*
2  * IDE ATAPI floppy driver.
3  *
4  * Copyright (C) 1996-1999  Gadi Oxman <gadio@netvision.net.il>
5  * Copyright (C) 2000-2002  Paul Bristow <paul@paulbristow.net>
6  * Copyright (C) 2005       Bartlomiej Zolnierkiewicz
7  *
8  * 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 IDEFLOPPY_VERSION "1.00"
19
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/kernel.h>
24 #include <linux/delay.h>
25 #include <linux/timer.h>
26 #include <linux/mm.h>
27 #include <linux/interrupt.h>
28 #include <linux/major.h>
29 #include <linux/errno.h>
30 #include <linux/genhd.h>
31 #include <linux/slab.h>
32 #include <linux/cdrom.h>
33 #include <linux/ide.h>
34 #include <linux/hdreg.h>
35 #include <linux/bitops.h>
36 #include <linux/mutex.h>
37
38 #include <scsi/scsi_ioctl.h>
39
40 #include <asm/byteorder.h>
41 #include <linux/irq.h>
42 #include <linux/uaccess.h>
43 #include <linux/io.h>
44 #include <asm/unaligned.h>
45
46 /* define to see debug info */
47 #define IDEFLOPPY_DEBUG_LOG             0
48
49 /* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
50 #define IDEFLOPPY_DEBUG(fmt, args...)
51
52 #if IDEFLOPPY_DEBUG_LOG
53 #define debug_log(fmt, args...) \
54         printk(KERN_INFO "ide-floppy: " fmt, ## args)
55 #else
56 #define debug_log(fmt, args...) do {} while (0)
57 #endif
58
59
60 /* Some drives require a longer irq timeout. */
61 #define IDEFLOPPY_WAIT_CMD              (5 * WAIT_CMD)
62
63 /*
64  * After each failed packet command we issue a request sense command and retry
65  * the packet command IDEFLOPPY_MAX_PC_RETRIES times.
66  */
67 #define IDEFLOPPY_MAX_PC_RETRIES        3
68
69 /*
70  * With each packet command, we allocate a buffer of IDEFLOPPY_PC_BUFFER_SIZE
71  * bytes.
72  */
73 #define IDEFLOPPY_PC_BUFFER_SIZE        256
74
75 /*
76  * In various places in the driver, we need to allocate storage for packet
77  * commands and requests, which will remain valid while we leave the driver to
78  * wait for an interrupt or a timeout event.
79  */
80 #define IDEFLOPPY_PC_STACK              (10 + IDEFLOPPY_MAX_PC_RETRIES)
81
82 /* format capacities descriptor codes */
83 #define CAPACITY_INVALID        0x00
84 #define CAPACITY_UNFORMATTED    0x01
85 #define CAPACITY_CURRENT        0x02
86 #define CAPACITY_NO_CARTRIDGE   0x03
87
88 /*
89  * Most of our global data which we need to save even as we leave the driver
90  * due to an interrupt or a timer event is stored in a variable of type
91  * idefloppy_floppy_t, defined below.
92  */
93 typedef struct ide_floppy_obj {
94         ide_drive_t     *drive;
95         ide_driver_t    *driver;
96         struct gendisk  *disk;
97         struct kref     kref;
98         unsigned int    openers;        /* protected by BKL for now */
99
100         /* Current packet command */
101         struct ide_atapi_pc *pc;
102         /* Last failed packet command */
103         struct ide_atapi_pc *failed_pc;
104         /* Packet command stack */
105         struct ide_atapi_pc pc_stack[IDEFLOPPY_PC_STACK];
106         /* Next free packet command storage space */
107         int pc_stack_index;
108         struct request rq_stack[IDEFLOPPY_PC_STACK];
109         /* We implement a circular array */
110         int rq_stack_index;
111
112         /* Last error information */
113         u8 sense_key, asc, ascq;
114         /* delay this long before sending packet command */
115         u8 ticks;
116         int progress_indication;
117
118         /* Device information */
119         /* Current format */
120         int blocks, block_size, bs_factor;
121         /* Last format capacity descriptor */
122         u8 cap_desc[8];
123         /* Copy of the flexible disk page */
124         u8 flexible_disk_page[32];
125         /* Write protect */
126         int wp;
127         /* Supports format progress report */
128         int srfp;
129 } idefloppy_floppy_t;
130
131 #define IDEFLOPPY_TICKS_DELAY   HZ/20   /* default delay for ZIP 100 (50ms) */
132
133 /* Defines for the MODE SENSE command */
134 #define MODE_SENSE_CURRENT              0x00
135 #define MODE_SENSE_CHANGEABLE           0x01
136 #define MODE_SENSE_DEFAULT              0x02
137 #define MODE_SENSE_SAVED                0x03
138
139 /* IOCTLs used in low-level formatting. */
140 #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED        0x4600
141 #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY     0x4601
142 #define IDEFLOPPY_IOCTL_FORMAT_START            0x4602
143 #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS     0x4603
144
145 /* Error code returned in rq->errors to the higher part of the driver. */
146 #define IDEFLOPPY_ERROR_GENERAL         101
147
148 /*
149  * Pages of the SELECT SENSE / MODE SENSE packet commands.
150  * See SFF-8070i spec.
151  */
152 #define IDEFLOPPY_CAPABILITIES_PAGE     0x1b
153 #define IDEFLOPPY_FLEXIBLE_DISK_PAGE    0x05
154
155 static DEFINE_MUTEX(idefloppy_ref_mutex);
156
157 #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
158
159 #define ide_floppy_g(disk) \
160         container_of((disk)->private_data, struct ide_floppy_obj, driver)
161
162 static void idefloppy_cleanup_obj(struct kref *);
163
164 static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
165 {
166         struct ide_floppy_obj *floppy = NULL;
167
168         mutex_lock(&idefloppy_ref_mutex);
169         floppy = ide_floppy_g(disk);
170         if (floppy) {
171                 if (ide_device_get(floppy->drive))
172                         floppy = NULL;
173                 else
174                         kref_get(&floppy->kref);
175         }
176         mutex_unlock(&idefloppy_ref_mutex);
177         return floppy;
178 }
179
180 static void ide_floppy_put(struct ide_floppy_obj *floppy)
181 {
182         ide_drive_t *drive = floppy->drive;
183
184         mutex_lock(&idefloppy_ref_mutex);
185         kref_put(&floppy->kref, idefloppy_cleanup_obj);
186         ide_device_put(drive);
187         mutex_unlock(&idefloppy_ref_mutex);
188 }
189
190 /*
191  * Used to finish servicing a request. For read/write requests, we will call
192  * ide_end_request to pass to the next buffer.
193  */
194 static int idefloppy_end_request(ide_drive_t *drive, int uptodate, int nsecs)
195 {
196         idefloppy_floppy_t *floppy = drive->driver_data;
197         struct request *rq = HWGROUP(drive)->rq;
198         int error;
199
200         debug_log("Reached %s\n", __func__);
201
202         switch (uptodate) {
203         case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
204         case 1: error = 0; break;
205         default: error = uptodate;
206         }
207         if (error)
208                 floppy->failed_pc = NULL;
209         /* Why does this happen? */
210         if (!rq)
211                 return 0;
212         if (!blk_special_request(rq)) {
213                 /* our real local end request function */
214                 ide_end_request(drive, uptodate, nsecs);
215                 return 0;
216         }
217         rq->errors = error;
218         /* fixme: need to move this local also */
219         ide_end_drive_cmd(drive, 0, 0);
220         return 0;
221 }
222
223 static void ide_floppy_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
224                                   unsigned int bcount, int direction)
225 {
226         ide_hwif_t *hwif = drive->hwif;
227         struct request *rq = pc->rq;
228         struct req_iterator iter;
229         struct bio_vec *bvec;
230         unsigned long flags;
231         int count, done = 0;
232         char *data;
233
234         rq_for_each_segment(bvec, rq, iter) {
235                 if (!bcount)
236                         break;
237
238                 count = min(bvec->bv_len, bcount);
239
240                 data = bvec_kmap_irq(bvec, &flags);
241                 if (direction)
242                         hwif->tp_ops->output_data(drive, NULL, data, count);
243                 else
244                         hwif->tp_ops->input_data(drive, NULL, data, count);
245                 bvec_kunmap_irq(data, &flags);
246
247                 bcount -= count;
248                 pc->b_count += count;
249                 done += count;
250         }
251
252         idefloppy_end_request(drive, 1, done >> 9);
253
254         if (bcount) {
255                 printk(KERN_ERR "%s: leftover data in %s, bcount == %d\n",
256                                 drive->name, __func__, bcount);
257                 ide_pad_transfer(drive, direction, bcount);
258         }
259 }
260
261 static void idefloppy_update_buffers(ide_drive_t *drive,
262                                 struct ide_atapi_pc *pc)
263 {
264         struct request *rq = pc->rq;
265         struct bio *bio = rq->bio;
266
267         while ((bio = rq->bio) != NULL)
268                 idefloppy_end_request(drive, 1, 0);
269 }
270
271 /*
272  * Generate a new packet command request in front of the request queue, before
273  * the current request so that it will be processed immediately, on the next
274  * pass through the driver.
275  */
276 static void idefloppy_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
277                 struct request *rq)
278 {
279         struct ide_floppy_obj *floppy = drive->driver_data;
280
281         blk_rq_init(NULL, rq);
282         rq->buffer = (char *) pc;
283         rq->cmd_type = REQ_TYPE_SPECIAL;
284         rq->cmd_flags |= REQ_PREEMPT;
285         rq->rq_disk = floppy->disk;
286         memcpy(rq->cmd, pc->c, 12);
287         ide_do_drive_cmd(drive, rq);
288 }
289
290 static struct ide_atapi_pc *idefloppy_next_pc_storage(ide_drive_t *drive)
291 {
292         idefloppy_floppy_t *floppy = drive->driver_data;
293
294         if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
295                 floppy->pc_stack_index = 0;
296         return (&floppy->pc_stack[floppy->pc_stack_index++]);
297 }
298
299 static struct request *idefloppy_next_rq_storage(ide_drive_t *drive)
300 {
301         idefloppy_floppy_t *floppy = drive->driver_data;
302
303         if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
304                 floppy->rq_stack_index = 0;
305         return (&floppy->rq_stack[floppy->rq_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_atapi_pc *pc;
367         struct request *rq;
368
369         (void)ide_read_error(drive);
370         pc = idefloppy_next_pc_storage(drive);
371         rq = idefloppy_next_rq_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 = cmd == READ ? 0 : rq->bio->bi_size;
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 = rq->data_len;
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         struct ide_atapi_pc *pc;
604         unsigned long block = (unsigned long)block_s;
605
606         debug_log("dev: %s, cmd_type: %x, errors: %d\n",
607                         rq->rq_disk ? rq->rq_disk->disk_name : "?",
608                         rq->cmd_type, rq->errors);
609         debug_log("sector: %ld, nr_sectors: %ld, "
610                         "current_nr_sectors: %d\n", (long)rq->sector,
611                         rq->nr_sectors, rq->current_nr_sectors);
612
613         if (rq->errors >= ERROR_MAX) {
614                 if (floppy->failed_pc)
615                         ide_floppy_report_error(floppy, floppy->failed_pc);
616                 else
617                         printk(KERN_ERR "ide-floppy: %s: I/O error\n",
618                                 drive->name);
619                 idefloppy_end_request(drive, 0, 0);
620                 return ide_stopped;
621         }
622         if (blk_fs_request(rq)) {
623                 if (((long)rq->sector % floppy->bs_factor) ||
624                     (rq->nr_sectors % floppy->bs_factor)) {
625                         printk(KERN_ERR "%s: unsupported r/w request size\n",
626                                         drive->name);
627                         idefloppy_end_request(drive, 0, 0);
628                         return ide_stopped;
629                 }
630                 pc = idefloppy_next_pc_storage(drive);
631                 idefloppy_create_rw_cmd(floppy, pc, rq, block);
632         } else if (blk_special_request(rq)) {
633                 pc = (struct ide_atapi_pc *) rq->buffer;
634         } else if (blk_pc_request(rq)) {
635                 pc = idefloppy_next_pc_storage(drive);
636                 idefloppy_blockpc_cmd(floppy, pc, rq);
637         } else {
638                 blk_dump_rq_flags(rq,
639                         "ide-floppy: unsupported command in queue");
640                 idefloppy_end_request(drive, 0, 0);
641                 return ide_stopped;
642         }
643
644         pc->rq = rq;
645
646         return idefloppy_issue_pc(drive, pc);
647 }
648
649 /*
650  * Add a special packet command request to the tail of the request queue,
651  * and wait for it to be serviced.
652  */
653 static int idefloppy_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
654 {
655         struct ide_floppy_obj *floppy = drive->driver_data;
656         struct request *rq;
657         int error;
658
659         rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
660         rq->buffer = (char *) pc;
661         rq->cmd_type = REQ_TYPE_SPECIAL;
662         memcpy(rq->cmd, pc->c, 12);
663         error = blk_execute_rq(drive->queue, floppy->disk, rq, 0);
664         blk_put_request(rq);
665
666         return error;
667 }
668
669 /*
670  * Look at the flexible disk page parameters. We ignore the CHS capacity
671  * parameters and use the LBA parameters instead.
672  */
673 static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
674 {
675         idefloppy_floppy_t *floppy = drive->driver_data;
676         struct ide_atapi_pc pc;
677         u8 *page;
678         int capacity, lba_capacity;
679         u16 transfer_rate, sector_size, cyls, rpm;
680         u8 heads, sectors;
681
682         idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE,
683                                         MODE_SENSE_CURRENT);
684
685         if (idefloppy_queue_pc_tail(drive, &pc)) {
686                 printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
687                                 " parameters\n");
688                 return 1;
689         }
690         floppy->wp = !!(pc.buf[3] & 0x80);
691         set_disk_ro(floppy->disk, floppy->wp);
692         page = &pc.buf[8];
693
694         transfer_rate = be16_to_cpup((__be16 *)&pc.buf[8 + 2]);
695         sector_size   = be16_to_cpup((__be16 *)&pc.buf[8 + 6]);
696         cyls          = be16_to_cpup((__be16 *)&pc.buf[8 + 8]);
697         rpm           = be16_to_cpup((__be16 *)&pc.buf[8 + 28]);
698         heads         = pc.buf[8 + 4];
699         sectors       = pc.buf[8 + 5];
700
701         capacity = cyls * heads * sectors * sector_size;
702
703         if (memcmp(page, &floppy->flexible_disk_page, 32))
704                 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
705                                 "%d sector size, %d rpm\n",
706                                 drive->name, capacity / 1024, cyls, heads,
707                                 sectors, transfer_rate / 8, sector_size, rpm);
708
709         memcpy(&floppy->flexible_disk_page, page, 32);
710         drive->bios_cyl = cyls;
711         drive->bios_head = heads;
712         drive->bios_sect = sectors;
713         lba_capacity = floppy->blocks * floppy->block_size;
714
715         if (capacity < lba_capacity) {
716                 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
717                         "bytes, but the drive only handles %d\n",
718                         drive->name, lba_capacity, capacity);
719                 floppy->blocks = floppy->block_size ?
720                         capacity / floppy->block_size : 0;
721         }
722         return 0;
723 }
724
725 static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
726 {
727         idefloppy_floppy_t *floppy = drive->driver_data;
728         struct ide_atapi_pc pc;
729
730         floppy->srfp = 0;
731         idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
732                                                  MODE_SENSE_CURRENT);
733
734         pc.flags |= PC_FLAG_SUPPRESS_ERROR;
735         if (idefloppy_queue_pc_tail(drive, &pc))
736                 return 1;
737
738         floppy->srfp = pc.buf[8 + 2] & 0x40;
739         return (0);
740 }
741
742 /*
743  * Determine if a media is present in the floppy drive, and if so, its LBA
744  * capacity.
745  */
746 static int ide_floppy_get_capacity(ide_drive_t *drive)
747 {
748         idefloppy_floppy_t *floppy = drive->driver_data;
749         struct ide_atapi_pc pc;
750         u8 *cap_desc;
751         u8 header_len, desc_cnt;
752         int i, rc = 1, blocks, length;
753
754         drive->bios_cyl = 0;
755         drive->bios_head = drive->bios_sect = 0;
756         floppy->blocks = 0;
757         floppy->bs_factor = 1;
758         set_capacity(floppy->disk, 0);
759
760         idefloppy_create_read_capacity_cmd(&pc);
761         if (idefloppy_queue_pc_tail(drive, &pc)) {
762                 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
763                 return 1;
764         }
765         header_len = pc.buf[3];
766         cap_desc = &pc.buf[4];
767         desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
768
769         for (i = 0; i < desc_cnt; i++) {
770                 unsigned int desc_start = 4 + i*8;
771
772                 blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
773                 length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
774
775                 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
776                                 i, blocks * length / 1024, blocks, length);
777
778                 if (i)
779                         continue;
780                 /*
781                  * the code below is valid only for the 1st descriptor, ie i=0
782                  */
783
784                 switch (pc.buf[desc_start + 4] & 0x03) {
785                 /* Clik! drive returns this instead of CAPACITY_CURRENT */
786                 case CAPACITY_UNFORMATTED:
787                         if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
788                                 /*
789                                  * If it is not a clik drive, break out
790                                  * (maintains previous driver behaviour)
791                                  */
792                                 break;
793                 case CAPACITY_CURRENT:
794                         /* Normal Zip/LS-120 disks */
795                         if (memcmp(cap_desc, &floppy->cap_desc, 8))
796                                 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
797                                         "sector size\n", drive->name,
798                                         blocks * length / 1024, blocks, length);
799                         memcpy(&floppy->cap_desc, cap_desc, 8);
800
801                         if (!length || length % 512) {
802                                 printk(KERN_NOTICE "%s: %d bytes block size "
803                                         "not supported\n", drive->name, length);
804                         } else {
805                                 floppy->blocks = blocks;
806                                 floppy->block_size = length;
807                                 floppy->bs_factor = length / 512;
808                                 if (floppy->bs_factor != 1)
809                                         printk(KERN_NOTICE "%s: warning: non "
810                                                 "512 bytes block size not "
811                                                 "fully supported\n",
812                                                 drive->name);
813                                 rc = 0;
814                         }
815                         break;
816                 case CAPACITY_NO_CARTRIDGE:
817                         /*
818                          * This is a KERN_ERR so it appears on screen
819                          * for the user to see
820                          */
821                         printk(KERN_ERR "%s: No disk in drive\n", drive->name);
822                         break;
823                 case CAPACITY_INVALID:
824                         printk(KERN_ERR "%s: Invalid capacity for disk "
825                                 "in drive\n", drive->name);
826                         break;
827                 }
828                 debug_log("Descriptor 0 Code: %d\n",
829                           pc.buf[desc_start + 4] & 0x03);
830         }
831
832         /* Clik! disk does not support get_flexible_disk_page */
833         if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
834                 (void) ide_floppy_get_flexible_disk_page(drive);
835
836         set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
837         return rc;
838 }
839
840 /*
841  * Obtain the list of formattable capacities.
842  * Very similar to ide_floppy_get_capacity, except that we push the capacity
843  * descriptors to userland, instead of our own structures.
844  *
845  * Userland gives us the following structure:
846  *
847  * struct idefloppy_format_capacities {
848  *      int nformats;
849  *      struct {
850  *              int nblocks;
851  *              int blocksize;
852  *      } formats[];
853  * };
854  *
855  * userland initializes nformats to the number of allocated formats[] records.
856  * On exit we set nformats to the number of records we've actually initialized.
857  */
858
859 static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
860 {
861         struct ide_atapi_pc pc;
862         u8 header_len, desc_cnt;
863         int i, blocks, length, u_array_size, u_index;
864         int __user *argp;
865
866         if (get_user(u_array_size, arg))
867                 return (-EFAULT);
868
869         if (u_array_size <= 0)
870                 return (-EINVAL);
871
872         idefloppy_create_read_capacity_cmd(&pc);
873         if (idefloppy_queue_pc_tail(drive, &pc)) {
874                 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
875                 return (-EIO);
876         }
877         header_len = pc.buf[3];
878         desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
879
880         u_index = 0;
881         argp = arg + 1;
882
883         /*
884          * We always skip the first capacity descriptor.  That's the current
885          * capacity.  We are interested in the remaining descriptors, the
886          * formattable capacities.
887          */
888         for (i = 1; i < desc_cnt; i++) {
889                 unsigned int desc_start = 4 + i*8;
890
891                 if (u_index >= u_array_size)
892                         break;  /* User-supplied buffer too small */
893
894                 blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
895                 length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
896
897                 if (put_user(blocks, argp))
898                         return(-EFAULT);
899                 ++argp;
900
901                 if (put_user(length, argp))
902                         return (-EFAULT);
903                 ++argp;
904
905                 ++u_index;
906         }
907
908         if (put_user(u_index, arg))
909                 return (-EFAULT);
910         return (0);
911 }
912
913 /*
914  * Get ATAPI_FORMAT_UNIT progress indication.
915  *
916  * Userland gives a pointer to an int.  The int is set to a progress
917  * indicator 0-65536, with 65536=100%.
918  *
919  * If the drive does not support format progress indication, we just check
920  * the dsc bit, and return either 0 or 65536.
921  */
922
923 static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
924 {
925         idefloppy_floppy_t *floppy = drive->driver_data;
926         struct ide_atapi_pc pc;
927         int progress_indication = 0x10000;
928
929         if (floppy->srfp) {
930                 idefloppy_create_request_sense_cmd(&pc);
931                 if (idefloppy_queue_pc_tail(drive, &pc))
932                         return (-EIO);
933
934                 if (floppy->sense_key == 2 &&
935                     floppy->asc == 4 &&
936                     floppy->ascq == 4)
937                         progress_indication = floppy->progress_indication;
938
939                 /* Else assume format_unit has finished, and we're at 0x10000 */
940         } else {
941                 ide_hwif_t *hwif = drive->hwif;
942                 unsigned long flags;
943                 u8 stat;
944
945                 local_irq_save(flags);
946                 stat = hwif->tp_ops->read_status(hwif);
947                 local_irq_restore(flags);
948
949                 progress_indication = ((stat & ATA_DSC) == 0) ? 0 : 0x10000;
950         }
951         if (put_user(progress_indication, arg))
952                 return (-EFAULT);
953
954         return (0);
955 }
956
957 static sector_t idefloppy_capacity(ide_drive_t *drive)
958 {
959         idefloppy_floppy_t *floppy = drive->driver_data;
960         unsigned long capacity = floppy->blocks * floppy->bs_factor;
961
962         return capacity;
963 }
964
965 /*
966  * Check whether we can support a drive, based on the ATAPI IDENTIFY command
967  * results.
968  */
969 static int idefloppy_identify_device(ide_drive_t *drive, u16 *id)
970 {
971         u8 gcw[2];
972         u8 device_type, protocol, removable, drq_type, packet_size;
973
974         *((u16 *)&gcw) = id[ATA_ID_CONFIG];
975
976         device_type =  gcw[1] & 0x1F;
977         removable   = (gcw[0] & 0x80) >> 7;
978         protocol    = (gcw[1] & 0xC0) >> 6;
979         drq_type    = (gcw[0] & 0x60) >> 5;
980         packet_size =  gcw[0] & 0x03;
981
982 #ifdef CONFIG_PPC
983         /* kludge for Apple PowerBook internal zip */
984         if (device_type == 5 &&
985             !strstr((char *)&id[ATA_ID_PROD], "CD-ROM") &&
986             strstr((char *)&id[ATA_ID_PROD], "ZIP"))
987                 device_type = 0;
988 #endif
989
990         if (protocol != 2)
991                 printk(KERN_ERR "ide-floppy: Protocol (0x%02x) is not ATAPI\n",
992                         protocol);
993         else if (device_type != 0)
994                 printk(KERN_ERR "ide-floppy: Device type (0x%02x) is not set "
995                                 "to floppy\n", device_type);
996         else if (!removable)
997                 printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
998         else if (drq_type == 3)
999                 printk(KERN_ERR "ide-floppy: Sorry, DRQ type (0x%02x) not "
1000                                 "supported\n", drq_type);
1001         else if (packet_size != 0)
1002                 printk(KERN_ERR "ide-floppy: Packet size (0x%02x) is not 12 "
1003                                 "bytes\n", packet_size);
1004         else
1005                 return 1;
1006         return 0;
1007 }
1008
1009 #ifdef CONFIG_IDE_PROC_FS
1010 ide_devset_rw(bios_cyl,  0, 1023, bios_cyl);
1011 ide_devset_rw(bios_head, 0,  255, bios_head);
1012 ide_devset_rw(bios_sect, 0,   63, bios_sect);
1013
1014 static int get_ticks(ide_drive_t *drive)
1015 {
1016         idefloppy_floppy_t *floppy = drive->driver_data;
1017         return floppy->ticks;
1018 }
1019
1020 static int set_ticks(ide_drive_t *drive, int arg)
1021 {
1022         idefloppy_floppy_t *floppy = drive->driver_data;
1023         floppy->ticks = arg;
1024         return 0;
1025 }
1026
1027 IDE_DEVSET(ticks, S_RW, 0, 255, get_ticks, set_ticks);
1028
1029 static const struct ide_devset *idefloppy_settings[] = {
1030         &ide_devset_bios_cyl,
1031         &ide_devset_bios_head,
1032         &ide_devset_bios_sect,
1033         &ide_devset_ticks,
1034         NULL
1035 };
1036 #endif
1037
1038 static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
1039 {
1040         u16 *id = drive->id;
1041         u8 gcw[2];
1042
1043         *((u16 *)&gcw) = id[ATA_ID_CONFIG];
1044         floppy->pc = floppy->pc_stack;
1045         drive->pc_callback = ide_floppy_callback;
1046
1047         if (((gcw[0] & 0x60) >> 5) == 1)
1048                 drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
1049         /*
1050          * We used to check revisions here. At this point however I'm giving up.
1051          * Just assume they are all broken, its easier.
1052          *
1053          * The actual reason for the workarounds was likely a driver bug after
1054          * all rather than a firmware bug, and the workaround below used to hide
1055          * it. It should be fixed as of version 1.9, but to be on the safe side
1056          * we'll leave the limitation below for the 2.2.x tree.
1057          */
1058         if (!strncmp((char *)&id[ATA_ID_PROD], "IOMEGA ZIP 100 ATAPI", 20)) {
1059                 drive->atapi_flags |= IDE_AFLAG_ZIP_DRIVE;
1060                 /* This value will be visible in the /proc/ide/hdx/settings */
1061                 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1062                 blk_queue_max_sectors(drive->queue, 64);
1063         }
1064
1065         /*
1066          * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
1067          * nasty clicking noises without it, so please don't remove this.
1068          */
1069         if (strncmp((char *)&id[ATA_ID_PROD], "IOMEGA Clik!", 11) == 0) {
1070                 blk_queue_max_sectors(drive->queue, 64);
1071                 drive->atapi_flags |= IDE_AFLAG_CLIK_DRIVE;
1072         }
1073
1074         (void) ide_floppy_get_capacity(drive);
1075
1076         ide_proc_register_driver(drive, floppy->driver);
1077 }
1078
1079 static void ide_floppy_remove(ide_drive_t *drive)
1080 {
1081         idefloppy_floppy_t *floppy = drive->driver_data;
1082         struct gendisk *g = floppy->disk;
1083
1084         ide_proc_unregister_driver(drive, floppy->driver);
1085
1086         del_gendisk(g);
1087
1088         ide_floppy_put(floppy);
1089 }
1090
1091 static void idefloppy_cleanup_obj(struct kref *kref)
1092 {
1093         struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1094         ide_drive_t *drive = floppy->drive;
1095         struct gendisk *g = floppy->disk;
1096
1097         drive->driver_data = NULL;
1098         g->private_data = NULL;
1099         put_disk(g);
1100         kfree(floppy);
1101 }
1102
1103 #ifdef CONFIG_IDE_PROC_FS
1104 static int proc_idefloppy_read_capacity(char *page, char **start, off_t off,
1105                 int count, int *eof, void *data)
1106 {
1107         ide_drive_t*drive = (ide_drive_t *)data;
1108         int len;
1109
1110         len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive));
1111         PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
1112 }
1113
1114 static ide_proc_entry_t idefloppy_proc[] = {
1115         { "capacity",   S_IFREG|S_IRUGO, proc_idefloppy_read_capacity,  NULL },
1116         { "geometry",   S_IFREG|S_IRUGO, proc_ide_read_geometry,        NULL },
1117         { NULL, 0, NULL, NULL }
1118 };
1119 #endif  /* CONFIG_IDE_PROC_FS */
1120
1121 static int ide_floppy_probe(ide_drive_t *);
1122
1123 static ide_driver_t idefloppy_driver = {
1124         .gen_driver = {
1125                 .owner          = THIS_MODULE,
1126                 .name           = "ide-floppy",
1127                 .bus            = &ide_bus_type,
1128         },
1129         .probe                  = ide_floppy_probe,
1130         .remove                 = ide_floppy_remove,
1131         .version                = IDEFLOPPY_VERSION,
1132         .media                  = ide_floppy,
1133         .supports_dsc_overlap   = 0,
1134         .do_request             = idefloppy_do_request,
1135         .end_request            = idefloppy_end_request,
1136         .error                  = __ide_error,
1137 #ifdef CONFIG_IDE_PROC_FS
1138         .proc                   = idefloppy_proc,
1139         .settings               = idefloppy_settings,
1140 #endif
1141 };
1142
1143 static int idefloppy_open(struct inode *inode, struct file *filp)
1144 {
1145         struct gendisk *disk = inode->i_bdev->bd_disk;
1146         struct ide_floppy_obj *floppy;
1147         ide_drive_t *drive;
1148         struct ide_atapi_pc pc;
1149         int ret = 0;
1150
1151         debug_log("Reached %s\n", __func__);
1152
1153         floppy = ide_floppy_get(disk);
1154         if (!floppy)
1155                 return -ENXIO;
1156
1157         drive = floppy->drive;
1158
1159         floppy->openers++;
1160
1161         if (floppy->openers == 1) {
1162                 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1163                 /* Just in case */
1164
1165                 idefloppy_init_pc(&pc);
1166                 pc.c[0] = GPCMD_TEST_UNIT_READY;
1167
1168                 if (idefloppy_queue_pc_tail(drive, &pc)) {
1169                         idefloppy_create_start_stop_cmd(&pc, 1);
1170                         (void) idefloppy_queue_pc_tail(drive, &pc);
1171                 }
1172
1173                 if (ide_floppy_get_capacity(drive)
1174                    && (filp->f_flags & O_NDELAY) == 0
1175                     /*
1176                      * Allow O_NDELAY to open a drive without a disk, or with an
1177                      * unreadable disk, so that we can get the format capacity
1178                      * of the drive or begin the format - Sam
1179                      */
1180                     ) {
1181                         ret = -EIO;
1182                         goto out_put_floppy;
1183                 }
1184
1185                 if (floppy->wp && (filp->f_mode & 2)) {
1186                         ret = -EROFS;
1187                         goto out_put_floppy;
1188                 }
1189                 drive->atapi_flags |= IDE_AFLAG_MEDIA_CHANGED;
1190                 /* IOMEGA Clik! drives do not support lock/unlock commands */
1191                 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
1192                         idefloppy_create_prevent_cmd(&pc, 1);
1193                         (void) idefloppy_queue_pc_tail(drive, &pc);
1194                 }
1195                 check_disk_change(inode->i_bdev);
1196         } else if (drive->atapi_flags & IDE_AFLAG_FORMAT_IN_PROGRESS) {
1197                 ret = -EBUSY;
1198                 goto out_put_floppy;
1199         }
1200         return 0;
1201
1202 out_put_floppy:
1203         floppy->openers--;
1204         ide_floppy_put(floppy);
1205         return ret;
1206 }
1207
1208 static int idefloppy_release(struct inode *inode, struct file *filp)
1209 {
1210         struct gendisk *disk = inode->i_bdev->bd_disk;
1211         struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1212         ide_drive_t *drive = floppy->drive;
1213         struct ide_atapi_pc pc;
1214
1215         debug_log("Reached %s\n", __func__);
1216
1217         if (floppy->openers == 1) {
1218                 /* IOMEGA Clik! drives do not support lock/unlock commands */
1219                 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
1220                         idefloppy_create_prevent_cmd(&pc, 0);
1221                         (void) idefloppy_queue_pc_tail(drive, &pc);
1222                 }
1223
1224                 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1225         }
1226
1227         floppy->openers--;
1228
1229         ide_floppy_put(floppy);
1230
1231         return 0;
1232 }
1233
1234 static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1235 {
1236         struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1237         ide_drive_t *drive = floppy->drive;
1238
1239         geo->heads = drive->bios_head;
1240         geo->sectors = drive->bios_sect;
1241         geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1242         return 0;
1243 }
1244
1245 static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
1246                                unsigned long arg, unsigned int cmd)
1247 {
1248         idefloppy_floppy_t *floppy = drive->driver_data;
1249
1250         if (floppy->openers > 1)
1251                 return -EBUSY;
1252
1253         /* The IOMEGA Clik! Drive doesn't support this command -
1254          * no room for an eject mechanism */
1255         if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
1256                 int prevent = arg ? 1 : 0;
1257
1258                 if (cmd == CDROMEJECT)
1259                         prevent = 0;
1260
1261                 idefloppy_create_prevent_cmd(pc, prevent);
1262                 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1263         }
1264
1265         if (cmd == CDROMEJECT) {
1266                 idefloppy_create_start_stop_cmd(pc, 2);
1267                 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1268         }
1269
1270         return 0;
1271 }
1272
1273 static int ide_floppy_format_unit(idefloppy_floppy_t *floppy,
1274                                   int __user *arg)
1275 {
1276         struct ide_atapi_pc pc;
1277         ide_drive_t *drive = floppy->drive;
1278         int blocks, length, flags, err = 0;
1279
1280         if (floppy->openers > 1) {
1281                 /* Don't format if someone is using the disk */
1282                 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1283                 return -EBUSY;
1284         }
1285
1286         drive->atapi_flags |= IDE_AFLAG_FORMAT_IN_PROGRESS;
1287
1288         /*
1289          * Send ATAPI_FORMAT_UNIT to the drive.
1290          *
1291          * Userland gives us the following structure:
1292          *
1293          * struct idefloppy_format_command {
1294          *        int nblocks;
1295          *        int blocksize;
1296          *        int flags;
1297          *        } ;
1298          *
1299          * flags is a bitmask, currently, the only defined flag is:
1300          *
1301          *        0x01 - verify media after format.
1302          */
1303         if (get_user(blocks, arg) ||
1304                         get_user(length, arg+1) ||
1305                         get_user(flags, arg+2)) {
1306                 err = -EFAULT;
1307                 goto out;
1308         }
1309
1310         (void) idefloppy_get_sfrp_bit(drive);
1311         idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1312
1313         if (idefloppy_queue_pc_tail(drive, &pc))
1314                 err = -EIO;
1315
1316 out:
1317         if (err)
1318                 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1319         return err;
1320 }
1321
1322
1323 static int idefloppy_ioctl(struct inode *inode, struct file *file,
1324                         unsigned int cmd, unsigned long arg)
1325 {
1326         struct block_device *bdev = inode->i_bdev;
1327         struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1328         ide_drive_t *drive = floppy->drive;
1329         struct ide_atapi_pc pc;
1330         void __user *argp = (void __user *)arg;
1331         int err;
1332
1333         switch (cmd) {
1334         case CDROMEJECT:
1335                 /* fall through */
1336         case CDROM_LOCKDOOR:
1337                 return ide_floppy_lockdoor(drive, &pc, arg, cmd);
1338         case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1339                 return 0;
1340         case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
1341                 return ide_floppy_get_format_capacities(drive, argp);
1342         case IDEFLOPPY_IOCTL_FORMAT_START:
1343                 if (!(file->f_mode & 2))
1344                         return -EPERM;
1345
1346                 return ide_floppy_format_unit(floppy, (int __user *)arg);
1347         case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1348                 return idefloppy_get_format_progress(drive, argp);
1349         }
1350
1351         /*
1352          * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1353          * and CDROM_SEND_PACKET (legacy) ioctls
1354          */
1355         if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1356                 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1357                                         bdev->bd_disk, cmd, argp);
1358         else
1359                 err = -ENOTTY;
1360
1361         if (err == -ENOTTY)
1362                 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1363
1364         return err;
1365 }
1366
1367 static int idefloppy_media_changed(struct gendisk *disk)
1368 {
1369         struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1370         ide_drive_t *drive = floppy->drive;
1371         int ret;
1372
1373         /* do not scan partitions twice if this is a removable device */
1374         if (drive->attach) {
1375                 drive->attach = 0;
1376                 return 0;
1377         }
1378         ret = !!(drive->atapi_flags & IDE_AFLAG_MEDIA_CHANGED);
1379         drive->atapi_flags &= ~IDE_AFLAG_MEDIA_CHANGED;
1380         return ret;
1381 }
1382
1383 static int idefloppy_revalidate_disk(struct gendisk *disk)
1384 {
1385         struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1386         set_capacity(disk, idefloppy_capacity(floppy->drive));
1387         return 0;
1388 }
1389
1390 static struct block_device_operations idefloppy_ops = {
1391         .owner                  = THIS_MODULE,
1392         .open                   = idefloppy_open,
1393         .release                = idefloppy_release,
1394         .ioctl                  = idefloppy_ioctl,
1395         .getgeo                 = idefloppy_getgeo,
1396         .media_changed          = idefloppy_media_changed,
1397         .revalidate_disk        = idefloppy_revalidate_disk
1398 };
1399
1400 static int ide_floppy_probe(ide_drive_t *drive)
1401 {
1402         idefloppy_floppy_t *floppy;
1403         struct gendisk *g;
1404
1405         if (!strstr("ide-floppy", drive->driver_req))
1406                 goto failed;
1407
1408         if (drive->media != ide_floppy)
1409                 goto failed;
1410
1411         if (!idefloppy_identify_device(drive, drive->id)) {
1412                 printk(KERN_ERR "ide-floppy: %s: not supported by this version"
1413                                 " of ide-floppy\n", drive->name);
1414                 goto failed;
1415         }
1416         floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
1417         if (!floppy) {
1418                 printk(KERN_ERR "ide-floppy: %s: Can't allocate a floppy"
1419                                 " structure\n", drive->name);
1420                 goto failed;
1421         }
1422
1423         g = alloc_disk(1 << PARTN_BITS);
1424         if (!g)
1425                 goto out_free_floppy;
1426
1427         ide_init_disk(g, drive);
1428
1429         kref_init(&floppy->kref);
1430
1431         floppy->drive = drive;
1432         floppy->driver = &idefloppy_driver;
1433         floppy->disk = g;
1434
1435         g->private_data = &floppy->driver;
1436
1437         drive->driver_data = floppy;
1438
1439         idefloppy_setup(drive, floppy);
1440
1441         g->minors = 1 << PARTN_BITS;
1442         g->driverfs_dev = &drive->gendev;
1443         g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1444         g->fops = &idefloppy_ops;
1445         drive->attach = 1;
1446         add_disk(g);
1447         return 0;
1448
1449 out_free_floppy:
1450         kfree(floppy);
1451 failed:
1452         return -ENODEV;
1453 }
1454
1455 static void __exit idefloppy_exit(void)
1456 {
1457         driver_unregister(&idefloppy_driver.gen_driver);
1458 }
1459
1460 static int __init idefloppy_init(void)
1461 {
1462         printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
1463         return driver_register(&idefloppy_driver.gen_driver);
1464 }
1465
1466 MODULE_ALIAS("ide:*m-floppy*");
1467 module_init(idefloppy_init);
1468 module_exit(idefloppy_exit);
1469 MODULE_LICENSE("GPL");
1470 MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
1471