vserver 1.9.5.x5
[linux-2.6.git] / sound / core / ioctl32 / ioctl32.h
1 /*
2  *   32bit -> 64bit ioctl helpers
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  * This file registers the converters from 32-bit ioctls to 64-bit ones.
21  * The converter assumes that a 32-bit user-pointer can be casted by compat_ptr(x)
22  * macro to a valid 64-bit pointer which is accessible via copy_from/to_user.
23  *
24  */
25
26 #ifndef __ALSA_IOCTL32_H
27 #define __ALSA_IOCTL32_H
28
29 #include <linux/compat.h>
30
31 #define COPY(x) \
32         do { \
33                 if (copy_in_user(&dst->x, &src->x, sizeof(dst->x))) \
34                         return -EFAULT; \
35         } while (0)
36
37 #define COPY_ARRAY(x) \
38         do { \
39                 if (copy_in_user(dst->x, src->x, sizeof(dst->x))) \
40                         return -EFAULT; \
41         } while (0)
42
43 #define COPY_CVT(x) \
44         do { \
45                 __typeof__(src->x) __val_tmp; \
46                 if (get_user(__val_tmp, &src->x) || \
47                     put_user(__val_tmp, &dst->x))\
48                         return -EFAULT; \
49         } while (0)
50
51 #define convert_from_32(type, dstp, srcp)\
52 {\
53         struct sndrv_##type __user *dst = dstp;\
54         struct sndrv_##type##32 __user *src = srcp;\
55         CVT_##sndrv_##type();\
56 }
57
58 #define convert_to_32(type, dstp, srcp)\
59 {\
60         struct sndrv_##type __user *src = srcp;\
61         struct sndrv_##type##32 __user *dst = dstp;\
62         CVT_##sndrv_##type();\
63 }
64
65
66 #define DEFINE_ALSA_IOCTL(type) \
67 static inline int _snd_ioctl32_##type(unsigned int fd, unsigned int cmd, unsigned long arg, struct file *file, unsigned int native_ctl)\
68 {\
69         struct sndrv_##type##32 __user *data32;\
70         struct sndrv_##type __user *data;\
71         int err;\
72         data32 = compat_ptr(arg);\
73         data = compat_alloc_user_space(sizeof(*data));\
74         convert_from_32(type, data, data32);\
75         err = file->f_op->ioctl(file->f_dentry->d_inode, file, native_ctl, (unsigned long)data);\
76         if (err < 0) \
77                 return err;\
78         if (native_ctl & (_IOC_READ << _IOC_DIRSHIFT)) {\
79                 convert_to_32(type, data32, data);\
80         }\
81         return 0;\
82 }
83
84 #define DEFINE_ALSA_IOCTL_ENTRY(name,type,native_ctl) \
85 static int snd_ioctl32_##name(unsigned int fd, unsigned int cmd, unsigned long arg, struct file *file) {\
86         return _snd_ioctl32_##type(fd, cmd, arg, file, native_ctl);\
87 }
88
89 #define MAP_COMPAT(ctl) { ctl, snd_ioctl32_compat }
90
91 struct ioctl32_mapper {
92         unsigned int cmd;
93         int (*handler)(unsigned int, unsigned int, unsigned long, struct file * filp);
94         int registered;
95 };
96
97 int snd_ioctl32_compat(unsigned int, unsigned int, unsigned long, struct file *);
98
99 int snd_ioctl32_register(struct ioctl32_mapper *mappers);
100 void snd_ioctl32_unregister(struct ioctl32_mapper *mappers);
101
102 #endif /* __ALSA_IOCTL32_H */