1 /*
2  *  Routines for driver control interface
3  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4  *
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 
22 #include <linux/threads.h>
23 #include <linux/interrupt.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
26 #include <linux/time.h>
27 #include <sound/core.h>
28 #include <sound/minors.h>
29 #include <sound/info.h>
30 #include <sound/control.h>
31 
32 /* max number of user-defined controls */
33 #define MAX_USER_CONTROLS	32
34 #define MAX_CONTROL_COUNT	1028
35 
36 struct snd_kctl_ioctl {
37 	struct list_head list;		/* list of all ioctls */
38 	snd_kctl_ioctl_func_t fioctl;
39 };
40 
41 static DECLARE_RWSEM(snd_ioctl_rwsem);
42 static LIST_HEAD(snd_control_ioctls);
43 #ifdef CONFIG_COMPAT
44 static LIST_HEAD(snd_control_compat_ioctls);
45 #endif
46 
snd_ctl_open(struct inode * inode,struct file * file)47 static int snd_ctl_open(struct inode *inode, struct file *file)
48 {
49 	unsigned long flags;
50 	struct snd_card *card;
51 	struct snd_ctl_file *ctl;
52 	int err;
53 
54 	err = nonseekable_open(inode, file);
55 	if (err < 0)
56 		return err;
57 
58 	card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
59 	if (!card) {
60 		err = -ENODEV;
61 		goto __error1;
62 	}
63 	err = snd_card_file_add(card, file);
64 	if (err < 0) {
65 		err = -ENODEV;
66 		goto __error1;
67 	}
68 	if (!try_module_get(card->module)) {
69 		err = -EFAULT;
70 		goto __error2;
71 	}
72 	ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
73 	if (ctl == NULL) {
74 		err = -ENOMEM;
75 		goto __error;
76 	}
77 	INIT_LIST_HEAD(&ctl->events);
78 	init_waitqueue_head(&ctl->change_sleep);
79 	spin_lock_init(&ctl->read_lock);
80 	ctl->card = card;
81 	ctl->prefer_pcm_subdevice = -1;
82 	ctl->prefer_rawmidi_subdevice = -1;
83 	ctl->pid = get_pid(task_pid(current));
84 	file->private_data = ctl;
85 	write_lock_irqsave(&card->ctl_files_rwlock, flags);
86 	list_add_tail(&ctl->list, &card->ctl_files);
87 	write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
88 	return 0;
89 
90       __error:
91 	module_put(card->module);
92       __error2:
93 	snd_card_file_remove(card, file);
94       __error1:
95       	return err;
96 }
97 
snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)98 static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
99 {
100 	unsigned long flags;
101 	struct snd_kctl_event *cread;
102 
103 	spin_lock_irqsave(&ctl->read_lock, flags);
104 	while (!list_empty(&ctl->events)) {
105 		cread = snd_kctl_event(ctl->events.next);
106 		list_del(&cread->list);
107 		kfree(cread);
108 	}
109 	spin_unlock_irqrestore(&ctl->read_lock, flags);
110 }
111 
snd_ctl_release(struct inode * inode,struct file * file)112 static int snd_ctl_release(struct inode *inode, struct file *file)
113 {
114 	unsigned long flags;
115 	struct snd_card *card;
116 	struct snd_ctl_file *ctl;
117 	struct snd_kcontrol *control;
118 	unsigned int idx;
119 
120 	ctl = file->private_data;
121 	file->private_data = NULL;
122 	card = ctl->card;
123 	write_lock_irqsave(&card->ctl_files_rwlock, flags);
124 	list_del(&ctl->list);
125 	write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
126 	down_write(&card->controls_rwsem);
127 	list_for_each_entry(control, &card->controls, list)
128 		for (idx = 0; idx < control->count; idx++)
129 			if (control->vd[idx].owner == ctl)
130 				control->vd[idx].owner = NULL;
131 	up_write(&card->controls_rwsem);
132 	snd_ctl_empty_read_queue(ctl);
133 	put_pid(ctl->pid);
134 	kfree(ctl);
135 	module_put(card->module);
136 	snd_card_file_remove(card, file);
137 	return 0;
138 }
139 
snd_ctl_notify(struct snd_card * card,unsigned int mask,struct snd_ctl_elem_id * id)140 void snd_ctl_notify(struct snd_card *card, unsigned int mask,
141 		    struct snd_ctl_elem_id *id)
142 {
143 	unsigned long flags;
144 	struct snd_ctl_file *ctl;
145 	struct snd_kctl_event *ev;
146 
147 	if (snd_BUG_ON(!card || !id))
148 		return;
149 	read_lock(&card->ctl_files_rwlock);
150 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
151 	card->mixer_oss_change_count++;
152 #endif
153 	list_for_each_entry(ctl, &card->ctl_files, list) {
154 		if (!ctl->subscribed)
155 			continue;
156 		spin_lock_irqsave(&ctl->read_lock, flags);
157 		list_for_each_entry(ev, &ctl->events, list) {
158 			if (ev->id.numid == id->numid) {
159 				ev->mask |= mask;
160 				goto _found;
161 			}
162 		}
163 		ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
164 		if (ev) {
165 			ev->id = *id;
166 			ev->mask = mask;
167 			list_add_tail(&ev->list, &ctl->events);
168 		} else {
169 			snd_printk(KERN_ERR "No memory available to allocate event\n");
170 		}
171 	_found:
172 		wake_up(&ctl->change_sleep);
173 		spin_unlock_irqrestore(&ctl->read_lock, flags);
174 		kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
175 	}
176 	read_unlock(&card->ctl_files_rwlock);
177 }
178 
179 EXPORT_SYMBOL(snd_ctl_notify);
180 
181 /**
182  * snd_ctl_new - create a control instance from the template
183  * @control: the control template
184  * @access: the default control access
185  *
186  * Allocates a new struct snd_kcontrol instance and copies the given template
187  * to the new instance. It does not copy volatile data (access).
188  *
189  * Returns the pointer of the new instance, or NULL on failure.
190  */
snd_ctl_new(struct snd_kcontrol * control,unsigned int access)191 static struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control,
192 					unsigned int access)
193 {
194 	struct snd_kcontrol *kctl;
195 	unsigned int idx;
196 
197 	if (snd_BUG_ON(!control || !control->count))
198 		return NULL;
199 
200 	if (control->count > MAX_CONTROL_COUNT)
201 		return NULL;
202 
203 	kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL);
204 	if (kctl == NULL) {
205 		snd_printk(KERN_ERR "Cannot allocate control instance\n");
206 		return NULL;
207 	}
208 	*kctl = *control;
209 	for (idx = 0; idx < kctl->count; idx++)
210 		kctl->vd[idx].access = access;
211 	return kctl;
212 }
213 
214 /**
215  * snd_ctl_new1 - create a control instance from the template
216  * @ncontrol: the initialization record
217  * @private_data: the private data to set
218  *
219  * Allocates a new struct snd_kcontrol instance and initialize from the given
220  * template.  When the access field of ncontrol is 0, it's assumed as
221  * READWRITE access. When the count field is 0, it's assumes as one.
222  *
223  * Returns the pointer of the newly generated instance, or NULL on failure.
224  */
snd_ctl_new1(const struct snd_kcontrol_new * ncontrol,void * private_data)225 struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
226 				  void *private_data)
227 {
228 	struct snd_kcontrol kctl;
229 	unsigned int access;
230 
231 	if (snd_BUG_ON(!ncontrol || !ncontrol->info))
232 		return NULL;
233 	memset(&kctl, 0, sizeof(kctl));
234 	kctl.id.iface = ncontrol->iface;
235 	kctl.id.device = ncontrol->device;
236 	kctl.id.subdevice = ncontrol->subdevice;
237 	if (ncontrol->name) {
238 		strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
239 		if (strcmp(ncontrol->name, kctl.id.name) != 0)
240 			snd_printk(KERN_WARNING
241 				   "Control name '%s' truncated to '%s'\n",
242 				   ncontrol->name, kctl.id.name);
243 	}
244 	kctl.id.index = ncontrol->index;
245 	kctl.count = ncontrol->count ? ncontrol->count : 1;
246 	access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
247 		 (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
248 				      SNDRV_CTL_ELEM_ACCESS_INACTIVE|
249 				      SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE|
250 				      SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND|
251 				      SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK));
252 	kctl.info = ncontrol->info;
253 	kctl.get = ncontrol->get;
254 	kctl.put = ncontrol->put;
255 	kctl.tlv.p = ncontrol->tlv.p;
256 	kctl.private_value = ncontrol->private_value;
257 	kctl.private_data = private_data;
258 	return snd_ctl_new(&kctl, access);
259 }
260 
261 EXPORT_SYMBOL(snd_ctl_new1);
262 
263 /**
264  * snd_ctl_free_one - release the control instance
265  * @kcontrol: the control instance
266  *
267  * Releases the control instance created via snd_ctl_new()
268  * or snd_ctl_new1().
269  * Don't call this after the control was added to the card.
270  */
snd_ctl_free_one(struct snd_kcontrol * kcontrol)271 void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
272 {
273 	if (kcontrol) {
274 		if (kcontrol->private_free)
275 			kcontrol->private_free(kcontrol);
276 		kfree(kcontrol);
277 	}
278 }
279 
280 EXPORT_SYMBOL(snd_ctl_free_one);
281 
snd_ctl_remove_numid_conflict(struct snd_card * card,unsigned int count)282 static bool snd_ctl_remove_numid_conflict(struct snd_card *card,
283 					  unsigned int count)
284 {
285 	struct snd_kcontrol *kctl;
286 
287 	list_for_each_entry(kctl, &card->controls, list) {
288 		if (kctl->id.numid < card->last_numid + 1 + count &&
289 		    kctl->id.numid + kctl->count > card->last_numid + 1) {
290 		    	card->last_numid = kctl->id.numid + kctl->count - 1;
291 			return true;
292 		}
293 	}
294 	return false;
295 }
296 
snd_ctl_find_hole(struct snd_card * card,unsigned int count)297 static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
298 {
299 	unsigned int iter = 100000;
300 
301 	while (snd_ctl_remove_numid_conflict(card, count)) {
302 		if (--iter == 0) {
303 			/* this situation is very unlikely */
304 			snd_printk(KERN_ERR "unable to allocate new control numid\n");
305 			return -ENOMEM;
306 		}
307 	}
308 	return 0;
309 }
310 
311 /**
312  * snd_ctl_add - add the control instance to the card
313  * @card: the card instance
314  * @kcontrol: the control instance to add
315  *
316  * Adds the control instance created via snd_ctl_new() or
317  * snd_ctl_new1() to the given card. Assigns also an unique
318  * numid used for fast search.
319  *
320  * Returns zero if successful, or a negative error code on failure.
321  *
322  * It frees automatically the control which cannot be added.
323  */
snd_ctl_add(struct snd_card * card,struct snd_kcontrol * kcontrol)324 int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
325 {
326 	struct snd_ctl_elem_id id;
327 	unsigned int idx;
328 	int err = -EINVAL;
329 
330 	if (! kcontrol)
331 		return err;
332 	if (snd_BUG_ON(!card || !kcontrol->info))
333 		goto error;
334 	id = kcontrol->id;
335 	down_write(&card->controls_rwsem);
336 	if (snd_ctl_find_id(card, &id)) {
337 		up_write(&card->controls_rwsem);
338 		snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
339 					id.iface,
340 					id.device,
341 					id.subdevice,
342 					id.name,
343 					id.index);
344 		err = -EBUSY;
345 		goto error;
346 	}
347 	if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
348 		up_write(&card->controls_rwsem);
349 		err = -ENOMEM;
350 		goto error;
351 	}
352 	list_add_tail(&kcontrol->list, &card->controls);
353 	card->controls_count += kcontrol->count;
354 	kcontrol->id.numid = card->last_numid + 1;
355 	card->last_numid += kcontrol->count;
356 	up_write(&card->controls_rwsem);
357 	for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
358 		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
359 	return 0;
360 
361  error:
362 	snd_ctl_free_one(kcontrol);
363 	return err;
364 }
365 
366 EXPORT_SYMBOL(snd_ctl_add);
367 
368 /**
369  * snd_ctl_remove - remove the control from the card and release it
370  * @card: the card instance
371  * @kcontrol: the control instance to remove
372  *
373  * Removes the control from the card and then releases the instance.
374  * You don't need to call snd_ctl_free_one(). You must be in
375  * the write lock - down_write(&card->controls_rwsem).
376  *
377  * Returns 0 if successful, or a negative error code on failure.
378  */
snd_ctl_remove(struct snd_card * card,struct snd_kcontrol * kcontrol)379 int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
380 {
381 	struct snd_ctl_elem_id id;
382 	unsigned int idx;
383 
384 	if (snd_BUG_ON(!card || !kcontrol))
385 		return -EINVAL;
386 	list_del(&kcontrol->list);
387 	card->controls_count -= kcontrol->count;
388 	id = kcontrol->id;
389 	for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
390 		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
391 	snd_ctl_free_one(kcontrol);
392 	return 0;
393 }
394 
395 EXPORT_SYMBOL(snd_ctl_remove);
396 
397 /**
398  * snd_ctl_remove_id - remove the control of the given id and release it
399  * @card: the card instance
400  * @id: the control id to remove
401  *
402  * Finds the control instance with the given id, removes it from the
403  * card list and releases it.
404  *
405  * Returns 0 if successful, or a negative error code on failure.
406  */
snd_ctl_remove_id(struct snd_card * card,struct snd_ctl_elem_id * id)407 int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
408 {
409 	struct snd_kcontrol *kctl;
410 	int ret;
411 
412 	down_write(&card->controls_rwsem);
413 	kctl = snd_ctl_find_id(card, id);
414 	if (kctl == NULL) {
415 		up_write(&card->controls_rwsem);
416 		return -ENOENT;
417 	}
418 	ret = snd_ctl_remove(card, kctl);
419 	up_write(&card->controls_rwsem);
420 	return ret;
421 }
422 
423 EXPORT_SYMBOL(snd_ctl_remove_id);
424 
425 /**
426  * snd_ctl_remove_user_ctl - remove and release the unlocked user control
427  * @file: active control handle
428  * @id: the control id to remove
429  *
430  * Finds the control instance with the given id, removes it from the
431  * card list and releases it.
432  *
433  * Returns 0 if successful, or a negative error code on failure.
434  */
snd_ctl_remove_user_ctl(struct snd_ctl_file * file,struct snd_ctl_elem_id * id)435 static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
436 				   struct snd_ctl_elem_id *id)
437 {
438 	struct snd_card *card = file->card;
439 	struct snd_kcontrol *kctl;
440 	int idx, ret;
441 
442 	down_write(&card->controls_rwsem);
443 	kctl = snd_ctl_find_id(card, id);
444 	if (kctl == NULL) {
445 		ret = -ENOENT;
446 		goto error;
447 	}
448 	if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
449 		ret = -EINVAL;
450 		goto error;
451 	}
452 	for (idx = 0; idx < kctl->count; idx++)
453 		if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
454 			ret = -EBUSY;
455 			goto error;
456 		}
457 	ret = snd_ctl_remove(card, kctl);
458 	if (ret < 0)
459 		goto error;
460 	card->user_ctl_count--;
461 error:
462 	up_write(&card->controls_rwsem);
463 	return ret;
464 }
465 
466 /**
467  * snd_ctl_activate_id - activate/inactivate the control of the given id
468  * @card: the card instance
469  * @id: the control id to activate/inactivate
470  * @active: non-zero to activate
471  *
472  * Finds the control instance with the given id, and activate or
473  * inactivate the control together with notification, if changed.
474  *
475  * Returns 0 if unchanged, 1 if changed, or a negative error code on failure.
476  */
snd_ctl_activate_id(struct snd_card * card,struct snd_ctl_elem_id * id,int active)477 int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
478 			int active)
479 {
480 	struct snd_kcontrol *kctl;
481 	struct snd_kcontrol_volatile *vd;
482 	unsigned int index_offset;
483 	int ret;
484 
485 	down_write(&card->controls_rwsem);
486 	kctl = snd_ctl_find_id(card, id);
487 	if (kctl == NULL) {
488 		ret = -ENOENT;
489 		goto unlock;
490 	}
491 	index_offset = snd_ctl_get_ioff(kctl, &kctl->id);
492 	vd = &kctl->vd[index_offset];
493 	ret = 0;
494 	if (active) {
495 		if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
496 			goto unlock;
497 		vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
498 	} else {
499 		if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)
500 			goto unlock;
501 		vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
502 	}
503 	ret = 1;
504  unlock:
505 	up_write(&card->controls_rwsem);
506 	if (ret > 0)
507 		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, id);
508 	return ret;
509 }
510 EXPORT_SYMBOL_GPL(snd_ctl_activate_id);
511 
512 /**
513  * snd_ctl_rename_id - replace the id of a control on the card
514  * @card: the card instance
515  * @src_id: the old id
516  * @dst_id: the new id
517  *
518  * Finds the control with the old id from the card, and replaces the
519  * id with the new one.
520  *
521  * Returns zero if successful, or a negative error code on failure.
522  */
snd_ctl_rename_id(struct snd_card * card,struct snd_ctl_elem_id * src_id,struct snd_ctl_elem_id * dst_id)523 int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
524 		      struct snd_ctl_elem_id *dst_id)
525 {
526 	struct snd_kcontrol *kctl;
527 
528 	down_write(&card->controls_rwsem);
529 	kctl = snd_ctl_find_id(card, src_id);
530 	if (kctl == NULL) {
531 		up_write(&card->controls_rwsem);
532 		return -ENOENT;
533 	}
534 	kctl->id = *dst_id;
535 	kctl->id.numid = card->last_numid + 1;
536 	card->last_numid += kctl->count;
537 	up_write(&card->controls_rwsem);
538 	return 0;
539 }
540 
541 EXPORT_SYMBOL(snd_ctl_rename_id);
542 
543 /**
544  * snd_ctl_find_numid - find the control instance with the given number-id
545  * @card: the card instance
546  * @numid: the number-id to search
547  *
548  * Finds the control instance with the given number-id from the card.
549  *
550  * Returns the pointer of the instance if found, or NULL if not.
551  *
552  * The caller must down card->controls_rwsem before calling this function
553  * (if the race condition can happen).
554  */
snd_ctl_find_numid(struct snd_card * card,unsigned int numid)555 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
556 {
557 	struct snd_kcontrol *kctl;
558 
559 	if (snd_BUG_ON(!card || !numid))
560 		return NULL;
561 	list_for_each_entry(kctl, &card->controls, list) {
562 		if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
563 			return kctl;
564 	}
565 	return NULL;
566 }
567 
568 EXPORT_SYMBOL(snd_ctl_find_numid);
569 
570 /**
571  * snd_ctl_find_id - find the control instance with the given id
572  * @card: the card instance
573  * @id: the id to search
574  *
575  * Finds the control instance with the given id from the card.
576  *
577  * Returns the pointer of the instance if found, or NULL if not.
578  *
579  * The caller must down card->controls_rwsem before calling this function
580  * (if the race condition can happen).
581  */
snd_ctl_find_id(struct snd_card * card,struct snd_ctl_elem_id * id)582 struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
583 				     struct snd_ctl_elem_id *id)
584 {
585 	struct snd_kcontrol *kctl;
586 
587 	if (snd_BUG_ON(!card || !id))
588 		return NULL;
589 	if (id->numid != 0)
590 		return snd_ctl_find_numid(card, id->numid);
591 	list_for_each_entry(kctl, &card->controls, list) {
592 		if (kctl->id.iface != id->iface)
593 			continue;
594 		if (kctl->id.device != id->device)
595 			continue;
596 		if (kctl->id.subdevice != id->subdevice)
597 			continue;
598 		if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
599 			continue;
600 		if (kctl->id.index > id->index)
601 			continue;
602 		if (kctl->id.index + kctl->count <= id->index)
603 			continue;
604 		return kctl;
605 	}
606 	return NULL;
607 }
608 
609 EXPORT_SYMBOL(snd_ctl_find_id);
610 
snd_ctl_card_info(struct snd_card * card,struct snd_ctl_file * ctl,unsigned int cmd,void __user * arg)611 static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
612 			     unsigned int cmd, void __user *arg)
613 {
614 	struct snd_ctl_card_info *info;
615 
616 	info = kzalloc(sizeof(*info), GFP_KERNEL);
617 	if (! info)
618 		return -ENOMEM;
619 	down_read(&snd_ioctl_rwsem);
620 	info->card = card->number;
621 	strlcpy(info->id, card->id, sizeof(info->id));
622 	strlcpy(info->driver, card->driver, sizeof(info->driver));
623 	strlcpy(info->name, card->shortname, sizeof(info->name));
624 	strlcpy(info->longname, card->longname, sizeof(info->longname));
625 	strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
626 	strlcpy(info->components, card->components, sizeof(info->components));
627 	up_read(&snd_ioctl_rwsem);
628 	if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
629 		kfree(info);
630 		return -EFAULT;
631 	}
632 	kfree(info);
633 	return 0;
634 }
635 
snd_ctl_elem_list(struct snd_card * card,struct snd_ctl_elem_list __user * _list)636 static int snd_ctl_elem_list(struct snd_card *card,
637 			     struct snd_ctl_elem_list __user *_list)
638 {
639 	struct list_head *plist;
640 	struct snd_ctl_elem_list list;
641 	struct snd_kcontrol *kctl;
642 	struct snd_ctl_elem_id *dst, *id;
643 	unsigned int offset, space, first, jidx;
644 
645 	if (copy_from_user(&list, _list, sizeof(list)))
646 		return -EFAULT;
647 	offset = list.offset;
648 	space = list.space;
649 	first = 0;
650 	/* try limit maximum space */
651 	if (space > 16384)
652 		return -ENOMEM;
653 	if (space > 0) {
654 		/* allocate temporary buffer for atomic operation */
655 		dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
656 		if (dst == NULL)
657 			return -ENOMEM;
658 		down_read(&card->controls_rwsem);
659 		list.count = card->controls_count;
660 		plist = card->controls.next;
661 		while (plist != &card->controls) {
662 			if (offset == 0)
663 				break;
664 			kctl = snd_kcontrol(plist);
665 			if (offset < kctl->count)
666 				break;
667 			offset -= kctl->count;
668 			plist = plist->next;
669 		}
670 		list.used = 0;
671 		id = dst;
672 		while (space > 0 && plist != &card->controls) {
673 			kctl = snd_kcontrol(plist);
674 			for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
675 				snd_ctl_build_ioff(id, kctl, jidx);
676 				id++;
677 				space--;
678 				list.used++;
679 			}
680 			plist = plist->next;
681 			offset = 0;
682 		}
683 		up_read(&card->controls_rwsem);
684 		if (list.used > 0 &&
685 		    copy_to_user(list.pids, dst,
686 				 list.used * sizeof(struct snd_ctl_elem_id))) {
687 			vfree(dst);
688 			return -EFAULT;
689 		}
690 		vfree(dst);
691 	} else {
692 		down_read(&card->controls_rwsem);
693 		list.count = card->controls_count;
694 		up_read(&card->controls_rwsem);
695 	}
696 	if (copy_to_user(_list, &list, sizeof(list)))
697 		return -EFAULT;
698 	return 0;
699 }
700 
snd_ctl_elem_info(struct snd_ctl_file * ctl,struct snd_ctl_elem_info * info)701 static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
702 			     struct snd_ctl_elem_info *info)
703 {
704 	struct snd_card *card = ctl->card;
705 	struct snd_kcontrol *kctl;
706 	struct snd_kcontrol_volatile *vd;
707 	unsigned int index_offset;
708 	int result;
709 
710 	down_read(&card->controls_rwsem);
711 	kctl = snd_ctl_find_id(card, &info->id);
712 	if (kctl == NULL) {
713 		up_read(&card->controls_rwsem);
714 		return -ENOENT;
715 	}
716 #ifdef CONFIG_SND_DEBUG
717 	info->access = 0;
718 #endif
719 	result = kctl->info(kctl, info);
720 	if (result >= 0) {
721 		snd_BUG_ON(info->access);
722 		index_offset = snd_ctl_get_ioff(kctl, &info->id);
723 		vd = &kctl->vd[index_offset];
724 		snd_ctl_build_ioff(&info->id, kctl, index_offset);
725 		info->access = vd->access;
726 		if (vd->owner) {
727 			info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
728 			if (vd->owner == ctl)
729 				info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
730 			info->owner = pid_vnr(vd->owner->pid);
731 		} else {
732 			info->owner = -1;
733 		}
734 	}
735 	up_read(&card->controls_rwsem);
736 	return result;
737 }
738 
snd_ctl_elem_info_user(struct snd_ctl_file * ctl,struct snd_ctl_elem_info __user * _info)739 static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
740 				  struct snd_ctl_elem_info __user *_info)
741 {
742 	struct snd_ctl_elem_info info;
743 	int result;
744 
745 	if (copy_from_user(&info, _info, sizeof(info)))
746 		return -EFAULT;
747 	snd_power_lock(ctl->card);
748 	result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
749 	if (result >= 0)
750 		result = snd_ctl_elem_info(ctl, &info);
751 	snd_power_unlock(ctl->card);
752 	if (result >= 0)
753 		if (copy_to_user(_info, &info, sizeof(info)))
754 			return -EFAULT;
755 	return result;
756 }
757 
snd_ctl_elem_read(struct snd_card * card,struct snd_ctl_elem_value * control)758 static int snd_ctl_elem_read(struct snd_card *card,
759 			     struct snd_ctl_elem_value *control)
760 {
761 	struct snd_kcontrol *kctl;
762 	struct snd_kcontrol_volatile *vd;
763 	unsigned int index_offset;
764 	int result;
765 
766 	down_read(&card->controls_rwsem);
767 	kctl = snd_ctl_find_id(card, &control->id);
768 	if (kctl == NULL) {
769 		result = -ENOENT;
770 	} else {
771 		index_offset = snd_ctl_get_ioff(kctl, &control->id);
772 		vd = &kctl->vd[index_offset];
773 		if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) &&
774 		    kctl->get != NULL) {
775 			snd_ctl_build_ioff(&control->id, kctl, index_offset);
776 			result = kctl->get(kctl, control);
777 		} else
778 			result = -EPERM;
779 	}
780 	up_read(&card->controls_rwsem);
781 	return result;
782 }
783 
snd_ctl_elem_read_user(struct snd_card * card,struct snd_ctl_elem_value __user * _control)784 static int snd_ctl_elem_read_user(struct snd_card *card,
785 				  struct snd_ctl_elem_value __user *_control)
786 {
787 	struct snd_ctl_elem_value *control;
788 	int result;
789 
790 	control = memdup_user(_control, sizeof(*control));
791 	if (IS_ERR(control))
792 		return PTR_ERR(control);
793 
794 	snd_power_lock(card);
795 	result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
796 	if (result >= 0)
797 		result = snd_ctl_elem_read(card, control);
798 	snd_power_unlock(card);
799 	if (result >= 0)
800 		if (copy_to_user(_control, control, sizeof(*control)))
801 			result = -EFAULT;
802 	kfree(control);
803 	return result;
804 }
805 
snd_ctl_elem_write(struct snd_card * card,struct snd_ctl_file * file,struct snd_ctl_elem_value * control)806 static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
807 			      struct snd_ctl_elem_value *control)
808 {
809 	struct snd_kcontrol *kctl;
810 	struct snd_kcontrol_volatile *vd;
811 	unsigned int index_offset;
812 	int result;
813 
814 	down_read(&card->controls_rwsem);
815 	kctl = snd_ctl_find_id(card, &control->id);
816 	if (kctl == NULL) {
817 		result = -ENOENT;
818 	} else {
819 		index_offset = snd_ctl_get_ioff(kctl, &control->id);
820 		vd = &kctl->vd[index_offset];
821 		if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
822 		    kctl->put == NULL ||
823 		    (file && vd->owner && vd->owner != file)) {
824 			result = -EPERM;
825 		} else {
826 			snd_ctl_build_ioff(&control->id, kctl, index_offset);
827 			result = kctl->put(kctl, control);
828 		}
829 		if (result > 0) {
830 			up_read(&card->controls_rwsem);
831 			snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
832 				       &control->id);
833 			return 0;
834 		}
835 	}
836 	up_read(&card->controls_rwsem);
837 	return result;
838 }
839 
snd_ctl_elem_write_user(struct snd_ctl_file * file,struct snd_ctl_elem_value __user * _control)840 static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
841 				   struct snd_ctl_elem_value __user *_control)
842 {
843 	struct snd_ctl_elem_value *control;
844 	struct snd_card *card;
845 	int result;
846 
847 	control = memdup_user(_control, sizeof(*control));
848 	if (IS_ERR(control))
849 		return PTR_ERR(control);
850 
851 	card = file->card;
852 	snd_power_lock(card);
853 	result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
854 	if (result >= 0)
855 		result = snd_ctl_elem_write(card, file, control);
856 	snd_power_unlock(card);
857 	if (result >= 0)
858 		if (copy_to_user(_control, control, sizeof(*control)))
859 			result = -EFAULT;
860 	kfree(control);
861 	return result;
862 }
863 
snd_ctl_elem_lock(struct snd_ctl_file * file,struct snd_ctl_elem_id __user * _id)864 static int snd_ctl_elem_lock(struct snd_ctl_file *file,
865 			     struct snd_ctl_elem_id __user *_id)
866 {
867 	struct snd_card *card = file->card;
868 	struct snd_ctl_elem_id id;
869 	struct snd_kcontrol *kctl;
870 	struct snd_kcontrol_volatile *vd;
871 	int result;
872 
873 	if (copy_from_user(&id, _id, sizeof(id)))
874 		return -EFAULT;
875 	down_write(&card->controls_rwsem);
876 	kctl = snd_ctl_find_id(card, &id);
877 	if (kctl == NULL) {
878 		result = -ENOENT;
879 	} else {
880 		vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
881 		if (vd->owner != NULL)
882 			result = -EBUSY;
883 		else {
884 			vd->owner = file;
885 			result = 0;
886 		}
887 	}
888 	up_write(&card->controls_rwsem);
889 	return result;
890 }
891 
snd_ctl_elem_unlock(struct snd_ctl_file * file,struct snd_ctl_elem_id __user * _id)892 static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
893 			       struct snd_ctl_elem_id __user *_id)
894 {
895 	struct snd_card *card = file->card;
896 	struct snd_ctl_elem_id id;
897 	struct snd_kcontrol *kctl;
898 	struct snd_kcontrol_volatile *vd;
899 	int result;
900 
901 	if (copy_from_user(&id, _id, sizeof(id)))
902 		return -EFAULT;
903 	down_write(&card->controls_rwsem);
904 	kctl = snd_ctl_find_id(card, &id);
905 	if (kctl == NULL) {
906 		result = -ENOENT;
907 	} else {
908 		vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
909 		if (vd->owner == NULL)
910 			result = -EINVAL;
911 		else if (vd->owner != file)
912 			result = -EPERM;
913 		else {
914 			vd->owner = NULL;
915 			result = 0;
916 		}
917 	}
918 	up_write(&card->controls_rwsem);
919 	return result;
920 }
921 
922 struct user_element {
923 	struct snd_ctl_elem_info info;
924 	void *elem_data;		/* element data */
925 	unsigned long elem_data_size;	/* size of element data in bytes */
926 	void *tlv_data;			/* TLV data */
927 	unsigned long tlv_data_size;	/* TLV data size */
928 	void *priv_data;		/* private data (like strings for enumerated type) */
929 	unsigned long priv_data_size;	/* size of private data in bytes */
930 };
931 
snd_ctl_elem_user_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)932 static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
933 				  struct snd_ctl_elem_info *uinfo)
934 {
935 	struct user_element *ue = kcontrol->private_data;
936 
937 	*uinfo = ue->info;
938 	return 0;
939 }
940 
snd_ctl_elem_user_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)941 static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
942 				 struct snd_ctl_elem_value *ucontrol)
943 {
944 	struct user_element *ue = kcontrol->private_data;
945 
946 	memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
947 	return 0;
948 }
949 
snd_ctl_elem_user_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)950 static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
951 				 struct snd_ctl_elem_value *ucontrol)
952 {
953 	int change;
954 	struct user_element *ue = kcontrol->private_data;
955 
956 	change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
957 	if (change)
958 		memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
959 	return change;
960 }
961 
snd_ctl_elem_user_tlv(struct snd_kcontrol * kcontrol,int op_flag,unsigned int size,unsigned int __user * tlv)962 static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol,
963 				 int op_flag,
964 				 unsigned int size,
965 				 unsigned int __user *tlv)
966 {
967 	struct user_element *ue = kcontrol->private_data;
968 	int change = 0;
969 	void *new_data;
970 
971 	if (op_flag > 0) {
972 		if (size > 1024 * 128)	/* sane value */
973 			return -EINVAL;
974 
975 		new_data = memdup_user(tlv, size);
976 		if (IS_ERR(new_data))
977 			return PTR_ERR(new_data);
978 		change = ue->tlv_data_size != size;
979 		if (!change)
980 			change = memcmp(ue->tlv_data, new_data, size);
981 		kfree(ue->tlv_data);
982 		ue->tlv_data = new_data;
983 		ue->tlv_data_size = size;
984 	} else {
985 		if (! ue->tlv_data_size || ! ue->tlv_data)
986 			return -ENXIO;
987 		if (size < ue->tlv_data_size)
988 			return -ENOSPC;
989 		if (copy_to_user(tlv, ue->tlv_data, ue->tlv_data_size))
990 			return -EFAULT;
991 	}
992 	return change;
993 }
994 
snd_ctl_elem_user_free(struct snd_kcontrol * kcontrol)995 static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
996 {
997 	struct user_element *ue = kcontrol->private_data;
998 	if (ue->tlv_data)
999 		kfree(ue->tlv_data);
1000 	kfree(ue);
1001 }
1002 
snd_ctl_elem_add(struct snd_ctl_file * file,struct snd_ctl_elem_info * info,int replace)1003 static int snd_ctl_elem_add(struct snd_ctl_file *file,
1004 			    struct snd_ctl_elem_info *info, int replace)
1005 {
1006 	struct snd_card *card = file->card;
1007 	struct snd_kcontrol kctl, *_kctl;
1008 	unsigned int access;
1009 	long private_size;
1010 	struct user_element *ue;
1011 	int idx, err;
1012 
1013 	if (card->user_ctl_count >= MAX_USER_CONTROLS)
1014 		return -ENOMEM;
1015 	if (info->count < 1)
1016 		return -EINVAL;
1017 	access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
1018 		(info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
1019 				 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
1020 				 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE));
1021 	info->id.numid = 0;
1022 	memset(&kctl, 0, sizeof(kctl));
1023 	down_write(&card->controls_rwsem);
1024 	_kctl = snd_ctl_find_id(card, &info->id);
1025 	err = 0;
1026 	if (_kctl) {
1027 		if (replace)
1028 			err = snd_ctl_remove(card, _kctl);
1029 		else
1030 			err = -EBUSY;
1031 	} else {
1032 		if (replace)
1033 			err = -ENOENT;
1034 	}
1035 	up_write(&card->controls_rwsem);
1036 	if (err < 0)
1037 		return err;
1038 	memcpy(&kctl.id, &info->id, sizeof(info->id));
1039 	kctl.count = info->owner ? info->owner : 1;
1040 	access |= SNDRV_CTL_ELEM_ACCESS_USER;
1041 	kctl.info = snd_ctl_elem_user_info;
1042 	if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1043 		kctl.get = snd_ctl_elem_user_get;
1044 	if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1045 		kctl.put = snd_ctl_elem_user_put;
1046 	if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
1047 		kctl.tlv.c = snd_ctl_elem_user_tlv;
1048 		access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1049 	}
1050 	switch (info->type) {
1051 	case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
1052 	case SNDRV_CTL_ELEM_TYPE_INTEGER:
1053 		private_size = sizeof(long);
1054 		if (info->count > 128)
1055 			return -EINVAL;
1056 		break;
1057 	case SNDRV_CTL_ELEM_TYPE_INTEGER64:
1058 		private_size = sizeof(long long);
1059 		if (info->count > 64)
1060 			return -EINVAL;
1061 		break;
1062 	case SNDRV_CTL_ELEM_TYPE_BYTES:
1063 		private_size = sizeof(unsigned char);
1064 		if (info->count > 512)
1065 			return -EINVAL;
1066 		break;
1067 	case SNDRV_CTL_ELEM_TYPE_IEC958:
1068 		private_size = sizeof(struct snd_aes_iec958);
1069 		if (info->count != 1)
1070 			return -EINVAL;
1071 		break;
1072 	default:
1073 		return -EINVAL;
1074 	}
1075 	private_size *= info->count;
1076 	ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
1077 	if (ue == NULL)
1078 		return -ENOMEM;
1079 	ue->info = *info;
1080 	ue->info.access = 0;
1081 	ue->elem_data = (char *)ue + sizeof(*ue);
1082 	ue->elem_data_size = private_size;
1083 	kctl.private_free = snd_ctl_elem_user_free;
1084 	_kctl = snd_ctl_new(&kctl, access);
1085 	if (_kctl == NULL) {
1086 		kfree(ue);
1087 		return -ENOMEM;
1088 	}
1089 	_kctl->private_data = ue;
1090 	for (idx = 0; idx < _kctl->count; idx++)
1091 		_kctl->vd[idx].owner = file;
1092 	err = snd_ctl_add(card, _kctl);
1093 	if (err < 0)
1094 		return err;
1095 
1096 	down_write(&card->controls_rwsem);
1097 	card->user_ctl_count++;
1098 	up_write(&card->controls_rwsem);
1099 
1100 	return 0;
1101 }
1102 
snd_ctl_elem_add_user(struct snd_ctl_file * file,struct snd_ctl_elem_info __user * _info,int replace)1103 static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1104 				 struct snd_ctl_elem_info __user *_info, int replace)
1105 {
1106 	struct snd_ctl_elem_info info;
1107 	if (copy_from_user(&info, _info, sizeof(info)))
1108 		return -EFAULT;
1109 	return snd_ctl_elem_add(file, &info, replace);
1110 }
1111 
snd_ctl_elem_remove(struct snd_ctl_file * file,struct snd_ctl_elem_id __user * _id)1112 static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1113 			       struct snd_ctl_elem_id __user *_id)
1114 {
1115 	struct snd_ctl_elem_id id;
1116 
1117 	if (copy_from_user(&id, _id, sizeof(id)))
1118 		return -EFAULT;
1119 	return snd_ctl_remove_user_ctl(file, &id);
1120 }
1121 
snd_ctl_subscribe_events(struct snd_ctl_file * file,int __user * ptr)1122 static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
1123 {
1124 	int subscribe;
1125 	if (get_user(subscribe, ptr))
1126 		return -EFAULT;
1127 	if (subscribe < 0) {
1128 		subscribe = file->subscribed;
1129 		if (put_user(subscribe, ptr))
1130 			return -EFAULT;
1131 		return 0;
1132 	}
1133 	if (subscribe) {
1134 		file->subscribed = 1;
1135 		return 0;
1136 	} else if (file->subscribed) {
1137 		snd_ctl_empty_read_queue(file);
1138 		file->subscribed = 0;
1139 	}
1140 	return 0;
1141 }
1142 
snd_ctl_tlv_ioctl(struct snd_ctl_file * file,struct snd_ctl_tlv __user * _tlv,int op_flag)1143 static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1144                              struct snd_ctl_tlv __user *_tlv,
1145                              int op_flag)
1146 {
1147 	struct snd_card *card = file->card;
1148 	struct snd_ctl_tlv tlv;
1149 	struct snd_kcontrol *kctl;
1150 	struct snd_kcontrol_volatile *vd;
1151 	unsigned int len;
1152 	int err = 0;
1153 
1154 	if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
1155 		return -EFAULT;
1156 	if (tlv.length < sizeof(unsigned int) * 2)
1157 		return -EINVAL;
1158 	down_read(&card->controls_rwsem);
1159 	kctl = snd_ctl_find_numid(card, tlv.numid);
1160 	if (kctl == NULL) {
1161 		err = -ENOENT;
1162 		goto __kctl_end;
1163 	}
1164 	if (kctl->tlv.p == NULL) {
1165 		err = -ENXIO;
1166 		goto __kctl_end;
1167 	}
1168 	vd = &kctl->vd[tlv.numid - kctl->id.numid];
1169 	if ((op_flag == 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) == 0) ||
1170 	    (op_flag > 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) == 0) ||
1171 	    (op_flag < 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND) == 0)) {
1172 	    	err = -ENXIO;
1173 	    	goto __kctl_end;
1174 	}
1175 	if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1176 		if (vd->owner != NULL && vd->owner != file) {
1177 			err = -EPERM;
1178 			goto __kctl_end;
1179 		}
1180 		err = kctl->tlv.c(kctl, op_flag, tlv.length, _tlv->tlv);
1181 		if (err > 0) {
1182 			up_read(&card->controls_rwsem);
1183 			snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &kctl->id);
1184 			return 0;
1185 		}
1186 	} else {
1187 		if (op_flag) {
1188 			err = -ENXIO;
1189 			goto __kctl_end;
1190 		}
1191 		len = kctl->tlv.p[1] + 2 * sizeof(unsigned int);
1192 		if (tlv.length < len) {
1193 			err = -ENOMEM;
1194 			goto __kctl_end;
1195 		}
1196 		if (copy_to_user(_tlv->tlv, kctl->tlv.p, len))
1197 			err = -EFAULT;
1198 	}
1199       __kctl_end:
1200 	up_read(&card->controls_rwsem);
1201 	return err;
1202 }
1203 
snd_ctl_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1204 static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1205 {
1206 	struct snd_ctl_file *ctl;
1207 	struct snd_card *card;
1208 	struct snd_kctl_ioctl *p;
1209 	void __user *argp = (void __user *)arg;
1210 	int __user *ip = argp;
1211 	int err;
1212 
1213 	ctl = file->private_data;
1214 	card = ctl->card;
1215 	if (snd_BUG_ON(!card))
1216 		return -ENXIO;
1217 	switch (cmd) {
1218 	case SNDRV_CTL_IOCTL_PVERSION:
1219 		return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1220 	case SNDRV_CTL_IOCTL_CARD_INFO:
1221 		return snd_ctl_card_info(card, ctl, cmd, argp);
1222 	case SNDRV_CTL_IOCTL_ELEM_LIST:
1223 		return snd_ctl_elem_list(card, argp);
1224 	case SNDRV_CTL_IOCTL_ELEM_INFO:
1225 		return snd_ctl_elem_info_user(ctl, argp);
1226 	case SNDRV_CTL_IOCTL_ELEM_READ:
1227 		return snd_ctl_elem_read_user(card, argp);
1228 	case SNDRV_CTL_IOCTL_ELEM_WRITE:
1229 		return snd_ctl_elem_write_user(ctl, argp);
1230 	case SNDRV_CTL_IOCTL_ELEM_LOCK:
1231 		return snd_ctl_elem_lock(ctl, argp);
1232 	case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1233 		return snd_ctl_elem_unlock(ctl, argp);
1234 	case SNDRV_CTL_IOCTL_ELEM_ADD:
1235 		return snd_ctl_elem_add_user(ctl, argp, 0);
1236 	case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1237 		return snd_ctl_elem_add_user(ctl, argp, 1);
1238 	case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1239 		return snd_ctl_elem_remove(ctl, argp);
1240 	case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1241 		return snd_ctl_subscribe_events(ctl, ip);
1242 	case SNDRV_CTL_IOCTL_TLV_READ:
1243 		return snd_ctl_tlv_ioctl(ctl, argp, 0);
1244 	case SNDRV_CTL_IOCTL_TLV_WRITE:
1245 		return snd_ctl_tlv_ioctl(ctl, argp, 1);
1246 	case SNDRV_CTL_IOCTL_TLV_COMMAND:
1247 		return snd_ctl_tlv_ioctl(ctl, argp, -1);
1248 	case SNDRV_CTL_IOCTL_POWER:
1249 		return -ENOPROTOOPT;
1250 	case SNDRV_CTL_IOCTL_POWER_STATE:
1251 #ifdef CONFIG_PM
1252 		return put_user(card->power_state, ip) ? -EFAULT : 0;
1253 #else
1254 		return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1255 #endif
1256 	}
1257 	down_read(&snd_ioctl_rwsem);
1258 	list_for_each_entry(p, &snd_control_ioctls, list) {
1259 		err = p->fioctl(card, ctl, cmd, arg);
1260 		if (err != -ENOIOCTLCMD) {
1261 			up_read(&snd_ioctl_rwsem);
1262 			return err;
1263 		}
1264 	}
1265 	up_read(&snd_ioctl_rwsem);
1266 	snd_printdd("unknown ioctl = 0x%x\n", cmd);
1267 	return -ENOTTY;
1268 }
1269 
snd_ctl_read(struct file * file,char __user * buffer,size_t count,loff_t * offset)1270 static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1271 			    size_t count, loff_t * offset)
1272 {
1273 	struct snd_ctl_file *ctl;
1274 	int err = 0;
1275 	ssize_t result = 0;
1276 
1277 	ctl = file->private_data;
1278 	if (snd_BUG_ON(!ctl || !ctl->card))
1279 		return -ENXIO;
1280 	if (!ctl->subscribed)
1281 		return -EBADFD;
1282 	if (count < sizeof(struct snd_ctl_event))
1283 		return -EINVAL;
1284 	spin_lock_irq(&ctl->read_lock);
1285 	while (count >= sizeof(struct snd_ctl_event)) {
1286 		struct snd_ctl_event ev;
1287 		struct snd_kctl_event *kev;
1288 		while (list_empty(&ctl->events)) {
1289 			wait_queue_t wait;
1290 			if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1291 				err = -EAGAIN;
1292 				goto __end_lock;
1293 			}
1294 			init_waitqueue_entry(&wait, current);
1295 			add_wait_queue(&ctl->change_sleep, &wait);
1296 			set_current_state(TASK_INTERRUPTIBLE);
1297 			spin_unlock_irq(&ctl->read_lock);
1298 			schedule();
1299 			remove_wait_queue(&ctl->change_sleep, &wait);
1300 			if (signal_pending(current))
1301 				return -ERESTARTSYS;
1302 			spin_lock_irq(&ctl->read_lock);
1303 		}
1304 		kev = snd_kctl_event(ctl->events.next);
1305 		ev.type = SNDRV_CTL_EVENT_ELEM;
1306 		ev.data.elem.mask = kev->mask;
1307 		ev.data.elem.id = kev->id;
1308 		list_del(&kev->list);
1309 		spin_unlock_irq(&ctl->read_lock);
1310 		kfree(kev);
1311 		if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
1312 			err = -EFAULT;
1313 			goto __end;
1314 		}
1315 		spin_lock_irq(&ctl->read_lock);
1316 		buffer += sizeof(struct snd_ctl_event);
1317 		count -= sizeof(struct snd_ctl_event);
1318 		result += sizeof(struct snd_ctl_event);
1319 	}
1320       __end_lock:
1321 	spin_unlock_irq(&ctl->read_lock);
1322       __end:
1323       	return result > 0 ? result : err;
1324 }
1325 
snd_ctl_poll(struct file * file,poll_table * wait)1326 static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1327 {
1328 	unsigned int mask;
1329 	struct snd_ctl_file *ctl;
1330 
1331 	ctl = file->private_data;
1332 	if (!ctl->subscribed)
1333 		return 0;
1334 	poll_wait(file, &ctl->change_sleep, wait);
1335 
1336 	mask = 0;
1337 	if (!list_empty(&ctl->events))
1338 		mask |= POLLIN | POLLRDNORM;
1339 
1340 	return mask;
1341 }
1342 
1343 /*
1344  * register the device-specific control-ioctls.
1345  * called from each device manager like pcm.c, hwdep.c, etc.
1346  */
_snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn,struct list_head * lists)1347 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1348 {
1349 	struct snd_kctl_ioctl *pn;
1350 
1351 	pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
1352 	if (pn == NULL)
1353 		return -ENOMEM;
1354 	pn->fioctl = fcn;
1355 	down_write(&snd_ioctl_rwsem);
1356 	list_add_tail(&pn->list, lists);
1357 	up_write(&snd_ioctl_rwsem);
1358 	return 0;
1359 }
1360 
snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)1361 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1362 {
1363 	return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1364 }
1365 
1366 EXPORT_SYMBOL(snd_ctl_register_ioctl);
1367 
1368 #ifdef CONFIG_COMPAT
snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)1369 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1370 {
1371 	return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1372 }
1373 
1374 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
1375 #endif
1376 
1377 /*
1378  * de-register the device-specific control-ioctls.
1379  */
_snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,struct list_head * lists)1380 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1381 				     struct list_head *lists)
1382 {
1383 	struct snd_kctl_ioctl *p;
1384 
1385 	if (snd_BUG_ON(!fcn))
1386 		return -EINVAL;
1387 	down_write(&snd_ioctl_rwsem);
1388 	list_for_each_entry(p, lists, list) {
1389 		if (p->fioctl == fcn) {
1390 			list_del(&p->list);
1391 			up_write(&snd_ioctl_rwsem);
1392 			kfree(p);
1393 			return 0;
1394 		}
1395 	}
1396 	up_write(&snd_ioctl_rwsem);
1397 	snd_BUG();
1398 	return -EINVAL;
1399 }
1400 
snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)1401 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1402 {
1403 	return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1404 }
1405 
1406 EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1407 
1408 #ifdef CONFIG_COMPAT
snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)1409 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1410 {
1411 	return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1412 }
1413 
1414 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
1415 #endif
1416 
snd_ctl_fasync(int fd,struct file * file,int on)1417 static int snd_ctl_fasync(int fd, struct file * file, int on)
1418 {
1419 	struct snd_ctl_file *ctl;
1420 
1421 	ctl = file->private_data;
1422 	return fasync_helper(fd, file, on, &ctl->fasync);
1423 }
1424 
1425 /*
1426  * ioctl32 compat
1427  */
1428 #ifdef CONFIG_COMPAT
1429 #include "control_compat.c"
1430 #else
1431 #define snd_ctl_ioctl_compat	NULL
1432 #endif
1433 
1434 /*
1435  *  INIT PART
1436  */
1437 
1438 static const struct file_operations snd_ctl_f_ops =
1439 {
1440 	.owner =	THIS_MODULE,
1441 	.read =		snd_ctl_read,
1442 	.open =		snd_ctl_open,
1443 	.release =	snd_ctl_release,
1444 	.llseek =	no_llseek,
1445 	.poll =		snd_ctl_poll,
1446 	.unlocked_ioctl =	snd_ctl_ioctl,
1447 	.compat_ioctl =	snd_ctl_ioctl_compat,
1448 	.fasync =	snd_ctl_fasync,
1449 };
1450 
1451 /*
1452  * registration of the control device
1453  */
snd_ctl_dev_register(struct snd_device * device)1454 static int snd_ctl_dev_register(struct snd_device *device)
1455 {
1456 	struct snd_card *card = device->device_data;
1457 	int err, cardnum;
1458 	char name[16];
1459 
1460 	if (snd_BUG_ON(!card))
1461 		return -ENXIO;
1462 	cardnum = card->number;
1463 	if (snd_BUG_ON(cardnum < 0 || cardnum >= SNDRV_CARDS))
1464 		return -ENXIO;
1465 	sprintf(name, "controlC%i", cardnum);
1466 	if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1467 				       &snd_ctl_f_ops, card, name)) < 0)
1468 		return err;
1469 	return 0;
1470 }
1471 
1472 /*
1473  * disconnection of the control device
1474  */
snd_ctl_dev_disconnect(struct snd_device * device)1475 static int snd_ctl_dev_disconnect(struct snd_device *device)
1476 {
1477 	struct snd_card *card = device->device_data;
1478 	struct snd_ctl_file *ctl;
1479 	int err, cardnum;
1480 
1481 	if (snd_BUG_ON(!card))
1482 		return -ENXIO;
1483 	cardnum = card->number;
1484 	if (snd_BUG_ON(cardnum < 0 || cardnum >= SNDRV_CARDS))
1485 		return -ENXIO;
1486 
1487 	read_lock(&card->ctl_files_rwlock);
1488 	list_for_each_entry(ctl, &card->ctl_files, list) {
1489 		wake_up(&ctl->change_sleep);
1490 		kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1491 	}
1492 	read_unlock(&card->ctl_files_rwlock);
1493 
1494 	if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
1495 					 card, -1)) < 0)
1496 		return err;
1497 	return 0;
1498 }
1499 
1500 /*
1501  * free all controls
1502  */
snd_ctl_dev_free(struct snd_device * device)1503 static int snd_ctl_dev_free(struct snd_device *device)
1504 {
1505 	struct snd_card *card = device->device_data;
1506 	struct snd_kcontrol *control;
1507 
1508 	down_write(&card->controls_rwsem);
1509 	while (!list_empty(&card->controls)) {
1510 		control = snd_kcontrol(card->controls.next);
1511 		snd_ctl_remove(card, control);
1512 	}
1513 	up_write(&card->controls_rwsem);
1514 	return 0;
1515 }
1516 
1517 /*
1518  * create control core:
1519  * called from init.c
1520  */
snd_ctl_create(struct snd_card * card)1521 int snd_ctl_create(struct snd_card *card)
1522 {
1523 	static struct snd_device_ops ops = {
1524 		.dev_free = snd_ctl_dev_free,
1525 		.dev_register =	snd_ctl_dev_register,
1526 		.dev_disconnect = snd_ctl_dev_disconnect,
1527 	};
1528 
1529 	if (snd_BUG_ON(!card))
1530 		return -ENXIO;
1531 	return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
1532 }
1533 
1534 /*
1535  * Frequently used control callbacks/helpers
1536  */
snd_ctl_boolean_mono_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1537 int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
1538 			      struct snd_ctl_elem_info *uinfo)
1539 {
1540 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1541 	uinfo->count = 1;
1542 	uinfo->value.integer.min = 0;
1543 	uinfo->value.integer.max = 1;
1544 	return 0;
1545 }
1546 
1547 EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
1548 
snd_ctl_boolean_stereo_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1549 int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
1550 				struct snd_ctl_elem_info *uinfo)
1551 {
1552 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1553 	uinfo->count = 2;
1554 	uinfo->value.integer.min = 0;
1555 	uinfo->value.integer.max = 1;
1556 	return 0;
1557 }
1558 
1559 EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
1560 
1561 /**
1562  * snd_ctl_enum_info - fills the info structure for an enumerated control
1563  * @info: the structure to be filled
1564  * @channels: the number of the control's channels; often one
1565  * @items: the number of control values; also the size of @names
1566  * @names: an array containing the names of all control values
1567  *
1568  * Sets all required fields in @info to their appropriate values.
1569  * If the control's accessibility is not the default (readable and writable),
1570  * the caller has to fill @info->access.
1571  */
snd_ctl_enum_info(struct snd_ctl_elem_info * info,unsigned int channels,unsigned int items,const char * const names[])1572 int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
1573 		      unsigned int items, const char *const names[])
1574 {
1575 	info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1576 	info->count = channels;
1577 	info->value.enumerated.items = items;
1578 	if (info->value.enumerated.item >= items)
1579 		info->value.enumerated.item = items - 1;
1580 	strlcpy(info->value.enumerated.name,
1581 		names[info->value.enumerated.item],
1582 		sizeof(info->value.enumerated.name));
1583 	return 0;
1584 }
1585 EXPORT_SYMBOL(snd_ctl_enum_info);
1586