5 #include <linux/hdreg.h>
7 #include <linux/slab.h>
9 static const struct ide_ioctl_devset ide_ioctl_settings
[] = {
10 { HDIO_GET_32BIT
, HDIO_SET_32BIT
, &ide_devset_io_32bit
},
11 { HDIO_GET_KEEPSETTINGS
, HDIO_SET_KEEPSETTINGS
, &ide_devset_keepsettings
},
12 { HDIO_GET_UNMASKINTR
, HDIO_SET_UNMASKINTR
, &ide_devset_unmaskirq
},
13 { HDIO_GET_DMA
, HDIO_SET_DMA
, &ide_devset_using_dma
},
14 { -1, HDIO_SET_PIO_MODE
, &ide_devset_pio_mode
},
18 int ide_setting_ioctl(ide_drive_t
*drive
, struct block_device
*bdev
,
19 unsigned int cmd
, unsigned long arg
,
20 const struct ide_ioctl_devset
*s
)
22 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
);
37 mutex_unlock(&ide_setting_mtx
);
38 return err
>= 0 ? put_user(err
, (long __user
*)arg
) : err
;
41 if (bdev
!= bdev
->bd_contains
)
44 if (!capable(CAP_SYS_ADMIN
))
47 mutex_lock(&ide_setting_mtx
);
48 err
= ide_devset_execute(drive
, ds
, arg
);
49 mutex_unlock(&ide_setting_mtx
);
54 EXPORT_SYMBOL_GPL(ide_setting_ioctl
);
56 static int ide_get_identity_ioctl(ide_drive_t
*drive
, unsigned int cmd
,
60 int size
= (cmd
== HDIO_GET_IDENTITY
) ? (ATA_ID_WORDS
* 2) : 142;
63 if ((drive
->dev_flags
& IDE_DFLAG_ID_READ
) == 0) {
68 /* ata_id_to_hd_driveid() relies on 'id' to be fully allocated. */
69 id
= kmalloc(ATA_ID_WORDS
* 2, 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
->dev_flags
& IDE_DFLAG_DSC_OVERLAP
)
89 << IDE_NICE_DSC_OVERLAP
) |
90 (!!(drive
->dev_flags
& IDE_DFLAG_NICE1
)
91 << IDE_NICE_1
), (long __user
*)arg
);
94 static int ide_set_nice_ioctl(ide_drive_t
*drive
, unsigned long arg
)
96 if (arg
!= (arg
& ((1 << IDE_NICE_DSC_OVERLAP
) | (1 << IDE_NICE_1
))))
99 if (((arg
>> IDE_NICE_DSC_OVERLAP
) & 1) &&
100 (drive
->media
!= ide_tape
))
103 if ((arg
>> IDE_NICE_DSC_OVERLAP
) & 1)
104 drive
->dev_flags
|= IDE_DFLAG_DSC_OVERLAP
;
106 drive
->dev_flags
&= ~IDE_DFLAG_DSC_OVERLAP
;
108 if ((arg
>> IDE_NICE_1
) & 1)
109 drive
->dev_flags
|= IDE_DFLAG_NICE1
;
111 drive
->dev_flags
&= ~IDE_DFLAG_NICE1
;
116 static int ide_cmd_ioctl(ide_drive_t
*drive
, unsigned long arg
)
119 int bufsize
= 0, err
= 0;
120 u8 args
[4], xfer_rate
= 0;
122 struct ide_taskfile
*tf
= &cmd
.tf
;
124 if (NULL
== (void *) arg
) {
127 rq
= blk_get_request(drive
->queue
, READ
, __GFP_WAIT
);
128 rq
->cmd_type
= REQ_TYPE_ATA_TASKFILE
;
129 err
= blk_execute_rq(drive
->queue
, NULL
, rq
, 0);
135 if (copy_from_user(args
, (void __user
*)arg
, 4))
138 memset(&cmd
, 0, sizeof(cmd
));
139 tf
->feature
= args
[2];
140 if (args
[0] == ATA_CMD_SMART
) {
145 cmd
.valid
.out
.tf
= IDE_VALID_OUT_TF
;
146 cmd
.valid
.in
.tf
= IDE_VALID_NSECT
;
149 cmd
.valid
.out
.tf
= IDE_VALID_FEATURE
| IDE_VALID_NSECT
;
150 cmd
.valid
.in
.tf
= IDE_VALID_NSECT
;
152 tf
->command
= args
[0];
153 cmd
.protocol
= args
[3] ? ATA_PROT_PIO
: ATA_PROT_NODATA
;
156 cmd
.tf_flags
|= IDE_TFLAG_IO_16BIT
;
157 bufsize
= SECTOR_SIZE
* args
[3];
158 buf
= kzalloc(bufsize
, GFP_KERNEL
);
163 if (tf
->command
== ATA_CMD_SET_FEATURES
&&
164 tf
->feature
== SETFEATURES_XFER
&&
165 tf
->nsect
>= XFER_SW_DMA_0
) {
166 xfer_rate
= ide_find_dma_mode(drive
, tf
->nsect
);
167 if (xfer_rate
!= tf
->nsect
) {
172 cmd
.tf_flags
|= IDE_TFLAG_SET_XFER
;
175 err
= ide_raw_taskfile(drive
, &cmd
, buf
, args
[3]);
177 args
[0] = tf
->status
;
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 long arg
)
193 void __user
*p
= (void __user
*)arg
;
198 if (copy_from_user(args
, p
, 7))
201 memset(&cmd
, 0, sizeof(cmd
));
202 memcpy(&cmd
.tf
.feature
, &args
[1], 6);
203 cmd
.tf
.command
= args
[0];
204 cmd
.valid
.out
.tf
= IDE_VALID_OUT_TF
| IDE_VALID_DEVICE
;
205 cmd
.valid
.in
.tf
= IDE_VALID_IN_TF
| IDE_VALID_DEVICE
;
207 err
= ide_no_data_taskfile(drive
, &cmd
);
209 args
[0] = cmd
.tf
.command
;
210 memcpy(&args
[1], &cmd
.tf
.feature
, 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 if (blk_execute_rq(drive
->queue
, NULL
, rq
, 1))
233 int generic_ide_ioctl(ide_drive_t
*drive
, struct block_device
*bdev
,
234 unsigned int cmd
, unsigned long arg
)
238 err
= ide_setting_ioctl(drive
, bdev
, cmd
, arg
, ide_ioctl_settings
);
239 if (err
!= -EOPNOTSUPP
)
243 case HDIO_OBSOLETE_IDENTITY
:
244 case HDIO_GET_IDENTITY
:
245 if (bdev
!= bdev
->bd_contains
)
247 return ide_get_identity_ioctl(drive
, cmd
, arg
);
249 return ide_get_nice_ioctl(drive
, arg
);
251 if (!capable(CAP_SYS_ADMIN
))
253 return ide_set_nice_ioctl(drive
, arg
);
254 #ifdef CONFIG_IDE_TASK_IOCTL
255 case HDIO_DRIVE_TASKFILE
:
256 if (!capable(CAP_SYS_ADMIN
) || !capable(CAP_SYS_RAWIO
))
258 if (drive
->media
== ide_disk
)
259 return ide_taskfile_ioctl(drive
, arg
);
263 if (!capable(CAP_SYS_RAWIO
))
265 return ide_cmd_ioctl(drive
, arg
);
266 case HDIO_DRIVE_TASK
:
267 if (!capable(CAP_SYS_RAWIO
))
269 return ide_task_ioctl(drive
, arg
);
270 case HDIO_DRIVE_RESET
:
271 if (!capable(CAP_SYS_ADMIN
))
273 return generic_drive_reset(drive
);
274 case HDIO_GET_BUSSTATE
:
275 if (!capable(CAP_SYS_ADMIN
))
277 if (put_user(BUSSTATE_ON
, (long __user
*)arg
))
280 case HDIO_SET_BUSSTATE
:
281 if (!capable(CAP_SYS_ADMIN
))
288 EXPORT_SYMBOL(generic_ide_ioctl
);