linux/drivers/ide/ide-floppy_ioctl.c
<<
>>
Prefs
   1/*
   2 * ide-floppy IOCTLs handling.
   3 */
   4
   5#include <linux/kernel.h>
   6#include <linux/ide.h>
   7#include <linux/cdrom.h>
   8#include <linux/mutex.h>
   9
  10#include <asm/unaligned.h>
  11
  12#include <scsi/scsi_ioctl.h>
  13
  14#include "ide-floppy.h"
  15
  16/*
  17 * Obtain the list of formattable capacities.
  18 * Very similar to ide_floppy_get_capacity, except that we push the capacity
  19 * descriptors to userland, instead of our own structures.
  20 *
  21 * Userland gives us the following structure:
  22 *
  23 * struct idefloppy_format_capacities {
  24 *      int nformats;
  25 *      struct {
  26 *              int nblocks;
  27 *              int blocksize;
  28 *      } formats[];
  29 * };
  30 *
  31 * userland initializes nformats to the number of allocated formats[] records.
  32 * On exit we set nformats to the number of records we've actually initialized.
  33 */
  34
  35static DEFINE_MUTEX(ide_floppy_ioctl_mutex);
  36static int ide_floppy_get_format_capacities(ide_drive_t *drive,
  37                                            struct ide_atapi_pc *pc,
  38                                            int __user *arg)
  39{
  40        struct ide_disk_obj *floppy = drive->driver_data;
  41        int i, blocks, length, u_array_size, u_index;
  42        int __user *argp;
  43        u8 pc_buf[256], header_len, desc_cnt;
  44
  45        if (get_user(u_array_size, arg))
  46                return -EFAULT;
  47
  48        if (u_array_size <= 0)
  49                return -EINVAL;
  50
  51        ide_floppy_create_read_capacity_cmd(pc);
  52
  53        if (ide_queue_pc_tail(drive, floppy->disk, pc, pc_buf, pc->req_xfer)) {
  54                printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
  55                return -EIO;
  56        }
  57
  58        header_len = pc_buf[3];
  59        desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
  60
  61        u_index = 0;
  62        argp = arg + 1;
  63
  64        /*
  65         * We always skip the first capacity descriptor.  That's the current
  66         * capacity.  We are interested in the remaining descriptors, the
  67         * formattable capacities.
  68         */
  69        for (i = 1; i < desc_cnt; i++) {
  70                unsigned int desc_start = 4 + i*8;
  71
  72                if (u_index >= u_array_size)
  73                        break;  /* User-supplied buffer too small */
  74
  75                blocks = be32_to_cpup((__be32 *)&pc_buf[desc_start]);
  76                length = be16_to_cpup((__be16 *)&pc_buf[desc_start + 6]);
  77
  78                if (put_user(blocks, argp))
  79                        return -EFAULT;
  80
  81                ++argp;
  82
  83                if (put_user(length, argp))
  84                        return -EFAULT;
  85
  86                ++argp;
  87
  88                ++u_index;
  89        }
  90
  91        if (put_user(u_index, arg))
  92                return -EFAULT;
  93
  94        return 0;
  95}
  96
  97static void ide_floppy_create_format_unit_cmd(struct ide_atapi_pc *pc,
  98                                              u8 *buf, int b, int l,
  99                                              int flags)
 100{
 101        ide_init_pc(pc);
 102        pc->c[0] = GPCMD_FORMAT_UNIT;
 103        pc->c[1] = 0x17;
 104
 105        memset(buf, 0, 12);
 106        buf[1] = 0xA2;
 107        /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
 108
 109        if (flags & 1)                          /* Verify bit on... */
 110                buf[1] ^= 0x20;                 /* ... turn off DCRT bit */
 111        buf[3] = 8;
 112
 113        put_unaligned(cpu_to_be32(b), (unsigned int *)(&buf[4]));
 114        put_unaligned(cpu_to_be32(l), (unsigned int *)(&buf[8]));
 115        pc->req_xfer = 12;
 116        pc->flags |= PC_FLAG_WRITING;
 117}
 118
 119static int ide_floppy_get_sfrp_bit(ide_drive_t *drive, struct ide_atapi_pc *pc)
 120{
 121        struct ide_disk_obj *floppy = drive->driver_data;
 122        u8 buf[20];
 123
 124        drive->atapi_flags &= ~IDE_AFLAG_SRFP;
 125
 126        ide_floppy_create_mode_sense_cmd(pc, IDEFLOPPY_CAPABILITIES_PAGE);
 127        pc->flags |= PC_FLAG_SUPPRESS_ERROR;
 128
 129        if (ide_queue_pc_tail(drive, floppy->disk, pc, buf, pc->req_xfer))
 130                return 1;
 131
 132        if (buf[8 + 2] & 0x40)
 133                drive->atapi_flags |= IDE_AFLAG_SRFP;
 134
 135        return 0;
 136}
 137
 138static int ide_floppy_format_unit(ide_drive_t *drive, struct ide_atapi_pc *pc,
 139                                  int __user *arg)
 140{
 141        struct ide_disk_obj *floppy = drive->driver_data;
 142        u8 buf[12];
 143        int blocks, length, flags, err = 0;
 144
 145        if (floppy->openers > 1) {
 146                /* Don't format if someone is using the disk */
 147                drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
 148                return -EBUSY;
 149        }
 150
 151        drive->dev_flags |= IDE_DFLAG_FORMAT_IN_PROGRESS;
 152
 153        /*
 154         * Send ATAPI_FORMAT_UNIT to the drive.
 155         *
 156         * Userland gives us the following structure:
 157         *
 158         * struct idefloppy_format_command {
 159         *        int nblocks;
 160         *        int blocksize;
 161         *        int flags;
 162         *        } ;
 163         *
 164         * flags is a bitmask, currently, the only defined flag is:
 165         *
 166         *        0x01 - verify media after format.
 167         */
 168        if (get_user(blocks, arg) ||
 169                        get_user(length, arg+1) ||
 170                        get_user(flags, arg+2)) {
 171                err = -EFAULT;
 172                goto out;
 173        }
 174
 175        ide_floppy_get_sfrp_bit(drive, pc);
 176        ide_floppy_create_format_unit_cmd(pc, buf, blocks, length, flags);
 177
 178        if (ide_queue_pc_tail(drive, floppy->disk, pc, buf, pc->req_xfer))
 179                err = -EIO;
 180
 181out:
 182        if (err)
 183                drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
 184        return err;
 185}
 186
 187/*
 188 * Get ATAPI_FORMAT_UNIT progress indication.
 189 *
 190 * Userland gives a pointer to an int.  The int is set to a progress
 191 * indicator 0-65536, with 65536=100%.
 192 *
 193 * If the drive does not support format progress indication, we just check
 194 * the dsc bit, and return either 0 or 65536.
 195 */
 196
 197static int ide_floppy_get_format_progress(ide_drive_t *drive,
 198                                          struct ide_atapi_pc *pc,
 199                                          int __user *arg)
 200{
 201        struct ide_disk_obj *floppy = drive->driver_data;
 202        u8 sense_buf[18];
 203        int progress_indication = 0x10000;
 204
 205        if (drive->atapi_flags & IDE_AFLAG_SRFP) {
 206                ide_create_request_sense_cmd(drive, pc);
 207                if (ide_queue_pc_tail(drive, floppy->disk, pc, sense_buf,
 208                                      pc->req_xfer))
 209                        return -EIO;
 210
 211                if (floppy->sense_key == 2 &&
 212                    floppy->asc == 4 &&
 213                    floppy->ascq == 4)
 214                        progress_indication = floppy->progress_indication;
 215
 216                /* Else assume format_unit has finished, and we're at 0x10000 */
 217        } else {
 218                ide_hwif_t *hwif = drive->hwif;
 219                unsigned long flags;
 220                u8 stat;
 221
 222                local_irq_save(flags);
 223                stat = hwif->tp_ops->read_status(hwif);
 224                local_irq_restore(flags);
 225
 226                progress_indication = ((stat & ATA_DSC) == 0) ? 0 : 0x10000;
 227        }
 228
 229        if (put_user(progress_indication, arg))
 230                return -EFAULT;
 231
 232        return 0;
 233}
 234
 235static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
 236                               unsigned long arg, unsigned int cmd)
 237{
 238        struct ide_disk_obj *floppy = drive->driver_data;
 239        struct gendisk *disk = floppy->disk;
 240        int prevent = (arg && cmd != CDROMEJECT) ? 1 : 0;
 241
 242        if (floppy->openers > 1)
 243                return -EBUSY;
 244
 245        ide_set_media_lock(drive, disk, prevent);
 246
 247        if (cmd == CDROMEJECT)
 248                ide_do_start_stop(drive, disk, 2);
 249=read_status" class="sref">read_status( class/a>( class/a>( class/a>( class/a>( clsif->
read_status2M0"> 140{ck(EBUSY;
EBUSY;
EBUSY;
;
;
CDROME 9lass2 8class5" id="L230" class="line" name="L230"" class="sref">get_user(EBUSY * the dsc bit, and return either 0 or 65536 class="sref">req_xfer))
        }
         */
