]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/ide/ide-floppy.c
ide: add ide_do_start_stop() helper
[linux-2.6-omap-h63xx.git] / drivers / ide / ide-floppy.c
index e04eaa49a8bf1508f461d4578770dbdac11eb6ee..b221a456e5356c6d98cb987007617ab00ec852a9 100644 (file)
@@ -46,6 +46,8 @@
 #include <linux/io.h>
 #include <asm/unaligned.h>
 
+#include "ide-floppy.h"
+
 /* define to see debug info */
 #define IDEFLOPPY_DEBUG_LOG            0
 
 #define CAPACITY_CURRENT       0x02
 #define CAPACITY_NO_CARTRIDGE  0x03
 
-/*
- * Most of our global data which we need to save even as we leave the driver
- * due to an interrupt or a timer event is stored in a variable of type
- * idefloppy_floppy_t, defined below.
- */
-typedef struct ide_floppy_obj {
-       ide_drive_t     *drive;
-       ide_driver_t    *driver;
-       struct gendisk  *disk;
-       struct kref     kref;
-       unsigned int    openers;        /* protected by BKL for now */
-
-       /* Current packet command */
-       struct ide_atapi_pc *pc;
-       /* Last failed packet command */
-       struct ide_atapi_pc *failed_pc;
-       /* used for blk_{fs,pc}_request() requests */
-       struct ide_atapi_pc queued_pc;
-
-       struct ide_atapi_pc request_sense_pc;
-       struct request request_sense_rq;
-
-       /* Last error information */
-       u8 sense_key, asc, ascq;
-       /* delay this long before sending packet command */
-       u8 ticks;
-       int progress_indication;
-
-       /* Device information */
-       /* Current format */
-       int blocks, block_size, bs_factor;
-       /* Last format capacity descriptor */
-       u8 cap_desc[8];
-       /* Copy of the flexible disk page */
-       u8 flexible_disk_page[32];
-       /* Write protect */
-       int wp;
-       /* Supports format progress report */
-       int srfp;
-} idefloppy_floppy_t;
-
 #define IDEFLOPPY_TICKS_DELAY  HZ/20   /* default delay for ZIP 100 (50ms) */
 
-/* IOCTLs used in low-level formatting. */
-#define        IDEFLOPPY_IOCTL_FORMAT_SUPPORTED        0x4600
-#define        IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY     0x4601
-#define        IDEFLOPPY_IOCTL_FORMAT_START            0x4602
-#define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS    0x4603
-
 /* Error code returned in rq->errors to the higher part of the driver. */
 #define        IDEFLOPPY_ERROR_GENERAL         101
 
-/*
- * Pages of the SELECT SENSE / MODE SENSE packet commands.
- * See SFF-8070i spec.
- */
-#define        IDEFLOPPY_CAPABILITIES_PAGE     0x1b
-#define IDEFLOPPY_FLEXIBLE_DISK_PAGE   0x05
-
 static DEFINE_MUTEX(idefloppy_ref_mutex);
 
 #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
@@ -212,25 +160,6 @@ static void idefloppy_update_buffers(ide_drive_t *drive,
                idefloppy_end_request(drive, 1, 0);
 }
 
-/*
- * Generate a new packet command request in front of the request queue, before
- * the current request so that it will be processed immediately, on the next
- * pass through the driver.
- */
-static void idefloppy_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
-               struct request *rq)
-{
-       struct ide_floppy_obj *floppy = drive->driver_data;
-
-       blk_rq_init(NULL, rq);
-       rq->buffer = (char *) pc;
-       rq->cmd_type = REQ_TYPE_SPECIAL;
-       rq->cmd_flags |= REQ_PREEMPT;
-       rq->rq_disk = floppy->disk;
-       memcpy(rq->cmd, pc->c, 12);
-       ide_do_drive_cmd(drive, rq);
-}
-
 static void ide_floppy_callback(ide_drive_t *drive)
 {
        idefloppy_floppy_t *floppy = drive->driver_data;
@@ -268,7 +197,7 @@ static void ide_floppy_callback(ide_drive_t *drive)
        idefloppy_end_request(drive, uptodate, 0);
 }
 
