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