pan>
 131

pc-&Dss="sref">arg, uns2"hc#L246" id="L246" class="line" name="L246"lass="sref">drive-> 180
 158drive-&gl.c#L249" id="L249" class="line" name="L249"> 249 ==          > href="+codIOCTLn class=GET="sreCITpevent = ( 196
( ==          = (floppy->flop!ivers/ide/ide-fl>get_user( 22FMODE6"> 1E_user( 1E(put_userCDROME openersPERM > 1)
 164 * the dsc bit, and RMAT_IN_PROGRESS" clas 137
(pc,
 ==          = ( 162<26+code=drive" class="sRMAT_IN_PROGRESS" clas 196
( = (get_user(get_user(get_user(( class/a>( class/a>( class/a>( class71"> 171<2a>                EBUSY;
EBUSY;
sref"_devics="sref">EBUSYEBUSYout;
get_user(EBUSY *pc,
put_user 173        }
drive->ide_2loppy_get_sfrp_bit(<2 href2"+code=drive" diref="+code=CDROMef">EBUSY;
(blocks, req_xfer))
d65536 class="sref">req_xfer))
pc,
flags);
IDE_DFLAG_FORMAT_IN_PROGRESS;
buf, drive,                 2a hre28ngth" class=""" class="sref">utex4" class="line" name>utex4" cl#L229de=put_unaligned" clutexef">EBUSY;
utexUSYEIO;
 246
 24ame="L246"> 246
drive->rt_unaligned" cl 234
EBUSY;
(pc,
 182 = -EFAULT;
dev2flags &= ~local_irq_restore(err;
drode=req_xfer" class="sref">rt_unaligned" clEBUSY;
EBUSY;
(get_user(EBUSYe" class="sref"drive" class="srAn5f 185}
req_xfer))
put_usercmd ==="ne" name="L246"lass="sref">drive->                   drode=req_xfer" cl=2loppy_ioctl.c#L181"2id= clL188" class=class184" id="L184" cl0ass="sref">utel.c#L186" id="L186" class="line" name="L186"> 186
 * Userl2nd gi2es a pointer to an int.  The         * skip SCSI>      SEND_COMMAND (deprecated)ss="line" name="L186"> 186
 191 * indicator 0-655        * -floname="SEND_PACKET (legacy) drivectl.c#L190" id="L190" class="line" name="L190"> 192e/ide-flo2py_ioctl.c#L192" id="L192" cla2s="line" name="L192"> 192        *L216" class="line" name="L216"> 216            pan class2"comment"> * If the driv2 does294_ioctl.c#L246ode=req_xfer" clas240"> 240        int  22FM class="line" name="L240"> 240        int  240     SCSI>      SEND_COMMAND_user" class="sref">put_user 194rt_unaligned" cl;
;
EBUSY/sdevps" class="sreode=" (EBUSYe" class="sref"drive" class="srAn5f 195req_xfer))
<= NOT=GET="sreCITpener NOT=G >ref="+code=put_user" class="sref">put_useri2e_drive_t *rt_unaligned" cl((e" class="sref"drive" class="srAn5f<"driref="+code=put_user249" class="line" name="L2="line" n2me="L185/ne" name="+code=ide_atapi_pc" cla2s="sr29         drode=req_xfer" cl=2loppy_ioctl.c#L1813        i3t drive->get3">arg3
utex4" cl#L229de=put_unune>utned" clutexef">EBUSY;
utexUSYdriv3-30y_ioctl.c#L231" id="sref">IDE_DFLAG_FORMAT_IN_PROGRESS;
bu3     u8<30d="L232" class="line" name="L232"> 232      303   3    int 
="droriginal LXR software bydicat" class="http://source idge.net/projects/lxr">LXR "> 1loppyef"drithis exp"Li192lxr@hreux.noef"d.
lxr.hreux.no kindly hosted byd" class="http://www.redpill-hrepro.no">Redpill Lrepro ASef"driprovL23r of Lreux ">nsultf="d-floop"Lme="L/ider0osr/idinceref"5.