-static void idefloppy_create_request_sense_cmd(struct ide_atapi_pc *pc)
+void ide_floppy_create_request_sense_cmd(struct ide_atapi_pc *pc)
 {
        ide_init_pc(pc);
        pc->c[0] = GPCMD_REQUEST_SENSE;
@@ -287,8 +216,8 @@ static void idefloppy_retry_pc(ide_drive_t *drive)
        struct ide_atapi_pc *pc = &floppy->request_sense_pc;
 
        (void)ide_read_error(drive);
-       idefloppy_create_request_sense_cmd(pc);
-       idefloppy_queue_pc_head(drive, pc, rq);
+       ide_floppy_create_request_sense_cmd(pc);
+       ide_queue_pc_head(drive, floppy->disk, pc, rq);
 }
 
 /* The usual interrupt handler called during a packet command. */
@@ -396,16 +325,7 @@ static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive,
                            IDEFLOPPY_WAIT_CMD, NULL);
 }
 
-static void idefloppy_create_prevent_cmd(struct ide_atapi_pc *pc, int prevent)
-{
-       debug_log("creating prevent removal command, prevent = %d\n", prevent);
-
-       ide_init_pc(pc);
-       pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
-       pc->c[4] = prevent;
-}
-
-static void idefloppy_create_read_capacity_cmd(struct ide_atapi_pc *pc)
+void ide_floppy_create_read_capacity_cmd(struct ide_atapi_pc *pc)
 {
        ide_init_pc(pc);
        pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
@@ -414,30 +334,8 @@ static void idefloppy_create_read_capacity_cmd(struct ide_atapi_pc *pc)
        pc->req_xfer = 255;
 }
 
-static void idefloppy_create_format_unit_cmd(struct ide_atapi_pc *pc, int b,
-               int l, int flags)
-{
-       ide_init_pc(pc);
-       pc->c[0] = GPCMD_FORMAT_UNIT;
-       pc->c[1] = 0x17;
-
-       memset(pc->buf, 0, 12);
-       pc->buf[1] = 0xA2;
-       /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
-
-       if (flags & 1)                          /* Verify bit on... */
-               pc->buf[1] ^= 0x20;             /* ... turn off DCRT bit */
-       pc->buf[3] = 8;
-
-       put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buf[4]));
-       put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buf[8]));
-       pc->buf_size = 12;
-       pc->flags |= PC_FLAG_WRITING;
-}
-
 /* A mode sense command is used to "sense" floppy parameters. */
-static void idefloppy_create_mode_sense_cmd(struct ide_atapi_pc *pc,
-                                           u8 page_code)
+void ide_floppy_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
 {
        u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
 
@@ -461,13 +359,6 @@ static void idefloppy_create_mode_sense_cmd(struct ide_atapi_pc *pc,
        pc->req_xfer = length;
 }
 
-static void idefloppy_create_start_stop_cmd(struct ide_atapi_pc *pc, int start)
-{
-       ide_init_pc(pc);
-       pc->c[0] = GPCMD_START_STOP_UNIT;
-       pc->c[4] = start;
-}
-
 static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
                                    struct ide_atapi_pc *pc, struct request *rq,
                                    unsigned long sector)
@@ -572,26 +463,6 @@ static ide_startstop_t idefloppy_do_request(ide_drive_t *drive,
        return idefloppy_issue_pc(drive, pc);
 }
 
