5 #include <linux/hdreg.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
},
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 int err
= -EOPNOTSUPP
;
24 for (; (ds
= s
->setting
); s
++) {
25 if (ds
->get
&& s
->get_ioctl
== cmd
)
27 else if (ds
->set
&& s
->set_ioctl
== cmd
)
34 mutex_lock(&ide_setting_mtx
);
36 mutex_unlock(&ide_setting_mtx
);
37 return err
>= 0 ? put_user(err
, (long __user
*)arg
) : err
;
40 if (bdev
!= bdev
->bd_contains
)
43 if (!capable(CAP_SYS_ADMIN
))
46 mutex_lock(&ide_setting_mtx
);
47 err
= ide_devset_execute(drive
, ds
, arg
);
48 mutex_unlock(&ide_setting_mtx
);
53 EXPORT_SYMBOL_GPL(ide_setting_ioctl
);
55 static int ide_get_identity_ioctl(ide_drive_t
*drive
, unsigned int cmd
,
59 int size
= (cmd
== HDIO_GET_IDENTITY
) ? (ATA_ID_WORDS
* 2) : 142;
62 if ((drive
->dev_flags
& IDE_DFLAG_ID_READ
) == 0) {
67 /* ata_id_to_hd_driveid() relies on 'id' to be fully allocated. */
68 id
= kmalloc(ATA_ID_WORDS
* 2, GFP_KERNEL
);
74 memcpy(id
, drive
->id
, size
);
75 ata_id_to_hd_driveid(id
);
77 if (copy_to_user((void __user
*)arg
, id
, size
))
85 static int ide_get_nice_ioctl(ide_drive_t
*drive
, unsigned long arg
)
87 return put_user((!!(drive
->dev_flags
& IDE_DFLAG_DSC_OVERLAP
)
88 << IDE_NICE_DSC_OVERLAP
) |
89 (!!(drive
->dev_flags
& IDE_DFLAG_NICE1
)
90 << IDE_NICE_1
), (long __user
*)arg
);
93 static int ide_set_nice_ioctl(ide_drive_t
*drive
, unsigned long arg
)
95 if (arg
!= (arg
& ((1 << IDE_NICE_DSC_OVERLAP
) | (1 << IDE_NICE_1
))))
98 if (((arg
>> IDE_NICE_DSC_OVERLAP
) & 1) &&
99 (drive
->media
!= ide_tape
))
102 if ((arg
>> IDE_NICE_DSC_OVERLAP
) & 1)
103 drive
->dev_flags
|= IDE_DFLAG_DSC_OVERLAP
;
105 drive
->dev_flags
&= ~IDE_DFLAG_DSC_OVERLAP
;
107 if ((arg
>> IDE_NICE_1
) & 1)
108 drive
->dev_flags
|= IDE_DFLAG_NICE1
;
110 drive
->dev_flags
&= ~IDE_DFLAG_NICE1
;
115 static int ide_cmd_ioctl(ide_drive_t
*drive
, unsigned long arg
)
118 int bufsize
= 0, err
= 0;
119 u8 args
[4], xfer_rate
= 0;
121 struct ide_taskfile
*tf
= &cmd
.tf
;
123 if (NULL
== (void *) arg
) {
126 rq
= blk_get_request(drive
->queue
, READ
, __GFP_WAIT
);
127 rq
->cmd_type
= REQ_TYPE_ATA_TASKFILE
;
128 err
= blk_execute_rq(drive
->queue
, NULL
, rq
, 0);
134 if (copy_from_user(args
, (void __user
*)arg
, 4))
137 memset(&cmd
, 0, sizeof(cmd
));
138 tf
->feature
= args
[2];
139 if (args
[0] == ATA_CMD_SMART
) {
144 cmd
.valid
.out
.tf
= IDE_VALID_OUT_TF
;
145 cmd
.valid
.in
.tf
= IDE_VALID_NSECT
;
148 cmd
.valid
.out
.tf
= IDE_VALID_FEATURE
| IDE_VALID_NSECT
;
149 cmd
.valid
.in
.tf
= IDE_VALID_NSECT
;
151 tf
->command
= args
[0];
152 cmd
.protocol
= args
[3] ? ATA_PROT_PIO
: ATA_PROT_NODATA
;
155 cmd
.tf_flags
|= IDE_TFLAG_IO_16BIT
;
156 bufsize
= SECTOR_SIZE
* args
[3];
157 buf
= kzalloc(bufsize
, GFP_KERNEL
);
162 if (tf
->command
== ATA_CMD_SET_FEATURES
&&
163 tf
->feature
== SETFEATURES_XFER
&&
164 tf
->nsect
>= XFER_SW_DMA_0
) {
165 xfer_rate
= ide_find_dma_mode(drive
, XFER_UDMA_6
);
166 if (xfer_rate
!= tf
->nsect
) {
171 cmd
.tf_flags
|= IDE_TFLAG_SET_XFER
;
174 err
= ide_raw_taskfile(drive
, &cmd
, buf
, args
[3]);
176 args
[0] = tf
->status
;
180 if (copy_to_user((void __user
*)arg
, &args
, 4))
183 if (copy_to_user((void __user
*)(arg
+ 4), buf
, bufsize
))
190 static int ide_task_ioctl(ide_drive_t
*drive
, unsigned long arg
)
192 void __user
*p
= (void __user
*)arg
;
197 if (copy_from_user(args
, p
, 7))
200 memset(&cmd
, 0, sizeof(cmd
));
201 memcpy(&cmd
.tf
.feature
, &args
[1], 6);
202 cmd
.tf
.command
= args
[0];
203 cmd
.valid
.out
.tf
= IDE_VALID_OUT_TF
| IDE_VALID_DEVICE
;
204 cmd
.valid
.in
.tf
= IDE_VALID_IN_TF
| IDE_VALID_DEVICE
;
206 err
= ide_no_data_taskfile(drive
, &cmd
);
208 args
[0] = cmd
.tf
.command
;
209 memcpy(&args
[1], &cmd
.tf
.feature
, 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 if (blk_execute_rq(drive
->queue
, NULL
, rq
, 1))
232 int generic_ide_ioctl(ide_drive_t
*drive
, struct block_device
*bdev
,
233 unsigned int cmd
, unsigned long arg
)
237 err
= ide_setting_ioctl(drive
, bdev
, cmd
, arg
, ide_ioctl_settings
);
238 if (err
!= -EOPNOTSUPP
)
242 case HDIO_OBSOLETE_IDENTITY
:
243 case HDIO_GET_IDENTITY
:
244 if (bdev
!= bdev
->bd_contains
)
246 return ide_get_identity_ioctl(drive
, cmd
, arg
);
248 return ide_get_nice_ioctl(drive
, arg
);
250 if (!capable(CAP_SYS_ADMIN
))
252 return ide_set_nice_ioctl(drive
, arg
);
253 #ifdef CONFIG_IDE_TASK_IOCTL
254 case HDIO_DRIVE_TASKFILE
:
255 if (!capable(CAP_SYS_ADMIN
) || !capable(CAP_SYS_RAWIO
))
257 if (drive
->media
== ide_disk
)
258 return ide_taskfile_ioctl(drive
, arg
);
262 if (!capable(CAP_SYS_RAWIO
))
264 return ide_cmd_ioctl(drive
, arg
);
265 case HDIO_DRIVE_TASK
:
266 if (!capable(CAP_SYS_RAWIO
))
268 return ide_task_ioctl(drive
, arg
);
269 case HDIO_DRIVE_RESET
:
270 if (!capable(CAP_SYS_ADMIN
))
272 return generic_drive_reset(drive
);
273 case HDIO_GET_BUSSTATE
:
274 if (!capable(CAP_SYS_ADMIN
))
276 if (put_user(BUSSTATE_ON
, (long __user
*)arg
))
279 case HDIO_SET_BUSSTATE
:
280 if (!capable(CAP_SYS_ADMIN
))
287 EXPORT_SYMBOL(generic_ide_ioctl
);