ALSA: move snd_pcm_ioctl_sync_ptr_compat into pcm_native.c
[muen/linux.git] / sound / core / pcm_compat.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   32bit -> 64bit ioctl wrapper for PCM API
4  *   Copyright (c) by Takashi Iwai <tiwai@suse.de>
5  */
6
7 /* This file included from pcm_native.c */
8
9 #include <linux/compat.h>
10 #include <linux/slab.h>
11
12 static int snd_pcm_ioctl_delay_compat(struct snd_pcm_substream *substream,
13                                       s32 __user *src)
14 {
15         snd_pcm_sframes_t delay;
16         int err;
17
18         err = snd_pcm_delay(substream, &delay);
19         if (err)
20                 return err;
21         if (put_user(delay, src))
22                 return -EFAULT;
23         return 0;
24 }
25
26 static int snd_pcm_ioctl_rewind_compat(struct snd_pcm_substream *substream,
27                                        u32 __user *src)
28 {
29         snd_pcm_uframes_t frames;
30         int err;
31
32         if (get_user(frames, src))
33                 return -EFAULT;
34         err = snd_pcm_rewind(substream, frames);
35         if (put_user(err, src))
36                 return -EFAULT;
37         return err < 0 ? err : 0;
38 }
39
40 static int snd_pcm_ioctl_forward_compat(struct snd_pcm_substream *substream,
41                                        u32 __user *src)
42 {
43         snd_pcm_uframes_t frames;
44         int err;
45
46         if (get_user(frames, src))
47                 return -EFAULT;
48         err = snd_pcm_forward(substream, frames);
49         if (put_user(err, src))
50                 return -EFAULT;
51         return err < 0 ? err : 0;
52 }
53
54 struct snd_pcm_hw_params32 {
55         u32 flags;
56         struct snd_mask masks[SNDRV_PCM_HW_PARAM_LAST_MASK - SNDRV_PCM_HW_PARAM_FIRST_MASK + 1]; /* this must be identical */
57         struct snd_mask mres[5];        /* reserved masks */
58         struct snd_interval intervals[SNDRV_PCM_HW_PARAM_LAST_INTERVAL - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL + 1];
59         struct snd_interval ires[9];    /* reserved intervals */
60         u32 rmask;
61         u32 cmask;
62         u32 info;
63         u32 msbits;
64         u32 rate_num;
65         u32 rate_den;
66         u32 fifo_size;
67         unsigned char reserved[64];
68 };
69
70 struct snd_pcm_sw_params32 {
71         s32 tstamp_mode;
72         u32 period_step;
73         u32 sleep_min;
74         u32 avail_min;
75         u32 xfer_align;
76         u32 start_threshold;
77         u32 stop_threshold;
78         u32 silence_threshold;
79         u32 silence_size;
80         u32 boundary;
81         u32 proto;
82         u32 tstamp_type;
83         unsigned char reserved[56];
84 };
85
86 static int snd_pcm_ioctl_sw_params_compat(struct snd_pcm_substream *substream,
87                                           struct snd_pcm_sw_params32 __user *src)
88 {
89         struct snd_pcm_sw_params params;
90         snd_pcm_uframes_t boundary;
91         int err;
92
93         memset(&params, 0, sizeof(params));
94         if (get_user(params.tstamp_mode, &src->tstamp_mode) ||
95             get_user(params.period_step, &src->period_step) ||
96             get_user(params.sleep_min, &src->sleep_min) ||
97             get_user(params.avail_min, &src->avail_min) ||
98             get_user(params.xfer_align, &src->xfer_align) ||
99             get_user(params.start_threshold, &src->start_threshold) ||
100             get_user(params.stop_threshold, &src->stop_threshold) ||
101             get_user(params.silence_threshold, &src->silence_threshold) ||
102             get_user(params.silence_size, &src->silence_size) ||
103             get_user(params.tstamp_type, &src->tstamp_type) ||
104             get_user(params.proto, &src->proto))
105                 return -EFAULT;
106         /*
107          * Check silent_size parameter.  Since we have 64bit boundary,
108          * silence_size must be compared with the 32bit boundary.
109          */
110         boundary = recalculate_boundary(substream->runtime);
111         if (boundary && params.silence_size >= boundary)
112                 params.silence_size = substream->runtime->boundary;
113         err = snd_pcm_sw_params(substream, &params);
114         if (err < 0)
115                 return err;
116         if (boundary && put_user(boundary, &src->boundary))
117                 return -EFAULT;
118         return err;
119 }
120
121 struct snd_pcm_channel_info32 {
122         u32 channel;
123         u32 offset;
124         u32 first;
125         u32 step;
126 };
127
128 static int snd_pcm_ioctl_channel_info_compat(struct snd_pcm_substream *substream,
129                                              struct snd_pcm_channel_info32 __user *src)
130 {
131         struct snd_pcm_channel_info info;
132         int err;
133
134         if (get_user(info.channel, &src->channel) ||
135             get_user(info.offset, &src->offset) ||
136             get_user(info.first, &src->first) ||
137             get_user(info.step, &src->step))
138                 return -EFAULT;
139         err = snd_pcm_channel_info(substream, &info);
140         if (err < 0)
141                 return err;
142         if (put_user(info.channel, &src->channel) ||
143             put_user(info.offset, &src->offset) ||
144             put_user(info.first, &src->first) ||
145             put_user(info.step, &src->step))
146                 return -EFAULT;
147         return err;
148 }
149
150 #ifdef CONFIG_X86_X32
151 /* X32 ABI has the same struct as x86-64 for snd_pcm_channel_info */
152 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
153                                      struct snd_pcm_channel_info __user *src);
154 #define snd_pcm_ioctl_channel_info_x32(s, p)    \
155         snd_pcm_channel_info_user(s, p)
156 #endif /* CONFIG_X86_X32 */
157
158 struct compat_snd_pcm_status64 {
159         s32 state;
160         u8 rsvd[4]; /* alignment */
161         s64 trigger_tstamp_sec;
162         s64 trigger_tstamp_nsec;
163         s64 tstamp_sec;
164         s64 tstamp_nsec;
165         u32 appl_ptr;
166         u32 hw_ptr;
167         s32 delay;
168         u32 avail;
169         u32 avail_max;
170         u32 overrange;
171         s32 suspended_state;
172         u32 audio_tstamp_data;
173         s64 audio_tstamp_sec;
174         s64 audio_tstamp_nsec;
175         s64 driver_tstamp_sec;
176         s64 driver_tstamp_nsec;
177         u32 audio_tstamp_accuracy;
178         unsigned char reserved[52-4*sizeof(s64)];
179 } __packed;
180
181 #define put_timespec(src, dst) copy_to_user(dst, src, sizeof(*dst))
182
183 static int snd_pcm_status_user_compat64(struct snd_pcm_substream *substream,
184                                         struct compat_snd_pcm_status64 __user *src,
185                                         bool ext)
186 {
187         struct snd_pcm_status64 status;
188         struct compat_snd_pcm_status64 compat_status64;
189         int err;
190
191         memset(&status, 0, sizeof(status));
192         memset(&compat_status64, 0, sizeof(compat_status64));
193         /*
194          * with extension, parameters are read/write,
195          * get audio_tstamp_data from user,
196          * ignore rest of status structure
197          */
198         if (ext && get_user(status.audio_tstamp_data,
199                                 (u32 __user *)(&src->audio_tstamp_data)))
200                 return -EFAULT;
201         err = snd_pcm_status64(substream, &status);
202         if (err < 0)
203                 return err;
204
205         if (clear_user(src, sizeof(*src)))
206                 return -EFAULT;
207
208         compat_status64 = (struct compat_snd_pcm_status64) {
209                 .state = status.state,
210                 .trigger_tstamp_sec = status.trigger_tstamp_sec,
211                 .trigger_tstamp_nsec = status.trigger_tstamp_nsec,
212                 .tstamp_sec = status.tstamp_sec,
213                 .tstamp_nsec = status.tstamp_nsec,
214                 .appl_ptr = status.appl_ptr,
215                 .hw_ptr = status.hw_ptr,
216                 .delay = status.delay,
217                 .avail = status.avail,
218                 .avail_max = status.avail_max,
219                 .overrange = status.overrange,
220                 .suspended_state = status.suspended_state,
221                 .audio_tstamp_data = status.audio_tstamp_data,
222                 .audio_tstamp_sec = status.audio_tstamp_sec,
223                 .audio_tstamp_nsec = status.audio_tstamp_nsec,
224                 .driver_tstamp_sec = status.audio_tstamp_sec,
225                 .driver_tstamp_nsec = status.audio_tstamp_nsec,
226                 .audio_tstamp_accuracy = status.audio_tstamp_accuracy,
227         };
228
229         if (copy_to_user(src, &compat_status64, sizeof(compat_status64)))
230                 return -EFAULT;
231
232         return err;
233 }
234
235 /* both for HW_PARAMS and HW_REFINE */
236 static int snd_pcm_ioctl_hw_params_compat(struct snd_pcm_substream *substream,
237                                           int refine, 
238                                           struct snd_pcm_hw_params32 __user *data32)
239 {
240         struct snd_pcm_hw_params *data;
241         struct snd_pcm_runtime *runtime;
242         int err;
243
244         if (! (runtime = substream->runtime))
245                 return -ENOTTY;
246
247         data = kmalloc(sizeof(*data), GFP_KERNEL);
248         if (!data)
249                 return -ENOMEM;
250
251         /* only fifo_size (RO from userspace) is different, so just copy all */
252         if (copy_from_user(data, data32, sizeof(*data32))) {
253                 err = -EFAULT;
254                 goto error;
255         }
256
257         if (refine)
258                 err = snd_pcm_hw_refine(substream, data);
259         else
260                 err = snd_pcm_hw_params(substream, data);
261         if (err < 0)
262                 goto error;
263         if (copy_to_user(data32, data, sizeof(*data32)) ||
264             put_user(data->fifo_size, &data32->fifo_size)) {
265                 err = -EFAULT;
266                 goto error;
267         }
268
269         if (! refine) {
270                 unsigned int new_boundary = recalculate_boundary(runtime);
271                 if (new_boundary)
272                         runtime->boundary = new_boundary;
273         }
274  error:
275         kfree(data);
276         return err;
277 }
278
279
280 /*
281  */
282 struct snd_xferi32 {
283         s32 result;
284         u32 buf;
285         u32 frames;
286 };
287
288 static int snd_pcm_ioctl_xferi_compat(struct snd_pcm_substream *substream,
289                                       int dir, struct snd_xferi32 __user *data32)
290 {
291         compat_caddr_t buf;
292         u32 frames;
293         int err;
294
295         if (! substream->runtime)
296                 return -ENOTTY;
297         if (substream->stream != dir)
298                 return -EINVAL;
299         if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
300                 return -EBADFD;
301
302         if (get_user(buf, &data32->buf) ||
303             get_user(frames, &data32->frames))
304                 return -EFAULT;
305
306         if (dir == SNDRV_PCM_STREAM_PLAYBACK)
307                 err = snd_pcm_lib_write(substream, compat_ptr(buf), frames);
308         else
309                 err = snd_pcm_lib_read(substream, compat_ptr(buf), frames);
310         if (err < 0)
311                 return err;
312         /* copy the result */
313         if (put_user(err, &data32->result))
314                 return -EFAULT;
315         return 0;
316 }
317
318
319 /* snd_xfern needs remapping of bufs */
320 struct snd_xfern32 {
321         s32 result;
322         u32 bufs;  /* this is void **; */
323         u32 frames;
324 };
325
326 /*
327  * xfern ioctl nees to copy (up to) 128 pointers on stack.
328  * although we may pass the copied pointers through f_op->ioctl, but the ioctl
329  * handler there expands again the same 128 pointers on stack, so it is better
330  * to handle the function (calling pcm_readv/writev) directly in this handler.
331  */
332 static int snd_pcm_ioctl_xfern_compat(struct snd_pcm_substream *substream,
333                                       int dir, struct snd_xfern32 __user *data32)
334 {
335         compat_caddr_t buf;
336         compat_caddr_t __user *bufptr;
337         u32 frames;
338         void __user **bufs;
339         int err, ch, i;
340
341         if (! substream->runtime)
342                 return -ENOTTY;
343         if (substream->stream != dir)
344                 return -EINVAL;
345         if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
346                 return -EBADFD;
347
348         if ((ch = substream->runtime->channels) > 128)
349                 return -EINVAL;
350         if (get_user(buf, &data32->bufs) ||
351             get_user(frames, &data32->frames))
352                 return -EFAULT;
353         bufptr = compat_ptr(buf);
354         bufs = kmalloc_array(ch, sizeof(void __user *), GFP_KERNEL);
355         if (bufs == NULL)
356                 return -ENOMEM;
357         for (i = 0; i < ch; i++) {
358                 u32 ptr;
359                 if (get_user(ptr, bufptr)) {
360                         kfree(bufs);
361                         return -EFAULT;
362                 }
363                 bufs[i] = compat_ptr(ptr);
364                 bufptr++;
365         }
366         if (dir == SNDRV_PCM_STREAM_PLAYBACK)
367                 err = snd_pcm_lib_writev(substream, bufs, frames);
368         else
369                 err = snd_pcm_lib_readv(substream, bufs, frames);
370         if (err >= 0) {
371                 if (put_user(err, &data32->result))
372                         err = -EFAULT;
373         }
374         kfree(bufs);
375         return err;
376 }
377
378 #ifdef CONFIG_X86_X32
379 /* X32 ABI has 64bit timespec and 64bit alignment */
380 struct snd_pcm_mmap_status_x32 {
381         s32 state;
382         s32 pad1;
383         u32 hw_ptr;
384         u32 pad2; /* alignment */
385         struct timespec tstamp;
386         s32 suspended_state;
387         s32 pad3;
388         struct timespec audio_tstamp;
389 } __packed;
390
391 struct snd_pcm_mmap_control_x32 {
392         u32 appl_ptr;
393         u32 avail_min;
394 };
395
396 struct snd_pcm_sync_ptr_x32 {
397         u32 flags;
398         u32 rsvd; /* alignment */
399         union {
400                 struct snd_pcm_mmap_status_x32 status;
401                 unsigned char reserved[64];
402         } s;
403         union {
404                 struct snd_pcm_mmap_control_x32 control;
405                 unsigned char reserved[64];
406         } c;
407 } __packed;
408
409 static int snd_pcm_ioctl_sync_ptr_x32(struct snd_pcm_substream *substream,
410                                       struct snd_pcm_sync_ptr_x32 __user *src)
411 {
412         struct snd_pcm_runtime *runtime = substream->runtime;
413         volatile struct snd_pcm_mmap_status *status;
414         volatile struct snd_pcm_mmap_control *control;
415         u32 sflags;
416         struct snd_pcm_mmap_control scontrol;
417         struct snd_pcm_mmap_status sstatus;
418         snd_pcm_uframes_t boundary;
419         int err;
420
421         if (snd_BUG_ON(!runtime))
422                 return -EINVAL;
423
424         if (get_user(sflags, &src->flags) ||
425             get_user(scontrol.appl_ptr, &src->c.control.appl_ptr) ||
426             get_user(scontrol.avail_min, &src->c.control.avail_min))
427                 return -EFAULT;
428         if (sflags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
429                 err = snd_pcm_hwsync(substream);
430                 if (err < 0)
431                         return err;
432         }
433         status = runtime->status;
434         control = runtime->control;
435         boundary = recalculate_boundary(runtime);
436         if (!boundary)
437                 boundary = 0x7fffffff;
438         snd_pcm_stream_lock_irq(substream);
439         /* FIXME: we should consider the boundary for the sync from app */
440         if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL))
441                 control->appl_ptr = scontrol.appl_ptr;
442         else
443                 scontrol.appl_ptr = control->appl_ptr % boundary;
444         if (!(sflags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
445                 control->avail_min = scontrol.avail_min;
446         else
447                 scontrol.avail_min = control->avail_min;
448         sstatus.state = status->state;
449         sstatus.hw_ptr = status->hw_ptr % boundary;
450         sstatus.tstamp = status->tstamp;
451         sstatus.suspended_state = status->suspended_state;
452         sstatus.audio_tstamp = status->audio_tstamp;
453         snd_pcm_stream_unlock_irq(substream);
454         if (put_user(sstatus.state, &src->s.status.state) ||
455             put_user(sstatus.hw_ptr, &src->s.status.hw_ptr) ||
456             put_timespec(&sstatus.tstamp, &src->s.status.tstamp) ||
457             put_user(sstatus.suspended_state, &src->s.status.suspended_state) ||
458             put_timespec(&sstatus.audio_tstamp, &src->s.status.audio_tstamp) ||
459             put_user(scontrol.appl_ptr, &src->c.control.appl_ptr) ||
460             put_user(scontrol.avail_min, &src->c.control.avail_min))
461                 return -EFAULT;
462
463         return 0;
464 }
465 #endif /* CONFIG_X86_X32 */
466
467 /*
468  */
469 enum {
470         SNDRV_PCM_IOCTL_HW_REFINE32 = _IOWR('A', 0x10, struct snd_pcm_hw_params32),
471         SNDRV_PCM_IOCTL_HW_PARAMS32 = _IOWR('A', 0x11, struct snd_pcm_hw_params32),
472         SNDRV_PCM_IOCTL_SW_PARAMS32 = _IOWR('A', 0x13, struct snd_pcm_sw_params32),
473         SNDRV_PCM_IOCTL_STATUS_COMPAT32 = _IOR('A', 0x20, struct snd_pcm_status32),
474         SNDRV_PCM_IOCTL_STATUS_EXT_COMPAT32 = _IOWR('A', 0x24, struct snd_pcm_status32),
475         SNDRV_PCM_IOCTL_DELAY32 = _IOR('A', 0x21, s32),
476         SNDRV_PCM_IOCTL_CHANNEL_INFO32 = _IOR('A', 0x32, struct snd_pcm_channel_info32),
477         SNDRV_PCM_IOCTL_REWIND32 = _IOW('A', 0x46, u32),
478         SNDRV_PCM_IOCTL_FORWARD32 = _IOW('A', 0x49, u32),
479         SNDRV_PCM_IOCTL_WRITEI_FRAMES32 = _IOW('A', 0x50, struct snd_xferi32),
480         SNDRV_PCM_IOCTL_READI_FRAMES32 = _IOR('A', 0x51, struct snd_xferi32),
481         SNDRV_PCM_IOCTL_WRITEN_FRAMES32 = _IOW('A', 0x52, struct snd_xfern32),
482         SNDRV_PCM_IOCTL_READN_FRAMES32 = _IOR('A', 0x53, struct snd_xfern32),
483         SNDRV_PCM_IOCTL_SYNC_PTR32 = _IOWR('A', 0x23, struct snd_pcm_sync_ptr32),
484         SNDRV_PCM_IOCTL_STATUS_COMPAT64 = _IOR('A', 0x20, struct compat_snd_pcm_status64),
485         SNDRV_PCM_IOCTL_STATUS_EXT_COMPAT64 = _IOWR('A', 0x24, struct compat_snd_pcm_status64),
486 #ifdef CONFIG_X86_X32
487         SNDRV_PCM_IOCTL_CHANNEL_INFO_X32 = _IOR('A', 0x32, struct snd_pcm_channel_info),
488         SNDRV_PCM_IOCTL_SYNC_PTR_X32 = _IOWR('A', 0x23, struct snd_pcm_sync_ptr_x32),
489 #endif /* CONFIG_X86_X32 */
490 };
491
492 static long snd_pcm_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
493 {
494         struct snd_pcm_file *pcm_file;
495         struct snd_pcm_substream *substream;
496         void __user *argp = compat_ptr(arg);
497
498         pcm_file = file->private_data;
499         if (! pcm_file)
500                 return -ENOTTY;
501         substream = pcm_file->substream;
502         if (! substream)
503                 return -ENOTTY;
504
505         /*
506          * When PCM is used on 32bit mode, we need to disable
507          * mmap of PCM status/control records because of the size
508          * incompatibility.
509          */
510         pcm_file->no_compat_mmap = 1;
511
512         switch (cmd) {
513         case SNDRV_PCM_IOCTL_PVERSION:
514         case SNDRV_PCM_IOCTL_INFO:
515         case SNDRV_PCM_IOCTL_TSTAMP:
516         case SNDRV_PCM_IOCTL_TTSTAMP:
517         case SNDRV_PCM_IOCTL_USER_PVERSION:
518         case SNDRV_PCM_IOCTL_HWSYNC:
519         case SNDRV_PCM_IOCTL_PREPARE:
520         case SNDRV_PCM_IOCTL_RESET:
521         case SNDRV_PCM_IOCTL_START:
522         case SNDRV_PCM_IOCTL_DROP:
523         case SNDRV_PCM_IOCTL_DRAIN:
524         case SNDRV_PCM_IOCTL_PAUSE:
525         case SNDRV_PCM_IOCTL_HW_FREE:
526         case SNDRV_PCM_IOCTL_RESUME:
527         case SNDRV_PCM_IOCTL_XRUN:
528         case SNDRV_PCM_IOCTL_LINK:
529         case SNDRV_PCM_IOCTL_UNLINK:
530                 return snd_pcm_common_ioctl(file, substream, cmd, argp);
531         case SNDRV_PCM_IOCTL_HW_REFINE32:
532                 return snd_pcm_ioctl_hw_params_compat(substream, 1, argp);
533         case SNDRV_PCM_IOCTL_HW_PARAMS32:
534                 return snd_pcm_ioctl_hw_params_compat(substream, 0, argp);
535         case SNDRV_PCM_IOCTL_SW_PARAMS32:
536                 return snd_pcm_ioctl_sw_params_compat(substream, argp);
537         case SNDRV_PCM_IOCTL_STATUS_COMPAT32:
538                 return snd_pcm_status_user32(substream, argp, false);
539         case SNDRV_PCM_IOCTL_STATUS_EXT_COMPAT32:
540                 return snd_pcm_status_user32(substream, argp, true);
541         case SNDRV_PCM_IOCTL_SYNC_PTR32:
542                 return snd_pcm_ioctl_sync_ptr_compat(substream, argp);
543         case SNDRV_PCM_IOCTL_CHANNEL_INFO32:
544                 return snd_pcm_ioctl_channel_info_compat(substream, argp);
545         case SNDRV_PCM_IOCTL_WRITEI_FRAMES32:
546                 return snd_pcm_ioctl_xferi_compat(substream, SNDRV_PCM_STREAM_PLAYBACK, argp);
547         case SNDRV_PCM_IOCTL_READI_FRAMES32:
548                 return snd_pcm_ioctl_xferi_compat(substream, SNDRV_PCM_STREAM_CAPTURE, argp);
549         case SNDRV_PCM_IOCTL_WRITEN_FRAMES32:
550                 return snd_pcm_ioctl_xfern_compat(substream, SNDRV_PCM_STREAM_PLAYBACK, argp);
551         case SNDRV_PCM_IOCTL_READN_FRAMES32:
552                 return snd_pcm_ioctl_xfern_compat(substream, SNDRV_PCM_STREAM_CAPTURE, argp);
553         case SNDRV_PCM_IOCTL_DELAY32:
554                 return snd_pcm_ioctl_delay_compat(substream, argp);
555         case SNDRV_PCM_IOCTL_REWIND32:
556                 return snd_pcm_ioctl_rewind_compat(substream, argp);
557         case SNDRV_PCM_IOCTL_FORWARD32:
558                 return snd_pcm_ioctl_forward_compat(substream, argp);
559         case SNDRV_PCM_IOCTL_STATUS_COMPAT64:
560                 return snd_pcm_status_user_compat64(substream, argp, false);
561         case SNDRV_PCM_IOCTL_STATUS_EXT_COMPAT64:
562                 return snd_pcm_status_user_compat64(substream, argp, true);
563 #ifdef CONFIG_X86_X32
564         case SNDRV_PCM_IOCTL_SYNC_PTR_X32:
565                 return snd_pcm_ioctl_sync_ptr_x32(substream, argp);
566         case SNDRV_PCM_IOCTL_CHANNEL_INFO_X32:
567                 return snd_pcm_ioctl_channel_info_x32(substream, argp);
568 #endif /* CONFIG_X86_X32 */
569         }
570
571         return -ENOIOCTLCMD;
572 }