-/*
- * Add a special packet command request to the tail of the request queue,
- * and wait for it to be serviced.
- */
-static int idefloppy_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
-{
-       struct ide_floppy_obj *floppy = drive->driver_data;
-       struct request *rq;
-       int error;
-
-       rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
-       rq->buffer = (char *) pc;
-       rq->cmd_type = REQ_TYPE_SPECIAL;
-       memcpy(rq->cmd, pc->c, 12);
-       error = blk_execute_rq(drive->queue, floppy->disk, rq, 0);
-       blk_put_request(rq);
-
-       return error;
-}
-
 /*
  * Look at the flexible disk page parameters. We ignore the CHS capacity
  * parameters and use the LBA parameters instead.
@@ -599,21 +470,28 @@ static int idefloppy_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
 static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
 {
        idefloppy_floppy_t *floppy = drive->driver_data;
+       struct gendisk *disk = floppy->disk;
        struct ide_atapi_pc pc;
        u8 *page;
        int capacity, lba_capacity;
        u16 transfer_rate, sector_size, cyls, rpm;
        u8 heads, sectors;
 
-       idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE);
+       ide_floppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE);
 
-       if (idefloppy_queue_pc_tail(drive, &pc)) {
+       if (ide_queue_pc_tail(drive, disk, &pc)) {
                printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
                                " parameters\n");
                return 1;
        }
-       floppy->wp = !!(pc.buf[3] & 0x80);
-       set_disk_ro(floppy->disk, floppy->wp);
+
+       if (pc.buf[3] & 0x80)
+               drive->atapi_flags |= IDE_AFLAG_WP;
+       else
+               drive->atapi_flags &= ~IDE_AFLAG_WP;
+
+       set_disk_ro(disk, !!(drive->atapi_flags & IDE_AFLAG_WP));
+
        page = &pc.buf[8];
 
        transfer_rate = be16_to_cpup((__be16 *)&pc.buf[8 + 2]);
@@ -647,23 +525,6 @@ static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
        return 0;
 }
 
-static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
-{
-       idefloppy_floppy_t *floppy = drive->driver_data;
-       struct ide_atapi_pc pc;
-
-       floppy->srfp = 0;
-
-       idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE);
-       pc.flags |= PC_FLAG_SUPPRESS_ERROR;
-
-       if (idefloppy_queue_pc_tail(drive, &pc))
-               return 1;
-
-       floppy->srfp = pc.buf[8 + 2] & 0x40;
-       return 0;
-}
-
 /*
  * Determine if a media is present in the floppy drive, and if so, its LBA
  * capacity.
@@ -671,6 +532,7 @@ static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
 static int ide_floppy_get_capacity(ide_drive_t *drive)
 {
        idefloppy_floppy_t *floppy = drive->driver_data;
+       struct gendisk *disk = floppy->disk;
        struct ide_atapi_pc pc;
        u8 *cap_desc;
        u8 header_len, desc_cnt;
@@ -682,8 +544,8 @@ static int ide_floppy_get_capacity(ide_drive_t *drive)
        floppy->bs_factor = 1;
        set_capacity(floppy->disk, 0);
 
-       idefloppy_create_read_capacity_cmd(&pc);
-       if (idefloppy_queue_pc_tail(drive, &pc)) {
+       ide_floppy_create_read_capacity_cmd(&pc);
+       if (ide_queue_pc_tail(drive, disk, &pc)) {
                printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
                return 1;
        }
@@ -758,130 +620,9 @@ static int ide_floppy_get_capacity(ide_drive_t *drive)
        if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
                (void) ide_floppy_get_flexible_disk_page(drive);
 
-       set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
-       return rc;
-}
-
-/*
- * Obtain the list of formattable capacities.
- * Very similar to ide_floppy_get_capacity, except that we push the capacity
- * descriptors to userland, instead of our own structures.
- *
- * Userland gives us the following structure:
- *
- * struct idefloppy_format_capacities {
- *     int nformats;
- *     struct {
- *             int nblocks;
- *             int blocksize;
- *     } formats[];
- * };
- *
- * userland initializes nformats to the number of allocated formats[] records.
- * On exit we set nformats to the number of records we've actually initialized.
- */
-
-static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
-{
-       struct ide_atapi_pc pc;
-       u8 header_len, desc_cnt;
-       int i, blocks, length, u_array_size, u_index;
-       int __user *argp;
-
-       if (get_user(u_array_size, arg))
-               return -EFAULT;
-
-       if (u_array_size <= 0)
-               return -EINVAL;
-
-       idefloppy_create_read_capacity_cmd(&pc);
-       if (idefloppy_queue_pc_tail(drive, &pc)) {
-               printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
-               return -EIO;
-       }
-
-       header_len = pc.buf[3];
-       desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
-
-       u_index = 0;
-       argp = arg + 1;
-
-       /*
-        * We always skip the first capacity descriptor.  That's the current
-        * capacity.  We are interested in the remaining descriptors, the
-        * formattable capacities.
-        */
-       for (i = 1; i < desc_cnt; i++) {
-               unsigned int desc_start = 4 + i*8;
-
-               if (u_index >= u_array_size)
-                       break;  /* User-supplied buffer too small */
-
-               blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
-               length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
-
-               if (put_user(blocks, argp))
-                       return -EFAULT;
-
-               ++argp;
-
-               if (put_user(length, argp))
-                       return -EFAULT;
-
-               ++argp;
-
-               ++u_index;
-       }
-
-       if (put_user(u_index, arg))
-               return -EFAULT;
-
-       return 0;
-}
-
-/*
- * Get ATAPI_FORMAT_UNIT progress indication.
- *
- * Userland gives a pointer to an int.  The int is set to a progress
- * indicator 0-65536, with 65536=100%.
- *
- * If the drive does not support format progress indication, we just check
- * the dsc bit, and return either 0 or 65536.
- */
-
-static int ide_floppy_get_format_progress(ide_drive_t *drive, int __user *arg)
-{
-       idefloppy_floppy_t *floppy = drive->driver_data;
-       struct ide_atapi_pc pc;
-       int progress_indication = 0x10000;
+       set_capacity(disk, floppy->blocks * floppy->bs_factor);
 
-       if (floppy->srfp) {
-               idefloppy_create_request_sense_cmd(&pc);
-               if (idefloppy_queue_pc_tail(drive, &pc))
-                       return -EIO;
-
-               if (floppy->sense_key == 2 &&
-                   floppy->asc == 4 &&
-                   floppy->ascq == 4)
-                       progress_indication = floppy->progress_indication;
-
-               /* Else assume format_unit has finished, and we're at 0x10000 */
-       } else {
-               ide_hwif_t *hwif = drive->hwif;
-               unsigned long flags;
-               u8 stat;
-
-               local_irq_save(flags);
-               stat = hwif->tp_ops->read_status(hwif);
-               local_irq_restore(flags);
-
-               progress_indication = ((stat & ATA_DSC) == 0) ? 0 : 0x10000;
-       }
-
-       if (put_user(progress_indication, arg))
-               return -EFAULT;
-
-       return 0;
+       return rc;
 }
 
 static sector_t idefloppy_capacity(ide_drive_t *drive)
