Loading...
1/*
2 * compat ioctls for control API
3 *
4 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
5 *
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.
10 *
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.
15 *
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
19 */
20
21/* this file included from control.c */
22
23#include <linux/compat.h>
24#include <linux/slab.h>
25
26struct snd_ctl_elem_list32 {
27 u32 offset;
28 u32 space;
29 u32 used;
30 u32 count;
31 u32 pids;
32 unsigned char reserved[50];
33} /* don't set packed attribute here */;
34
35static int snd_ctl_elem_list_compat(struct snd_card *card,
36 struct snd_ctl_elem_list32 __user *data32)
37{
38 struct snd_ctl_elem_list __user *data;
39 compat_caddr_t ptr;
40 int err;
41
42 data = compat_alloc_user_space(sizeof(*data));
43
44 /* offset, space, used, count */
45 if (copy_in_user(data, data32, 4 * sizeof(u32)))
46 return -EFAULT;
47 /* pids */
48 if (get_user(ptr, &data32->pids) ||
49 put_user(compat_ptr(ptr), &data->pids))
50 return -EFAULT;
51 err = snd_ctl_elem_list(card, data);
52 if (err < 0)
53 return err;
54 /* copy the result */
55 if (copy_in_user(data32, data, 4 * sizeof(u32)))
56 return -EFAULT;
57 return 0;
58}
59
60/*
61 * control element info
62 * it uses union, so the things are not easy..
63 */
64
65struct snd_ctl_elem_info32 {
66 struct snd_ctl_elem_id id; // the size of struct is same
67 s32 type;
68 u32 access;
69 u32 count;
70 s32 owner;
71 union {
72 struct {
73 s32 min;
74 s32 max;
75 s32 step;
76 } integer;
77 struct {
78 u64 min;
79 u64 max;
80 u64 step;
81 } integer64;
82 struct {
83 u32 items;
84 u32 item;
85 char name[64];
86 u64 names_ptr;
87 u32 names_length;
88 } enumerated;
89 unsigned char reserved[128];
90 } value;
91 unsigned char reserved[64];
92} __attribute__((packed));
93
94static int snd_ctl_elem_info_compat(struct snd_ctl_file *ctl,
95 struct snd_ctl_elem_info32 __user *data32)
96{
97 struct snd_ctl_elem_info *data;
98 int err;
99
100 data = kzalloc(sizeof(*data), GFP_KERNEL);
101 if (! data)
102 return -ENOMEM;
103
104 err = -EFAULT;
105 /* copy id */
106 if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
107 goto error;
108 /* we need to copy the item index.
109 * hope this doesn't break anything..
110 */
111 if (get_user(data->value.enumerated.item, &data32->value.enumerated.item))
112 goto error;
113
114 snd_power_lock(ctl->card);
115 err = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
116 if (err >= 0)
117 err = snd_ctl_elem_info(ctl, data);
118 snd_power_unlock(ctl->card);
119
120 if (err < 0)
121 goto error;
122 /* restore info to 32bit */
123 err = -EFAULT;
124 /* id, type, access, count */
125 if (copy_to_user(&data32->id, &data->id, sizeof(data->id)) ||
126 copy_to_user(&data32->type, &data->type, 3 * sizeof(u32)))
127 goto error;
128 if (put_user(data->owner, &data32->owner))
129 goto error;
130 switch (data->type) {
131 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
132 case SNDRV_CTL_ELEM_TYPE_INTEGER:
133 if (put_user(data->value.integer.min, &data32->value.integer.min) ||
134 put_user(data->value.integer.max, &data32->value.integer.max) ||
135 put_user(data->value.integer.step, &data32->value.integer.step))
136 goto error;
137 break;
138 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
139 if (copy_to_user(&data32->value.integer64,
140 &data->value.integer64,
141 sizeof(data->value.integer64)))
142 goto error;
143 break;
144 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
145 if (copy_to_user(&data32->value.enumerated,
146 &data->value.enumerated,
147 sizeof(data->value.enumerated)))
148 goto error;
149 break;
150 default:
151 break;
152 }
153 err = 0;
154 error:
155 kfree(data);
156 return err;
157}
158
159/* read / write */
160struct snd_ctl_elem_value32 {
161 struct snd_ctl_elem_id id;
162 unsigned int indirect; /* bit-field causes misalignment */
163 union {
164 s32 integer[128];
165 unsigned char data[512];
166#ifndef CONFIG_X86_64
167 s64 integer64[64];
168#endif
169 } value;
170 unsigned char reserved[128];
171};
172
173#ifdef CONFIG_X86_X32
174/* x32 has a different alignment for 64bit values from ia32 */
175struct snd_ctl_elem_value_x32 {
176 struct snd_ctl_elem_id id;
177 unsigned int indirect; /* bit-field causes misalignment */
178 union {
179 s32 integer[128];
180 unsigned char data[512];
181 s64 integer64[64];
182 } value;
183 unsigned char reserved[128];
184};
185#endif /* CONFIG_X86_X32 */
186
187/* get the value type and count of the control */
188static int get_ctl_type(struct snd_card *card, struct snd_ctl_elem_id *id,
189 int *countp)
190{
191 struct snd_kcontrol *kctl;
192 struct snd_ctl_elem_info *info;
193 int err;
194
195 down_read(&card->controls_rwsem);
196 kctl = snd_ctl_find_id(card, id);
197 if (! kctl) {
198 up_read(&card->controls_rwsem);
199 return -ENOENT;
200 }
201 info = kzalloc(sizeof(*info), GFP_KERNEL);
202 if (info == NULL) {
203 up_read(&card->controls_rwsem);
204 return -ENOMEM;
205 }
206 info->id = *id;
207 err = kctl->info(kctl, info);
208 up_read(&card->controls_rwsem);
209 if (err >= 0) {
210 err = info->type;
211 *countp = info->count;
212 }
213 kfree(info);
214 return err;
215}
216
217static int get_elem_size(int type, int count)
218{
219 switch (type) {
220 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
221 return sizeof(s64) * count;
222 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
223 return sizeof(int) * count;
224 case SNDRV_CTL_ELEM_TYPE_BYTES:
225 return 512;
226 case SNDRV_CTL_ELEM_TYPE_IEC958:
227 return sizeof(struct snd_aes_iec958);
228 default:
229 return -1;
230 }
231}
232
233static int copy_ctl_value_from_user(struct snd_card *card,
234 struct snd_ctl_elem_value *data,
235 void __user *userdata,
236 void __user *valuep,
237 int *typep, int *countp)
238{
239 struct snd_ctl_elem_value32 __user *data32 = userdata;
240 int i, type, size;
241 int uninitialized_var(count);
242 unsigned int indirect;
243
244 if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
245 return -EFAULT;
246 if (get_user(indirect, &data32->indirect))
247 return -EFAULT;
248 if (indirect)
249 return -EINVAL;
250 type = get_ctl_type(card, &data->id, &count);
251 if (type < 0)
252 return type;
253
254 if (type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
255 type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
256 for (i = 0; i < count; i++) {
257 s32 __user *intp = valuep;
258 int val;
259 if (get_user(val, &intp[i]))
260 return -EFAULT;
261 data->value.integer.value[i] = val;
262 }
263 } else {
264 size = get_elem_size(type, count);
265 if (size < 0) {
266 dev_err(card->dev, "snd_ioctl32_ctl_elem_value: unknown type %d\n", type);
267 return -EINVAL;
268 }
269 if (copy_from_user(data->value.bytes.data, valuep, size))
270 return -EFAULT;
271 }
272
273 *typep = type;
274 *countp = count;
275 return 0;
276}
277
278/* restore the value to 32bit */
279static int copy_ctl_value_to_user(void __user *userdata,
280 void __user *valuep,
281 struct snd_ctl_elem_value *data,
282 int type, int count)
283{
284 int i, size;
285
286 if (type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
287 type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
288 for (i = 0; i < count; i++) {
289 s32 __user *intp = valuep;
290 int val;
291 val = data->value.integer.value[i];
292 if (put_user(val, &intp[i]))
293 return -EFAULT;
294 }
295 } else {
296 size = get_elem_size(type, count);
297 if (copy_to_user(valuep, data->value.bytes.data, size))
298 return -EFAULT;
299 }
300 return 0;
301}
302
303static int ctl_elem_read_user(struct snd_card *card,
304 void __user *userdata, void __user *valuep)
305{
306 struct snd_ctl_elem_value *data;
307 int err, type, count;
308
309 data = kzalloc(sizeof(*data), GFP_KERNEL);
310 if (data == NULL)
311 return -ENOMEM;
312
313 err = copy_ctl_value_from_user(card, data, userdata, valuep,
314 &type, &count);
315 if (err < 0)
316 goto error;
317
318 snd_power_lock(card);
319 err = snd_power_wait(card, SNDRV_CTL_POWER_D0);
320 if (err >= 0)
321 err = snd_ctl_elem_read(card, data);
322 snd_power_unlock(card);
323 if (err >= 0)
324 err = copy_ctl_value_to_user(userdata, valuep, data,
325 type, count);
326 error:
327 kfree(data);
328 return err;
329}
330
331static int ctl_elem_write_user(struct snd_ctl_file *file,
332 void __user *userdata, void __user *valuep)
333{
334 struct snd_ctl_elem_value *data;
335 struct snd_card *card = file->card;
336 int err, type, count;
337
338 data = kzalloc(sizeof(*data), GFP_KERNEL);
339 if (data == NULL)
340 return -ENOMEM;
341
342 err = copy_ctl_value_from_user(card, data, userdata, valuep,
343 &type, &count);
344 if (err < 0)
345 goto error;
346
347 snd_power_lock(card);
348 err = snd_power_wait(card, SNDRV_CTL_POWER_D0);
349 if (err >= 0)
350 err = snd_ctl_elem_write(card, file, data);
351 snd_power_unlock(card);
352 if (err >= 0)
353 err = copy_ctl_value_to_user(userdata, valuep, data,
354 type, count);
355 error:
356 kfree(data);
357 return err;
358}
359
360static int snd_ctl_elem_read_user_compat(struct snd_card *card,
361 struct snd_ctl_elem_value32 __user *data32)
362{
363 return ctl_elem_read_user(card, data32, &data32->value);
364}
365
366static int snd_ctl_elem_write_user_compat(struct snd_ctl_file *file,
367 struct snd_ctl_elem_value32 __user *data32)
368{
369 return ctl_elem_write_user(file, data32, &data32->value);
370}
371
372#ifdef CONFIG_X86_X32
373static int snd_ctl_elem_read_user_x32(struct snd_card *card,
374 struct snd_ctl_elem_value_x32 __user *data32)
375{
376 return ctl_elem_read_user(card, data32, &data32->value);
377}
378
379static int snd_ctl_elem_write_user_x32(struct snd_ctl_file *file,
380 struct snd_ctl_elem_value_x32 __user *data32)
381{
382 return ctl_elem_write_user(file, data32, &data32->value);
383}
384#endif /* CONFIG_X86_X32 */
385
386/* add or replace a user control */
387static int snd_ctl_elem_add_compat(struct snd_ctl_file *file,
388 struct snd_ctl_elem_info32 __user *data32,
389 int replace)
390{
391 struct snd_ctl_elem_info *data;
392 int err;
393
394 data = kzalloc(sizeof(*data), GFP_KERNEL);
395 if (! data)
396 return -ENOMEM;
397
398 err = -EFAULT;
399 /* id, type, access, count */ \
400 if (copy_from_user(&data->id, &data32->id, sizeof(data->id)) ||
401 copy_from_user(&data->type, &data32->type, 3 * sizeof(u32)))
402 goto error;
403 if (get_user(data->owner, &data32->owner) ||
404 get_user(data->type, &data32->type))
405 goto error;
406 switch (data->type) {
407 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
408 case SNDRV_CTL_ELEM_TYPE_INTEGER:
409 if (get_user(data->value.integer.min, &data32->value.integer.min) ||
410 get_user(data->value.integer.max, &data32->value.integer.max) ||
411 get_user(data->value.integer.step, &data32->value.integer.step))
412 goto error;
413 break;
414 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
415 if (copy_from_user(&data->value.integer64,
416 &data32->value.integer64,
417 sizeof(data->value.integer64)))
418 goto error;
419 break;
420 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
421 if (copy_from_user(&data->value.enumerated,
422 &data32->value.enumerated,
423 sizeof(data->value.enumerated)))
424 goto error;
425 data->value.enumerated.names_ptr =
426 (uintptr_t)compat_ptr(data->value.enumerated.names_ptr);
427 break;
428 default:
429 break;
430 }
431 err = snd_ctl_elem_add(file, data, replace);
432 error:
433 kfree(data);
434 return err;
435}
436
437enum {
438 SNDRV_CTL_IOCTL_ELEM_LIST32 = _IOWR('U', 0x10, struct snd_ctl_elem_list32),
439 SNDRV_CTL_IOCTL_ELEM_INFO32 = _IOWR('U', 0x11, struct snd_ctl_elem_info32),
440 SNDRV_CTL_IOCTL_ELEM_READ32 = _IOWR('U', 0x12, struct snd_ctl_elem_value32),
441 SNDRV_CTL_IOCTL_ELEM_WRITE32 = _IOWR('U', 0x13, struct snd_ctl_elem_value32),
442 SNDRV_CTL_IOCTL_ELEM_ADD32 = _IOWR('U', 0x17, struct snd_ctl_elem_info32),
443 SNDRV_CTL_IOCTL_ELEM_REPLACE32 = _IOWR('U', 0x18, struct snd_ctl_elem_info32),
444#ifdef CONFIG_X86_X32
445 SNDRV_CTL_IOCTL_ELEM_READ_X32 = _IOWR('U', 0x12, struct snd_ctl_elem_value_x32),
446 SNDRV_CTL_IOCTL_ELEM_WRITE_X32 = _IOWR('U', 0x13, struct snd_ctl_elem_value_x32),
447#endif /* CONFIG_X86_X32 */
448};
449
450static inline long snd_ctl_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
451{
452 struct snd_ctl_file *ctl;
453 struct snd_kctl_ioctl *p;
454 void __user *argp = compat_ptr(arg);
455 int err;
456
457 ctl = file->private_data;
458 if (snd_BUG_ON(!ctl || !ctl->card))
459 return -ENXIO;
460
461 switch (cmd) {
462 case SNDRV_CTL_IOCTL_PVERSION:
463 case SNDRV_CTL_IOCTL_CARD_INFO:
464 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
465 case SNDRV_CTL_IOCTL_POWER:
466 case SNDRV_CTL_IOCTL_POWER_STATE:
467 case SNDRV_CTL_IOCTL_ELEM_LOCK:
468 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
469 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
470 case SNDRV_CTL_IOCTL_TLV_READ:
471 case SNDRV_CTL_IOCTL_TLV_WRITE:
472 case SNDRV_CTL_IOCTL_TLV_COMMAND:
473 return snd_ctl_ioctl(file, cmd, (unsigned long)argp);
474 case SNDRV_CTL_IOCTL_ELEM_LIST32:
475 return snd_ctl_elem_list_compat(ctl->card, argp);
476 case SNDRV_CTL_IOCTL_ELEM_INFO32:
477 return snd_ctl_elem_info_compat(ctl, argp);
478 case SNDRV_CTL_IOCTL_ELEM_READ32:
479 return snd_ctl_elem_read_user_compat(ctl->card, argp);
480 case SNDRV_CTL_IOCTL_ELEM_WRITE32:
481 return snd_ctl_elem_write_user_compat(ctl, argp);
482 case SNDRV_CTL_IOCTL_ELEM_ADD32:
483 return snd_ctl_elem_add_compat(ctl, argp, 0);
484 case SNDRV_CTL_IOCTL_ELEM_REPLACE32:
485 return snd_ctl_elem_add_compat(ctl, argp, 1);
486#ifdef CONFIG_X86_X32
487 case SNDRV_CTL_IOCTL_ELEM_READ_X32:
488 return snd_ctl_elem_read_user_x32(ctl->card, argp);
489 case SNDRV_CTL_IOCTL_ELEM_WRITE_X32:
490 return snd_ctl_elem_write_user_x32(ctl, argp);
491#endif /* CONFIG_X86_X32 */
492 }
493
494 down_read(&snd_ioctl_rwsem);
495 list_for_each_entry(p, &snd_control_compat_ioctls, list) {
496 if (p->fioctl) {
497 err = p->fioctl(ctl->card, ctl, cmd, arg);
498 if (err != -ENOIOCTLCMD) {
499 up_read(&snd_ioctl_rwsem);
500 return err;
501 }
502 }
503 }
504 up_read(&snd_ioctl_rwsem);
505 return -ENOIOCTLCMD;
506}
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * compat ioctls for control API
4 *
5 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
6 */
7
8/* this file included from control.c */
9
10#include <linux/compat.h>
11#include <linux/slab.h>
12
13struct snd_ctl_elem_list32 {
14 u32 offset;
15 u32 space;
16 u32 used;
17 u32 count;
18 u32 pids;
19 unsigned char reserved[50];
20} /* don't set packed attribute here */;
21
22static int snd_ctl_elem_list_compat(struct snd_card *card,
23 struct snd_ctl_elem_list32 __user *data32)
24{
25 struct snd_ctl_elem_list data = {};
26 compat_caddr_t ptr;
27 int err;
28
29 /* offset, space, used, count */
30 if (copy_from_user(&data, data32, 4 * sizeof(u32)))
31 return -EFAULT;
32 /* pids */
33 if (get_user(ptr, &data32->pids))
34 return -EFAULT;
35 data.pids = compat_ptr(ptr);
36 err = snd_ctl_elem_list(card, &data);
37 if (err < 0)
38 return err;
39 /* copy the result */
40 if (copy_to_user(data32, &data, 4 * sizeof(u32)))
41 return -EFAULT;
42 return 0;
43}
44
45/*
46 * control element info
47 * it uses union, so the things are not easy..
48 */
49
50struct snd_ctl_elem_info32 {
51 struct snd_ctl_elem_id id; // the size of struct is same
52 s32 type;
53 u32 access;
54 u32 count;
55 s32 owner;
56 union {
57 struct {
58 s32 min;
59 s32 max;
60 s32 step;
61 } integer;
62 struct {
63 u64 min;
64 u64 max;
65 u64 step;
66 } integer64;
67 struct {
68 u32 items;
69 u32 item;
70 char name[64];
71 u64 names_ptr;
72 u32 names_length;
73 } enumerated;
74 unsigned char reserved[128];
75 } value;
76 unsigned char reserved[64];
77} __attribute__((packed));
78
79static int snd_ctl_elem_info_compat(struct snd_ctl_file *ctl,
80 struct snd_ctl_elem_info32 __user *data32)
81{
82 struct snd_ctl_elem_info *data;
83 int err;
84
85 data = kzalloc(sizeof(*data), GFP_KERNEL);
86 if (! data)
87 return -ENOMEM;
88
89 err = -EFAULT;
90 /* copy id */
91 if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
92 goto error;
93 /* we need to copy the item index.
94 * hope this doesn't break anything..
95 */
96 if (get_user(data->value.enumerated.item, &data32->value.enumerated.item))
97 goto error;
98
99 err = snd_ctl_elem_info(ctl, data);
100 if (err < 0)
101 goto error;
102 /* restore info to 32bit */
103 err = -EFAULT;
104 /* id, type, access, count */
105 if (copy_to_user(&data32->id, &data->id, sizeof(data->id)) ||
106 copy_to_user(&data32->type, &data->type, 3 * sizeof(u32)))
107 goto error;
108 if (put_user(data->owner, &data32->owner))
109 goto error;
110 switch (data->type) {
111 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
112 case SNDRV_CTL_ELEM_TYPE_INTEGER:
113 if (put_user(data->value.integer.min, &data32->value.integer.min) ||
114 put_user(data->value.integer.max, &data32->value.integer.max) ||
115 put_user(data->value.integer.step, &data32->value.integer.step))
116 goto error;
117 break;
118 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
119 if (copy_to_user(&data32->value.integer64,
120 &data->value.integer64,
121 sizeof(data->value.integer64)))
122 goto error;
123 break;
124 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
125 if (copy_to_user(&data32->value.enumerated,
126 &data->value.enumerated,
127 sizeof(data->value.enumerated)))
128 goto error;
129 break;
130 default:
131 break;
132 }
133 err = 0;
134 error:
135 kfree(data);
136 return err;
137}
138
139/* read / write */
140struct snd_ctl_elem_value32 {
141 struct snd_ctl_elem_id id;
142 unsigned int indirect; /* bit-field causes misalignment */
143 union {
144 s32 integer[128];
145 unsigned char data[512];
146#ifndef CONFIG_X86_64
147 s64 integer64[64];
148#endif
149 } value;
150 unsigned char reserved[128];
151};
152
153#ifdef CONFIG_X86_X32_ABI
154/* x32 has a different alignment for 64bit values from ia32 */
155struct snd_ctl_elem_value_x32 {
156 struct snd_ctl_elem_id id;
157 unsigned int indirect; /* bit-field causes misalignment */
158 union {
159 s32 integer[128];
160 unsigned char data[512];
161 s64 integer64[64];
162 } value;
163 unsigned char reserved[128];
164};
165#endif /* CONFIG_X86_X32_ABI */
166
167/* get the value type and count of the control */
168static int get_ctl_type(struct snd_card *card, struct snd_ctl_elem_id *id,
169 int *countp)
170{
171 struct snd_kcontrol *kctl;
172 struct snd_ctl_elem_info *info;
173 int err;
174
175 down_read(&card->controls_rwsem);
176 kctl = snd_ctl_find_id(card, id);
177 if (! kctl) {
178 up_read(&card->controls_rwsem);
179 return -ENOENT;
180 }
181 info = kzalloc(sizeof(*info), GFP_KERNEL);
182 if (info == NULL) {
183 up_read(&card->controls_rwsem);
184 return -ENOMEM;
185 }
186 info->id = *id;
187 err = snd_power_ref_and_wait(card);
188 if (!err)
189 err = kctl->info(kctl, info);
190 snd_power_unref(card);
191 up_read(&card->controls_rwsem);
192 if (err >= 0) {
193 err = info->type;
194 *countp = info->count;
195 }
196 kfree(info);
197 return err;
198}
199
200static int get_elem_size(int type, int count)
201{
202 switch (type) {
203 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
204 return sizeof(s64) * count;
205 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
206 return sizeof(int) * count;
207 case SNDRV_CTL_ELEM_TYPE_BYTES:
208 return 512;
209 case SNDRV_CTL_ELEM_TYPE_IEC958:
210 return sizeof(struct snd_aes_iec958);
211 default:
212 return -1;
213 }
214}
215
216static int copy_ctl_value_from_user(struct snd_card *card,
217 struct snd_ctl_elem_value *data,
218 void __user *userdata,
219 void __user *valuep,
220 int *typep, int *countp)
221{
222 struct snd_ctl_elem_value32 __user *data32 = userdata;
223 int i, type, size;
224 int count;
225 unsigned int indirect;
226
227 if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
228 return -EFAULT;
229 if (get_user(indirect, &data32->indirect))
230 return -EFAULT;
231 if (indirect)
232 return -EINVAL;
233 type = get_ctl_type(card, &data->id, &count);
234 if (type < 0)
235 return type;
236
237 if (type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
238 type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
239 for (i = 0; i < count; i++) {
240 s32 __user *intp = valuep;
241 int val;
242 if (get_user(val, &intp[i]))
243 return -EFAULT;
244 data->value.integer.value[i] = val;
245 }
246 } else {
247 size = get_elem_size(type, count);
248 if (size < 0) {
249 dev_err(card->dev, "snd_ioctl32_ctl_elem_value: unknown type %d\n", type);
250 return -EINVAL;
251 }
252 if (copy_from_user(data->value.bytes.data, valuep, size))
253 return -EFAULT;
254 }
255
256 *typep = type;
257 *countp = count;
258 return 0;
259}
260
261/* restore the value to 32bit */
262static int copy_ctl_value_to_user(void __user *userdata,
263 void __user *valuep,
264 struct snd_ctl_elem_value *data,
265 int type, int count)
266{
267 struct snd_ctl_elem_value32 __user *data32 = userdata;
268 int i, size;
269
270 if (type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
271 type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
272 for (i = 0; i < count; i++) {
273 s32 __user *intp = valuep;
274 int val;
275 val = data->value.integer.value[i];
276 if (put_user(val, &intp[i]))
277 return -EFAULT;
278 }
279 } else {
280 size = get_elem_size(type, count);
281 if (copy_to_user(valuep, data->value.bytes.data, size))
282 return -EFAULT;
283 }
284 if (copy_to_user(&data32->id, &data->id, sizeof(data32->id)))
285 return -EFAULT;
286 return 0;
287}
288
289static int ctl_elem_read_user(struct snd_card *card,
290 void __user *userdata, void __user *valuep)
291{
292 struct snd_ctl_elem_value *data;
293 int err, type, count;
294
295 data = kzalloc(sizeof(*data), GFP_KERNEL);
296 if (data == NULL)
297 return -ENOMEM;
298
299 err = copy_ctl_value_from_user(card, data, userdata, valuep,
300 &type, &count);
301 if (err < 0)
302 goto error;
303
304 err = snd_ctl_elem_read(card, data);
305 if (err < 0)
306 goto error;
307 err = copy_ctl_value_to_user(userdata, valuep, data, type, count);
308 error:
309 kfree(data);
310 return err;
311}
312
313static int ctl_elem_write_user(struct snd_ctl_file *file,
314 void __user *userdata, void __user *valuep)
315{
316 struct snd_ctl_elem_value *data;
317 struct snd_card *card = file->card;
318 int err, type, count;
319
320 data = kzalloc(sizeof(*data), GFP_KERNEL);
321 if (data == NULL)
322 return -ENOMEM;
323
324 err = copy_ctl_value_from_user(card, data, userdata, valuep,
325 &type, &count);
326 if (err < 0)
327 goto error;
328
329 err = snd_ctl_elem_write(card, file, data);
330 if (err < 0)
331 goto error;
332 err = copy_ctl_value_to_user(userdata, valuep, data, type, count);
333 error:
334 kfree(data);
335 return err;
336}
337
338static int snd_ctl_elem_read_user_compat(struct snd_card *card,
339 struct snd_ctl_elem_value32 __user *data32)
340{
341 return ctl_elem_read_user(card, data32, &data32->value);
342}
343
344static int snd_ctl_elem_write_user_compat(struct snd_ctl_file *file,
345 struct snd_ctl_elem_value32 __user *data32)
346{
347 return ctl_elem_write_user(file, data32, &data32->value);
348}
349
350#ifdef CONFIG_X86_X32_ABI
351static int snd_ctl_elem_read_user_x32(struct snd_card *card,
352 struct snd_ctl_elem_value_x32 __user *data32)
353{
354 return ctl_elem_read_user(card, data32, &data32->value);
355}
356
357static int snd_ctl_elem_write_user_x32(struct snd_ctl_file *file,
358 struct snd_ctl_elem_value_x32 __user *data32)
359{
360 return ctl_elem_write_user(file, data32, &data32->value);
361}
362#endif /* CONFIG_X86_X32_ABI */
363
364/* add or replace a user control */
365static int snd_ctl_elem_add_compat(struct snd_ctl_file *file,
366 struct snd_ctl_elem_info32 __user *data32,
367 int replace)
368{
369 struct snd_ctl_elem_info *data;
370 int err;
371
372 data = kzalloc(sizeof(*data), GFP_KERNEL);
373 if (! data)
374 return -ENOMEM;
375
376 err = -EFAULT;
377 /* id, type, access, count */ \
378 if (copy_from_user(&data->id, &data32->id, sizeof(data->id)) ||
379 copy_from_user(&data->type, &data32->type, 3 * sizeof(u32)))
380 goto error;
381 if (get_user(data->owner, &data32->owner))
382 goto error;
383 switch (data->type) {
384 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
385 case SNDRV_CTL_ELEM_TYPE_INTEGER:
386 if (get_user(data->value.integer.min, &data32->value.integer.min) ||
387 get_user(data->value.integer.max, &data32->value.integer.max) ||
388 get_user(data->value.integer.step, &data32->value.integer.step))
389 goto error;
390 break;
391 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
392 if (copy_from_user(&data->value.integer64,
393 &data32->value.integer64,
394 sizeof(data->value.integer64)))
395 goto error;
396 break;
397 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
398 if (copy_from_user(&data->value.enumerated,
399 &data32->value.enumerated,
400 sizeof(data->value.enumerated)))
401 goto error;
402 data->value.enumerated.names_ptr =
403 (uintptr_t)compat_ptr(data->value.enumerated.names_ptr);
404 break;
405 default:
406 break;
407 }
408 err = snd_ctl_elem_add(file, data, replace);
409 error:
410 kfree(data);
411 return err;
412}
413
414enum {
415 SNDRV_CTL_IOCTL_ELEM_LIST32 = _IOWR('U', 0x10, struct snd_ctl_elem_list32),
416 SNDRV_CTL_IOCTL_ELEM_INFO32 = _IOWR('U', 0x11, struct snd_ctl_elem_info32),
417 SNDRV_CTL_IOCTL_ELEM_READ32 = _IOWR('U', 0x12, struct snd_ctl_elem_value32),
418 SNDRV_CTL_IOCTL_ELEM_WRITE32 = _IOWR('U', 0x13, struct snd_ctl_elem_value32),
419 SNDRV_CTL_IOCTL_ELEM_ADD32 = _IOWR('U', 0x17, struct snd_ctl_elem_info32),
420 SNDRV_CTL_IOCTL_ELEM_REPLACE32 = _IOWR('U', 0x18, struct snd_ctl_elem_info32),
421#ifdef CONFIG_X86_X32_ABI
422 SNDRV_CTL_IOCTL_ELEM_READ_X32 = _IOWR('U', 0x12, struct snd_ctl_elem_value_x32),
423 SNDRV_CTL_IOCTL_ELEM_WRITE_X32 = _IOWR('U', 0x13, struct snd_ctl_elem_value_x32),
424#endif /* CONFIG_X86_X32_ABI */
425};
426
427static inline long snd_ctl_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
428{
429 struct snd_ctl_file *ctl;
430 struct snd_kctl_ioctl *p;
431 void __user *argp = compat_ptr(arg);
432 int err;
433
434 ctl = file->private_data;
435 if (snd_BUG_ON(!ctl || !ctl->card))
436 return -ENXIO;
437
438 switch (cmd) {
439 case SNDRV_CTL_IOCTL_PVERSION:
440 case SNDRV_CTL_IOCTL_CARD_INFO:
441 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
442 case SNDRV_CTL_IOCTL_POWER:
443 case SNDRV_CTL_IOCTL_POWER_STATE:
444 case SNDRV_CTL_IOCTL_ELEM_LOCK:
445 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
446 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
447 case SNDRV_CTL_IOCTL_TLV_READ:
448 case SNDRV_CTL_IOCTL_TLV_WRITE:
449 case SNDRV_CTL_IOCTL_TLV_COMMAND:
450 return snd_ctl_ioctl(file, cmd, (unsigned long)argp);
451 case SNDRV_CTL_IOCTL_ELEM_LIST32:
452 return snd_ctl_elem_list_compat(ctl->card, argp);
453 case SNDRV_CTL_IOCTL_ELEM_INFO32:
454 return snd_ctl_elem_info_compat(ctl, argp);
455 case SNDRV_CTL_IOCTL_ELEM_READ32:
456 return snd_ctl_elem_read_user_compat(ctl->card, argp);
457 case SNDRV_CTL_IOCTL_ELEM_WRITE32:
458 return snd_ctl_elem_write_user_compat(ctl, argp);
459 case SNDRV_CTL_IOCTL_ELEM_ADD32:
460 return snd_ctl_elem_add_compat(ctl, argp, 0);
461 case SNDRV_CTL_IOCTL_ELEM_REPLACE32:
462 return snd_ctl_elem_add_compat(ctl, argp, 1);
463#ifdef CONFIG_X86_X32_ABI
464 case SNDRV_CTL_IOCTL_ELEM_READ_X32:
465 return snd_ctl_elem_read_user_x32(ctl->card, argp);
466 case SNDRV_CTL_IOCTL_ELEM_WRITE_X32:
467 return snd_ctl_elem_write_user_x32(ctl, argp);
468#endif /* CONFIG_X86_X32_ABI */
469 }
470
471 down_read(&snd_ioctl_rwsem);
472 list_for_each_entry(p, &snd_control_compat_ioctls, list) {
473 if (p->fioctl) {
474 err = p->fioctl(ctl->card, ctl, cmd, arg);
475 if (err != -ENOIOCTLCMD) {
476 up_read(&snd_ioctl_rwsem);
477 return err;
478 }
479 }
480 }
481 up_read(&snd_ioctl_rwsem);
482 return -ENOIOCTLCMD;
483}