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
8 * This driver supports the following IDE floppy drives:
10 * LS-120/240 SuperDisk
12 * Iomega PC Card Clik!/PocketZip
14 * For a historical changelog see
15 * Documentation/ide/ChangeLog.ide-floppy.1996-2002
18 #define IDEFLOPPY_VERSION "1.00"
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/kernel.h>
24 #include <linux/delay.h>
25 #include <linux/timer.h>
27 #include <linux/interrupt.h>
28 #include <linux/major.h>
29 #include <linux/errno.h>
30 #include <linux/genhd.h>
31 #include <linux/slab.h>
32 #include <linux/cdrom.h>
33 #include <linux/ide.h>
34 #include <linux/bitops.h>
35 #include <linux/mutex.h>
37 #include <scsi/scsi_ioctl.h>
39 #include <asm/byteorder.h>
40 #include <linux/irq.h>
41 #include <linux/uaccess.h>
43 #include <asm/unaligned.h>
45 /* define to see debug info */
46 #define IDEFLOPPY_DEBUG_LOG 0
48 /* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
49 #define IDEFLOPPY_DEBUG(fmt, args...)
51 #if IDEFLOPPY_DEBUG_LOG
52 #define debug_log(fmt, args...) \
53 printk(KERN_INFO "ide-floppy: " fmt, ## args)
55 #define debug_log(fmt, args...) do {} while (0)
59 /* Some drives require a longer irq timeout. */
60 #define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
63 * After each failed packet command we issue a request sense command and retry
64 * the packet command IDEFLOPPY_MAX_PC_RETRIES times.
66 #define IDEFLOPPY_MAX_PC_RETRIES 3
69 * With each packet command, we allocate a buffer of IDEFLOPPY_PC_BUFFER_SIZE
72 #define IDEFLOPPY_PC_BUFFER_SIZE 256
75 * In various places in the driver, we need to allocate storage for packet
76 * commands and requests, which will remain valid while we leave the driver to
77 * wait for an interrupt or a timeout event.
79 #define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
81 typedef struct idefloppy_packet_command_s
{
82 u8 c
[12]; /* Actual packet bytes */
83 int retries
; /* On each retry, we increment
85 int error
; /* Error code */
86 int request_transfer
; /* Bytes to transfer */
87 int actually_transferred
; /* Bytes actually transferred */
88 int buffer_size
; /* Size of our data buffer */
89 int b_count
; /* Missing/Available data on
91 struct request
*rq
; /* The corresponding request */
92 u8
*buffer
; /* Data buffer */
93 u8
*current_position
; /* Pointer into above buffer */
94 void (*callback
) (ide_drive_t
*); /* Called when this packet
95 command is completed */
96 u8 pc_buffer
[IDEFLOPPY_PC_BUFFER_SIZE
]; /* Temporary buffer */
97 unsigned long flags
; /* Status/Action bit flags: long
101 /* Packet command flag bits. */
103 /* 1 when we prefer to use DMA if possible */
104 PC_FLAG_DMA_RECOMMENDED
= (1 << 0),
105 /* 1 while DMA in progress */
106 PC_FLAG_DMA_IN_PROGRESS
= (1 << 1),
107 /* 1 when encountered problem during DMA */
108 PC_FLAG_DMA_ERROR
= (1 << 2),
110 PC_FLAG_WRITING
= (1 << 3),
111 /* Suppress error reporting */
112 PC_FLAG_SUPPRESS_ERROR
= (1 << 4),
115 /* format capacities descriptor codes */
116 #define CAPACITY_INVALID 0x00
117 #define CAPACITY_UNFORMATTED 0x01
118 #define CAPACITY_CURRENT 0x02
119 #define CAPACITY_NO_CARTRIDGE 0x03
122 * Most of our global data which we need to save even as we leave the driver
123 * due to an interrupt or a timer event is stored in a variable of type
124 * idefloppy_floppy_t, defined below.
126 typedef struct ide_floppy_obj
{
128 ide_driver_t
*driver
;
129 struct gendisk
*disk
;
131 unsigned int openers
; /* protected by BKL for now */
133 /* Current packet command */
135 /* Last failed packet command */
136 idefloppy_pc_t
*failed_pc
;
137 /* Packet command stack */
138 idefloppy_pc_t pc_stack
[IDEFLOPPY_PC_STACK
];
139 /* Next free packet command storage space */
141 struct request rq_stack
[IDEFLOPPY_PC_STACK
];
142 /* We implement a circular array */
145 /* Last error information */
146 u8 sense_key
, asc
, ascq
;
147 /* delay this long before sending packet command */
149 int progress_indication
;
151 /* Device information */
153 int blocks
, block_size
, bs_factor
;
154 /* Last format capacity descriptor */
156 /* Copy of the flexible disk page */
157 u8 flexible_disk_page
[32];
160 /* Supports format progress report */
162 /* Status/Action flags */
164 } idefloppy_floppy_t
;
166 #define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
168 /* Floppy flag bits values. */
170 /* DRQ interrupt device */
171 IDEFLOPPY_FLAG_DRQ_INTERRUPT
= (1 << 0),
172 /* Media may have changed */
173 IDEFLOPPY_FLAG_MEDIA_CHANGED
= (1 << 1),
174 /* Format in progress */
175 IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS
= (1 << 2),
176 /* Avoid commands not supported in Clik drive */
177 IDEFLOPPY_FLAG_CLIK_DRIVE
= (1 << 3),
178 /* Requires BH algorithm for packets */
179 IDEFLOPPY_FLAG_ZIP_DRIVE
= (1 << 4),
182 /* Defines for the MODE SENSE command */
183 #define MODE_SENSE_CURRENT 0x00
184 #define MODE_SENSE_CHANGEABLE 0x01
185 #define MODE_SENSE_DEFAULT 0x02
186 #define MODE_SENSE_SAVED 0x03
188 /* IOCTLs used in low-level formatting. */
189 #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
190 #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
191 #define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
192 #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
194 /* Error code returned in rq->errors to the higher part of the driver. */
195 #define IDEFLOPPY_ERROR_GENERAL 101
198 * Pages of the SELECT SENSE / MODE SENSE packet commands.
199 * See SFF-8070i spec.
201 #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
202 #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
204 static DEFINE_MUTEX(idefloppy_ref_mutex
);
206 #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
208 #define ide_floppy_g(disk) \
209 container_of((disk)->private_data, struct ide_floppy_obj, driver)
211 static struct ide_floppy_obj
*ide_floppy_get(struct gendisk
*disk
)
213 struct ide_floppy_obj
*floppy
= NULL
;
215 mutex_lock(&idefloppy_ref_mutex
);
216 floppy
= ide_floppy_g(disk
);
218 kref_get(&floppy
->kref
);
219 mutex_unlock(&idefloppy_ref_mutex
);
223 static void idefloppy_cleanup_obj(struct kref
*);
225 static void ide_floppy_put(struct ide_floppy_obj
*floppy
)
227 mutex_lock(&idefloppy_ref_mutex
);
228 kref_put(&floppy
->kref
, idefloppy_cleanup_obj
);
229 mutex_unlock(&idefloppy_ref_mutex
);
233 * Used to finish servicing a request. For read/write requests, we will call
234 * ide_end_request to pass to the next buffer.
236 static int idefloppy_do_end_request(ide_drive_t
*drive
, int uptodate
, int nsecs
)
238 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
239 struct request
*rq
= HWGROUP(drive
)->rq
;
242 debug_log("Reached %s\n", __func__
);
245 case 0: error
= IDEFLOPPY_ERROR_GENERAL
; break;
246 case 1: error
= 0; break;
247 default: error
= uptodate
;
250 floppy
->failed_pc
= NULL
;
251 /* Why does this happen? */
254 if (!blk_special_request(rq
)) {
255 /* our real local end request function */
256 ide_end_request(drive
, uptodate
, nsecs
);
260 /* fixme: need to move this local also */
261 ide_end_drive_cmd(drive
, 0, 0);
265 static void ide_floppy_io_buffers(ide_drive_t
*drive
, idefloppy_pc_t
*pc
,
266 unsigned int bcount
, int direction
)
268 struct request
*rq
= pc
->rq
;
269 struct req_iterator iter
;
270 struct bio_vec
*bvec
;
275 rq_for_each_segment(bvec
, rq
, iter
) {
279 count
= min(bvec
->bv_len
, bcount
);
281 data
= bvec_kmap_irq(bvec
, &flags
);
283 drive
->hwif
->atapi_output_bytes(drive
, data
, count
);
285 drive
->hwif
->atapi_input_bytes(drive
, data
, count
);
286 bvec_kunmap_irq(data
, &flags
);
289 pc
->b_count
+= count
;
293 idefloppy_do_end_request(drive
, 1, done
>> 9);
296 printk(KERN_ERR
"%s: leftover data in %s, bcount == %d\n",
297 drive
->name
, __func__
, bcount
);
299 ide_atapi_write_zeros(drive
, bcount
);
301 ide_atapi_discard_data(drive
, bcount
);
305 static void idefloppy_update_buffers(ide_drive_t
*drive
, idefloppy_pc_t
*pc
)
307 struct request
*rq
= pc
->rq
;
308 struct bio
*bio
= rq
->bio
;
310 while ((bio
= rq
->bio
) != NULL
)
311 idefloppy_do_end_request(drive
, 1, 0);
315 * Generate a new packet command request in front of the request queue, before
316 * the current request so that it will be processed immediately, on the next
317 * pass through the driver.
319 static void idefloppy_queue_pc_head(ide_drive_t
*drive
, idefloppy_pc_t
*pc
,
322 struct ide_floppy_obj
*floppy
= drive
->driver_data
;
324 ide_init_drive_cmd(rq
);
325 rq
->buffer
= (char *) pc
;
326 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
327 rq
->rq_disk
= floppy
->disk
;
328 (void) ide_do_drive_cmd(drive
, rq
, ide_preempt
);
331 static idefloppy_pc_t
*idefloppy_next_pc_storage(ide_drive_t
*drive
)
333 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
335 if (floppy
->pc_stack_index
== IDEFLOPPY_PC_STACK
)
336 floppy
->pc_stack_index
= 0;
337 return (&floppy
->pc_stack
[floppy
->pc_stack_index
++]);
340 static struct request
*idefloppy_next_rq_storage(ide_drive_t
*drive
)
342 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
344 if (floppy
->rq_stack_index
== IDEFLOPPY_PC_STACK
)
345 floppy
->rq_stack_index
= 0;
346 return (&floppy
->rq_stack
[floppy
->rq_stack_index
++]);
349 static void idefloppy_request_sense_callback(ide_drive_t
*drive
)
351 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
352 u8
*buf
= floppy
->pc
->buffer
;
354 debug_log("Reached %s\n", __func__
);
356 if (!floppy
->pc
->error
) {
357 floppy
->sense_key
= buf
[2] & 0x0F;
358 floppy
->asc
= buf
[12];
359 floppy
->ascq
= buf
[13];
360 floppy
->progress_indication
= buf
[15] & 0x80 ?
361 (u16
)get_unaligned((u16
*)&buf
[16]) : 0x10000;
363 if (floppy
->failed_pc
)
364 debug_log("pc = %x, sense key = %x, asc = %x,"
366 floppy
->failed_pc
->c
[0],
371 debug_log("sense key = %x, asc = %x, ascq = %x\n",
377 idefloppy_do_end_request(drive
, 1, 0);
379 printk(KERN_ERR
"Error in REQUEST SENSE itself - Aborting"
381 idefloppy_do_end_request(drive
, 0, 0);
385 /* General packet command callback function. */
386 static void idefloppy_pc_callback(ide_drive_t
*drive
)
388 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
390 debug_log("Reached %s\n", __func__
);
392 idefloppy_do_end_request(drive
, floppy
->pc
->error
? 0 : 1, 0);
395 static void idefloppy_init_pc(idefloppy_pc_t
*pc
)
397 memset(pc
->c
, 0, 12);
400 pc
->request_transfer
= 0;
401 pc
->buffer
= pc
->pc_buffer
;
402 pc
->buffer_size
= IDEFLOPPY_PC_BUFFER_SIZE
;
403 pc
->callback
= &idefloppy_pc_callback
;
406 static void idefloppy_create_request_sense_cmd(idefloppy_pc_t
*pc
)
408 idefloppy_init_pc(pc
);
409 pc
->c
[0] = GPCMD_REQUEST_SENSE
;
411 pc
->request_transfer
= 18;
412 pc
->callback
= &idefloppy_request_sense_callback
;
416 * Called when an error was detected during the last packet command. We queue a
417 * request sense packet command in the head of the request list.
419 static void idefloppy_retry_pc(ide_drive_t
*drive
)
424 (void)ide_read_error(drive
);
425 pc
= idefloppy_next_pc_storage(drive
);
426 rq
= idefloppy_next_rq_storage(drive
);
427 idefloppy_create_request_sense_cmd(pc
);
428 idefloppy_queue_pc_head(drive
, pc
, rq
);
431 /* The usual interrupt handler called during a packet command. */
432 static ide_startstop_t
idefloppy_pc_intr (ide_drive_t
*drive
)
434 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
435 ide_hwif_t
*hwif
= drive
->hwif
;
436 idefloppy_pc_t
*pc
= floppy
->pc
;
437 struct request
*rq
= pc
->rq
;
438 xfer_func_t
*xferfunc
;
444 debug_log("Reached %s interrupt handler\n", __func__
);
446 if (pc
->flags
& PC_FLAG_DMA_IN_PROGRESS
) {
447 dma_error
= hwif
->ide_dma_end(drive
);
449 printk(KERN_ERR
"%s: DMA %s error\n", drive
->name
,
450 rq_data_dir(rq
) ? "write" : "read");
451 pc
->flags
|= PC_FLAG_DMA_ERROR
;
453 pc
->actually_transferred
= pc
->request_transfer
;
454 idefloppy_update_buffers(drive
, pc
);
456 debug_log("DMA finished\n");
459 /* Clear the interrupt */
460 stat
= ide_read_status(drive
);
462 /* No more interrupts */
463 if ((stat
& DRQ_STAT
) == 0) {
464 debug_log("Packet command completed, %d bytes transferred\n",
465 pc
->actually_transferred
);
466 pc
->flags
&= ~PC_FLAG_DMA_IN_PROGRESS
;
468 local_irq_enable_in_hardirq();
470 if ((stat
& ERR_STAT
) || (pc
->flags
& PC_FLAG_DMA_ERROR
)) {
472 debug_log("%s: I/O error\n", drive
->name
);
474 if (pc
->c
[0] == GPCMD_REQUEST_SENSE
) {
475 printk(KERN_ERR
"ide-floppy: I/O error in "
476 "request sense command\n");
477 return ide_do_reset(drive
);
479 /* Retry operation */
480 idefloppy_retry_pc(drive
);
481 /* queued, but not started */
485 if (floppy
->failed_pc
== pc
)
486 floppy
->failed_pc
= NULL
;
487 /* Command finished - Call the callback function */
492 if (pc
->flags
& PC_FLAG_DMA_IN_PROGRESS
) {
493 pc
->flags
&= ~PC_FLAG_DMA_IN_PROGRESS
;
494 printk(KERN_ERR
"ide-floppy: The floppy wants to issue "
495 "more interrupts in DMA mode\n");
497 return ide_do_reset(drive
);
500 /* Get the number of bytes to transfer */
501 bcount
= (hwif
->INB(hwif
->io_ports
[IDE_BCOUNTH_OFFSET
]) << 8) |
502 hwif
->INB(hwif
->io_ports
[IDE_BCOUNTL_OFFSET
]);
503 /* on this interrupt */
504 ireason
= hwif
->INB(hwif
->io_ports
[IDE_IREASON_OFFSET
]);
507 printk(KERN_ERR
"ide-floppy: CoD != 0 in %s\n", __func__
);
508 return ide_do_reset(drive
);
510 if (((ireason
& IO
) == IO
) == !!(pc
->flags
& PC_FLAG_WRITING
)) {
511 /* Hopefully, we will never get here */
512 printk(KERN_ERR
"ide-floppy: We wanted to %s, ",
513 (ireason
& IO
) ? "Write" : "Read");
514 printk(KERN_ERR
"but the floppy wants us to %s !\n",
515 (ireason
& IO
) ? "Read" : "Write");
516 return ide_do_reset(drive
);
518 if (!(pc
->flags
& PC_FLAG_WRITING
)) {
519 /* Reading - Check that we have enough space */
520 temp
= pc
->actually_transferred
+ bcount
;
521 if (temp
> pc
->request_transfer
) {
522 if (temp
> pc
->buffer_size
) {
523 printk(KERN_ERR
"ide-floppy: The floppy wants "
524 "to send us more data than expected "
525 "- discarding data\n");
526 ide_atapi_discard_data(drive
, bcount
);
528 ide_set_handler(drive
,
534 debug_log("The floppy wants to send us more data than"
535 " expected - allowing transfer\n");
538 if (pc
->flags
& PC_FLAG_WRITING
)
539 xferfunc
= hwif
->atapi_output_bytes
;
541 xferfunc
= hwif
->atapi_input_bytes
;
544 xferfunc(drive
, pc
->current_position
, bcount
);
546 ide_floppy_io_buffers(drive
, pc
, bcount
,
547 !!(pc
->flags
& PC_FLAG_WRITING
));
549 /* Update the current position */
550 pc
->actually_transferred
+= bcount
;
551 pc
->current_position
+= bcount
;
553 /* And set the interrupt handler again */
554 ide_set_handler(drive
, &idefloppy_pc_intr
, IDEFLOPPY_WAIT_CMD
, NULL
);
559 * This is the original routine that did the packet transfer.
560 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
561 * for that drive below. The algorithm is chosen based on drive type
563 static ide_startstop_t
idefloppy_transfer_pc(ide_drive_t
*drive
)
565 ide_hwif_t
*hwif
= drive
->hwif
;
566 ide_startstop_t startstop
;
567 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
570 if (ide_wait_stat(&startstop
, drive
, DRQ_STAT
, BUSY_STAT
, WAIT_READY
)) {
571 printk(KERN_ERR
"ide-floppy: Strange, packet command "
572 "initiated yet DRQ isn't asserted\n");
575 ireason
= hwif
->INB(hwif
->io_ports
[IDE_IREASON_OFFSET
]);
576 if ((ireason
& CD
) == 0 || (ireason
& IO
)) {
577 printk(KERN_ERR
"ide-floppy: (IO,CoD) != (0,1) while "
578 "issuing a packet command\n");
579 return ide_do_reset(drive
);
582 /* Set the interrupt routine */
583 ide_set_handler(drive
, &idefloppy_pc_intr
, IDEFLOPPY_WAIT_CMD
, NULL
);
584 /* Send the actual packet */
585 HWIF(drive
)->atapi_output_bytes(drive
, floppy
->pc
->c
, 12);
591 * What we have here is a classic case of a top half / bottom half interrupt
592 * service routine. In interrupt mode, the device sends an interrupt to signal
593 * that it is ready to receive a packet. However, we need to delay about 2-3
594 * ticks before issuing the packet or we gets in trouble.
596 * So, follow carefully. transfer_pc1 is called as an interrupt (or directly).
597 * In either case, when the device says it's ready for a packet, we schedule
598 * the packet transfer to occur about 2-3 ticks later in transfer_pc2.
600 static int idefloppy_transfer_pc2(ide_drive_t
*drive
)
602 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
604 /* Send the actual packet */
605 HWIF(drive
)->atapi_output_bytes(drive
, floppy
->pc
->c
, 12);
606 /* Timeout for the packet command */
607 return IDEFLOPPY_WAIT_CMD
;
610 static ide_startstop_t
idefloppy_transfer_pc1(ide_drive_t
*drive
)
612 ide_hwif_t
*hwif
= drive
->hwif
;
613 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
614 ide_startstop_t startstop
;
617 if (ide_wait_stat(&startstop
, drive
, DRQ_STAT
, BUSY_STAT
, WAIT_READY
)) {
618 printk(KERN_ERR
"ide-floppy: Strange, packet command "
619 "initiated yet DRQ isn't asserted\n");
622 ireason
= hwif
->INB(hwif
->io_ports
[IDE_IREASON_OFFSET
]);
623 if ((ireason
& CD
) == 0 || (ireason
& IO
)) {
624 printk(KERN_ERR
"ide-floppy: (IO,CoD) != (0,1) "
625 "while issuing a packet command\n");
626 return ide_do_reset(drive
);
629 * The following delay solves a problem with ATAPI Zip 100 drives
630 * where the Busy flag was apparently being deasserted before the
631 * unit was ready to receive data. This was happening on a
632 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
633 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
634 * used until after the packet is moved in about 50 msec.
637 ide_set_handler(drive
, &idefloppy_pc_intr
, floppy
->ticks
,
638 &idefloppy_transfer_pc2
);
642 static void ide_floppy_report_error(idefloppy_floppy_t
*floppy
,
645 /* supress error messages resulting from Medium not present */
646 if (floppy
->sense_key
== 0x02 &&
647 floppy
->asc
== 0x3a &&
648 floppy
->ascq
== 0x00)
651 printk(KERN_ERR
"ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
652 "asc = %2x, ascq = %2x\n",
653 floppy
->drive
->name
, pc
->c
[0], floppy
->sense_key
,
654 floppy
->asc
, floppy
->ascq
);
658 static ide_startstop_t
idefloppy_issue_pc(ide_drive_t
*drive
,
661 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
662 ide_hwif_t
*hwif
= drive
->hwif
;
663 ide_handler_t
*pkt_xfer_routine
;
667 if (floppy
->failed_pc
== NULL
&&
668 pc
->c
[0] != GPCMD_REQUEST_SENSE
)
669 floppy
->failed_pc
= pc
;
670 /* Set the current packet command */
673 if (pc
->retries
> IDEFLOPPY_MAX_PC_RETRIES
) {
674 if (!(pc
->flags
& PC_FLAG_SUPPRESS_ERROR
))
675 ide_floppy_report_error(floppy
, pc
);
677 pc
->error
= IDEFLOPPY_ERROR_GENERAL
;
679 floppy
->failed_pc
= NULL
;
684 debug_log("Retry number - %d\n", pc
->retries
);
687 /* We haven't transferred any data yet */
688 pc
->actually_transferred
= 0;
689 pc
->current_position
= pc
->buffer
;
690 bcount
= min(pc
->request_transfer
, 63 * 1024);
692 if (pc
->flags
& PC_FLAG_DMA_ERROR
) {
693 pc
->flags
&= ~PC_FLAG_DMA_ERROR
;
698 if ((pc
->flags
& PC_FLAG_DMA_RECOMMENDED
) && drive
->using_dma
)
699 dma
= !hwif
->dma_setup(drive
);
701 ide_pktcmd_tf_load(drive
, IDE_TFLAG_NO_SELECT_MASK
|
702 IDE_TFLAG_OUT_DEVICE
, bcount
, dma
);
705 /* Begin DMA, if necessary */
706 pc
->flags
|= PC_FLAG_DMA_IN_PROGRESS
;
707 hwif
->dma_start(drive
);
710 /* Can we transfer the packet when we get the interrupt or wait? */
711 if (floppy
->flags
& IDEFLOPPY_FLAG_ZIP_DRIVE
) {
713 pkt_xfer_routine
= &idefloppy_transfer_pc1
;
716 pkt_xfer_routine
= &idefloppy_transfer_pc
;
719 if (floppy
->flags
& IDEFLOPPY_FLAG_DRQ_INTERRUPT
) {
720 /* Issue the packet command */
721 ide_execute_command(drive
, WIN_PACKETCMD
,
727 /* Issue the packet command */
728 hwif
->OUTB(WIN_PACKETCMD
, hwif
->io_ports
[IDE_COMMAND_OFFSET
]);
729 return (*pkt_xfer_routine
) (drive
);
733 static void idefloppy_rw_callback(ide_drive_t
*drive
)
735 debug_log("Reached %s\n", __func__
);
737 idefloppy_do_end_request(drive
, 1, 0);
741 static void idefloppy_create_prevent_cmd(idefloppy_pc_t
*pc
, int prevent
)
743 debug_log("creating prevent removal command, prevent = %d\n", prevent
);
745 idefloppy_init_pc(pc
);
746 pc
->c
[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL
;
750 static void idefloppy_create_read_capacity_cmd(idefloppy_pc_t
*pc
)
752 idefloppy_init_pc(pc
);
753 pc
->c
[0] = GPCMD_READ_FORMAT_CAPACITIES
;
756 pc
->request_transfer
= 255;
759 static void idefloppy_create_format_unit_cmd(idefloppy_pc_t
*pc
, int b
, int l
,
762 idefloppy_init_pc(pc
);
763 pc
->c
[0] = GPCMD_FORMAT_UNIT
;
766 memset(pc
->buffer
, 0, 12);
767 pc
->buffer
[1] = 0xA2;
768 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
770 if (flags
& 1) /* Verify bit on... */
771 pc
->buffer
[1] ^= 0x20; /* ... turn off DCRT bit */
774 put_unaligned(cpu_to_be32(b
), (unsigned int *)(&pc
->buffer
[4]));
775 put_unaligned(cpu_to_be32(l
), (unsigned int *)(&pc
->buffer
[8]));
776 pc
->buffer_size
= 12;
777 pc
->flags
|= PC_FLAG_WRITING
;
780 /* A mode sense command is used to "sense" floppy parameters. */
781 static void idefloppy_create_mode_sense_cmd(idefloppy_pc_t
*pc
, u8 page_code
,
784 u16 length
= 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
786 idefloppy_init_pc(pc
);
787 pc
->c
[0] = GPCMD_MODE_SENSE_10
;
789 pc
->c
[2] = page_code
+ (type
<< 6);
792 case IDEFLOPPY_CAPABILITIES_PAGE
:
795 case IDEFLOPPY_FLEXIBLE_DISK_PAGE
:
799 printk(KERN_ERR
"ide-floppy: unsupported page code "
800 "in create_mode_sense_cmd\n");
802 put_unaligned(cpu_to_be16(length
), (u16
*) &pc
->c
[7]);
803 pc
->request_transfer
= length
;
806 static void idefloppy_create_start_stop_cmd(idefloppy_pc_t
*pc
, int start
)
808 idefloppy_init_pc(pc
);
809 pc
->c
[0] = GPCMD_START_STOP_UNIT
;
813 static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t
*pc
)
815 idefloppy_init_pc(pc
);
816 pc
->c
[0] = GPCMD_TEST_UNIT_READY
;
819 static void idefloppy_create_rw_cmd(idefloppy_floppy_t
*floppy
,
820 idefloppy_pc_t
*pc
, struct request
*rq
,
821 unsigned long sector
)
823 int block
= sector
/ floppy
->bs_factor
;
824 int blocks
= rq
->nr_sectors
/ floppy
->bs_factor
;
825 int cmd
= rq_data_dir(rq
);
827 debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
830 idefloppy_init_pc(pc
);
831 pc
->c
[0] = cmd
== READ
? GPCMD_READ_10
: GPCMD_WRITE_10
;
832 put_unaligned(cpu_to_be16(blocks
), (unsigned short *)&pc
->c
[7]);
833 put_unaligned(cpu_to_be32(block
), (unsigned int *) &pc
->c
[2]);
835 pc
->callback
= &idefloppy_rw_callback
;
837 pc
->b_count
= cmd
== READ
? 0 : rq
->bio
->bi_size
;
838 if (rq
->cmd_flags
& REQ_RW
)
839 pc
->flags
|= PC_FLAG_WRITING
;
841 pc
->request_transfer
= pc
->buffer_size
= blocks
* floppy
->block_size
;
842 pc
->flags
|= PC_FLAG_DMA_RECOMMENDED
;
845 static void idefloppy_blockpc_cmd(idefloppy_floppy_t
*floppy
,
846 idefloppy_pc_t
*pc
, struct request
*rq
)
848 idefloppy_init_pc(pc
);
849 pc
->callback
= &idefloppy_rw_callback
;
850 memcpy(pc
->c
, rq
->cmd
, sizeof(pc
->c
));
852 pc
->b_count
= rq
->data_len
;
853 if (rq
->data_len
&& rq_data_dir(rq
) == WRITE
)
854 pc
->flags
|= PC_FLAG_WRITING
;
855 pc
->buffer
= rq
->data
;
857 pc
->flags
|= PC_FLAG_DMA_RECOMMENDED
;
859 * possibly problematic, doesn't look like ide-floppy correctly
860 * handled scattered requests if dma fails...
862 pc
->request_transfer
= pc
->buffer_size
= rq
->data_len
;
865 static ide_startstop_t
idefloppy_do_request(ide_drive_t
*drive
,
866 struct request
*rq
, sector_t block_s
)
868 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
870 unsigned long block
= (unsigned long)block_s
;
872 debug_log("dev: %s, cmd_type: %x, errors: %d\n",
873 rq
->rq_disk
? rq
->rq_disk
->disk_name
: "?",
874 rq
->cmd_type
, rq
->errors
);
875 debug_log("sector: %ld, nr_sectors: %ld, "
876 "current_nr_sectors: %d\n", (long)rq
->sector
,
877 rq
->nr_sectors
, rq
->current_nr_sectors
);
879 if (rq
->errors
>= ERROR_MAX
) {
880 if (floppy
->failed_pc
)
881 ide_floppy_report_error(floppy
, floppy
->failed_pc
);
883 printk(KERN_ERR
"ide-floppy: %s: I/O error\n",
885 idefloppy_do_end_request(drive
, 0, 0);
888 if (blk_fs_request(rq
)) {
889 if (((long)rq
->sector
% floppy
->bs_factor
) ||
890 (rq
->nr_sectors
% floppy
->bs_factor
)) {
891 printk(KERN_ERR
"%s: unsupported r/w request size\n",
893 idefloppy_do_end_request(drive
, 0, 0);
896 pc
= idefloppy_next_pc_storage(drive
);
897 idefloppy_create_rw_cmd(floppy
, pc
, rq
, block
);
898 } else if (blk_special_request(rq
)) {
899 pc
= (idefloppy_pc_t
*) rq
->buffer
;
900 } else if (blk_pc_request(rq
)) {
901 pc
= idefloppy_next_pc_storage(drive
);
902 idefloppy_blockpc_cmd(floppy
, pc
, rq
);
904 blk_dump_rq_flags(rq
,
905 "ide-floppy: unsupported command in queue");
906 idefloppy_do_end_request(drive
, 0, 0);
911 return idefloppy_issue_pc(drive
, pc
);
915 * Add a special packet command request to the tail of the request queue,
916 * and wait for it to be serviced.
918 static int idefloppy_queue_pc_tail(ide_drive_t
*drive
, idefloppy_pc_t
*pc
)
920 struct ide_floppy_obj
*floppy
= drive
->driver_data
;
923 ide_init_drive_cmd(&rq
);
924 rq
.buffer
= (char *) pc
;
925 rq
.cmd_type
= REQ_TYPE_SPECIAL
;
926 rq
.rq_disk
= floppy
->disk
;
928 return ide_do_drive_cmd(drive
, &rq
, ide_wait
);
932 * Look at the flexible disk page parameters. We ignore the CHS capacity
933 * parameters and use the LBA parameters instead.
935 static int ide_floppy_get_flexible_disk_page(ide_drive_t
*drive
)
937 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
940 int capacity
, lba_capacity
;
941 u16 transfer_rate
, sector_size
, cyls
, rpm
;
944 idefloppy_create_mode_sense_cmd(&pc
, IDEFLOPPY_FLEXIBLE_DISK_PAGE
,
947 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
948 printk(KERN_ERR
"ide-floppy: Can't get flexible disk page"
952 floppy
->wp
= !!(pc
.buffer
[3] & 0x80);
953 set_disk_ro(floppy
->disk
, floppy
->wp
);
954 page
= &pc
.buffer
[8];
956 transfer_rate
= be16_to_cpu(*(u16
*)&pc
.buffer
[8 + 2]);
957 sector_size
= be16_to_cpu(*(u16
*)&pc
.buffer
[8 + 6]);
958 cyls
= be16_to_cpu(*(u16
*)&pc
.buffer
[8 + 8]);
959 rpm
= be16_to_cpu(*(u16
*)&pc
.buffer
[8 + 28]);
960 heads
= pc
.buffer
[8 + 4];
961 sectors
= pc
.buffer
[8 + 5];
963 capacity
= cyls
* heads
* sectors
* sector_size
;
965 if (memcmp(page
, &floppy
->flexible_disk_page
, 32))
966 printk(KERN_INFO
"%s: %dkB, %d/%d/%d CHS, %d kBps, "
967 "%d sector size, %d rpm\n",
968 drive
->name
, capacity
/ 1024, cyls
, heads
,
969 sectors
, transfer_rate
/ 8, sector_size
, rpm
);
971 memcpy(&floppy
->flexible_disk_page
, page
, 32);
972 drive
->bios_cyl
= cyls
;
973 drive
->bios_head
= heads
;
974 drive
->bios_sect
= sectors
;
975 lba_capacity
= floppy
->blocks
* floppy
->block_size
;
977 if (capacity
< lba_capacity
) {
978 printk(KERN_NOTICE
"%s: The disk reports a capacity of %d "
979 "bytes, but the drive only handles %d\n",
980 drive
->name
, lba_capacity
, capacity
);
981 floppy
->blocks
= floppy
->block_size
?
982 capacity
/ floppy
->block_size
: 0;
987 static int idefloppy_get_sfrp_bit(ide_drive_t
*drive
)
989 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
993 idefloppy_create_mode_sense_cmd(&pc
, IDEFLOPPY_CAPABILITIES_PAGE
,
996 pc
.flags
|= PC_FLAG_SUPPRESS_ERROR
;
997 if (idefloppy_queue_pc_tail(drive
, &pc
))
1000 floppy
->srfp
= pc
.buffer
[8 + 2] & 0x40;
1005 * Determine if a media is present in the floppy drive, and if so, its LBA
1008 static int ide_floppy_get_capacity(ide_drive_t
*drive
)
1010 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1013 u8 header_len
, desc_cnt
;
1014 int i
, rc
= 1, blocks
, length
;
1016 drive
->bios_cyl
= 0;
1017 drive
->bios_head
= drive
->bios_sect
= 0;
1019 floppy
->bs_factor
= 1;
1020 set_capacity(floppy
->disk
, 0);
1022 idefloppy_create_read_capacity_cmd(&pc
);
1023 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1024 printk(KERN_ERR
"ide-floppy: Can't get floppy parameters\n");
1027 header_len
= pc
.buffer
[3];
1028 cap_desc
= &pc
.buffer
[4];
1029 desc_cnt
= header_len
/ 8; /* capacity descriptor of 8 bytes */
1031 for (i
= 0; i
< desc_cnt
; i
++) {
1032 unsigned int desc_start
= 4 + i
*8;
1034 blocks
= be32_to_cpu(*(u32
*)&pc
.buffer
[desc_start
]);
1035 length
= be16_to_cpu(*(u16
*)&pc
.buffer
[desc_start
+ 6]);
1037 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
1038 i
, blocks
* length
/ 1024, blocks
, length
);
1043 * the code below is valid only for the 1st descriptor, ie i=0
1046 switch (pc
.buffer
[desc_start
+ 4] & 0x03) {
1047 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1048 case CAPACITY_UNFORMATTED
:
1049 if (!(floppy
->flags
& IDEFLOPPY_FLAG_CLIK_DRIVE
))
1051 * If it is not a clik drive, break out
1052 * (maintains previous driver behaviour)
1055 case CAPACITY_CURRENT
:
1056 /* Normal Zip/LS-120 disks */
1057 if (memcmp(cap_desc
, &floppy
->cap_desc
, 8))
1058 printk(KERN_INFO
"%s: %dkB, %d blocks, %d "
1059 "sector size\n", drive
->name
,
1060 blocks
* length
/ 1024, blocks
, length
);
1061 memcpy(&floppy
->cap_desc
, cap_desc
, 8);
1063 if (!length
|| length
% 512) {
1064 printk(KERN_NOTICE
"%s: %d bytes block size "
1065 "not supported\n", drive
->name
, length
);
1067 floppy
->blocks
= blocks
;
1068 floppy
->block_size
= length
;
1069 floppy
->bs_factor
= length
/ 512;
1070 if (floppy
->bs_factor
!= 1)
1071 printk(KERN_NOTICE
"%s: warning: non "
1072 "512 bytes block size not "
1073 "fully supported\n",
1078 case CAPACITY_NO_CARTRIDGE
:
1080 * This is a KERN_ERR so it appears on screen
1081 * for the user to see
1083 printk(KERN_ERR
"%s: No disk in drive\n", drive
->name
);
1085 case CAPACITY_INVALID
:
1086 printk(KERN_ERR
"%s: Invalid capacity for disk "
1087 "in drive\n", drive
->name
);
1090 debug_log("Descriptor 0 Code: %d\n",
1091 pc
.buffer
[desc_start
+ 4] & 0x03);
1094 /* Clik! disk does not support get_flexible_disk_page */
1095 if (!(floppy
->flags
& IDEFLOPPY_FLAG_CLIK_DRIVE
))
1096 (void) ide_floppy_get_flexible_disk_page(drive
);
1098 set_capacity(floppy
->disk
, floppy
->blocks
* floppy
->bs_factor
);
1103 * Obtain the list of formattable capacities.
1104 * Very similar to ide_floppy_get_capacity, except that we push the capacity
1105 * descriptors to userland, instead of our own structures.
1107 * Userland gives us the following structure:
1109 * struct idefloppy_format_capacities {
1117 * userland initializes nformats to the number of allocated formats[] records.
1118 * On exit we set nformats to the number of records we've actually initialized.
1121 static int ide_floppy_get_format_capacities(ide_drive_t
*drive
, int __user
*arg
)
1124 u8 header_len
, desc_cnt
;
1125 int i
, blocks
, length
, u_array_size
, u_index
;
1128 if (get_user(u_array_size
, arg
))
1131 if (u_array_size
<= 0)
1134 idefloppy_create_read_capacity_cmd(&pc
);
1135 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1136 printk(KERN_ERR
"ide-floppy: Can't get floppy parameters\n");
1139 header_len
= pc
.buffer
[3];
1140 desc_cnt
= header_len
/ 8; /* capacity descriptor of 8 bytes */
1146 * We always skip the first capacity descriptor. That's the current
1147 * capacity. We are interested in the remaining descriptors, the
1148 * formattable capacities.
1150 for (i
= 1; i
< desc_cnt
; i
++) {
1151 unsigned int desc_start
= 4 + i
*8;
1153 if (u_index
>= u_array_size
)
1154 break; /* User-supplied buffer too small */
1156 blocks
= be32_to_cpu(*(u32
*)&pc
.buffer
[desc_start
]);
1157 length
= be16_to_cpu(*(u16
*)&pc
.buffer
[desc_start
+ 6]);
1159 if (put_user(blocks
, argp
))
1163 if (put_user(length
, argp
))
1170 if (put_user(u_index
, arg
))
1176 * Get ATAPI_FORMAT_UNIT progress indication.
1178 * Userland gives a pointer to an int. The int is set to a progress
1179 * indicator 0-65536, with 65536=100%.
1181 * If the drive does not support format progress indication, we just check
1182 * the dsc bit, and return either 0 or 65536.
1185 static int idefloppy_get_format_progress(ide_drive_t
*drive
, int __user
*arg
)
1187 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1189 int progress_indication
= 0x10000;
1192 idefloppy_create_request_sense_cmd(&pc
);
1193 if (idefloppy_queue_pc_tail(drive
, &pc
))
1196 if (floppy
->sense_key
== 2 &&
1199 progress_indication
= floppy
->progress_indication
;
1201 /* Else assume format_unit has finished, and we're at 0x10000 */
1203 unsigned long flags
;
1206 local_irq_save(flags
);
1207 stat
= ide_read_status(drive
);
1208 local_irq_restore(flags
);
1210 progress_indication
= ((stat
& SEEK_STAT
) == 0) ? 0 : 0x10000;
1212 if (put_user(progress_indication
, arg
))
1218 static sector_t
idefloppy_capacity(ide_drive_t
*drive
)
1220 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1221 unsigned long capacity
= floppy
->blocks
* floppy
->bs_factor
;
1227 * Check whether we can support a drive, based on the ATAPI IDENTIFY command
1230 static int idefloppy_identify_device(ide_drive_t
*drive
, struct hd_driveid
*id
)
1233 u8 device_type
, protocol
, removable
, drq_type
, packet_size
;
1235 *((u16
*) &gcw
) = id
->config
;
1237 device_type
= gcw
[1] & 0x1F;
1238 removable
= (gcw
[0] & 0x80) >> 7;
1239 protocol
= (gcw
[1] & 0xC0) >> 6;
1240 drq_type
= (gcw
[0] & 0x60) >> 5;
1241 packet_size
= gcw
[0] & 0x03;
1244 /* kludge for Apple PowerBook internal zip */
1245 if (device_type
== 5 &&
1246 !strstr(id
->model
, "CD-ROM") && strstr(id
->model
, "ZIP"))
1251 printk(KERN_ERR
"ide-floppy: Protocol (0x%02x) is not ATAPI\n",
1253 else if (device_type
!= 0)
1254 printk(KERN_ERR
"ide-floppy: Device type (0x%02x) is not set "
1255 "to floppy\n", device_type
);
1256 else if (!removable
)
1257 printk(KERN_ERR
"ide-floppy: The removable flag is not set\n");
1258 else if (drq_type
== 3)
1259 printk(KERN_ERR
"ide-floppy: Sorry, DRQ type (0x%02x) not "
1260 "supported\n", drq_type
);
1261 else if (packet_size
!= 0)
1262 printk(KERN_ERR
"ide-floppy: Packet size (0x%02x) is not 12 "
1263 "bytes\n", packet_size
);
1269 #ifdef CONFIG_IDE_PROC_FS
1270 static void idefloppy_add_settings(ide_drive_t
*drive
)
1272 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1274 ide_add_setting(drive
, "bios_cyl", SETTING_RW
, TYPE_INT
, 0, 1023, 1, 1,
1275 &drive
->bios_cyl
, NULL
);
1276 ide_add_setting(drive
, "bios_head", SETTING_RW
, TYPE_BYTE
, 0, 255, 1, 1,
1277 &drive
->bios_head
, NULL
);
1278 ide_add_setting(drive
, "bios_sect", SETTING_RW
, TYPE_BYTE
, 0, 63, 1, 1,
1279 &drive
->bios_sect
, NULL
);
1280 ide_add_setting(drive
, "ticks", SETTING_RW
, TYPE_BYTE
, 0, 255, 1, 1,
1281 &floppy
->ticks
, NULL
);
1284 static inline void idefloppy_add_settings(ide_drive_t
*drive
) { ; }
1287 static void idefloppy_setup(ide_drive_t
*drive
, idefloppy_floppy_t
*floppy
)
1291 *((u16
*) &gcw
) = drive
->id
->config
;
1292 floppy
->pc
= floppy
->pc_stack
;
1294 if (((gcw
[0] & 0x60) >> 5) == 1)
1295 floppy
->flags
|= IDEFLOPPY_FLAG_DRQ_INTERRUPT
;
1297 * We used to check revisions here. At this point however I'm giving up.
1298 * Just assume they are all broken, its easier.
1300 * The actual reason for the workarounds was likely a driver bug after
1301 * all rather than a firmware bug, and the workaround below used to hide
1302 * it. It should be fixed as of version 1.9, but to be on the safe side
1303 * we'll leave the limitation below for the 2.2.x tree.
1305 if (!strncmp(drive
->id
->model
, "IOMEGA ZIP 100 ATAPI", 20)) {
1306 floppy
->flags
|= IDEFLOPPY_FLAG_ZIP_DRIVE
;
1307 /* This value will be visible in the /proc/ide/hdx/settings */
1308 floppy
->ticks
= IDEFLOPPY_TICKS_DELAY
;
1309 blk_queue_max_sectors(drive
->queue
, 64);
1313 * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
1314 * nasty clicking noises without it, so please don't remove this.
1316 if (strncmp(drive
->id
->model
, "IOMEGA Clik!", 11) == 0) {
1317 blk_queue_max_sectors(drive
->queue
, 64);
1318 floppy
->flags
|= IDEFLOPPY_FLAG_CLIK_DRIVE
;
1321 (void) ide_floppy_get_capacity(drive
);
1322 idefloppy_add_settings(drive
);
1325 static void ide_floppy_remove(ide_drive_t
*drive
)
1327 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1328 struct gendisk
*g
= floppy
->disk
;
1330 ide_proc_unregister_driver(drive
, floppy
->driver
);
1334 ide_floppy_put(floppy
);
1337 static void idefloppy_cleanup_obj(struct kref
*kref
)
1339 struct ide_floppy_obj
*floppy
= to_ide_floppy(kref
);
1340 ide_drive_t
*drive
= floppy
->drive
;
1341 struct gendisk
*g
= floppy
->disk
;
1343 drive
->driver_data
= NULL
;
1344 g
->private_data
= NULL
;
1349 #ifdef CONFIG_IDE_PROC_FS
1350 static int proc_idefloppy_read_capacity(char *page
, char **start
, off_t off
,
1351 int count
, int *eof
, void *data
)
1353 ide_drive_t
*drive
= (ide_drive_t
*)data
;
1356 len
= sprintf(page
, "%llu\n", (long long)idefloppy_capacity(drive
));
1357 PROC_IDE_READ_RETURN(page
, start
, off
, count
, eof
, len
);
1360 static ide_proc_entry_t idefloppy_proc
[] = {
1361 { "capacity", S_IFREG
|S_IRUGO
, proc_idefloppy_read_capacity
, NULL
},
1362 { "geometry", S_IFREG
|S_IRUGO
, proc_ide_read_geometry
, NULL
},
1363 { NULL
, 0, NULL
, NULL
}
1365 #endif /* CONFIG_IDE_PROC_FS */
1367 static int ide_floppy_probe(ide_drive_t
*);
1369 static ide_driver_t idefloppy_driver
= {
1371 .owner
= THIS_MODULE
,
1372 .name
= "ide-floppy",
1373 .bus
= &ide_bus_type
,
1375 .probe
= ide_floppy_probe
,
1376 .remove
= ide_floppy_remove
,
1377 .version
= IDEFLOPPY_VERSION
,
1378 .media
= ide_floppy
,
1379 .supports_dsc_overlap
= 0,
1380 .do_request
= idefloppy_do_request
,
1381 .end_request
= idefloppy_do_end_request
,
1382 .error
= __ide_error
,
1383 .abort
= __ide_abort
,
1384 #ifdef CONFIG_IDE_PROC_FS
1385 .proc
= idefloppy_proc
,
1389 static int idefloppy_open(struct inode
*inode
, struct file
*filp
)
1391 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
1392 struct ide_floppy_obj
*floppy
;
1397 debug_log("Reached %s\n", __func__
);
1399 floppy
= ide_floppy_get(disk
);
1403 drive
= floppy
->drive
;
1407 if (floppy
->openers
== 1) {
1408 floppy
->flags
&= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS
;
1411 idefloppy_create_test_unit_ready_cmd(&pc
);
1412 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1413 idefloppy_create_start_stop_cmd(&pc
, 1);
1414 (void) idefloppy_queue_pc_tail(drive
, &pc
);
1417 if (ide_floppy_get_capacity(drive
)
1418 && (filp
->f_flags
& O_NDELAY
) == 0
1420 * Allow O_NDELAY to open a drive without a disk, or with an
1421 * unreadable disk, so that we can get the format capacity
1422 * of the drive or begin the format - Sam
1426 goto out_put_floppy
;
1429 if (floppy
->wp
&& (filp
->f_mode
& 2)) {
1431 goto out_put_floppy
;
1433 floppy
->flags
|= IDEFLOPPY_FLAG_MEDIA_CHANGED
;
1434 /* IOMEGA Clik! drives do not support lock/unlock commands */
1435 if (!(floppy
->flags
& IDEFLOPPY_FLAG_CLIK_DRIVE
)) {
1436 idefloppy_create_prevent_cmd(&pc
, 1);
1437 (void) idefloppy_queue_pc_tail(drive
, &pc
);
1439 check_disk_change(inode
->i_bdev
);
1440 } else if (floppy
->flags
& IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS
) {
1442 goto out_put_floppy
;
1448 ide_floppy_put(floppy
);
1452 static int idefloppy_release(struct inode
*inode
, struct file
*filp
)
1454 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
1455 struct ide_floppy_obj
*floppy
= ide_floppy_g(disk
);
1456 ide_drive_t
*drive
= floppy
->drive
;
1459 debug_log("Reached %s\n", __func__
);
1461 if (floppy
->openers
== 1) {
1462 /* IOMEGA Clik! drives do not support lock/unlock commands */
1463 if (!(floppy
->flags
& IDEFLOPPY_FLAG_CLIK_DRIVE
)) {
1464 idefloppy_create_prevent_cmd(&pc
, 0);
1465 (void) idefloppy_queue_pc_tail(drive
, &pc
);
1468 floppy
->flags
&= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS
;
1473 ide_floppy_put(floppy
);
1478 static int idefloppy_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1480 struct ide_floppy_obj
*floppy
= ide_floppy_g(bdev
->bd_disk
);
1481 ide_drive_t
*drive
= floppy
->drive
;
1483 geo
->heads
= drive
->bios_head
;
1484 geo
->sectors
= drive
->bios_sect
;
1485 geo
->cylinders
= (u16
)drive
->bios_cyl
; /* truncate */
1489 static int ide_floppy_lockdoor(idefloppy_floppy_t
*floppy
, idefloppy_pc_t
*pc
,
1490 unsigned long arg
, unsigned int cmd
)
1492 if (floppy
->openers
> 1)
1495 /* The IOMEGA Clik! Drive doesn't support this command -
1496 * no room for an eject mechanism */
1497 if (!(floppy
->flags
& IDEFLOPPY_FLAG_CLIK_DRIVE
)) {
1498 int prevent
= arg
? 1 : 0;
1500 if (cmd
== CDROMEJECT
)
1503 idefloppy_create_prevent_cmd(pc
, prevent
);
1504 (void) idefloppy_queue_pc_tail(floppy
->drive
, pc
);
1507 if (cmd
== CDROMEJECT
) {
1508 idefloppy_create_start_stop_cmd(pc
, 2);
1509 (void) idefloppy_queue_pc_tail(floppy
->drive
, pc
);
1515 static int ide_floppy_format_unit(idefloppy_floppy_t
*floppy
,
1518 int blocks
, length
, flags
, err
= 0;
1521 if (floppy
->openers
> 1) {
1522 /* Don't format if someone is using the disk */
1523 floppy
->flags
&= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS
;
1527 floppy
->flags
|= IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS
;
1530 * Send ATAPI_FORMAT_UNIT to the drive.
1532 * Userland gives us the following structure:
1534 * struct idefloppy_format_command {
1540 * flags is a bitmask, currently, the only defined flag is:
1542 * 0x01 - verify media after format.
1544 if (get_user(blocks
, arg
) ||
1545 get_user(length
, arg
+1) ||
1546 get_user(flags
, arg
+2)) {
1551 (void) idefloppy_get_sfrp_bit(floppy
->drive
);
1552 idefloppy_create_format_unit_cmd(&pc
, blocks
, length
, flags
);
1554 if (idefloppy_queue_pc_tail(floppy
->drive
, &pc
))
1559 floppy
->flags
&= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS
;
1564 static int idefloppy_ioctl(struct inode
*inode
, struct file
*file
,
1565 unsigned int cmd
, unsigned long arg
)
1567 struct block_device
*bdev
= inode
->i_bdev
;
1568 struct ide_floppy_obj
*floppy
= ide_floppy_g(bdev
->bd_disk
);
1569 ide_drive_t
*drive
= floppy
->drive
;
1571 void __user
*argp
= (void __user
*)arg
;
1577 case CDROM_LOCKDOOR
:
1578 return ide_floppy_lockdoor(floppy
, &pc
, arg
, cmd
);
1579 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED
:
1581 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY
:
1582 return ide_floppy_get_format_capacities(drive
, argp
);
1583 case IDEFLOPPY_IOCTL_FORMAT_START
:
1584 if (!(file
->f_mode
& 2))
1587 return ide_floppy_format_unit(floppy
, (int __user
*)arg
);
1588 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS
:
1589 return idefloppy_get_format_progress(drive
, argp
);
1593 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1594 * and CDROM_SEND_PACKET (legacy) ioctls
1596 if (cmd
!= CDROM_SEND_PACKET
&& cmd
!= SCSI_IOCTL_SEND_COMMAND
)
1597 err
= scsi_cmd_ioctl(file
, bdev
->bd_disk
->queue
,
1598 bdev
->bd_disk
, cmd
, argp
);
1603 err
= generic_ide_ioctl(drive
, file
, bdev
, cmd
, arg
);
1608 static int idefloppy_media_changed(struct gendisk
*disk
)
1610 struct ide_floppy_obj
*floppy
= ide_floppy_g(disk
);
1611 ide_drive_t
*drive
= floppy
->drive
;
1614 /* do not scan partitions twice if this is a removable device */
1615 if (drive
->attach
) {
1619 ret
= !!(floppy
->flags
& IDEFLOPPY_FLAG_MEDIA_CHANGED
);
1620 floppy
->flags
&= ~IDEFLOPPY_FLAG_MEDIA_CHANGED
;
1624 static int idefloppy_revalidate_disk(struct gendisk
*disk
)
1626 struct ide_floppy_obj
*floppy
= ide_floppy_g(disk
);
1627 set_capacity(disk
, idefloppy_capacity(floppy
->drive
));
1631 static struct block_device_operations idefloppy_ops
= {
1632 .owner
= THIS_MODULE
,
1633 .open
= idefloppy_open
,
1634 .release
= idefloppy_release
,
1635 .ioctl
= idefloppy_ioctl
,
1636 .getgeo
= idefloppy_getgeo
,
1637 .media_changed
= idefloppy_media_changed
,
1638 .revalidate_disk
= idefloppy_revalidate_disk
1641 static int ide_floppy_probe(ide_drive_t
*drive
)
1643 idefloppy_floppy_t
*floppy
;
1646 if (!strstr("ide-floppy", drive
->driver_req
))
1648 if (!drive
->present
)
1650 if (drive
->media
!= ide_floppy
)
1652 if (!idefloppy_identify_device(drive
, drive
->id
)) {
1653 printk(KERN_ERR
"ide-floppy: %s: not supported by this version"
1654 " of ide-floppy\n", drive
->name
);
1658 printk(KERN_INFO
"ide-floppy: passing drive %s to ide-scsi"
1659 " emulation.\n", drive
->name
);
1662 floppy
= kzalloc(sizeof(idefloppy_floppy_t
), GFP_KERNEL
);
1664 printk(KERN_ERR
"ide-floppy: %s: Can't allocate a floppy"
1665 " structure\n", drive
->name
);
1669 g
= alloc_disk(1 << PARTN_BITS
);
1671 goto out_free_floppy
;
1673 ide_init_disk(g
, drive
);
1675 ide_proc_register_driver(drive
, &idefloppy_driver
);
1677 kref_init(&floppy
->kref
);
1679 floppy
->drive
= drive
;
1680 floppy
->driver
= &idefloppy_driver
;
1683 g
->private_data
= &floppy
->driver
;
1685 drive
->driver_data
= floppy
;
1687 idefloppy_setup(drive
, floppy
);
1689 g
->minors
= 1 << PARTN_BITS
;
1690 g
->driverfs_dev
= &drive
->gendev
;
1691 g
->flags
= drive
->removable
? GENHD_FL_REMOVABLE
: 0;
1692 g
->fops
= &idefloppy_ops
;
1703 static void __exit
idefloppy_exit(void)
1705 driver_unregister(&idefloppy_driver
.gen_driver
);
1708 static int __init
idefloppy_init(void)
1710 printk("ide-floppy driver " IDEFLOPPY_VERSION
"\n");
1711 return driver_register(&idefloppy_driver
.gen_driver
);
1714 MODULE_ALIAS("ide:*m-floppy*");
1715 module_init(idefloppy_init
);
1716 module_exit(idefloppy_exit
);
1717 MODULE_LICENSE("GPL");
1718 MODULE_DESCRIPTION("ATAPI FLOPPY Driver");