]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - sound/core/control_compat.c
[ALSA] Fix typos in document
[linux-2.6.git] / sound / core / control_compat.c
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
25 struct snd_ctl_elem_list32 {
26         u32 offset;
27         u32 space;
28         u32 used;
29         u32 count;
30         u32 pids;
31         unsigned char reserved[50];
32 } /* don't set packed attribute here */;
33
34 static int snd_ctl_elem_list_compat(struct snd_card *card,
35                                     struct snd_ctl_elem_list32 __user *data32)
36 {
37         struct snd_ctl_elem_list __user *data;
38         compat_caddr_t ptr;
39         int err;
40
41         data = compat_alloc_user_space(sizeof(*data));
42
43         /* offset, space, used, count */
44         if (copy_in_user(data, data32, 4 * sizeof(u32)))
45                 return -EFAULT;
46         /* pids */
47         if (get_user(ptr, &data32->pids) ||
48             put_user(compat_ptr(ptr), &data->pids))
49                 return -EFAULT;
50         err = snd_ctl_elem_list(card, data);
51         if (err < 0)
52                 return err;
53         /* copy the result */
54         if (copy_in_user(data32, data, 4 * sizeof(u32)))
55                 return -EFAULT;
56         return 0;
57 }
58
59 /*
60  * control element info
61  * it uses union, so the things are not easy..
62  */
63
64 struct snd_ctl_elem_info32 {
65         struct snd_ctl_elem_id id; // the size of struct is same
66         s32 type;
67         u32 access;
68         u32 count;
69         s32 owner;
70         union {
71                 struct {
72                         s32 min;
73                         s32 max;
74                         s32 step;
75                 } integer;
76                 struct {
77                         u64 min;
78                         u64 max;
79                         u64 step;
80                 } integer64;
81                 struct {
82                         u32 items;
83                         u32 item;
84                         char name[64];
85                 } enumerated;
86                 unsigned char reserved[128];
87         } value;
88         unsigned char reserved[64];
89 } __attribute__((packed));
90
91 static int snd_ctl_elem_info_compat(struct snd_ctl_file *ctl,
92                                     struct snd_ctl_elem_info32 __user *data32)
93 {
94         struct snd_ctl_elem_info *data;
95         int err;
96
97         data = kzalloc(sizeof(*data), GFP_KERNEL);
98         if (! data)
99                 return -ENOMEM;
100
101         err = -EFAULT;
102         /* copy id */
103         if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
104                 goto error;
105         /* we need to copy the item index.
106          * hope this doesn't break anything..
107          */
108         if (get_user(data->value.enumerated.item, &data32->value.enumerated.item))
109                 goto error;
110         err = snd_ctl_elem_info(ctl, data);
111         if (err < 0)
112                 goto error;
113         /* restore info to 32bit */
114         err = -EFAULT;
115         /* id, type, access, count */
116         if (copy_to_user(&data32->id, &data->id, sizeof(data->id)) ||
117             copy_to_user(&data32->type, &data->type, 3 * sizeof(u32)))
118                 goto error;
119         if (put_user(data->owner, &data32->owner))
120                 goto error;
121         switch (data->type) {
122         case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
123         case SNDRV_CTL_ELEM_TYPE_INTEGER:
124                 if (put_user(data->value.integer.min, &data32->value.integer.min) ||
125                     put_user(data->value.integer.max, &data32->value.integer.max) ||
126                     put_user(data->value.integer.step, &data32->value.integer.step))
127                         goto error;
128                 break;
129         case SNDRV_CTL_ELEM_TYPE_INTEGER64:
130                 if (copy_to_user(&data32->value.integer64,
131                                  &data->value.integer64,
132                                  sizeof(data->value.integer64)))
133                         goto error;
134                 break;
135         case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
136                 if (copy_to_user(&data32->value.enumerated,
137                                  &data->value.enumerated,
138                                  sizeof(data->value.enumerated)))
139                         goto error;
140                 break;
141         default:
142                 break;
143         }
144         err = 0;
145  error:
146         kfree(data);
147         return err;
148 }
149
150 /* read / write */
151 struct snd_ctl_elem_value32 {
152         struct snd_ctl_elem_id id;
153         unsigned int indirect;  /* bit-field causes misalignment */
154         union {
155                 s32 integer[128];
156                 unsigned char data[512];
157 #ifndef CONFIG_X86_64
158                 s64 integer64[64];
159 #endif
160         } value;
161         unsigned char reserved[128];
162 };
163
164
165 /* get the value type and count of the control */
166 static int get_ctl_type(struct snd_card *card, struct snd_ctl_elem_id *id,
167                         int *countp)
168 {
169         struct snd_kcontrol *kctl;
170         struct snd_ctl_elem_info *info;
171         int err;
172
173         down_read(&card->controls_rwsem);
174         kctl = snd_ctl_find_id(card, id);
175         if (! kctl) {
176                 up_read(&card->controls_rwsem);
177                 return -ENXIO;
178         }
179         info = kzalloc(sizeof(*info), GFP_KERNEL);
180         if (info == NULL) {
181                 up_read(&card->controls_rwsem);
182                 return -ENOMEM;
183         }
184         info->id = *id;
185         err = kctl->info(kctl, info);
186         up_read(&card->controls_rwsem);
187         if (err >= 0) {
188                 err = info->type;
189                 *countp = info->count;
190         }
191         kfree(info);
192         return err;
193 }
194
195 static int get_elem_size(int type, int count)
196 {
197         switch (type) {
198         case SNDRV_CTL_ELEM_TYPE_INTEGER64:
199                 return sizeof(s64) * count;
200         case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
201                 return sizeof(int) * count;
202         case SNDRV_CTL_ELEM_TYPE_BYTES:
203                 return 512;
204         case SNDRV_CTL_ELEM_TYPE_IEC958:
205                 return sizeof(struct snd_aes_iec958);
206         default:
207                 return -1;
208         }
209 }
210
211 static int copy_ctl_value_from_user(struct snd_card *card,
212                                     struct snd_ctl_elem_value *data,
213                                     struct snd_ctl_elem_value32 __user *data32,
214                                     int *typep, int *countp)
215 {
216         int i, type, count, size;
217         unsigned int indirect;
218
219         if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
220                 return -EFAULT;
221         if (get_user(indirect, &data32->indirect))
222                 return -EFAULT;
223         if (indirect)
224                 return -EINVAL;
225         type = get_ctl_type(card, &data->id, &count);
226         if (type < 0)
227                 return type;
228
229         if (type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
230             type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
231                 for (i = 0; i < count; i++) {
232                         int val;
233                         if (get_user(val, &data32->value.integer[i]))
234                                 return -EFAULT;
235                         data->value.integer.value[i] = val;
236                 }
237         } else {
238                 size = get_elem_size(type, count);
239                 if (size < 0) {
240                         printk(KERN_ERR "snd_ioctl32_ctl_elem_value: unknown type %d\n", type);
241                         return -EINVAL;
242                 }
243                 if (copy_from_user(data->value.bytes.data,
244                                    data32->value.data, size))
245                         return -EFAULT;
246         }
247
248         *typep = type;
249         *countp = count;
250         return 0;
251 }
252
253 /* restore the value to 32bit */
254 static int copy_ctl_value_to_user(struct snd_ctl_elem_value32 __user *data32,
255                                   struct snd_ctl_elem_value *data,
256                                   int type, int count)
257 {
258         int i, size;
259
260         if (type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
261             type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
262                 for (i = 0; i < count; i++) {
263                         int val;
264                         val = data->value.integer.value[i];
265                         if (put_user(val, &data32->value.integer[i]))
266                                 return -EFAULT;
267                 }
268         } else {
269                 size = get_elem_size(type, count);
270                 if (copy_to_user(data32->value.data,
271                                  data->value.bytes.data, size))
272                         return -EFAULT;
273         }
274         return 0;
275 }
276
277 static int snd_ctl_elem_read_user_compat(struct snd_card *card, 
278                                          struct snd_ctl_elem_value32 __user *data32)
279 {
280         struct snd_ctl_elem_value *data;
281         int err, type, count;
282
283         data = kzalloc(sizeof(*data), GFP_KERNEL);
284         if (data == NULL)
285                 return -ENOMEM;
286
287         if ((err = copy_ctl_value_from_user(card, data, data32, &type, &count)) < 0)
288                 goto error;
289         if ((err = snd_ctl_elem_read(card, data)) < 0)
290                 goto error;
291         err = copy_ctl_value_to_user(data32, data, type, count);
292  error:
293         kfree(data);
294         return err;
295 }
296
297 static int snd_ctl_elem_write_user_compat(struct snd_ctl_file *file,
298                                           struct snd_ctl_elem_value32 __user *data32)
299 {
300         struct snd_ctl_elem_value *data;
301         int err, type, count;
302
303         data = kzalloc(sizeof(*data), GFP_KERNEL);
304         if (data == NULL)
305                 return -ENOMEM;
306
307         if ((err = copy_ctl_value_from_user(file->card, data, data32, &type, &count)) < 0)
308                 goto error;
309         if ((err = snd_ctl_elem_write(file->card, file, data)) < 0)
310                 goto error;
311         err = copy_ctl_value_to_user(data32, data, type, count);
312  error:
313         kfree(data);
314         return err;
315 }
316
317 /* add or replace a user control */
318 static int snd_ctl_elem_add_compat(struct snd_ctl_file *file,
319                                    struct snd_ctl_elem_info32 __user *data32,
320                                    int replace)
321 {
322         struct snd_ctl_elem_info *data;
323         int err;
324
325         data = kzalloc(sizeof(*data), GFP_KERNEL);
326         if (! data)
327                 return -ENOMEM;
328
329         err = -EFAULT;
330         /* id, type, access, count */ \
331         if (copy_from_user(&data->id, &data32->id, sizeof(data->id)) ||
332             copy_from_user(&data->type, &data32->type, 3 * sizeof(u32)))
333                 goto error;
334         if (get_user(data->owner, &data32->owner) ||
335             get_user(data->type, &data32->type))
336                 goto error;
337         switch (data->type) {
338         case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
339         case SNDRV_CTL_ELEM_TYPE_INTEGER:
340                 if (get_user(data->value.integer.min, &data32->value.integer.min) ||
341                     get_user(data->value.integer.max, &data32->value.integer.max) ||
342                     get_user(data->value.integer.step, &data32->value.integer.step))
343                         goto error;
344                 break;
345         case SNDRV_CTL_ELEM_TYPE_INTEGER64:
346                 if (copy_from_user(&data->value.integer64,
347                                    &data32->value.integer64,
348                                    sizeof(data->value.integer64)))
349                         goto error;
350                 break;
351         case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
352                 if (copy_from_user(&data->value.enumerated,
353                                    &data32->value.enumerated,
354                                    sizeof(data->value.enumerated)))
355                         goto error;
356                 break;
357         default:
358                 break;
359         }
360         err = snd_ctl_elem_add(file, data, replace);
361  error:
362         kfree(data);
363         return err;
364 }  
365
366 enum {
367         SNDRV_CTL_IOCTL_ELEM_LIST32 = _IOWR('U', 0x10, struct snd_ctl_elem_list32),
368         SNDRV_CTL_IOCTL_ELEM_INFO32 = _IOWR('U', 0x11, struct snd_ctl_elem_info32),
369         SNDRV_CTL_IOCTL_ELEM_READ32 = _IOWR('U', 0x12, struct snd_ctl_elem_value32),
370         SNDRV_CTL_IOCTL_ELEM_WRITE32 = _IOWR('U', 0x13, struct snd_ctl_elem_value32),
371         SNDRV_CTL_IOCTL_ELEM_ADD32 = _IOWR('U', 0x17, struct snd_ctl_elem_info32),
372         SNDRV_CTL_IOCTL_ELEM_REPLACE32 = _IOWR('U', 0x18, struct snd_ctl_elem_info32),
373 };
374
375 static inline long snd_ctl_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
376 {
377         struct snd_ctl_file *ctl;
378         struct list_head *list;
379         void __user *argp = compat_ptr(arg);
380         int err;
381
382         ctl = file->private_data;
383         snd_assert(ctl && ctl->card, return -ENXIO);
384
385         switch (cmd) {
386         case SNDRV_CTL_IOCTL_PVERSION:
387         case SNDRV_CTL_IOCTL_CARD_INFO:
388         case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
389         case SNDRV_CTL_IOCTL_POWER:
390         case SNDRV_CTL_IOCTL_POWER_STATE:
391         case SNDRV_CTL_IOCTL_ELEM_LOCK:
392         case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
393                 return snd_ctl_ioctl(file, cmd, (unsigned long)argp);
394         case SNDRV_CTL_IOCTL_ELEM_LIST32:
395                 return snd_ctl_elem_list_compat(ctl->card, argp);
396         case SNDRV_CTL_IOCTL_ELEM_INFO32:
397                 return snd_ctl_elem_info_compat(ctl, argp);
398         case SNDRV_CTL_IOCTL_ELEM_READ32:
399                 return snd_ctl_elem_read_user_compat(ctl->card, argp);
400         case SNDRV_CTL_IOCTL_ELEM_WRITE32:
401                 return snd_ctl_elem_write_user_compat(ctl, argp);
402         case SNDRV_CTL_IOCTL_ELEM_ADD32:
403                 return snd_ctl_elem_add_compat(ctl, argp, 0);
404         case SNDRV_CTL_IOCTL_ELEM_REPLACE32:
405                 return snd_ctl_elem_add_compat(ctl, argp, 1);
406         }
407
408         down_read(&snd_ioctl_rwsem);
409         list_for_each(list, &snd_control_compat_ioctls) {
410                 struct snd_kctl_ioctl *p = list_entry(list, struct snd_kctl_ioctl, list);
411                 if (p->fioctl) {
412                         err = p->fioctl(ctl->card, ctl, cmd, arg);
413                         if (err != -ENOIOCTLCMD) {
414                                 up_read(&snd_ioctl_rwsem);
415                                 return err;
416                         }
417                 }
418         }
419         up_read(&snd_ioctl_rwsem);
420         return -ENOIOCTLCMD;
421 }