xref: /linux/sound/core/control_compat.c (revision 87c2ce3b9305b9b723faeedf6e32ef703ec9b33a)
1 /*
2  * compat ioctls for control API
3  *
4  *   Copyright (c) by Takashi Iwai <tiwai@suse.de>
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  */
20 
21 /* this file included from control.c */
22 
23 #include <linux/compat.h>
24 
25 struct snd_ctl_elem_list32 {
26 	u32 offset;
27 	u32 space;
28 	u32 used;
29 	u32 count;
30 	u32 pids;
31 	unsigned char reserved[50];
32 } /* don't set packed attribute here */;
33 
34 static int snd_ctl_elem_list_compat(struct snd_card *card,
35 				    struct snd_ctl_elem_list32 __user *data32)
36 {
37 	struct snd_ctl_elem_list __user *data;
38 	compat_caddr_t ptr;
39 	int err;
40 
41 	data = compat_alloc_user_space(sizeof(*data));
42 
43 	/* offset, space, used, count */
44 	if (copy_in_user(data, data32, 4 * sizeof(u32)))
45 		return -EFAULT;
46 	/* pids */
47 	if (get_user(ptr, &data32->pids) ||
48 	    put_user(compat_ptr(ptr), &data->pids))
49 		return -EFAULT;
50 	err = snd_ctl_elem_list(card, data);
51 	if (err < 0)
52 		return err;
53 	/* copy the result */
54 	if (copy_in_user(data32, data, 4 * sizeof(u32)))
55 		return -EFAULT;
56 	return 0;
57 }
58 
59 /*
60  * control element info
61  * it uses union, so the things are not easy..
62  */
63 
64 struct snd_ctl_elem_info32 {
65 	struct snd_ctl_elem_id id; // the size of struct is same
66 	s32 type;
67 	u32 access;
68 	u32 count;
69 	s32 owner;
70 	union {
71 		struct {
72 			s32 min;
73 			s32 max;
74 			s32 step;
75 		} integer;
76 		struct {
77 			u64 min;
78 			u64 max;
79 			u64 step;
80 		} integer64;
81 		struct {
82 			u32 items;
83 			u32 item;
84 			char name[64];
85 		} enumerated;
86 		unsigned char reserved[128];
87 	} value;
88 	unsigned char reserved[64];
89 } __attribute__((packed));
90 
91 static int snd_ctl_elem_info_compat(struct snd_ctl_file *ctl,
92 				    struct snd_ctl_elem_info32 __user *data32)
93 {
94 	struct snd_ctl_elem_info *data;
95 	int err;
96 
97 	data = kzalloc(sizeof(*data), GFP_KERNEL);
98 	if (! data)
99 		return -ENOMEM;
100 
101 	err = -EFAULT;
102 	/* copy id */
103 	if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
104 		goto error;
105 	/* we need to copy the item index.
106 	 * hope this doesn't break anything..
107 	 */
108 	if (get_user(data->value.enumerated.item, &data32->value.enumerated.item))
109 		goto error;
110 	err = snd_ctl_elem_info(ctl, data);
111 	if (err < 0)
112 		goto error;
113 	/* restore info to 32bit */
114 	err = -EFAULT;
115 	/* id, type, access, count */
116 	if (copy_to_user(&data32->id, &data->id, sizeof(data->id)) ||
117 	    copy_to_user(&data32->type, &data->type, 3 * sizeof(u32)))
118 		goto error;
119 	if (put_user(data->owner, &data32->owner))
120 		goto error;
121 	switch (data->type) {
122 	case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
123 	case SNDRV_CTL_ELEM_TYPE_INTEGER:
124 		if (put_user(data->value.integer.min, &data32->value.integer.min) ||
125 		    put_user(data->value.integer.max, &data32->value.integer.max) ||
126 		    put_user(data->value.integer.step, &data32->value.integer.step))
127 			goto error;
128 		break;
129 	case SNDRV_CTL_ELEM_TYPE_INTEGER64:
130 		if (copy_to_user(&data32->value.integer64,
131 				 &data->value.integer64,
132 				 sizeof(data->value.integer64)))
133 			goto error;
134 		break;
135 	case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
136 		if (copy_to_user(&data32->value.enumerated,
137 				 &data->value.enumerated,
138 				 sizeof(data->value.enumerated)))
139 			goto error;
140 		break;
141 	default:
142 		break;
143 	}
144 	err = 0;
145  error:
146 	kfree(data);
147 	return err;
148 }
149 
150 /* read / write */
151 struct snd_ctl_elem_value32 {
152 	struct snd_ctl_elem_id id;
153 	unsigned int indirect;	/* bit-field causes misalignment */
154         union {
155 		s32 integer[128];
156 		unsigned char data[512];
157 #ifndef CONFIG_X86_64
158 		s64 integer64[64];
159 #endif
160         } value;
161         unsigned char reserved[128];
162 };
163 
164 
165 /* get the value type and count of the control */
166 static int get_ctl_type(struct snd_card *card, struct snd_ctl_elem_id *id,
167 			int *countp)
168 {
169 	struct snd_kcontrol *kctl;
170 	struct snd_ctl_elem_info info;
171 	int err;
172 
173 	down_read(&card->controls_rwsem);
174 	kctl = snd_ctl_find_id(card, id);
175 	if (! kctl) {
176 		up_read(&card->controls_rwsem);
177 		return -ENXIO;
178 	}
179 	info.id = *id;
180 	err = kctl->info(kctl, &info);
181 	up_read(&card->controls_rwsem);
182 	if (err >= 0) {
183 		err = info.type;
184 		*countp = info.count;
185 	}
186 	return err;
187 }
188 
189 static int get_elem_size(int type, int count)
190 {
191 	switch (type) {
192 	case SNDRV_CTL_ELEM_TYPE_INTEGER64:
193 		return sizeof(s64) * count;
194 	case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
195 		return sizeof(int) * count;
196 	case SNDRV_CTL_ELEM_TYPE_BYTES:
197 		return 512;
198 	case SNDRV_CTL_ELEM_TYPE_IEC958:
199 		return sizeof(struct snd_aes_iec958);
200 	default:
201 		return -1;
202 	}
203 }
204 
205 static int copy_ctl_value_from_user(struct snd_card *card,
206 				    struct snd_ctl_elem_value *data,
207 				    struct snd_ctl_elem_value32 __user *data32,
208 				    int *typep, int *countp)
209 {
210 	int i, type, count, size;
211 	unsigned int indirect;
212 
213 	if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
214 		return -EFAULT;
215 	if (get_user(indirect, &data32->indirect))
216 		return -EFAULT;
217 	if (indirect)
218 		return -EINVAL;
219 	type = get_ctl_type(card, &data->id, &count);
220 	if (type < 0)
221 		return type;
222 
223 	if (type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
224 	    type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
225 		for (i = 0; i < count; i++) {
226 			int val;
227 			if (get_user(val, &data32->value.integer[i]))
228 				return -EFAULT;
229 			data->value.integer.value[i] = val;
230 		}
231 	} else {
232 		size = get_elem_size(type, count);
233 		if (size < 0) {
234 			printk(KERN_ERR "snd_ioctl32_ctl_elem_value: unknown type %d\n", type);
235 			return -EINVAL;
236 		}
237 		if (copy_from_user(data->value.bytes.data,
238 				   data32->value.data, size))
239 			return -EFAULT;
240 	}
241 
242 	*typep = type;
243 	*countp = count;
244 	return 0;
245 }
246 
247 /* restore the value to 32bit */
248 static int copy_ctl_value_to_user(struct snd_ctl_elem_value32 __user *data32,
249 				  struct snd_ctl_elem_value *data,
250 				  int type, int count)
251 {
252 	int i, size;
253 
254 	if (type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
255 	    type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
256 		for (i = 0; i < count; i++) {
257 			int val;
258 			val = data->value.integer.value[i];
259 			if (put_user(val, &data32->value.integer[i]))
260 				return -EFAULT;
261 		}
262 	} else {
263 		size = get_elem_size(type, count);
264 		if (copy_to_user(data32->value.data,
265 				 data->value.bytes.data, size))
266 			return -EFAULT;
267 	}
268 	return 0;
269 }
270 
271 static int snd_ctl_elem_read_user_compat(struct snd_card *card,
272 					 struct snd_ctl_elem_value32 __user *data32)
273 {
274 	struct snd_ctl_elem_value *data;
275 	int err, type, count;
276 
277 	data = kzalloc(sizeof(*data), GFP_KERNEL);
278 	if (data == NULL)
279 		return -ENOMEM;
280 
281 	if ((err = copy_ctl_value_from_user(card, data, data32, &type, &count)) < 0)
282 		goto error;
283 	if ((err = snd_ctl_elem_read(card, data)) < 0)
284 		goto error;
285 	err = copy_ctl_value_to_user(data32, data, type, count);
286  error:
287 	kfree(data);
288 	return err;
289 }
290 
291 static int snd_ctl_elem_write_user_compat(struct snd_ctl_file *file,
292 					  struct snd_ctl_elem_value32 __user *data32)
293 {
294 	struct snd_ctl_elem_value *data;
295 	int err, type, count;
296 
297 	data = kzalloc(sizeof(*data), GFP_KERNEL);
298 	if (data == NULL)
299 		return -ENOMEM;
300 
301 	if ((err = copy_ctl_value_from_user(file->card, data, data32, &type, &count)) < 0)
302 		goto error;
303 	if ((err = snd_ctl_elem_write(file->card, file, data)) < 0)
304 		goto error;
305 	err = copy_ctl_value_to_user(data32, data, type, count);
306  error:
307 	kfree(data);
308 	return err;
309 }
310 
311 /* add or replace a user control */
312 static int snd_ctl_elem_add_compat(struct snd_ctl_file *file,
313 				   struct snd_ctl_elem_info32 __user *data32,
314 				   int replace)
315 {
316 	struct snd_ctl_elem_info *data;
317 	int err;
318 
319 	data = kzalloc(sizeof(*data), GFP_KERNEL);
320 	if (! data)
321 		return -ENOMEM;
322 
323 	err = -EFAULT;
324 	/* id, type, access, count */ \
325 	if (copy_from_user(&data->id, &data32->id, sizeof(data->id)) ||
326 	    copy_from_user(&data->type, &data32->type, 3 * sizeof(u32)))
327 		goto error;
328 	if (get_user(data->owner, &data32->owner) ||
329 	    get_user(data->type, &data32->type))
330 		goto error;
331 	switch (data->type) {
332 	case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
333 	case SNDRV_CTL_ELEM_TYPE_INTEGER:
334 		if (get_user(data->value.integer.min, &data32->value.integer.min) ||
335 		    get_user(data->value.integer.max, &data32->value.integer.max) ||
336 		    get_user(data->value.integer.step, &data32->value.integer.step))
337 			goto error;
338 		break;
339 	case SNDRV_CTL_ELEM_TYPE_INTEGER64:
340 		if (copy_from_user(&data->value.integer64,
341 				   &data32->value.integer64,
342 				   sizeof(data->value.integer64)))
343 			goto error;
344 		break;
345 	case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
346 		if (copy_from_user(&data->value.enumerated,
347 				   &data32->value.enumerated,
348 				   sizeof(data->value.enumerated)))
349 			goto error;
350 		break;
351 	default:
352 		break;
353 	}
354 	err = snd_ctl_elem_add(file, data, replace);
355  error:
356 	kfree(data);
357 	return err;
358 }
359 
360 enum {
361 	SNDRV_CTL_IOCTL_ELEM_LIST32 = _IOWR('U', 0x10, struct snd_ctl_elem_list32),
362 	SNDRV_CTL_IOCTL_ELEM_INFO32 = _IOWR('U', 0x11, struct snd_ctl_elem_info32),
363 	SNDRV_CTL_IOCTL_ELEM_READ32 = _IOWR('U', 0x12, struct snd_ctl_elem_value32),
364 	SNDRV_CTL_IOCTL_ELEM_WRITE32 = _IOWR('U', 0x13, struct snd_ctl_elem_value32),
365 	SNDRV_CTL_IOCTL_ELEM_ADD32 = _IOWR('U', 0x17, struct snd_ctl_elem_info32),
366 	SNDRV_CTL_IOCTL_ELEM_REPLACE32 = _IOWR('U', 0x18, struct snd_ctl_elem_info32),
367 };
368 
369 static inline long snd_ctl_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
370 {
371 	struct snd_ctl_file *ctl;
372 	struct list_head *list;
373 	void __user *argp = compat_ptr(arg);
374 	int err;
375 
376 	ctl = file->private_data;
377 	snd_assert(ctl && ctl->card, return -ENXIO);
378 
379 	switch (cmd) {
380 	case SNDRV_CTL_IOCTL_PVERSION:
381 	case SNDRV_CTL_IOCTL_CARD_INFO:
382 	case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
383 	case SNDRV_CTL_IOCTL_POWER:
384 	case SNDRV_CTL_IOCTL_POWER_STATE:
385 	case SNDRV_CTL_IOCTL_ELEM_LOCK:
386 	case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
387 		return snd_ctl_ioctl(file, cmd, (unsigned long)argp);
388 	case SNDRV_CTL_IOCTL_ELEM_LIST32:
389 		return snd_ctl_elem_list_compat(ctl->card, argp);
390 	case SNDRV_CTL_IOCTL_ELEM_INFO32:
391 		return snd_ctl_elem_info_compat(ctl, argp);
392 	case SNDRV_CTL_IOCTL_ELEM_READ32:
393 		return snd_ctl_elem_read_user_compat(ctl->card, argp);
394 	case SNDRV_CTL_IOCTL_ELEM_WRITE32:
395 		return snd_ctl_elem_write_user_compat(ctl, argp);
396 	case SNDRV_CTL_IOCTL_ELEM_ADD32:
397 		return snd_ctl_elem_add_compat(ctl, argp, 0);
398 	case SNDRV_CTL_IOCTL_ELEM_REPLACE32:
399 		return snd_ctl_elem_add_compat(ctl, argp, 1);
400 	}
401 
402 	down_read(&snd_ioctl_rwsem);
403 	list_for_each(list, &snd_control_compat_ioctls) {
404 		struct snd_kctl_ioctl *p = list_entry(list, struct snd_kctl_ioctl, list);
405 		if (p->fioctl) {
406 			err = p->fioctl(ctl->card, ctl, cmd, arg);
407 			if (err != -ENOIOCTLCMD) {
408 				up_read(&snd_ioctl_rwsem);
409 				return err;
410 			}
411 		}
412 	}
413 	up_read(&snd_ioctl_rwsem);
414 	return -ENOIOCTLCMD;
415 }
416