5 #include <linux/export.h>
6 #include <linux/hdreg.h>
8 #include <linux/slab.h>
10 static const struct ide_ioctl_devset ide_ioctl_settings[] = {
11 { HDIO_GET_32BIT, HDIO_SET_32BIT, &ide_devset_io_32bit },
12 { HDIO_GET_KEEPSETTINGS, HDIO_SET_KEEPSETTINGS, &ide_devset_keepsettings },
13 { HDIO_GET_UNMASKINTR, HDIO_SET_UNMASKINTR, &ide_devset_unmaskirq },
14 { HDIO_GET_DMA, HDIO_SET_DMA, &ide_devset_using_dma },
15 { -1, HDIO_SET_PIO_MODE, &ide_devset_pio_mode },
19 int ide_setting_ioctl(ide_drive_t *drive, struct block_device *bdev,
20 unsigned int cmd, unsigned long arg,
21 const struct ide_ioctl_devset *s)
23 const struct ide_devset *ds;
24 int err = -EOPNOTSUPP;
26 for (; (ds = s->setting); s++) {
27 if (ds->get && s->get_ioctl == cmd)
29 else if (ds->set && s->set_ioctl == cmd)
36 mutex_lock(&ide_setting_mtx);
38 mutex_unlock(&ide_setting_mtx);
39 return err >= 0 ? put_user(err, (long __user *)arg) : err;
42 if (bdev != bdev->bd_contains)
45 if (!capable(CAP_SYS_ADMIN))
48 mutex_lock(&ide_setting_mtx);
49 err = ide_devset_execute(drive, ds, arg);
50 mutex_unlock(&ide_setting_mtx);
55 EXPORT_SYMBOL_GPL(ide_setting_ioctl);
57 static int ide_get_identity_ioctl(ide_drive_t *drive, unsigned int cmd,
61 int size = (cmd == HDIO_GET_IDENTITY) ? (ATA_ID_WORDS * 2) : 142;
64 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
69 /* ata_id_to_hd_driveid() relies on 'id' to be fully allocated. */
70 id = kmalloc(ATA_ID_WORDS * 2, GFP_KERNEL);
76 memcpy(id, drive->id, size);
77 ata_id_to_hd_driveid(id);
79 if (copy_to_user((void __user *)arg, id, size))
87 static int ide_get_nice_ioctl(ide_drive_t *drive, unsigned long arg)
89 return put_user((!!(drive->dev_flags & IDE_DFLAG_DSC_OVERLAP)
90 << IDE_NICE_DSC_OVERLAP) |
91 (!!(drive->dev_flags & IDE_DFLAG_NICE1)
92 << IDE_NICE_1), (long __user *)arg);
95 static int ide_set_nice_ioctl(ide_drive_t *drive, unsigned long arg)
97 if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
100 if (((arg >> IDE_NICE_DSC_OVERLAP) & 1) &&
101 (drive->media != ide_tape))
104 if ((arg >> IDE_NICE_DSC_OVERLAP) & 1)
105 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
107 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
109 if ((arg >> IDE_NICE_1) & 1)
110 drive->dev_flags |= IDE_DFLAG_NICE1;
112 drive->dev_flags &= ~IDE_DFLAG_NICE1;
117 static int ide_cmd_ioctl(ide_drive_t *drive, unsigned long arg)
120 int bufsize = 0, err = 0;
121 u8 args[4], xfer_rate = 0;
123 struct ide_taskfile *tf = &cmd.tf;
125 if (NULL == (void *) arg) {
128 rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
130 ide_req(rq)->type = ATA_PRIV_TASKFILE;
131 blk_execute_rq(drive->queue, NULL, rq, 0);
132 err = scsi_req(rq)->result ? -EIO : 0;
138 if (copy_from_user(args, (void __user *)arg, 4))
141 memset(&cmd, 0, sizeof(cmd));
142 tf->feature = args[2];
143 if (args[0] == ATA_CMD_SMART) {
146 tf->lbam = ATA_SMART_LBAM_PASS;
147 tf->lbah = ATA_SMART_LBAH_PASS;
148 cmd.valid.out.tf = IDE_VALID_OUT_TF;
149 cmd.valid.in.tf = IDE_VALID_NSECT;
152 cmd.valid.out.tf = IDE_VALID_FEATURE | IDE_VALID_NSECT;
153 cmd.valid.in.tf = IDE_VALID_NSECT;
155 tf->command = args[0];
156 cmd.protocol = args[3] ? ATA_PROT_PIO : ATA_PROT_NODATA;
159 cmd.tf_flags |= IDE_TFLAG_IO_16BIT;
160 bufsize = SECTOR_SIZE * args[3];
161 buf = kzalloc(bufsize, GFP_KERNEL);
166 if (tf->command == ATA_CMD_SET_FEATURES &&
167 tf->feature == SETFEATURES_XFER &&
168 tf->nsect >= XFER_SW_DMA_0) {
169 xfer_rate = ide_find_dma_mode(drive, tf->nsect);
170 if (xfer_rate != tf->nsect) {
175 cmd.tf_flags |= IDE_TFLAG_SET_XFER;
178 err = ide_raw_taskfile(drive, &cmd, buf, args[3]);
180 args[0] = tf->status;
184 if (copy_to_user((void __user *)arg, &args, 4))
187 if (copy_to_user((void __user *)(arg + 4), buf, bufsize))
194 static int ide_task_ioctl(ide_drive_t *drive, unsigned long arg)
196 void __user *p = (void __user *)arg;
201 if (copy_from_user(args, p, 7))
204 memset(&cmd, 0, sizeof(cmd));
205 memcpy(&cmd.tf.feature, &args[1], 6);
206 cmd.tf.command = args[0];
207 cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
208 cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
210 err = ide_no_data_taskfile(drive, &cmd);
212 args[0] = cmd.tf.command;
213 memcpy(&args[1], &cmd.tf.feature, 6);
215 if (copy_to_user(p, args, 7))
221 static int generic_drive_reset(ide_drive_t *drive)
226 rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
228 ide_req(rq)->type = ATA_PRIV_MISC;
229 scsi_req(rq)->cmd_len = 1;
230 scsi_req(rq)->cmd[0] = REQ_DRIVE_RESET;
231 blk_execute_rq(drive->queue, NULL, rq, 1);
232 ret = scsi_req(rq)->result;
237 int generic_ide_ioctl(ide_drive_t *drive, struct block_device *bdev,
238 unsigned int cmd, unsigned long arg)
242 err = ide_setting_ioctl(drive, bdev, cmd, arg, ide_ioctl_settings);
243 if (err != -EOPNOTSUPP)
247 case HDIO_OBSOLETE_IDENTITY:
248 case HDIO_GET_IDENTITY:
249 if (bdev != bdev->bd_contains)
251 return ide_get_identity_ioctl(drive, cmd, arg);
253 return ide_get_nice_ioctl(drive, arg);
255 if (!capable(CAP_SYS_ADMIN))
257 return ide_set_nice_ioctl(drive, arg);
258 #ifdef CONFIG_IDE_TASK_IOCTL
259 case HDIO_DRIVE_TASKFILE:
260 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
262 if (drive->media == ide_disk)
263 return ide_taskfile_ioctl(drive, arg);
267 if (!capable(CAP_SYS_RAWIO))
269 return ide_cmd_ioctl(drive, arg);
270 case HDIO_DRIVE_TASK:
271 if (!capable(CAP_SYS_RAWIO))
273 return ide_task_ioctl(drive, arg);
274 case HDIO_DRIVE_RESET:
275 if (!capable(CAP_SYS_ADMIN))
277 return generic_drive_reset(drive);
278 case HDIO_GET_BUSSTATE:
279 if (!capable(CAP_SYS_ADMIN))
281 if (put_user(BUSSTATE_ON, (long __user *)arg))
284 case HDIO_SET_BUSSTATE:
285 if (!capable(CAP_SYS_ADMIN))
292 EXPORT_SYMBOL(generic_ide_ioctl);