• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/module.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
27 #include <linux/time.h>
28 #include <linux/sched/signal.h>
29 #include <sound/core.h>
30 #include <sound/minors.h>
31 #include <sound/info.h>
32 #include <sound/control.h>
33 
34 /* max number of user-defined controls */
35 #define MAX_USER_CONTROLS	32
36 #define MAX_CONTROL_COUNT	1028
37 
38 struct snd_kctl_ioctl {
39 	struct list_head list;		/* list of all ioctls */
40 	snd_kctl_ioctl_func_t fioctl;
41 };
42 
43 static DECLARE_RWSEM(snd_ioctl_rwsem);
44 static LIST_HEAD(snd_control_ioctls);
45 #ifdef CONFIG_COMPAT
46 static LIST_HEAD(snd_control_compat_ioctls);
47 #endif
48 
snd_ctl_open(struct inode * inode,struct file * file)49 static int snd_ctl_open(struct inode *inode, struct file *file)
50 {
51 	unsigned long flags;
52 	struct snd_card *card;
53 	struct snd_ctl_file *ctl;
54 	int i, err;
55 
56 	err = nonseekable_open(inode, file);
57 	if (err < 0)
58 		return err;
59 
60 	card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
61 	if (!card) {
62 		err = -ENODEV;
63 		goto __error1;
64 	}
65 	err = snd_card_file_add(card, file);
66 	if (err < 0) {
67 		err = -ENODEV;
68 		goto __error1;
69 	}
70 	if (!try_module_get(card->module)) {
71 		err = -EFAULT;
72 		goto __error2;
73 	}
74 	ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
75 	if (ctl == NULL) {
76 		err = -ENOMEM;
77 		goto __error;
78 	}
79 	INIT_LIST_HEAD(&ctl->events);
80 	init_waitqueue_head(&ctl->change_sleep);
81 	spin_lock_init(&ctl->read_lock);
82 	ctl->card = card;
83 	for (i = 0; i < SND_CTL_SUBDEV_ITEMS; i++)
84 		ctl->preferred_subdevice[i] = -1;
85 	ctl->pid = get_pid(task_pid(current));
86 	file->private_data = ctl;
87 	write_lock_irqsave(&card->ctl_files_rwlock, flags);
88 	list_add_tail(&ctl->list, &card->ctl_files);
89 	write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
90 	snd_card_unref(card);
91 	return 0;
92 
93       __error:
94 	module_put(card->module);
95       __error2:
96 	snd_card_file_remove(card, file);
97       __error1:
98 	if (card)
99 		snd_card_unref(card);
100       	return err;
101 }
102 
snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)103 static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
104 {
105 	unsigned long flags;
106 	struct snd_kctl_event *cread;
107 
108 	spin_lock_irqsave(&ctl->read_lock, flags);
109 	while (!list_empty(&ctl->events)) {
110 		cread = snd_kctl_event(ctl->events.next);
111 		list_del(&cread->list);
112 		kfree(cread);
113 	}
114 	spin_unlock_irqrestore(&ctl->read_lock, flags);
115 }
116 
snd_ctl_release(struct inode * inode,struct file * file)117 static int snd_ctl_release(struct inode *inode, struct file *file)
118 {
119 	unsigned long flags;
120 	struct snd_card *card;
121 	struct snd_ctl_file *ctl;
122 	struct snd_kcontrol *control;
123 	unsigned int idx;
124 
125 	ctl = file->private_data;
126 	file->private_data = NULL;
127 	card = ctl->card;
128 	write_lock_irqsave(&card->ctl_files_rwlock, flags);
129 	list_del(&ctl->list);
130 	write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
131 	down_write(&card->controls_rwsem);
132 	list_for_each_entry(control, &card->controls, list)
133 		for (idx = 0; idx < control->count; idx++)
134 			if (control->vd[idx].owner == ctl)
135 				control->vd[idx].owner = NULL;
136 	up_write(&card->controls_rwsem);
137 	snd_ctl_empty_read_queue(ctl);
138 	put_pid(ctl->pid);
139 	kfree(ctl);
140 	module_put(card->module);
141 	snd_card_file_remove(card, file);
142 	return 0;
143 }
144 
145 /**
146  * snd_ctl_notify - Send notification to user-space for a control change
147  * @card: the card to send notification
148  * @mask: the event mask, SNDRV_CTL_EVENT_*
149  * @id: the ctl element id to send notification
150  *
151  * This function adds an event record with the given id and mask, appends
152  * to the list and wakes up the user-space for notification.  This can be
153  * called in the atomic context.
154  */
snd_ctl_notify(struct snd_card * card,unsigned int mask,struct snd_ctl_elem_id * id)155 void snd_ctl_notify(struct snd_card *card, unsigned int mask,
156 		    struct snd_ctl_elem_id *id)
157 {
158 	unsigned long flags;
159 	struct snd_ctl_file *ctl;
160 	struct snd_kctl_event *ev;
161 
162 	if (snd_BUG_ON(!card || !id))
163 		return;
164 	if (card->shutdown)
165 		return;
166 	read_lock(&card->ctl_files_rwlock);
167 #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
168 	card->mixer_oss_change_count++;
169 #endif
170 	list_for_each_entry(ctl, &card->ctl_files, list) {
171 		if (!ctl->subscribed)
172 			continue;
173 		spin_lock_irqsave(&ctl->read_lock, flags);
174 		list_for_each_entry(ev, &ctl->events, list) {
175 			if (ev->id.numid == id->numid) {
176 				ev->mask |= mask;
177 				goto _found;
178 			}
179 		}
180 		ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
181 		if (ev) {
182 			ev->id = *id;
183 			ev->mask = mask;
184 			list_add_tail(&ev->list, &ctl->events);
185 		} else {
186 			dev_err(card->dev, "No memory available to allocate event\n");
187 		}
188 	_found:
189 		wake_up(&ctl->change_sleep);
190 		spin_unlock_irqrestore(&ctl->read_lock, flags);
191 		kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
192 	}
193 	read_unlock(&card->ctl_files_rwlock);
194 }
195 EXPORT_SYMBOL(snd_ctl_notify);
196 
197 /**
198  * snd_ctl_new - create a new control instance with some elements
199  * @kctl: the pointer to store new control instance
200  * @count: the number of elements in this control
201  * @access: the default access flags for elements in this control
202  * @file: given when locking these elements
203  *
204  * Allocates a memory object for a new control instance. The instance has
205  * elements as many as the given number (@count). Each element has given
206  * access permissions (@access). Each element is locked when @file is given.
207  *
208  * Return: 0 on success, error code on failure
209  */
snd_ctl_new(struct snd_kcontrol ** kctl,unsigned int count,unsigned int access,struct snd_ctl_file * file)210 static int snd_ctl_new(struct snd_kcontrol **kctl, unsigned int count,
211 		       unsigned int access, struct snd_ctl_file *file)
212 {
213 	unsigned int size;
214 	unsigned int idx;
215 
216 	if (count == 0 || count > MAX_CONTROL_COUNT)
217 		return -EINVAL;
218 
219 	size  = sizeof(struct snd_kcontrol);
220 	size += sizeof(struct snd_kcontrol_volatile) * count;
221 
222 	*kctl = kzalloc(size, GFP_KERNEL);
223 	if (!*kctl)
224 		return -ENOMEM;
225 
226 	for (idx = 0; idx < count; idx++) {
227 		(*kctl)->vd[idx].access = access;
228 		(*kctl)->vd[idx].owner = file;
229 	}
230 	(*kctl)->count = count;
231 
232 	return 0;
233 }
234 
235 /**
236  * snd_ctl_new1 - create a control instance from the template
237  * @ncontrol: the initialization record
238  * @private_data: the private data to set
239  *
240  * Allocates a new struct snd_kcontrol instance and initialize from the given
241  * template.  When the access field of ncontrol is 0, it's assumed as
242  * READWRITE access. When the count field is 0, it's assumes as one.
243  *
244  * Return: The pointer of the newly generated instance, or %NULL on failure.
245  */
snd_ctl_new1(const struct snd_kcontrol_new * ncontrol,void * private_data)246 struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
247 				  void *private_data)
248 {
249 	struct snd_kcontrol *kctl;
250 	unsigned int count;
251 	unsigned int access;
252 	int err;
253 
254 	if (snd_BUG_ON(!ncontrol || !ncontrol->info))
255 		return NULL;
256 
257 	count = ncontrol->count;
258 	if (count == 0)
259 		count = 1;
260 
261 	access = ncontrol->access;
262 	if (access == 0)
263 		access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
264 	access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
265 		   SNDRV_CTL_ELEM_ACCESS_VOLATILE |
266 		   SNDRV_CTL_ELEM_ACCESS_INACTIVE |
267 		   SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE |
268 		   SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND |
269 		   SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
270 
271 	err = snd_ctl_new(&kctl, count, access, NULL);
272 	if (err < 0)
273 		return NULL;
274 
275 	/* The 'numid' member is decided when calling snd_ctl_add(). */
276 	kctl->id.iface = ncontrol->iface;
277 	kctl->id.device = ncontrol->device;
278 	kctl->id.subdevice = ncontrol->subdevice;
279 	if (ncontrol->name) {
280 		strlcpy(kctl->id.name, ncontrol->name, sizeof(kctl->id.name));
281 		if (strcmp(ncontrol->name, kctl->id.name) != 0)
282 			pr_warn("ALSA: Control name '%s' truncated to '%s'\n",
283 				ncontrol->name, kctl->id.name);
284 	}
285 	kctl->id.index = ncontrol->index;
286 
287 	kctl->info = ncontrol->info;
288 	kctl->get = ncontrol->get;
289 	kctl->put = ncontrol->put;
290 	kctl->tlv.p = ncontrol->tlv.p;
291 
292 	kctl->private_value = ncontrol->private_value;
293 	kctl->private_data = private_data;
294 
295 	return kctl;
296 }
297 EXPORT_SYMBOL(snd_ctl_new1);
298 
299 /**
300  * snd_ctl_free_one - release the control instance
301  * @kcontrol: the control instance
302  *
303  * Releases the control instance created via snd_ctl_new()
304  * or snd_ctl_new1().
305  * Don't call this after the control was added to the card.
306  */
snd_ctl_free_one(struct snd_kcontrol * kcontrol)307 void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
308 {
309 	if (kcontrol) {
310 		if (kcontrol->private_free)
311 			kcontrol->private_free(kcontrol);
312 		kfree(kcontrol);
313 	}
314 }
315 EXPORT_SYMBOL(snd_ctl_free_one);
316 
snd_ctl_remove_numid_conflict(struct snd_card * card,unsigned int count)317 static bool snd_ctl_remove_numid_conflict(struct snd_card *card,
318 					  unsigned int count)
319 {
320 	struct snd_kcontrol *kctl;
321 
322 	/* Make sure that the ids assigned to the control do not wrap around */
323 	if (card->last_numid >= UINT_MAX - count)
324 		card->last_numid = 0;
325 
326 	list_for_each_entry(kctl, &card->controls, list) {
327 		if (kctl->id.numid < card->last_numid + 1 + count &&
328 		    kctl->id.numid + kctl->count > card->last_numid + 1) {
329 		    	card->last_numid = kctl->id.numid + kctl->count - 1;
330 			return true;
331 		}
332 	}
333 	return false;
334 }
335 
snd_ctl_find_hole(struct snd_card * card,unsigned int count)336 static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
337 {
338 	unsigned int iter = 100000;
339 
340 	while (snd_ctl_remove_numid_conflict(card, count)) {
341 		if (--iter == 0) {
342 			/* this situation is very unlikely */
343 			dev_err(card->dev, "unable to allocate new control numid\n");
344 			return -ENOMEM;
345 		}
346 	}
347 	return 0;
348 }
349 
350 /* add a new kcontrol object; call with card->controls_rwsem locked */
__snd_ctl_add(struct snd_card * card,struct snd_kcontrol * kcontrol)351 static int __snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
352 {
353 	struct snd_ctl_elem_id id;
354 	unsigned int idx;
355 	unsigned int count;
356 
357 	id = kcontrol->id;
358 	if (id.index > UINT_MAX - kcontrol->count)
359 		return -EINVAL;
360 
361 	if (snd_ctl_find_id(card, &id)) {
362 		dev_err(card->dev,
363 			"control %i:%i:%i:%s:%i is already present\n",
364 			id.iface, id.device, id.subdevice, id.name, id.index);
365 		return -EBUSY;
366 	}
367 
368 	if (snd_ctl_find_hole(card, kcontrol->count) < 0)
369 		return -ENOMEM;
370 
371 	list_add_tail(&kcontrol->list, &card->controls);
372 	card->controls_count += kcontrol->count;
373 	kcontrol->id.numid = card->last_numid + 1;
374 	card->last_numid += kcontrol->count;
375 
376 	id = kcontrol->id;
377 	count = kcontrol->count;
378 	for (idx = 0; idx < count; idx++, id.index++, id.numid++)
379 		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
380 
381 	return 0;
382 }
383 
384 /**
385  * snd_ctl_add - add the control instance to the card
386  * @card: the card instance
387  * @kcontrol: the control instance to add
388  *
389  * Adds the control instance created via snd_ctl_new() or
390  * snd_ctl_new1() to the given card. Assigns also an unique
391  * numid used for fast search.
392  *
393  * It frees automatically the control which cannot be added.
394  *
395  * Return: Zero if successful, or a negative error code on failure.
396  *
397  */
snd_ctl_add(struct snd_card * card,struct snd_kcontrol * kcontrol)398 int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
399 {
400 	int err = -EINVAL;
401 
402 	if (! kcontrol)
403 		return err;
404 	if (snd_BUG_ON(!card || !kcontrol->info))
405 		goto error;
406 
407 	down_write(&card->controls_rwsem);
408 	err = __snd_ctl_add(card, kcontrol);
409 	up_write(&card->controls_rwsem);
410 	if (err < 0)
411 		goto error;
412 	return 0;
413 
414  error:
415 	snd_ctl_free_one(kcontrol);
416 	return err;
417 }
418 EXPORT_SYMBOL(snd_ctl_add);
419 
420 /**
421  * snd_ctl_replace - replace the control instance of the card
422  * @card: the card instance
423  * @kcontrol: the control instance to replace
424  * @add_on_replace: add the control if not already added
425  *
426  * Replaces the given control.  If the given control does not exist
427  * and the add_on_replace flag is set, the control is added.  If the
428  * control exists, it is destroyed first.
429  *
430  * It frees automatically the control which cannot be added or replaced.
431  *
432  * Return: Zero if successful, or a negative error code on failure.
433  */
snd_ctl_replace(struct snd_card * card,struct snd_kcontrol * kcontrol,bool add_on_replace)434 int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol,
435 		    bool add_on_replace)
436 {
437 	struct snd_ctl_elem_id id;
438 	unsigned int count;
439 	unsigned int idx;
440 	struct snd_kcontrol *old;
441 	int ret;
442 
443 	if (!kcontrol)
444 		return -EINVAL;
445 	if (snd_BUG_ON(!card || !kcontrol->info)) {
446 		ret = -EINVAL;
447 		goto error;
448 	}
449 	id = kcontrol->id;
450 	down_write(&card->controls_rwsem);
451 	old = snd_ctl_find_id(card, &id);
452 	if (!old) {
453 		if (add_on_replace)
454 			goto add;
455 		up_write(&card->controls_rwsem);
456 		ret = -EINVAL;
457 		goto error;
458 	}
459 	ret = snd_ctl_remove(card, old);
460 	if (ret < 0) {
461 		up_write(&card->controls_rwsem);
462 		goto error;
463 	}
464 add:
465 	if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
466 		up_write(&card->controls_rwsem);
467 		ret = -ENOMEM;
468 		goto error;
469 	}
470 	list_add_tail(&kcontrol->list, &card->controls);
471 	card->controls_count += kcontrol->count;
472 	kcontrol->id.numid = card->last_numid + 1;
473 	card->last_numid += kcontrol->count;
474 	id = kcontrol->id;
475 	count = kcontrol->count;
476 	up_write(&card->controls_rwsem);
477 	for (idx = 0; idx < count; idx++, id.index++, id.numid++)
478 		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
479 	return 0;
480 
481 error:
482 	snd_ctl_free_one(kcontrol);
483 	return ret;
484 }
485 EXPORT_SYMBOL(snd_ctl_replace);
486 
487 /**
488  * snd_ctl_remove - remove the control from the card and release it
489  * @card: the card instance
490  * @kcontrol: the control instance to remove
491  *
492  * Removes the control from the card and then releases the instance.
493  * You don't need to call snd_ctl_free_one(). You must be in
494  * the write lock - down_write(&card->controls_rwsem).
495  *
496  * Return: 0 if successful, or a negative error code on failure.
497  */
snd_ctl_remove(struct snd_card * card,struct snd_kcontrol * kcontrol)498 int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
499 {
500 	struct snd_ctl_elem_id id;
501 	unsigned int idx;
502 
503 	if (snd_BUG_ON(!card || !kcontrol))
504 		return -EINVAL;
505 	list_del(&kcontrol->list);
506 	card->controls_count -= kcontrol->count;
507 	id = kcontrol->id;
508 	for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
509 		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
510 	snd_ctl_free_one(kcontrol);
511 	return 0;
512 }
513 EXPORT_SYMBOL(snd_ctl_remove);
514 
515 /**
516  * snd_ctl_remove_id - remove the control of the given id and release it
517  * @card: the card instance
518  * @id: the control id to remove
519  *
520  * Finds the control instance with the given id, removes it from the
521  * card list and releases it.
522  *
523  * Return: 0 if successful, or a negative error code on failure.
524  */
snd_ctl_remove_id(struct snd_card * card,struct snd_ctl_elem_id * id)525 int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
526 {
527 	struct snd_kcontrol *kctl;
528 	int ret;
529 
530 	down_write(&card->controls_rwsem);
531 	kctl = snd_ctl_find_id(card, id);
532 	if (kctl == NULL) {
533 		up_write(&card->controls_rwsem);
534 		return -ENOENT;
535 	}
536 	ret = snd_ctl_remove(card, kctl);
537 	up_write(&card->controls_rwsem);
538 	return ret;
539 }
540 EXPORT_SYMBOL(snd_ctl_remove_id);
541 
542 /**
543  * snd_ctl_remove_user_ctl - remove and release the unlocked user control
544  * @file: active control handle
545  * @id: the control id to remove
546  *
547  * Finds the control instance with the given id, removes it from the
548  * card list and releases it.
549  *
550  * Return: 0 if successful, or a negative error code on failure.
551  */
snd_ctl_remove_user_ctl(struct snd_ctl_file * file,struct snd_ctl_elem_id * id)552 static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
553 				   struct snd_ctl_elem_id *id)
554 {
555 	struct snd_card *card = file->card;
556 	struct snd_kcontrol *kctl;
557 	int idx, ret;
558 
559 	down_write(&card->controls_rwsem);
560 	kctl = snd_ctl_find_id(card, id);
561 	if (kctl == NULL) {
562 		ret = -ENOENT;
563 		goto error;
564 	}
565 	if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
566 		ret = -EINVAL;
567 		goto error;
568 	}
569 	for (idx = 0; idx < kctl->count; idx++)
570 		if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
571 			ret = -EBUSY;
572 			goto error;
573 		}
574 	ret = snd_ctl_remove(card, kctl);
575 	if (ret < 0)
576 		goto error;
577 	card->user_ctl_count--;
578 error:
579 	up_write(&card->controls_rwsem);
580 	return ret;
581 }
582 
583 /**
584  * snd_ctl_activate_id - activate/inactivate the control of the given id
585  * @card: the card instance
586  * @id: the control id to activate/inactivate
587  * @active: non-zero to activate
588  *
589  * Finds the control instance with the given id, and activate or
590  * inactivate the control together with notification, if changed.
591  * The given ID data is filled with full information.
592  *
593  * Return: 0 if unchanged, 1 if changed, or a negative error code on failure.
594  */
snd_ctl_activate_id(struct snd_card * card,struct snd_ctl_elem_id * id,int active)595 int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
596 			int active)
597 {
598 	struct snd_kcontrol *kctl;
599 	struct snd_kcontrol_volatile *vd;
600 	unsigned int index_offset;
601 	int ret;
602 
603 	down_write(&card->controls_rwsem);
604 	kctl = snd_ctl_find_id(card, id);
605 	if (kctl == NULL) {
606 		ret = -ENOENT;
607 		goto unlock;
608 	}
609 	index_offset = snd_ctl_get_ioff(kctl, id);
610 	vd = &kctl->vd[index_offset];
611 	ret = 0;
612 	if (active) {
613 		if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
614 			goto unlock;
615 		vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
616 	} else {
617 		if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)
618 			goto unlock;
619 		vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
620 	}
621 	snd_ctl_build_ioff(id, kctl, index_offset);
622 	ret = 1;
623  unlock:
624 	up_write(&card->controls_rwsem);
625 	if (ret > 0)
626 		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, id);
627 	return ret;
628 }
629 EXPORT_SYMBOL_GPL(snd_ctl_activate_id);
630 
631 /**
632  * snd_ctl_rename_id - replace the id of a control on the card
633  * @card: the card instance
634  * @src_id: the old id
635  * @dst_id: the new id
636  *
637  * Finds the control with the old id from the card, and replaces the
638  * id with the new one.
639  *
640  * Return: Zero if successful, or a negative error code on failure.
641  */
snd_ctl_rename_id(struct snd_card * card,struct snd_ctl_elem_id * src_id,struct snd_ctl_elem_id * dst_id)642 int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
643 		      struct snd_ctl_elem_id *dst_id)
644 {
645 	struct snd_kcontrol *kctl;
646 
647 	down_write(&card->controls_rwsem);
648 	kctl = snd_ctl_find_id(card, src_id);
649 	if (kctl == NULL) {
650 		up_write(&card->controls_rwsem);
651 		return -ENOENT;
652 	}
653 	kctl->id = *dst_id;
654 	kctl->id.numid = card->last_numid + 1;
655 	card->last_numid += kctl->count;
656 	up_write(&card->controls_rwsem);
657 	return 0;
658 }
659 EXPORT_SYMBOL(snd_ctl_rename_id);
660 
661 /**
662  * snd_ctl_find_numid - find the control instance with the given number-id
663  * @card: the card instance
664  * @numid: the number-id to search
665  *
666  * Finds the control instance with the given number-id from the card.
667  *
668  * The caller must down card->controls_rwsem before calling this function
669  * (if the race condition can happen).
670  *
671  * Return: The pointer of the instance if found, or %NULL if not.
672  *
673  */
snd_ctl_find_numid(struct snd_card * card,unsigned int numid)674 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
675 {
676 	struct snd_kcontrol *kctl;
677 
678 	if (snd_BUG_ON(!card || !numid))
679 		return NULL;
680 	list_for_each_entry(kctl, &card->controls, list) {
681 		if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
682 			return kctl;
683 	}
684 	return NULL;
685 }
686 EXPORT_SYMBOL(snd_ctl_find_numid);
687 
688 /**
689  * snd_ctl_find_id - find the control instance with the given id
690  * @card: the card instance
691  * @id: the id to search
692  *
693  * Finds the control instance with the given id from the card.
694  *
695  * The caller must down card->controls_rwsem before calling this function
696  * (if the race condition can happen).
697  *
698  * Return: The pointer of the instance if found, or %NULL if not.
699  *
700  */
snd_ctl_find_id(struct snd_card * card,struct snd_ctl_elem_id * id)701 struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
702 				     struct snd_ctl_elem_id *id)
703 {
704 	struct snd_kcontrol *kctl;
705 
706 	if (snd_BUG_ON(!card || !id))
707 		return NULL;
708 	if (id->numid != 0)
709 		return snd_ctl_find_numid(card, id->numid);
710 	list_for_each_entry(kctl, &card->controls, list) {
711 		if (kctl->id.iface != id->iface)
712 			continue;
713 		if (kctl->id.device != id->device)
714 			continue;
715 		if (kctl->id.subdevice != id->subdevice)
716 			continue;
717 		if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
718 			continue;
719 		if (kctl->id.index > id->index)
720 			continue;
721 		if (kctl->id.index + kctl->count <= id->index)
722 			continue;
723 		return kctl;
724 	}
725 	return NULL;
726 }
727 EXPORT_SYMBOL(snd_ctl_find_id);
728 
snd_ctl_card_info(struct snd_card * card,struct snd_ctl_file * ctl,unsigned int cmd,void __user * arg)729 static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
730 			     unsigned int cmd, void __user *arg)
731 {
732 	struct snd_ctl_card_info *info;
733 
734 	info = kzalloc(sizeof(*info), GFP_KERNEL);
735 	if (! info)
736 		return -ENOMEM;
737 	down_read(&snd_ioctl_rwsem);
738 	info->card = card->number;
739 	strlcpy(info->id, card->id, sizeof(info->id));
740 	strlcpy(info->driver, card->driver, sizeof(info->driver));
741 	strlcpy(info->name, card->shortname, sizeof(info->name));
742 	strlcpy(info->longname, card->longname, sizeof(info->longname));
743 	strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
744 	strlcpy(info->components, card->components, sizeof(info->components));
745 	up_read(&snd_ioctl_rwsem);
746 	if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
747 		kfree(info);
748 		return -EFAULT;
749 	}
750 	kfree(info);
751 	return 0;
752 }
753 
snd_ctl_elem_list(struct snd_card * card,struct snd_ctl_elem_list __user * _list)754 static int snd_ctl_elem_list(struct snd_card *card,
755 			     struct snd_ctl_elem_list __user *_list)
756 {
757 	struct snd_ctl_elem_list list;
758 	struct snd_kcontrol *kctl;
759 	struct snd_ctl_elem_id id;
760 	unsigned int offset, space, jidx;
761 	int err = 0;
762 
763 	if (copy_from_user(&list, _list, sizeof(list)))
764 		return -EFAULT;
765 	offset = list.offset;
766 	space = list.space;
767 
768 	down_read(&card->controls_rwsem);
769 	list.count = card->controls_count;
770 	list.used = 0;
771 	if (space > 0) {
772 		list_for_each_entry(kctl, &card->controls, list) {
773 			if (offset >= kctl->count) {
774 				offset -= kctl->count;
775 				continue;
776 			}
777 			for (jidx = offset; jidx < kctl->count; jidx++) {
778 				snd_ctl_build_ioff(&id, kctl, jidx);
779 				if (copy_to_user(list.pids + list.used, &id,
780 						 sizeof(id))) {
781 					err = -EFAULT;
782 					goto out;
783 				}
784 				list.used++;
785 				if (!--space)
786 					goto out;
787 			}
788 			offset = 0;
789 		}
790 	}
791  out:
792 	up_read(&card->controls_rwsem);
793 	if (!err && copy_to_user(_list, &list, sizeof(list)))
794 		err = -EFAULT;
795 	return err;
796 }
797 
validate_element_member_dimension(struct snd_ctl_elem_info * info)798 static bool validate_element_member_dimension(struct snd_ctl_elem_info *info)
799 {
800 	unsigned int members;
801 	unsigned int i;
802 
803 	if (info->dimen.d[0] == 0)
804 		return true;
805 
806 	members = 1;
807 	for (i = 0; i < ARRAY_SIZE(info->dimen.d); ++i) {
808 		if (info->dimen.d[i] == 0)
809 			break;
810 		members *= info->dimen.d[i];
811 
812 		/*
813 		 * info->count should be validated in advance, to guarantee
814 		 * calculation soundness.
815 		 */
816 		if (members > info->count)
817 			return false;
818 	}
819 
820 	for (++i; i < ARRAY_SIZE(info->dimen.d); ++i) {
821 		if (info->dimen.d[i] > 0)
822 			return false;
823 	}
824 
825 	return members == info->count;
826 }
827 
snd_ctl_elem_info(struct snd_ctl_file * ctl,struct snd_ctl_elem_info * info)828 static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
829 			     struct snd_ctl_elem_info *info)
830 {
831 	struct snd_card *card = ctl->card;
832 	struct snd_kcontrol *kctl;
833 	struct snd_kcontrol_volatile *vd;
834 	unsigned int index_offset;
835 	int result;
836 
837 	down_read(&card->controls_rwsem);
838 	kctl = snd_ctl_find_id(card, &info->id);
839 	if (kctl == NULL) {
840 		up_read(&card->controls_rwsem);
841 		return -ENOENT;
842 	}
843 #ifdef CONFIG_SND_DEBUG
844 	info->access = 0;
845 #endif
846 	result = kctl->info(kctl, info);
847 	if (result >= 0) {
848 		snd_BUG_ON(info->access);
849 		index_offset = snd_ctl_get_ioff(kctl, &info->id);
850 		vd = &kctl->vd[index_offset];
851 		snd_ctl_build_ioff(&info->id, kctl, index_offset);
852 		info->access = vd->access;
853 		if (vd->owner) {
854 			info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
855 			if (vd->owner == ctl)
856 				info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
857 			info->owner = pid_vnr(vd->owner->pid);
858 		} else {
859 			info->owner = -1;
860 		}
861 	}
862 	up_read(&card->controls_rwsem);
863 	return result;
864 }
865 
snd_ctl_elem_info_user(struct snd_ctl_file * ctl,struct snd_ctl_elem_info __user * _info)866 static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
867 				  struct snd_ctl_elem_info __user *_info)
868 {
869 	struct snd_ctl_elem_info info;
870 	int result;
871 
872 	if (copy_from_user(&info, _info, sizeof(info)))
873 		return -EFAULT;
874 	result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
875 	if (result < 0)
876 		return result;
877 	result = snd_ctl_elem_info(ctl, &info);
878 	if (result < 0)
879 		return result;
880 	if (copy_to_user(_info, &info, sizeof(info)))
881 		return -EFAULT;
882 	return result;
883 }
884 
snd_ctl_elem_read(struct snd_card * card,struct snd_ctl_elem_value * control)885 static int snd_ctl_elem_read(struct snd_card *card,
886 			     struct snd_ctl_elem_value *control)
887 {
888 	struct snd_kcontrol *kctl;
889 	struct snd_kcontrol_volatile *vd;
890 	unsigned int index_offset;
891 
892 	kctl = snd_ctl_find_id(card, &control->id);
893 	if (kctl == NULL)
894 		return -ENOENT;
895 
896 	index_offset = snd_ctl_get_ioff(kctl, &control->id);
897 	vd = &kctl->vd[index_offset];
898 	if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_READ) || kctl->get == NULL)
899 		return -EPERM;
900 
901 	snd_ctl_build_ioff(&control->id, kctl, index_offset);
902 	return kctl->get(kctl, control);
903 }
904 
snd_ctl_elem_read_user(struct snd_card * card,struct snd_ctl_elem_value __user * _control)905 static int snd_ctl_elem_read_user(struct snd_card *card,
906 				  struct snd_ctl_elem_value __user *_control)
907 {
908 	struct snd_ctl_elem_value *control;
909 	int result;
910 
911 	control = memdup_user(_control, sizeof(*control));
912 	if (IS_ERR(control))
913 		return PTR_ERR(control);
914 
915 	result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
916 	if (result < 0)
917 		goto error;
918 
919 	down_read(&card->controls_rwsem);
920 	result = snd_ctl_elem_read(card, control);
921 	up_read(&card->controls_rwsem);
922 	if (result < 0)
923 		goto error;
924 
925 	if (copy_to_user(_control, control, sizeof(*control)))
926 		result = -EFAULT;
927  error:
928 	kfree(control);
929 	return result;
930 }
931 
snd_ctl_elem_write(struct snd_card * card,struct snd_ctl_file * file,struct snd_ctl_elem_value * control)932 static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
933 			      struct snd_ctl_elem_value *control)
934 {
935 	struct snd_kcontrol *kctl;
936 	struct snd_kcontrol_volatile *vd;
937 	unsigned int index_offset;
938 	int result;
939 
940 	kctl = snd_ctl_find_id(card, &control->id);
941 	if (kctl == NULL)
942 		return -ENOENT;
943 
944 	index_offset = snd_ctl_get_ioff(kctl, &control->id);
945 	vd = &kctl->vd[index_offset];
946 	if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) || kctl->put == NULL ||
947 	    (file && vd->owner && vd->owner != file)) {
948 		return -EPERM;
949 	}
950 
951 	snd_ctl_build_ioff(&control->id, kctl, index_offset);
952 	result = kctl->put(kctl, control);
953 	if (result < 0)
954 		return result;
955 
956 	if (result > 0) {
957 		struct snd_ctl_elem_id id = control->id;
958 		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &id);
959 	}
960 
961 	return 0;
962 }
963 
snd_ctl_elem_write_user(struct snd_ctl_file * file,struct snd_ctl_elem_value __user * _control)964 static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
965 				   struct snd_ctl_elem_value __user *_control)
966 {
967 	struct snd_ctl_elem_value *control;
968 	struct snd_card *card;
969 	int result;
970 
971 	control = memdup_user(_control, sizeof(*control));
972 	if (IS_ERR(control))
973 		return PTR_ERR(control);
974 
975 	card = file->card;
976 	result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
977 	if (result < 0)
978 		goto error;
979 
980 	down_write(&card->controls_rwsem);
981 	result = snd_ctl_elem_write(card, file, control);
982 	up_write(&card->controls_rwsem);
983 	if (result < 0)
984 		goto error;
985 
986 	if (copy_to_user(_control, control, sizeof(*control)))
987 		result = -EFAULT;
988  error:
989 	kfree(control);
990 	return result;
991 }
992 
snd_ctl_elem_lock(struct snd_ctl_file * file,struct snd_ctl_elem_id __user * _id)993 static int snd_ctl_elem_lock(struct snd_ctl_file *file,
994 			     struct snd_ctl_elem_id __user *_id)
995 {
996 	struct snd_card *card = file->card;
997 	struct snd_ctl_elem_id id;
998 	struct snd_kcontrol *kctl;
999 	struct snd_kcontrol_volatile *vd;
1000 	int result;
1001 
1002 	if (copy_from_user(&id, _id, sizeof(id)))
1003 		return -EFAULT;
1004 	down_write(&card->controls_rwsem);
1005 	kctl = snd_ctl_find_id(card, &id);
1006 	if (kctl == NULL) {
1007 		result = -ENOENT;
1008 	} else {
1009 		vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1010 		if (vd->owner != NULL)
1011 			result = -EBUSY;
1012 		else {
1013 			vd->owner = file;
1014 			result = 0;
1015 		}
1016 	}
1017 	up_write(&card->controls_rwsem);
1018 	return result;
1019 }
1020 
snd_ctl_elem_unlock(struct snd_ctl_file * file,struct snd_ctl_elem_id __user * _id)1021 static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
1022 			       struct snd_ctl_elem_id __user *_id)
1023 {
1024 	struct snd_card *card = file->card;
1025 	struct snd_ctl_elem_id id;
1026 	struct snd_kcontrol *kctl;
1027 	struct snd_kcontrol_volatile *vd;
1028 	int result;
1029 
1030 	if (copy_from_user(&id, _id, sizeof(id)))
1031 		return -EFAULT;
1032 	down_write(&card->controls_rwsem);
1033 	kctl = snd_ctl_find_id(card, &id);
1034 	if (kctl == NULL) {
1035 		result = -ENOENT;
1036 	} else {
1037 		vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1038 		if (vd->owner == NULL)
1039 			result = -EINVAL;
1040 		else if (vd->owner != file)
1041 			result = -EPERM;
1042 		else {
1043 			vd->owner = NULL;
1044 			result = 0;
1045 		}
1046 	}
1047 	up_write(&card->controls_rwsem);
1048 	return result;
1049 }
1050 
1051 struct user_element {
1052 	struct snd_ctl_elem_info info;
1053 	struct snd_card *card;
1054 	char *elem_data;		/* element data */
1055 	unsigned long elem_data_size;	/* size of element data in bytes */
1056 	void *tlv_data;			/* TLV data */
1057 	unsigned long tlv_data_size;	/* TLV data size */
1058 	void *priv_data;		/* private data (like strings for enumerated type) */
1059 };
1060 
snd_ctl_elem_user_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1061 static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
1062 				  struct snd_ctl_elem_info *uinfo)
1063 {
1064 	struct user_element *ue = kcontrol->private_data;
1065 	unsigned int offset;
1066 
1067 	offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
1068 	*uinfo = ue->info;
1069 	snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
1070 
1071 	return 0;
1072 }
1073 
snd_ctl_elem_user_enum_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1074 static int snd_ctl_elem_user_enum_info(struct snd_kcontrol *kcontrol,
1075 				       struct snd_ctl_elem_info *uinfo)
1076 {
1077 	struct user_element *ue = kcontrol->private_data;
1078 	const char *names;
1079 	unsigned int item;
1080 	unsigned int offset;
1081 
1082 	item = uinfo->value.enumerated.item;
1083 
1084 	offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
1085 	*uinfo = ue->info;
1086 	snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
1087 
1088 	item = min(item, uinfo->value.enumerated.items - 1);
1089 	uinfo->value.enumerated.item = item;
1090 
1091 	names = ue->priv_data;
1092 	for (; item > 0; --item)
1093 		names += strlen(names) + 1;
1094 	strcpy(uinfo->value.enumerated.name, names);
1095 
1096 	return 0;
1097 }
1098 
snd_ctl_elem_user_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1099 static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
1100 				 struct snd_ctl_elem_value *ucontrol)
1101 {
1102 	struct user_element *ue = kcontrol->private_data;
1103 	unsigned int size = ue->elem_data_size;
1104 	char *src = ue->elem_data +
1105 			snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
1106 
1107 	memcpy(&ucontrol->value, src, size);
1108 	return 0;
1109 }
1110 
snd_ctl_elem_user_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1111 static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
1112 				 struct snd_ctl_elem_value *ucontrol)
1113 {
1114 	int change;
1115 	struct user_element *ue = kcontrol->private_data;
1116 	unsigned int size = ue->elem_data_size;
1117 	char *dst = ue->elem_data +
1118 			snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
1119 
1120 	change = memcmp(&ucontrol->value, dst, size) != 0;
1121 	if (change)
1122 		memcpy(dst, &ucontrol->value, size);
1123 	return change;
1124 }
1125 
replace_user_tlv(struct snd_kcontrol * kctl,unsigned int __user * buf,unsigned int size)1126 static int replace_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
1127 			    unsigned int size)
1128 {
1129 	struct user_element *ue = kctl->private_data;
1130 	unsigned int *container;
1131 	struct snd_ctl_elem_id id;
1132 	unsigned int mask = 0;
1133 	int i;
1134 	int change;
1135 
1136 	if (size > 1024 * 128)	/* sane value */
1137 		return -EINVAL;
1138 
1139 	container = memdup_user(buf, size);
1140 	if (IS_ERR(container))
1141 		return PTR_ERR(container);
1142 
1143 	change = ue->tlv_data_size != size;
1144 	if (!change)
1145 		change = memcmp(ue->tlv_data, container, size) != 0;
1146 	if (!change) {
1147 		kfree(container);
1148 		return 0;
1149 	}
1150 
1151 	if (ue->tlv_data == NULL) {
1152 		/* Now TLV data is available. */
1153 		for (i = 0; i < kctl->count; ++i)
1154 			kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1155 		mask = SNDRV_CTL_EVENT_MASK_INFO;
1156 	}
1157 
1158 	kfree(ue->tlv_data);
1159 	ue->tlv_data = container;
1160 	ue->tlv_data_size = size;
1161 
1162 	mask |= SNDRV_CTL_EVENT_MASK_TLV;
1163 	for (i = 0; i < kctl->count; ++i) {
1164 		snd_ctl_build_ioff(&id, kctl, i);
1165 		snd_ctl_notify(ue->card, mask, &id);
1166 	}
1167 
1168 	return change;
1169 }
1170 
read_user_tlv(struct snd_kcontrol * kctl,unsigned int __user * buf,unsigned int size)1171 static int read_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
1172 			 unsigned int size)
1173 {
1174 	struct user_element *ue = kctl->private_data;
1175 
1176 	if (ue->tlv_data_size == 0 || ue->tlv_data == NULL)
1177 		return -ENXIO;
1178 
1179 	if (size < ue->tlv_data_size)
1180 		return -ENOSPC;
1181 
1182 	if (copy_to_user(buf, ue->tlv_data, ue->tlv_data_size))
1183 		return -EFAULT;
1184 
1185 	return 0;
1186 }
1187 
snd_ctl_elem_user_tlv(struct snd_kcontrol * kctl,int op_flag,unsigned int size,unsigned int __user * buf)1188 static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kctl, int op_flag,
1189 				 unsigned int size, unsigned int __user *buf)
1190 {
1191 	if (op_flag == SNDRV_CTL_TLV_OP_WRITE)
1192 		return replace_user_tlv(kctl, buf, size);
1193 	else
1194 		return read_user_tlv(kctl, buf, size);
1195 }
1196 
snd_ctl_elem_init_enum_names(struct user_element * ue)1197 static int snd_ctl_elem_init_enum_names(struct user_element *ue)
1198 {
1199 	char *names, *p;
1200 	size_t buf_len, name_len;
1201 	unsigned int i;
1202 	const uintptr_t user_ptrval = ue->info.value.enumerated.names_ptr;
1203 
1204 	if (ue->info.value.enumerated.names_length > 64 * 1024)
1205 		return -EINVAL;
1206 
1207 	names = memdup_user((const void __user *)user_ptrval,
1208 		ue->info.value.enumerated.names_length);
1209 	if (IS_ERR(names))
1210 		return PTR_ERR(names);
1211 
1212 	/* check that there are enough valid names */
1213 	buf_len = ue->info.value.enumerated.names_length;
1214 	p = names;
1215 	for (i = 0; i < ue->info.value.enumerated.items; ++i) {
1216 		name_len = strnlen(p, buf_len);
1217 		if (name_len == 0 || name_len >= 64 || name_len == buf_len) {
1218 			kfree(names);
1219 			return -EINVAL;
1220 		}
1221 		p += name_len + 1;
1222 		buf_len -= name_len + 1;
1223 	}
1224 
1225 	ue->priv_data = names;
1226 	ue->info.value.enumerated.names_ptr = 0;
1227 
1228 	return 0;
1229 }
1230 
snd_ctl_elem_user_free(struct snd_kcontrol * kcontrol)1231 static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
1232 {
1233 	struct user_element *ue = kcontrol->private_data;
1234 
1235 	kfree(ue->tlv_data);
1236 	kfree(ue->priv_data);
1237 	kfree(ue);
1238 }
1239 
snd_ctl_elem_add(struct snd_ctl_file * file,struct snd_ctl_elem_info * info,int replace)1240 static int snd_ctl_elem_add(struct snd_ctl_file *file,
1241 			    struct snd_ctl_elem_info *info, int replace)
1242 {
1243 	/* The capacity of struct snd_ctl_elem_value.value.*/
1244 	static const unsigned int value_sizes[] = {
1245 		[SNDRV_CTL_ELEM_TYPE_BOOLEAN]	= sizeof(long),
1246 		[SNDRV_CTL_ELEM_TYPE_INTEGER]	= sizeof(long),
1247 		[SNDRV_CTL_ELEM_TYPE_ENUMERATED] = sizeof(unsigned int),
1248 		[SNDRV_CTL_ELEM_TYPE_BYTES]	= sizeof(unsigned char),
1249 		[SNDRV_CTL_ELEM_TYPE_IEC958]	= sizeof(struct snd_aes_iec958),
1250 		[SNDRV_CTL_ELEM_TYPE_INTEGER64] = sizeof(long long),
1251 	};
1252 	static const unsigned int max_value_counts[] = {
1253 		[SNDRV_CTL_ELEM_TYPE_BOOLEAN]	= 128,
1254 		[SNDRV_CTL_ELEM_TYPE_INTEGER]	= 128,
1255 		[SNDRV_CTL_ELEM_TYPE_ENUMERATED] = 128,
1256 		[SNDRV_CTL_ELEM_TYPE_BYTES]	= 512,
1257 		[SNDRV_CTL_ELEM_TYPE_IEC958]	= 1,
1258 		[SNDRV_CTL_ELEM_TYPE_INTEGER64] = 64,
1259 	};
1260 	struct snd_card *card = file->card;
1261 	struct snd_kcontrol *kctl;
1262 	unsigned int count;
1263 	unsigned int access;
1264 	long private_size;
1265 	struct user_element *ue;
1266 	unsigned int offset;
1267 	int err;
1268 
1269 	if (!*info->id.name)
1270 		return -EINVAL;
1271 	if (strnlen(info->id.name, sizeof(info->id.name)) >= sizeof(info->id.name))
1272 		return -EINVAL;
1273 
1274 	/* Delete a control to replace them if needed. */
1275 	if (replace) {
1276 		info->id.numid = 0;
1277 		err = snd_ctl_remove_user_ctl(file, &info->id);
1278 		if (err)
1279 			return err;
1280 	}
1281 
1282 	/*
1283 	 * The number of userspace controls are counted control by control,
1284 	 * not element by element.
1285 	 */
1286 	if (card->user_ctl_count + 1 > MAX_USER_CONTROLS)
1287 		return -ENOMEM;
1288 
1289 	/* Check the number of elements for this userspace control. */
1290 	count = info->owner;
1291 	if (count == 0)
1292 		count = 1;
1293 
1294 	/* Arrange access permissions if needed. */
1295 	access = info->access;
1296 	if (access == 0)
1297 		access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1298 	access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1299 		   SNDRV_CTL_ELEM_ACCESS_INACTIVE |
1300 		   SNDRV_CTL_ELEM_ACCESS_TLV_WRITE);
1301 
1302 	/* In initial state, nothing is available as TLV container. */
1303 	if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
1304 		access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1305 	access |= SNDRV_CTL_ELEM_ACCESS_USER;
1306 
1307 	/*
1308 	 * Check information and calculate the size of data specific to
1309 	 * this userspace control.
1310 	 */
1311 	if (info->type < SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
1312 	    info->type > SNDRV_CTL_ELEM_TYPE_INTEGER64)
1313 		return -EINVAL;
1314 	if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED &&
1315 	    info->value.enumerated.items == 0)
1316 		return -EINVAL;
1317 	if (info->count < 1 ||
1318 	    info->count > max_value_counts[info->type])
1319 		return -EINVAL;
1320 	if (!validate_element_member_dimension(info))
1321 		return -EINVAL;
1322 	private_size = value_sizes[info->type] * info->count;
1323 
1324 	/*
1325 	 * Keep memory object for this userspace control. After passing this
1326 	 * code block, the instance should be freed by snd_ctl_free_one().
1327 	 *
1328 	 * Note that these elements in this control are locked.
1329 	 */
1330 	err = snd_ctl_new(&kctl, count, access, file);
1331 	if (err < 0)
1332 		return err;
1333 	memcpy(&kctl->id, &info->id, sizeof(kctl->id));
1334 	kctl->private_data = kzalloc(sizeof(struct user_element) + private_size * count,
1335 				     GFP_KERNEL);
1336 	if (kctl->private_data == NULL) {
1337 		kfree(kctl);
1338 		return -ENOMEM;
1339 	}
1340 	kctl->private_free = snd_ctl_elem_user_free;
1341 
1342 	/* Set private data for this userspace control. */
1343 	ue = (struct user_element *)kctl->private_data;
1344 	ue->card = card;
1345 	ue->info = *info;
1346 	ue->info.access = 0;
1347 	ue->elem_data = (char *)ue + sizeof(*ue);
1348 	ue->elem_data_size = private_size;
1349 	if (ue->info.type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
1350 		err = snd_ctl_elem_init_enum_names(ue);
1351 		if (err < 0) {
1352 			snd_ctl_free_one(kctl);
1353 			return err;
1354 		}
1355 	}
1356 
1357 	/* Set callback functions. */
1358 	if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED)
1359 		kctl->info = snd_ctl_elem_user_enum_info;
1360 	else
1361 		kctl->info = snd_ctl_elem_user_info;
1362 	if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1363 		kctl->get = snd_ctl_elem_user_get;
1364 	if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1365 		kctl->put = snd_ctl_elem_user_put;
1366 	if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
1367 		kctl->tlv.c = snd_ctl_elem_user_tlv;
1368 
1369 	/* This function manage to free the instance on failure. */
1370 	down_write(&card->controls_rwsem);
1371 	err = __snd_ctl_add(card, kctl);
1372 	if (err < 0) {
1373 		snd_ctl_free_one(kctl);
1374 		goto unlock;
1375 	}
1376 	offset = snd_ctl_get_ioff(kctl, &info->id);
1377 	snd_ctl_build_ioff(&info->id, kctl, offset);
1378 	/*
1379 	 * Here we cannot fill any field for the number of elements added by
1380 	 * this operation because there're no specific fields. The usage of
1381 	 * 'owner' field for this purpose may cause any bugs to userspace
1382 	 * applications because the field originally means PID of a process
1383 	 * which locks the element.
1384 	 */
1385 
1386 	card->user_ctl_count++;
1387 
1388  unlock:
1389 	up_write(&card->controls_rwsem);
1390 	return 0;
1391 }
1392 
snd_ctl_elem_add_user(struct snd_ctl_file * file,struct snd_ctl_elem_info __user * _info,int replace)1393 static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1394 				 struct snd_ctl_elem_info __user *_info, int replace)
1395 {
1396 	struct snd_ctl_elem_info info;
1397 	int err;
1398 
1399 	if (copy_from_user(&info, _info, sizeof(info)))
1400 		return -EFAULT;
1401 	err = snd_ctl_elem_add(file, &info, replace);
1402 	if (err < 0)
1403 		return err;
1404 	if (copy_to_user(_info, &info, sizeof(info))) {
1405 		snd_ctl_remove_user_ctl(file, &info.id);
1406 		return -EFAULT;
1407 	}
1408 
1409 	return 0;
1410 }
1411 
snd_ctl_elem_remove(struct snd_ctl_file * file,struct snd_ctl_elem_id __user * _id)1412 static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1413 			       struct snd_ctl_elem_id __user *_id)
1414 {
1415 	struct snd_ctl_elem_id id;
1416 
1417 	if (copy_from_user(&id, _id, sizeof(id)))
1418 		return -EFAULT;
1419 	return snd_ctl_remove_user_ctl(file, &id);
1420 }
1421 
snd_ctl_subscribe_events(struct snd_ctl_file * file,int __user * ptr)1422 static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
1423 {
1424 	int subscribe;
1425 	if (get_user(subscribe, ptr))
1426 		return -EFAULT;
1427 	if (subscribe < 0) {
1428 		subscribe = file->subscribed;
1429 		if (put_user(subscribe, ptr))
1430 			return -EFAULT;
1431 		return 0;
1432 	}
1433 	if (subscribe) {
1434 		file->subscribed = 1;
1435 		return 0;
1436 	} else if (file->subscribed) {
1437 		snd_ctl_empty_read_queue(file);
1438 		file->subscribed = 0;
1439 	}
1440 	return 0;
1441 }
1442 
call_tlv_handler(struct snd_ctl_file * file,int op_flag,struct snd_kcontrol * kctl,struct snd_ctl_elem_id * id,unsigned int __user * buf,unsigned int size)1443 static int call_tlv_handler(struct snd_ctl_file *file, int op_flag,
1444 			    struct snd_kcontrol *kctl,
1445 			    struct snd_ctl_elem_id *id,
1446 			    unsigned int __user *buf, unsigned int size)
1447 {
1448 	static const struct {
1449 		int op;
1450 		int perm;
1451 	} pairs[] = {
1452 		{SNDRV_CTL_TLV_OP_READ,  SNDRV_CTL_ELEM_ACCESS_TLV_READ},
1453 		{SNDRV_CTL_TLV_OP_WRITE, SNDRV_CTL_ELEM_ACCESS_TLV_WRITE},
1454 		{SNDRV_CTL_TLV_OP_CMD,   SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND},
1455 	};
1456 	struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
1457 	int i;
1458 
1459 	/* Check support of the request for this element. */
1460 	for (i = 0; i < ARRAY_SIZE(pairs); ++i) {
1461 		if (op_flag == pairs[i].op && (vd->access & pairs[i].perm))
1462 			break;
1463 	}
1464 	if (i == ARRAY_SIZE(pairs))
1465 		return -ENXIO;
1466 
1467 	if (kctl->tlv.c == NULL)
1468 		return -ENXIO;
1469 
1470 	/* Write and command operations are not allowed for locked element. */
1471 	if (op_flag != SNDRV_CTL_TLV_OP_READ &&
1472 	    vd->owner != NULL && vd->owner != file)
1473 		return -EPERM;
1474 
1475 	return kctl->tlv.c(kctl, op_flag, size, buf);
1476 }
1477 
read_tlv_buf(struct snd_kcontrol * kctl,struct snd_ctl_elem_id * id,unsigned int __user * buf,unsigned int size)1478 static int read_tlv_buf(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id,
1479 			unsigned int __user *buf, unsigned int size)
1480 {
1481 	struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
1482 	unsigned int len;
1483 
1484 	if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ))
1485 		return -ENXIO;
1486 
1487 	if (kctl->tlv.p == NULL)
1488 		return -ENXIO;
1489 
1490 	len = sizeof(unsigned int) * 2 + kctl->tlv.p[1];
1491 	if (size < len)
1492 		return -ENOMEM;
1493 
1494 	if (copy_to_user(buf, kctl->tlv.p, len))
1495 		return -EFAULT;
1496 
1497 	return 0;
1498 }
1499 
snd_ctl_tlv_ioctl(struct snd_ctl_file * file,struct snd_ctl_tlv __user * buf,int op_flag)1500 static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1501 			     struct snd_ctl_tlv __user *buf,
1502                              int op_flag)
1503 {
1504 	struct snd_ctl_tlv header;
1505 	unsigned int *container;
1506 	unsigned int container_size;
1507 	struct snd_kcontrol *kctl;
1508 	struct snd_ctl_elem_id id;
1509 	struct snd_kcontrol_volatile *vd;
1510 
1511 	if (copy_from_user(&header, buf, sizeof(header)))
1512 		return -EFAULT;
1513 
1514 	/* In design of control core, numerical ID starts at 1. */
1515 	if (header.numid == 0)
1516 		return -EINVAL;
1517 
1518 	/* At least, container should include type and length fields.  */
1519 	if (header.length < sizeof(unsigned int) * 2)
1520 		return -EINVAL;
1521 	container_size = header.length;
1522 	container = buf->tlv;
1523 
1524 	kctl = snd_ctl_find_numid(file->card, header.numid);
1525 	if (kctl == NULL)
1526 		return -ENOENT;
1527 
1528 	/* Calculate index of the element in this set. */
1529 	id = kctl->id;
1530 	snd_ctl_build_ioff(&id, kctl, header.numid - id.numid);
1531 	vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1532 
1533 	if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1534 		return call_tlv_handler(file, op_flag, kctl, &id, container,
1535 					container_size);
1536 	} else {
1537 		if (op_flag == SNDRV_CTL_TLV_OP_READ) {
1538 			return read_tlv_buf(kctl, &id, container,
1539 					    container_size);
1540 		}
1541 	}
1542 
1543 	/* Not supported. */
1544 	return -ENXIO;
1545 }
1546 
snd_ctl_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1547 static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1548 {
1549 	struct snd_ctl_file *ctl;
1550 	struct snd_card *card;
1551 	struct snd_kctl_ioctl *p;
1552 	void __user *argp = (void __user *)arg;
1553 	int __user *ip = argp;
1554 	int err;
1555 
1556 	ctl = file->private_data;
1557 	card = ctl->card;
1558 	if (snd_BUG_ON(!card))
1559 		return -ENXIO;
1560 	switch (cmd) {
1561 	case SNDRV_CTL_IOCTL_PVERSION:
1562 		return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1563 	case SNDRV_CTL_IOCTL_CARD_INFO:
1564 		return snd_ctl_card_info(card, ctl, cmd, argp);
1565 	case SNDRV_CTL_IOCTL_ELEM_LIST:
1566 		return snd_ctl_elem_list(card, argp);
1567 	case SNDRV_CTL_IOCTL_ELEM_INFO:
1568 		return snd_ctl_elem_info_user(ctl, argp);
1569 	case SNDRV_CTL_IOCTL_ELEM_READ:
1570 		return snd_ctl_elem_read_user(card, argp);
1571 	case SNDRV_CTL_IOCTL_ELEM_WRITE:
1572 		return snd_ctl_elem_write_user(ctl, argp);
1573 	case SNDRV_CTL_IOCTL_ELEM_LOCK:
1574 		return snd_ctl_elem_lock(ctl, argp);
1575 	case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1576 		return snd_ctl_elem_unlock(ctl, argp);
1577 	case SNDRV_CTL_IOCTL_ELEM_ADD:
1578 		return snd_ctl_elem_add_user(ctl, argp, 0);
1579 	case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1580 		return snd_ctl_elem_add_user(ctl, argp, 1);
1581 	case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1582 		return snd_ctl_elem_remove(ctl, argp);
1583 	case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1584 		return snd_ctl_subscribe_events(ctl, ip);
1585 	case SNDRV_CTL_IOCTL_TLV_READ:
1586 		down_read(&ctl->card->controls_rwsem);
1587 		err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_READ);
1588 		up_read(&ctl->card->controls_rwsem);
1589 		return err;
1590 	case SNDRV_CTL_IOCTL_TLV_WRITE:
1591 		down_write(&ctl->card->controls_rwsem);
1592 		err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_WRITE);
1593 		up_write(&ctl->card->controls_rwsem);
1594 		return err;
1595 	case SNDRV_CTL_IOCTL_TLV_COMMAND:
1596 		down_write(&ctl->card->controls_rwsem);
1597 		err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_CMD);
1598 		up_write(&ctl->card->controls_rwsem);
1599 		return err;
1600 	case SNDRV_CTL_IOCTL_POWER:
1601 		return -ENOPROTOOPT;
1602 	case SNDRV_CTL_IOCTL_POWER_STATE:
1603 #ifdef CONFIG_PM
1604 		return put_user(card->power_state, ip) ? -EFAULT : 0;
1605 #else
1606 		return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1607 #endif
1608 	}
1609 	down_read(&snd_ioctl_rwsem);
1610 	list_for_each_entry(p, &snd_control_ioctls, list) {
1611 		err = p->fioctl(card, ctl, cmd, arg);
1612 		if (err != -ENOIOCTLCMD) {
1613 			up_read(&snd_ioctl_rwsem);
1614 			return err;
1615 		}
1616 	}
1617 	up_read(&snd_ioctl_rwsem);
1618 	dev_dbg(card->dev, "unknown ioctl = 0x%x\n", cmd);
1619 	return -ENOTTY;
1620 }
1621 
snd_ctl_read(struct file * file,char __user * buffer,size_t count,loff_t * offset)1622 static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1623 			    size_t count, loff_t * offset)
1624 {
1625 	struct snd_ctl_file *ctl;
1626 	int err = 0;
1627 	ssize_t result = 0;
1628 
1629 	ctl = file->private_data;
1630 	if (snd_BUG_ON(!ctl || !ctl->card))
1631 		return -ENXIO;
1632 	if (!ctl->subscribed)
1633 		return -EBADFD;
1634 	if (count < sizeof(struct snd_ctl_event))
1635 		return -EINVAL;
1636 	spin_lock_irq(&ctl->read_lock);
1637 	while (count >= sizeof(struct snd_ctl_event)) {
1638 		struct snd_ctl_event ev;
1639 		struct snd_kctl_event *kev;
1640 		while (list_empty(&ctl->events)) {
1641 			wait_queue_entry_t wait;
1642 			if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1643 				err = -EAGAIN;
1644 				goto __end_lock;
1645 			}
1646 			init_waitqueue_entry(&wait, current);
1647 			add_wait_queue(&ctl->change_sleep, &wait);
1648 			set_current_state(TASK_INTERRUPTIBLE);
1649 			spin_unlock_irq(&ctl->read_lock);
1650 			schedule();
1651 			remove_wait_queue(&ctl->change_sleep, &wait);
1652 			if (ctl->card->shutdown)
1653 				return -ENODEV;
1654 			if (signal_pending(current))
1655 				return -ERESTARTSYS;
1656 			spin_lock_irq(&ctl->read_lock);
1657 		}
1658 		kev = snd_kctl_event(ctl->events.next);
1659 		ev.type = SNDRV_CTL_EVENT_ELEM;
1660 		ev.data.elem.mask = kev->mask;
1661 		ev.data.elem.id = kev->id;
1662 		list_del(&kev->list);
1663 		spin_unlock_irq(&ctl->read_lock);
1664 		kfree(kev);
1665 		if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
1666 			err = -EFAULT;
1667 			goto __end;
1668 		}
1669 		spin_lock_irq(&ctl->read_lock);
1670 		buffer += sizeof(struct snd_ctl_event);
1671 		count -= sizeof(struct snd_ctl_event);
1672 		result += sizeof(struct snd_ctl_event);
1673 	}
1674       __end_lock:
1675 	spin_unlock_irq(&ctl->read_lock);
1676       __end:
1677       	return result > 0 ? result : err;
1678 }
1679 
snd_ctl_poll(struct file * file,poll_table * wait)1680 static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1681 {
1682 	unsigned int mask;
1683 	struct snd_ctl_file *ctl;
1684 
1685 	ctl = file->private_data;
1686 	if (!ctl->subscribed)
1687 		return 0;
1688 	poll_wait(file, &ctl->change_sleep, wait);
1689 
1690 	mask = 0;
1691 	if (!list_empty(&ctl->events))
1692 		mask |= POLLIN | POLLRDNORM;
1693 
1694 	return mask;
1695 }
1696 
1697 /*
1698  * register the device-specific control-ioctls.
1699  * called from each device manager like pcm.c, hwdep.c, etc.
1700  */
_snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn,struct list_head * lists)1701 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1702 {
1703 	struct snd_kctl_ioctl *pn;
1704 
1705 	pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
1706 	if (pn == NULL)
1707 		return -ENOMEM;
1708 	pn->fioctl = fcn;
1709 	down_write(&snd_ioctl_rwsem);
1710 	list_add_tail(&pn->list, lists);
1711 	up_write(&snd_ioctl_rwsem);
1712 	return 0;
1713 }
1714 
1715 /**
1716  * snd_ctl_register_ioctl - register the device-specific control-ioctls
1717  * @fcn: ioctl callback function
1718  *
1719  * called from each device manager like pcm.c, hwdep.c, etc.
1720  */
snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)1721 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1722 {
1723 	return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1724 }
1725 EXPORT_SYMBOL(snd_ctl_register_ioctl);
1726 
1727 #ifdef CONFIG_COMPAT
1728 /**
1729  * snd_ctl_register_ioctl_compat - register the device-specific 32bit compat
1730  * control-ioctls
1731  * @fcn: ioctl callback function
1732  */
snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)1733 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1734 {
1735 	return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1736 }
1737 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
1738 #endif
1739 
1740 /*
1741  * de-register the device-specific control-ioctls.
1742  */
_snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,struct list_head * lists)1743 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1744 				     struct list_head *lists)
1745 {
1746 	struct snd_kctl_ioctl *p;
1747 
1748 	if (snd_BUG_ON(!fcn))
1749 		return -EINVAL;
1750 	down_write(&snd_ioctl_rwsem);
1751 	list_for_each_entry(p, lists, list) {
1752 		if (p->fioctl == fcn) {
1753 			list_del(&p->list);
1754 			up_write(&snd_ioctl_rwsem);
1755 			kfree(p);
1756 			return 0;
1757 		}
1758 	}
1759 	up_write(&snd_ioctl_rwsem);
1760 	snd_BUG();
1761 	return -EINVAL;
1762 }
1763 
1764 /**
1765  * snd_ctl_unregister_ioctl - de-register the device-specific control-ioctls
1766  * @fcn: ioctl callback function to unregister
1767  */
snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)1768 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1769 {
1770 	return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1771 }
1772 EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1773 
1774 #ifdef CONFIG_COMPAT
1775 /**
1776  * snd_ctl_unregister_ioctl - de-register the device-specific compat 32bit
1777  * control-ioctls
1778  * @fcn: ioctl callback function to unregister
1779  */
snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)1780 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1781 {
1782 	return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1783 }
1784 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
1785 #endif
1786 
snd_ctl_fasync(int fd,struct file * file,int on)1787 static int snd_ctl_fasync(int fd, struct file * file, int on)
1788 {
1789 	struct snd_ctl_file *ctl;
1790 
1791 	ctl = file->private_data;
1792 	return fasync_helper(fd, file, on, &ctl->fasync);
1793 }
1794 
1795 /* return the preferred subdevice number if already assigned;
1796  * otherwise return -1
1797  */
snd_ctl_get_preferred_subdevice(struct snd_card * card,int type)1798 int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type)
1799 {
1800 	struct snd_ctl_file *kctl;
1801 	int subdevice = -1;
1802 
1803 	read_lock(&card->ctl_files_rwlock);
1804 	list_for_each_entry(kctl, &card->ctl_files, list) {
1805 		if (kctl->pid == task_pid(current)) {
1806 			subdevice = kctl->preferred_subdevice[type];
1807 			if (subdevice != -1)
1808 				break;
1809 		}
1810 	}
1811 	read_unlock(&card->ctl_files_rwlock);
1812 	return subdevice;
1813 }
1814 EXPORT_SYMBOL_GPL(snd_ctl_get_preferred_subdevice);
1815 
1816 /*
1817  * ioctl32 compat
1818  */
1819 #ifdef CONFIG_COMPAT
1820 #include "control_compat.c"
1821 #else
1822 #define snd_ctl_ioctl_compat	NULL
1823 #endif
1824 
1825 /*
1826  *  INIT PART
1827  */
1828 
1829 static const struct file_operations snd_ctl_f_ops =
1830 {
1831 	.owner =	THIS_MODULE,
1832 	.read =		snd_ctl_read,
1833 	.open =		snd_ctl_open,
1834 	.release =	snd_ctl_release,
1835 	.llseek =	no_llseek,
1836 	.poll =		snd_ctl_poll,
1837 	.unlocked_ioctl =	snd_ctl_ioctl,
1838 	.compat_ioctl =	snd_ctl_ioctl_compat,
1839 	.fasync =	snd_ctl_fasync,
1840 };
1841 
1842 /*
1843  * registration of the control device
1844  */
snd_ctl_dev_register(struct snd_device * device)1845 static int snd_ctl_dev_register(struct snd_device *device)
1846 {
1847 	struct snd_card *card = device->device_data;
1848 
1849 	return snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1850 				   &snd_ctl_f_ops, card, &card->ctl_dev);
1851 }
1852 
1853 /*
1854  * disconnection of the control device
1855  */
snd_ctl_dev_disconnect(struct snd_device * device)1856 static int snd_ctl_dev_disconnect(struct snd_device *device)
1857 {
1858 	struct snd_card *card = device->device_data;
1859 	struct snd_ctl_file *ctl;
1860 
1861 	read_lock(&card->ctl_files_rwlock);
1862 	list_for_each_entry(ctl, &card->ctl_files, list) {
1863 		wake_up(&ctl->change_sleep);
1864 		kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1865 	}
1866 	read_unlock(&card->ctl_files_rwlock);
1867 
1868 	return snd_unregister_device(&card->ctl_dev);
1869 }
1870 
1871 /*
1872  * free all controls
1873  */
snd_ctl_dev_free(struct snd_device * device)1874 static int snd_ctl_dev_free(struct snd_device *device)
1875 {
1876 	struct snd_card *card = device->device_data;
1877 	struct snd_kcontrol *control;
1878 
1879 	down_write(&card->controls_rwsem);
1880 	while (!list_empty(&card->controls)) {
1881 		control = snd_kcontrol(card->controls.next);
1882 		snd_ctl_remove(card, control);
1883 	}
1884 	up_write(&card->controls_rwsem);
1885 	put_device(&card->ctl_dev);
1886 	return 0;
1887 }
1888 
1889 /*
1890  * create control core:
1891  * called from init.c
1892  */
snd_ctl_create(struct snd_card * card)1893 int snd_ctl_create(struct snd_card *card)
1894 {
1895 	static struct snd_device_ops ops = {
1896 		.dev_free = snd_ctl_dev_free,
1897 		.dev_register =	snd_ctl_dev_register,
1898 		.dev_disconnect = snd_ctl_dev_disconnect,
1899 	};
1900 	int err;
1901 
1902 	if (snd_BUG_ON(!card))
1903 		return -ENXIO;
1904 	if (snd_BUG_ON(card->number < 0 || card->number >= SNDRV_CARDS))
1905 		return -ENXIO;
1906 
1907 	snd_device_initialize(&card->ctl_dev, card);
1908 	dev_set_name(&card->ctl_dev, "controlC%d", card->number);
1909 
1910 	err = snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
1911 	if (err < 0)
1912 		put_device(&card->ctl_dev);
1913 	return err;
1914 }
1915 
1916 /*
1917  * Frequently used control callbacks/helpers
1918  */
1919 
1920 /**
1921  * snd_ctl_boolean_mono_info - Helper function for a standard boolean info
1922  * callback with a mono channel
1923  * @kcontrol: the kcontrol instance
1924  * @uinfo: info to store
1925  *
1926  * This is a function that can be used as info callback for a standard
1927  * boolean control with a single mono channel.
1928  */
snd_ctl_boolean_mono_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1929 int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
1930 			      struct snd_ctl_elem_info *uinfo)
1931 {
1932 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1933 	uinfo->count = 1;
1934 	uinfo->value.integer.min = 0;
1935 	uinfo->value.integer.max = 1;
1936 	return 0;
1937 }
1938 EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
1939 
1940 /**
1941  * snd_ctl_boolean_stereo_info - Helper function for a standard boolean info
1942  * callback with stereo two channels
1943  * @kcontrol: the kcontrol instance
1944  * @uinfo: info to store
1945  *
1946  * This is a function that can be used as info callback for a standard
1947  * boolean control with stereo two channels.
1948  */
snd_ctl_boolean_stereo_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1949 int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
1950 				struct snd_ctl_elem_info *uinfo)
1951 {
1952 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1953 	uinfo->count = 2;
1954 	uinfo->value.integer.min = 0;
1955 	uinfo->value.integer.max = 1;
1956 	return 0;
1957 }
1958 EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
1959 
1960 /**
1961  * snd_ctl_enum_info - fills the info structure for an enumerated control
1962  * @info: the structure to be filled
1963  * @channels: the number of the control's channels; often one
1964  * @items: the number of control values; also the size of @names
1965  * @names: an array containing the names of all control values
1966  *
1967  * Sets all required fields in @info to their appropriate values.
1968  * If the control's accessibility is not the default (readable and writable),
1969  * the caller has to fill @info->access.
1970  *
1971  * Return: Zero.
1972  */
snd_ctl_enum_info(struct snd_ctl_elem_info * info,unsigned int channels,unsigned int items,const char * const names[])1973 int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
1974 		      unsigned int items, const char *const names[])
1975 {
1976 	info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1977 	info->count = channels;
1978 	info->value.enumerated.items = items;
1979 	if (!items)
1980 		return 0;
1981 	if (info->value.enumerated.item >= items)
1982 		info->value.enumerated.item = items - 1;
1983 	WARN(strlen(names[info->value.enumerated.item]) >= sizeof(info->value.enumerated.name),
1984 	     "ALSA: too long item name '%s'\n",
1985 	     names[info->value.enumerated.item]);
1986 	strlcpy(info->value.enumerated.name,
1987 		names[info->value.enumerated.item],
1988 		sizeof(info->value.enumerated.name));
1989 	return 0;
1990 }
1991 EXPORT_SYMBOL(snd_ctl_enum_info);
1992