19133ee076c5bb090d9fec63c83bb6b18285360e
[linux-2.6-microblaze.git] / sound / core / control_compat.c
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
13 struct 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
22 static 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
50 struct 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
79 static 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_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
100         if (err < 0)
101                 goto error;
102         err = snd_ctl_elem_info(ctl, data);
103         if (err < 0)
104                 goto error;
105         /* restore info to 32bit */
106         err = -EFAULT;
107         /* id, type, access, count */
108         if (copy_to_user(&data32->id, &data->id, sizeof(data->id)) ||
109             copy_to_user(&data32->type, &data->type, 3 * sizeof(u32)))
110                 goto error;
111         if (put_user(data->owner, &data32->owner))
112                 goto error;
113         switch (data->type) {
114         case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
115         case SNDRV_CTL_ELEM_TYPE_INTEGER:
116                 if (put_user(data->value.integer.min, &data32->value.integer.min) ||
117                     put_user(data->value.integer.max, &data32->value.integer.max) ||
118                     put_user(data->value.integer.step, &data32->value.integer.step))
119                         goto error;
120                 break;
121         case SNDRV_CTL_ELEM_TYPE_INTEGER64:
122                 if (copy_to_user(&data32->value.integer64,
123                                  &data->value.integer64,
124                                  sizeof(data->value.integer64)))
125                         goto error;
126                 break;
127         case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
128                 if (copy_to_user(&data32->value.enumerated,
129                                  &data->value.enumerated,
130                                  sizeof(data->value.enumerated)))
131                         goto error;
132                 break;
133         default:
134                 break;
135         }
136         err = 0;
137  error:
138         kfree(data);
139         return err;
140 }
141
142 /* read / write */
143 struct snd_ctl_elem_value32 {
144         struct snd_ctl_elem_id id;
145         unsigned int indirect;  /* bit-field causes misalignment */
146         union {
147                 s32 integer[128];
148                 unsigned char data[512];
149 #ifndef CONFIG_X86_64
150                 s64 integer64[64];
151 #endif
152         } value;
153         unsigned char reserved[128];
154 };
155
156 #ifdef CONFIG_X86_X32
157 /* x32 has a different alignment for 64bit values from ia32 */
158 struct snd_ctl_elem_value_x32 {
159         struct snd_ctl_elem_id id;
160         unsigned int indirect;  /* bit-field causes misalignment */
161         union {
162                 s32 integer[128];
163                 unsigned char data[512];
164                 s64 integer64[64];
165         } value;
166         unsigned char reserved[128];
167 };
168 #endif /* CONFIG_X86_X32 */
169
170 /* get the value type and count of the control */
171 static int get_ctl_type(struct snd_card *card, struct snd_ctl_elem_id *id,
172                         int *countp)
173 {
174         struct snd_kcontrol *kctl;
175         struct snd_ctl_elem_info *info;
176         int err;
177
178         down_read(&card->controls_rwsem);
179         kctl = snd_ctl_find_id(card, id);
180         if (! kctl) {
181                 up_read(&card->controls_rwsem);
182                 return -ENOENT;
183         }
184         info = kzalloc(sizeof(*info), GFP_KERNEL);
185         if (info == NULL) {
186                 up_read(&card->controls_rwsem);
187                 return -ENOMEM;
188         }
189         info->id = *id;
190         err = snd_power_ref_and_wait(card);
191         if (!err)
192                 err = kctl->info(kctl, info);
193         snd_power_unref(card);
194         up_read(&card->controls_rwsem);
195         if (err >= 0) {
196                 err = info->type;
197                 *countp = info->count;
198         }
199         kfree(info);
200         return err;
201 }
202
203 static int get_elem_size(int type, int count)
204 {
205         switch (type) {
206         case SNDRV_CTL_ELEM_TYPE_INTEGER64:
207                 return sizeof(s64) * count;
208         case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
209                 return sizeof(int) * count;
210         case SNDRV_CTL_ELEM_TYPE_BYTES:
211                 return 512;
212         case SNDRV_CTL_ELEM_TYPE_IEC958:
213                 return sizeof(struct snd_aes_iec958);
214         default:
215                 return -1;
216         }
217 }
218
219 static int copy_ctl_value_from_user(struct snd_card *card,
220                                     struct snd_ctl_elem_value *data,
221                                     void __user *userdata,
222                                     void __user *valuep,
223                                     int *typep, int *countp)
224 {
225         struct snd_ctl_elem_value32 __user *data32 = userdata;
226         int i, type, size;
227         int count;
228         unsigned int indirect;
229
230         if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
231                 return -EFAULT;
232         if (get_user(indirect, &data32->indirect))
233                 return -EFAULT;
234         if (indirect)
235                 return -EINVAL;
236         type = get_ctl_type(card, &data->id, &count);
237         if (type < 0)
238                 return type;
239
240         if (type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
241             type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
242                 for (i = 0; i < count; i++) {
243                         s32 __user *intp = valuep;
244                         int val;
245                         if (get_user(val, &intp[i]))
246                                 return -EFAULT;
247                         data->value.integer.value[i] = val;
248                 }
249         } else {
250                 size = get_elem_size(type, count);
251                 if (size < 0) {
252                         dev_err(card->dev, "snd_ioctl32_ctl_elem_value: unknown type %d\n", type);
253                         return -EINVAL;
254                 }
255                 if (copy_from_user(data->value.bytes.data, valuep, size))
256                         return -EFAULT;
257         }
258
259         *typep = type;
260         *countp = count;
261         return 0;
262 }
263
264 /* restore the value to 32bit */
265 static int copy_ctl_value_to_user(void __user *userdata,
266                                   void __user *valuep,
267                                   struct snd_ctl_elem_value *data,
268                                   int type, int count)
269 {
270         int i, size;
271
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;
276                         int val;
277                         val = data->value.integer.value[i];
278                         if (put_user(val, &intp[i]))
279                                 return -EFAULT;
280                 }
281         } else {
282                 size = get_elem_size(type, count);
283                 if (copy_to_user(valuep, data->value.bytes.data, size))
284                         return -EFAULT;
285         }
286         return 0;
287 }
288
289 static 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_power_wait(card, SNDRV_CTL_POWER_D0);
305         if (err < 0)
306                 goto error;
307         err = snd_ctl_elem_read(card, data);
308         if (err < 0)
309                 goto error;
310         err = copy_ctl_value_to_user(userdata, valuep, data, type, count);
311  error:
312         kfree(data);
313         return err;
314 }
315
316 static int ctl_elem_write_user(struct snd_ctl_file *file,
317                                void __user *userdata, void __user *valuep)
318 {
319         struct snd_ctl_elem_value *data;
320         struct snd_card *card = file->card;
321         int err, type, count;
322
323         data = kzalloc(sizeof(*data), GFP_KERNEL);
324         if (data == NULL)
325                 return -ENOMEM;
326
327         err = copy_ctl_value_from_user(card, data, userdata, valuep,
328                                        &type, &count);
329         if (err < 0)
330                 goto error;
331
332         err = snd_power_wait(card, SNDRV_CTL_POWER_D0);
333         if (err < 0)
334                 goto error;
335         err = snd_ctl_elem_write(card, file, data);
336         if (err < 0)
337                 goto error;
338         err = copy_ctl_value_to_user(userdata, valuep, data, type, count);
339  error:
340         kfree(data);
341         return err;
342 }
343
344 static int snd_ctl_elem_read_user_compat(struct snd_card *card,
345                                          struct snd_ctl_elem_value32 __user *data32)
346 {
347         return ctl_elem_read_user(card, data32, &data32->value);
348 }
349
350 static int snd_ctl_elem_write_user_compat(struct snd_ctl_file *file,
351                                           struct snd_ctl_elem_value32 __user *data32)
352 {
353         return ctl_elem_write_user(file, data32, &data32->value);
354 }
355
356 #ifdef CONFIG_X86_X32
357 static int snd_ctl_elem_read_user_x32(struct snd_card *card,
358                                       struct snd_ctl_elem_value_x32 __user *data32)
359 {
360         return ctl_elem_read_user(card, data32, &data32->value);
361 }
362
363 static int snd_ctl_elem_write_user_x32(struct snd_ctl_file *file,
364                                        struct snd_ctl_elem_value_x32 __user *data32)
365 {
366         return ctl_elem_write_user(file, data32, &data32->value);
367 }
368 #endif /* CONFIG_X86_X32 */
369
370 /* add or replace a user control */
371 static int snd_ctl_elem_add_compat(struct snd_ctl_file *file,
372                                    struct snd_ctl_elem_info32 __user *data32,
373                                    int replace)
374 {
375         struct snd_ctl_elem_info *data;
376         int err;
377
378         data = kzalloc(sizeof(*data), GFP_KERNEL);
379         if (! data)
380                 return -ENOMEM;
381
382         err = -EFAULT;
383         /* id, type, access, count */ \
384         if (copy_from_user(&data->id, &data32->id, sizeof(data->id)) ||
385             copy_from_user(&data->type, &data32->type, 3 * sizeof(u32)))
386                 goto error;
387         if (get_user(data->owner, &data32->owner))
388                 goto error;
389         switch (data->type) {
390         case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
391         case SNDRV_CTL_ELEM_TYPE_INTEGER:
392                 if (get_user(data->value.integer.min, &data32->value.integer.min) ||
393                     get_user(data->value.integer.max, &data32->value.integer.max) ||
394                     get_user(data->value.integer.step, &data32->value.integer.step))
395                         goto error;
396                 break;
397         case SNDRV_CTL_ELEM_TYPE_INTEGER64:
398                 if (copy_from_user(&data->value.integer64,
399                                    &data32->value.integer64,
400                                    sizeof(data->value.integer64)))
401                         goto error;
402                 break;
403         case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
404                 if (copy_from_user(&data->value.enumerated,
405                                    &data32->value.enumerated,
406                                    sizeof(data->value.enumerated)))
407                         goto error;
408                 data->value.enumerated.names_ptr =
409                         (uintptr_t)compat_ptr(data->value.enumerated.names_ptr);
410                 break;
411         default:
412                 break;
413         }
414         err = snd_ctl_elem_add(file, data, replace);
415  error:
416         kfree(data);
417         return err;
418 }  
419
420 enum {
421         SNDRV_CTL_IOCTL_ELEM_LIST32 = _IOWR('U', 0x10, struct snd_ctl_elem_list32),
422         SNDRV_CTL_IOCTL_ELEM_INFO32 = _IOWR('U', 0x11, struct snd_ctl_elem_info32),
423         SNDRV_CTL_IOCTL_ELEM_READ32 = _IOWR('U', 0x12, struct snd_ctl_elem_value32),
424         SNDRV_CTL_IOCTL_ELEM_WRITE32 = _IOWR('U', 0x13, struct snd_ctl_elem_value32),
425         SNDRV_CTL_IOCTL_ELEM_ADD32 = _IOWR('U', 0x17, struct snd_ctl_elem_info32),
426         SNDRV_CTL_IOCTL_ELEM_REPLACE32 = _IOWR('U', 0x18, struct snd_ctl_elem_info32),
427 #ifdef CONFIG_X86_X32
428         SNDRV_CTL_IOCTL_ELEM_READ_X32 = _IOWR('U', 0x12, struct snd_ctl_elem_value_x32),
429         SNDRV_CTL_IOCTL_ELEM_WRITE_X32 = _IOWR('U', 0x13, struct snd_ctl_elem_value_x32),
430 #endif /* CONFIG_X86_X32 */
431 };
432
433 static inline long snd_ctl_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
434 {
435         struct snd_ctl_file *ctl;
436         struct snd_kctl_ioctl *p;
437         void __user *argp = compat_ptr(arg);
438         int err;
439
440         ctl = file->private_data;
441         if (snd_BUG_ON(!ctl || !ctl->card))
442                 return -ENXIO;
443
444         switch (cmd) {
445         case SNDRV_CTL_IOCTL_PVERSION:
446         case SNDRV_CTL_IOCTL_CARD_INFO:
447         case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
448         case SNDRV_CTL_IOCTL_POWER:
449         case SNDRV_CTL_IOCTL_POWER_STATE:
450         case SNDRV_CTL_IOCTL_ELEM_LOCK:
451         case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
452         case SNDRV_CTL_IOCTL_ELEM_REMOVE:
453         case SNDRV_CTL_IOCTL_TLV_READ:
454         case SNDRV_CTL_IOCTL_TLV_WRITE:
455         case SNDRV_CTL_IOCTL_TLV_COMMAND:
456                 return snd_ctl_ioctl(file, cmd, (unsigned long)argp);
457         case SNDRV_CTL_IOCTL_ELEM_LIST32:
458                 return snd_ctl_elem_list_compat(ctl->card, argp);
459         case SNDRV_CTL_IOCTL_ELEM_INFO32:
460                 return snd_ctl_elem_info_compat(ctl, argp);
461         case SNDRV_CTL_IOCTL_ELEM_READ32:
462                 return snd_ctl_elem_read_user_compat(ctl->card, argp);
463         case SNDRV_CTL_IOCTL_ELEM_WRITE32:
464                 return snd_ctl_elem_write_user_compat(ctl, argp);
465         case SNDRV_CTL_IOCTL_ELEM_ADD32:
466                 return snd_ctl_elem_add_compat(ctl, argp, 0);
467         case SNDRV_CTL_IOCTL_ELEM_REPLACE32:
468                 return snd_ctl_elem_add_compat(ctl, argp, 1);
469 #ifdef CONFIG_X86_X32
470         case SNDRV_CTL_IOCTL_ELEM_READ_X32:
471                 return snd_ctl_elem_read_user_x32(ctl->card, argp);
472         case SNDRV_CTL_IOCTL_ELEM_WRITE_X32:
473                 return snd_ctl_elem_write_user_x32(ctl, argp);
474 #endif /* CONFIG_X86_X32 */
475         }
476
477         down_read(&snd_ioctl_rwsem);
478         list_for_each_entry(p, &snd_control_compat_ioctls, list) {
479                 if (p->fioctl) {
480                         err = p->fioctl(ctl->card, ctl, cmd, arg);
481                         if (err != -ENOIOCTLCMD) {
482                                 up_read(&snd_ioctl_rwsem);
483                                 return err;
484                         }
485                 }
486         }
487         up_read(&snd_ioctl_rwsem);
488         return -ENOIOCTLCMD;
489 }