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>
46 #include <asm/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 printk
63 #define debug_log(fmt, args... ) do {} while(0)
68 * Some drives require a longer irq timeout.
70 #define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
73 * After each failed packet command we issue a request sense command
74 * and retry the packet command IDEFLOPPY_MAX_PC_RETRIES times.
76 #define IDEFLOPPY_MAX_PC_RETRIES 3
79 * With each packet command, we allocate a buffer of
80 * IDEFLOPPY_PC_BUFFER_SIZE bytes.
82 #define IDEFLOPPY_PC_BUFFER_SIZE 256
85 * In various places in the driver, we need to allocate storage
86 * for packet commands and requests, which will remain valid while
87 * we leave the driver to wait for an interrupt or a timeout event.
89 #define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
92 * Our view of a packet command.
94 typedef struct idefloppy_packet_command_s {
95 u8 c[12]; /* Actual packet bytes */
96 int retries; /* On each retry, we increment retries */
97 int error; /* Error code */
98 int request_transfer; /* Bytes to transfer */
99 int actually_transferred; /* Bytes actually transferred */
100 int buffer_size; /* Size of our data buffer */
101 int b_count; /* Missing/Available data on the current buffer */
102 struct request *rq; /* The corresponding request */
103 u8 *buffer; /* Data buffer */
104 u8 *current_position; /* Pointer into the above buffer */
105 void (*callback) (ide_drive_t *); /* Called when this packet command is completed */
106 u8 pc_buffer[IDEFLOPPY_PC_BUFFER_SIZE]; /* Temporary buffer */
107 unsigned long flags; /* Status/Action bit flags: long for set_bit */
111 * Packet command flag bits.
113 #define PC_ABORT 0 /* Set when an error is considered normal - We won't retry */
114 #define PC_DMA_RECOMMENDED 2 /* 1 when we prefer to use DMA if possible */
115 #define PC_DMA_IN_PROGRESS 3 /* 1 while DMA in progress */
116 #define PC_DMA_ERROR 4 /* 1 when encountered problem during DMA */
117 #define PC_WRITING 5 /* Data direction */
119 #define PC_SUPPRESS_ERROR 6 /* Suppress error reporting */
122 * Removable Block Access Capabilities Page
125 #if defined(__LITTLE_ENDIAN_BITFIELD)
126 unsigned page_code :6; /* Page code - Should be 0x1b */
127 unsigned reserved1_6 :1; /* Reserved */
128 unsigned ps :1; /* Should be 0 */
129 #elif defined(__BIG_ENDIAN_BITFIELD)
130 unsigned ps :1; /* Should be 0 */
131 unsigned reserved1_6 :1; /* Reserved */
132 unsigned page_code :6; /* Page code - Should be 0x1b */
134 #error "Bitfield endianness not defined! Check your byteorder.h"
136 u8 page_length; /* Page Length - Should be 0xa */
137 #if defined(__LITTLE_ENDIAN_BITFIELD)
138 unsigned reserved2 :6;
139 unsigned srfp :1; /* Supports reporting progress of format */
140 unsigned sflp :1; /* System floppy type device */
141 unsigned tlun :3; /* Total logical units supported by the device */
142 unsigned reserved3 :3;
143 unsigned sml :1; /* Single / Multiple lun supported */
144 unsigned ncd :1; /* Non cd optical device */
145 #elif defined(__BIG_ENDIAN_BITFIELD)
146 unsigned sflp :1; /* System floppy type device */
147 unsigned srfp :1; /* Supports reporting progress of format */
148 unsigned reserved2 :6;
149 unsigned ncd :1; /* Non cd optical device */
150 unsigned sml :1; /* Single / Multiple lun supported */
151 unsigned reserved3 :3;
152 unsigned tlun :3; /* Total logical units supported by the device */
154 #error "Bitfield endianness not defined! Check your byteorder.h"
157 } idefloppy_capabilities_page_t;
160 * Flexible disk page.
163 #if defined(__LITTLE_ENDIAN_BITFIELD)
164 unsigned page_code :6; /* Page code - Should be 0x5 */
165 unsigned reserved1_6 :1; /* Reserved */
166 unsigned ps :1; /* The device is capable of saving the page */
167 #elif defined(__BIG_ENDIAN_BITFIELD)
168 unsigned ps :1; /* The device is capable of saving the page */
169 unsigned reserved1_6 :1; /* Reserved */
170 unsigned page_code :6; /* Page code - Should be 0x5 */
172 #error "Bitfield endianness not defined! Check your byteorder.h"
174 u8 page_length; /* Page Length - Should be 0x1e */
175 u16 transfer_rate; /* In kilobits per second */
176 u8 heads, sectors; /* Number of heads, Number of sectors per track */
177 u16 sector_size; /* Byes per sector */
178 u16 cyls; /* Number of cylinders */
180 u8 motor_delay; /* Motor off delay */
182 u16 rpm; /* Rotations per minute */
184 } idefloppy_flexible_disk_page_t;
191 u8 length; /* Length of the following descriptors in bytes */
192 } idefloppy_capacity_header_t;
195 u32 blocks; /* Number of blocks */
196 #if defined(__LITTLE_ENDIAN_BITFIELD)
197 unsigned dc :2; /* Descriptor Code */
198 unsigned reserved :6;
199 #elif defined(__BIG_ENDIAN_BITFIELD)
200 unsigned reserved :6;
201 unsigned dc :2; /* Descriptor Code */
203 #error "Bitfield endianness not defined! Check your byteorder.h"
205 u8 length_msb; /* Block Length (MSB)*/
206 u16 length; /* Block Length */
207 } idefloppy_capacity_descriptor_t;
209 #define CAPACITY_INVALID 0x00
210 #define CAPACITY_UNFORMATTED 0x01
211 #define CAPACITY_CURRENT 0x02
212 #define CAPACITY_NO_CARTRIDGE 0x03
215 * Most of our global data which we need to save even as we leave the
216 * driver due to an interrupt or a timer event is stored in a variable
217 * of type idefloppy_floppy_t, defined below.
219 typedef struct ide_floppy_obj {
221 ide_driver_t *driver;
222 struct gendisk *disk;
224 unsigned int openers; /* protected by BKL for now */
226 /* Current packet command */
228 /* Last failed packet command */
229 idefloppy_pc_t *failed_pc;
230 /* Packet command stack */
231 idefloppy_pc_t pc_stack[IDEFLOPPY_PC_STACK];
232 /* Next free packet command storage space */
234 struct request rq_stack[IDEFLOPPY_PC_STACK];
235 /* We implement a circular array */
239 * Last error information
241 u8 sense_key, asc, ascq;
242 /* delay this long before sending packet command */
244 int progress_indication;
250 int blocks, block_size, bs_factor;
251 /* Last format capacity */
252 idefloppy_capacity_descriptor_t capacity;
253 /* Copy of the flexible disk page */
254 idefloppy_flexible_disk_page_t flexible_disk_page;
257 /* Supports format progress report */
259 /* Status/Action flags */
261 } idefloppy_floppy_t;
263 #define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
266 * Floppy flag bits values.
268 #define IDEFLOPPY_DRQ_INTERRUPT 0 /* DRQ interrupt device */
269 #define IDEFLOPPY_MEDIA_CHANGED 1 /* Media may have changed */
270 #define IDEFLOPPY_USE_READ12 2 /* Use READ12/WRITE12 or READ10/WRITE10 */
271 #define IDEFLOPPY_FORMAT_IN_PROGRESS 3 /* Format in progress */
272 #define IDEFLOPPY_CLIK_DRIVE 4 /* Avoid commands not supported in Clik drive */
273 #define IDEFLOPPY_ZIP_DRIVE 5 /* Requires BH algorithm for packets */
276 * ATAPI floppy drive packet commands
278 #define IDEFLOPPY_FORMAT_UNIT_CMD 0x04
279 #define IDEFLOPPY_INQUIRY_CMD 0x12
280 #define IDEFLOPPY_MODE_SELECT_CMD 0x55
281 #define IDEFLOPPY_MODE_SENSE_CMD 0x5a
282 #define IDEFLOPPY_READ10_CMD 0x28
283 #define IDEFLOPPY_READ12_CMD 0xa8
284 #define IDEFLOPPY_READ_CAPACITY_CMD 0x23
285 #define IDEFLOPPY_REQUEST_SENSE_CMD 0x03
286 #define IDEFLOPPY_PREVENT_REMOVAL_CMD 0x1e
287 #define IDEFLOPPY_SEEK_CMD 0x2b
288 #define IDEFLOPPY_START_STOP_CMD 0x1b
289 #define IDEFLOPPY_TEST_UNIT_READY_CMD 0x00
290 #define IDEFLOPPY_VERIFY_CMD 0x2f
291 #define IDEFLOPPY_WRITE10_CMD 0x2a
292 #define IDEFLOPPY_WRITE12_CMD 0xaa
293 #define IDEFLOPPY_WRITE_VERIFY_CMD 0x2e
296 * Defines for the mode sense command
298 #define MODE_SENSE_CURRENT 0x00
299 #define MODE_SENSE_CHANGEABLE 0x01
300 #define MODE_SENSE_DEFAULT 0x02
301 #define MODE_SENSE_SAVED 0x03
304 * IOCTLs used in low-level formatting.
307 #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
308 #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
309 #define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
310 #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
313 * Error codes which are returned in rq->errors to the higher part
316 #define IDEFLOPPY_ERROR_GENERAL 101
319 * The following is used to format the general configuration word of
320 * the ATAPI IDENTIFY DEVICE command.
322 struct idefloppy_id_gcw {
323 #if defined(__LITTLE_ENDIAN_BITFIELD)
324 unsigned packet_size :2; /* Packet Size */
325 unsigned reserved234 :3; /* Reserved */
326 unsigned drq_type :2; /* Command packet DRQ type */
327 unsigned removable :1; /* Removable media */
328 unsigned device_type :5; /* Device type */
329 unsigned reserved13 :1; /* Reserved */
330 unsigned protocol :2; /* Protocol type */
331 #elif defined(__BIG_ENDIAN_BITFIELD)
332 unsigned protocol :2; /* Protocol type */
333 unsigned reserved13 :1; /* Reserved */
334 unsigned device_type :5; /* Device type */
335 unsigned removable :1; /* Removable media */
336 unsigned drq_type :2; /* Command packet DRQ type */
337 unsigned reserved234 :3; /* Reserved */
338 unsigned packet_size :2; /* Packet Size */
340 #error "Bitfield endianness not defined! Check your byteorder.h"
345 * INQUIRY packet command - Data Format
348 #if defined(__LITTLE_ENDIAN_BITFIELD)
349 unsigned device_type :5; /* Peripheral Device Type */
350 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
351 unsigned reserved1_6t0 :7; /* Reserved */
352 unsigned rmb :1; /* Removable Medium Bit */
353 unsigned ansi_version :3; /* ANSI Version */
354 unsigned ecma_version :3; /* ECMA Version */
355 unsigned iso_version :2; /* ISO Version */
356 unsigned response_format :4; /* Response Data Format */
357 unsigned reserved3_45 :2; /* Reserved */
358 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
359 unsigned reserved3_7 :1; /* AENC - Reserved */
360 #elif defined(__BIG_ENDIAN_BITFIELD)
361 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
362 unsigned device_type :5; /* Peripheral Device Type */
363 unsigned rmb :1; /* Removable Medium Bit */
364 unsigned reserved1_6t0 :7; /* Reserved */
365 unsigned iso_version :2; /* ISO Version */
366 unsigned ecma_version :3; /* ECMA Version */
367 unsigned ansi_version :3; /* ANSI Version */
368 unsigned reserved3_7 :1; /* AENC - Reserved */
369 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
370 unsigned reserved3_45 :2; /* Reserved */
371 unsigned response_format :4; /* Response Data Format */
373 #error "Bitfield endianness not defined! Check your byteorder.h"
375 u8 additional_length; /* Additional Length (total_length-4) */
376 u8 rsv5, rsv6, rsv7; /* Reserved */
377 u8 vendor_id[8]; /* Vendor Identification */
378 u8 product_id[16]; /* Product Identification */
379 u8 revision_level[4]; /* Revision Level */
380 u8 vendor_specific[20]; /* Vendor Specific - Optional */
381 u8 reserved56t95[40]; /* Reserved - Optional */
382 /* Additional information may be returned */
383 } idefloppy_inquiry_result_t;
386 * REQUEST SENSE packet command result - Data Format.
389 #if defined(__LITTLE_ENDIAN_BITFIELD)
390 unsigned error_code :7; /* Current error (0x70) */
391 unsigned valid :1; /* The information field conforms to SFF-8070i */
392 u8 reserved1 :8; /* Reserved */
393 unsigned sense_key :4; /* Sense Key */
394 unsigned reserved2_4 :1; /* Reserved */
395 unsigned ili :1; /* Incorrect Length Indicator */
396 unsigned reserved2_67 :2;
397 #elif defined(__BIG_ENDIAN_BITFIELD)
398 unsigned valid :1; /* The information field conforms to SFF-8070i */
399 unsigned error_code :7; /* Current error (0x70) */
400 u8 reserved1 :8; /* Reserved */
401 unsigned reserved2_67 :2;
402 unsigned ili :1; /* Incorrect Length Indicator */
403 unsigned reserved2_4 :1; /* Reserved */
404 unsigned sense_key :4; /* Sense Key */
406 #error "Bitfield endianness not defined! Check your byteorder.h"
408 u32 information __attribute__ ((packed));
409 u8 asl; /* Additional sense length (n-7) */
410 u32 command_specific; /* Additional command specific information */
411 u8 asc; /* Additional Sense Code */
412 u8 ascq; /* Additional Sense Code Qualifier */
413 u8 replaceable_unit_code; /* Field Replaceable Unit Code */
415 u8 pad[2]; /* Padding to 20 bytes */
416 } idefloppy_request_sense_result_t;
419 * Pages of the SELECT SENSE / MODE SENSE packet commands.
421 #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
422 #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
425 * Mode Parameter Header for the MODE SENSE packet command
428 u16 mode_data_length; /* Length of the following data transfer */
429 u8 medium_type; /* Medium Type */
430 #if defined(__LITTLE_ENDIAN_BITFIELD)
431 unsigned reserved3 :7;
432 unsigned wp :1; /* Write protect */
433 #elif defined(__BIG_ENDIAN_BITFIELD)
434 unsigned wp :1; /* Write protect */
435 unsigned reserved3 :7;
437 #error "Bitfield endianness not defined! Check your byteorder.h"
440 } idefloppy_mode_parameter_header_t;
442 static DEFINE_MUTEX(idefloppy_ref_mutex);
444 #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
446 #define ide_floppy_g(disk) \
447 container_of((disk)->private_data, struct ide_floppy_obj, driver)
449 static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
451 struct ide_floppy_obj *floppy = NULL;
453 mutex_lock(&idefloppy_ref_mutex);
454 floppy = ide_floppy_g(disk);
456 kref_get(&floppy->kref);
457 mutex_unlock(&idefloppy_ref_mutex);
461 static void ide_floppy_release(struct kref *);
463 static void ide_floppy_put(struct ide_floppy_obj *floppy)
465 mutex_lock(&idefloppy_ref_mutex);
466 kref_put(&floppy->kref, ide_floppy_release);
467 mutex_unlock(&idefloppy_ref_mutex);
471 * Too bad. The drive wants to send us data which we are not ready to accept.
472 * Just throw it away.
474 static void idefloppy_discard_data (ide_drive_t *drive, unsigned int bcount)
477 (void) HWIF(drive)->INB(IDE_DATA_REG);
480 #if IDEFLOPPY_DEBUG_BUGS
481 static void idefloppy_write_zeros (ide_drive_t *drive, unsigned int bcount)
484 HWIF(drive)->OUTB(0, IDE_DATA_REG);
486 #endif /* IDEFLOPPY_DEBUG_BUGS */
490 * idefloppy_do_end_request is used to finish servicing a request.
492 * For read/write requests, we will call ide_end_request to pass to the
495 static int idefloppy_do_end_request(ide_drive_t *drive, int uptodate, int nsecs)
497 idefloppy_floppy_t *floppy = drive->driver_data;
498 struct request *rq = HWGROUP(drive)->rq;
501 debug_log(KERN_INFO "Reached idefloppy_end_request\n");
504 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
505 case 1: error = 0; break;
506 default: error = uptodate;
509 floppy->failed_pc = NULL;
510 /* Why does this happen? */
513 if (!blk_special_request(rq)) {
514 /* our real local end request function */
515 ide_end_request(drive, uptodate, nsecs);
519 /* fixme: need to move this local also */
520 ide_end_drive_cmd(drive, 0, 0);
524 static void idefloppy_input_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
526 struct request *rq = pc->rq;
527 struct bio_vec *bvec;
528 struct req_iterator iter;
533 rq_for_each_segment(bvec, rq, iter) {
537 count = min(bvec->bv_len, bcount);
539 data = bvec_kmap_irq(bvec, &flags);
540 drive->hwif->atapi_input_bytes(drive, data, count);
541 bvec_kunmap_irq(data, &flags);
544 pc->b_count += count;
548 idefloppy_do_end_request(drive, 1, done >> 9);
551 printk(KERN_ERR "%s: leftover data in idefloppy_input_buffers, bcount == %d\n", drive->name, bcount);
552 idefloppy_discard_data(drive, bcount);
556 static void idefloppy_output_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
558 struct request *rq = pc->rq;
559 struct req_iterator iter;
560 struct bio_vec *bvec;
565 rq_for_each_segment(bvec, rq, iter) {
569 count = min(bvec->bv_len, bcount);
571 data = bvec_kmap_irq(bvec, &flags);
572 drive->hwif->atapi_output_bytes(drive, data, count);
573 bvec_kunmap_irq(data, &flags);
576 pc->b_count += count;
580 idefloppy_do_end_request(drive, 1, done >> 9);
582 #if IDEFLOPPY_DEBUG_BUGS
584 printk(KERN_ERR "%s: leftover data in idefloppy_output_buffers, bcount == %d\n", drive->name, bcount);
585 idefloppy_write_zeros(drive, bcount);
590 static void idefloppy_update_buffers (ide_drive_t *drive, idefloppy_pc_t *pc)
592 struct request *rq = pc->rq;
593 struct bio *bio = rq->bio;
595 while ((bio = rq->bio) != NULL)
596 idefloppy_do_end_request(drive, 1, 0);
600 * idefloppy_queue_pc_head generates a new packet command request in front
601 * of the request queue, before the current request, so that it will be
602 * processed immediately, on the next pass through the driver.
604 static void idefloppy_queue_pc_head (ide_drive_t *drive,idefloppy_pc_t *pc,struct request *rq)
606 struct ide_floppy_obj *floppy = drive->driver_data;
608 ide_init_drive_cmd(rq);
609 rq->buffer = (char *) pc;
610 rq->cmd_type = REQ_TYPE_SPECIAL;
611 rq->rq_disk = floppy->disk;
612 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
615 static idefloppy_pc_t *idefloppy_next_pc_storage (ide_drive_t *drive)
617 idefloppy_floppy_t *floppy = drive->driver_data;
619 if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
620 floppy->pc_stack_index=0;
621 return (&floppy->pc_stack[floppy->pc_stack_index++]);
624 static struct request *idefloppy_next_rq_storage (ide_drive_t *drive)
626 idefloppy_floppy_t *floppy = drive->driver_data;
628 if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
629 floppy->rq_stack_index = 0;
630 return (&floppy->rq_stack[floppy->rq_stack_index++]);
634 * idefloppy_analyze_error is called on each failed packet command retry
635 * to analyze the request sense.
637 static void idefloppy_analyze_error (ide_drive_t *drive,idefloppy_request_sense_result_t *result)
639 idefloppy_floppy_t *floppy = drive->driver_data;
641 floppy->sense_key = result->sense_key;
642 floppy->asc = result->asc;
643 floppy->ascq = result->ascq;
644 floppy->progress_indication = result->sksv[0] & 0x80 ?
645 (u16)get_unaligned((u16 *)(result->sksv+1)):0x10000;
646 if (floppy->failed_pc)
647 debug_log(KERN_INFO "ide-floppy: pc = %x, sense key = %x, "
648 "asc = %x, ascq = %x\n", floppy->failed_pc->c[0],
649 result->sense_key, result->asc, result->ascq);
651 debug_log(KERN_INFO "ide-floppy: sense key = %x, asc = %x, "
652 "ascq = %x\n", result->sense_key,
653 result->asc, result->ascq);
656 static void idefloppy_request_sense_callback (ide_drive_t *drive)
658 idefloppy_floppy_t *floppy = drive->driver_data;
660 debug_log(KERN_INFO "ide-floppy: Reached %s\n", __FUNCTION__);
662 if (!floppy->pc->error) {
663 idefloppy_analyze_error(drive,(idefloppy_request_sense_result_t *) floppy->pc->buffer);
664 idefloppy_do_end_request(drive, 1, 0);
666 printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting request!\n");
667 idefloppy_do_end_request(drive, 0, 0);
672 * General packet command callback function.
674 static void idefloppy_pc_callback (ide_drive_t *drive)
676 idefloppy_floppy_t *floppy = drive->driver_data;
678 debug_log(KERN_INFO "ide-floppy: Reached %s\n", __FUNCTION__);
680 idefloppy_do_end_request(drive, floppy->pc->error ? 0 : 1, 0);
684 * idefloppy_init_pc initializes a packet command.
686 static void idefloppy_init_pc (idefloppy_pc_t *pc)
688 memset(pc->c, 0, 12);
691 pc->request_transfer = 0;
692 pc->buffer = pc->pc_buffer;
693 pc->buffer_size = IDEFLOPPY_PC_BUFFER_SIZE;
694 pc->callback = &idefloppy_pc_callback;
697 static void idefloppy_create_request_sense_cmd (idefloppy_pc_t *pc)
699 idefloppy_init_pc(pc);
700 pc->c[0] = IDEFLOPPY_REQUEST_SENSE_CMD;
702 pc->request_transfer = 18;
703 pc->callback = &idefloppy_request_sense_callback;
707 * idefloppy_retry_pc is called when an error was detected during the
708 * last packet command. We queue a request sense packet command in
709 * the head of the request list.
711 static void idefloppy_retry_pc (ide_drive_t *drive)
716 (void)drive->hwif->INB(IDE_ERROR_REG);
717 pc = idefloppy_next_pc_storage(drive);
718 rq = idefloppy_next_rq_storage(drive);
719 idefloppy_create_request_sense_cmd(pc);
720 idefloppy_queue_pc_head(drive, pc, rq);
724 * idefloppy_pc_intr is the usual interrupt handler which will be called
725 * during a packet command.
727 static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
729 idefloppy_floppy_t *floppy = drive->driver_data;
730 ide_hwif_t *hwif = drive->hwif;
731 idefloppy_pc_t *pc = floppy->pc;
732 struct request *rq = pc->rq;
737 debug_log(KERN_INFO "ide-floppy: Reached %s interrupt handler\n",
740 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
741 if (HWIF(drive)->ide_dma_end(drive)) {
742 set_bit(PC_DMA_ERROR, &pc->flags);
744 pc->actually_transferred = pc->request_transfer;
745 idefloppy_update_buffers(drive, pc);
747 debug_log(KERN_INFO "ide-floppy: DMA finished\n");
750 /* Clear the interrupt */
751 stat = drive->hwif->INB(IDE_STATUS_REG);
753 if ((stat & DRQ_STAT) == 0) { /* No more interrupts */
754 debug_log(KERN_INFO "Packet command completed, %d bytes "
755 "transferred\n", pc->actually_transferred);
756 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
758 local_irq_enable_in_hardirq();
760 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
762 debug_log(KERN_INFO "ide-floppy: %s: I/O error\n",
765 if (pc->c[0] == IDEFLOPPY_REQUEST_SENSE_CMD) {
766 printk(KERN_ERR "ide-floppy: I/O error in "
767 "request sense command\n");
768 return ide_do_reset(drive);
770 /* Retry operation */
771 idefloppy_retry_pc(drive);
772 /* queued, but not started */
776 if (floppy->failed_pc == pc)
777 floppy->failed_pc = NULL;
778 /* Command finished - Call the callback function */
783 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
784 printk(KERN_ERR "ide-floppy: The floppy wants to issue "
785 "more interrupts in DMA mode\n");
787 return ide_do_reset(drive);
790 /* Get the number of bytes to transfer */
791 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
792 hwif->INB(IDE_BCOUNTL_REG);
793 /* on this interrupt */
794 ireason = hwif->INB(IDE_IREASON_REG);
797 printk(KERN_ERR "ide-floppy: CoD != 0 in idefloppy_pc_intr\n");
798 return ide_do_reset(drive);
800 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
801 /* Hopefully, we will never get here */
802 printk(KERN_ERR "ide-floppy: We wanted to %s, ",
803 (ireason & IO) ? "Write" : "Read");
804 printk(KERN_ERR "but the floppy wants us to %s !\n",
805 (ireason & IO) ? "Read" : "Write");
806 return ide_do_reset(drive);
808 if (!test_bit(PC_WRITING, &pc->flags)) {
809 /* Reading - Check that we have enough space */
810 temp = pc->actually_transferred + bcount;
811 if (temp > pc->request_transfer) {
812 if (temp > pc->buffer_size) {
813 printk(KERN_ERR "ide-floppy: The floppy wants "
814 "to send us more data than expected "
815 "- discarding data\n");
816 idefloppy_discard_data(drive, bcount);
817 BUG_ON(HWGROUP(drive)->handler != NULL);
818 ide_set_handler(drive,
824 debug_log(KERN_NOTICE "ide-floppy: The floppy wants to "
825 "send us more data than expected - "
826 "allowing transfer\n");
829 if (test_bit(PC_WRITING, &pc->flags)) {
830 if (pc->buffer != NULL)
831 /* Write the current buffer */
832 hwif->atapi_output_bytes(drive, pc->current_position,
835 idefloppy_output_buffers(drive, pc, bcount);
837 if (pc->buffer != NULL)
838 /* Read the current buffer */
839 hwif->atapi_input_bytes(drive, pc->current_position,
842 idefloppy_input_buffers(drive, pc, bcount);
844 /* Update the current position */
845 pc->actually_transferred += bcount;
846 pc->current_position += bcount;
848 BUG_ON(HWGROUP(drive)->handler != NULL);
849 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL); /* And set the interrupt handler again */
854 * This is the original routine that did the packet transfer.
855 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
856 * for that drive below. The algorithm is chosen based on drive type
858 static ide_startstop_t idefloppy_transfer_pc (ide_drive_t *drive)
860 ide_startstop_t startstop;
861 idefloppy_floppy_t *floppy = drive->driver_data;
864 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
865 printk(KERN_ERR "ide-floppy: Strange, packet command "
866 "initiated yet DRQ isn't asserted\n");
869 ireason = drive->hwif->INB(IDE_IREASON_REG);
870 if ((ireason & CD) == 0 || (ireason & IO)) {
871 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
872 "issuing a packet command\n");
873 return ide_do_reset(drive);
875 BUG_ON(HWGROUP(drive)->handler != NULL);
876 /* Set the interrupt routine */
877 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
878 /* Send the actual packet */
879 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
885 * What we have here is a classic case of a top half / bottom half
886 * interrupt service routine. In interrupt mode, the device sends
887 * an interrupt to signal it's ready to receive a packet. However,
888 * we need to delay about 2-3 ticks before issuing the packet or we
891 * So, follow carefully. transfer_pc1 is called as an interrupt (or
892 * directly). In either case, when the device says it's ready for a
893 * packet, we schedule the packet transfer to occur about 2-3 ticks
894 * later in transfer_pc2.
896 static int idefloppy_transfer_pc2 (ide_drive_t *drive)
898 idefloppy_floppy_t *floppy = drive->driver_data;
900 /* Send the actual packet */
901 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
902 /* Timeout for the packet command */
903 return IDEFLOPPY_WAIT_CMD;
906 static ide_startstop_t idefloppy_transfer_pc1 (ide_drive_t *drive)
908 idefloppy_floppy_t *floppy = drive->driver_data;
909 ide_startstop_t startstop;
912 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
913 printk(KERN_ERR "ide-floppy: Strange, packet command "
914 "initiated yet DRQ isn't asserted\n");
917 ireason = drive->hwif->INB(IDE_IREASON_REG);
918 if ((ireason & CD) == 0 || (ireason & IO)) {
919 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
920 "while issuing a packet command\n");
921 return ide_do_reset(drive);
924 * The following delay solves a problem with ATAPI Zip 100 drives
925 * where the Busy flag was apparently being deasserted before the
926 * unit was ready to receive data. This was happening on a
927 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
928 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
929 * used until after the packet is moved in about 50 msec.
931 BUG_ON(HWGROUP(drive)->handler != NULL);
932 ide_set_handler(drive,
933 &idefloppy_pc_intr, /* service routine for packet command */
934 floppy->ticks, /* wait this long before "failing" */
935 &idefloppy_transfer_pc2); /* fail == transfer_pc2 */
940 * idefloppy_should_report_error()
942 * Supresses error messages resulting from Medium not present
944 static inline int idefloppy_should_report_error(idefloppy_floppy_t *floppy)
946 if (floppy->sense_key == 0x02 &&
947 floppy->asc == 0x3a &&
948 floppy->ascq == 0x00)
954 * Issue a packet command
956 static ide_startstop_t idefloppy_issue_pc (ide_drive_t *drive, idefloppy_pc_t *pc)
958 idefloppy_floppy_t *floppy = drive->driver_data;
959 ide_hwif_t *hwif = drive->hwif;
960 ide_handler_t *pkt_xfer_routine;
964 if (floppy->failed_pc == NULL &&
965 pc->c[0] != IDEFLOPPY_REQUEST_SENSE_CMD)
966 floppy->failed_pc = pc;
967 /* Set the current packet command */
970 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES ||
971 test_bit(PC_ABORT, &pc->flags)) {
973 * We will "abort" retrying a packet command in case
974 * a legitimate error code was received.
976 if (!test_bit(PC_ABORT, &pc->flags)) {
977 if (!test_bit(PC_SUPPRESS_ERROR, &pc->flags)) {
978 if (idefloppy_should_report_error(floppy))
979 printk(KERN_ERR "ide-floppy: %s: I/O error, "
980 "pc = %2x, key = %2x, "
981 "asc = %2x, ascq = %2x\n",
982 drive->name, pc->c[0],
984 floppy->asc, floppy->ascq);
987 pc->error = IDEFLOPPY_ERROR_GENERAL;
989 floppy->failed_pc = NULL;
994 debug_log(KERN_INFO "Retry number - %d\n",pc->retries);
997 /* We haven't transferred any data yet */
998 pc->actually_transferred = 0;
999 pc->current_position = pc->buffer;
1000 bcount = min(pc->request_transfer, 63 * 1024);
1002 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags))
1007 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
1008 dma = !hwif->dma_setup(drive);
1010 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1011 IDE_TFLAG_OUT_DEVICE, bcount, dma);
1013 if (dma) { /* Begin DMA, if necessary */
1014 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1015 hwif->dma_start(drive);
1018 /* Can we transfer the packet when we get the interrupt or wait? */
1019 if (test_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags)) {
1021 pkt_xfer_routine = &idefloppy_transfer_pc1;
1024 pkt_xfer_routine = &idefloppy_transfer_pc;
1027 if (test_bit (IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags)) {
1028 /* Issue the packet command */
1029 ide_execute_command(drive, WIN_PACKETCMD,
1035 /* Issue the packet command */
1036 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
1037 return (*pkt_xfer_routine) (drive);
1041 static void idefloppy_rw_callback (ide_drive_t *drive)
1043 debug_log(KERN_INFO "ide-floppy: Reached idefloppy_rw_callback\n");
1045 idefloppy_do_end_request(drive, 1, 0);
1049 static void idefloppy_create_prevent_cmd (idefloppy_pc_t *pc, int prevent)
1051 debug_log(KERN_INFO "ide-floppy: creating prevent removal command, "
1052 "prevent = %d\n", prevent);
1054 idefloppy_init_pc(pc);
1055 pc->c[0] = IDEFLOPPY_PREVENT_REMOVAL_CMD;
1059 static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t *pc)
1061 idefloppy_init_pc(pc);
1062 pc->c[0] = IDEFLOPPY_READ_CAPACITY_CMD;
1065 pc->request_transfer = 255;
1068 static void idefloppy_create_format_unit_cmd (idefloppy_pc_t *pc, int b, int l,
1071 idefloppy_init_pc(pc);
1072 pc->c[0] = IDEFLOPPY_FORMAT_UNIT_CMD;
1075 memset(pc->buffer, 0, 12);
1076 pc->buffer[1] = 0xA2;
1077 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
1079 if (flags & 1) /* Verify bit on... */
1080 pc->buffer[1] ^= 0x20; /* ... turn off DCRT bit */
1083 put_unaligned(htonl(b), (unsigned int *)(&pc->buffer[4]));
1084 put_unaligned(htonl(l), (unsigned int *)(&pc->buffer[8]));
1086 set_bit(PC_WRITING, &pc->flags);
1090 * A mode sense command is used to "sense" floppy parameters.
1092 static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t *pc, u8 page_code, u8 type)
1094 u16 length = sizeof(idefloppy_mode_parameter_header_t);
1096 idefloppy_init_pc(pc);
1097 pc->c[0] = IDEFLOPPY_MODE_SENSE_CMD;
1099 pc->c[2] = page_code + (type << 6);
1101 switch (page_code) {
1102 case IDEFLOPPY_CAPABILITIES_PAGE:
1105 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
1109 printk(KERN_ERR "ide-floppy: unsupported page code "
1110 "in create_mode_sense_cmd\n");
1112 put_unaligned(htons(length), (u16 *) &pc->c[7]);
1113 pc->request_transfer = length;
1116 static void idefloppy_create_start_stop_cmd (idefloppy_pc_t *pc, int start)
1118 idefloppy_init_pc(pc);
1119 pc->c[0] = IDEFLOPPY_START_STOP_CMD;
1123 static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc)
1125 idefloppy_init_pc(pc);
1126 pc->c[0] = IDEFLOPPY_TEST_UNIT_READY_CMD;
1129 static void idefloppy_create_rw_cmd (idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq, unsigned long sector)
1131 int block = sector / floppy->bs_factor;
1132 int blocks = rq->nr_sectors / floppy->bs_factor;
1133 int cmd = rq_data_dir(rq);
1135 debug_log("create_rw1%d_cmd: block == %d, blocks == %d\n",
1136 2 * test_bit (IDEFLOPPY_USE_READ12, &floppy->flags),
1139 idefloppy_init_pc(pc);
1140 if (test_bit(IDEFLOPPY_USE_READ12, &floppy->flags)) {
1141 pc->c[0] = cmd == READ ? IDEFLOPPY_READ12_CMD : IDEFLOPPY_WRITE12_CMD;
1142 put_unaligned(htonl(blocks), (unsigned int *) &pc->c[6]);
1144 pc->c[0] = cmd == READ ? IDEFLOPPY_READ10_CMD : IDEFLOPPY_WRITE10_CMD;
1145 put_unaligned(htons(blocks), (unsigned short *) &pc->c[7]);
1147 put_unaligned(htonl(block), (unsigned int *) &pc->c[2]);
1148 pc->callback = &idefloppy_rw_callback;
1150 pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
1151 if (rq->cmd_flags & REQ_RW)
1152 set_bit(PC_WRITING, &pc->flags);
1154 pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
1155 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1159 idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq)
1161 idefloppy_init_pc(pc);
1162 pc->callback = &idefloppy_rw_callback;
1163 memcpy(pc->c, rq->cmd, sizeof(pc->c));
1165 pc->b_count = rq->data_len;
1166 if (rq->data_len && rq_data_dir(rq) == WRITE)
1167 set_bit(PC_WRITING, &pc->flags);
1168 pc->buffer = rq->data;
1170 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1173 * possibly problematic, doesn't look like ide-floppy correctly
1174 * handled scattered requests if dma fails...
1176 pc->request_transfer = pc->buffer_size = rq->data_len;
1180 * idefloppy_do_request is our request handling function.
1182 static ide_startstop_t idefloppy_do_request (ide_drive_t *drive, struct request *rq, sector_t block_s)
1184 idefloppy_floppy_t *floppy = drive->driver_data;
1186 unsigned long block = (unsigned long)block_s;
1188 debug_log(KERN_INFO "dev: %s, flags: %lx, errors: %d\n",
1189 rq->rq_disk ? rq->rq_disk->disk_name : "?",
1190 rq->flags, rq->errors);
1191 debug_log(KERN_INFO "sector: %ld, nr_sectors: %ld, "
1192 "current_nr_sectors: %d\n", (long)rq->sector,
1193 rq->nr_sectors, rq->current_nr_sectors);
1195 if (rq->errors >= ERROR_MAX) {
1196 if (floppy->failed_pc != NULL) {
1197 if (idefloppy_should_report_error(floppy))
1198 printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x,"
1199 " key = %2x, asc = %2x, ascq = %2x\n",
1200 drive->name, floppy->failed_pc->c[0],
1201 floppy->sense_key, floppy->asc, floppy->ascq);
1204 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
1206 idefloppy_do_end_request(drive, 0, 0);
1209 if (blk_fs_request(rq)) {
1210 if (((long)rq->sector % floppy->bs_factor) ||
1211 (rq->nr_sectors % floppy->bs_factor)) {
1212 printk("%s: unsupported r/w request size\n",
1214 idefloppy_do_end_request(drive, 0, 0);
1217 pc = idefloppy_next_pc_storage(drive);
1218 idefloppy_create_rw_cmd(floppy, pc, rq, block);
1219 } else if (blk_special_request(rq)) {
1220 pc = (idefloppy_pc_t *) rq->buffer;
1221 } else if (blk_pc_request(rq)) {
1222 pc = idefloppy_next_pc_storage(drive);
1223 idefloppy_blockpc_cmd(floppy, pc, rq);
1225 blk_dump_rq_flags(rq,
1226 "ide-floppy: unsupported command in queue");
1227 idefloppy_do_end_request(drive, 0, 0);
1232 return idefloppy_issue_pc(drive, pc);
1236 * idefloppy_queue_pc_tail adds a special packet command request to the
1237 * tail of the request queue, and waits for it to be serviced.
1239 static int idefloppy_queue_pc_tail (ide_drive_t *drive,idefloppy_pc_t *pc)
1241 struct ide_floppy_obj *floppy = drive->driver_data;
1244 ide_init_drive_cmd (&rq);
1245 rq.buffer = (char *) pc;
1246 rq.cmd_type = REQ_TYPE_SPECIAL;
1247 rq.rq_disk = floppy->disk;
1249 return ide_do_drive_cmd(drive, &rq, ide_wait);
1253 * Look at the flexible disk page parameters. We will ignore the CHS
1254 * capacity parameters and use the LBA parameters instead.
1256 static int idefloppy_get_flexible_disk_page (ide_drive_t *drive)
1258 idefloppy_floppy_t *floppy = drive->driver_data;
1260 idefloppy_mode_parameter_header_t *header;
1261 idefloppy_flexible_disk_page_t *page;
1262 int capacity, lba_capacity;
1264 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE, MODE_SENSE_CURRENT);
1265 if (idefloppy_queue_pc_tail(drive,&pc)) {
1266 printk(KERN_ERR "ide-floppy: Can't get flexible disk "
1267 "page parameters\n");
1270 header = (idefloppy_mode_parameter_header_t *) pc.buffer;
1271 floppy->wp = header->wp;
1272 set_disk_ro(floppy->disk, floppy->wp);
1273 page = (idefloppy_flexible_disk_page_t *) (header + 1);
1275 page->transfer_rate = ntohs(page->transfer_rate);
1276 page->sector_size = ntohs(page->sector_size);
1277 page->cyls = ntohs(page->cyls);
1278 page->rpm = ntohs(page->rpm);
1279 capacity = page->cyls * page->heads * page->sectors * page->sector_size;
1280 if (memcmp (page, &floppy->flexible_disk_page, sizeof (idefloppy_flexible_disk_page_t)))
1281 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
1282 "%d sector size, %d rpm\n",
1283 drive->name, capacity / 1024, page->cyls,
1284 page->heads, page->sectors,
1285 page->transfer_rate / 8, page->sector_size, page->rpm);
1287 floppy->flexible_disk_page = *page;
1288 drive->bios_cyl = page->cyls;
1289 drive->bios_head = page->heads;
1290 drive->bios_sect = page->sectors;
1291 lba_capacity = floppy->blocks * floppy->block_size;
1292 if (capacity < lba_capacity) {
1293 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
1294 "bytes, but the drive only handles %d\n",
1295 drive->name, lba_capacity, capacity);
1296 floppy->blocks = floppy->block_size ? capacity / floppy->block_size : 0;
1301 static int idefloppy_get_capability_page(ide_drive_t *drive)
1303 idefloppy_floppy_t *floppy = drive->driver_data;
1305 idefloppy_mode_parameter_header_t *header;
1306 idefloppy_capabilities_page_t *page;
1309 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
1310 MODE_SENSE_CURRENT);
1312 set_bit(PC_SUPPRESS_ERROR, &pc.flags);
1313 if (idefloppy_queue_pc_tail(drive,&pc)) {
1317 header = (idefloppy_mode_parameter_header_t *) pc.buffer;
1318 page= (idefloppy_capabilities_page_t *)(header+1);
1319 floppy->srfp = page->srfp;
1324 * Determine if a media is present in the floppy drive, and if so,
1327 static int idefloppy_get_capacity (ide_drive_t *drive)
1329 idefloppy_floppy_t *floppy = drive->driver_data;
1331 idefloppy_capacity_header_t *header;
1332 idefloppy_capacity_descriptor_t *descriptor;
1333 int i, descriptors, rc = 1, blocks, length;
1335 drive->bios_cyl = 0;
1336 drive->bios_head = drive->bios_sect = 0;
1338 floppy->bs_factor = 1;
1339 set_capacity(floppy->disk, 0);
1341 idefloppy_create_read_capacity_cmd(&pc);
1342 if (idefloppy_queue_pc_tail(drive, &pc)) {
1343 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1346 header = (idefloppy_capacity_header_t *) pc.buffer;
1347 descriptors = header->length / sizeof(idefloppy_capacity_descriptor_t);
1348 descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
1350 for (i = 0; i < descriptors; i++, descriptor++) {
1351 blocks = descriptor->blocks = ntohl(descriptor->blocks);
1352 length = descriptor->length = ntohs(descriptor->length);
1356 switch (descriptor->dc) {
1357 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1358 case CAPACITY_UNFORMATTED:
1359 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags))
1361 * If it is not a clik drive, break out
1362 * (maintains previous driver behaviour)
1365 case CAPACITY_CURRENT:
1366 /* Normal Zip/LS-120 disks */
1367 if (memcmp(descriptor, &floppy->capacity, sizeof (idefloppy_capacity_descriptor_t)))
1368 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
1369 "sector size\n", drive->name,
1370 blocks * length / 1024, blocks, length);
1371 floppy->capacity = *descriptor;
1372 if (!length || length % 512) {
1373 printk(KERN_NOTICE "%s: %d bytes block size "
1374 "not supported\n", drive->name, length);
1376 floppy->blocks = blocks;
1377 floppy->block_size = length;
1378 if ((floppy->bs_factor = length / 512) != 1)
1379 printk(KERN_NOTICE "%s: warning: non "
1380 "512 bytes block size not "
1381 "fully supported\n",
1386 case CAPACITY_NO_CARTRIDGE:
1388 * This is a KERN_ERR so it appears on screen
1389 * for the user to see
1391 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
1393 case CAPACITY_INVALID:
1394 printk(KERN_ERR "%s: Invalid capacity for disk "
1395 "in drive\n", drive->name);
1400 debug_log( "Descriptor 0 Code: %d\n",
1403 debug_log( "Descriptor %d: %dkB, %d blocks, %d "
1404 "sector size\n", i, blocks * length / 1024, blocks,
1408 /* Clik! disk does not support get_flexible_disk_page */
1409 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1410 (void) idefloppy_get_flexible_disk_page(drive);
1413 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
1418 ** Obtain the list of formattable capacities.
1419 ** Very similar to idefloppy_get_capacity, except that we push the capacity
1420 ** descriptors to userland, instead of our own structures.
1422 ** Userland gives us the following structure:
1424 ** struct idefloppy_format_capacities {
1432 ** userland initializes nformats to the number of allocated formats[]
1433 ** records. On exit we set nformats to the number of records we've
1434 ** actually initialized.
1438 static int idefloppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
1441 idefloppy_capacity_header_t *header;
1442 idefloppy_capacity_descriptor_t *descriptor;
1443 int i, descriptors, blocks, length;
1448 if (get_user(u_array_size, arg))
1451 if (u_array_size <= 0)
1454 idefloppy_create_read_capacity_cmd(&pc);
1455 if (idefloppy_queue_pc_tail(drive, &pc)) {
1456 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1459 header = (idefloppy_capacity_header_t *) pc.buffer;
1460 descriptors = header->length /
1461 sizeof(idefloppy_capacity_descriptor_t);
1462 descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
1468 ** We always skip the first capacity descriptor. That's the
1469 ** current capacity. We are interested in the remaining descriptors,
1470 ** the formattable capacities.
1473 for (i=0; i<descriptors; i++, descriptor++) {
1474 if (u_index >= u_array_size)
1475 break; /* User-supplied buffer too small */
1477 continue; /* Skip the first descriptor */
1479 blocks = ntohl(descriptor->blocks);
1480 length = ntohs(descriptor->length);
1482 if (put_user(blocks, argp))
1486 if (put_user(length, argp))
1493 if (put_user(u_index, arg))
1499 ** Send ATAPI_FORMAT_UNIT to the drive.
1501 ** Userland gives us the following structure:
1503 ** struct idefloppy_format_command {
1509 ** flags is a bitmask, currently, the only defined flag is:
1511 ** 0x01 - verify media after format.
1514 static int idefloppy_begin_format(ide_drive_t *drive, int __user *arg)
1521 if (get_user(blocks, arg) ||
1522 get_user(length, arg+1) ||
1523 get_user(flags, arg+2)) {
1527 /* Get the SFRP bit */
1528 (void) idefloppy_get_capability_page(drive);
1529 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1530 if (idefloppy_queue_pc_tail(drive, &pc)) {
1538 ** Get ATAPI_FORMAT_UNIT progress indication.
1540 ** Userland gives a pointer to an int. The int is set to a progress
1541 ** indicator 0-65536, with 65536=100%.
1543 ** If the drive does not support format progress indication, we just check
1544 ** the dsc bit, and return either 0 or 65536.
1547 static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
1549 idefloppy_floppy_t *floppy = drive->driver_data;
1551 int progress_indication = 0x10000;
1554 idefloppy_create_request_sense_cmd(&pc);
1555 if (idefloppy_queue_pc_tail(drive, &pc)) {
1559 if (floppy->sense_key == 2 &&
1561 floppy->ascq == 4) {
1562 progress_indication = floppy->progress_indication;
1564 /* Else assume format_unit has finished, and we're
1567 unsigned long flags;
1570 local_irq_save(flags);
1571 stat = drive->hwif->INB(IDE_STATUS_REG);
1572 local_irq_restore(flags);
1574 progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
1576 if (put_user(progress_indication, arg))
1583 * Return the current floppy capacity.
1585 static sector_t idefloppy_capacity (ide_drive_t *drive)
1587 idefloppy_floppy_t *floppy = drive->driver_data;
1588 unsigned long capacity = floppy->blocks * floppy->bs_factor;
1594 * idefloppy_identify_device checks if we can support a drive,
1595 * based on the ATAPI IDENTIFY command results.
1597 static int idefloppy_identify_device (ide_drive_t *drive,struct hd_driveid *id)
1599 struct idefloppy_id_gcw gcw;
1600 #if IDEFLOPPY_DEBUG_INFO
1602 #endif /* IDEFLOPPY_DEBUG_INFO */
1604 *((u16 *) &gcw) = id->config;
1607 /* kludge for Apple PowerBook internal zip */
1608 if ((gcw.device_type == 5) &&
1609 !strstr(id->model, "CD-ROM") &&
1610 strstr(id->model, "ZIP"))
1611 gcw.device_type = 0;
1614 #if IDEFLOPPY_DEBUG_INFO
1615 printk(KERN_INFO "Dumping ATAPI Identify Device floppy parameters\n");
1616 switch (gcw.protocol) {
1617 case 0: case 1: sprintf(buffer, "ATA");break;
1618 case 2: sprintf(buffer, "ATAPI");break;
1619 case 3: sprintf(buffer, "Reserved (Unknown to ide-floppy)");break;
1621 printk(KERN_INFO "Protocol Type: %s\n", buffer);
1622 switch (gcw.device_type) {
1623 case 0: sprintf(buffer, "Direct-access Device");break;
1624 case 1: sprintf(buffer, "Streaming Tape Device");break;
1625 case 2: case 3: case 4: sprintf (buffer, "Reserved");break;
1626 case 5: sprintf(buffer, "CD-ROM Device");break;
1627 case 6: sprintf(buffer, "Reserved");
1628 case 7: sprintf(buffer, "Optical memory Device");break;
1629 case 0x1f: sprintf(buffer, "Unknown or no Device type");break;
1630 default: sprintf(buffer, "Reserved");
1632 printk(KERN_INFO "Device Type: %x - %s\n", gcw.device_type, buffer);
1633 printk(KERN_INFO "Removable: %s\n",gcw.removable ? "Yes":"No");
1634 switch (gcw.drq_type) {
1635 case 0: sprintf(buffer, "Microprocessor DRQ");break;
1636 case 1: sprintf(buffer, "Interrupt DRQ");break;
1637 case 2: sprintf(buffer, "Accelerated DRQ");break;
1638 case 3: sprintf(buffer, "Reserved");break;
1640 printk(KERN_INFO "Command Packet DRQ Type: %s\n", buffer);
1641 switch (gcw.packet_size) {
1642 case 0: sprintf(buffer, "12 bytes");break;
1643 case 1: sprintf(buffer, "16 bytes");break;
1644 default: sprintf(buffer, "Reserved");break;
1646 printk(KERN_INFO "Command Packet Size: %s\n", buffer);
1647 #endif /* IDEFLOPPY_DEBUG_INFO */
1649 if (gcw.protocol != 2)
1650 printk(KERN_ERR "ide-floppy: Protocol is not ATAPI\n");
1651 else if (gcw.device_type != 0)
1652 printk(KERN_ERR "ide-floppy: Device type is not set to floppy\n");
1653 else if (!gcw.removable)
1654 printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
1655 else if (gcw.drq_type == 3) {
1656 printk(KERN_ERR "ide-floppy: Sorry, DRQ type %d not supported\n", gcw.drq_type);
1657 } else if (gcw.packet_size != 0) {
1658 printk(KERN_ERR "ide-floppy: Packet size is not 12 bytes long\n");
1664 #ifdef CONFIG_IDE_PROC_FS
1665 static void idefloppy_add_settings(ide_drive_t *drive)
1667 idefloppy_floppy_t *floppy = drive->driver_data;
1670 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
1672 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
1673 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
1674 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
1675 ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &floppy->ticks, NULL);
1678 static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
1682 * Driver initialization.
1684 static void idefloppy_setup (ide_drive_t *drive, idefloppy_floppy_t *floppy)
1686 struct idefloppy_id_gcw gcw;
1688 *((u16 *) &gcw) = drive->id->config;
1689 floppy->pc = floppy->pc_stack;
1690 if (gcw.drq_type == 1)
1691 set_bit(IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags);
1693 * We used to check revisions here. At this point however
1694 * I'm giving up. Just assume they are all broken, its easier.
1696 * The actual reason for the workarounds was likely
1697 * a driver bug after all rather than a firmware bug,
1698 * and the workaround below used to hide it. It should
1699 * be fixed as of version 1.9, but to be on the safe side
1700 * we'll leave the limitation below for the 2.2.x tree.
1703 if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
1704 set_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags);
1705 /* This value will be visible in the /proc/ide/hdx/settings */
1706 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1707 blk_queue_max_sectors(drive->queue, 64);
1711 * Guess what? The IOMEGA Clik! drive also needs the
1712 * above fix. It makes nasty clicking noises without
1713 * it, so please don't remove this.
1715 if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
1716 blk_queue_max_sectors(drive->queue, 64);
1717 set_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags);
1721 (void) idefloppy_get_capacity(drive);
1722 idefloppy_add_settings(drive);
1725 static void ide_floppy_remove(ide_drive_t *drive)
1727 idefloppy_floppy_t *floppy = drive->driver_data;
1728 struct gendisk *g = floppy->disk;
1730 ide_proc_unregister_driver(drive, floppy->driver);
1734 ide_floppy_put(floppy);
1737 static void ide_floppy_release(struct kref *kref)
1739 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1740 ide_drive_t *drive = floppy->drive;
1741 struct gendisk *g = floppy->disk;
1743 drive->driver_data = NULL;
1744 g->private_data = NULL;
1749 #ifdef CONFIG_IDE_PROC_FS
1750 static int proc_idefloppy_read_capacity
1751 (char *page, char **start, off_t off, int count, int *eof, void *data)
1753 ide_drive_t*drive = (ide_drive_t *)data;
1756 len = sprintf(page,"%llu\n", (long long)idefloppy_capacity(drive));
1757 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1760 static ide_proc_entry_t idefloppy_proc[] = {
1761 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
1762 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1763 { NULL, 0, NULL, NULL }
1765 #endif /* CONFIG_IDE_PROC_FS */
1767 static int ide_floppy_probe(ide_drive_t *);
1769 static ide_driver_t idefloppy_driver = {
1771 .owner = THIS_MODULE,
1772 .name = "ide-floppy",
1773 .bus = &ide_bus_type,
1775 .probe = ide_floppy_probe,
1776 .remove = ide_floppy_remove,
1777 .version = IDEFLOPPY_VERSION,
1778 .media = ide_floppy,
1779 .supports_dsc_overlap = 0,
1780 .do_request = idefloppy_do_request,
1781 .end_request = idefloppy_do_end_request,
1782 .error = __ide_error,
1783 .abort = __ide_abort,
1784 #ifdef CONFIG_IDE_PROC_FS
1785 .proc = idefloppy_proc,
1789 static int idefloppy_open(struct inode *inode, struct file *filp)
1791 struct gendisk *disk = inode->i_bdev->bd_disk;
1792 struct ide_floppy_obj *floppy;
1797 debug_log(KERN_INFO "Reached idefloppy_open\n");
1799 if (!(floppy = ide_floppy_get(disk)))
1802 drive = floppy->drive;
1806 if (floppy->openers == 1) {
1807 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1810 idefloppy_create_test_unit_ready_cmd(&pc);
1811 if (idefloppy_queue_pc_tail(drive, &pc)) {
1812 idefloppy_create_start_stop_cmd(&pc, 1);
1813 (void) idefloppy_queue_pc_tail(drive, &pc);
1816 if (idefloppy_get_capacity (drive)
1817 && (filp->f_flags & O_NDELAY) == 0
1819 ** Allow O_NDELAY to open a drive without a disk, or with
1820 ** an unreadable disk, so that we can get the format
1821 ** capacity of the drive or begin the format - Sam
1825 goto out_put_floppy;
1828 if (floppy->wp && (filp->f_mode & 2)) {
1830 goto out_put_floppy;
1832 set_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1833 /* IOMEGA Clik! drives do not support lock/unlock commands */
1834 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1835 idefloppy_create_prevent_cmd(&pc, 1);
1836 (void) idefloppy_queue_pc_tail(drive, &pc);
1838 check_disk_change(inode->i_bdev);
1839 } else if (test_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags)) {
1841 goto out_put_floppy;
1847 ide_floppy_put(floppy);
1851 static int idefloppy_release(struct inode *inode, struct file *filp)
1853 struct gendisk *disk = inode->i_bdev->bd_disk;
1854 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1855 ide_drive_t *drive = floppy->drive;
1858 debug_log(KERN_INFO "Reached idefloppy_release\n");
1860 if (floppy->openers == 1) {
1861 /* IOMEGA Clik! drives do not support lock/unlock commands */
1862 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1863 idefloppy_create_prevent_cmd(&pc, 0);
1864 (void) idefloppy_queue_pc_tail(drive, &pc);
1867 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1872 ide_floppy_put(floppy);
1877 static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1879 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1880 ide_drive_t *drive = floppy->drive;
1882 geo->heads = drive->bios_head;
1883 geo->sectors = drive->bios_sect;
1884 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1888 static int idefloppy_ioctl(struct inode *inode, struct file *file,
1889 unsigned int cmd, unsigned long arg)
1891 struct block_device *bdev = inode->i_bdev;
1892 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1893 ide_drive_t *drive = floppy->drive;
1894 void __user *argp = (void __user *)arg;
1896 int prevent = (arg) ? 1 : 0;
1903 case CDROM_LOCKDOOR:
1904 if (floppy->openers > 1)
1907 /* The IOMEGA Clik! Drive doesn't support this command - no room for an eject mechanism */
1908 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1909 idefloppy_create_prevent_cmd(&pc, prevent);
1910 (void) idefloppy_queue_pc_tail(drive, &pc);
1912 if (cmd == CDROMEJECT) {
1913 idefloppy_create_start_stop_cmd(&pc, 2);
1914 (void) idefloppy_queue_pc_tail(drive, &pc);
1917 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1919 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
1920 return idefloppy_get_format_capacities(drive, argp);
1921 case IDEFLOPPY_IOCTL_FORMAT_START:
1923 if (!(file->f_mode & 2))
1926 if (floppy->openers > 1) {
1927 /* Don't format if someone is using the disk */
1929 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS,
1934 set_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1936 err = idefloppy_begin_format(drive, argp);
1938 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1941 ** Note, the bit will be cleared when the device is
1942 ** closed. This is the cleanest way to handle the
1943 ** situation where the drive does not support
1944 ** format progress reporting.
1946 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1947 return idefloppy_get_format_progress(drive, argp);
1951 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1952 * and CDROM_SEND_PACKET (legacy) ioctls
1954 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1955 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1956 bdev->bd_disk, cmd, argp);
1961 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1966 static int idefloppy_media_changed(struct gendisk *disk)
1968 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1969 ide_drive_t *drive = floppy->drive;
1971 /* do not scan partitions twice if this is a removable device */
1972 if (drive->attach) {
1976 return test_and_clear_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1979 static int idefloppy_revalidate_disk(struct gendisk *disk)
1981 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1982 set_capacity(disk, idefloppy_capacity(floppy->drive));
1986 static struct block_device_operations idefloppy_ops = {
1987 .owner = THIS_MODULE,
1988 .open = idefloppy_open,
1989 .release = idefloppy_release,
1990 .ioctl = idefloppy_ioctl,
1991 .getgeo = idefloppy_getgeo,
1992 .media_changed = idefloppy_media_changed,
1993 .revalidate_disk= idefloppy_revalidate_disk
1996 static int ide_floppy_probe(ide_drive_t *drive)
1998 idefloppy_floppy_t *floppy;
2001 if (!strstr("ide-floppy", drive->driver_req))
2003 if (!drive->present)
2005 if (drive->media != ide_floppy)
2007 if (!idefloppy_identify_device (drive, drive->id)) {
2008 printk (KERN_ERR "ide-floppy: %s: not supported by this version of ide-floppy\n", drive->name);
2012 printk("ide-floppy: passing drive %s to ide-scsi emulation.\n", drive->name);
2015 if ((floppy = kzalloc(sizeof (idefloppy_floppy_t), GFP_KERNEL)) == NULL) {
2016 printk (KERN_ERR "ide-floppy: %s: Can't allocate a floppy structure\n", drive->name);
2020 g = alloc_disk(1 << PARTN_BITS);
2022 goto out_free_floppy;
2024 ide_init_disk(g, drive);
2026 ide_proc_register_driver(drive, &idefloppy_driver);
2028 kref_init(&floppy->kref);
2030 floppy->drive = drive;
2031 floppy->driver = &idefloppy_driver;
2034 g->private_data = &floppy->driver;
2036 drive->driver_data = floppy;
2038 idefloppy_setup (drive, floppy);
2040 g->minors = 1 << PARTN_BITS;
2041 g->driverfs_dev = &drive->gendev;
2042 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
2043 g->fops = &idefloppy_ops;
2054 MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
2056 static void __exit idefloppy_exit (void)
2058 driver_unregister(&idefloppy_driver.gen_driver);
2061 static int __init idefloppy_init(void)
2063 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
2064 return driver_register(&idefloppy_driver.gen_driver);
2067 MODULE_ALIAS("ide:*m-floppy*");
2068 module_init(idefloppy_init);
2069 module_exit(idefloppy_exit);
2070 MODULE_LICENSE("GPL");