Linux Audio

Check our new training course

Loading...
Note: File does not exist in v6.8.
  1/*
  2 * IDE ioctls handling.
  3 */
  4
  5#include <linux/export.h>
  6#include <linux/hdreg.h>
  7#include <linux/ide.h>
  8#include <linux/slab.h>
  9
 10static 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  },
 16{ 0 }
 17};
 18
 19int 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)
 22{
 23	const struct ide_devset *ds;
 24	int err = -EOPNOTSUPP;
 25
 26	for (; (ds = s->setting); s++) {
 27		if (ds->get && s->get_ioctl == cmd)
 28			goto read_val;
 29		else if (ds->set && s->set_ioctl == cmd)
 30			goto set_val;
 31	}
 32
 33	return err;
 34
 35read_val:
 36	mutex_lock(&ide_setting_mtx);
 37	err = ds->get(drive);
 38	mutex_unlock(&ide_setting_mtx);
 39	return err >= 0 ? put_user(err, (long __user *)arg) : err;
 40
 41set_val:
 42	if (bdev != bdev->bd_contains)
 43		err = -EINVAL;
 44	else {
 45		if (!capable(CAP_SYS_ADMIN))
 46			err = -EACCES;
 47		else {
 48			mutex_lock(&ide_setting_mtx);
 49			err = ide_devset_execute(drive, ds, arg);
 50			mutex_unlock(&ide_setting_mtx);
 51		}
 52	}
 53	return err;
 54}
 55EXPORT_SYMBOL_GPL(ide_setting_ioctl);
 56
 57static int ide_get_identity_ioctl(ide_drive_t *drive, unsigned int cmd,
 58				  unsigned long arg)
 59{
 60	u16 *id = NULL;
 61	int size = (cmd == HDIO_GET_IDENTITY) ? (ATA_ID_WORDS * 2) : 142;
 62	int rc = 0;
 63
 64	if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
 65		rc = -ENOMSG;
 66		goto out;
 67	}
 68
 69	/* ata_id_to_hd_driveid() relies on 'id' to be fully allocated. */
 70	id = kmalloc(ATA_ID_WORDS * 2, 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);
 83out:
 84	return rc;
 85}
 86
 87static int ide_get_nice_ioctl(ide_drive_t *drive, unsigned long arg)
 88{
 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);
 93}
 94
 95static int ide_set_nice_ioctl(ide_drive_t *drive, unsigned long arg)
 96{
 97	if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
 98		return -EPERM;
 99
100	if (((arg >> IDE_NICE_DSC_OVERLAP) & 1) &&
101	    (drive->media != ide_tape))
102		return -EPERM;
103
104	if ((arg >> IDE_NICE_DSC_OVERLAP) & 1)
105		drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
106	else
107		drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
108
109	if ((arg >> IDE_NICE_1) & 1)
110		drive->dev_flags |= IDE_DFLAG_NICE1;
111	else
112		drive->dev_flags &= ~IDE_DFLAG_NICE1;
113
114	return 0;
115}
116
117static int ide_cmd_ioctl(ide_drive_t *drive, unsigned long arg)
118{
119	u8 *buf = NULL;
120	int bufsize = 0, err = 0;
121	u8 args[4], xfer_rate = 0;
122	struct ide_cmd cmd;
123	struct ide_taskfile *tf = &cmd.tf;
124
125	if (NULL == (void *) arg) {
126		struct request *rq;
127
128		rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
129		ide_req(rq)->type = ATA_PRIV_TASKFILE;
130		blk_execute_rq(drive->queue, NULL, rq, 0);
131		err = scsi_req(rq)->result ? -EIO : 0;
132		blk_put_request(rq);
133
134		return err;
135	}
136
137	if (copy_from_user(args, (void __user *)arg, 4))
138		return -EFAULT;
139
140	memset(&cmd, 0, sizeof(cmd));
141	tf->feature = args[2];
142	if (args[0] == ATA_CMD_SMART) {
143		tf->nsect = args[3];
144		tf->lbal  = args[1];
145		tf->lbam  = ATA_SMART_LBAM_PASS;
146		tf->lbah  = ATA_SMART_LBAH_PASS;
147		cmd.valid.out.tf = IDE_VALID_OUT_TF;
148		cmd.valid.in.tf  = IDE_VALID_NSECT;
149	} else {
150		tf->nsect = args[1];
151		cmd.valid.out.tf = IDE_VALID_FEATURE | IDE_VALID_NSECT;
152		cmd.valid.in.tf  = IDE_VALID_NSECT;
153	}
154	tf->command = args[0];
155	cmd.protocol = args[3] ? ATA_PROT_PIO : ATA_PROT_NODATA;
156
157	if (args[3]) {
158		cmd.tf_flags |= IDE_TFLAG_IO_16BIT;
159		bufsize = SECTOR_SIZE * args[3];
160		buf = kzalloc(bufsize, GFP_KERNEL);
161		if (buf == NULL)
162			return -ENOMEM;
163	}
164
165	if (tf->command == ATA_CMD_SET_FEATURES &&
166	    tf->feature == SETFEATURES_XFER &&
167	    tf->nsect >= XFER_SW_DMA_0) {
168		xfer_rate = ide_find_dma_mode(drive, tf->nsect);
169		if (xfer_rate != tf->nsect) {
170			err = -EINVAL;
171			goto abort;
172		}
173
174		cmd.tf_flags |= IDE_TFLAG_SET_XFER;
175	}
176
177	err = ide_raw_taskfile(drive, &cmd, buf, args[3]);
178
179	args[0] = tf->status;
180	args[1] = tf->error;
181	args[2] = tf->nsect;
182abort:
183	if (copy_to_user((void __user *)arg, &args, 4))
184		err = -EFAULT;
185	if (buf) {
186		if (copy_to_user((void __user *)(arg + 4), buf, bufsize))
187			err = -EFAULT;
188		kfree(buf);
189	}
190	return err;
191}
192
193static int ide_task_ioctl(ide_drive_t *drive, unsigned long arg)
194{
195	void __user *p = (void __user *)arg;
196	int err = 0;
197	u8 args[7];
198	struct ide_cmd cmd;
199
200	if (copy_from_user(args, p, 7))
201		return -EFAULT;
202
203	memset(&cmd, 0, sizeof(cmd));
204	memcpy(&cmd.tf.feature, &args[1], 6);
205	cmd.tf.command = args[0];
206	cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
207	cmd.valid.in.tf  = IDE_VALID_IN_TF  | IDE_VALID_DEVICE;
208
209	err = ide_no_data_taskfile(drive, &cmd);
210
211	args[0] = cmd.tf.command;
212	memcpy(&args[1], &cmd.tf.feature, 6);
213
214	if (copy_to_user(p, args, 7))
215		err = -EFAULT;
216
217	return err;
218}
219
220static int generic_drive_reset(ide_drive_t *drive)
221{
222	struct request *rq;
223	int ret = 0;
224
225	rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
226	ide_req(rq)->type = ATA_PRIV_MISC;
227	scsi_req(rq)->cmd_len = 1;
228	scsi_req(rq)->cmd[0] = REQ_DRIVE_RESET;
229	blk_execute_rq(drive->queue, NULL, rq, 1);
230	ret = scsi_req(rq)->result;
231	blk_put_request(rq);
232	return ret;
233}
234
235int generic_ide_ioctl(ide_drive_t *drive, 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, 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, arg);
268	case HDIO_DRIVE_TASK:
269		if (!capable(CAP_SYS_RAWIO))
270			return -EACCES;
271		return ide_task_ioctl(drive, 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}
290EXPORT_SYMBOL(generic_ide_ioctl);