blob: e30e30ba6e3984804ec325457d76d8195d69245d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Takashi Iwai48c9d412005-11-17 13:56:51 +010025struct snd_rawmidi_params32 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 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
Takashi Iwai48c9d412005-11-17 13:56:51 +010033static int snd_rawmidi_ioctl_params_compat(struct snd_rawmidi_file *rfile,
34 struct snd_rawmidi_params32 __user *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Takashi Iwai48c9d412005-11-17 13:56:51 +010036 struct snd_rawmidi_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 unsigned int val;
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 if (get_user(params.stream, &src->stream) ||
40 get_user(params.buffer_size, &src->buffer_size) ||
41 get_user(params.avail_min, &src->avail_min) ||
42 get_user(val, &src->no_active_sensing))
43 return -EFAULT;
44 params.no_active_sensing = val;
45 switch (params.stream) {
46 case SNDRV_RAWMIDI_STREAM_OUTPUT:
Takashi Iwaib291c272018-04-19 18:16:15 +020047 if (!rfile->output)
48 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 return snd_rawmidi_output_params(rfile->output, &params);
50 case SNDRV_RAWMIDI_STREAM_INPUT:
Takashi Iwaib291c272018-04-19 18:16:15 +020051 if (!rfile->input)
52 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 return snd_rawmidi_input_params(rfile->input, &params);
54 }
55 return -EINVAL;
56}
57
Takashi Iwai48c9d412005-11-17 13:56:51 +010058struct snd_rawmidi_status32 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 s32 stream;
60 struct compat_timespec tstamp;
61 u32 avail;
62 u32 xruns;
63 unsigned char reserved[16];
64} __attribute__((packed));
65
Takashi Iwai48c9d412005-11-17 13:56:51 +010066static int snd_rawmidi_ioctl_status_compat(struct snd_rawmidi_file *rfile,
67 struct snd_rawmidi_status32 __user *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 int err;
Takashi Iwai48c9d412005-11-17 13:56:51 +010070 struct snd_rawmidi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (get_user(status.stream, &src->stream))
73 return -EFAULT;
74
75 switch (status.stream) {
76 case SNDRV_RAWMIDI_STREAM_OUTPUT:
Takashi Iwaib291c272018-04-19 18:16:15 +020077 if (!rfile->output)
78 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 err = snd_rawmidi_output_status(rfile->output, &status);
80 break;
81 case SNDRV_RAWMIDI_STREAM_INPUT:
Takashi Iwaib291c272018-04-19 18:16:15 +020082 if (!rfile->input)
83 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 err = snd_rawmidi_input_status(rfile->input, &status);
85 break;
86 default:
87 return -EINVAL;
88 }
89 if (err < 0)
90 return err;
91
Takashi Iwaidd7e3f82016-02-28 11:30:53 +010092 if (compat_put_timespec(&status.tstamp, &src->tstamp) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 put_user(status.avail, &src->avail) ||
94 put_user(status.xruns, &src->xruns))
95 return -EFAULT;
96
97 return 0;
98}
99
Takashi Iwai2251fbb2016-02-28 11:28:08 +0100100#ifdef CONFIG_X86_X32
101/* X32 ABI has 64bit timespec and 64bit alignment */
102struct snd_rawmidi_status_x32 {
103 s32 stream;
104 u32 rsvd; /* alignment */
105 struct timespec tstamp;
106 u32 avail;
107 u32 xruns;
108 unsigned char reserved[16];
109} __attribute__((packed));
110
111#define put_timespec(src, dst) copy_to_user(dst, src, sizeof(*dst))
112
113static int snd_rawmidi_ioctl_status_x32(struct snd_rawmidi_file *rfile,
114 struct snd_rawmidi_status_x32 __user *src)
115{
116 int err;
117 struct snd_rawmidi_status status;
118
Takashi Iwai2251fbb2016-02-28 11:28:08 +0100119 if (get_user(status.stream, &src->stream))
120 return -EFAULT;
121
122 switch (status.stream) {
123 case SNDRV_RAWMIDI_STREAM_OUTPUT:
Takashi Iwaib291c272018-04-19 18:16:15 +0200124 if (!rfile->output)
125 return -EINVAL;
Takashi Iwai2251fbb2016-02-28 11:28:08 +0100126 err = snd_rawmidi_output_status(rfile->output, &status);
127 break;
128 case SNDRV_RAWMIDI_STREAM_INPUT:
Takashi Iwaib291c272018-04-19 18:16:15 +0200129 if (!rfile->input)
130 return -EINVAL;
Takashi Iwai2251fbb2016-02-28 11:28:08 +0100131 err = snd_rawmidi_input_status(rfile->input, &status);
132 break;
133 default:
134 return -EINVAL;
135 }
136 if (err < 0)
137 return err;
138
139 if (put_timespec(&status.tstamp, &src->tstamp) ||
140 put_user(status.avail, &src->avail) ||
141 put_user(status.xruns, &src->xruns))
142 return -EFAULT;
143
144 return 0;
145}
146#endif /* CONFIG_X86_X32 */
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148enum {
Takashi Iwai48c9d412005-11-17 13:56:51 +0100149 SNDRV_RAWMIDI_IOCTL_PARAMS32 = _IOWR('W', 0x10, struct snd_rawmidi_params32),
150 SNDRV_RAWMIDI_IOCTL_STATUS32 = _IOWR('W', 0x20, struct snd_rawmidi_status32),
Takashi Iwai2251fbb2016-02-28 11:28:08 +0100151#ifdef CONFIG_X86_X32
152 SNDRV_RAWMIDI_IOCTL_STATUS_X32 = _IOWR('W', 0x20, struct snd_rawmidi_status_x32),
153#endif /* CONFIG_X86_X32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154};
155
156static long snd_rawmidi_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
157{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100158 struct snd_rawmidi_file *rfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 void __user *argp = compat_ptr(arg);
160
161 rfile = file->private_data;
162 switch (cmd) {
163 case SNDRV_RAWMIDI_IOCTL_PVERSION:
164 case SNDRV_RAWMIDI_IOCTL_INFO:
165 case SNDRV_RAWMIDI_IOCTL_DROP:
166 case SNDRV_RAWMIDI_IOCTL_DRAIN:
167 return snd_rawmidi_ioctl(file, cmd, (unsigned long)argp);
168 case SNDRV_RAWMIDI_IOCTL_PARAMS32:
169 return snd_rawmidi_ioctl_params_compat(rfile, argp);
170 case SNDRV_RAWMIDI_IOCTL_STATUS32:
171 return snd_rawmidi_ioctl_status_compat(rfile, argp);
Takashi Iwai2251fbb2016-02-28 11:28:08 +0100172#ifdef CONFIG_X86_X32
173 case SNDRV_RAWMIDI_IOCTL_STATUS_X32:
174 return snd_rawmidi_ioctl_status_x32(rfile, argp);
175#endif /* CONFIG_X86_X32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177 return -ENOIOCTLCMD;
178}