5 #include <linux/hdreg.h>
8 static const struct ide_ioctl_devset ide_ioctl_settings[] = {
9 { HDIO_GET_32BIT, HDIO_SET_32BIT, get_io_32bit, set_io_32bit },
10 { HDIO_GET_KEEPSETTINGS, HDIO_SET_KEEPSETTINGS, get_ksettings, set_ksettings },
11 { HDIO_GET_UNMASKINTR, HDIO_SET_UNMASKINTR, get_unmaskirq, set_unmaskirq },
12 { HDIO_GET_DMA, HDIO_SET_DMA, get_using_dma, set_using_dma },
13 { -1, HDIO_SET_PIO_MODE, NULL, set_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)
22 int err = -EOPNOTSUPP;
24 for (; s->get_ioctl; s++) {
25 if (s->get && s->get_ioctl == cmd)
27 else if (s->set && s->set_ioctl == cmd)
34 mutex_lock(&ide_setting_mtx);
35 spin_lock_irqsave(&ide_lock, flags);
37 spin_unlock_irqrestore(&ide_lock, flags);
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 = s->set(drive, 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->id_read == 0) {
69 id = kmalloc(size, GFP_KERNEL);
75 memcpy(id, drive->id, size);
76 ata_id_to_hd_driveid(id);
78 if (copy_to_user((void __user *)arg, id, size))
86 static int ide_get_nice_ioctl(ide_drive_t *drive, unsigned long arg)
88 return put_user((drive->dsc_overlap << IDE_NICE_DSC_OVERLAP) |
89 (drive->nice1 << IDE_NICE_1), (long __user *)arg);
92 static int ide_set_nice_ioctl(ide_drive_t *drive, unsigned long arg)
94 if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
97 if (((arg >> IDE_NICE_DSC_OVERLAP) & 1) &&
98 (drive->media == ide_disk || drive->media == ide_floppy ||
102 drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
103 drive->nice1 = (arg >> IDE_NICE_1) & 1;
108 static int ide_cmd_ioctl(ide_drive_t *drive, unsigned cmd, unsigned long arg)
111 int bufsize = 0, err = 0;
112 u8 args[4], xfer_rate = 0;
114 struct ide_taskfile *tf = &tfargs.tf;
117 if (NULL == (void *) arg) {
120 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
121 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
122 err = blk_execute_rq(drive->queue, NULL, rq, 0);
128 if (copy_from_user(args, (void __user *)arg, 4))
131 memset(&tfargs, 0, sizeof(ide_task_t));
132 tf->feature = args[2];
133 if (args[0] == ATA_CMD_SMART) {
138 tfargs.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_IN_NSECT;
141 tfargs.tf_flags = IDE_TFLAG_OUT_FEATURE |
142 IDE_TFLAG_OUT_NSECT | IDE_TFLAG_IN_NSECT;
144 tf->command = args[0];
145 tfargs.data_phase = args[3] ? TASKFILE_IN : TASKFILE_NO_DATA;
148 tfargs.tf_flags |= IDE_TFLAG_IO_16BIT;
149 bufsize = SECTOR_SIZE * args[3];
150 buf = kzalloc(bufsize, GFP_KERNEL);
155 if (tf->command == ATA_CMD_SET_FEATURES &&
156 tf->feature == SETFEATURES_XFER &&
157 tf->nsect >= XFER_SW_DMA_0 &&
158 (id[ATA_ID_UDMA_MODES] ||
159 id[ATA_ID_MWDMA_MODES] ||
160 id[ATA_ID_SWDMA_MODES])) {
162 if (tf->nsect > XFER_UDMA_2 && !eighty_ninty_three(drive)) {
163 printk(KERN_WARNING "%s: UDMA speeds >UDMA33 cannot "
164 "be set\n", drive->name);
169 err = ide_raw_taskfile(drive, &tfargs, buf, args[3]);
171 args[0] = tf->status;
175 if (!err && xfer_rate) {
176 /* active-retuning-calls future */
177 ide_set_xfer_rate(drive, xfer_rate);
178 ide_driveid_update(drive);
181 if (copy_to_user((void __user *)arg, &args, 4))
184 if (copy_to_user((void __user *)(arg + 4), buf, bufsize))
191 static int ide_task_ioctl(ide_drive_t *drive, unsigned cmd, unsigned long arg)
193 void __user *p = (void __user *)arg;
198 if (copy_from_user(args, p, 7))
201 memset(&task, 0, sizeof(task));
202 memcpy(&task.tf_array[7], &args[1], 6);
203 task.tf.command = args[0];
204 task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
206 err = ide_no_data_taskfile(drive, &task);
208 args[0] = task.tf.command;
209 memcpy(&args[1], &task.tf_array[7], 6);
211 if (copy_to_user(p, args, 7))
217 static int generic_drive_reset(ide_drive_t *drive)
222 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
223 rq->cmd_type = REQ_TYPE_SPECIAL;
225 rq->cmd[0] = REQ_DRIVE_RESET;
226 rq->cmd_flags |= REQ_SOFTBARRIER;
227 if (blk_execute_rq(drive->queue, NULL, rq, 1))
233 int generic_ide_ioctl(ide_drive_t *drive, struct file *file,
234 struct block_device *bdev,
235 unsigned int cmd, unsigned long arg)
239 err = ide_setting_ioctl(drive, bdev, cmd, arg, ide_ioctl_settings);
240 if (err != -EOPNOTSUPP)
244 case HDIO_OBSOLETE_IDENTITY:
245 case HDIO_GET_IDENTITY:
246 if (bdev != bdev->bd_contains)
248 return ide_get_identity_ioctl(drive, cmd, arg);
250 return ide_get_nice_ioctl(drive, arg);
252 if (!capable(CAP_SYS_ADMIN))
254 return ide_set_nice_ioctl(drive, arg);
255 #ifdef CONFIG_IDE_TASK_IOCTL
256 case HDIO_DRIVE_TASKFILE:
257 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
259 if (drive->media == ide_disk)
260 return ide_taskfile_ioctl(drive, cmd, arg);
264 if (!capable(CAP_SYS_RAWIO))
266 return ide_cmd_ioctl(drive, cmd, arg);
267 case HDIO_DRIVE_TASK:
268 if (!capable(CAP_SYS_RAWIO))
270 return ide_task_ioctl(drive, cmd, arg);
271 case HDIO_DRIVE_RESET:
272 if (!capable(CAP_SYS_ADMIN))
274 return generic_drive_reset(drive);
275 case HDIO_GET_BUSSTATE:
276 if (!capable(CAP_SYS_ADMIN))
278 if (put_user(BUSSTATE_ON, (long __user *)arg))
281 case HDIO_SET_BUSSTATE:
282 if (!capable(CAP_SYS_ADMIN))
289 EXPORT_SYMBOL(generic_ide_ioctl);