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 DRV_NAME "ide-floppy"
20 #define IDEFLOPPY_VERSION "1.00"
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/string.h>
25 #include <linux/kernel.h>
26 #include <linux/delay.h>
27 #include <linux/timer.h>
29 #include <linux/interrupt.h>
30 #include <linux/major.h>
31 #include <linux/errno.h>
32 #include <linux/genhd.h>
33 #include <linux/slab.h>
34 #include <linux/cdrom.h>
35 #include <linux/ide.h>
36 #include <linux/hdreg.h>
37 #include <linux/bitops.h>
38 #include <linux/mutex.h>
39 #include <linux/scatterlist.h>
41 #include <scsi/scsi_ioctl.h>
43 #include <asm/byteorder.h>
44 #include <linux/irq.h>
45 #include <linux/uaccess.h>
47 #include <asm/unaligned.h>
49 /* define to see debug info */
50 #define IDEFLOPPY_DEBUG_LOG 0
52 /* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
53 #define IDEFLOPPY_DEBUG(fmt, args...)
55 #if IDEFLOPPY_DEBUG_LOG
56 #define debug_log(fmt, args...) \
57 printk(KERN_INFO "ide-floppy: " fmt, ## args)
59 #define debug_log(fmt, args...) do {} while (0)
63 /* Some drives require a longer irq timeout. */
64 #define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
67 * After each failed packet command we issue a request sense command and retry
68 * the packet command IDEFLOPPY_MAX_PC_RETRIES times.
70 #define IDEFLOPPY_MAX_PC_RETRIES 3
73 * With each packet command, we allocate a buffer of IDEFLOPPY_PC_BUFFER_SIZE
76 #define IDEFLOPPY_PC_BUFFER_SIZE 256
78 /* format capacities descriptor codes */
79 #define CAPACITY_INVALID 0x00
80 #define CAPACITY_UNFORMATTED 0x01
81 #define CAPACITY_CURRENT 0x02
82 #define CAPACITY_NO_CARTRIDGE 0x03
85 * Most of our global data which we need to save even as we leave the driver
86 * due to an interrupt or a timer event is stored in a variable of type
87 * idefloppy_floppy_t, defined below.
89 typedef struct ide_floppy_obj {
94 unsigned int openers; /* protected by BKL for now */
96 /* Current packet command */
97 struct ide_atapi_pc *pc;
98 /* Last failed packet command */
99 struct ide_atapi_pc *failed_pc;
100 /* used for blk_{fs,pc}_request() requests */
101 struct ide_atapi_pc queued_pc;
103 struct ide_atapi_pc request_sense_pc;
104 struct request request_sense_rq;
106 /* Last error information */
107 u8 sense_key, asc, ascq;
108 /* delay this long before sending packet command */
110 int progress_indication;
112 /* Device information */
114 int blocks, block_size, bs_factor;
115 /* Last format capacity descriptor */
117 /* Copy of the flexible disk page */
118 u8 flexible_disk_page[32];
121 /* Supports format progress report */
123 } idefloppy_floppy_t;
125 #define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
127 /* IOCTLs used in low-level formatting. */
128 #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
129 #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
130 #define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
131 #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
133 /* Error code returned in rq->errors to the higher part of the driver. */
134 #define IDEFLOPPY_ERROR_GENERAL 101
137 * Pages of the SELECT SENSE / MODE SENSE packet commands.
138 * See SFF-8070i spec.
140 #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
141 #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
143 static DEFINE_MUTEX(idefloppy_ref_mutex);
145 #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
147 #define ide_floppy_g(disk) \
148 container_of((disk)->private_data, struct ide_floppy_obj, driver)
150 static void idefloppy_cleanup_obj(struct kref *);
152 static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
154 struct ide_floppy_obj *floppy = NULL;
156 mutex_lock(&idefloppy_ref_mutex);
157 floppy = ide_floppy_g(disk);
159 if (ide_device_get(floppy->drive))
162 kref_get(&floppy->kref);
164 mutex_unlock(&idefloppy_ref_mutex);
168 static void ide_floppy_put(struct ide_floppy_obj *floppy)
170 ide_drive_t *drive = floppy->drive;
172 mutex_lock(&idefloppy_ref_mutex);
173 kref_put(&floppy->kref, idefloppy_cleanup_obj);
174 ide_device_put(drive);
175 mutex_unlock(&idefloppy_ref_mutex);
179 * Used to finish servicing a request. For read/write requests, we will call
180 * ide_end_request to pass to the next buffer.
182 static int idefloppy_end_request(ide_drive_t *drive, int uptodate, int nsecs)
184 idefloppy_floppy_t *floppy = drive->driver_data;
185 struct request *rq = HWGROUP(drive)->rq;
188 debug_log("Reached %s\n", __func__);
191 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
192 case 1: error = 0; break;
193 default: error = uptodate;
196 floppy->failed_pc = NULL;
197 /* Why does this happen? */
200 if (!blk_special_request(rq)) {
201 /* our real local end request function */
202 ide_end_request(drive, uptodate, nsecs);
206 /* fixme: need to move this local also */
207 ide_end_drive_cmd(drive, 0, 0);
211 static void idefloppy_update_buffers(ide_drive_t *drive,
212 struct ide_atapi_pc *pc)
214 struct request *rq = pc->rq;
215 struct bio *bio = rq->bio;
217 while ((bio = rq->bio) != NULL)
218 idefloppy_end_request(drive, 1, 0);
222 * Generate a new packet command request in front of the request queue, before
223 * the current request so that it will be processed immediately, on the next
224 * pass through the driver.
226 static void idefloppy_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
229 struct ide_floppy_obj *floppy = drive->driver_data;
231 blk_rq_init(NULL, rq);
232 rq->buffer = (char *) pc;
233 rq->cmd_type = REQ_TYPE_SPECIAL;
234 rq->cmd_flags |= REQ_PREEMPT;
235 rq->rq_disk = floppy->disk;
236 memcpy(rq->cmd, pc->c, 12);
237 ide_do_drive_cmd(drive, rq);
240 static void ide_floppy_callback(ide_drive_t *drive)
242 idefloppy_floppy_t *floppy = drive->driver_data;
243 struct ide_atapi_pc *pc = floppy->pc;
244 int uptodate = pc->error ? 0 : 1;
246 debug_log("Reached %s\n", __func__);
248 if (floppy->failed_pc == pc)
249 floppy->failed_pc = NULL;
251 if (pc->c[0] == GPCMD_READ_10 || pc->c[0] == GPCMD_WRITE_10 ||
252 (pc->rq && blk_pc_request(pc->rq)))
253 uptodate = 1; /* FIXME */
254 else if (pc->c[0] == GPCMD_REQUEST_SENSE) {
255 u8 *buf = floppy->pc->buf;
258 floppy->sense_key = buf[2] & 0x0F;
259 floppy->asc = buf[12];
260 floppy->ascq = buf[13];
261 floppy->progress_indication = buf[15] & 0x80 ?
262 (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
264 if (floppy->failed_pc)
265 debug_log("pc = %x, ", floppy->failed_pc->c[0]);
267 debug_log("sense key = %x, asc = %x, ascq = %x\n",
268 floppy->sense_key, floppy->asc, floppy->ascq);
270 printk(KERN_ERR "Error in REQUEST SENSE itself - "
271 "Aborting request!\n");
274 idefloppy_end_request(drive, uptodate, 0);
277 static void idefloppy_init_pc(struct ide_atapi_pc *pc)
279 memset(pc, 0, sizeof(*pc));
280 pc->buf = pc->pc_buf;
281 pc->buf_size = IDEFLOPPY_PC_BUFFER_SIZE;
284 static void idefloppy_create_request_sense_cmd(struct ide_atapi_pc *pc)
286 idefloppy_init_pc(pc);
287 pc->c[0] = GPCMD_REQUEST_SENSE;
293 * Called when an error was detected during the last packet command. We queue a
294 * request sense packet command in the head of the request list.
296 static void idefloppy_retry_pc(ide_drive_t *drive)
298 struct ide_floppy_obj *floppy = drive->driver_data;
299 struct request *rq = &floppy->request_sense_rq;
300 struct ide_atapi_pc *pc = &floppy->request_sense_pc;
302 (void)ide_read_error(drive);
303 idefloppy_create_request_sense_cmd(pc);
304 idefloppy_queue_pc_head(drive, pc, rq);
307 /* The usual interrupt handler called during a packet command. */
308 static ide_startstop_t idefloppy_pc_intr(ide_drive_t *drive)
310 idefloppy_floppy_t *floppy = drive->driver_data;
312 return ide_pc_intr(drive, floppy->pc, idefloppy_pc_intr,
313 IDEFLOPPY_WAIT_CMD, NULL, idefloppy_update_buffers,
314 idefloppy_retry_pc, NULL, ide_io_buffers);
318 * What we have here is a classic case of a top half / bottom half interrupt
319 * service routine. In interrupt mode, the device sends an interrupt to signal
320 * that it is ready to receive a packet. However, we need to delay about 2-3
321 * ticks before issuing the packet or we gets in trouble.
323 static int idefloppy_transfer_pc(ide_drive_t *drive)
325 idefloppy_floppy_t *floppy = drive->driver_data;
327 /* Send the actual packet */
328 drive->hwif->tp_ops->output_data(drive, NULL, floppy->pc->c, 12);
330 /* Timeout for the packet command */
331 return IDEFLOPPY_WAIT_CMD;
336 * Called as an interrupt (or directly). When the device says it's ready for a
337 * packet, we schedule the packet transfer to occur about 2-3 ticks later in
340 static ide_startstop_t idefloppy_start_pc_transfer(ide_drive_t *drive)
342 idefloppy_floppy_t *floppy = drive->driver_data;
343 struct ide_atapi_pc *pc = floppy->pc;
344 ide_expiry_t *expiry;
345 unsigned int timeout;
348 * The following delay solves a problem with ATAPI Zip 100 drives
349 * where the Busy flag was apparently being deasserted before the
350 * unit was ready to receive data. This was happening on a
351 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
352 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
353 * used until after the packet is moved in about 50 msec.
355 if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
356 timeout = floppy->ticks;
357 expiry = &idefloppy_transfer_pc;
359 timeout = IDEFLOPPY_WAIT_CMD;
363 return ide_transfer_pc(drive, pc, idefloppy_pc_intr, timeout, expiry);
366 static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
367 struct ide_atapi_pc *pc)
369 /* supress error messages resulting from Medium not present */
370 if (floppy->sense_key == 0x02 &&
371 floppy->asc == 0x3a &&
372 floppy->ascq == 0x00)
375 printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
376 "asc = %2x, ascq = %2x\n",
377 floppy->drive->name, pc->c[0], floppy->sense_key,
378 floppy->asc, floppy->ascq);
382 static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive,
383 struct ide_atapi_pc *pc)
385 idefloppy_floppy_t *floppy = drive->driver_data;
387 if (floppy->failed_pc == NULL &&
388 pc->c[0] != GPCMD_REQUEST_SENSE)
389 floppy->failed_pc = pc;
390 /* Set the current packet command */
393 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
394 if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
395 ide_floppy_report_error(floppy, pc);
397 pc->error = IDEFLOPPY_ERROR_GENERAL;
399 floppy->failed_pc = NULL;
400 drive->pc_callback(drive);
404 debug_log("Retry number - %d\n", pc->retries);
408 return ide_issue_pc(drive, pc, idefloppy_start_pc_transfer,
409 IDEFLOPPY_WAIT_CMD, NULL);
412 static void idefloppy_create_prevent_cmd(struct ide_atapi_pc *pc, int prevent)
414 debug_log("creating prevent removal command, prevent = %d\n", prevent);
416 idefloppy_init_pc(pc);
417 pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
421 static void idefloppy_create_read_capacity_cmd(struct ide_atapi_pc *pc)
423 idefloppy_init_pc(pc);
424 pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
430 static void idefloppy_create_format_unit_cmd(struct ide_atapi_pc *pc, int b,
433 idefloppy_init_pc(pc);
434 pc->c[0] = GPCMD_FORMAT_UNIT;
437 memset(pc->buf, 0, 12);
439 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
441 if (flags & 1) /* Verify bit on... */
442 pc->buf[1] ^= 0x20; /* ... turn off DCRT bit */
445 put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buf[4]));
446 put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buf[8]));
448 pc->flags |= PC_FLAG_WRITING;
451 /* A mode sense command is used to "sense" floppy parameters. */
452 static void idefloppy_create_mode_sense_cmd(struct ide_atapi_pc *pc,
455 u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
457 idefloppy_init_pc(pc);
458 pc->c[0] = GPCMD_MODE_SENSE_10;
460 pc->c[2] = page_code;
463 case IDEFLOPPY_CAPABILITIES_PAGE:
466 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
470 printk(KERN_ERR "ide-floppy: unsupported page code "
471 "in create_mode_sense_cmd\n");
473 put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
474 pc->req_xfer = length;
477 static void idefloppy_create_start_stop_cmd(struct ide_atapi_pc *pc, int start)
479 idefloppy_init_pc(pc);
480 pc->c[0] = GPCMD_START_STOP_UNIT;
484 static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
485 struct ide_atapi_pc *pc, struct request *rq,
486 unsigned long sector)
488 int block = sector / floppy->bs_factor;
489 int blocks = rq->nr_sectors / floppy->bs_factor;
490 int cmd = rq_data_dir(rq);
492 debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
495 idefloppy_init_pc(pc);
496 pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
497 put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
498 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
500 memcpy(rq->cmd, pc->c, 12);
504 if (rq->cmd_flags & REQ_RW)
505 pc->flags |= PC_FLAG_WRITING;
507 pc->req_xfer = pc->buf_size = blocks * floppy->block_size;
508 pc->flags |= PC_FLAG_DMA_OK;
511 static void idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy,
512 struct ide_atapi_pc *pc, struct request *rq)
514 idefloppy_init_pc(pc);
515 memcpy(pc->c, rq->cmd, sizeof(pc->c));
518 if (rq->data_len && rq_data_dir(rq) == WRITE)
519 pc->flags |= PC_FLAG_WRITING;
522 pc->flags |= PC_FLAG_DMA_OK;
524 * possibly problematic, doesn't look like ide-floppy correctly
525 * handled scattered requests if dma fails...
527 pc->req_xfer = pc->buf_size = rq->data_len;
530 static ide_startstop_t idefloppy_do_request(ide_drive_t *drive,
531 struct request *rq, sector_t block_s)
533 idefloppy_floppy_t *floppy = drive->driver_data;
534 ide_hwif_t *hwif = drive->hwif;
535 struct ide_atapi_pc *pc;
536 unsigned long block = (unsigned long)block_s;
538 debug_log("%s: dev: %s, cmd: 0x%x, cmd_type: %x, errors: %d\n",
539 __func__, rq->rq_disk ? rq->rq_disk->disk_name : "?",
540 rq->cmd[0], rq->cmd_type, rq->errors);
542 debug_log("%s: sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",
543 __func__, (long)rq->sector, rq->nr_sectors,
544 rq->current_nr_sectors);
546 if (rq->errors >= ERROR_MAX) {
547 if (floppy->failed_pc)
548 ide_floppy_report_error(floppy, floppy->failed_pc);
550 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
552 idefloppy_end_request(drive, 0, 0);
555 if (blk_fs_request(rq)) {
556 if (((long)rq->sector % floppy->bs_factor) ||
557 (rq->nr_sectors % floppy->bs_factor)) {
558 printk(KERN_ERR "%s: unsupported r/w request size\n",
560 idefloppy_end_request(drive, 0, 0);
563 pc = &floppy->queued_pc;
564 idefloppy_create_rw_cmd(floppy, pc, rq, block);
565 } else if (blk_special_request(rq)) {
566 pc = (struct ide_atapi_pc *) rq->buffer;
567 } else if (blk_pc_request(rq)) {
568 pc = &floppy->queued_pc;
569 idefloppy_blockpc_cmd(floppy, pc, rq);
571 blk_dump_rq_flags(rq,
572 "ide-floppy: unsupported command in queue");
573 idefloppy_end_request(drive, 0, 0);
577 ide_init_sg_cmd(drive, rq);
578 ide_map_sg(drive, rq);
580 pc->sg = hwif->sg_table;
581 pc->sg_cnt = hwif->sg_nents;
585 return idefloppy_issue_pc(drive, pc);
589 * Add a special packet command request to the tail of the request queue,
590 * and wait for it to be serviced.
592 static int idefloppy_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
594 struct ide_floppy_obj *floppy = drive->driver_data;
598 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
599 rq->buffer = (char *) pc;
600 rq->cmd_type = REQ_TYPE_SPECIAL;
601 memcpy(rq->cmd, pc->c, 12);
602 error = blk_execute_rq(drive->queue, floppy->disk, rq, 0);
609 * Look at the flexible disk page parameters. We ignore the CHS capacity
610 * parameters and use the LBA parameters instead.
612 static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
614 idefloppy_floppy_t *floppy = drive->driver_data;
615 struct ide_atapi_pc pc;
617 int capacity, lba_capacity;
618 u16 transfer_rate, sector_size, cyls, rpm;
621 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE);
623 if (idefloppy_queue_pc_tail(drive, &pc)) {
624 printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
628 floppy->wp = !!(pc.buf[3] & 0x80);
629 set_disk_ro(floppy->disk, floppy->wp);
632 transfer_rate = be16_to_cpup((__be16 *)&pc.buf[8 + 2]);
633 sector_size = be16_to_cpup((__be16 *)&pc.buf[8 + 6]);
634 cyls = be16_to_cpup((__be16 *)&pc.buf[8 + 8]);
635 rpm = be16_to_cpup((__be16 *)&pc.buf[8 + 28]);
636 heads = pc.buf[8 + 4];
637 sectors = pc.buf[8 + 5];
639 capacity = cyls * heads * sectors * sector_size;
641 if (memcmp(page, &floppy->flexible_disk_page, 32))
642 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
643 "%d sector size, %d rpm\n",
644 drive->name, capacity / 1024, cyls, heads,
645 sectors, transfer_rate / 8, sector_size, rpm);
647 memcpy(&floppy->flexible_disk_page, page, 32);
648 drive->bios_cyl = cyls;
649 drive->bios_head = heads;
650 drive->bios_sect = sectors;
651 lba_capacity = floppy->blocks * floppy->block_size;
653 if (capacity < lba_capacity) {
654 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
655 "bytes, but the drive only handles %d\n",
656 drive->name, lba_capacity, capacity);
657 floppy->blocks = floppy->block_size ?
658 capacity / floppy->block_size : 0;
663 static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
665 idefloppy_floppy_t *floppy = drive->driver_data;
666 struct ide_atapi_pc pc;
670 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE);
671 pc.flags |= PC_FLAG_SUPPRESS_ERROR;
673 if (idefloppy_queue_pc_tail(drive, &pc))
676 floppy->srfp = pc.buf[8 + 2] & 0x40;
681 * Determine if a media is present in the floppy drive, and if so, its LBA
684 static int ide_floppy_get_capacity(ide_drive_t *drive)
686 idefloppy_floppy_t *floppy = drive->driver_data;
687 struct ide_atapi_pc pc;
689 u8 header_len, desc_cnt;
690 int i, rc = 1, blocks, length;
693 drive->bios_head = drive->bios_sect = 0;
695 floppy->bs_factor = 1;
696 set_capacity(floppy->disk, 0);
698 idefloppy_create_read_capacity_cmd(&pc);
699 if (idefloppy_queue_pc_tail(drive, &pc)) {
700 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
703 header_len = pc.buf[3];
704 cap_desc = &pc.buf[4];
705 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
707 for (i = 0; i < desc_cnt; i++) {
708 unsigned int desc_start = 4 + i*8;
710 blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
711 length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
713 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
714 i, blocks * length / 1024, blocks, length);
719 * the code below is valid only for the 1st descriptor, ie i=0
722 switch (pc.buf[desc_start + 4] & 0x03) {
723 /* Clik! drive returns this instead of CAPACITY_CURRENT */
724 case CAPACITY_UNFORMATTED:
725 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
727 * If it is not a clik drive, break out
728 * (maintains previous driver behaviour)
731 case CAPACITY_CURRENT:
732 /* Normal Zip/LS-120 disks */
733 if (memcmp(cap_desc, &floppy->cap_desc, 8))
734 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
735 "sector size\n", drive->name,
736 blocks * length / 1024, blocks, length);
737 memcpy(&floppy->cap_desc, cap_desc, 8);
739 if (!length || length % 512) {
740 printk(KERN_NOTICE "%s: %d bytes block size "
741 "not supported\n", drive->name, length);
743 floppy->blocks = blocks;
744 floppy->block_size = length;
745 floppy->bs_factor = length / 512;
746 if (floppy->bs_factor != 1)
747 printk(KERN_NOTICE "%s: warning: non "
748 "512 bytes block size not "
754 case CAPACITY_NO_CARTRIDGE:
756 * This is a KERN_ERR so it appears on screen
757 * for the user to see
759 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
761 case CAPACITY_INVALID:
762 printk(KERN_ERR "%s: Invalid capacity for disk "
763 "in drive\n", drive->name);
766 debug_log("Descriptor 0 Code: %d\n",
767 pc.buf[desc_start + 4] & 0x03);
770 /* Clik! disk does not support get_flexible_disk_page */
771 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
772 (void) ide_floppy_get_flexible_disk_page(drive);
774 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
779 * Obtain the list of formattable capacities.
780 * Very similar to ide_floppy_get_capacity, except that we push the capacity
781 * descriptors to userland, instead of our own structures.
783 * Userland gives us the following structure:
785 * struct idefloppy_format_capacities {
793 * userland initializes nformats to the number of allocated formats[] records.
794 * On exit we set nformats to the number of records we've actually initialized.
797 static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
799 struct ide_atapi_pc pc;
800 u8 header_len, desc_cnt;
801 int i, blocks, length, u_array_size, u_index;
804 if (get_user(u_array_size, arg))
807 if (u_array_size <= 0)
810 idefloppy_create_read_capacity_cmd(&pc);
811 if (idefloppy_queue_pc_tail(drive, &pc)) {
812 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
816 header_len = pc.buf[3];
817 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
823 * We always skip the first capacity descriptor. That's the current
824 * capacity. We are interested in the remaining descriptors, the
825 * formattable capacities.
827 for (i = 1; i < desc_cnt; i++) {
828 unsigned int desc_start = 4 + i*8;
830 if (u_index >= u_array_size)
831 break; /* User-supplied buffer too small */
833 blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
834 length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
836 if (put_user(blocks, argp))
841 if (put_user(length, argp))
849 if (put_user(u_index, arg))
856 * Get ATAPI_FORMAT_UNIT progress indication.
858 * Userland gives a pointer to an int. The int is set to a progress
859 * indicator 0-65536, with 65536=100%.
861 * If the drive does not support format progress indication, we just check
862 * the dsc bit, and return either 0 or 65536.
865 static int ide_floppy_get_format_progress(ide_drive_t *drive, int __user *arg)
867 idefloppy_floppy_t *floppy = drive->driver_data;
868 struct ide_atapi_pc pc;
869 int progress_indication = 0x10000;
872 idefloppy_create_request_sense_cmd(&pc);
873 if (idefloppy_queue_pc_tail(drive, &pc))
876 if (floppy->sense_key == 2 &&
879 progress_indication = floppy->progress_indication;
881 /* Else assume format_unit has finished, and we're at 0x10000 */
883 ide_hwif_t *hwif = drive->hwif;
887 local_irq_save(flags);
888 stat = hwif->tp_ops->read_status(hwif);
889 local_irq_restore(flags);
891 progress_indication = ((stat & ATA_DSC) == 0) ? 0 : 0x10000;
894 if (put_user(progress_indication, arg))
900 static sector_t idefloppy_capacity(ide_drive_t *drive)
902 idefloppy_floppy_t *floppy = drive->driver_data;
903 unsigned long capacity = floppy->blocks * floppy->bs_factor;
908 #ifdef CONFIG_IDE_PROC_FS
909 ide_devset_rw(bios_cyl, 0, 1023, bios_cyl);
910 ide_devset_rw(bios_head, 0, 255, bios_head);
911 ide_devset_rw(bios_sect, 0, 63, bios_sect);
913 static int get_ticks(ide_drive_t *drive)
915 idefloppy_floppy_t *floppy = drive->driver_data;
916 return floppy->ticks;
919 static int set_ticks(ide_drive_t *drive, int arg)
921 idefloppy_floppy_t *floppy = drive->driver_data;
926 IDE_DEVSET(ticks, S_RW, 0, 255, get_ticks, set_ticks);
928 static const struct ide_devset *idefloppy_settings[] = {
929 &ide_devset_bios_cyl,
930 &ide_devset_bios_head,
931 &ide_devset_bios_sect,
937 static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
942 *((u16 *)&gcw) = id[ATA_ID_CONFIG];
944 drive->pc_callback = ide_floppy_callback;
946 if (((gcw[0] & 0x60) >> 5) == 1)
947 drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
949 * We used to check revisions here. At this point however I'm giving up.
950 * Just assume they are all broken, its easier.
952 * The actual reason for the workarounds was likely a driver bug after
953 * all rather than a firmware bug, and the workaround below used to hide
954 * it. It should be fixed as of version 1.9, but to be on the safe side
955 * we'll leave the limitation below for the 2.2.x tree.
957 if (!strncmp((char *)&id[ATA_ID_PROD], "IOMEGA ZIP 100 ATAPI", 20)) {
958 drive->atapi_flags |= IDE_AFLAG_ZIP_DRIVE;
959 /* This value will be visible in the /proc/ide/hdx/settings */
960 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
961 blk_queue_max_sectors(drive->queue, 64);
965 * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
966 * nasty clicking noises without it, so please don't remove this.
968 if (strncmp((char *)&id[ATA_ID_PROD], "IOMEGA Clik!", 11) == 0) {
969 blk_queue_max_sectors(drive->queue, 64);
970 drive->atapi_flags |= IDE_AFLAG_CLIK_DRIVE;
973 (void) ide_floppy_get_capacity(drive);
975 ide_proc_register_driver(drive, floppy->driver);
978 static void ide_floppy_remove(ide_drive_t *drive)
980 idefloppy_floppy_t *floppy = drive->driver_data;
981 struct gendisk *g = floppy->disk;
983 ide_proc_unregister_driver(drive, floppy->driver);
987 ide_floppy_put(floppy);
990 static void idefloppy_cleanup_obj(struct kref *kref)
992 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
993 ide_drive_t *drive = floppy->drive;
994 struct gendisk *g = floppy->disk;
996 drive->driver_data = NULL;
997 g->private_data = NULL;
1002 #ifdef CONFIG_IDE_PROC_FS
1003 static int proc_idefloppy_read_capacity(char *page, char **start, off_t off,
1004 int count, int *eof, void *data)
1006 ide_drive_t*drive = (ide_drive_t *)data;
1009 len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive));
1010 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
1013 static ide_proc_entry_t idefloppy_proc[] = {
1014 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
1015 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1016 { NULL, 0, NULL, NULL }
1018 #endif /* CONFIG_IDE_PROC_FS */
1020 static int ide_floppy_probe(ide_drive_t *);
1022 static ide_driver_t idefloppy_driver = {
1024 .owner = THIS_MODULE,
1025 .name = "ide-floppy",
1026 .bus = &ide_bus_type,
1028 .probe = ide_floppy_probe,
1029 .remove = ide_floppy_remove,
1030 .version = IDEFLOPPY_VERSION,
1031 .media = ide_floppy,
1032 .do_request = idefloppy_do_request,
1033 .end_request = idefloppy_end_request,
1034 .error = __ide_error,
1035 #ifdef CONFIG_IDE_PROC_FS
1036 .proc = idefloppy_proc,
1037 .settings = idefloppy_settings,
1041 static int idefloppy_open(struct inode *inode, struct file *filp)
1043 struct gendisk *disk = inode->i_bdev->bd_disk;
1044 struct ide_floppy_obj *floppy;
1046 struct ide_atapi_pc pc;
1049 debug_log("Reached %s\n", __func__);
1051 floppy = ide_floppy_get(disk);
1055 drive = floppy->drive;
1059 if (floppy->openers == 1) {
1060 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1063 idefloppy_init_pc(&pc);
1064 pc.c[0] = GPCMD_TEST_UNIT_READY;
1066 if (idefloppy_queue_pc_tail(drive, &pc)) {
1067 idefloppy_create_start_stop_cmd(&pc, 1);
1068 (void) idefloppy_queue_pc_tail(drive, &pc);
1071 if (ide_floppy_get_capacity(drive)
1072 && (filp->f_flags & O_NDELAY) == 0
1074 * Allow O_NDELAY to open a drive without a disk, or with an
1075 * unreadable disk, so that we can get the format capacity
1076 * of the drive or begin the format - Sam
1080 goto out_put_floppy;
1083 if (floppy->wp && (filp->f_mode & 2)) {
1085 goto out_put_floppy;
1087 drive->atapi_flags |= IDE_AFLAG_MEDIA_CHANGED;
1088 /* IOMEGA Clik! drives do not support lock/unlock commands */
1089 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
1090 idefloppy_create_prevent_cmd(&pc, 1);
1091 (void) idefloppy_queue_pc_tail(drive, &pc);
1093 check_disk_change(inode->i_bdev);
1094 } else if (drive->atapi_flags & IDE_AFLAG_FORMAT_IN_PROGRESS) {
1096 goto out_put_floppy;
1102 ide_floppy_put(floppy);
1106 static int idefloppy_release(struct inode *inode, struct file *filp)
1108 struct gendisk *disk = inode->i_bdev->bd_disk;
1109 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1110 ide_drive_t *drive = floppy->drive;
1111 struct ide_atapi_pc pc;
1113 debug_log("Reached %s\n", __func__);
1115 if (floppy->openers == 1) {
1116 /* IOMEGA Clik! drives do not support lock/unlock commands */
1117 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
1118 idefloppy_create_prevent_cmd(&pc, 0);
1119 (void) idefloppy_queue_pc_tail(drive, &pc);
1122 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1127 ide_floppy_put(floppy);
1132 static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1134 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1135 ide_drive_t *drive = floppy->drive;
1137 geo->heads = drive->bios_head;
1138 geo->sectors = drive->bios_sect;
1139 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1143 static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
1144 unsigned long arg, unsigned int cmd)
1146 idefloppy_floppy_t *floppy = drive->driver_data;
1148 if (floppy->openers > 1)
1151 /* The IOMEGA Clik! Drive doesn't support this command -
1152 * no room for an eject mechanism */
1153 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
1154 int prevent = arg ? 1 : 0;
1156 if (cmd == CDROMEJECT)
1159 idefloppy_create_prevent_cmd(pc, prevent);
1160 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1163 if (cmd == CDROMEJECT) {
1164 idefloppy_create_start_stop_cmd(pc, 2);
1165 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1171 static int ide_floppy_format_unit(ide_drive_t *drive, int __user *arg)
1173 idefloppy_floppy_t *floppy = drive->driver_data;
1174 struct ide_atapi_pc pc;
1175 int blocks, length, flags, err = 0;
1177 if (floppy->openers > 1) {
1178 /* Don't format if someone is using the disk */
1179 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1183 drive->atapi_flags |= IDE_AFLAG_FORMAT_IN_PROGRESS;
1186 * Send ATAPI_FORMAT_UNIT to the drive.
1188 * Userland gives us the following structure:
1190 * struct idefloppy_format_command {
1196 * flags is a bitmask, currently, the only defined flag is:
1198 * 0x01 - verify media after format.
1200 if (get_user(blocks, arg) ||
1201 get_user(length, arg+1) ||
1202 get_user(flags, arg+2)) {
1207 (void) idefloppy_get_sfrp_bit(drive);
1208 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1210 if (idefloppy_queue_pc_tail(drive, &pc))
1215 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1219 static int ide_floppy_format_ioctl(ide_drive_t *drive, struct file *file,
1220 unsigned int cmd, void __user *argp)
1223 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1225 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
1226 return ide_floppy_get_format_capacities(drive, argp);
1227 case IDEFLOPPY_IOCTL_FORMAT_START:
1228 if (!(file->f_mode & 2))
1230 return ide_floppy_format_unit(drive, (int __user *)argp);
1231 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1232 return ide_floppy_get_format_progress(drive, argp);
1238 static int idefloppy_ioctl(struct inode *inode, struct file *file,
1239 unsigned int cmd, unsigned long arg)
1241 struct block_device *bdev = inode->i_bdev;
1242 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1243 ide_drive_t *drive = floppy->drive;
1244 struct ide_atapi_pc pc;
1245 void __user *argp = (void __user *)arg;
1248 if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR)
1249 return ide_floppy_lockdoor(drive, &pc, arg, cmd);
1251 err = ide_floppy_format_ioctl(drive, file, cmd, argp);
1256 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1257 * and CDROM_SEND_PACKET (legacy) ioctls
1259 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1260 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1261 bdev->bd_disk, cmd, argp);
1264 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1269 static int idefloppy_media_changed(struct gendisk *disk)
1271 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1272 ide_drive_t *drive = floppy->drive;
1275 /* do not scan partitions twice if this is a removable device */
1276 if (drive->attach) {
1280 ret = !!(drive->atapi_flags & IDE_AFLAG_MEDIA_CHANGED);
1281 drive->atapi_flags &= ~IDE_AFLAG_MEDIA_CHANGED;
1285 static int idefloppy_revalidate_disk(struct gendisk *disk)
1287 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1288 set_capacity(disk, idefloppy_capacity(floppy->drive));
1292 static struct block_device_operations idefloppy_ops = {
1293 .owner = THIS_MODULE,
1294 .open = idefloppy_open,
1295 .release = idefloppy_release,
1296 .ioctl = idefloppy_ioctl,
1297 .getgeo = idefloppy_getgeo,
1298 .media_changed = idefloppy_media_changed,
1299 .revalidate_disk = idefloppy_revalidate_disk
1302 static int ide_floppy_probe(ide_drive_t *drive)
1304 idefloppy_floppy_t *floppy;
1307 if (!strstr("ide-floppy", drive->driver_req))
1310 if (drive->media != ide_floppy)
1313 if (!ide_check_atapi_device(drive, DRV_NAME)) {
1314 printk(KERN_ERR "ide-floppy: %s: not supported by this version"
1315 " of ide-floppy\n", drive->name);
1318 floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
1320 printk(KERN_ERR "ide-floppy: %s: Can't allocate a floppy"
1321 " structure\n", drive->name);
1325 g = alloc_disk(1 << PARTN_BITS);
1327 goto out_free_floppy;
1329 ide_init_disk(g, drive);
1331 kref_init(&floppy->kref);
1333 floppy->drive = drive;
1334 floppy->driver = &idefloppy_driver;
1337 g->private_data = &floppy->driver;
1339 drive->driver_data = floppy;
1341 idefloppy_setup(drive, floppy);
1343 g->minors = 1 << PARTN_BITS;
1344 g->driverfs_dev = &drive->gendev;
1345 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1346 g->fops = &idefloppy_ops;
1357 static void __exit idefloppy_exit(void)
1359 driver_unregister(&idefloppy_driver.gen_driver);
1362 static int __init idefloppy_init(void)
1364 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
1365 return driver_register(&idefloppy_driver.gen_driver);
1368 MODULE_ALIAS("ide:*m-floppy*");
1369 module_init(idefloppy_init);
1370 module_exit(idefloppy_exit);
1371 MODULE_LICENSE("GPL");
1372 MODULE_DESCRIPTION("ATAPI FLOPPY Driver");