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 * INQUIRY packet command - Data Format
291 #if defined(__LITTLE_ENDIAN_BITFIELD)
292 unsigned device_type :5; /* Peripheral Device Type */
293 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
294 unsigned reserved1_6t0 :7; /* Reserved */
295 unsigned rmb :1; /* Removable Medium Bit */
296 unsigned ansi_version :3; /* ANSI Version */
297 unsigned ecma_version :3; /* ECMA Version */
298 unsigned iso_version :2; /* ISO Version */
299 unsigned response_format :4; /* Response Data Format */
300 unsigned reserved3_45 :2; /* Reserved */
301 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
302 unsigned reserved3_7 :1; /* AENC - Reserved */
303 #elif defined(__BIG_ENDIAN_BITFIELD)
304 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
305 unsigned device_type :5; /* Peripheral Device Type */
306 unsigned rmb :1; /* Removable Medium Bit */
307 unsigned reserved1_6t0 :7; /* Reserved */
308 unsigned iso_version :2; /* ISO Version */
309 unsigned ecma_version :3; /* ECMA Version */
310 unsigned ansi_version :3; /* ANSI Version */
311 unsigned reserved3_7 :1; /* AENC - Reserved */
312 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
313 unsigned reserved3_45 :2; /* Reserved */
314 unsigned response_format :4; /* Response Data Format */
316 #error "Bitfield endianness not defined! Check your byteorder.h"
318 u8 additional_length; /* Additional Length (total_length-4) */
319 u8 rsv5, rsv6, rsv7; /* Reserved */
320 u8 vendor_id[8]; /* Vendor Identification */
321 u8 product_id[16]; /* Product Identification */
322 u8 revision_level[4]; /* Revision Level */
323 u8 vendor_specific[20]; /* Vendor Specific - Optional */
324 u8 reserved56t95[40]; /* Reserved - Optional */
325 /* Additional information may be returned */
326 } idefloppy_inquiry_result_t;
329 * REQUEST SENSE packet command result - Data Format.
332 #if defined(__LITTLE_ENDIAN_BITFIELD)
333 unsigned error_code :7; /* Current error (0x70) */
334 unsigned valid :1; /* The information field conforms to SFF-8070i */
335 u8 reserved1 :8; /* Reserved */
336 unsigned sense_key :4; /* Sense Key */
337 unsigned reserved2_4 :1; /* Reserved */
338 unsigned ili :1; /* Incorrect Length Indicator */
339 unsigned reserved2_67 :2;
340 #elif defined(__BIG_ENDIAN_BITFIELD)
341 unsigned valid :1; /* The information field conforms to SFF-8070i */
342 unsigned error_code :7; /* Current error (0x70) */
343 u8 reserved1 :8; /* Reserved */
344 unsigned reserved2_67 :2;
345 unsigned ili :1; /* Incorrect Length Indicator */
346 unsigned reserved2_4 :1; /* Reserved */
347 unsigned sense_key :4; /* Sense Key */
349 #error "Bitfield endianness not defined! Check your byteorder.h"
351 u32 information __attribute__ ((packed));
352 u8 asl; /* Additional sense length (n-7) */
353 u32 command_specific; /* Additional command specific information */
354 u8 asc; /* Additional Sense Code */
355 u8 ascq; /* Additional Sense Code Qualifier */
356 u8 replaceable_unit_code; /* Field Replaceable Unit Code */
358 u8 pad[2]; /* Padding to 20 bytes */
359 } idefloppy_request_sense_result_t;
362 * Pages of the SELECT SENSE / MODE SENSE packet commands.
363 * See SFF-8070i spec.
365 #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
366 #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
369 * Mode Parameter Header for the MODE SENSE packet command
372 u16 mode_data_length; /* Length of the following data transfer */
373 u8 medium_type; /* Medium Type */
374 #if defined(__LITTLE_ENDIAN_BITFIELD)
375 unsigned reserved3 :7;
376 unsigned wp :1; /* Write protect */
377 #elif defined(__BIG_ENDIAN_BITFIELD)
378 unsigned wp :1; /* Write protect */
379 unsigned reserved3 :7;
381 #error "Bitfield endianness not defined! Check your byteorder.h"
384 } idefloppy_mode_parameter_header_t;
386 static DEFINE_MUTEX(idefloppy_ref_mutex);
388 #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
390 #define ide_floppy_g(disk) \
391 container_of((disk)->private_data, struct ide_floppy_obj, driver)
393 static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
395 struct ide_floppy_obj *floppy = NULL;
397 mutex_lock(&idefloppy_ref_mutex);
398 floppy = ide_floppy_g(disk);
400 kref_get(&floppy->kref);
401 mutex_unlock(&idefloppy_ref_mutex);
405 static void idefloppy_cleanup_obj(struct kref *);
407 static void ide_floppy_put(struct ide_floppy_obj *floppy)
409 mutex_lock(&idefloppy_ref_mutex);
410 kref_put(&floppy->kref, idefloppy_cleanup_obj);
411 mutex_unlock(&idefloppy_ref_mutex);
415 * Too bad. The drive wants to send us data which we are not ready to accept.
416 * Just throw it away.
418 static void idefloppy_discard_data (ide_drive_t *drive, unsigned int bcount)
421 (void) HWIF(drive)->INB(IDE_DATA_REG);
424 #if IDEFLOPPY_DEBUG_BUGS
425 static void idefloppy_write_zeros (ide_drive_t *drive, unsigned int bcount)
428 HWIF(drive)->OUTB(0, IDE_DATA_REG);
430 #endif /* IDEFLOPPY_DEBUG_BUGS */
434 * idefloppy_do_end_request is used to finish servicing a request.
436 * For read/write requests, we will call ide_end_request to pass to the
439 static int idefloppy_do_end_request(ide_drive_t *drive, int uptodate, int nsecs)
441 idefloppy_floppy_t *floppy = drive->driver_data;
442 struct request *rq = HWGROUP(drive)->rq;
445 debug_log("Reached %s\n", __func__);
448 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
449 case 1: error = 0; break;
450 default: error = uptodate;
453 floppy->failed_pc = NULL;
454 /* Why does this happen? */
457 if (!blk_special_request(rq)) {
458 /* our real local end request function */
459 ide_end_request(drive, uptodate, nsecs);
463 /* fixme: need to move this local also */
464 ide_end_drive_cmd(drive, 0, 0);
468 static void idefloppy_input_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
470 struct request *rq = pc->rq;
471 struct bio_vec *bvec;
472 struct req_iterator iter;
477 rq_for_each_segment(bvec, rq, iter) {
481 count = min(bvec->bv_len, bcount);
483 data = bvec_kmap_irq(bvec, &flags);
484 drive->hwif->atapi_input_bytes(drive, data, count);
485 bvec_kunmap_irq(data, &flags);
488 pc->b_count += count;
492 idefloppy_do_end_request(drive, 1, done >> 9);
495 printk(KERN_ERR "%s: leftover data in idefloppy_input_buffers, bcount == %d\n", drive->name, bcount);
496 idefloppy_discard_data(drive, bcount);
500 static void idefloppy_output_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
502 struct request *rq = pc->rq;
503 struct req_iterator iter;
504 struct bio_vec *bvec;
509 rq_for_each_segment(bvec, rq, iter) {
513 count = min(bvec->bv_len, bcount);
515 data = bvec_kmap_irq(bvec, &flags);
516 drive->hwif->atapi_output_bytes(drive, data, count);
517 bvec_kunmap_irq(data, &flags);
520 pc->b_count += count;
524 idefloppy_do_end_request(drive, 1, done >> 9);
526 #if IDEFLOPPY_DEBUG_BUGS
528 printk(KERN_ERR "%s: leftover data in idefloppy_output_buffers, bcount == %d\n", drive->name, bcount);
529 idefloppy_write_zeros(drive, bcount);
534 static void idefloppy_update_buffers (ide_drive_t *drive, idefloppy_pc_t *pc)
536 struct request *rq = pc->rq;
537 struct bio *bio = rq->bio;
539 while ((bio = rq->bio) != NULL)
540 idefloppy_do_end_request(drive, 1, 0);
544 * idefloppy_queue_pc_head generates a new packet command request in front
545 * of the request queue, before the current request, so that it will be
546 * processed immediately, on the next pass through the driver.
548 static void idefloppy_queue_pc_head (ide_drive_t *drive,idefloppy_pc_t *pc,struct request *rq)
550 struct ide_floppy_obj *floppy = drive->driver_data;
552 ide_init_drive_cmd(rq);
553 rq->buffer = (char *) pc;
554 rq->cmd_type = REQ_TYPE_SPECIAL;
555 rq->rq_disk = floppy->disk;
556 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
559 static idefloppy_pc_t *idefloppy_next_pc_storage (ide_drive_t *drive)
561 idefloppy_floppy_t *floppy = drive->driver_data;
563 if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
564 floppy->pc_stack_index=0;
565 return (&floppy->pc_stack[floppy->pc_stack_index++]);
568 static struct request *idefloppy_next_rq_storage (ide_drive_t *drive)
570 idefloppy_floppy_t *floppy = drive->driver_data;
572 if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
573 floppy->rq_stack_index = 0;
574 return (&floppy->rq_stack[floppy->rq_stack_index++]);
578 * idefloppy_analyze_error is called on each failed packet command retry
579 * to analyze the request sense.
581 static void idefloppy_analyze_error (ide_drive_t *drive,idefloppy_request_sense_result_t *result)
583 idefloppy_floppy_t *floppy = drive->driver_data;
585 floppy->sense_key = result->sense_key;
586 floppy->asc = result->asc;
587 floppy->ascq = result->ascq;
588 floppy->progress_indication = result->sksv[0] & 0x80 ?
589 (u16)get_unaligned((u16 *)(result->sksv+1)):0x10000;
590 if (floppy->failed_pc)
591 debug_log("pc = %x, sense key = %x, asc = %x, ascq = %x\n",
592 floppy->failed_pc->c[0], result->sense_key,
593 result->asc, result->ascq);
595 debug_log("sense key = %x, asc = %x, ascq = %x\n",
596 result->sense_key, result->asc, result->ascq);
599 static void idefloppy_request_sense_callback (ide_drive_t *drive)
601 idefloppy_floppy_t *floppy = drive->driver_data;
603 debug_log("Reached %s\n", __func__);
605 if (!floppy->pc->error) {
606 idefloppy_analyze_error(drive,(idefloppy_request_sense_result_t *) floppy->pc->buffer);
607 idefloppy_do_end_request(drive, 1, 0);
609 printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting request!\n");
610 idefloppy_do_end_request(drive, 0, 0);
615 * General packet command callback function.
617 static void idefloppy_pc_callback (ide_drive_t *drive)
619 idefloppy_floppy_t *floppy = drive->driver_data;
621 debug_log("Reached %s\n", __func__);
623 idefloppy_do_end_request(drive, floppy->pc->error ? 0 : 1, 0);
627 * idefloppy_init_pc initializes a packet command.
629 static void idefloppy_init_pc (idefloppy_pc_t *pc)
631 memset(pc->c, 0, 12);
634 pc->request_transfer = 0;
635 pc->buffer = pc->pc_buffer;
636 pc->buffer_size = IDEFLOPPY_PC_BUFFER_SIZE;
637 pc->callback = &idefloppy_pc_callback;
640 static void idefloppy_create_request_sense_cmd (idefloppy_pc_t *pc)
642 idefloppy_init_pc(pc);
643 pc->c[0] = GPCMD_REQUEST_SENSE;
645 pc->request_transfer = 18;
646 pc->callback = &idefloppy_request_sense_callback;
650 * idefloppy_retry_pc is called when an error was detected during the
651 * last packet command. We queue a request sense packet command in
652 * the head of the request list.
654 static void idefloppy_retry_pc (ide_drive_t *drive)
659 (void)drive->hwif->INB(IDE_ERROR_REG);
660 pc = idefloppy_next_pc_storage(drive);
661 rq = idefloppy_next_rq_storage(drive);
662 idefloppy_create_request_sense_cmd(pc);
663 idefloppy_queue_pc_head(drive, pc, rq);
667 * idefloppy_pc_intr is the usual interrupt handler which will be called
668 * during a packet command.
670 static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
672 idefloppy_floppy_t *floppy = drive->driver_data;
673 ide_hwif_t *hwif = drive->hwif;
674 idefloppy_pc_t *pc = floppy->pc;
675 struct request *rq = pc->rq;
680 debug_log("Reached %s interrupt handler\n", __func__);
682 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
683 if (HWIF(drive)->ide_dma_end(drive)) {
684 set_bit(PC_DMA_ERROR, &pc->flags);
686 pc->actually_transferred = pc->request_transfer;
687 idefloppy_update_buffers(drive, pc);
689 debug_log("DMA finished\n");
692 /* Clear the interrupt */
693 stat = drive->hwif->INB(IDE_STATUS_REG);
695 if ((stat & DRQ_STAT) == 0) { /* No more interrupts */
696 debug_log("Packet command completed, %d bytes transferred\n",
697 pc->actually_transferred);
698 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
700 local_irq_enable_in_hardirq();
702 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
704 debug_log("%s: I/O error\n", drive->name);
706 if (pc->c[0] == GPCMD_REQUEST_SENSE) {
707 printk(KERN_ERR "ide-floppy: I/O error in "
708 "request sense command\n");
709 return ide_do_reset(drive);
711 /* Retry operation */
712 idefloppy_retry_pc(drive);
713 /* queued, but not started */
717 if (floppy->failed_pc == pc)
718 floppy->failed_pc = NULL;
719 /* Command finished - Call the callback function */
724 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
725 printk(KERN_ERR "ide-floppy: The floppy wants to issue "
726 "more interrupts in DMA mode\n");
728 return ide_do_reset(drive);
731 /* Get the number of bytes to transfer */
732 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
733 hwif->INB(IDE_BCOUNTL_REG);
734 /* on this interrupt */
735 ireason = hwif->INB(IDE_IREASON_REG);
738 printk(KERN_ERR "ide-floppy: CoD != 0 in idefloppy_pc_intr\n");
739 return ide_do_reset(drive);
741 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
742 /* Hopefully, we will never get here */
743 printk(KERN_ERR "ide-floppy: We wanted to %s, ",
744 (ireason & IO) ? "Write" : "Read");
745 printk(KERN_ERR "but the floppy wants us to %s !\n",
746 (ireason & IO) ? "Read" : "Write");
747 return ide_do_reset(drive);
749 if (!test_bit(PC_WRITING, &pc->flags)) {
750 /* Reading - Check that we have enough space */
751 temp = pc->actually_transferred + bcount;
752 if (temp > pc->request_transfer) {
753 if (temp > pc->buffer_size) {
754 printk(KERN_ERR "ide-floppy: The floppy wants "
755 "to send us more data than expected "
756 "- discarding data\n");
757 idefloppy_discard_data(drive, bcount);
759 ide_set_handler(drive,
765 debug_log("The floppy wants to send us more data than"
766 " expected - allowing transfer\n");
769 if (test_bit(PC_WRITING, &pc->flags)) {
770 if (pc->buffer != NULL)
771 /* Write the current buffer */
772 hwif->atapi_output_bytes(drive, pc->current_position,
775 idefloppy_output_buffers(drive, pc, bcount);
777 if (pc->buffer != NULL)
778 /* Read the current buffer */
779 hwif->atapi_input_bytes(drive, pc->current_position,
782 idefloppy_input_buffers(drive, pc, bcount);
784 /* Update the current position */
785 pc->actually_transferred += bcount;
786 pc->current_position += bcount;
788 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL); /* And set the interrupt handler again */
793 * This is the original routine that did the packet transfer.
794 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
795 * for that drive below. The algorithm is chosen based on drive type
797 static ide_startstop_t idefloppy_transfer_pc (ide_drive_t *drive)
799 ide_startstop_t startstop;
800 idefloppy_floppy_t *floppy = drive->driver_data;
803 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
804 printk(KERN_ERR "ide-floppy: Strange, packet command "
805 "initiated yet DRQ isn't asserted\n");
808 ireason = drive->hwif->INB(IDE_IREASON_REG);
809 if ((ireason & CD) == 0 || (ireason & IO)) {
810 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
811 "issuing a packet command\n");
812 return ide_do_reset(drive);
815 /* Set the interrupt routine */
816 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
817 /* Send the actual packet */
818 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
824 * What we have here is a classic case of a top half / bottom half
825 * interrupt service routine. In interrupt mode, the device sends
826 * an interrupt to signal it's ready to receive a packet. However,
827 * we need to delay about 2-3 ticks before issuing the packet or we
830 * So, follow carefully. transfer_pc1 is called as an interrupt (or
831 * directly). In either case, when the device says it's ready for a
832 * packet, we schedule the packet transfer to occur about 2-3 ticks
833 * later in transfer_pc2.
835 static int idefloppy_transfer_pc2 (ide_drive_t *drive)
837 idefloppy_floppy_t *floppy = drive->driver_data;
839 /* Send the actual packet */
840 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
841 /* Timeout for the packet command */
842 return IDEFLOPPY_WAIT_CMD;
845 static ide_startstop_t idefloppy_transfer_pc1 (ide_drive_t *drive)
847 idefloppy_floppy_t *floppy = drive->driver_data;
848 ide_startstop_t startstop;
851 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
852 printk(KERN_ERR "ide-floppy: Strange, packet command "
853 "initiated yet DRQ isn't asserted\n");
856 ireason = drive->hwif->INB(IDE_IREASON_REG);
857 if ((ireason & CD) == 0 || (ireason & IO)) {
858 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
859 "while issuing a packet command\n");
860 return ide_do_reset(drive);
863 * The following delay solves a problem with ATAPI Zip 100 drives
864 * where the Busy flag was apparently being deasserted before the
865 * unit was ready to receive data. This was happening on a
866 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
867 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
868 * used until after the packet is moved in about 50 msec.
871 ide_set_handler(drive,
872 &idefloppy_pc_intr, /* service routine for packet command */
873 floppy->ticks, /* wait this long before "failing" */
874 &idefloppy_transfer_pc2); /* fail == transfer_pc2 */
879 * idefloppy_should_report_error()
881 * Supresses error messages resulting from Medium not present
883 static inline int idefloppy_should_report_error(idefloppy_floppy_t *floppy)
885 if (floppy->sense_key == 0x02 &&
886 floppy->asc == 0x3a &&
887 floppy->ascq == 0x00)
893 * Issue a packet command
895 static ide_startstop_t idefloppy_issue_pc (ide_drive_t *drive, idefloppy_pc_t *pc)
897 idefloppy_floppy_t *floppy = drive->driver_data;
898 ide_hwif_t *hwif = drive->hwif;
899 ide_handler_t *pkt_xfer_routine;
903 if (floppy->failed_pc == NULL &&
904 pc->c[0] != GPCMD_REQUEST_SENSE)
905 floppy->failed_pc = pc;
906 /* Set the current packet command */
909 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES ||
910 test_bit(PC_ABORT, &pc->flags)) {
912 * We will "abort" retrying a packet command in case
913 * a legitimate error code was received.
915 if (!test_bit(PC_ABORT, &pc->flags)) {
916 if (!test_bit(PC_SUPPRESS_ERROR, &pc->flags)) {
917 if (idefloppy_should_report_error(floppy))
918 printk(KERN_ERR "ide-floppy: %s: I/O error, "
919 "pc = %2x, key = %2x, "
920 "asc = %2x, ascq = %2x\n",
921 drive->name, pc->c[0],
923 floppy->asc, floppy->ascq);
926 pc->error = IDEFLOPPY_ERROR_GENERAL;
928 floppy->failed_pc = NULL;
933 debug_log("Retry number - %d\n", pc->retries);
936 /* We haven't transferred any data yet */
937 pc->actually_transferred = 0;
938 pc->current_position = pc->buffer;
939 bcount = min(pc->request_transfer, 63 * 1024);
941 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags))
946 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
947 dma = !hwif->dma_setup(drive);
949 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
950 IDE_TFLAG_OUT_DEVICE, bcount, dma);
952 if (dma) { /* Begin DMA, if necessary */
953 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
954 hwif->dma_start(drive);
957 /* Can we transfer the packet when we get the interrupt or wait? */
958 if (test_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags)) {
960 pkt_xfer_routine = &idefloppy_transfer_pc1;
963 pkt_xfer_routine = &idefloppy_transfer_pc;
966 if (test_bit (IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags)) {
967 /* Issue the packet command */
968 ide_execute_command(drive, WIN_PACKETCMD,
974 /* Issue the packet command */
975 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
976 return (*pkt_xfer_routine) (drive);
980 static void idefloppy_rw_callback (ide_drive_t *drive)
982 debug_log("Reached %s\n", __func__);
984 idefloppy_do_end_request(drive, 1, 0);
988 static void idefloppy_create_prevent_cmd (idefloppy_pc_t *pc, int prevent)
990 debug_log("creating prevent removal command, prevent = %d\n", prevent);
992 idefloppy_init_pc(pc);
993 pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
997 static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t *pc)
999 idefloppy_init_pc(pc);
1000 pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
1003 pc->request_transfer = 255;
1006 static void idefloppy_create_format_unit_cmd (idefloppy_pc_t *pc, int b, int l,
1009 idefloppy_init_pc(pc);
1010 pc->c[0] = GPCMD_FORMAT_UNIT;
1013 memset(pc->buffer, 0, 12);
1014 pc->buffer[1] = 0xA2;
1015 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
1017 if (flags & 1) /* Verify bit on... */
1018 pc->buffer[1] ^= 0x20; /* ... turn off DCRT bit */
1021 put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buffer[4]));
1022 put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buffer[8]));
1024 set_bit(PC_WRITING, &pc->flags);
1028 * A mode sense command is used to "sense" floppy parameters.
1030 static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t *pc, u8 page_code, u8 type)
1032 u16 length = sizeof(idefloppy_mode_parameter_header_t);
1034 idefloppy_init_pc(pc);
1035 pc->c[0] = GPCMD_MODE_SENSE_10;
1037 pc->c[2] = page_code + (type << 6);
1039 switch (page_code) {
1040 case IDEFLOPPY_CAPABILITIES_PAGE:
1043 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
1047 printk(KERN_ERR "ide-floppy: unsupported page code "
1048 "in create_mode_sense_cmd\n");
1050 put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
1051 pc->request_transfer = length;
1054 static void idefloppy_create_start_stop_cmd (idefloppy_pc_t *pc, int start)
1056 idefloppy_init_pc(pc);
1057 pc->c[0] = GPCMD_START_STOP_UNIT;
1061 static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc)
1063 idefloppy_init_pc(pc);
1064 pc->c[0] = GPCMD_TEST_UNIT_READY;
1067 static void idefloppy_create_rw_cmd (idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq, unsigned long sector)
1069 int block = sector / floppy->bs_factor;
1070 int blocks = rq->nr_sectors / floppy->bs_factor;
1071 int cmd = rq_data_dir(rq);
1073 debug_log("create_rw1%d_cmd: block == %d, blocks == %d\n",
1074 2 * test_bit (IDEFLOPPY_USE_READ12, &floppy->flags),
1077 idefloppy_init_pc(pc);
1078 if (test_bit(IDEFLOPPY_USE_READ12, &floppy->flags)) {
1079 pc->c[0] = cmd == READ ? GPCMD_READ_12 : GPCMD_WRITE_12;
1080 put_unaligned(cpu_to_be32(blocks), (unsigned int *) &pc->c[6]);
1082 pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
1083 put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
1085 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
1086 pc->callback = &idefloppy_rw_callback;
1088 pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
1089 if (rq->cmd_flags & REQ_RW)
1090 set_bit(PC_WRITING, &pc->flags);
1092 pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
1093 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1097 idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq)
1099 idefloppy_init_pc(pc);
1100 pc->callback = &idefloppy_rw_callback;
1101 memcpy(pc->c, rq->cmd, sizeof(pc->c));
1103 pc->b_count = rq->data_len;
1104 if (rq->data_len && rq_data_dir(rq) == WRITE)
1105 set_bit(PC_WRITING, &pc->flags);
1106 pc->buffer = rq->data;
1108 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1111 * possibly problematic, doesn't look like ide-floppy correctly
1112 * handled scattered requests if dma fails...
1114 pc->request_transfer = pc->buffer_size = rq->data_len;
1118 * idefloppy_do_request is our request handling function.
1120 static ide_startstop_t idefloppy_do_request (ide_drive_t *drive, struct request *rq, sector_t block_s)
1122 idefloppy_floppy_t *floppy = drive->driver_data;
1124 unsigned long block = (unsigned long)block_s;
1126 debug_log("dev: %s, cmd_type: %x, errors: %d\n",
1127 rq->rq_disk ? rq->rq_disk->disk_name : "?",
1128 rq->cmd_type, rq->errors);
1129 debug_log("sector: %ld, nr_sectors: %ld, "
1130 "current_nr_sectors: %d\n", (long)rq->sector,
1131 rq->nr_sectors, rq->current_nr_sectors);
1133 if (rq->errors >= ERROR_MAX) {
1134 if (floppy->failed_pc != NULL) {
1135 if (idefloppy_should_report_error(floppy))
1136 printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x,"
1137 " key = %2x, asc = %2x, ascq = %2x\n",
1138 drive->name, floppy->failed_pc->c[0],
1139 floppy->sense_key, floppy->asc, floppy->ascq);
1142 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
1144 idefloppy_do_end_request(drive, 0, 0);
1147 if (blk_fs_request(rq)) {
1148 if (((long)rq->sector % floppy->bs_factor) ||
1149 (rq->nr_sectors % floppy->bs_factor)) {
1150 printk("%s: unsupported r/w request size\n",
1152 idefloppy_do_end_request(drive, 0, 0);
1155 pc = idefloppy_next_pc_storage(drive);
1156 idefloppy_create_rw_cmd(floppy, pc, rq, block);
1157 } else if (blk_special_request(rq)) {
1158 pc = (idefloppy_pc_t *) rq->buffer;
1159 } else if (blk_pc_request(rq)) {
1160 pc = idefloppy_next_pc_storage(drive);
1161 idefloppy_blockpc_cmd(floppy, pc, rq);
1163 blk_dump_rq_flags(rq,
1164 "ide-floppy: unsupported command in queue");
1165 idefloppy_do_end_request(drive, 0, 0);
1170 return idefloppy_issue_pc(drive, pc);
1174 * idefloppy_queue_pc_tail adds a special packet command request to the
1175 * tail of the request queue, and waits for it to be serviced.
1177 static int idefloppy_queue_pc_tail (ide_drive_t *drive,idefloppy_pc_t *pc)
1179 struct ide_floppy_obj *floppy = drive->driver_data;
1182 ide_init_drive_cmd (&rq);
1183 rq.buffer = (char *) pc;
1184 rq.cmd_type = REQ_TYPE_SPECIAL;
1185 rq.rq_disk = floppy->disk;
1187 return ide_do_drive_cmd(drive, &rq, ide_wait);
1191 * Look at the flexible disk page parameters. We will ignore the CHS
1192 * capacity parameters and use the LBA parameters instead.
1194 static int idefloppy_get_flexible_disk_page (ide_drive_t *drive)
1196 idefloppy_floppy_t *floppy = drive->driver_data;
1198 idefloppy_mode_parameter_header_t *header;
1199 idefloppy_flexible_disk_page_t *page;
1200 int capacity, lba_capacity;
1202 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE, MODE_SENSE_CURRENT);
1203 if (idefloppy_queue_pc_tail(drive,&pc)) {
1204 printk(KERN_ERR "ide-floppy: Can't get flexible disk "
1205 "page parameters\n");
1208 header = (idefloppy_mode_parameter_header_t *) pc.buffer;
1209 floppy->wp = header->wp;
1210 set_disk_ro(floppy->disk, floppy->wp);
1211 page = (idefloppy_flexible_disk_page_t *) (header + 1);
1213 page->transfer_rate = be16_to_cpu(page->transfer_rate);
1214 page->sector_size = be16_to_cpu(page->sector_size);
1215 page->cyls = be16_to_cpu(page->cyls);
1216 page->rpm = be16_to_cpu(page->rpm);
1217 capacity = page->cyls * page->heads * page->sectors * page->sector_size;
1218 if (memcmp (page, &floppy->flexible_disk_page, sizeof (idefloppy_flexible_disk_page_t)))
1219 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
1220 "%d sector size, %d rpm\n",
1221 drive->name, capacity / 1024, page->cyls,
1222 page->heads, page->sectors,
1223 page->transfer_rate / 8, page->sector_size, page->rpm);
1225 floppy->flexible_disk_page = *page;
1226 drive->bios_cyl = page->cyls;
1227 drive->bios_head = page->heads;
1228 drive->bios_sect = page->sectors;
1229 lba_capacity = floppy->blocks * floppy->block_size;
1230 if (capacity < lba_capacity) {
1231 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
1232 "bytes, but the drive only handles %d\n",
1233 drive->name, lba_capacity, capacity);
1234 floppy->blocks = floppy->block_size ? capacity / floppy->block_size : 0;
1239 static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
1241 idefloppy_floppy_t *floppy = drive->driver_data;
1245 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
1246 MODE_SENSE_CURRENT);
1248 set_bit(PC_SUPPRESS_ERROR, &pc.flags);
1249 if (idefloppy_queue_pc_tail(drive, &pc))
1252 floppy->srfp = pc.buffer[8 + 2] & 0x40;
1257 * Determine if a media is present in the floppy drive, and if so,
1260 static int idefloppy_get_capacity (ide_drive_t *drive)
1262 idefloppy_floppy_t *floppy = drive->driver_data;
1264 idefloppy_capacity_header_t *header;
1265 idefloppy_capacity_descriptor_t *descriptor;
1266 int i, descriptors, rc = 1, blocks, length;
1268 drive->bios_cyl = 0;
1269 drive->bios_head = drive->bios_sect = 0;
1271 floppy->bs_factor = 1;
1272 set_capacity(floppy->disk, 0);
1274 idefloppy_create_read_capacity_cmd(&pc);
1275 if (idefloppy_queue_pc_tail(drive, &pc)) {
1276 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1279 header = (idefloppy_capacity_header_t *) pc.buffer;
1280 descriptors = header->length / sizeof(idefloppy_capacity_descriptor_t);
1281 descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
1283 for (i = 0; i < descriptors; i++, descriptor++) {
1284 blocks = descriptor->blocks = be32_to_cpu(descriptor->blocks);
1285 length = descriptor->length = be16_to_cpu(descriptor->length);
1289 switch (descriptor->dc) {
1290 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1291 case CAPACITY_UNFORMATTED:
1292 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags))
1294 * If it is not a clik drive, break out
1295 * (maintains previous driver behaviour)
1298 case CAPACITY_CURRENT:
1299 /* Normal Zip/LS-120 disks */
1300 if (memcmp(descriptor, &floppy->capacity, sizeof (idefloppy_capacity_descriptor_t)))
1301 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
1302 "sector size\n", drive->name,
1303 blocks * length / 1024, blocks, length);
1304 floppy->capacity = *descriptor;
1305 if (!length || length % 512) {
1306 printk(KERN_NOTICE "%s: %d bytes block size "
1307 "not supported\n", drive->name, length);
1309 floppy->blocks = blocks;
1310 floppy->block_size = length;
1311 if ((floppy->bs_factor = length / 512) != 1)
1312 printk(KERN_NOTICE "%s: warning: non "
1313 "512 bytes block size not "
1314 "fully supported\n",
1319 case CAPACITY_NO_CARTRIDGE:
1321 * This is a KERN_ERR so it appears on screen
1322 * for the user to see
1324 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
1326 case CAPACITY_INVALID:
1327 printk(KERN_ERR "%s: Invalid capacity for disk "
1328 "in drive\n", drive->name);
1333 debug_log("Descriptor 0 Code: %d\n", descriptor->dc);
1335 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
1336 i, blocks * length / 1024, blocks, length);
1339 /* Clik! disk does not support get_flexible_disk_page */
1340 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1341 (void) idefloppy_get_flexible_disk_page(drive);
1344 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
1349 ** Obtain the list of formattable capacities.
1350 ** Very similar to idefloppy_get_capacity, except that we push the capacity
1351 ** descriptors to userland, instead of our own structures.
1353 ** Userland gives us the following structure:
1355 ** struct idefloppy_format_capacities {
1363 ** userland initializes nformats to the number of allocated formats[]
1364 ** records. On exit we set nformats to the number of records we've
1365 ** actually initialized.
1369 static int idefloppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
1372 idefloppy_capacity_header_t *header;
1373 idefloppy_capacity_descriptor_t *descriptor;
1374 int i, descriptors, blocks, length;
1379 if (get_user(u_array_size, arg))
1382 if (u_array_size <= 0)
1385 idefloppy_create_read_capacity_cmd(&pc);
1386 if (idefloppy_queue_pc_tail(drive, &pc)) {
1387 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1390 header = (idefloppy_capacity_header_t *) pc.buffer;
1391 descriptors = header->length /
1392 sizeof(idefloppy_capacity_descriptor_t);
1393 descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
1399 ** We always skip the first capacity descriptor. That's the
1400 ** current capacity. We are interested in the remaining descriptors,
1401 ** the formattable capacities.
1404 for (i=0; i<descriptors; i++, descriptor++) {
1405 if (u_index >= u_array_size)
1406 break; /* User-supplied buffer too small */
1408 continue; /* Skip the first descriptor */
1410 blocks = be32_to_cpu(descriptor->blocks);
1411 length = be16_to_cpu(descriptor->length);
1413 if (put_user(blocks, argp))
1417 if (put_user(length, argp))
1424 if (put_user(u_index, arg))
1430 ** Send ATAPI_FORMAT_UNIT to the drive.
1432 ** Userland gives us the following structure:
1434 ** struct idefloppy_format_command {
1440 ** flags is a bitmask, currently, the only defined flag is:
1442 ** 0x01 - verify media after format.
1445 static int idefloppy_begin_format(ide_drive_t *drive, int __user *arg)
1452 if (get_user(blocks, arg) ||
1453 get_user(length, arg+1) ||
1454 get_user(flags, arg+2)) {
1458 (void) idefloppy_get_sfrp_bit(drive);
1459 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1460 if (idefloppy_queue_pc_tail(drive, &pc)) {
1468 ** Get ATAPI_FORMAT_UNIT progress indication.
1470 ** Userland gives a pointer to an int. The int is set to a progress
1471 ** indicator 0-65536, with 65536=100%.
1473 ** If the drive does not support format progress indication, we just check
1474 ** the dsc bit, and return either 0 or 65536.
1477 static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
1479 idefloppy_floppy_t *floppy = drive->driver_data;
1481 int progress_indication = 0x10000;
1484 idefloppy_create_request_sense_cmd(&pc);
1485 if (idefloppy_queue_pc_tail(drive, &pc)) {
1489 if (floppy->sense_key == 2 &&
1491 floppy->ascq == 4) {
1492 progress_indication = floppy->progress_indication;
1494 /* Else assume format_unit has finished, and we're
1497 unsigned long flags;
1500 local_irq_save(flags);
1501 stat = drive->hwif->INB(IDE_STATUS_REG);
1502 local_irq_restore(flags);
1504 progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
1506 if (put_user(progress_indication, arg))
1513 * Return the current floppy capacity.
1515 static sector_t idefloppy_capacity (ide_drive_t *drive)
1517 idefloppy_floppy_t *floppy = drive->driver_data;
1518 unsigned long capacity = floppy->blocks * floppy->bs_factor;
1524 * idefloppy_identify_device checks if we can support a drive,
1525 * based on the ATAPI IDENTIFY command results.
1527 static int idefloppy_identify_device (ide_drive_t *drive,struct hd_driveid *id)
1529 struct idefloppy_id_gcw gcw;
1530 #if IDEFLOPPY_DEBUG_INFO
1532 #endif /* IDEFLOPPY_DEBUG_INFO */
1534 *((u16 *) &gcw) = id->config;
1537 /* kludge for Apple PowerBook internal zip */
1538 if ((gcw.device_type == 5) &&
1539 !strstr(id->model, "CD-ROM") &&
1540 strstr(id->model, "ZIP"))
1541 gcw.device_type = 0;
1544 #if IDEFLOPPY_DEBUG_INFO
1545 printk(KERN_INFO "Dumping ATAPI Identify Device floppy parameters\n");
1546 switch (gcw.protocol) {
1547 case 0: case 1: sprintf(buffer, "ATA");break;
1548 case 2: sprintf(buffer, "ATAPI");break;
1549 case 3: sprintf(buffer, "Reserved (Unknown to ide-floppy)");break;
1551 printk(KERN_INFO "Protocol Type: %s\n", buffer);
1552 switch (gcw.device_type) {
1553 case 0: sprintf(buffer, "Direct-access Device");break;
1554 case 1: sprintf(buffer, "Streaming Tape Device");break;
1555 case 2: case 3: case 4: sprintf (buffer, "Reserved");break;
1556 case 5: sprintf(buffer, "CD-ROM Device");break;
1557 case 6: sprintf(buffer, "Reserved");
1558 case 7: sprintf(buffer, "Optical memory Device");break;
1559 case 0x1f: sprintf(buffer, "Unknown or no Device type");break;
1560 default: sprintf(buffer, "Reserved");
1562 printk(KERN_INFO "Device Type: %x - %s\n", gcw.device_type, buffer);
1563 printk(KERN_INFO "Removable: %s\n",gcw.removable ? "Yes":"No");
1564 switch (gcw.drq_type) {
1565 case 0: sprintf(buffer, "Microprocessor DRQ");break;
1566 case 1: sprintf(buffer, "Interrupt DRQ");break;
1567 case 2: sprintf(buffer, "Accelerated DRQ");break;
1568 case 3: sprintf(buffer, "Reserved");break;
1570 printk(KERN_INFO "Command Packet DRQ Type: %s\n", buffer);
1571 switch (gcw.packet_size) {
1572 case 0: sprintf(buffer, "12 bytes");break;
1573 case 1: sprintf(buffer, "16 bytes");break;
1574 default: sprintf(buffer, "Reserved");break;
1576 printk(KERN_INFO "Command Packet Size: %s\n", buffer);
1577 #endif /* IDEFLOPPY_DEBUG_INFO */
1579 if (gcw.protocol != 2)
1580 printk(KERN_ERR "ide-floppy: Protocol is not ATAPI\n");
1581 else if (gcw.device_type != 0)
1582 printk(KERN_ERR "ide-floppy: Device type is not set to floppy\n");
1583 else if (!gcw.removable)
1584 printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
1585 else if (gcw.drq_type == 3) {
1586 printk(KERN_ERR "ide-floppy: Sorry, DRQ type %d not supported\n", gcw.drq_type);
1587 } else if (gcw.packet_size != 0) {
1588 printk(KERN_ERR "ide-floppy: Packet size is not 12 bytes long\n");
1594 #ifdef CONFIG_IDE_PROC_FS
1595 static void idefloppy_add_settings(ide_drive_t *drive)
1597 idefloppy_floppy_t *floppy = drive->driver_data;
1600 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
1602 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
1603 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
1604 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
1605 ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &floppy->ticks, NULL);
1608 static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
1612 * Driver initialization.
1614 static void idefloppy_setup (ide_drive_t *drive, idefloppy_floppy_t *floppy)
1616 struct idefloppy_id_gcw gcw;
1618 *((u16 *) &gcw) = drive->id->config;
1619 floppy->pc = floppy->pc_stack;
1620 if (gcw.drq_type == 1)
1621 set_bit(IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags);
1623 * We used to check revisions here. At this point however
1624 * I'm giving up. Just assume they are all broken, its easier.
1626 * The actual reason for the workarounds was likely
1627 * a driver bug after all rather than a firmware bug,
1628 * and the workaround below used to hide it. It should
1629 * be fixed as of version 1.9, but to be on the safe side
1630 * we'll leave the limitation below for the 2.2.x tree.
1633 if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
1634 set_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags);
1635 /* This value will be visible in the /proc/ide/hdx/settings */
1636 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1637 blk_queue_max_sectors(drive->queue, 64);
1641 * Guess what? The IOMEGA Clik! drive also needs the
1642 * above fix. It makes nasty clicking noises without
1643 * it, so please don't remove this.
1645 if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
1646 blk_queue_max_sectors(drive->queue, 64);
1647 set_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags);
1651 (void) idefloppy_get_capacity(drive);
1652 idefloppy_add_settings(drive);
1655 static void ide_floppy_remove(ide_drive_t *drive)
1657 idefloppy_floppy_t *floppy = drive->driver_data;
1658 struct gendisk *g = floppy->disk;
1660 ide_proc_unregister_driver(drive, floppy->driver);
1664 ide_floppy_put(floppy);
1667 static void idefloppy_cleanup_obj(struct kref *kref)
1669 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1670 ide_drive_t *drive = floppy->drive;
1671 struct gendisk *g = floppy->disk;
1673 drive->driver_data = NULL;
1674 g->private_data = NULL;
1679 #ifdef CONFIG_IDE_PROC_FS
1680 static int proc_idefloppy_read_capacity
1681 (char *page, char **start, off_t off, int count, int *eof, void *data)
1683 ide_drive_t*drive = (ide_drive_t *)data;
1686 len = sprintf(page,"%llu\n", (long long)idefloppy_capacity(drive));
1687 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1690 static ide_proc_entry_t idefloppy_proc[] = {
1691 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
1692 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1693 { NULL, 0, NULL, NULL }
1695 #endif /* CONFIG_IDE_PROC_FS */
1697 static int ide_floppy_probe(ide_drive_t *);
1699 static ide_driver_t idefloppy_driver = {
1701 .owner = THIS_MODULE,
1702 .name = "ide-floppy",
1703 .bus = &ide_bus_type,
1705 .probe = ide_floppy_probe,
1706 .remove = ide_floppy_remove,
1707 .version = IDEFLOPPY_VERSION,
1708 .media = ide_floppy,
1709 .supports_dsc_overlap = 0,
1710 .do_request = idefloppy_do_request,
1711 .end_request = idefloppy_do_end_request,
1712 .error = __ide_error,
1713 .abort = __ide_abort,
1714 #ifdef CONFIG_IDE_PROC_FS
1715 .proc = idefloppy_proc,
1719 static int idefloppy_open(struct inode *inode, struct file *filp)
1721 struct gendisk *disk = inode->i_bdev->bd_disk;
1722 struct ide_floppy_obj *floppy;
1727 debug_log("Reached %s\n", __func__);
1729 if (!(floppy = ide_floppy_get(disk)))
1732 drive = floppy->drive;
1736 if (floppy->openers == 1) {
1737 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1740 idefloppy_create_test_unit_ready_cmd(&pc);
1741 if (idefloppy_queue_pc_tail(drive, &pc)) {
1742 idefloppy_create_start_stop_cmd(&pc, 1);
1743 (void) idefloppy_queue_pc_tail(drive, &pc);
1746 if (idefloppy_get_capacity (drive)
1747 && (filp->f_flags & O_NDELAY) == 0
1749 ** Allow O_NDELAY to open a drive without a disk, or with
1750 ** an unreadable disk, so that we can get the format
1751 ** capacity of the drive or begin the format - Sam
1755 goto out_put_floppy;
1758 if (floppy->wp && (filp->f_mode & 2)) {
1760 goto out_put_floppy;
1762 set_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1763 /* IOMEGA Clik! drives do not support lock/unlock commands */
1764 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1765 idefloppy_create_prevent_cmd(&pc, 1);
1766 (void) idefloppy_queue_pc_tail(drive, &pc);
1768 check_disk_change(inode->i_bdev);
1769 } else if (test_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags)) {
1771 goto out_put_floppy;
1777 ide_floppy_put(floppy);
1781 static int idefloppy_release(struct inode *inode, struct file *filp)
1783 struct gendisk *disk = inode->i_bdev->bd_disk;
1784 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1785 ide_drive_t *drive = floppy->drive;
1788 debug_log("Reached %s\n", __func__);
1790 if (floppy->openers == 1) {
1791 /* IOMEGA Clik! drives do not support lock/unlock commands */
1792 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1793 idefloppy_create_prevent_cmd(&pc, 0);
1794 (void) idefloppy_queue_pc_tail(drive, &pc);
1797 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1802 ide_floppy_put(floppy);
1807 static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1809 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1810 ide_drive_t *drive = floppy->drive;
1812 geo->heads = drive->bios_head;
1813 geo->sectors = drive->bios_sect;
1814 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1818 static int idefloppy_ioctl(struct inode *inode, struct file *file,
1819 unsigned int cmd, unsigned long arg)
1821 struct block_device *bdev = inode->i_bdev;
1822 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1823 ide_drive_t *drive = floppy->drive;
1824 void __user *argp = (void __user *)arg;
1826 int prevent = (arg) ? 1 : 0;
1833 case CDROM_LOCKDOOR:
1834 if (floppy->openers > 1)
1837 /* The IOMEGA Clik! Drive doesn't support this command - no room for an eject mechanism */
1838 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1839 idefloppy_create_prevent_cmd(&pc, prevent);
1840 (void) idefloppy_queue_pc_tail(drive, &pc);
1842 if (cmd == CDROMEJECT) {
1843 idefloppy_create_start_stop_cmd(&pc, 2);
1844 (void) idefloppy_queue_pc_tail(drive, &pc);
1847 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1849 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
1850 return idefloppy_get_format_capacities(drive, argp);
1851 case IDEFLOPPY_IOCTL_FORMAT_START:
1853 if (!(file->f_mode & 2))
1856 if (floppy->openers > 1) {
1857 /* Don't format if someone is using the disk */
1859 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS,
1864 set_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1866 err = idefloppy_begin_format(drive, argp);
1868 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1871 ** Note, the bit will be cleared when the device is
1872 ** closed. This is the cleanest way to handle the
1873 ** situation where the drive does not support
1874 ** format progress reporting.
1876 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1877 return idefloppy_get_format_progress(drive, argp);
1881 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1882 * and CDROM_SEND_PACKET (legacy) ioctls
1884 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1885 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1886 bdev->bd_disk, cmd, argp);
1891 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1896 static int idefloppy_media_changed(struct gendisk *disk)
1898 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1899 ide_drive_t *drive = floppy->drive;
1901 /* do not scan partitions twice if this is a removable device */
1902 if (drive->attach) {
1906 return test_and_clear_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1909 static int idefloppy_revalidate_disk(struct gendisk *disk)
1911 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1912 set_capacity(disk, idefloppy_capacity(floppy->drive));
1916 static struct block_device_operations idefloppy_ops = {
1917 .owner = THIS_MODULE,
1918 .open = idefloppy_open,
1919 .release = idefloppy_release,
1920 .ioctl = idefloppy_ioctl,
1921 .getgeo = idefloppy_getgeo,
1922 .media_changed = idefloppy_media_changed,
1923 .revalidate_disk= idefloppy_revalidate_disk
1926 static int ide_floppy_probe(ide_drive_t *drive)
1928 idefloppy_floppy_t *floppy;
1931 if (!strstr("ide-floppy", drive->driver_req))
1933 if (!drive->present)
1935 if (drive->media != ide_floppy)
1937 if (!idefloppy_identify_device (drive, drive->id)) {
1938 printk (KERN_ERR "ide-floppy: %s: not supported by this version of ide-floppy\n", drive->name);
1942 printk("ide-floppy: passing drive %s to ide-scsi emulation.\n", drive->name);
1945 if ((floppy = kzalloc(sizeof (idefloppy_floppy_t), GFP_KERNEL)) == NULL) {
1946 printk (KERN_ERR "ide-floppy: %s: Can't allocate a floppy structure\n", drive->name);
1950 g = alloc_disk(1 << PARTN_BITS);
1952 goto out_free_floppy;
1954 ide_init_disk(g, drive);
1956 ide_proc_register_driver(drive, &idefloppy_driver);
1958 kref_init(&floppy->kref);
1960 floppy->drive = drive;
1961 floppy->driver = &idefloppy_driver;
1964 g->private_data = &floppy->driver;
1966 drive->driver_data = floppy;
1968 idefloppy_setup (drive, floppy);
1970 g->minors = 1 << PARTN_BITS;
1971 g->driverfs_dev = &drive->gendev;
1972 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1973 g->fops = &idefloppy_ops;
1984 MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
1986 static void __exit idefloppy_exit (void)
1988 driver_unregister(&idefloppy_driver.gen_driver);
1991 static int __init idefloppy_init(void)
1993 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
1994 return driver_register(&idefloppy_driver.gen_driver);
1997 MODULE_ALIAS("ide:*m-floppy*");
1998 module_init(idefloppy_init);
1999 module_exit(idefloppy_exit);
2000 MODULE_LICENSE("GPL");