1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * compat ioctls for control API
5 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
8 /* this file included from control.c */
10 #include <linux/compat.h>
11 #include <linux/slab.h>
13 struct snd_ctl_elem_list32 {
19 unsigned char reserved[50];
20 } /* don't set packed attribute here */;
22 static int snd_ctl_elem_list_compat(struct snd_card *card,
23 struct snd_ctl_elem_list32 __user *data32)
25 struct snd_ctl_elem_list __user *data;
29 data = compat_alloc_user_space(sizeof(*data));
31 /* offset, space, used, count */
32 if (copy_in_user(data, data32, 4 * sizeof(u32)))
35 if (get_user(ptr, &data32->pids) ||
36 put_user(compat_ptr(ptr), &data->pids))
38 err = snd_ctl_elem_list(card, data);
42 if (copy_in_user(data32, data, 4 * sizeof(u32)))
48 * control element info
49 * it uses union, so the things are not easy..
52 struct snd_ctl_elem_info32 {
53 struct snd_ctl_elem_id id; // the size of struct is same
76 unsigned char reserved[128];
78 unsigned char reserved[64];
79 } __attribute__((packed));
81 static int snd_ctl_elem_info_compat(struct snd_ctl_file *ctl,
82 struct snd_ctl_elem_info32 __user *data32)
84 struct snd_ctl_elem_info *data;
87 data = kzalloc(sizeof(*data), GFP_KERNEL);
93 if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
95 /* we need to copy the item index.
96 * hope this doesn't break anything..
98 if (get_user(data->value.enumerated.item, &data32->value.enumerated.item))
101 err = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
104 err = snd_ctl_elem_info(ctl, data);
107 /* restore info to 32bit */
109 /* id, type, access, count */
110 if (copy_to_user(&data32->id, &data->id, sizeof(data->id)) ||
111 copy_to_user(&data32->type, &data->type, 3 * sizeof(u32)))
113 if (put_user(data->owner, &data32->owner))
115 switch (data->type) {
116 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
117 case SNDRV_CTL_ELEM_TYPE_INTEGER:
118 if (put_user(data->value.integer.min, &data32->value.integer.min) ||
119 put_user(data->value.integer.max, &data32->value.integer.max) ||
120 put_user(data->value.integer.step, &data32->value.integer.step))
123 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
124 if (copy_to_user(&data32->value.integer64,
125 &data->value.integer64,
126 sizeof(data->value.integer64)))
129 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
130 if (copy_to_user(&data32->value.enumerated,
131 &data->value.enumerated,
132 sizeof(data->value.enumerated)))
145 struct snd_ctl_elem_value32 {
146 struct snd_ctl_elem_id id;
147 unsigned int indirect; /* bit-field causes misalignment */
150 unsigned char data[512];
151 #ifndef CONFIG_X86_64
155 unsigned char reserved[128];
158 #ifdef CONFIG_X86_X32
159 /* x32 has a different alignment for 64bit values from ia32 */
160 struct snd_ctl_elem_value_x32 {
161 struct snd_ctl_elem_id id;
162 unsigned int indirect; /* bit-field causes misalignment */
165 unsigned char data[512];
168 unsigned char reserved[128];
170 #endif /* CONFIG_X86_X32 */
172 /* get the value type and count of the control */
173 static int get_ctl_type(struct snd_card *card, struct snd_ctl_elem_id *id,
176 struct snd_kcontrol *kctl;
177 struct snd_ctl_elem_info *info;
180 down_read(&card->controls_rwsem);
181 kctl = snd_ctl_find_id(card, id);
183 up_read(&card->controls_rwsem);
186 info = kzalloc(sizeof(*info), GFP_KERNEL);
188 up_read(&card->controls_rwsem);
192 err = kctl->info(kctl, info);
193 up_read(&card->controls_rwsem);
196 *countp = info->count;
202 static int get_elem_size(int type, int count)
205 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
206 return sizeof(s64) * count;
207 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
208 return sizeof(int) * count;
209 case SNDRV_CTL_ELEM_TYPE_BYTES:
211 case SNDRV_CTL_ELEM_TYPE_IEC958:
212 return sizeof(struct snd_aes_iec958);
218 static int copy_ctl_value_from_user(struct snd_card *card,
219 struct snd_ctl_elem_value *data,
220 void __user *userdata,
222 int *typep, int *countp)
224 struct snd_ctl_elem_value32 __user *data32 = userdata;
227 unsigned int indirect;
229 if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
231 if (get_user(indirect, &data32->indirect))
235 type = get_ctl_type(card, &data->id, &count);
239 if (type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
240 type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
241 for (i = 0; i < count; i++) {
242 s32 __user *intp = valuep;
244 if (get_user(val, &intp[i]))
246 data->value.integer.value[i] = val;
249 size = get_elem_size(type, count);
251 dev_err(card->dev, "snd_ioctl32_ctl_elem_value: unknown type %d\n", type);
254 if (copy_from_user(data->value.bytes.data, valuep, size))
263 /* restore the value to 32bit */
264 static int copy_ctl_value_to_user(void __user *userdata,
266 struct snd_ctl_elem_value *data,
269 struct snd_ctl_elem_value32 __user *data32 = userdata;
272 if (type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
273 type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
274 for (i = 0; i < count; i++) {
275 s32 __user *intp = valuep;
277 val = data->value.integer.value[i];
278 if (put_user(val, &intp[i]))
282 size = get_elem_size(type, count);
283 if (copy_to_user(valuep, data->value.bytes.data, size))
286 if (copy_to_user(&data32->id, &data->id, sizeof(data32->id)))
291 static int ctl_elem_read_user(struct snd_card *card,
292 void __user *userdata, void __user *valuep)
294 struct snd_ctl_elem_value *data;
295 int err, type, count;
297 data = kzalloc(sizeof(*data), GFP_KERNEL);
301 err = copy_ctl_value_from_user(card, data, userdata, valuep,
306 err = snd_power_wait(card, SNDRV_CTL_POWER_D0);
309 down_read(&card->controls_rwsem);
310 err = snd_ctl_elem_read(card, data);
311 up_read(&card->controls_rwsem);
314 err = copy_ctl_value_to_user(userdata, valuep, data, type, count);
320 static int ctl_elem_write_user(struct snd_ctl_file *file,
321 void __user *userdata, void __user *valuep)
323 struct snd_ctl_elem_value *data;
324 struct snd_card *card = file->card;
325 int err, type, count;
327 data = kzalloc(sizeof(*data), GFP_KERNEL);
331 err = copy_ctl_value_from_user(card, data, userdata, valuep,
336 err = snd_power_wait(card, SNDRV_CTL_POWER_D0);
339 down_write(&card->controls_rwsem);
340 err = snd_ctl_elem_write(card, file, data);
341 up_write(&card->controls_rwsem);
344 err = copy_ctl_value_to_user(userdata, valuep, data, type, count);
350 static int snd_ctl_elem_read_user_compat(struct snd_card *card,
351 struct snd_ctl_elem_value32 __user *data32)
353 return ctl_elem_read_user(card, data32, &data32->value);
356 static int snd_ctl_elem_write_user_compat(struct snd_ctl_file *file,
357 struct snd_ctl_elem_value32 __user *data32)
359 return ctl_elem_write_user(file, data32, &data32->value);
362 #ifdef CONFIG_X86_X32
363 static int snd_ctl_elem_read_user_x32(struct snd_card *card,
364 struct snd_ctl_elem_value_x32 __user *data32)
366 return ctl_elem_read_user(card, data32, &data32->value);
369 static int snd_ctl_elem_write_user_x32(struct snd_ctl_file *file,
370 struct snd_ctl_elem_value_x32 __user *data32)
372 return ctl_elem_write_user(file, data32, &data32->value);
374 #endif /* CONFIG_X86_X32 */
376 /* add or replace a user control */
377 static int snd_ctl_elem_add_compat(struct snd_ctl_file *file,
378 struct snd_ctl_elem_info32 __user *data32,
381 struct snd_ctl_elem_info *data;
384 data = kzalloc(sizeof(*data), GFP_KERNEL);
389 /* id, type, access, count */ \
390 if (copy_from_user(&data->id, &data32->id, sizeof(data->id)) ||
391 copy_from_user(&data->type, &data32->type, 3 * sizeof(u32)))
393 if (get_user(data->owner, &data32->owner))
395 switch (data->type) {
396 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
397 case SNDRV_CTL_ELEM_TYPE_INTEGER:
398 if (get_user(data->value.integer.min, &data32->value.integer.min) ||
399 get_user(data->value.integer.max, &data32->value.integer.max) ||
400 get_user(data->value.integer.step, &data32->value.integer.step))
403 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
404 if (copy_from_user(&data->value.integer64,
405 &data32->value.integer64,
406 sizeof(data->value.integer64)))
409 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
410 if (copy_from_user(&data->value.enumerated,
411 &data32->value.enumerated,
412 sizeof(data->value.enumerated)))
414 data->value.enumerated.names_ptr =
415 (uintptr_t)compat_ptr(data->value.enumerated.names_ptr);
420 err = snd_ctl_elem_add(file, data, replace);
427 SNDRV_CTL_IOCTL_ELEM_LIST32 = _IOWR('U', 0x10, struct snd_ctl_elem_list32),
428 SNDRV_CTL_IOCTL_ELEM_INFO32 = _IOWR('U', 0x11, struct snd_ctl_elem_info32),
429 SNDRV_CTL_IOCTL_ELEM_READ32 = _IOWR('U', 0x12, struct snd_ctl_elem_value32),
430 SNDRV_CTL_IOCTL_ELEM_WRITE32 = _IOWR('U', 0x13, struct snd_ctl_elem_value32),
431 SNDRV_CTL_IOCTL_ELEM_ADD32 = _IOWR('U', 0x17, struct snd_ctl_elem_info32),
432 SNDRV_CTL_IOCTL_ELEM_REPLACE32 = _IOWR('U', 0x18, struct snd_ctl_elem_info32),
433 #ifdef CONFIG_X86_X32
434 SNDRV_CTL_IOCTL_ELEM_READ_X32 = _IOWR('U', 0x12, struct snd_ctl_elem_value_x32),
435 SNDRV_CTL_IOCTL_ELEM_WRITE_X32 = _IOWR('U', 0x13, struct snd_ctl_elem_value_x32),
436 #endif /* CONFIG_X86_X32 */
439 static inline long snd_ctl_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
441 struct snd_ctl_file *ctl;
442 struct snd_kctl_ioctl *p;
443 void __user *argp = compat_ptr(arg);
446 ctl = file->private_data;
447 if (snd_BUG_ON(!ctl || !ctl->card))
451 case SNDRV_CTL_IOCTL_PVERSION:
452 case SNDRV_CTL_IOCTL_CARD_INFO:
453 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
454 case SNDRV_CTL_IOCTL_POWER:
455 case SNDRV_CTL_IOCTL_POWER_STATE:
456 case SNDRV_CTL_IOCTL_ELEM_LOCK:
457 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
458 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
459 case SNDRV_CTL_IOCTL_TLV_READ:
460 case SNDRV_CTL_IOCTL_TLV_WRITE:
461 case SNDRV_CTL_IOCTL_TLV_COMMAND:
462 return snd_ctl_ioctl(file, cmd, (unsigned long)argp);
463 case SNDRV_CTL_IOCTL_ELEM_LIST32:
464 return snd_ctl_elem_list_compat(ctl->card, argp);
465 case SNDRV_CTL_IOCTL_ELEM_INFO32:
466 return snd_ctl_elem_info_compat(ctl, argp);
467 case SNDRV_CTL_IOCTL_ELEM_READ32:
468 return snd_ctl_elem_read_user_compat(ctl->card, argp);
469 case SNDRV_CTL_IOCTL_ELEM_WRITE32:
470 return snd_ctl_elem_write_user_compat(ctl, argp);
471 case SNDRV_CTL_IOCTL_ELEM_ADD32:
472 return snd_ctl_elem_add_compat(ctl, argp, 0);
473 case SNDRV_CTL_IOCTL_ELEM_REPLACE32:
474 return snd_ctl_elem_add_compat(ctl, argp, 1);
475 #ifdef CONFIG_X86_X32
476 case SNDRV_CTL_IOCTL_ELEM_READ_X32:
477 return snd_ctl_elem_read_user_x32(ctl->card, argp);
478 case SNDRV_CTL_IOCTL_ELEM_WRITE_X32:
479 return snd_ctl_elem_write_user_x32(ctl, argp);
480 #endif /* CONFIG_X86_X32 */
483 down_read(&snd_ioctl_rwsem);
484 list_for_each_entry(p, &snd_control_compat_ioctls, list) {
486 err = p->fioctl(ctl->card, ctl, cmd, arg);
487 if (err != -ENOIOCTLCMD) {
488 up_read(&snd_ioctl_rwsem);
493 up_read(&snd_ioctl_rwsem);