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 */
123 * Flexible disk page.
126 #if defined(__LITTLE_ENDIAN_BITFIELD)
127 unsigned page_code :6; /* Page code - Should be 0x5 */
128 unsigned reserved1_6 :1; /* Reserved */
129 unsigned ps :1; /* The device is capable of saving the page */
130 #elif defined(__BIG_ENDIAN_BITFIELD)
131 unsigned ps :1; /* The device is capable of saving the page */
132 unsigned reserved1_6 :1; /* Reserved */
133 unsigned page_code :6; /* Page code - Should be 0x5 */
135 #error "Bitfield endianness not defined! Check your byteorder.h"
137 u8 page_length; /* Page Length - Should be 0x1e */
138 u16 transfer_rate; /* In kilobits per second */
139 u8 heads, sectors; /* Number of heads, Number of sectors per track */
140 u16 sector_size; /* Byes per sector */
141 u16 cyls; /* Number of cylinders */
143 u8 motor_delay; /* Motor off delay */
145 u16 rpm; /* Rotations per minute */
147 } idefloppy_flexible_disk_page_t;
154 u8 length; /* Length of the following descriptors in bytes */
155 } idefloppy_capacity_header_t;
158 u32 blocks; /* Number of blocks */
159 #if defined(__LITTLE_ENDIAN_BITFIELD)
160 unsigned dc :2; /* Descriptor Code */
161 unsigned reserved :6;
162 #elif defined(__BIG_ENDIAN_BITFIELD)
163 unsigned reserved :6;
164 unsigned dc :2; /* Descriptor Code */
166 #error "Bitfield endianness not defined! Check your byteorder.h"
168 u8 length_msb; /* Block Length (MSB)*/
169 u16 length; /* Block Length */
170 } idefloppy_capacity_descriptor_t;
172 #define CAPACITY_INVALID 0x00
173 #define CAPACITY_UNFORMATTED 0x01
174 #define CAPACITY_CURRENT 0x02
175 #define CAPACITY_NO_CARTRIDGE 0x03
178 * Most of our global data which we need to save even as we leave the
179 * driver due to an interrupt or a timer event is stored in a variable
180 * of type idefloppy_floppy_t, defined below.
182 typedef struct ide_floppy_obj {
184 ide_driver_t *driver;
185 struct gendisk *disk;
187 unsigned int openers; /* protected by BKL for now */
189 /* Current packet command */
191 /* Last failed packet command */
192 idefloppy_pc_t *failed_pc;
193 /* Packet command stack */
194 idefloppy_pc_t pc_stack[IDEFLOPPY_PC_STACK];
195 /* Next free packet command storage space */
197 struct request rq_stack[IDEFLOPPY_PC_STACK];
198 /* We implement a circular array */
202 * Last error information
204 u8 sense_key, asc, ascq;
205 /* delay this long before sending packet command */
207 int progress_indication;
213 int blocks, block_size, bs_factor;
214 /* Last format capacity */
215 idefloppy_capacity_descriptor_t capacity;
216 /* Copy of the flexible disk page */
217 idefloppy_flexible_disk_page_t flexible_disk_page;
220 /* Supports format progress report */
222 /* Status/Action flags */
224 } idefloppy_floppy_t;
226 #define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
229 * Floppy flag bits values.
231 #define IDEFLOPPY_DRQ_INTERRUPT 0 /* DRQ interrupt device */
232 #define IDEFLOPPY_MEDIA_CHANGED 1 /* Media may have changed */
233 #define IDEFLOPPY_USE_READ12 2 /* Use READ12/WRITE12 or READ10/WRITE10 */
234 #define IDEFLOPPY_FORMAT_IN_PROGRESS 3 /* Format in progress */
235 #define IDEFLOPPY_CLIK_DRIVE 4 /* Avoid commands not supported in Clik drive */
236 #define IDEFLOPPY_ZIP_DRIVE 5 /* Requires BH algorithm for packets */
239 * Defines for the mode sense command
241 #define MODE_SENSE_CURRENT 0x00
242 #define MODE_SENSE_CHANGEABLE 0x01
243 #define MODE_SENSE_DEFAULT 0x02
244 #define MODE_SENSE_SAVED 0x03
247 * IOCTLs used in low-level formatting.
250 #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
251 #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
252 #define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
253 #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
256 * Error codes which are returned in rq->errors to the higher part
259 #define IDEFLOPPY_ERROR_GENERAL 101
262 * The following is used to format the general configuration word of
263 * the ATAPI IDENTIFY DEVICE command.
265 struct idefloppy_id_gcw {
266 #if defined(__LITTLE_ENDIAN_BITFIELD)
267 unsigned packet_size :2; /* Packet Size */
268 unsigned reserved234 :3; /* Reserved */
269 unsigned drq_type :2; /* Command packet DRQ type */
270 unsigned removable :1; /* Removable media */
271 unsigned device_type :5; /* Device type */
272 unsigned reserved13 :1; /* Reserved */
273 unsigned protocol :2; /* Protocol type */
274 #elif defined(__BIG_ENDIAN_BITFIELD)
275 unsigned protocol :2; /* Protocol type */
276 unsigned reserved13 :1; /* Reserved */
277 unsigned device_type :5; /* Device type */
278 unsigned removable :1; /* Removable media */
279 unsigned drq_type :2; /* Command packet DRQ type */
280 unsigned reserved234 :3; /* Reserved */
281 unsigned packet_size :2; /* Packet Size */
283 #error "Bitfield endianness not defined! Check your byteorder.h"
288 * REQUEST SENSE packet command result - Data Format.
291 #if defined(__LITTLE_ENDIAN_BITFIELD)
292 unsigned error_code :7; /* Current error (0x70) */
293 unsigned valid :1; /* The information field conforms to SFF-8070i */
294 u8 reserved1 :8; /* Reserved */
295 unsigned sense_key :4; /* Sense Key */
296 unsigned reserved2_4 :1; /* Reserved */
297 unsigned ili :1; /* Incorrect Length Indicator */
298 unsigned reserved2_67 :2;
299 #elif defined(__BIG_ENDIAN_BITFIELD)
300 unsigned valid :1; /* The information field conforms to SFF-8070i */
301 unsigned error_code :7; /* Current error (0x70) */
302 u8 reserved1 :8; /* Reserved */
303 unsigned reserved2_67 :2;
304 unsigned ili :1; /* Incorrect Length Indicator */
305 unsigned reserved2_4 :1; /* Reserved */
306 unsigned sense_key :4; /* Sense Key */
308 #error "Bitfield endianness not defined! Check your byteorder.h"
310 u32 information __attribute__ ((packed));
311 u8 asl; /* Additional sense length (n-7) */
312 u32 command_specific; /* Additional command specific information */
313 u8 asc; /* Additional Sense Code */
314 u8 ascq; /* Additional Sense Code Qualifier */
315 u8 replaceable_unit_code; /* Field Replaceable Unit Code */
317 u8 pad[2]; /* Padding to 20 bytes */
318 } idefloppy_request_sense_result_t;
321 * Pages of the SELECT SENSE / MODE SENSE packet commands.
322 * See SFF-8070i spec.
324 #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
325 #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
328 * Mode Parameter Header for the MODE SENSE packet command
331 u16 mode_data_length; /* Length of the following data transfer */
332 u8 medium_type; /* Medium Type */
333 #if defined(__LITTLE_ENDIAN_BITFIELD)
334 unsigned reserved3 :7;
335 unsigned wp :1; /* Write protect */
336 #elif defined(__BIG_ENDIAN_BITFIELD)
337 unsigned wp :1; /* Write protect */
338 unsigned reserved3 :7;
340 #error "Bitfield endianness not defined! Check your byteorder.h"
343 } idefloppy_mode_parameter_header_t;
345 static DEFINE_MUTEX(idefloppy_ref_mutex);
347 #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
349 #define ide_floppy_g(disk) \
350 container_of((disk)->private_data, struct ide_floppy_obj, driver)
352 static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
354 struct ide_floppy_obj *floppy = NULL;
356 mutex_lock(&idefloppy_ref_mutex);
357 floppy = ide_floppy_g(disk);
359 kref_get(&floppy->kref);
360 mutex_unlock(&idefloppy_ref_mutex);
364 static void idefloppy_cleanup_obj(struct kref *);
366 static void ide_floppy_put(struct ide_floppy_obj *floppy)
368 mutex_lock(&idefloppy_ref_mutex);
369 kref_put(&floppy->kref, idefloppy_cleanup_obj);
370 mutex_unlock(&idefloppy_ref_mutex);
374 * Too bad. The drive wants to send us data which we are not ready to accept.
375 * Just throw it away.
377 static void idefloppy_discard_data (ide_drive_t *drive, unsigned int bcount)
380 (void) HWIF(drive)->INB(IDE_DATA_REG);
383 #if IDEFLOPPY_DEBUG_BUGS
384 static void idefloppy_write_zeros (ide_drive_t *drive, unsigned int bcount)
387 HWIF(drive)->OUTB(0, IDE_DATA_REG);
389 #endif /* IDEFLOPPY_DEBUG_BUGS */
393 * idefloppy_do_end_request is used to finish servicing a request.
395 * For read/write requests, we will call ide_end_request to pass to the
398 static int idefloppy_do_end_request(ide_drive_t *drive, int uptodate, int nsecs)
400 idefloppy_floppy_t *floppy = drive->driver_data;
401 struct request *rq = HWGROUP(drive)->rq;
404 debug_log("Reached %s\n", __func__);
407 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
408 case 1: error = 0; break;
409 default: error = uptodate;
412 floppy->failed_pc = NULL;
413 /* Why does this happen? */
416 if (!blk_special_request(rq)) {
417 /* our real local end request function */
418 ide_end_request(drive, uptodate, nsecs);
422 /* fixme: need to move this local also */
423 ide_end_drive_cmd(drive, 0, 0);
427 static void idefloppy_input_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
429 struct request *rq = pc->rq;
430 struct bio_vec *bvec;
431 struct req_iterator iter;
436 rq_for_each_segment(bvec, rq, iter) {
440 count = min(bvec->bv_len, bcount);
442 data = bvec_kmap_irq(bvec, &flags);
443 drive->hwif->atapi_input_bytes(drive, data, count);
444 bvec_kunmap_irq(data, &flags);
447 pc->b_count += count;
451 idefloppy_do_end_request(drive, 1, done >> 9);
454 printk(KERN_ERR "%s: leftover data in idefloppy_input_buffers, bcount == %d\n", drive->name, bcount);
455 idefloppy_discard_data(drive, bcount);
459 static void idefloppy_output_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
461 struct request *rq = pc->rq;
462 struct req_iterator iter;
463 struct bio_vec *bvec;
468 rq_for_each_segment(bvec, rq, iter) {
472 count = min(bvec->bv_len, bcount);
474 data = bvec_kmap_irq(bvec, &flags);
475 drive->hwif->atapi_output_bytes(drive, data, count);
476 bvec_kunmap_irq(data, &flags);
479 pc->b_count += count;
483 idefloppy_do_end_request(drive, 1, done >> 9);
485 #if IDEFLOPPY_DEBUG_BUGS
487 printk(KERN_ERR "%s: leftover data in idefloppy_output_buffers, bcount == %d\n", drive->name, bcount);
488 idefloppy_write_zeros(drive, bcount);
493 static void idefloppy_update_buffers (ide_drive_t *drive, idefloppy_pc_t *pc)
495 struct request *rq = pc->rq;
496 struct bio *bio = rq->bio;
498 while ((bio = rq->bio) != NULL)
499 idefloppy_do_end_request(drive, 1, 0);
503 * idefloppy_queue_pc_head generates a new packet command request in front
504 * of the request queue, before the current request, so that it will be
505 * processed immediately, on the next pass through the driver.
507 static void idefloppy_queue_pc_head (ide_drive_t *drive,idefloppy_pc_t *pc,struct request *rq)
509 struct ide_floppy_obj *floppy = drive->driver_data;
511 ide_init_drive_cmd(rq);
512 rq->buffer = (char *) pc;
513 rq->cmd_type = REQ_TYPE_SPECIAL;
514 rq->rq_disk = floppy->disk;
515 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
518 static idefloppy_pc_t *idefloppy_next_pc_storage (ide_drive_t *drive)
520 idefloppy_floppy_t *floppy = drive->driver_data;
522 if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
523 floppy->pc_stack_index=0;
524 return (&floppy->pc_stack[floppy->pc_stack_index++]);
527 static struct request *idefloppy_next_rq_storage (ide_drive_t *drive)
529 idefloppy_floppy_t *floppy = drive->driver_data;
531 if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
532 floppy->rq_stack_index = 0;
533 return (&floppy->rq_stack[floppy->rq_stack_index++]);
537 * idefloppy_analyze_error is called on each failed packet command retry
538 * to analyze the request sense.
540 static void idefloppy_analyze_error (ide_drive_t *drive,idefloppy_request_sense_result_t *result)
542 idefloppy_floppy_t *floppy = drive->driver_data;
544 floppy->sense_key = result->sense_key;
545 floppy->asc = result->asc;
546 floppy->ascq = result->ascq;
547 floppy->progress_indication = result->sksv[0] & 0x80 ?
548 (u16)get_unaligned((u16 *)(result->sksv+1)):0x10000;
549 if (floppy->failed_pc)
550 debug_log("pc = %x, sense key = %x, asc = %x, ascq = %x\n",
551 floppy->failed_pc->c[0], result->sense_key,
552 result->asc, result->ascq);
554 debug_log("sense key = %x, asc = %x, ascq = %x\n",
555 result->sense_key, result->asc, result->ascq);
558 static void idefloppy_request_sense_callback (ide_drive_t *drive)
560 idefloppy_floppy_t *floppy = drive->driver_data;
562 debug_log("Reached %s\n", __func__);
564 if (!floppy->pc->error) {
565 idefloppy_analyze_error(drive,(idefloppy_request_sense_result_t *) floppy->pc->buffer);
566 idefloppy_do_end_request(drive, 1, 0);
568 printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting request!\n");
569 idefloppy_do_end_request(drive, 0, 0);
574 * General packet command callback function.
576 static void idefloppy_pc_callback (ide_drive_t *drive)
578 idefloppy_floppy_t *floppy = drive->driver_data;
580 debug_log("Reached %s\n", __func__);
582 idefloppy_do_end_request(drive, floppy->pc->error ? 0 : 1, 0);
586 * idefloppy_init_pc initializes a packet command.
588 static void idefloppy_init_pc (idefloppy_pc_t *pc)
590 memset(pc->c, 0, 12);
593 pc->request_transfer = 0;
594 pc->buffer = pc->pc_buffer;
595 pc->buffer_size = IDEFLOPPY_PC_BUFFER_SIZE;
596 pc->callback = &idefloppy_pc_callback;
599 static void idefloppy_create_request_sense_cmd (idefloppy_pc_t *pc)
601 idefloppy_init_pc(pc);
602 pc->c[0] = GPCMD_REQUEST_SENSE;
604 pc->request_transfer = 18;
605 pc->callback = &idefloppy_request_sense_callback;
609 * idefloppy_retry_pc is called when an error was detected during the
610 * last packet command. We queue a request sense packet command in
611 * the head of the request list.
613 static void idefloppy_retry_pc (ide_drive_t *drive)
618 (void)drive->hwif->INB(IDE_ERROR_REG);
619 pc = idefloppy_next_pc_storage(drive);
620 rq = idefloppy_next_rq_storage(drive);
621 idefloppy_create_request_sense_cmd(pc);
622 idefloppy_queue_pc_head(drive, pc, rq);
626 * idefloppy_pc_intr is the usual interrupt handler which will be called
627 * during a packet command.
629 static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
631 idefloppy_floppy_t *floppy = drive->driver_data;
632 ide_hwif_t *hwif = drive->hwif;
633 idefloppy_pc_t *pc = floppy->pc;
634 struct request *rq = pc->rq;
639 debug_log("Reached %s interrupt handler\n", __func__);
641 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
642 if (HWIF(drive)->ide_dma_end(drive)) {
643 set_bit(PC_DMA_ERROR, &pc->flags);
645 pc->actually_transferred = pc->request_transfer;
646 idefloppy_update_buffers(drive, pc);
648 debug_log("DMA finished\n");
651 /* Clear the interrupt */
652 stat = drive->hwif->INB(IDE_STATUS_REG);
654 if ((stat & DRQ_STAT) == 0) { /* No more interrupts */
655 debug_log("Packet command completed, %d bytes transferred\n",
656 pc->actually_transferred);
657 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
659 local_irq_enable_in_hardirq();
661 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
663 debug_log("%s: I/O error\n", drive->name);
665 if (pc->c[0] == GPCMD_REQUEST_SENSE) {
666 printk(KERN_ERR "ide-floppy: I/O error in "
667 "request sense command\n");
668 return ide_do_reset(drive);
670 /* Retry operation */
671 idefloppy_retry_pc(drive);
672 /* queued, but not started */
676 if (floppy->failed_pc == pc)
677 floppy->failed_pc = NULL;
678 /* Command finished - Call the callback function */
683 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
684 printk(KERN_ERR "ide-floppy: The floppy wants to issue "
685 "more interrupts in DMA mode\n");
687 return ide_do_reset(drive);
690 /* Get the number of bytes to transfer */
691 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
692 hwif->INB(IDE_BCOUNTL_REG);
693 /* on this interrupt */
694 ireason = hwif->INB(IDE_IREASON_REG);
697 printk(KERN_ERR "ide-floppy: CoD != 0 in idefloppy_pc_intr\n");
698 return ide_do_reset(drive);
700 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
701 /* Hopefully, we will never get here */
702 printk(KERN_ERR "ide-floppy: We wanted to %s, ",
703 (ireason & IO) ? "Write" : "Read");
704 printk(KERN_ERR "but the floppy wants us to %s !\n",
705 (ireason & IO) ? "Read" : "Write");
706 return ide_do_reset(drive);
708 if (!test_bit(PC_WRITING, &pc->flags)) {
709 /* Reading - Check that we have enough space */
710 temp = pc->actually_transferred + bcount;
711 if (temp > pc->request_transfer) {
712 if (temp > pc->buffer_size) {
713 printk(KERN_ERR "ide-floppy: The floppy wants "
714 "to send us more data than expected "
715 "- discarding data\n");
716 idefloppy_discard_data(drive, bcount);
718 ide_set_handler(drive,
724 debug_log("The floppy wants to send us more data than"
725 " expected - allowing transfer\n");
728 if (test_bit(PC_WRITING, &pc->flags)) {
729 if (pc->buffer != NULL)
730 /* Write the current buffer */
731 hwif->atapi_output_bytes(drive, pc->current_position,
734 idefloppy_output_buffers(drive, pc, bcount);
736 if (pc->buffer != NULL)
737 /* Read the current buffer */
738 hwif->atapi_input_bytes(drive, pc->current_position,
741 idefloppy_input_buffers(drive, pc, bcount);
743 /* Update the current position */
744 pc->actually_transferred += bcount;
745 pc->current_position += bcount;
747 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL); /* And set the interrupt handler again */
752 * This is the original routine that did the packet transfer.
753 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
754 * for that drive below. The algorithm is chosen based on drive type
756 static ide_startstop_t idefloppy_transfer_pc (ide_drive_t *drive)
758 ide_startstop_t startstop;
759 idefloppy_floppy_t *floppy = drive->driver_data;
762 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
763 printk(KERN_ERR "ide-floppy: Strange, packet command "
764 "initiated yet DRQ isn't asserted\n");
767 ireason = drive->hwif->INB(IDE_IREASON_REG);
768 if ((ireason & CD) == 0 || (ireason & IO)) {
769 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
770 "issuing a packet command\n");
771 return ide_do_reset(drive);
774 /* Set the interrupt routine */
775 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
776 /* Send the actual packet */
777 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
783 * What we have here is a classic case of a top half / bottom half
784 * interrupt service routine. In interrupt mode, the device sends
785 * an interrupt to signal it's ready to receive a packet. However,
786 * we need to delay about 2-3 ticks before issuing the packet or we
789 * So, follow carefully. transfer_pc1 is called as an interrupt (or
790 * directly). In either case, when the device says it's ready for a
791 * packet, we schedule the packet transfer to occur about 2-3 ticks
792 * later in transfer_pc2.
794 static int idefloppy_transfer_pc2 (ide_drive_t *drive)
796 idefloppy_floppy_t *floppy = drive->driver_data;
798 /* Send the actual packet */
799 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
800 /* Timeout for the packet command */
801 return IDEFLOPPY_WAIT_CMD;
804 static ide_startstop_t idefloppy_transfer_pc1 (ide_drive_t *drive)
806 idefloppy_floppy_t *floppy = drive->driver_data;
807 ide_startstop_t startstop;
810 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
811 printk(KERN_ERR "ide-floppy: Strange, packet command "
812 "initiated yet DRQ isn't asserted\n");
815 ireason = drive->hwif->INB(IDE_IREASON_REG);
816 if ((ireason & CD) == 0 || (ireason & IO)) {
817 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
818 "while issuing a packet command\n");
819 return ide_do_reset(drive);
822 * The following delay solves a problem with ATAPI Zip 100 drives
823 * where the Busy flag was apparently being deasserted before the
824 * unit was ready to receive data. This was happening on a
825 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
826 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
827 * used until after the packet is moved in about 50 msec.
830 ide_set_handler(drive,
831 &idefloppy_pc_intr, /* service routine for packet command */
832 floppy->ticks, /* wait this long before "failing" */
833 &idefloppy_transfer_pc2); /* fail == transfer_pc2 */
838 * idefloppy_should_report_error()
840 * Supresses error messages resulting from Medium not present
842 static inline int idefloppy_should_report_error(idefloppy_floppy_t *floppy)
844 if (floppy->sense_key == 0x02 &&
845 floppy->asc == 0x3a &&
846 floppy->ascq == 0x00)
852 * Issue a packet command
854 static ide_startstop_t idefloppy_issue_pc (ide_drive_t *drive, idefloppy_pc_t *pc)
856 idefloppy_floppy_t *floppy = drive->driver_data;
857 ide_hwif_t *hwif = drive->hwif;
858 ide_handler_t *pkt_xfer_routine;
862 if (floppy->failed_pc == NULL &&
863 pc->c[0] != GPCMD_REQUEST_SENSE)
864 floppy->failed_pc = pc;
865 /* Set the current packet command */
868 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES ||
869 test_bit(PC_ABORT, &pc->flags)) {
871 * We will "abort" retrying a packet command in case
872 * a legitimate error code was received.
874 if (!test_bit(PC_ABORT, &pc->flags)) {
875 if (!test_bit(PC_SUPPRESS_ERROR, &pc->flags)) {
876 if (idefloppy_should_report_error(floppy))
877 printk(KERN_ERR "ide-floppy: %s: I/O error, "
878 "pc = %2x, key = %2x, "
879 "asc = %2x, ascq = %2x\n",
880 drive->name, pc->c[0],
882 floppy->asc, floppy->ascq);
885 pc->error = IDEFLOPPY_ERROR_GENERAL;
887 floppy->failed_pc = NULL;
892 debug_log("Retry number - %d\n", pc->retries);
895 /* We haven't transferred any data yet */
896 pc->actually_transferred = 0;
897 pc->current_position = pc->buffer;
898 bcount = min(pc->request_transfer, 63 * 1024);
900 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags))
905 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
906 dma = !hwif->dma_setup(drive);
908 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
909 IDE_TFLAG_OUT_DEVICE, bcount, dma);
911 if (dma) { /* Begin DMA, if necessary */
912 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
913 hwif->dma_start(drive);
916 /* Can we transfer the packet when we get the interrupt or wait? */
917 if (test_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags)) {
919 pkt_xfer_routine = &idefloppy_transfer_pc1;
922 pkt_xfer_routine = &idefloppy_transfer_pc;
925 if (test_bit (IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags)) {
926 /* Issue the packet command */
927 ide_execute_command(drive, WIN_PACKETCMD,
933 /* Issue the packet command */
934 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
935 return (*pkt_xfer_routine) (drive);
939 static void idefloppy_rw_callback (ide_drive_t *drive)
941 debug_log("Reached %s\n", __func__);
943 idefloppy_do_end_request(drive, 1, 0);
947 static void idefloppy_create_prevent_cmd (idefloppy_pc_t *pc, int prevent)
949 debug_log("creating prevent removal command, prevent = %d\n", prevent);
951 idefloppy_init_pc(pc);
952 pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
956 static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t *pc)
958 idefloppy_init_pc(pc);
959 pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
962 pc->request_transfer = 255;
965 static void idefloppy_create_format_unit_cmd (idefloppy_pc_t *pc, int b, int l,
968 idefloppy_init_pc(pc);
969 pc->c[0] = GPCMD_FORMAT_UNIT;
972 memset(pc->buffer, 0, 12);
973 pc->buffer[1] = 0xA2;
974 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
976 if (flags & 1) /* Verify bit on... */
977 pc->buffer[1] ^= 0x20; /* ... turn off DCRT bit */
980 put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buffer[4]));
981 put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buffer[8]));
983 set_bit(PC_WRITING, &pc->flags);
987 * A mode sense command is used to "sense" floppy parameters.
989 static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t *pc, u8 page_code, u8 type)
991 u16 length = sizeof(idefloppy_mode_parameter_header_t);
993 idefloppy_init_pc(pc);
994 pc->c[0] = GPCMD_MODE_SENSE_10;
996 pc->c[2] = page_code + (type << 6);
999 case IDEFLOPPY_CAPABILITIES_PAGE:
1002 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
1006 printk(KERN_ERR "ide-floppy: unsupported page code "
1007 "in create_mode_sense_cmd\n");
1009 put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
1010 pc->request_transfer = length;
1013 static void idefloppy_create_start_stop_cmd (idefloppy_pc_t *pc, int start)
1015 idefloppy_init_pc(pc);
1016 pc->c[0] = GPCMD_START_STOP_UNIT;
1020 static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc)
1022 idefloppy_init_pc(pc);
1023 pc->c[0] = GPCMD_TEST_UNIT_READY;
1026 static void idefloppy_create_rw_cmd (idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq, unsigned long sector)
1028 int block = sector / floppy->bs_factor;
1029 int blocks = rq->nr_sectors / floppy->bs_factor;
1030 int cmd = rq_data_dir(rq);
1032 debug_log("create_rw1%d_cmd: block == %d, blocks == %d\n",
1033 2 * test_bit (IDEFLOPPY_USE_READ12, &floppy->flags),
1036 idefloppy_init_pc(pc);
1037 if (test_bit(IDEFLOPPY_USE_READ12, &floppy->flags)) {
1038 pc->c[0] = cmd == READ ? GPCMD_READ_12 : GPCMD_WRITE_12;
1039 put_unaligned(cpu_to_be32(blocks), (unsigned int *) &pc->c[6]);
1041 pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
1042 put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
1044 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
1045 pc->callback = &idefloppy_rw_callback;
1047 pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
1048 if (rq->cmd_flags & REQ_RW)
1049 set_bit(PC_WRITING, &pc->flags);
1051 pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
1052 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1056 idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq)
1058 idefloppy_init_pc(pc);
1059 pc->callback = &idefloppy_rw_callback;
1060 memcpy(pc->c, rq->cmd, sizeof(pc->c));
1062 pc->b_count = rq->data_len;
1063 if (rq->data_len && rq_data_dir(rq) == WRITE)
1064 set_bit(PC_WRITING, &pc->flags);
1065 pc->buffer = rq->data;
1067 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1070 * possibly problematic, doesn't look like ide-floppy correctly
1071 * handled scattered requests if dma fails...
1073 pc->request_transfer = pc->buffer_size = rq->data_len;
1077 * idefloppy_do_request is our request handling function.
1079 static ide_startstop_t idefloppy_do_request (ide_drive_t *drive, struct request *rq, sector_t block_s)
1081 idefloppy_floppy_t *floppy = drive->driver_data;
1083 unsigned long block = (unsigned long)block_s;
1085 debug_log("dev: %s, cmd_type: %x, errors: %d\n",
1086 rq->rq_disk ? rq->rq_disk->disk_name : "?",
1087 rq->cmd_type, rq->errors);
1088 debug_log("sector: %ld, nr_sectors: %ld, "
1089 "current_nr_sectors: %d\n", (long)rq->sector,
1090 rq->nr_sectors, rq->current_nr_sectors);
1092 if (rq->errors >= ERROR_MAX) {
1093 if (floppy->failed_pc != NULL) {
1094 if (idefloppy_should_report_error(floppy))
1095 printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x,"
1096 " key = %2x, asc = %2x, ascq = %2x\n",
1097 drive->name, floppy->failed_pc->c[0],
1098 floppy->sense_key, floppy->asc, floppy->ascq);
1101 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
1103 idefloppy_do_end_request(drive, 0, 0);
1106 if (blk_fs_request(rq)) {
1107 if (((long)rq->sector % floppy->bs_factor) ||
1108 (rq->nr_sectors % floppy->bs_factor)) {
1109 printk("%s: unsupported r/w request size\n",
1111 idefloppy_do_end_request(drive, 0, 0);
1114 pc = idefloppy_next_pc_storage(drive);
1115 idefloppy_create_rw_cmd(floppy, pc, rq, block);
1116 } else if (blk_special_request(rq)) {
1117 pc = (idefloppy_pc_t *) rq->buffer;
1118 } else if (blk_pc_request(rq)) {
1119 pc = idefloppy_next_pc_storage(drive);
1120 idefloppy_blockpc_cmd(floppy, pc, rq);
1122 blk_dump_rq_flags(rq,
1123 "ide-floppy: unsupported command in queue");
1124 idefloppy_do_end_request(drive, 0, 0);
1129 return idefloppy_issue_pc(drive, pc);
1133 * idefloppy_queue_pc_tail adds a special packet command request to the
1134 * tail of the request queue, and waits for it to be serviced.
1136 static int idefloppy_queue_pc_tail (ide_drive_t *drive,idefloppy_pc_t *pc)
1138 struct ide_floppy_obj *floppy = drive->driver_data;
1141 ide_init_drive_cmd (&rq);
1142 rq.buffer = (char *) pc;
1143 rq.cmd_type = REQ_TYPE_SPECIAL;
1144 rq.rq_disk = floppy->disk;
1146 return ide_do_drive_cmd(drive, &rq, ide_wait);
1150 * Look at the flexible disk page parameters. We will ignore the CHS
1151 * capacity parameters and use the LBA parameters instead.
1153 static int idefloppy_get_flexible_disk_page (ide_drive_t *drive)
1155 idefloppy_floppy_t *floppy = drive->driver_data;
1157 idefloppy_mode_parameter_header_t *header;
1158 idefloppy_flexible_disk_page_t *page;
1159 int capacity, lba_capacity;
1161 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE, MODE_SENSE_CURRENT);
1162 if (idefloppy_queue_pc_tail(drive,&pc)) {
1163 printk(KERN_ERR "ide-floppy: Can't get flexible disk "
1164 "page parameters\n");
1167 header = (idefloppy_mode_parameter_header_t *) pc.buffer;
1168 floppy->wp = header->wp;
1169 set_disk_ro(floppy->disk, floppy->wp);
1170 page = (idefloppy_flexible_disk_page_t *) (header + 1);
1172 page->transfer_rate = be16_to_cpu(page->transfer_rate);
1173 page->sector_size = be16_to_cpu(page->sector_size);
1174 page->cyls = be16_to_cpu(page->cyls);
1175 page->rpm = be16_to_cpu(page->rpm);
1176 capacity = page->cyls * page->heads * page->sectors * page->sector_size;
1177 if (memcmp (page, &floppy->flexible_disk_page, sizeof (idefloppy_flexible_disk_page_t)))
1178 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
1179 "%d sector size, %d rpm\n",
1180 drive->name, capacity / 1024, page->cyls,
1181 page->heads, page->sectors,
1182 page->transfer_rate / 8, page->sector_size, page->rpm);
1184 floppy->flexible_disk_page = *page;
1185 drive->bios_cyl = page->cyls;
1186 drive->bios_head = page->heads;
1187 drive->bios_sect = page->sectors;
1188 lba_capacity = floppy->blocks * floppy->block_size;
1189 if (capacity < lba_capacity) {
1190 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
1191 "bytes, but the drive only handles %d\n",
1192 drive->name, lba_capacity, capacity);
1193 floppy->blocks = floppy->block_size ? capacity / floppy->block_size : 0;
1198 static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
1200 idefloppy_floppy_t *floppy = drive->driver_data;
1204 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
1205 MODE_SENSE_CURRENT);
1207 set_bit(PC_SUPPRESS_ERROR, &pc.flags);
1208 if (idefloppy_queue_pc_tail(drive, &pc))
1211 floppy->srfp = pc.buffer[8 + 2] & 0x40;
1216 * Determine if a media is present in the floppy drive, and if so,
1219 static int idefloppy_get_capacity (ide_drive_t *drive)
1221 idefloppy_floppy_t *floppy = drive->driver_data;
1223 idefloppy_capacity_header_t *header;
1224 idefloppy_capacity_descriptor_t *descriptor;
1225 int i, descriptors, rc = 1, blocks, length;
1227 drive->bios_cyl = 0;
1228 drive->bios_head = drive->bios_sect = 0;
1230 floppy->bs_factor = 1;
1231 set_capacity(floppy->disk, 0);
1233 idefloppy_create_read_capacity_cmd(&pc);
1234 if (idefloppy_queue_pc_tail(drive, &pc)) {
1235 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1238 header = (idefloppy_capacity_header_t *) pc.buffer;
1239 descriptors = header->length / sizeof(idefloppy_capacity_descriptor_t);
1240 descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
1242 for (i = 0; i < descriptors; i++, descriptor++) {
1243 blocks = descriptor->blocks = be32_to_cpu(descriptor->blocks);
1244 length = descriptor->length = be16_to_cpu(descriptor->length);
1248 switch (descriptor->dc) {
1249 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1250 case CAPACITY_UNFORMATTED:
1251 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags))
1253 * If it is not a clik drive, break out
1254 * (maintains previous driver behaviour)
1257 case CAPACITY_CURRENT:
1258 /* Normal Zip/LS-120 disks */
1259 if (memcmp(descriptor, &floppy->capacity, sizeof (idefloppy_capacity_descriptor_t)))
1260 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
1261 "sector size\n", drive->name,
1262 blocks * length / 1024, blocks, length);
1263 floppy->capacity = *descriptor;
1264 if (!length || length % 512) {
1265 printk(KERN_NOTICE "%s: %d bytes block size "
1266 "not supported\n", drive->name, length);
1268 floppy->blocks = blocks;
1269 floppy->block_size = length;
1270 if ((floppy->bs_factor = length / 512) != 1)
1271 printk(KERN_NOTICE "%s: warning: non "
1272 "512 bytes block size not "
1273 "fully supported\n",
1278 case CAPACITY_NO_CARTRIDGE:
1280 * This is a KERN_ERR so it appears on screen
1281 * for the user to see
1283 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
1285 case CAPACITY_INVALID:
1286 printk(KERN_ERR "%s: Invalid capacity for disk "
1287 "in drive\n", drive->name);
1292 debug_log("Descriptor 0 Code: %d\n", descriptor->dc);
1294 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
1295 i, blocks * length / 1024, blocks, length);
1298 /* Clik! disk does not support get_flexible_disk_page */
1299 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1300 (void) idefloppy_get_flexible_disk_page(drive);
1303 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
1308 ** Obtain the list of formattable capacities.
1309 ** Very similar to idefloppy_get_capacity, except that we push the capacity
1310 ** descriptors to userland, instead of our own structures.
1312 ** Userland gives us the following structure:
1314 ** struct idefloppy_format_capacities {
1322 ** userland initializes nformats to the number of allocated formats[]
1323 ** records. On exit we set nformats to the number of records we've
1324 ** actually initialized.
1328 static int idefloppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
1331 idefloppy_capacity_header_t *header;
1332 idefloppy_capacity_descriptor_t *descriptor;
1333 int i, descriptors, blocks, length;
1338 if (get_user(u_array_size, arg))
1341 if (u_array_size <= 0)
1344 idefloppy_create_read_capacity_cmd(&pc);
1345 if (idefloppy_queue_pc_tail(drive, &pc)) {
1346 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1349 header = (idefloppy_capacity_header_t *) pc.buffer;
1350 descriptors = header->length /
1351 sizeof(idefloppy_capacity_descriptor_t);
1352 descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
1358 ** We always skip the first capacity descriptor. That's the
1359 ** current capacity. We are interested in the remaining descriptors,
1360 ** the formattable capacities.
1363 for (i=0; i<descriptors; i++, descriptor++) {
1364 if (u_index >= u_array_size)
1365 break; /* User-supplied buffer too small */
1367 continue; /* Skip the first descriptor */
1369 blocks = be32_to_cpu(descriptor->blocks);
1370 length = be16_to_cpu(descriptor->length);
1372 if (put_user(blocks, argp))
1376 if (put_user(length, argp))
1383 if (put_user(u_index, arg))
1389 ** Send ATAPI_FORMAT_UNIT to the drive.
1391 ** Userland gives us the following structure:
1393 ** struct idefloppy_format_command {
1399 ** flags is a bitmask, currently, the only defined flag is:
1401 ** 0x01 - verify media after format.
1404 static int idefloppy_begin_format(ide_drive_t *drive, int __user *arg)
1411 if (get_user(blocks, arg) ||
1412 get_user(length, arg+1) ||
1413 get_user(flags, arg+2)) {
1417 (void) idefloppy_get_sfrp_bit(drive);
1418 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1419 if (idefloppy_queue_pc_tail(drive, &pc)) {
1427 ** Get ATAPI_FORMAT_UNIT progress indication.
1429 ** Userland gives a pointer to an int. The int is set to a progress
1430 ** indicator 0-65536, with 65536=100%.
1432 ** If the drive does not support format progress indication, we just check
1433 ** the dsc bit, and return either 0 or 65536.
1436 static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
1438 idefloppy_floppy_t *floppy = drive->driver_data;
1440 int progress_indication = 0x10000;
1443 idefloppy_create_request_sense_cmd(&pc);
1444 if (idefloppy_queue_pc_tail(drive, &pc)) {
1448 if (floppy->sense_key == 2 &&
1450 floppy->ascq == 4) {
1451 progress_indication = floppy->progress_indication;
1453 /* Else assume format_unit has finished, and we're
1456 unsigned long flags;
1459 local_irq_save(flags);
1460 stat = drive->hwif->INB(IDE_STATUS_REG);
1461 local_irq_restore(flags);
1463 progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
1465 if (put_user(progress_indication, arg))
1472 * Return the current floppy capacity.
1474 static sector_t idefloppy_capacity (ide_drive_t *drive)
1476 idefloppy_floppy_t *floppy = drive->driver_data;
1477 unsigned long capacity = floppy->blocks * floppy->bs_factor;
1483 * idefloppy_identify_device checks if we can support a drive,
1484 * based on the ATAPI IDENTIFY command results.
1486 static int idefloppy_identify_device (ide_drive_t *drive,struct hd_driveid *id)
1488 struct idefloppy_id_gcw gcw;
1489 #if IDEFLOPPY_DEBUG_INFO
1491 #endif /* IDEFLOPPY_DEBUG_INFO */
1493 *((u16 *) &gcw) = id->config;
1496 /* kludge for Apple PowerBook internal zip */
1497 if ((gcw.device_type == 5) &&
1498 !strstr(id->model, "CD-ROM") &&
1499 strstr(id->model, "ZIP"))
1500 gcw.device_type = 0;
1503 #if IDEFLOPPY_DEBUG_INFO
1504 printk(KERN_INFO "Dumping ATAPI Identify Device floppy parameters\n");
1505 switch (gcw.protocol) {
1506 case 0: case 1: sprintf(buffer, "ATA");break;
1507 case 2: sprintf(buffer, "ATAPI");break;
1508 case 3: sprintf(buffer, "Reserved (Unknown to ide-floppy)");break;
1510 printk(KERN_INFO "Protocol Type: %s\n", buffer);
1511 switch (gcw.device_type) {
1512 case 0: sprintf(buffer, "Direct-access Device");break;
1513 case 1: sprintf(buffer, "Streaming Tape Device");break;
1514 case 2: case 3: case 4: sprintf (buffer, "Reserved");break;
1515 case 5: sprintf(buffer, "CD-ROM Device");break;
1516 case 6: sprintf(buffer, "Reserved");
1517 case 7: sprintf(buffer, "Optical memory Device");break;
1518 case 0x1f: sprintf(buffer, "Unknown or no Device type");break;
1519 default: sprintf(buffer, "Reserved");
1521 printk(KERN_INFO "Device Type: %x - %s\n", gcw.device_type, buffer);
1522 printk(KERN_INFO "Removable: %s\n",gcw.removable ? "Yes":"No");
1523 switch (gcw.drq_type) {
1524 case 0: sprintf(buffer, "Microprocessor DRQ");break;
1525 case 1: sprintf(buffer, "Interrupt DRQ");break;
1526 case 2: sprintf(buffer, "Accelerated DRQ");break;
1527 case 3: sprintf(buffer, "Reserved");break;
1529 printk(KERN_INFO "Command Packet DRQ Type: %s\n", buffer);
1530 switch (gcw.packet_size) {
1531 case 0: sprintf(buffer, "12 bytes");break;
1532 case 1: sprintf(buffer, "16 bytes");break;
1533 default: sprintf(buffer, "Reserved");break;
1535 printk(KERN_INFO "Command Packet Size: %s\n", buffer);
1536 #endif /* IDEFLOPPY_DEBUG_INFO */
1538 if (gcw.protocol != 2)
1539 printk(KERN_ERR "ide-floppy: Protocol is not ATAPI\n");
1540 else if (gcw.device_type != 0)
1541 printk(KERN_ERR "ide-floppy: Device type is not set to floppy\n");
1542 else if (!gcw.removable)
1543 printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
1544 else if (gcw.drq_type == 3) {
1545 printk(KERN_ERR "ide-floppy: Sorry, DRQ type %d not supported\n", gcw.drq_type);
1546 } else if (gcw.packet_size != 0) {
1547 printk(KERN_ERR "ide-floppy: Packet size is not 12 bytes long\n");
1553 #ifdef CONFIG_IDE_PROC_FS
1554 static void idefloppy_add_settings(ide_drive_t *drive)
1556 idefloppy_floppy_t *floppy = drive->driver_data;
1559 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
1561 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
1562 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
1563 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
1564 ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &floppy->ticks, NULL);
1567 static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
1571 * Driver initialization.
1573 static void idefloppy_setup (ide_drive_t *drive, idefloppy_floppy_t *floppy)
1575 struct idefloppy_id_gcw gcw;
1577 *((u16 *) &gcw) = drive->id->config;
1578 floppy->pc = floppy->pc_stack;
1579 if (gcw.drq_type == 1)
1580 set_bit(IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags);
1582 * We used to check revisions here. At this point however
1583 * I'm giving up. Just assume they are all broken, its easier.
1585 * The actual reason for the workarounds was likely
1586 * a driver bug after all rather than a firmware bug,
1587 * and the workaround below used to hide it. It should
1588 * be fixed as of version 1.9, but to be on the safe side
1589 * we'll leave the limitation below for the 2.2.x tree.
1592 if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
1593 set_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags);
1594 /* This value will be visible in the /proc/ide/hdx/settings */
1595 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1596 blk_queue_max_sectors(drive->queue, 64);
1600 * Guess what? The IOMEGA Clik! drive also needs the
1601 * above fix. It makes nasty clicking noises without
1602 * it, so please don't remove this.
1604 if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
1605 blk_queue_max_sectors(drive->queue, 64);
1606 set_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags);
1610 (void) idefloppy_get_capacity(drive);
1611 idefloppy_add_settings(drive);
1614 static void ide_floppy_remove(ide_drive_t *drive)
1616 idefloppy_floppy_t *floppy = drive->driver_data;
1617 struct gendisk *g = floppy->disk;
1619 ide_proc_unregister_driver(drive, floppy->driver);
1623 ide_floppy_put(floppy);
1626 static void idefloppy_cleanup_obj(struct kref *kref)
1628 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1629 ide_drive_t *drive = floppy->drive;
1630 struct gendisk *g = floppy->disk;
1632 drive->driver_data = NULL;
1633 g->private_data = NULL;
1638 #ifdef CONFIG_IDE_PROC_FS
1639 static int proc_idefloppy_read_capacity
1640 (char *page, char **start, off_t off, int count, int *eof, void *data)
1642 ide_drive_t*drive = (ide_drive_t *)data;
1645 len = sprintf(page,"%llu\n", (long long)idefloppy_capacity(drive));
1646 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1649 static ide_proc_entry_t idefloppy_proc[] = {
1650 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
1651 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1652 { NULL, 0, NULL, NULL }
1654 #endif /* CONFIG_IDE_PROC_FS */
1656 static int ide_floppy_probe(ide_drive_t *);
1658 static ide_driver_t idefloppy_driver = {
1660 .owner = THIS_MODULE,
1661 .name = "ide-floppy",
1662 .bus = &ide_bus_type,
1664 .probe = ide_floppy_probe,
1665 .remove = ide_floppy_remove,
1666 .version = IDEFLOPPY_VERSION,
1667 .media = ide_floppy,
1668 .supports_dsc_overlap = 0,
1669 .do_request = idefloppy_do_request,
1670 .end_request = idefloppy_do_end_request,
1671 .error = __ide_error,
1672 .abort = __ide_abort,
1673 #ifdef CONFIG_IDE_PROC_FS
1674 .proc = idefloppy_proc,
1678 static int idefloppy_open(struct inode *inode, struct file *filp)
1680 struct gendisk *disk = inode->i_bdev->bd_disk;
1681 struct ide_floppy_obj *floppy;
1686 debug_log("Reached %s\n", __func__);
1688 if (!(floppy = ide_floppy_get(disk)))
1691 drive = floppy->drive;
1695 if (floppy->openers == 1) {
1696 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1699 idefloppy_create_test_unit_ready_cmd(&pc);
1700 if (idefloppy_queue_pc_tail(drive, &pc)) {
1701 idefloppy_create_start_stop_cmd(&pc, 1);
1702 (void) idefloppy_queue_pc_tail(drive, &pc);
1705 if (idefloppy_get_capacity (drive)
1706 && (filp->f_flags & O_NDELAY) == 0
1708 ** Allow O_NDELAY to open a drive without a disk, or with
1709 ** an unreadable disk, so that we can get the format
1710 ** capacity of the drive or begin the format - Sam
1714 goto out_put_floppy;
1717 if (floppy->wp && (filp->f_mode & 2)) {
1719 goto out_put_floppy;
1721 set_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1722 /* IOMEGA Clik! drives do not support lock/unlock commands */
1723 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1724 idefloppy_create_prevent_cmd(&pc, 1);
1725 (void) idefloppy_queue_pc_tail(drive, &pc);
1727 check_disk_change(inode->i_bdev);
1728 } else if (test_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags)) {
1730 goto out_put_floppy;
1736 ide_floppy_put(floppy);
1740 static int idefloppy_release(struct inode *inode, struct file *filp)
1742 struct gendisk *disk = inode->i_bdev->bd_disk;
1743 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1744 ide_drive_t *drive = floppy->drive;
1747 debug_log("Reached %s\n", __func__);
1749 if (floppy->openers == 1) {
1750 /* IOMEGA Clik! drives do not support lock/unlock commands */
1751 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1752 idefloppy_create_prevent_cmd(&pc, 0);
1753 (void) idefloppy_queue_pc_tail(drive, &pc);
1756 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1761 ide_floppy_put(floppy);
1766 static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1768 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1769 ide_drive_t *drive = floppy->drive;
1771 geo->heads = drive->bios_head;
1772 geo->sectors = drive->bios_sect;
1773 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1777 static int idefloppy_ioctl(struct inode *inode, struct file *file,
1778 unsigned int cmd, unsigned long arg)
1780 struct block_device *bdev = inode->i_bdev;
1781 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1782 ide_drive_t *drive = floppy->drive;
1783 void __user *argp = (void __user *)arg;
1785 int prevent = (arg) ? 1 : 0;
1792 case CDROM_LOCKDOOR:
1793 if (floppy->openers > 1)
1796 /* The IOMEGA Clik! Drive doesn't support this command - no room for an eject mechanism */
1797 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1798 idefloppy_create_prevent_cmd(&pc, prevent);
1799 (void) idefloppy_queue_pc_tail(drive, &pc);
1801 if (cmd == CDROMEJECT) {
1802 idefloppy_create_start_stop_cmd(&pc, 2);
1803 (void) idefloppy_queue_pc_tail(drive, &pc);
1806 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1808 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
1809 return idefloppy_get_format_capacities(drive, argp);
1810 case IDEFLOPPY_IOCTL_FORMAT_START:
1812 if (!(file->f_mode & 2))
1815 if (floppy->openers > 1) {
1816 /* Don't format if someone is using the disk */
1818 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS,
1823 set_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1825 err = idefloppy_begin_format(drive, argp);
1827 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1830 ** Note, the bit will be cleared when the device is
1831 ** closed. This is the cleanest way to handle the
1832 ** situation where the drive does not support
1833 ** format progress reporting.
1835 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1836 return idefloppy_get_format_progress(drive, argp);
1840 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1841 * and CDROM_SEND_PACKET (legacy) ioctls
1843 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1844 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1845 bdev->bd_disk, cmd, argp);
1850 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1855 static int idefloppy_media_changed(struct gendisk *disk)
1857 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1858 ide_drive_t *drive = floppy->drive;
1860 /* do not scan partitions twice if this is a removable device */
1861 if (drive->attach) {
1865 return test_and_clear_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1868 static int idefloppy_revalidate_disk(struct gendisk *disk)
1870 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1871 set_capacity(disk, idefloppy_capacity(floppy->drive));
1875 static struct block_device_operations idefloppy_ops = {
1876 .owner = THIS_MODULE,
1877 .open = idefloppy_open,
1878 .release = idefloppy_release,
1879 .ioctl = idefloppy_ioctl,
1880 .getgeo = idefloppy_getgeo,
1881 .media_changed = idefloppy_media_changed,
1882 .revalidate_disk= idefloppy_revalidate_disk
1885 static int ide_floppy_probe(ide_drive_t *drive)
1887 idefloppy_floppy_t *floppy;
1890 if (!strstr("ide-floppy", drive->driver_req))
1892 if (!drive->present)
1894 if (drive->media != ide_floppy)
1896 if (!idefloppy_identify_device (drive, drive->id)) {
1897 printk (KERN_ERR "ide-floppy: %s: not supported by this version of ide-floppy\n", drive->name);
1901 printk("ide-floppy: passing drive %s to ide-scsi emulation.\n", drive->name);
1904 if ((floppy = kzalloc(sizeof (idefloppy_floppy_t), GFP_KERNEL)) == NULL) {
1905 printk (KERN_ERR "ide-floppy: %s: Can't allocate a floppy structure\n", drive->name);
1909 g = alloc_disk(1 << PARTN_BITS);
1911 goto out_free_floppy;
1913 ide_init_disk(g, drive);
1915 ide_proc_register_driver(drive, &idefloppy_driver);
1917 kref_init(&floppy->kref);
1919 floppy->drive = drive;
1920 floppy->driver = &idefloppy_driver;
1923 g->private_data = &floppy->driver;
1925 drive->driver_data = floppy;
1927 idefloppy_setup (drive, floppy);
1929 g->minors = 1 << PARTN_BITS;
1930 g->driverfs_dev = &drive->gendev;
1931 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1932 g->fops = &idefloppy_ops;
1943 MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
1945 static void __exit idefloppy_exit (void)
1947 driver_unregister(&idefloppy_driver.gen_driver);
1950 static int __init idefloppy_init(void)
1952 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
1953 return driver_register(&idefloppy_driver.gen_driver);
1956 MODULE_ALIAS("ide:*m-floppy*");
1957 module_init(idefloppy_init);
1958 module_exit(idefloppy_exit);
1959 MODULE_LICENSE("GPL");