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"
19 #define PFX DRV_NAME ": "
21 #define IDEFLOPPY_VERSION "1.00"
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/string.h>
26 #include <linux/kernel.h>
27 #include <linux/delay.h>
28 #include <linux/timer.h>
30 #include <linux/interrupt.h>
31 #include <linux/major.h>
32 #include <linux/errno.h>
33 #include <linux/genhd.h>
34 #include <linux/slab.h>
35 #include <linux/cdrom.h>
36 #include <linux/ide.h>
37 #include <linux/hdreg.h>
38 #include <linux/bitops.h>
39 #include <linux/mutex.h>
40 #include <linux/scatterlist.h>
42 #include <scsi/scsi_ioctl.h>
44 #include <asm/byteorder.h>
45 #include <linux/irq.h>
46 #include <linux/uaccess.h>
48 #include <asm/unaligned.h>
50 #include "ide-floppy.h"
52 /* define to see debug info */
53 #define IDEFLOPPY_DEBUG_LOG 0
55 #if IDEFLOPPY_DEBUG_LOG
56 #define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, args)
58 #define ide_debug_log(lvl, fmt, args...) do {} while (0)
62 * After each failed packet command we issue a request sense command and retry
63 * the packet command IDEFLOPPY_MAX_PC_RETRIES times.
65 #define IDEFLOPPY_MAX_PC_RETRIES 3
67 /* format capacities descriptor codes */
68 #define CAPACITY_INVALID 0x00
69 #define CAPACITY_UNFORMATTED 0x01
70 #define CAPACITY_CURRENT 0x02
71 #define CAPACITY_NO_CARTRIDGE 0x03
74 * The following delay solves a problem with ATAPI Zip 100 drive where BSY bit
75 * was apparently being deasserted before the unit was ready to receive data.
77 #define IDEFLOPPY_PC_DELAY (HZ/20) /* default delay for ZIP 100 (50ms) */
79 /* Error code returned in rq->errors to the higher part of the driver. */
80 #define IDEFLOPPY_ERROR_GENERAL 101
82 static DEFINE_MUTEX(idefloppy_ref_mutex);
84 static void idefloppy_cleanup_obj(struct kref *);
86 static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
88 struct ide_floppy_obj *floppy = NULL;
90 mutex_lock(&idefloppy_ref_mutex);
91 floppy = ide_drv_g(disk, ide_floppy_obj);
93 if (ide_device_get(floppy->drive))
96 kref_get(&floppy->kref);
98 mutex_unlock(&idefloppy_ref_mutex);
102 static void ide_floppy_put(struct ide_floppy_obj *floppy)
104 ide_drive_t *drive = floppy->drive;
106 mutex_lock(&idefloppy_ref_mutex);
107 kref_put(&floppy->kref, idefloppy_cleanup_obj);
108 ide_device_put(drive);
109 mutex_unlock(&idefloppy_ref_mutex);
113 * Used to finish servicing a request. For read/write requests, we will call
114 * ide_end_request to pass to the next buffer.
116 static int idefloppy_end_request(ide_drive_t *drive, int uptodate, int nsecs)
118 idefloppy_floppy_t *floppy = drive->driver_data;
119 struct request *rq = HWGROUP(drive)->rq;
122 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
126 error = IDEFLOPPY_ERROR_GENERAL;
138 floppy->failed_pc = NULL;
139 /* Why does this happen? */
142 if (!blk_special_request(rq)) {
143 /* our real local end request function */
144 ide_end_request(drive, uptodate, nsecs);
148 /* fixme: need to move this local also */
149 ide_end_drive_cmd(drive, 0, 0);
153 static void idefloppy_update_buffers(ide_drive_t *drive,
154 struct ide_atapi_pc *pc)
156 struct request *rq = pc->rq;
157 struct bio *bio = rq->bio;
159 while ((bio = rq->bio) != NULL)
160 idefloppy_end_request(drive, 1, 0);
163 static void ide_floppy_callback(ide_drive_t *drive, int dsc)
165 idefloppy_floppy_t *floppy = drive->driver_data;
166 struct ide_atapi_pc *pc = drive->pc;
167 int uptodate = pc->error ? 0 : 1;
169 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
171 if (floppy->failed_pc == pc)
172 floppy->failed_pc = NULL;
174 if (pc->c[0] == GPCMD_READ_10 || pc->c[0] == GPCMD_WRITE_10 ||
175 (pc->rq && blk_pc_request(pc->rq)))
176 uptodate = 1; /* FIXME */
177 else if (pc->c[0] == GPCMD_REQUEST_SENSE) {
181 floppy->sense_key = buf[2] & 0x0F;
182 floppy->asc = buf[12];
183 floppy->ascq = buf[13];
184 floppy->progress_indication = buf[15] & 0x80 ?
185 (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
187 if (floppy->failed_pc)
188 ide_debug_log(IDE_DBG_PC, "pc = %x, ",
189 floppy->failed_pc->c[0]);
191 ide_debug_log(IDE_DBG_SENSE, "sense key = %x, asc = %x,"
192 "ascq = %x\n", floppy->sense_key,
193 floppy->asc, floppy->ascq);
195 printk(KERN_ERR PFX "Error in REQUEST SENSE itself - "
196 "Aborting request!\n");
199 idefloppy_end_request(drive, uptodate, 0);
202 static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
203 struct ide_atapi_pc *pc)
205 /* supress error messages resulting from Medium not present */
206 if (floppy->sense_key == 0x02 &&
207 floppy->asc == 0x3a &&
208 floppy->ascq == 0x00)
211 printk(KERN_ERR PFX "%s: I/O error, pc = %2x, key = %2x, "
212 "asc = %2x, ascq = %2x\n",
213 floppy->drive->name, pc->c[0], floppy->sense_key,
214 floppy->asc, floppy->ascq);
218 static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive,
219 struct ide_atapi_pc *pc)
221 idefloppy_floppy_t *floppy = drive->driver_data;
223 if (floppy->failed_pc == NULL &&
224 pc->c[0] != GPCMD_REQUEST_SENSE)
225 floppy->failed_pc = pc;
227 /* Set the current packet command */
230 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
231 if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
232 ide_floppy_report_error(floppy, pc);
234 pc->error = IDEFLOPPY_ERROR_GENERAL;
236 floppy->failed_pc = NULL;
237 drive->pc_callback(drive, 0);
241 ide_debug_log(IDE_DBG_FUNC, "%s: Retry #%d\n", __func__, pc->retries);
245 return ide_issue_pc(drive, WAIT_FLOPPY_CMD, NULL);
248 void ide_floppy_create_read_capacity_cmd(struct ide_atapi_pc *pc)
251 pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
257 /* A mode sense command is used to "sense" floppy parameters. */
258 void ide_floppy_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
260 u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
263 pc->c[0] = GPCMD_MODE_SENSE_10;
265 pc->c[2] = page_code;
268 case IDEFLOPPY_CAPABILITIES_PAGE:
271 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
275 printk(KERN_ERR PFX "unsupported page code in %s\n", __func__);
277 put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
278 pc->req_xfer = length;
281 static void idefloppy_create_rw_cmd(ide_drive_t *drive,
282 struct ide_atapi_pc *pc, struct request *rq,
283 unsigned long sector)
285 idefloppy_floppy_t *floppy = drive->driver_data;
286 int block = sector / floppy->bs_factor;
287 int blocks = rq->nr_sectors / floppy->bs_factor;
288 int cmd = rq_data_dir(rq);
290 ide_debug_log(IDE_DBG_FUNC, "%s: block: %d, blocks: %d\n", __func__,
294 pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
295 put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
296 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
298 memcpy(rq->cmd, pc->c, 12);
302 if (rq->cmd_flags & REQ_RW)
303 pc->flags |= PC_FLAG_WRITING;
305 pc->req_xfer = pc->buf_size = blocks * floppy->block_size;
306 pc->flags |= PC_FLAG_DMA_OK;
309 static void idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy,
310 struct ide_atapi_pc *pc, struct request *rq)
313 memcpy(pc->c, rq->cmd, sizeof(pc->c));
316 if (rq->data_len && rq_data_dir(rq) == WRITE)
317 pc->flags |= PC_FLAG_WRITING;
320 pc->flags |= PC_FLAG_DMA_OK;
322 * possibly problematic, doesn't look like ide-floppy correctly
323 * handled scattered requests if dma fails...
325 pc->req_xfer = pc->buf_size = rq->data_len;
328 static ide_startstop_t idefloppy_do_request(ide_drive_t *drive,
329 struct request *rq, sector_t block_s)
331 idefloppy_floppy_t *floppy = drive->driver_data;
332 ide_hwif_t *hwif = drive->hwif;
333 struct ide_atapi_pc *pc;
334 unsigned long block = (unsigned long)block_s;
336 ide_debug_log(IDE_DBG_FUNC, "%s: dev: %s, cmd: 0x%x, cmd_type: %x, "
338 __func__, rq->rq_disk ? rq->rq_disk->disk_name : "?",
339 rq->cmd[0], rq->cmd_type, rq->errors);
341 ide_debug_log(IDE_DBG_FUNC, "%s: sector: %ld, nr_sectors: %ld, "
342 "current_nr_sectors: %d\n",
343 __func__, (long)rq->sector, rq->nr_sectors,
344 rq->current_nr_sectors);
346 if (rq->errors >= ERROR_MAX) {
347 if (floppy->failed_pc)
348 ide_floppy_report_error(floppy, floppy->failed_pc);
350 printk(KERN_ERR PFX "%s: I/O error\n", drive->name);
352 idefloppy_end_request(drive, 0, 0);
355 if (blk_fs_request(rq)) {
356 if (((long)rq->sector % floppy->bs_factor) ||
357 (rq->nr_sectors % floppy->bs_factor)) {
358 printk(KERN_ERR PFX "%s: unsupported r/w rq size\n",
360 idefloppy_end_request(drive, 0, 0);
363 pc = &floppy->queued_pc;
364 idefloppy_create_rw_cmd(drive, pc, rq, block);
365 } else if (blk_special_request(rq)) {
366 pc = (struct ide_atapi_pc *) rq->buffer;
367 } else if (blk_pc_request(rq)) {
368 pc = &floppy->queued_pc;
369 idefloppy_blockpc_cmd(floppy, pc, rq);
371 blk_dump_rq_flags(rq, PFX "unsupported command in queue");
372 idefloppy_end_request(drive, 0, 0);
376 ide_init_sg_cmd(drive, rq);
377 ide_map_sg(drive, rq);
379 pc->sg = hwif->sg_table;
380 pc->sg_cnt = hwif->sg_nents;
384 return idefloppy_issue_pc(drive, pc);
388 * Look at the flexible disk page parameters. We ignore the CHS capacity
389 * parameters and use the LBA parameters instead.
391 static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
393 idefloppy_floppy_t *floppy = drive->driver_data;
394 struct gendisk *disk = floppy->disk;
395 struct ide_atapi_pc pc;
397 int capacity, lba_capacity;
398 u16 transfer_rate, sector_size, cyls, rpm;
401 ide_floppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE);
403 if (ide_queue_pc_tail(drive, disk, &pc)) {
404 printk(KERN_ERR PFX "Can't get flexible disk page params\n");
408 if (pc.buf[3] & 0x80)
409 drive->atapi_flags |= IDE_AFLAG_WP;
411 drive->atapi_flags &= ~IDE_AFLAG_WP;
413 set_disk_ro(disk, !!(drive->atapi_flags & IDE_AFLAG_WP));
417 transfer_rate = be16_to_cpup((__be16 *)&pc.buf[8 + 2]);
418 sector_size = be16_to_cpup((__be16 *)&pc.buf[8 + 6]);
419 cyls = be16_to_cpup((__be16 *)&pc.buf[8 + 8]);
420 rpm = be16_to_cpup((__be16 *)&pc.buf[8 + 28]);
421 heads = pc.buf[8 + 4];
422 sectors = pc.buf[8 + 5];
424 capacity = cyls * heads * sectors * sector_size;
426 if (memcmp(page, &floppy->flexible_disk_page, 32))
427 printk(KERN_INFO PFX "%s: %dkB, %d/%d/%d CHS, %d kBps, "
428 "%d sector size, %d rpm\n",
429 drive->name, capacity / 1024, cyls, heads,
430 sectors, transfer_rate / 8, sector_size, rpm);
432 memcpy(&floppy->flexible_disk_page, page, 32);
433 drive->bios_cyl = cyls;
434 drive->bios_head = heads;
435 drive->bios_sect = sectors;
436 lba_capacity = floppy->blocks * floppy->block_size;
438 if (capacity < lba_capacity) {
439 printk(KERN_NOTICE PFX "%s: The disk reports a capacity of %d "
440 "bytes, but the drive only handles %d\n",
441 drive->name, lba_capacity, capacity);
442 floppy->blocks = floppy->block_size ?
443 capacity / floppy->block_size : 0;
449 * Determine if a media is present in the floppy drive, and if so, its LBA
452 static int ide_floppy_get_capacity(ide_drive_t *drive)
454 idefloppy_floppy_t *floppy = drive->driver_data;
455 struct gendisk *disk = floppy->disk;
456 struct ide_atapi_pc pc;
458 u8 header_len, desc_cnt;
459 int i, rc = 1, blocks, length;
462 drive->bios_head = drive->bios_sect = 0;
464 floppy->bs_factor = 1;
465 set_capacity(floppy->disk, 0);
467 ide_floppy_create_read_capacity_cmd(&pc);
468 if (ide_queue_pc_tail(drive, disk, &pc)) {
469 printk(KERN_ERR PFX "Can't get floppy parameters\n");
472 header_len = pc.buf[3];
473 cap_desc = &pc.buf[4];
474 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
476 for (i = 0; i < desc_cnt; i++) {
477 unsigned int desc_start = 4 + i*8;
479 blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
480 length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
482 ide_debug_log(IDE_DBG_PROBE, "Descriptor %d: %dkB, %d blocks, "
484 i, blocks * length / 1024, blocks, length);
489 * the code below is valid only for the 1st descriptor, ie i=0
492 switch (pc.buf[desc_start + 4] & 0x03) {
493 /* Clik! drive returns this instead of CAPACITY_CURRENT */
494 case CAPACITY_UNFORMATTED:
495 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
497 * If it is not a clik drive, break out
498 * (maintains previous driver behaviour)
501 case CAPACITY_CURRENT:
502 /* Normal Zip/LS-120 disks */
503 if (memcmp(cap_desc, &floppy->cap_desc, 8))
504 printk(KERN_INFO PFX "%s: %dkB, %d blocks, %d "
506 drive->name, blocks * length / 1024,
508 memcpy(&floppy->cap_desc, cap_desc, 8);
510 if (!length || length % 512) {
511 printk(KERN_NOTICE PFX "%s: %d bytes block size"
512 " not supported\n", drive->name, length);
514 floppy->blocks = blocks;
515 floppy->block_size = length;
516 floppy->bs_factor = length / 512;
517 if (floppy->bs_factor != 1)
518 printk(KERN_NOTICE PFX "%s: Warning: "
519 "non 512 bytes block size not "
525 case CAPACITY_NO_CARTRIDGE:
527 * This is a KERN_ERR so it appears on screen
528 * for the user to see
530 printk(KERN_ERR PFX "%s: No disk in drive\n",
533 case CAPACITY_INVALID:
534 printk(KERN_ERR PFX "%s: Invalid capacity for disk "
535 "in drive\n", drive->name);
538 ide_debug_log(IDE_DBG_PROBE, "Descriptor 0 Code: %d\n",
539 pc.buf[desc_start + 4] & 0x03);
542 /* Clik! disk does not support get_flexible_disk_page */
543 if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
544 (void) ide_floppy_get_flexible_disk_page(drive);
546 set_capacity(disk, floppy->blocks * floppy->bs_factor);
551 static sector_t idefloppy_capacity(ide_drive_t *drive)
553 idefloppy_floppy_t *floppy = drive->driver_data;
554 unsigned long capacity = floppy->blocks * floppy->bs_factor;
559 #ifdef CONFIG_IDE_PROC_FS
560 ide_devset_rw_field(bios_cyl, bios_cyl);
561 ide_devset_rw_field(bios_head, bios_head);
562 ide_devset_rw_field(bios_sect, bios_sect);
563 ide_devset_rw_field(ticks, pc_delay);
565 static const struct ide_proc_devset idefloppy_settings[] = {
566 IDE_PROC_DEVSET(bios_cyl, 0, 1023),
567 IDE_PROC_DEVSET(bios_head, 0, 255),
568 IDE_PROC_DEVSET(bios_sect, 0, 63),
569 IDE_PROC_DEVSET(ticks, 0, 255),
574 static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
579 *((u16 *)&gcw) = id[ATA_ID_CONFIG];
581 drive->pc_callback = ide_floppy_callback;
582 drive->pc_update_buffers = idefloppy_update_buffers;
583 drive->pc_io_buffers = ide_io_buffers;
585 if (((gcw[0] & 0x60) >> 5) == 1)
586 drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
588 * We used to check revisions here. At this point however I'm giving up.
589 * Just assume they are all broken, its easier.
591 * The actual reason for the workarounds was likely a driver bug after
592 * all rather than a firmware bug, and the workaround below used to hide
593 * it. It should be fixed as of version 1.9, but to be on the safe side
594 * we'll leave the limitation below for the 2.2.x tree.
596 if (!strncmp((char *)&id[ATA_ID_PROD], "IOMEGA ZIP 100 ATAPI", 20)) {
597 drive->atapi_flags |= IDE_AFLAG_ZIP_DRIVE;
598 /* This value will be visible in the /proc/ide/hdx/settings */
599 drive->pc_delay = IDEFLOPPY_PC_DELAY;
600 blk_queue_max_sectors(drive->queue, 64);
604 * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
605 * nasty clicking noises without it, so please don't remove this.
607 if (strncmp((char *)&id[ATA_ID_PROD], "IOMEGA Clik!", 11) == 0) {
608 blk_queue_max_sectors(drive->queue, 64);
609 drive->atapi_flags |= IDE_AFLAG_CLIK_DRIVE;
610 /* IOMEGA Clik! drives do not support lock/unlock commands */
611 drive->atapi_flags |= IDE_AFLAG_NO_DOORLOCK;
614 (void) ide_floppy_get_capacity(drive);
616 ide_proc_register_driver(drive, floppy->driver);
619 static void ide_floppy_remove(ide_drive_t *drive)
621 idefloppy_floppy_t *floppy = drive->driver_data;
622 struct gendisk *g = floppy->disk;
624 ide_proc_unregister_driver(drive, floppy->driver);
628 ide_floppy_put(floppy);
631 static void idefloppy_cleanup_obj(struct kref *kref)
633 struct ide_floppy_obj *floppy = to_ide_drv(kref, ide_floppy_obj);
634 ide_drive_t *drive = floppy->drive;
635 struct gendisk *g = floppy->disk;
637 drive->driver_data = NULL;
638 g->private_data = NULL;
643 #ifdef CONFIG_IDE_PROC_FS
644 static int proc_idefloppy_read_capacity(char *page, char **start, off_t off,
645 int count, int *eof, void *data)
647 ide_drive_t*drive = (ide_drive_t *)data;
650 len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive));
651 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
654 static ide_proc_entry_t idefloppy_proc[] = {
655 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
656 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
657 { NULL, 0, NULL, NULL }
659 #endif /* CONFIG_IDE_PROC_FS */
661 static int ide_floppy_probe(ide_drive_t *);
663 static ide_driver_t idefloppy_driver = {
665 .owner = THIS_MODULE,
666 .name = "ide-floppy",
667 .bus = &ide_bus_type,
669 .probe = ide_floppy_probe,
670 .remove = ide_floppy_remove,
671 .version = IDEFLOPPY_VERSION,
673 .do_request = idefloppy_do_request,
674 .end_request = idefloppy_end_request,
675 .error = __ide_error,
676 #ifdef CONFIG_IDE_PROC_FS
677 .proc = idefloppy_proc,
678 .settings = idefloppy_settings,
682 static int idefloppy_open(struct inode *inode, struct file *filp)
684 struct gendisk *disk = inode->i_bdev->bd_disk;
685 struct ide_floppy_obj *floppy;
689 floppy = ide_floppy_get(disk);
693 drive = floppy->drive;
695 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
699 if (floppy->openers == 1) {
700 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
703 if (ide_do_test_unit_ready(drive, disk))
704 ide_do_start_stop(drive, disk, 1);
706 if (ide_floppy_get_capacity(drive)
707 && (filp->f_flags & O_NDELAY) == 0
709 * Allow O_NDELAY to open a drive without a disk, or with an
710 * unreadable disk, so that we can get the format capacity
711 * of the drive or begin the format - Sam
718 if ((drive->atapi_flags & IDE_AFLAG_WP) && (filp->f_mode & 2)) {
723 drive->atapi_flags |= IDE_AFLAG_MEDIA_CHANGED;
724 ide_set_media_lock(drive, disk, 1);
725 check_disk_change(inode->i_bdev);
726 } else if (drive->atapi_flags & IDE_AFLAG_FORMAT_IN_PROGRESS) {
734 ide_floppy_put(floppy);
738 static int idefloppy_release(struct inode *inode, struct file *filp)
740 struct gendisk *disk = inode->i_bdev->bd_disk;
741 struct ide_floppy_obj *floppy = ide_drv_g(disk, ide_floppy_obj);
742 ide_drive_t *drive = floppy->drive;
744 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
746 if (floppy->openers == 1) {
747 ide_set_media_lock(drive, disk, 0);
748 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
753 ide_floppy_put(floppy);
758 static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
760 struct ide_floppy_obj *floppy = ide_drv_g(bdev->bd_disk,
762 ide_drive_t *drive = floppy->drive;
764 geo->heads = drive->bios_head;
765 geo->sectors = drive->bios_sect;
766 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
770 static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
771 unsigned long arg, unsigned int cmd)
773 idefloppy_floppy_t *floppy = drive->driver_data;
774 struct gendisk *disk = floppy->disk;
775 int prevent = (arg && cmd != CDROMEJECT) ? 1 : 0;
777 if (floppy->openers > 1)
780 ide_set_media_lock(drive, disk, prevent);
782 if (cmd == CDROMEJECT)
783 ide_do_start_stop(drive, disk, 2);
788 static int idefloppy_ioctl(struct inode *inode, struct file *file,
789 unsigned int cmd, unsigned long arg)
791 struct block_device *bdev = inode->i_bdev;
792 struct ide_floppy_obj *floppy = ide_drv_g(bdev->bd_disk,
794 ide_drive_t *drive = floppy->drive;
795 struct ide_atapi_pc pc;
796 void __user *argp = (void __user *)arg;
799 if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR)
800 return ide_floppy_lockdoor(drive, &pc, arg, cmd);
802 err = ide_floppy_format_ioctl(drive, file, cmd, argp);
807 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
808 * and CDROM_SEND_PACKET (legacy) ioctls
810 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
811 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
812 bdev->bd_disk, cmd, argp);
815 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
820 static int idefloppy_media_changed(struct gendisk *disk)
822 struct ide_floppy_obj *floppy = ide_drv_g(disk, ide_floppy_obj);
823 ide_drive_t *drive = floppy->drive;
826 /* do not scan partitions twice if this is a removable device */
831 ret = !!(drive->atapi_flags & IDE_AFLAG_MEDIA_CHANGED);
832 drive->atapi_flags &= ~IDE_AFLAG_MEDIA_CHANGED;
836 static int idefloppy_revalidate_disk(struct gendisk *disk)
838 struct ide_floppy_obj *floppy = ide_drv_g(disk, ide_floppy_obj);
839 set_capacity(disk, idefloppy_capacity(floppy->drive));
843 static struct block_device_operations idefloppy_ops = {
844 .owner = THIS_MODULE,
845 .open = idefloppy_open,
846 .release = idefloppy_release,
847 .ioctl = idefloppy_ioctl,
848 .getgeo = idefloppy_getgeo,
849 .media_changed = idefloppy_media_changed,
850 .revalidate_disk = idefloppy_revalidate_disk
853 static int ide_floppy_probe(ide_drive_t *drive)
855 idefloppy_floppy_t *floppy;
858 if (!strstr("ide-floppy", drive->driver_req))
861 if (drive->media != ide_floppy)
864 if (!ide_check_atapi_device(drive, DRV_NAME)) {
865 printk(KERN_ERR PFX "%s: not supported by this version of "
866 DRV_NAME "\n", drive->name);
869 floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
871 printk(KERN_ERR PFX "%s: Can't allocate a floppy structure\n",
876 g = alloc_disk(1 << PARTN_BITS);
878 goto out_free_floppy;
880 ide_init_disk(g, drive);
882 kref_init(&floppy->kref);
884 floppy->drive = drive;
885 floppy->driver = &idefloppy_driver;
888 g->private_data = &floppy->driver;
890 drive->driver_data = floppy;
892 idefloppy_setup(drive, floppy);
894 g->minors = 1 << PARTN_BITS;
895 g->driverfs_dev = &drive->gendev;
896 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
897 g->fops = &idefloppy_ops;
908 static void __exit idefloppy_exit(void)
910 driver_unregister(&idefloppy_driver.gen_driver);
913 static int __init idefloppy_init(void)
915 printk(KERN_INFO DRV_NAME " driver " IDEFLOPPY_VERSION "\n");
916 return driver_register(&idefloppy_driver.gen_driver);
919 MODULE_ALIAS("ide:*m-floppy*");
920 MODULE_ALIAS("ide-floppy");
921 module_init(idefloppy_init);
922 module_exit(idefloppy_exit);
923 MODULE_LICENSE("GPL");
924 MODULE_DESCRIPTION("ATAPI FLOPPY Driver");