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
10 * The driver currently doesn't have any fancy features, just the bare
11 * minimum read/write support.
13 * This driver supports the following IDE floppy drives:
15 * LS-120/240 SuperDisk
17 * Iomega PC Card Clik!/PocketZip
19 * For a historical changelog see
20 * Documentation/ide/ChangeLog.ide-floppy.1996-2002
23 #define IDEFLOPPY_VERSION "0.99.newide"
25 #include <linux/module.h>
26 #include <linux/types.h>
27 #include <linux/string.h>
28 #include <linux/kernel.h>
29 #include <linux/delay.h>
30 #include <linux/timer.h>
32 #include <linux/interrupt.h>
33 #include <linux/major.h>
34 #include <linux/errno.h>
35 #include <linux/genhd.h>
36 #include <linux/slab.h>
37 #include <linux/cdrom.h>
38 #include <linux/ide.h>
39 #include <linux/bitops.h>
40 #include <linux/mutex.h>
42 #include <scsi/scsi_ioctl.h>
44 #include <asm/byteorder.h>
45 #include <linux/irq.h>
46 #include <linux/uaccess.h>
48 #include <asm/unaligned.h>
51 * The following are used to debug the driver.
53 #define IDEFLOPPY_DEBUG_LOG 0
54 #define IDEFLOPPY_DEBUG_INFO 0
55 #define IDEFLOPPY_DEBUG_BUGS 1
57 /* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
58 #define IDEFLOPPY_DEBUG( fmt, args... )
60 #if IDEFLOPPY_DEBUG_LOG
61 #define debug_log(fmt, args...) \
62 printk(KERN_INFO "ide-floppy: " fmt, ## args)
64 #define debug_log(fmt, args... ) do {} while(0)
69 * Some drives require a longer irq timeout.
71 #define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
74 * After each failed packet command we issue a request sense command
75 * and retry the packet command IDEFLOPPY_MAX_PC_RETRIES times.
77 #define IDEFLOPPY_MAX_PC_RETRIES 3
80 * With each packet command, we allocate a buffer of
81 * IDEFLOPPY_PC_BUFFER_SIZE bytes.
83 #define IDEFLOPPY_PC_BUFFER_SIZE 256
86 * In various places in the driver, we need to allocate storage
87 * for packet commands and requests, which will remain valid while
88 * we leave the driver to wait for an interrupt or a timeout event.
90 #define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
93 * Our view of a packet command.
95 typedef struct idefloppy_packet_command_s {
96 u8 c[12]; /* Actual packet bytes */
97 int retries; /* On each retry, we increment retries */
98 int error; /* Error code */
99 int request_transfer; /* Bytes to transfer */
100 int actually_transferred; /* Bytes actually transferred */
101 int buffer_size; /* Size of our data buffer */
102 int b_count; /* Missing/Available data on the current buffer */
103 struct request *rq; /* The corresponding request */
104 u8 *buffer; /* Data buffer */
105 u8 *current_position; /* Pointer into the above buffer */
106 void (*callback) (ide_drive_t *); /* Called when this packet command is completed */
107 u8 pc_buffer[IDEFLOPPY_PC_BUFFER_SIZE]; /* Temporary buffer */
108 unsigned long flags; /* Status/Action bit flags: long for set_bit */
112 * Packet command flag bits.
114 #define PC_ABORT 0 /* Set when an error is considered normal - We won't retry */
115 #define PC_DMA_RECOMMENDED 2 /* 1 when we prefer to use DMA if possible */
116 #define PC_DMA_IN_PROGRESS 3 /* 1 while DMA in progress */
117 #define PC_DMA_ERROR 4 /* 1 when encountered problem during DMA */
118 #define PC_WRITING 5 /* Data direction */
120 #define PC_SUPPRESS_ERROR 6 /* Suppress error reporting */
127 u8 length; /* Length of the following descriptors in bytes */
128 } idefloppy_capacity_header_t;
131 u32 blocks; /* Number of blocks */
132 #if defined(__LITTLE_ENDIAN_BITFIELD)
133 unsigned dc :2; /* Descriptor Code */
134 unsigned reserved :6;
135 #elif defined(__BIG_ENDIAN_BITFIELD)
136 unsigned reserved :6;
137 unsigned dc :2; /* Descriptor Code */
139 #error "Bitfield endianness not defined! Check your byteorder.h"
141 u8 length_msb; /* Block Length (MSB)*/
142 u16 length; /* Block Length */
143 } idefloppy_capacity_descriptor_t;
145 #define CAPACITY_INVALID 0x00
146 #define CAPACITY_UNFORMATTED 0x01
147 #define CAPACITY_CURRENT 0x02
148 #define CAPACITY_NO_CARTRIDGE 0x03
151 * Most of our global data which we need to save even as we leave the
152 * driver due to an interrupt or a timer event is stored in a variable
153 * of type idefloppy_floppy_t, defined below.
155 typedef struct ide_floppy_obj {
157 ide_driver_t *driver;
158 struct gendisk *disk;
160 unsigned int openers; /* protected by BKL for now */
162 /* Current packet command */
164 /* Last failed packet command */
165 idefloppy_pc_t *failed_pc;
166 /* Packet command stack */
167 idefloppy_pc_t pc_stack[IDEFLOPPY_PC_STACK];
168 /* Next free packet command storage space */
170 struct request rq_stack[IDEFLOPPY_PC_STACK];
171 /* We implement a circular array */
175 * Last error information
177 u8 sense_key, asc, ascq;
178 /* delay this long before sending packet command */
180 int progress_indication;
186 int blocks, block_size, bs_factor;
187 /* Last format capacity */
188 idefloppy_capacity_descriptor_t capacity;
189 /* Copy of the flexible disk page */
190 u8 flexible_disk_page[32];
193 /* Supports format progress report */
195 /* Status/Action flags */
197 } idefloppy_floppy_t;
199 #define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
202 * Floppy flag bits values.
204 #define IDEFLOPPY_DRQ_INTERRUPT 0 /* DRQ interrupt device */
205 #define IDEFLOPPY_MEDIA_CHANGED 1 /* Media may have changed */
206 #define IDEFLOPPY_USE_READ12 2 /* Use READ12/WRITE12 or READ10/WRITE10 */
207 #define IDEFLOPPY_FORMAT_IN_PROGRESS 3 /* Format in progress */
208 #define IDEFLOPPY_CLIK_DRIVE 4 /* Avoid commands not supported in Clik drive */
209 #define IDEFLOPPY_ZIP_DRIVE 5 /* Requires BH algorithm for packets */
212 * Defines for the mode sense command
214 #define MODE_SENSE_CURRENT 0x00
215 #define MODE_SENSE_CHANGEABLE 0x01
216 #define MODE_SENSE_DEFAULT 0x02
217 #define MODE_SENSE_SAVED 0x03
220 * IOCTLs used in low-level formatting.
223 #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
224 #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
225 #define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
226 #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
229 * Error codes which are returned in rq->errors to the higher part
232 #define IDEFLOPPY_ERROR_GENERAL 101
235 * The following is used to format the general configuration word of
236 * the ATAPI IDENTIFY DEVICE command.
238 struct idefloppy_id_gcw {
239 #if defined(__LITTLE_ENDIAN_BITFIELD)
240 unsigned packet_size :2; /* Packet Size */
241 unsigned reserved234 :3; /* Reserved */
242 unsigned drq_type :2; /* Command packet DRQ type */
243 unsigned removable :1; /* Removable media */
244 unsigned device_type :5; /* Device type */
245 unsigned reserved13 :1; /* Reserved */
246 unsigned protocol :2; /* Protocol type */
247 #elif defined(__BIG_ENDIAN_BITFIELD)
248 unsigned protocol :2; /* Protocol type */
249 unsigned reserved13 :1; /* Reserved */
250 unsigned device_type :5; /* Device type */
251 unsigned removable :1; /* Removable media */
252 unsigned drq_type :2; /* Command packet DRQ type */
253 unsigned reserved234 :3; /* Reserved */
254 unsigned packet_size :2; /* Packet Size */
256 #error "Bitfield endianness not defined! Check your byteorder.h"
261 * Pages of the SELECT SENSE / MODE SENSE packet commands.
262 * See SFF-8070i spec.
264 #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
265 #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
267 static DEFINE_MUTEX(idefloppy_ref_mutex);
269 #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
271 #define ide_floppy_g(disk) \
272 container_of((disk)->private_data, struct ide_floppy_obj, driver)
274 static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
276 struct ide_floppy_obj *floppy = NULL;
278 mutex_lock(&idefloppy_ref_mutex);
279 floppy = ide_floppy_g(disk);
281 kref_get(&floppy->kref);
282 mutex_unlock(&idefloppy_ref_mutex);
286 static void idefloppy_cleanup_obj(struct kref *);
288 static void ide_floppy_put(struct ide_floppy_obj *floppy)
290 mutex_lock(&idefloppy_ref_mutex);
291 kref_put(&floppy->kref, idefloppy_cleanup_obj);
292 mutex_unlock(&idefloppy_ref_mutex);
296 * Too bad. The drive wants to send us data which we are not ready to accept.
297 * Just throw it away.
299 static void idefloppy_discard_data (ide_drive_t *drive, unsigned int bcount)
302 (void) HWIF(drive)->INB(IDE_DATA_REG);
305 #if IDEFLOPPY_DEBUG_BUGS
306 static void idefloppy_write_zeros (ide_drive_t *drive, unsigned int bcount)
309 HWIF(drive)->OUTB(0, IDE_DATA_REG);
311 #endif /* IDEFLOPPY_DEBUG_BUGS */
315 * idefloppy_do_end_request is used to finish servicing a request.
317 * For read/write requests, we will call ide_end_request to pass to the
320 static int idefloppy_do_end_request(ide_drive_t *drive, int uptodate, int nsecs)
322 idefloppy_floppy_t *floppy = drive->driver_data;
323 struct request *rq = HWGROUP(drive)->rq;
326 debug_log("Reached %s\n", __func__);
329 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
330 case 1: error = 0; break;
331 default: error = uptodate;
334 floppy->failed_pc = NULL;
335 /* Why does this happen? */
338 if (!blk_special_request(rq)) {
339 /* our real local end request function */
340 ide_end_request(drive, uptodate, nsecs);
344 /* fixme: need to move this local also */
345 ide_end_drive_cmd(drive, 0, 0);
349 static void idefloppy_input_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
351 struct request *rq = pc->rq;
352 struct bio_vec *bvec;
353 struct req_iterator iter;
358 rq_for_each_segment(bvec, rq, iter) {
362 count = min(bvec->bv_len, bcount);
364 data = bvec_kmap_irq(bvec, &flags);
365 drive->hwif->atapi_input_bytes(drive, data, count);
366 bvec_kunmap_irq(data, &flags);
369 pc->b_count += count;
373 idefloppy_do_end_request(drive, 1, done >> 9);
376 printk(KERN_ERR "%s: leftover data in idefloppy_input_buffers, bcount == %d\n", drive->name, bcount);
377 idefloppy_discard_data(drive, bcount);
381 static void idefloppy_output_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
383 struct request *rq = pc->rq;
384 struct req_iterator iter;
385 struct bio_vec *bvec;
390 rq_for_each_segment(bvec, rq, iter) {
394 count = min(bvec->bv_len, bcount);
396 data = bvec_kmap_irq(bvec, &flags);
397 drive->hwif->atapi_output_bytes(drive, data, count);
398 bvec_kunmap_irq(data, &flags);
401 pc->b_count += count;
405 idefloppy_do_end_request(drive, 1, done >> 9);
407 #if IDEFLOPPY_DEBUG_BUGS
409 printk(KERN_ERR "%s: leftover data in idefloppy_output_buffers, bcount == %d\n", drive->name, bcount);
410 idefloppy_write_zeros(drive, bcount);
415 static void idefloppy_update_buffers (ide_drive_t *drive, idefloppy_pc_t *pc)
417 struct request *rq = pc->rq;
418 struct bio *bio = rq->bio;
420 while ((bio = rq->bio) != NULL)
421 idefloppy_do_end_request(drive, 1, 0);
425 * idefloppy_queue_pc_head generates a new packet command request in front
426 * of the request queue, before the current request, so that it will be
427 * processed immediately, on the next pass through the driver.
429 static void idefloppy_queue_pc_head (ide_drive_t *drive,idefloppy_pc_t *pc,struct request *rq)
431 struct ide_floppy_obj *floppy = drive->driver_data;
433 ide_init_drive_cmd(rq);
434 rq->buffer = (char *) pc;
435 rq->cmd_type = REQ_TYPE_SPECIAL;
436 rq->rq_disk = floppy->disk;
437 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
440 static idefloppy_pc_t *idefloppy_next_pc_storage (ide_drive_t *drive)
442 idefloppy_floppy_t *floppy = drive->driver_data;
444 if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
445 floppy->pc_stack_index=0;
446 return (&floppy->pc_stack[floppy->pc_stack_index++]);
449 static struct request *idefloppy_next_rq_storage (ide_drive_t *drive)
451 idefloppy_floppy_t *floppy = drive->driver_data;
453 if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
454 floppy->rq_stack_index = 0;
455 return (&floppy->rq_stack[floppy->rq_stack_index++]);
458 static void idefloppy_request_sense_callback(ide_drive_t *drive)
460 idefloppy_floppy_t *floppy = drive->driver_data;
461 u8 *buf = floppy->pc->buffer;
463 debug_log("Reached %s\n", __func__);
465 if (!floppy->pc->error) {
466 floppy->sense_key = buf[2] & 0x0F;
467 floppy->asc = buf[12];
468 floppy->ascq = buf[13];
469 floppy->progress_indication = buf[15] & 0x80 ?
470 (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
472 if (floppy->failed_pc)
473 debug_log("pc = %x, sense key = %x, asc = %x,"
475 floppy->failed_pc->c[0],
480 debug_log("sense key = %x, asc = %x, ascq = %x\n",
486 idefloppy_do_end_request(drive, 1, 0);
488 printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting"
490 idefloppy_do_end_request(drive, 0, 0);
495 * General packet command callback function.
497 static void idefloppy_pc_callback (ide_drive_t *drive)
499 idefloppy_floppy_t *floppy = drive->driver_data;
501 debug_log("Reached %s\n", __func__);
503 idefloppy_do_end_request(drive, floppy->pc->error ? 0 : 1, 0);
507 * idefloppy_init_pc initializes a packet command.
509 static void idefloppy_init_pc (idefloppy_pc_t *pc)
511 memset(pc->c, 0, 12);
514 pc->request_transfer = 0;
515 pc->buffer = pc->pc_buffer;
516 pc->buffer_size = IDEFLOPPY_PC_BUFFER_SIZE;
517 pc->callback = &idefloppy_pc_callback;
520 static void idefloppy_create_request_sense_cmd (idefloppy_pc_t *pc)
522 idefloppy_init_pc(pc);
523 pc->c[0] = GPCMD_REQUEST_SENSE;
525 pc->request_transfer = 18;
526 pc->callback = &idefloppy_request_sense_callback;
530 * idefloppy_retry_pc is called when an error was detected during the
531 * last packet command. We queue a request sense packet command in
532 * the head of the request list.
534 static void idefloppy_retry_pc (ide_drive_t *drive)
539 (void)drive->hwif->INB(IDE_ERROR_REG);
540 pc = idefloppy_next_pc_storage(drive);
541 rq = idefloppy_next_rq_storage(drive);
542 idefloppy_create_request_sense_cmd(pc);
543 idefloppy_queue_pc_head(drive, pc, rq);
547 * idefloppy_pc_intr is the usual interrupt handler which will be called
548 * during a packet command.
550 static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
552 idefloppy_floppy_t *floppy = drive->driver_data;
553 ide_hwif_t *hwif = drive->hwif;
554 idefloppy_pc_t *pc = floppy->pc;
555 struct request *rq = pc->rq;
560 debug_log("Reached %s interrupt handler\n", __func__);
562 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
563 if (HWIF(drive)->ide_dma_end(drive)) {
564 set_bit(PC_DMA_ERROR, &pc->flags);
566 pc->actually_transferred = pc->request_transfer;
567 idefloppy_update_buffers(drive, pc);
569 debug_log("DMA finished\n");
572 /* Clear the interrupt */
573 stat = drive->hwif->INB(IDE_STATUS_REG);
575 if ((stat & DRQ_STAT) == 0) { /* No more interrupts */
576 debug_log("Packet command completed, %d bytes transferred\n",
577 pc->actually_transferred);
578 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
580 local_irq_enable_in_hardirq();
582 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
584 debug_log("%s: I/O error\n", drive->name);
586 if (pc->c[0] == GPCMD_REQUEST_SENSE) {
587 printk(KERN_ERR "ide-floppy: I/O error in "
588 "request sense command\n");
589 return ide_do_reset(drive);
591 /* Retry operation */
592 idefloppy_retry_pc(drive);
593 /* queued, but not started */
597 if (floppy->failed_pc == pc)
598 floppy->failed_pc = NULL;
599 /* Command finished - Call the callback function */
604 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
605 printk(KERN_ERR "ide-floppy: The floppy wants to issue "
606 "more interrupts in DMA mode\n");
608 return ide_do_reset(drive);
611 /* Get the number of bytes to transfer */
612 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
613 hwif->INB(IDE_BCOUNTL_REG);
614 /* on this interrupt */
615 ireason = hwif->INB(IDE_IREASON_REG);
618 printk(KERN_ERR "ide-floppy: CoD != 0 in idefloppy_pc_intr\n");
619 return ide_do_reset(drive);
621 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
622 /* Hopefully, we will never get here */
623 printk(KERN_ERR "ide-floppy: We wanted to %s, ",
624 (ireason & IO) ? "Write" : "Read");
625 printk(KERN_ERR "but the floppy wants us to %s !\n",
626 (ireason & IO) ? "Read" : "Write");
627 return ide_do_reset(drive);
629 if (!test_bit(PC_WRITING, &pc->flags)) {
630 /* Reading - Check that we have enough space */
631 temp = pc->actually_transferred + bcount;
632 if (temp > pc->request_transfer) {
633 if (temp > pc->buffer_size) {
634 printk(KERN_ERR "ide-floppy: The floppy wants "
635 "to send us more data than expected "
636 "- discarding data\n");
637 idefloppy_discard_data(drive, bcount);
639 ide_set_handler(drive,
645 debug_log("The floppy wants to send us more data than"
646 " expected - allowing transfer\n");
649 if (test_bit(PC_WRITING, &pc->flags)) {
650 if (pc->buffer != NULL)
651 /* Write the current buffer */
652 hwif->atapi_output_bytes(drive, pc->current_position,
655 idefloppy_output_buffers(drive, pc, bcount);
657 if (pc->buffer != NULL)
658 /* Read the current buffer */
659 hwif->atapi_input_bytes(drive, pc->current_position,
662 idefloppy_input_buffers(drive, pc, bcount);
664 /* Update the current position */
665 pc->actually_transferred += bcount;
666 pc->current_position += bcount;
668 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL); /* And set the interrupt handler again */
673 * This is the original routine that did the packet transfer.
674 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
675 * for that drive below. The algorithm is chosen based on drive type
677 static ide_startstop_t idefloppy_transfer_pc (ide_drive_t *drive)
679 ide_startstop_t startstop;
680 idefloppy_floppy_t *floppy = drive->driver_data;
683 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
684 printk(KERN_ERR "ide-floppy: Strange, packet command "
685 "initiated yet DRQ isn't asserted\n");
688 ireason = drive->hwif->INB(IDE_IREASON_REG);
689 if ((ireason & CD) == 0 || (ireason & IO)) {
690 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
691 "issuing a packet command\n");
692 return ide_do_reset(drive);
695 /* Set the interrupt routine */
696 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
697 /* Send the actual packet */
698 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
704 * What we have here is a classic case of a top half / bottom half
705 * interrupt service routine. In interrupt mode, the device sends
706 * an interrupt to signal it's ready to receive a packet. However,
707 * we need to delay about 2-3 ticks before issuing the packet or we
710 * So, follow carefully. transfer_pc1 is called as an interrupt (or
711 * directly). In either case, when the device says it's ready for a
712 * packet, we schedule the packet transfer to occur about 2-3 ticks
713 * later in transfer_pc2.
715 static int idefloppy_transfer_pc2 (ide_drive_t *drive)
717 idefloppy_floppy_t *floppy = drive->driver_data;
719 /* Send the actual packet */
720 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
721 /* Timeout for the packet command */
722 return IDEFLOPPY_WAIT_CMD;
725 static ide_startstop_t idefloppy_transfer_pc1 (ide_drive_t *drive)
727 idefloppy_floppy_t *floppy = drive->driver_data;
728 ide_startstop_t startstop;
731 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
732 printk(KERN_ERR "ide-floppy: Strange, packet command "
733 "initiated yet DRQ isn't asserted\n");
736 ireason = drive->hwif->INB(IDE_IREASON_REG);
737 if ((ireason & CD) == 0 || (ireason & IO)) {
738 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
739 "while issuing a packet command\n");
740 return ide_do_reset(drive);
743 * The following delay solves a problem with ATAPI Zip 100 drives
744 * where the Busy flag was apparently being deasserted before the
745 * unit was ready to receive data. This was happening on a
746 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
747 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
748 * used until after the packet is moved in about 50 msec.
751 ide_set_handler(drive,
752 &idefloppy_pc_intr, /* service routine for packet command */
753 floppy->ticks, /* wait this long before "failing" */
754 &idefloppy_transfer_pc2); /* fail == transfer_pc2 */
759 * idefloppy_should_report_error()
761 * Supresses error messages resulting from Medium not present
763 static inline int idefloppy_should_report_error(idefloppy_floppy_t *floppy)
765 if (floppy->sense_key == 0x02 &&
766 floppy->asc == 0x3a &&
767 floppy->ascq == 0x00)
773 * Issue a packet command
775 static ide_startstop_t idefloppy_issue_pc (ide_drive_t *drive, idefloppy_pc_t *pc)
777 idefloppy_floppy_t *floppy = drive->driver_data;
778 ide_hwif_t *hwif = drive->hwif;
779 ide_handler_t *pkt_xfer_routine;
783 if (floppy->failed_pc == NULL &&
784 pc->c[0] != GPCMD_REQUEST_SENSE)
785 floppy->failed_pc = pc;
786 /* Set the current packet command */
789 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES ||
790 test_bit(PC_ABORT, &pc->flags)) {
792 * We will "abort" retrying a packet command in case
793 * a legitimate error code was received.
795 if (!test_bit(PC_ABORT, &pc->flags)) {
796 if (!test_bit(PC_SUPPRESS_ERROR, &pc->flags)) {
797 if (idefloppy_should_report_error(floppy))
798 printk(KERN_ERR "ide-floppy: %s: I/O error, "
799 "pc = %2x, key = %2x, "
800 "asc = %2x, ascq = %2x\n",
801 drive->name, pc->c[0],
803 floppy->asc, floppy->ascq);
806 pc->error = IDEFLOPPY_ERROR_GENERAL;
808 floppy->failed_pc = NULL;
813 debug_log("Retry number - %d\n", pc->retries);
816 /* We haven't transferred any data yet */
817 pc->actually_transferred = 0;
818 pc->current_position = pc->buffer;
819 bcount = min(pc->request_transfer, 63 * 1024);
821 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags))
826 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
827 dma = !hwif->dma_setup(drive);
829 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
830 IDE_TFLAG_OUT_DEVICE, bcount, dma);
832 if (dma) { /* Begin DMA, if necessary */
833 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
834 hwif->dma_start(drive);
837 /* Can we transfer the packet when we get the interrupt or wait? */
838 if (test_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags)) {
840 pkt_xfer_routine = &idefloppy_transfer_pc1;
843 pkt_xfer_routine = &idefloppy_transfer_pc;
846 if (test_bit (IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags)) {
847 /* Issue the packet command */
848 ide_execute_command(drive, WIN_PACKETCMD,
854 /* Issue the packet command */
855 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
856 return (*pkt_xfer_routine) (drive);
860 static void idefloppy_rw_callback (ide_drive_t *drive)
862 debug_log("Reached %s\n", __func__);
864 idefloppy_do_end_request(drive, 1, 0);
868 static void idefloppy_create_prevent_cmd (idefloppy_pc_t *pc, int prevent)
870 debug_log("creating prevent removal command, prevent = %d\n", prevent);
872 idefloppy_init_pc(pc);
873 pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
877 static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t *pc)
879 idefloppy_init_pc(pc);
880 pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
883 pc->request_transfer = 255;
886 static void idefloppy_create_format_unit_cmd (idefloppy_pc_t *pc, int b, int l,
889 idefloppy_init_pc(pc);
890 pc->c[0] = GPCMD_FORMAT_UNIT;
893 memset(pc->buffer, 0, 12);
894 pc->buffer[1] = 0xA2;
895 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
897 if (flags & 1) /* Verify bit on... */
898 pc->buffer[1] ^= 0x20; /* ... turn off DCRT bit */
901 put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buffer[4]));
902 put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buffer[8]));
904 set_bit(PC_WRITING, &pc->flags);
908 * A mode sense command is used to "sense" floppy parameters.
910 static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t *pc, u8 page_code, u8 type)
912 u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
914 idefloppy_init_pc(pc);
915 pc->c[0] = GPCMD_MODE_SENSE_10;
917 pc->c[2] = page_code + (type << 6);
920 case IDEFLOPPY_CAPABILITIES_PAGE:
923 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
927 printk(KERN_ERR "ide-floppy: unsupported page code "
928 "in create_mode_sense_cmd\n");
930 put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
931 pc->request_transfer = length;
934 static void idefloppy_create_start_stop_cmd (idefloppy_pc_t *pc, int start)
936 idefloppy_init_pc(pc);
937 pc->c[0] = GPCMD_START_STOP_UNIT;
941 static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc)
943 idefloppy_init_pc(pc);
944 pc->c[0] = GPCMD_TEST_UNIT_READY;
947 static void idefloppy_create_rw_cmd (idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq, unsigned long sector)
949 int block = sector / floppy->bs_factor;
950 int blocks = rq->nr_sectors / floppy->bs_factor;
951 int cmd = rq_data_dir(rq);
953 debug_log("create_rw1%d_cmd: block == %d, blocks == %d\n",
954 2 * test_bit (IDEFLOPPY_USE_READ12, &floppy->flags),
957 idefloppy_init_pc(pc);
958 if (test_bit(IDEFLOPPY_USE_READ12, &floppy->flags)) {
959 pc->c[0] = cmd == READ ? GPCMD_READ_12 : GPCMD_WRITE_12;
960 put_unaligned(cpu_to_be32(blocks), (unsigned int *) &pc->c[6]);
962 pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
963 put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
965 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
966 pc->callback = &idefloppy_rw_callback;
968 pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
969 if (rq->cmd_flags & REQ_RW)
970 set_bit(PC_WRITING, &pc->flags);
972 pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
973 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
977 idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq)
979 idefloppy_init_pc(pc);
980 pc->callback = &idefloppy_rw_callback;
981 memcpy(pc->c, rq->cmd, sizeof(pc->c));
983 pc->b_count = rq->data_len;
984 if (rq->data_len && rq_data_dir(rq) == WRITE)
985 set_bit(PC_WRITING, &pc->flags);
986 pc->buffer = rq->data;
988 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
991 * possibly problematic, doesn't look like ide-floppy correctly
992 * handled scattered requests if dma fails...
994 pc->request_transfer = pc->buffer_size = rq->data_len;
998 * idefloppy_do_request is our request handling function.
1000 static ide_startstop_t idefloppy_do_request (ide_drive_t *drive, struct request *rq, sector_t block_s)
1002 idefloppy_floppy_t *floppy = drive->driver_data;
1004 unsigned long block = (unsigned long)block_s;
1006 debug_log("dev: %s, cmd_type: %x, errors: %d\n",
1007 rq->rq_disk ? rq->rq_disk->disk_name : "?",
1008 rq->cmd_type, rq->errors);
1009 debug_log("sector: %ld, nr_sectors: %ld, "
1010 "current_nr_sectors: %d\n", (long)rq->sector,
1011 rq->nr_sectors, rq->current_nr_sectors);
1013 if (rq->errors >= ERROR_MAX) {
1014 if (floppy->failed_pc != NULL) {
1015 if (idefloppy_should_report_error(floppy))
1016 printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x,"
1017 " key = %2x, asc = %2x, ascq = %2x\n",
1018 drive->name, floppy->failed_pc->c[0],
1019 floppy->sense_key, floppy->asc, floppy->ascq);
1022 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
1024 idefloppy_do_end_request(drive, 0, 0);
1027 if (blk_fs_request(rq)) {
1028 if (((long)rq->sector % floppy->bs_factor) ||
1029 (rq->nr_sectors % floppy->bs_factor)) {
1030 printk("%s: unsupported r/w request size\n",
1032 idefloppy_do_end_request(drive, 0, 0);
1035 pc = idefloppy_next_pc_storage(drive);
1036 idefloppy_create_rw_cmd(floppy, pc, rq, block);
1037 } else if (blk_special_request(rq)) {
1038 pc = (idefloppy_pc_t *) rq->buffer;
1039 } else if (blk_pc_request(rq)) {
1040 pc = idefloppy_next_pc_storage(drive);
1041 idefloppy_blockpc_cmd(floppy, pc, rq);
1043 blk_dump_rq_flags(rq,
1044 "ide-floppy: unsupported command in queue");
1045 idefloppy_do_end_request(drive, 0, 0);
1050 return idefloppy_issue_pc(drive, pc);
1054 * idefloppy_queue_pc_tail adds a special packet command request to the
1055 * tail of the request queue, and waits for it to be serviced.
1057 static int idefloppy_queue_pc_tail (ide_drive_t *drive,idefloppy_pc_t *pc)
1059 struct ide_floppy_obj *floppy = drive->driver_data;
1062 ide_init_drive_cmd (&rq);
1063 rq.buffer = (char *) pc;
1064 rq.cmd_type = REQ_TYPE_SPECIAL;
1065 rq.rq_disk = floppy->disk;
1067 return ide_do_drive_cmd(drive, &rq, ide_wait);
1071 * Look at the flexible disk page parameters. We ignore the CHS capacity
1072 * parameters and use the LBA parameters instead.
1074 static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
1076 idefloppy_floppy_t *floppy = drive->driver_data;
1079 int capacity, lba_capacity;
1080 u16 transfer_rate, sector_size, cyls, rpm;
1083 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE,
1084 MODE_SENSE_CURRENT);
1086 if (idefloppy_queue_pc_tail(drive, &pc)) {
1087 printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
1091 floppy->wp = !!(pc.buffer[3] & 0x80);
1092 set_disk_ro(floppy->disk, floppy->wp);
1093 page = &pc.buffer[8];
1095 transfer_rate = be16_to_cpu(*(u16 *)&pc.buffer[8 + 2]);
1096 sector_size = be16_to_cpu(*(u16 *)&pc.buffer[8 + 6]);
1097 cyls = be16_to_cpu(*(u16 *)&pc.buffer[8 + 8]);
1098 rpm = be16_to_cpu(*(u16 *)&pc.buffer[8 + 28]);
1099 heads = pc.buffer[8 + 4];
1100 sectors = pc.buffer[8 + 5];
1102 capacity = cyls * heads * sectors * sector_size;
1104 if (memcmp(page, &floppy->flexible_disk_page, 32))
1105 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
1106 "%d sector size, %d rpm\n",
1107 drive->name, capacity / 1024, cyls, heads,
1108 sectors, transfer_rate / 8, sector_size, rpm);
1110 memcpy(&floppy->flexible_disk_page, page, 32);
1111 drive->bios_cyl = cyls;
1112 drive->bios_head = heads;
1113 drive->bios_sect = sectors;
1114 lba_capacity = floppy->blocks * floppy->block_size;
1116 if (capacity < lba_capacity) {
1117 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
1118 "bytes, but the drive only handles %d\n",
1119 drive->name, lba_capacity, capacity);
1120 floppy->blocks = floppy->block_size ?
1121 capacity / floppy->block_size : 0;
1126 static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
1128 idefloppy_floppy_t *floppy = drive->driver_data;
1132 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
1133 MODE_SENSE_CURRENT);
1135 set_bit(PC_SUPPRESS_ERROR, &pc.flags);
1136 if (idefloppy_queue_pc_tail(drive, &pc))
1139 floppy->srfp = pc.buffer[8 + 2] & 0x40;
1144 * Determine if a media is present in the floppy drive, and if so,
1147 static int idefloppy_get_capacity (ide_drive_t *drive)
1149 idefloppy_floppy_t *floppy = drive->driver_data;
1151 idefloppy_capacity_header_t *header;
1152 idefloppy_capacity_descriptor_t *descriptor;
1153 int i, descriptors, rc = 1, blocks, length;
1155 drive->bios_cyl = 0;
1156 drive->bios_head = drive->bios_sect = 0;
1158 floppy->bs_factor = 1;
1159 set_capacity(floppy->disk, 0);
1161 idefloppy_create_read_capacity_cmd(&pc);
1162 if (idefloppy_queue_pc_tail(drive, &pc)) {
1163 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1166 header = (idefloppy_capacity_header_t *) pc.buffer;
1167 descriptors = header->length / sizeof(idefloppy_capacity_descriptor_t);
1168 descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
1170 for (i = 0; i < descriptors; i++, descriptor++) {
1171 blocks = descriptor->blocks = be32_to_cpu(descriptor->blocks);
1172 length = descriptor->length = be16_to_cpu(descriptor->length);
1176 switch (descriptor->dc) {
1177 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1178 case CAPACITY_UNFORMATTED:
1179 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags))
1181 * If it is not a clik drive, break out
1182 * (maintains previous driver behaviour)
1185 case CAPACITY_CURRENT:
1186 /* Normal Zip/LS-120 disks */
1187 if (memcmp(descriptor, &floppy->capacity, sizeof (idefloppy_capacity_descriptor_t)))
1188 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
1189 "sector size\n", drive->name,
1190 blocks * length / 1024, blocks, length);
1191 floppy->capacity = *descriptor;
1192 if (!length || length % 512) {
1193 printk(KERN_NOTICE "%s: %d bytes block size "
1194 "not supported\n", drive->name, length);
1196 floppy->blocks = blocks;
1197 floppy->block_size = length;
1198 if ((floppy->bs_factor = length / 512) != 1)
1199 printk(KERN_NOTICE "%s: warning: non "
1200 "512 bytes block size not "
1201 "fully supported\n",
1206 case CAPACITY_NO_CARTRIDGE:
1208 * This is a KERN_ERR so it appears on screen
1209 * for the user to see
1211 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
1213 case CAPACITY_INVALID:
1214 printk(KERN_ERR "%s: Invalid capacity for disk "
1215 "in drive\n", drive->name);
1220 debug_log("Descriptor 0 Code: %d\n", descriptor->dc);
1222 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
1223 i, blocks * length / 1024, blocks, length);
1226 /* Clik! disk does not support get_flexible_disk_page */
1227 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1228 (void) ide_floppy_get_flexible_disk_page(drive);
1231 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
1236 ** Obtain the list of formattable capacities.
1237 ** Very similar to idefloppy_get_capacity, except that we push the capacity
1238 ** descriptors to userland, instead of our own structures.
1240 ** Userland gives us the following structure:
1242 ** struct idefloppy_format_capacities {
1250 ** userland initializes nformats to the number of allocated formats[]
1251 ** records. On exit we set nformats to the number of records we've
1252 ** actually initialized.
1256 static int idefloppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
1259 idefloppy_capacity_header_t *header;
1260 idefloppy_capacity_descriptor_t *descriptor;
1261 int i, descriptors, blocks, length;
1266 if (get_user(u_array_size, arg))
1269 if (u_array_size <= 0)
1272 idefloppy_create_read_capacity_cmd(&pc);
1273 if (idefloppy_queue_pc_tail(drive, &pc)) {
1274 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1277 header = (idefloppy_capacity_header_t *) pc.buffer;
1278 descriptors = header->length /
1279 sizeof(idefloppy_capacity_descriptor_t);
1280 descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
1286 ** We always skip the first capacity descriptor. That's the
1287 ** current capacity. We are interested in the remaining descriptors,
1288 ** the formattable capacities.
1291 for (i=0; i<descriptors; i++, descriptor++) {
1292 if (u_index >= u_array_size)
1293 break; /* User-supplied buffer too small */
1295 continue; /* Skip the first descriptor */
1297 blocks = be32_to_cpu(descriptor->blocks);
1298 length = be16_to_cpu(descriptor->length);
1300 if (put_user(blocks, argp))
1304 if (put_user(length, argp))
1311 if (put_user(u_index, arg))
1317 ** Send ATAPI_FORMAT_UNIT to the drive.
1319 ** Userland gives us the following structure:
1321 ** struct idefloppy_format_command {
1327 ** flags is a bitmask, currently, the only defined flag is:
1329 ** 0x01 - verify media after format.
1332 static int idefloppy_begin_format(ide_drive_t *drive, int __user *arg)
1339 if (get_user(blocks, arg) ||
1340 get_user(length, arg+1) ||
1341 get_user(flags, arg+2)) {
1345 (void) idefloppy_get_sfrp_bit(drive);
1346 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1347 if (idefloppy_queue_pc_tail(drive, &pc)) {
1355 ** Get ATAPI_FORMAT_UNIT progress indication.
1357 ** Userland gives a pointer to an int. The int is set to a progress
1358 ** indicator 0-65536, with 65536=100%.
1360 ** If the drive does not support format progress indication, we just check
1361 ** the dsc bit, and return either 0 or 65536.
1364 static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
1366 idefloppy_floppy_t *floppy = drive->driver_data;
1368 int progress_indication = 0x10000;
1371 idefloppy_create_request_sense_cmd(&pc);
1372 if (idefloppy_queue_pc_tail(drive, &pc)) {
1376 if (floppy->sense_key == 2 &&
1378 floppy->ascq == 4) {
1379 progress_indication = floppy->progress_indication;
1381 /* Else assume format_unit has finished, and we're
1384 unsigned long flags;
1387 local_irq_save(flags);
1388 stat = drive->hwif->INB(IDE_STATUS_REG);
1389 local_irq_restore(flags);
1391 progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
1393 if (put_user(progress_indication, arg))
1400 * Return the current floppy capacity.
1402 static sector_t idefloppy_capacity (ide_drive_t *drive)
1404 idefloppy_floppy_t *floppy = drive->driver_data;
1405 unsigned long capacity = floppy->blocks * floppy->bs_factor;
1411 * idefloppy_identify_device checks if we can support a drive,
1412 * based on the ATAPI IDENTIFY command results.
1414 static int idefloppy_identify_device (ide_drive_t *drive,struct hd_driveid *id)
1416 struct idefloppy_id_gcw gcw;
1417 #if IDEFLOPPY_DEBUG_INFO
1419 #endif /* IDEFLOPPY_DEBUG_INFO */
1421 *((u16 *) &gcw) = id->config;
1424 /* kludge for Apple PowerBook internal zip */
1425 if ((gcw.device_type == 5) &&
1426 !strstr(id->model, "CD-ROM") &&
1427 strstr(id->model, "ZIP"))
1428 gcw.device_type = 0;
1431 #if IDEFLOPPY_DEBUG_INFO
1432 printk(KERN_INFO "Dumping ATAPI Identify Device floppy parameters\n");
1433 switch (gcw.protocol) {
1434 case 0: case 1: sprintf(buffer, "ATA");break;
1435 case 2: sprintf(buffer, "ATAPI");break;
1436 case 3: sprintf(buffer, "Reserved (Unknown to ide-floppy)");break;
1438 printk(KERN_INFO "Protocol Type: %s\n", buffer);
1439 switch (gcw.device_type) {
1440 case 0: sprintf(buffer, "Direct-access Device");break;
1441 case 1: sprintf(buffer, "Streaming Tape Device");break;
1442 case 2: case 3: case 4: sprintf (buffer, "Reserved");break;
1443 case 5: sprintf(buffer, "CD-ROM Device");break;
1444 case 6: sprintf(buffer, "Reserved");
1445 case 7: sprintf(buffer, "Optical memory Device");break;
1446 case 0x1f: sprintf(buffer, "Unknown or no Device type");break;
1447 default: sprintf(buffer, "Reserved");
1449 printk(KERN_INFO "Device Type: %x - %s\n", gcw.device_type, buffer);
1450 printk(KERN_INFO "Removable: %s\n",gcw.removable ? "Yes":"No");
1451 switch (gcw.drq_type) {
1452 case 0: sprintf(buffer, "Microprocessor DRQ");break;
1453 case 1: sprintf(buffer, "Interrupt DRQ");break;
1454 case 2: sprintf(buffer, "Accelerated DRQ");break;
1455 case 3: sprintf(buffer, "Reserved");break;
1457 printk(KERN_INFO "Command Packet DRQ Type: %s\n", buffer);
1458 switch (gcw.packet_size) {
1459 case 0: sprintf(buffer, "12 bytes");break;
1460 case 1: sprintf(buffer, "16 bytes");break;
1461 default: sprintf(buffer, "Reserved");break;
1463 printk(KERN_INFO "Command Packet Size: %s\n", buffer);
1464 #endif /* IDEFLOPPY_DEBUG_INFO */
1466 if (gcw.protocol != 2)
1467 printk(KERN_ERR "ide-floppy: Protocol is not ATAPI\n");
1468 else if (gcw.device_type != 0)
1469 printk(KERN_ERR "ide-floppy: Device type is not set to floppy\n");
1470 else if (!gcw.removable)
1471 printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
1472 else if (gcw.drq_type == 3) {
1473 printk(KERN_ERR "ide-floppy: Sorry, DRQ type %d not supported\n", gcw.drq_type);
1474 } else if (gcw.packet_size != 0) {
1475 printk(KERN_ERR "ide-floppy: Packet size is not 12 bytes long\n");
1481 #ifdef CONFIG_IDE_PROC_FS
1482 static void idefloppy_add_settings(ide_drive_t *drive)
1484 idefloppy_floppy_t *floppy = drive->driver_data;
1487 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
1489 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
1490 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
1491 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
1492 ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &floppy->ticks, NULL);
1495 static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
1499 * Driver initialization.
1501 static void idefloppy_setup (ide_drive_t *drive, idefloppy_floppy_t *floppy)
1503 struct idefloppy_id_gcw gcw;
1505 *((u16 *) &gcw) = drive->id->config;
1506 floppy->pc = floppy->pc_stack;
1507 if (gcw.drq_type == 1)
1508 set_bit(IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags);
1510 * We used to check revisions here. At this point however
1511 * I'm giving up. Just assume they are all broken, its easier.
1513 * The actual reason for the workarounds was likely
1514 * a driver bug after all rather than a firmware bug,
1515 * and the workaround below used to hide it. It should
1516 * be fixed as of version 1.9, but to be on the safe side
1517 * we'll leave the limitation below for the 2.2.x tree.
1520 if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
1521 set_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags);
1522 /* This value will be visible in the /proc/ide/hdx/settings */
1523 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1524 blk_queue_max_sectors(drive->queue, 64);
1528 * Guess what? The IOMEGA Clik! drive also needs the
1529 * above fix. It makes nasty clicking noises without
1530 * it, so please don't remove this.
1532 if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
1533 blk_queue_max_sectors(drive->queue, 64);
1534 set_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags);
1538 (void) idefloppy_get_capacity(drive);
1539 idefloppy_add_settings(drive);
1542 static void ide_floppy_remove(ide_drive_t *drive)
1544 idefloppy_floppy_t *floppy = drive->driver_data;
1545 struct gendisk *g = floppy->disk;
1547 ide_proc_unregister_driver(drive, floppy->driver);
1551 ide_floppy_put(floppy);
1554 static void idefloppy_cleanup_obj(struct kref *kref)
1556 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1557 ide_drive_t *drive = floppy->drive;
1558 struct gendisk *g = floppy->disk;
1560 drive->driver_data = NULL;
1561 g->private_data = NULL;
1566 #ifdef CONFIG_IDE_PROC_FS
1567 static int proc_idefloppy_read_capacity
1568 (char *page, char **start, off_t off, int count, int *eof, void *data)
1570 ide_drive_t*drive = (ide_drive_t *)data;
1573 len = sprintf(page,"%llu\n", (long long)idefloppy_capacity(drive));
1574 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1577 static ide_proc_entry_t idefloppy_proc[] = {
1578 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
1579 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1580 { NULL, 0, NULL, NULL }
1582 #endif /* CONFIG_IDE_PROC_FS */
1584 static int ide_floppy_probe(ide_drive_t *);
1586 static ide_driver_t idefloppy_driver = {
1588 .owner = THIS_MODULE,
1589 .name = "ide-floppy",
1590 .bus = &ide_bus_type,
1592 .probe = ide_floppy_probe,
1593 .remove = ide_floppy_remove,
1594 .version = IDEFLOPPY_VERSION,
1595 .media = ide_floppy,
1596 .supports_dsc_overlap = 0,
1597 .do_request = idefloppy_do_request,
1598 .end_request = idefloppy_do_end_request,
1599 .error = __ide_error,
1600 .abort = __ide_abort,
1601 #ifdef CONFIG_IDE_PROC_FS
1602 .proc = idefloppy_proc,
1606 static int idefloppy_open(struct inode *inode, struct file *filp)
1608 struct gendisk *disk = inode->i_bdev->bd_disk;
1609 struct ide_floppy_obj *floppy;
1614 debug_log("Reached %s\n", __func__);
1616 if (!(floppy = ide_floppy_get(disk)))
1619 drive = floppy->drive;
1623 if (floppy->openers == 1) {
1624 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1627 idefloppy_create_test_unit_ready_cmd(&pc);
1628 if (idefloppy_queue_pc_tail(drive, &pc)) {
1629 idefloppy_create_start_stop_cmd(&pc, 1);
1630 (void) idefloppy_queue_pc_tail(drive, &pc);
1633 if (idefloppy_get_capacity (drive)
1634 && (filp->f_flags & O_NDELAY) == 0
1636 ** Allow O_NDELAY to open a drive without a disk, or with
1637 ** an unreadable disk, so that we can get the format
1638 ** capacity of the drive or begin the format - Sam
1642 goto out_put_floppy;
1645 if (floppy->wp && (filp->f_mode & 2)) {
1647 goto out_put_floppy;
1649 set_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1650 /* IOMEGA Clik! drives do not support lock/unlock commands */
1651 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1652 idefloppy_create_prevent_cmd(&pc, 1);
1653 (void) idefloppy_queue_pc_tail(drive, &pc);
1655 check_disk_change(inode->i_bdev);
1656 } else if (test_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags)) {
1658 goto out_put_floppy;
1664 ide_floppy_put(floppy);
1668 static int idefloppy_release(struct inode *inode, struct file *filp)
1670 struct gendisk *disk = inode->i_bdev->bd_disk;
1671 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1672 ide_drive_t *drive = floppy->drive;
1675 debug_log("Reached %s\n", __func__);
1677 if (floppy->openers == 1) {
1678 /* IOMEGA Clik! drives do not support lock/unlock commands */
1679 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1680 idefloppy_create_prevent_cmd(&pc, 0);
1681 (void) idefloppy_queue_pc_tail(drive, &pc);
1684 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1689 ide_floppy_put(floppy);
1694 static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1696 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1697 ide_drive_t *drive = floppy->drive;
1699 geo->heads = drive->bios_head;
1700 geo->sectors = drive->bios_sect;
1701 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1705 static int idefloppy_ioctl(struct inode *inode, struct file *file,
1706 unsigned int cmd, unsigned long arg)
1708 struct block_device *bdev = inode->i_bdev;
1709 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1710 ide_drive_t *drive = floppy->drive;
1711 void __user *argp = (void __user *)arg;
1713 int prevent = (arg) ? 1 : 0;
1720 case CDROM_LOCKDOOR:
1721 if (floppy->openers > 1)
1724 /* The IOMEGA Clik! Drive doesn't support this command - no room for an eject mechanism */
1725 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1726 idefloppy_create_prevent_cmd(&pc, prevent);
1727 (void) idefloppy_queue_pc_tail(drive, &pc);
1729 if (cmd == CDROMEJECT) {
1730 idefloppy_create_start_stop_cmd(&pc, 2);
1731 (void) idefloppy_queue_pc_tail(drive, &pc);
1734 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1736 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
1737 return idefloppy_get_format_capacities(drive, argp);
1738 case IDEFLOPPY_IOCTL_FORMAT_START:
1740 if (!(file->f_mode & 2))
1743 if (floppy->openers > 1) {
1744 /* Don't format if someone is using the disk */
1746 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS,
1751 set_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1753 err = idefloppy_begin_format(drive, argp);
1755 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1758 ** Note, the bit will be cleared when the device is
1759 ** closed. This is the cleanest way to handle the
1760 ** situation where the drive does not support
1761 ** format progress reporting.
1763 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1764 return idefloppy_get_format_progress(drive, argp);
1768 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1769 * and CDROM_SEND_PACKET (legacy) ioctls
1771 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1772 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1773 bdev->bd_disk, cmd, argp);
1778 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1783 static int idefloppy_media_changed(struct gendisk *disk)
1785 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1786 ide_drive_t *drive = floppy->drive;
1788 /* do not scan partitions twice if this is a removable device */
1789 if (drive->attach) {
1793 return test_and_clear_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1796 static int idefloppy_revalidate_disk(struct gendisk *disk)
1798 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1799 set_capacity(disk, idefloppy_capacity(floppy->drive));
1803 static struct block_device_operations idefloppy_ops = {
1804 .owner = THIS_MODULE,
1805 .open = idefloppy_open,
1806 .release = idefloppy_release,
1807 .ioctl = idefloppy_ioctl,
1808 .getgeo = idefloppy_getgeo,
1809 .media_changed = idefloppy_media_changed,
1810 .revalidate_disk= idefloppy_revalidate_disk
1813 static int ide_floppy_probe(ide_drive_t *drive)
1815 idefloppy_floppy_t *floppy;
1818 if (!strstr("ide-floppy", drive->driver_req))
1820 if (!drive->present)
1822 if (drive->media != ide_floppy)
1824 if (!idefloppy_identify_device (drive, drive->id)) {
1825 printk (KERN_ERR "ide-floppy: %s: not supported by this version of ide-floppy\n", drive->name);
1829 printk("ide-floppy: passing drive %s to ide-scsi emulation.\n", drive->name);
1832 if ((floppy = kzalloc(sizeof (idefloppy_floppy_t), GFP_KERNEL)) == NULL) {
1833 printk (KERN_ERR "ide-floppy: %s: Can't allocate a floppy structure\n", drive->name);
1837 g = alloc_disk(1 << PARTN_BITS);
1839 goto out_free_floppy;
1841 ide_init_disk(g, drive);
1843 ide_proc_register_driver(drive, &idefloppy_driver);
1845 kref_init(&floppy->kref);
1847 floppy->drive = drive;
1848 floppy->driver = &idefloppy_driver;
1851 g->private_data = &floppy->driver;
1853 drive->driver_data = floppy;
1855 idefloppy_setup (drive, floppy);
1857 g->minors = 1 << PARTN_BITS;
1858 g->driverfs_dev = &drive->gendev;
1859 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1860 g->fops = &idefloppy_ops;
1871 MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
1873 static void __exit idefloppy_exit (void)
1875 driver_unregister(&idefloppy_driver.gen_driver);
1878 static int __init idefloppy_init(void)
1880 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
1881 return driver_register(&idefloppy_driver.gen_driver);
1884 MODULE_ALIAS("ide:*m-floppy*");
1885 module_init(idefloppy_init);
1886 module_exit(idefloppy_exit);
1887 MODULE_LICENSE("GPL");