VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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 A(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)  (dst->x = src->x)
32 #define CPTR(x)  (dst->x = compat_ptr(src->x))
33
34 #define convert_from_32(type, dstp, srcp)\
35 {\
36         struct sndrv_##type *dst = dstp;\
37         struct sndrv_##type##32 *src = srcp;\
38         CVT_##sndrv_##type();\
39 }
40
41 #define convert_to_32(type, dstp, srcp)\
42 {\
43         struct sndrv_##type *src = srcp;\
44         struct sndrv_##type##32 *dst = dstp;\
45         CVT_##sndrv_##type();\
46 }
47
48
49 #define DEFINE_ALSA_IOCTL(type) \
50 static int _snd_ioctl32_##type(unsigned int fd, unsigned int cmd, unsigned long arg, struct file *file, unsigned int native_ctl)\
51 {\
52         struct sndrv_##type##32 data32;\
53         struct sndrv_##type data;\
54         mm_segment_t oldseg;\
55         int err;\
56         if (copy_from_user(&data32, (void __user *)arg, sizeof(data32)))\
57                 return -EFAULT;\
58         memset(&data, 0, sizeof(data));\
59         convert_from_32(type, &data, &data32);\
60         oldseg = get_fs();\
61         set_fs(KERNEL_DS);\
62         err = file->f_op->ioctl(file->f_dentry->d_inode, file, native_ctl, (unsigned long)&data);\
63         set_fs(oldseg);\
64         if (err < 0) \
65                 return err;\
66         if (native_ctl & (_IOC_READ << _IOC_DIRSHIFT)) {\
67                 convert_to_32(type, &data32, &data);\
68                 if (copy_to_user((void __user *)arg, &data32, sizeof(data32)))\
69                         return -EFAULT;\
70         }\
71         return 0;\
72 }
73
74 #define DEFINE_ALSA_IOCTL_BIG(type) \
75 static int _snd_ioctl32_##type(unsigned int fd, unsigned int cmd, unsigned long arg, struct file *file, unsigned int native_ctl)\
76 {\
77         struct sndrv_##type##32 *data32;\
78         struct sndrv_##type *data;\
79         mm_segment_t oldseg;\
80         int err;\
81         data32 = kmalloc(sizeof(*data32), GFP_KERNEL); \
82         data = kmalloc(sizeof(*data), GFP_KERNEL); \
83         if (data32 == NULL || data == NULL) { \
84                 err = -ENOMEM; \
85                 goto __end; \
86         }\
87         if (copy_from_user(data32, (void __user *)arg, sizeof(*data32))) { \
88                 err = -EFAULT; \
89                 goto __end; \
90         }\
91         memset(data, 0, sizeof(*data));\
92         convert_from_32(type, data, data32);\
93         oldseg = get_fs();\
94         set_fs(KERNEL_DS);\
95         err = file->f_op->ioctl(file->f_dentry->d_inode, file, native_ctl, (unsigned long)data);\
96         set_fs(oldseg);\
97         if (err < 0) \
98                 goto __end;\
99         err = 0;\
100         if (native_ctl & (_IOC_READ << _IOC_DIRSHIFT)) {\
101                 convert_to_32(type, data32, data);\
102                 if (copy_to_user((void __user *)arg, data32, sizeof(*data32)))\
103                         err = -EFAULT;\
104         }\
105       __end:\
106         if (data)\
107                 kfree(data);\
108         if (data32)\
109                 kfree(data32);\
110         return err;\
111 }
112
113 #define DEFINE_ALSA_IOCTL_ENTRY(name,type,native_ctl) \
114 static int snd_ioctl32_##name(unsigned int fd, unsigned int cmd, unsigned long arg, struct file *file) {\
115         return _snd_ioctl32_##type(fd, cmd, arg, file, native_ctl);\
116 }
117
118 #define MAP_COMPAT(ctl) { ctl, snd_ioctl32_compat }
119
120 struct ioctl32_mapper {
121         unsigned int cmd;
122         int (*handler)(unsigned int, unsigned int, unsigned long, struct file * filp);
123         int registered;
124 };
125
126 int snd_ioctl32_compat(unsigned int, unsigned int, unsigned long, struct file *);
127
128 int snd_ioctl32_register(struct ioctl32_mapper *mappers);
129 void snd_ioctl32_unregister(struct ioctl32_mapper *mappers);
130
131 #endif /* __ALSA_IOCTL32_H */