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