vserver 1.9.5.x5
[linux-2.6.git] / sound / core / ioctl32 / pcm32.c
1 /*
2  *   32bit -> 64bit ioctl wrapper for PCM 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/time.h>
23 #include <linux/slab.h>
24 #include <linux/compat.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/minors.h>
28 #include "ioctl32.h"
29
30
31 /* wrapper for sndrv_pcm_[us]frames */
32 struct sndrv_pcm_sframes_str {
33         sndrv_pcm_sframes_t val;
34 };
35 struct sndrv_pcm_sframes_str32 {
36         s32 val;
37 };
38 struct sndrv_pcm_uframes_str {
39         sndrv_pcm_uframes_t val;
40 };
41 struct sndrv_pcm_uframes_str32 {
42         u32 val;
43 };
44
45 #define CVT_sndrv_pcm_sframes_str() { COPY_CVT(val); }
46 #define CVT_sndrv_pcm_uframes_str() { COPY_CVT(val); }
47
48
49 struct sndrv_pcm_hw_params32 {
50         u32 flags;
51         struct sndrv_mask masks[SNDRV_PCM_HW_PARAM_LAST_MASK - SNDRV_PCM_HW_PARAM_FIRST_MASK + 1]; /* this must be identical */
52         struct sndrv_mask mres[5];      /* reserved masks */
53         struct sndrv_interval intervals[SNDRV_PCM_HW_PARAM_LAST_INTERVAL - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL + 1];
54         struct sndrv_interval ires[9];  /* reserved intervals */
55         u32 rmask;
56         u32 cmask;
57         u32 info;
58         u32 msbits;
59         u32 rate_num;
60         u32 rate_den;
61         u32 fifo_size;
62         unsigned char reserved[64];
63 } __attribute__((packed));
64
65 struct sndrv_pcm_sw_params32 {
66         s32 tstamp_mode;
67         u32 period_step;
68         u32 sleep_min;
69         u32 avail_min;
70         u32 xfer_align;
71         u32 start_threshold;
72         u32 stop_threshold;
73         u32 silence_threshold;
74         u32 silence_size;
75         u32 boundary;
76         unsigned char reserved[64];
77 } __attribute__((packed));
78
79 #define CVT_sndrv_pcm_sw_params()\
80 {\
81         COPY(tstamp_mode);\
82         COPY(period_step);\
83         COPY(sleep_min);\
84         COPY_CVT(avail_min);\
85         COPY_CVT(xfer_align);\
86         COPY_CVT(start_threshold);\
87         COPY_CVT(stop_threshold);\
88         COPY_CVT(silence_threshold);\
89         COPY_CVT(silence_size);\
90         COPY_CVT(boundary);\
91 }
92
93 struct sndrv_pcm_channel_info32 {
94         u32 channel;
95         u32 offset;
96         u32 first;
97         u32 step;
98 } __attribute__((packed));
99
100 #define CVT_sndrv_pcm_channel_info()\
101 {\
102         COPY(channel);\
103         COPY_CVT(offset);\
104         COPY(first);\
105         COPY(step);\
106 }
107
108 struct sndrv_pcm_status32 {
109         s32 state;
110         struct compat_timespec trigger_tstamp;
111         struct compat_timespec tstamp;
112         u32 appl_ptr;
113         u32 hw_ptr;
114         s32 delay;
115         u32 avail;
116         u32 avail_max;
117         u32 overrange;
118         s32 suspended_state;
119         unsigned char reserved[60];
120 } __attribute__((packed));
121
122 #define CVT_sndrv_pcm_status()\
123 {\
124         COPY(state);\
125         COPY_CVT(trigger_tstamp.tv_sec);\
126         COPY_CVT(trigger_tstamp.tv_nsec);\
127         COPY_CVT(tstamp.tv_sec);\
128         COPY_CVT(tstamp.tv_nsec);\
129         COPY_CVT(appl_ptr);\
130         COPY_CVT(hw_ptr);\
131         COPY_CVT(delay);\
132         COPY_CVT(avail);\
133         COPY_CVT(avail_max);\
134         COPY_CVT(overrange);\
135         COPY(suspended_state);\
136 }
137
138 DEFINE_ALSA_IOCTL(pcm_uframes_str);
139 DEFINE_ALSA_IOCTL(pcm_sframes_str);
140 DEFINE_ALSA_IOCTL(pcm_sw_params);
141 DEFINE_ALSA_IOCTL(pcm_channel_info);
142 DEFINE_ALSA_IOCTL(pcm_status);
143
144 /* sanity device check */
145 extern int snd_major;
146 static int sanity_check_pcm(struct file *file)
147 {
148         unsigned short minor;
149         if (imajor(file->f_dentry->d_inode) != snd_major)
150                 return -ENOTTY;
151         minor = iminor(file->f_dentry->d_inode);
152         if (minor >= 256 || 
153             minor % SNDRV_MINOR_DEVICES < SNDRV_MINOR_PCM_PLAYBACK)
154                 return -ENOTTY;
155         return 0;
156 }
157
158 /* recalcuate the boundary within 32bit */
159 static void recalculate_boundary(snd_pcm_runtime_t *runtime)
160 {
161         if (! runtime->buffer_size)
162                 return;
163         runtime->boundary = runtime->buffer_size;
164         while (runtime->boundary * 2 <= 0x7fffffffUL - runtime->buffer_size)
165                 runtime->boundary *= 2;
166 }
167
168 /* both for HW_PARAMS and HW_REFINE */
169 static int _snd_ioctl32_pcm_hw_params(unsigned int fd, unsigned int cmd, unsigned long arg, struct file *file, unsigned int native_ctl)
170 {
171         struct sndrv_pcm_hw_params32 __user *data32;
172         struct sndrv_pcm_hw_params *data;
173         snd_pcm_file_t *pcm_file;
174         snd_pcm_substream_t *substream;
175         snd_pcm_runtime_t *runtime;
176         int err;
177
178         if (sanity_check_pcm(file))
179                 return -ENOTTY;
180         if (! (pcm_file = file->private_data))
181                 return -ENOTTY;
182         if (! (substream = pcm_file->substream))
183                 return -ENOTTY;
184         if (! (runtime = substream->runtime))
185                 return -ENOTTY;
186
187         data32 = compat_ptr(arg);
188         data = kmalloc(sizeof(*data), GFP_KERNEL);
189         if (data == NULL)
190                 return -ENOMEM;
191         if (copy_from_user(data, data32, sizeof(*data32))) {
192                 err = -EFAULT;
193                 goto error;
194         }
195         if (native_ctl == SNDRV_PCM_IOCTL_HW_REFINE)
196                 err = snd_pcm_hw_refine(substream, data);
197         else
198                 err = snd_pcm_hw_params(substream, data);
199         if (err < 0)
200                 goto error;
201         if (copy_to_user(data32, data, sizeof(*data32)) ||
202             __put_user((u32)data->fifo_size, &data32->fifo_size)) {
203                 err = -EFAULT;
204                 goto error;
205         }
206
207         if (native_ctl == SNDRV_PCM_IOCTL_HW_PARAMS)
208                 recalculate_boundary(runtime);
209  error:
210         kfree(data);
211         return err;
212 }
213
214
215 /*
216  */
217 struct sndrv_xferi32 {
218         s32 result;
219         u32 buf;
220         u32 frames;
221 } __attribute__((packed));
222
223 static int _snd_ioctl32_xferi(unsigned int fd, unsigned int cmd, unsigned long arg, struct file *file, unsigned int native_ctl)
224 {
225         struct sndrv_xferi32 data32;
226         struct sndrv_xferi __user *data;
227         snd_pcm_sframes_t result;
228         int err;
229
230         if (copy_from_user(&data32, (void __user *)arg, sizeof(data32)))
231                 return -EFAULT;
232         data = compat_alloc_user_space(sizeof(*data));
233         if (put_user((snd_pcm_sframes_t)data32.result, &data->result) ||
234             __put_user(compat_ptr(data32.buf), &data->buf) ||
235             __put_user((snd_pcm_uframes_t)data32.frames, &data->frames))
236                 return -EFAULT;
237         err = file->f_op->ioctl(file->f_dentry->d_inode, file, native_ctl, (unsigned long)data);
238         if (err < 0)
239                 return err;
240         /* copy the result */
241         if (__get_user(result, &data->result))
242                 return -EFAULT;
243         data32.result = result;
244         if (copy_to_user((void __user *)arg, &data32, sizeof(data32)))
245                 return -EFAULT;
246         return 0;
247 }
248
249
250 /* snd_xfern needs remapping of bufs */
251 struct sndrv_xfern32 {
252         s32 result;
253         u32 bufs;  /* this is void **; */
254         u32 frames;
255 } __attribute__((packed));
256
257 /*
258  * xfern ioctl nees to copy (up to) 128 pointers on stack.
259  * although we may pass the copied pointers through f_op->ioctl, but the ioctl
260  * handler there expands again the same 128 pointers on stack, so it is better
261  * to handle the function (calling pcm_readv/writev) directly in this handler.
262  */
263 static int _snd_ioctl32_xfern(unsigned int fd, unsigned int cmd, unsigned long arg, struct file *file, unsigned int native_ctl)
264 {
265         snd_pcm_file_t *pcm_file;
266         snd_pcm_substream_t *substream;
267         struct sndrv_xfern32 __user *srcptr = compat_ptr(arg);
268         struct sndrv_xfern32 data32;
269         void __user **bufs;
270         int err = 0, ch, i;
271         u32 __user *bufptr;
272
273         if (sanity_check_pcm(file))
274                 return -ENOTTY;
275         if (! (pcm_file = file->private_data))
276                 return -ENOTTY;
277         if (! (substream = pcm_file->substream))
278                 return -ENOTTY;
279         if (! substream->runtime)
280                 return -ENOTTY;
281
282         /* check validty of the command */
283         switch (native_ctl) {
284         case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
285                 if (substream->stream  != SNDRV_PCM_STREAM_PLAYBACK)
286                         return -EINVAL;
287                 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
288                         return -EBADFD;
289                 break;
290         case SNDRV_PCM_IOCTL_READN_FRAMES:
291                 if (substream->stream  != SNDRV_PCM_STREAM_CAPTURE)
292                         return -EINVAL;
293                 break;
294         }
295         if ((ch = substream->runtime->channels) > 128)
296                 return -EINVAL;
297         if (copy_from_user(&data32, (void __user *)arg, sizeof(data32)))
298                 return -EFAULT;
299         bufptr = compat_ptr(data32.bufs);
300         bufs = kmalloc(sizeof(void __user *) * ch, GFP_KERNEL);
301         if (bufs == NULL)
302                 return -ENOMEM;
303         for (i = 0; i < ch; i++) {
304                 u32 ptr;
305                 if (get_user(ptr, bufptr)) {
306                         kfree(bufs);
307                         return -EFAULT;
308                 }
309                 bufs[ch] = compat_ptr(ptr);
310                 bufptr++;
311         }
312         switch (native_ctl) {
313         case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
314                 err = snd_pcm_lib_writev(substream, bufs, data32.frames);
315                 break;
316         case SNDRV_PCM_IOCTL_READN_FRAMES:
317                 err = snd_pcm_lib_readv(substream, bufs, data32.frames);
318                 break;
319         }
320         if (err >= 0) {
321                 if (put_user(err, &srcptr->result))
322                         err = -EFAULT;
323         }
324         kfree(bufs);
325         return err;
326 }
327
328
329 struct sndrv_pcm_mmap_status32 {
330         s32 state;
331         s32 pad1;
332         u32 hw_ptr;
333         struct compat_timespec tstamp;
334         s32 suspended_state;
335 } __attribute__((packed));
336
337 struct sndrv_pcm_mmap_control32 {
338         u32 appl_ptr;
339         u32 avail_min;
340 } __attribute__((packed));
341
342 struct sndrv_pcm_sync_ptr32 {
343         u32 flags;
344         union {
345                 struct sndrv_pcm_mmap_status32 status;
346                 unsigned char reserved[64];
347         } s;
348         union {
349                 struct sndrv_pcm_mmap_control32 control;
350                 unsigned char reserved[64];
351         } c;
352 } __attribute__((packed));
353
354 #define CVT_sndrv_pcm_sync_ptr()\
355 {\
356         COPY(flags);\
357         COPY(s.status.state);\
358         COPY(s.status.pad1);\
359         COPY_CVT(s.status.hw_ptr);\
360         COPY_CVT(s.status.tstamp.tv_sec);\
361         COPY_CVT(s.status.tstamp.tv_nsec);\
362         COPY(s.status.suspended_state);\
363         COPY_CVT(c.control.appl_ptr);\
364         COPY_CVT(c.control.avail_min);\
365 }
366
367 DEFINE_ALSA_IOCTL(pcm_sync_ptr);
368
369 /*
370  */
371
372 DEFINE_ALSA_IOCTL_ENTRY(pcm_hw_refine, pcm_hw_params, SNDRV_PCM_IOCTL_HW_REFINE);
373 DEFINE_ALSA_IOCTL_ENTRY(pcm_hw_params, pcm_hw_params, SNDRV_PCM_IOCTL_HW_PARAMS);
374 DEFINE_ALSA_IOCTL_ENTRY(pcm_sw_params, pcm_sw_params, SNDRV_PCM_IOCTL_SW_PARAMS);
375 DEFINE_ALSA_IOCTL_ENTRY(pcm_status, pcm_status, SNDRV_PCM_IOCTL_STATUS);
376 DEFINE_ALSA_IOCTL_ENTRY(pcm_delay, pcm_sframes_str, SNDRV_PCM_IOCTL_DELAY);
377 DEFINE_ALSA_IOCTL_ENTRY(pcm_channel_info, pcm_channel_info, SNDRV_PCM_IOCTL_CHANNEL_INFO);
378 DEFINE_ALSA_IOCTL_ENTRY(pcm_rewind, pcm_uframes_str, SNDRV_PCM_IOCTL_REWIND);
379 DEFINE_ALSA_IOCTL_ENTRY(pcm_forward, pcm_uframes_str, SNDRV_PCM_IOCTL_FORWARD);
380 DEFINE_ALSA_IOCTL_ENTRY(pcm_readi, xferi, SNDRV_PCM_IOCTL_READI_FRAMES);
381 DEFINE_ALSA_IOCTL_ENTRY(pcm_writei, xferi, SNDRV_PCM_IOCTL_WRITEI_FRAMES);
382 DEFINE_ALSA_IOCTL_ENTRY(pcm_readn, xfern, SNDRV_PCM_IOCTL_READN_FRAMES);
383 DEFINE_ALSA_IOCTL_ENTRY(pcm_writen, xfern, SNDRV_PCM_IOCTL_WRITEN_FRAMES);
384 DEFINE_ALSA_IOCTL_ENTRY(pcm_sync_ptr, pcm_sync_ptr, SNDRV_PCM_IOCTL_SYNC_PTR);
385
386
387 /*
388  * When PCM is used on 32bit mode, we need to disable
389  * mmap of PCM status/control records because of the size
390  * incompatibility.
391  * 
392  * Since INFO ioctl is always called at first, we mark the
393  * mmap-disabling in this ioctl wrapper.
394  */
395 static int snd_pcm_info_ioctl32(unsigned int fd, unsigned int cmd, unsigned long arg, struct file *filp)
396 {
397         snd_pcm_file_t *pcm_file;
398         snd_pcm_substream_t *substream;
399         if (! filp->f_op || ! filp->f_op->ioctl)
400                 return -ENOTTY;
401         pcm_file = filp->private_data;
402         if (! pcm_file)
403                 return -ENOTTY;
404         substream = pcm_file->substream;
405         if (! substream)
406                 return -ENOTTY;
407         substream->no_mmap_ctrl = 1;
408         return filp->f_op->ioctl(filp->f_dentry->d_inode, filp, cmd, arg);
409 }
410
411 /*
412  */
413 #define AP(x) snd_ioctl32_##x
414
415 enum {
416         SNDRV_PCM_IOCTL_HW_REFINE32 = _IOWR('A', 0x10, struct sndrv_pcm_hw_params32),
417         SNDRV_PCM_IOCTL_HW_PARAMS32 = _IOWR('A', 0x11, struct sndrv_pcm_hw_params32),
418         SNDRV_PCM_IOCTL_SW_PARAMS32 = _IOWR('A', 0x13, struct sndrv_pcm_sw_params32),
419         SNDRV_PCM_IOCTL_STATUS32 = _IOR('A', 0x20, struct sndrv_pcm_status32),
420         SNDRV_PCM_IOCTL_DELAY32 = _IOR('A', 0x21, s32),
421         SNDRV_PCM_IOCTL_CHANNEL_INFO32 = _IOR('A', 0x32, struct sndrv_pcm_channel_info32),
422         SNDRV_PCM_IOCTL_REWIND32 = _IOW('A', 0x46, u32),
423         SNDRV_PCM_IOCTL_FORWARD32 = _IOW('A', 0x49, u32),
424         SNDRV_PCM_IOCTL_WRITEI_FRAMES32 = _IOW('A', 0x50, struct sndrv_xferi32),
425         SNDRV_PCM_IOCTL_READI_FRAMES32 = _IOR('A', 0x51, struct sndrv_xferi32),
426         SNDRV_PCM_IOCTL_WRITEN_FRAMES32 = _IOW('A', 0x52, struct sndrv_xfern32),
427         SNDRV_PCM_IOCTL_READN_FRAMES32 = _IOR('A', 0x53, struct sndrv_xfern32),
428         SNDRV_PCM_IOCTL_SYNC_PTR32 = _IOWR('A', 0x23, struct sndrv_pcm_sync_ptr32),
429
430 };
431
432 struct ioctl32_mapper pcm_mappers[] = {
433         MAP_COMPAT(SNDRV_PCM_IOCTL_PVERSION),
434         /* MAP_COMPAT(SNDRV_PCM_IOCTL_INFO), */
435         { SNDRV_PCM_IOCTL_INFO, snd_pcm_info_ioctl32 },
436         MAP_COMPAT(SNDRV_PCM_IOCTL_TSTAMP),
437         { SNDRV_PCM_IOCTL_HW_REFINE32, AP(pcm_hw_refine) },
438         { SNDRV_PCM_IOCTL_HW_PARAMS32, AP(pcm_hw_params) },
439         MAP_COMPAT(SNDRV_PCM_IOCTL_HW_FREE),
440         { SNDRV_PCM_IOCTL_SW_PARAMS32, AP(pcm_sw_params) },
441         { SNDRV_PCM_IOCTL_STATUS32, AP(pcm_status) },
442         { SNDRV_PCM_IOCTL_DELAY32, AP(pcm_delay) },
443         MAP_COMPAT(SNDRV_PCM_IOCTL_HWSYNC),
444         { SNDRV_PCM_IOCTL_SYNC_PTR32, AP(pcm_sync_ptr) },
445         { SNDRV_PCM_IOCTL_CHANNEL_INFO32, AP(pcm_channel_info) },
446         MAP_COMPAT(SNDRV_PCM_IOCTL_PREPARE),
447         MAP_COMPAT(SNDRV_PCM_IOCTL_RESET),
448         MAP_COMPAT(SNDRV_PCM_IOCTL_START),
449         MAP_COMPAT(SNDRV_PCM_IOCTL_DROP),
450         MAP_COMPAT(SNDRV_PCM_IOCTL_DRAIN),
451         MAP_COMPAT(SNDRV_PCM_IOCTL_PAUSE),
452         { SNDRV_PCM_IOCTL_REWIND32, AP(pcm_rewind) },
453         MAP_COMPAT(SNDRV_PCM_IOCTL_RESUME),
454         MAP_COMPAT(SNDRV_PCM_IOCTL_XRUN),
455         { SNDRV_PCM_IOCTL_FORWARD32, AP(pcm_forward) },
456         { SNDRV_PCM_IOCTL_WRITEI_FRAMES32, AP(pcm_writei) },
457         { SNDRV_PCM_IOCTL_READI_FRAMES32, AP(pcm_readi) },
458         { SNDRV_PCM_IOCTL_WRITEN_FRAMES32, AP(pcm_writen) },
459         { SNDRV_PCM_IOCTL_READN_FRAMES32, AP(pcm_readn) },
460         MAP_COMPAT(SNDRV_PCM_IOCTL_LINK),
461         MAP_COMPAT(SNDRV_PCM_IOCTL_UNLINK),
462
463         { 0 },
464 };