ide: move IDE{FLOPPY,TAPE}_WAIT_CMD defines to <linux/ide.h>
[linux-2.6-block.git] / drivers / ide / ide-ioctls.c
CommitLineData
05236ea6
BZ
1/*
2 * IDE ioctls handling.
3 */
4
5#include <linux/hdreg.h>
6#include <linux/ide.h>
7
8static 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 },
14{ 0 }
15};
16
17int 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{
21 unsigned long flags;
22 int err = -EOPNOTSUPP;
23
24 for (; s->get_ioctl; s++) {
25 if (s->get && s->get_ioctl == cmd)
26 goto read_val;
27 else if (s->set && s->set_ioctl == cmd)
28 goto set_val;
29 }
30
31 return err;
32
33read_val:
34 mutex_lock(&ide_setting_mtx);
35 spin_lock_irqsave(&ide_lock, flags);
36 err = s->get(drive);
37 spin_unlock_irqrestore(&ide_lock, flags);
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 = s->set(drive, 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->id_read == 0) {
65 rc = -ENOMSG;
66 goto out;
67 }
68
69 id = kmalloc(size, GFP_KERNEL);
70 if (id == NULL) {
71 rc = -ENOMEM;
72 goto out;
73 }
74
75 memcpy(id, drive->id, size);
76 ata_id_to_hd_driveid(id);
77
78 if (copy_to_user((void __user *)arg, id, size))
79 rc = -EFAULT;
80
81 kfree(id);
82out:
83 return rc;
84}
85
86static int ide_get_nice_ioctl(ide_drive_t *drive, unsigned long arg)
87{
88 return put_user((drive->dsc_overlap << IDE_NICE_DSC_OVERLAP) |
89 (drive->nice1 << IDE_NICE_1), (long __user *)arg);
90}
91
92static int ide_set_nice_ioctl(ide_drive_t *drive, unsigned long arg)
93{
94 if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
95 return -EPERM;
96
97 if (((arg >> IDE_NICE_DSC_OVERLAP) & 1) &&
98 (drive->media == ide_disk || drive->media == ide_floppy ||
99 drive->scsi))
100 return -EPERM;
101
102 drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
103 drive->nice1 = (arg >> IDE_NICE_1) & 1;
104
105 return 0;
106}
107
108static int ide_cmd_ioctl(ide_drive_t *drive, unsigned cmd, unsigned long arg)
109{
110 u8 *buf = NULL;
111 int bufsize = 0, err = 0;
112 u8 args[4], xfer_rate = 0;
113 ide_task_t tfargs;
114 struct ide_taskfile *tf = &tfargs.tf;
115 u16 *id = drive->id;
116
117 if (NULL == (void *) arg) {
118 struct request *rq;
119
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);
123 blk_put_request(rq);
124
125 return err;
126 }
127
128 if (copy_from_user(args, (void __user *)arg, 4))
129 return -EFAULT;
130
131 memset(&tfargs, 0, sizeof(ide_task_t));
132 tf->feature = args[2];
133 if (args[0] == ATA_CMD_SMART) {
134 tf->nsect = args[3];
135 tf->lbal = args[1];
136 tf->lbam = 0x4f;
137 tf->lbah = 0xc2;
138 tfargs.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_IN_NSECT;
139 } else {
140 tf->nsect = args[1];
141 tfargs.tf_flags = IDE_TFLAG_OUT_FEATURE |
142 IDE_TFLAG_OUT_NSECT | IDE_TFLAG_IN_NSECT;
143 }
144 tf->command = args[0];
145 tfargs.data_phase = args[3] ? TASKFILE_IN : TASKFILE_NO_DATA;
146
147 if (args[3]) {
148 tfargs.tf_flags |= IDE_TFLAG_IO_16BIT;
149 bufsize = SECTOR_SIZE * args[3];
150 buf = kzalloc(bufsize, GFP_KERNEL);
151 if (buf == NULL)
152 return -ENOMEM;
153 }
154
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])) {
161 xfer_rate = args[1];
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);
165 goto abort;
166 }
167 }
168
169 err = ide_raw_taskfile(drive, &tfargs, buf, args[3]);
170
171 args[0] = tf->status;
172 args[1] = tf->error;
173 args[2] = tf->nsect;
174
175 if (!err && xfer_rate) {
176 /* active-retuning-calls future */
177 ide_set_xfer_rate(drive, xfer_rate);
178 ide_driveid_update(drive);
179 }
180abort:
181 if (copy_to_user((void __user *)arg, &args, 4))
182 err = -EFAULT;
183 if (buf) {
184 if (copy_to_user((void __user *)(arg + 4), buf, bufsize))
185 err = -EFAULT;
186 kfree(buf);
187 }
188 return err;
189}
190
191static int ide_task_ioctl(ide_drive_t *drive, unsigned cmd, unsigned long arg)
192{
193 void __user *p = (void __user *)arg;
194 int err = 0;
195 u8 args[7];
196 ide_task_t task;
197
198 if (copy_from_user(args, p, 7))
199 return -EFAULT;
200
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;
205
206 err = ide_no_data_taskfile(drive, &task);
207
208 args[0] = task.tf.command;
209 memcpy(&args[1], &task.tf_array[7], 6);
210
211 if (copy_to_user(p, args, 7))
212 err = -EFAULT;
213
214 return err;
215}
216
217static int generic_drive_reset(ide_drive_t *drive)
218{
219 struct request *rq;
220 int ret = 0;
221
222 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
223 rq->cmd_type = REQ_TYPE_SPECIAL;
224 rq->cmd_len = 1;
225 rq->cmd[0] = REQ_DRIVE_RESET;
226 rq->cmd_flags |= REQ_SOFTBARRIER;
227 if (blk_execute_rq(drive->queue, NULL, rq, 1))
228 ret = rq->errors;
229 blk_put_request(rq);
230 return ret;
231}
232
233int generic_ide_ioctl(ide_drive_t *drive, struct file *file,
234 struct block_device *bdev,
235 unsigned int cmd, unsigned long arg)
236{
237 int err;
238
239 err = ide_setting_ioctl(drive, bdev, cmd, arg, ide_ioctl_settings);
240 if (err != -EOPNOTSUPP)
241 return err;
242
243 switch (cmd) {
244 case HDIO_OBSOLETE_IDENTITY:
245 case HDIO_GET_IDENTITY:
246 if (bdev != bdev->bd_contains)
247 return -EINVAL;
248 return ide_get_identity_ioctl(drive, cmd, arg);
249 case HDIO_GET_NICE:
250 return ide_get_nice_ioctl(drive, arg);
251 case HDIO_SET_NICE:
252 if (!capable(CAP_SYS_ADMIN))
253 return -EACCES;
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))
258 return -EACCES;
259 if (drive->media == ide_disk)
260 return ide_taskfile_ioctl(drive, cmd, arg);
261 return -ENOMSG;
262#endif
263 case HDIO_DRIVE_CMD:
264 if (!capable(CAP_SYS_RAWIO))
265 return -EACCES;
266 return ide_cmd_ioctl(drive, cmd, arg);
267 case HDIO_DRIVE_TASK:
268 if (!capable(CAP_SYS_RAWIO))
269 return -EACCES;
270 return ide_task_ioctl(drive, cmd, arg);
271 case HDIO_DRIVE_RESET:
272 if (!capable(CAP_SYS_ADMIN))
273 return -EACCES;
274 return generic_drive_reset(drive);
275 case HDIO_GET_BUSSTATE:
276 if (!capable(CAP_SYS_ADMIN))
277 return -EACCES;
278 if (put_user(BUSSTATE_ON, (long __user *)arg))
279 return -EFAULT;
280 return 0;
281 case HDIO_SET_BUSSTATE:
282 if (!capable(CAP_SYS_ADMIN))
283 return -EACCES;
284 return -EOPNOTSUPP;
285 default:
286 return -EINVAL;
287 }
288}
289EXPORT_SYMBOL(generic_ide_ioctl);