2 * IDE ATAPI floppy driver.
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
8 * This driver supports the following IDE floppy drives:
10 * LS-120/240 SuperDisk
12 * Iomega PC Card Clik!/PocketZip
14 * For a historical changelog see
15 * Documentation/ide/ChangeLog.ide-floppy.1996-2002
18 #define IDEFLOPPY_VERSION "1.00"
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>
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/bitops.h>
35 #include <linux/mutex.h>
37 #include <scsi/scsi_ioctl.h>
39 #include <asm/byteorder.h>
40 #include <linux/irq.h>
41 #include <linux/uaccess.h>
43 #include <asm/unaligned.h>
45 /* define to see debug info */
46 #define IDEFLOPPY_DEBUG_LOG 0
48 /* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
49 #define IDEFLOPPY_DEBUG(fmt, args...)
51 #if IDEFLOPPY_DEBUG_LOG
52 #define debug_log(fmt, args...) \
53 printk(KERN_INFO "ide-floppy: " fmt, ## args)
55 #define debug_log(fmt, args...) do {} while (0)
59 /* Some drives require a longer irq timeout. */
60 #define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
63 * After each failed packet command we issue a request sense command and retry
64 * the packet command IDEFLOPPY_MAX_PC_RETRIES times.
66 #define IDEFLOPPY_MAX_PC_RETRIES 3
69 * With each packet command, we allocate a buffer of IDEFLOPPY_PC_BUFFER_SIZE
72 #define IDEFLOPPY_PC_BUFFER_SIZE 256
75 * In various places in the driver, we need to allocate storage for packet
76 * commands and requests, which will remain valid while we leave the driver to
77 * wait for an interrupt or a timeout event.
79 #define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
81 typedef struct idefloppy_packet_command_s {
82 u8 c[12]; /* Actual packet bytes */
83 int retries; /* On each retry, we increment
85 int error; /* Error code */
86 int request_transfer; /* Bytes to transfer */
87 int actually_transferred; /* Bytes actually transferred */
88 int buffer_size; /* Size of our data buffer */
89 int b_count; /* Missing/Available data on
91 struct request *rq; /* The corresponding request */
92 u8 *buffer; /* Data buffer */
93 u8 *current_position; /* Pointer into above buffer */
94 void (*callback) (ide_drive_t *); /* Called when this packet
95 command is completed */
96 u8 pc_buffer[IDEFLOPPY_PC_BUFFER_SIZE]; /* Temporary buffer */
97 unsigned long flags; /* Status/Action bit flags: long
101 /* Packet command flag bits. */
103 /* 1 when we prefer to use DMA if possible */
104 PC_FLAG_DMA_RECOMMENDED = (1 << 0),
105 /* 1 while DMA in progress */
106 PC_FLAG_DMA_IN_PROGRESS = (1 << 1),
107 /* 1 when encountered problem during DMA */
108 PC_FLAG_DMA_ERROR = (1 << 2),
110 PC_FLAG_WRITING = (1 << 3),
111 /* Suppress error reporting */
112 PC_FLAG_SUPPRESS_ERROR = (1 << 4),
115 /* format capacities descriptor codes */
116 #define CAPACITY_INVALID 0x00
117 #define CAPACITY_UNFORMATTED 0x01
118 #define CAPACITY_CURRENT 0x02
119 #define CAPACITY_NO_CARTRIDGE 0x03
122 * Most of our global data which we need to save even as we leave the driver
123 * due to an interrupt or a timer event is stored in a variable of type
124 * idefloppy_floppy_t, defined below.
126 typedef struct ide_floppy_obj {
128 ide_driver_t *driver;
129 struct gendisk *disk;
131 unsigned int openers; /* protected by BKL for now */
133 /* Current packet command */
135 /* Last failed packet command */
136 idefloppy_pc_t *failed_pc;
137 /* Packet command stack */
138 idefloppy_pc_t pc_stack[IDEFLOPPY_PC_STACK];
139 /* Next free packet command storage space */
141 struct request rq_stack[IDEFLOPPY_PC_STACK];
142 /* We implement a circular array */
145 /* Last error information */
146 u8 sense_key, asc, ascq;
147 /* delay this long before sending packet command */
149 int progress_indication;
151 /* Device information */
153 int blocks, block_size, bs_factor;
154 /* Last format capacity descriptor */
156 /* Copy of the flexible disk page */
157 u8 flexible_disk_page[32];
160 /* Supports format progress report */
162 /* Status/Action flags */
164 } idefloppy_floppy_t;
166 #define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
168 /* Floppy flag bits values. */
170 /* DRQ interrupt device */
171 IDEFLOPPY_FLAG_DRQ_INTERRUPT = (1 << 0),
172 /* Media may have changed */
173 IDEFLOPPY_FLAG_MEDIA_CHANGED = (1 << 1),
174 /* Format in progress */
175 IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS = (1 << 2),
176 /* Avoid commands not supported in Clik drive */
177 IDEFLOPPY_FLAG_CLIK_DRIVE = (1 << 3),
178 /* Requires BH algorithm for packets */
179 IDEFLOPPY_FLAG_ZIP_DRIVE = (1 << 4),
182 /* Defines for the MODE SENSE command */
183 #define MODE_SENSE_CURRENT 0x00
184 #define MODE_SENSE_CHANGEABLE 0x01
185 #define MODE_SENSE_DEFAULT 0x02
186 #define MODE_SENSE_SAVED 0x03
188 /* IOCTLs used in low-level formatting. */
189 #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
190 #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
191 #define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
192 #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
194 /* Error code returned in rq->errors to the higher part of the driver. */
195 #define IDEFLOPPY_ERROR_GENERAL 101
198 * The following is used to format the general configuration word of the
199 * ATAPI IDENTIFY DEVICE command.
201 struct idefloppy_id_gcw {
202 #if defined(__LITTLE_ENDIAN_BITFIELD)
203 unsigned packet_size :2; /* Packet Size */
204 unsigned reserved234 :3; /* Reserved */
205 unsigned drq_type :2; /* Command packet DRQ type */
206 unsigned removable :1; /* Removable media */
207 unsigned device_type :5; /* Device type */
208 unsigned reserved13 :1; /* Reserved */
209 unsigned protocol :2; /* Protocol type */
210 #elif defined(__BIG_ENDIAN_BITFIELD)
211 unsigned protocol :2; /* Protocol type */
212 unsigned reserved13 :1; /* Reserved */
213 unsigned device_type :5; /* Device type */
214 unsigned removable :1; /* Removable media */
215 unsigned drq_type :2; /* Command packet DRQ type */
216 unsigned reserved234 :3; /* Reserved */
217 unsigned packet_size :2; /* Packet Size */
219 #error "Bitfield endianness not defined! Check your byteorder.h"
224 * Pages of the SELECT SENSE / MODE SENSE packet commands.
225 * See SFF-8070i spec.
227 #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
228 #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
230 static DEFINE_MUTEX(idefloppy_ref_mutex);
232 #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
234 #define ide_floppy_g(disk) \
235 container_of((disk)->private_data, struct ide_floppy_obj, driver)
237 static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
239 struct ide_floppy_obj *floppy = NULL;
241 mutex_lock(&idefloppy_ref_mutex);
242 floppy = ide_floppy_g(disk);
244 kref_get(&floppy->kref);
245 mutex_unlock(&idefloppy_ref_mutex);
249 static void idefloppy_cleanup_obj(struct kref *);
251 static void ide_floppy_put(struct ide_floppy_obj *floppy)
253 mutex_lock(&idefloppy_ref_mutex);
254 kref_put(&floppy->kref, idefloppy_cleanup_obj);
255 mutex_unlock(&idefloppy_ref_mutex);
259 * Too bad. The drive wants to send us data which we are not ready to accept.
260 * Just throw it away.
262 static void idefloppy_discard_data(ide_drive_t *drive, unsigned int bcount)
265 (void) HWIF(drive)->INB(IDE_DATA_REG);
268 static void idefloppy_write_zeros(ide_drive_t *drive, unsigned int bcount)
271 HWIF(drive)->OUTB(0, IDE_DATA_REG);
276 * Used to finish servicing a request. For read/write requests, we will call
277 * ide_end_request to pass to the next buffer.
279 static int idefloppy_do_end_request(ide_drive_t *drive, int uptodate, int nsecs)
281 idefloppy_floppy_t *floppy = drive->driver_data;
282 struct request *rq = HWGROUP(drive)->rq;
285 debug_log("Reached %s\n", __func__);
288 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
289 case 1: error = 0; break;
290 default: error = uptodate;
293 floppy->failed_pc = NULL;
294 /* Why does this happen? */
297 if (!blk_special_request(rq)) {
298 /* our real local end request function */
299 ide_end_request(drive, uptodate, nsecs);
303 /* fixme: need to move this local also */
304 ide_end_drive_cmd(drive, 0, 0);
308 static void ide_floppy_io_buffers(ide_drive_t *drive, idefloppy_pc_t *pc,
309 unsigned int bcount, int direction)
311 struct request *rq = pc->rq;
312 struct req_iterator iter;
313 struct bio_vec *bvec;
318 rq_for_each_segment(bvec, rq, iter) {
322 count = min(bvec->bv_len, bcount);
324 data = bvec_kmap_irq(bvec, &flags);
326 drive->hwif->atapi_output_bytes(drive, data, count);
328 drive->hwif->atapi_input_bytes(drive, data, count);
329 bvec_kunmap_irq(data, &flags);
332 pc->b_count += count;
336 idefloppy_do_end_request(drive, 1, done >> 9);
339 printk(KERN_ERR "%s: leftover data in %s, bcount == %d\n",
340 drive->name, __func__, bcount);
342 idefloppy_write_zeros(drive, bcount);
344 idefloppy_discard_data(drive, bcount);
349 static void idefloppy_update_buffers(ide_drive_t *drive, idefloppy_pc_t *pc)
351 struct request *rq = pc->rq;
352 struct bio *bio = rq->bio;
354 while ((bio = rq->bio) != NULL)
355 idefloppy_do_end_request(drive, 1, 0);
359 * Generate a new packet command request in front of the request queue, before
360 * the current request so that it will be processed immediately, on the next
361 * pass through the driver.
363 static void idefloppy_queue_pc_head(ide_drive_t *drive, idefloppy_pc_t *pc,
366 struct ide_floppy_obj *floppy = drive->driver_data;
368 ide_init_drive_cmd(rq);
369 rq->buffer = (char *) pc;
370 rq->cmd_type = REQ_TYPE_SPECIAL;
371 rq->rq_disk = floppy->disk;
372 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
375 static idefloppy_pc_t *idefloppy_next_pc_storage(ide_drive_t *drive)
377 idefloppy_floppy_t *floppy = drive->driver_data;
379 if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
380 floppy->pc_stack_index = 0;
381 return (&floppy->pc_stack[floppy->pc_stack_index++]);
384 static struct request *idefloppy_next_rq_storage(ide_drive_t *drive)
386 idefloppy_floppy_t *floppy = drive->driver_data;
388 if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
389 floppy->rq_stack_index = 0;
390 return (&floppy->rq_stack[floppy->rq_stack_index++]);
393 static void idefloppy_request_sense_callback(ide_drive_t *drive)
395 idefloppy_floppy_t *floppy = drive->driver_data;
396 u8 *buf = floppy->pc->buffer;
398 debug_log("Reached %s\n", __func__);
400 if (!floppy->pc->error) {
401 floppy->sense_key = buf[2] & 0x0F;
402 floppy->asc = buf[12];
403 floppy->ascq = buf[13];
404 floppy->progress_indication = buf[15] & 0x80 ?
405 (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
407 if (floppy->failed_pc)
408 debug_log("pc = %x, sense key = %x, asc = %x,"
410 floppy->failed_pc->c[0],
415 debug_log("sense key = %x, asc = %x, ascq = %x\n",
421 idefloppy_do_end_request(drive, 1, 0);
423 printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting"
425 idefloppy_do_end_request(drive, 0, 0);
429 /* General packet command callback function. */
430 static void idefloppy_pc_callback(ide_drive_t *drive)
432 idefloppy_floppy_t *floppy = drive->driver_data;
434 debug_log("Reached %s\n", __func__);
436 idefloppy_do_end_request(drive, floppy->pc->error ? 0 : 1, 0);
439 static void idefloppy_init_pc(idefloppy_pc_t *pc)
441 memset(pc->c, 0, 12);
444 pc->request_transfer = 0;
445 pc->buffer = pc->pc_buffer;
446 pc->buffer_size = IDEFLOPPY_PC_BUFFER_SIZE;
447 pc->callback = &idefloppy_pc_callback;
450 static void idefloppy_create_request_sense_cmd(idefloppy_pc_t *pc)
452 idefloppy_init_pc(pc);
453 pc->c[0] = GPCMD_REQUEST_SENSE;
455 pc->request_transfer = 18;
456 pc->callback = &idefloppy_request_sense_callback;
460 * Called when an error was detected during the last packet command. We queue a
461 * request sense packet command in the head of the request list.
463 static void idefloppy_retry_pc(ide_drive_t *drive)
468 (void)ide_read_error(drive);
469 pc = idefloppy_next_pc_storage(drive);
470 rq = idefloppy_next_rq_storage(drive);
471 idefloppy_create_request_sense_cmd(pc);
472 idefloppy_queue_pc_head(drive, pc, rq);
475 /* The usual interrupt handler called during a packet command. */
476 static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
478 idefloppy_floppy_t *floppy = drive->driver_data;
479 ide_hwif_t *hwif = drive->hwif;
480 idefloppy_pc_t *pc = floppy->pc;
481 struct request *rq = pc->rq;
482 xfer_func_t *xferfunc;
488 debug_log("Reached %s interrupt handler\n", __func__);
490 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
491 dma_error = hwif->ide_dma_end(drive);
493 printk(KERN_ERR "%s: DMA %s error\n", drive->name,
494 rq_data_dir(rq) ? "write" : "read");
495 pc->flags |= PC_FLAG_DMA_ERROR;
497 pc->actually_transferred = pc->request_transfer;
498 idefloppy_update_buffers(drive, pc);
500 debug_log("DMA finished\n");
503 /* Clear the interrupt */
504 stat = ide_read_status(drive);
506 /* No more interrupts */
507 if ((stat & DRQ_STAT) == 0) {
508 debug_log("Packet command completed, %d bytes transferred\n",
509 pc->actually_transferred);
510 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
512 local_irq_enable_in_hardirq();
514 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
516 debug_log("%s: I/O error\n", drive->name);
518 if (pc->c[0] == GPCMD_REQUEST_SENSE) {
519 printk(KERN_ERR "ide-floppy: I/O error in "
520 "request sense command\n");
521 return ide_do_reset(drive);
523 /* Retry operation */
524 idefloppy_retry_pc(drive);
525 /* queued, but not started */
529 if (floppy->failed_pc == pc)
530 floppy->failed_pc = NULL;
531 /* Command finished - Call the callback function */
536 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
537 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
538 printk(KERN_ERR "ide-floppy: The floppy wants to issue "
539 "more interrupts in DMA mode\n");
541 return ide_do_reset(drive);
544 /* Get the number of bytes to transfer */
545 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
546 hwif->INB(IDE_BCOUNTL_REG);
547 /* on this interrupt */
548 ireason = hwif->INB(IDE_IREASON_REG);
551 printk(KERN_ERR "ide-floppy: CoD != 0 in %s\n", __func__);
552 return ide_do_reset(drive);
554 if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
555 /* Hopefully, we will never get here */
556 printk(KERN_ERR "ide-floppy: We wanted to %s, ",
557 (ireason & IO) ? "Write" : "Read");
558 printk(KERN_ERR "but the floppy wants us to %s !\n",
559 (ireason & IO) ? "Read" : "Write");
560 return ide_do_reset(drive);
562 if (!(pc->flags & PC_FLAG_WRITING)) {
563 /* Reading - Check that we have enough space */
564 temp = pc->actually_transferred + bcount;
565 if (temp > pc->request_transfer) {
566 if (temp > pc->buffer_size) {
567 printk(KERN_ERR "ide-floppy: The floppy wants "
568 "to send us more data than expected "
569 "- discarding data\n");
570 idefloppy_discard_data(drive, bcount);
572 ide_set_handler(drive,
578 debug_log("The floppy wants to send us more data than"
579 " expected - allowing transfer\n");
582 if (pc->flags & PC_FLAG_WRITING)
583 xferfunc = hwif->atapi_output_bytes;
585 xferfunc = hwif->atapi_input_bytes;
588 xferfunc(drive, pc->current_position, bcount);
590 ide_floppy_io_buffers(drive, pc, bcount,
591 !!(pc->flags & PC_FLAG_WRITING));
593 /* Update the current position */
594 pc->actually_transferred += bcount;
595 pc->current_position += bcount;
597 /* And set the interrupt handler again */
598 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
603 * This is the original routine that did the packet transfer.
604 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
605 * for that drive below. The algorithm is chosen based on drive type
607 static ide_startstop_t idefloppy_transfer_pc(ide_drive_t *drive)
609 ide_startstop_t startstop;
610 idefloppy_floppy_t *floppy = drive->driver_data;
613 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
614 printk(KERN_ERR "ide-floppy: Strange, packet command "
615 "initiated yet DRQ isn't asserted\n");
618 ireason = drive->hwif->INB(IDE_IREASON_REG);
619 if ((ireason & CD) == 0 || (ireason & IO)) {
620 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
621 "issuing a packet command\n");
622 return ide_do_reset(drive);
625 /* Set the interrupt routine */
626 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
627 /* Send the actual packet */
628 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
634 * What we have here is a classic case of a top half / bottom half interrupt
635 * service routine. In interrupt mode, the device sends an interrupt to signal
636 * that it is ready to receive a packet. However, we need to delay about 2-3
637 * ticks before issuing the packet or we gets in trouble.
639 * So, follow carefully. transfer_pc1 is called as an interrupt (or directly).
640 * In either case, when the device says it's ready for a packet, we schedule
641 * the packet transfer to occur about 2-3 ticks later in transfer_pc2.
643 static int idefloppy_transfer_pc2(ide_drive_t *drive)
645 idefloppy_floppy_t *floppy = drive->driver_data;
647 /* Send the actual packet */
648 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
649 /* Timeout for the packet command */
650 return IDEFLOPPY_WAIT_CMD;
653 static ide_startstop_t idefloppy_transfer_pc1(ide_drive_t *drive)
655 idefloppy_floppy_t *floppy = drive->driver_data;
656 ide_startstop_t startstop;
659 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
660 printk(KERN_ERR "ide-floppy: Strange, packet command "
661 "initiated yet DRQ isn't asserted\n");
664 ireason = drive->hwif->INB(IDE_IREASON_REG);
665 if ((ireason & CD) == 0 || (ireason & IO)) {
666 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
667 "while issuing a packet command\n");
668 return ide_do_reset(drive);
671 * The following delay solves a problem with ATAPI Zip 100 drives
672 * where the Busy flag was apparently being deasserted before the
673 * unit was ready to receive data. This was happening on a
674 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
675 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
676 * used until after the packet is moved in about 50 msec.
679 ide_set_handler(drive, &idefloppy_pc_intr, floppy->ticks,
680 &idefloppy_transfer_pc2);
684 static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
687 /* supress error messages resulting from Medium not present */
688 if (floppy->sense_key == 0x02 &&
689 floppy->asc == 0x3a &&
690 floppy->ascq == 0x00)
693 printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
694 "asc = %2x, ascq = %2x\n",
695 floppy->drive->name, pc->c[0], floppy->sense_key,
696 floppy->asc, floppy->ascq);
700 static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive,
703 idefloppy_floppy_t *floppy = drive->driver_data;
704 ide_hwif_t *hwif = drive->hwif;
705 ide_handler_t *pkt_xfer_routine;
709 if (floppy->failed_pc == NULL &&
710 pc->c[0] != GPCMD_REQUEST_SENSE)
711 floppy->failed_pc = pc;
712 /* Set the current packet command */
715 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
716 if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
717 ide_floppy_report_error(floppy, pc);
719 pc->error = IDEFLOPPY_ERROR_GENERAL;
721 floppy->failed_pc = NULL;
726 debug_log("Retry number - %d\n", pc->retries);
729 /* We haven't transferred any data yet */
730 pc->actually_transferred = 0;
731 pc->current_position = pc->buffer;
732 bcount = min(pc->request_transfer, 63 * 1024);
734 if (pc->flags & PC_FLAG_DMA_ERROR) {
735 pc->flags &= ~PC_FLAG_DMA_ERROR;
740 if ((pc->flags & PC_FLAG_DMA_RECOMMENDED) && drive->using_dma)
741 dma = !hwif->dma_setup(drive);
743 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
744 IDE_TFLAG_OUT_DEVICE, bcount, dma);
747 /* Begin DMA, if necessary */
748 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
749 hwif->dma_start(drive);
752 /* Can we transfer the packet when we get the interrupt or wait? */
753 if (floppy->flags & IDEFLOPPY_FLAG_ZIP_DRIVE) {
755 pkt_xfer_routine = &idefloppy_transfer_pc1;
758 pkt_xfer_routine = &idefloppy_transfer_pc;
761 if (floppy->flags & IDEFLOPPY_FLAG_DRQ_INTERRUPT) {
762 /* Issue the packet command */
763 ide_execute_command(drive, WIN_PACKETCMD,
769 /* Issue the packet command */
770 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
771 return (*pkt_xfer_routine) (drive);
775 static void idefloppy_rw_callback(ide_drive_t *drive)
777 debug_log("Reached %s\n", __func__);
779 idefloppy_do_end_request(drive, 1, 0);
783 static void idefloppy_create_prevent_cmd(idefloppy_pc_t *pc, int prevent)
785 debug_log("creating prevent removal command, prevent = %d\n", prevent);
787 idefloppy_init_pc(pc);
788 pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
792 static void idefloppy_create_read_capacity_cmd(idefloppy_pc_t *pc)
794 idefloppy_init_pc(pc);
795 pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
798 pc->request_transfer = 255;
801 static void idefloppy_create_format_unit_cmd(idefloppy_pc_t *pc, int b, int l,
804 idefloppy_init_pc(pc);
805 pc->c[0] = GPCMD_FORMAT_UNIT;
808 memset(pc->buffer, 0, 12);
809 pc->buffer[1] = 0xA2;
810 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
812 if (flags & 1) /* Verify bit on... */
813 pc->buffer[1] ^= 0x20; /* ... turn off DCRT bit */
816 put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buffer[4]));
817 put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buffer[8]));
818 pc->buffer_size = 12;
819 pc->flags |= PC_FLAG_WRITING;
822 /* A mode sense command is used to "sense" floppy parameters. */
823 static void idefloppy_create_mode_sense_cmd(idefloppy_pc_t *pc, u8 page_code,
826 u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
828 idefloppy_init_pc(pc);
829 pc->c[0] = GPCMD_MODE_SENSE_10;
831 pc->c[2] = page_code + (type << 6);
834 case IDEFLOPPY_CAPABILITIES_PAGE:
837 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
841 printk(KERN_ERR "ide-floppy: unsupported page code "
842 "in create_mode_sense_cmd\n");
844 put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
845 pc->request_transfer = length;
848 static void idefloppy_create_start_stop_cmd(idefloppy_pc_t *pc, int start)
850 idefloppy_init_pc(pc);
851 pc->c[0] = GPCMD_START_STOP_UNIT;
855 static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc)
857 idefloppy_init_pc(pc);
858 pc->c[0] = GPCMD_TEST_UNIT_READY;
861 static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
862 idefloppy_pc_t *pc, struct request *rq,
863 unsigned long sector)
865 int block = sector / floppy->bs_factor;
866 int blocks = rq->nr_sectors / floppy->bs_factor;
867 int cmd = rq_data_dir(rq);
869 debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
872 idefloppy_init_pc(pc);
873 pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
874 put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
875 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
877 pc->callback = &idefloppy_rw_callback;
879 pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
880 if (rq->cmd_flags & REQ_RW)
881 pc->flags |= PC_FLAG_WRITING;
883 pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
884 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
887 static void idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy,
888 idefloppy_pc_t *pc, struct request *rq)
890 idefloppy_init_pc(pc);
891 pc->callback = &idefloppy_rw_callback;
892 memcpy(pc->c, rq->cmd, sizeof(pc->c));
894 pc->b_count = rq->data_len;
895 if (rq->data_len && rq_data_dir(rq) == WRITE)
896 pc->flags |= PC_FLAG_WRITING;
897 pc->buffer = rq->data;
899 pc->flags |= PC_FLAG_DMA_RECOMMENDED;
901 * possibly problematic, doesn't look like ide-floppy correctly
902 * handled scattered requests if dma fails...
904 pc->request_transfer = pc->buffer_size = rq->data_len;
907 static ide_startstop_t idefloppy_do_request(ide_drive_t *drive,
908 struct request *rq, sector_t block_s)
910 idefloppy_floppy_t *floppy = drive->driver_data;
912 unsigned long block = (unsigned long)block_s;
914 debug_log("dev: %s, cmd_type: %x, errors: %d\n",
915 rq->rq_disk ? rq->rq_disk->disk_name : "?",
916 rq->cmd_type, rq->errors);
917 debug_log("sector: %ld, nr_sectors: %ld, "
918 "current_nr_sectors: %d\n", (long)rq->sector,
919 rq->nr_sectors, rq->current_nr_sectors);
921 if (rq->errors >= ERROR_MAX) {
922 if (floppy->failed_pc)
923 ide_floppy_report_error(floppy, floppy->failed_pc);
925 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
927 idefloppy_do_end_request(drive, 0, 0);
930 if (blk_fs_request(rq)) {
931 if (((long)rq->sector % floppy->bs_factor) ||
932 (rq->nr_sectors % floppy->bs_factor)) {
933 printk(KERN_ERR "%s: unsupported r/w request size\n",
935 idefloppy_do_end_request(drive, 0, 0);
938 pc = idefloppy_next_pc_storage(drive);
939 idefloppy_create_rw_cmd(floppy, pc, rq, block);
940 } else if (blk_special_request(rq)) {
941 pc = (idefloppy_pc_t *) rq->buffer;
942 } else if (blk_pc_request(rq)) {
943 pc = idefloppy_next_pc_storage(drive);
944 idefloppy_blockpc_cmd(floppy, pc, rq);
946 blk_dump_rq_flags(rq,
947 "ide-floppy: unsupported command in queue");
948 idefloppy_do_end_request(drive, 0, 0);
953 return idefloppy_issue_pc(drive, pc);
957 * Add a special packet command request to the tail of the request queue,
958 * and wait for it to be serviced.
960 static int idefloppy_queue_pc_tail(ide_drive_t *drive, idefloppy_pc_t *pc)
962 struct ide_floppy_obj *floppy = drive->driver_data;
965 ide_init_drive_cmd(&rq);
966 rq.buffer = (char *) pc;
967 rq.cmd_type = REQ_TYPE_SPECIAL;
968 rq.rq_disk = floppy->disk;
970 return ide_do_drive_cmd(drive, &rq, ide_wait);
974 * Look at the flexible disk page parameters. We ignore the CHS capacity
975 * parameters and use the LBA parameters instead.
977 static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
979 idefloppy_floppy_t *floppy = drive->driver_data;
982 int capacity, lba_capacity;
983 u16 transfer_rate, sector_size, cyls, rpm;
986 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE,
989 if (idefloppy_queue_pc_tail(drive, &pc)) {
990 printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
994 floppy->wp = !!(pc.buffer[3] & 0x80);
995 set_disk_ro(floppy->disk, floppy->wp);
996 page = &pc.buffer[8];
998 transfer_rate = be16_to_cpu(*(u16 *)&pc.buffer[8 + 2]);
999 sector_size = be16_to_cpu(*(u16 *)&pc.buffer[8 + 6]);
1000 cyls = be16_to_cpu(*(u16 *)&pc.buffer[8 + 8]);
1001 rpm = be16_to_cpu(*(u16 *)&pc.buffer[8 + 28]);
1002 heads = pc.buffer[8 + 4];
1003 sectors = pc.buffer[8 + 5];
1005 capacity = cyls * heads * sectors * sector_size;
1007 if (memcmp(page, &floppy->flexible_disk_page, 32))
1008 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
1009 "%d sector size, %d rpm\n",
1010 drive->name, capacity / 1024, cyls, heads,
1011 sectors, transfer_rate / 8, sector_size, rpm);
1013 memcpy(&floppy->flexible_disk_page, page, 32);
1014 drive->bios_cyl = cyls;
1015 drive->bios_head = heads;
1016 drive->bios_sect = sectors;
1017 lba_capacity = floppy->blocks * floppy->block_size;
1019 if (capacity < lba_capacity) {
1020 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
1021 "bytes, but the drive only handles %d\n",
1022 drive->name, lba_capacity, capacity);
1023 floppy->blocks = floppy->block_size ?
1024 capacity / floppy->block_size : 0;
1029 static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
1031 idefloppy_floppy_t *floppy = drive->driver_data;
1035 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
1036 MODE_SENSE_CURRENT);
1038 pc.flags |= PC_FLAG_SUPPRESS_ERROR;
1039 if (idefloppy_queue_pc_tail(drive, &pc))
1042 floppy->srfp = pc.buffer[8 + 2] & 0x40;
1047 * Determine if a media is present in the floppy drive, and if so, its LBA
1050 static int ide_floppy_get_capacity(ide_drive_t *drive)
1052 idefloppy_floppy_t *floppy = drive->driver_data;
1055 u8 header_len, desc_cnt;
1056 int i, rc = 1, blocks, length;
1058 drive->bios_cyl = 0;
1059 drive->bios_head = drive->bios_sect = 0;
1061 floppy->bs_factor = 1;
1062 set_capacity(floppy->disk, 0);
1064 idefloppy_create_read_capacity_cmd(&pc);
1065 if (idefloppy_queue_pc_tail(drive, &pc)) {
1066 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1069 header_len = pc.buffer[3];
1070 cap_desc = &pc.buffer[4];
1071 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
1073 for (i = 0; i < desc_cnt; i++) {
1074 unsigned int desc_start = 4 + i*8;
1076 blocks = be32_to_cpu(*(u32 *)&pc.buffer[desc_start]);
1077 length = be16_to_cpu(*(u16 *)&pc.buffer[desc_start + 6]);
1079 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
1080 i, blocks * length / 1024, blocks, length);
1085 * the code below is valid only for the 1st descriptor, ie i=0
1088 switch (pc.buffer[desc_start + 4] & 0x03) {
1089 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1090 case CAPACITY_UNFORMATTED:
1091 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE))
1093 * If it is not a clik drive, break out
1094 * (maintains previous driver behaviour)
1097 case CAPACITY_CURRENT:
1098 /* Normal Zip/LS-120 disks */
1099 if (memcmp(cap_desc, &floppy->cap_desc, 8))
1100 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
1101 "sector size\n", drive->name,
1102 blocks * length / 1024, blocks, length);
1103 memcpy(&floppy->cap_desc, cap_desc, 8);
1105 if (!length || length % 512) {
1106 printk(KERN_NOTICE "%s: %d bytes block size "
1107 "not supported\n", drive->name, length);
1109 floppy->blocks = blocks;
1110 floppy->block_size = length;
1111 floppy->bs_factor = length / 512;
1112 if (floppy->bs_factor != 1)
1113 printk(KERN_NOTICE "%s: warning: non "
1114 "512 bytes block size not "
1115 "fully supported\n",
1120 case CAPACITY_NO_CARTRIDGE:
1122 * This is a KERN_ERR so it appears on screen
1123 * for the user to see
1125 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
1127 case CAPACITY_INVALID:
1128 printk(KERN_ERR "%s: Invalid capacity for disk "
1129 "in drive\n", drive->name);
1132 debug_log("Descriptor 0 Code: %d\n",
1133 pc.buffer[desc_start + 4] & 0x03);
1136 /* Clik! disk does not support get_flexible_disk_page */
1137 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE))
1138 (void) ide_floppy_get_flexible_disk_page(drive);
1140 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
1145 * Obtain the list of formattable capacities.
1146 * Very similar to ide_floppy_get_capacity, except that we push the capacity
1147 * descriptors to userland, instead of our own structures.
1149 * Userland gives us the following structure:
1151 * struct idefloppy_format_capacities {
1159 * userland initializes nformats to the number of allocated formats[] records.
1160 * On exit we set nformats to the number of records we've actually initialized.
1163 static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
1166 u8 header_len, desc_cnt;
1167 int i, blocks, length, u_array_size, u_index;
1170 if (get_user(u_array_size, arg))
1173 if (u_array_size <= 0)
1176 idefloppy_create_read_capacity_cmd(&pc);
1177 if (idefloppy_queue_pc_tail(drive, &pc)) {
1178 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1181 header_len = pc.buffer[3];
1182 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
1188 * We always skip the first capacity descriptor. That's the current
1189 * capacity. We are interested in the remaining descriptors, the
1190 * formattable capacities.
1192 for (i = 1; i < desc_cnt; i++) {
1193 unsigned int desc_start = 4 + i*8;
1195 if (u_index >= u_array_size)
1196 break; /* User-supplied buffer too small */
1198 blocks = be32_to_cpu(*(u32 *)&pc.buffer[desc_start]);
1199 length = be16_to_cpu(*(u16 *)&pc.buffer[desc_start + 6]);
1201 if (put_user(blocks, argp))
1205 if (put_user(length, argp))
1212 if (put_user(u_index, arg))
1218 * Get ATAPI_FORMAT_UNIT progress indication.
1220 * Userland gives a pointer to an int. The int is set to a progress
1221 * indicator 0-65536, with 65536=100%.
1223 * If the drive does not support format progress indication, we just check
1224 * the dsc bit, and return either 0 or 65536.
1227 static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
1229 idefloppy_floppy_t *floppy = drive->driver_data;
1231 int progress_indication = 0x10000;
1234 idefloppy_create_request_sense_cmd(&pc);
1235 if (idefloppy_queue_pc_tail(drive, &pc))
1238 if (floppy->sense_key == 2 &&
1241 progress_indication = floppy->progress_indication;
1243 /* Else assume format_unit has finished, and we're at 0x10000 */
1245 unsigned long flags;
1248 local_irq_save(flags);
1249 stat = ide_read_status(drive);
1250 local_irq_restore(flags);
1252 progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
1254 if (put_user(progress_indication, arg))
1260 static sector_t idefloppy_capacity(ide_drive_t *drive)
1262 idefloppy_floppy_t *floppy = drive->driver_data;
1263 unsigned long capacity = floppy->blocks * floppy->bs_factor;
1269 * Check whether we can support a drive, based on the ATAPI IDENTIFY command
1272 static int idefloppy_identify_device(ide_drive_t *drive, struct hd_driveid *id)
1274 struct idefloppy_id_gcw gcw;
1276 *((u16 *) &gcw) = id->config;
1279 /* kludge for Apple PowerBook internal zip */
1280 if ((gcw.device_type == 5) &&
1281 !strstr(id->model, "CD-ROM") &&
1282 strstr(id->model, "ZIP"))
1283 gcw.device_type = 0;
1286 if (gcw.protocol != 2)
1287 printk(KERN_ERR "ide-floppy: Protocol (0x%02x) is not ATAPI\n",
1289 else if (gcw.device_type != 0)
1290 printk(KERN_ERR "ide-floppy: Device type (0x%02x) is not set "
1291 "to floppy\n", gcw.device_type);
1292 else if (!gcw.removable)
1293 printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
1294 else if (gcw.drq_type == 3) {
1295 printk(KERN_ERR "ide-floppy: Sorry, DRQ type (0x%02x) not "
1296 "supported\n", gcw.drq_type);
1297 } else if (gcw.packet_size != 0) {
1298 printk(KERN_ERR "ide-floppy: Packet size (0x%02x) is not 12 "
1299 "bytes long\n", gcw.packet_size);
1305 #ifdef CONFIG_IDE_PROC_FS
1306 static void idefloppy_add_settings(ide_drive_t *drive)
1308 idefloppy_floppy_t *floppy = drive->driver_data;
1310 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1,
1311 &drive->bios_cyl, NULL);
1312 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1,
1313 &drive->bios_head, NULL);
1314 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1,
1315 &drive->bios_sect, NULL);
1316 ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1,
1317 &floppy->ticks, NULL);
1320 static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
1323 static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
1325 struct idefloppy_id_gcw gcw;
1327 *((u16 *) &gcw) = drive->id->config;
1328 floppy->pc = floppy->pc_stack;
1329 if (gcw.drq_type == 1)
1330 floppy->flags |= IDEFLOPPY_FLAG_DRQ_INTERRUPT;
1332 * We used to check revisions here. At this point however I'm giving up.
1333 * Just assume they are all broken, its easier.
1335 * The actual reason for the workarounds was likely a driver bug after
1336 * all rather than a firmware bug, and the workaround below used to hide
1337 * it. It should be fixed as of version 1.9, but to be on the safe side
1338 * we'll leave the limitation below for the 2.2.x tree.
1340 if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
1341 floppy->flags |= IDEFLOPPY_FLAG_ZIP_DRIVE;
1342 /* This value will be visible in the /proc/ide/hdx/settings */
1343 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1344 blk_queue_max_sectors(drive->queue, 64);
1348 * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
1349 * nasty clicking noises without it, so please don't remove this.
1351 if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
1352 blk_queue_max_sectors(drive->queue, 64);
1353 floppy->flags |= IDEFLOPPY_FLAG_CLIK_DRIVE;
1356 (void) ide_floppy_get_capacity(drive);
1357 idefloppy_add_settings(drive);
1360 static void ide_floppy_remove(ide_drive_t *drive)
1362 idefloppy_floppy_t *floppy = drive->driver_data;
1363 struct gendisk *g = floppy->disk;
1365 ide_proc_unregister_driver(drive, floppy->driver);
1369 ide_floppy_put(floppy);
1372 static void idefloppy_cleanup_obj(struct kref *kref)
1374 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1375 ide_drive_t *drive = floppy->drive;
1376 struct gendisk *g = floppy->disk;
1378 drive->driver_data = NULL;
1379 g->private_data = NULL;
1384 #ifdef CONFIG_IDE_PROC_FS
1385 static int proc_idefloppy_read_capacity(char *page, char **start, off_t off,
1386 int count, int *eof, void *data)
1388 ide_drive_t*drive = (ide_drive_t *)data;
1391 len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive));
1392 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
1395 static ide_proc_entry_t idefloppy_proc[] = {
1396 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
1397 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1398 { NULL, 0, NULL, NULL }
1400 #endif /* CONFIG_IDE_PROC_FS */
1402 static int ide_floppy_probe(ide_drive_t *);
1404 static ide_driver_t idefloppy_driver = {
1406 .owner = THIS_MODULE,
1407 .name = "ide-floppy",
1408 .bus = &ide_bus_type,
1410 .probe = ide_floppy_probe,
1411 .remove = ide_floppy_remove,
1412 .version = IDEFLOPPY_VERSION,
1413 .media = ide_floppy,
1414 .supports_dsc_overlap = 0,
1415 .do_request = idefloppy_do_request,
1416 .end_request = idefloppy_do_end_request,
1417 .error = __ide_error,
1418 .abort = __ide_abort,
1419 #ifdef CONFIG_IDE_PROC_FS
1420 .proc = idefloppy_proc,
1424 static int idefloppy_open(struct inode *inode, struct file *filp)
1426 struct gendisk *disk = inode->i_bdev->bd_disk;
1427 struct ide_floppy_obj *floppy;
1432 debug_log("Reached %s\n", __func__);
1434 floppy = ide_floppy_get(disk);
1438 drive = floppy->drive;
1442 if (floppy->openers == 1) {
1443 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
1446 idefloppy_create_test_unit_ready_cmd(&pc);
1447 if (idefloppy_queue_pc_tail(drive, &pc)) {
1448 idefloppy_create_start_stop_cmd(&pc, 1);
1449 (void) idefloppy_queue_pc_tail(drive, &pc);
1452 if (ide_floppy_get_capacity(drive)
1453 && (filp->f_flags & O_NDELAY) == 0
1455 * Allow O_NDELAY to open a drive without a disk, or with an
1456 * unreadable disk, so that we can get the format capacity
1457 * of the drive or begin the format - Sam
1461 goto out_put_floppy;
1464 if (floppy->wp && (filp->f_mode & 2)) {
1466 goto out_put_floppy;
1468 floppy->flags |= IDEFLOPPY_FLAG_MEDIA_CHANGED;
1469 /* IOMEGA Clik! drives do not support lock/unlock commands */
1470 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
1471 idefloppy_create_prevent_cmd(&pc, 1);
1472 (void) idefloppy_queue_pc_tail(drive, &pc);
1474 check_disk_change(inode->i_bdev);
1475 } else if (floppy->flags & IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS) {
1477 goto out_put_floppy;
1483 ide_floppy_put(floppy);
1487 static int idefloppy_release(struct inode *inode, struct file *filp)
1489 struct gendisk *disk = inode->i_bdev->bd_disk;
1490 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1491 ide_drive_t *drive = floppy->drive;
1494 debug_log("Reached %s\n", __func__);
1496 if (floppy->openers == 1) {
1497 /* IOMEGA Clik! drives do not support lock/unlock commands */
1498 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
1499 idefloppy_create_prevent_cmd(&pc, 0);
1500 (void) idefloppy_queue_pc_tail(drive, &pc);
1503 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
1508 ide_floppy_put(floppy);
1513 static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1515 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1516 ide_drive_t *drive = floppy->drive;
1518 geo->heads = drive->bios_head;
1519 geo->sectors = drive->bios_sect;
1520 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1524 static int ide_floppy_lockdoor(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc,
1525 unsigned long arg, unsigned int cmd)
1527 if (floppy->openers > 1)
1530 /* The IOMEGA Clik! Drive doesn't support this command -
1531 * no room for an eject mechanism */
1532 if (!(floppy->flags & IDEFLOPPY_FLAG_CLIK_DRIVE)) {
1533 int prevent = arg ? 1 : 0;
1535 if (cmd == CDROMEJECT)
1538 idefloppy_create_prevent_cmd(pc, prevent);
1539 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1542 if (cmd == CDROMEJECT) {
1543 idefloppy_create_start_stop_cmd(pc, 2);
1544 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1550 static int ide_floppy_format_unit(idefloppy_floppy_t *floppy,
1553 int blocks, length, flags, err = 0;
1556 if (floppy->openers > 1) {
1557 /* Don't format if someone is using the disk */
1558 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
1562 floppy->flags |= IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
1565 * Send ATAPI_FORMAT_UNIT to the drive.
1567 * Userland gives us the following structure:
1569 * struct idefloppy_format_command {
1575 * flags is a bitmask, currently, the only defined flag is:
1577 * 0x01 - verify media after format.
1579 if (get_user(blocks, arg) ||
1580 get_user(length, arg+1) ||
1581 get_user(flags, arg+2)) {
1586 (void) idefloppy_get_sfrp_bit(floppy->drive);
1587 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1589 if (idefloppy_queue_pc_tail(floppy->drive, &pc))
1594 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
1599 static int idefloppy_ioctl(struct inode *inode, struct file *file,
1600 unsigned int cmd, unsigned long arg)
1602 struct block_device *bdev = inode->i_bdev;
1603 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1604 ide_drive_t *drive = floppy->drive;
1606 void __user *argp = (void __user *)arg;
1612 case CDROM_LOCKDOOR:
1613 return ide_floppy_lockdoor(floppy, &pc, arg, cmd);
1614 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1616 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
1617 return ide_floppy_get_format_capacities(drive, argp);
1618 case IDEFLOPPY_IOCTL_FORMAT_START:
1619 if (!(file->f_mode & 2))
1622 return ide_floppy_format_unit(floppy, (int __user *)arg);
1623 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1624 return idefloppy_get_format_progress(drive, argp);
1628 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1629 * and CDROM_SEND_PACKET (legacy) ioctls
1631 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1632 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1633 bdev->bd_disk, cmd, argp);
1638 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1643 static int idefloppy_media_changed(struct gendisk *disk)
1645 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1646 ide_drive_t *drive = floppy->drive;
1649 /* do not scan partitions twice if this is a removable device */
1650 if (drive->attach) {
1654 ret = !!(floppy->flags & IDEFLOPPY_FLAG_MEDIA_CHANGED);
1655 floppy->flags &= ~IDEFLOPPY_FLAG_MEDIA_CHANGED;
1659 static int idefloppy_revalidate_disk(struct gendisk *disk)
1661 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1662 set_capacity(disk, idefloppy_capacity(floppy->drive));
1666 static struct block_device_operations idefloppy_ops = {
1667 .owner = THIS_MODULE,
1668 .open = idefloppy_open,
1669 .release = idefloppy_release,
1670 .ioctl = idefloppy_ioctl,
1671 .getgeo = idefloppy_getgeo,
1672 .media_changed = idefloppy_media_changed,
1673 .revalidate_disk= idefloppy_revalidate_disk
1676 static int ide_floppy_probe(ide_drive_t *drive)
1678 idefloppy_floppy_t *floppy;
1681 if (!strstr("ide-floppy", drive->driver_req))
1683 if (!drive->present)
1685 if (drive->media != ide_floppy)
1687 if (!idefloppy_identify_device(drive, drive->id)) {
1688 printk(KERN_ERR "ide-floppy: %s: not supported by this version"
1689 " of ide-floppy\n", drive->name);
1693 printk(KERN_INFO "ide-floppy: passing drive %s to ide-scsi"
1694 " emulation.\n", drive->name);
1697 floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
1699 printk(KERN_ERR "ide-floppy: %s: Can't allocate a floppy"
1700 " structure\n", drive->name);
1704 g = alloc_disk(1 << PARTN_BITS);
1706 goto out_free_floppy;
1708 ide_init_disk(g, drive);
1710 ide_proc_register_driver(drive, &idefloppy_driver);
1712 kref_init(&floppy->kref);
1714 floppy->drive = drive;
1715 floppy->driver = &idefloppy_driver;
1718 g->private_data = &floppy->driver;
1720 drive->driver_data = floppy;
1722 idefloppy_setup(drive, floppy);
1724 g->minors = 1 << PARTN_BITS;
1725 g->driverfs_dev = &drive->gendev;
1726 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1727 g->fops = &idefloppy_ops;
1738 static void __exit idefloppy_exit(void)
1740 driver_unregister(&idefloppy_driver.gen_driver);
1743 static int __init idefloppy_init(void)
1745 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
1746 return driver_register(&idefloppy_driver.gen_driver);
1749 MODULE_ALIAS("ide:*m-floppy*");
1750 module_init(idefloppy_init);
1751 module_exit(idefloppy_exit);
1752 MODULE_LICENSE("GPL");
1753 MODULE_DESCRIPTION("ATAPI FLOPPY Driver");