5 #include <linux/hdreg.h>
8 static const struct ide_ioctl_devset ide_ioctl_settings[] = {
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 },
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)
21 const struct ide_devset *ds;
23 int err = -EOPNOTSUPP;
25 for (; (ds = s->setting); s++) {
26 if (ds->get && s->get_ioctl == cmd)
28 else if (ds->set && s->set_ioctl == cmd)
35 mutex_lock(&ide_setting_mtx);
36 spin_lock_irqsave(&ide_lock, flags);
38 spin_unlock_irqrestore(&ide_lock, flags);
39 mutex_unlock(&ide_setting_mtx);
40 return err >= 0 ? put_user(err, (long __user *)arg) : err;
43 if (bdev != bdev->bd_contains)
46 if (!capable(CAP_SYS_ADMIN))
49 mutex_lock(&ide_setting_mtx);
50 err = ide_devset_execute(drive, ds, arg);
51 mutex_unlock(&ide_setting_mtx);
56 EXPORT_SYMBOL_GPL(ide_setting_ioctl);
58 static int ide_get_identity_ioctl(ide_drive_t *drive, unsigned int cmd,
62 int size = (cmd == HDIO_GET_IDENTITY) ? (ATA_ID_WORDS * 2) : 142;
65 if (drive->id_read == 0) {
70 id = kmalloc(size, 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->dsc_overlap << IDE_NICE_DSC_OVERLAP) |
90 (drive->nice1 << IDE_NICE_1), (long __user *)arg);
93 static int ide_set_nice_ioctl(ide_drive_t *drive, unsigned long arg)
95 if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
98 if (((arg >> IDE_NICE_DSC_OVERLAP) & 1) &&
99 (drive->media == ide_disk || drive->media == ide_floppy ||
103 drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
104 drive->nice1 = (arg >> IDE_NICE_1) & 1;
109 static int ide_cmd_ioctl(ide_drive_t *drive, unsigned cmd, unsigned long arg)
112 int bufsize = 0, err = 0;
113 u8 args[4], xfer_rate = 0;
115 struct ide_taskfile *tf = &tfargs.tf;
118 if (NULL == (void *) arg) {
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);
129 if (copy_from_user(args, (void __user *)arg, 4))
132 memset(&tfargs, 0, sizeof(ide_task_t));
133 tf->feature = args[2];
134 if (args[0] == ATA_CMD_SMART) {
139 tfargs.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_IN_NSECT;
142 tfargs.tf_flags = IDE_TFLAG_OUT_FEATURE |
143 IDE_TFLAG_OUT_NSECT | IDE_TFLAG_IN_NSECT;
145 tf->command = args[0];
146 tfargs.data_phase = args[3] ? TASKFILE_IN : TASKFILE_NO_DATA;
149 tfargs.tf_flags |= IDE_TFLAG_IO_16BIT;
150 bufsize = SECTOR_SIZE * args[3];
151 buf = kzalloc(bufsize, GFP_KERNEL);
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])) {
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);
170 err = ide_raw_taskfile(drive, &tfargs, buf, args[3]);
172 args[0] = tf->status;
176 if (!err && xfer_rate) {
177 /* active-retuning-calls future */
178 ide_set_xfer_rate(drive, xfer_rate);
179 ide_driveid_update(drive);
182 if (copy_to_user((void __user *)arg, &args, 4))
185 if (copy_to_user((void __user *)(arg + 4), buf, bufsize))
192 static int ide_task_ioctl(ide_drive_t *drive, unsigned cmd, unsigned long arg)
194 void __user *p = (void __user *)arg;
199 if (copy_from_user(args, p, 7))
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;
207 err = ide_no_data_taskfile(drive, &task);
209 args[0] = task.tf.command;
210 memcpy(&args[1], &task.tf_array[7], 6);
212 if (copy_to_user(p, args, 7))
218 static int generic_drive_reset(ide_drive_t *drive)
223 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
224 rq->cmd_type = REQ_TYPE_SPECIAL;
226 rq->cmd[0] = REQ_DRIVE_RESET;
227 rq->cmd_flags |= REQ_SOFTBARRIER;
228 if (blk_execute_rq(drive->queue, NULL, rq, 1))
234 int generic_ide_ioctl(ide_drive_t *drive, struct file *file,
235 struct block_device *bdev,
236 unsigned int cmd, unsigned long arg)
240 err = ide_setting_ioctl(drive, bdev, cmd, arg, ide_ioctl_settings);
241 if (err != -EOPNOTSUPP)
245 case HDIO_OBSOLETE_IDENTITY:
246 case HDIO_GET_IDENTITY:
247 if (bdev != bdev->bd_contains)
249 return ide_get_identity_ioctl(drive, cmd, arg);
251 return ide_get_nice_ioctl(drive, arg);
253 if (!capable(CAP_SYS_ADMIN))
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))
260 if (drive->media == ide_disk)
261 return ide_taskfile_ioctl(drive, cmd, arg);
265 if (!capable(CAP_SYS_RAWIO))
267 return ide_cmd_ioctl(drive, cmd, arg);
268 case HDIO_DRIVE_TASK:
269 if (!capable(CAP_SYS_RAWIO))
271 return ide_task_ioctl(drive, cmd, arg);
272 case HDIO_DRIVE_RESET:
273 if (!capable(CAP_SYS_ADMIN))
275 return generic_drive_reset(drive);
276 case HDIO_GET_BUSSTATE:
277 if (!capable(CAP_SYS_ADMIN))
279 if (put_user(BUSSTATE_ON, (long __user *)arg))
282 case HDIO_SET_BUSSTATE:
283 if (!capable(CAP_SYS_ADMIN))
290 EXPORT_SYMBOL(generic_ide_ioctl);