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>
40 #include <scsi/scsi_ioctl.h>
42 #include <asm/byteorder.h>
43 #include <linux/irq.h>
44 #include <linux/uaccess.h>
46 #include <asm/unaligned.h>
48 /* define to see debug info */
49 #define IDEFLOPPY_DEBUG_LOG 0
51 /* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
52 #define IDEFLOPPY_DEBUG(fmt, args...)
54 #if IDEFLOPPY_DEBUG_LOG
55 #define debug_log(fmt, args...) \
56 printk(KERN_INFO "ide-floppy: " fmt, ## args)
58 #define debug_log(fmt, args...) do {} while (0)
62 /* Some drives require a longer irq timeout. */
63 #define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
66 * After each failed packet command we issue a request sense command and retry
67 * the packet command IDEFLOPPY_MAX_PC_RETRIES times.
69 #define IDEFLOPPY_MAX_PC_RETRIES 3
72 * With each packet command, we allocate a buffer of IDEFLOPPY_PC_BUFFER_SIZE
75 #define IDEFLOPPY_PC_BUFFER_SIZE 256
78 * In various places in the driver, we need to allocate storage for packet
79 * commands and requests, which will remain valid while we leave the driver to
80 * wait for an interrupt or a timeout event.
82 #define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
84 /* format capacities descriptor codes */
85 #define CAPACITY_INVALID 0x00
86 #define CAPACITY_UNFORMATTED 0x01
87 #define CAPACITY_CURRENT 0x02
88 #define CAPACITY_NO_CARTRIDGE 0x03
91 * Most of our global data which we need to save even as we leave the driver
92 * due to an interrupt or a timer event is stored in a variable of type
93 * idefloppy_floppy_t, defined below.
95 typedef struct ide_floppy_obj {
100 unsigned int openers; /* protected by BKL for now */
102 /* Current packet command */
103 struct ide_atapi_pc *pc;
104 /* Last failed packet command */
105 struct ide_atapi_pc *failed_pc;
106 /* Packet command stack */
107 struct ide_atapi_pc pc_stack[IDEFLOPPY_PC_STACK];
108 /* Next free packet command storage space */
110 struct request rq_stack[IDEFLOPPY_PC_STACK];
111 /* We implement a circular array */
114 /* Last error information */
115 u8 sense_key, asc, ascq;
116 /* delay this long before sending packet command */
118 int progress_indication;
120 /* Device information */
122 int blocks, block_size, bs_factor;
123 /* Last format capacity descriptor */
125 /* Copy of the flexible disk page */
126 u8 flexible_disk_page[32];
129 /* Supports format progress report */
131 } idefloppy_floppy_t;
133 #define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
135 /* Defines for the MODE SENSE command */
136 #define MODE_SENSE_CURRENT 0x00
137 #define MODE_SENSE_CHANGEABLE 0x01
138 #define MODE_SENSE_DEFAULT 0x02
139 #define MODE_SENSE_SAVED 0x03
141 /* IOCTLs used in low-level formatting. */
142 #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
143 #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
144 #define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
145 #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
147 /* Error code returned in rq->errors to the higher part of the driver. */
148 #define IDEFLOPPY_ERROR_GENERAL 101
151 * Pages of the SELECT SENSE / MODE SENSE packet commands.
152 * See SFF-8070i spec.
154 #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
155 #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
157 static DEFINE_MUTEX(idefloppy_ref_mutex);
159 #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
161 #define ide_floppy_g(disk) \
162 container_of((disk)->private_data, struct ide_floppy_obj, driver)
164 static void idefloppy_cleanup_obj(struct kref *);
166 static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
168 struct ide_floppy_obj *floppy = NULL;
170 mutex_lock(&idefloppy_ref_mutex);
171 floppy = ide_floppy_g(disk);
173 if (ide_device_get(floppy->drive))
176 kref_get(&floppy->kref);
178 mutex_unlock(&idefloppy_ref_mutex);
182 static void ide_floppy_put(struct ide_floppy_obj *floppy)
184 ide_drive_t *drive = floppy->drive;
186 mutex_lock(&idefloppy_ref_mutex);
187 kref_put(&floppy->kref, idefloppy_cleanup_obj);
188 ide_device_put(drive);
189 mutex_unlock(&idefloppy_ref_mutex);
193 * Used to finish servicing a request. For read/write requests, we will call
194 * ide_end_request to pass to the next buffer.
196 static int idefloppy_end_request(ide_drive_t *drive, int uptodate, int nsecs)
198 idefloppy_floppy_t *floppy = drive->driver_data;
199 struct request *rq = HWGROUP(drive)->rq;
202 debug_log("Reached %s\n", __func__);
205 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
206 case 1: error = 0; break;
207 default: error = uptodate;
210 floppy->failed_pc = NULL;
211 /* Why does this happen? */
214 if (!blk_special_request(rq)) {
215 /* our real local end request function */
216 ide_end_request(drive, uptodate, nsecs);
220 /* fixme: need to move this local also */
221 ide_end_drive_cmd(drive, 0, 0);
225 static void ide_floppy_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
226 unsigned int bcount, int direction)
228 ide_hwif_t *hwif = drive->hwif;
229 struct request *rq = pc->rq;
230 struct req_iterator iter;
231 struct bio_vec *bvec;
236 rq_for_each_segment(bvec, rq, iter) {
240 count = min(bvec->bv_len, bcount);
242 data = bvec_kmap_irq(bvec, &flags);
244 hwif->tp_ops->output_data(drive, NULL, data, count);
246 hwif->tp_ops->input_data(drive, NULL, data, count);
247 bvec_kunmap_irq(data, &flags);
250 pc->b_count += count;
254 idefloppy_end_request(drive, 1, done >> 9);
257 printk(KERN_ERR "%s: leftover data in %s, bcount == %d\n",
258 drive->name, __func__, bcount);
259 ide_pad_transfer(drive, direction, bcount);
263 static void idefloppy_update_buffers(ide_drive_t *drive,
264 struct ide_atapi_pc *pc)
266 struct request *rq = pc->rq;
267 struct bio *bio = rq->bio;
269 while ((bio = rq->bio) != NULL)
270 idefloppy_end_request(drive, 1, 0);
274 * Generate a new packet command request in front of the request queue, before
275 * the current request so that it will be processed immediately, on the next
276 * pass through the driver.
278 static void idefloppy_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
281 struct ide_floppy_obj *floppy = drive->driver_data;
283 blk_rq_init(NULL, rq);
284 rq->buffer = (char *) pc;
285 rq->cmd_type = REQ_TYPE_SPECIAL;
286 rq->cmd_flags |= REQ_PREEMPT;
287 rq->rq_disk = floppy->disk;
288 memcpy(rq->cmd, pc->c, 12);
289 ide_do_drive_cmd(drive, rq);
292 static struct ide_atapi_pc *idefloppy_next_pc_storage(ide_drive_t *drive)
294 idefloppy_floppy_t *floppy = drive->driver_data;
296 if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
297 floppy->pc_stack_index = 0;
298 return (&floppy->pc_stack[floppy->pc_stack_index++]);
301 static struct request *idefloppy_next_rq_storage(ide_drive_t *drive)
303 idefloppy_floppy_t *floppy = drive->driver_data;
305 if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
306 floppy->rq_stack_index = 0;
307 return (&floppy->rq_stack[floppy->rq_stack_index++]);
310 static void ide_floppy_callback(ide_drive_t *drive)
312 idefloppy_floppy_t *floppy = drive->driver_data;
313 struct ide_atapi_pc *pc = floppy->pc;
314 int uptodate = pc->error ? 0 : 1;
316 debug_log("Reached %s\n", __func__);
318 if (floppy->failed_pc == pc)
319 floppy->failed_pc = NULL;
321 if (pc->c[0] == GPCMD_READ_10 || pc->c[0] == GPCMD_WRITE_10 ||
322 (pc->rq && blk_pc_request(pc->rq)))
323 uptodate = 1; /* FIXME */
324 else if (pc->c[0] == GPCMD_REQUEST_SENSE) {
325 u8 *buf = floppy->pc->buf;
328 floppy->sense_key = buf[2] & 0x0F;
329 floppy->asc = buf[12];
330 floppy->ascq = buf[13];
331 floppy->progress_indication = buf[15] & 0x80 ?
332 (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
334 if (floppy->failed_pc)
335 debug_log("pc = %x, ", floppy->failed_pc->c[0]);
337 debug_log("sense key = %x, asc = %x, ascq = %x\n",
338 floppy->sense_key, floppy->asc, floppy->ascq);
340 printk(KERN_ERR "Error in REQUEST SENSE itself - "
341 "Aborting request!\n");
344 idefloppy_end_request(drive, uptodate, 0);
347 static void idefloppy_init_pc(struct ide_atapi_pc *pc)
349 memset(pc, 0, sizeof(*pc));
350 pc->buf = pc->pc_buf;
351 pc->buf_size = IDEFLOPPY_PC_BUFFER_SIZE;
354 static void idefloppy_create_request_sense_cmd(struct ide_atapi_pc *pc)
356 idefloppy_init_pc(pc);
357 pc->c[0] = GPCMD_REQUEST_SENSE;
363 * Called when an error was detected during the last packet command. We queue a
364 * request sense packet command in the head of the request list.
366 static void idefloppy_retry_pc(ide_drive_t *drive)
368 struct ide_atapi_pc *pc;
371 (void)ide_read_error(drive);
372 pc = idefloppy_next_pc_storage(drive);
373 rq = idefloppy_next_rq_storage(drive);
374 idefloppy_create_request_sense_cmd(pc);
375 idefloppy_queue_pc_head(drive, pc, rq);
378 /* The usual interrupt handler called during a packet command. */
379 static ide_startstop_t idefloppy_pc_intr(ide_drive_t *drive)
381 idefloppy_floppy_t *floppy = drive->driver_data;
383 return ide_pc_intr(drive, floppy->pc, idefloppy_pc_intr,
384 IDEFLOPPY_WAIT_CMD, NULL, idefloppy_update_buffers,
385 idefloppy_retry_pc, NULL, ide_floppy_io_buffers);
389 * What we have here is a classic case of a top half / bottom half interrupt
390 * service routine. In interrupt mode, the device sends an interrupt to signal
391 * that it is ready to receive a packet. However, we need to delay about 2-3
392 * ticks before issuing the packet or we gets in trouble.
394 static int idefloppy_transfer_pc(ide_drive_t *drive)
396 idefloppy_floppy_t *floppy = drive->driver_data;
398 /* Send the actual packet */
399 drive->hwif->tp_ops->output_data(drive, NULL, floppy->pc->c, 12);
401 /* Timeout for the packet command */
402 return IDEFLOPPY_WAIT_CMD;
407 * Called as an interrupt (or directly). When the device says it's ready for a
408 * packet, we schedule the packet transfer to occur about 2-3 ticks later in
411 static ide_startstop_t idefloppy_start_pc_transfer(ide_drive_t *drive)
413 idefloppy_floppy_t *floppy = drive->driver_data;
414 struct ide_atapi_pc *pc = floppy->pc;
415 ide_expiry_t *expiry;
416 unsigned int timeout;
419 * The following delay solves a problem with ATAPI Zip 100 drives
420 * where the Busy flag was apparently being deasserted before the
421 * unit was ready to receive data. This was happening on a
422 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
423 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
424 * used until after the packet is moved in about 50 msec.
426 if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
427 timeout = floppy->ticks;
428 expiry = &idefloppy_transfer_pc;
430 timeout = IDEFLOPPY_WAIT_CMD;
434 return ide_transfer_pc(drive, pc, idefloppy_pc_intr, timeout, expiry);
437 static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
438 struct ide_atapi_pc *pc)
440 /* supress error messages resulting from Medium not present */
441 if (floppy->sense_key == 0x02 &&
442 floppy->asc == 0x3a &&
443 floppy->ascq == 0x00)
446 printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
447 "asc = %2x, ascq = %2x\n",
448 floppy->drive->name, pc->c[0], floppy->sense_key,
449 floppy->asc, floppy->ascq);
453 static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive,
454 struct ide_atapi_pc *pc)
456 idefloppy_floppy_t *floppy = drive->driver_data;
458 if (floppy->failed_pc == NULL &&
459 pc->c[0] != GPCMD_REQUEST_SENSE)
460 floppy->failed_pc = pc;
461 /* Set the current packet command */
464 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
465 if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
466 ide_floppy_report_error(floppy, pc);
468 pc->error = IDEFLOPPY_ERROR_GENERAL;
470 floppy->failed_pc = NULL;
471 drive->pc_callback(drive);
475 debug_log("Retry number - %d\n", pc->retries);
479 return ide_issue_pc(drive, pc, idefloppy_start_pc_transfer,
480 IDEFLOPPY_WAIT_CMD, NULL);
483 static void idefloppy_create_prevent_cmd(struct ide_atapi_pc *pc, int prevent)
485 debug_log("creating prevent removal command, prevent = %d\n", prevent);
487 idefloppy_init_pc(pc);
488 pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
492 static void idefloppy_create_read_capacity_cmd(struct ide_atapi_pc *pc)
494 idefloppy_init_pc(pc);
495 pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
501 static void idefloppy_create_format_unit_cmd(struct ide_atapi_pc *pc, int b,
504 idefloppy_init_pc(pc);
505 pc->c[0] = GPCMD_FORMAT_UNIT;
508 memset(pc->buf, 0, 12);
510 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
512 if (flags & 1) /* Verify bit on... */
513 pc->buf[1] ^= 0x20; /* ... turn off DCRT bit */
516 put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buf[4]));
517 put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buf[8]));
519 pc->flags |= PC_FLAG_WRITING;
522 /* A mode sense command is used to "sense" floppy parameters. */
523 static void idefloppy_create_mode_sense_cmd(struct ide_atapi_pc *pc,
524 u8 page_code, u8 type)
526 u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
528 idefloppy_init_pc(pc);
529 pc->c[0] = GPCMD_MODE_SENSE_10;
531 pc->c[2] = page_code + (type << 6);
534 case IDEFLOPPY_CAPABILITIES_PAGE:
537 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
541 printk(KERN_ERR "ide-floppy: unsupported page code "
542 "in create_mode_sense_cmd\n");
544 put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
545 pc->req_xfer = length;
548 static void idefloppy_create_start_stop_cmd(struct ide_atapi_pc *pc, int start)
550 idefloppy_init_pc(pc);
551 pc->c[0] = GPCMD_START_STOP_UNIT;
555 static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
556 struct ide_atapi_pc *pc, struct request *rq,
557 unsigned long sector)
559 int block = sector / floppy->bs_factor;
560 int blocks = rq->nr_sectors / floppy->bs_factor;
561 int cmd = rq_data_dir(rq);
563 debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
566 idefloppy_init_pc(pc);
567 pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
568 put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
569 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
571 memcpy(rq->cmd, pc->c, 12);
574 pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
575 if (rq->cmd_flags & REQ_RW)
576 pc->flags |= PC_FLAG_WRITING;
578 pc->req_xfer = pc->buf_size = blocks * floppy->block_size;
579 pc->flags |= PC_FLAG_DMA_OK;
582 static void idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy,
583 struct ide_atapi_pc *pc, struct request *rq)
585 idefloppy_init_pc(pc);
586 memcpy(pc->c, rq->cmd, sizeof(pc->c));
588 pc->b_count = rq->data_len;
589 if (rq->data_len && rq_data_dir(rq) == WRITE)
590 pc->flags |= PC_FLAG_WRITING;
593 pc->flags |= PC_FLAG_DMA_OK;
595 * possibly problematic, doesn't look like ide-floppy correctly
596 * handled scattered requests if dma fails...
598 pc->req_xfer = pc->buf_size = rq->data_len;
601 static ide_startstop_t idefloppy_do_request(ide_drive_t *drive,
602 struct request *rq, sector_t block_s)
604 idefloppy_floppy_t *floppy = drive->driver_data;
605 struct ide_atapi_pc *pc;
606 unsigned long block = (unsigned long)block_s;
608 debug_log("dev: %s, cmd_type: %x, errors: %d\n",
609 rq->rq_disk ? rq->rq_disk->disk_name : "?",
610 rq->cmd_type, rq->errors);
611 debug_log("sector: %ld, nr_sectors: %ld, "
612 "current_nr_sectors: %d\n", (long)rq->sector,
613 rq->nr_sectors, rq->current_nr_sectors);
615 if (rq->errors >= ERROR_MAX) {
616 if (floppy->failed_pc)
617 ide_floppy_report_error(floppy, floppy->failed_pc);
619 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
621 idefloppy_end_request(drive, 0, 0);
624 if (blk_fs_request(rq)) {
625 if (((long)rq->sector % floppy->bs_factor) ||
626 (rq->nr_sectors % floppy->bs_factor)) {
627 printk(KERN_ERR "%s: unsupported r/w request size\n",
629 idefloppy_end_request(drive, 0, 0);
632 pc = idefloppy_next_pc_storage(drive);
633 idefloppy_create_rw_cmd(floppy, pc, rq, block);
634 } else if (blk_special_request(rq)) {
635 pc = (struct ide_atapi_pc *) rq->buffer;
636 } else if (blk_pc_request(rq)) {
637 pc = idefloppy_next_pc_storage(drive);
638 idefloppy_blockpc_cmd(floppy, pc, rq);
640 blk_dump_rq_flags(rq,
641 "ide-floppy: unsupported command in queue");
642 idefloppy_end_request(drive, 0, 0);
648 return idefloppy_issue_pc(drive, pc);
652 * Add a special packet command request to the tail of the request queue,
653 * and wait for it to be serviced.
655 static int idefloppy_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
657 struct ide_floppy_obj *floppy = drive->driver_data;
661 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
662 rq->buffer = (char *) pc;
663 rq->cmd_type = REQ_TYPE_SPECIAL;
664 memcpy(rq->cmd, pc->c, 12);
665 error = blk_execute_rq(drive->queue, floppy->disk, rq, 0);
672 * Look at the flexible disk page parameters. We ignore the CHS capacity
673 * parameters and use the LBA parameters instead.
675 static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
677 idefloppy_floppy_t *floppy = drive->driver_data;
678 struct ide_atapi_pc pc;
680 int capacity, lba_capacity;
681 u16 transfer_rate, sector_size, cyls, rpm;
684 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE,
687 if (idefloppy_queue_pc_tail(drive, &pc)) {
688 printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
692 floppy->wp = !!(pc.buf[3] & 0x80);
693 set_disk_ro(floppy->disk, floppy->wp);
696 transfer_rate = be16_to_cpup((__be16 *)&pc.buf[8 + 2]);
697 sector_size = be16_to_cpup((__be16 *)&pc.buf[8 + 6]);
698 cyls = be16_to_cpup((__be16 *)&pc.buf[8 + 8]);
699 rpm = be16_to_cpup((__be16 *)&pc.buf[8 + 28]);
700 heads = pc.buf[8 + 4];
701 sectors = pc.buf[8 + 5];
703 capacity = cyls * heads * sectors * sector_size;
705 if (memcmp(page, &floppy->flexible_disk_page, 32))
706 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
707 "%d sector size, %d rpm\n",
708 drive->name, capacity / 1024, cyls, heads,
709 sectors, transfer_rate / 8, sector_size, rpm);
711 memcpy(&floppy->flexible_disk_page, page, 32);
712 drive->bios_cyl = cyls;
713 drive->bios_head = heads;
714 drive->bios_sect = sectors;
715 lba_capacity = floppy->blocks * floppy->block_size;
717 if (capacity < lba_capacity) {
718 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
719 "bytes, but the drive only handles %d\n",
720 drive->name, lba_capacity, capacity);
721 floppy->blocks = floppy->block_size ?
722 capacity / floppy->block_size : 0;
727 static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
729 idefloppy_floppy_t *floppy = drive->driver_data;
730 struct ide_atapi_pc pc;
733 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
736 pc.flags |= PC_FLAG_SUPPRESS_ERROR;
737 if (idefloppy_queue_pc_tail(drive, &pc))
740 floppy->srfp = pc.buf[8 + 2] & 0x40;
745 * Determine if a media is present in the floppy drive, and if so, its LBA
748 static int ide_floppy_get_capacity(ide_drive_t *drive)
750 idefloppy_floppy_t *floppy = drive->driver_data;
751 struct ide_atapi_pc pc;
753 u8 header_len, desc_cnt;
754 int i, rc = 1, blocks, length;
757 drive->bios_head = drive->bios_sect = 0;
759 floppy->bs_factor = 1;
760 set_capacity(floppy->disk, 0);
762 idefloppy_create_read_capacity_cmd(&pc);
763 if (idefloppy_queue_pc_tail(drive, &pc)) {
764 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
767 header_len = pc.buf[3];
768 cap_desc = &pc.buf[4];
769 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
771 for (i = 0; i < desc_cnt; i++) {
772 unsigned int desc_start = 4 + i*8;
774 blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
775 length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
777 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
778 i, blocks * length / 1024, blocks, length);
783 * the code below is valid only for the 1st descriptor, ie i=0
786 switch (pc.buf[desc_start + 4] & 0x03) {
787 /* Clik! drive returns this instead of CAPACITY_CURRENT */
788 case CAPACITY_UNFORMATTED:
789 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
791 * If it is not a clik drive, break out
792 * (maintains previous driver behaviour)
795 case CAPACITY_CURRENT:
796 /* Normal Zip/LS-120 disks */
797 if (memcmp(cap_desc, &floppy->cap_desc, 8))
798 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
799 "sector size\n", drive->name,
800 blocks * length / 1024, blocks, length);
801 memcpy(&floppy->cap_desc, cap_desc, 8);
803 if (!length || length % 512) {
804 printk(KERN_NOTICE "%s: %d bytes block size "
805 "not supported\n", drive->name, length);
807 floppy->blocks = blocks;
808 floppy->block_size = length;
809 floppy->bs_factor = length / 512;
810 if (floppy->bs_factor != 1)
811 printk(KERN_NOTICE "%s: warning: non "
812 "512 bytes block size not "
818 case CAPACITY_NO_CARTRIDGE:
820 * This is a KERN_ERR so it appears on screen
821 * for the user to see
823 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
825 case CAPACITY_INVALID:
826 printk(KERN_ERR "%s: Invalid capacity for disk "
827 "in drive\n", drive->name);
830 debug_log("Descriptor 0 Code: %d\n",
831 pc.buf[desc_start + 4] & 0x03);
834 /* Clik! disk does not support get_flexible_disk_page */
835 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
836 (void) ide_floppy_get_flexible_disk_page(drive);
838 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
843 * Obtain the list of formattable capacities.
844 * Very similar to ide_floppy_get_capacity, except that we push the capacity
845 * descriptors to userland, instead of our own structures.
847 * Userland gives us the following structure:
849 * struct idefloppy_format_capacities {
857 * userland initializes nformats to the number of allocated formats[] records.
858 * On exit we set nformats to the number of records we've actually initialized.
861 static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
863 struct ide_atapi_pc pc;
864 u8 header_len, desc_cnt;
865 int i, blocks, length, u_array_size, u_index;
868 if (get_user(u_array_size, arg))
871 if (u_array_size <= 0)
874 idefloppy_create_read_capacity_cmd(&pc);
875 if (idefloppy_queue_pc_tail(drive, &pc)) {
876 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
880 header_len = pc.buf[3];
881 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
887 * We always skip the first capacity descriptor. That's the current
888 * capacity. We are interested in the remaining descriptors, the
889 * formattable capacities.
891 for (i = 1; i < desc_cnt; i++) {
892 unsigned int desc_start = 4 + i*8;
894 if (u_index >= u_array_size)
895 break; /* User-supplied buffer too small */
897 blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
898 length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
900 if (put_user(blocks, argp))
905 if (put_user(length, argp))
913 if (put_user(u_index, arg))
920 * Get ATAPI_FORMAT_UNIT progress indication.
922 * Userland gives a pointer to an int. The int is set to a progress
923 * indicator 0-65536, with 65536=100%.
925 * If the drive does not support format progress indication, we just check
926 * the dsc bit, and return either 0 or 65536.
929 static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
931 idefloppy_floppy_t *floppy = drive->driver_data;
932 struct ide_atapi_pc pc;
933 int progress_indication = 0x10000;
936 idefloppy_create_request_sense_cmd(&pc);
937 if (idefloppy_queue_pc_tail(drive, &pc))
940 if (floppy->sense_key == 2 &&
943 progress_indication = floppy->progress_indication;
945 /* Else assume format_unit has finished, and we're at 0x10000 */
947 ide_hwif_t *hwif = drive->hwif;
951 local_irq_save(flags);
952 stat = hwif->tp_ops->read_status(hwif);
953 local_irq_restore(flags);
955 progress_indication = ((stat & ATA_DSC) == 0) ? 0 : 0x10000;
958 if (put_user(progress_indication, arg))
964 static sector_t idefloppy_capacity(ide_drive_t *drive)
966 idefloppy_floppy_t *floppy = drive->driver_data;
967 unsigned long capacity = floppy->blocks * floppy->bs_factor;
972 #ifdef CONFIG_IDE_PROC_FS
973 ide_devset_rw(bios_cyl, 0, 1023, bios_cyl);
974 ide_devset_rw(bios_head, 0, 255, bios_head);
975 ide_devset_rw(bios_sect, 0, 63, bios_sect);
977 static int get_ticks(ide_drive_t *drive)
979 idefloppy_floppy_t *floppy = drive->driver_data;
980 return floppy->ticks;
983 static int set_ticks(ide_drive_t *drive, int arg)
985 idefloppy_floppy_t *floppy = drive->driver_data;
990 IDE_DEVSET(ticks, S_RW, 0, 255, get_ticks, set_ticks);
992 static const struct ide_devset *idefloppy_settings[] = {
993 &ide_devset_bios_cyl,
994 &ide_devset_bios_head,
995 &ide_devset_bios_sect,
1001 static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
1003 u16 *id = drive->id;
1006 *((u16 *)&gcw) = id[ATA_ID_CONFIG];
1007 floppy->pc = floppy->pc_stack;
1008 drive->pc_callback = ide_floppy_callback;
1010 if (((gcw[0] & 0x60) >> 5) == 1)
1011 drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
1013 * We used to check revisions here. At this point however I'm giving up.
1014 * Just assume they are all broken, its easier.
1016 * The actual reason for the workarounds was likely a driver bug after
1017 * all rather than a firmware bug, and the workaround below used to hide
1018 * it. It should be fixed as of version 1.9, but to be on the safe side
1019 * we'll leave the limitation below for the 2.2.x tree.
1021 if (!strncmp((char *)&id[ATA_ID_PROD], "IOMEGA ZIP 100 ATAPI", 20)) {
1022 drive->atapi_flags |= IDE_AFLAG_ZIP_DRIVE;
1023 /* This value will be visible in the /proc/ide/hdx/settings */
1024 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1025 blk_queue_max_sectors(drive->queue, 64);
1029 * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
1030 * nasty clicking noises without it, so please don't remove this.
1032 if (strncmp((char *)&id[ATA_ID_PROD], "IOMEGA Clik!", 11) == 0) {
1033 blk_queue_max_sectors(drive->queue, 64);
1034 drive->atapi_flags |= IDE_AFLAG_CLIK_DRIVE;
1037 (void) ide_floppy_get_capacity(drive);
1039 ide_proc_register_driver(drive, floppy->driver);
1042 static void ide_floppy_remove(ide_drive_t *drive)
1044 idefloppy_floppy_t *floppy = drive->driver_data;
1045 struct gendisk *g = floppy->disk;
1047 ide_proc_unregister_driver(drive, floppy->driver);
1051 ide_floppy_put(floppy);
1054 static void idefloppy_cleanup_obj(struct kref *kref)
1056 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1057 ide_drive_t *drive = floppy->drive;
1058 struct gendisk *g = floppy->disk;
1060 drive->driver_data = NULL;
1061 g->private_data = NULL;
1066 #ifdef CONFIG_IDE_PROC_FS
1067 static int proc_idefloppy_read_capacity(char *page, char **start, off_t off,
1068 int count, int *eof, void *data)
1070 ide_drive_t*drive = (ide_drive_t *)data;
1073 len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive));
1074 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
1077 static ide_proc_entry_t idefloppy_proc[] = {
1078 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
1079 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1080 { NULL, 0, NULL, NULL }
1082 #endif /* CONFIG_IDE_PROC_FS */
1084 static int ide_floppy_probe(ide_drive_t *);
1086 static ide_driver_t idefloppy_driver = {
1088 .owner = THIS_MODULE,
1089 .name = "ide-floppy",
1090 .bus = &ide_bus_type,
1092 .probe = ide_floppy_probe,
1093 .remove = ide_floppy_remove,
1094 .version = IDEFLOPPY_VERSION,
1095 .media = ide_floppy,
1096 .do_request = idefloppy_do_request,
1097 .end_request = idefloppy_end_request,
1098 .error = __ide_error,
1099 #ifdef CONFIG_IDE_PROC_FS
1100 .proc = idefloppy_proc,
1101 .settings = idefloppy_settings,
1105 static int idefloppy_open(struct inode *inode, struct file *filp)
1107 struct gendisk *disk = inode->i_bdev->bd_disk;
1108 struct ide_floppy_obj *floppy;
1110 struct ide_atapi_pc pc;
1113 debug_log("Reached %s\n", __func__);
1115 floppy = ide_floppy_get(disk);
1119 drive = floppy->drive;
1123 if (floppy->openers == 1) {
1124 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1127 idefloppy_init_pc(&pc);
1128 pc.c[0] = GPCMD_TEST_UNIT_READY;
1130 if (idefloppy_queue_pc_tail(drive, &pc)) {
1131 idefloppy_create_start_stop_cmd(&pc, 1);
1132 (void) idefloppy_queue_pc_tail(drive, &pc);
1135 if (ide_floppy_get_capacity(drive)
1136 && (filp->f_flags & O_NDELAY) == 0
1138 * Allow O_NDELAY to open a drive without a disk, or with an
1139 * unreadable disk, so that we can get the format capacity
1140 * of the drive or begin the format - Sam
1144 goto out_put_floppy;
1147 if (floppy->wp && (filp->f_mode & 2)) {
1149 goto out_put_floppy;
1151 drive->atapi_flags |= IDE_AFLAG_MEDIA_CHANGED;
1152 /* IOMEGA Clik! drives do not support lock/unlock commands */
1153 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
1154 idefloppy_create_prevent_cmd(&pc, 1);
1155 (void) idefloppy_queue_pc_tail(drive, &pc);
1157 check_disk_change(inode->i_bdev);
1158 } else if (drive->atapi_flags & IDE_AFLAG_FORMAT_IN_PROGRESS) {
1160 goto out_put_floppy;
1166 ide_floppy_put(floppy);
1170 static int idefloppy_release(struct inode *inode, struct file *filp)
1172 struct gendisk *disk = inode->i_bdev->bd_disk;
1173 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1174 ide_drive_t *drive = floppy->drive;
1175 struct ide_atapi_pc pc;
1177 debug_log("Reached %s\n", __func__);
1179 if (floppy->openers == 1) {
1180 /* IOMEGA Clik! drives do not support lock/unlock commands */
1181 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
1182 idefloppy_create_prevent_cmd(&pc, 0);
1183 (void) idefloppy_queue_pc_tail(drive, &pc);
1186 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1191 ide_floppy_put(floppy);
1196 static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1198 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1199 ide_drive_t *drive = floppy->drive;
1201 geo->heads = drive->bios_head;
1202 geo->sectors = drive->bios_sect;
1203 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1207 static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
1208 unsigned long arg, unsigned int cmd)
1210 idefloppy_floppy_t *floppy = drive->driver_data;
1212 if (floppy->openers > 1)
1215 /* The IOMEGA Clik! Drive doesn't support this command -
1216 * no room for an eject mechanism */
1217 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
1218 int prevent = arg ? 1 : 0;
1220 if (cmd == CDROMEJECT)
1223 idefloppy_create_prevent_cmd(pc, prevent);
1224 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1227 if (cmd == CDROMEJECT) {
1228 idefloppy_create_start_stop_cmd(pc, 2);
1229 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1235 static int ide_floppy_format_unit(idefloppy_floppy_t *floppy,
1238 struct ide_atapi_pc pc;
1239 ide_drive_t *drive = floppy->drive;
1240 int blocks, length, flags, err = 0;
1242 if (floppy->openers > 1) {
1243 /* Don't format if someone is using the disk */
1244 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1248 drive->atapi_flags |= IDE_AFLAG_FORMAT_IN_PROGRESS;
1251 * Send ATAPI_FORMAT_UNIT to the drive.
1253 * Userland gives us the following structure:
1255 * struct idefloppy_format_command {
1261 * flags is a bitmask, currently, the only defined flag is:
1263 * 0x01 - verify media after format.
1265 if (get_user(blocks, arg) ||
1266 get_user(length, arg+1) ||
1267 get_user(flags, arg+2)) {
1272 (void) idefloppy_get_sfrp_bit(drive);
1273 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1275 if (idefloppy_queue_pc_tail(drive, &pc))
1280 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1285 static int idefloppy_ioctl(struct inode *inode, struct file *file,
1286 unsigned int cmd, unsigned long arg)
1288 struct block_device *bdev = inode->i_bdev;
1289 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1290 ide_drive_t *drive = floppy->drive;
1291 struct ide_atapi_pc pc;
1292 void __user *argp = (void __user *)arg;
1298 case CDROM_LOCKDOOR:
1299 return ide_floppy_lockdoor(drive, &pc, arg, cmd);
1300 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1302 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
1303 return ide_floppy_get_format_capacities(drive, argp);
1304 case IDEFLOPPY_IOCTL_FORMAT_START:
1305 if (!(file->f_mode & 2))
1308 return ide_floppy_format_unit(floppy, (int __user *)arg);
1309 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1310 return idefloppy_get_format_progress(drive, argp);
1314 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1315 * and CDROM_SEND_PACKET (legacy) ioctls
1317 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1318 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1319 bdev->bd_disk, cmd, argp);
1324 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1329 static int idefloppy_media_changed(struct gendisk *disk)
1331 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1332 ide_drive_t *drive = floppy->drive;
1335 /* do not scan partitions twice if this is a removable device */
1336 if (drive->attach) {
1340 ret = !!(drive->atapi_flags & IDE_AFLAG_MEDIA_CHANGED);
1341 drive->atapi_flags &= ~IDE_AFLAG_MEDIA_CHANGED;
1345 static int idefloppy_revalidate_disk(struct gendisk *disk)
1347 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1348 set_capacity(disk, idefloppy_capacity(floppy->drive));
1352 static struct block_device_operations idefloppy_ops = {
1353 .owner = THIS_MODULE,
1354 .open = idefloppy_open,
1355 .release = idefloppy_release,
1356 .ioctl = idefloppy_ioctl,
1357 .getgeo = idefloppy_getgeo,
1358 .media_changed = idefloppy_media_changed,
1359 .revalidate_disk = idefloppy_revalidate_disk
1362 static int ide_floppy_probe(ide_drive_t *drive)
1364 idefloppy_floppy_t *floppy;
1367 if (!strstr("ide-floppy", drive->driver_req))
1370 if (drive->media != ide_floppy)
1373 if (!ide_check_atapi_device(drive, DRV_NAME)) {
1374 printk(KERN_ERR "ide-floppy: %s: not supported by this version"
1375 " of ide-floppy\n", drive->name);
1378 floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
1380 printk(KERN_ERR "ide-floppy: %s: Can't allocate a floppy"
1381 " structure\n", drive->name);
1385 g = alloc_disk(1 << PARTN_BITS);
1387 goto out_free_floppy;
1389 ide_init_disk(g, drive);
1391 kref_init(&floppy->kref);
1393 floppy->drive = drive;
1394 floppy->driver = &idefloppy_driver;
1397 g->private_data = &floppy->driver;
1399 drive->driver_data = floppy;
1401 idefloppy_setup(drive, floppy);
1403 g->minors = 1 << PARTN_BITS;
1404 g->driverfs_dev = &drive->gendev;
1405 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1406 g->fops = &idefloppy_ops;
1417 static void __exit idefloppy_exit(void)
1419 driver_unregister(&idefloppy_driver.gen_driver);
1422 static int __init idefloppy_init(void)
1424 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
1425 return driver_register(&idefloppy_driver.gen_driver);
1428 MODULE_ALIAS("ide:*m-floppy*");
1429 module_init(idefloppy_init);
1430 module_exit(idefloppy_exit);
1431 MODULE_LICENSE("GPL");
1432 MODULE_DESCRIPTION("ATAPI FLOPPY Driver");