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 * Removable Block Access Capabilities Page
126 #if defined(__LITTLE_ENDIAN_BITFIELD)
127 unsigned page_code :6; /* Page code - Should be 0x1b */
128 unsigned reserved1_6 :1; /* Reserved */
129 unsigned ps :1; /* Should be 0 */
130 #elif defined(__BIG_ENDIAN_BITFIELD)
131 unsigned ps :1; /* Should be 0 */
132 unsigned reserved1_6 :1; /* Reserved */
133 unsigned page_code :6; /* Page code - Should be 0x1b */
135 #error "Bitfield endianness not defined! Check your byteorder.h"
137 u8 page_length; /* Page Length - Should be 0xa */
138 #if defined(__LITTLE_ENDIAN_BITFIELD)
139 unsigned reserved2 :6;
140 unsigned srfp :1; /* Supports reporting progress of format */
141 unsigned sflp :1; /* System floppy type device */
142 unsigned tlun :3; /* Total logical units supported by the device */
143 unsigned reserved3 :3;
144 unsigned sml :1; /* Single / Multiple lun supported */
145 unsigned ncd :1; /* Non cd optical device */
146 #elif defined(__BIG_ENDIAN_BITFIELD)
147 unsigned sflp :1; /* System floppy type device */
148 unsigned srfp :1; /* Supports reporting progress of format */
149 unsigned reserved2 :6;
150 unsigned ncd :1; /* Non cd optical device */
151 unsigned sml :1; /* Single / Multiple lun supported */
152 unsigned reserved3 :3;
153 unsigned tlun :3; /* Total logical units supported by the device */
155 #error "Bitfield endianness not defined! Check your byteorder.h"
158 } idefloppy_capabilities_page_t;
161 * Flexible disk page.
164 #if defined(__LITTLE_ENDIAN_BITFIELD)
165 unsigned page_code :6; /* Page code - Should be 0x5 */
166 unsigned reserved1_6 :1; /* Reserved */
167 unsigned ps :1; /* The device is capable of saving the page */
168 #elif defined(__BIG_ENDIAN_BITFIELD)
169 unsigned ps :1; /* The device is capable of saving the page */
170 unsigned reserved1_6 :1; /* Reserved */
171 unsigned page_code :6; /* Page code - Should be 0x5 */
173 #error "Bitfield endianness not defined! Check your byteorder.h"
175 u8 page_length; /* Page Length - Should be 0x1e */
176 u16 transfer_rate; /* In kilobits per second */
177 u8 heads, sectors; /* Number of heads, Number of sectors per track */
178 u16 sector_size; /* Byes per sector */
179 u16 cyls; /* Number of cylinders */
181 u8 motor_delay; /* Motor off delay */
183 u16 rpm; /* Rotations per minute */
185 } idefloppy_flexible_disk_page_t;
192 u8 length; /* Length of the following descriptors in bytes */
193 } idefloppy_capacity_header_t;
196 u32 blocks; /* Number of blocks */
197 #if defined(__LITTLE_ENDIAN_BITFIELD)
198 unsigned dc :2; /* Descriptor Code */
199 unsigned reserved :6;
200 #elif defined(__BIG_ENDIAN_BITFIELD)
201 unsigned reserved :6;
202 unsigned dc :2; /* Descriptor Code */
204 #error "Bitfield endianness not defined! Check your byteorder.h"
206 u8 length_msb; /* Block Length (MSB)*/
207 u16 length; /* Block Length */
208 } idefloppy_capacity_descriptor_t;
210 #define CAPACITY_INVALID 0x00
211 #define CAPACITY_UNFORMATTED 0x01
212 #define CAPACITY_CURRENT 0x02
213 #define CAPACITY_NO_CARTRIDGE 0x03
216 * Most of our global data which we need to save even as we leave the
217 * driver due to an interrupt or a timer event is stored in a variable
218 * of type idefloppy_floppy_t, defined below.
220 typedef struct ide_floppy_obj {
222 ide_driver_t *driver;
223 struct gendisk *disk;
225 unsigned int openers; /* protected by BKL for now */
227 /* Current packet command */
229 /* Last failed packet command */
230 idefloppy_pc_t *failed_pc;
231 /* Packet command stack */
232 idefloppy_pc_t pc_stack[IDEFLOPPY_PC_STACK];
233 /* Next free packet command storage space */
235 struct request rq_stack[IDEFLOPPY_PC_STACK];
236 /* We implement a circular array */
240 * Last error information
242 u8 sense_key, asc, ascq;
243 /* delay this long before sending packet command */
245 int progress_indication;
251 int blocks, block_size, bs_factor;
252 /* Last format capacity */
253 idefloppy_capacity_descriptor_t capacity;
254 /* Copy of the flexible disk page */
255 idefloppy_flexible_disk_page_t flexible_disk_page;
258 /* Supports format progress report */
260 /* Status/Action flags */
262 } idefloppy_floppy_t;
264 #define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
267 * Floppy flag bits values.
269 #define IDEFLOPPY_DRQ_INTERRUPT 0 /* DRQ interrupt device */
270 #define IDEFLOPPY_MEDIA_CHANGED 1 /* Media may have changed */
271 #define IDEFLOPPY_USE_READ12 2 /* Use READ12/WRITE12 or READ10/WRITE10 */
272 #define IDEFLOPPY_FORMAT_IN_PROGRESS 3 /* Format in progress */
273 #define IDEFLOPPY_CLIK_DRIVE 4 /* Avoid commands not supported in Clik drive */
274 #define IDEFLOPPY_ZIP_DRIVE 5 /* Requires BH algorithm for packets */
277 * Defines for the mode sense command
279 #define MODE_SENSE_CURRENT 0x00
280 #define MODE_SENSE_CHANGEABLE 0x01
281 #define MODE_SENSE_DEFAULT 0x02
282 #define MODE_SENSE_SAVED 0x03
285 * IOCTLs used in low-level formatting.
288 #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
289 #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
290 #define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
291 #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
294 * Error codes which are returned in rq->errors to the higher part
297 #define IDEFLOPPY_ERROR_GENERAL 101
300 * The following is used to format the general configuration word of
301 * the ATAPI IDENTIFY DEVICE command.
303 struct idefloppy_id_gcw {
304 #if defined(__LITTLE_ENDIAN_BITFIELD)
305 unsigned packet_size :2; /* Packet Size */
306 unsigned reserved234 :3; /* Reserved */
307 unsigned drq_type :2; /* Command packet DRQ type */
308 unsigned removable :1; /* Removable media */
309 unsigned device_type :5; /* Device type */
310 unsigned reserved13 :1; /* Reserved */
311 unsigned protocol :2; /* Protocol type */
312 #elif defined(__BIG_ENDIAN_BITFIELD)
313 unsigned protocol :2; /* Protocol type */
314 unsigned reserved13 :1; /* Reserved */
315 unsigned device_type :5; /* Device type */
316 unsigned removable :1; /* Removable media */
317 unsigned drq_type :2; /* Command packet DRQ type */
318 unsigned reserved234 :3; /* Reserved */
319 unsigned packet_size :2; /* Packet Size */
321 #error "Bitfield endianness not defined! Check your byteorder.h"
326 * INQUIRY packet command - Data Format
329 #if defined(__LITTLE_ENDIAN_BITFIELD)
330 unsigned device_type :5; /* Peripheral Device Type */
331 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
332 unsigned reserved1_6t0 :7; /* Reserved */
333 unsigned rmb :1; /* Removable Medium Bit */
334 unsigned ansi_version :3; /* ANSI Version */
335 unsigned ecma_version :3; /* ECMA Version */
336 unsigned iso_version :2; /* ISO Version */
337 unsigned response_format :4; /* Response Data Format */
338 unsigned reserved3_45 :2; /* Reserved */
339 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
340 unsigned reserved3_7 :1; /* AENC - Reserved */
341 #elif defined(__BIG_ENDIAN_BITFIELD)
342 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
343 unsigned device_type :5; /* Peripheral Device Type */
344 unsigned rmb :1; /* Removable Medium Bit */
345 unsigned reserved1_6t0 :7; /* Reserved */
346 unsigned iso_version :2; /* ISO Version */
347 unsigned ecma_version :3; /* ECMA Version */
348 unsigned ansi_version :3; /* ANSI Version */
349 unsigned reserved3_7 :1; /* AENC - Reserved */
350 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
351 unsigned reserved3_45 :2; /* Reserved */
352 unsigned response_format :4; /* Response Data Format */
354 #error "Bitfield endianness not defined! Check your byteorder.h"
356 u8 additional_length; /* Additional Length (total_length-4) */
357 u8 rsv5, rsv6, rsv7; /* Reserved */
358 u8 vendor_id[8]; /* Vendor Identification */
359 u8 product_id[16]; /* Product Identification */
360 u8 revision_level[4]; /* Revision Level */
361 u8 vendor_specific[20]; /* Vendor Specific - Optional */
362 u8 reserved56t95[40]; /* Reserved - Optional */
363 /* Additional information may be returned */
364 } idefloppy_inquiry_result_t;
367 * REQUEST SENSE packet command result - Data Format.
370 #if defined(__LITTLE_ENDIAN_BITFIELD)
371 unsigned error_code :7; /* Current error (0x70) */
372 unsigned valid :1; /* The information field conforms to SFF-8070i */
373 u8 reserved1 :8; /* Reserved */
374 unsigned sense_key :4; /* Sense Key */
375 unsigned reserved2_4 :1; /* Reserved */
376 unsigned ili :1; /* Incorrect Length Indicator */
377 unsigned reserved2_67 :2;
378 #elif defined(__BIG_ENDIAN_BITFIELD)
379 unsigned valid :1; /* The information field conforms to SFF-8070i */
380 unsigned error_code :7; /* Current error (0x70) */
381 u8 reserved1 :8; /* Reserved */
382 unsigned reserved2_67 :2;
383 unsigned ili :1; /* Incorrect Length Indicator */
384 unsigned reserved2_4 :1; /* Reserved */
385 unsigned sense_key :4; /* Sense Key */
387 #error "Bitfield endianness not defined! Check your byteorder.h"
389 u32 information __attribute__ ((packed));
390 u8 asl; /* Additional sense length (n-7) */
391 u32 command_specific; /* Additional command specific information */
392 u8 asc; /* Additional Sense Code */
393 u8 ascq; /* Additional Sense Code Qualifier */
394 u8 replaceable_unit_code; /* Field Replaceable Unit Code */
396 u8 pad[2]; /* Padding to 20 bytes */
397 } idefloppy_request_sense_result_t;
400 * Pages of the SELECT SENSE / MODE SENSE packet commands.
402 #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
403 #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
406 * Mode Parameter Header for the MODE SENSE packet command
409 u16 mode_data_length; /* Length of the following data transfer */
410 u8 medium_type; /* Medium Type */
411 #if defined(__LITTLE_ENDIAN_BITFIELD)
412 unsigned reserved3 :7;
413 unsigned wp :1; /* Write protect */
414 #elif defined(__BIG_ENDIAN_BITFIELD)
415 unsigned wp :1; /* Write protect */
416 unsigned reserved3 :7;
418 #error "Bitfield endianness not defined! Check your byteorder.h"
421 } idefloppy_mode_parameter_header_t;
423 static DEFINE_MUTEX(idefloppy_ref_mutex);
425 #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
427 #define ide_floppy_g(disk) \
428 container_of((disk)->private_data, struct ide_floppy_obj, driver)
430 static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
432 struct ide_floppy_obj *floppy = NULL;
434 mutex_lock(&idefloppy_ref_mutex);
435 floppy = ide_floppy_g(disk);
437 kref_get(&floppy->kref);
438 mutex_unlock(&idefloppy_ref_mutex);
442 static void idefloppy_cleanup_obj(struct kref *);
444 static void ide_floppy_put(struct ide_floppy_obj *floppy)
446 mutex_lock(&idefloppy_ref_mutex);
447 kref_put(&floppy->kref, idefloppy_cleanup_obj);
448 mutex_unlock(&idefloppy_ref_mutex);
452 * Too bad. The drive wants to send us data which we are not ready to accept.
453 * Just throw it away.
455 static void idefloppy_discard_data (ide_drive_t *drive, unsigned int bcount)
458 (void) HWIF(drive)->INB(IDE_DATA_REG);
461 #if IDEFLOPPY_DEBUG_BUGS
462 static void idefloppy_write_zeros (ide_drive_t *drive, unsigned int bcount)
465 HWIF(drive)->OUTB(0, IDE_DATA_REG);
467 #endif /* IDEFLOPPY_DEBUG_BUGS */
471 * idefloppy_do_end_request is used to finish servicing a request.
473 * For read/write requests, we will call ide_end_request to pass to the
476 static int idefloppy_do_end_request(ide_drive_t *drive, int uptodate, int nsecs)
478 idefloppy_floppy_t *floppy = drive->driver_data;
479 struct request *rq = HWGROUP(drive)->rq;
482 debug_log("Reached %s\n", __func__);
485 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
486 case 1: error = 0; break;
487 default: error = uptodate;
490 floppy->failed_pc = NULL;
491 /* Why does this happen? */
494 if (!blk_special_request(rq)) {
495 /* our real local end request function */
496 ide_end_request(drive, uptodate, nsecs);
500 /* fixme: need to move this local also */
501 ide_end_drive_cmd(drive, 0, 0);
505 static void idefloppy_input_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
507 struct request *rq = pc->rq;
508 struct bio_vec *bvec;
509 struct req_iterator iter;
514 rq_for_each_segment(bvec, rq, iter) {
518 count = min(bvec->bv_len, bcount);
520 data = bvec_kmap_irq(bvec, &flags);
521 drive->hwif->atapi_input_bytes(drive, data, count);
522 bvec_kunmap_irq(data, &flags);
525 pc->b_count += count;
529 idefloppy_do_end_request(drive, 1, done >> 9);
532 printk(KERN_ERR "%s: leftover data in idefloppy_input_buffers, bcount == %d\n", drive->name, bcount);
533 idefloppy_discard_data(drive, bcount);
537 static void idefloppy_output_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
539 struct request *rq = pc->rq;
540 struct req_iterator iter;
541 struct bio_vec *bvec;
546 rq_for_each_segment(bvec, rq, iter) {
550 count = min(bvec->bv_len, bcount);
552 data = bvec_kmap_irq(bvec, &flags);
553 drive->hwif->atapi_output_bytes(drive, data, count);
554 bvec_kunmap_irq(data, &flags);
557 pc->b_count += count;
561 idefloppy_do_end_request(drive, 1, done >> 9);
563 #if IDEFLOPPY_DEBUG_BUGS
565 printk(KERN_ERR "%s: leftover data in idefloppy_output_buffers, bcount == %d\n", drive->name, bcount);
566 idefloppy_write_zeros(drive, bcount);
571 static void idefloppy_update_buffers (ide_drive_t *drive, idefloppy_pc_t *pc)
573 struct request *rq = pc->rq;
574 struct bio *bio = rq->bio;
576 while ((bio = rq->bio) != NULL)
577 idefloppy_do_end_request(drive, 1, 0);
581 * idefloppy_queue_pc_head generates a new packet command request in front
582 * of the request queue, before the current request, so that it will be
583 * processed immediately, on the next pass through the driver.
585 static void idefloppy_queue_pc_head (ide_drive_t *drive,idefloppy_pc_t *pc,struct request *rq)
587 struct ide_floppy_obj *floppy = drive->driver_data;
589 ide_init_drive_cmd(rq);
590 rq->buffer = (char *) pc;
591 rq->cmd_type = REQ_TYPE_SPECIAL;
592 rq->rq_disk = floppy->disk;
593 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
596 static idefloppy_pc_t *idefloppy_next_pc_storage (ide_drive_t *drive)
598 idefloppy_floppy_t *floppy = drive->driver_data;
600 if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
601 floppy->pc_stack_index=0;
602 return (&floppy->pc_stack[floppy->pc_stack_index++]);
605 static struct request *idefloppy_next_rq_storage (ide_drive_t *drive)
607 idefloppy_floppy_t *floppy = drive->driver_data;
609 if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
610 floppy->rq_stack_index = 0;
611 return (&floppy->rq_stack[floppy->rq_stack_index++]);
615 * idefloppy_analyze_error is called on each failed packet command retry
616 * to analyze the request sense.
618 static void idefloppy_analyze_error (ide_drive_t *drive,idefloppy_request_sense_result_t *result)
620 idefloppy_floppy_t *floppy = drive->driver_data;
622 floppy->sense_key = result->sense_key;
623 floppy->asc = result->asc;
624 floppy->ascq = result->ascq;
625 floppy->progress_indication = result->sksv[0] & 0x80 ?
626 (u16)get_unaligned((u16 *)(result->sksv+1)):0x10000;
627 if (floppy->failed_pc)
628 debug_log("pc = %x, sense key = %x, asc = %x, ascq = %x\n",
629 floppy->failed_pc->c[0], result->sense_key,
630 result->asc, result->ascq);
632 debug_log("sense key = %x, asc = %x, ascq = %x\n",
633 result->sense_key, result->asc, result->ascq);
636 static void idefloppy_request_sense_callback (ide_drive_t *drive)
638 idefloppy_floppy_t *floppy = drive->driver_data;
640 debug_log("Reached %s\n", __func__);
642 if (!floppy->pc->error) {
643 idefloppy_analyze_error(drive,(idefloppy_request_sense_result_t *) floppy->pc->buffer);
644 idefloppy_do_end_request(drive, 1, 0);
646 printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting request!\n");
647 idefloppy_do_end_request(drive, 0, 0);
652 * General packet command callback function.
654 static void idefloppy_pc_callback (ide_drive_t *drive)
656 idefloppy_floppy_t *floppy = drive->driver_data;
658 debug_log("Reached %s\n", __func__);
660 idefloppy_do_end_request(drive, floppy->pc->error ? 0 : 1, 0);
664 * idefloppy_init_pc initializes a packet command.
666 static void idefloppy_init_pc (idefloppy_pc_t *pc)
668 memset(pc->c, 0, 12);
671 pc->request_transfer = 0;
672 pc->buffer = pc->pc_buffer;
673 pc->buffer_size = IDEFLOPPY_PC_BUFFER_SIZE;
674 pc->callback = &idefloppy_pc_callback;
677 static void idefloppy_create_request_sense_cmd (idefloppy_pc_t *pc)
679 idefloppy_init_pc(pc);
680 pc->c[0] = GPCMD_REQUEST_SENSE;
682 pc->request_transfer = 18;
683 pc->callback = &idefloppy_request_sense_callback;
687 * idefloppy_retry_pc is called when an error was detected during the
688 * last packet command. We queue a request sense packet command in
689 * the head of the request list.
691 static void idefloppy_retry_pc (ide_drive_t *drive)
696 (void)drive->hwif->INB(IDE_ERROR_REG);
697 pc = idefloppy_next_pc_storage(drive);
698 rq = idefloppy_next_rq_storage(drive);
699 idefloppy_create_request_sense_cmd(pc);
700 idefloppy_queue_pc_head(drive, pc, rq);
704 * idefloppy_pc_intr is the usual interrupt handler which will be called
705 * during a packet command.
707 static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
709 idefloppy_floppy_t *floppy = drive->driver_data;
710 ide_hwif_t *hwif = drive->hwif;
711 idefloppy_pc_t *pc = floppy->pc;
712 struct request *rq = pc->rq;
717 debug_log("Reached %s interrupt handler\n", __func__);
719 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
720 if (HWIF(drive)->ide_dma_end(drive)) {
721 set_bit(PC_DMA_ERROR, &pc->flags);
723 pc->actually_transferred = pc->request_transfer;
724 idefloppy_update_buffers(drive, pc);
726 debug_log("DMA finished\n");
729 /* Clear the interrupt */
730 stat = drive->hwif->INB(IDE_STATUS_REG);
732 if ((stat & DRQ_STAT) == 0) { /* No more interrupts */
733 debug_log("Packet command completed, %d bytes transferred\n",
734 pc->actually_transferred);
735 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
737 local_irq_enable_in_hardirq();
739 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
741 debug_log("%s: I/O error\n", drive->name);
743 if (pc->c[0] == GPCMD_REQUEST_SENSE) {
744 printk(KERN_ERR "ide-floppy: I/O error in "
745 "request sense command\n");
746 return ide_do_reset(drive);
748 /* Retry operation */
749 idefloppy_retry_pc(drive);
750 /* queued, but not started */
754 if (floppy->failed_pc == pc)
755 floppy->failed_pc = NULL;
756 /* Command finished - Call the callback function */
761 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
762 printk(KERN_ERR "ide-floppy: The floppy wants to issue "
763 "more interrupts in DMA mode\n");
765 return ide_do_reset(drive);
768 /* Get the number of bytes to transfer */
769 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
770 hwif->INB(IDE_BCOUNTL_REG);
771 /* on this interrupt */
772 ireason = hwif->INB(IDE_IREASON_REG);
775 printk(KERN_ERR "ide-floppy: CoD != 0 in idefloppy_pc_intr\n");
776 return ide_do_reset(drive);
778 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
779 /* Hopefully, we will never get here */
780 printk(KERN_ERR "ide-floppy: We wanted to %s, ",
781 (ireason & IO) ? "Write" : "Read");
782 printk(KERN_ERR "but the floppy wants us to %s !\n",
783 (ireason & IO) ? "Read" : "Write");
784 return ide_do_reset(drive);
786 if (!test_bit(PC_WRITING, &pc->flags)) {
787 /* Reading - Check that we have enough space */
788 temp = pc->actually_transferred + bcount;
789 if (temp > pc->request_transfer) {
790 if (temp > pc->buffer_size) {
791 printk(KERN_ERR "ide-floppy: The floppy wants "
792 "to send us more data than expected "
793 "- discarding data\n");
794 idefloppy_discard_data(drive, bcount);
796 ide_set_handler(drive,
802 debug_log("The floppy wants to send us more data than"
803 " expected - allowing transfer\n");
806 if (test_bit(PC_WRITING, &pc->flags)) {
807 if (pc->buffer != NULL)
808 /* Write the current buffer */
809 hwif->atapi_output_bytes(drive, pc->current_position,
812 idefloppy_output_buffers(drive, pc, bcount);
814 if (pc->buffer != NULL)
815 /* Read the current buffer */
816 hwif->atapi_input_bytes(drive, pc->current_position,
819 idefloppy_input_buffers(drive, pc, bcount);
821 /* Update the current position */
822 pc->actually_transferred += bcount;
823 pc->current_position += bcount;
825 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL); /* And set the interrupt handler again */
830 * This is the original routine that did the packet transfer.
831 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
832 * for that drive below. The algorithm is chosen based on drive type
834 static ide_startstop_t idefloppy_transfer_pc (ide_drive_t *drive)
836 ide_startstop_t startstop;
837 idefloppy_floppy_t *floppy = drive->driver_data;
840 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
841 printk(KERN_ERR "ide-floppy: Strange, packet command "
842 "initiated yet DRQ isn't asserted\n");
845 ireason = drive->hwif->INB(IDE_IREASON_REG);
846 if ((ireason & CD) == 0 || (ireason & IO)) {
847 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
848 "issuing a packet command\n");
849 return ide_do_reset(drive);
852 /* Set the interrupt routine */
853 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
854 /* Send the actual packet */
855 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
861 * What we have here is a classic case of a top half / bottom half
862 * interrupt service routine. In interrupt mode, the device sends
863 * an interrupt to signal it's ready to receive a packet. However,
864 * we need to delay about 2-3 ticks before issuing the packet or we
867 * So, follow carefully. transfer_pc1 is called as an interrupt (or
868 * directly). In either case, when the device says it's ready for a
869 * packet, we schedule the packet transfer to occur about 2-3 ticks
870 * later in transfer_pc2.
872 static int idefloppy_transfer_pc2 (ide_drive_t *drive)
874 idefloppy_floppy_t *floppy = drive->driver_data;
876 /* Send the actual packet */
877 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
878 /* Timeout for the packet command */
879 return IDEFLOPPY_WAIT_CMD;
882 static ide_startstop_t idefloppy_transfer_pc1 (ide_drive_t *drive)
884 idefloppy_floppy_t *floppy = drive->driver_data;
885 ide_startstop_t startstop;
888 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
889 printk(KERN_ERR "ide-floppy: Strange, packet command "
890 "initiated yet DRQ isn't asserted\n");
893 ireason = drive->hwif->INB(IDE_IREASON_REG);
894 if ((ireason & CD) == 0 || (ireason & IO)) {
895 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
896 "while issuing a packet command\n");
897 return ide_do_reset(drive);
900 * The following delay solves a problem with ATAPI Zip 100 drives
901 * where the Busy flag was apparently being deasserted before the
902 * unit was ready to receive data. This was happening on a
903 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
904 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
905 * used until after the packet is moved in about 50 msec.
908 ide_set_handler(drive,
909 &idefloppy_pc_intr, /* service routine for packet command */
910 floppy->ticks, /* wait this long before "failing" */
911 &idefloppy_transfer_pc2); /* fail == transfer_pc2 */
916 * idefloppy_should_report_error()
918 * Supresses error messages resulting from Medium not present
920 static inline int idefloppy_should_report_error(idefloppy_floppy_t *floppy)
922 if (floppy->sense_key == 0x02 &&
923 floppy->asc == 0x3a &&
924 floppy->ascq == 0x00)
930 * Issue a packet command
932 static ide_startstop_t idefloppy_issue_pc (ide_drive_t *drive, idefloppy_pc_t *pc)
934 idefloppy_floppy_t *floppy = drive->driver_data;
935 ide_hwif_t *hwif = drive->hwif;
936 ide_handler_t *pkt_xfer_routine;
940 if (floppy->failed_pc == NULL &&
941 pc->c[0] != GPCMD_REQUEST_SENSE)
942 floppy->failed_pc = pc;
943 /* Set the current packet command */
946 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES ||
947 test_bit(PC_ABORT, &pc->flags)) {
949 * We will "abort" retrying a packet command in case
950 * a legitimate error code was received.
952 if (!test_bit(PC_ABORT, &pc->flags)) {
953 if (!test_bit(PC_SUPPRESS_ERROR, &pc->flags)) {
954 if (idefloppy_should_report_error(floppy))
955 printk(KERN_ERR "ide-floppy: %s: I/O error, "
956 "pc = %2x, key = %2x, "
957 "asc = %2x, ascq = %2x\n",
958 drive->name, pc->c[0],
960 floppy->asc, floppy->ascq);
963 pc->error = IDEFLOPPY_ERROR_GENERAL;
965 floppy->failed_pc = NULL;
970 debug_log("Retry number - %d\n", pc->retries);
973 /* We haven't transferred any data yet */
974 pc->actually_transferred = 0;
975 pc->current_position = pc->buffer;
976 bcount = min(pc->request_transfer, 63 * 1024);
978 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags))
983 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
984 dma = !hwif->dma_setup(drive);
986 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
987 IDE_TFLAG_OUT_DEVICE, bcount, dma);
989 if (dma) { /* Begin DMA, if necessary */
990 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
991 hwif->dma_start(drive);
994 /* Can we transfer the packet when we get the interrupt or wait? */
995 if (test_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags)) {
997 pkt_xfer_routine = &idefloppy_transfer_pc1;
1000 pkt_xfer_routine = &idefloppy_transfer_pc;
1003 if (test_bit (IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags)) {
1004 /* Issue the packet command */
1005 ide_execute_command(drive, WIN_PACKETCMD,
1011 /* Issue the packet command */
1012 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
1013 return (*pkt_xfer_routine) (drive);
1017 static void idefloppy_rw_callback (ide_drive_t *drive)
1019 debug_log("Reached %s\n", __func__);
1021 idefloppy_do_end_request(drive, 1, 0);
1025 static void idefloppy_create_prevent_cmd (idefloppy_pc_t *pc, int prevent)
1027 debug_log("creating prevent removal command, prevent = %d\n", prevent);
1029 idefloppy_init_pc(pc);
1030 pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
1034 static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t *pc)
1036 idefloppy_init_pc(pc);
1037 pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
1040 pc->request_transfer = 255;
1043 static void idefloppy_create_format_unit_cmd (idefloppy_pc_t *pc, int b, int l,
1046 idefloppy_init_pc(pc);
1047 pc->c[0] = GPCMD_FORMAT_UNIT;
1050 memset(pc->buffer, 0, 12);
1051 pc->buffer[1] = 0xA2;
1052 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
1054 if (flags & 1) /* Verify bit on... */
1055 pc->buffer[1] ^= 0x20; /* ... turn off DCRT bit */
1058 put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buffer[4]));
1059 put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buffer[8]));
1061 set_bit(PC_WRITING, &pc->flags);
1065 * A mode sense command is used to "sense" floppy parameters.
1067 static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t *pc, u8 page_code, u8 type)
1069 u16 length = sizeof(idefloppy_mode_parameter_header_t);
1071 idefloppy_init_pc(pc);
1072 pc->c[0] = GPCMD_MODE_SENSE_10;
1074 pc->c[2] = page_code + (type << 6);
1076 switch (page_code) {
1077 case IDEFLOPPY_CAPABILITIES_PAGE:
1080 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
1084 printk(KERN_ERR "ide-floppy: unsupported page code "
1085 "in create_mode_sense_cmd\n");
1087 put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
1088 pc->request_transfer = length;
1091 static void idefloppy_create_start_stop_cmd (idefloppy_pc_t *pc, int start)
1093 idefloppy_init_pc(pc);
1094 pc->c[0] = GPCMD_START_STOP_UNIT;
1098 static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc)
1100 idefloppy_init_pc(pc);
1101 pc->c[0] = GPCMD_TEST_UNIT_READY;
1104 static void idefloppy_create_rw_cmd (idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq, unsigned long sector)
1106 int block = sector / floppy->bs_factor;
1107 int blocks = rq->nr_sectors / floppy->bs_factor;
1108 int cmd = rq_data_dir(rq);
1110 debug_log("create_rw1%d_cmd: block == %d, blocks == %d\n",
1111 2 * test_bit (IDEFLOPPY_USE_READ12, &floppy->flags),
1114 idefloppy_init_pc(pc);
1115 if (test_bit(IDEFLOPPY_USE_READ12, &floppy->flags)) {
1116 pc->c[0] = cmd == READ ? GPCMD_READ_12 : GPCMD_WRITE_12;
1117 put_unaligned(cpu_to_be32(blocks), (unsigned int *) &pc->c[6]);
1119 pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
1120 put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
1122 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
1123 pc->callback = &idefloppy_rw_callback;
1125 pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
1126 if (rq->cmd_flags & REQ_RW)
1127 set_bit(PC_WRITING, &pc->flags);
1129 pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
1130 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1134 idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq)
1136 idefloppy_init_pc(pc);
1137 pc->callback = &idefloppy_rw_callback;
1138 memcpy(pc->c, rq->cmd, sizeof(pc->c));
1140 pc->b_count = rq->data_len;
1141 if (rq->data_len && rq_data_dir(rq) == WRITE)
1142 set_bit(PC_WRITING, &pc->flags);
1143 pc->buffer = rq->data;
1145 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1148 * possibly problematic, doesn't look like ide-floppy correctly
1149 * handled scattered requests if dma fails...
1151 pc->request_transfer = pc->buffer_size = rq->data_len;
1155 * idefloppy_do_request is our request handling function.
1157 static ide_startstop_t idefloppy_do_request (ide_drive_t *drive, struct request *rq, sector_t block_s)
1159 idefloppy_floppy_t *floppy = drive->driver_data;
1161 unsigned long block = (unsigned long)block_s;
1163 debug_log("dev: %s, cmd_type: %x, errors: %d\n",
1164 rq->rq_disk ? rq->rq_disk->disk_name : "?",
1165 rq->cmd_type, rq->errors);
1166 debug_log("sector: %ld, nr_sectors: %ld, "
1167 "current_nr_sectors: %d\n", (long)rq->sector,
1168 rq->nr_sectors, rq->current_nr_sectors);
1170 if (rq->errors >= ERROR_MAX) {
1171 if (floppy->failed_pc != NULL) {
1172 if (idefloppy_should_report_error(floppy))
1173 printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x,"
1174 " key = %2x, asc = %2x, ascq = %2x\n",
1175 drive->name, floppy->failed_pc->c[0],
1176 floppy->sense_key, floppy->asc, floppy->ascq);
1179 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
1181 idefloppy_do_end_request(drive, 0, 0);
1184 if (blk_fs_request(rq)) {
1185 if (((long)rq->sector % floppy->bs_factor) ||
1186 (rq->nr_sectors % floppy->bs_factor)) {
1187 printk("%s: unsupported r/w request size\n",
1189 idefloppy_do_end_request(drive, 0, 0);
1192 pc = idefloppy_next_pc_storage(drive);
1193 idefloppy_create_rw_cmd(floppy, pc, rq, block);
1194 } else if (blk_special_request(rq)) {
1195 pc = (idefloppy_pc_t *) rq->buffer;
1196 } else if (blk_pc_request(rq)) {
1197 pc = idefloppy_next_pc_storage(drive);
1198 idefloppy_blockpc_cmd(floppy, pc, rq);
1200 blk_dump_rq_flags(rq,
1201 "ide-floppy: unsupported command in queue");
1202 idefloppy_do_end_request(drive, 0, 0);
1207 return idefloppy_issue_pc(drive, pc);
1211 * idefloppy_queue_pc_tail adds a special packet command request to the
1212 * tail of the request queue, and waits for it to be serviced.
1214 static int idefloppy_queue_pc_tail (ide_drive_t *drive,idefloppy_pc_t *pc)
1216 struct ide_floppy_obj *floppy = drive->driver_data;
1219 ide_init_drive_cmd (&rq);
1220 rq.buffer = (char *) pc;
1221 rq.cmd_type = REQ_TYPE_SPECIAL;
1222 rq.rq_disk = floppy->disk;
1224 return ide_do_drive_cmd(drive, &rq, ide_wait);
1228 * Look at the flexible disk page parameters. We will ignore the CHS
1229 * capacity parameters and use the LBA parameters instead.
1231 static int idefloppy_get_flexible_disk_page (ide_drive_t *drive)
1233 idefloppy_floppy_t *floppy = drive->driver_data;
1235 idefloppy_mode_parameter_header_t *header;
1236 idefloppy_flexible_disk_page_t *page;
1237 int capacity, lba_capacity;
1239 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE, MODE_SENSE_CURRENT);
1240 if (idefloppy_queue_pc_tail(drive,&pc)) {
1241 printk(KERN_ERR "ide-floppy: Can't get flexible disk "
1242 "page parameters\n");
1245 header = (idefloppy_mode_parameter_header_t *) pc.buffer;
1246 floppy->wp = header->wp;
1247 set_disk_ro(floppy->disk, floppy->wp);
1248 page = (idefloppy_flexible_disk_page_t *) (header + 1);
1250 page->transfer_rate = be16_to_cpu(page->transfer_rate);
1251 page->sector_size = be16_to_cpu(page->sector_size);
1252 page->cyls = be16_to_cpu(page->cyls);
1253 page->rpm = be16_to_cpu(page->rpm);
1254 capacity = page->cyls * page->heads * page->sectors * page->sector_size;
1255 if (memcmp (page, &floppy->flexible_disk_page, sizeof (idefloppy_flexible_disk_page_t)))
1256 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
1257 "%d sector size, %d rpm\n",
1258 drive->name, capacity / 1024, page->cyls,
1259 page->heads, page->sectors,
1260 page->transfer_rate / 8, page->sector_size, page->rpm);
1262 floppy->flexible_disk_page = *page;
1263 drive->bios_cyl = page->cyls;
1264 drive->bios_head = page->heads;
1265 drive->bios_sect = page->sectors;
1266 lba_capacity = floppy->blocks * floppy->block_size;
1267 if (capacity < lba_capacity) {
1268 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
1269 "bytes, but the drive only handles %d\n",
1270 drive->name, lba_capacity, capacity);
1271 floppy->blocks = floppy->block_size ? capacity / floppy->block_size : 0;
1276 static int idefloppy_get_capability_page(ide_drive_t *drive)
1278 idefloppy_floppy_t *floppy = drive->driver_data;
1280 idefloppy_mode_parameter_header_t *header;
1281 idefloppy_capabilities_page_t *page;
1284 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
1285 MODE_SENSE_CURRENT);
1287 set_bit(PC_SUPPRESS_ERROR, &pc.flags);
1288 if (idefloppy_queue_pc_tail(drive,&pc)) {
1292 header = (idefloppy_mode_parameter_header_t *) pc.buffer;
1293 page= (idefloppy_capabilities_page_t *)(header+1);
1294 floppy->srfp = page->srfp;
1299 * Determine if a media is present in the floppy drive, and if so,
1302 static int idefloppy_get_capacity (ide_drive_t *drive)
1304 idefloppy_floppy_t *floppy = drive->driver_data;
1306 idefloppy_capacity_header_t *header;
1307 idefloppy_capacity_descriptor_t *descriptor;
1308 int i, descriptors, rc = 1, blocks, length;
1310 drive->bios_cyl = 0;
1311 drive->bios_head = drive->bios_sect = 0;
1313 floppy->bs_factor = 1;
1314 set_capacity(floppy->disk, 0);
1316 idefloppy_create_read_capacity_cmd(&pc);
1317 if (idefloppy_queue_pc_tail(drive, &pc)) {
1318 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1321 header = (idefloppy_capacity_header_t *) pc.buffer;
1322 descriptors = header->length / sizeof(idefloppy_capacity_descriptor_t);
1323 descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
1325 for (i = 0; i < descriptors; i++, descriptor++) {
1326 blocks = descriptor->blocks = be32_to_cpu(descriptor->blocks);
1327 length = descriptor->length = be16_to_cpu(descriptor->length);
1331 switch (descriptor->dc) {
1332 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1333 case CAPACITY_UNFORMATTED:
1334 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags))
1336 * If it is not a clik drive, break out
1337 * (maintains previous driver behaviour)
1340 case CAPACITY_CURRENT:
1341 /* Normal Zip/LS-120 disks */
1342 if (memcmp(descriptor, &floppy->capacity, sizeof (idefloppy_capacity_descriptor_t)))
1343 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
1344 "sector size\n", drive->name,
1345 blocks * length / 1024, blocks, length);
1346 floppy->capacity = *descriptor;
1347 if (!length || length % 512) {
1348 printk(KERN_NOTICE "%s: %d bytes block size "
1349 "not supported\n", drive->name, length);
1351 floppy->blocks = blocks;
1352 floppy->block_size = length;
1353 if ((floppy->bs_factor = length / 512) != 1)
1354 printk(KERN_NOTICE "%s: warning: non "
1355 "512 bytes block size not "
1356 "fully supported\n",
1361 case CAPACITY_NO_CARTRIDGE:
1363 * This is a KERN_ERR so it appears on screen
1364 * for the user to see
1366 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
1368 case CAPACITY_INVALID:
1369 printk(KERN_ERR "%s: Invalid capacity for disk "
1370 "in drive\n", drive->name);
1375 debug_log("Descriptor 0 Code: %d\n", descriptor->dc);
1377 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
1378 i, blocks * length / 1024, blocks, length);
1381 /* Clik! disk does not support get_flexible_disk_page */
1382 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1383 (void) idefloppy_get_flexible_disk_page(drive);
1386 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
1391 ** Obtain the list of formattable capacities.
1392 ** Very similar to idefloppy_get_capacity, except that we push the capacity
1393 ** descriptors to userland, instead of our own structures.
1395 ** Userland gives us the following structure:
1397 ** struct idefloppy_format_capacities {
1405 ** userland initializes nformats to the number of allocated formats[]
1406 ** records. On exit we set nformats to the number of records we've
1407 ** actually initialized.
1411 static int idefloppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
1414 idefloppy_capacity_header_t *header;
1415 idefloppy_capacity_descriptor_t *descriptor;
1416 int i, descriptors, blocks, length;
1421 if (get_user(u_array_size, arg))
1424 if (u_array_size <= 0)
1427 idefloppy_create_read_capacity_cmd(&pc);
1428 if (idefloppy_queue_pc_tail(drive, &pc)) {
1429 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1432 header = (idefloppy_capacity_header_t *) pc.buffer;
1433 descriptors = header->length /
1434 sizeof(idefloppy_capacity_descriptor_t);
1435 descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
1441 ** We always skip the first capacity descriptor. That's the
1442 ** current capacity. We are interested in the remaining descriptors,
1443 ** the formattable capacities.
1446 for (i=0; i<descriptors; i++, descriptor++) {
1447 if (u_index >= u_array_size)
1448 break; /* User-supplied buffer too small */
1450 continue; /* Skip the first descriptor */
1452 blocks = be32_to_cpu(descriptor->blocks);
1453 length = be16_to_cpu(descriptor->length);
1455 if (put_user(blocks, argp))
1459 if (put_user(length, argp))
1466 if (put_user(u_index, arg))
1472 ** Send ATAPI_FORMAT_UNIT to the drive.
1474 ** Userland gives us the following structure:
1476 ** struct idefloppy_format_command {
1482 ** flags is a bitmask, currently, the only defined flag is:
1484 ** 0x01 - verify media after format.
1487 static int idefloppy_begin_format(ide_drive_t *drive, int __user *arg)
1494 if (get_user(blocks, arg) ||
1495 get_user(length, arg+1) ||
1496 get_user(flags, arg+2)) {
1500 /* Get the SFRP bit */
1501 (void) idefloppy_get_capability_page(drive);
1502 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1503 if (idefloppy_queue_pc_tail(drive, &pc)) {
1511 ** Get ATAPI_FORMAT_UNIT progress indication.
1513 ** Userland gives a pointer to an int. The int is set to a progress
1514 ** indicator 0-65536, with 65536=100%.
1516 ** If the drive does not support format progress indication, we just check
1517 ** the dsc bit, and return either 0 or 65536.
1520 static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
1522 idefloppy_floppy_t *floppy = drive->driver_data;
1524 int progress_indication = 0x10000;
1527 idefloppy_create_request_sense_cmd(&pc);
1528 if (idefloppy_queue_pc_tail(drive, &pc)) {
1532 if (floppy->sense_key == 2 &&
1534 floppy->ascq == 4) {
1535 progress_indication = floppy->progress_indication;
1537 /* Else assume format_unit has finished, and we're
1540 unsigned long flags;
1543 local_irq_save(flags);
1544 stat = drive->hwif->INB(IDE_STATUS_REG);
1545 local_irq_restore(flags);
1547 progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
1549 if (put_user(progress_indication, arg))
1556 * Return the current floppy capacity.
1558 static sector_t idefloppy_capacity (ide_drive_t *drive)
1560 idefloppy_floppy_t *floppy = drive->driver_data;
1561 unsigned long capacity = floppy->blocks * floppy->bs_factor;
1567 * idefloppy_identify_device checks if we can support a drive,
1568 * based on the ATAPI IDENTIFY command results.
1570 static int idefloppy_identify_device (ide_drive_t *drive,struct hd_driveid *id)
1572 struct idefloppy_id_gcw gcw;
1573 #if IDEFLOPPY_DEBUG_INFO
1575 #endif /* IDEFLOPPY_DEBUG_INFO */
1577 *((u16 *) &gcw) = id->config;
1580 /* kludge for Apple PowerBook internal zip */
1581 if ((gcw.device_type == 5) &&
1582 !strstr(id->model, "CD-ROM") &&
1583 strstr(id->model, "ZIP"))
1584 gcw.device_type = 0;
1587 #if IDEFLOPPY_DEBUG_INFO
1588 printk(KERN_INFO "Dumping ATAPI Identify Device floppy parameters\n");
1589 switch (gcw.protocol) {
1590 case 0: case 1: sprintf(buffer, "ATA");break;
1591 case 2: sprintf(buffer, "ATAPI");break;
1592 case 3: sprintf(buffer, "Reserved (Unknown to ide-floppy)");break;
1594 printk(KERN_INFO "Protocol Type: %s\n", buffer);
1595 switch (gcw.device_type) {
1596 case 0: sprintf(buffer, "Direct-access Device");break;
1597 case 1: sprintf(buffer, "Streaming Tape Device");break;
1598 case 2: case 3: case 4: sprintf (buffer, "Reserved");break;
1599 case 5: sprintf(buffer, "CD-ROM Device");break;
1600 case 6: sprintf(buffer, "Reserved");
1601 case 7: sprintf(buffer, "Optical memory Device");break;
1602 case 0x1f: sprintf(buffer, "Unknown or no Device type");break;
1603 default: sprintf(buffer, "Reserved");
1605 printk(KERN_INFO "Device Type: %x - %s\n", gcw.device_type, buffer);
1606 printk(KERN_INFO "Removable: %s\n",gcw.removable ? "Yes":"No");
1607 switch (gcw.drq_type) {
1608 case 0: sprintf(buffer, "Microprocessor DRQ");break;
1609 case 1: sprintf(buffer, "Interrupt DRQ");break;
1610 case 2: sprintf(buffer, "Accelerated DRQ");break;
1611 case 3: sprintf(buffer, "Reserved");break;
1613 printk(KERN_INFO "Command Packet DRQ Type: %s\n", buffer);
1614 switch (gcw.packet_size) {
1615 case 0: sprintf(buffer, "12 bytes");break;
1616 case 1: sprintf(buffer, "16 bytes");break;
1617 default: sprintf(buffer, "Reserved");break;
1619 printk(KERN_INFO "Command Packet Size: %s\n", buffer);
1620 #endif /* IDEFLOPPY_DEBUG_INFO */
1622 if (gcw.protocol != 2)
1623 printk(KERN_ERR "ide-floppy: Protocol is not ATAPI\n");
1624 else if (gcw.device_type != 0)
1625 printk(KERN_ERR "ide-floppy: Device type is not set to floppy\n");
1626 else if (!gcw.removable)
1627 printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
1628 else if (gcw.drq_type == 3) {
1629 printk(KERN_ERR "ide-floppy: Sorry, DRQ type %d not supported\n", gcw.drq_type);
1630 } else if (gcw.packet_size != 0) {
1631 printk(KERN_ERR "ide-floppy: Packet size is not 12 bytes long\n");
1637 #ifdef CONFIG_IDE_PROC_FS
1638 static void idefloppy_add_settings(ide_drive_t *drive)
1640 idefloppy_floppy_t *floppy = drive->driver_data;
1643 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
1645 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
1646 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
1647 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
1648 ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &floppy->ticks, NULL);
1651 static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
1655 * Driver initialization.
1657 static void idefloppy_setup (ide_drive_t *drive, idefloppy_floppy_t *floppy)
1659 struct idefloppy_id_gcw gcw;
1661 *((u16 *) &gcw) = drive->id->config;
1662 floppy->pc = floppy->pc_stack;
1663 if (gcw.drq_type == 1)
1664 set_bit(IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags);
1666 * We used to check revisions here. At this point however
1667 * I'm giving up. Just assume they are all broken, its easier.
1669 * The actual reason for the workarounds was likely
1670 * a driver bug after all rather than a firmware bug,
1671 * and the workaround below used to hide it. It should
1672 * be fixed as of version 1.9, but to be on the safe side
1673 * we'll leave the limitation below for the 2.2.x tree.
1676 if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
1677 set_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags);
1678 /* This value will be visible in the /proc/ide/hdx/settings */
1679 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1680 blk_queue_max_sectors(drive->queue, 64);
1684 * Guess what? The IOMEGA Clik! drive also needs the
1685 * above fix. It makes nasty clicking noises without
1686 * it, so please don't remove this.
1688 if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
1689 blk_queue_max_sectors(drive->queue, 64);
1690 set_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags);
1694 (void) idefloppy_get_capacity(drive);
1695 idefloppy_add_settings(drive);
1698 static void ide_floppy_remove(ide_drive_t *drive)
1700 idefloppy_floppy_t *floppy = drive->driver_data;
1701 struct gendisk *g = floppy->disk;
1703 ide_proc_unregister_driver(drive, floppy->driver);
1707 ide_floppy_put(floppy);
1710 static void idefloppy_cleanup_obj(struct kref *kref)
1712 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1713 ide_drive_t *drive = floppy->drive;
1714 struct gendisk *g = floppy->disk;
1716 drive->driver_data = NULL;
1717 g->private_data = NULL;
1722 #ifdef CONFIG_IDE_PROC_FS
1723 static int proc_idefloppy_read_capacity
1724 (char *page, char **start, off_t off, int count, int *eof, void *data)
1726 ide_drive_t*drive = (ide_drive_t *)data;
1729 len = sprintf(page,"%llu\n", (long long)idefloppy_capacity(drive));
1730 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1733 static ide_proc_entry_t idefloppy_proc[] = {
1734 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
1735 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1736 { NULL, 0, NULL, NULL }
1738 #endif /* CONFIG_IDE_PROC_FS */
1740 static int ide_floppy_probe(ide_drive_t *);
1742 static ide_driver_t idefloppy_driver = {
1744 .owner = THIS_MODULE,
1745 .name = "ide-floppy",
1746 .bus = &ide_bus_type,
1748 .probe = ide_floppy_probe,
1749 .remove = ide_floppy_remove,
1750 .version = IDEFLOPPY_VERSION,
1751 .media = ide_floppy,
1752 .supports_dsc_overlap = 0,
1753 .do_request = idefloppy_do_request,
1754 .end_request = idefloppy_do_end_request,
1755 .error = __ide_error,
1756 .abort = __ide_abort,
1757 #ifdef CONFIG_IDE_PROC_FS
1758 .proc = idefloppy_proc,
1762 static int idefloppy_open(struct inode *inode, struct file *filp)
1764 struct gendisk *disk = inode->i_bdev->bd_disk;
1765 struct ide_floppy_obj *floppy;
1770 debug_log("Reached %s\n", __func__);
1772 if (!(floppy = ide_floppy_get(disk)))
1775 drive = floppy->drive;
1779 if (floppy->openers == 1) {
1780 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1783 idefloppy_create_test_unit_ready_cmd(&pc);
1784 if (idefloppy_queue_pc_tail(drive, &pc)) {
1785 idefloppy_create_start_stop_cmd(&pc, 1);
1786 (void) idefloppy_queue_pc_tail(drive, &pc);
1789 if (idefloppy_get_capacity (drive)
1790 && (filp->f_flags & O_NDELAY) == 0
1792 ** Allow O_NDELAY to open a drive without a disk, or with
1793 ** an unreadable disk, so that we can get the format
1794 ** capacity of the drive or begin the format - Sam
1798 goto out_put_floppy;
1801 if (floppy->wp && (filp->f_mode & 2)) {
1803 goto out_put_floppy;
1805 set_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1806 /* IOMEGA Clik! drives do not support lock/unlock commands */
1807 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1808 idefloppy_create_prevent_cmd(&pc, 1);
1809 (void) idefloppy_queue_pc_tail(drive, &pc);
1811 check_disk_change(inode->i_bdev);
1812 } else if (test_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags)) {
1814 goto out_put_floppy;
1820 ide_floppy_put(floppy);
1824 static int idefloppy_release(struct inode *inode, struct file *filp)
1826 struct gendisk *disk = inode->i_bdev->bd_disk;
1827 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1828 ide_drive_t *drive = floppy->drive;
1831 debug_log("Reached %s\n", __func__);
1833 if (floppy->openers == 1) {
1834 /* IOMEGA Clik! drives do not support lock/unlock commands */
1835 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1836 idefloppy_create_prevent_cmd(&pc, 0);
1837 (void) idefloppy_queue_pc_tail(drive, &pc);
1840 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1845 ide_floppy_put(floppy);
1850 static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1852 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1853 ide_drive_t *drive = floppy->drive;
1855 geo->heads = drive->bios_head;
1856 geo->sectors = drive->bios_sect;
1857 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1861 static int idefloppy_ioctl(struct inode *inode, struct file *file,
1862 unsigned int cmd, unsigned long arg)
1864 struct block_device *bdev = inode->i_bdev;
1865 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1866 ide_drive_t *drive = floppy->drive;
1867 void __user *argp = (void __user *)arg;
1869 int prevent = (arg) ? 1 : 0;
1876 case CDROM_LOCKDOOR:
1877 if (floppy->openers > 1)
1880 /* The IOMEGA Clik! Drive doesn't support this command - no room for an eject mechanism */
1881 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1882 idefloppy_create_prevent_cmd(&pc, prevent);
1883 (void) idefloppy_queue_pc_tail(drive, &pc);
1885 if (cmd == CDROMEJECT) {
1886 idefloppy_create_start_stop_cmd(&pc, 2);
1887 (void) idefloppy_queue_pc_tail(drive, &pc);
1890 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1892 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
1893 return idefloppy_get_format_capacities(drive, argp);
1894 case IDEFLOPPY_IOCTL_FORMAT_START:
1896 if (!(file->f_mode & 2))
1899 if (floppy->openers > 1) {
1900 /* Don't format if someone is using the disk */
1902 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS,
1907 set_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1909 err = idefloppy_begin_format(drive, argp);
1911 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1914 ** Note, the bit will be cleared when the device is
1915 ** closed. This is the cleanest way to handle the
1916 ** situation where the drive does not support
1917 ** format progress reporting.
1919 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1920 return idefloppy_get_format_progress(drive, argp);
1924 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1925 * and CDROM_SEND_PACKET (legacy) ioctls
1927 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1928 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1929 bdev->bd_disk, cmd, argp);
1934 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1939 static int idefloppy_media_changed(struct gendisk *disk)
1941 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1942 ide_drive_t *drive = floppy->drive;
1944 /* do not scan partitions twice if this is a removable device */
1945 if (drive->attach) {
1949 return test_and_clear_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1952 static int idefloppy_revalidate_disk(struct gendisk *disk)
1954 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1955 set_capacity(disk, idefloppy_capacity(floppy->drive));
1959 static struct block_device_operations idefloppy_ops = {
1960 .owner = THIS_MODULE,
1961 .open = idefloppy_open,
1962 .release = idefloppy_release,
1963 .ioctl = idefloppy_ioctl,
1964 .getgeo = idefloppy_getgeo,
1965 .media_changed = idefloppy_media_changed,
1966 .revalidate_disk= idefloppy_revalidate_disk
1969 static int ide_floppy_probe(ide_drive_t *drive)
1971 idefloppy_floppy_t *floppy;
1974 if (!strstr("ide-floppy", drive->driver_req))
1976 if (!drive->present)
1978 if (drive->media != ide_floppy)
1980 if (!idefloppy_identify_device (drive, drive->id)) {
1981 printk (KERN_ERR "ide-floppy: %s: not supported by this version of ide-floppy\n", drive->name);
1985 printk("ide-floppy: passing drive %s to ide-scsi emulation.\n", drive->name);
1988 if ((floppy = kzalloc(sizeof (idefloppy_floppy_t), GFP_KERNEL)) == NULL) {
1989 printk (KERN_ERR "ide-floppy: %s: Can't allocate a floppy structure\n", drive->name);
1993 g = alloc_disk(1 << PARTN_BITS);
1995 goto out_free_floppy;
1997 ide_init_disk(g, drive);
1999 ide_proc_register_driver(drive, &idefloppy_driver);
2001 kref_init(&floppy->kref);
2003 floppy->drive = drive;
2004 floppy->driver = &idefloppy_driver;
2007 g->private_data = &floppy->driver;
2009 drive->driver_data = floppy;
2011 idefloppy_setup (drive, floppy);
2013 g->minors = 1 << PARTN_BITS;
2014 g->driverfs_dev = &drive->gendev;
2015 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
2016 g->fops = &idefloppy_ops;
2027 MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
2029 static void __exit idefloppy_exit (void)
2031 driver_unregister(&idefloppy_driver.gen_driver);
2034 static int __init idefloppy_init(void)
2036 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
2037 return driver_register(&idefloppy_driver.gen_driver);
2040 MODULE_ALIAS("ide:*m-floppy*");
2041 module_init(idefloppy_init);
2042 module_exit(idefloppy_exit);
2043 MODULE_LICENSE("GPL");