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>
50 /* define to see debug info */
51 #define IDEFLOPPY_DEBUG_LOG 0
53 /* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
54 #define IDEFLOPPY_DEBUG( fmt, args... )
56 #if IDEFLOPPY_DEBUG_LOG
57 #define debug_log(fmt, args...) \
58 printk(KERN_INFO "ide-floppy: " fmt, ## args)
60 #define debug_log(fmt, args... ) do {} while(0)
65 * Some drives require a longer irq timeout.
67 #define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
70 * After each failed packet command we issue a request sense command
71 * and retry the packet command IDEFLOPPY_MAX_PC_RETRIES times.
73 #define IDEFLOPPY_MAX_PC_RETRIES 3
76 * With each packet command, we allocate a buffer of
77 * IDEFLOPPY_PC_BUFFER_SIZE bytes.
79 #define IDEFLOPPY_PC_BUFFER_SIZE 256
82 * In various places in the driver, we need to allocate storage
83 * for packet commands and requests, which will remain valid while
84 * we leave the driver to wait for an interrupt or a timeout event.
86 #define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
89 * Our view of a packet command.
91 typedef struct idefloppy_packet_command_s
{
92 u8 c
[12]; /* Actual packet bytes */
93 int retries
; /* On each retry, we increment retries */
94 int error
; /* Error code */
95 int request_transfer
; /* Bytes to transfer */
96 int actually_transferred
; /* Bytes actually transferred */
97 int buffer_size
; /* Size of our data buffer */
98 int b_count
; /* Missing/Available data on the current buffer */
99 struct request
*rq
; /* The corresponding request */
100 u8
*buffer
; /* Data buffer */
101 u8
*current_position
; /* Pointer into the above buffer */
102 void (*callback
) (ide_drive_t
*); /* Called when this packet command is completed */
103 u8 pc_buffer
[IDEFLOPPY_PC_BUFFER_SIZE
]; /* Temporary buffer */
104 unsigned long flags
; /* Status/Action bit flags: long for set_bit */
108 * Packet command flag bits.
110 #define PC_ABORT 0 /* Set when an error is considered normal - We won't retry */
111 #define PC_DMA_RECOMMENDED 2 /* 1 when we prefer to use DMA if possible */
112 #define PC_DMA_IN_PROGRESS 3 /* 1 while DMA in progress */
113 #define PC_DMA_ERROR 4 /* 1 when encountered problem during DMA */
114 #define PC_WRITING 5 /* Data direction */
116 #define PC_SUPPRESS_ERROR 6 /* Suppress error reporting */
118 /* format capacities descriptor codes */
119 #define CAPACITY_INVALID 0x00
120 #define CAPACITY_UNFORMATTED 0x01
121 #define CAPACITY_CURRENT 0x02
122 #define CAPACITY_NO_CARTRIDGE 0x03
125 * Most of our global data which we need to save even as we leave the
126 * driver due to an interrupt or a timer event is stored in a variable
127 * of type idefloppy_floppy_t, defined below.
129 typedef struct ide_floppy_obj
{
131 ide_driver_t
*driver
;
132 struct gendisk
*disk
;
134 unsigned int openers
; /* protected by BKL for now */
136 /* Current packet command */
138 /* Last failed packet command */
139 idefloppy_pc_t
*failed_pc
;
140 /* Packet command stack */
141 idefloppy_pc_t pc_stack
[IDEFLOPPY_PC_STACK
];
142 /* Next free packet command storage space */
144 struct request rq_stack
[IDEFLOPPY_PC_STACK
];
145 /* We implement a circular array */
149 * Last error information
151 u8 sense_key
, asc
, ascq
;
152 /* delay this long before sending packet command */
154 int progress_indication
;
160 int blocks
, block_size
, bs_factor
;
161 /* Last format capacity descriptor */
163 /* Copy of the flexible disk page */
164 u8 flexible_disk_page
[32];
167 /* Supports format progress report */
169 /* Status/Action flags */
171 } idefloppy_floppy_t
;
173 #define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
176 * Floppy flag bits values.
178 #define IDEFLOPPY_DRQ_INTERRUPT 0 /* DRQ interrupt device */
179 #define IDEFLOPPY_MEDIA_CHANGED 1 /* Media may have changed */
180 #define IDEFLOPPY_FORMAT_IN_PROGRESS 3 /* Format in progress */
181 #define IDEFLOPPY_CLIK_DRIVE 4 /* Avoid commands not supported in Clik drive */
182 #define IDEFLOPPY_ZIP_DRIVE 5 /* Requires BH algorithm for packets */
185 * Defines for the mode sense command
187 #define MODE_SENSE_CURRENT 0x00
188 #define MODE_SENSE_CHANGEABLE 0x01
189 #define MODE_SENSE_DEFAULT 0x02
190 #define MODE_SENSE_SAVED 0x03
193 * IOCTLs used in low-level formatting.
196 #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
197 #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
198 #define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
199 #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
202 * Error codes which are returned in rq->errors to the higher part
205 #define IDEFLOPPY_ERROR_GENERAL 101
208 * The following is used to format the general configuration word of
209 * the ATAPI IDENTIFY DEVICE command.
211 struct idefloppy_id_gcw
{
212 #if defined(__LITTLE_ENDIAN_BITFIELD)
213 unsigned packet_size
:2; /* Packet Size */
214 unsigned reserved234
:3; /* Reserved */
215 unsigned drq_type
:2; /* Command packet DRQ type */
216 unsigned removable
:1; /* Removable media */
217 unsigned device_type
:5; /* Device type */
218 unsigned reserved13
:1; /* Reserved */
219 unsigned protocol
:2; /* Protocol type */
220 #elif defined(__BIG_ENDIAN_BITFIELD)
221 unsigned protocol
:2; /* Protocol type */
222 unsigned reserved13
:1; /* Reserved */
223 unsigned device_type
:5; /* Device type */
224 unsigned removable
:1; /* Removable media */
225 unsigned drq_type
:2; /* Command packet DRQ type */
226 unsigned reserved234
:3; /* Reserved */
227 unsigned packet_size
:2; /* Packet Size */
229 #error "Bitfield endianness not defined! Check your byteorder.h"
234 * Pages of the SELECT SENSE / MODE SENSE packet commands.
235 * See SFF-8070i spec.
237 #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
238 #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
240 static DEFINE_MUTEX(idefloppy_ref_mutex
);
242 #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
244 #define ide_floppy_g(disk) \
245 container_of((disk)->private_data, struct ide_floppy_obj, driver)
247 static struct ide_floppy_obj
*ide_floppy_get(struct gendisk
*disk
)
249 struct ide_floppy_obj
*floppy
= NULL
;
251 mutex_lock(&idefloppy_ref_mutex
);
252 floppy
= ide_floppy_g(disk
);
254 kref_get(&floppy
->kref
);
255 mutex_unlock(&idefloppy_ref_mutex
);
259 static void idefloppy_cleanup_obj(struct kref
*);
261 static void ide_floppy_put(struct ide_floppy_obj
*floppy
)
263 mutex_lock(&idefloppy_ref_mutex
);
264 kref_put(&floppy
->kref
, idefloppy_cleanup_obj
);
265 mutex_unlock(&idefloppy_ref_mutex
);
269 * Too bad. The drive wants to send us data which we are not ready to accept.
270 * Just throw it away.
272 static void idefloppy_discard_data (ide_drive_t
*drive
, unsigned int bcount
)
275 (void) HWIF(drive
)->INB(IDE_DATA_REG
);
278 static void idefloppy_write_zeros(ide_drive_t
*drive
, unsigned int bcount
)
281 HWIF(drive
)->OUTB(0, IDE_DATA_REG
);
286 * idefloppy_do_end_request is used to finish servicing a request.
288 * For read/write requests, we will call ide_end_request to pass to the
291 static int idefloppy_do_end_request(ide_drive_t
*drive
, int uptodate
, int nsecs
)
293 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
294 struct request
*rq
= HWGROUP(drive
)->rq
;
297 debug_log("Reached %s\n", __func__
);
300 case 0: error
= IDEFLOPPY_ERROR_GENERAL
; break;
301 case 1: error
= 0; break;
302 default: error
= uptodate
;
305 floppy
->failed_pc
= NULL
;
306 /* Why does this happen? */
309 if (!blk_special_request(rq
)) {
310 /* our real local end request function */
311 ide_end_request(drive
, uptodate
, nsecs
);
315 /* fixme: need to move this local also */
316 ide_end_drive_cmd(drive
, 0, 0);
320 static void ide_floppy_io_buffers(ide_drive_t
*drive
, idefloppy_pc_t
*pc
,
321 unsigned int bcount
, int direction
)
323 struct request
*rq
= pc
->rq
;
324 struct req_iterator iter
;
325 struct bio_vec
*bvec
;
330 rq_for_each_segment(bvec
, rq
, iter
) {
334 count
= min(bvec
->bv_len
, bcount
);
336 data
= bvec_kmap_irq(bvec
, &flags
);
338 drive
->hwif
->atapi_output_bytes(drive
, data
, count
);
340 drive
->hwif
->atapi_input_bytes(drive
, data
, count
);
341 bvec_kunmap_irq(data
, &flags
);
344 pc
->b_count
+= count
;
348 idefloppy_do_end_request(drive
, 1, done
>> 9);
351 printk(KERN_ERR
"%s: leftover data in %s, bcount == %d\n",
352 drive
->name
, __func__
, bcount
);
354 idefloppy_write_zeros(drive
, bcount
);
356 idefloppy_discard_data(drive
, bcount
);
361 static void idefloppy_update_buffers (ide_drive_t
*drive
, idefloppy_pc_t
*pc
)
363 struct request
*rq
= pc
->rq
;
364 struct bio
*bio
= rq
->bio
;
366 while ((bio
= rq
->bio
) != NULL
)
367 idefloppy_do_end_request(drive
, 1, 0);
371 * idefloppy_queue_pc_head generates a new packet command request in front
372 * of the request queue, before the current request, so that it will be
373 * processed immediately, on the next pass through the driver.
375 static void idefloppy_queue_pc_head (ide_drive_t
*drive
,idefloppy_pc_t
*pc
,struct request
*rq
)
377 struct ide_floppy_obj
*floppy
= drive
->driver_data
;
379 ide_init_drive_cmd(rq
);
380 rq
->buffer
= (char *) pc
;
381 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
382 rq
->rq_disk
= floppy
->disk
;
383 (void) ide_do_drive_cmd(drive
, rq
, ide_preempt
);
386 static idefloppy_pc_t
*idefloppy_next_pc_storage (ide_drive_t
*drive
)
388 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
390 if (floppy
->pc_stack_index
== IDEFLOPPY_PC_STACK
)
391 floppy
->pc_stack_index
=0;
392 return (&floppy
->pc_stack
[floppy
->pc_stack_index
++]);
395 static struct request
*idefloppy_next_rq_storage (ide_drive_t
*drive
)
397 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
399 if (floppy
->rq_stack_index
== IDEFLOPPY_PC_STACK
)
400 floppy
->rq_stack_index
= 0;
401 return (&floppy
->rq_stack
[floppy
->rq_stack_index
++]);
404 static void idefloppy_request_sense_callback(ide_drive_t
*drive
)
406 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
407 u8
*buf
= floppy
->pc
->buffer
;
409 debug_log("Reached %s\n", __func__
);
411 if (!floppy
->pc
->error
) {
412 floppy
->sense_key
= buf
[2] & 0x0F;
413 floppy
->asc
= buf
[12];
414 floppy
->ascq
= buf
[13];
415 floppy
->progress_indication
= buf
[15] & 0x80 ?
416 (u16
)get_unaligned((u16
*)&buf
[16]) : 0x10000;
418 if (floppy
->failed_pc
)
419 debug_log("pc = %x, sense key = %x, asc = %x,"
421 floppy
->failed_pc
->c
[0],
426 debug_log("sense key = %x, asc = %x, ascq = %x\n",
432 idefloppy_do_end_request(drive
, 1, 0);
434 printk(KERN_ERR
"Error in REQUEST SENSE itself - Aborting"
436 idefloppy_do_end_request(drive
, 0, 0);
441 * General packet command callback function.
443 static void idefloppy_pc_callback (ide_drive_t
*drive
)
445 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
447 debug_log("Reached %s\n", __func__
);
449 idefloppy_do_end_request(drive
, floppy
->pc
->error
? 0 : 1, 0);
453 * idefloppy_init_pc initializes a packet command.
455 static void idefloppy_init_pc (idefloppy_pc_t
*pc
)
457 memset(pc
->c
, 0, 12);
460 pc
->request_transfer
= 0;
461 pc
->buffer
= pc
->pc_buffer
;
462 pc
->buffer_size
= IDEFLOPPY_PC_BUFFER_SIZE
;
463 pc
->callback
= &idefloppy_pc_callback
;
466 static void idefloppy_create_request_sense_cmd (idefloppy_pc_t
*pc
)
468 idefloppy_init_pc(pc
);
469 pc
->c
[0] = GPCMD_REQUEST_SENSE
;
471 pc
->request_transfer
= 18;
472 pc
->callback
= &idefloppy_request_sense_callback
;
476 * idefloppy_retry_pc is called when an error was detected during the
477 * last packet command. We queue a request sense packet command in
478 * the head of the request list.
480 static void idefloppy_retry_pc (ide_drive_t
*drive
)
485 (void)drive
->hwif
->INB(IDE_ERROR_REG
);
486 pc
= idefloppy_next_pc_storage(drive
);
487 rq
= idefloppy_next_rq_storage(drive
);
488 idefloppy_create_request_sense_cmd(pc
);
489 idefloppy_queue_pc_head(drive
, pc
, rq
);
492 /* The usual interrupt handler called during a packet command. */
493 static ide_startstop_t
idefloppy_pc_intr (ide_drive_t
*drive
)
495 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
496 ide_hwif_t
*hwif
= drive
->hwif
;
497 idefloppy_pc_t
*pc
= floppy
->pc
;
498 struct request
*rq
= pc
->rq
;
499 xfer_func_t
*xferfunc
;
505 debug_log("Reached %s interrupt handler\n", __func__
);
507 if (test_bit(PC_DMA_IN_PROGRESS
, &pc
->flags
)) {
508 dma_error
= hwif
->ide_dma_end(drive
);
510 printk(KERN_ERR
"%s: DMA %s error\n", drive
->name
,
511 rq_data_dir(rq
) ? "write" : "read");
512 set_bit(PC_DMA_ERROR
, &pc
->flags
);
514 pc
->actually_transferred
= pc
->request_transfer
;
515 idefloppy_update_buffers(drive
, pc
);
517 debug_log("DMA finished\n");
520 /* Clear the interrupt */
521 stat
= drive
->hwif
->INB(IDE_STATUS_REG
);
523 if ((stat
& DRQ_STAT
) == 0) { /* No more interrupts */
524 debug_log("Packet command completed, %d bytes transferred\n",
525 pc
->actually_transferred
);
526 clear_bit(PC_DMA_IN_PROGRESS
, &pc
->flags
);
528 local_irq_enable_in_hardirq();
530 if ((stat
& ERR_STAT
) || test_bit(PC_DMA_ERROR
, &pc
->flags
)) {
532 debug_log("%s: I/O error\n", drive
->name
);
534 if (pc
->c
[0] == GPCMD_REQUEST_SENSE
) {
535 printk(KERN_ERR
"ide-floppy: I/O error in "
536 "request sense command\n");
537 return ide_do_reset(drive
);
539 /* Retry operation */
540 idefloppy_retry_pc(drive
);
541 /* queued, but not started */
545 if (floppy
->failed_pc
== pc
)
546 floppy
->failed_pc
= NULL
;
547 /* Command finished - Call the callback function */
552 if (test_and_clear_bit(PC_DMA_IN_PROGRESS
, &pc
->flags
)) {
553 printk(KERN_ERR
"ide-floppy: The floppy wants to issue "
554 "more interrupts in DMA mode\n");
556 return ide_do_reset(drive
);
559 /* Get the number of bytes to transfer */
560 bcount
= (hwif
->INB(IDE_BCOUNTH_REG
) << 8) |
561 hwif
->INB(IDE_BCOUNTL_REG
);
562 /* on this interrupt */
563 ireason
= hwif
->INB(IDE_IREASON_REG
);
566 printk(KERN_ERR
"ide-floppy: CoD != 0 in %s\n", __func__
);
567 return ide_do_reset(drive
);
569 if (((ireason
& IO
) == IO
) == test_bit(PC_WRITING
, &pc
->flags
)) {
570 /* Hopefully, we will never get here */
571 printk(KERN_ERR
"ide-floppy: We wanted to %s, ",
572 (ireason
& IO
) ? "Write" : "Read");
573 printk(KERN_ERR
"but the floppy wants us to %s !\n",
574 (ireason
& IO
) ? "Read" : "Write");
575 return ide_do_reset(drive
);
577 if (!test_bit(PC_WRITING
, &pc
->flags
)) {
578 /* Reading - Check that we have enough space */
579 temp
= pc
->actually_transferred
+ bcount
;
580 if (temp
> pc
->request_transfer
) {
581 if (temp
> pc
->buffer_size
) {
582 printk(KERN_ERR
"ide-floppy: The floppy wants "
583 "to send us more data than expected "
584 "- discarding data\n");
585 idefloppy_discard_data(drive
, bcount
);
587 ide_set_handler(drive
,
593 debug_log("The floppy wants to send us more data than"
594 " expected - allowing transfer\n");
597 if (test_bit(PC_WRITING
, &pc
->flags
))
598 xferfunc
= hwif
->atapi_output_bytes
;
600 xferfunc
= hwif
->atapi_input_bytes
;
603 xferfunc(drive
, pc
->current_position
, bcount
);
605 ide_floppy_io_buffers(drive
, pc
, bcount
,
606 test_bit(PC_WRITING
, &pc
->flags
));
608 /* Update the current position */
609 pc
->actually_transferred
+= bcount
;
610 pc
->current_position
+= bcount
;
612 /* And set the interrupt handler again */
613 ide_set_handler(drive
, &idefloppy_pc_intr
, IDEFLOPPY_WAIT_CMD
, NULL
);
618 * This is the original routine that did the packet transfer.
619 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
620 * for that drive below. The algorithm is chosen based on drive type
622 static ide_startstop_t
idefloppy_transfer_pc (ide_drive_t
*drive
)
624 ide_startstop_t startstop
;
625 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
628 if (ide_wait_stat(&startstop
, drive
, DRQ_STAT
, BUSY_STAT
, WAIT_READY
)) {
629 printk(KERN_ERR
"ide-floppy: Strange, packet command "
630 "initiated yet DRQ isn't asserted\n");
633 ireason
= drive
->hwif
->INB(IDE_IREASON_REG
);
634 if ((ireason
& CD
) == 0 || (ireason
& IO
)) {
635 printk(KERN_ERR
"ide-floppy: (IO,CoD) != (0,1) while "
636 "issuing a packet command\n");
637 return ide_do_reset(drive
);
640 /* Set the interrupt routine */
641 ide_set_handler(drive
, &idefloppy_pc_intr
, IDEFLOPPY_WAIT_CMD
, NULL
);
642 /* Send the actual packet */
643 HWIF(drive
)->atapi_output_bytes(drive
, floppy
->pc
->c
, 12);
649 * What we have here is a classic case of a top half / bottom half
650 * interrupt service routine. In interrupt mode, the device sends
651 * an interrupt to signal it's ready to receive a packet. However,
652 * we need to delay about 2-3 ticks before issuing the packet or we
655 * So, follow carefully. transfer_pc1 is called as an interrupt (or
656 * directly). In either case, when the device says it's ready for a
657 * packet, we schedule the packet transfer to occur about 2-3 ticks
658 * later in transfer_pc2.
660 static int idefloppy_transfer_pc2 (ide_drive_t
*drive
)
662 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
664 /* Send the actual packet */
665 HWIF(drive
)->atapi_output_bytes(drive
, floppy
->pc
->c
, 12);
666 /* Timeout for the packet command */
667 return IDEFLOPPY_WAIT_CMD
;
670 static ide_startstop_t
idefloppy_transfer_pc1 (ide_drive_t
*drive
)
672 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
673 ide_startstop_t startstop
;
676 if (ide_wait_stat(&startstop
, drive
, DRQ_STAT
, BUSY_STAT
, WAIT_READY
)) {
677 printk(KERN_ERR
"ide-floppy: Strange, packet command "
678 "initiated yet DRQ isn't asserted\n");
681 ireason
= drive
->hwif
->INB(IDE_IREASON_REG
);
682 if ((ireason
& CD
) == 0 || (ireason
& IO
)) {
683 printk(KERN_ERR
"ide-floppy: (IO,CoD) != (0,1) "
684 "while issuing a packet command\n");
685 return ide_do_reset(drive
);
688 * The following delay solves a problem with ATAPI Zip 100 drives
689 * where the Busy flag was apparently being deasserted before the
690 * unit was ready to receive data. This was happening on a
691 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
692 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
693 * used until after the packet is moved in about 50 msec.
696 ide_set_handler(drive
,
697 &idefloppy_pc_intr
, /* service routine for packet command */
698 floppy
->ticks
, /* wait this long before "failing" */
699 &idefloppy_transfer_pc2
); /* fail == transfer_pc2 */
703 static void ide_floppy_report_error(idefloppy_floppy_t
*floppy
,
706 /* supress error messages resulting from Medium not present */
707 if (floppy
->sense_key
== 0x02 &&
708 floppy
->asc
== 0x3a &&
709 floppy
->ascq
== 0x00)
712 printk(KERN_ERR
"ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
713 "asc = %2x, ascq = %2x\n",
714 floppy
->drive
->name
, pc
->c
[0], floppy
->sense_key
,
715 floppy
->asc
, floppy
->ascq
);
720 * Issue a packet command
722 static ide_startstop_t
idefloppy_issue_pc (ide_drive_t
*drive
, idefloppy_pc_t
*pc
)
724 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
725 ide_hwif_t
*hwif
= drive
->hwif
;
726 ide_handler_t
*pkt_xfer_routine
;
730 if (floppy
->failed_pc
== NULL
&&
731 pc
->c
[0] != GPCMD_REQUEST_SENSE
)
732 floppy
->failed_pc
= pc
;
733 /* Set the current packet command */
736 if (pc
->retries
> IDEFLOPPY_MAX_PC_RETRIES
||
737 test_bit(PC_ABORT
, &pc
->flags
)) {
739 * We will "abort" retrying a packet command in case
740 * a legitimate error code was received.
742 if (!test_bit(PC_ABORT
, &pc
->flags
)) {
743 if (!test_bit(PC_SUPPRESS_ERROR
, &pc
->flags
))
744 ide_floppy_report_error(floppy
, pc
);
746 pc
->error
= IDEFLOPPY_ERROR_GENERAL
;
748 floppy
->failed_pc
= NULL
;
753 debug_log("Retry number - %d\n", pc
->retries
);
756 /* We haven't transferred any data yet */
757 pc
->actually_transferred
= 0;
758 pc
->current_position
= pc
->buffer
;
759 bcount
= min(pc
->request_transfer
, 63 * 1024);
761 if (test_and_clear_bit(PC_DMA_ERROR
, &pc
->flags
))
766 if (test_bit(PC_DMA_RECOMMENDED
, &pc
->flags
) && drive
->using_dma
)
767 dma
= !hwif
->dma_setup(drive
);
769 ide_pktcmd_tf_load(drive
, IDE_TFLAG_NO_SELECT_MASK
|
770 IDE_TFLAG_OUT_DEVICE
, bcount
, dma
);
772 if (dma
) { /* Begin DMA, if necessary */
773 set_bit(PC_DMA_IN_PROGRESS
, &pc
->flags
);
774 hwif
->dma_start(drive
);
777 /* Can we transfer the packet when we get the interrupt or wait? */
778 if (test_bit(IDEFLOPPY_ZIP_DRIVE
, &floppy
->flags
)) {
780 pkt_xfer_routine
= &idefloppy_transfer_pc1
;
783 pkt_xfer_routine
= &idefloppy_transfer_pc
;
786 if (test_bit (IDEFLOPPY_DRQ_INTERRUPT
, &floppy
->flags
)) {
787 /* Issue the packet command */
788 ide_execute_command(drive
, WIN_PACKETCMD
,
794 /* Issue the packet command */
795 HWIF(drive
)->OUTB(WIN_PACKETCMD
, IDE_COMMAND_REG
);
796 return (*pkt_xfer_routine
) (drive
);
800 static void idefloppy_rw_callback (ide_drive_t
*drive
)
802 debug_log("Reached %s\n", __func__
);
804 idefloppy_do_end_request(drive
, 1, 0);
808 static void idefloppy_create_prevent_cmd (idefloppy_pc_t
*pc
, int prevent
)
810 debug_log("creating prevent removal command, prevent = %d\n", prevent
);
812 idefloppy_init_pc(pc
);
813 pc
->c
[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL
;
817 static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t
*pc
)
819 idefloppy_init_pc(pc
);
820 pc
->c
[0] = GPCMD_READ_FORMAT_CAPACITIES
;
823 pc
->request_transfer
= 255;
826 static void idefloppy_create_format_unit_cmd (idefloppy_pc_t
*pc
, int b
, int l
,
829 idefloppy_init_pc(pc
);
830 pc
->c
[0] = GPCMD_FORMAT_UNIT
;
833 memset(pc
->buffer
, 0, 12);
834 pc
->buffer
[1] = 0xA2;
835 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
837 if (flags
& 1) /* Verify bit on... */
838 pc
->buffer
[1] ^= 0x20; /* ... turn off DCRT bit */
841 put_unaligned(cpu_to_be32(b
), (unsigned int *)(&pc
->buffer
[4]));
842 put_unaligned(cpu_to_be32(l
), (unsigned int *)(&pc
->buffer
[8]));
844 set_bit(PC_WRITING
, &pc
->flags
);
848 * A mode sense command is used to "sense" floppy parameters.
850 static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t
*pc
, u8 page_code
, u8 type
)
852 u16 length
= 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
854 idefloppy_init_pc(pc
);
855 pc
->c
[0] = GPCMD_MODE_SENSE_10
;
857 pc
->c
[2] = page_code
+ (type
<< 6);
860 case IDEFLOPPY_CAPABILITIES_PAGE
:
863 case IDEFLOPPY_FLEXIBLE_DISK_PAGE
:
867 printk(KERN_ERR
"ide-floppy: unsupported page code "
868 "in create_mode_sense_cmd\n");
870 put_unaligned(cpu_to_be16(length
), (u16
*) &pc
->c
[7]);
871 pc
->request_transfer
= length
;
874 static void idefloppy_create_start_stop_cmd (idefloppy_pc_t
*pc
, int start
)
876 idefloppy_init_pc(pc
);
877 pc
->c
[0] = GPCMD_START_STOP_UNIT
;
881 static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t
*pc
)
883 idefloppy_init_pc(pc
);
884 pc
->c
[0] = GPCMD_TEST_UNIT_READY
;
887 static void idefloppy_create_rw_cmd(idefloppy_floppy_t
*floppy
,
888 idefloppy_pc_t
*pc
, struct request
*rq
,
889 unsigned long sector
)
891 int block
= sector
/ floppy
->bs_factor
;
892 int blocks
= rq
->nr_sectors
/ floppy
->bs_factor
;
893 int cmd
= rq_data_dir(rq
);
895 debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
898 idefloppy_init_pc(pc
);
899 pc
->c
[0] = cmd
== READ
? GPCMD_READ_10
: GPCMD_WRITE_10
;
900 put_unaligned(cpu_to_be16(blocks
), (unsigned short *)&pc
->c
[7]);
901 put_unaligned(cpu_to_be32(block
), (unsigned int *) &pc
->c
[2]);
903 pc
->callback
= &idefloppy_rw_callback
;
905 pc
->b_count
= cmd
== READ
? 0 : rq
->bio
->bi_size
;
906 if (rq
->cmd_flags
& REQ_RW
)
907 set_bit(PC_WRITING
, &pc
->flags
);
909 pc
->request_transfer
= pc
->buffer_size
= blocks
* floppy
->block_size
;
910 set_bit(PC_DMA_RECOMMENDED
, &pc
->flags
);
914 idefloppy_blockpc_cmd(idefloppy_floppy_t
*floppy
, idefloppy_pc_t
*pc
, struct request
*rq
)
916 idefloppy_init_pc(pc
);
917 pc
->callback
= &idefloppy_rw_callback
;
918 memcpy(pc
->c
, rq
->cmd
, sizeof(pc
->c
));
920 pc
->b_count
= rq
->data_len
;
921 if (rq
->data_len
&& rq_data_dir(rq
) == WRITE
)
922 set_bit(PC_WRITING
, &pc
->flags
);
923 pc
->buffer
= rq
->data
;
925 set_bit(PC_DMA_RECOMMENDED
, &pc
->flags
);
928 * possibly problematic, doesn't look like ide-floppy correctly
929 * handled scattered requests if dma fails...
931 pc
->request_transfer
= pc
->buffer_size
= rq
->data_len
;
935 * idefloppy_do_request is our request handling function.
937 static ide_startstop_t
idefloppy_do_request (ide_drive_t
*drive
, struct request
*rq
, sector_t block_s
)
939 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
941 unsigned long block
= (unsigned long)block_s
;
943 debug_log("dev: %s, cmd_type: %x, errors: %d\n",
944 rq
->rq_disk
? rq
->rq_disk
->disk_name
: "?",
945 rq
->cmd_type
, rq
->errors
);
946 debug_log("sector: %ld, nr_sectors: %ld, "
947 "current_nr_sectors: %d\n", (long)rq
->sector
,
948 rq
->nr_sectors
, rq
->current_nr_sectors
);
950 if (rq
->errors
>= ERROR_MAX
) {
951 if (floppy
->failed_pc
)
952 ide_floppy_report_error(floppy
, floppy
->failed_pc
);
954 printk(KERN_ERR
"ide-floppy: %s: I/O error\n",
956 idefloppy_do_end_request(drive
, 0, 0);
959 if (blk_fs_request(rq
)) {
960 if (((long)rq
->sector
% floppy
->bs_factor
) ||
961 (rq
->nr_sectors
% floppy
->bs_factor
)) {
962 printk("%s: unsupported r/w request size\n",
964 idefloppy_do_end_request(drive
, 0, 0);
967 pc
= idefloppy_next_pc_storage(drive
);
968 idefloppy_create_rw_cmd(floppy
, pc
, rq
, block
);
969 } else if (blk_special_request(rq
)) {
970 pc
= (idefloppy_pc_t
*) rq
->buffer
;
971 } else if (blk_pc_request(rq
)) {
972 pc
= idefloppy_next_pc_storage(drive
);
973 idefloppy_blockpc_cmd(floppy
, pc
, rq
);
975 blk_dump_rq_flags(rq
,
976 "ide-floppy: unsupported command in queue");
977 idefloppy_do_end_request(drive
, 0, 0);
982 return idefloppy_issue_pc(drive
, pc
);
986 * idefloppy_queue_pc_tail adds a special packet command request to the
987 * tail of the request queue, and waits for it to be serviced.
989 static int idefloppy_queue_pc_tail (ide_drive_t
*drive
,idefloppy_pc_t
*pc
)
991 struct ide_floppy_obj
*floppy
= drive
->driver_data
;
994 ide_init_drive_cmd (&rq
);
995 rq
.buffer
= (char *) pc
;
996 rq
.cmd_type
= REQ_TYPE_SPECIAL
;
997 rq
.rq_disk
= floppy
->disk
;
999 return ide_do_drive_cmd(drive
, &rq
, ide_wait
);
1003 * Look at the flexible disk page parameters. We ignore the CHS capacity
1004 * parameters and use the LBA parameters instead.
1006 static int ide_floppy_get_flexible_disk_page(ide_drive_t
*drive
)
1008 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1011 int capacity
, lba_capacity
;
1012 u16 transfer_rate
, sector_size
, cyls
, rpm
;
1015 idefloppy_create_mode_sense_cmd(&pc
, IDEFLOPPY_FLEXIBLE_DISK_PAGE
,
1016 MODE_SENSE_CURRENT
);
1018 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1019 printk(KERN_ERR
"ide-floppy: Can't get flexible disk page"
1023 floppy
->wp
= !!(pc
.buffer
[3] & 0x80);
1024 set_disk_ro(floppy
->disk
, floppy
->wp
);
1025 page
= &pc
.buffer
[8];
1027 transfer_rate
= be16_to_cpu(*(u16
*)&pc
.buffer
[8 + 2]);
1028 sector_size
= be16_to_cpu(*(u16
*)&pc
.buffer
[8 + 6]);
1029 cyls
= be16_to_cpu(*(u16
*)&pc
.buffer
[8 + 8]);
1030 rpm
= be16_to_cpu(*(u16
*)&pc
.buffer
[8 + 28]);
1031 heads
= pc
.buffer
[8 + 4];
1032 sectors
= pc
.buffer
[8 + 5];
1034 capacity
= cyls
* heads
* sectors
* sector_size
;
1036 if (memcmp(page
, &floppy
->flexible_disk_page
, 32))
1037 printk(KERN_INFO
"%s: %dkB, %d/%d/%d CHS, %d kBps, "
1038 "%d sector size, %d rpm\n",
1039 drive
->name
, capacity
/ 1024, cyls
, heads
,
1040 sectors
, transfer_rate
/ 8, sector_size
, rpm
);
1042 memcpy(&floppy
->flexible_disk_page
, page
, 32);
1043 drive
->bios_cyl
= cyls
;
1044 drive
->bios_head
= heads
;
1045 drive
->bios_sect
= sectors
;
1046 lba_capacity
= floppy
->blocks
* floppy
->block_size
;
1048 if (capacity
< lba_capacity
) {
1049 printk(KERN_NOTICE
"%s: The disk reports a capacity of %d "
1050 "bytes, but the drive only handles %d\n",
1051 drive
->name
, lba_capacity
, capacity
);
1052 floppy
->blocks
= floppy
->block_size
?
1053 capacity
/ floppy
->block_size
: 0;
1058 static int idefloppy_get_sfrp_bit(ide_drive_t
*drive
)
1060 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1064 idefloppy_create_mode_sense_cmd(&pc
, IDEFLOPPY_CAPABILITIES_PAGE
,
1065 MODE_SENSE_CURRENT
);
1067 set_bit(PC_SUPPRESS_ERROR
, &pc
.flags
);
1068 if (idefloppy_queue_pc_tail(drive
, &pc
))
1071 floppy
->srfp
= pc
.buffer
[8 + 2] & 0x40;
1076 * Determine if a media is present in the floppy drive, and if so, its LBA
1079 static int ide_floppy_get_capacity(ide_drive_t
*drive
)
1081 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1084 u8 header_len
, desc_cnt
;
1085 int i
, rc
= 1, blocks
, length
;
1087 drive
->bios_cyl
= 0;
1088 drive
->bios_head
= drive
->bios_sect
= 0;
1090 floppy
->bs_factor
= 1;
1091 set_capacity(floppy
->disk
, 0);
1093 idefloppy_create_read_capacity_cmd(&pc
);
1094 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1095 printk(KERN_ERR
"ide-floppy: Can't get floppy parameters\n");
1098 header_len
= pc
.buffer
[3];
1099 cap_desc
= &pc
.buffer
[4];
1100 desc_cnt
= header_len
/ 8; /* capacity descriptor of 8 bytes */
1102 for (i
= 0; i
< desc_cnt
; i
++) {
1103 unsigned int desc_start
= 4 + i
*8;
1105 blocks
= be32_to_cpu(*(u32
*)&pc
.buffer
[desc_start
]);
1106 length
= be16_to_cpu(*(u16
*)&pc
.buffer
[desc_start
+ 6]);
1108 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
1109 i
, blocks
* length
/ 1024, blocks
, length
);
1114 * the code below is valid only for the 1st descriptor, ie i=0
1117 switch (pc
.buffer
[desc_start
+ 4] & 0x03) {
1118 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1119 case CAPACITY_UNFORMATTED
:
1120 if (!test_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
))
1122 * If it is not a clik drive, break out
1123 * (maintains previous driver behaviour)
1126 case CAPACITY_CURRENT
:
1127 /* Normal Zip/LS-120 disks */
1128 if (memcmp(cap_desc
, &floppy
->cap_desc
, 8))
1129 printk(KERN_INFO
"%s: %dkB, %d blocks, %d "
1130 "sector size\n", drive
->name
,
1131 blocks
* length
/ 1024, blocks
, length
);
1132 memcpy(&floppy
->cap_desc
, cap_desc
, 8);
1134 if (!length
|| length
% 512) {
1135 printk(KERN_NOTICE
"%s: %d bytes block size "
1136 "not supported\n", drive
->name
, length
);
1138 floppy
->blocks
= blocks
;
1139 floppy
->block_size
= length
;
1140 floppy
->bs_factor
= length
/ 512;
1141 if (floppy
->bs_factor
!= 1)
1142 printk(KERN_NOTICE
"%s: warning: non "
1143 "512 bytes block size not "
1144 "fully supported\n",
1149 case CAPACITY_NO_CARTRIDGE
:
1151 * This is a KERN_ERR so it appears on screen
1152 * for the user to see
1154 printk(KERN_ERR
"%s: No disk in drive\n", drive
->name
);
1156 case CAPACITY_INVALID
:
1157 printk(KERN_ERR
"%s: Invalid capacity for disk "
1158 "in drive\n", drive
->name
);
1161 debug_log("Descriptor 0 Code: %d\n",
1162 pc
.buffer
[desc_start
+ 4] & 0x03);
1165 /* Clik! disk does not support get_flexible_disk_page */
1166 if (!test_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
))
1167 (void) ide_floppy_get_flexible_disk_page(drive
);
1169 set_capacity(floppy
->disk
, floppy
->blocks
* floppy
->bs_factor
);
1174 * Obtain the list of formattable capacities.
1175 * Very similar to ide_floppy_get_capacity, except that we push the capacity
1176 * descriptors to userland, instead of our own structures.
1178 * Userland gives us the following structure:
1180 * struct idefloppy_format_capacities {
1188 * userland initializes nformats to the number of allocated formats[] records.
1189 * On exit we set nformats to the number of records we've actually initialized.
1192 static int ide_floppy_get_format_capacities(ide_drive_t
*drive
, int __user
*arg
)
1195 u8 header_len
, desc_cnt
;
1196 int i
, blocks
, length
, u_array_size
, u_index
;
1199 if (get_user(u_array_size
, arg
))
1202 if (u_array_size
<= 0)
1205 idefloppy_create_read_capacity_cmd(&pc
);
1206 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1207 printk(KERN_ERR
"ide-floppy: Can't get floppy parameters\n");
1210 header_len
= pc
.buffer
[3];
1211 desc_cnt
= header_len
/ 8; /* capacity descriptor of 8 bytes */
1217 * We always skip the first capacity descriptor. That's the current
1218 * capacity. We are interested in the remaining descriptors, the
1219 * formattable capacities.
1221 for (i
= 1; i
< desc_cnt
; i
++) {
1222 unsigned int desc_start
= 4 + i
*8;
1224 if (u_index
>= u_array_size
)
1225 break; /* User-supplied buffer too small */
1227 blocks
= be32_to_cpu(*(u32
*)&pc
.buffer
[desc_start
]);
1228 length
= be16_to_cpu(*(u16
*)&pc
.buffer
[desc_start
+ 6]);
1230 if (put_user(blocks
, argp
))
1234 if (put_user(length
, argp
))
1241 if (put_user(u_index
, arg
))
1247 ** Get ATAPI_FORMAT_UNIT progress indication.
1249 ** Userland gives a pointer to an int. The int is set to a progress
1250 ** indicator 0-65536, with 65536=100%.
1252 ** If the drive does not support format progress indication, we just check
1253 ** the dsc bit, and return either 0 or 65536.
1256 static int idefloppy_get_format_progress(ide_drive_t
*drive
, int __user
*arg
)
1258 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1260 int progress_indication
= 0x10000;
1263 idefloppy_create_request_sense_cmd(&pc
);
1264 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1268 if (floppy
->sense_key
== 2 &&
1270 floppy
->ascq
== 4) {
1271 progress_indication
= floppy
->progress_indication
;
1273 /* Else assume format_unit has finished, and we're
1276 unsigned long flags
;
1279 local_irq_save(flags
);
1280 stat
= drive
->hwif
->INB(IDE_STATUS_REG
);
1281 local_irq_restore(flags
);
1283 progress_indication
= ((stat
& SEEK_STAT
) == 0) ? 0 : 0x10000;
1285 if (put_user(progress_indication
, arg
))
1292 * Return the current floppy capacity.
1294 static sector_t
idefloppy_capacity (ide_drive_t
*drive
)
1296 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1297 unsigned long capacity
= floppy
->blocks
* floppy
->bs_factor
;
1303 * Check whether we can support a drive, based on the ATAPI IDENTIFY command
1306 static int idefloppy_identify_device(ide_drive_t
*drive
, struct hd_driveid
*id
)
1308 struct idefloppy_id_gcw gcw
;
1310 *((u16
*) &gcw
) = id
->config
;
1313 /* kludge for Apple PowerBook internal zip */
1314 if ((gcw
.device_type
== 5) &&
1315 !strstr(id
->model
, "CD-ROM") &&
1316 strstr(id
->model
, "ZIP"))
1317 gcw
.device_type
= 0;
1320 if (gcw
.protocol
!= 2)
1321 printk(KERN_ERR
"ide-floppy: Protocol (0x%02x) is not ATAPI\n",
1323 else if (gcw
.device_type
!= 0)
1324 printk(KERN_ERR
"ide-floppy: Device type (0x%02x) is not set "
1325 "to floppy\n", gcw
.device_type
);
1326 else if (!gcw
.removable
)
1327 printk(KERN_ERR
"ide-floppy: The removable flag is not set\n");
1328 else if (gcw
.drq_type
== 3) {
1329 printk(KERN_ERR
"ide-floppy: Sorry, DRQ type (0x%02x) not "
1330 "supported\n", gcw
.drq_type
);
1331 } else if (gcw
.packet_size
!= 0) {
1332 printk(KERN_ERR
"ide-floppy: Packet size (0x%02x) is not 12 "
1333 "bytes long\n", gcw
.packet_size
);
1339 #ifdef CONFIG_IDE_PROC_FS
1340 static void idefloppy_add_settings(ide_drive_t
*drive
)
1342 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1345 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
1347 ide_add_setting(drive
, "bios_cyl", SETTING_RW
, TYPE_INT
, 0, 1023, 1, 1, &drive
->bios_cyl
, NULL
);
1348 ide_add_setting(drive
, "bios_head", SETTING_RW
, TYPE_BYTE
, 0, 255, 1, 1, &drive
->bios_head
, NULL
);
1349 ide_add_setting(drive
, "bios_sect", SETTING_RW
, TYPE_BYTE
, 0, 63, 1, 1, &drive
->bios_sect
, NULL
);
1350 ide_add_setting(drive
, "ticks", SETTING_RW
, TYPE_BYTE
, 0, 255, 1, 1, &floppy
->ticks
, NULL
);
1353 static inline void idefloppy_add_settings(ide_drive_t
*drive
) { ; }
1357 * Driver initialization.
1359 static void idefloppy_setup (ide_drive_t
*drive
, idefloppy_floppy_t
*floppy
)
1361 struct idefloppy_id_gcw gcw
;
1363 *((u16
*) &gcw
) = drive
->id
->config
;
1364 floppy
->pc
= floppy
->pc_stack
;
1365 if (gcw
.drq_type
== 1)
1366 set_bit(IDEFLOPPY_DRQ_INTERRUPT
, &floppy
->flags
);
1368 * We used to check revisions here. At this point however
1369 * I'm giving up. Just assume they are all broken, its easier.
1371 * The actual reason for the workarounds was likely
1372 * a driver bug after all rather than a firmware bug,
1373 * and the workaround below used to hide it. It should
1374 * be fixed as of version 1.9, but to be on the safe side
1375 * we'll leave the limitation below for the 2.2.x tree.
1378 if (!strncmp(drive
->id
->model
, "IOMEGA ZIP 100 ATAPI", 20)) {
1379 set_bit(IDEFLOPPY_ZIP_DRIVE
, &floppy
->flags
);
1380 /* This value will be visible in the /proc/ide/hdx/settings */
1381 floppy
->ticks
= IDEFLOPPY_TICKS_DELAY
;
1382 blk_queue_max_sectors(drive
->queue
, 64);
1386 * Guess what? The IOMEGA Clik! drive also needs the
1387 * above fix. It makes nasty clicking noises without
1388 * it, so please don't remove this.
1390 if (strncmp(drive
->id
->model
, "IOMEGA Clik!", 11) == 0) {
1391 blk_queue_max_sectors(drive
->queue
, 64);
1392 set_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
);
1396 (void) ide_floppy_get_capacity(drive
);
1397 idefloppy_add_settings(drive
);
1400 static void ide_floppy_remove(ide_drive_t
*drive
)
1402 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1403 struct gendisk
*g
= floppy
->disk
;
1405 ide_proc_unregister_driver(drive
, floppy
->driver
);
1409 ide_floppy_put(floppy
);
1412 static void idefloppy_cleanup_obj(struct kref
*kref
)
1414 struct ide_floppy_obj
*floppy
= to_ide_floppy(kref
);
1415 ide_drive_t
*drive
= floppy
->drive
;
1416 struct gendisk
*g
= floppy
->disk
;
1418 drive
->driver_data
= NULL
;
1419 g
->private_data
= NULL
;
1424 #ifdef CONFIG_IDE_PROC_FS
1425 static int proc_idefloppy_read_capacity
1426 (char *page
, char **start
, off_t off
, int count
, int *eof
, void *data
)
1428 ide_drive_t
*drive
= (ide_drive_t
*)data
;
1431 len
= sprintf(page
,"%llu\n", (long long)idefloppy_capacity(drive
));
1432 PROC_IDE_READ_RETURN(page
,start
,off
,count
,eof
,len
);
1435 static ide_proc_entry_t idefloppy_proc
[] = {
1436 { "capacity", S_IFREG
|S_IRUGO
, proc_idefloppy_read_capacity
, NULL
},
1437 { "geometry", S_IFREG
|S_IRUGO
, proc_ide_read_geometry
, NULL
},
1438 { NULL
, 0, NULL
, NULL
}
1440 #endif /* CONFIG_IDE_PROC_FS */
1442 static int ide_floppy_probe(ide_drive_t
*);
1444 static ide_driver_t idefloppy_driver
= {
1446 .owner
= THIS_MODULE
,
1447 .name
= "ide-floppy",
1448 .bus
= &ide_bus_type
,
1450 .probe
= ide_floppy_probe
,
1451 .remove
= ide_floppy_remove
,
1452 .version
= IDEFLOPPY_VERSION
,
1453 .media
= ide_floppy
,
1454 .supports_dsc_overlap
= 0,
1455 .do_request
= idefloppy_do_request
,
1456 .end_request
= idefloppy_do_end_request
,
1457 .error
= __ide_error
,
1458 .abort
= __ide_abort
,
1459 #ifdef CONFIG_IDE_PROC_FS
1460 .proc
= idefloppy_proc
,
1464 static int idefloppy_open(struct inode
*inode
, struct file
*filp
)
1466 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
1467 struct ide_floppy_obj
*floppy
;
1472 debug_log("Reached %s\n", __func__
);
1474 if (!(floppy
= ide_floppy_get(disk
)))
1477 drive
= floppy
->drive
;
1481 if (floppy
->openers
== 1) {
1482 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
, &floppy
->flags
);
1485 idefloppy_create_test_unit_ready_cmd(&pc
);
1486 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1487 idefloppy_create_start_stop_cmd(&pc
, 1);
1488 (void) idefloppy_queue_pc_tail(drive
, &pc
);
1491 if (ide_floppy_get_capacity(drive
)
1492 && (filp
->f_flags
& O_NDELAY
) == 0
1494 ** Allow O_NDELAY to open a drive without a disk, or with
1495 ** an unreadable disk, so that we can get the format
1496 ** capacity of the drive or begin the format - Sam
1500 goto out_put_floppy
;
1503 if (floppy
->wp
&& (filp
->f_mode
& 2)) {
1505 goto out_put_floppy
;
1507 set_bit(IDEFLOPPY_MEDIA_CHANGED
, &floppy
->flags
);
1508 /* IOMEGA Clik! drives do not support lock/unlock commands */
1509 if (!test_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
)) {
1510 idefloppy_create_prevent_cmd(&pc
, 1);
1511 (void) idefloppy_queue_pc_tail(drive
, &pc
);
1513 check_disk_change(inode
->i_bdev
);
1514 } else if (test_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
, &floppy
->flags
)) {
1516 goto out_put_floppy
;
1522 ide_floppy_put(floppy
);
1526 static int idefloppy_release(struct inode
*inode
, struct file
*filp
)
1528 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
1529 struct ide_floppy_obj
*floppy
= ide_floppy_g(disk
);
1530 ide_drive_t
*drive
= floppy
->drive
;
1533 debug_log("Reached %s\n", __func__
);
1535 if (floppy
->openers
== 1) {
1536 /* IOMEGA Clik! drives do not support lock/unlock commands */
1537 if (!test_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
)) {
1538 idefloppy_create_prevent_cmd(&pc
, 0);
1539 (void) idefloppy_queue_pc_tail(drive
, &pc
);
1542 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
, &floppy
->flags
);
1547 ide_floppy_put(floppy
);
1552 static int idefloppy_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1554 struct ide_floppy_obj
*floppy
= ide_floppy_g(bdev
->bd_disk
);
1555 ide_drive_t
*drive
= floppy
->drive
;
1557 geo
->heads
= drive
->bios_head
;
1558 geo
->sectors
= drive
->bios_sect
;
1559 geo
->cylinders
= (u16
)drive
->bios_cyl
; /* truncate */
1563 static int ide_floppy_lockdoor(idefloppy_floppy_t
*floppy
, idefloppy_pc_t
*pc
,
1564 unsigned long arg
, unsigned int cmd
)
1566 if (floppy
->openers
> 1)
1569 /* The IOMEGA Clik! Drive doesn't support this command -
1570 * no room for an eject mechanism */
1571 if (!test_bit(IDEFLOPPY_CLIK_DRIVE
, &floppy
->flags
)) {
1572 int prevent
= arg
? 1 : 0;
1574 if (cmd
== CDROMEJECT
)
1577 idefloppy_create_prevent_cmd(pc
, prevent
);
1578 (void) idefloppy_queue_pc_tail(floppy
->drive
, pc
);
1581 if (cmd
== CDROMEJECT
) {
1582 idefloppy_create_start_stop_cmd(pc
, 2);
1583 (void) idefloppy_queue_pc_tail(floppy
->drive
, pc
);
1589 static int ide_floppy_format_unit(idefloppy_floppy_t
*floppy
,
1592 int blocks
, length
, flags
, err
= 0;
1595 if (floppy
->openers
> 1) {
1596 /* Don't format if someone is using the disk */
1597 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
, &floppy
->flags
);
1601 set_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
, &floppy
->flags
);
1604 * Send ATAPI_FORMAT_UNIT to the drive.
1606 * Userland gives us the following structure:
1608 * struct idefloppy_format_command {
1614 * flags is a bitmask, currently, the only defined flag is:
1616 * 0x01 - verify media after format.
1618 if (get_user(blocks
, arg
) ||
1619 get_user(length
, arg
+1) ||
1620 get_user(flags
, arg
+2)) {
1625 (void) idefloppy_get_sfrp_bit(floppy
->drive
);
1626 idefloppy_create_format_unit_cmd(&pc
, blocks
, length
, flags
);
1628 if (idefloppy_queue_pc_tail(floppy
->drive
, &pc
))
1633 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS
, &floppy
->flags
);
1638 static int idefloppy_ioctl(struct inode
*inode
, struct file
*file
,
1639 unsigned int cmd
, unsigned long arg
)
1641 struct block_device
*bdev
= inode
->i_bdev
;
1642 struct ide_floppy_obj
*floppy
= ide_floppy_g(bdev
->bd_disk
);
1643 ide_drive_t
*drive
= floppy
->drive
;
1645 void __user
*argp
= (void __user
*)arg
;
1651 case CDROM_LOCKDOOR
:
1652 return ide_floppy_lockdoor(floppy
, &pc
, arg
, cmd
);
1653 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED
:
1655 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY
:
1656 return ide_floppy_get_format_capacities(drive
, argp
);
1657 case IDEFLOPPY_IOCTL_FORMAT_START
:
1658 if (!(file
->f_mode
& 2))
1661 return ide_floppy_format_unit(floppy
, (int __user
*)arg
);
1662 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS
:
1663 return idefloppy_get_format_progress(drive
, argp
);
1667 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1668 * and CDROM_SEND_PACKET (legacy) ioctls
1670 if (cmd
!= CDROM_SEND_PACKET
&& cmd
!= SCSI_IOCTL_SEND_COMMAND
)
1671 err
= scsi_cmd_ioctl(file
, bdev
->bd_disk
->queue
,
1672 bdev
->bd_disk
, cmd
, argp
);
1677 err
= generic_ide_ioctl(drive
, file
, bdev
, cmd
, arg
);
1682 static int idefloppy_media_changed(struct gendisk
*disk
)
1684 struct ide_floppy_obj
*floppy
= ide_floppy_g(disk
);
1685 ide_drive_t
*drive
= floppy
->drive
;
1687 /* do not scan partitions twice if this is a removable device */
1688 if (drive
->attach
) {
1692 return test_and_clear_bit(IDEFLOPPY_MEDIA_CHANGED
, &floppy
->flags
);
1695 static int idefloppy_revalidate_disk(struct gendisk
*disk
)
1697 struct ide_floppy_obj
*floppy
= ide_floppy_g(disk
);
1698 set_capacity(disk
, idefloppy_capacity(floppy
->drive
));
1702 static struct block_device_operations idefloppy_ops
= {
1703 .owner
= THIS_MODULE
,
1704 .open
= idefloppy_open
,
1705 .release
= idefloppy_release
,
1706 .ioctl
= idefloppy_ioctl
,
1707 .getgeo
= idefloppy_getgeo
,
1708 .media_changed
= idefloppy_media_changed
,
1709 .revalidate_disk
= idefloppy_revalidate_disk
1712 static int ide_floppy_probe(ide_drive_t
*drive
)
1714 idefloppy_floppy_t
*floppy
;
1717 if (!strstr("ide-floppy", drive
->driver_req
))
1719 if (!drive
->present
)
1721 if (drive
->media
!= ide_floppy
)
1723 if (!idefloppy_identify_device (drive
, drive
->id
)) {
1724 printk (KERN_ERR
"ide-floppy: %s: not supported by this version of ide-floppy\n", drive
->name
);
1728 printk("ide-floppy: passing drive %s to ide-scsi emulation.\n", drive
->name
);
1731 if ((floppy
= kzalloc(sizeof (idefloppy_floppy_t
), GFP_KERNEL
)) == NULL
) {
1732 printk (KERN_ERR
"ide-floppy: %s: Can't allocate a floppy structure\n", drive
->name
);
1736 g
= alloc_disk(1 << PARTN_BITS
);
1738 goto out_free_floppy
;
1740 ide_init_disk(g
, drive
);
1742 ide_proc_register_driver(drive
, &idefloppy_driver
);
1744 kref_init(&floppy
->kref
);
1746 floppy
->drive
= drive
;
1747 floppy
->driver
= &idefloppy_driver
;
1750 g
->private_data
= &floppy
->driver
;
1752 drive
->driver_data
= floppy
;
1754 idefloppy_setup (drive
, floppy
);
1756 g
->minors
= 1 << PARTN_BITS
;
1757 g
->driverfs_dev
= &drive
->gendev
;
1758 g
->flags
= drive
->removable
? GENHD_FL_REMOVABLE
: 0;
1759 g
->fops
= &idefloppy_ops
;
1770 MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
1772 static void __exit
idefloppy_exit (void)
1774 driver_unregister(&idefloppy_driver
.gen_driver
);
1777 static int __init
idefloppy_init(void)
1779 printk("ide-floppy driver " IDEFLOPPY_VERSION
"\n");
1780 return driver_register(&idefloppy_driver
.gen_driver
);
1783 MODULE_ALIAS("ide:*m-floppy*");
1784 module_init(idefloppy_init
);
1785 module_exit(idefloppy_exit
);
1786 MODULE_LICENSE("GPL");