Linux Audio

Check our new training course

Loading...
v4.10.11
 
  1/*
  2 *   32bit -> 64bit ioctl wrapper for raw MIDI 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/* This file included from rawmidi.c */
 22
 23#include <linux/compat.h>
 24
 25struct snd_rawmidi_params32 {
 26	s32 stream;
 27	u32 buffer_size;
 28	u32 avail_min;
 29	unsigned int no_active_sensing; /* avoid bit-field */
 30	unsigned char reserved[16];
 
 31} __attribute__((packed));
 32
 33static int snd_rawmidi_ioctl_params_compat(struct snd_rawmidi_file *rfile,
 34					   struct snd_rawmidi_params32 __user *src)
 35{
 36	struct snd_rawmidi_params params;
 37	unsigned int val;
 38
 39	if (rfile->output == NULL)
 40		return -EINVAL;
 41	if (get_user(params.stream, &src->stream) ||
 42	    get_user(params.buffer_size, &src->buffer_size) ||
 43	    get_user(params.avail_min, &src->avail_min) ||
 
 44	    get_user(val, &src->no_active_sensing))
 45		return -EFAULT;
 46	params.no_active_sensing = val;
 47	switch (params.stream) {
 48	case SNDRV_RAWMIDI_STREAM_OUTPUT:
 
 
 49		return snd_rawmidi_output_params(rfile->output, &params);
 50	case SNDRV_RAWMIDI_STREAM_INPUT:
 
 
 51		return snd_rawmidi_input_params(rfile->input, &params);
 52	}
 53	return -EINVAL;
 54}
 55
 56struct snd_rawmidi_status32 {
 57	s32 stream;
 58	struct compat_timespec tstamp;
 
 
 59	u32 avail;
 60	u32 xruns;
 61	unsigned char reserved[16];
 62} __attribute__((packed));
 63
 64static int snd_rawmidi_ioctl_status_compat(struct snd_rawmidi_file *rfile,
 65					   struct snd_rawmidi_status32 __user *src)
 66{
 67	int err;
 68	struct snd_rawmidi_status status;
 
 69
 70	if (rfile->output == NULL)
 71		return -EINVAL;
 72	if (get_user(status.stream, &src->stream))
 73		return -EFAULT;
 74
 75	switch (status.stream) {
 76	case SNDRV_RAWMIDI_STREAM_OUTPUT:
 
 
 77		err = snd_rawmidi_output_status(rfile->output, &status);
 78		break;
 79	case SNDRV_RAWMIDI_STREAM_INPUT:
 
 
 80		err = snd_rawmidi_input_status(rfile->input, &status);
 81		break;
 82	default:
 83		return -EINVAL;
 84	}
 85	if (err < 0)
 86		return err;
 87
 88	if (compat_put_timespec(&status.tstamp, &src->tstamp) ||
 89	    put_user(status.avail, &src->avail) ||
 90	    put_user(status.xruns, &src->xruns))
 91		return -EFAULT;
 92
 93	return 0;
 94}
 95
 96#ifdef CONFIG_X86_X32
 97/* X32 ABI has 64bit timespec and 64bit alignment */
 98struct snd_rawmidi_status_x32 {
 99	s32 stream;
100	u32 rsvd; /* alignment */
101	struct timespec tstamp;
102	u32 avail;
103	u32 xruns;
104	unsigned char reserved[16];
105} __attribute__((packed));
106
107#define put_timespec(src, dst) copy_to_user(dst, src, sizeof(*dst))
108
109static int snd_rawmidi_ioctl_status_x32(struct snd_rawmidi_file *rfile,
110					struct snd_rawmidi_status_x32 __user *src)
111{
112	int err;
113	struct snd_rawmidi_status status;
114
115	if (rfile->output == NULL)
116		return -EINVAL;
117	if (get_user(status.stream, &src->stream))
118		return -EFAULT;
119
120	switch (status.stream) {
121	case SNDRV_RAWMIDI_STREAM_OUTPUT:
122		err = snd_rawmidi_output_status(rfile->output, &status);
123		break;
124	case SNDRV_RAWMIDI_STREAM_INPUT:
125		err = snd_rawmidi_input_status(rfile->input, &status);
126		break;
127	default:
128		return -EINVAL;
129	}
130	if (err < 0)
131		return err;
132
133	if (put_timespec(&status.tstamp, &src->tstamp) ||
134	    put_user(status.avail, &src->avail) ||
135	    put_user(status.xruns, &src->xruns))
136		return -EFAULT;
137
138	return 0;
139}
140#endif /* CONFIG_X86_X32 */
141
142enum {
143	SNDRV_RAWMIDI_IOCTL_PARAMS32 = _IOWR('W', 0x10, struct snd_rawmidi_params32),
144	SNDRV_RAWMIDI_IOCTL_STATUS32 = _IOWR('W', 0x20, struct snd_rawmidi_status32),
145#ifdef CONFIG_X86_X32
146	SNDRV_RAWMIDI_IOCTL_STATUS_X32 = _IOWR('W', 0x20, struct snd_rawmidi_status_x32),
147#endif /* CONFIG_X86_X32 */
148};
149
150static long snd_rawmidi_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
151{
152	struct snd_rawmidi_file *rfile;
153	void __user *argp = compat_ptr(arg);
154
155	rfile = file->private_data;
156	switch (cmd) {
157	case SNDRV_RAWMIDI_IOCTL_PVERSION:
158	case SNDRV_RAWMIDI_IOCTL_INFO:
159	case SNDRV_RAWMIDI_IOCTL_DROP:
160	case SNDRV_RAWMIDI_IOCTL_DRAIN:
161		return snd_rawmidi_ioctl(file, cmd, (unsigned long)argp);
162	case SNDRV_RAWMIDI_IOCTL_PARAMS32:
163		return snd_rawmidi_ioctl_params_compat(rfile, argp);
164	case SNDRV_RAWMIDI_IOCTL_STATUS32:
165		return snd_rawmidi_ioctl_status_compat(rfile, argp);
166#ifdef CONFIG_X86_X32
167	case SNDRV_RAWMIDI_IOCTL_STATUS_X32:
168		return snd_rawmidi_ioctl_status_x32(rfile, argp);
169#endif /* CONFIG_X86_X32 */
170	}
171	return -ENOIOCTLCMD;
172}
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 *   32bit -> 64bit ioctl wrapper for raw MIDI API
  4 *   Copyright (c) by Takashi Iwai <tiwai@suse.de>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  5 */
  6
  7/* This file included from rawmidi.c */
  8
  9#include <linux/compat.h>
 10
 11struct snd_rawmidi_params32 {
 12	s32 stream;
 13	u32 buffer_size;
 14	u32 avail_min;
 15	unsigned int no_active_sensing; /* avoid bit-field */
 16	unsigned int mode;
 17	unsigned char reserved[12];
 18} __attribute__((packed));
 19
 20static int snd_rawmidi_ioctl_params_compat(struct snd_rawmidi_file *rfile,
 21					   struct snd_rawmidi_params32 __user *src)
 22{
 23	struct snd_rawmidi_params params;
 24	unsigned int val;
 25
 
 
 26	if (get_user(params.stream, &src->stream) ||
 27	    get_user(params.buffer_size, &src->buffer_size) ||
 28	    get_user(params.avail_min, &src->avail_min) ||
 29	    get_user(params.mode, &src->mode) ||
 30	    get_user(val, &src->no_active_sensing))
 31		return -EFAULT;
 32	params.no_active_sensing = val;
 33	switch (params.stream) {
 34	case SNDRV_RAWMIDI_STREAM_OUTPUT:
 35		if (!rfile->output)
 36			return -EINVAL;
 37		return snd_rawmidi_output_params(rfile->output, &params);
 38	case SNDRV_RAWMIDI_STREAM_INPUT:
 39		if (!rfile->input)
 40			return -EINVAL;
 41		return snd_rawmidi_input_params(rfile->input, &params);
 42	}
 43	return -EINVAL;
 44}
 45
 46struct compat_snd_rawmidi_status64 {
 47	s32 stream;
 48	u8 rsvd[4]; /* alignment */
 49	s64 tstamp_sec;
 50	s64 tstamp_nsec;
 51	u32 avail;
 52	u32 xruns;
 53	unsigned char reserved[16];
 54} __attribute__((packed));
 55
 56static int snd_rawmidi_ioctl_status_compat64(struct snd_rawmidi_file *rfile,
 57					     struct compat_snd_rawmidi_status64 __user *src)
 58{
 59	int err;
 60	struct snd_rawmidi_status64 status;
 61	struct compat_snd_rawmidi_status64 compat_status;
 62
 
 
 63	if (get_user(status.stream, &src->stream))
 64		return -EFAULT;
 65
 66	switch (status.stream) {
 67	case SNDRV_RAWMIDI_STREAM_OUTPUT:
 68		if (!rfile->output)
 69			return -EINVAL;
 70		err = snd_rawmidi_output_status(rfile->output, &status);
 71		break;
 72	case SNDRV_RAWMIDI_STREAM_INPUT:
 73		if (!rfile->input)
 74			return -EINVAL;
 75		err = snd_rawmidi_input_status(rfile->input, &status);
 76		break;
 77	default:
 78		return -EINVAL;
 79	}
 80	if (err < 0)
 81		return err;
 82
 83	compat_status = (struct compat_snd_rawmidi_status64) {
 84		.stream = status.stream,
 85		.tstamp_sec = status.tstamp_sec,
 86		.tstamp_nsec = status.tstamp_nsec,
 87		.avail = status.avail,
 88		.xruns = status.xruns,
 89	};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 90
 91	if (copy_to_user(src, &compat_status, sizeof(*src)))
 
 
 92		return -EFAULT;
 93
 94	return 0;
 95}
 
 96
 97enum {
 98	SNDRV_RAWMIDI_IOCTL_PARAMS32 = _IOWR('W', 0x10, struct snd_rawmidi_params32),
 99	SNDRV_RAWMIDI_IOCTL_STATUS_COMPAT32 = _IOWR('W', 0x20, struct snd_rawmidi_status32),
100	SNDRV_RAWMIDI_IOCTL_STATUS_COMPAT64 = _IOWR('W', 0x20, struct compat_snd_rawmidi_status64),
 
 
101};
102
103static long snd_rawmidi_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
104{
105	struct snd_rawmidi_file *rfile;
106	void __user *argp = compat_ptr(arg);
107
108	rfile = file->private_data;
109	switch (cmd) {
110	case SNDRV_RAWMIDI_IOCTL_PVERSION:
111	case SNDRV_RAWMIDI_IOCTL_INFO:
112	case SNDRV_RAWMIDI_IOCTL_DROP:
113	case SNDRV_RAWMIDI_IOCTL_DRAIN:
114		return snd_rawmidi_ioctl(file, cmd, (unsigned long)argp);
115	case SNDRV_RAWMIDI_IOCTL_PARAMS32:
116		return snd_rawmidi_ioctl_params_compat(rfile, argp);
117	case SNDRV_RAWMIDI_IOCTL_STATUS_COMPAT32:
118		return snd_rawmidi_ioctl_status32(rfile, argp);
119	case SNDRV_RAWMIDI_IOCTL_STATUS_COMPAT64:
120		return snd_rawmidi_ioctl_status_compat64(rfile, argp);
 
 
121	}
122	return -ENOIOCTLCMD;
123}