GNU Linux-libre 4.4.282-gnu1
[releases.git] / sound / core / rawmidi_compat.c
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
25 struct 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
33 static 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 (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:
47                 if (!rfile->output)
48                         return -EINVAL;
49                 return snd_rawmidi_output_params(rfile->output, &params);
50         case SNDRV_RAWMIDI_STREAM_INPUT:
51                 if (!rfile->input)
52                         return -EINVAL;
53                 return snd_rawmidi_input_params(rfile->input, &params);
54         }
55         return -EINVAL;
56 }
57
58 struct snd_rawmidi_status32 {
59         s32 stream;
60         struct compat_timespec tstamp;
61         u32 avail;
62         u32 xruns;
63         unsigned char reserved[16];
64 } __attribute__((packed));
65
66 static int snd_rawmidi_ioctl_status_compat(struct snd_rawmidi_file *rfile,
67                                            struct snd_rawmidi_status32 __user *src)
68 {
69         int err;
70         struct snd_rawmidi_status status;
71
72         if (get_user(status.stream, &src->stream))
73                 return -EFAULT;
74
75         switch (status.stream) {
76         case SNDRV_RAWMIDI_STREAM_OUTPUT:
77                 if (!rfile->output)
78                         return -EINVAL;
79                 err = snd_rawmidi_output_status(rfile->output, &status);
80                 break;
81         case SNDRV_RAWMIDI_STREAM_INPUT:
82                 if (!rfile->input)
83                         return -EINVAL;
84                 err = snd_rawmidi_input_status(rfile->input, &status);
85                 break;
86         default:
87                 return -EINVAL;
88         }
89         if (err < 0)
90                 return err;
91
92         if (put_user(status.tstamp.tv_sec, &src->tstamp.tv_sec) ||
93             put_user(status.tstamp.tv_nsec, &src->tstamp.tv_nsec) ||
94             put_user(status.avail, &src->avail) ||
95             put_user(status.xruns, &src->xruns))
96                 return -EFAULT;
97
98         return 0;
99 }
100
101 #ifdef CONFIG_X86_X32
102 /* X32 ABI has 64bit timespec and 64bit alignment */
103 struct snd_rawmidi_status_x32 {
104         s32 stream;
105         u32 rsvd; /* alignment */
106         struct timespec tstamp;
107         u32 avail;
108         u32 xruns;
109         unsigned char reserved[16];
110 } __attribute__((packed));
111
112 #define put_timespec(src, dst) copy_to_user(dst, src, sizeof(*dst))
113
114 static int snd_rawmidi_ioctl_status_x32(struct snd_rawmidi_file *rfile,
115                                         struct snd_rawmidi_status_x32 __user *src)
116 {
117         int err;
118         struct snd_rawmidi_status status;
119
120         if (get_user(status.stream, &src->stream))
121                 return -EFAULT;
122
123         switch (status.stream) {
124         case SNDRV_RAWMIDI_STREAM_OUTPUT:
125                 if (!rfile->output)
126                         return -EINVAL;
127                 err = snd_rawmidi_output_status(rfile->output, &status);
128                 break;
129         case SNDRV_RAWMIDI_STREAM_INPUT:
130                 if (!rfile->input)
131                         return -EINVAL;
132                 err = snd_rawmidi_input_status(rfile->input, &status);
133                 break;
134         default:
135                 return -EINVAL;
136         }
137         if (err < 0)
138                 return err;
139
140         if (put_timespec(&status.tstamp, &src->tstamp) ||
141             put_user(status.avail, &src->avail) ||
142             put_user(status.xruns, &src->xruns))
143                 return -EFAULT;
144
145         return 0;
146 }
147 #endif /* CONFIG_X86_X32 */
148
149 enum {
150         SNDRV_RAWMIDI_IOCTL_PARAMS32 = _IOWR('W', 0x10, struct snd_rawmidi_params32),
151         SNDRV_RAWMIDI_IOCTL_STATUS32 = _IOWR('W', 0x20, struct snd_rawmidi_status32),
152 #ifdef CONFIG_X86_X32
153         SNDRV_RAWMIDI_IOCTL_STATUS_X32 = _IOWR('W', 0x20, struct snd_rawmidi_status_x32),
154 #endif /* CONFIG_X86_X32 */
155 };
156
157 static long snd_rawmidi_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
158 {
159         struct snd_rawmidi_file *rfile;
160         void __user *argp = compat_ptr(arg);
161
162         rfile = file->private_data;
163         switch (cmd) {
164         case SNDRV_RAWMIDI_IOCTL_PVERSION:
165         case SNDRV_RAWMIDI_IOCTL_INFO:
166         case SNDRV_RAWMIDI_IOCTL_DROP:
167         case SNDRV_RAWMIDI_IOCTL_DRAIN:
168                 return snd_rawmidi_ioctl(file, cmd, (unsigned long)argp);
169         case SNDRV_RAWMIDI_IOCTL_PARAMS32:
170                 return snd_rawmidi_ioctl_params_compat(rfile, argp);
171         case SNDRV_RAWMIDI_IOCTL_STATUS32:
172                 return snd_rawmidi_ioctl_status_compat(rfile, argp);
173 #ifdef CONFIG_X86_X32
174         case SNDRV_RAWMIDI_IOCTL_STATUS_X32:
175                 return snd_rawmidi_ioctl_status_x32(rfile, argp);
176 #endif /* CONFIG_X86_X32 */
177         }
178         return -ENOIOCTLCMD;
179 }