~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/core/ioctl32/pcm32.c

Version: ~ [ linux-5.8 ] ~ [ linux-5.7.12 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.55 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.136 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.191 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.232 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.232 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp