2 * compat ioctls for control API
4 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /* this file included from control.c */
23 #include <linux/compat.h>
24 #include <linux/slab.h>
26 struct snd_ctl_elem_list32
{
32 unsigned char reserved
[50];
33 } /* don't set packed attribute here */;
35 static int snd_ctl_elem_list_compat(struct snd_card
*card
,
36 struct snd_ctl_elem_list32 __user
*data32
)
38 struct snd_ctl_elem_list __user
*data
;
42 data
= compat_alloc_user_space(sizeof(*data
));
44 /* offset, space, used, count */
45 if (copy_in_user(data
, data32
, 4 * sizeof(u32
)))
48 if (get_user(ptr
, &data32
->pids
) ||
49 put_user(compat_ptr(ptr
), &data
->pids
))
51 err
= snd_ctl_elem_list(card
, data
);
55 if (copy_in_user(data32
, data
, 4 * sizeof(u32
)))
61 * control element info
62 * it uses union, so the things are not easy..
65 struct snd_ctl_elem_info32
{
66 struct snd_ctl_elem_id id
; // the size of struct is same
89 unsigned char reserved
[128];
91 unsigned char reserved
[64];
92 } __attribute__((packed
));
94 static int snd_ctl_elem_info_compat(struct snd_ctl_file
*ctl
,
95 struct snd_ctl_elem_info32 __user
*data32
)
97 struct snd_ctl_elem_info
*data
;
100 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
106 if (copy_from_user(&data
->id
, &data32
->id
, sizeof(data
->id
)))
108 /* we need to copy the item index.
109 * hope this doesn't break anything..
111 if (get_user(data
->value
.enumerated
.item
, &data32
->value
.enumerated
.item
))
114 err
= snd_power_wait(ctl
->card
, SNDRV_CTL_POWER_D0
);
117 err
= snd_ctl_elem_info(ctl
, data
);
120 /* restore info to 32bit */
122 /* id, type, access, count */
123 if (copy_to_user(&data32
->id
, &data
->id
, sizeof(data
->id
)) ||
124 copy_to_user(&data32
->type
, &data
->type
, 3 * sizeof(u32
)))
126 if (put_user(data
->owner
, &data32
->owner
))
128 switch (data
->type
) {
129 case SNDRV_CTL_ELEM_TYPE_BOOLEAN
:
130 case SNDRV_CTL_ELEM_TYPE_INTEGER
:
131 if (put_user(data
->value
.integer
.min
, &data32
->value
.integer
.min
) ||
132 put_user(data
->value
.integer
.max
, &data32
->value
.integer
.max
) ||
133 put_user(data
->value
.integer
.step
, &data32
->value
.integer
.step
))
136 case SNDRV_CTL_ELEM_TYPE_INTEGER64
:
137 if (copy_to_user(&data32
->value
.integer64
,
138 &data
->value
.integer64
,
139 sizeof(data
->value
.integer64
)))
142 case SNDRV_CTL_ELEM_TYPE_ENUMERATED
:
143 if (copy_to_user(&data32
->value
.enumerated
,
144 &data
->value
.enumerated
,
145 sizeof(data
->value
.enumerated
)))
158 struct snd_ctl_elem_value32
{
159 struct snd_ctl_elem_id id
;
160 unsigned int indirect
; /* bit-field causes misalignment */
163 unsigned char data
[512];
164 #ifndef CONFIG_X86_64
168 unsigned char reserved
[128];
171 #ifdef CONFIG_X86_X32
172 /* x32 has a different alignment for 64bit values from ia32 */
173 struct snd_ctl_elem_value_x32
{
174 struct snd_ctl_elem_id id
;
175 unsigned int indirect
; /* bit-field causes misalignment */
178 unsigned char data
[512];
181 unsigned char reserved
[128];
183 #endif /* CONFIG_X86_X32 */
185 /* get the value type and count of the control */
186 static int get_ctl_type(struct snd_card
*card
, struct snd_ctl_elem_id
*id
,
189 struct snd_kcontrol
*kctl
;
190 struct snd_ctl_elem_info
*info
;
193 down_read(&card
->controls_rwsem
);
194 kctl
= snd_ctl_find_id(card
, id
);
196 up_read(&card
->controls_rwsem
);
199 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
201 up_read(&card
->controls_rwsem
);
205 err
= kctl
->info(kctl
, info
);
206 up_read(&card
->controls_rwsem
);
209 *countp
= info
->count
;
215 static int get_elem_size(int type
, int count
)
218 case SNDRV_CTL_ELEM_TYPE_INTEGER64
:
219 return sizeof(s64
) * count
;
220 case SNDRV_CTL_ELEM_TYPE_ENUMERATED
:
221 return sizeof(int) * count
;
222 case SNDRV_CTL_ELEM_TYPE_BYTES
:
224 case SNDRV_CTL_ELEM_TYPE_IEC958
:
225 return sizeof(struct snd_aes_iec958
);
231 static int copy_ctl_value_from_user(struct snd_card
*card
,
232 struct snd_ctl_elem_value
*data
,
233 void __user
*userdata
,
235 int *typep
, int *countp
)
237 struct snd_ctl_elem_value32 __user
*data32
= userdata
;
239 int uninitialized_var(count
);
240 unsigned int indirect
;
242 if (copy_from_user(&data
->id
, &data32
->id
, sizeof(data
->id
)))
244 if (get_user(indirect
, &data32
->indirect
))
248 type
= get_ctl_type(card
, &data
->id
, &count
);
252 if (type
== SNDRV_CTL_ELEM_TYPE_BOOLEAN
||
253 type
== SNDRV_CTL_ELEM_TYPE_INTEGER
) {
254 for (i
= 0; i
< count
; i
++) {
255 s32 __user
*intp
= valuep
;
257 if (get_user(val
, &intp
[i
]))
259 data
->value
.integer
.value
[i
] = val
;
262 size
= get_elem_size(type
, count
);
264 dev_err(card
->dev
, "snd_ioctl32_ctl_elem_value: unknown type %d\n", type
);
267 if (copy_from_user(data
->value
.bytes
.data
, valuep
, size
))
276 /* restore the value to 32bit */
277 static int copy_ctl_value_to_user(void __user
*userdata
,
279 struct snd_ctl_elem_value
*data
,
284 if (type
== SNDRV_CTL_ELEM_TYPE_BOOLEAN
||
285 type
== SNDRV_CTL_ELEM_TYPE_INTEGER
) {
286 for (i
= 0; i
< count
; i
++) {
287 s32 __user
*intp
= valuep
;
289 val
= data
->value
.integer
.value
[i
];
290 if (put_user(val
, &intp
[i
]))
294 size
= get_elem_size(type
, count
);
295 if (copy_to_user(valuep
, data
->value
.bytes
.data
, size
))
301 static int ctl_elem_read_user(struct snd_card
*card
,
302 void __user
*userdata
, void __user
*valuep
)
304 struct snd_ctl_elem_value
*data
;
305 int err
, type
, count
;
307 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
311 err
= copy_ctl_value_from_user(card
, data
, userdata
, valuep
,
316 err
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
319 err
= snd_ctl_elem_read(card
, data
);
322 err
= copy_ctl_value_to_user(userdata
, valuep
, data
, type
, count
);
328 static int ctl_elem_write_user(struct snd_ctl_file
*file
,
329 void __user
*userdata
, void __user
*valuep
)
331 struct snd_ctl_elem_value
*data
;
332 struct snd_card
*card
= file
->card
;
333 int err
, type
, count
;
335 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
339 err
= copy_ctl_value_from_user(card
, data
, userdata
, valuep
,
344 err
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
347 err
= snd_ctl_elem_write(card
, file
, data
);
350 err
= copy_ctl_value_to_user(userdata
, valuep
, data
, type
, count
);
356 static int snd_ctl_elem_read_user_compat(struct snd_card
*card
,
357 struct snd_ctl_elem_value32 __user
*data32
)
359 return ctl_elem_read_user(card
, data32
, &data32
->value
);
362 static int snd_ctl_elem_write_user_compat(struct snd_ctl_file
*file
,
363 struct snd_ctl_elem_value32 __user
*data32
)
365 return ctl_elem_write_user(file
, data32
, &data32
->value
);
368 #ifdef CONFIG_X86_X32
369 static int snd_ctl_elem_read_user_x32(struct snd_card
*card
,
370 struct snd_ctl_elem_value_x32 __user
*data32
)
372 return ctl_elem_read_user(card
, data32
, &data32
->value
);
375 static int snd_ctl_elem_write_user_x32(struct snd_ctl_file
*file
,
376 struct snd_ctl_elem_value_x32 __user
*data32
)
378 return ctl_elem_write_user(file
, data32
, &data32
->value
);
380 #endif /* CONFIG_X86_X32 */
382 /* add or replace a user control */
383 static int snd_ctl_elem_add_compat(struct snd_ctl_file
*file
,
384 struct snd_ctl_elem_info32 __user
*data32
,
387 struct snd_ctl_elem_info
*data
;
390 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
395 /* id, type, access, count */ \
396 if (copy_from_user(&data
->id
, &data32
->id
, sizeof(data
->id
)) ||
397 copy_from_user(&data
->type
, &data32
->type
, 3 * sizeof(u32
)))
399 if (get_user(data
->owner
, &data32
->owner
) ||
400 get_user(data
->type
, &data32
->type
))
402 switch (data
->type
) {
403 case SNDRV_CTL_ELEM_TYPE_BOOLEAN
:
404 case SNDRV_CTL_ELEM_TYPE_INTEGER
:
405 if (get_user(data
->value
.integer
.min
, &data32
->value
.integer
.min
) ||
406 get_user(data
->value
.integer
.max
, &data32
->value
.integer
.max
) ||
407 get_user(data
->value
.integer
.step
, &data32
->value
.integer
.step
))
410 case SNDRV_CTL_ELEM_TYPE_INTEGER64
:
411 if (copy_from_user(&data
->value
.integer64
,
412 &data32
->value
.integer64
,
413 sizeof(data
->value
.integer64
)))
416 case SNDRV_CTL_ELEM_TYPE_ENUMERATED
:
417 if (copy_from_user(&data
->value
.enumerated
,
418 &data32
->value
.enumerated
,
419 sizeof(data
->value
.enumerated
)))
421 data
->value
.enumerated
.names_ptr
=
422 (uintptr_t)compat_ptr(data
->value
.enumerated
.names_ptr
);
427 err
= snd_ctl_elem_add(file
, data
, replace
);
434 SNDRV_CTL_IOCTL_ELEM_LIST32
= _IOWR('U', 0x10, struct snd_ctl_elem_list32
),
435 SNDRV_CTL_IOCTL_ELEM_INFO32
= _IOWR('U', 0x11, struct snd_ctl_elem_info32
),
436 SNDRV_CTL_IOCTL_ELEM_READ32
= _IOWR('U', 0x12, struct snd_ctl_elem_value32
),
437 SNDRV_CTL_IOCTL_ELEM_WRITE32
= _IOWR('U', 0x13, struct snd_ctl_elem_value32
),
438 SNDRV_CTL_IOCTL_ELEM_ADD32
= _IOWR('U', 0x17, struct snd_ctl_elem_info32
),
439 SNDRV_CTL_IOCTL_ELEM_REPLACE32
= _IOWR('U', 0x18, struct snd_ctl_elem_info32
),
440 #ifdef CONFIG_X86_X32
441 SNDRV_CTL_IOCTL_ELEM_READ_X32
= _IOWR('U', 0x12, struct snd_ctl_elem_value_x32
),
442 SNDRV_CTL_IOCTL_ELEM_WRITE_X32
= _IOWR('U', 0x13, struct snd_ctl_elem_value_x32
),
443 #endif /* CONFIG_X86_X32 */
446 static inline long snd_ctl_ioctl_compat(struct file
*file
, unsigned int cmd
, unsigned long arg
)
448 struct snd_ctl_file
*ctl
;
449 struct snd_kctl_ioctl
*p
;
450 void __user
*argp
= compat_ptr(arg
);
453 ctl
= file
->private_data
;
454 if (snd_BUG_ON(!ctl
|| !ctl
->card
))
458 case SNDRV_CTL_IOCTL_PVERSION
:
459 case SNDRV_CTL_IOCTL_CARD_INFO
:
460 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS
:
461 case SNDRV_CTL_IOCTL_POWER
:
462 case SNDRV_CTL_IOCTL_POWER_STATE
:
463 case SNDRV_CTL_IOCTL_ELEM_LOCK
:
464 case SNDRV_CTL_IOCTL_ELEM_UNLOCK
:
465 case SNDRV_CTL_IOCTL_ELEM_REMOVE
:
466 case SNDRV_CTL_IOCTL_TLV_READ
:
467 case SNDRV_CTL_IOCTL_TLV_WRITE
:
468 case SNDRV_CTL_IOCTL_TLV_COMMAND
:
469 return snd_ctl_ioctl(file
, cmd
, (unsigned long)argp
);
470 case SNDRV_CTL_IOCTL_ELEM_LIST32
:
471 return snd_ctl_elem_list_compat(ctl
->card
, argp
);
472 case SNDRV_CTL_IOCTL_ELEM_INFO32
:
473 return snd_ctl_elem_info_compat(ctl
, argp
);
474 case SNDRV_CTL_IOCTL_ELEM_READ32
:
475 return snd_ctl_elem_read_user_compat(ctl
->card
, argp
);
476 case SNDRV_CTL_IOCTL_ELEM_WRITE32
:
477 return snd_ctl_elem_write_user_compat(ctl
, argp
);
478 case SNDRV_CTL_IOCTL_ELEM_ADD32
:
479 return snd_ctl_elem_add_compat(ctl
, argp
, 0);
480 case SNDRV_CTL_IOCTL_ELEM_REPLACE32
:
481 return snd_ctl_elem_add_compat(ctl
, argp
, 1);
482 #ifdef CONFIG_X86_X32
483 case SNDRV_CTL_IOCTL_ELEM_READ_X32
:
484 return snd_ctl_elem_read_user_x32(ctl
->card
, argp
);
485 case SNDRV_CTL_IOCTL_ELEM_WRITE_X32
:
486 return snd_ctl_elem_write_user_x32(ctl
, argp
);
487 #endif /* CONFIG_X86_X32 */
490 down_read(&snd_ioctl_rwsem
);
491 list_for_each_entry(p
, &snd_control_compat_ioctls
, list
) {
493 err
= p
->fioctl(ctl
->card
, ctl
, cmd
, arg
);
494 if (err
!= -ENOIOCTLCMD
) {
495 up_read(&snd_ioctl_rwsem
);
500 up_read(&snd_ioctl_rwsem
);