2 * IDE ATAPI floppy driver.
4 * Copyright (C) 1996-1999 Gadi Oxman <gadio@netvision.net.il>
5 * Copyright (C) 2000-2002 Paul Bristow <paul@paulbristow.net>
6 * Copyright (C) 2005 Bartlomiej Zolnierkiewicz
10 * The driver currently doesn't have any fancy features, just the bare
11 * minimum read/write support.
13 * This driver supports the following IDE floppy drives:
15 * LS-120/240 SuperDisk
17 * Iomega PC Card Clik!/PocketZip
19 * For a historical changelog see
20 * Documentation/ide/ChangeLog.ide-floppy.1996-2002
23 #define IDEFLOPPY_VERSION "0.99.newide"
25 #include <linux/module.h>
26 #include <linux/types.h>
27 #include <linux/string.h>
28 #include <linux/kernel.h>
29 #include <linux/delay.h>
30 #include <linux/timer.h>
32 #include <linux/interrupt.h>
33 #include <linux/major.h>
34 #include <linux/errno.h>
35 #include <linux/genhd.h>
36 #include <linux/slab.h>
37 #include <linux/cdrom.h>
38 #include <linux/ide.h>
39 #include <linux/bitops.h>
40 #include <linux/mutex.h>
42 #include <scsi/scsi_ioctl.h>
44 #include <asm/byteorder.h>
45 #include <linux/irq.h>
46 #include <linux/uaccess.h>
48 #include <asm/unaligned.h>
51 * The following are used to debug the driver.
53 #define IDEFLOPPY_DEBUG_LOG 0
54 #define IDEFLOPPY_DEBUG_INFO 0
55 #define IDEFLOPPY_DEBUG_BUGS 1
57 /* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
58 #define IDEFLOPPY_DEBUG( fmt, args... )
60 #if IDEFLOPPY_DEBUG_LOG
61 #define debug_log(fmt, args...) \
62 printk(KERN_INFO "ide-floppy: " fmt, ## args)
64 #define debug_log(fmt, args... ) do {} while(0)
69 * Some drives require a longer irq timeout.
71 #define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
74 * After each failed packet command we issue a request sense command
75 * and retry the packet command IDEFLOPPY_MAX_PC_RETRIES times.
77 #define IDEFLOPPY_MAX_PC_RETRIES 3
80 * With each packet command, we allocate a buffer of
81 * IDEFLOPPY_PC_BUFFER_SIZE bytes.
83 #define IDEFLOPPY_PC_BUFFER_SIZE 256
86 * In various places in the driver, we need to allocate storage
87 * for packet commands and requests, which will remain valid while
88 * we leave the driver to wait for an interrupt or a timeout event.
90 #define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
93 * Our view of a packet command.
95 typedef struct idefloppy_packet_command_s
{
96 u8 c
[12]; /* Actual packet bytes */
97 int retries
; /* On each retry, we increment retries */
98 int error
; /* Error code */
99 int request_transfer
; /* Bytes to transfer */
100 int actually_transferred
; /* Bytes actually transferred */
101 int buffer_size
; /* Size of our data buffer */
102 int b_count
; /* Missing/Available data on the current buffer */
103 struct request
*rq
; /* The corresponding request */
104 u8
*buffer
; /* Data buffer */
105 u8
*current_position
; /* Pointer into the above buffer */
106 void (*callback
) (ide_drive_t
*); /* Called when this packet command is completed */
107 u8 pc_buffer
[IDEFLOPPY_PC_BUFFER_SIZE
]; /* Temporary buffer */
108 unsigned long flags
; /* Status/Action bit flags: long for set_bit */
112 * Packet command flag bits.
114 #define PC_ABORT 0 /* Set when an error is considered normal - We won't retry */
115 #define PC_DMA_RECOMMENDED 2 /* 1 when we prefer to use DMA if possible */
116 #define PC_DMA_IN_PROGRESS 3 /* 1 while DMA in progress */
117 #define PC_DMA_ERROR 4 /* 1 when encountered problem during DMA */
118 #define PC_WRITING 5 /* Data direction */
120 #define PC_SUPPRESS_ERROR 6 /* Suppress error reporting */
123 * Flexible disk page.
126 #if defined(__LITTLE_ENDIAN_BITFIELD)
127 unsigned page_code
:6; /* Page code - Should be 0x5 */
128 unsigned reserved1_6
:1; /* Reserved */
129 unsigned ps
:1; /* The device is capable of saving the page */
130 #elif defined(__BIG_ENDIAN_BITFIELD)
131 unsigned ps
:1; /* The device is capable of saving the page */
132 unsigned reserved1_6
:1; /* Reserved */
133 unsigned page_code
:6; /* Page code - Should be 0x5 */
135 #error "Bitfield endianness not defined! Check your byteorder.h"
137 u8 page_length
; /* Page Length - Should be 0x1e */
138 u16 transfer_rate
; /* In kilobits per second */
139 u8 heads
, sectors
; /* Number of heads, Number of sectors per track */
140 u16 sector_size
; /* Byes per sector */
141 u16 cyls
; /* Number of cylinders */
143 u8 motor_delay
; /* Motor off delay */
145 u16 rpm
; /* Rotations per minute */
147 } idefloppy_flexible_disk_page_t
;
154 u8 length
; /* Length of the following descriptors in bytes */
155 } idefloppy_capacity_header_t
;
158 u32 blocks
; /* Number of blocks */
159 #if defined(__LITTLE_ENDIAN_BITFIELD)
160 unsigned dc
:2; /* Descriptor Code */
161 unsigned reserved
:6;
162 #elif defined(__BIG_ENDIAN_BITFIELD)
163 unsigned reserved
:6;
164 unsigned dc
:2; /* Descriptor Code */
166 #error "Bitfield endianness not defined! Check your byteorder.h"
168 u8 length_msb
; /* Block Length (MSB)*/
169 u16 length
; /* Block Length */
170 } idefloppy_capacity_descriptor_t
;
172 #define CAPACITY_INVALID 0x00
173 #define CAPACITY_UNFORMATTED 0x01
174 #define CAPACITY_CURRENT 0x02
175 #define CAPACITY_NO_CARTRIDGE 0x03
178 * Most of our global data which we need to save even as we leave the
179 * driver due to an interrupt or a timer event is stored in a variable
180 * of type idefloppy_floppy_t, defined below.
182 typedef struct ide_floppy_obj
{
184 ide_driver_t
*driver
;
185 struct gendisk
*disk
;
187 unsigned int openers
; /* protected by BKL for now */
189 /* Current packet command */
191 /* Last failed packet command */
192 idefloppy_pc_t
*failed_pc
;
193 /* Packet command stack */
194 idefloppy_pc_t pc_stack
[IDEFLOPPY_PC_STACK
];
195 /* Next free packet command storage space */
197 struct request rq_stack
[IDEFLOPPY_PC_STACK
];
198 /* We implement a circular array */
202 * Last error information
204 u8 sense_key
, asc
, ascq
;
205 /* delay this long before sending packet command */
207 int progress_indication
;
213 int blocks
, block_size
, bs_factor
;
214 /* Last format capacity */
215 idefloppy_capacity_descriptor_t capacity
;
216 /* Copy of the flexible disk page */
217 idefloppy_flexible_disk_page_t flexible_disk_page
;
220 /* Supports format progress report */
222 /* Status/Action flags */
224 } idefloppy_floppy_t
;
226 #define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
229 * Floppy flag bits values.
231 #define IDEFLOPPY_DRQ_INTERRUPT 0 /* DRQ interrupt device */
232 #define IDEFLOPPY_MEDIA_CHANGED 1 /* Media may have changed */
233 #define IDEFLOPPY_USE_READ12 2 /* Use READ12/WRITE12 or READ10/WRITE10 */
234 #define IDEFLOPPY_FORMAT_IN_PROGRESS 3 /* Format in progress */
235 #define IDEFLOPPY_CLIK_DRIVE 4 /* Avoid commands not supported in Clik drive */
236 #define IDEFLOPPY_ZIP_DRIVE 5 /* Requires BH algorithm for packets */
239 * Defines for the mode sense command
241 #define MODE_SENSE_CURRENT 0x00
242 #define MODE_SENSE_CHANGEABLE 0x01
243 #define MODE_SENSE_DEFAULT 0x02
244 #define MODE_SENSE_SAVED 0x03
247 * IOCTLs used in low-level formatting.
250 #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
251 #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
252 #define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
253 #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
256 * Error codes which are returned in rq->errors to the higher part
259 #define IDEFLOPPY_ERROR_GENERAL 101
262 * The following is used to format the general configuration word of
263 * the ATAPI IDENTIFY DEVICE command.
265 struct idefloppy_id_gcw
{
266 #if defined(__LITTLE_ENDIAN_BITFIELD)
267 unsigned packet_size
:2; /* Packet Size */
268 unsigned reserved234
:3; /* Reserved */
269 unsigned drq_type
:2; /* Command packet DRQ type */
270 unsigned removable
:1; /* Removable media */
271 unsigned device_type
:5; /* Device type */
272 unsigned reserved13
:1; /* Reserved */
273 unsigned protocol
:2; /* Protocol type */
274 #elif defined(__BIG_ENDIAN_BITFIELD)
275 unsigned protocol
:2; /* Protocol type */
276 unsigned reserved13
:1; /* Reserved */
277 unsigned device_type
:5; /* Device type */
278 unsigned removable
:1; /* Removable media */
279 unsigned drq_type
:2; /* Command packet DRQ type */
280 unsigned reserved234
:3; /* Reserved */
281 unsigned packet_size
:2; /* Packet Size */
283 #error "Bitfield endianness not defined! Check your byteorder.h"
288 * Pages of the SELECT SENSE / MODE SENSE packet commands.
289 * See SFF-8070i spec.
291 #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
292 #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
295 * Mode Parameter Header for the MODE SENSE packet command
298 u16 mode_data_length
; /* Length of the following data transfer */
299 u8 medium_type
; /* Medium Type */
300 #if defined(__LITTLE_ENDIAN_BITFIELD)
301 unsigned reserved3
:7;
302 unsigned wp
:1; /* Write protect */
303 #elif defined(__BIG_ENDIAN_BITFIELD)
304 unsigned wp
:1; /* Write protect */
305 unsigned reserved3
:7;
307 #error "Bitfield endianness not defined! Check your byteorder.h"
310 } idefloppy_mode_parameter_header_t
;
312 static DEFINE_MUTEX(idefloppy_ref_mutex
);
314 #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
316 #define ide_floppy_g(disk) \
317 container_of((disk)->private_data, struct ide_floppy_obj, driver)
319 static struct ide_floppy_obj
*ide_floppy_get(struct gendisk
*disk
)
321 struct ide_floppy_obj
*floppy
= NULL
;
323 mutex_lock(&idefloppy_ref_mutex
);
324 floppy
= ide_floppy_g(disk
);
326 kref_get(&floppy
->kref
);
327 mutex_unlock(&idefloppy_ref_mutex
);
331 static void idefloppy_cleanup_obj(struct kref
*);
333 static void ide_floppy_put(struct ide_floppy_obj
*floppy
)
335 mutex_lock(&idefloppy_ref_mutex
);
336 kref_put(&floppy
->kref
, idefloppy_cleanup_obj
);
337 mutex_unlock(&idefloppy_ref_mutex
);
341 * Too bad. The drive wants to send us data which we are not ready to accept.
342 * Just throw it away.
344 static void idefloppy_discard_data (ide_drive_t
*drive
, unsigned int bcount
)
347 (void) HWIF(drive
)->INB(IDE_DATA_REG
);
350 #if IDEFLOPPY_DEBUG_BUGS
351 static void idefloppy_write_zeros (ide_drive_t
*drive
, unsigned int bcount
)
354 HWIF(drive
)->OUTB(0, IDE_DATA_REG
);
356 #endif /* IDEFLOPPY_DEBUG_BUGS */
360 * idefloppy_do_end_request is used to finish servicing a request.
362 * For read/write requests, we will call ide_end_request to pass to the
365 static int idefloppy_do_end_request(ide_drive_t
*drive
, int uptodate
, int nsecs
)
367 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
368 struct request
*rq
= HWGROUP(drive
)->rq
;
371 debug_log("Reached %s\n", __func__
);
374 case 0: error
= IDEFLOPPY_ERROR_GENERAL
; break;
375 case 1: error
= 0; break;
376 default: error
= uptodate
;
379 floppy
->failed_pc
= NULL
;
380 /* Why does this happen? */
383 if (!blk_special_request(rq
)) {
384 /* our real local end request function */
385 ide_end_request(drive
, uptodate
, nsecs
);
389 /* fixme: need to move this local also */
390 ide_end_drive_cmd(drive
, 0, 0);
394 static void idefloppy_input_buffers (ide_drive_t
*drive
, idefloppy_pc_t
*pc
, unsigned int bcount
)
396 struct request
*rq
= pc
->rq
;
397 struct bio_vec
*bvec
;
398 struct req_iterator iter
;
403 rq_for_each_segment(bvec
, rq
, iter
) {
407 count
= min(bvec
->bv_len
, bcount
);
409 data
= bvec_kmap_irq(bvec
, &flags
);
410 drive
->hwif
->atapi_input_bytes(drive
, data
, count
);
411 bvec_kunmap_irq(data
, &flags
);
414 pc
->b_count
+= count
;
418 idefloppy_do_end_request(drive
, 1, done
>> 9);
421 printk(KERN_ERR
"%s: leftover data in idefloppy_input_buffers, bcount == %d\n", drive
->name
, bcount
);
422 idefloppy_discard_data(drive
, bcount
);
426 static void idefloppy_output_buffers (ide_drive_t
*drive
, idefloppy_pc_t
*pc
, unsigned int bcount
)
428 struct request
*rq
= pc
->rq
;
429 struct req_iterator iter
;
430 struct bio_vec
*bvec
;
435 rq_for_each_segment(bvec
, rq
, iter
) {
439 count
= min(bvec
->bv_len
, bcount
);
441 data
= bvec_kmap_irq(bvec
, &flags
);
442 drive
->hwif
->atapi_output_bytes(drive
, data
, count
);
443 bvec_kunmap_irq(data
, &flags
);
446 pc
->b_count
+= count
;
450 idefloppy_do_end_request(drive
, 1, done
>> 9);
452 #if IDEFLOPPY_DEBUG_BUGS
454 printk(KERN_ERR
"%s: leftover data in idefloppy_output_buffers, bcount == %d\n", drive
->name
, bcount
);
455 idefloppy_write_zeros(drive
, bcount
);
460 static void idefloppy_update_buffers (ide_drive_t
*drive
, idefloppy_pc_t
*pc
)
462 struct request
*rq
= pc
->rq
;
463 struct bio
*bio
= rq
->bio
;
465 while ((bio
= rq
->bio
) != NULL
)
466 idefloppy_do_end_request(drive
, 1, 0);
470 * idefloppy_queue_pc_head generates a new packet command request in front
471 * of the request queue, before the current request, so that it will be
472 * processed immediately, on the next pass through the driver.
474 static void idefloppy_queue_pc_head (ide_drive_t
*drive
,idefloppy_pc_t
*pc
,struct request
*rq
)
476 struct ide_floppy_obj
*floppy
= drive
->driver_data
;
478 ide_init_drive_cmd(rq
);
479 rq
->buffer
= (char *) pc
;
480 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
481 rq
->rq_disk
= floppy
->disk
;
482 (void) ide_do_drive_cmd(drive
, rq
, ide_preempt
);
485 static idefloppy_pc_t
*idefloppy_next_pc_storage (ide_drive_t
*drive
)
487 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
489 if (floppy
->pc_stack_index
== IDEFLOPPY_PC_STACK
)
490 floppy
->pc_stack_index
=0;
491 return (&floppy
->pc_stack
[floppy
->pc_stack_index
++]);
494 static struct request
*idefloppy_next_rq_storage (ide_drive_t
*drive
)
496 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
498 if (floppy
->rq_stack_index
== IDEFLOPPY_PC_STACK
)
499 floppy
->rq_stack_index
= 0;
500 return (&floppy
->rq_stack
[floppy
->rq_stack_index
++]);
503 static void idefloppy_request_sense_callback(ide_drive_t
*drive
)
505 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
506 u8
*buf
= floppy
->pc
->buffer
;
508 debug_log("Reached %s\n", __func__
);
510 if (!floppy
->pc
->error
) {
511 floppy
->sense_key
= buf
[2] & 0x0F;
512 floppy
->asc
= buf
[12];
513 floppy
->ascq
= buf
[13];
514 floppy
->progress_indication
= buf
[15] & 0x80 ?
515 (u16
)get_unaligned((u16
*)&buf
[16]) : 0x10000;
517 if (floppy
->failed_pc
)
518 debug_log("pc = %x, sense key = %x, asc = %x,"
520 floppy
->failed_pc
->c
[0],
525 debug_log("sense key = %x, asc = %x, ascq = %x\n",
531 idefloppy_do_end_request(drive
, 1, 0);
533 printk(KERN_ERR
"Error in REQUEST SENSE itself - Aborting"
535 idefloppy_do_end_request(drive
, 0, 0);
540 * General packet command callback function.
542 static void idefloppy_pc_callback (ide_drive_t
*drive
)
544 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
546 debug_log("Reached %s\n", __func__
);
548 idefloppy_do_end_request(drive
, floppy
->pc
->error
? 0 : 1, 0);
552 * idefloppy_init_pc initializes a packet command.
554 static void idefloppy_init_pc (idefloppy_pc_t
*pc
)
556 memset(pc
->c
, 0, 12);
559 pc
->request_transfer
= 0;
560 pc
->buffer
= pc
->pc_buffer
;
561 pc
->buffer_size
= IDEFLOPPY_PC_BUFFER_SIZE
;
562 pc
->callback
= &idefloppy_pc_callback
;
565 static void idefloppy_create_request_sense_cmd (idefloppy_pc_t
*pc
)
567 idefloppy_init_pc(pc
);
568 pc
->c
[0] = GPCMD_REQUEST_SENSE
;
570 pc
->request_transfer
= 18;
571 pc
->callback
= &idefloppy_request_sense_callback
;
575 * idefloppy_retry_pc is called when an error was detected during the
576 * last packet command. We queue a request sense packet command in
577 * the head of the request list.
579 static void idefloppy_retry_pc (ide_drive_t
*drive
)
584 (void)drive
->hwif
->INB(IDE_ERROR_REG
);
585 pc
= idefloppy_next_pc_storage(drive
);
586 rq
= idefloppy_next_rq_storage(drive
);
587 idefloppy_create_request_sense_cmd(pc
);
588 idefloppy_queue_pc_head(drive
, pc
, rq
);
592 * idefloppy_pc_intr is the usual interrupt handler which will be called
593 * during a packet command.
595 static ide_startstop_t
idefloppy_pc_intr (ide_drive_t
*drive
)
597 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
598 ide_hwif_t
*hwif
= drive
->hwif
;
599 idefloppy_pc_t
*pc
= floppy
->pc
;
600 struct request
*rq
= pc
->rq
;
605 debug_log("Reached %s interrupt handler\n", __func__
);
607 if (test_bit(PC_DMA_IN_PROGRESS
, &pc
->flags
)) {
608 if (HWIF(drive
)->ide_dma_end(drive
)) {
609 set_bit(PC_DMA_ERROR
, &pc
->flags
);
611 pc
->actually_transferred
= pc
->request_transfer
;
612 idefloppy_update_buffers(drive
, pc
);
614 debug_log("DMA finished\n");
617 /* Clear the interrupt */
618 stat
= drive
->hwif
->INB(IDE_STATUS_REG
);
620 if ((stat
& DRQ_STAT
) == 0) { /* No more interrupts */
621 debug_log("Packet command completed, %d bytes transferred\n",
622 pc
->actually_transferred
);
623 clear_bit(PC_DMA_IN_PROGRESS
, &pc
->flags
);
625 local_irq_enable_in_hardirq();
627 if ((stat
& ERR_STAT
) || test_bit(PC_DMA_ERROR
, &pc
->flags
)) {
629 debug_log("%s: I/O error\n", drive
->name
);
631 if (pc
->c
[0] == GPCMD_REQUEST_SENSE
) {
632 printk(KERN_ERR
"ide-floppy: I/O error in "
633 "request sense command\n");
634 return ide_do_reset(drive
);
636 /* Retry operation */
637 idefloppy_retry_pc(drive
);
638 /* queued, but not started */
642 if (floppy
->failed_pc
== pc
)
643 floppy
->failed_pc
= NULL
;
644 /* Command finished - Call the callback function */
649 if (test_and_clear_bit(PC_DMA_IN_PROGRESS
, &pc
->flags
)) {
650 printk(KERN_ERR
"ide-floppy: The floppy wants to issue "
651 "more interrupts in DMA mode\n");
653 return ide_do_reset(drive
);
656 /* Get the number of bytes to transfer */
657 bcount
= (hwif
->INB(IDE_BCOUNTH_REG
) << 8) |
658 hwif
->INB(IDE_BCOUNTL_REG
);
659 /* on this interrupt */
660 ireason
= hwif
->INB(IDE_IREASON_REG
);
663 printk(KERN_ERR
"ide-floppy: CoD != 0 in idefloppy_pc_intr\n");
664 return ide_do_reset(drive
);
666 if (((ireason
& IO
) == IO
) == test_bit(PC_WRITING
, &pc
->flags
)) {
667 /* Hopefully, we will never get here */
668 printk(KERN_ERR
"ide-floppy: We wanted to %s, ",
669 (ireason
& IO
) ? "Write" : "Read");
670 printk(KERN_ERR
"but the floppy wants us to %s !\n",
671 (ireason
& IO
) ? "Read" : "Write");
672 return ide_do_reset(drive
);
674 if (!test_bit(PC_WRITING
, &pc
->flags
)) {
675 /* Reading - Check that we have enough space */
676 temp
= pc
->actually_transferred
+ bcount
;
677 if (temp
> pc
->request_transfer
) {
678 if (temp
> pc
->buffer_size
) {
679 printk(KERN_ERR
"ide-floppy: The floppy wants "
680 "to send us more data than expected "
681 "- discarding data\n");
682 idefloppy_discard_data(drive
, bcount
);
684 ide_set_handler(drive
,
690 debug_log("The floppy wants to send us more data than"
691 " expected - allowing transfer\n");
694 if (test_bit(PC_WRITING
, &pc
->flags
)) {
695 if (pc
->buffer
!= NULL
)
696 /* Write the current buffer */
697 hwif
->atapi_output_bytes(drive
, pc
->current_position
,
700 idefloppy_output_buffers(drive
, pc
, bcount
);
702 if (pc
->buffer
!= NULL
)
703 /* Read the current buffer */
704 hwif
->atapi_input_bytes(drive
, pc
->current_position
,
707 idefloppy_input_buffers(drive
, pc
, bcount
);
709 /* Update the current position */
710 pc
->actually_transferred
+= bcount
;
711 pc
->current_position
+= bcount
;
713 ide_set_handler(drive
, &idefloppy_pc_intr
, IDEFLOPPY_WAIT_CMD
, NULL
); /* And set the interrupt handler again */
718 * This is the original routine that did the packet transfer.
719 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
720 * for that drive below. The algorithm is chosen based on drive type
722 static ide_startstop_t
idefloppy_transfer_pc (ide_drive_t
*drive
)
724 ide_startstop_t startstop
;
725 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
728 if (ide_wait_stat(&startstop
, drive
, DRQ_STAT
, BUSY_STAT
, WAIT_READY
)) {
729 printk(KERN_ERR
"ide-floppy: Strange, packet command "
730 "initiated yet DRQ isn't asserted\n");
733 ireason
= drive
->hwif
->INB(IDE_IREASON_REG
);
734 if ((ireason
& CD
) == 0 || (ireason
& IO
)) {
735 printk(KERN_ERR
"ide-floppy: (IO,CoD) != (0,1) while "
736 "issuing a packet command\n");
737 return ide_do_reset(drive
);
740 /* Set the interrupt routine */
741 ide_set_handler(drive
, &idefloppy_pc_intr
, IDEFLOPPY_WAIT_CMD
, NULL
);
742 /* Send the actual packet */
743 HWIF(drive
)->atapi_output_bytes(drive
, floppy
->pc
->c
, 12);
749 * What we have here is a classic case of a top half / bottom half
750 * interrupt service routine. In interrupt mode, the device sends
751 * an interrupt to signal it's ready to receive a packet. However,
752 * we need to delay about 2-3 ticks before issuing the packet or we
755 * So, follow carefully. transfer_pc1 is called as an interrupt (or
756 * directly). In either case, when the device says it's ready for a
757 * packet, we schedule the packet transfer to occur about 2-3 ticks
758 * later in transfer_pc2.
760 static int idefloppy_transfer_pc2 (ide_drive_t
*drive
)
762 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
764 /* Send the actual packet */
765 HWIF(drive
)->atapi_output_bytes(drive
, floppy
->pc
->c
, 12);
766 /* Timeout for the packet command */
767 return IDEFLOPPY_WAIT_CMD
;
770 static ide_startstop_t
idefloppy_transfer_pc1 (ide_drive_t
*drive
)
772 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
773 ide_startstop_t startstop
;
776 if (ide_wait_stat(&startstop
, drive
, DRQ_STAT
, BUSY_STAT
, WAIT_READY
)) {
777 printk(KERN_ERR
"ide-floppy: Strange, packet command "
778 "initiated yet DRQ isn't asserted\n");
781 ireason
= drive
->hwif
->INB(IDE_IREASON_REG
);
782 if ((ireason
& CD
) == 0 || (ireason
& IO
)) {
783 printk(KERN_ERR
"ide-floppy: (IO,CoD) != (0,1) "
784 "while issuing a packet command\n");
785 return ide_do_reset(drive
);
788 * The following delay solves a problem with ATAPI Zip 100 drives
789 * where the Busy flag was apparently being deasserted before the
790 * unit was ready to receive data. This was happening on a
791 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
792 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
793 * used until after the packet is moved in about 50 msec.
796 ide_set_handler(drive
,
797 &idefloppy_pc_intr
, /* service routine for packet command */
798 floppy
->ticks
, /* wait this long before "failing" */
799 &idefloppy_transfer_pc2
); /* fail == transfer_pc2 */
804 * idefloppy_should_report_error()
806 * Supresses error messages resulting from Medium not present
808 static inline int idefloppy_should_report_error(idefloppy_floppy_t
*floppy
)
810 if (floppy
->sense_key
== 0x02 &&
811 floppy
->asc
== 0x3a &&
812 floppy
->ascq
== 0x00)
818 * Issue a packet command
820 static ide_startstop_t
idefloppy_issue_pc (ide_drive_t
*drive
, idefloppy_pc_t
*pc
)
822 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
823 ide_hwif_t
*hwif
= drive
->hwif
;
824 ide_handler_t
*pkt_xfer_routine
;
828 if (floppy
->failed_pc
== NULL
&&
829 pc
->c
[0] != GPCMD_REQUEST_SENSE
)
830 floppy
->failed_pc
= pc
;
831 /* Set the current packet command */
834 if (pc
->retries
> IDEFLOPPY_MAX_PC_RETRIES
||
835 test_bit(PC_ABORT
, &pc
->flags
)) {
837 * We will "abort" retrying a packet command in case
838 * a legitimate error code was received.
840 if (!test_bit(PC_ABORT
, &pc
->flags
)) {
841 if (!test_bit(PC_SUPPRESS_ERROR
, &pc
->flags
)) {
842 if (idefloppy_should_report_error(floppy
))
843 printk(KERN_ERR
"ide-floppy: %s: I/O error, "
844 "pc = %2x, key = %2x, "
845 "asc = %2x, ascq = %2x\n",
846 drive
->name
, pc
->c
[0],
848 floppy
->asc
, floppy
->ascq
);
851 pc
->error
= IDEFLOPPY_ERROR_GENERAL
;
853 floppy
->failed_pc
= NULL
;
858 debug_log("Retry number - %d\n", pc
->retries
);
861 /* We haven't transferred any data yet */
862 pc
->actually_transferred
= 0;
863 pc
->current_position
= pc
->buffer
;
864 bcount
= min(pc
->request_transfer
, 63 * 1024);
866 if (test_and_clear_bit(PC_DMA_ERROR
, &pc
->flags
))
871 if (test_bit(PC_DMA_RECOMMENDED
, &pc
->flags
) && drive
->using_dma
)
872 dma
= !hwif
->dma_setup(drive
);
874 ide_pktcmd_tf_load(drive
, IDE_TFLAG_NO_SELECT_MASK
|
875 IDE_TFLAG_OUT_DEVICE
, bcount
, dma
);
877 if (dma
) { /* Begin DMA, if necessary */
878 set_bit(PC_DMA_IN_PROGRESS
, &pc
->flags
);
879 hwif
->dma_start(drive
);
882 /* Can we transfer the packet when we get the interrupt or wait? */
883 if (test_bit(IDEFLOPPY_ZIP_DRIVE
, &floppy
->flags
)) {
885 pkt_xfer_routine
= &idefloppy_transfer_pc1
;
888 pkt_xfer_routine
= &idefloppy_transfer_pc
;
891 if (test_bit (IDEFLOPPY_DRQ_INTERRUPT
, &floppy
->flags
)) {
892 /* Issue the packet command */
893 ide_execute_command(drive
, WIN_PACKETCMD
,
899 /* Issue the packet command */
900 HWIF(drive
)->OUTB(WIN_PACKETCMD
, IDE_COMMAND_REG
);
901 return (*pkt_xfer_routine
) (drive
);
905 static void idefloppy_rw_callback (ide_drive_t
*drive
)
907 debug_log("Reached %s\n", __func__
);
909 idefloppy_do_end_request(drive
, 1, 0);
913 static void idefloppy_create_prevent_cmd (idefloppy_pc_t
*pc
, int prevent
)
915 debug_log("creating prevent removal command, prevent = %d\n", prevent
);
917 idefloppy_init_pc(pc
);
918 pc
->c
[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL
;
922 static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t
*pc
)
924 idefloppy_init_pc(pc
);
925 pc
->c
[0] = GPCMD_READ_FORMAT_CAPACITIES
;
928 pc
->request_transfer
= 255;
931 static void idefloppy_create_format_unit_cmd (idefloppy_pc_t
*pc
, int b
, int l
,
934 idefloppy_init_pc(pc
);
935 pc
->c
[0] = GPCMD_FORMAT_UNIT
;
938 memset(pc
->buffer
, 0, 12);
939 pc
->buffer
[1] = 0xA2;
940 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
942 if (flags
& 1) /* Verify bit on... */
943 pc
->buffer
[1] ^= 0x20; /* ... turn off DCRT bit */
946 put_unaligned(cpu_to_be32(b
), (unsigned int *)(&pc
->buffer
[4]));
947 put_unaligned(cpu_to_be32(l
), (unsigned int *)(&pc
->buffer
[8]));
949 set_bit(PC_WRITING
, &pc
->flags
);
953 * A mode sense command is used to "sense" floppy parameters.
955 static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t
*pc
, u8 page_code
, u8 type
)
957 u16 length
= sizeof(idefloppy_mode_parameter_header_t
);
959 idefloppy_init_pc(pc
);
960 pc
->c
[0] = GPCMD_MODE_SENSE_10
;
962 pc
->c
[2] = page_code
+ (type
<< 6);
965 case IDEFLOPPY_CAPABILITIES_PAGE
:
968 case IDEFLOPPY_FLEXIBLE_DISK_PAGE
:
972 printk(KERN_ERR
"ide-floppy: unsupported page code "
973 "in create_mode_sense_cmd\n");
975 put_unaligned(cpu_to_be16(length
), (u16
*) &pc
->c
[7]);
976 pc
->request_transfer
= length
;
979 static void idefloppy_create_start_stop_cmd (idefloppy_pc_t
*pc
, int start
)
981 idefloppy_init_pc(pc
);
982 pc
->c
[0] = GPCMD_START_STOP_UNIT
;
986 static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t
*pc
)
988 idefloppy_init_pc(pc
);
989 pc
->c
[0] = GPCMD_TEST_UNIT_READY
;
992 static void idefloppy_create_rw_cmd (idefloppy_floppy_t
*floppy
, idefloppy_pc_t
*pc
, struct request
*rq
, unsigned long sector
)
994 int block
= sector
/ floppy
->bs_factor
;
995 int blocks
= rq
->nr_sectors
/ floppy
->bs_factor
;
996 int cmd
= rq_data_dir(rq
);
998 debug_log("create_rw1%d_cmd: block == %d, blocks == %d\n",
999 2 * test_bit (IDEFLOPPY_USE_READ12
, &floppy
->flags
),
1002 idefloppy_init_pc(pc
);
1003 if (test_bit(IDEFLOPPY_USE_READ12
, &floppy
->flags
)) {
1004 pc
->c
[0] = cmd
== READ
? GPCMD_READ_12
: GPCMD_WRITE_12
;
1005 put_unaligned(cpu_to_be32(blocks
), (unsigned int *) &pc
->c
[6]);
1007 pc
->c
[0] = cmd
== READ
? GPCMD_READ_10
: GPCMD_WRITE_10
;
1008 put_unaligned(cpu_to_be16(blocks
), (unsigned short *)&pc
->c
[7]);
1010 put_unaligned(cpu_to_be32(block
), (unsigned int *) &pc
->c
[2]);
1011 pc
->callback
= &idefloppy_rw_callback
;
1013 pc
->b_count
= cmd
== READ
? 0 : rq
->bio
->bi_size
;
1014 if (rq
->cmd_flags
& REQ_RW
)
1015 set_bit(PC_WRITING
, &pc
->flags
);
1017 pc
->request_transfer
= pc
->buffer_size
= blocks
* floppy
->block_size
;
1018 set_bit(PC_DMA_RECOMMENDED
, &pc
->flags
);
1022 idefloppy_blockpc_cmd(idefloppy_floppy_t
*floppy
, idefloppy_pc_t
*pc
, struct request
*rq
)
1024 idefloppy_init_pc(pc
);
1025 pc
->callback
= &idefloppy_rw_callback
;
1026 memcpy(pc
->c
, rq
->cmd
, sizeof(pc
->c
));
1028 pc
->b_count
= rq
->data_len
;
1029 if (rq
->data_len
&& rq_data_dir(rq
) == WRITE
)
1030 set_bit(PC_WRITING
, &pc
->flags
);
1031 pc
->buffer
= rq
->data
;
1033 set_bit(PC_DMA_RECOMMENDED
, &pc
->flags
);
1036 * possibly problematic, doesn't look like ide-floppy correctly
1037 * handled scattered requests if dma fails...
1039 pc
->request_transfer
= pc
->buffer_size
= rq
->data_len
;
1043 * idefloppy_do_request is our request handling function.
1045 static ide_startstop_t
idefloppy_do_request (ide_drive_t
*drive
, struct request
*rq
, sector_t block_s
)
1047 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1049 unsigned long block
= (unsigned long)block_s
;
1051 debug_log("dev: %s, cmd_type: %x, errors: %d\n",
1052 rq
->rq_disk
? rq
->rq_disk
->disk_name
: "?",
1053 rq
->cmd_type
, rq
->errors
);
1054 debug_log("sector: %ld, nr_sectors: %ld, "
1055 "current_nr_sectors: %d\n", (long)rq
->sector
,
1056 rq
->nr_sectors
, rq
->current_nr_sectors
);
1058 if (rq
->errors
>= ERROR_MAX
) {
1059 if (floppy
->failed_pc
!= NULL
) {
1060 if (idefloppy_should_report_error(floppy
))
1061 printk(KERN_ERR
"ide-floppy: %s: I/O error, pc = %2x,"
1062 " key = %2x, asc = %2x, ascq = %2x\n",
1063 drive
->name
, floppy
->failed_pc
->c
[0],
1064 floppy
->sense_key
, floppy
->asc
, floppy
->ascq
);
1067 printk(KERN_ERR
"ide-floppy: %s: I/O error\n",
1069 idefloppy_do_end_request(drive
, 0, 0);
1072 if (blk_fs_request(rq
)) {
1073 if (((long)rq
->sector
% floppy
->bs_factor
) ||
1074 (rq
->nr_sectors
% floppy
->bs_factor
)) {
1075 printk("%s: unsupported r/w request size\n",
1077 idefloppy_do_end_request(drive
, 0, 0);
1080 pc
= idefloppy_next_pc_storage(drive
);
1081 idefloppy_create_rw_cmd(floppy
, pc
, rq
, block
);
1082 } else if (blk_special_request(rq
)) {
1083 pc
= (idefloppy_pc_t
*) rq
->buffer
;
1084 } else if (blk_pc_request(rq
)) {
1085 pc
= idefloppy_next_pc_storage(drive
);
1086 idefloppy_blockpc_cmd(floppy
, pc
, rq
);
1088 blk_dump_rq_flags(rq
,
1089 "ide-floppy: unsupported command in queue");
1090 idefloppy_do_end_request(drive
, 0, 0);
1095 return idefloppy_issue_pc(drive
, pc
);
1099 * idefloppy_queue_pc_tail adds a special packet command request to the
1100 * tail of the request queue, and waits for it to be serviced.
1102 static int idefloppy_queue_pc_tail (ide_drive_t
*drive
,idefloppy_pc_t
*pc
)
1104 struct ide_floppy_obj
*floppy
= drive
->driver_data
;
1107 ide_init_drive_cmd (&rq
);
1108 rq
.buffer
= (char *) pc
;
1109 rq
.cmd_type
= REQ_TYPE_SPECIAL
;
1110 rq
.rq_disk
= floppy
->disk
;
1112 return ide_do_drive_cmd(drive
, &rq
, ide_wait
);
1116 * Look at the flexible disk page parameters. We will ignore the CHS
1117 * capacity parameters and use the LBA parameters instead.
1119 static int idefloppy_get_flexible_disk_page (ide_drive_t
*drive
)
1121 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1123 idefloppy_mode_parameter_header_t
*header
;
1124 idefloppy_flexible_disk_page_t
*page
;
1125 int capacity
, lba_capacity
;
1127 idefloppy_create_mode_sense_cmd(&pc
, IDEFLOPPY_FLEXIBLE_DISK_PAGE
, MODE_SENSE_CURRENT
);
1128 if (idefloppy_queue_pc_tail(drive
,&pc
)) {
1129 printk(KERN_ERR
"ide-floppy: Can't get flexible disk "
1130 "page parameters\n");
1133 header
= (idefloppy_mode_parameter_header_t
*) pc
.buffer
;
1134 floppy
->wp
= header
->wp
;
1135 set_disk_ro(floppy
->disk
, floppy
->wp
);
1136 page
= (idefloppy_flexible_disk_page_t
*) (header
+ 1);
1138 page
->transfer_rate
= be16_to_cpu(page
->transfer_rate
);
1139 page
->sector_size
= be16_to_cpu(page
->sector_size
);
1140 page
->cyls
= be16_to_cpu(page
->cyls
);
1141 page
->rpm
= be16_to_cpu(page
->rpm
);
1142 capacity
= page
->cyls
* page
->heads
* page
->sectors
* page
->sector_size
;
1143 if (memcmp (page
, &floppy
->flexible_disk_page
, sizeof (idefloppy_flexible_disk_page_t
)))
1144 printk(KERN_INFO
"%s: %dkB, %d/%d/%d CHS, %d kBps, "
1145 "%d sector size, %d rpm\n",
1146 drive
->name
, capacity
/ 1024, page
->cyls
,
1147 page
->heads
, page
->sectors
,
1148 page
->transfer_rate
/ 8, page
->sector_size
, page
->rpm
);
1150 floppy
->flexible_disk_page
= *page
;
1151 drive
->bios_cyl
= page
->cyls
;
1152 drive
->bios_head
= page
->heads
;
1153 drive
->bios_sect
= page
->sectors
;
1154 lba_capacity
= floppy
->blocks
* floppy
->block_size
;
1155 if (capacity
< lba_capacity
) {
1156 printk(KERN_NOTICE
"%s: The disk reports a capacity of %d "
1157 "bytes, but the drive only handles %d\n",
1158 drive
->name
, lba_capacity
, capacity
);
1159 floppy
->blocks
= floppy
->block_size
? capacity
/ floppy
->block_size
: 0;
1164 static int idefloppy_get_sfrp_bit(ide_drive_t
*drive
)
1166 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1170 idefloppy_create_mode_sense_cmd(&pc
, IDEFLOPPY_CAPABILITIES_PAGE
,
1171 MODE_SENSE_CURRENT
);
1173 set_bit(PC_SUPPRESS_ERROR
, &pc
.flags
);
1174 if (idefloppy_queue_pc_tail(drive
, &pc
))
1177 floppy
->srfp
= pc
.buffer
[8 + 2] & 0x40;
1182 * Determine if a media is present in the floppy drive, and if so,
1185 static int idefloppy_get_capacity (ide_drive_t
*drive
)
1187 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1189 idefloppy_capacity_header_t
*header
;
1190 idefloppy_capacity_descriptor_t
*descriptor
;
1191 int i
, descriptors
, rc
= 1, blocks
, length
;
1193 drive
->bios_cyl
= 0;
1194 drive
->bios_head
= drive
->bios_sect
= 0;
1196 floppy
->bs_factor
= 1;
1197 set_capacity(floppy
->disk
, 0);
1199 idefloppy_create_read_capacity_cmd(&pc
);
1200 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1201 printk(KERN_ERR
"ide-floppy: Can't get floppy parameters\n");
1204 header
= (idefloppy_capacity_header_t
*) pc
.buffer
;
1205 descriptors
= header
->length
/ sizeof(idefloppy_capacity_descriptor_t
);
1206 descriptor
= (idefloppy_capacity_descriptor_t
*) (header
+ 1);
1208 for (i
= 0; i
< descriptors
; i
++, descriptor
++) {
1209 blocks
= descriptor
->blocks
= be32_to_cpu(descriptor
->blocks
);
1210 length
= descriptor
->length
= be16_to_cpu(descriptor
->length
);
1214 switch (descriptor
->dc
) {
1215 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1216 case CAPACITY_UNFORMATTED
:
1217 if (!test_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
))
1219 * If it is not a clik drive, break out
1220 * (maintains previous driver behaviour)
1223 case CAPACITY_CURRENT
:
1224 /* Normal Zip/LS-120 disks */
1225 if (memcmp(descriptor
, &floppy
->capacity
, sizeof (idefloppy_capacity_descriptor_t
)))
1226 printk(KERN_INFO
"%s: %dkB, %d blocks, %d "
1227 "sector size\n", drive
->name
,
1228 blocks
* length
/ 1024, blocks
, length
);
1229 floppy
->capacity
= *descriptor
;
1230 if (!length
|| length
% 512) {
1231 printk(KERN_NOTICE
"%s: %d bytes block size "
1232 "not supported\n", drive
->name
, length
);
1234 floppy
->blocks
= blocks
;
1235 floppy
->block_size
= length
;
1236 if ((floppy
->bs_factor
= length
/ 512) != 1)
1237 printk(KERN_NOTICE
"%s: warning: non "
1238 "512 bytes block size not "
1239 "fully supported\n",
1244 case CAPACITY_NO_CARTRIDGE
:
1246 * This is a KERN_ERR so it appears on screen
1247 * for the user to see
1249 printk(KERN_ERR
"%s: No disk in drive\n", drive
->name
);
1251 case CAPACITY_INVALID
:
1252 printk(KERN_ERR
"%s: Invalid capacity for disk "
1253 "in drive\n", drive
->name
);
1258 debug_log("Descriptor 0 Code: %d\n", descriptor
->dc
);
1260 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
1261 i
, blocks
* length
/ 1024, blocks
, length
);
1264 /* Clik! disk does not support get_flexible_disk_page */
1265 if (!test_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
)) {
1266 (void) idefloppy_get_flexible_disk_page(drive
);
1269 set_capacity(floppy
->disk
, floppy
->blocks
* floppy
->bs_factor
);
1274 ** Obtain the list of formattable capacities.
1275 ** Very similar to idefloppy_get_capacity, except that we push the capacity
1276 ** descriptors to userland, instead of our own structures.
1278 ** Userland gives us the following structure:
1280 ** struct idefloppy_format_capacities {
1288 ** userland initializes nformats to the number of allocated formats[]
1289 ** records. On exit we set nformats to the number of records we've
1290 ** actually initialized.
1294 static int idefloppy_get_format_capacities(ide_drive_t
*drive
, int __user
*arg
)
1297 idefloppy_capacity_header_t
*header
;
1298 idefloppy_capacity_descriptor_t
*descriptor
;
1299 int i
, descriptors
, blocks
, length
;
1304 if (get_user(u_array_size
, arg
))
1307 if (u_array_size
<= 0)
1310 idefloppy_create_read_capacity_cmd(&pc
);
1311 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1312 printk(KERN_ERR
"ide-floppy: Can't get floppy parameters\n");
1315 header
= (idefloppy_capacity_header_t
*) pc
.buffer
;
1316 descriptors
= header
->length
/
1317 sizeof(idefloppy_capacity_descriptor_t
);
1318 descriptor
= (idefloppy_capacity_descriptor_t
*) (header
+ 1);
1324 ** We always skip the first capacity descriptor. That's the
1325 ** current capacity. We are interested in the remaining descriptors,
1326 ** the formattable capacities.
1329 for (i
=0; i
<descriptors
; i
++, descriptor
++) {
1330 if (u_index
>= u_array_size
)
1331 break; /* User-supplied buffer too small */
1333 continue; /* Skip the first descriptor */
1335 blocks
= be32_to_cpu(descriptor
->blocks
);
1336 length
= be16_to_cpu(descriptor
->length
);
1338 if (put_user(blocks
, argp
))
1342 if (put_user(length
, argp
))
1349 if (put_user(u_index
, arg
))
1355 ** Send ATAPI_FORMAT_UNIT to the drive.
1357 ** Userland gives us the following structure:
1359 ** struct idefloppy_format_command {
1365 ** flags is a bitmask, currently, the only defined flag is:
1367 ** 0x01 - verify media after format.
1370 static int idefloppy_begin_format(ide_drive_t
*drive
, int __user
*arg
)
1377 if (get_user(blocks
, arg
) ||
1378 get_user(length
, arg
+1) ||
1379 get_user(flags
, arg
+2)) {
1383 (void) idefloppy_get_sfrp_bit(drive
);
1384 idefloppy_create_format_unit_cmd(&pc
, blocks
, length
, flags
);
1385 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1393 ** Get ATAPI_FORMAT_UNIT progress indication.
1395 ** Userland gives a pointer to an int. The int is set to a progress
1396 ** indicator 0-65536, with 65536=100%.
1398 ** If the drive does not support format progress indication, we just check
1399 ** the dsc bit, and return either 0 or 65536.
1402 static int idefloppy_get_format_progress(ide_drive_t
*drive
, int __user
*arg
)
1404 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1406 int progress_indication
= 0x10000;
1409 idefloppy_create_request_sense_cmd(&pc
);
1410 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1414 if (floppy
->sense_key
== 2 &&
1416 floppy
->ascq
== 4) {
1417 progress_indication
= floppy
->progress_indication
;
1419 /* Else assume format_unit has finished, and we're
1422 unsigned long flags
;
1425 local_irq_save(flags
);
1426 stat
= drive
->hwif
->INB(IDE_STATUS_REG
);
1427 local_irq_restore(flags
);
1429 progress_indication
= ((stat
& SEEK_STAT
) == 0) ? 0 : 0x10000;
1431 if (put_user(progress_indication
, arg
))
1438 * Return the current floppy capacity.
1440 static sector_t
idefloppy_capacity (ide_drive_t
*drive
)
1442 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1443 unsigned long capacity
= floppy
->blocks
* floppy
->bs_factor
;
1449 * idefloppy_identify_device checks if we can support a drive,
1450 * based on the ATAPI IDENTIFY command results.
1452 static int idefloppy_identify_device (ide_drive_t
*drive
,struct hd_driveid
*id
)
1454 struct idefloppy_id_gcw gcw
;
1455 #if IDEFLOPPY_DEBUG_INFO
1457 #endif /* IDEFLOPPY_DEBUG_INFO */
1459 *((u16
*) &gcw
) = id
->config
;
1462 /* kludge for Apple PowerBook internal zip */
1463 if ((gcw
.device_type
== 5) &&
1464 !strstr(id
->model
, "CD-ROM") &&
1465 strstr(id
->model
, "ZIP"))
1466 gcw
.device_type
= 0;
1469 #if IDEFLOPPY_DEBUG_INFO
1470 printk(KERN_INFO
"Dumping ATAPI Identify Device floppy parameters\n");
1471 switch (gcw
.protocol
) {
1472 case 0: case 1: sprintf(buffer
, "ATA");break;
1473 case 2: sprintf(buffer
, "ATAPI");break;
1474 case 3: sprintf(buffer
, "Reserved (Unknown to ide-floppy)");break;
1476 printk(KERN_INFO
"Protocol Type: %s\n", buffer
);
1477 switch (gcw
.device_type
) {
1478 case 0: sprintf(buffer
, "Direct-access Device");break;
1479 case 1: sprintf(buffer
, "Streaming Tape Device");break;
1480 case 2: case 3: case 4: sprintf (buffer
, "Reserved");break;
1481 case 5: sprintf(buffer
, "CD-ROM Device");break;
1482 case 6: sprintf(buffer
, "Reserved");
1483 case 7: sprintf(buffer
, "Optical memory Device");break;
1484 case 0x1f: sprintf(buffer
, "Unknown or no Device type");break;
1485 default: sprintf(buffer
, "Reserved");
1487 printk(KERN_INFO
"Device Type: %x - %s\n", gcw
.device_type
, buffer
);
1488 printk(KERN_INFO
"Removable: %s\n",gcw
.removable
? "Yes":"No");
1489 switch (gcw
.drq_type
) {
1490 case 0: sprintf(buffer
, "Microprocessor DRQ");break;
1491 case 1: sprintf(buffer
, "Interrupt DRQ");break;
1492 case 2: sprintf(buffer
, "Accelerated DRQ");break;
1493 case 3: sprintf(buffer
, "Reserved");break;
1495 printk(KERN_INFO
"Command Packet DRQ Type: %s\n", buffer
);
1496 switch (gcw
.packet_size
) {
1497 case 0: sprintf(buffer
, "12 bytes");break;
1498 case 1: sprintf(buffer
, "16 bytes");break;
1499 default: sprintf(buffer
, "Reserved");break;
1501 printk(KERN_INFO
"Command Packet Size: %s\n", buffer
);
1502 #endif /* IDEFLOPPY_DEBUG_INFO */
1504 if (gcw
.protocol
!= 2)
1505 printk(KERN_ERR
"ide-floppy: Protocol is not ATAPI\n");
1506 else if (gcw
.device_type
!= 0)
1507 printk(KERN_ERR
"ide-floppy: Device type is not set to floppy\n");
1508 else if (!gcw
.removable
)
1509 printk(KERN_ERR
"ide-floppy: The removable flag is not set\n");
1510 else if (gcw
.drq_type
== 3) {
1511 printk(KERN_ERR
"ide-floppy: Sorry, DRQ type %d not supported\n", gcw
.drq_type
);
1512 } else if (gcw
.packet_size
!= 0) {
1513 printk(KERN_ERR
"ide-floppy: Packet size is not 12 bytes long\n");
1519 #ifdef CONFIG_IDE_PROC_FS
1520 static void idefloppy_add_settings(ide_drive_t
*drive
)
1522 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1525 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
1527 ide_add_setting(drive
, "bios_cyl", SETTING_RW
, TYPE_INT
, 0, 1023, 1, 1, &drive
->bios_cyl
, NULL
);
1528 ide_add_setting(drive
, "bios_head", SETTING_RW
, TYPE_BYTE
, 0, 255, 1, 1, &drive
->bios_head
, NULL
);
1529 ide_add_setting(drive
, "bios_sect", SETTING_RW
, TYPE_BYTE
, 0, 63, 1, 1, &drive
->bios_sect
, NULL
);
1530 ide_add_setting(drive
, "ticks", SETTING_RW
, TYPE_BYTE
, 0, 255, 1, 1, &floppy
->ticks
, NULL
);
1533 static inline void idefloppy_add_settings(ide_drive_t
*drive
) { ; }
1537 * Driver initialization.
1539 static void idefloppy_setup (ide_drive_t
*drive
, idefloppy_floppy_t
*floppy
)
1541 struct idefloppy_id_gcw gcw
;
1543 *((u16
*) &gcw
) = drive
->id
->config
;
1544 floppy
->pc
= floppy
->pc_stack
;
1545 if (gcw
.drq_type
== 1)
1546 set_bit(IDEFLOPPY_DRQ_INTERRUPT
, &floppy
->flags
);
1548 * We used to check revisions here. At this point however
1549 * I'm giving up. Just assume they are all broken, its easier.
1551 * The actual reason for the workarounds was likely
1552 * a driver bug after all rather than a firmware bug,
1553 * and the workaround below used to hide it. It should
1554 * be fixed as of version 1.9, but to be on the safe side
1555 * we'll leave the limitation below for the 2.2.x tree.
1558 if (!strncmp(drive
->id
->model
, "IOMEGA ZIP 100 ATAPI", 20)) {
1559 set_bit(IDEFLOPPY_ZIP_DRIVE
, &floppy
->flags
);
1560 /* This value will be visible in the /proc/ide/hdx/settings */
1561 floppy
->ticks
= IDEFLOPPY_TICKS_DELAY
;
1562 blk_queue_max_sectors(drive
->queue
, 64);
1566 * Guess what? The IOMEGA Clik! drive also needs the
1567 * above fix. It makes nasty clicking noises without
1568 * it, so please don't remove this.
1570 if (strncmp(drive
->id
->model
, "IOMEGA Clik!", 11) == 0) {
1571 blk_queue_max_sectors(drive
->queue
, 64);
1572 set_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
);
1576 (void) idefloppy_get_capacity(drive
);
1577 idefloppy_add_settings(drive
);
1580 static void ide_floppy_remove(ide_drive_t
*drive
)
1582 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1583 struct gendisk
*g
= floppy
->disk
;
1585 ide_proc_unregister_driver(drive
, floppy
->driver
);
1589 ide_floppy_put(floppy
);
1592 static void idefloppy_cleanup_obj(struct kref
*kref
)
1594 struct ide_floppy_obj
*floppy
= to_ide_floppy(kref
);
1595 ide_drive_t
*drive
= floppy
->drive
;
1596 struct gendisk
*g
= floppy
->disk
;
1598 drive
->driver_data
= NULL
;
1599 g
->private_data
= NULL
;
1604 #ifdef CONFIG_IDE_PROC_FS
1605 static int proc_idefloppy_read_capacity
1606 (char *page
, char **start
, off_t off
, int count
, int *eof
, void *data
)
1608 ide_drive_t
*drive
= (ide_drive_t
*)data
;
1611 len
= sprintf(page
,"%llu\n", (long long)idefloppy_capacity(drive
));
1612 PROC_IDE_READ_RETURN(page
,start
,off
,count
,eof
,len
);
1615 static ide_proc_entry_t idefloppy_proc
[] = {
1616 { "capacity", S_IFREG
|S_IRUGO
, proc_idefloppy_read_capacity
, NULL
},
1617 { "geometry", S_IFREG
|S_IRUGO
, proc_ide_read_geometry
, NULL
},
1618 { NULL
, 0, NULL
, NULL
}
1620 #endif /* CONFIG_IDE_PROC_FS */
1622 static int ide_floppy_probe(ide_drive_t
*);
1624 static ide_driver_t idefloppy_driver
= {
1626 .owner
= THIS_MODULE
,
1627 .name
= "ide-floppy",
1628 .bus
= &ide_bus_type
,
1630 .probe
= ide_floppy_probe
,
1631 .remove
= ide_floppy_remove
,
1632 .version
= IDEFLOPPY_VERSION
,
1633 .media
= ide_floppy
,
1634 .supports_dsc_overlap
= 0,
1635 .do_request
= idefloppy_do_request
,
1636 .end_request
= idefloppy_do_end_request
,
1637 .error
= __ide_error
,
1638 .abort
= __ide_abort
,
1639 #ifdef CONFIG_IDE_PROC_FS
1640 .proc
= idefloppy_proc
,
1644 static int idefloppy_open(struct inode
*inode
, struct file
*filp
)
1646 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
1647 struct ide_floppy_obj
*floppy
;
1652 debug_log("Reached %s\n", __func__
);
1654 if (!(floppy
= ide_floppy_get(disk
)))
1657 drive
= floppy
->drive
;
1661 if (floppy
->openers
== 1) {
1662 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
, &floppy
->flags
);
1665 idefloppy_create_test_unit_ready_cmd(&pc
);
1666 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1667 idefloppy_create_start_stop_cmd(&pc
, 1);
1668 (void) idefloppy_queue_pc_tail(drive
, &pc
);
1671 if (idefloppy_get_capacity (drive
)
1672 && (filp
->f_flags
& O_NDELAY
) == 0
1674 ** Allow O_NDELAY to open a drive without a disk, or with
1675 ** an unreadable disk, so that we can get the format
1676 ** capacity of the drive or begin the format - Sam
1680 goto out_put_floppy
;
1683 if (floppy
->wp
&& (filp
->f_mode
& 2)) {
1685 goto out_put_floppy
;
1687 set_bit(IDEFLOPPY_MEDIA_CHANGED
, &floppy
->flags
);
1688 /* IOMEGA Clik! drives do not support lock/unlock commands */
1689 if (!test_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
)) {
1690 idefloppy_create_prevent_cmd(&pc
, 1);
1691 (void) idefloppy_queue_pc_tail(drive
, &pc
);
1693 check_disk_change(inode
->i_bdev
);
1694 } else if (test_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
, &floppy
->flags
)) {
1696 goto out_put_floppy
;
1702 ide_floppy_put(floppy
);
1706 static int idefloppy_release(struct inode
*inode
, struct file
*filp
)
1708 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
1709 struct ide_floppy_obj
*floppy
= ide_floppy_g(disk
);
1710 ide_drive_t
*drive
= floppy
->drive
;
1713 debug_log("Reached %s\n", __func__
);
1715 if (floppy
->openers
== 1) {
1716 /* IOMEGA Clik! drives do not support lock/unlock commands */
1717 if (!test_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
)) {
1718 idefloppy_create_prevent_cmd(&pc
, 0);
1719 (void) idefloppy_queue_pc_tail(drive
, &pc
);
1722 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
, &floppy
->flags
);
1727 ide_floppy_put(floppy
);
1732 static int idefloppy_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1734 struct ide_floppy_obj
*floppy
= ide_floppy_g(bdev
->bd_disk
);
1735 ide_drive_t
*drive
= floppy
->drive
;
1737 geo
->heads
= drive
->bios_head
;
1738 geo
->sectors
= drive
->bios_sect
;
1739 geo
->cylinders
= (u16
)drive
->bios_cyl
; /* truncate */
1743 static int idefloppy_ioctl(struct inode
*inode
, struct file
*file
,
1744 unsigned int cmd
, unsigned long arg
)
1746 struct block_device
*bdev
= inode
->i_bdev
;
1747 struct ide_floppy_obj
*floppy
= ide_floppy_g(bdev
->bd_disk
);
1748 ide_drive_t
*drive
= floppy
->drive
;
1749 void __user
*argp
= (void __user
*)arg
;
1751 int prevent
= (arg
) ? 1 : 0;
1758 case CDROM_LOCKDOOR
:
1759 if (floppy
->openers
> 1)
1762 /* The IOMEGA Clik! Drive doesn't support this command - no room for an eject mechanism */
1763 if (!test_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
)) {
1764 idefloppy_create_prevent_cmd(&pc
, prevent
);
1765 (void) idefloppy_queue_pc_tail(drive
, &pc
);
1767 if (cmd
== CDROMEJECT
) {
1768 idefloppy_create_start_stop_cmd(&pc
, 2);
1769 (void) idefloppy_queue_pc_tail(drive
, &pc
);
1772 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED
:
1774 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY
:
1775 return idefloppy_get_format_capacities(drive
, argp
);
1776 case IDEFLOPPY_IOCTL_FORMAT_START
:
1778 if (!(file
->f_mode
& 2))
1781 if (floppy
->openers
> 1) {
1782 /* Don't format if someone is using the disk */
1784 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
,
1789 set_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
, &floppy
->flags
);
1791 err
= idefloppy_begin_format(drive
, argp
);
1793 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
, &floppy
->flags
);
1796 ** Note, the bit will be cleared when the device is
1797 ** closed. This is the cleanest way to handle the
1798 ** situation where the drive does not support
1799 ** format progress reporting.
1801 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS
:
1802 return idefloppy_get_format_progress(drive
, argp
);
1806 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1807 * and CDROM_SEND_PACKET (legacy) ioctls
1809 if (cmd
!= CDROM_SEND_PACKET
&& cmd
!= SCSI_IOCTL_SEND_COMMAND
)
1810 err
= scsi_cmd_ioctl(file
, bdev
->bd_disk
->queue
,
1811 bdev
->bd_disk
, cmd
, argp
);
1816 err
= generic_ide_ioctl(drive
, file
, bdev
, cmd
, arg
);
1821 static int idefloppy_media_changed(struct gendisk
*disk
)
1823 struct ide_floppy_obj
*floppy
= ide_floppy_g(disk
);
1824 ide_drive_t
*drive
= floppy
->drive
;
1826 /* do not scan partitions twice if this is a removable device */
1827 if (drive
->attach
) {
1831 return test_and_clear_bit(IDEFLOPPY_MEDIA_CHANGED
, &floppy
->flags
);
1834 static int idefloppy_revalidate_disk(struct gendisk
*disk
)
1836 struct ide_floppy_obj
*floppy
= ide_floppy_g(disk
);
1837 set_capacity(disk
, idefloppy_capacity(floppy
->drive
));
1841 static struct block_device_operations idefloppy_ops
= {
1842 .owner
= THIS_MODULE
,
1843 .open
= idefloppy_open
,
1844 .release
= idefloppy_release
,
1845 .ioctl
= idefloppy_ioctl
,
1846 .getgeo
= idefloppy_getgeo
,
1847 .media_changed
= idefloppy_media_changed
,
1848 .revalidate_disk
= idefloppy_revalidate_disk
1851 static int ide_floppy_probe(ide_drive_t
*drive
)
1853 idefloppy_floppy_t
*floppy
;
1856 if (!strstr("ide-floppy", drive
->driver_req
))
1858 if (!drive
->present
)
1860 if (drive
->media
!= ide_floppy
)
1862 if (!idefloppy_identify_device (drive
, drive
->id
)) {
1863 printk (KERN_ERR
"ide-floppy: %s: not supported by this version of ide-floppy\n", drive
->name
);
1867 printk("ide-floppy: passing drive %s to ide-scsi emulation.\n", drive
->name
);
1870 if ((floppy
= kzalloc(sizeof (idefloppy_floppy_t
), GFP_KERNEL
)) == NULL
) {
1871 printk (KERN_ERR
"ide-floppy: %s: Can't allocate a floppy structure\n", drive
->name
);
1875 g
= alloc_disk(1 << PARTN_BITS
);
1877 goto out_free_floppy
;
1879 ide_init_disk(g
, drive
);
1881 ide_proc_register_driver(drive
, &idefloppy_driver
);
1883 kref_init(&floppy
->kref
);
1885 floppy
->drive
= drive
;
1886 floppy
->driver
= &idefloppy_driver
;
1889 g
->private_data
= &floppy
->driver
;
1891 drive
->driver_data
= floppy
;
1893 idefloppy_setup (drive
, floppy
);
1895 g
->minors
= 1 << PARTN_BITS
;
1896 g
->driverfs_dev
= &drive
->gendev
;
1897 g
->flags
= drive
->removable
? GENHD_FL_REMOVABLE
: 0;
1898 g
->fops
= &idefloppy_ops
;
1909 MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
1911 static void __exit
idefloppy_exit (void)
1913 driver_unregister(&idefloppy_driver
.gen_driver
);
1916 static int __init
idefloppy_init(void)
1918 printk("ide-floppy driver " IDEFLOPPY_VERSION
"\n");
1919 return driver_register(&idefloppy_driver
.gen_driver
);
1922 MODULE_ALIAS("ide:*m-floppy*");
1923 module_init(idefloppy_init
);
1924 module_exit(idefloppy_exit
);
1925 MODULE_LICENSE("GPL");