Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 1 | /* |
| 2 | * IDE ioctls handling. |
| 3 | */ |
| 4 | |
| 5 | #include <linux/hdreg.h> |
| 6 | #include <linux/ide.h> |
| 7 | |
| 8 | static const struct ide_ioctl_devset ide_ioctl_settings[] = { |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame^] | 9 | { HDIO_GET_32BIT, HDIO_SET_32BIT, &ide_devset_io_32bit }, |
| 10 | { HDIO_GET_KEEPSETTINGS, HDIO_SET_KEEPSETTINGS, &ide_devset_keepsettings }, |
| 11 | { HDIO_GET_UNMASKINTR, HDIO_SET_UNMASKINTR, &ide_devset_unmaskirq }, |
| 12 | { HDIO_GET_DMA, HDIO_SET_DMA, &ide_devset_using_dma }, |
| 13 | { -1, HDIO_SET_PIO_MODE, &ide_devset_pio_mode }, |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 14 | { 0 } |
| 15 | }; |
| 16 | |
| 17 | int ide_setting_ioctl(ide_drive_t *drive, struct block_device *bdev, |
| 18 | unsigned int cmd, unsigned long arg, |
| 19 | const struct ide_ioctl_devset *s) |
| 20 | { |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame^] | 21 | const struct ide_devset *ds; |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 22 | unsigned long flags; |
| 23 | int err = -EOPNOTSUPP; |
| 24 | |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame^] | 25 | for (; (ds = s->setting); s++) { |
| 26 | if (ds->get && s->get_ioctl == cmd) |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 27 | goto read_val; |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame^] | 28 | else if (ds->set && s->set_ioctl == cmd) |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 29 | goto set_val; |
| 30 | } |
| 31 | |
| 32 | return err; |
| 33 | |
| 34 | read_val: |
| 35 | mutex_lock(&ide_setting_mtx); |
| 36 | spin_lock_irqsave(&ide_lock, flags); |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame^] | 37 | err = ds->get(drive); |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 38 | spin_unlock_irqrestore(&ide_lock, flags); |
| 39 | mutex_unlock(&ide_setting_mtx); |
| 40 | return err >= 0 ? put_user(err, (long __user *)arg) : err; |
| 41 | |
| 42 | set_val: |
| 43 | if (bdev != bdev->bd_contains) |
| 44 | err = -EINVAL; |
| 45 | else { |
| 46 | if (!capable(CAP_SYS_ADMIN)) |
| 47 | err = -EACCES; |
| 48 | else { |
| 49 | mutex_lock(&ide_setting_mtx); |
Elias Oltmanns | 92f1f8f | 2008-10-10 22:39:40 +0200 | [diff] [blame^] | 50 | err = ide_devset_execute(drive, ds, arg); |
Bartlomiej Zolnierkiewicz | 05236ea | 2008-10-10 22:39:33 +0200 | [diff] [blame] | 51 | mutex_unlock(&ide_setting_mtx); |
| 52 | } |
| 53 | } |
| 54 | return err; |
| 55 | } |
| 56 | EXPORT_SYMBOL_GPL(ide_setting_ioctl); |
| 57 | |
| 58 | static int ide_get_identity_ioctl(ide_drive_t *drive, unsigned int cmd, |
| 59 | unsigned long arg) |
| 60 | { |
| 61 | u16 *id = NULL; |
| 62 | int size = (cmd == HDIO_GET_IDENTITY) ? (ATA_ID_WORDS * 2) : 142; |
| 63 | int rc = 0; |
| 64 | |
| 65 | if (drive->id_read == 0) { |
| 66 | rc = -ENOMSG; |
| 67 | goto out; |
| 68 | } |
| 69 | |
| 70 | id = kmalloc(size, GFP_KERNEL); |
| 71 | if (id == NULL) { |
| 72 | rc = -ENOMEM; |
| 73 | goto out; |
| 74 | } |
| 75 | |
| 76 | memcpy(id, drive->id, size); |
| 77 | ata_id_to_hd_driveid(id); |
| 78 | |
| 79 | if (copy_to_user((void __user *)arg, id, size)) |
| 80 | rc = -EFAULT; |
| 81 | |
| 82 | kfree(id); |
| 83 | out: |
| 84 | return rc; |
| 85 | } |
| 86 | |
| 87 | static int ide_get_nice_ioctl(ide_drive_t *drive, unsigned long arg) |
| 88 | { |
| 89 | return put_user((drive->dsc_overlap << IDE_NICE_DSC_OVERLAP) | |
| 90 | (drive->nice1 << IDE_NICE_1), (long __user *)arg); |
| 91 | } |
| 92 | |
| 93 | static int ide_set_nice_ioctl(ide_drive_t *drive, unsigned long arg) |
| 94 | { |
| 95 | if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1)))) |
| 96 | return -EPERM; |
| 97 | |
| 98 | if (((arg >> IDE_NICE_DSC_OVERLAP) & 1) && |
| 99 | (drive->media == ide_disk || drive->media == ide_floppy || |
| 100 | drive->scsi)) |
| 101 | return -EPERM; |
| 102 | |
| 103 | drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1; |
| 104 | drive->nice1 = (arg >> IDE_NICE_1) & 1; |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static int ide_cmd_ioctl(ide_drive_t *drive, unsigned cmd, unsigned long arg) |
| 110 | { |
| 111 | u8 *buf = NULL; |
| 112 | int bufsize = 0, err = 0; |
| 113 | u8 args[4], xfer_rate = 0; |
| 114 | ide_task_t tfargs; |
| 115 | struct ide_taskfile *tf = &tfargs.tf; |
| 116 | u16 *id = drive->id; |
| 117 | |
| 118 | if (NULL == (void *) arg) { |
| 119 | struct request *rq; |
| 120 | |
| 121 | rq = blk_get_request(drive->queue, READ, __GFP_WAIT); |
| 122 | rq->cmd_type = REQ_TYPE_ATA_TASKFILE; |
| 123 | err = blk_execute_rq(drive->queue, NULL, rq, 0); |
| 124 | blk_put_request(rq); |
| 125 | |
| 126 | return err; |
| 127 | } |
| 128 | |
| 129 | if (copy_from_user(args, (void __user *)arg, 4)) |
| 130 | return -EFAULT; |
| 131 | |
| 132 | memset(&tfargs, 0, sizeof(ide_task_t)); |
| 133 | tf->feature = args[2]; |
| 134 | if (args[0] == ATA_CMD_SMART) { |
| 135 | tf->nsect = args[3]; |
| 136 | tf->lbal = args[1]; |
| 137 | tf->lbam = 0x4f; |
| 138 | tf->lbah = 0xc2; |
| 139 | tfargs.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_IN_NSECT; |
| 140 | } else { |
| 141 | tf->nsect = args[1]; |
| 142 | tfargs.tf_flags = IDE_TFLAG_OUT_FEATURE | |
| 143 | IDE_TFLAG_OUT_NSECT | IDE_TFLAG_IN_NSECT; |
| 144 | } |
| 145 | tf->command = args[0]; |
| 146 | tfargs.data_phase = args[3] ? TASKFILE_IN : TASKFILE_NO_DATA; |
| 147 | |
| 148 | if (args[3]) { |
| 149 | tfargs.tf_flags |= IDE_TFLAG_IO_16BIT; |
| 150 | bufsize = SECTOR_SIZE * args[3]; |
| 151 | buf = kzalloc(bufsize, GFP_KERNEL); |
| 152 | if (buf == NULL) |
| 153 | return -ENOMEM; |
| 154 | } |
| 155 | |
| 156 | if (tf->command == ATA_CMD_SET_FEATURES && |
| 157 | tf->feature == SETFEATURES_XFER && |
| 158 | tf->nsect >= XFER_SW_DMA_0 && |
| 159 | (id[ATA_ID_UDMA_MODES] || |
| 160 | id[ATA_ID_MWDMA_MODES] || |
| 161 | id[ATA_ID_SWDMA_MODES])) { |
| 162 | xfer_rate = args[1]; |
| 163 | if (tf->nsect > XFER_UDMA_2 && !eighty_ninty_three(drive)) { |
| 164 | printk(KERN_WARNING "%s: UDMA speeds >UDMA33 cannot " |
| 165 | "be set\n", drive->name); |
| 166 | goto abort; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | err = ide_raw_taskfile(drive, &tfargs, buf, args[3]); |
| 171 | |
| 172 | args[0] = tf->status; |
| 173 | args[1] = tf->error; |
| 174 | args[2] = tf->nsect; |
| 175 | |
| 176 | if (!err && xfer_rate) { |
| 177 | /* active-retuning-calls future */ |
| 178 | ide_set_xfer_rate(drive, xfer_rate); |
| 179 | ide_driveid_update(drive); |
| 180 | } |
| 181 | abort: |
| 182 | if (copy_to_user((void __user *)arg, &args, 4)) |
| 183 | err = -EFAULT; |
| 184 | if (buf) { |
| 185 | if (copy_to_user((void __user *)(arg + 4), buf, bufsize)) |
| 186 | err = -EFAULT; |
| 187 | kfree(buf); |
| 188 | } |
| 189 | return err; |
| 190 | } |
| 191 | |
| 192 | static int ide_task_ioctl(ide_drive_t *drive, unsigned cmd, unsigned long arg) |
| 193 | { |
| 194 | void __user *p = (void __user *)arg; |
| 195 | int err = 0; |
| 196 | u8 args[7]; |
| 197 | ide_task_t task; |
| 198 | |
| 199 | if (copy_from_user(args, p, 7)) |
| 200 | return -EFAULT; |
| 201 | |
| 202 | memset(&task, 0, sizeof(task)); |
| 203 | memcpy(&task.tf_array[7], &args[1], 6); |
| 204 | task.tf.command = args[0]; |
| 205 | task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE; |
| 206 | |
| 207 | err = ide_no_data_taskfile(drive, &task); |
| 208 | |
| 209 | args[0] = task.tf.command; |
| 210 | memcpy(&args[1], &task.tf_array[7], 6); |
| 211 | |
| 212 | if (copy_to_user(p, args, 7)) |
| 213 | err = -EFAULT; |
| 214 | |
| 215 | return err; |
| 216 | } |
| 217 | |
| 218 | static int generic_drive_reset(ide_drive_t *drive) |
| 219 | { |
| 220 | struct request *rq; |
| 221 | int ret = 0; |
| 222 | |
| 223 | rq = blk_get_request(drive->queue, READ, __GFP_WAIT); |
| 224 | rq->cmd_type = REQ_TYPE_SPECIAL; |
| 225 | rq->cmd_len = 1; |
| 226 | rq->cmd[0] = REQ_DRIVE_RESET; |
| 227 | rq->cmd_flags |= REQ_SOFTBARRIER; |
| 228 | if (blk_execute_rq(drive->queue, NULL, rq, 1)) |
| 229 | ret = rq->errors; |
| 230 | blk_put_request(rq); |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | int generic_ide_ioctl(ide_drive_t *drive, struct file *file, |
| 235 | struct block_device *bdev, |
| 236 | unsigned int cmd, unsigned long arg) |
| 237 | { |
| 238 | int err; |
| 239 | |
| 240 | err = ide_setting_ioctl(drive, bdev, cmd, arg, ide_ioctl_settings); |
| 241 | if (err != -EOPNOTSUPP) |
| 242 | return err; |
| 243 | |
| 244 | switch (cmd) { |
| 245 | case HDIO_OBSOLETE_IDENTITY: |
| 246 | case HDIO_GET_IDENTITY: |
| 247 | if (bdev != bdev->bd_contains) |
| 248 | return -EINVAL; |
| 249 | return ide_get_identity_ioctl(drive, cmd, arg); |
| 250 | case HDIO_GET_NICE: |
| 251 | return ide_get_nice_ioctl(drive, arg); |
| 252 | case HDIO_SET_NICE: |
| 253 | if (!capable(CAP_SYS_ADMIN)) |
| 254 | return -EACCES; |
| 255 | return ide_set_nice_ioctl(drive, arg); |
| 256 | #ifdef CONFIG_IDE_TASK_IOCTL |
| 257 | case HDIO_DRIVE_TASKFILE: |
| 258 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) |
| 259 | return -EACCES; |
| 260 | if (drive->media == ide_disk) |
| 261 | return ide_taskfile_ioctl(drive, cmd, arg); |
| 262 | return -ENOMSG; |
| 263 | #endif |
| 264 | case HDIO_DRIVE_CMD: |
| 265 | if (!capable(CAP_SYS_RAWIO)) |
| 266 | return -EACCES; |
| 267 | return ide_cmd_ioctl(drive, cmd, arg); |
| 268 | case HDIO_DRIVE_TASK: |
| 269 | if (!capable(CAP_SYS_RAWIO)) |
| 270 | return -EACCES; |
| 271 | return ide_task_ioctl(drive, cmd, arg); |
| 272 | case HDIO_DRIVE_RESET: |
| 273 | if (!capable(CAP_SYS_ADMIN)) |
| 274 | return -EACCES; |
| 275 | return generic_drive_reset(drive); |
| 276 | case HDIO_GET_BUSSTATE: |
| 277 | if (!capable(CAP_SYS_ADMIN)) |
| 278 | return -EACCES; |
| 279 | if (put_user(BUSSTATE_ON, (long __user *)arg)) |
| 280 | return -EFAULT; |
| 281 | return 0; |
| 282 | case HDIO_SET_BUSSTATE: |
| 283 | if (!capable(CAP_SYS_ADMIN)) |
| 284 | return -EACCES; |
| 285 | return -EOPNOTSUPP; |
| 286 | default: |
| 287 | return -EINVAL; |
| 288 | } |
| 289 | } |
| 290 | EXPORT_SYMBOL(generic_ide_ioctl); |