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
79 * In various places in the driver, we need to allocate storage for packet
80 * commands, which will remain valid while we leave the driver to wait for
81 * an interrupt or a timeout event.
83 #define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
85 /* format capacities descriptor codes */
86 #define CAPACITY_INVALID 0x00
87 #define CAPACITY_UNFORMATTED 0x01
88 #define CAPACITY_CURRENT 0x02
89 #define CAPACITY_NO_CARTRIDGE 0x03
92 * Most of our global data which we need to save even as we leave the driver
93 * due to an interrupt or a timer event is stored in a variable of type
94 * idefloppy_floppy_t, defined below.
96 typedef struct ide_floppy_obj {
101 unsigned int openers; /* protected by BKL for now */
103 /* Current packet command */
104 struct ide_atapi_pc *pc;
105 /* Last failed packet command */
106 struct ide_atapi_pc *failed_pc;
107 /* Packet command stack */
108 struct ide_atapi_pc pc_stack[IDEFLOPPY_PC_STACK];
109 /* Next free packet command storage space */
112 struct request request_sense_rq;
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 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
230 xfer_func_t *xf = direction ? tp_ops->output_data : tp_ops->input_data;
231 struct scatterlist *sg = pc->sg;
236 count = min(sg->length - pc->b_count, bcount);
237 if (PageHighMem(sg_page(sg))) {
240 local_irq_save(flags);
241 buf = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
242 xf(drive, NULL, buf + pc->b_count, count);
243 kunmap_atomic(buf - sg->offset, KM_IRQ0);
244 local_irq_restore(flags);
247 xf(drive, NULL, buf + pc->b_count, count);
250 pc->b_count += count;
253 if (pc->b_count == sg->length) {
256 pc->sg = sg = sg_next(sg);
261 idefloppy_end_request(drive, 1, done >> 9);
264 printk(KERN_ERR "%s: leftover data in %s, bcount == %d\n",
265 drive->name, __func__, bcount);
266 ide_pad_transfer(drive, direction, bcount);
270 static void idefloppy_update_buffers(ide_drive_t *drive,
271 struct ide_atapi_pc *pc)
273 struct request *rq = pc->rq;
274 struct bio *bio = rq->bio;
276 while ((bio = rq->bio) != NULL)
277 idefloppy_end_request(drive, 1, 0);
281 * Generate a new packet command request in front of the request queue, before
282 * the current request so that it will be processed immediately, on the next
283 * pass through the driver.
285 static void idefloppy_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
288 struct ide_floppy_obj *floppy = drive->driver_data;
290 blk_rq_init(NULL, rq);
291 rq->buffer = (char *) pc;
292 rq->cmd_type = REQ_TYPE_SPECIAL;
293 rq->cmd_flags |= REQ_PREEMPT;
294 rq->rq_disk = floppy->disk;
295 memcpy(rq->cmd, pc->c, 12);
296 ide_do_drive_cmd(drive, rq);
299 static struct ide_atapi_pc *idefloppy_next_pc_storage(ide_drive_t *drive)
301 idefloppy_floppy_t *floppy = drive->driver_data;
303 if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
304 floppy->pc_stack_index = 0;
305 return (&floppy->pc_stack[floppy->pc_stack_index++]);
308 static void ide_floppy_callback(ide_drive_t *drive)
310 idefloppy_floppy_t *floppy = drive->driver_data;
311 struct ide_atapi_pc *pc = floppy->pc;
312 int uptodate = pc->error ? 0 : 1;
314 debug_log("Reached %s\n", __func__);
316 if (floppy->failed_pc == pc)
317 floppy->failed_pc = NULL;
319 if (pc->c[0] == GPCMD_READ_10 || pc->c[0] == GPCMD_WRITE_10 ||
320 (pc->rq && blk_pc_request(pc->rq)))
321 uptodate = 1; /* FIXME */
322 else if (pc->c[0] == GPCMD_REQUEST_SENSE) {
323 u8 *buf = floppy->pc->buf;
326 floppy->sense_key = buf[2] & 0x0F;
327 floppy->asc = buf[12];
328 floppy->ascq = buf[13];
329 floppy->progress_indication = buf[15] & 0x80 ?
330 (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
332 if (floppy->failed_pc)
333 debug_log("pc = %x, ", floppy->failed_pc->c[0]);
335 debug_log("sense key = %x, asc = %x, ascq = %x\n",
336 floppy->sense_key, floppy->asc, floppy->ascq);
338 printk(KERN_ERR "Error in REQUEST SENSE itself - "
339 "Aborting request!\n");
342 idefloppy_end_request(drive, uptodate, 0);
345 static void idefloppy_init_pc(struct ide_atapi_pc *pc)
347 memset(pc, 0, sizeof(*pc));
348 pc->buf = pc->pc_buf;
349 pc->buf_size = IDEFLOPPY_PC_BUFFER_SIZE;
352 static void idefloppy_create_request_sense_cmd(struct ide_atapi_pc *pc)
354 idefloppy_init_pc(pc);
355 pc->c[0] = GPCMD_REQUEST_SENSE;
361 * Called when an error was detected during the last packet command. We queue a
362 * request sense packet command in the head of the request list.
364 static void idefloppy_retry_pc(ide_drive_t *drive)
366 struct ide_floppy_obj *floppy = drive->driver_data;
367 struct request *rq = &floppy->request_sense_rq;
368 struct ide_atapi_pc *pc;
370 (void)ide_read_error(drive);
371 pc = idefloppy_next_pc_storage(drive);
372 idefloppy_create_request_sense_cmd(pc);
373 idefloppy_queue_pc_head(drive, pc, rq);
376 /* The usual interrupt handler called during a packet command. */
377 static ide_startstop_t idefloppy_pc_intr(ide_drive_t *drive)
379 idefloppy_floppy_t *floppy = drive->driver_data;
381 return ide_pc_intr(drive, floppy->pc, idefloppy_pc_intr,
382 IDEFLOPPY_WAIT_CMD, NULL, idefloppy_update_buffers,
383 idefloppy_retry_pc, NULL, ide_floppy_io_buffers);
387 * What we have here is a classic case of a top half / bottom half interrupt
388 * service routine. In interrupt mode, the device sends an interrupt to signal
389 * that it is ready to receive a packet. However, we need to delay about 2-3
390 * ticks before issuing the packet or we gets in trouble.
392 static int idefloppy_transfer_pc(ide_drive_t *drive)
394 idefloppy_floppy_t *floppy = drive->driver_data;
396 /* Send the actual packet */
397 drive->hwif->tp_ops->output_data(drive, NULL, floppy->pc->c, 12);
399 /* Timeout for the packet command */
400 return IDEFLOPPY_WAIT_CMD;
405 * Called as an interrupt (or directly). When the device says it's ready for a
406 * packet, we schedule the packet transfer to occur about 2-3 ticks later in
409 static ide_startstop_t idefloppy_start_pc_transfer(ide_drive_t *drive)
411 idefloppy_floppy_t *floppy = drive->driver_data;
412 struct ide_atapi_pc *pc = floppy->pc;
413 ide_expiry_t *expiry;
414 unsigned int timeout;
417 * The following delay solves a problem with ATAPI Zip 100 drives
418 * where the Busy flag was apparently being deasserted before the
419 * unit was ready to receive data. This was happening on a
420 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
421 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
422 * used until after the packet is moved in about 50 msec.
424 if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
425 timeout = floppy->ticks;
426 expiry = &idefloppy_transfer_pc;
428 timeout = IDEFLOPPY_WAIT_CMD;
432 return ide_transfer_pc(drive, pc, idefloppy_pc_intr, timeout, expiry);
435 static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
436 struct ide_atapi_pc *pc)
438 /* supress error messages resulting from Medium not present */
439 if (floppy->sense_key == 0x02 &&
440 floppy->asc == 0x3a &&
441 floppy->ascq == 0x00)
444 printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
445 "asc = %2x, ascq = %2x\n",
446 floppy->drive->name, pc->c[0], floppy->sense_key,
447 floppy->asc, floppy->ascq);
451 static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive,
452 struct ide_atapi_pc *pc)
454 idefloppy_floppy_t *floppy = drive->driver_data;
456 if (floppy->failed_pc == NULL &&
457 pc->c[0] != GPCMD_REQUEST_SENSE)
458 floppy->failed_pc = pc;
459 /* Set the current packet command */
462 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
463 if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
464 ide_floppy_report_error(floppy, pc);
466 pc->error = IDEFLOPPY_ERROR_GENERAL;
468 floppy->failed_pc = NULL;
469 drive->pc_callback(drive);
473 debug_log("Retry number - %d\n", pc->retries);
477 return ide_issue_pc(drive, pc, idefloppy_start_pc_transfer,
478 IDEFLOPPY_WAIT_CMD, NULL);
481 static void idefloppy_create_prevent_cmd(struct ide_atapi_pc *pc, int prevent)
483 debug_log("creating prevent removal command, prevent = %d\n", prevent);
485 idefloppy_init_pc(pc);
486 pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
490 static void idefloppy_create_read_capacity_cmd(struct ide_atapi_pc *pc)
492 idefloppy_init_pc(pc);
493 pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
499 static void idefloppy_create_format_unit_cmd(struct ide_atapi_pc *pc, int b,
502 idefloppy_init_pc(pc);
503 pc->c[0] = GPCMD_FORMAT_UNIT;
506 memset(pc->buf, 0, 12);
508 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
510 if (flags & 1) /* Verify bit on... */
511 pc->buf[1] ^= 0x20; /* ... turn off DCRT bit */
514 put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buf[4]));
515 put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buf[8]));
517 pc->flags |= PC_FLAG_WRITING;
520 /* A mode sense command is used to "sense" floppy parameters. */
521 static void idefloppy_create_mode_sense_cmd(struct ide_atapi_pc *pc,
522 u8 page_code, u8 type)
524 u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
526 idefloppy_init_pc(pc);
527 pc->c[0] = GPCMD_MODE_SENSE_10;
529 pc->c[2] = page_code + (type << 6);
532 case IDEFLOPPY_CAPABILITIES_PAGE:
535 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
539 printk(KERN_ERR "ide-floppy: unsupported page code "
540 "in create_mode_sense_cmd\n");
542 put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
543 pc->req_xfer = length;
546 static void idefloppy_create_start_stop_cmd(struct ide_atapi_pc *pc, int start)
548 idefloppy_init_pc(pc);
549 pc->c[0] = GPCMD_START_STOP_UNIT;
553 static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
554 struct ide_atapi_pc *pc, struct request *rq,
555 unsigned long sector)
557 int block = sector / floppy->bs_factor;
558 int blocks = rq->nr_sectors / floppy->bs_factor;
559 int cmd = rq_data_dir(rq);
561 debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
564 idefloppy_init_pc(pc);
565 pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
566 put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
567 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
569 memcpy(rq->cmd, pc->c, 12);
573 if (rq->cmd_flags & REQ_RW)
574 pc->flags |= PC_FLAG_WRITING;
576 pc->req_xfer = pc->buf_size = blocks * floppy->block_size;
577 pc->flags |= PC_FLAG_DMA_OK;
580 static void idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy,
581 struct ide_atapi_pc *pc, struct request *rq)
583 idefloppy_init_pc(pc);
584 memcpy(pc->c, rq->cmd, sizeof(pc->c));
587 if (rq->data_len && rq_data_dir(rq) == WRITE)
588 pc->flags |= PC_FLAG_WRITING;
591 pc->flags |= PC_FLAG_DMA_OK;
593 * possibly problematic, doesn't look like ide-floppy correctly
594 * handled scattered requests if dma fails...
596 pc->req_xfer = pc->buf_size = rq->data_len;
599 static ide_startstop_t idefloppy_do_request(ide_drive_t *drive,
600 struct request *rq, sector_t block_s)
602 idefloppy_floppy_t *floppy = drive->driver_data;
603 ide_hwif_t *hwif = drive->hwif;
604 struct ide_atapi_pc *pc;
605 unsigned long block = (unsigned long)block_s;
607 debug_log("%s: dev: %s, cmd: 0x%x, cmd_type: %x, errors: %d\n",
608 __func__, rq->rq_disk ? rq->rq_disk->disk_name : "?",
609 rq->cmd[0], rq->cmd_type, rq->errors);
611 debug_log("%s: sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",
612 __func__, (long)rq->sector, rq->nr_sectors,
613 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);
646 ide_init_sg_cmd(drive, rq);
647 ide_map_sg(drive, rq);
649 pc->sg = hwif->sg_table;
650 pc->sg_cnt = hwif->sg_nents;
654 return idefloppy_issue_pc(drive, pc);
658 * Add a special packet command request to the tail of the request queue,
659 * and wait for it to be serviced.
661 static int idefloppy_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
663 struct ide_floppy_obj *floppy = drive->driver_data;
667 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
668 rq->buffer = (char *) pc;
669 rq->cmd_type = REQ_TYPE_SPECIAL;
670 memcpy(rq->cmd, pc->c, 12);
671 error = blk_execute_rq(drive->queue, floppy->disk, rq, 0);
678 * Look at the flexible disk page parameters. We ignore the CHS capacity
679 * parameters and use the LBA parameters instead.
681 static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
683 idefloppy_floppy_t *floppy = drive->driver_data;
684 struct ide_atapi_pc pc;
686 int capacity, lba_capacity;
687 u16 transfer_rate, sector_size, cyls, rpm;
690 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE,
693 if (idefloppy_queue_pc_tail(drive, &pc)) {
694 printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
698 floppy->wp = !!(pc.buf[3] & 0x80);
699 set_disk_ro(floppy->disk, floppy->wp);
702 transfer_rate = be16_to_cpup((__be16 *)&pc.buf[8 + 2]);
703 sector_size = be16_to_cpup((__be16 *)&pc.buf[8 + 6]);
704 cyls = be16_to_cpup((__be16 *)&pc.buf[8 + 8]);
705 rpm = be16_to_cpup((__be16 *)&pc.buf[8 + 28]);
706 heads = pc.buf[8 + 4];
707 sectors = pc.buf[8 + 5];
709 capacity = cyls * heads * sectors * sector_size;
711 if (memcmp(page, &floppy->flexible_disk_page, 32))
712 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
713 "%d sector size, %d rpm\n",
714 drive->name, capacity / 1024, cyls, heads,
715 sectors, transfer_rate / 8, sector_size, rpm);
717 memcpy(&floppy->flexible_disk_page, page, 32);
718 drive->bios_cyl = cyls;
719 drive->bios_head = heads;
720 drive->bios_sect = sectors;
721 lba_capacity = floppy->blocks * floppy->block_size;
723 if (capacity < lba_capacity) {
724 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
725 "bytes, but the drive only handles %d\n",
726 drive->name, lba_capacity, capacity);
727 floppy->blocks = floppy->block_size ?
728 capacity / floppy->block_size : 0;
733 static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
735 idefloppy_floppy_t *floppy = drive->driver_data;
736 struct ide_atapi_pc pc;
739 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
742 pc.flags |= PC_FLAG_SUPPRESS_ERROR;
743 if (idefloppy_queue_pc_tail(drive, &pc))
746 floppy->srfp = pc.buf[8 + 2] & 0x40;
751 * Determine if a media is present in the floppy drive, and if so, its LBA
754 static int ide_floppy_get_capacity(ide_drive_t *drive)
756 idefloppy_floppy_t *floppy = drive->driver_data;
757 struct ide_atapi_pc pc;
759 u8 header_len, desc_cnt;
760 int i, rc = 1, blocks, length;
763 drive->bios_head = drive->bios_sect = 0;
765 floppy->bs_factor = 1;
766 set_capacity(floppy->disk, 0);
768 idefloppy_create_read_capacity_cmd(&pc);
769 if (idefloppy_queue_pc_tail(drive, &pc)) {
770 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
773 header_len = pc.buf[3];
774 cap_desc = &pc.buf[4];
775 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
777 for (i = 0; i < desc_cnt; i++) {
778 unsigned int desc_start = 4 + i*8;
780 blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
781 length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
783 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
784 i, blocks * length / 1024, blocks, length);
789 * the code below is valid only for the 1st descriptor, ie i=0
792 switch (pc.buf[desc_start + 4] & 0x03) {
793 /* Clik! drive returns this instead of CAPACITY_CURRENT */
794 case CAPACITY_UNFORMATTED:
795 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
797 * If it is not a clik drive, break out
798 * (maintains previous driver behaviour)
801 case CAPACITY_CURRENT:
802 /* Normal Zip/LS-120 disks */
803 if (memcmp(cap_desc, &floppy->cap_desc, 8))
804 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
805 "sector size\n", drive->name,
806 blocks * length / 1024, blocks, length);
807 memcpy(&floppy->cap_desc, cap_desc, 8);
809 if (!length || length % 512) {
810 printk(KERN_NOTICE "%s: %d bytes block size "
811 "not supported\n", drive->name, length);
813 floppy->blocks = blocks;
814 floppy->block_size = length;
815 floppy->bs_factor = length / 512;
816 if (floppy->bs_factor != 1)
817 printk(KERN_NOTICE "%s: warning: non "
818 "512 bytes block size not "
824 case CAPACITY_NO_CARTRIDGE:
826 * This is a KERN_ERR so it appears on screen
827 * for the user to see
829 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
831 case CAPACITY_INVALID:
832 printk(KERN_ERR "%s: Invalid capacity for disk "
833 "in drive\n", drive->name);
836 debug_log("Descriptor 0 Code: %d\n",
837 pc.buf[desc_start + 4] & 0x03);
840 /* Clik! disk does not support get_flexible_disk_page */
841 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
842 (void) ide_floppy_get_flexible_disk_page(drive);
844 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
849 * Obtain the list of formattable capacities.
850 * Very similar to ide_floppy_get_capacity, except that we push the capacity
851 * descriptors to userland, instead of our own structures.
853 * Userland gives us the following structure:
855 * struct idefloppy_format_capacities {
863 * userland initializes nformats to the number of allocated formats[] records.
864 * On exit we set nformats to the number of records we've actually initialized.
867 static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
869 struct ide_atapi_pc pc;
870 u8 header_len, desc_cnt;
871 int i, blocks, length, u_array_size, u_index;
874 if (get_user(u_array_size, arg))
877 if (u_array_size <= 0)
880 idefloppy_create_read_capacity_cmd(&pc);
881 if (idefloppy_queue_pc_tail(drive, &pc)) {
882 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
886 header_len = pc.buf[3];
887 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
893 * We always skip the first capacity descriptor. That's the current
894 * capacity. We are interested in the remaining descriptors, the
895 * formattable capacities.
897 for (i = 1; i < desc_cnt; i++) {
898 unsigned int desc_start = 4 + i*8;
900 if (u_index >= u_array_size)
901 break; /* User-supplied buffer too small */
903 blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
904 length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
906 if (put_user(blocks, argp))
911 if (put_user(length, argp))
919 if (put_user(u_index, arg))
926 * Get ATAPI_FORMAT_UNIT progress indication.
928 * Userland gives a pointer to an int. The int is set to a progress
929 * indicator 0-65536, with 65536=100%.
931 * If the drive does not support format progress indication, we just check
932 * the dsc bit, and return either 0 or 65536.
935 static int ide_floppy_get_format_progress(ide_drive_t *drive, int __user *arg)
937 idefloppy_floppy_t *floppy = drive->driver_data;
938 struct ide_atapi_pc pc;
939 int progress_indication = 0x10000;
942 idefloppy_create_request_sense_cmd(&pc);
943 if (idefloppy_queue_pc_tail(drive, &pc))
946 if (floppy->sense_key == 2 &&
949 progress_indication = floppy->progress_indication;
951 /* Else assume format_unit has finished, and we're at 0x10000 */
953 ide_hwif_t *hwif = drive->hwif;
957 local_irq_save(flags);
958 stat = hwif->tp_ops->read_status(hwif);
959 local_irq_restore(flags);
961 progress_indication = ((stat & ATA_DSC) == 0) ? 0 : 0x10000;
964 if (put_user(progress_indication, arg))
970 static sector_t idefloppy_capacity(ide_drive_t *drive)
972 idefloppy_floppy_t *floppy = drive->driver_data;
973 unsigned long capacity = floppy->blocks * floppy->bs_factor;
978 #ifdef CONFIG_IDE_PROC_FS
979 ide_devset_rw(bios_cyl, 0, 1023, bios_cyl);
980 ide_devset_rw(bios_head, 0, 255, bios_head);
981 ide_devset_rw(bios_sect, 0, 63, bios_sect);
983 static int get_ticks(ide_drive_t *drive)
985 idefloppy_floppy_t *floppy = drive->driver_data;
986 return floppy->ticks;
989 static int set_ticks(ide_drive_t *drive, int arg)
991 idefloppy_floppy_t *floppy = drive->driver_data;
996 IDE_DEVSET(ticks, S_RW, 0, 255, get_ticks, set_ticks);
998 static const struct ide_devset *idefloppy_settings[] = {
999 &ide_devset_bios_cyl,
1000 &ide_devset_bios_head,
1001 &ide_devset_bios_sect,
1007 static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
1009 u16 *id = drive->id;
1012 *((u16 *)&gcw) = id[ATA_ID_CONFIG];
1013 floppy->pc = floppy->pc_stack;
1014 drive->pc_callback = ide_floppy_callback;
1016 if (((gcw[0] & 0x60) >> 5) == 1)
1017 drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
1019 * We used to check revisions here. At this point however I'm giving up.
1020 * Just assume they are all broken, its easier.
1022 * The actual reason for the workarounds was likely a driver bug after
1023 * all rather than a firmware bug, and the workaround below used to hide
1024 * it. It should be fixed as of version 1.9, but to be on the safe side
1025 * we'll leave the limitation below for the 2.2.x tree.
1027 if (!strncmp((char *)&id[ATA_ID_PROD], "IOMEGA ZIP 100 ATAPI", 20)) {
1028 drive->atapi_flags |= IDE_AFLAG_ZIP_DRIVE;
1029 /* This value will be visible in the /proc/ide/hdx/settings */
1030 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1031 blk_queue_max_sectors(drive->queue, 64);
1035 * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
1036 * nasty clicking noises without it, so please don't remove this.
1038 if (strncmp((char *)&id[ATA_ID_PROD], "IOMEGA Clik!", 11) == 0) {
1039 blk_queue_max_sectors(drive->queue, 64);
1040 drive->atapi_flags |= IDE_AFLAG_CLIK_DRIVE;
1043 (void) ide_floppy_get_capacity(drive);
1045 ide_proc_register_driver(drive, floppy->driver);
1048 static void ide_floppy_remove(ide_drive_t *drive)
1050 idefloppy_floppy_t *floppy = drive->driver_data;
1051 struct gendisk *g = floppy->disk;
1053 ide_proc_unregister_driver(drive, floppy->driver);
1057 ide_floppy_put(floppy);
1060 static void idefloppy_cleanup_obj(struct kref *kref)
1062 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1063 ide_drive_t *drive = floppy->drive;
1064 struct gendisk *g = floppy->disk;
1066 drive->driver_data = NULL;
1067 g->private_data = NULL;
1072 #ifdef CONFIG_IDE_PROC_FS
1073 static int proc_idefloppy_read_capacity(char *page, char **start, off_t off,
1074 int count, int *eof, void *data)
1076 ide_drive_t*drive = (ide_drive_t *)data;
1079 len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive));
1080 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
1083 static ide_proc_entry_t idefloppy_proc[] = {
1084 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
1085 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1086 { NULL, 0, NULL, NULL }
1088 #endif /* CONFIG_IDE_PROC_FS */
1090 static int ide_floppy_probe(ide_drive_t *);
1092 static ide_driver_t idefloppy_driver = {
1094 .owner = THIS_MODULE,
1095 .name = "ide-floppy",
1096 .bus = &ide_bus_type,
1098 .probe = ide_floppy_probe,
1099 .remove = ide_floppy_remove,
1100 .version = IDEFLOPPY_VERSION,
1101 .media = ide_floppy,
1102 .do_request = idefloppy_do_request,
1103 .end_request = idefloppy_end_request,
1104 .error = __ide_error,
1105 #ifdef CONFIG_IDE_PROC_FS
1106 .proc = idefloppy_proc,
1107 .settings = idefloppy_settings,
1111 static int idefloppy_open(struct inode *inode, struct file *filp)
1113 struct gendisk *disk = inode->i_bdev->bd_disk;
1114 struct ide_floppy_obj *floppy;
1116 struct ide_atapi_pc pc;
1119 debug_log("Reached %s\n", __func__);
1121 floppy = ide_floppy_get(disk);
1125 drive = floppy->drive;
1129 if (floppy->openers == 1) {
1130 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1133 idefloppy_init_pc(&pc);
1134 pc.c[0] = GPCMD_TEST_UNIT_READY;
1136 if (idefloppy_queue_pc_tail(drive, &pc)) {
1137 idefloppy_create_start_stop_cmd(&pc, 1);
1138 (void) idefloppy_queue_pc_tail(drive, &pc);
1141 if (ide_floppy_get_capacity(drive)
1142 && (filp->f_flags & O_NDELAY) == 0
1144 * Allow O_NDELAY to open a drive without a disk, or with an
1145 * unreadable disk, so that we can get the format capacity
1146 * of the drive or begin the format - Sam
1150 goto out_put_floppy;
1153 if (floppy->wp && (filp->f_mode & 2)) {
1155 goto out_put_floppy;
1157 drive->atapi_flags |= IDE_AFLAG_MEDIA_CHANGED;
1158 /* IOMEGA Clik! drives do not support lock/unlock commands */
1159 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
1160 idefloppy_create_prevent_cmd(&pc, 1);
1161 (void) idefloppy_queue_pc_tail(drive, &pc);
1163 check_disk_change(inode->i_bdev);
1164 } else if (drive->atapi_flags & IDE_AFLAG_FORMAT_IN_PROGRESS) {
1166 goto out_put_floppy;
1172 ide_floppy_put(floppy);
1176 static int idefloppy_release(struct inode *inode, struct file *filp)
1178 struct gendisk *disk = inode->i_bdev->bd_disk;
1179 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1180 ide_drive_t *drive = floppy->drive;
1181 struct ide_atapi_pc pc;
1183 debug_log("Reached %s\n", __func__);
1185 if (floppy->openers == 1) {
1186 /* IOMEGA Clik! drives do not support lock/unlock commands */
1187 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
1188 idefloppy_create_prevent_cmd(&pc, 0);
1189 (void) idefloppy_queue_pc_tail(drive, &pc);
1192 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1197 ide_floppy_put(floppy);
1202 static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1204 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1205 ide_drive_t *drive = floppy->drive;
1207 geo->heads = drive->bios_head;
1208 geo->sectors = drive->bios_sect;
1209 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1213 static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
1214 unsigned long arg, unsigned int cmd)
1216 idefloppy_floppy_t *floppy = drive->driver_data;
1218 if (floppy->openers > 1)
1221 /* The IOMEGA Clik! Drive doesn't support this command -
1222 * no room for an eject mechanism */
1223 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
1224 int prevent = arg ? 1 : 0;
1226 if (cmd == CDROMEJECT)
1229 idefloppy_create_prevent_cmd(pc, prevent);
1230 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1233 if (cmd == CDROMEJECT) {
1234 idefloppy_create_start_stop_cmd(pc, 2);
1235 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1241 static int ide_floppy_format_unit(ide_drive_t *drive, int __user *arg)
1243 idefloppy_floppy_t *floppy = drive->driver_data;
1244 struct ide_atapi_pc pc;
1245 int blocks, length, flags, err = 0;
1247 if (floppy->openers > 1) {
1248 /* Don't format if someone is using the disk */
1249 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1253 drive->atapi_flags |= IDE_AFLAG_FORMAT_IN_PROGRESS;
1256 * Send ATAPI_FORMAT_UNIT to the drive.
1258 * Userland gives us the following structure:
1260 * struct idefloppy_format_command {
1266 * flags is a bitmask, currently, the only defined flag is:
1268 * 0x01 - verify media after format.
1270 if (get_user(blocks, arg) ||
1271 get_user(length, arg+1) ||
1272 get_user(flags, arg+2)) {
1277 (void) idefloppy_get_sfrp_bit(drive);
1278 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1280 if (idefloppy_queue_pc_tail(drive, &pc))
1285 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1289 static int ide_floppy_format_ioctl(ide_drive_t *drive, struct file *file,
1290 unsigned int cmd, void __user *argp)
1293 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1295 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
1296 return ide_floppy_get_format_capacities(drive, argp);
1297 case IDEFLOPPY_IOCTL_FORMAT_START:
1298 if (!(file->f_mode & 2))
1300 return ide_floppy_format_unit(drive, (int __user *)argp);
1301 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1302 return ide_floppy_get_format_progress(drive, argp);
1308 static int idefloppy_ioctl(struct inode *inode, struct file *file,
1309 unsigned int cmd, unsigned long arg)
1311 struct block_device *bdev = inode->i_bdev;
1312 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1313 ide_drive_t *drive = floppy->drive;
1314 struct ide_atapi_pc pc;
1315 void __user *argp = (void __user *)arg;
1318 if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR)
1319 return ide_floppy_lockdoor(drive, &pc, arg, cmd);
1321 err = ide_floppy_format_ioctl(drive, file, cmd, argp);
1326 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1327 * and CDROM_SEND_PACKET (legacy) ioctls
1329 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1330 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1331 bdev->bd_disk, cmd, argp);
1334 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1339 static int idefloppy_media_changed(struct gendisk *disk)
1341 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1342 ide_drive_t *drive = floppy->drive;
1345 /* do not scan partitions twice if this is a removable device */
1346 if (drive->attach) {
1350 ret = !!(drive->atapi_flags & IDE_AFLAG_MEDIA_CHANGED);
1351 drive->atapi_flags &= ~IDE_AFLAG_MEDIA_CHANGED;
1355 static int idefloppy_revalidate_disk(struct gendisk *disk)
1357 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1358 set_capacity(disk, idefloppy_capacity(floppy->drive));
1362 static struct block_device_operations idefloppy_ops = {
1363 .owner = THIS_MODULE,
1364 .open = idefloppy_open,
1365 .release = idefloppy_release,
1366 .ioctl = idefloppy_ioctl,
1367 .getgeo = idefloppy_getgeo,
1368 .media_changed = idefloppy_media_changed,
1369 .revalidate_disk = idefloppy_revalidate_disk
1372 static int ide_floppy_probe(ide_drive_t *drive)
1374 idefloppy_floppy_t *floppy;
1377 if (!strstr("ide-floppy", drive->driver_req))
1380 if (drive->media != ide_floppy)
1383 if (!ide_check_atapi_device(drive, DRV_NAME)) {
1384 printk(KERN_ERR "ide-floppy: %s: not supported by this version"
1385 " of ide-floppy\n", drive->name);
1388 floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
1390 printk(KERN_ERR "ide-floppy: %s: Can't allocate a floppy"
1391 " structure\n", drive->name);
1395 g = alloc_disk(1 << PARTN_BITS);
1397 goto out_free_floppy;
1399 ide_init_disk(g, drive);
1401 kref_init(&floppy->kref);
1403 floppy->drive = drive;
1404 floppy->driver = &idefloppy_driver;
1407 g->private_data = &floppy->driver;
1409 drive->driver_data = floppy;
1411 idefloppy_setup(drive, floppy);
1413 g->minors = 1 << PARTN_BITS;
1414 g->driverfs_dev = &drive->gendev;
1415 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1416 g->fops = &idefloppy_ops;
1427 static void __exit idefloppy_exit(void)
1429 driver_unregister(&idefloppy_driver.gen_driver);
1432 static int __init idefloppy_init(void)
1434 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
1435 return driver_register(&idefloppy_driver.gen_driver);
1438 MODULE_ALIAS("ide:*m-floppy*");
1439 module_init(idefloppy_init);
1440 module_exit(idefloppy_exit);
1441 MODULE_LICENSE("GPL");
1442 MODULE_DESCRIPTION("ATAPI FLOPPY Driver");