xref: /openbmc/linux/sound/core/control.c (revision 192c4cccd015f52c94d0420eb5d7305a1ca28998)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Routines for driver control interface
4  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
5  */
6 
7 #include <linux/threads.h>
8 #include <linux/interrupt.h>
9 #include <linux/module.h>
10 #include <linux/moduleparam.h>
11 #include <linux/slab.h>
12 #include <linux/vmalloc.h>
13 #include <linux/time.h>
14 #include <linux/mm.h>
15 #include <linux/math64.h>
16 #include <linux/sched/signal.h>
17 #include <sound/core.h>
18 #include <sound/minors.h>
19 #include <sound/info.h>
20 #include <sound/control.h>
21 
22 // Max allocation size for user controls.
23 static int max_user_ctl_alloc_size = 8 * 1024 * 1024;
24 module_param_named(max_user_ctl_alloc_size, max_user_ctl_alloc_size, int, 0444);
25 MODULE_PARM_DESC(max_user_ctl_alloc_size, "Max allocation size for user controls");
26 
27 #define MAX_CONTROL_COUNT	1028
28 
29 struct snd_kctl_ioctl {
30 	struct list_head list;		/* list of all ioctls */
31 	snd_kctl_ioctl_func_t fioctl;
32 };
33 
34 static DECLARE_RWSEM(snd_ioctl_rwsem);
35 static DECLARE_RWSEM(snd_ctl_layer_rwsem);
36 static LIST_HEAD(snd_control_ioctls);
37 #ifdef CONFIG_COMPAT
38 static LIST_HEAD(snd_control_compat_ioctls);
39 #endif
40 static struct snd_ctl_layer_ops *snd_ctl_layer;
41 
42 static int snd_ctl_remove_locked(struct snd_card *card,
43 				 struct snd_kcontrol *kcontrol);
44 
45 static int snd_ctl_open(struct inode *inode, struct file *file)
46 {
47 	unsigned long flags;
48 	struct snd_card *card;
49 	struct snd_ctl_file *ctl;
50 	int i, err;
51 
52 	err = stream_open(inode, file);
53 	if (err < 0)
54 		return err;
55 
56 	card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
57 	if (!card) {
58 		err = -ENODEV;
59 		goto __error1;
60 	}
61 	err = snd_card_file_add(card, file);
62 	if (err < 0) {
63 		err = -ENODEV;
64 		goto __error1;
65 	}
66 	if (!try_module_get(card->module)) {
67 		err = -EFAULT;
68 		goto __error2;
69 	}
70 	ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
71 	if (ctl == NULL) {
72 		err = -ENOMEM;
73 		goto __error;
74 	}
75 	INIT_LIST_HEAD(&ctl->events);
76 	init_waitqueue_head(&ctl->change_sleep);
77 	spin_lock_init(&ctl->read_lock);
78 	ctl->card = card;
79 	for (i = 0; i < SND_CTL_SUBDEV_ITEMS; i++)
80 		ctl->preferred_subdevice[i] = -1;
81 	ctl->pid = get_pid(task_pid(current));
82 	file->private_data = ctl;
83 	write_lock_irqsave(&card->ctl_files_rwlock, flags);
84 	list_add_tail(&ctl->list, &card->ctl_files);
85 	write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
86 	snd_card_unref(card);
87 	return 0;
88 
89       __error:
90 	module_put(card->module);
91       __error2:
92 	snd_card_file_remove(card, file);
93       __error1:
94 	if (card)
95 		snd_card_unref(card);
96       	return err;
97 }
98 
99 static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
100 {
101 	unsigned long flags;
102 	struct snd_kctl_event *cread;
103 
104 	spin_lock_irqsave(&ctl->read_lock, flags);
105 	while (!list_empty(&ctl->events)) {
106 		cread = snd_kctl_event(ctl->events.next);
107 		list_del(&cread->list);
108 		kfree(cread);
109 	}
110 	spin_unlock_irqrestore(&ctl->read_lock, flags);
111 }
112 
113 static int snd_ctl_release(struct inode *inode, struct file *file)
114 {
115 	unsigned long flags;
116 	struct snd_card *card;
117 	struct snd_ctl_file *ctl;
118 	struct snd_kcontrol *control;
119 	unsigned int idx;
120 
121 	ctl = file->private_data;
122 	file->private_data = NULL;
123 	card = ctl->card;
124 	write_lock_irqsave(&card->ctl_files_rwlock, flags);
125 	list_del(&ctl->list);
126 	write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
127 	down_write(&card->controls_rwsem);
128 	list_for_each_entry(control, &card->controls, list)
129 		for (idx = 0; idx < control->count; idx++)
130 			if (control->vd[idx].owner == ctl)
131 				control->vd[idx].owner = NULL;
132 	up_write(&card->controls_rwsem);
133 	snd_fasync_free(ctl->fasync);
134 	snd_ctl_empty_read_queue(ctl);
135 	put_pid(ctl->pid);
136 	kfree(ctl);
137 	module_put(card->module);
138 	snd_card_file_remove(card, file);
139 	return 0;
140 }
141 
142 /**
143  * snd_ctl_notify - Send notification to user-space for a control change
144  * @card: the card to send notification
145  * @mask: the event mask, SNDRV_CTL_EVENT_*
146  * @id: the ctl element id to send notification
147  *
148  * This function adds an event record with the given id and mask, appends
149  * to the list and wakes up the user-space for notification.  This can be
150  * called in the atomic context.
151  */
152 void snd_ctl_notify(struct snd_card *card, unsigned int mask,
153 		    struct snd_ctl_elem_id *id)
154 {
155 	unsigned long flags;
156 	struct snd_ctl_file *ctl;
157 	struct snd_kctl_event *ev;
158 
159 	if (snd_BUG_ON(!card || !id))
160 		return;
161 	if (card->shutdown)
162 		return;
163 	read_lock_irqsave(&card->ctl_files_rwlock, flags);
164 #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
165 	card->mixer_oss_change_count++;
166 #endif
167 	list_for_each_entry(ctl, &card->ctl_files, list) {
168 		if (!ctl->subscribed)
169 			continue;
170 		spin_lock(&ctl->read_lock);
171 		list_for_each_entry(ev, &ctl->events, list) {
172 			if (ev->id.numid == id->numid) {
173 				ev->mask |= mask;
174 				goto _found;
175 			}
176 		}
177 		ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
178 		if (ev) {
179 			ev->id = *id;
180 			ev->mask = mask;
181 			list_add_tail(&ev->list, &ctl->events);
182 		} else {
183 			dev_err(card->dev, "No memory available to allocate event\n");
184 		}
185 	_found:
186 		wake_up(&ctl->change_sleep);
187 		spin_unlock(&ctl->read_lock);
188 		snd_kill_fasync(ctl->fasync, SIGIO, POLL_IN);
189 	}
190 	read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
191 }
192 EXPORT_SYMBOL(snd_ctl_notify);
193 
194 /**
195  * snd_ctl_notify_one - Send notification to user-space for a control change
196  * @card: the card to send notification
197  * @mask: the event mask, SNDRV_CTL_EVENT_*
198  * @kctl: the pointer with the control instance
199  * @ioff: the additional offset to the control index
200  *
201  * This function calls snd_ctl_notify() and does additional jobs
202  * like LED state changes.
203  */
204 void snd_ctl_notify_one(struct snd_card *card, unsigned int mask,
205 			struct snd_kcontrol *kctl, unsigned int ioff)
206 {
207 	struct snd_ctl_elem_id id = kctl->id;
208 	struct snd_ctl_layer_ops *lops;
209 
210 	id.index += ioff;
211 	id.numid += ioff;
212 	snd_ctl_notify(card, mask, &id);
213 	down_read(&snd_ctl_layer_rwsem);
214 	for (lops = snd_ctl_layer; lops; lops = lops->next)
215 		lops->lnotify(card, mask, kctl, ioff);
216 	up_read(&snd_ctl_layer_rwsem);
217 }
218 EXPORT_SYMBOL(snd_ctl_notify_one);
219 
220 /**
221  * snd_ctl_new - create a new control instance with some elements
222  * @kctl: the pointer to store new control instance
223  * @count: the number of elements in this control
224  * @access: the default access flags for elements in this control
225  * @file: given when locking these elements
226  *
227  * Allocates a memory object for a new control instance. The instance has
228  * elements as many as the given number (@count). Each element has given
229  * access permissions (@access). Each element is locked when @file is given.
230  *
231  * Return: 0 on success, error code on failure
232  */
233 static int snd_ctl_new(struct snd_kcontrol **kctl, unsigned int count,
234 		       unsigned int access, struct snd_ctl_file *file)
235 {
236 	unsigned int idx;
237 
238 	if (count == 0 || count > MAX_CONTROL_COUNT)
239 		return -EINVAL;
240 
241 	*kctl = kzalloc(struct_size(*kctl, vd, count), GFP_KERNEL);
242 	if (!*kctl)
243 		return -ENOMEM;
244 
245 	for (idx = 0; idx < count; idx++) {
246 		(*kctl)->vd[idx].access = access;
247 		(*kctl)->vd[idx].owner = file;
248 	}
249 	(*kctl)->count = count;
250 
251 	return 0;
252 }
253 
254 /**
255  * snd_ctl_new1 - create a control instance from the template
256  * @ncontrol: the initialization record
257  * @private_data: the private data to set
258  *
259  * Allocates a new struct snd_kcontrol instance and initialize from the given
260  * template.  When the access field of ncontrol is 0, it's assumed as
261  * READWRITE access. When the count field is 0, it's assumes as one.
262  *
263  * Return: The pointer of the newly generated instance, or %NULL on failure.
264  */
265 struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
266 				  void *private_data)
267 {
268 	struct snd_kcontrol *kctl;
269 	unsigned int count;
270 	unsigned int access;
271 	int err;
272 
273 	if (snd_BUG_ON(!ncontrol || !ncontrol->info))
274 		return NULL;
275 
276 	count = ncontrol->count;
277 	if (count == 0)
278 		count = 1;
279 
280 	access = ncontrol->access;
281 	if (access == 0)
282 		access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
283 	access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
284 		   SNDRV_CTL_ELEM_ACCESS_VOLATILE |
285 		   SNDRV_CTL_ELEM_ACCESS_INACTIVE |
286 		   SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE |
287 		   SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND |
288 		   SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK |
289 		   SNDRV_CTL_ELEM_ACCESS_LED_MASK |
290 		   SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK);
291 
292 	err = snd_ctl_new(&kctl, count, access, NULL);
293 	if (err < 0)
294 		return NULL;
295 
296 	/* The 'numid' member is decided when calling snd_ctl_add(). */
297 	kctl->id.iface = ncontrol->iface;
298 	kctl->id.device = ncontrol->device;
299 	kctl->id.subdevice = ncontrol->subdevice;
300 	if (ncontrol->name) {
301 		strscpy(kctl->id.name, ncontrol->name, sizeof(kctl->id.name));
302 		if (strcmp(ncontrol->name, kctl->id.name) != 0)
303 			pr_warn("ALSA: Control name '%s' truncated to '%s'\n",
304 				ncontrol->name, kctl->id.name);
305 	}
306 	kctl->id.index = ncontrol->index;
307 
308 	kctl->info = ncontrol->info;
309 	kctl->get = ncontrol->get;
310 	kctl->put = ncontrol->put;
311 	kctl->tlv.p = ncontrol->tlv.p;
312 
313 	kctl->private_value = ncontrol->private_value;
314 	kctl->private_data = private_data;
315 
316 	return kctl;
317 }
318 EXPORT_SYMBOL(snd_ctl_new1);
319 
320 /**
321  * snd_ctl_free_one - release the control instance
322  * @kcontrol: the control instance
323  *
324  * Releases the control instance created via snd_ctl_new()
325  * or snd_ctl_new1().
326  * Don't call this after the control was added to the card.
327  */
328 void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
329 {
330 	if (kcontrol) {
331 		if (kcontrol->private_free)
332 			kcontrol->private_free(kcontrol);
333 		kfree(kcontrol);
334 	}
335 }
336 EXPORT_SYMBOL(snd_ctl_free_one);
337 
338 static bool snd_ctl_remove_numid_conflict(struct snd_card *card,
339 					  unsigned int count)
340 {
341 	struct snd_kcontrol *kctl;
342 
343 	/* Make sure that the ids assigned to the control do not wrap around */
344 	if (card->last_numid >= UINT_MAX - count)
345 		card->last_numid = 0;
346 
347 	list_for_each_entry(kctl, &card->controls, list) {
348 		if (kctl->id.numid < card->last_numid + 1 + count &&
349 		    kctl->id.numid + kctl->count > card->last_numid + 1) {
350 		    	card->last_numid = kctl->id.numid + kctl->count - 1;
351 			return true;
352 		}
353 	}
354 	return false;
355 }
356 
357 static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
358 {
359 	unsigned int iter = 100000;
360 
361 	while (snd_ctl_remove_numid_conflict(card, count)) {
362 		if (--iter == 0) {
363 			/* this situation is very unlikely */
364 			dev_err(card->dev, "unable to allocate new control numid\n");
365 			return -ENOMEM;
366 		}
367 	}
368 	return 0;
369 }
370 
371 /* check whether the given id is contained in the given kctl */
372 static bool elem_id_matches(const struct snd_kcontrol *kctl,
373 			    const struct snd_ctl_elem_id *id)
374 {
375 	return kctl->id.iface == id->iface &&
376 		kctl->id.device == id->device &&
377 		kctl->id.subdevice == id->subdevice &&
378 		!strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)) &&
379 		kctl->id.index <= id->index &&
380 		kctl->id.index + kctl->count > id->index;
381 }
382 
383 #ifdef CONFIG_SND_CTL_FAST_LOOKUP
384 /* Compute a hash key for the corresponding ctl id
385  * It's for the name lookup, hence the numid is excluded.
386  * The hash key is bound in LONG_MAX to be used for Xarray key.
387  */
388 #define MULTIPLIER	37
389 static unsigned long get_ctl_id_hash(const struct snd_ctl_elem_id *id)
390 {
391 	int i;
392 	unsigned long h;
393 
394 	h = id->iface;
395 	h = MULTIPLIER * h + id->device;
396 	h = MULTIPLIER * h + id->subdevice;
397 	for (i = 0; i < SNDRV_CTL_ELEM_ID_NAME_MAXLEN && id->name[i]; i++)
398 		h = MULTIPLIER * h + id->name[i];
399 	h = MULTIPLIER * h + id->index;
400 	h &= LONG_MAX;
401 	return h;
402 }
403 
404 /* add hash entries to numid and ctl xarray tables */
405 static void add_hash_entries(struct snd_card *card,
406 			     struct snd_kcontrol *kcontrol)
407 {
408 	struct snd_ctl_elem_id id = kcontrol->id;
409 	int i;
410 
411 	xa_store_range(&card->ctl_numids, kcontrol->id.numid,
412 		       kcontrol->id.numid + kcontrol->count - 1,
413 		       kcontrol, GFP_KERNEL);
414 
415 	for (i = 0; i < kcontrol->count; i++) {
416 		id.index = kcontrol->id.index + i;
417 		if (xa_insert(&card->ctl_hash, get_ctl_id_hash(&id),
418 			      kcontrol, GFP_KERNEL)) {
419 			/* skip hash for this entry, noting we had collision */
420 			card->ctl_hash_collision = true;
421 			dev_dbg(card->dev, "ctl_hash collision %d:%s:%d\n",
422 				id.iface, id.name, id.index);
423 		}
424 	}
425 }
426 
427 /* remove hash entries that have been added */
428 static void remove_hash_entries(struct snd_card *card,
429 				struct snd_kcontrol *kcontrol)
430 {
431 	struct snd_ctl_elem_id id = kcontrol->id;
432 	struct snd_kcontrol *matched;
433 	unsigned long h;
434 	int i;
435 
436 	for (i = 0; i < kcontrol->count; i++) {
437 		xa_erase(&card->ctl_numids, id.numid);
438 		h = get_ctl_id_hash(&id);
439 		matched = xa_load(&card->ctl_hash, h);
440 		if (matched && (matched == kcontrol ||
441 				elem_id_matches(matched, &id)))
442 			xa_erase(&card->ctl_hash, h);
443 		id.index++;
444 		id.numid++;
445 	}
446 }
447 #else /* CONFIG_SND_CTL_FAST_LOOKUP */
448 static inline void add_hash_entries(struct snd_card *card,
449 				    struct snd_kcontrol *kcontrol)
450 {
451 }
452 static inline void remove_hash_entries(struct snd_card *card,
453 				       struct snd_kcontrol *kcontrol)
454 {
455 }
456 #endif /* CONFIG_SND_CTL_FAST_LOOKUP */
457 
458 enum snd_ctl_add_mode {
459 	CTL_ADD_EXCLUSIVE, CTL_REPLACE, CTL_ADD_ON_REPLACE,
460 };
461 
462 /* add/replace a new kcontrol object; call with card->controls_rwsem locked */
463 static int __snd_ctl_add_replace(struct snd_card *card,
464 				 struct snd_kcontrol *kcontrol,
465 				 enum snd_ctl_add_mode mode)
466 {
467 	struct snd_ctl_elem_id id;
468 	unsigned int idx;
469 	struct snd_kcontrol *old;
470 	int err;
471 
472 	id = kcontrol->id;
473 	if (id.index > UINT_MAX - kcontrol->count)
474 		return -EINVAL;
475 
476 	old = snd_ctl_find_id(card, &id);
477 	if (!old) {
478 		if (mode == CTL_REPLACE)
479 			return -EINVAL;
480 	} else {
481 		if (mode == CTL_ADD_EXCLUSIVE) {
482 			dev_err(card->dev,
483 				"control %i:%i:%i:%s:%i is already present\n",
484 				id.iface, id.device, id.subdevice, id.name,
485 				id.index);
486 			return -EBUSY;
487 		}
488 
489 		err = snd_ctl_remove_locked(card, old);
490 		if (err < 0)
491 			return err;
492 	}
493 
494 	if (snd_ctl_find_hole(card, kcontrol->count) < 0)
495 		return -ENOMEM;
496 
497 	list_add_tail(&kcontrol->list, &card->controls);
498 	card->controls_count += kcontrol->count;
499 	kcontrol->id.numid = card->last_numid + 1;
500 	card->last_numid += kcontrol->count;
501 
502 	add_hash_entries(card, kcontrol);
503 
504 	for (idx = 0; idx < kcontrol->count; idx++)
505 		snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_ADD, kcontrol, idx);
506 
507 	return 0;
508 }
509 
510 static int snd_ctl_add_replace(struct snd_card *card,
511 			       struct snd_kcontrol *kcontrol,
512 			       enum snd_ctl_add_mode mode)
513 {
514 	int err = -EINVAL;
515 
516 	if (! kcontrol)
517 		return err;
518 	if (snd_BUG_ON(!card || !kcontrol->info))
519 		goto error;
520 
521 	down_write(&card->controls_rwsem);
522 	err = __snd_ctl_add_replace(card, kcontrol, mode);
523 	up_write(&card->controls_rwsem);
524 	if (err < 0)
525 		goto error;
526 	return 0;
527 
528  error:
529 	snd_ctl_free_one(kcontrol);
530 	return err;
531 }
532 
533 /**
534  * snd_ctl_add - add the control instance to the card
535  * @card: the card instance
536  * @kcontrol: the control instance to add
537  *
538  * Adds the control instance created via snd_ctl_new() or
539  * snd_ctl_new1() to the given card. Assigns also an unique
540  * numid used for fast search.
541  *
542  * It frees automatically the control which cannot be added.
543  *
544  * Return: Zero if successful, or a negative error code on failure.
545  *
546  */
547 int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
548 {
549 	return snd_ctl_add_replace(card, kcontrol, CTL_ADD_EXCLUSIVE);
550 }
551 EXPORT_SYMBOL(snd_ctl_add);
552 
553 /**
554  * snd_ctl_replace - replace the control instance of the card
555  * @card: the card instance
556  * @kcontrol: the control instance to replace
557  * @add_on_replace: add the control if not already added
558  *
559  * Replaces the given control.  If the given control does not exist
560  * and the add_on_replace flag is set, the control is added.  If the
561  * control exists, it is destroyed first.
562  *
563  * It frees automatically the control which cannot be added or replaced.
564  *
565  * Return: Zero if successful, or a negative error code on failure.
566  */
567 int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol,
568 		    bool add_on_replace)
569 {
570 	return snd_ctl_add_replace(card, kcontrol,
571 				   add_on_replace ? CTL_ADD_ON_REPLACE : CTL_REPLACE);
572 }
573 EXPORT_SYMBOL(snd_ctl_replace);
574 
575 static int __snd_ctl_remove(struct snd_card *card,
576 			    struct snd_kcontrol *kcontrol,
577 			    bool remove_hash)
578 {
579 	unsigned int idx;
580 
581 	if (snd_BUG_ON(!card || !kcontrol))
582 		return -EINVAL;
583 	list_del(&kcontrol->list);
584 
585 	if (remove_hash)
586 		remove_hash_entries(card, kcontrol);
587 
588 	card->controls_count -= kcontrol->count;
589 	for (idx = 0; idx < kcontrol->count; idx++)
590 		snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_REMOVE, kcontrol, idx);
591 	snd_ctl_free_one(kcontrol);
592 	return 0;
593 }
594 
595 static inline int snd_ctl_remove_locked(struct snd_card *card,
596 					struct snd_kcontrol *kcontrol)
597 {
598 	return __snd_ctl_remove(card, kcontrol, true);
599 }
600 
601 /**
602  * snd_ctl_remove - remove the control from the card and release it
603  * @card: the card instance
604  * @kcontrol: the control instance to remove
605  *
606  * Removes the control from the card and then releases the instance.
607  * You don't need to call snd_ctl_free_one().
608  *
609  * Return: 0 if successful, or a negative error code on failure.
610  *
611  * Note that this function takes card->controls_rwsem lock internally.
612  */
613 int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
614 {
615 	int ret;
616 
617 	down_write(&card->controls_rwsem);
618 	ret = snd_ctl_remove_locked(card, kcontrol);
619 	up_write(&card->controls_rwsem);
620 	return ret;
621 }
622 EXPORT_SYMBOL(snd_ctl_remove);
623 
624 /**
625  * snd_ctl_remove_id - remove the control of the given id and release it
626  * @card: the card instance
627  * @id: the control id to remove
628  *
629  * Finds the control instance with the given id, removes it from the
630  * card list and releases it.
631  *
632  * Return: 0 if successful, or a negative error code on failure.
633  */
634 int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
635 {
636 	struct snd_kcontrol *kctl;
637 	int ret;
638 
639 	down_write(&card->controls_rwsem);
640 	kctl = snd_ctl_find_id(card, id);
641 	if (kctl == NULL) {
642 		up_write(&card->controls_rwsem);
643 		return -ENOENT;
644 	}
645 	ret = snd_ctl_remove_locked(card, kctl);
646 	up_write(&card->controls_rwsem);
647 	return ret;
648 }
649 EXPORT_SYMBOL(snd_ctl_remove_id);
650 
651 /**
652  * snd_ctl_remove_user_ctl - remove and release the unlocked user control
653  * @file: active control handle
654  * @id: the control id to remove
655  *
656  * Finds the control instance with the given id, removes it from the
657  * card list and releases it.
658  *
659  * Return: 0 if successful, or a negative error code on failure.
660  */
661 static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
662 				   struct snd_ctl_elem_id *id)
663 {
664 	struct snd_card *card = file->card;
665 	struct snd_kcontrol *kctl;
666 	int idx, ret;
667 
668 	down_write(&card->controls_rwsem);
669 	kctl = snd_ctl_find_id(card, id);
670 	if (kctl == NULL) {
671 		ret = -ENOENT;
672 		goto error;
673 	}
674 	if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
675 		ret = -EINVAL;
676 		goto error;
677 	}
678 	for (idx = 0; idx < kctl->count; idx++)
679 		if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
680 			ret = -EBUSY;
681 			goto error;
682 		}
683 	ret = snd_ctl_remove_locked(card, kctl);
684 error:
685 	up_write(&card->controls_rwsem);
686 	return ret;
687 }
688 
689 /**
690  * snd_ctl_activate_id - activate/inactivate the control of the given id
691  * @card: the card instance
692  * @id: the control id to activate/inactivate
693  * @active: non-zero to activate
694  *
695  * Finds the control instance with the given id, and activate or
696  * inactivate the control together with notification, if changed.
697  * The given ID data is filled with full information.
698  *
699  * Return: 0 if unchanged, 1 if changed, or a negative error code on failure.
700  */
701 int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
702 			int active)
703 {
704 	struct snd_kcontrol *kctl;
705 	struct snd_kcontrol_volatile *vd;
706 	unsigned int index_offset;
707 	int ret;
708 
709 	down_write(&card->controls_rwsem);
710 	kctl = snd_ctl_find_id(card, id);
711 	if (kctl == NULL) {
712 		ret = -ENOENT;
713 		goto unlock;
714 	}
715 	index_offset = snd_ctl_get_ioff(kctl, id);
716 	vd = &kctl->vd[index_offset];
717 	ret = 0;
718 	if (active) {
719 		if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
720 			goto unlock;
721 		vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
722 	} else {
723 		if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)
724 			goto unlock;
725 		vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
726 	}
727 	snd_ctl_build_ioff(id, kctl, index_offset);
728 	downgrade_write(&card->controls_rwsem);
729 	snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_INFO, kctl, index_offset);
730 	up_read(&card->controls_rwsem);
731 	return 1;
732 
733  unlock:
734 	up_write(&card->controls_rwsem);
735 	return ret;
736 }
737 EXPORT_SYMBOL_GPL(snd_ctl_activate_id);
738 
739 /**
740  * snd_ctl_rename_id - replace the id of a control on the card
741  * @card: the card instance
742  * @src_id: the old id
743  * @dst_id: the new id
744  *
745  * Finds the control with the old id from the card, and replaces the
746  * id with the new one.
747  *
748  * The function tries to keep the already assigned numid while replacing
749  * the rest.
750  *
751  * Note that this function should be used only in the card initialization
752  * phase.  Calling after the card instantiation may cause issues with
753  * user-space expecting persistent numids.
754  *
755  * Return: Zero if successful, or a negative error code on failure.
756  */
757 int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
758 		      struct snd_ctl_elem_id *dst_id)
759 {
760 	struct snd_kcontrol *kctl;
761 	int saved_numid;
762 
763 	down_write(&card->controls_rwsem);
764 	kctl = snd_ctl_find_id(card, src_id);
765 	if (kctl == NULL) {
766 		up_write(&card->controls_rwsem);
767 		return -ENOENT;
768 	}
769 	saved_numid = kctl->id.numid;
770 	remove_hash_entries(card, kctl);
771 	kctl->id = *dst_id;
772 	kctl->id.numid = saved_numid;
773 	add_hash_entries(card, kctl);
774 	up_write(&card->controls_rwsem);
775 	return 0;
776 }
777 EXPORT_SYMBOL(snd_ctl_rename_id);
778 
779 /**
780  * snd_ctl_rename - rename the control on the card
781  * @card: the card instance
782  * @kctl: the control to rename
783  * @name: the new name
784  *
785  * Renames the specified control on the card to the new name.
786  *
787  * Note that this function takes card->controls_rwsem lock internally.
788  */
789 void snd_ctl_rename(struct snd_card *card, struct snd_kcontrol *kctl,
790 		    const char *name)
791 {
792 	down_write(&card->controls_rwsem);
793 	remove_hash_entries(card, kctl);
794 
795 	if (strscpy(kctl->id.name, name, sizeof(kctl->id.name)) < 0)
796 		pr_warn("ALSA: Renamed control new name '%s' truncated to '%s'\n",
797 			name, kctl->id.name);
798 
799 	add_hash_entries(card, kctl);
800 	up_write(&card->controls_rwsem);
801 }
802 EXPORT_SYMBOL(snd_ctl_rename);
803 
804 #ifndef CONFIG_SND_CTL_FAST_LOOKUP
805 static struct snd_kcontrol *
806 snd_ctl_find_numid_slow(struct snd_card *card, unsigned int numid)
807 {
808 	struct snd_kcontrol *kctl;
809 
810 	list_for_each_entry(kctl, &card->controls, list) {
811 		if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
812 			return kctl;
813 	}
814 	return NULL;
815 }
816 #endif /* !CONFIG_SND_CTL_FAST_LOOKUP */
817 
818 /**
819  * snd_ctl_find_numid - find the control instance with the given number-id
820  * @card: the card instance
821  * @numid: the number-id to search
822  *
823  * Finds the control instance with the given number-id from the card.
824  *
825  * The caller must down card->controls_rwsem before calling this function
826  * (if the race condition can happen).
827  *
828  * Return: The pointer of the instance if found, or %NULL if not.
829  *
830  */
831 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
832 {
833 	if (snd_BUG_ON(!card || !numid))
834 		return NULL;
835 #ifdef CONFIG_SND_CTL_FAST_LOOKUP
836 	return xa_load(&card->ctl_numids, numid);
837 #else
838 	return snd_ctl_find_numid_slow(card, numid);
839 #endif
840 }
841 EXPORT_SYMBOL(snd_ctl_find_numid);
842 
843 /**
844  * snd_ctl_find_id - find the control instance with the given id
845  * @card: the card instance
846  * @id: the id to search
847  *
848  * Finds the control instance with the given id from the card.
849  *
850  * The caller must down card->controls_rwsem before calling this function
851  * (if the race condition can happen).
852  *
853  * Return: The pointer of the instance if found, or %NULL if not.
854  *
855  */
856 struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
857 				     struct snd_ctl_elem_id *id)
858 {
859 	struct snd_kcontrol *kctl;
860 
861 	if (snd_BUG_ON(!card || !id))
862 		return NULL;
863 	if (id->numid != 0)
864 		return snd_ctl_find_numid(card, id->numid);
865 #ifdef CONFIG_SND_CTL_FAST_LOOKUP
866 	kctl = xa_load(&card->ctl_hash, get_ctl_id_hash(id));
867 	if (kctl && elem_id_matches(kctl, id))
868 		return kctl;
869 	if (!card->ctl_hash_collision)
870 		return NULL; /* we can rely on only hash table */
871 #endif
872 	/* no matching in hash table - try all as the last resort */
873 	list_for_each_entry(kctl, &card->controls, list)
874 		if (elem_id_matches(kctl, id))
875 			return kctl;
876 
877 	return NULL;
878 }
879 EXPORT_SYMBOL(snd_ctl_find_id);
880 
881 static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
882 			     unsigned int cmd, void __user *arg)
883 {
884 	struct snd_ctl_card_info *info;
885 
886 	info = kzalloc(sizeof(*info), GFP_KERNEL);
887 	if (! info)
888 		return -ENOMEM;
889 	down_read(&snd_ioctl_rwsem);
890 	info->card = card->number;
891 	strscpy(info->id, card->id, sizeof(info->id));
892 	strscpy(info->driver, card->driver, sizeof(info->driver));
893 	strscpy(info->name, card->shortname, sizeof(info->name));
894 	strscpy(info->longname, card->longname, sizeof(info->longname));
895 	strscpy(info->mixername, card->mixername, sizeof(info->mixername));
896 	strscpy(info->components, card->components, sizeof(info->components));
897 	up_read(&snd_ioctl_rwsem);
898 	if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
899 		kfree(info);
900 		return -EFAULT;
901 	}
902 	kfree(info);
903 	return 0;
904 }
905 
906 static int snd_ctl_elem_list(struct snd_card *card,
907 			     struct snd_ctl_elem_list *list)
908 {
909 	struct snd_kcontrol *kctl;
910 	struct snd_ctl_elem_id id;
911 	unsigned int offset, space, jidx;
912 	int err = 0;
913 
914 	offset = list->offset;
915 	space = list->space;
916 
917 	down_read(&card->controls_rwsem);
918 	list->count = card->controls_count;
919 	list->used = 0;
920 	if (space > 0) {
921 		list_for_each_entry(kctl, &card->controls, list) {
922 			if (offset >= kctl->count) {
923 				offset -= kctl->count;
924 				continue;
925 			}
926 			for (jidx = offset; jidx < kctl->count; jidx++) {
927 				snd_ctl_build_ioff(&id, kctl, jidx);
928 				if (copy_to_user(list->pids + list->used, &id,
929 						 sizeof(id))) {
930 					err = -EFAULT;
931 					goto out;
932 				}
933 				list->used++;
934 				if (!--space)
935 					goto out;
936 			}
937 			offset = 0;
938 		}
939 	}
940  out:
941 	up_read(&card->controls_rwsem);
942 	return err;
943 }
944 
945 static int snd_ctl_elem_list_user(struct snd_card *card,
946 				  struct snd_ctl_elem_list __user *_list)
947 {
948 	struct snd_ctl_elem_list list;
949 	int err;
950 
951 	if (copy_from_user(&list, _list, sizeof(list)))
952 		return -EFAULT;
953 	err = snd_ctl_elem_list(card, &list);
954 	if (err)
955 		return err;
956 	if (copy_to_user(_list, &list, sizeof(list)))
957 		return -EFAULT;
958 
959 	return 0;
960 }
961 
962 /* Check whether the given kctl info is valid */
963 static int snd_ctl_check_elem_info(struct snd_card *card,
964 				   const struct snd_ctl_elem_info *info)
965 {
966 	static const unsigned int max_value_counts[] = {
967 		[SNDRV_CTL_ELEM_TYPE_BOOLEAN]	= 128,
968 		[SNDRV_CTL_ELEM_TYPE_INTEGER]	= 128,
969 		[SNDRV_CTL_ELEM_TYPE_ENUMERATED] = 128,
970 		[SNDRV_CTL_ELEM_TYPE_BYTES]	= 512,
971 		[SNDRV_CTL_ELEM_TYPE_IEC958]	= 1,
972 		[SNDRV_CTL_ELEM_TYPE_INTEGER64] = 64,
973 	};
974 
975 	if (info->type < SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
976 	    info->type > SNDRV_CTL_ELEM_TYPE_INTEGER64) {
977 		if (card)
978 			dev_err(card->dev,
979 				"control %i:%i:%i:%s:%i: invalid type %d\n",
980 				info->id.iface, info->id.device,
981 				info->id.subdevice, info->id.name,
982 				info->id.index, info->type);
983 		return -EINVAL;
984 	}
985 	if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED &&
986 	    info->value.enumerated.items == 0) {
987 		if (card)
988 			dev_err(card->dev,
989 				"control %i:%i:%i:%s:%i: zero enum items\n",
990 				info->id.iface, info->id.device,
991 				info->id.subdevice, info->id.name,
992 				info->id.index);
993 		return -EINVAL;
994 	}
995 	if (info->count > max_value_counts[info->type]) {
996 		if (card)
997 			dev_err(card->dev,
998 				"control %i:%i:%i:%s:%i: invalid count %d\n",
999 				info->id.iface, info->id.device,
1000 				info->id.subdevice, info->id.name,
1001 				info->id.index, info->count);
1002 		return -EINVAL;
1003 	}
1004 
1005 	return 0;
1006 }
1007 
1008 /* The capacity of struct snd_ctl_elem_value.value.*/
1009 static const unsigned int value_sizes[] = {
1010 	[SNDRV_CTL_ELEM_TYPE_BOOLEAN]	= sizeof(long),
1011 	[SNDRV_CTL_ELEM_TYPE_INTEGER]	= sizeof(long),
1012 	[SNDRV_CTL_ELEM_TYPE_ENUMERATED] = sizeof(unsigned int),
1013 	[SNDRV_CTL_ELEM_TYPE_BYTES]	= sizeof(unsigned char),
1014 	[SNDRV_CTL_ELEM_TYPE_IEC958]	= sizeof(struct snd_aes_iec958),
1015 	[SNDRV_CTL_ELEM_TYPE_INTEGER64] = sizeof(long long),
1016 };
1017 
1018 /* fill the remaining snd_ctl_elem_value data with the given pattern */
1019 static void fill_remaining_elem_value(struct snd_ctl_elem_value *control,
1020 				      struct snd_ctl_elem_info *info,
1021 				      u32 pattern)
1022 {
1023 	size_t offset = value_sizes[info->type] * info->count;
1024 
1025 	offset = DIV_ROUND_UP(offset, sizeof(u32));
1026 	memset32((u32 *)control->value.bytes.data + offset, pattern,
1027 		 sizeof(control->value) / sizeof(u32) - offset);
1028 }
1029 
1030 /* check whether the given integer ctl value is valid */
1031 static int sanity_check_int_value(struct snd_card *card,
1032 				  const struct snd_ctl_elem_value *control,
1033 				  const struct snd_ctl_elem_info *info,
1034 				  int i, bool print_error)
1035 {
1036 	long long lval, lmin, lmax, lstep;
1037 	u64 rem;
1038 
1039 	switch (info->type) {
1040 	default:
1041 	case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
1042 		lval = control->value.integer.value[i];
1043 		lmin = 0;
1044 		lmax = 1;
1045 		lstep = 0;
1046 		break;
1047 	case SNDRV_CTL_ELEM_TYPE_INTEGER:
1048 		lval = control->value.integer.value[i];
1049 		lmin = info->value.integer.min;
1050 		lmax = info->value.integer.max;
1051 		lstep = info->value.integer.step;
1052 		break;
1053 	case SNDRV_CTL_ELEM_TYPE_INTEGER64:
1054 		lval = control->value.integer64.value[i];
1055 		lmin = info->value.integer64.min;
1056 		lmax = info->value.integer64.max;
1057 		lstep = info->value.integer64.step;
1058 		break;
1059 	case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
1060 		lval = control->value.enumerated.item[i];
1061 		lmin = 0;
1062 		lmax = info->value.enumerated.items - 1;
1063 		lstep = 0;
1064 		break;
1065 	}
1066 
1067 	if (lval < lmin || lval > lmax) {
1068 		if (print_error)
1069 			dev_err(card->dev,
1070 				"control %i:%i:%i:%s:%i: value out of range %lld (%lld/%lld) at count %i\n",
1071 				control->id.iface, control->id.device,
1072 				control->id.subdevice, control->id.name,
1073 				control->id.index, lval, lmin, lmax, i);
1074 		return -EINVAL;
1075 	}
1076 	if (lstep) {
1077 		div64_u64_rem(lval, lstep, &rem);
1078 		if (rem) {
1079 			if (print_error)
1080 				dev_err(card->dev,
1081 					"control %i:%i:%i:%s:%i: unaligned value %lld (step %lld) at count %i\n",
1082 					control->id.iface, control->id.device,
1083 					control->id.subdevice, control->id.name,
1084 					control->id.index, lval, lstep, i);
1085 			return -EINVAL;
1086 		}
1087 	}
1088 
1089 	return 0;
1090 }
1091 
1092 /* check whether the all input values are valid for the given elem value */
1093 static int sanity_check_input_values(struct snd_card *card,
1094 				     const struct snd_ctl_elem_value *control,
1095 				     const struct snd_ctl_elem_info *info,
1096 				     bool print_error)
1097 {
1098 	int i, ret;
1099 
1100 	switch (info->type) {
1101 	case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
1102 	case SNDRV_CTL_ELEM_TYPE_INTEGER:
1103 	case SNDRV_CTL_ELEM_TYPE_INTEGER64:
1104 	case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
1105 		for (i = 0; i < info->count; i++) {
1106 			ret = sanity_check_int_value(card, control, info, i,
1107 						     print_error);
1108 			if (ret < 0)
1109 				return ret;
1110 		}
1111 		break;
1112 	default:
1113 		break;
1114 	}
1115 
1116 	return 0;
1117 }
1118 
1119 /* perform sanity checks to the given snd_ctl_elem_value object */
1120 static int sanity_check_elem_value(struct snd_card *card,
1121 				   const struct snd_ctl_elem_value *control,
1122 				   const struct snd_ctl_elem_info *info,
1123 				   u32 pattern)
1124 {
1125 	size_t offset;
1126 	int ret;
1127 	u32 *p;
1128 
1129 	ret = sanity_check_input_values(card, control, info, true);
1130 	if (ret < 0)
1131 		return ret;
1132 
1133 	/* check whether the remaining area kept untouched */
1134 	offset = value_sizes[info->type] * info->count;
1135 	offset = DIV_ROUND_UP(offset, sizeof(u32));
1136 	p = (u32 *)control->value.bytes.data + offset;
1137 	for (; offset < sizeof(control->value) / sizeof(u32); offset++, p++) {
1138 		if (*p != pattern) {
1139 			ret = -EINVAL;
1140 			break;
1141 		}
1142 		*p = 0; /* clear the checked area */
1143 	}
1144 
1145 	return ret;
1146 }
1147 
1148 static int __snd_ctl_elem_info(struct snd_card *card,
1149 			       struct snd_kcontrol *kctl,
1150 			       struct snd_ctl_elem_info *info,
1151 			       struct snd_ctl_file *ctl)
1152 {
1153 	struct snd_kcontrol_volatile *vd;
1154 	unsigned int index_offset;
1155 	int result;
1156 
1157 #ifdef CONFIG_SND_DEBUG
1158 	info->access = 0;
1159 #endif
1160 	result = snd_power_ref_and_wait(card);
1161 	if (!result)
1162 		result = kctl->info(kctl, info);
1163 	snd_power_unref(card);
1164 	if (result >= 0) {
1165 		snd_BUG_ON(info->access);
1166 		index_offset = snd_ctl_get_ioff(kctl, &info->id);
1167 		vd = &kctl->vd[index_offset];
1168 		snd_ctl_build_ioff(&info->id, kctl, index_offset);
1169 		info->access = vd->access;
1170 		if (vd->owner) {
1171 			info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
1172 			if (vd->owner == ctl)
1173 				info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
1174 			info->owner = pid_vnr(vd->owner->pid);
1175 		} else {
1176 			info->owner = -1;
1177 		}
1178 		if (!snd_ctl_skip_validation(info) &&
1179 		    snd_ctl_check_elem_info(card, info) < 0)
1180 			result = -EINVAL;
1181 	}
1182 	return result;
1183 }
1184 
1185 static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
1186 			     struct snd_ctl_elem_info *info)
1187 {
1188 	struct snd_card *card = ctl->card;
1189 	struct snd_kcontrol *kctl;
1190 	int result;
1191 
1192 	down_read(&card->controls_rwsem);
1193 	kctl = snd_ctl_find_id(card, &info->id);
1194 	if (kctl == NULL)
1195 		result = -ENOENT;
1196 	else
1197 		result = __snd_ctl_elem_info(card, kctl, info, ctl);
1198 	up_read(&card->controls_rwsem);
1199 	return result;
1200 }
1201 
1202 static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
1203 				  struct snd_ctl_elem_info __user *_info)
1204 {
1205 	struct snd_ctl_elem_info info;
1206 	int result;
1207 
1208 	if (copy_from_user(&info, _info, sizeof(info)))
1209 		return -EFAULT;
1210 	result = snd_ctl_elem_info(ctl, &info);
1211 	if (result < 0)
1212 		return result;
1213 	/* drop internal access flags */
1214 	info.access &= ~(SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK|
1215 			 SNDRV_CTL_ELEM_ACCESS_LED_MASK);
1216 	if (copy_to_user(_info, &info, sizeof(info)))
1217 		return -EFAULT;
1218 	return result;
1219 }
1220 
1221 static int snd_ctl_elem_read(struct snd_card *card,
1222 			     struct snd_ctl_elem_value *control)
1223 {
1224 	struct snd_kcontrol *kctl;
1225 	struct snd_kcontrol_volatile *vd;
1226 	unsigned int index_offset;
1227 	struct snd_ctl_elem_info info;
1228 	const u32 pattern = 0xdeadbeef;
1229 	int ret;
1230 
1231 	down_read(&card->controls_rwsem);
1232 	kctl = snd_ctl_find_id(card, &control->id);
1233 	if (kctl == NULL) {
1234 		ret = -ENOENT;
1235 		goto unlock;
1236 	}
1237 
1238 	index_offset = snd_ctl_get_ioff(kctl, &control->id);
1239 	vd = &kctl->vd[index_offset];
1240 	if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_READ) || kctl->get == NULL) {
1241 		ret = -EPERM;
1242 		goto unlock;
1243 	}
1244 
1245 	snd_ctl_build_ioff(&control->id, kctl, index_offset);
1246 
1247 #ifdef CONFIG_SND_CTL_DEBUG
1248 	/* info is needed only for validation */
1249 	memset(&info, 0, sizeof(info));
1250 	info.id = control->id;
1251 	ret = __snd_ctl_elem_info(card, kctl, &info, NULL);
1252 	if (ret < 0)
1253 		goto unlock;
1254 #endif
1255 
1256 	if (!snd_ctl_skip_validation(&info))
1257 		fill_remaining_elem_value(control, &info, pattern);
1258 	ret = snd_power_ref_and_wait(card);
1259 	if (!ret)
1260 		ret = kctl->get(kctl, control);
1261 	snd_power_unref(card);
1262 	if (ret < 0)
1263 		goto unlock;
1264 	if (!snd_ctl_skip_validation(&info) &&
1265 	    sanity_check_elem_value(card, control, &info, pattern) < 0) {
1266 		dev_err(card->dev,
1267 			"control %i:%i:%i:%s:%i: access overflow\n",
1268 			control->id.iface, control->id.device,
1269 			control->id.subdevice, control->id.name,
1270 			control->id.index);
1271 		ret = -EINVAL;
1272 		goto unlock;
1273 	}
1274 unlock:
1275 	up_read(&card->controls_rwsem);
1276 	return ret;
1277 }
1278 
1279 static int snd_ctl_elem_read_user(struct snd_card *card,
1280 				  struct snd_ctl_elem_value __user *_control)
1281 {
1282 	struct snd_ctl_elem_value *control;
1283 	int result;
1284 
1285 	control = memdup_user(_control, sizeof(*control));
1286 	if (IS_ERR(control))
1287 		return PTR_ERR(control);
1288 
1289 	result = snd_ctl_elem_read(card, control);
1290 	if (result < 0)
1291 		goto error;
1292 
1293 	if (copy_to_user(_control, control, sizeof(*control)))
1294 		result = -EFAULT;
1295  error:
1296 	kfree(control);
1297 	return result;
1298 }
1299 
1300 static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
1301 			      struct snd_ctl_elem_value *control)
1302 {
1303 	struct snd_kcontrol *kctl;
1304 	struct snd_kcontrol_volatile *vd;
1305 	unsigned int index_offset;
1306 	int result;
1307 
1308 	down_write(&card->controls_rwsem);
1309 	kctl = snd_ctl_find_id(card, &control->id);
1310 	if (kctl == NULL) {
1311 		up_write(&card->controls_rwsem);
1312 		return -ENOENT;
1313 	}
1314 
1315 	index_offset = snd_ctl_get_ioff(kctl, &control->id);
1316 	vd = &kctl->vd[index_offset];
1317 	if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) || kctl->put == NULL ||
1318 	    (file && vd->owner && vd->owner != file)) {
1319 		up_write(&card->controls_rwsem);
1320 		return -EPERM;
1321 	}
1322 
1323 	snd_ctl_build_ioff(&control->id, kctl, index_offset);
1324 	result = snd_power_ref_and_wait(card);
1325 	/* validate input values */
1326 	if (IS_ENABLED(CONFIG_SND_CTL_INPUT_VALIDATION) && !result) {
1327 		struct snd_ctl_elem_info info;
1328 
1329 		memset(&info, 0, sizeof(info));
1330 		info.id = control->id;
1331 		result = __snd_ctl_elem_info(card, kctl, &info, NULL);
1332 		if (!result)
1333 			result = sanity_check_input_values(card, control, &info,
1334 							   false);
1335 	}
1336 	if (!result)
1337 		result = kctl->put(kctl, control);
1338 	snd_power_unref(card);
1339 	if (result < 0) {
1340 		up_write(&card->controls_rwsem);
1341 		return result;
1342 	}
1343 
1344 	if (result > 0) {
1345 		downgrade_write(&card->controls_rwsem);
1346 		snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_VALUE, kctl, index_offset);
1347 		up_read(&card->controls_rwsem);
1348 	} else {
1349 		up_write(&card->controls_rwsem);
1350 	}
1351 
1352 	return 0;
1353 }
1354 
1355 static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
1356 				   struct snd_ctl_elem_value __user *_control)
1357 {
1358 	struct snd_ctl_elem_value *control;
1359 	struct snd_card *card;
1360 	int result;
1361 
1362 	control = memdup_user(_control, sizeof(*control));
1363 	if (IS_ERR(control))
1364 		return PTR_ERR(control);
1365 
1366 	card = file->card;
1367 	result = snd_ctl_elem_write(card, file, control);
1368 	if (result < 0)
1369 		goto error;
1370 
1371 	if (copy_to_user(_control, control, sizeof(*control)))
1372 		result = -EFAULT;
1373  error:
1374 	kfree(control);
1375 	return result;
1376 }
1377 
1378 static int snd_ctl_elem_lock(struct snd_ctl_file *file,
1379 			     struct snd_ctl_elem_id __user *_id)
1380 {
1381 	struct snd_card *card = file->card;
1382 	struct snd_ctl_elem_id id;
1383 	struct snd_kcontrol *kctl;
1384 	struct snd_kcontrol_volatile *vd;
1385 	int result;
1386 
1387 	if (copy_from_user(&id, _id, sizeof(id)))
1388 		return -EFAULT;
1389 	down_write(&card->controls_rwsem);
1390 	kctl = snd_ctl_find_id(card, &id);
1391 	if (kctl == NULL) {
1392 		result = -ENOENT;
1393 	} else {
1394 		vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1395 		if (vd->owner != NULL)
1396 			result = -EBUSY;
1397 		else {
1398 			vd->owner = file;
1399 			result = 0;
1400 		}
1401 	}
1402 	up_write(&card->controls_rwsem);
1403 	return result;
1404 }
1405 
1406 static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
1407 			       struct snd_ctl_elem_id __user *_id)
1408 {
1409 	struct snd_card *card = file->card;
1410 	struct snd_ctl_elem_id id;
1411 	struct snd_kcontrol *kctl;
1412 	struct snd_kcontrol_volatile *vd;
1413 	int result;
1414 
1415 	if (copy_from_user(&id, _id, sizeof(id)))
1416 		return -EFAULT;
1417 	down_write(&card->controls_rwsem);
1418 	kctl = snd_ctl_find_id(card, &id);
1419 	if (kctl == NULL) {
1420 		result = -ENOENT;
1421 	} else {
1422 		vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1423 		if (vd->owner == NULL)
1424 			result = -EINVAL;
1425 		else if (vd->owner != file)
1426 			result = -EPERM;
1427 		else {
1428 			vd->owner = NULL;
1429 			result = 0;
1430 		}
1431 	}
1432 	up_write(&card->controls_rwsem);
1433 	return result;
1434 }
1435 
1436 struct user_element {
1437 	struct snd_ctl_elem_info info;
1438 	struct snd_card *card;
1439 	char *elem_data;		/* element data */
1440 	unsigned long elem_data_size;	/* size of element data in bytes */
1441 	void *tlv_data;			/* TLV data */
1442 	unsigned long tlv_data_size;	/* TLV data size */
1443 	void *priv_data;		/* private data (like strings for enumerated type) */
1444 };
1445 
1446 // check whether the addition (in bytes) of user ctl element may overflow the limit.
1447 static bool check_user_elem_overflow(struct snd_card *card, ssize_t add)
1448 {
1449 	return (ssize_t)card->user_ctl_alloc_size + add > max_user_ctl_alloc_size;
1450 }
1451 
1452 static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
1453 				  struct snd_ctl_elem_info *uinfo)
1454 {
1455 	struct user_element *ue = kcontrol->private_data;
1456 	unsigned int offset;
1457 
1458 	offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
1459 	*uinfo = ue->info;
1460 	snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
1461 
1462 	return 0;
1463 }
1464 
1465 static int snd_ctl_elem_user_enum_info(struct snd_kcontrol *kcontrol,
1466 				       struct snd_ctl_elem_info *uinfo)
1467 {
1468 	struct user_element *ue = kcontrol->private_data;
1469 	const char *names;
1470 	unsigned int item;
1471 	unsigned int offset;
1472 
1473 	item = uinfo->value.enumerated.item;
1474 
1475 	offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
1476 	*uinfo = ue->info;
1477 	snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
1478 
1479 	item = min(item, uinfo->value.enumerated.items - 1);
1480 	uinfo->value.enumerated.item = item;
1481 
1482 	names = ue->priv_data;
1483 	for (; item > 0; --item)
1484 		names += strlen(names) + 1;
1485 	strcpy(uinfo->value.enumerated.name, names);
1486 
1487 	return 0;
1488 }
1489 
1490 static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
1491 				 struct snd_ctl_elem_value *ucontrol)
1492 {
1493 	struct user_element *ue = kcontrol->private_data;
1494 	unsigned int size = ue->elem_data_size;
1495 	char *src = ue->elem_data +
1496 			snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
1497 
1498 	memcpy(&ucontrol->value, src, size);
1499 	return 0;
1500 }
1501 
1502 static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
1503 				 struct snd_ctl_elem_value *ucontrol)
1504 {
1505 	int change;
1506 	struct user_element *ue = kcontrol->private_data;
1507 	unsigned int size = ue->elem_data_size;
1508 	char *dst = ue->elem_data +
1509 			snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
1510 
1511 	change = memcmp(&ucontrol->value, dst, size) != 0;
1512 	if (change)
1513 		memcpy(dst, &ucontrol->value, size);
1514 	return change;
1515 }
1516 
1517 /* called in controls_rwsem write lock */
1518 static int replace_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
1519 			    unsigned int size)
1520 {
1521 	struct user_element *ue = kctl->private_data;
1522 	unsigned int *container;
1523 	unsigned int mask = 0;
1524 	int i;
1525 	int change;
1526 
1527 	if (size > 1024 * 128)	/* sane value */
1528 		return -EINVAL;
1529 
1530 	// does the TLV size change cause overflow?
1531 	if (check_user_elem_overflow(ue->card, (ssize_t)(size - ue->tlv_data_size)))
1532 		return -ENOMEM;
1533 
1534 	container = vmemdup_user(buf, size);
1535 	if (IS_ERR(container))
1536 		return PTR_ERR(container);
1537 
1538 	change = ue->tlv_data_size != size;
1539 	if (!change)
1540 		change = memcmp(ue->tlv_data, container, size) != 0;
1541 	if (!change) {
1542 		kvfree(container);
1543 		return 0;
1544 	}
1545 
1546 	if (ue->tlv_data == NULL) {
1547 		/* Now TLV data is available. */
1548 		for (i = 0; i < kctl->count; ++i)
1549 			kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1550 		mask = SNDRV_CTL_EVENT_MASK_INFO;
1551 	} else {
1552 		ue->card->user_ctl_alloc_size -= ue->tlv_data_size;
1553 		ue->tlv_data_size = 0;
1554 		kvfree(ue->tlv_data);
1555 	}
1556 
1557 	ue->tlv_data = container;
1558 	ue->tlv_data_size = size;
1559 	// decremented at private_free.
1560 	ue->card->user_ctl_alloc_size += size;
1561 
1562 	mask |= SNDRV_CTL_EVENT_MASK_TLV;
1563 	for (i = 0; i < kctl->count; ++i)
1564 		snd_ctl_notify_one(ue->card, mask, kctl, i);
1565 
1566 	return change;
1567 }
1568 
1569 static int read_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
1570 			 unsigned int size)
1571 {
1572 	struct user_element *ue = kctl->private_data;
1573 
1574 	if (ue->tlv_data_size == 0 || ue->tlv_data == NULL)
1575 		return -ENXIO;
1576 
1577 	if (size < ue->tlv_data_size)
1578 		return -ENOSPC;
1579 
1580 	if (copy_to_user(buf, ue->tlv_data, ue->tlv_data_size))
1581 		return -EFAULT;
1582 
1583 	return 0;
1584 }
1585 
1586 static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kctl, int op_flag,
1587 				 unsigned int size, unsigned int __user *buf)
1588 {
1589 	if (op_flag == SNDRV_CTL_TLV_OP_WRITE)
1590 		return replace_user_tlv(kctl, buf, size);
1591 	else
1592 		return read_user_tlv(kctl, buf, size);
1593 }
1594 
1595 /* called in controls_rwsem write lock */
1596 static int snd_ctl_elem_init_enum_names(struct user_element *ue)
1597 {
1598 	char *names, *p;
1599 	size_t buf_len, name_len;
1600 	unsigned int i;
1601 	const uintptr_t user_ptrval = ue->info.value.enumerated.names_ptr;
1602 
1603 	buf_len = ue->info.value.enumerated.names_length;
1604 	if (buf_len > 64 * 1024)
1605 		return -EINVAL;
1606 
1607 	if (check_user_elem_overflow(ue->card, buf_len))
1608 		return -ENOMEM;
1609 	names = vmemdup_user((const void __user *)user_ptrval, buf_len);
1610 	if (IS_ERR(names))
1611 		return PTR_ERR(names);
1612 
1613 	/* check that there are enough valid names */
1614 	p = names;
1615 	for (i = 0; i < ue->info.value.enumerated.items; ++i) {
1616 		name_len = strnlen(p, buf_len);
1617 		if (name_len == 0 || name_len >= 64 || name_len == buf_len) {
1618 			kvfree(names);
1619 			return -EINVAL;
1620 		}
1621 		p += name_len + 1;
1622 		buf_len -= name_len + 1;
1623 	}
1624 
1625 	ue->priv_data = names;
1626 	ue->info.value.enumerated.names_ptr = 0;
1627 	// increment the allocation size; decremented again at private_free.
1628 	ue->card->user_ctl_alloc_size += ue->info.value.enumerated.names_length;
1629 
1630 	return 0;
1631 }
1632 
1633 static size_t compute_user_elem_size(size_t size, unsigned int count)
1634 {
1635 	return sizeof(struct user_element) + size * count;
1636 }
1637 
1638 static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
1639 {
1640 	struct user_element *ue = kcontrol->private_data;
1641 
1642 	// decrement the allocation size.
1643 	ue->card->user_ctl_alloc_size -= compute_user_elem_size(ue->elem_data_size, kcontrol->count);
1644 	ue->card->user_ctl_alloc_size -= ue->tlv_data_size;
1645 	if (ue->priv_data)
1646 		ue->card->user_ctl_alloc_size -= ue->info.value.enumerated.names_length;
1647 
1648 	kvfree(ue->tlv_data);
1649 	kvfree(ue->priv_data);
1650 	kfree(ue);
1651 }
1652 
1653 static int snd_ctl_elem_add(struct snd_ctl_file *file,
1654 			    struct snd_ctl_elem_info *info, int replace)
1655 {
1656 	struct snd_card *card = file->card;
1657 	struct snd_kcontrol *kctl;
1658 	unsigned int count;
1659 	unsigned int access;
1660 	long private_size;
1661 	size_t alloc_size;
1662 	struct user_element *ue;
1663 	unsigned int offset;
1664 	int err;
1665 
1666 	if (!*info->id.name)
1667 		return -EINVAL;
1668 	if (strnlen(info->id.name, sizeof(info->id.name)) >= sizeof(info->id.name))
1669 		return -EINVAL;
1670 
1671 	/* Delete a control to replace them if needed. */
1672 	if (replace) {
1673 		info->id.numid = 0;
1674 		err = snd_ctl_remove_user_ctl(file, &info->id);
1675 		if (err)
1676 			return err;
1677 	}
1678 
1679 	/* Check the number of elements for this userspace control. */
1680 	count = info->owner;
1681 	if (count == 0)
1682 		count = 1;
1683 
1684 	/* Arrange access permissions if needed. */
1685 	access = info->access;
1686 	if (access == 0)
1687 		access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1688 	access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1689 		   SNDRV_CTL_ELEM_ACCESS_INACTIVE |
1690 		   SNDRV_CTL_ELEM_ACCESS_TLV_WRITE);
1691 
1692 	/* In initial state, nothing is available as TLV container. */
1693 	if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
1694 		access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1695 	access |= SNDRV_CTL_ELEM_ACCESS_USER;
1696 
1697 	/*
1698 	 * Check information and calculate the size of data specific to
1699 	 * this userspace control.
1700 	 */
1701 	/* pass NULL to card for suppressing error messages */
1702 	err = snd_ctl_check_elem_info(NULL, info);
1703 	if (err < 0)
1704 		return err;
1705 	/* user-space control doesn't allow zero-size data */
1706 	if (info->count < 1)
1707 		return -EINVAL;
1708 	private_size = value_sizes[info->type] * info->count;
1709 	alloc_size = compute_user_elem_size(private_size, count);
1710 
1711 	down_write(&card->controls_rwsem);
1712 	if (check_user_elem_overflow(card, alloc_size)) {
1713 		err = -ENOMEM;
1714 		goto unlock;
1715 	}
1716 
1717 	/*
1718 	 * Keep memory object for this userspace control. After passing this
1719 	 * code block, the instance should be freed by snd_ctl_free_one().
1720 	 *
1721 	 * Note that these elements in this control are locked.
1722 	 */
1723 	err = snd_ctl_new(&kctl, count, access, file);
1724 	if (err < 0)
1725 		goto unlock;
1726 	memcpy(&kctl->id, &info->id, sizeof(kctl->id));
1727 	ue = kzalloc(alloc_size, GFP_KERNEL);
1728 	if (!ue) {
1729 		kfree(kctl);
1730 		err = -ENOMEM;
1731 		goto unlock;
1732 	}
1733 	kctl->private_data = ue;
1734 	kctl->private_free = snd_ctl_elem_user_free;
1735 
1736 	// increment the allocated size; decremented again at private_free.
1737 	card->user_ctl_alloc_size += alloc_size;
1738 
1739 	/* Set private data for this userspace control. */
1740 	ue->card = card;
1741 	ue->info = *info;
1742 	ue->info.access = 0;
1743 	ue->elem_data = (char *)ue + sizeof(*ue);
1744 	ue->elem_data_size = private_size;
1745 	if (ue->info.type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
1746 		err = snd_ctl_elem_init_enum_names(ue);
1747 		if (err < 0) {
1748 			snd_ctl_free_one(kctl);
1749 			goto unlock;
1750 		}
1751 	}
1752 
1753 	/* Set callback functions. */
1754 	if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED)
1755 		kctl->info = snd_ctl_elem_user_enum_info;
1756 	else
1757 		kctl->info = snd_ctl_elem_user_info;
1758 	if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1759 		kctl->get = snd_ctl_elem_user_get;
1760 	if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1761 		kctl->put = snd_ctl_elem_user_put;
1762 	if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
1763 		kctl->tlv.c = snd_ctl_elem_user_tlv;
1764 
1765 	/* This function manage to free the instance on failure. */
1766 	err = __snd_ctl_add_replace(card, kctl, CTL_ADD_EXCLUSIVE);
1767 	if (err < 0) {
1768 		snd_ctl_free_one(kctl);
1769 		goto unlock;
1770 	}
1771 	offset = snd_ctl_get_ioff(kctl, &info->id);
1772 	snd_ctl_build_ioff(&info->id, kctl, offset);
1773 	/*
1774 	 * Here we cannot fill any field for the number of elements added by
1775 	 * this operation because there're no specific fields. The usage of
1776 	 * 'owner' field for this purpose may cause any bugs to userspace
1777 	 * applications because the field originally means PID of a process
1778 	 * which locks the element.
1779 	 */
1780  unlock:
1781 	up_write(&card->controls_rwsem);
1782 	return err;
1783 }
1784 
1785 static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1786 				 struct snd_ctl_elem_info __user *_info, int replace)
1787 {
1788 	struct snd_ctl_elem_info info;
1789 	int err;
1790 
1791 	if (copy_from_user(&info, _info, sizeof(info)))
1792 		return -EFAULT;
1793 	err = snd_ctl_elem_add(file, &info, replace);
1794 	if (err < 0)
1795 		return err;
1796 	if (copy_to_user(_info, &info, sizeof(info))) {
1797 		snd_ctl_remove_user_ctl(file, &info.id);
1798 		return -EFAULT;
1799 	}
1800 
1801 	return 0;
1802 }
1803 
1804 static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1805 			       struct snd_ctl_elem_id __user *_id)
1806 {
1807 	struct snd_ctl_elem_id id;
1808 
1809 	if (copy_from_user(&id, _id, sizeof(id)))
1810 		return -EFAULT;
1811 	return snd_ctl_remove_user_ctl(file, &id);
1812 }
1813 
1814 static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
1815 {
1816 	int subscribe;
1817 	if (get_user(subscribe, ptr))
1818 		return -EFAULT;
1819 	if (subscribe < 0) {
1820 		subscribe = file->subscribed;
1821 		if (put_user(subscribe, ptr))
1822 			return -EFAULT;
1823 		return 0;
1824 	}
1825 	if (subscribe) {
1826 		file->subscribed = 1;
1827 		return 0;
1828 	} else if (file->subscribed) {
1829 		snd_ctl_empty_read_queue(file);
1830 		file->subscribed = 0;
1831 	}
1832 	return 0;
1833 }
1834 
1835 static int call_tlv_handler(struct snd_ctl_file *file, int op_flag,
1836 			    struct snd_kcontrol *kctl,
1837 			    struct snd_ctl_elem_id *id,
1838 			    unsigned int __user *buf, unsigned int size)
1839 {
1840 	static const struct {
1841 		int op;
1842 		int perm;
1843 	} pairs[] = {
1844 		{SNDRV_CTL_TLV_OP_READ,  SNDRV_CTL_ELEM_ACCESS_TLV_READ},
1845 		{SNDRV_CTL_TLV_OP_WRITE, SNDRV_CTL_ELEM_ACCESS_TLV_WRITE},
1846 		{SNDRV_CTL_TLV_OP_CMD,   SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND},
1847 	};
1848 	struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
1849 	int i, ret;
1850 
1851 	/* Check support of the request for this element. */
1852 	for (i = 0; i < ARRAY_SIZE(pairs); ++i) {
1853 		if (op_flag == pairs[i].op && (vd->access & pairs[i].perm))
1854 			break;
1855 	}
1856 	if (i == ARRAY_SIZE(pairs))
1857 		return -ENXIO;
1858 
1859 	if (kctl->tlv.c == NULL)
1860 		return -ENXIO;
1861 
1862 	/* Write and command operations are not allowed for locked element. */
1863 	if (op_flag != SNDRV_CTL_TLV_OP_READ &&
1864 	    vd->owner != NULL && vd->owner != file)
1865 		return -EPERM;
1866 
1867 	ret = snd_power_ref_and_wait(file->card);
1868 	if (!ret)
1869 		ret = kctl->tlv.c(kctl, op_flag, size, buf);
1870 	snd_power_unref(file->card);
1871 	return ret;
1872 }
1873 
1874 static int read_tlv_buf(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id,
1875 			unsigned int __user *buf, unsigned int size)
1876 {
1877 	struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
1878 	unsigned int len;
1879 
1880 	if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ))
1881 		return -ENXIO;
1882 
1883 	if (kctl->tlv.p == NULL)
1884 		return -ENXIO;
1885 
1886 	len = sizeof(unsigned int) * 2 + kctl->tlv.p[1];
1887 	if (size < len)
1888 		return -ENOMEM;
1889 
1890 	if (copy_to_user(buf, kctl->tlv.p, len))
1891 		return -EFAULT;
1892 
1893 	return 0;
1894 }
1895 
1896 static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1897 			     struct snd_ctl_tlv __user *buf,
1898                              int op_flag)
1899 {
1900 	struct snd_ctl_tlv header;
1901 	unsigned int __user *container;
1902 	unsigned int container_size;
1903 	struct snd_kcontrol *kctl;
1904 	struct snd_ctl_elem_id id;
1905 	struct snd_kcontrol_volatile *vd;
1906 
1907 	if (copy_from_user(&header, buf, sizeof(header)))
1908 		return -EFAULT;
1909 
1910 	/* In design of control core, numerical ID starts at 1. */
1911 	if (header.numid == 0)
1912 		return -EINVAL;
1913 
1914 	/* At least, container should include type and length fields.  */
1915 	if (header.length < sizeof(unsigned int) * 2)
1916 		return -EINVAL;
1917 	container_size = header.length;
1918 	container = buf->tlv;
1919 
1920 	kctl = snd_ctl_find_numid(file->card, header.numid);
1921 	if (kctl == NULL)
1922 		return -ENOENT;
1923 
1924 	/* Calculate index of the element in this set. */
1925 	id = kctl->id;
1926 	snd_ctl_build_ioff(&id, kctl, header.numid - id.numid);
1927 	vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1928 
1929 	if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1930 		return call_tlv_handler(file, op_flag, kctl, &id, container,
1931 					container_size);
1932 	} else {
1933 		if (op_flag == SNDRV_CTL_TLV_OP_READ) {
1934 			return read_tlv_buf(kctl, &id, container,
1935 					    container_size);
1936 		}
1937 	}
1938 
1939 	/* Not supported. */
1940 	return -ENXIO;
1941 }
1942 
1943 static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1944 {
1945 	struct snd_ctl_file *ctl;
1946 	struct snd_card *card;
1947 	struct snd_kctl_ioctl *p;
1948 	void __user *argp = (void __user *)arg;
1949 	int __user *ip = argp;
1950 	int err;
1951 
1952 	ctl = file->private_data;
1953 	card = ctl->card;
1954 	if (snd_BUG_ON(!card))
1955 		return -ENXIO;
1956 	switch (cmd) {
1957 	case SNDRV_CTL_IOCTL_PVERSION:
1958 		return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1959 	case SNDRV_CTL_IOCTL_CARD_INFO:
1960 		return snd_ctl_card_info(card, ctl, cmd, argp);
1961 	case SNDRV_CTL_IOCTL_ELEM_LIST:
1962 		return snd_ctl_elem_list_user(card, argp);
1963 	case SNDRV_CTL_IOCTL_ELEM_INFO:
1964 		return snd_ctl_elem_info_user(ctl, argp);
1965 	case SNDRV_CTL_IOCTL_ELEM_READ:
1966 		return snd_ctl_elem_read_user(card, argp);
1967 	case SNDRV_CTL_IOCTL_ELEM_WRITE:
1968 		return snd_ctl_elem_write_user(ctl, argp);
1969 	case SNDRV_CTL_IOCTL_ELEM_LOCK:
1970 		return snd_ctl_elem_lock(ctl, argp);
1971 	case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1972 		return snd_ctl_elem_unlock(ctl, argp);
1973 	case SNDRV_CTL_IOCTL_ELEM_ADD:
1974 		return snd_ctl_elem_add_user(ctl, argp, 0);
1975 	case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1976 		return snd_ctl_elem_add_user(ctl, argp, 1);
1977 	case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1978 		return snd_ctl_elem_remove(ctl, argp);
1979 	case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1980 		return snd_ctl_subscribe_events(ctl, ip);
1981 	case SNDRV_CTL_IOCTL_TLV_READ:
1982 		down_read(&ctl->card->controls_rwsem);
1983 		err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_READ);
1984 		up_read(&ctl->card->controls_rwsem);
1985 		return err;
1986 	case SNDRV_CTL_IOCTL_TLV_WRITE:
1987 		down_write(&ctl->card->controls_rwsem);
1988 		err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_WRITE);
1989 		up_write(&ctl->card->controls_rwsem);
1990 		return err;
1991 	case SNDRV_CTL_IOCTL_TLV_COMMAND:
1992 		down_write(&ctl->card->controls_rwsem);
1993 		err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_CMD);
1994 		up_write(&ctl->card->controls_rwsem);
1995 		return err;
1996 	case SNDRV_CTL_IOCTL_POWER:
1997 		return -ENOPROTOOPT;
1998 	case SNDRV_CTL_IOCTL_POWER_STATE:
1999 		return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
2000 	}
2001 	down_read(&snd_ioctl_rwsem);
2002 	list_for_each_entry(p, &snd_control_ioctls, list) {
2003 		err = p->fioctl(card, ctl, cmd, arg);
2004 		if (err != -ENOIOCTLCMD) {
2005 			up_read(&snd_ioctl_rwsem);
2006 			return err;
2007 		}
2008 	}
2009 	up_read(&snd_ioctl_rwsem);
2010 	dev_dbg(card->dev, "unknown ioctl = 0x%x\n", cmd);
2011 	return -ENOTTY;
2012 }
2013 
2014 static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
2015 			    size_t count, loff_t * offset)
2016 {
2017 	struct snd_ctl_file *ctl;
2018 	int err = 0;
2019 	ssize_t result = 0;
2020 
2021 	ctl = file->private_data;
2022 	if (snd_BUG_ON(!ctl || !ctl->card))
2023 		return -ENXIO;
2024 	if (!ctl->subscribed)
2025 		return -EBADFD;
2026 	if (count < sizeof(struct snd_ctl_event))
2027 		return -EINVAL;
2028 	spin_lock_irq(&ctl->read_lock);
2029 	while (count >= sizeof(struct snd_ctl_event)) {
2030 		struct snd_ctl_event ev;
2031 		struct snd_kctl_event *kev;
2032 		while (list_empty(&ctl->events)) {
2033 			wait_queue_entry_t wait;
2034 			if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
2035 				err = -EAGAIN;
2036 				goto __end_lock;
2037 			}
2038 			init_waitqueue_entry(&wait, current);
2039 			add_wait_queue(&ctl->change_sleep, &wait);
2040 			set_current_state(TASK_INTERRUPTIBLE);
2041 			spin_unlock_irq(&ctl->read_lock);
2042 			schedule();
2043 			remove_wait_queue(&ctl->change_sleep, &wait);
2044 			if (ctl->card->shutdown)
2045 				return -ENODEV;
2046 			if (signal_pending(current))
2047 				return -ERESTARTSYS;
2048 			spin_lock_irq(&ctl->read_lock);
2049 		}
2050 		kev = snd_kctl_event(ctl->events.next);
2051 		ev.type = SNDRV_CTL_EVENT_ELEM;
2052 		ev.data.elem.mask = kev->mask;
2053 		ev.data.elem.id = kev->id;
2054 		list_del(&kev->list);
2055 		spin_unlock_irq(&ctl->read_lock);
2056 		kfree(kev);
2057 		if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
2058 			err = -EFAULT;
2059 			goto __end;
2060 		}
2061 		spin_lock_irq(&ctl->read_lock);
2062 		buffer += sizeof(struct snd_ctl_event);
2063 		count -= sizeof(struct snd_ctl_event);
2064 		result += sizeof(struct snd_ctl_event);
2065 	}
2066       __end_lock:
2067 	spin_unlock_irq(&ctl->read_lock);
2068       __end:
2069       	return result > 0 ? result : err;
2070 }
2071 
2072 static __poll_t snd_ctl_poll(struct file *file, poll_table * wait)
2073 {
2074 	__poll_t mask;
2075 	struct snd_ctl_file *ctl;
2076 
2077 	ctl = file->private_data;
2078 	if (!ctl->subscribed)
2079 		return 0;
2080 	poll_wait(file, &ctl->change_sleep, wait);
2081 
2082 	mask = 0;
2083 	if (!list_empty(&ctl->events))
2084 		mask |= EPOLLIN | EPOLLRDNORM;
2085 
2086 	return mask;
2087 }
2088 
2089 /*
2090  * register the device-specific control-ioctls.
2091  * called from each device manager like pcm.c, hwdep.c, etc.
2092  */
2093 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
2094 {
2095 	struct snd_kctl_ioctl *pn;
2096 
2097 	pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
2098 	if (pn == NULL)
2099 		return -ENOMEM;
2100 	pn->fioctl = fcn;
2101 	down_write(&snd_ioctl_rwsem);
2102 	list_add_tail(&pn->list, lists);
2103 	up_write(&snd_ioctl_rwsem);
2104 	return 0;
2105 }
2106 
2107 /**
2108  * snd_ctl_register_ioctl - register the device-specific control-ioctls
2109  * @fcn: ioctl callback function
2110  *
2111  * called from each device manager like pcm.c, hwdep.c, etc.
2112  *
2113  * Return: zero if successful, or a negative error code
2114  */
2115 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
2116 {
2117 	return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
2118 }
2119 EXPORT_SYMBOL(snd_ctl_register_ioctl);
2120 
2121 #ifdef CONFIG_COMPAT
2122 /**
2123  * snd_ctl_register_ioctl_compat - register the device-specific 32bit compat
2124  * control-ioctls
2125  * @fcn: ioctl callback function
2126  *
2127  * Return: zero if successful, or a negative error code
2128  */
2129 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
2130 {
2131 	return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
2132 }
2133 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
2134 #endif
2135 
2136 /*
2137  * de-register the device-specific control-ioctls.
2138  */
2139 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
2140 				     struct list_head *lists)
2141 {
2142 	struct snd_kctl_ioctl *p;
2143 
2144 	if (snd_BUG_ON(!fcn))
2145 		return -EINVAL;
2146 	down_write(&snd_ioctl_rwsem);
2147 	list_for_each_entry(p, lists, list) {
2148 		if (p->fioctl == fcn) {
2149 			list_del(&p->list);
2150 			up_write(&snd_ioctl_rwsem);
2151 			kfree(p);
2152 			return 0;
2153 		}
2154 	}
2155 	up_write(&snd_ioctl_rwsem);
2156 	snd_BUG();
2157 	return -EINVAL;
2158 }
2159 
2160 /**
2161  * snd_ctl_unregister_ioctl - de-register the device-specific control-ioctls
2162  * @fcn: ioctl callback function to unregister
2163  *
2164  * Return: zero if successful, or a negative error code
2165  */
2166 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
2167 {
2168 	return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
2169 }
2170 EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
2171 
2172 #ifdef CONFIG_COMPAT
2173 /**
2174  * snd_ctl_unregister_ioctl_compat - de-register the device-specific compat
2175  * 32bit control-ioctls
2176  * @fcn: ioctl callback function to unregister
2177  *
2178  * Return: zero if successful, or a negative error code
2179  */
2180 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
2181 {
2182 	return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
2183 }
2184 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
2185 #endif
2186 
2187 static int snd_ctl_fasync(int fd, struct file * file, int on)
2188 {
2189 	struct snd_ctl_file *ctl;
2190 
2191 	ctl = file->private_data;
2192 	return snd_fasync_helper(fd, file, on, &ctl->fasync);
2193 }
2194 
2195 /* return the preferred subdevice number if already assigned;
2196  * otherwise return -1
2197  */
2198 int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type)
2199 {
2200 	struct snd_ctl_file *kctl;
2201 	int subdevice = -1;
2202 	unsigned long flags;
2203 
2204 	read_lock_irqsave(&card->ctl_files_rwlock, flags);
2205 	list_for_each_entry(kctl, &card->ctl_files, list) {
2206 		if (kctl->pid == task_pid(current)) {
2207 			subdevice = kctl->preferred_subdevice[type];
2208 			if (subdevice != -1)
2209 				break;
2210 		}
2211 	}
2212 	read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
2213 	return subdevice;
2214 }
2215 EXPORT_SYMBOL_GPL(snd_ctl_get_preferred_subdevice);
2216 
2217 /*
2218  * ioctl32 compat
2219  */
2220 #ifdef CONFIG_COMPAT
2221 #include "control_compat.c"
2222 #else
2223 #define snd_ctl_ioctl_compat	NULL
2224 #endif
2225 
2226 /*
2227  * control layers (audio LED etc.)
2228  */
2229 
2230 /**
2231  * snd_ctl_request_layer - request to use the layer
2232  * @module_name: Name of the kernel module (NULL == build-in)
2233  *
2234  * Return: zero if successful, or an error code when the module cannot be loaded
2235  */
2236 int snd_ctl_request_layer(const char *module_name)
2237 {
2238 	struct snd_ctl_layer_ops *lops;
2239 
2240 	if (module_name == NULL)
2241 		return 0;
2242 	down_read(&snd_ctl_layer_rwsem);
2243 	for (lops = snd_ctl_layer; lops; lops = lops->next)
2244 		if (strcmp(lops->module_name, module_name) == 0)
2245 			break;
2246 	up_read(&snd_ctl_layer_rwsem);
2247 	if (lops)
2248 		return 0;
2249 	return request_module(module_name);
2250 }
2251 EXPORT_SYMBOL_GPL(snd_ctl_request_layer);
2252 
2253 /**
2254  * snd_ctl_register_layer - register new control layer
2255  * @lops: operation structure
2256  *
2257  * The new layer can track all control elements and do additional
2258  * operations on top (like audio LED handling).
2259  */
2260 void snd_ctl_register_layer(struct snd_ctl_layer_ops *lops)
2261 {
2262 	struct snd_card *card;
2263 	int card_number;
2264 
2265 	down_write(&snd_ctl_layer_rwsem);
2266 	lops->next = snd_ctl_layer;
2267 	snd_ctl_layer = lops;
2268 	up_write(&snd_ctl_layer_rwsem);
2269 	for (card_number = 0; card_number < SNDRV_CARDS; card_number++) {
2270 		card = snd_card_ref(card_number);
2271 		if (card) {
2272 			down_read(&card->controls_rwsem);
2273 			lops->lregister(card);
2274 			up_read(&card->controls_rwsem);
2275 			snd_card_unref(card);
2276 		}
2277 	}
2278 }
2279 EXPORT_SYMBOL_GPL(snd_ctl_register_layer);
2280 
2281 /**
2282  * snd_ctl_disconnect_layer - disconnect control layer
2283  * @lops: operation structure
2284  *
2285  * It is expected that the information about tracked cards
2286  * is freed before this call (the disconnect callback is
2287  * not called here).
2288  */
2289 void snd_ctl_disconnect_layer(struct snd_ctl_layer_ops *lops)
2290 {
2291 	struct snd_ctl_layer_ops *lops2, *prev_lops2;
2292 
2293 	down_write(&snd_ctl_layer_rwsem);
2294 	for (lops2 = snd_ctl_layer, prev_lops2 = NULL; lops2; lops2 = lops2->next) {
2295 		if (lops2 == lops) {
2296 			if (!prev_lops2)
2297 				snd_ctl_layer = lops->next;
2298 			else
2299 				prev_lops2->next = lops->next;
2300 			break;
2301 		}
2302 		prev_lops2 = lops2;
2303 	}
2304 	up_write(&snd_ctl_layer_rwsem);
2305 }
2306 EXPORT_SYMBOL_GPL(snd_ctl_disconnect_layer);
2307 
2308 /*
2309  *  INIT PART
2310  */
2311 
2312 static const struct file_operations snd_ctl_f_ops =
2313 {
2314 	.owner =	THIS_MODULE,
2315 	.read =		snd_ctl_read,
2316 	.open =		snd_ctl_open,
2317 	.release =	snd_ctl_release,
2318 	.llseek =	no_llseek,
2319 	.poll =		snd_ctl_poll,
2320 	.unlocked_ioctl =	snd_ctl_ioctl,
2321 	.compat_ioctl =	snd_ctl_ioctl_compat,
2322 	.fasync =	snd_ctl_fasync,
2323 };
2324 
2325 /*
2326  * registration of the control device
2327  */
2328 static int snd_ctl_dev_register(struct snd_device *device)
2329 {
2330 	struct snd_card *card = device->device_data;
2331 	struct snd_ctl_layer_ops *lops;
2332 	int err;
2333 
2334 	err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
2335 				  &snd_ctl_f_ops, card, &card->ctl_dev);
2336 	if (err < 0)
2337 		return err;
2338 	down_read(&card->controls_rwsem);
2339 	down_read(&snd_ctl_layer_rwsem);
2340 	for (lops = snd_ctl_layer; lops; lops = lops->next)
2341 		lops->lregister(card);
2342 	up_read(&snd_ctl_layer_rwsem);
2343 	up_read(&card->controls_rwsem);
2344 	return 0;
2345 }
2346 
2347 /*
2348  * disconnection of the control device
2349  */
2350 static int snd_ctl_dev_disconnect(struct snd_device *device)
2351 {
2352 	struct snd_card *card = device->device_data;
2353 	struct snd_ctl_file *ctl;
2354 	struct snd_ctl_layer_ops *lops;
2355 	unsigned long flags;
2356 
2357 	read_lock_irqsave(&card->ctl_files_rwlock, flags);
2358 	list_for_each_entry(ctl, &card->ctl_files, list) {
2359 		wake_up(&ctl->change_sleep);
2360 		snd_kill_fasync(ctl->fasync, SIGIO, POLL_ERR);
2361 	}
2362 	read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
2363 
2364 	down_read(&card->controls_rwsem);
2365 	down_read(&snd_ctl_layer_rwsem);
2366 	for (lops = snd_ctl_layer; lops; lops = lops->next)
2367 		lops->ldisconnect(card);
2368 	up_read(&snd_ctl_layer_rwsem);
2369 	up_read(&card->controls_rwsem);
2370 
2371 	return snd_unregister_device(&card->ctl_dev);
2372 }
2373 
2374 /*
2375  * free all controls
2376  */
2377 static int snd_ctl_dev_free(struct snd_device *device)
2378 {
2379 	struct snd_card *card = device->device_data;
2380 	struct snd_kcontrol *control;
2381 
2382 	down_write(&card->controls_rwsem);
2383 	while (!list_empty(&card->controls)) {
2384 		control = snd_kcontrol(card->controls.next);
2385 		__snd_ctl_remove(card, control, false);
2386 	}
2387 
2388 #ifdef CONFIG_SND_CTL_FAST_LOOKUP
2389 	xa_destroy(&card->ctl_numids);
2390 	xa_destroy(&card->ctl_hash);
2391 #endif
2392 	up_write(&card->controls_rwsem);
2393 	put_device(&card->ctl_dev);
2394 	return 0;
2395 }
2396 
2397 /*
2398  * create control core:
2399  * called from init.c
2400  */
2401 int snd_ctl_create(struct snd_card *card)
2402 {
2403 	static const struct snd_device_ops ops = {
2404 		.dev_free = snd_ctl_dev_free,
2405 		.dev_register =	snd_ctl_dev_register,
2406 		.dev_disconnect = snd_ctl_dev_disconnect,
2407 	};
2408 	int err;
2409 
2410 	if (snd_BUG_ON(!card))
2411 		return -ENXIO;
2412 	if (snd_BUG_ON(card->number < 0 || card->number >= SNDRV_CARDS))
2413 		return -ENXIO;
2414 
2415 	snd_device_initialize(&card->ctl_dev, card);
2416 	dev_set_name(&card->ctl_dev, "controlC%d", card->number);
2417 
2418 	err = snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
2419 	if (err < 0)
2420 		put_device(&card->ctl_dev);
2421 	return err;
2422 }
2423 
2424 /*
2425  * Frequently used control callbacks/helpers
2426  */
2427 
2428 /**
2429  * snd_ctl_boolean_mono_info - Helper function for a standard boolean info
2430  * callback with a mono channel
2431  * @kcontrol: the kcontrol instance
2432  * @uinfo: info to store
2433  *
2434  * This is a function that can be used as info callback for a standard
2435  * boolean control with a single mono channel.
2436  *
2437  * Return: Zero (always successful)
2438  */
2439 int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
2440 			      struct snd_ctl_elem_info *uinfo)
2441 {
2442 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2443 	uinfo->count = 1;
2444 	uinfo->value.integer.min = 0;
2445 	uinfo->value.integer.max = 1;
2446 	return 0;
2447 }
2448 EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
2449 
2450 /**
2451  * snd_ctl_boolean_stereo_info - Helper function for a standard boolean info
2452  * callback with stereo two channels
2453  * @kcontrol: the kcontrol instance
2454  * @uinfo: info to store
2455  *
2456  * This is a function that can be used as info callback for a standard
2457  * boolean control with stereo two channels.
2458  *
2459  * Return: Zero (always successful)
2460  */
2461 int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
2462 				struct snd_ctl_elem_info *uinfo)
2463 {
2464 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2465 	uinfo->count = 2;
2466 	uinfo->value.integer.min = 0;
2467 	uinfo->value.integer.max = 1;
2468 	return 0;
2469 }
2470 EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
2471 
2472 /**
2473  * snd_ctl_enum_info - fills the info structure for an enumerated control
2474  * @info: the structure to be filled
2475  * @channels: the number of the control's channels; often one
2476  * @items: the number of control values; also the size of @names
2477  * @names: an array containing the names of all control values
2478  *
2479  * Sets all required fields in @info to their appropriate values.
2480  * If the control's accessibility is not the default (readable and writable),
2481  * the caller has to fill @info->access.
2482  *
2483  * Return: Zero (always successful)
2484  */
2485 int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
2486 		      unsigned int items, const char *const names[])
2487 {
2488 	info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2489 	info->count = channels;
2490 	info->value.enumerated.items = items;
2491 	if (!items)
2492 		return 0;
2493 	if (info->value.enumerated.item >= items)
2494 		info->value.enumerated.item = items - 1;
2495 	WARN(strlen(names[info->value.enumerated.item]) >= sizeof(info->value.enumerated.name),
2496 	     "ALSA: too long item name '%s'\n",
2497 	     names[info->value.enumerated.item]);
2498 	strscpy(info->value.enumerated.name,
2499 		names[info->value.enumerated.item],
2500 		sizeof(info->value.enumerated.name));
2501 	return 0;
2502 }
2503 EXPORT_SYMBOL(snd_ctl_enum_info);
2504