vserver 1.9.5.x5
[linux-2.6.git] / sound / core / ioctl32 / ioctl32.c
1 /*
2  *   32bit -> 64bit ioctl wrapper for control API
3  *   Copyright (c) by Takashi Iwai <tiwai@suse.de>
4  *
5  *   This program is free software; you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *   GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program; if not, write to the Free Software
17  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18  *
19  */
20
21 #include <sound/driver.h>
22 #include <linux/sched.h>
23 #include <linux/smp_lock.h>
24 #include <linux/init.h>
25 #include <linux/time.h>
26 #include <linux/slab.h>
27 #include <linux/fs.h>
28 #include <sound/core.h>
29 #include <sound/control.h>
30 #include <sound/minors.h>
31 #include <asm/uaccess.h>
32 #include "ioctl32.h"
33
34
35 /*
36  * register/unregister mappers
37  * exported for other modules
38  */
39
40 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
41 MODULE_DESCRIPTION("ioctl32 wrapper for ALSA");
42 MODULE_LICENSE("GPL");
43
44 int register_ioctl32_conversion(unsigned int cmd, int (*handler)(unsigned int, unsigned int, unsigned long, struct file *));
45 int unregister_ioctl32_conversion(unsigned int cmd);
46
47
48 int snd_ioctl32_register(struct ioctl32_mapper *mappers)
49 {
50         int err;
51         struct ioctl32_mapper *m;
52
53         for (m = mappers; m->cmd; m++) {
54                 err = register_ioctl32_conversion(m->cmd, m->handler);
55                 if (err >= 0)
56                         m->registered++;
57         }
58         return 0;
59 }
60
61 void snd_ioctl32_unregister(struct ioctl32_mapper *mappers)
62 {
63         struct ioctl32_mapper *m;
64
65         for (m = mappers; m->cmd; m++) {
66                 if (m->registered) {
67                         unregister_ioctl32_conversion(m->cmd);
68                         m->registered = 0;
69                 }
70         }
71 }
72
73
74 /*
75  * compatible wrapper
76  */
77 int snd_ioctl32_compat(unsigned int fd, unsigned int cmd, unsigned long arg, struct file *filp)
78 {
79         if (! filp->f_op || ! filp->f_op->ioctl)
80                 return -ENOTTY;
81         return filp->f_op->ioctl(filp->f_dentry->d_inode, filp, cmd, arg);
82 }
83
84
85 /*
86  * Controls
87  */
88
89 struct sndrv_ctl_elem_list32 {
90         u32 offset;
91         u32 space;
92         u32 used;
93         u32 count;
94         u32 pids;
95         unsigned char reserved[50];
96 } /* don't set packed attribute here */;
97
98 static inline int _snd_ioctl32_ctl_elem_list(unsigned int fd, unsigned int cmd, unsigned long arg, struct file *file, unsigned int native_ctl)
99 {
100         struct sndrv_ctl_elem_list32 __user *data32;
101         struct sndrv_ctl_elem_list __user *data;
102         compat_caddr_t ptr;
103         int err;
104
105         data32 = compat_ptr(arg);
106         data = compat_alloc_user_space(sizeof(*data));
107
108         /* offset, space, used, count */
109         if (copy_in_user(data, data32, 4 * sizeof(u32)))
110                 return -EFAULT;
111         /* pids */
112         if (__get_user(ptr, &data32->pids) ||
113             __put_user(compat_ptr(ptr), &data->pids))
114                 return -EFAULT;
115         err = file->f_op->ioctl(file->f_dentry->d_inode, file, native_ctl, (unsigned long)data);
116         if (err < 0)
117                 return err;
118         /* copy the result */
119         if (copy_in_user(data32, data, 4 * sizeof(u32)))
120                 return -EFAULT;
121         return 0;
122 }
123
124 DEFINE_ALSA_IOCTL_ENTRY(ctl_elem_list, ctl_elem_list, SNDRV_CTL_IOCTL_ELEM_LIST);
125
126 /*
127  * control element info
128  * it uses union, so the things are not easy..
129  */
130
131 struct sndrv_ctl_elem_info32 {
132         struct sndrv_ctl_elem_id id; // the size of struct is same
133         s32 type;
134         u32 access;
135         u32 count;
136         s32 owner;
137         union {
138                 struct {
139                         s32 min;
140                         s32 max;
141                         s32 step;
142                 } integer;
143                 struct {
144                         u64 min;
145                         u64 max;
146                         u64 step;
147                 } integer64;
148                 struct {
149                         u32 items;
150                         u32 item;
151                         char name[64];
152                 } enumerated;
153                 unsigned char reserved[128];
154         } value;
155         unsigned char reserved[64];
156 } __attribute__((packed));
157
158 static inline int _snd_ioctl32_ctl_elem_info(unsigned int fd, unsigned int cmd, unsigned long arg, struct file *file, unsigned int native_ctl)
159 {
160         struct sndrv_ctl_elem_info __user *data, *src;
161         struct sndrv_ctl_elem_info32 __user *data32, *dst;
162         unsigned int type;
163         int err;
164
165         data32 = compat_ptr(arg);
166         data = compat_alloc_user_space(sizeof(*data));
167
168         /* copy id */
169         if (copy_in_user(&data->id, &data32->id, sizeof(data->id)))
170                 return -EFAULT;
171         /* we need to copy the item index.
172          * hope this doesn't break anything..
173          */
174         if (copy_in_user(&data->value.enumerated.item,
175                          &data32->value.enumerated.item,
176                          sizeof(data->value.enumerated.item)))
177                 return -EFAULT;
178         err = file->f_op->ioctl(file->f_dentry->d_inode, file, native_ctl, (unsigned long)data);
179         if (err < 0)
180                 return err;
181         /* restore info to 32bit */
182         /* for COPY_CVT macro */
183         src = data;
184         dst = data32;
185         /* id, type, access, count */
186         if (copy_in_user(&data32->id, &data->id, sizeof(data->id)) ||
187             copy_in_user(&data32->type, &data->type, 3 * sizeof(u32)))
188                 return -EFAULT;
189         COPY_CVT(owner);
190         __get_user(type, &data->type);
191         switch (type) {
192         case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
193         case SNDRV_CTL_ELEM_TYPE_INTEGER:
194                 COPY_CVT(value.integer.min);
195                 COPY_CVT(value.integer.max);
196                 COPY_CVT(value.integer.step);
197                 break;
198         case SNDRV_CTL_ELEM_TYPE_INTEGER64:
199                 if (copy_in_user(&data32->value.integer64,
200                                  &data->value.integer64,
201                                  sizeof(data->value.integer64)))
202                         return -EFAULT;
203                 break;
204         case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
205                 if (copy_in_user(&data32->value.enumerated,
206                                  &data->value.enumerated,
207                                  sizeof(data->value.enumerated)))
208                         return -EFAULT;
209                 break;
210         default:
211                 break;
212         }
213         return 0;
214 }
215
216 DEFINE_ALSA_IOCTL_ENTRY(ctl_elem_info, ctl_elem_info, SNDRV_CTL_IOCTL_ELEM_INFO);
217
218 struct sndrv_ctl_elem_value32 {
219         struct sndrv_ctl_elem_id id;
220         unsigned int indirect;  /* bit-field causes misalignment */
221         union {
222                 s32 integer[128];       /* integer and boolean need conversion */
223 #ifndef CONFIG_X86_64
224                 s64 integer64[64];      /* for alignment */
225 #endif
226                 unsigned char data[512];        /* others should be compatible */
227         } value;
228         unsigned char reserved[128];    /* not used */
229 };
230
231
232 /* hmm, it's so hard to retrieve the value type from the control id.. */
233 static int get_ctl_type(snd_card_t *card, snd_ctl_elem_id_t *id)
234 {
235         snd_kcontrol_t *kctl;
236         snd_ctl_elem_info_t info;
237         int err;
238
239         down_read(&card->controls_rwsem);
240         kctl = snd_ctl_find_id(card, id);
241         if (! kctl) {
242                 up_read(&card->controls_rwsem);
243                 return -ENXIO;
244         }
245         info.id = *id;
246         err = kctl->info(kctl, &info);
247         up_read(&card->controls_rwsem);
248         if (err >= 0)
249                 err = info.type;
250         return err;
251 }
252
253 extern int snd_major;
254
255 static inline int _snd_ioctl32_ctl_elem_value(unsigned int fd, unsigned int cmd, unsigned long arg, struct file *file, unsigned int native_ctl)
256 {
257         struct sndrv_ctl_elem_value *data;
258         struct sndrv_ctl_elem_value32 __user *data32;
259         snd_ctl_file_t *ctl;
260         int err, i, indirect;
261         int type;
262
263         /* sanity check */
264         if (imajor(file->f_dentry->d_inode) != snd_major ||
265             SNDRV_MINOR_DEVICE(iminor(file->f_dentry->d_inode)) != SNDRV_MINOR_CONTROL)
266                 return -ENOTTY;
267
268         if ((ctl = file->private_data) == NULL)
269                 return -ENOTTY;
270
271         data32 = compat_ptr(arg);
272         data = kcalloc(1, sizeof(*data), GFP_KERNEL);
273         if (data == NULL)
274                 return -ENOMEM;
275
276         if (copy_from_user(&data->id, &data32->id, sizeof(data->id))) {
277                 err = -EFAULT;
278                 goto __end;
279         }
280         if (__get_user(indirect, &data32->indirect)) {
281                 err = -EFAULT;
282                 goto __end;
283         }
284         /* FIXME: indirect access is not supported */
285         if (indirect) {
286                 err = -EINVAL;
287                 goto __end;
288         }
289         type = get_ctl_type(ctl->card, &data->id);
290         if (type < 0) {
291                 err = type;
292                 goto __end;
293         }
294
295         switch (type) {
296         case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
297         case SNDRV_CTL_ELEM_TYPE_INTEGER:
298                 for (i = 0; i < 128; i++) {
299                         int val;
300                         if (__get_user(val, &data32->value.integer[i])) {
301                                 err = -EFAULT;
302                                 goto __end;
303                         }
304                         data->value.integer.value[i] = val;
305                 }
306                 break;
307         case SNDRV_CTL_ELEM_TYPE_INTEGER64:
308         case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
309         case SNDRV_CTL_ELEM_TYPE_BYTES:
310         case SNDRV_CTL_ELEM_TYPE_IEC958:
311                 if (__copy_from_user(data->value.bytes.data,
312                                      data32->value.data,
313                                      sizeof(data32->value.data))) {
314                         err = -EFAULT;
315                         goto __end;
316                 }
317                 break;
318         default:
319                 printk(KERN_ERR "snd_ioctl32_ctl_elem_value: unknown type %d\n", type);
320                 err = -EINVAL;
321                 goto __end;
322         }
323
324         if (native_ctl == SNDRV_CTL_IOCTL_ELEM_READ)
325                 err = snd_ctl_elem_read(ctl->card, data);
326         else
327                 err = snd_ctl_elem_write(ctl->card, ctl, data);
328         if (err < 0)
329                 goto __end;
330         /* restore info to 32bit */
331         switch (type) {
332         case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
333         case SNDRV_CTL_ELEM_TYPE_INTEGER:
334                 for (i = 0; i < 128; i++) {
335                         int val;
336                         val = data->value.integer.value[i];
337                         if (__put_user(val, &data32->value.integer[i])) {
338                                 err = -EFAULT;
339                                 goto __end;
340                         }
341                 }
342                 break;
343         default:
344                 if (__copy_to_user(data32->value.data,
345                                    data->value.bytes.data,
346                                    sizeof(data32->value.data))) {
347                         err = -EFAULT;
348                         goto __end;
349                 }
350                 break;
351                 break;
352         }
353         err = 0;
354       __end:
355         kfree(data);
356         return err;
357 }
358
359 DEFINE_ALSA_IOCTL_ENTRY(ctl_elem_read, ctl_elem_value, SNDRV_CTL_IOCTL_ELEM_READ);
360 DEFINE_ALSA_IOCTL_ENTRY(ctl_elem_write, ctl_elem_value, SNDRV_CTL_IOCTL_ELEM_WRITE);
361
362 /*
363  */
364
365 #define AP(x) snd_ioctl32_##x
366
367 enum {
368         SNDRV_CTL_IOCTL_ELEM_LIST32 = _IOWR('U', 0x10, struct sndrv_ctl_elem_list32),
369         SNDRV_CTL_IOCTL_ELEM_INFO32 = _IOWR('U', 0x11, struct sndrv_ctl_elem_info32),
370         SNDRV_CTL_IOCTL_ELEM_READ32 = _IOWR('U', 0x12, struct sndrv_ctl_elem_value32),
371         SNDRV_CTL_IOCTL_ELEM_WRITE32 = _IOWR('U', 0x13, struct sndrv_ctl_elem_value32),
372 };
373
374 static struct ioctl32_mapper control_mappers[] = {
375         /* controls (without rawmidi, hwdep, timer releated ones) */
376         MAP_COMPAT(SNDRV_CTL_IOCTL_PVERSION),
377         MAP_COMPAT(SNDRV_CTL_IOCTL_CARD_INFO),
378         { SNDRV_CTL_IOCTL_ELEM_LIST32, AP(ctl_elem_list) },
379         { SNDRV_CTL_IOCTL_ELEM_INFO32, AP(ctl_elem_info) },
380         { SNDRV_CTL_IOCTL_ELEM_READ32, AP(ctl_elem_read) },
381         { SNDRV_CTL_IOCTL_ELEM_WRITE32, AP(ctl_elem_write) },
382         MAP_COMPAT(SNDRV_CTL_IOCTL_ELEM_LOCK),
383         MAP_COMPAT(SNDRV_CTL_IOCTL_ELEM_UNLOCK),
384         MAP_COMPAT(SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS),
385         MAP_COMPAT(SNDRV_CTL_IOCTL_HWDEP_INFO),
386         MAP_COMPAT(SNDRV_CTL_IOCTL_HWDEP_NEXT_DEVICE),
387         MAP_COMPAT(SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE),
388         MAP_COMPAT(SNDRV_CTL_IOCTL_PCM_INFO),
389         MAP_COMPAT(SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE),
390         MAP_COMPAT(SNDRV_CTL_IOCTL_POWER),
391         MAP_COMPAT(SNDRV_CTL_IOCTL_POWER_STATE),
392         { 0 }
393 };
394
395
396 /*
397  */
398
399 extern struct ioctl32_mapper pcm_mappers[];
400 extern struct ioctl32_mapper rawmidi_mappers[];
401 extern struct ioctl32_mapper timer_mappers[];
402 extern struct ioctl32_mapper hwdep_mappers[];
403 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
404 extern struct ioctl32_mapper seq_mappers[];
405 #endif
406
407 static void snd_ioctl32_done(void)
408 {
409 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
410         snd_ioctl32_unregister(seq_mappers);
411 #endif
412         snd_ioctl32_unregister(hwdep_mappers);
413         snd_ioctl32_unregister(timer_mappers);
414         snd_ioctl32_unregister(rawmidi_mappers);
415         snd_ioctl32_unregister(pcm_mappers);
416         snd_ioctl32_unregister(control_mappers);
417 }
418
419 static int __init snd_ioctl32_init(void)
420 {
421         snd_ioctl32_register(control_mappers);
422         snd_ioctl32_register(pcm_mappers);
423         snd_ioctl32_register(rawmidi_mappers);
424         snd_ioctl32_register(timer_mappers);
425         snd_ioctl32_register(hwdep_mappers);
426 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
427         snd_ioctl32_register(seq_mappers);
428 #endif
429         return 0;
430 }
431
432 module_init(snd_ioctl32_init)
433 module_exit(snd_ioctl32_done)