libata: mask off DET when restoring SControl for detach
[linux-2.6/mini2440.git] / drivers / ide / ide-ioctls.c
blobfcde16bb53a71498ab6000626b49a2581e9df591
1 /*
2 * IDE ioctls handling.
3 */
5 #include <linux/hdreg.h>
6 #include <linux/ide.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 },
14 { 0 }
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;
22 unsigned long flags;
23 int err = -EOPNOTSUPP;
25 for (; (ds = s->setting); s++) {
26 if (ds->get && s->get_ioctl == cmd)
27 goto read_val;
28 else if (ds->set && s->set_ioctl == cmd)
29 goto set_val;
32 return err;
34 read_val:
35 mutex_lock(&ide_setting_mtx);
36 spin_lock_irqsave(&ide_lock, flags);
37 err = ds->get(drive);
38 spin_unlock_irqrestore(&ide_lock, flags);
39 mutex_unlock(&ide_setting_mtx);
40 return err >= 0 ? put_user(err, (long __user *)arg) : err;
42 set_val:
43 if (bdev != bdev->bd_contains)
44 err = -EINVAL;
45 else {
46 if (!capable(CAP_SYS_ADMIN))
47 err = -EACCES;
48 else {
49 mutex_lock(&ide_setting_mtx);
50 err = ide_devset_execute(drive, ds, arg);
51 mutex_unlock(&ide_setting_mtx);
54 return err;
56 EXPORT_SYMBOL_GPL(ide_setting_ioctl);
58 static int ide_get_identity_ioctl(ide_drive_t *drive, unsigned int cmd,
59 unsigned long arg)
61 u16 *id = NULL;
62 int size = (cmd == HDIO_GET_IDENTITY) ? (ATA_ID_WORDS * 2) : 142;
63 int rc = 0;
65 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
66 rc = -ENOMSG;
67 goto out;
70 id = kmalloc(size, GFP_KERNEL);
71 if (id == NULL) {
72 rc = -ENOMEM;
73 goto out;
76 memcpy(id, drive->id, size);
77 ata_id_to_hd_driveid(id);
79 if (copy_to_user((void __user *)arg, id, size))
80 rc = -EFAULT;
82 kfree(id);
83 out:
84 return rc;
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))))
98 return -EPERM;
100 if (((arg >> IDE_NICE_DSC_OVERLAP) & 1) &&
101 (drive->media == ide_disk || drive->media == ide_floppy ||
102 (drive->dev_flags & IDE_DFLAG_SCSI)))
103 return -EPERM;
105 if ((arg >> IDE_NICE_DSC_OVERLAP) & 1)
106 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
107 else
108 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
110 if ((arg >> IDE_NICE_1) & 1)
111 drive->dev_flags |= IDE_DFLAG_NICE1;
112 else
113 drive->dev_flags &= ~IDE_DFLAG_NICE1;
115 return 0;
118 static int ide_cmd_ioctl(ide_drive_t *drive, unsigned cmd, unsigned long arg)
120 u8 *buf = NULL;
121 int bufsize = 0, err = 0;
122 u8 args[4], xfer_rate = 0;
123 ide_task_t tfargs;
124 struct ide_taskfile *tf = &tfargs.tf;
125 u16 *id = drive->id;
127 if (NULL == (void *) arg) {
128 struct request *rq;
130 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
131 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
132 err = blk_execute_rq(drive->queue, NULL, rq, 0);
133 blk_put_request(rq);
135 return err;
138 if (copy_from_user(args, (void __user *)arg, 4))
139 return -EFAULT;
141 memset(&tfargs, 0, sizeof(ide_task_t));
142 tf->feature = args[2];
143 if (args[0] == ATA_CMD_SMART) {
144 tf->nsect = args[3];
145 tf->lbal = args[1];
146 tf->lbam = 0x4f;
147 tf->lbah = 0xc2;
148 tfargs.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_IN_NSECT;
149 } else {
150 tf->nsect = args[1];
151 tfargs.tf_flags = IDE_TFLAG_OUT_FEATURE |
152 IDE_TFLAG_OUT_NSECT | IDE_TFLAG_IN_NSECT;
154 tf->command = args[0];
155 tfargs.data_phase = args[3] ? TASKFILE_IN : TASKFILE_NO_DATA;
157 if (args[3]) {
158 tfargs.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;
165 if (tf->command == ATA_CMD_SET_FEATURES &&
166 tf->feature == SETFEATURES_XFER &&
167 tf->nsect >= XFER_SW_DMA_0 &&
168 (id[ATA_ID_UDMA_MODES] ||
169 id[ATA_ID_MWDMA_MODES] ||
170 id[ATA_ID_SWDMA_MODES])) {
171 xfer_rate = args[1];
172 if (tf->nsect > XFER_UDMA_2 && !eighty_ninty_three(drive)) {
173 printk(KERN_WARNING "%s: UDMA speeds >UDMA33 cannot "
174 "be set\n", drive->name);
175 goto abort;
179 err = ide_raw_taskfile(drive, &tfargs, buf, args[3]);
181 args[0] = tf->status;
182 args[1] = tf->error;
183 args[2] = tf->nsect;
185 if (!err && xfer_rate) {
186 /* active-retuning-calls future */
187 ide_set_xfer_rate(drive, xfer_rate);
188 ide_driveid_update(drive);
190 abort:
191 if (copy_to_user((void __user *)arg, &args, 4))
192 err = -EFAULT;
193 if (buf) {
194 if (copy_to_user((void __user *)(arg + 4), buf, bufsize))
195 err = -EFAULT;
196 kfree(buf);
198 return err;
201 static int ide_task_ioctl(ide_drive_t *drive, unsigned cmd, unsigned long arg)
203 void __user *p = (void __user *)arg;
204 int err = 0;
205 u8 args[7];
206 ide_task_t task;
208 if (copy_from_user(args, p, 7))
209 return -EFAULT;
211 memset(&task, 0, sizeof(task));
212 memcpy(&task.tf_array[7], &args[1], 6);
213 task.tf.command = args[0];
214 task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
216 err = ide_no_data_taskfile(drive, &task);
218 args[0] = task.tf.command;
219 memcpy(&args[1], &task.tf_array[7], 6);
221 if (copy_to_user(p, args, 7))
222 err = -EFAULT;
224 return err;
227 static int generic_drive_reset(ide_drive_t *drive)
229 struct request *rq;
230 int ret = 0;
232 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
233 rq->cmd_type = REQ_TYPE_SPECIAL;
234 rq->cmd_len = 1;
235 rq->cmd[0] = REQ_DRIVE_RESET;
236 rq->cmd_flags |= REQ_SOFTBARRIER;
237 if (blk_execute_rq(drive->queue, NULL, rq, 1))
238 ret = rq->errors;
239 blk_put_request(rq);
240 return ret;
243 int generic_ide_ioctl(ide_drive_t *drive, struct block_device *bdev,
244 unsigned int cmd, unsigned long arg)
246 int err;
248 err = ide_setting_ioctl(drive, bdev, cmd, arg, ide_ioctl_settings);
249 if (err != -EOPNOTSUPP)
250 return err;
252 switch (cmd) {
253 case HDIO_OBSOLETE_IDENTITY:
254 case HDIO_GET_IDENTITY:
255 if (bdev != bdev->bd_contains)
256 return -EINVAL;
257 return ide_get_identity_ioctl(drive, cmd, arg);
258 case HDIO_GET_NICE:
259 return ide_get_nice_ioctl(drive, arg);
260 case HDIO_SET_NICE:
261 if (!capable(CAP_SYS_ADMIN))
262 return -EACCES;
263 return ide_set_nice_ioctl(drive, arg);
264 #ifdef CONFIG_IDE_TASK_IOCTL
265 case HDIO_DRIVE_TASKFILE:
266 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
267 return -EACCES;
268 if (drive->media == ide_disk)
269 return ide_taskfile_ioctl(drive, cmd, arg);
270 return -ENOMSG;
271 #endif
272 case HDIO_DRIVE_CMD:
273 if (!capable(CAP_SYS_RAWIO))
274 return -EACCES;
275 return ide_cmd_ioctl(drive, cmd, arg);
276 case HDIO_DRIVE_TASK:
277 if (!capable(CAP_SYS_RAWIO))
278 return -EACCES;
279 return ide_task_ioctl(drive, cmd, arg);
280 case HDIO_DRIVE_RESET:
281 if (!capable(CAP_SYS_ADMIN))
282 return -EACCES;
283 return generic_drive_reset(drive);
284 case HDIO_GET_BUSSTATE:
285 if (!capable(CAP_SYS_ADMIN))
286 return -EACCES;
287 if (put_user(BUSSTATE_ON, (long __user *)arg))
288 return -EFAULT;
289 return 0;
290 case HDIO_SET_BUSSTATE:
291 if (!capable(CAP_SYS_ADMIN))
292 return -EACCES;
293 return -EOPNOTSUPP;
294 default:
295 return -EINVAL;
298 EXPORT_SYMBOL(generic_ide_ioctl);