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 */
122 /* format capacities descriptor codes */
123 #define CAPACITY_INVALID 0x00
124 #define CAPACITY_UNFORMATTED 0x01
125 #define CAPACITY_CURRENT 0x02
126 #define CAPACITY_NO_CARTRIDGE 0x03
129 * Most of our global data which we need to save even as we leave the
130 * driver due to an interrupt or a timer event is stored in a variable
131 * of type idefloppy_floppy_t, defined below.
133 typedef struct ide_floppy_obj
{
135 ide_driver_t
*driver
;
136 struct gendisk
*disk
;
138 unsigned int openers
; /* protected by BKL for now */
140 /* Current packet command */
142 /* Last failed packet command */
143 idefloppy_pc_t
*failed_pc
;
144 /* Packet command stack */
145 idefloppy_pc_t pc_stack
[IDEFLOPPY_PC_STACK
];
146 /* Next free packet command storage space */
148 struct request rq_stack
[IDEFLOPPY_PC_STACK
];
149 /* We implement a circular array */
153 * Last error information
155 u8 sense_key
, asc
, ascq
;
156 /* delay this long before sending packet command */
158 int progress_indication
;
164 int blocks
, block_size
, bs_factor
;
165 /* Last format capacity descriptor */
167 /* Copy of the flexible disk page */
168 u8 flexible_disk_page
[32];
171 /* Supports format progress report */
173 /* Status/Action flags */
175 } idefloppy_floppy_t
;
177 #define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
180 * Floppy flag bits values.
182 #define IDEFLOPPY_DRQ_INTERRUPT 0 /* DRQ interrupt device */
183 #define IDEFLOPPY_MEDIA_CHANGED 1 /* Media may have changed */
184 #define IDEFLOPPY_USE_READ12 2 /* Use READ12/WRITE12 or READ10/WRITE10 */
185 #define IDEFLOPPY_FORMAT_IN_PROGRESS 3 /* Format in progress */
186 #define IDEFLOPPY_CLIK_DRIVE 4 /* Avoid commands not supported in Clik drive */
187 #define IDEFLOPPY_ZIP_DRIVE 5 /* Requires BH algorithm for packets */
190 * Defines for the mode sense command
192 #define MODE_SENSE_CURRENT 0x00
193 #define MODE_SENSE_CHANGEABLE 0x01
194 #define MODE_SENSE_DEFAULT 0x02
195 #define MODE_SENSE_SAVED 0x03
198 * IOCTLs used in low-level formatting.
201 #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
202 #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
203 #define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
204 #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
207 * Error codes which are returned in rq->errors to the higher part
210 #define IDEFLOPPY_ERROR_GENERAL 101
213 * The following is used to format the general configuration word of
214 * the ATAPI IDENTIFY DEVICE command.
216 struct idefloppy_id_gcw
{
217 #if defined(__LITTLE_ENDIAN_BITFIELD)
218 unsigned packet_size
:2; /* Packet Size */
219 unsigned reserved234
:3; /* Reserved */
220 unsigned drq_type
:2; /* Command packet DRQ type */
221 unsigned removable
:1; /* Removable media */
222 unsigned device_type
:5; /* Device type */
223 unsigned reserved13
:1; /* Reserved */
224 unsigned protocol
:2; /* Protocol type */
225 #elif defined(__BIG_ENDIAN_BITFIELD)
226 unsigned protocol
:2; /* Protocol type */
227 unsigned reserved13
:1; /* Reserved */
228 unsigned device_type
:5; /* Device type */
229 unsigned removable
:1; /* Removable media */
230 unsigned drq_type
:2; /* Command packet DRQ type */
231 unsigned reserved234
:3; /* Reserved */
232 unsigned packet_size
:2; /* Packet Size */
234 #error "Bitfield endianness not defined! Check your byteorder.h"
239 * Pages of the SELECT SENSE / MODE SENSE packet commands.
240 * See SFF-8070i spec.
242 #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
243 #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
245 static DEFINE_MUTEX(idefloppy_ref_mutex
);
247 #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
249 #define ide_floppy_g(disk) \
250 container_of((disk)->private_data, struct ide_floppy_obj, driver)
252 static struct ide_floppy_obj
*ide_floppy_get(struct gendisk
*disk
)
254 struct ide_floppy_obj
*floppy
= NULL
;
256 mutex_lock(&idefloppy_ref_mutex
);
257 floppy
= ide_floppy_g(disk
);
259 kref_get(&floppy
->kref
);
260 mutex_unlock(&idefloppy_ref_mutex
);
264 static void idefloppy_cleanup_obj(struct kref
*);
266 static void ide_floppy_put(struct ide_floppy_obj
*floppy
)
268 mutex_lock(&idefloppy_ref_mutex
);
269 kref_put(&floppy
->kref
, idefloppy_cleanup_obj
);
270 mutex_unlock(&idefloppy_ref_mutex
);
274 * Too bad. The drive wants to send us data which we are not ready to accept.
275 * Just throw it away.
277 static void idefloppy_discard_data (ide_drive_t
*drive
, unsigned int bcount
)
280 (void) HWIF(drive
)->INB(IDE_DATA_REG
);
283 #if IDEFLOPPY_DEBUG_BUGS
284 static void idefloppy_write_zeros (ide_drive_t
*drive
, unsigned int bcount
)
287 HWIF(drive
)->OUTB(0, IDE_DATA_REG
);
289 #endif /* IDEFLOPPY_DEBUG_BUGS */
293 * idefloppy_do_end_request is used to finish servicing a request.
295 * For read/write requests, we will call ide_end_request to pass to the
298 static int idefloppy_do_end_request(ide_drive_t
*drive
, int uptodate
, int nsecs
)
300 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
301 struct request
*rq
= HWGROUP(drive
)->rq
;
304 debug_log("Reached %s\n", __func__
);
307 case 0: error
= IDEFLOPPY_ERROR_GENERAL
; break;
308 case 1: error
= 0; break;
309 default: error
= uptodate
;
312 floppy
->failed_pc
= NULL
;
313 /* Why does this happen? */
316 if (!blk_special_request(rq
)) {
317 /* our real local end request function */
318 ide_end_request(drive
, uptodate
, nsecs
);
322 /* fixme: need to move this local also */
323 ide_end_drive_cmd(drive
, 0, 0);
327 static void idefloppy_input_buffers (ide_drive_t
*drive
, idefloppy_pc_t
*pc
, unsigned int bcount
)
329 struct request
*rq
= pc
->rq
;
330 struct bio_vec
*bvec
;
331 struct req_iterator iter
;
336 rq_for_each_segment(bvec
, rq
, iter
) {
340 count
= min(bvec
->bv_len
, bcount
);
342 data
= bvec_kmap_irq(bvec
, &flags
);
343 drive
->hwif
->atapi_input_bytes(drive
, data
, count
);
344 bvec_kunmap_irq(data
, &flags
);
347 pc
->b_count
+= count
;
351 idefloppy_do_end_request(drive
, 1, done
>> 9);
354 printk(KERN_ERR
"%s: leftover data in idefloppy_input_buffers, bcount == %d\n", drive
->name
, bcount
);
355 idefloppy_discard_data(drive
, bcount
);
359 static void idefloppy_output_buffers (ide_drive_t
*drive
, idefloppy_pc_t
*pc
, unsigned int bcount
)
361 struct request
*rq
= pc
->rq
;
362 struct req_iterator iter
;
363 struct bio_vec
*bvec
;
368 rq_for_each_segment(bvec
, rq
, iter
) {
372 count
= min(bvec
->bv_len
, bcount
);
374 data
= bvec_kmap_irq(bvec
, &flags
);
375 drive
->hwif
->atapi_output_bytes(drive
, data
, count
);
376 bvec_kunmap_irq(data
, &flags
);
379 pc
->b_count
+= count
;
383 idefloppy_do_end_request(drive
, 1, done
>> 9);
385 #if IDEFLOPPY_DEBUG_BUGS
387 printk(KERN_ERR
"%s: leftover data in idefloppy_output_buffers, bcount == %d\n", drive
->name
, bcount
);
388 idefloppy_write_zeros(drive
, bcount
);
393 static void idefloppy_update_buffers (ide_drive_t
*drive
, idefloppy_pc_t
*pc
)
395 struct request
*rq
= pc
->rq
;
396 struct bio
*bio
= rq
->bio
;
398 while ((bio
= rq
->bio
) != NULL
)
399 idefloppy_do_end_request(drive
, 1, 0);
403 * idefloppy_queue_pc_head generates a new packet command request in front
404 * of the request queue, before the current request, so that it will be
405 * processed immediately, on the next pass through the driver.
407 static void idefloppy_queue_pc_head (ide_drive_t
*drive
,idefloppy_pc_t
*pc
,struct request
*rq
)
409 struct ide_floppy_obj
*floppy
= drive
->driver_data
;
411 ide_init_drive_cmd(rq
);
412 rq
->buffer
= (char *) pc
;
413 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
414 rq
->rq_disk
= floppy
->disk
;
415 (void) ide_do_drive_cmd(drive
, rq
, ide_preempt
);
418 static idefloppy_pc_t
*idefloppy_next_pc_storage (ide_drive_t
*drive
)
420 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
422 if (floppy
->pc_stack_index
== IDEFLOPPY_PC_STACK
)
423 floppy
->pc_stack_index
=0;
424 return (&floppy
->pc_stack
[floppy
->pc_stack_index
++]);
427 static struct request
*idefloppy_next_rq_storage (ide_drive_t
*drive
)
429 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
431 if (floppy
->rq_stack_index
== IDEFLOPPY_PC_STACK
)
432 floppy
->rq_stack_index
= 0;
433 return (&floppy
->rq_stack
[floppy
->rq_stack_index
++]);
436 static void idefloppy_request_sense_callback(ide_drive_t
*drive
)
438 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
439 u8
*buf
= floppy
->pc
->buffer
;
441 debug_log("Reached %s\n", __func__
);
443 if (!floppy
->pc
->error
) {
444 floppy
->sense_key
= buf
[2] & 0x0F;
445 floppy
->asc
= buf
[12];
446 floppy
->ascq
= buf
[13];
447 floppy
->progress_indication
= buf
[15] & 0x80 ?
448 (u16
)get_unaligned((u16
*)&buf
[16]) : 0x10000;
450 if (floppy
->failed_pc
)
451 debug_log("pc = %x, sense key = %x, asc = %x,"
453 floppy
->failed_pc
->c
[0],
458 debug_log("sense key = %x, asc = %x, ascq = %x\n",
464 idefloppy_do_end_request(drive
, 1, 0);
466 printk(KERN_ERR
"Error in REQUEST SENSE itself - Aborting"
468 idefloppy_do_end_request(drive
, 0, 0);
473 * General packet command callback function.
475 static void idefloppy_pc_callback (ide_drive_t
*drive
)
477 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
479 debug_log("Reached %s\n", __func__
);
481 idefloppy_do_end_request(drive
, floppy
->pc
->error
? 0 : 1, 0);
485 * idefloppy_init_pc initializes a packet command.
487 static void idefloppy_init_pc (idefloppy_pc_t
*pc
)
489 memset(pc
->c
, 0, 12);
492 pc
->request_transfer
= 0;
493 pc
->buffer
= pc
->pc_buffer
;
494 pc
->buffer_size
= IDEFLOPPY_PC_BUFFER_SIZE
;
495 pc
->callback
= &idefloppy_pc_callback
;
498 static void idefloppy_create_request_sense_cmd (idefloppy_pc_t
*pc
)
500 idefloppy_init_pc(pc
);
501 pc
->c
[0] = GPCMD_REQUEST_SENSE
;
503 pc
->request_transfer
= 18;
504 pc
->callback
= &idefloppy_request_sense_callback
;
508 * idefloppy_retry_pc is called when an error was detected during the
509 * last packet command. We queue a request sense packet command in
510 * the head of the request list.
512 static void idefloppy_retry_pc (ide_drive_t
*drive
)
517 (void)drive
->hwif
->INB(IDE_ERROR_REG
);
518 pc
= idefloppy_next_pc_storage(drive
);
519 rq
= idefloppy_next_rq_storage(drive
);
520 idefloppy_create_request_sense_cmd(pc
);
521 idefloppy_queue_pc_head(drive
, pc
, rq
);
525 * idefloppy_pc_intr is the usual interrupt handler which will be called
526 * during a packet command.
528 static ide_startstop_t
idefloppy_pc_intr (ide_drive_t
*drive
)
530 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
531 ide_hwif_t
*hwif
= drive
->hwif
;
532 idefloppy_pc_t
*pc
= floppy
->pc
;
533 struct request
*rq
= pc
->rq
;
538 debug_log("Reached %s interrupt handler\n", __func__
);
540 if (test_bit(PC_DMA_IN_PROGRESS
, &pc
->flags
)) {
541 if (HWIF(drive
)->ide_dma_end(drive
)) {
542 set_bit(PC_DMA_ERROR
, &pc
->flags
);
544 pc
->actually_transferred
= pc
->request_transfer
;
545 idefloppy_update_buffers(drive
, pc
);
547 debug_log("DMA finished\n");
550 /* Clear the interrupt */
551 stat
= drive
->hwif
->INB(IDE_STATUS_REG
);
553 if ((stat
& DRQ_STAT
) == 0) { /* No more interrupts */
554 debug_log("Packet command completed, %d bytes transferred\n",
555 pc
->actually_transferred
);
556 clear_bit(PC_DMA_IN_PROGRESS
, &pc
->flags
);
558 local_irq_enable_in_hardirq();
560 if ((stat
& ERR_STAT
) || test_bit(PC_DMA_ERROR
, &pc
->flags
)) {
562 debug_log("%s: I/O error\n", drive
->name
);
564 if (pc
->c
[0] == GPCMD_REQUEST_SENSE
) {
565 printk(KERN_ERR
"ide-floppy: I/O error in "
566 "request sense command\n");
567 return ide_do_reset(drive
);
569 /* Retry operation */
570 idefloppy_retry_pc(drive
);
571 /* queued, but not started */
575 if (floppy
->failed_pc
== pc
)
576 floppy
->failed_pc
= NULL
;
577 /* Command finished - Call the callback function */
582 if (test_and_clear_bit(PC_DMA_IN_PROGRESS
, &pc
->flags
)) {
583 printk(KERN_ERR
"ide-floppy: The floppy wants to issue "
584 "more interrupts in DMA mode\n");
586 return ide_do_reset(drive
);
589 /* Get the number of bytes to transfer */
590 bcount
= (hwif
->INB(IDE_BCOUNTH_REG
) << 8) |
591 hwif
->INB(IDE_BCOUNTL_REG
);
592 /* on this interrupt */
593 ireason
= hwif
->INB(IDE_IREASON_REG
);
596 printk(KERN_ERR
"ide-floppy: CoD != 0 in idefloppy_pc_intr\n");
597 return ide_do_reset(drive
);
599 if (((ireason
& IO
) == IO
) == test_bit(PC_WRITING
, &pc
->flags
)) {
600 /* Hopefully, we will never get here */
601 printk(KERN_ERR
"ide-floppy: We wanted to %s, ",
602 (ireason
& IO
) ? "Write" : "Read");
603 printk(KERN_ERR
"but the floppy wants us to %s !\n",
604 (ireason
& IO
) ? "Read" : "Write");
605 return ide_do_reset(drive
);
607 if (!test_bit(PC_WRITING
, &pc
->flags
)) {
608 /* Reading - Check that we have enough space */
609 temp
= pc
->actually_transferred
+ bcount
;
610 if (temp
> pc
->request_transfer
) {
611 if (temp
> pc
->buffer_size
) {
612 printk(KERN_ERR
"ide-floppy: The floppy wants "
613 "to send us more data than expected "
614 "- discarding data\n");
615 idefloppy_discard_data(drive
, bcount
);
617 ide_set_handler(drive
,
623 debug_log("The floppy wants to send us more data than"
624 " expected - allowing transfer\n");
627 if (test_bit(PC_WRITING
, &pc
->flags
)) {
628 if (pc
->buffer
!= NULL
)
629 /* Write the current buffer */
630 hwif
->atapi_output_bytes(drive
, pc
->current_position
,
633 idefloppy_output_buffers(drive
, pc
, bcount
);
635 if (pc
->buffer
!= NULL
)
636 /* Read the current buffer */
637 hwif
->atapi_input_bytes(drive
, pc
->current_position
,
640 idefloppy_input_buffers(drive
, pc
, bcount
);
642 /* Update the current position */
643 pc
->actually_transferred
+= bcount
;
644 pc
->current_position
+= bcount
;
646 ide_set_handler(drive
, &idefloppy_pc_intr
, IDEFLOPPY_WAIT_CMD
, NULL
); /* And set the interrupt handler again */
651 * This is the original routine that did the packet transfer.
652 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
653 * for that drive below. The algorithm is chosen based on drive type
655 static ide_startstop_t
idefloppy_transfer_pc (ide_drive_t
*drive
)
657 ide_startstop_t startstop
;
658 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
661 if (ide_wait_stat(&startstop
, drive
, DRQ_STAT
, BUSY_STAT
, WAIT_READY
)) {
662 printk(KERN_ERR
"ide-floppy: Strange, packet command "
663 "initiated yet DRQ isn't asserted\n");
666 ireason
= drive
->hwif
->INB(IDE_IREASON_REG
);
667 if ((ireason
& CD
) == 0 || (ireason
& IO
)) {
668 printk(KERN_ERR
"ide-floppy: (IO,CoD) != (0,1) while "
669 "issuing a packet command\n");
670 return ide_do_reset(drive
);
673 /* Set the interrupt routine */
674 ide_set_handler(drive
, &idefloppy_pc_intr
, IDEFLOPPY_WAIT_CMD
, NULL
);
675 /* Send the actual packet */
676 HWIF(drive
)->atapi_output_bytes(drive
, floppy
->pc
->c
, 12);
682 * What we have here is a classic case of a top half / bottom half
683 * interrupt service routine. In interrupt mode, the device sends
684 * an interrupt to signal it's ready to receive a packet. However,
685 * we need to delay about 2-3 ticks before issuing the packet or we
688 * So, follow carefully. transfer_pc1 is called as an interrupt (or
689 * directly). In either case, when the device says it's ready for a
690 * packet, we schedule the packet transfer to occur about 2-3 ticks
691 * later in transfer_pc2.
693 static int idefloppy_transfer_pc2 (ide_drive_t
*drive
)
695 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
697 /* Send the actual packet */
698 HWIF(drive
)->atapi_output_bytes(drive
, floppy
->pc
->c
, 12);
699 /* Timeout for the packet command */
700 return IDEFLOPPY_WAIT_CMD
;
703 static ide_startstop_t
idefloppy_transfer_pc1 (ide_drive_t
*drive
)
705 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
706 ide_startstop_t startstop
;
709 if (ide_wait_stat(&startstop
, drive
, DRQ_STAT
, BUSY_STAT
, WAIT_READY
)) {
710 printk(KERN_ERR
"ide-floppy: Strange, packet command "
711 "initiated yet DRQ isn't asserted\n");
714 ireason
= drive
->hwif
->INB(IDE_IREASON_REG
);
715 if ((ireason
& CD
) == 0 || (ireason
& IO
)) {
716 printk(KERN_ERR
"ide-floppy: (IO,CoD) != (0,1) "
717 "while issuing a packet command\n");
718 return ide_do_reset(drive
);
721 * The following delay solves a problem with ATAPI Zip 100 drives
722 * where the Busy flag was apparently being deasserted before the
723 * unit was ready to receive data. This was happening on a
724 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
725 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
726 * used until after the packet is moved in about 50 msec.
729 ide_set_handler(drive
,
730 &idefloppy_pc_intr
, /* service routine for packet command */
731 floppy
->ticks
, /* wait this long before "failing" */
732 &idefloppy_transfer_pc2
); /* fail == transfer_pc2 */
737 * idefloppy_should_report_error()
739 * Supresses error messages resulting from Medium not present
741 static inline int idefloppy_should_report_error(idefloppy_floppy_t
*floppy
)
743 if (floppy
->sense_key
== 0x02 &&
744 floppy
->asc
== 0x3a &&
745 floppy
->ascq
== 0x00)
751 * Issue a packet command
753 static ide_startstop_t
idefloppy_issue_pc (ide_drive_t
*drive
, idefloppy_pc_t
*pc
)
755 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
756 ide_hwif_t
*hwif
= drive
->hwif
;
757 ide_handler_t
*pkt_xfer_routine
;
761 if (floppy
->failed_pc
== NULL
&&
762 pc
->c
[0] != GPCMD_REQUEST_SENSE
)
763 floppy
->failed_pc
= pc
;
764 /* Set the current packet command */
767 if (pc
->retries
> IDEFLOPPY_MAX_PC_RETRIES
||
768 test_bit(PC_ABORT
, &pc
->flags
)) {
770 * We will "abort" retrying a packet command in case
771 * a legitimate error code was received.
773 if (!test_bit(PC_ABORT
, &pc
->flags
)) {
774 if (!test_bit(PC_SUPPRESS_ERROR
, &pc
->flags
)) {
775 if (idefloppy_should_report_error(floppy
))
776 printk(KERN_ERR
"ide-floppy: %s: I/O error, "
777 "pc = %2x, key = %2x, "
778 "asc = %2x, ascq = %2x\n",
779 drive
->name
, pc
->c
[0],
781 floppy
->asc
, floppy
->ascq
);
784 pc
->error
= IDEFLOPPY_ERROR_GENERAL
;
786 floppy
->failed_pc
= NULL
;
791 debug_log("Retry number - %d\n", pc
->retries
);
794 /* We haven't transferred any data yet */
795 pc
->actually_transferred
= 0;
796 pc
->current_position
= pc
->buffer
;
797 bcount
= min(pc
->request_transfer
, 63 * 1024);
799 if (test_and_clear_bit(PC_DMA_ERROR
, &pc
->flags
))
804 if (test_bit(PC_DMA_RECOMMENDED
, &pc
->flags
) && drive
->using_dma
)
805 dma
= !hwif
->dma_setup(drive
);
807 ide_pktcmd_tf_load(drive
, IDE_TFLAG_NO_SELECT_MASK
|
808 IDE_TFLAG_OUT_DEVICE
, bcount
, dma
);
810 if (dma
) { /* Begin DMA, if necessary */
811 set_bit(PC_DMA_IN_PROGRESS
, &pc
->flags
);
812 hwif
->dma_start(drive
);
815 /* Can we transfer the packet when we get the interrupt or wait? */
816 if (test_bit(IDEFLOPPY_ZIP_DRIVE
, &floppy
->flags
)) {
818 pkt_xfer_routine
= &idefloppy_transfer_pc1
;
821 pkt_xfer_routine
= &idefloppy_transfer_pc
;
824 if (test_bit (IDEFLOPPY_DRQ_INTERRUPT
, &floppy
->flags
)) {
825 /* Issue the packet command */
826 ide_execute_command(drive
, WIN_PACKETCMD
,
832 /* Issue the packet command */
833 HWIF(drive
)->OUTB(WIN_PACKETCMD
, IDE_COMMAND_REG
);
834 return (*pkt_xfer_routine
) (drive
);
838 static void idefloppy_rw_callback (ide_drive_t
*drive
)
840 debug_log("Reached %s\n", __func__
);
842 idefloppy_do_end_request(drive
, 1, 0);
846 static void idefloppy_create_prevent_cmd (idefloppy_pc_t
*pc
, int prevent
)
848 debug_log("creating prevent removal command, prevent = %d\n", prevent
);
850 idefloppy_init_pc(pc
);
851 pc
->c
[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL
;
855 static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t
*pc
)
857 idefloppy_init_pc(pc
);
858 pc
->c
[0] = GPCMD_READ_FORMAT_CAPACITIES
;
861 pc
->request_transfer
= 255;
864 static void idefloppy_create_format_unit_cmd (idefloppy_pc_t
*pc
, int b
, int l
,
867 idefloppy_init_pc(pc
);
868 pc
->c
[0] = GPCMD_FORMAT_UNIT
;
871 memset(pc
->buffer
, 0, 12);
872 pc
->buffer
[1] = 0xA2;
873 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
875 if (flags
& 1) /* Verify bit on... */
876 pc
->buffer
[1] ^= 0x20; /* ... turn off DCRT bit */
879 put_unaligned(cpu_to_be32(b
), (unsigned int *)(&pc
->buffer
[4]));
880 put_unaligned(cpu_to_be32(l
), (unsigned int *)(&pc
->buffer
[8]));
882 set_bit(PC_WRITING
, &pc
->flags
);
886 * A mode sense command is used to "sense" floppy parameters.
888 static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t
*pc
, u8 page_code
, u8 type
)
890 u16 length
= 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
892 idefloppy_init_pc(pc
);
893 pc
->c
[0] = GPCMD_MODE_SENSE_10
;
895 pc
->c
[2] = page_code
+ (type
<< 6);
898 case IDEFLOPPY_CAPABILITIES_PAGE
:
901 case IDEFLOPPY_FLEXIBLE_DISK_PAGE
:
905 printk(KERN_ERR
"ide-floppy: unsupported page code "
906 "in create_mode_sense_cmd\n");
908 put_unaligned(cpu_to_be16(length
), (u16
*) &pc
->c
[7]);
909 pc
->request_transfer
= length
;
912 static void idefloppy_create_start_stop_cmd (idefloppy_pc_t
*pc
, int start
)
914 idefloppy_init_pc(pc
);
915 pc
->c
[0] = GPCMD_START_STOP_UNIT
;
919 static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t
*pc
)
921 idefloppy_init_pc(pc
);
922 pc
->c
[0] = GPCMD_TEST_UNIT_READY
;
925 static void idefloppy_create_rw_cmd (idefloppy_floppy_t
*floppy
, idefloppy_pc_t
*pc
, struct request
*rq
, unsigned long sector
)
927 int block
= sector
/ floppy
->bs_factor
;
928 int blocks
= rq
->nr_sectors
/ floppy
->bs_factor
;
929 int cmd
= rq_data_dir(rq
);
931 debug_log("create_rw1%d_cmd: block == %d, blocks == %d\n",
932 2 * test_bit (IDEFLOPPY_USE_READ12
, &floppy
->flags
),
935 idefloppy_init_pc(pc
);
936 if (test_bit(IDEFLOPPY_USE_READ12
, &floppy
->flags
)) {
937 pc
->c
[0] = cmd
== READ
? GPCMD_READ_12
: GPCMD_WRITE_12
;
938 put_unaligned(cpu_to_be32(blocks
), (unsigned int *) &pc
->c
[6]);
940 pc
->c
[0] = cmd
== READ
? GPCMD_READ_10
: GPCMD_WRITE_10
;
941 put_unaligned(cpu_to_be16(blocks
), (unsigned short *)&pc
->c
[7]);
943 put_unaligned(cpu_to_be32(block
), (unsigned int *) &pc
->c
[2]);
944 pc
->callback
= &idefloppy_rw_callback
;
946 pc
->b_count
= cmd
== READ
? 0 : rq
->bio
->bi_size
;
947 if (rq
->cmd_flags
& REQ_RW
)
948 set_bit(PC_WRITING
, &pc
->flags
);
950 pc
->request_transfer
= pc
->buffer_size
= blocks
* floppy
->block_size
;
951 set_bit(PC_DMA_RECOMMENDED
, &pc
->flags
);
955 idefloppy_blockpc_cmd(idefloppy_floppy_t
*floppy
, idefloppy_pc_t
*pc
, struct request
*rq
)
957 idefloppy_init_pc(pc
);
958 pc
->callback
= &idefloppy_rw_callback
;
959 memcpy(pc
->c
, rq
->cmd
, sizeof(pc
->c
));
961 pc
->b_count
= rq
->data_len
;
962 if (rq
->data_len
&& rq_data_dir(rq
) == WRITE
)
963 set_bit(PC_WRITING
, &pc
->flags
);
964 pc
->buffer
= rq
->data
;
966 set_bit(PC_DMA_RECOMMENDED
, &pc
->flags
);
969 * possibly problematic, doesn't look like ide-floppy correctly
970 * handled scattered requests if dma fails...
972 pc
->request_transfer
= pc
->buffer_size
= rq
->data_len
;
976 * idefloppy_do_request is our request handling function.
978 static ide_startstop_t
idefloppy_do_request (ide_drive_t
*drive
, struct request
*rq
, sector_t block_s
)
980 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
982 unsigned long block
= (unsigned long)block_s
;
984 debug_log("dev: %s, cmd_type: %x, errors: %d\n",
985 rq
->rq_disk
? rq
->rq_disk
->disk_name
: "?",
986 rq
->cmd_type
, rq
->errors
);
987 debug_log("sector: %ld, nr_sectors: %ld, "
988 "current_nr_sectors: %d\n", (long)rq
->sector
,
989 rq
->nr_sectors
, rq
->current_nr_sectors
);
991 if (rq
->errors
>= ERROR_MAX
) {
992 if (floppy
->failed_pc
!= NULL
) {
993 if (idefloppy_should_report_error(floppy
))
994 printk(KERN_ERR
"ide-floppy: %s: I/O error, pc = %2x,"
995 " key = %2x, asc = %2x, ascq = %2x\n",
996 drive
->name
, floppy
->failed_pc
->c
[0],
997 floppy
->sense_key
, floppy
->asc
, floppy
->ascq
);
1000 printk(KERN_ERR
"ide-floppy: %s: I/O error\n",
1002 idefloppy_do_end_request(drive
, 0, 0);
1005 if (blk_fs_request(rq
)) {
1006 if (((long)rq
->sector
% floppy
->bs_factor
) ||
1007 (rq
->nr_sectors
% floppy
->bs_factor
)) {
1008 printk("%s: unsupported r/w request size\n",
1010 idefloppy_do_end_request(drive
, 0, 0);
1013 pc
= idefloppy_next_pc_storage(drive
);
1014 idefloppy_create_rw_cmd(floppy
, pc
, rq
, block
);
1015 } else if (blk_special_request(rq
)) {
1016 pc
= (idefloppy_pc_t
*) rq
->buffer
;
1017 } else if (blk_pc_request(rq
)) {
1018 pc
= idefloppy_next_pc_storage(drive
);
1019 idefloppy_blockpc_cmd(floppy
, pc
, rq
);
1021 blk_dump_rq_flags(rq
,
1022 "ide-floppy: unsupported command in queue");
1023 idefloppy_do_end_request(drive
, 0, 0);
1028 return idefloppy_issue_pc(drive
, pc
);
1032 * idefloppy_queue_pc_tail adds a special packet command request to the
1033 * tail of the request queue, and waits for it to be serviced.
1035 static int idefloppy_queue_pc_tail (ide_drive_t
*drive
,idefloppy_pc_t
*pc
)
1037 struct ide_floppy_obj
*floppy
= drive
->driver_data
;
1040 ide_init_drive_cmd (&rq
);
1041 rq
.buffer
= (char *) pc
;
1042 rq
.cmd_type
= REQ_TYPE_SPECIAL
;
1043 rq
.rq_disk
= floppy
->disk
;
1045 return ide_do_drive_cmd(drive
, &rq
, ide_wait
);
1049 * Look at the flexible disk page parameters. We ignore the CHS capacity
1050 * parameters and use the LBA parameters instead.
1052 static int ide_floppy_get_flexible_disk_page(ide_drive_t
*drive
)
1054 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1057 int capacity
, lba_capacity
;
1058 u16 transfer_rate
, sector_size
, cyls
, rpm
;
1061 idefloppy_create_mode_sense_cmd(&pc
, IDEFLOPPY_FLEXIBLE_DISK_PAGE
,
1062 MODE_SENSE_CURRENT
);
1064 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1065 printk(KERN_ERR
"ide-floppy: Can't get flexible disk page"
1069 floppy
->wp
= !!(pc
.buffer
[3] & 0x80);
1070 set_disk_ro(floppy
->disk
, floppy
->wp
);
1071 page
= &pc
.buffer
[8];
1073 transfer_rate
= be16_to_cpu(*(u16
*)&pc
.buffer
[8 + 2]);
1074 sector_size
= be16_to_cpu(*(u16
*)&pc
.buffer
[8 + 6]);
1075 cyls
= be16_to_cpu(*(u16
*)&pc
.buffer
[8 + 8]);
1076 rpm
= be16_to_cpu(*(u16
*)&pc
.buffer
[8 + 28]);
1077 heads
= pc
.buffer
[8 + 4];
1078 sectors
= pc
.buffer
[8 + 5];
1080 capacity
= cyls
* heads
* sectors
* sector_size
;
1082 if (memcmp(page
, &floppy
->flexible_disk_page
, 32))
1083 printk(KERN_INFO
"%s: %dkB, %d/%d/%d CHS, %d kBps, "
1084 "%d sector size, %d rpm\n",
1085 drive
->name
, capacity
/ 1024, cyls
, heads
,
1086 sectors
, transfer_rate
/ 8, sector_size
, rpm
);
1088 memcpy(&floppy
->flexible_disk_page
, page
, 32);
1089 drive
->bios_cyl
= cyls
;
1090 drive
->bios_head
= heads
;
1091 drive
->bios_sect
= sectors
;
1092 lba_capacity
= floppy
->blocks
* floppy
->block_size
;
1094 if (capacity
< lba_capacity
) {
1095 printk(KERN_NOTICE
"%s: The disk reports a capacity of %d "
1096 "bytes, but the drive only handles %d\n",
1097 drive
->name
, lba_capacity
, capacity
);
1098 floppy
->blocks
= floppy
->block_size
?
1099 capacity
/ floppy
->block_size
: 0;
1104 static int idefloppy_get_sfrp_bit(ide_drive_t
*drive
)
1106 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1110 idefloppy_create_mode_sense_cmd(&pc
, IDEFLOPPY_CAPABILITIES_PAGE
,
1111 MODE_SENSE_CURRENT
);
1113 set_bit(PC_SUPPRESS_ERROR
, &pc
.flags
);
1114 if (idefloppy_queue_pc_tail(drive
, &pc
))
1117 floppy
->srfp
= pc
.buffer
[8 + 2] & 0x40;
1122 * Determine if a media is present in the floppy drive, and if so, its LBA
1125 static int ide_floppy_get_capacity(ide_drive_t
*drive
)
1127 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1130 u8 header_len
, desc_cnt
;
1131 int i
, rc
= 1, blocks
, length
;
1133 drive
->bios_cyl
= 0;
1134 drive
->bios_head
= drive
->bios_sect
= 0;
1136 floppy
->bs_factor
= 1;
1137 set_capacity(floppy
->disk
, 0);
1139 idefloppy_create_read_capacity_cmd(&pc
);
1140 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1141 printk(KERN_ERR
"ide-floppy: Can't get floppy parameters\n");
1144 header_len
= pc
.buffer
[3];
1145 cap_desc
= &pc
.buffer
[4];
1146 desc_cnt
= header_len
/ 8; /* capacity descriptor of 8 bytes */
1148 for (i
= 0; i
< desc_cnt
; i
++) {
1149 unsigned int desc_start
= 4 + i
*8;
1151 blocks
= be32_to_cpu(*(u32
*)&pc
.buffer
[desc_start
]);
1152 length
= be16_to_cpu(*(u16
*)&pc
.buffer
[desc_start
+ 6]);
1154 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
1155 i
, blocks
* length
/ 1024, blocks
, length
);
1160 * the code below is valid only for the 1st descriptor, ie i=0
1163 switch (pc
.buffer
[desc_start
+ 4] & 0x03) {
1164 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1165 case CAPACITY_UNFORMATTED
:
1166 if (!test_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
))
1168 * If it is not a clik drive, break out
1169 * (maintains previous driver behaviour)
1172 case CAPACITY_CURRENT
:
1173 /* Normal Zip/LS-120 disks */
1174 if (memcmp(cap_desc
, &floppy
->cap_desc
, 8))
1175 printk(KERN_INFO
"%s: %dkB, %d blocks, %d "
1176 "sector size\n", drive
->name
,
1177 blocks
* length
/ 1024, blocks
, length
);
1178 memcpy(&floppy
->cap_desc
, cap_desc
, 8);
1180 if (!length
|| length
% 512) {
1181 printk(KERN_NOTICE
"%s: %d bytes block size "
1182 "not supported\n", drive
->name
, length
);
1184 floppy
->blocks
= blocks
;
1185 floppy
->block_size
= length
;
1186 floppy
->bs_factor
= length
/ 512;
1187 if (floppy
->bs_factor
!= 1)
1188 printk(KERN_NOTICE
"%s: warning: non "
1189 "512 bytes block size not "
1190 "fully supported\n",
1195 case CAPACITY_NO_CARTRIDGE
:
1197 * This is a KERN_ERR so it appears on screen
1198 * for the user to see
1200 printk(KERN_ERR
"%s: No disk in drive\n", drive
->name
);
1202 case CAPACITY_INVALID
:
1203 printk(KERN_ERR
"%s: Invalid capacity for disk "
1204 "in drive\n", drive
->name
);
1207 debug_log("Descriptor 0 Code: %d\n",
1208 pc
.buffer
[desc_start
+ 4] & 0x03);
1211 /* Clik! disk does not support get_flexible_disk_page */
1212 if (!test_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
))
1213 (void) ide_floppy_get_flexible_disk_page(drive
);
1215 set_capacity(floppy
->disk
, floppy
->blocks
* floppy
->bs_factor
);
1220 * Obtain the list of formattable capacities.
1221 * Very similar to ide_floppy_get_capacity, except that we push the capacity
1222 * descriptors to userland, instead of our own structures.
1224 * Userland gives us the following structure:
1226 * struct idefloppy_format_capacities {
1234 * userland initializes nformats to the number of allocated formats[] records.
1235 * On exit we set nformats to the number of records we've actually initialized.
1238 static int ide_floppy_get_format_capacities(ide_drive_t
*drive
, int __user
*arg
)
1241 u8 header_len
, desc_cnt
;
1242 int i
, blocks
, length
, u_array_size
, u_index
;
1245 if (get_user(u_array_size
, arg
))
1248 if (u_array_size
<= 0)
1251 idefloppy_create_read_capacity_cmd(&pc
);
1252 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1253 printk(KERN_ERR
"ide-floppy: Can't get floppy parameters\n");
1256 header_len
= pc
.buffer
[3];
1257 desc_cnt
= header_len
/ 8; /* capacity descriptor of 8 bytes */
1263 * We always skip the first capacity descriptor. That's the current
1264 * capacity. We are interested in the remaining descriptors, the
1265 * formattable capacities.
1267 for (i
= 1; i
< desc_cnt
; i
++) {
1268 unsigned int desc_start
= 4 + i
*8;
1270 if (u_index
>= u_array_size
)
1271 break; /* User-supplied buffer too small */
1273 blocks
= be32_to_cpu(*(u32
*)&pc
.buffer
[desc_start
]);
1274 length
= be16_to_cpu(*(u16
*)&pc
.buffer
[desc_start
+ 6]);
1276 if (put_user(blocks
, argp
))
1280 if (put_user(length
, argp
))
1287 if (put_user(u_index
, arg
))
1293 ** Get ATAPI_FORMAT_UNIT progress indication.
1295 ** Userland gives a pointer to an int. The int is set to a progress
1296 ** indicator 0-65536, with 65536=100%.
1298 ** If the drive does not support format progress indication, we just check
1299 ** the dsc bit, and return either 0 or 65536.
1302 static int idefloppy_get_format_progress(ide_drive_t
*drive
, int __user
*arg
)
1304 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1306 int progress_indication
= 0x10000;
1309 idefloppy_create_request_sense_cmd(&pc
);
1310 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1314 if (floppy
->sense_key
== 2 &&
1316 floppy
->ascq
== 4) {
1317 progress_indication
= floppy
->progress_indication
;
1319 /* Else assume format_unit has finished, and we're
1322 unsigned long flags
;
1325 local_irq_save(flags
);
1326 stat
= drive
->hwif
->INB(IDE_STATUS_REG
);
1327 local_irq_restore(flags
);
1329 progress_indication
= ((stat
& SEEK_STAT
) == 0) ? 0 : 0x10000;
1331 if (put_user(progress_indication
, arg
))
1338 * Return the current floppy capacity.
1340 static sector_t
idefloppy_capacity (ide_drive_t
*drive
)
1342 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1343 unsigned long capacity
= floppy
->blocks
* floppy
->bs_factor
;
1349 * idefloppy_identify_device checks if we can support a drive,
1350 * based on the ATAPI IDENTIFY command results.
1352 static int idefloppy_identify_device (ide_drive_t
*drive
,struct hd_driveid
*id
)
1354 struct idefloppy_id_gcw gcw
;
1355 #if IDEFLOPPY_DEBUG_INFO
1357 #endif /* IDEFLOPPY_DEBUG_INFO */
1359 *((u16
*) &gcw
) = id
->config
;
1362 /* kludge for Apple PowerBook internal zip */
1363 if ((gcw
.device_type
== 5) &&
1364 !strstr(id
->model
, "CD-ROM") &&
1365 strstr(id
->model
, "ZIP"))
1366 gcw
.device_type
= 0;
1369 #if IDEFLOPPY_DEBUG_INFO
1370 printk(KERN_INFO
"Dumping ATAPI Identify Device floppy parameters\n");
1371 switch (gcw
.protocol
) {
1372 case 0: case 1: sprintf(buffer
, "ATA");break;
1373 case 2: sprintf(buffer
, "ATAPI");break;
1374 case 3: sprintf(buffer
, "Reserved (Unknown to ide-floppy)");break;
1376 printk(KERN_INFO
"Protocol Type: %s\n", buffer
);
1377 switch (gcw
.device_type
) {
1378 case 0: sprintf(buffer
, "Direct-access Device");break;
1379 case 1: sprintf(buffer
, "Streaming Tape Device");break;
1380 case 2: case 3: case 4: sprintf (buffer
, "Reserved");break;
1381 case 5: sprintf(buffer
, "CD-ROM Device");break;
1382 case 6: sprintf(buffer
, "Reserved");
1383 case 7: sprintf(buffer
, "Optical memory Device");break;
1384 case 0x1f: sprintf(buffer
, "Unknown or no Device type");break;
1385 default: sprintf(buffer
, "Reserved");
1387 printk(KERN_INFO
"Device Type: %x - %s\n", gcw
.device_type
, buffer
);
1388 printk(KERN_INFO
"Removable: %s\n",gcw
.removable
? "Yes":"No");
1389 switch (gcw
.drq_type
) {
1390 case 0: sprintf(buffer
, "Microprocessor DRQ");break;
1391 case 1: sprintf(buffer
, "Interrupt DRQ");break;
1392 case 2: sprintf(buffer
, "Accelerated DRQ");break;
1393 case 3: sprintf(buffer
, "Reserved");break;
1395 printk(KERN_INFO
"Command Packet DRQ Type: %s\n", buffer
);
1396 switch (gcw
.packet_size
) {
1397 case 0: sprintf(buffer
, "12 bytes");break;
1398 case 1: sprintf(buffer
, "16 bytes");break;
1399 default: sprintf(buffer
, "Reserved");break;
1401 printk(KERN_INFO
"Command Packet Size: %s\n", buffer
);
1402 #endif /* IDEFLOPPY_DEBUG_INFO */
1404 if (gcw
.protocol
!= 2)
1405 printk(KERN_ERR
"ide-floppy: Protocol is not ATAPI\n");
1406 else if (gcw
.device_type
!= 0)
1407 printk(KERN_ERR
"ide-floppy: Device type is not set to floppy\n");
1408 else if (!gcw
.removable
)
1409 printk(KERN_ERR
"ide-floppy: The removable flag is not set\n");
1410 else if (gcw
.drq_type
== 3) {
1411 printk(KERN_ERR
"ide-floppy: Sorry, DRQ type %d not supported\n", gcw
.drq_type
);
1412 } else if (gcw
.packet_size
!= 0) {
1413 printk(KERN_ERR
"ide-floppy: Packet size is not 12 bytes long\n");
1419 #ifdef CONFIG_IDE_PROC_FS
1420 static void idefloppy_add_settings(ide_drive_t
*drive
)
1422 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1425 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
1427 ide_add_setting(drive
, "bios_cyl", SETTING_RW
, TYPE_INT
, 0, 1023, 1, 1, &drive
->bios_cyl
, NULL
);
1428 ide_add_setting(drive
, "bios_head", SETTING_RW
, TYPE_BYTE
, 0, 255, 1, 1, &drive
->bios_head
, NULL
);
1429 ide_add_setting(drive
, "bios_sect", SETTING_RW
, TYPE_BYTE
, 0, 63, 1, 1, &drive
->bios_sect
, NULL
);
1430 ide_add_setting(drive
, "ticks", SETTING_RW
, TYPE_BYTE
, 0, 255, 1, 1, &floppy
->ticks
, NULL
);
1433 static inline void idefloppy_add_settings(ide_drive_t
*drive
) { ; }
1437 * Driver initialization.
1439 static void idefloppy_setup (ide_drive_t
*drive
, idefloppy_floppy_t
*floppy
)
1441 struct idefloppy_id_gcw gcw
;
1443 *((u16
*) &gcw
) = drive
->id
->config
;
1444 floppy
->pc
= floppy
->pc_stack
;
1445 if (gcw
.drq_type
== 1)
1446 set_bit(IDEFLOPPY_DRQ_INTERRUPT
, &floppy
->flags
);
1448 * We used to check revisions here. At this point however
1449 * I'm giving up. Just assume they are all broken, its easier.
1451 * The actual reason for the workarounds was likely
1452 * a driver bug after all rather than a firmware bug,
1453 * and the workaround below used to hide it. It should
1454 * be fixed as of version 1.9, but to be on the safe side
1455 * we'll leave the limitation below for the 2.2.x tree.
1458 if (!strncmp(drive
->id
->model
, "IOMEGA ZIP 100 ATAPI", 20)) {
1459 set_bit(IDEFLOPPY_ZIP_DRIVE
, &floppy
->flags
);
1460 /* This value will be visible in the /proc/ide/hdx/settings */
1461 floppy
->ticks
= IDEFLOPPY_TICKS_DELAY
;
1462 blk_queue_max_sectors(drive
->queue
, 64);
1466 * Guess what? The IOMEGA Clik! drive also needs the
1467 * above fix. It makes nasty clicking noises without
1468 * it, so please don't remove this.
1470 if (strncmp(drive
->id
->model
, "IOMEGA Clik!", 11) == 0) {
1471 blk_queue_max_sectors(drive
->queue
, 64);
1472 set_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
);
1476 (void) ide_floppy_get_capacity(drive
);
1477 idefloppy_add_settings(drive
);
1480 static void ide_floppy_remove(ide_drive_t
*drive
)
1482 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1483 struct gendisk
*g
= floppy
->disk
;
1485 ide_proc_unregister_driver(drive
, floppy
->driver
);
1489 ide_floppy_put(floppy
);
1492 static void idefloppy_cleanup_obj(struct kref
*kref
)
1494 struct ide_floppy_obj
*floppy
= to_ide_floppy(kref
);
1495 ide_drive_t
*drive
= floppy
->drive
;
1496 struct gendisk
*g
= floppy
->disk
;
1498 drive
->driver_data
= NULL
;
1499 g
->private_data
= NULL
;
1504 #ifdef CONFIG_IDE_PROC_FS
1505 static int proc_idefloppy_read_capacity
1506 (char *page
, char **start
, off_t off
, int count
, int *eof
, void *data
)
1508 ide_drive_t
*drive
= (ide_drive_t
*)data
;
1511 len
= sprintf(page
,"%llu\n", (long long)idefloppy_capacity(drive
));
1512 PROC_IDE_READ_RETURN(page
,start
,off
,count
,eof
,len
);
1515 static ide_proc_entry_t idefloppy_proc
[] = {
1516 { "capacity", S_IFREG
|S_IRUGO
, proc_idefloppy_read_capacity
, NULL
},
1517 { "geometry", S_IFREG
|S_IRUGO
, proc_ide_read_geometry
, NULL
},
1518 { NULL
, 0, NULL
, NULL
}
1520 #endif /* CONFIG_IDE_PROC_FS */
1522 static int ide_floppy_probe(ide_drive_t
*);
1524 static ide_driver_t idefloppy_driver
= {
1526 .owner
= THIS_MODULE
,
1527 .name
= "ide-floppy",
1528 .bus
= &ide_bus_type
,
1530 .probe
= ide_floppy_probe
,
1531 .remove
= ide_floppy_remove
,
1532 .version
= IDEFLOPPY_VERSION
,
1533 .media
= ide_floppy
,
1534 .supports_dsc_overlap
= 0,
1535 .do_request
= idefloppy_do_request
,
1536 .end_request
= idefloppy_do_end_request
,
1537 .error
= __ide_error
,
1538 .abort
= __ide_abort
,
1539 #ifdef CONFIG_IDE_PROC_FS
1540 .proc
= idefloppy_proc
,
1544 static int idefloppy_open(struct inode
*inode
, struct file
*filp
)
1546 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
1547 struct ide_floppy_obj
*floppy
;
1552 debug_log("Reached %s\n", __func__
);
1554 if (!(floppy
= ide_floppy_get(disk
)))
1557 drive
= floppy
->drive
;
1561 if (floppy
->openers
== 1) {
1562 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
, &floppy
->flags
);
1565 idefloppy_create_test_unit_ready_cmd(&pc
);
1566 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1567 idefloppy_create_start_stop_cmd(&pc
, 1);
1568 (void) idefloppy_queue_pc_tail(drive
, &pc
);
1571 if (ide_floppy_get_capacity(drive
)
1572 && (filp
->f_flags
& O_NDELAY
) == 0
1574 ** Allow O_NDELAY to open a drive without a disk, or with
1575 ** an unreadable disk, so that we can get the format
1576 ** capacity of the drive or begin the format - Sam
1580 goto out_put_floppy
;
1583 if (floppy
->wp
&& (filp
->f_mode
& 2)) {
1585 goto out_put_floppy
;
1587 set_bit(IDEFLOPPY_MEDIA_CHANGED
, &floppy
->flags
);
1588 /* IOMEGA Clik! drives do not support lock/unlock commands */
1589 if (!test_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
)) {
1590 idefloppy_create_prevent_cmd(&pc
, 1);
1591 (void) idefloppy_queue_pc_tail(drive
, &pc
);
1593 check_disk_change(inode
->i_bdev
);
1594 } else if (test_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
, &floppy
->flags
)) {
1596 goto out_put_floppy
;
1602 ide_floppy_put(floppy
);
1606 static int idefloppy_release(struct inode
*inode
, struct file
*filp
)
1608 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
1609 struct ide_floppy_obj
*floppy
= ide_floppy_g(disk
);
1610 ide_drive_t
*drive
= floppy
->drive
;
1613 debug_log("Reached %s\n", __func__
);
1615 if (floppy
->openers
== 1) {
1616 /* IOMEGA Clik! drives do not support lock/unlock commands */
1617 if (!test_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
)) {
1618 idefloppy_create_prevent_cmd(&pc
, 0);
1619 (void) idefloppy_queue_pc_tail(drive
, &pc
);
1622 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
, &floppy
->flags
);
1627 ide_floppy_put(floppy
);
1632 static int idefloppy_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1634 struct ide_floppy_obj
*floppy
= ide_floppy_g(bdev
->bd_disk
);
1635 ide_drive_t
*drive
= floppy
->drive
;
1637 geo
->heads
= drive
->bios_head
;
1638 geo
->sectors
= drive
->bios_sect
;
1639 geo
->cylinders
= (u16
)drive
->bios_cyl
; /* truncate */
1643 static int ide_floppy_lockdoor(idefloppy_floppy_t
*floppy
, idefloppy_pc_t
*pc
,
1644 unsigned long arg
, unsigned int cmd
)
1646 if (floppy
->openers
> 1)
1649 /* The IOMEGA Clik! Drive doesn't support this command -
1650 * no room for an eject mechanism */
1651 if (!test_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
)) {
1652 int prevent
= arg
? 1 : 0;
1654 if (cmd
== CDROMEJECT
)
1657 idefloppy_create_prevent_cmd(pc
, prevent
);
1658 (void) idefloppy_queue_pc_tail(floppy
->drive
, pc
);
1661 if (cmd
== CDROMEJECT
) {
1662 idefloppy_create_start_stop_cmd(pc
, 2);
1663 (void) idefloppy_queue_pc_tail(floppy
->drive
, pc
);
1669 static int ide_floppy_format_unit(idefloppy_floppy_t
*floppy
,
1672 int blocks
, length
, flags
, err
= 0;
1675 if (floppy
->openers
> 1) {
1676 /* Don't format if someone is using the disk */
1677 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
, &floppy
->flags
);
1681 set_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
, &floppy
->flags
);
1684 * Send ATAPI_FORMAT_UNIT to the drive.
1686 * Userland gives us the following structure:
1688 * struct idefloppy_format_command {
1694 * flags is a bitmask, currently, the only defined flag is:
1696 * 0x01 - verify media after format.
1698 if (get_user(blocks
, arg
) ||
1699 get_user(length
, arg
+1) ||
1700 get_user(flags
, arg
+2)) {
1705 (void) idefloppy_get_sfrp_bit(floppy
->drive
);
1706 idefloppy_create_format_unit_cmd(&pc
, blocks
, length
, flags
);
1708 if (idefloppy_queue_pc_tail(floppy
->drive
, &pc
))
1713 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
, &floppy
->flags
);
1718 static int idefloppy_ioctl(struct inode
*inode
, struct file
*file
,
1719 unsigned int cmd
, unsigned long arg
)
1721 struct block_device
*bdev
= inode
->i_bdev
;
1722 struct ide_floppy_obj
*floppy
= ide_floppy_g(bdev
->bd_disk
);
1723 ide_drive_t
*drive
= floppy
->drive
;
1725 void __user
*argp
= (void __user
*)arg
;
1731 case CDROM_LOCKDOOR
:
1732 return ide_floppy_lockdoor(floppy
, &pc
, arg
, cmd
);
1733 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED
:
1735 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY
:
1736 return ide_floppy_get_format_capacities(drive
, argp
);
1737 case IDEFLOPPY_IOCTL_FORMAT_START
:
1738 if (!(file
->f_mode
& 2))
1741 return ide_floppy_format_unit(floppy
, (int __user
*)arg
);
1742 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS
:
1743 return idefloppy_get_format_progress(drive
, argp
);
1747 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1748 * and CDROM_SEND_PACKET (legacy) ioctls
1750 if (cmd
!= CDROM_SEND_PACKET
&& cmd
!= SCSI_IOCTL_SEND_COMMAND
)
1751 err
= scsi_cmd_ioctl(file
, bdev
->bd_disk
->queue
,
1752 bdev
->bd_disk
, cmd
, argp
);
1757 err
= generic_ide_ioctl(drive
, file
, bdev
, cmd
, arg
);
1762 static int idefloppy_media_changed(struct gendisk
*disk
)
1764 struct ide_floppy_obj
*floppy
= ide_floppy_g(disk
);
1765 ide_drive_t
*drive
= floppy
->drive
;
1767 /* do not scan partitions twice if this is a removable device */
1768 if (drive
->attach
) {
1772 return test_and_clear_bit(IDEFLOPPY_MEDIA_CHANGED
, &floppy
->flags
);
1775 static int idefloppy_revalidate_disk(struct gendisk
*disk
)
1777 struct ide_floppy_obj
*floppy
= ide_floppy_g(disk
);
1778 set_capacity(disk
, idefloppy_capacity(floppy
->drive
));
1782 static struct block_device_operations idefloppy_ops
= {
1783 .owner
= THIS_MODULE
,
1784 .open
= idefloppy_open
,
1785 .release
= idefloppy_release
,
1786 .ioctl
= idefloppy_ioctl
,
1787 .getgeo
= idefloppy_getgeo
,
1788 .media_changed
= idefloppy_media_changed
,
1789 .revalidate_disk
= idefloppy_revalidate_disk
1792 static int ide_floppy_probe(ide_drive_t
*drive
)
1794 idefloppy_floppy_t
*floppy
;
1797 if (!strstr("ide-floppy", drive
->driver_req
))
1799 if (!drive
->present
)
1801 if (drive
->media
!= ide_floppy
)
1803 if (!idefloppy_identify_device (drive
, drive
->id
)) {
1804 printk (KERN_ERR
"ide-floppy: %s: not supported by this version of ide-floppy\n", drive
->name
);
1808 printk("ide-floppy: passing drive %s to ide-scsi emulation.\n", drive
->name
);
1811 if ((floppy
= kzalloc(sizeof (idefloppy_floppy_t
), GFP_KERNEL
)) == NULL
) {
1812 printk (KERN_ERR
"ide-floppy: %s: Can't allocate a floppy structure\n", drive
->name
);
1816 g
= alloc_disk(1 << PARTN_BITS
);
1818 goto out_free_floppy
;
1820 ide_init_disk(g
, drive
);
1822 ide_proc_register_driver(drive
, &idefloppy_driver
);
1824 kref_init(&floppy
->kref
);
1826 floppy
->drive
= drive
;
1827 floppy
->driver
= &idefloppy_driver
;
1830 g
->private_data
= &floppy
->driver
;
1832 drive
->driver_data
= floppy
;
1834 idefloppy_setup (drive
, floppy
);
1836 g
->minors
= 1 << PARTN_BITS
;
1837 g
->driverfs_dev
= &drive
->gendev
;
1838 g
->flags
= drive
->removable
? GENHD_FL_REMOVABLE
: 0;
1839 g
->fops
= &idefloppy_ops
;
1850 MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
1852 static void __exit
idefloppy_exit (void)
1854 driver_unregister(&idefloppy_driver
.gen_driver
);
1857 static int __init
idefloppy_init(void)
1859 printk("ide-floppy driver " IDEFLOPPY_VERSION
"\n");
1860 return driver_register(&idefloppy_driver
.gen_driver
);
1863 MODULE_ALIAS("ide:*m-floppy*");
1864 module_init(idefloppy_init
);
1865 module_exit(idefloppy_exit
);
1866 MODULE_LICENSE("GPL");