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 /* Packet command flag bits. */
83 /* 1 when we prefer to use DMA if possible */
84 PC_FLAG_DMA_RECOMMENDED
= (1 << 0),
85 /* 1 while DMA in progress */
86 PC_FLAG_DMA_IN_PROGRESS
= (1 << 1),
87 /* 1 when encountered problem during DMA */
88 PC_FLAG_DMA_ERROR
= (1 << 2),
90 PC_FLAG_WRITING
= (1 << 3),
91 /* Suppress error reporting */
92 PC_FLAG_SUPPRESS_ERROR
= (1 << 4),
95 /* format capacities descriptor codes */
96 #define CAPACITY_INVALID 0x00
97 #define CAPACITY_UNFORMATTED 0x01
98 #define CAPACITY_CURRENT 0x02
99 #define CAPACITY_NO_CARTRIDGE 0x03
102 * Most of our global data which we need to save even as we leave the driver
103 * due to an interrupt or a timer event is stored in a variable of type
104 * idefloppy_floppy_t, defined below.
106 typedef struct ide_floppy_obj
{
108 ide_driver_t
*driver
;
109 struct gendisk
*disk
;
111 unsigned int openers
; /* protected by BKL for now */
113 /* Current packet command */
114 struct ide_atapi_pc
*pc
;
115 /* Last failed packet command */
116 struct ide_atapi_pc
*failed_pc
;
117 /* Packet command stack */
118 struct ide_atapi_pc pc_stack
[IDEFLOPPY_PC_STACK
];
119 /* Next free packet command storage space */
121 struct request rq_stack
[IDEFLOPPY_PC_STACK
];
122 /* We implement a circular array */
125 /* Last error information */
126 u8 sense_key
, asc
, ascq
;
127 /* delay this long before sending packet command */
129 int progress_indication
;
131 /* Device information */
133 int blocks
, block_size
, bs_factor
;
134 /* Last format capacity descriptor */
136 /* Copy of the flexible disk page */
137 u8 flexible_disk_page
[32];
140 /* Supports format progress report */
142 /* Status/Action flags */
144 } idefloppy_floppy_t
;
146 #define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
148 /* Floppy flag bits values. */
150 /* DRQ interrupt device */
151 IDEFLOPPY_FLAG_DRQ_INTERRUPT
= (1 << 0),
152 /* Media may have changed */
153 IDEFLOPPY_FLAG_MEDIA_CHANGED
= (1 << 1),
154 /* Format in progress */
155 IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS
= (1 << 2),
156 /* Avoid commands not supported in Clik drive */
157 IDEFLOPPY_FLAG_CLIK_DRIVE
= (1 << 3),
158 /* Requires BH algorithm for packets */
159 IDEFLOPPY_FLAG_ZIP_DRIVE
= (1 << 4),
162 /* Defines for the MODE SENSE command */
163 #define MODE_SENSE_CURRENT 0x00
164 #define MODE_SENSE_CHANGEABLE 0x01
165 #define MODE_SENSE_DEFAULT 0x02
166 #define MODE_SENSE_SAVED 0x03
168 /* IOCTLs used in low-level formatting. */
169 #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
170 #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
171 #define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
172 #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
174 /* Error code returned in rq->errors to the higher part of the driver. */
175 #define IDEFLOPPY_ERROR_GENERAL 101
178 * Pages of the SELECT SENSE / MODE SENSE packet commands.
179 * See SFF-8070i spec.
181 #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
182 #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
184 static DEFINE_MUTEX(idefloppy_ref_mutex
);
186 #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
188 #define ide_floppy_g(disk) \
189 container_of((disk)->private_data, struct ide_floppy_obj, driver)
191 static struct ide_floppy_obj
*ide_floppy_get(struct gendisk
*disk
)
193 struct ide_floppy_obj
*floppy
= NULL
;
195 mutex_lock(&idefloppy_ref_mutex
);
196 floppy
= ide_floppy_g(disk
);
198 kref_get(&floppy
->kref
);
199 mutex_unlock(&idefloppy_ref_mutex
);
203 static void idefloppy_cleanup_obj(struct kref
*);
205 static void ide_floppy_put(struct ide_floppy_obj
*floppy
)
207 mutex_lock(&idefloppy_ref_mutex
);
208 kref_put(&floppy
->kref
, idefloppy_cleanup_obj
);
209 mutex_unlock(&idefloppy_ref_mutex
);
213 * Used to finish servicing a request. For read/write requests, we will call
214 * ide_end_request to pass to the next buffer.
216 static int idefloppy_do_end_request(ide_drive_t
*drive
, int uptodate
, int nsecs
)
218 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
219 struct request
*rq
= HWGROUP(drive
)->rq
;
222 debug_log("Reached %s\n", __func__
);
225 case 0: error
= IDEFLOPPY_ERROR_GENERAL
; break;
226 case 1: error
= 0; break;
227 default: error
= uptodate
;
230 floppy
->failed_pc
= NULL
;
231 /* Why does this happen? */
234 if (!blk_special_request(rq
)) {
235 /* our real local end request function */
236 ide_end_request(drive
, uptodate
, nsecs
);
240 /* fixme: need to move this local also */
241 ide_end_drive_cmd(drive
, 0, 0);
245 static void ide_floppy_io_buffers(ide_drive_t
*drive
, struct ide_atapi_pc
*pc
,
246 unsigned int bcount
, int direction
)
248 struct request
*rq
= pc
->rq
;
249 struct req_iterator iter
;
250 struct bio_vec
*bvec
;
255 rq_for_each_segment(bvec
, rq
, iter
) {
259 count
= min(bvec
->bv_len
, bcount
);
261 data
= bvec_kmap_irq(bvec
, &flags
);
263 drive
->hwif
->atapi_output_bytes(drive
, data
, count
);
265 drive
->hwif
->atapi_input_bytes(drive
, data
, count
);
266 bvec_kunmap_irq(data
, &flags
);
269 pc
->b_count
+= count
;
273 idefloppy_do_end_request(drive
, 1, done
>> 9);
276 printk(KERN_ERR
"%s: leftover data in %s, bcount == %d\n",
277 drive
->name
, __func__
, bcount
);
279 ide_atapi_write_zeros(drive
, bcount
);
281 ide_atapi_discard_data(drive
, bcount
);
285 static void idefloppy_update_buffers(ide_drive_t
*drive
,
286 struct ide_atapi_pc
*pc
)
288 struct request
*rq
= pc
->rq
;
289 struct bio
*bio
= rq
->bio
;
291 while ((bio
= rq
->bio
) != NULL
)
292 idefloppy_do_end_request(drive
, 1, 0);
296 * Generate a new packet command request in front of the request queue, before
297 * the current request so that it will be processed immediately, on the next
298 * pass through the driver.
300 static void idefloppy_queue_pc_head(ide_drive_t
*drive
, struct ide_atapi_pc
*pc
,
303 struct ide_floppy_obj
*floppy
= drive
->driver_data
;
305 ide_init_drive_cmd(rq
);
306 rq
->buffer
= (char *) pc
;
307 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
308 rq
->rq_disk
= floppy
->disk
;
309 (void) ide_do_drive_cmd(drive
, rq
, ide_preempt
);
312 static struct ide_atapi_pc
*idefloppy_next_pc_storage(ide_drive_t
*drive
)
314 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
316 if (floppy
->pc_stack_index
== IDEFLOPPY_PC_STACK
)
317 floppy
->pc_stack_index
= 0;
318 return (&floppy
->pc_stack
[floppy
->pc_stack_index
++]);
321 static struct request
*idefloppy_next_rq_storage(ide_drive_t
*drive
)
323 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
325 if (floppy
->rq_stack_index
== IDEFLOPPY_PC_STACK
)
326 floppy
->rq_stack_index
= 0;
327 return (&floppy
->rq_stack
[floppy
->rq_stack_index
++]);
330 static void idefloppy_request_sense_callback(ide_drive_t
*drive
)
332 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
333 u8
*buf
= floppy
->pc
->buf
;
335 debug_log("Reached %s\n", __func__
);
337 if (!floppy
->pc
->error
) {
338 floppy
->sense_key
= buf
[2] & 0x0F;
339 floppy
->asc
= buf
[12];
340 floppy
->ascq
= buf
[13];
341 floppy
->progress_indication
= buf
[15] & 0x80 ?
342 (u16
)get_unaligned((u16
*)&buf
[16]) : 0x10000;
344 if (floppy
->failed_pc
)
345 debug_log("pc = %x, sense key = %x, asc = %x,"
347 floppy
->failed_pc
->c
[0],
352 debug_log("sense key = %x, asc = %x, ascq = %x\n",
358 idefloppy_do_end_request(drive
, 1, 0);
360 printk(KERN_ERR
"Error in REQUEST SENSE itself - Aborting"
362 idefloppy_do_end_request(drive
, 0, 0);
366 /* General packet command callback function. */
367 static void idefloppy_pc_callback(ide_drive_t
*drive
)
369 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
371 debug_log("Reached %s\n", __func__
);
373 idefloppy_do_end_request(drive
, floppy
->pc
->error
? 0 : 1, 0);
376 static void idefloppy_init_pc(struct ide_atapi_pc
*pc
)
378 memset(pc
->c
, 0, 12);
382 pc
->buf
= pc
->pc_buf
;
383 pc
->buf_size
= IDEFLOPPY_PC_BUFFER_SIZE
;
384 pc
->idefloppy_callback
= &idefloppy_pc_callback
;
387 static void idefloppy_create_request_sense_cmd(struct ide_atapi_pc
*pc
)
389 idefloppy_init_pc(pc
);
390 pc
->c
[0] = GPCMD_REQUEST_SENSE
;
393 pc
->idefloppy_callback
= &idefloppy_request_sense_callback
;
397 * Called when an error was detected during the last packet command. We queue a
398 * request sense packet command in the head of the request list.
400 static void idefloppy_retry_pc(ide_drive_t
*drive
)
402 struct ide_atapi_pc
*pc
;
405 (void)ide_read_error(drive
);
406 pc
= idefloppy_next_pc_storage(drive
);
407 rq
= idefloppy_next_rq_storage(drive
);
408 idefloppy_create_request_sense_cmd(pc
);
409 idefloppy_queue_pc_head(drive
, pc
, rq
);
412 /* The usual interrupt handler called during a packet command. */
413 static ide_startstop_t
idefloppy_pc_intr (ide_drive_t
*drive
)
415 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
416 ide_hwif_t
*hwif
= drive
->hwif
;
417 struct ide_atapi_pc
*pc
= floppy
->pc
;
418 struct request
*rq
= pc
->rq
;
419 xfer_func_t
*xferfunc
;
425 debug_log("Reached %s interrupt handler\n", __func__
);
427 if (pc
->flags
& PC_FLAG_DMA_IN_PROGRESS
) {
428 dma_error
= hwif
->ide_dma_end(drive
);
430 printk(KERN_ERR
"%s: DMA %s error\n", drive
->name
,
431 rq_data_dir(rq
) ? "write" : "read");
432 pc
->flags
|= PC_FLAG_DMA_ERROR
;
434 pc
->xferred
= pc
->req_xfer
;
435 idefloppy_update_buffers(drive
, pc
);
437 debug_log("DMA finished\n");
440 /* Clear the interrupt */
441 stat
= ide_read_status(drive
);
443 /* No more interrupts */
444 if ((stat
& DRQ_STAT
) == 0) {
445 debug_log("Packet command completed, %d bytes transferred\n",
447 pc
->flags
&= ~PC_FLAG_DMA_IN_PROGRESS
;
449 local_irq_enable_in_hardirq();
451 if ((stat
& ERR_STAT
) || (pc
->flags
& PC_FLAG_DMA_ERROR
)) {
453 debug_log("%s: I/O error\n", drive
->name
);
455 if (pc
->c
[0] == GPCMD_REQUEST_SENSE
) {
456 printk(KERN_ERR
"ide-floppy: I/O error in "
457 "request sense command\n");
458 return ide_do_reset(drive
);
460 /* Retry operation */
461 idefloppy_retry_pc(drive
);
462 /* queued, but not started */
466 if (floppy
->failed_pc
== pc
)
467 floppy
->failed_pc
= NULL
;
468 /* Command finished - Call the callback function */
469 pc
->idefloppy_callback(drive
);
473 if (pc
->flags
& PC_FLAG_DMA_IN_PROGRESS
) {
474 pc
->flags
&= ~PC_FLAG_DMA_IN_PROGRESS
;
475 printk(KERN_ERR
"ide-floppy: The floppy wants to issue "
476 "more interrupts in DMA mode\n");
478 return ide_do_reset(drive
);
481 /* Get the number of bytes to transfer */
482 bcount
= (hwif
->INB(hwif
->io_ports
[IDE_BCOUNTH_OFFSET
]) << 8) |
483 hwif
->INB(hwif
->io_ports
[IDE_BCOUNTL_OFFSET
]);
484 /* on this interrupt */
485 ireason
= hwif
->INB(hwif
->io_ports
[IDE_IREASON_OFFSET
]);
488 printk(KERN_ERR
"ide-floppy: CoD != 0 in %s\n", __func__
);
489 return ide_do_reset(drive
);
491 if (((ireason
& IO
) == IO
) == !!(pc
->flags
& PC_FLAG_WRITING
)) {
492 /* Hopefully, we will never get here */
493 printk(KERN_ERR
"ide-floppy: We wanted to %s, ",
494 (ireason
& IO
) ? "Write" : "Read");
495 printk(KERN_ERR
"but the floppy wants us to %s !\n",
496 (ireason
& IO
) ? "Read" : "Write");
497 return ide_do_reset(drive
);
499 if (!(pc
->flags
& PC_FLAG_WRITING
)) {
500 /* Reading - Check that we have enough space */
501 temp
= pc
->xferred
+ bcount
;
502 if (temp
> pc
->req_xfer
) {
503 if (temp
> pc
->buf_size
) {
504 printk(KERN_ERR
"ide-floppy: The floppy wants "
505 "to send us more data than expected "
506 "- discarding data\n");
507 ide_atapi_discard_data(drive
, bcount
);
509 ide_set_handler(drive
,
515 debug_log("The floppy wants to send us more data than"
516 " expected - allowing transfer\n");
519 if (pc
->flags
& PC_FLAG_WRITING
)
520 xferfunc
= hwif
->atapi_output_bytes
;
522 xferfunc
= hwif
->atapi_input_bytes
;
525 xferfunc(drive
, pc
->cur_pos
, bcount
);
527 ide_floppy_io_buffers(drive
, pc
, bcount
,
528 !!(pc
->flags
& PC_FLAG_WRITING
));
530 /* Update the current position */
531 pc
->xferred
+= bcount
;
532 pc
->cur_pos
+= bcount
;
534 /* And set the interrupt handler again */
535 ide_set_handler(drive
, &idefloppy_pc_intr
, IDEFLOPPY_WAIT_CMD
, NULL
);
540 * This is the original routine that did the packet transfer.
541 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
542 * for that drive below. The algorithm is chosen based on drive type
544 static ide_startstop_t
idefloppy_transfer_pc(ide_drive_t
*drive
)
546 ide_hwif_t
*hwif
= drive
->hwif
;
547 ide_startstop_t startstop
;
548 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
551 if (ide_wait_stat(&startstop
, drive
, DRQ_STAT
, BUSY_STAT
, WAIT_READY
)) {
552 printk(KERN_ERR
"ide-floppy: Strange, packet command "
553 "initiated yet DRQ isn't asserted\n");
556 ireason
= hwif
->INB(hwif
->io_ports
[IDE_IREASON_OFFSET
]);
557 if ((ireason
& CD
) == 0 || (ireason
& IO
)) {
558 printk(KERN_ERR
"ide-floppy: (IO,CoD) != (0,1) while "
559 "issuing a packet command\n");
560 return ide_do_reset(drive
);
563 /* Set the interrupt routine */
564 ide_set_handler(drive
, &idefloppy_pc_intr
, IDEFLOPPY_WAIT_CMD
, NULL
);
565 /* Send the actual packet */
566 HWIF(drive
)->atapi_output_bytes(drive
, floppy
->pc
->c
, 12);
572 * What we have here is a classic case of a top half / bottom half interrupt
573 * service routine. In interrupt mode, the device sends an interrupt to signal
574 * that it is ready to receive a packet. However, we need to delay about 2-3
575 * ticks before issuing the packet or we gets in trouble.
577 * So, follow carefully. transfer_pc1 is called as an interrupt (or directly).
578 * In either case, when the device says it's ready for a packet, we schedule
579 * the packet transfer to occur about 2-3 ticks later in transfer_pc2.
581 static int idefloppy_transfer_pc2(ide_drive_t
*drive
)
583 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
585 /* Send the actual packet */
586 HWIF(drive
)->atapi_output_bytes(drive
, floppy
->pc
->c
, 12);
587 /* Timeout for the packet command */
588 return IDEFLOPPY_WAIT_CMD
;
591 static ide_startstop_t
idefloppy_transfer_pc1(ide_drive_t
*drive
)
593 ide_hwif_t
*hwif
= drive
->hwif
;
594 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
595 ide_startstop_t startstop
;
598 if (ide_wait_stat(&startstop
, drive
, DRQ_STAT
, BUSY_STAT
, WAIT_READY
)) {
599 printk(KERN_ERR
"ide-floppy: Strange, packet command "
600 "initiated yet DRQ isn't asserted\n");
603 ireason
= hwif
->INB(hwif
->io_ports
[IDE_IREASON_OFFSET
]);
604 if ((ireason
& CD
) == 0 || (ireason
& IO
)) {
605 printk(KERN_ERR
"ide-floppy: (IO,CoD) != (0,1) "
606 "while issuing a packet command\n");
607 return ide_do_reset(drive
);
610 * The following delay solves a problem with ATAPI Zip 100 drives
611 * where the Busy flag was apparently being deasserted before the
612 * unit was ready to receive data. This was happening on a
613 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
614 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
615 * used until after the packet is moved in about 50 msec.
618 ide_set_handler(drive
, &idefloppy_pc_intr
, floppy
->ticks
,
619 &idefloppy_transfer_pc2
);
623 static void ide_floppy_report_error(idefloppy_floppy_t
*floppy
,
624 struct ide_atapi_pc
*pc
)
626 /* supress error messages resulting from Medium not present */
627 if (floppy
->sense_key
== 0x02 &&
628 floppy
->asc
== 0x3a &&
629 floppy
->ascq
== 0x00)
632 printk(KERN_ERR
"ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
633 "asc = %2x, ascq = %2x\n",
634 floppy
->drive
->name
, pc
->c
[0], floppy
->sense_key
,
635 floppy
->asc
, floppy
->ascq
);
639 static ide_startstop_t
idefloppy_issue_pc(ide_drive_t
*drive
,
640 struct ide_atapi_pc
*pc
)
642 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
643 ide_hwif_t
*hwif
= drive
->hwif
;
644 ide_handler_t
*pkt_xfer_routine
;
648 if (floppy
->failed_pc
== NULL
&&
649 pc
->c
[0] != GPCMD_REQUEST_SENSE
)
650 floppy
->failed_pc
= pc
;
651 /* Set the current packet command */
654 if (pc
->retries
> IDEFLOPPY_MAX_PC_RETRIES
) {
655 if (!(pc
->flags
& PC_FLAG_SUPPRESS_ERROR
))
656 ide_floppy_report_error(floppy
, pc
);
658 pc
->error
= IDEFLOPPY_ERROR_GENERAL
;
660 floppy
->failed_pc
= NULL
;
661 pc
->idefloppy_callback(drive
);
665 debug_log("Retry number - %d\n", pc
->retries
);
668 /* We haven't transferred any data yet */
670 pc
->cur_pos
= pc
->buf
;
671 bcount
= min(pc
->req_xfer
, 63 * 1024);
673 if (pc
->flags
& PC_FLAG_DMA_ERROR
) {
674 pc
->flags
&= ~PC_FLAG_DMA_ERROR
;
679 if ((pc
->flags
& PC_FLAG_DMA_RECOMMENDED
) && drive
->using_dma
)
680 dma
= !hwif
->dma_setup(drive
);
682 ide_pktcmd_tf_load(drive
, IDE_TFLAG_NO_SELECT_MASK
|
683 IDE_TFLAG_OUT_DEVICE
, bcount
, dma
);
686 /* Begin DMA, if necessary */
687 pc
->flags
|= PC_FLAG_DMA_IN_PROGRESS
;
688 hwif
->dma_start(drive
);
691 /* Can we transfer the packet when we get the interrupt or wait? */
692 if (floppy
->flags
& IDEFLOPPY_FLAG_ZIP_DRIVE
) {
694 pkt_xfer_routine
= &idefloppy_transfer_pc1
;
697 pkt_xfer_routine
= &idefloppy_transfer_pc
;
700 if (floppy
->flags
& IDEFLOPPY_FLAG_DRQ_INTERRUPT
) {
701 /* Issue the packet command */
702 ide_execute_command(drive
, WIN_PACKETCMD
,
708 /* Issue the packet command */
709 hwif
->OUTB(WIN_PACKETCMD
, hwif
->io_ports
[IDE_COMMAND_OFFSET
]);
710 return (*pkt_xfer_routine
) (drive
);
714 static void idefloppy_rw_callback(ide_drive_t
*drive
)
716 debug_log("Reached %s\n", __func__
);
718 idefloppy_do_end_request(drive
, 1, 0);
722 static void idefloppy_create_prevent_cmd(struct ide_atapi_pc
*pc
, int prevent
)
724 debug_log("creating prevent removal command, prevent = %d\n", prevent
);
726 idefloppy_init_pc(pc
);
727 pc
->c
[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL
;
731 static void idefloppy_create_read_capacity_cmd(struct ide_atapi_pc
*pc
)
733 idefloppy_init_pc(pc
);
734 pc
->c
[0] = GPCMD_READ_FORMAT_CAPACITIES
;
740 static void idefloppy_create_format_unit_cmd(struct ide_atapi_pc
*pc
, int b
,
743 idefloppy_init_pc(pc
);
744 pc
->c
[0] = GPCMD_FORMAT_UNIT
;
747 memset(pc
->buf
, 0, 12);
749 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
751 if (flags
& 1) /* Verify bit on... */
752 pc
->buf
[1] ^= 0x20; /* ... turn off DCRT bit */
755 put_unaligned(cpu_to_be32(b
), (unsigned int *)(&pc
->buf
[4]));
756 put_unaligned(cpu_to_be32(l
), (unsigned int *)(&pc
->buf
[8]));
758 pc
->flags
|= PC_FLAG_WRITING
;
761 /* A mode sense command is used to "sense" floppy parameters. */
762 static void idefloppy_create_mode_sense_cmd(struct ide_atapi_pc
*pc
,
763 u8 page_code
, u8 type
)
765 u16 length
= 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
767 idefloppy_init_pc(pc
);
768 pc
->c
[0] = GPCMD_MODE_SENSE_10
;
770 pc
->c
[2] = page_code
+ (type
<< 6);
773 case IDEFLOPPY_CAPABILITIES_PAGE
:
776 case IDEFLOPPY_FLEXIBLE_DISK_PAGE
:
780 printk(KERN_ERR
"ide-floppy: unsupported page code "
781 "in create_mode_sense_cmd\n");
783 put_unaligned(cpu_to_be16(length
), (u16
*) &pc
->c
[7]);
784 pc
->req_xfer
= length
;
787 static void idefloppy_create_start_stop_cmd(struct ide_atapi_pc
*pc
, int start
)
789 idefloppy_init_pc(pc
);
790 pc
->c
[0] = GPCMD_START_STOP_UNIT
;
794 static void idefloppy_create_test_unit_ready_cmd(struct ide_atapi_pc
*pc
)
796 idefloppy_init_pc(pc
);
797 pc
->c
[0] = GPCMD_TEST_UNIT_READY
;
800 static void idefloppy_create_rw_cmd(idefloppy_floppy_t
*floppy
,
801 struct ide_atapi_pc
*pc
, struct request
*rq
,
802 unsigned long sector
)
804 int block
= sector
/ floppy
->bs_factor
;
805 int blocks
= rq
->nr_sectors
/ floppy
->bs_factor
;
806 int cmd
= rq_data_dir(rq
);
808 debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
811 idefloppy_init_pc(pc
);
812 pc
->c
[0] = cmd
== READ
? GPCMD_READ_10
: GPCMD_WRITE_10
;
813 put_unaligned(cpu_to_be16(blocks
), (unsigned short *)&pc
->c
[7]);
814 put_unaligned(cpu_to_be32(block
), (unsigned int *) &pc
->c
[2]);
816 pc
->idefloppy_callback
= &idefloppy_rw_callback
;
818 pc
->b_count
= cmd
== READ
? 0 : rq
->bio
->bi_size
;
819 if (rq
->cmd_flags
& REQ_RW
)
820 pc
->flags
|= PC_FLAG_WRITING
;
822 pc
->req_xfer
= pc
->buf_size
= blocks
* floppy
->block_size
;
823 pc
->flags
|= PC_FLAG_DMA_RECOMMENDED
;
826 static void idefloppy_blockpc_cmd(idefloppy_floppy_t
*floppy
,
827 struct ide_atapi_pc
*pc
, struct request
*rq
)
829 idefloppy_init_pc(pc
);
830 pc
->idefloppy_callback
= &idefloppy_rw_callback
;
831 memcpy(pc
->c
, rq
->cmd
, sizeof(pc
->c
));
833 pc
->b_count
= rq
->data_len
;
834 if (rq
->data_len
&& rq_data_dir(rq
) == WRITE
)
835 pc
->flags
|= PC_FLAG_WRITING
;
838 pc
->flags
|= PC_FLAG_DMA_RECOMMENDED
;
840 * possibly problematic, doesn't look like ide-floppy correctly
841 * handled scattered requests if dma fails...
843 pc
->req_xfer
= pc
->buf_size
= rq
->data_len
;
846 static ide_startstop_t
idefloppy_do_request(ide_drive_t
*drive
,
847 struct request
*rq
, sector_t block_s
)
849 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
850 struct ide_atapi_pc
*pc
;
851 unsigned long block
= (unsigned long)block_s
;
853 debug_log("dev: %s, cmd_type: %x, errors: %d\n",
854 rq
->rq_disk
? rq
->rq_disk
->disk_name
: "?",
855 rq
->cmd_type
, rq
->errors
);
856 debug_log("sector: %ld, nr_sectors: %ld, "
857 "current_nr_sectors: %d\n", (long)rq
->sector
,
858 rq
->nr_sectors
, rq
->current_nr_sectors
);
860 if (rq
->errors
>= ERROR_MAX
) {
861 if (floppy
->failed_pc
)
862 ide_floppy_report_error(floppy
, floppy
->failed_pc
);
864 printk(KERN_ERR
"ide-floppy: %s: I/O error\n",
866 idefloppy_do_end_request(drive
, 0, 0);
869 if (blk_fs_request(rq
)) {
870 if (((long)rq
->sector
% floppy
->bs_factor
) ||
871 (rq
->nr_sectors
% floppy
->bs_factor
)) {
872 printk(KERN_ERR
"%s: unsupported r/w request size\n",
874 idefloppy_do_end_request(drive
, 0, 0);
877 pc
= idefloppy_next_pc_storage(drive
);
878 idefloppy_create_rw_cmd(floppy
, pc
, rq
, block
);
879 } else if (blk_special_request(rq
)) {
880 pc
= (struct ide_atapi_pc
*) rq
->buffer
;
881 } else if (blk_pc_request(rq
)) {
882 pc
= idefloppy_next_pc_storage(drive
);
883 idefloppy_blockpc_cmd(floppy
, pc
, rq
);
885 blk_dump_rq_flags(rq
,
886 "ide-floppy: unsupported command in queue");
887 idefloppy_do_end_request(drive
, 0, 0);
892 return idefloppy_issue_pc(drive
, pc
);
896 * Add a special packet command request to the tail of the request queue,
897 * and wait for it to be serviced.
899 static int idefloppy_queue_pc_tail(ide_drive_t
*drive
, struct ide_atapi_pc
*pc
)
901 struct ide_floppy_obj
*floppy
= drive
->driver_data
;
904 ide_init_drive_cmd(&rq
);
905 rq
.buffer
= (char *) pc
;
906 rq
.cmd_type
= REQ_TYPE_SPECIAL
;
907 rq
.rq_disk
= floppy
->disk
;
909 return ide_do_drive_cmd(drive
, &rq
, ide_wait
);
913 * Look at the flexible disk page parameters. We ignore the CHS capacity
914 * parameters and use the LBA parameters instead.
916 static int ide_floppy_get_flexible_disk_page(ide_drive_t
*drive
)
918 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
919 struct ide_atapi_pc pc
;
921 int capacity
, lba_capacity
;
922 u16 transfer_rate
, sector_size
, cyls
, rpm
;
925 idefloppy_create_mode_sense_cmd(&pc
, IDEFLOPPY_FLEXIBLE_DISK_PAGE
,
928 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
929 printk(KERN_ERR
"ide-floppy: Can't get flexible disk page"
933 floppy
->wp
= !!(pc
.buf
[3] & 0x80);
934 set_disk_ro(floppy
->disk
, floppy
->wp
);
937 transfer_rate
= be16_to_cpu(*(u16
*)&pc
.buf
[8 + 2]);
938 sector_size
= be16_to_cpu(*(u16
*)&pc
.buf
[8 + 6]);
939 cyls
= be16_to_cpu(*(u16
*)&pc
.buf
[8 + 8]);
940 rpm
= be16_to_cpu(*(u16
*)&pc
.buf
[8 + 28]);
941 heads
= pc
.buf
[8 + 4];
942 sectors
= pc
.buf
[8 + 5];
944 capacity
= cyls
* heads
* sectors
* sector_size
;
946 if (memcmp(page
, &floppy
->flexible_disk_page
, 32))
947 printk(KERN_INFO
"%s: %dkB, %d/%d/%d CHS, %d kBps, "
948 "%d sector size, %d rpm\n",
949 drive
->name
, capacity
/ 1024, cyls
, heads
,
950 sectors
, transfer_rate
/ 8, sector_size
, rpm
);
952 memcpy(&floppy
->flexible_disk_page
, page
, 32);
953 drive
->bios_cyl
= cyls
;
954 drive
->bios_head
= heads
;
955 drive
->bios_sect
= sectors
;
956 lba_capacity
= floppy
->blocks
* floppy
->block_size
;
958 if (capacity
< lba_capacity
) {
959 printk(KERN_NOTICE
"%s: The disk reports a capacity of %d "
960 "bytes, but the drive only handles %d\n",
961 drive
->name
, lba_capacity
, capacity
);
962 floppy
->blocks
= floppy
->block_size
?
963 capacity
/ floppy
->block_size
: 0;
968 static int idefloppy_get_sfrp_bit(ide_drive_t
*drive
)
970 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
971 struct ide_atapi_pc pc
;
974 idefloppy_create_mode_sense_cmd(&pc
, IDEFLOPPY_CAPABILITIES_PAGE
,
977 pc
.flags
|= PC_FLAG_SUPPRESS_ERROR
;
978 if (idefloppy_queue_pc_tail(drive
, &pc
))
981 floppy
->srfp
= pc
.buf
[8 + 2] & 0x40;
986 * Determine if a media is present in the floppy drive, and if so, its LBA
989 static int ide_floppy_get_capacity(ide_drive_t
*drive
)
991 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
992 struct ide_atapi_pc pc
;
994 u8 header_len
, desc_cnt
;
995 int i
, rc
= 1, blocks
, length
;
998 drive
->bios_head
= drive
->bios_sect
= 0;
1000 floppy
->bs_factor
= 1;
1001 set_capacity(floppy
->disk
, 0);
1003 idefloppy_create_read_capacity_cmd(&pc
);
1004 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1005 printk(KERN_ERR
"ide-floppy: Can't get floppy parameters\n");
1008 header_len
= pc
.buf
[3];
1009 cap_desc
= &pc
.buf
[4];
1010 desc_cnt
= header_len
/ 8; /* capacity descriptor of 8 bytes */
1012 for (i
= 0; i
< desc_cnt
; i
++) {
1013 unsigned int desc_start
= 4 + i
*8;
1015 blocks
= be32_to_cpu(*(u32
*)&pc
.buf
[desc_start
]);
1016 length
= be16_to_cpu(*(u16
*)&pc
.buf
[desc_start
+ 6]);
1018 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
1019 i
, blocks
* length
/ 1024, blocks
, length
);
1024 * the code below is valid only for the 1st descriptor, ie i=0
1027 switch (pc
.buf
[desc_start
+ 4] & 0x03) {
1028 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1029 case CAPACITY_UNFORMATTED
:
1030 if (!(floppy
->flags
& IDEFLOPPY_FLAG_CLIK_DRIVE
))
1032 * If it is not a clik drive, break out
1033 * (maintains previous driver behaviour)
1036 case CAPACITY_CURRENT
:
1037 /* Normal Zip/LS-120 disks */
1038 if (memcmp(cap_desc
, &floppy
->cap_desc
, 8))
1039 printk(KERN_INFO
"%s: %dkB, %d blocks, %d "
1040 "sector size\n", drive
->name
,
1041 blocks
* length
/ 1024, blocks
, length
);
1042 memcpy(&floppy
->cap_desc
, cap_desc
, 8);
1044 if (!length
|| length
% 512) {
1045 printk(KERN_NOTICE
"%s: %d bytes block size "
1046 "not supported\n", drive
->name
, length
);
1048 floppy
->blocks
= blocks
;
1049 floppy
->block_size
= length
;
1050 floppy
->bs_factor
= length
/ 512;
1051 if (floppy
->bs_factor
!= 1)
1052 printk(KERN_NOTICE
"%s: warning: non "
1053 "512 bytes block size not "
1054 "fully supported\n",
1059 case CAPACITY_NO_CARTRIDGE
:
1061 * This is a KERN_ERR so it appears on screen
1062 * for the user to see
1064 printk(KERN_ERR
"%s: No disk in drive\n", drive
->name
);
1066 case CAPACITY_INVALID
:
1067 printk(KERN_ERR
"%s: Invalid capacity for disk "
1068 "in drive\n", drive
->name
);
1071 debug_log("Descriptor 0 Code: %d\n",
1072 pc
.buf
[desc_start
+ 4] & 0x03);
1075 /* Clik! disk does not support get_flexible_disk_page */
1076 if (!(floppy
->flags
& IDEFLOPPY_FLAG_CLIK_DRIVE
))
1077 (void) ide_floppy_get_flexible_disk_page(drive
);
1079 set_capacity(floppy
->disk
, floppy
->blocks
* floppy
->bs_factor
);
1084 * Obtain the list of formattable capacities.
1085 * Very similar to ide_floppy_get_capacity, except that we push the capacity
1086 * descriptors to userland, instead of our own structures.
1088 * Userland gives us the following structure:
1090 * struct idefloppy_format_capacities {
1098 * userland initializes nformats to the number of allocated formats[] records.
1099 * On exit we set nformats to the number of records we've actually initialized.
1102 static int ide_floppy_get_format_capacities(ide_drive_t
*drive
, int __user
*arg
)
1104 struct ide_atapi_pc pc
;
1105 u8 header_len
, desc_cnt
;
1106 int i
, blocks
, length
, u_array_size
, u_index
;
1109 if (get_user(u_array_size
, arg
))
1112 if (u_array_size
<= 0)
1115 idefloppy_create_read_capacity_cmd(&pc
);
1116 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1117 printk(KERN_ERR
"ide-floppy: Can't get floppy parameters\n");
1120 header_len
= pc
.buf
[3];
1121 desc_cnt
= header_len
/ 8; /* capacity descriptor of 8 bytes */
1127 * We always skip the first capacity descriptor. That's the current
1128 * capacity. We are interested in the remaining descriptors, the
1129 * formattable capacities.
1131 for (i
= 1; i
< desc_cnt
; i
++) {
1132 unsigned int desc_start
= 4 + i
*8;
1134 if (u_index
>= u_array_size
)
1135 break; /* User-supplied buffer too small */
1137 blocks
= be32_to_cpu(*(u32
*)&pc
.buf
[desc_start
]);
1138 length
= be16_to_cpu(*(u16
*)&pc
.buf
[desc_start
+ 6]);
1140 if (put_user(blocks
, argp
))
1144 if (put_user(length
, argp
))
1151 if (put_user(u_index
, arg
))
1157 * Get ATAPI_FORMAT_UNIT progress indication.
1159 * Userland gives a pointer to an int. The int is set to a progress
1160 * indicator 0-65536, with 65536=100%.
1162 * If the drive does not support format progress indication, we just check
1163 * the dsc bit, and return either 0 or 65536.
1166 static int idefloppy_get_format_progress(ide_drive_t
*drive
, int __user
*arg
)
1168 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1169 struct ide_atapi_pc pc
;
1170 int progress_indication
= 0x10000;
1173 idefloppy_create_request_sense_cmd(&pc
);
1174 if (idefloppy_queue_pc_tail(drive
, &pc
))
1177 if (floppy
->sense_key
== 2 &&
1180 progress_indication
= floppy
->progress_indication
;
1182 /* Else assume format_unit has finished, and we're at 0x10000 */
1184 unsigned long flags
;
1187 local_irq_save(flags
);
1188 stat
= ide_read_status(drive
);
1189 local_irq_restore(flags
);
1191 progress_indication
= ((stat
& SEEK_STAT
) == 0) ? 0 : 0x10000;
1193 if (put_user(progress_indication
, arg
))
1199 static sector_t
idefloppy_capacity(ide_drive_t
*drive
)
1201 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1202 unsigned long capacity
= floppy
->blocks
* floppy
->bs_factor
;
1208 * Check whether we can support a drive, based on the ATAPI IDENTIFY command
1211 static int idefloppy_identify_device(ide_drive_t
*drive
, struct hd_driveid
*id
)
1214 u8 device_type
, protocol
, removable
, drq_type
, packet_size
;
1216 *((u16
*) &gcw
) = id
->config
;
1218 device_type
= gcw
[1] & 0x1F;
1219 removable
= (gcw
[0] & 0x80) >> 7;
1220 protocol
= (gcw
[1] & 0xC0) >> 6;
1221 drq_type
= (gcw
[0] & 0x60) >> 5;
1222 packet_size
= gcw
[0] & 0x03;
1225 /* kludge for Apple PowerBook internal zip */
1226 if (device_type
== 5 &&
1227 !strstr(id
->model
, "CD-ROM") && strstr(id
->model
, "ZIP"))
1232 printk(KERN_ERR
"ide-floppy: Protocol (0x%02x) is not ATAPI\n",
1234 else if (device_type
!= 0)
1235 printk(KERN_ERR
"ide-floppy: Device type (0x%02x) is not set "
1236 "to floppy\n", device_type
);
1237 else if (!removable
)
1238 printk(KERN_ERR
"ide-floppy: The removable flag is not set\n");
1239 else if (drq_type
== 3)
1240 printk(KERN_ERR
"ide-floppy: Sorry, DRQ type (0x%02x) not "
1241 "supported\n", drq_type
);
1242 else if (packet_size
!= 0)
1243 printk(KERN_ERR
"ide-floppy: Packet size (0x%02x) is not 12 "
1244 "bytes\n", packet_size
);
1250 #ifdef CONFIG_IDE_PROC_FS
1251 static void idefloppy_add_settings(ide_drive_t
*drive
)
1253 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1255 ide_add_setting(drive
, "bios_cyl", SETTING_RW
, TYPE_INT
, 0, 1023, 1, 1,
1256 &drive
->bios_cyl
, NULL
);
1257 ide_add_setting(drive
, "bios_head", SETTING_RW
, TYPE_BYTE
, 0, 255, 1, 1,
1258 &drive
->bios_head
, NULL
);
1259 ide_add_setting(drive
, "bios_sect", SETTING_RW
, TYPE_BYTE
, 0, 63, 1, 1,
1260 &drive
->bios_sect
, NULL
);
1261 ide_add_setting(drive
, "ticks", SETTING_RW
, TYPE_BYTE
, 0, 255, 1, 1,
1262 &floppy
->ticks
, NULL
);
1265 static inline void idefloppy_add_settings(ide_drive_t
*drive
) { ; }
1268 static void idefloppy_setup(ide_drive_t
*drive
, idefloppy_floppy_t
*floppy
)
1272 *((u16
*) &gcw
) = drive
->id
->config
;
1273 floppy
->pc
= floppy
->pc_stack
;
1275 if (((gcw
[0] & 0x60) >> 5) == 1)
1276 floppy
->flags
|= IDEFLOPPY_FLAG_DRQ_INTERRUPT
;
1278 * We used to check revisions here. At this point however I'm giving up.
1279 * Just assume they are all broken, its easier.
1281 * The actual reason for the workarounds was likely a driver bug after
1282 * all rather than a firmware bug, and the workaround below used to hide
1283 * it. It should be fixed as of version 1.9, but to be on the safe side
1284 * we'll leave the limitation below for the 2.2.x tree.
1286 if (!strncmp(drive
->id
->model
, "IOMEGA ZIP 100 ATAPI", 20)) {
1287 floppy
->flags
|= IDEFLOPPY_FLAG_ZIP_DRIVE
;
1288 /* This value will be visible in the /proc/ide/hdx/settings */
1289 floppy
->ticks
= IDEFLOPPY_TICKS_DELAY
;
1290 blk_queue_max_sectors(drive
->queue
, 64);
1294 * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
1295 * nasty clicking noises without it, so please don't remove this.
1297 if (strncmp(drive
->id
->model
, "IOMEGA Clik!", 11) == 0) {
1298 blk_queue_max_sectors(drive
->queue
, 64);
1299 floppy
->flags
|= IDEFLOPPY_FLAG_CLIK_DRIVE
;
1302 (void) ide_floppy_get_capacity(drive
);
1303 idefloppy_add_settings(drive
);
1306 static void ide_floppy_remove(ide_drive_t
*drive
)
1308 idefloppy_floppy_t
*floppy
= drive
->driver_data
;
1309 struct gendisk
*g
= floppy
->disk
;
1311 ide_proc_unregister_driver(drive
, floppy
->driver
);
1315 ide_floppy_put(floppy
);
1318 static void idefloppy_cleanup_obj(struct kref
*kref
)
1320 struct ide_floppy_obj
*floppy
= to_ide_floppy(kref
);
1321 ide_drive_t
*drive
= floppy
->drive
;
1322 struct gendisk
*g
= floppy
->disk
;
1324 drive
->driver_data
= NULL
;
1325 g
->private_data
= NULL
;
1330 #ifdef CONFIG_IDE_PROC_FS
1331 static int proc_idefloppy_read_capacity(char *page
, char **start
, off_t off
,
1332 int count
, int *eof
, void *data
)
1334 ide_drive_t
*drive
= (ide_drive_t
*)data
;
1337 len
= sprintf(page
, "%llu\n", (long long)idefloppy_capacity(drive
));
1338 PROC_IDE_READ_RETURN(page
, start
, off
, count
, eof
, len
);
1341 static ide_proc_entry_t idefloppy_proc
[] = {
1342 { "capacity", S_IFREG
|S_IRUGO
, proc_idefloppy_read_capacity
, NULL
},
1343 { "geometry", S_IFREG
|S_IRUGO
, proc_ide_read_geometry
, NULL
},
1344 { NULL
, 0, NULL
, NULL
}
1346 #endif /* CONFIG_IDE_PROC_FS */
1348 static int ide_floppy_probe(ide_drive_t
*);
1350 static ide_driver_t idefloppy_driver
= {
1352 .owner
= THIS_MODULE
,
1353 .name
= "ide-floppy",
1354 .bus
= &ide_bus_type
,
1356 .probe
= ide_floppy_probe
,
1357 .remove
= ide_floppy_remove
,
1358 .version
= IDEFLOPPY_VERSION
,
1359 .media
= ide_floppy
,
1360 .supports_dsc_overlap
= 0,
1361 .do_request
= idefloppy_do_request
,
1362 .end_request
= idefloppy_do_end_request
,
1363 .error
= __ide_error
,
1364 .abort
= __ide_abort
,
1365 #ifdef CONFIG_IDE_PROC_FS
1366 .proc
= idefloppy_proc
,
1370 static int idefloppy_open(struct inode
*inode
, struct file
*filp
)
1372 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
1373 struct ide_floppy_obj
*floppy
;
1375 struct ide_atapi_pc pc
;
1378 debug_log("Reached %s\n", __func__
);
1380 floppy
= ide_floppy_get(disk
);
1384 drive
= floppy
->drive
;
1388 if (floppy
->openers
== 1) {
1389 floppy
->flags
&= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS
;
1392 idefloppy_create_test_unit_ready_cmd(&pc
);
1393 if (idefloppy_queue_pc_tail(drive
, &pc
)) {
1394 idefloppy_create_start_stop_cmd(&pc
, 1);
1395 (void) idefloppy_queue_pc_tail(drive
, &pc
);
1398 if (ide_floppy_get_capacity(drive
)
1399 && (filp
->f_flags
& O_NDELAY
) == 0
1401 * Allow O_NDELAY to open a drive without a disk, or with an
1402 * unreadable disk, so that we can get the format capacity
1403 * of the drive or begin the format - Sam
1407 goto out_put_floppy
;
1410 if (floppy
->wp
&& (filp
->f_mode
& 2)) {
1412 goto out_put_floppy
;
1414 floppy
->flags
|= IDEFLOPPY_FLAG_MEDIA_CHANGED
;
1415 /* IOMEGA Clik! drives do not support lock/unlock commands */
1416 if (!(floppy
->flags
& IDEFLOPPY_FLAG_CLIK_DRIVE
)) {
1417 idefloppy_create_prevent_cmd(&pc
, 1);
1418 (void) idefloppy_queue_pc_tail(drive
, &pc
);
1420 check_disk_change(inode
->i_bdev
);
1421 } else if (floppy
->flags
& IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS
) {
1423 goto out_put_floppy
;
1429 ide_floppy_put(floppy
);
1433 static int idefloppy_release(struct inode
*inode
, struct file
*filp
)
1435 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
1436 struct ide_floppy_obj
*floppy
= ide_floppy_g(disk
);
1437 ide_drive_t
*drive
= floppy
->drive
;
1438 struct ide_atapi_pc pc
;
1440 debug_log("Reached %s\n", __func__
);
1442 if (floppy
->openers
== 1) {
1443 /* IOMEGA Clik! drives do not support lock/unlock commands */
1444 if (!(floppy
->flags
& IDEFLOPPY_FLAG_CLIK_DRIVE
)) {
1445 idefloppy_create_prevent_cmd(&pc
, 0);
1446 (void) idefloppy_queue_pc_tail(drive
, &pc
);
1449 floppy
->flags
&= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS
;
1454 ide_floppy_put(floppy
);
1459 static int idefloppy_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1461 struct ide_floppy_obj
*floppy
= ide_floppy_g(bdev
->bd_disk
);
1462 ide_drive_t
*drive
= floppy
->drive
;
1464 geo
->heads
= drive
->bios_head
;
1465 geo
->sectors
= drive
->bios_sect
;
1466 geo
->cylinders
= (u16
)drive
->bios_cyl
; /* truncate */
1470 static int ide_floppy_lockdoor(idefloppy_floppy_t
*floppy
,
1471 struct ide_atapi_pc
*pc
, unsigned long arg
, unsigned int cmd
)
1473 if (floppy
->openers
> 1)
1476 /* The IOMEGA Clik! Drive doesn't support this command -
1477 * no room for an eject mechanism */
1478 if (!(floppy
->flags
& IDEFLOPPY_FLAG_CLIK_DRIVE
)) {
1479 int prevent
= arg
? 1 : 0;
1481 if (cmd
== CDROMEJECT
)
1484 idefloppy_create_prevent_cmd(pc
, prevent
);
1485 (void) idefloppy_queue_pc_tail(floppy
->drive
, pc
);
1488 if (cmd
== CDROMEJECT
) {
1489 idefloppy_create_start_stop_cmd(pc
, 2);
1490 (void) idefloppy_queue_pc_tail(floppy
->drive
, pc
);
1496 static int ide_floppy_format_unit(idefloppy_floppy_t
*floppy
,
1499 int blocks
, length
, flags
, err
= 0;
1500 struct ide_atapi_pc pc
;
1502 if (floppy
->openers
> 1) {
1503 /* Don't format if someone is using the disk */
1504 floppy
->flags
&= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS
;
1508 floppy
->flags
|= IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS
;
1511 * Send ATAPI_FORMAT_UNIT to the drive.
1513 * Userland gives us the following structure:
1515 * struct idefloppy_format_command {
1521 * flags is a bitmask, currently, the only defined flag is:
1523 * 0x01 - verify media after format.
1525 if (get_user(blocks
, arg
) ||
1526 get_user(length
, arg
+1) ||
1527 get_user(flags
, arg
+2)) {
1532 (void) idefloppy_get_sfrp_bit(floppy
->drive
);
1533 idefloppy_create_format_unit_cmd(&pc
, blocks
, length
, flags
);
1535 if (idefloppy_queue_pc_tail(floppy
->drive
, &pc
))
1540 floppy
->flags
&= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS
;
1545 static int idefloppy_ioctl(struct inode
*inode
, struct file
*file
,
1546 unsigned int cmd
, unsigned long arg
)
1548 struct block_device
*bdev
= inode
->i_bdev
;
1549 struct ide_floppy_obj
*floppy
= ide_floppy_g(bdev
->bd_disk
);
1550 ide_drive_t
*drive
= floppy
->drive
;
1551 struct ide_atapi_pc pc
;
1552 void __user
*argp
= (void __user
*)arg
;
1558 case CDROM_LOCKDOOR
:
1559 return ide_floppy_lockdoor(floppy
, &pc
, arg
, cmd
);
1560 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED
:
1562 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY
:
1563 return ide_floppy_get_format_capacities(drive
, argp
);
1564 case IDEFLOPPY_IOCTL_FORMAT_START
:
1565 if (!(file
->f_mode
& 2))
1568 return ide_floppy_format_unit(floppy
, (int __user
*)arg
);
1569 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS
:
1570 return idefloppy_get_format_progress(drive
, argp
);
1574 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1575 * and CDROM_SEND_PACKET (legacy) ioctls
1577 if (cmd
!= CDROM_SEND_PACKET
&& cmd
!= SCSI_IOCTL_SEND_COMMAND
)
1578 err
= scsi_cmd_ioctl(file
, bdev
->bd_disk
->queue
,
1579 bdev
->bd_disk
, cmd
, argp
);
1584 err
= generic_ide_ioctl(drive
, file
, bdev
, cmd
, arg
);
1589 static int idefloppy_media_changed(struct gendisk
*disk
)
1591 struct ide_floppy_obj
*floppy
= ide_floppy_g(disk
);
1592 ide_drive_t
*drive
= floppy
->drive
;
1595 /* do not scan partitions twice if this is a removable device */
1596 if (drive
->attach
) {
1600 ret
= !!(floppy
->flags
& IDEFLOPPY_FLAG_MEDIA_CHANGED
);
1601 floppy
->flags
&= ~IDEFLOPPY_FLAG_MEDIA_CHANGED
;
1605 static int idefloppy_revalidate_disk(struct gendisk
*disk
)
1607 struct ide_floppy_obj
*floppy
= ide_floppy_g(disk
);
1608 set_capacity(disk
, idefloppy_capacity(floppy
->drive
));
1612 static struct block_device_operations idefloppy_ops
= {
1613 .owner
= THIS_MODULE
,
1614 .open
= idefloppy_open
,
1615 .release
= idefloppy_release
,
1616 .ioctl
= idefloppy_ioctl
,
1617 .getgeo
= idefloppy_getgeo
,
1618 .media_changed
= idefloppy_media_changed
,
1619 .revalidate_disk
= idefloppy_revalidate_disk
1622 static int ide_floppy_probe(ide_drive_t
*drive
)
1624 idefloppy_floppy_t
*floppy
;
1627 if (!strstr("ide-floppy", drive
->driver_req
))
1629 if (!drive
->present
)
1631 if (drive
->media
!= ide_floppy
)
1633 if (!idefloppy_identify_device(drive
, drive
->id
)) {
1634 printk(KERN_ERR
"ide-floppy: %s: not supported by this version"
1635 " of ide-floppy\n", drive
->name
);
1639 printk(KERN_INFO
"ide-floppy: passing drive %s to ide-scsi"
1640 " emulation.\n", drive
->name
);
1643 floppy
= kzalloc(sizeof(idefloppy_floppy_t
), GFP_KERNEL
);
1645 printk(KERN_ERR
"ide-floppy: %s: Can't allocate a floppy"
1646 " structure\n", drive
->name
);
1650 g
= alloc_disk(1 << PARTN_BITS
);
1652 goto out_free_floppy
;
1654 ide_init_disk(g
, drive
);
1656 ide_proc_register_driver(drive
, &idefloppy_driver
);
1658 kref_init(&floppy
->kref
);
1660 floppy
->drive
= drive
;
1661 floppy
->driver
= &idefloppy_driver
;
1664 g
->private_data
= &floppy
->driver
;
1666 drive
->driver_data
= floppy
;
1668 idefloppy_setup(drive
, floppy
);
1670 g
->minors
= 1 << PARTN_BITS
;
1671 g
->driverfs_dev
= &drive
->gendev
;
1672 g
->flags
= drive
->removable
? GENHD_FL_REMOVABLE
: 0;
1673 g
->fops
= &idefloppy_ops
;
1684 static void __exit
idefloppy_exit(void)
1686 driver_unregister(&idefloppy_driver
.gen_driver
);
1689 static int __init
idefloppy_init(void)
1691 printk("ide-floppy driver " IDEFLOPPY_VERSION
"\n");
1692 return driver_register(&idefloppy_driver
.gen_driver
);
1695 MODULE_ALIAS("ide:*m-floppy*");
1696 module_init(idefloppy_init
);
1697 module_exit(idefloppy_exit
);
1698 MODULE_LICENSE("GPL");
1699 MODULE_DESCRIPTION("ATAPI FLOPPY Driver");