2 * IDE ATAPI floppy driver.
4 * Copyright (C) 1996-1999 Gadi Oxman <gadio@netvision.net.il>
5 * Copyright (C) 2000-2002 Paul Bristow <paul@paulbristow.net>
6 * Copyright (C) 2005 Bartlomiej Zolnierkiewicz
8 * This driver supports the following IDE floppy drives:
10 * LS-120/240 SuperDisk
12 * Iomega PC Card Clik!/PocketZip
14 * For a historical changelog see
15 * Documentation/ide/ChangeLog.ide-floppy.1996-2002
18 #define IDEFLOPPY_VERSION "1.00"
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/kernel.h>
24 #include <linux/delay.h>
25 #include <linux/timer.h>
27 #include <linux/interrupt.h>
28 #include <linux/major.h>
29 #include <linux/errno.h>
30 #include <linux/genhd.h>
31 #include <linux/slab.h>
32 #include <linux/cdrom.h>
33 #include <linux/ide.h>
34 #include <linux/hdreg.h>
35 #include <linux/bitops.h>
36 #include <linux/mutex.h>
38 #include <scsi/scsi_ioctl.h>
40 #include <asm/byteorder.h>
41 #include <linux/irq.h>
42 #include <linux/uaccess.h>
44 #include <asm/unaligned.h>
46 /* define to see debug info */
47 #define IDEFLOPPY_DEBUG_LOG 0
49 /* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
50 #define IDEFLOPPY_DEBUG(fmt, args...)
52 #if IDEFLOPPY_DEBUG_LOG
53 #define debug_log(fmt, args...) \
54 printk(KERN_INFO "ide-floppy: " fmt, ## args)
56 #define debug_log(fmt, args...) do {} while (0)
60 /* Some drives require a longer irq timeout. */
61 #define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
64 * After each failed packet command we issue a request sense command and retry
65 * the packet command IDEFLOPPY_MAX_PC_RETRIES times.
67 #define IDEFLOPPY_MAX_PC_RETRIES 3
70 * With each packet command, we allocate a buffer of IDEFLOPPY_PC_BUFFER_SIZE
73 #define IDEFLOPPY_PC_BUFFER_SIZE 256
76 * In various places in the driver, we need to allocate storage for packet
77 * commands and requests, which will remain valid while we leave the driver to
78 * wait for an interrupt or a timeout event.
80 #define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
82 /* format capacities descriptor codes */
83 #define CAPACITY_INVALID 0x00
84 #define CAPACITY_UNFORMATTED 0x01
85 #define CAPACITY_CURRENT 0x02
86 #define CAPACITY_NO_CARTRIDGE 0x03
89 * Most of our global data which we need to save even as we leave the driver
90 * due to an interrupt or a timer event is stored in a variable of type
91 * idefloppy_floppy_t, defined below.
93 typedef struct ide_floppy_obj {
98 unsigned int openers; /* protected by BKL for now */
100 /* Current packet command */
101 struct ide_atapi_pc *pc;
102 /* Last failed packet command */
103 struct ide_atapi_pc *failed_pc;
104 /* Packet command stack */
105 struct ide_atapi_pc pc_stack[IDEFLOPPY_PC_STACK];
106 /* Next free packet command storage space */
108 struct request rq_stack[IDEFLOPPY_PC_STACK];
109 /* We implement a circular array */
112 /* Last error information */
113 u8 sense_key, asc, ascq;
114 /* delay this long before sending packet command */
116 int progress_indication;
118 /* Device information */
120 int blocks, block_size, bs_factor;
121 /* Last format capacity descriptor */
123 /* Copy of the flexible disk page */
124 u8 flexible_disk_page[32];
127 /* Supports format progress report */
129 } idefloppy_floppy_t;
131 #define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
133 /* Defines for the MODE SENSE command */
134 #define MODE_SENSE_CURRENT 0x00
135 #define MODE_SENSE_CHANGEABLE 0x01
136 #define MODE_SENSE_DEFAULT 0x02
137 #define MODE_SENSE_SAVED 0x03
139 /* IOCTLs used in low-level formatting. */
140 #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
141 #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
142 #define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
143 #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
145 /* Error code returned in rq->errors to the higher part of the driver. */
146 #define IDEFLOPPY_ERROR_GENERAL 101
149 * Pages of the SELECT SENSE / MODE SENSE packet commands.
150 * See SFF-8070i spec.
152 #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
153 #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
155 static DEFINE_MUTEX(idefloppy_ref_mutex);
157 #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
159 #define ide_floppy_g(disk) \
160 container_of((disk)->private_data, struct ide_floppy_obj, driver)
162 static void idefloppy_cleanup_obj(struct kref *);
164 static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
166 struct ide_floppy_obj *floppy = NULL;
168 mutex_lock(&idefloppy_ref_mutex);
169 floppy = ide_floppy_g(disk);
171 if (ide_device_get(floppy->drive))
174 kref_get(&floppy->kref);
176 mutex_unlock(&idefloppy_ref_mutex);
180 static void ide_floppy_put(struct ide_floppy_obj *floppy)
182 ide_drive_t *drive = floppy->drive;
184 mutex_lock(&idefloppy_ref_mutex);
185 kref_put(&floppy->kref, idefloppy_cleanup_obj);
186 ide_device_put(drive);
187 mutex_unlock(&idefloppy_ref_mutex);
191 * Used to finish servicing a request. For read/write requests, we will call
192 * ide_end_request to pass to the next buffer.
194 static int idefloppy_end_request(ide_drive_t *drive, int uptodate, int nsecs)
196 idefloppy_floppy_t *floppy = drive->driver_data;
197 struct request *rq = HWGROUP(drive)->rq;
200 debug_log("Reached %s\n", __func__);
203 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
204 case 1: error = 0; break;
205 default: error = uptodate;
208 floppy->failed_pc = NULL;
209 /* Why does this happen? */
212 if (!blk_special_request(rq)) {
213 /* our real local end request function */
214 ide_end_request(drive, uptodate, nsecs);
218 /* fixme: need to move this local also */
219 ide_end_drive_cmd(drive, 0, 0);
223 static void ide_floppy_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
224 unsigned int bcount, int direction)
226 ide_hwif_t *hwif = drive->hwif;
227 struct request *rq = pc->rq;
228 struct req_iterator iter;
229 struct bio_vec *bvec;
234 rq_for_each_segment(bvec, rq, iter) {
238 count = min(bvec->bv_len, bcount);
240 data = bvec_kmap_irq(bvec, &flags);
242 hwif->tp_ops->output_data(drive, NULL, data, count);
244 hwif->tp_ops->input_data(drive, NULL, data, count);
245 bvec_kunmap_irq(data, &flags);
248 pc->b_count += count;
252 idefloppy_end_request(drive, 1, done >> 9);
255 printk(KERN_ERR "%s: leftover data in %s, bcount == %d\n",
256 drive->name, __func__, bcount);
257 ide_pad_transfer(drive, direction, bcount);
261 static void idefloppy_update_buffers(ide_drive_t *drive,
262 struct ide_atapi_pc *pc)
264 struct request *rq = pc->rq;
265 struct bio *bio = rq->bio;
267 while ((bio = rq->bio) != NULL)
268 idefloppy_end_request(drive, 1, 0);
272 * Generate a new packet command request in front of the request queue, before
273 * the current request so that it will be processed immediately, on the next
274 * pass through the driver.
276 static void idefloppy_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
279 struct ide_floppy_obj *floppy = drive->driver_data;
281 blk_rq_init(NULL, rq);
282 rq->buffer = (char *) pc;
283 rq->cmd_type = REQ_TYPE_SPECIAL;
284 rq->cmd_flags |= REQ_PREEMPT;
285 rq->rq_disk = floppy->disk;
286 memcpy(rq->cmd, pc->c, 12);
287 ide_do_drive_cmd(drive, rq);
290 static struct ide_atapi_pc *idefloppy_next_pc_storage(ide_drive_t *drive)
292 idefloppy_floppy_t *floppy = drive->driver_data;
294 if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
295 floppy->pc_stack_index = 0;
296 return (&floppy->pc_stack[floppy->pc_stack_index++]);
299 static struct request *idefloppy_next_rq_storage(ide_drive_t *drive)
301 idefloppy_floppy_t *floppy = drive->driver_data;
303 if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
304 floppy->rq_stack_index = 0;
305 return (&floppy->rq_stack[floppy->rq_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_atapi_pc *pc;
369 (void)ide_read_error(drive);
370 pc = idefloppy_next_pc_storage(drive);
371 rq = idefloppy_next_rq_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);
572 pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
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));
586 pc->b_count = rq->data_len;
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 struct ide_atapi_pc *pc;
604 unsigned long block = (unsigned long)block_s;
606 debug_log("dev: %s, cmd_type: %x, errors: %d\n",
607 rq->rq_disk ? rq->rq_disk->disk_name : "?",
608 rq->cmd_type, rq->errors);
609 debug_log("sector: %ld, nr_sectors: %ld, "
610 "current_nr_sectors: %d\n", (long)rq->sector,
611 rq->nr_sectors, rq->current_nr_sectors);
613 if (rq->errors >= ERROR_MAX) {
614 if (floppy->failed_pc)
615 ide_floppy_report_error(floppy, floppy->failed_pc);
617 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
619 idefloppy_end_request(drive, 0, 0);
622 if (blk_fs_request(rq)) {
623 if (((long)rq->sector % floppy->bs_factor) ||
624 (rq->nr_sectors % floppy->bs_factor)) {
625 printk(KERN_ERR "%s: unsupported r/w request size\n",
627 idefloppy_end_request(drive, 0, 0);
630 pc = idefloppy_next_pc_storage(drive);
631 idefloppy_create_rw_cmd(floppy, pc, rq, block);
632 } else if (blk_special_request(rq)) {
633 pc = (struct ide_atapi_pc *) rq->buffer;
634 } else if (blk_pc_request(rq)) {
635 pc = idefloppy_next_pc_storage(drive);
636 idefloppy_blockpc_cmd(floppy, pc, rq);
638 blk_dump_rq_flags(rq,
639 "ide-floppy: unsupported command in queue");
640 idefloppy_end_request(drive, 0, 0);
646 return idefloppy_issue_pc(drive, pc);
650 * Add a special packet command request to the tail of the request queue,
651 * and wait for it to be serviced.
653 static int idefloppy_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
655 struct ide_floppy_obj *floppy = drive->driver_data;
659 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
660 rq->buffer = (char *) pc;
661 rq->cmd_type = REQ_TYPE_SPECIAL;
662 memcpy(rq->cmd, pc->c, 12);
663 error = blk_execute_rq(drive->queue, floppy->disk, rq, 0);
670 * Look at the flexible disk page parameters. We ignore the CHS capacity
671 * parameters and use the LBA parameters instead.
673 static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
675 idefloppy_floppy_t *floppy = drive->driver_data;
676 struct ide_atapi_pc pc;
678 int capacity, lba_capacity;
679 u16 transfer_rate, sector_size, cyls, rpm;
682 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE,
685 if (idefloppy_queue_pc_tail(drive, &pc)) {
686 printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
690 floppy->wp = !!(pc.buf[3] & 0x80);
691 set_disk_ro(floppy->disk, floppy->wp);
694 transfer_rate = be16_to_cpup((__be16 *)&pc.buf[8 + 2]);
695 sector_size = be16_to_cpup((__be16 *)&pc.buf[8 + 6]);
696 cyls = be16_to_cpup((__be16 *)&pc.buf[8 + 8]);
697 rpm = be16_to_cpup((__be16 *)&pc.buf[8 + 28]);
698 heads = pc.buf[8 + 4];
699 sectors = pc.buf[8 + 5];
701 capacity = cyls * heads * sectors * sector_size;
703 if (memcmp(page, &floppy->flexible_disk_page, 32))
704 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
705 "%d sector size, %d rpm\n",
706 drive->name, capacity / 1024, cyls, heads,
707 sectors, transfer_rate / 8, sector_size, rpm);
709 memcpy(&floppy->flexible_disk_page, page, 32);
710 drive->bios_cyl = cyls;
711 drive->bios_head = heads;
712 drive->bios_sect = sectors;
713 lba_capacity = floppy->blocks * floppy->block_size;
715 if (capacity < lba_capacity) {
716 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
717 "bytes, but the drive only handles %d\n",
718 drive->name, lba_capacity, capacity);
719 floppy->blocks = floppy->block_size ?
720 capacity / floppy->block_size : 0;
725 static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
727 idefloppy_floppy_t *floppy = drive->driver_data;
728 struct ide_atapi_pc pc;
731 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
734 pc.flags |= PC_FLAG_SUPPRESS_ERROR;
735 if (idefloppy_queue_pc_tail(drive, &pc))
738 floppy->srfp = pc.buf[8 + 2] & 0x40;
743 * Determine if a media is present in the floppy drive, and if so, its LBA
746 static int ide_floppy_get_capacity(ide_drive_t *drive)
748 idefloppy_floppy_t *floppy = drive->driver_data;
749 struct ide_atapi_pc pc;
751 u8 header_len, desc_cnt;
752 int i, rc = 1, blocks, length;
755 drive->bios_head = drive->bios_sect = 0;
757 floppy->bs_factor = 1;
758 set_capacity(floppy->disk, 0);
760 idefloppy_create_read_capacity_cmd(&pc);
761 if (idefloppy_queue_pc_tail(drive, &pc)) {
762 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
765 header_len = pc.buf[3];
766 cap_desc = &pc.buf[4];
767 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
769 for (i = 0; i < desc_cnt; i++) {
770 unsigned int desc_start = 4 + i*8;
772 blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
773 length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
775 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
776 i, blocks * length / 1024, blocks, length);
781 * the code below is valid only for the 1st descriptor, ie i=0
784 switch (pc.buf[desc_start + 4] & 0x03) {
785 /* Clik! drive returns this instead of CAPACITY_CURRENT */
786 case CAPACITY_UNFORMATTED:
787 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
789 * If it is not a clik drive, break out
790 * (maintains previous driver behaviour)
793 case CAPACITY_CURRENT:
794 /* Normal Zip/LS-120 disks */
795 if (memcmp(cap_desc, &floppy->cap_desc, 8))
796 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
797 "sector size\n", drive->name,
798 blocks * length / 1024, blocks, length);
799 memcpy(&floppy->cap_desc, cap_desc, 8);
801 if (!length || length % 512) {
802 printk(KERN_NOTICE "%s: %d bytes block size "
803 "not supported\n", drive->name, length);
805 floppy->blocks = blocks;
806 floppy->block_size = length;
807 floppy->bs_factor = length / 512;
808 if (floppy->bs_factor != 1)
809 printk(KERN_NOTICE "%s: warning: non "
810 "512 bytes block size not "
816 case CAPACITY_NO_CARTRIDGE:
818 * This is a KERN_ERR so it appears on screen
819 * for the user to see
821 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
823 case CAPACITY_INVALID:
824 printk(KERN_ERR "%s: Invalid capacity for disk "
825 "in drive\n", drive->name);
828 debug_log("Descriptor 0 Code: %d\n",
829 pc.buf[desc_start + 4] & 0x03);
832 /* Clik! disk does not support get_flexible_disk_page */
833 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
834 (void) ide_floppy_get_flexible_disk_page(drive);
836 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
841 * Obtain the list of formattable capacities.
842 * Very similar to ide_floppy_get_capacity, except that we push the capacity
843 * descriptors to userland, instead of our own structures.
845 * Userland gives us the following structure:
847 * struct idefloppy_format_capacities {
855 * userland initializes nformats to the number of allocated formats[] records.
856 * On exit we set nformats to the number of records we've actually initialized.
859 static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
861 struct ide_atapi_pc pc;
862 u8 header_len, desc_cnt;
863 int i, blocks, length, u_array_size, u_index;
866 if (get_user(u_array_size, arg))
869 if (u_array_size <= 0)
872 idefloppy_create_read_capacity_cmd(&pc);
873 if (idefloppy_queue_pc_tail(drive, &pc)) {
874 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
877 header_len = pc.buf[3];
878 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
884 * We always skip the first capacity descriptor. That's the current
885 * capacity. We are interested in the remaining descriptors, the
886 * formattable capacities.
888 for (i = 1; i < desc_cnt; i++) {
889 unsigned int desc_start = 4 + i*8;
891 if (u_index >= u_array_size)
892 break; /* User-supplied buffer too small */
894 blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
895 length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
897 if (put_user(blocks, argp))
901 if (put_user(length, argp))
908 if (put_user(u_index, arg))
914 * Get ATAPI_FORMAT_UNIT progress indication.
916 * Userland gives a pointer to an int. The int is set to a progress
917 * indicator 0-65536, with 65536=100%.
919 * If the drive does not support format progress indication, we just check
920 * the dsc bit, and return either 0 or 65536.
923 static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
925 idefloppy_floppy_t *floppy = drive->driver_data;
926 struct ide_atapi_pc pc;
927 int progress_indication = 0x10000;
930 idefloppy_create_request_sense_cmd(&pc);
931 if (idefloppy_queue_pc_tail(drive, &pc))
934 if (floppy->sense_key == 2 &&
937 progress_indication = floppy->progress_indication;
939 /* Else assume format_unit has finished, and we're at 0x10000 */
941 ide_hwif_t *hwif = drive->hwif;
945 local_irq_save(flags);
946 stat = hwif->tp_ops->read_status(hwif);
947 local_irq_restore(flags);
949 progress_indication = ((stat & ATA_DSC) == 0) ? 0 : 0x10000;
951 if (put_user(progress_indication, arg))
957 static sector_t idefloppy_capacity(ide_drive_t *drive)
959 idefloppy_floppy_t *floppy = drive->driver_data;
960 unsigned long capacity = floppy->blocks * floppy->bs_factor;
966 * Check whether we can support a drive, based on the ATAPI IDENTIFY command
969 static int idefloppy_identify_device(ide_drive_t *drive, u16 *id)
972 u8 device_type, protocol, removable, drq_type, packet_size;
974 *((u16 *)&gcw) = id[ATA_ID_CONFIG];
976 device_type = gcw[1] & 0x1F;
977 removable = (gcw[0] & 0x80) >> 7;
978 protocol = (gcw[1] & 0xC0) >> 6;
979 drq_type = (gcw[0] & 0x60) >> 5;
980 packet_size = gcw[0] & 0x03;
983 /* kludge for Apple PowerBook internal zip */
984 if (device_type == 5 &&
985 !strstr((char *)&id[ATA_ID_PROD], "CD-ROM") &&
986 strstr((char *)&id[ATA_ID_PROD], "ZIP"))
991 printk(KERN_ERR "ide-floppy: Protocol (0x%02x) is not ATAPI\n",
993 else if (device_type != 0)
994 printk(KERN_ERR "ide-floppy: Device type (0x%02x) is not set "
995 "to floppy\n", device_type);
997 printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
998 else if (drq_type == 3)
999 printk(KERN_ERR "ide-floppy: Sorry, DRQ type (0x%02x) not "
1000 "supported\n", drq_type);
1001 else if (packet_size != 0)
1002 printk(KERN_ERR "ide-floppy: Packet size (0x%02x) is not 12 "
1003 "bytes\n", packet_size);
1009 #ifdef CONFIG_IDE_PROC_FS
1010 ide_devset_rw(bios_cyl, 0, 1023, bios_cyl);
1011 ide_devset_rw(bios_head, 0, 255, bios_head);
1012 ide_devset_rw(bios_sect, 0, 63, bios_sect);
1014 static int get_ticks(ide_drive_t *drive)
1016 idefloppy_floppy_t *floppy = drive->driver_data;
1017 return floppy->ticks;
1020 static int set_ticks(ide_drive_t *drive, int arg)
1022 idefloppy_floppy_t *floppy = drive->driver_data;
1023 floppy->ticks = arg;
1027 IDE_DEVSET(ticks, S_RW, 0, 255, get_ticks, set_ticks);
1029 static const struct ide_devset *idefloppy_settings[] = {
1030 &ide_devset_bios_cyl,
1031 &ide_devset_bios_head,
1032 &ide_devset_bios_sect,
1038 static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
1040 u16 *id = drive->id;
1043 *((u16 *)&gcw) = id[ATA_ID_CONFIG];
1044 floppy->pc = floppy->pc_stack;
1045 drive->pc_callback = ide_floppy_callback;
1047 if (((gcw[0] & 0x60) >> 5) == 1)
1048 drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
1050 * We used to check revisions here. At this point however I'm giving up.
1051 * Just assume they are all broken, its easier.
1053 * The actual reason for the workarounds was likely a driver bug after
1054 * all rather than a firmware bug, and the workaround below used to hide
1055 * it. It should be fixed as of version 1.9, but to be on the safe side
1056 * we'll leave the limitation below for the 2.2.x tree.
1058 if (!strncmp((char *)&id[ATA_ID_PROD], "IOMEGA ZIP 100 ATAPI", 20)) {
1059 drive->atapi_flags |= IDE_AFLAG_ZIP_DRIVE;
1060 /* This value will be visible in the /proc/ide/hdx/settings */
1061 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1062 blk_queue_max_sectors(drive->queue, 64);
1066 * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
1067 * nasty clicking noises without it, so please don't remove this.
1069 if (strncmp((char *)&id[ATA_ID_PROD], "IOMEGA Clik!", 11) == 0) {
1070 blk_queue_max_sectors(drive->queue, 64);
1071 drive->atapi_flags |= IDE_AFLAG_CLIK_DRIVE;
1074 (void) ide_floppy_get_capacity(drive);
1076 ide_proc_register_driver(drive, floppy->driver);
1079 static void ide_floppy_remove(ide_drive_t *drive)
1081 idefloppy_floppy_t *floppy = drive->driver_data;
1082 struct gendisk *g = floppy->disk;
1084 ide_proc_unregister_driver(drive, floppy->driver);
1088 ide_floppy_put(floppy);
1091 static void idefloppy_cleanup_obj(struct kref *kref)
1093 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1094 ide_drive_t *drive = floppy->drive;
1095 struct gendisk *g = floppy->disk;
1097 drive->driver_data = NULL;
1098 g->private_data = NULL;
1103 #ifdef CONFIG_IDE_PROC_FS
1104 static int proc_idefloppy_read_capacity(char *page, char **start, off_t off,
1105 int count, int *eof, void *data)
1107 ide_drive_t*drive = (ide_drive_t *)data;
1110 len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive));
1111 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
1114 static ide_proc_entry_t idefloppy_proc[] = {
1115 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
1116 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1117 { NULL, 0, NULL, NULL }
1119 #endif /* CONFIG_IDE_PROC_FS */
1121 static int ide_floppy_probe(ide_drive_t *);
1123 static ide_driver_t idefloppy_driver = {
1125 .owner = THIS_MODULE,
1126 .name = "ide-floppy",
1127 .bus = &ide_bus_type,
1129 .probe = ide_floppy_probe,
1130 .remove = ide_floppy_remove,
1131 .version = IDEFLOPPY_VERSION,
1132 .media = ide_floppy,
1133 .do_request = idefloppy_do_request,
1134 .end_request = idefloppy_end_request,
1135 .error = __ide_error,
1136 #ifdef CONFIG_IDE_PROC_FS
1137 .proc = idefloppy_proc,
1138 .settings = idefloppy_settings,
1142 static int idefloppy_open(struct inode *inode, struct file *filp)
1144 struct gendisk *disk = inode->i_bdev->bd_disk;
1145 struct ide_floppy_obj *floppy;
1147 struct ide_atapi_pc pc;
1150 debug_log("Reached %s\n", __func__);
1152 floppy = ide_floppy_get(disk);
1156 drive = floppy->drive;
1160 if (floppy->openers == 1) {
1161 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1164 idefloppy_init_pc(&pc);
1165 pc.c[0] = GPCMD_TEST_UNIT_READY;
1167 if (idefloppy_queue_pc_tail(drive, &pc)) {
1168 idefloppy_create_start_stop_cmd(&pc, 1);
1169 (void) idefloppy_queue_pc_tail(drive, &pc);
1172 if (ide_floppy_get_capacity(drive)
1173 && (filp->f_flags & O_NDELAY) == 0
1175 * Allow O_NDELAY to open a drive without a disk, or with an
1176 * unreadable disk, so that we can get the format capacity
1177 * of the drive or begin the format - Sam
1181 goto out_put_floppy;
1184 if (floppy->wp && (filp->f_mode & 2)) {
1186 goto out_put_floppy;
1188 drive->atapi_flags |= IDE_AFLAG_MEDIA_CHANGED;
1189 /* IOMEGA Clik! drives do not support lock/unlock commands */
1190 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
1191 idefloppy_create_prevent_cmd(&pc, 1);
1192 (void) idefloppy_queue_pc_tail(drive, &pc);
1194 check_disk_change(inode->i_bdev);
1195 } else if (drive->atapi_flags & IDE_AFLAG_FORMAT_IN_PROGRESS) {
1197 goto out_put_floppy;
1203 ide_floppy_put(floppy);
1207 static int idefloppy_release(struct inode *inode, struct file *filp)
1209 struct gendisk *disk = inode->i_bdev->bd_disk;
1210 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1211 ide_drive_t *drive = floppy->drive;
1212 struct ide_atapi_pc pc;
1214 debug_log("Reached %s\n", __func__);
1216 if (floppy->openers == 1) {
1217 /* IOMEGA Clik! drives do not support lock/unlock commands */
1218 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
1219 idefloppy_create_prevent_cmd(&pc, 0);
1220 (void) idefloppy_queue_pc_tail(drive, &pc);
1223 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1228 ide_floppy_put(floppy);
1233 static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1235 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1236 ide_drive_t *drive = floppy->drive;
1238 geo->heads = drive->bios_head;
1239 geo->sectors = drive->bios_sect;
1240 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1244 static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
1245 unsigned long arg, unsigned int cmd)
1247 idefloppy_floppy_t *floppy = drive->driver_data;
1249 if (floppy->openers > 1)
1252 /* The IOMEGA Clik! Drive doesn't support this command -
1253 * no room for an eject mechanism */
1254 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE)) {
1255 int prevent = arg ? 1 : 0;
1257 if (cmd == CDROMEJECT)
1260 idefloppy_create_prevent_cmd(pc, prevent);
1261 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1264 if (cmd == CDROMEJECT) {
1265 idefloppy_create_start_stop_cmd(pc, 2);
1266 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1272 static int ide_floppy_format_unit(idefloppy_floppy_t *floppy,
1275 struct ide_atapi_pc pc;
1276 ide_drive_t *drive = floppy->drive;
1277 int blocks, length, flags, err = 0;
1279 if (floppy->openers > 1) {
1280 /* Don't format if someone is using the disk */
1281 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1285 drive->atapi_flags |= IDE_AFLAG_FORMAT_IN_PROGRESS;
1288 * Send ATAPI_FORMAT_UNIT to the drive.
1290 * Userland gives us the following structure:
1292 * struct idefloppy_format_command {
1298 * flags is a bitmask, currently, the only defined flag is:
1300 * 0x01 - verify media after format.
1302 if (get_user(blocks, arg) ||
1303 get_user(length, arg+1) ||
1304 get_user(flags, arg+2)) {
1309 (void) idefloppy_get_sfrp_bit(drive);
1310 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1312 if (idefloppy_queue_pc_tail(drive, &pc))
1317 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1322 static int idefloppy_ioctl(struct inode *inode, struct file *file,
1323 unsigned int cmd, unsigned long arg)
1325 struct block_device *bdev = inode->i_bdev;
1326 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1327 ide_drive_t *drive = floppy->drive;
1328 struct ide_atapi_pc pc;
1329 void __user *argp = (void __user *)arg;
1335 case CDROM_LOCKDOOR:
1336 return ide_floppy_lockdoor(drive, &pc, arg, cmd);
1337 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1339 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
1340 return ide_floppy_get_format_capacities(drive, argp);
1341 case IDEFLOPPY_IOCTL_FORMAT_START:
1342 if (!(file->f_mode & 2))
1345 return ide_floppy_format_unit(floppy, (int __user *)arg);
1346 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1347 return idefloppy_get_format_progress(drive, argp);
1351 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1352 * and CDROM_SEND_PACKET (legacy) ioctls
1354 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1355 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1356 bdev->bd_disk, cmd, argp);
1361 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1366 static int idefloppy_media_changed(struct gendisk *disk)
1368 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1369 ide_drive_t *drive = floppy->drive;
1372 /* do not scan partitions twice if this is a removable device */
1373 if (drive->attach) {
1377 ret = !!(drive->atapi_flags & IDE_AFLAG_MEDIA_CHANGED);
1378 drive->atapi_flags &= ~IDE_AFLAG_MEDIA_CHANGED;
1382 static int idefloppy_revalidate_disk(struct gendisk *disk)
1384 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1385 set_capacity(disk, idefloppy_capacity(floppy->drive));
1389 static struct block_device_operations idefloppy_ops = {
1390 .owner = THIS_MODULE,
1391 .open = idefloppy_open,
1392 .release = idefloppy_release,
1393 .ioctl = idefloppy_ioctl,
1394 .getgeo = idefloppy_getgeo,
1395 .media_changed = idefloppy_media_changed,
1396 .revalidate_disk = idefloppy_revalidate_disk
1399 static int ide_floppy_probe(ide_drive_t *drive)
1401 idefloppy_floppy_t *floppy;
1404 if (!strstr("ide-floppy", drive->driver_req))
1407 if (drive->media != ide_floppy)
1410 if (!idefloppy_identify_device(drive, drive->id)) {
1411 printk(KERN_ERR "ide-floppy: %s: not supported by this version"
1412 " of ide-floppy\n", drive->name);
1415 floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
1417 printk(KERN_ERR "ide-floppy: %s: Can't allocate a floppy"
1418 " structure\n", drive->name);
1422 g = alloc_disk(1 << PARTN_BITS);
1424 goto out_free_floppy;
1426 ide_init_disk(g, drive);
1428 kref_init(&floppy->kref);
1430 floppy->drive = drive;
1431 floppy->driver = &idefloppy_driver;
1434 g->private_data = &floppy->driver;
1436 drive->driver_data = floppy;
1438 idefloppy_setup(drive, floppy);
1440 g->minors = 1 << PARTN_BITS;
1441 g->driverfs_dev = &drive->gendev;
1442 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1443 g->fops = &idefloppy_ops;
1454 static void __exit idefloppy_exit(void)
1456 driver_unregister(&idefloppy_driver.gen_driver);
1459 static int __init idefloppy_init(void)
1461 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
1462 return driver_register(&idefloppy_driver.gen_driver);
1465 MODULE_ALIAS("ide:*m-floppy*");
1466 module_init(idefloppy_init);
1467 module_exit(idefloppy_exit);
1468 MODULE_LICENSE("GPL");
1469 MODULE_DESCRIPTION("ATAPI FLOPPY Driver");