@@ -955,6 +696,8 @@ static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
        if (strncmp((char *)&id[ATA_ID_PROD], "IOMEGA Clik!", 11) == 0) {
                blk_queue_max_sectors(drive->queue, 64);
                drive->atapi_flags |= IDE_AFLAG_CLIK_DRIVE;
+               /* IOMEGA Clik! drives do not support lock/unlock commands */
+               drive->atapi_flags |= IDE_AFLAG_NO_DOORLOCK;
        }
 
        (void) ide_floppy_get_capacity(drive);
@@ -1025,17 +768,6 @@ static ide_driver_t idefloppy_driver = {
 #endif
 };
 
-static void ide_floppy_set_media_lock(ide_drive_t *drive, int on)
-{
-       struct ide_atapi_pc pc;
-
-       /* IOMEGA Clik! drives do not support lock/unlock commands */
-       if ((drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE) == 0) {
-               idefloppy_create_prevent_cmd(&pc, on);
-               (void)idefloppy_queue_pc_tail(drive, &pc);
-       }
-}
-
 static int idefloppy_open(struct inode *inode, struct file *filp)
 {
        struct gendisk *disk = inode->i_bdev->bd_disk;
@@ -1061,10 +793,8 @@ static int idefloppy_open(struct inode *inode, struct file *filp)
                ide_init_pc(&pc);
                pc.c[0] = GPCMD_TEST_UNIT_READY;
 
-               if (idefloppy_queue_pc_tail(drive, &pc)) {
-                       idefloppy_create_start_stop_cmd(&pc, 1);
-                       (void) idefloppy_queue_pc_tail(drive, &pc);
-               }
+               if (ide_queue_pc_tail(drive, disk, &pc))
+                       ide_do_start_stop(drive, disk, 1);
 
                if (ide_floppy_get_capacity(drive)
                   && (filp->f_flags & O_NDELAY) == 0
@@ -1078,13 +808,13 @@ static int idefloppy_open(struct inode *inode, struct file *filp)
                        goto out_put_floppy;
                }
 
-               if (floppy->wp && (filp->f_mode & 2)) {
+               if ((drive->atapi_flags & IDE_AFLAG_WP) && (filp->f_mode & 2)) {
                        ret = -EROFS;
                        goto out_put_floppy;
                }
 
                drive->atapi_flags |= IDE_AFLAG_MEDIA_CHANGED;
-               ide_floppy_set_media_lock(drive, 1);
+               ide_set_media_lock(drive, disk, 1);
                check_disk_change(inode->i_bdev);
        } else if (drive->atapi_flags & IDE_AFLAG_FORMAT_IN_PROGRESS) {
                ret = -EBUSY;
@@ -1107,7 +837,7 @@ static int idefloppy_release(struct inode *inode, struct file *filp)
        debug_log("Reached %s\n", __func__);
 
        if (floppy->openers == 1) {
-               ide_floppy_set_media_lock(drive, 0);
+               ide_set_media_lock(drive, disk, 0);
                drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
        }
 
@@ -1133,88 +863,20 @@ static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
                               unsigned long arg, unsigned int cmd)
 {
        idefloppy_floppy_t *floppy = drive->driver_data;
+       struct gendisk *disk = floppy->disk;
        int prevent = (arg && cmd != CDROMEJECT) ? 1 : 0;
 
        if (floppy->openers > 1)
                return -EBUSY;
 
-       ide_floppy_set_media_lock(drive, prevent);
+       ide_set_media_lock(drive, disk, prevent);
 
-       if (cmd == CDROMEJECT) {
-               idefloppy_create_start_stop_cmd(pc, 2);
-               (void) idefloppy_queue_pc_tail(floppy->drive, pc);
-       }
+       if (cmd == CDROMEJECT)
+               ide_do_start_stop(drive, disk, 2);
 
        return 0;
 }
 
-static int ide_floppy_format_unit(ide_drive_t *drive, int __user *arg)
-{
-       idefloppy_floppy_t *floppy = drive->driver_data;
-       struct ide_atapi_pc pc;
-       int blocks, length, flags, err = 0;
-
-       if (floppy->openers > 1) {
-               /* Don't format if someone is using the disk */
-               drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
-               return -EBUSY;
-       }
-
-       drive->atapi_flags |= IDE_AFLAG_FORMAT_IN_PROGRESS;
-
-       /*
-        * Send ATAPI_FORMAT_UNIT to the drive.
-        *
-        * Userland gives us the following structure:
-        *
-        * struct idefloppy_format_command {
-        *        int nblocks;
-        *        int blocksize;
-        *        int flags;
-        *        } ;
-        *
-        * flags is a bitmask, currently, the only defined flag is:
-        *
-        *        0x01 - verify media after format.
-        */
-       if (get_user(blocks, arg) ||
-                       get_user(length, arg+1) ||
-                       get_user(flags, arg+2)) {
-               err = -EFAULT;
-               goto out;
-       }
-
-       (void) idefloppy_get_sfrp_bit(drive);
-       idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
-
-       if (idefloppy_queue_pc_tail(drive, &pc))
-               err = -EIO;
-
-out:
-       if (err)
-               drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
-       return err;
-}
-
-static int ide_floppy_format_ioctl(ide_drive_t *drive, struct file *file,
-                                  unsigned int cmd, void __user *argp)
-{
-       switch (cmd) {
-       case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
-               return 0;
-       case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
-               return ide_floppy_get_format_capacities(drive, argp);
-       case IDEFLOPPY_IOCTL_FORMAT_START:
-               if (!(file->f_mode & 2))
-                       return -EPERM;
-               return ide_floppy_format_unit(drive, (int __user *)argp);
-       case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
-               return ide_floppy_get_format_progress(drive, argp);
-       default:
-               return -ENOTTY;
-       }
-}
-
 static int idefloppy_ioctl(struct inode *inode, struct file *file,
                        unsigned int cmd, unsigned long arg)
 {
@@ -1346,6 +1008,7 @@ static int __init idefloppy_init(void)
 }
 
 MODULE_ALIAS("ide:*m-floppy*");
+MODULE_ALIAS("ide-floppy");
 module_init(idefloppy_init);
 module_exit(idefloppy_exit);
 MODULE_LICENSE("GPL");