2 * 32bit -> 64bit ioctl wrapper for PCM API
3 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
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.
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.
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
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>
31 /* wrapper for sndrv_pcm_[us]frames */
32 struct sndrv_pcm_sframes_str {
33 sndrv_pcm_sframes_t val;
35 struct sndrv_pcm_sframes_str32 {
38 struct sndrv_pcm_uframes_str {
39 sndrv_pcm_uframes_t val;
41 struct sndrv_pcm_uframes_str32 {
45 #define CVT_sndrv_pcm_sframes_str() { COPY_CVT(val); }
46 #define CVT_sndrv_pcm_uframes_str() { COPY_CVT(val); }
49 struct sndrv_pcm_hw_params32 {
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 */
62 unsigned char reserved[64];
63 } __attribute__((packed));
65 struct sndrv_pcm_sw_params32 {
73 u32 silence_threshold;
76 unsigned char reserved[64];
77 } __attribute__((packed));
79 #define CVT_sndrv_pcm_sw_params()\
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);\
93 struct sndrv_pcm_channel_info32 {
98 } __attribute__((packed));
100 #define CVT_sndrv_pcm_channel_info()\
108 struct sndrv_pcm_status32 {
110 struct compat_timespec trigger_tstamp;
111 struct compat_timespec tstamp;
119 unsigned char reserved[60];
120 } __attribute__((packed));
122 #define CVT_sndrv_pcm_status()\
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);\
133 COPY_CVT(avail_max);\
134 COPY_CVT(overrange);\
135 COPY(suspended_state);\
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);
144 /* sanity device check */
145 extern int snd_major;
146 static int sanity_check_pcm(struct file *file)
148 unsigned short minor;
149 if (imajor(file->f_dentry->d_inode) != snd_major)
151 minor = iminor(file->f_dentry->d_inode);
153 minor % SNDRV_MINOR_DEVICES < SNDRV_MINOR_PCM_PLAYBACK)
158 /* recalcuate the boundary within 32bit */
159 static void recalculate_boundary(snd_pcm_runtime_t *runtime)
161 if (! runtime->buffer_size)
163 runtime->boundary = runtime->buffer_size;
164 while (runtime->boundary * 2 <= 0x7fffffffUL - runtime->buffer_size)
165 runtime->boundary *= 2;
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)
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;
178 if (sanity_check_pcm(file))
180 if (! (pcm_file = file->private_data))
182 if (! (substream = pcm_file->substream))
184 if (! (runtime = substream->runtime))
187 data32 = compat_ptr(arg);
188 data = kmalloc(sizeof(*data), GFP_KERNEL);
191 if (copy_from_user(data, data32, sizeof(*data32))) {
195 if (native_ctl == SNDRV_PCM_IOCTL_HW_REFINE)
196 err = snd_pcm_hw_refine(substream, data);
198 err = snd_pcm_hw_params(substream, data);
201 if (copy_to_user(data32, data, sizeof(*data32)) ||
202 __put_user((u32)data->fifo_size, &data32->fifo_size)) {
207 if (native_ctl == SNDRV_PCM_IOCTL_HW_PARAMS)
208 recalculate_boundary(runtime);
217 struct sndrv_xferi32 {
221 } __attribute__((packed));
223 static int _snd_ioctl32_xferi(unsigned int fd, unsigned int cmd, unsigned long arg, struct file *file, unsigned int native_ctl)
225 struct sndrv_xferi32 data32;
226 struct sndrv_xferi __user *data;
227 snd_pcm_sframes_t result;
230 if (copy_from_user(&data32, (void __user *)arg, sizeof(data32)))
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))
237 err = file->f_op->ioctl(file->f_dentry->d_inode, file, native_ctl, (unsigned long)data);
240 /* copy the result */
241 if (__get_user(result, &data->result))
243 data32.result = result;
244 if (copy_to_user((void __user *)arg, &data32, sizeof(data32)))
250 /* snd_xfern needs remapping of bufs */
251 struct sndrv_xfern32 {
253 u32 bufs; /* this is void **; */
255 } __attribute__((packed));
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.
263 static int _snd_ioctl32_xfern(unsigned int fd, unsigned int cmd, unsigned long arg, struct file *file, unsigned int native_ctl)
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;
273 if (sanity_check_pcm(file))
275 if (! (pcm_file = file->private_data))
277 if (! (substream = pcm_file->substream))
279 if (! substream->runtime)
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)
287 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
290 case SNDRV_PCM_IOCTL_READN_FRAMES:
291 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
295 if ((ch = substream->runtime->channels) > 128)
297 if (copy_from_user(&data32, (void __user *)arg, sizeof(data32)))
299 bufptr = compat_ptr(data32.bufs);
300 bufs = kmalloc(sizeof(void __user *) * ch, GFP_KERNEL);
303 for (i = 0; i < ch; i++) {
305 if (get_user(ptr, bufptr)) {
309 bufs[ch] = compat_ptr(ptr);
312 switch (native_ctl) {
313 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
314 err = snd_pcm_lib_writev(substream, bufs, data32.frames);
316 case SNDRV_PCM_IOCTL_READN_FRAMES:
317 err = snd_pcm_lib_readv(substream, bufs, data32.frames);
321 if (put_user(err, &srcptr->result))
329 struct sndrv_pcm_mmap_status32 {
333 struct compat_timespec tstamp;
335 } __attribute__((packed));
337 struct sndrv_pcm_mmap_control32 {
340 } __attribute__((packed));
342 struct sndrv_pcm_sync_ptr32 {
345 struct sndrv_pcm_mmap_status32 status;
346 unsigned char reserved[64];
349 struct sndrv_pcm_mmap_control32 control;
350 unsigned char reserved[64];
352 } __attribute__((packed));
354 #define CVT_sndrv_pcm_sync_ptr()\
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);\
367 DEFINE_ALSA_IOCTL(pcm_sync_ptr);
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);
388 * When PCM is used on 32bit mode, we need to disable
389 * mmap of PCM status/control records because of the size
392 * Since INFO ioctl is always called at first, we mark the
393 * mmap-disabling in this ioctl wrapper.
395 static int snd_pcm_info_ioctl32(unsigned int fd, unsigned int cmd, unsigned long arg, struct file *filp)
397 snd_pcm_file_t *pcm_file;
398 snd_pcm_substream_t *substream;
399 if (! filp->f_op || ! filp->f_op->ioctl)
401 pcm_file = filp->private_data;
404 substream = pcm_file->substream;
407 substream->no_mmap_ctrl = 1;
408 return filp->f_op->ioctl(filp->f_dentry->d_inode, filp, cmd, arg);
413 #define AP(x) snd_ioctl32_##x
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),
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),