xref: /linux/sound/core/control.c (revision a8e7ef3cec99ba2487110e01d77a8a278593b3e9)
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 *container;
1554 	unsigned int mask = 0;
1555 	int i;
1556 	int change;
1557 
1558 	lockdep_assert_held_write(&ue->card->controls_rwsem);
1559 
1560 	if (size > 1024 * 128)	/* sane value */
1561 		return -EINVAL;
1562 
1563 	// does the TLV size change cause overflow?
1564 	if (check_user_elem_overflow(ue->card, (ssize_t)(size - ue->tlv_data_size)))
1565 		return -ENOMEM;
1566 
1567 	container = vmemdup_user(buf, size);
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 		kvfree(container);
1576 		return 0;
1577 	}
1578 
1579 	if (ue->tlv_data == NULL) {
1580 		/* Now TLV data is available. */
1581 		for (i = 0; i < kctl->count; ++i)
1582 			kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1583 		mask = SNDRV_CTL_EVENT_MASK_INFO;
1584 	} else {
1585 		ue->card->user_ctl_alloc_size -= ue->tlv_data_size;
1586 		ue->tlv_data_size = 0;
1587 		kvfree(ue->tlv_data);
1588 	}
1589 
1590 	ue->tlv_data = container;
1591 	ue->tlv_data_size = size;
1592 	// decremented at private_free.
1593 	ue->card->user_ctl_alloc_size += size;
1594 
1595 	mask |= SNDRV_CTL_EVENT_MASK_TLV;
1596 	for (i = 0; i < kctl->count; ++i)
1597 		snd_ctl_notify_one(ue->card, mask, kctl, i);
1598 
1599 	return change;
1600 }
1601 
1602 static int read_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
1603 			 unsigned int size)
1604 {
1605 	struct user_element *ue = snd_kcontrol_chip(kctl);
1606 
1607 	if (ue->tlv_data_size == 0 || ue->tlv_data == NULL)
1608 		return -ENXIO;
1609 
1610 	if (size < ue->tlv_data_size)
1611 		return -ENOSPC;
1612 
1613 	if (copy_to_user(buf, ue->tlv_data, ue->tlv_data_size))
1614 		return -EFAULT;
1615 
1616 	return 0;
1617 }
1618 
1619 static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kctl, int op_flag,
1620 				 unsigned int size, unsigned int __user *buf)
1621 {
1622 	if (op_flag == SNDRV_CTL_TLV_OP_WRITE)
1623 		return replace_user_tlv(kctl, buf, size);
1624 	else
1625 		return read_user_tlv(kctl, buf, size);
1626 }
1627 
1628 /* called in controls_rwsem write lock */
1629 static int snd_ctl_elem_init_enum_names(struct user_element *ue)
1630 {
1631 	char *names, *p;
1632 	size_t buf_len, name_len;
1633 	unsigned int i;
1634 	const uintptr_t user_ptrval = ue->info.value.enumerated.names_ptr;
1635 
1636 	lockdep_assert_held_write(&ue->card->controls_rwsem);
1637 
1638 	buf_len = ue->info.value.enumerated.names_length;
1639 	if (buf_len > 64 * 1024)
1640 		return -EINVAL;
1641 
1642 	if (check_user_elem_overflow(ue->card, buf_len))
1643 		return -ENOMEM;
1644 	names = vmemdup_user((const void __user *)user_ptrval, buf_len);
1645 	if (IS_ERR(names))
1646 		return PTR_ERR(names);
1647 
1648 	/* check that there are enough valid names */
1649 	p = names;
1650 	for (i = 0; i < ue->info.value.enumerated.items; ++i) {
1651 		if (buf_len == 0) {
1652 			kvfree(names);
1653 			return -EINVAL;
1654 		}
1655 		name_len = strnlen(p, buf_len);
1656 		if (name_len == 0 || name_len >= 64 || name_len == buf_len) {
1657 			kvfree(names);
1658 			return -EINVAL;
1659 		}
1660 		p += name_len + 1;
1661 		buf_len -= name_len + 1;
1662 	}
1663 
1664 	ue->priv_data = names;
1665 	ue->info.value.enumerated.names_ptr = 0;
1666 	// increment the allocation size; decremented again at private_free.
1667 	ue->card->user_ctl_alloc_size += ue->info.value.enumerated.names_length;
1668 
1669 	return 0;
1670 }
1671 
1672 static size_t compute_user_elem_size(size_t size, unsigned int count)
1673 {
1674 	return sizeof(struct user_element) + size * count;
1675 }
1676 
1677 static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
1678 {
1679 	struct user_element *ue = snd_kcontrol_chip(kcontrol);
1680 
1681 	// decrement the allocation size.
1682 	ue->card->user_ctl_alloc_size -= compute_user_elem_size(ue->elem_data_size, kcontrol->count);
1683 	ue->card->user_ctl_alloc_size -= ue->tlv_data_size;
1684 	if (ue->priv_data)
1685 		ue->card->user_ctl_alloc_size -= ue->info.value.enumerated.names_length;
1686 
1687 	kvfree(ue->tlv_data);
1688 	kvfree(ue->priv_data);
1689 	kfree(ue);
1690 }
1691 
1692 static int snd_ctl_elem_add(struct snd_ctl_file *file,
1693 			    struct snd_ctl_elem_info *info, int replace)
1694 {
1695 	struct snd_card *card = file->card;
1696 	struct snd_kcontrol *kctl;
1697 	unsigned int count;
1698 	unsigned int access;
1699 	long private_size;
1700 	size_t alloc_size;
1701 	struct user_element *ue;
1702 	unsigned int offset;
1703 	int err;
1704 
1705 	if (!*info->id.name)
1706 		return -EINVAL;
1707 	if (strnlen(info->id.name, sizeof(info->id.name)) >= sizeof(info->id.name))
1708 		return -EINVAL;
1709 
1710 	/* Delete a control to replace them if needed. */
1711 	if (replace) {
1712 		info->id.numid = 0;
1713 		err = snd_ctl_remove_user_ctl(file, &info->id);
1714 		if (err)
1715 			return err;
1716 	}
1717 
1718 	/* Check the number of elements for this userspace control. */
1719 	count = info->owner;
1720 	if (count == 0)
1721 		count = 1;
1722 	if (count > MAX_CONTROL_COUNT)
1723 		return -EINVAL;
1724 
1725 	/* Arrange access permissions if needed. */
1726 	access = info->access;
1727 	if (access == 0)
1728 		access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1729 	access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1730 		   SNDRV_CTL_ELEM_ACCESS_INACTIVE |
1731 		   SNDRV_CTL_ELEM_ACCESS_TLV_WRITE);
1732 
1733 	/* In initial state, nothing is available as TLV container. */
1734 	if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
1735 		access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1736 	access |= SNDRV_CTL_ELEM_ACCESS_USER;
1737 
1738 	/*
1739 	 * Check information and calculate the size of data specific to
1740 	 * this userspace control.
1741 	 */
1742 	/* pass NULL to card for suppressing error messages */
1743 	err = snd_ctl_check_elem_info(NULL, info);
1744 	if (err < 0)
1745 		return err;
1746 	/* user-space control doesn't allow zero-size data */
1747 	if (info->count < 1)
1748 		return -EINVAL;
1749 	private_size = value_sizes[info->type] * info->count;
1750 	alloc_size = compute_user_elem_size(private_size, count);
1751 
1752 	guard(rwsem_write)(&card->controls_rwsem);
1753 	if (check_user_elem_overflow(card, alloc_size))
1754 		return -ENOMEM;
1755 
1756 	/*
1757 	 * Keep memory object for this userspace control. After passing this
1758 	 * code block, the instance should be freed by snd_ctl_free_one().
1759 	 *
1760 	 * Note that these elements in this control are locked.
1761 	 */
1762 	err = snd_ctl_new(&kctl, count, access, file);
1763 	if (err < 0)
1764 		return err;
1765 	memcpy(&kctl->id, &info->id, sizeof(kctl->id));
1766 	ue = kzalloc(alloc_size, GFP_KERNEL);
1767 	if (!ue) {
1768 		kfree(kctl);
1769 		return -ENOMEM;
1770 	}
1771 	kctl->private_data = ue;
1772 	kctl->private_free = snd_ctl_elem_user_free;
1773 
1774 	// increment the allocated size; decremented again at private_free.
1775 	card->user_ctl_alloc_size += alloc_size;
1776 
1777 	/* Set private data for this userspace control. */
1778 	ue->card = card;
1779 	ue->info = *info;
1780 	ue->info.access = 0;
1781 	ue->elem_data = (char *)ue + sizeof(*ue);
1782 	ue->elem_data_size = private_size;
1783 	if (ue->info.type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
1784 		err = snd_ctl_elem_init_enum_names(ue);
1785 		if (err < 0) {
1786 			snd_ctl_free_one(kctl);
1787 			return err;
1788 		}
1789 	}
1790 
1791 	/* Set callback functions. */
1792 	if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED)
1793 		kctl->info = snd_ctl_elem_user_enum_info;
1794 	else
1795 		kctl->info = snd_ctl_elem_user_info;
1796 	if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1797 		kctl->get = snd_ctl_elem_user_get;
1798 	if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1799 		kctl->put = snd_ctl_elem_user_put;
1800 	if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
1801 		kctl->tlv.c = snd_ctl_elem_user_tlv;
1802 
1803 	/* This function manage to free the instance on failure. */
1804 	err = __snd_ctl_add_replace(card, kctl, CTL_ADD_EXCLUSIVE);
1805 	if (err < 0) {
1806 		snd_ctl_free_one(kctl);
1807 		return err;
1808 	}
1809 	offset = snd_ctl_get_ioff(kctl, &info->id);
1810 	snd_ctl_build_ioff(&info->id, kctl, offset);
1811 	/*
1812 	 * Here we cannot fill any field for the number of elements added by
1813 	 * this operation because there're no specific fields. The usage of
1814 	 * 'owner' field for this purpose may cause any bugs to userspace
1815 	 * applications because the field originally means PID of a process
1816 	 * which locks the element.
1817 	 */
1818 	return 0;
1819 }
1820 
1821 static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1822 				 struct snd_ctl_elem_info __user *_info, int replace)
1823 {
1824 	struct snd_ctl_elem_info info;
1825 	int err;
1826 
1827 	if (copy_from_user(&info, _info, sizeof(info)))
1828 		return -EFAULT;
1829 	err = snd_ctl_elem_add(file, &info, replace);
1830 	if (err < 0)
1831 		return err;
1832 	if (copy_to_user(_info, &info, sizeof(info))) {
1833 		snd_ctl_remove_user_ctl(file, &info.id);
1834 		return -EFAULT;
1835 	}
1836 
1837 	return 0;
1838 }
1839 
1840 static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1841 			       struct snd_ctl_elem_id __user *_id)
1842 {
1843 	struct snd_ctl_elem_id id;
1844 
1845 	if (copy_from_user(&id, _id, sizeof(id)))
1846 		return -EFAULT;
1847 	return snd_ctl_remove_user_ctl(file, &id);
1848 }
1849 
1850 static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
1851 {
1852 	int subscribe;
1853 	if (get_user(subscribe, ptr))
1854 		return -EFAULT;
1855 	if (subscribe < 0) {
1856 		subscribe = file->subscribed;
1857 		if (put_user(subscribe, ptr))
1858 			return -EFAULT;
1859 		return 0;
1860 	}
1861 	if (subscribe) {
1862 		file->subscribed = 1;
1863 		return 0;
1864 	} else if (file->subscribed) {
1865 		snd_ctl_empty_read_queue(file);
1866 		file->subscribed = 0;
1867 	}
1868 	return 0;
1869 }
1870 
1871 static int call_tlv_handler(struct snd_ctl_file *file, int op_flag,
1872 			    struct snd_kcontrol *kctl,
1873 			    struct snd_ctl_elem_id *id,
1874 			    unsigned int __user *buf, unsigned int size)
1875 {
1876 	static const struct {
1877 		int op;
1878 		int perm;
1879 	} pairs[] = {
1880 		{SNDRV_CTL_TLV_OP_READ,  SNDRV_CTL_ELEM_ACCESS_TLV_READ},
1881 		{SNDRV_CTL_TLV_OP_WRITE, SNDRV_CTL_ELEM_ACCESS_TLV_WRITE},
1882 		{SNDRV_CTL_TLV_OP_CMD,   SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND},
1883 	};
1884 	struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
1885 	int i;
1886 
1887 	/* Check support of the request for this element. */
1888 	for (i = 0; i < ARRAY_SIZE(pairs); ++i) {
1889 		if (op_flag == pairs[i].op && (vd->access & pairs[i].perm))
1890 			break;
1891 	}
1892 	if (i == ARRAY_SIZE(pairs))
1893 		return -ENXIO;
1894 
1895 	if (kctl->tlv.c == NULL)
1896 		return -ENXIO;
1897 
1898 	/* Write and command operations are not allowed for locked element. */
1899 	if (op_flag != SNDRV_CTL_TLV_OP_READ &&
1900 	    vd->owner != NULL && vd->owner != file)
1901 		return -EPERM;
1902 
1903 	return kctl->tlv.c(kctl, op_flag, size, buf);
1904 }
1905 
1906 static int read_tlv_buf(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id,
1907 			unsigned int __user *buf, unsigned int size)
1908 {
1909 	struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
1910 	unsigned int len;
1911 
1912 	if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ))
1913 		return -ENXIO;
1914 
1915 	if (kctl->tlv.p == NULL)
1916 		return -ENXIO;
1917 
1918 	len = sizeof(unsigned int) * 2 + kctl->tlv.p[1];
1919 	if (size < len)
1920 		return -ENOMEM;
1921 
1922 	if (copy_to_user(buf, kctl->tlv.p, len))
1923 		return -EFAULT;
1924 
1925 	return 0;
1926 }
1927 
1928 static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1929 			     struct snd_ctl_tlv __user *buf,
1930                              int op_flag)
1931 {
1932 	struct snd_ctl_tlv header;
1933 	unsigned int __user *container;
1934 	unsigned int container_size;
1935 	struct snd_kcontrol *kctl;
1936 	struct snd_ctl_elem_id id;
1937 	struct snd_kcontrol_volatile *vd;
1938 
1939 	lockdep_assert_held(&file->card->controls_rwsem);
1940 
1941 	if (copy_from_user(&header, buf, sizeof(header)))
1942 		return -EFAULT;
1943 
1944 	/* In design of control core, numerical ID starts at 1. */
1945 	if (header.numid == 0)
1946 		return -EINVAL;
1947 
1948 	/* At least, container should include type and length fields.  */
1949 	if (header.length < sizeof(unsigned int) * 2)
1950 		return -EINVAL;
1951 	container_size = header.length;
1952 	container = buf->tlv;
1953 
1954 	kctl = snd_ctl_find_numid(file->card, header.numid);
1955 	if (kctl == NULL)
1956 		return -ENOENT;
1957 
1958 	/* Calculate index of the element in this set. */
1959 	id = kctl->id;
1960 	snd_ctl_build_ioff(&id, kctl, header.numid - id.numid);
1961 	vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1962 
1963 	if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1964 		return call_tlv_handler(file, op_flag, kctl, &id, container,
1965 					container_size);
1966 	} else {
1967 		if (op_flag == SNDRV_CTL_TLV_OP_READ) {
1968 			return read_tlv_buf(kctl, &id, container,
1969 					    container_size);
1970 		}
1971 	}
1972 
1973 	/* Not supported. */
1974 	return -ENXIO;
1975 }
1976 
1977 static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1978 {
1979 	struct snd_ctl_file *ctl;
1980 	struct snd_card *card;
1981 	struct snd_kctl_ioctl *p;
1982 	void __user *argp = (void __user *)arg;
1983 	int __user *ip = argp;
1984 	int err;
1985 
1986 	ctl = file->private_data;
1987 	card = ctl->card;
1988 	if (snd_BUG_ON(!card))
1989 		return -ENXIO;
1990 	switch (cmd) {
1991 	case SNDRV_CTL_IOCTL_PVERSION:
1992 		return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1993 	case SNDRV_CTL_IOCTL_CARD_INFO:
1994 		return snd_ctl_card_info(card, ctl, cmd, argp);
1995 	case SNDRV_CTL_IOCTL_ELEM_LIST:
1996 		return snd_ctl_elem_list_user(card, argp);
1997 	case SNDRV_CTL_IOCTL_ELEM_INFO:
1998 		return snd_ctl_elem_info_user(ctl, argp);
1999 	case SNDRV_CTL_IOCTL_ELEM_READ:
2000 		return snd_ctl_elem_read_user(card, argp);
2001 	case SNDRV_CTL_IOCTL_ELEM_WRITE:
2002 		return snd_ctl_elem_write_user(ctl, argp);
2003 	case SNDRV_CTL_IOCTL_ELEM_LOCK:
2004 		return snd_ctl_elem_lock(ctl, argp);
2005 	case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
2006 		return snd_ctl_elem_unlock(ctl, argp);
2007 	case SNDRV_CTL_IOCTL_ELEM_ADD:
2008 		return snd_ctl_elem_add_user(ctl, argp, 0);
2009 	case SNDRV_CTL_IOCTL_ELEM_REPLACE:
2010 		return snd_ctl_elem_add_user(ctl, argp, 1);
2011 	case SNDRV_CTL_IOCTL_ELEM_REMOVE:
2012 		return snd_ctl_elem_remove(ctl, argp);
2013 	case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
2014 		return snd_ctl_subscribe_events(ctl, ip);
2015 	case SNDRV_CTL_IOCTL_TLV_READ:
2016 		err = snd_power_ref_and_wait(card);
2017 		if (err < 0)
2018 			return err;
2019 		scoped_guard(rwsem_read, &card->controls_rwsem)
2020 			err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_READ);
2021 		snd_power_unref(card);
2022 		return err;
2023 	case SNDRV_CTL_IOCTL_TLV_WRITE:
2024 		err = snd_power_ref_and_wait(card);
2025 		if (err < 0)
2026 			return err;
2027 		scoped_guard(rwsem_write, &card->controls_rwsem)
2028 			err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_WRITE);
2029 		snd_power_unref(card);
2030 		return err;
2031 	case SNDRV_CTL_IOCTL_TLV_COMMAND:
2032 		err = snd_power_ref_and_wait(card);
2033 		if (err < 0)
2034 			return err;
2035 		scoped_guard(rwsem_write, &card->controls_rwsem)
2036 			err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_CMD);
2037 		snd_power_unref(card);
2038 		return err;
2039 	case SNDRV_CTL_IOCTL_POWER:
2040 		return -ENOPROTOOPT;
2041 	case SNDRV_CTL_IOCTL_POWER_STATE:
2042 		return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
2043 	}
2044 
2045 	guard(rwsem_read)(&snd_ioctl_rwsem);
2046 	list_for_each_entry(p, &snd_control_ioctls, list) {
2047 		err = p->fioctl(card, ctl, cmd, arg);
2048 		if (err != -ENOIOCTLCMD)
2049 			return err;
2050 	}
2051 	dev_dbg(card->dev, "unknown ioctl = 0x%x\n", cmd);
2052 	return -ENOTTY;
2053 }
2054 
2055 static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
2056 			    size_t count, loff_t * offset)
2057 {
2058 	struct snd_ctl_file *ctl;
2059 	int err = 0;
2060 	ssize_t result = 0;
2061 
2062 	ctl = file->private_data;
2063 	if (snd_BUG_ON(!ctl || !ctl->card))
2064 		return -ENXIO;
2065 	if (!ctl->subscribed)
2066 		return -EBADFD;
2067 	if (count < sizeof(struct snd_ctl_event))
2068 		return -EINVAL;
2069 	spin_lock_irq(&ctl->read_lock);
2070 	while (count >= sizeof(struct snd_ctl_event)) {
2071 		struct snd_ctl_event ev;
2072 		struct snd_kctl_event *kev;
2073 		while (list_empty(&ctl->events)) {
2074 			wait_queue_entry_t wait;
2075 			if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
2076 				err = -EAGAIN;
2077 				goto __end_lock;
2078 			}
2079 			init_waitqueue_entry(&wait, current);
2080 			add_wait_queue(&ctl->change_sleep, &wait);
2081 			set_current_state(TASK_INTERRUPTIBLE);
2082 			spin_unlock_irq(&ctl->read_lock);
2083 			schedule();
2084 			remove_wait_queue(&ctl->change_sleep, &wait);
2085 			if (ctl->card->shutdown)
2086 				return -ENODEV;
2087 			if (signal_pending(current))
2088 				return -ERESTARTSYS;
2089 			spin_lock_irq(&ctl->read_lock);
2090 		}
2091 		kev = snd_kctl_event(ctl->events.next);
2092 		ev.type = SNDRV_CTL_EVENT_ELEM;
2093 		ev.data.elem.mask = kev->mask;
2094 		ev.data.elem.id = kev->id;
2095 		list_del(&kev->list);
2096 		spin_unlock_irq(&ctl->read_lock);
2097 		kfree(kev);
2098 		if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
2099 			err = -EFAULT;
2100 			goto __end;
2101 		}
2102 		spin_lock_irq(&ctl->read_lock);
2103 		buffer += sizeof(struct snd_ctl_event);
2104 		count -= sizeof(struct snd_ctl_event);
2105 		result += sizeof(struct snd_ctl_event);
2106 	}
2107       __end_lock:
2108 	spin_unlock_irq(&ctl->read_lock);
2109       __end:
2110       	return result > 0 ? result : err;
2111 }
2112 
2113 static __poll_t snd_ctl_poll(struct file *file, poll_table * wait)
2114 {
2115 	__poll_t mask;
2116 	struct snd_ctl_file *ctl;
2117 
2118 	ctl = file->private_data;
2119 	if (!ctl->subscribed)
2120 		return 0;
2121 	poll_wait(file, &ctl->change_sleep, wait);
2122 
2123 	mask = 0;
2124 	if (!list_empty(&ctl->events))
2125 		mask |= EPOLLIN | EPOLLRDNORM;
2126 
2127 	return mask;
2128 }
2129 
2130 /*
2131  * register the device-specific control-ioctls.
2132  * called from each device manager like pcm.c, hwdep.c, etc.
2133  */
2134 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
2135 {
2136 	struct snd_kctl_ioctl *pn;
2137 
2138 	pn = kzalloc_obj(struct snd_kctl_ioctl);
2139 	if (pn == NULL)
2140 		return -ENOMEM;
2141 	pn->fioctl = fcn;
2142 	guard(rwsem_write)(&snd_ioctl_rwsem);
2143 	list_add_tail(&pn->list, lists);
2144 	return 0;
2145 }
2146 
2147 /**
2148  * snd_ctl_register_ioctl - register the device-specific control-ioctls
2149  * @fcn: ioctl callback function
2150  *
2151  * called from each device manager like pcm.c, hwdep.c, etc.
2152  *
2153  * Return: zero if successful, or a negative error code
2154  */
2155 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
2156 {
2157 	return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
2158 }
2159 EXPORT_SYMBOL(snd_ctl_register_ioctl);
2160 
2161 #ifdef CONFIG_COMPAT
2162 /**
2163  * snd_ctl_register_ioctl_compat - register the device-specific 32bit compat
2164  * control-ioctls
2165  * @fcn: ioctl callback function
2166  *
2167  * Return: zero if successful, or a negative error code
2168  */
2169 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
2170 {
2171 	return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
2172 }
2173 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
2174 #endif
2175 
2176 /*
2177  * de-register the device-specific control-ioctls.
2178  */
2179 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
2180 				     struct list_head *lists)
2181 {
2182 	struct snd_kctl_ioctl *p;
2183 
2184 	if (snd_BUG_ON(!fcn))
2185 		return -EINVAL;
2186 	guard(rwsem_write)(&snd_ioctl_rwsem);
2187 	list_for_each_entry(p, lists, list) {
2188 		if (p->fioctl == fcn) {
2189 			list_del(&p->list);
2190 			kfree(p);
2191 			return 0;
2192 		}
2193 	}
2194 	snd_BUG();
2195 	return -EINVAL;
2196 }
2197 
2198 /**
2199  * snd_ctl_unregister_ioctl - de-register the device-specific control-ioctls
2200  * @fcn: ioctl callback function to unregister
2201  *
2202  * Return: zero if successful, or a negative error code
2203  */
2204 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
2205 {
2206 	return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
2207 }
2208 EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
2209 
2210 #ifdef CONFIG_COMPAT
2211 /**
2212  * snd_ctl_unregister_ioctl_compat - de-register the device-specific compat
2213  * 32bit control-ioctls
2214  * @fcn: ioctl callback function to unregister
2215  *
2216  * Return: zero if successful, or a negative error code
2217  */
2218 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
2219 {
2220 	return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
2221 }
2222 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
2223 #endif
2224 
2225 static int snd_ctl_fasync(int fd, struct file * file, int on)
2226 {
2227 	struct snd_ctl_file *ctl;
2228 
2229 	ctl = file->private_data;
2230 	return snd_fasync_helper(fd, file, on, &ctl->fasync);
2231 }
2232 
2233 /* return the preferred subdevice number if already assigned;
2234  * otherwise return -1
2235  */
2236 int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type)
2237 {
2238 	struct snd_ctl_file *kctl;
2239 	int subdevice = -1;
2240 
2241 	guard(read_lock_irqsave)(&card->controls_rwlock);
2242 	list_for_each_entry(kctl, &card->ctl_files, list) {
2243 		if (kctl->pid == task_pid(current)) {
2244 			subdevice = kctl->preferred_subdevice[type];
2245 			if (subdevice != -1)
2246 				break;
2247 		}
2248 	}
2249 	return subdevice;
2250 }
2251 EXPORT_SYMBOL_GPL(snd_ctl_get_preferred_subdevice);
2252 
2253 /*
2254  * ioctl32 compat
2255  */
2256 #ifdef CONFIG_COMPAT
2257 #include "control_compat.c"
2258 #else
2259 #define snd_ctl_ioctl_compat	NULL
2260 #endif
2261 
2262 /*
2263  * control layers (audio LED etc.)
2264  */
2265 
2266 /**
2267  * snd_ctl_request_layer - request to use the layer
2268  * @module_name: Name of the kernel module (NULL == build-in)
2269  *
2270  * Return: zero if successful, or an error code when the module cannot be loaded
2271  */
2272 int snd_ctl_request_layer(const char *module_name)
2273 {
2274 	struct snd_ctl_layer_ops *lops;
2275 
2276 	if (module_name == NULL)
2277 		return 0;
2278 	scoped_guard(rwsem_read, &snd_ctl_layer_rwsem) {
2279 		for (lops = snd_ctl_layer; lops; lops = lops->next)
2280 			if (strcmp(lops->module_name, module_name) == 0)
2281 				return 0;
2282 	}
2283 	return request_module(module_name);
2284 }
2285 EXPORT_SYMBOL_GPL(snd_ctl_request_layer);
2286 
2287 /**
2288  * snd_ctl_register_layer - register new control layer
2289  * @lops: operation structure
2290  *
2291  * The new layer can track all control elements and do additional
2292  * operations on top (like audio LED handling).
2293  */
2294 void snd_ctl_register_layer(struct snd_ctl_layer_ops *lops)
2295 {
2296 	struct snd_card *card;
2297 	int card_number;
2298 
2299 	scoped_guard(rwsem_write, &snd_ctl_layer_rwsem) {
2300 		lops->next = snd_ctl_layer;
2301 		snd_ctl_layer = lops;
2302 	}
2303 	for (card_number = 0; card_number < SNDRV_CARDS; card_number++) {
2304 		card = snd_card_ref(card_number);
2305 		if (card) {
2306 			scoped_guard(rwsem_read, &card->controls_rwsem)
2307 				lops->lregister(card);
2308 			snd_card_unref(card);
2309 		}
2310 	}
2311 }
2312 EXPORT_SYMBOL_GPL(snd_ctl_register_layer);
2313 
2314 /**
2315  * snd_ctl_disconnect_layer - disconnect control layer
2316  * @lops: operation structure
2317  *
2318  * It is expected that the information about tracked cards
2319  * is freed before this call (the disconnect callback is
2320  * not called here).
2321  */
2322 void snd_ctl_disconnect_layer(struct snd_ctl_layer_ops *lops)
2323 {
2324 	struct snd_ctl_layer_ops *lops2, *prev_lops2;
2325 
2326 	guard(rwsem_write)(&snd_ctl_layer_rwsem);
2327 	for (lops2 = snd_ctl_layer, prev_lops2 = NULL; lops2; lops2 = lops2->next) {
2328 		if (lops2 == lops) {
2329 			if (!prev_lops2)
2330 				snd_ctl_layer = lops->next;
2331 			else
2332 				prev_lops2->next = lops->next;
2333 			break;
2334 		}
2335 		prev_lops2 = lops2;
2336 	}
2337 }
2338 EXPORT_SYMBOL_GPL(snd_ctl_disconnect_layer);
2339 
2340 /*
2341  *  INIT PART
2342  */
2343 
2344 static const struct file_operations snd_ctl_f_ops =
2345 {
2346 	.owner =	THIS_MODULE,
2347 	.read =		snd_ctl_read,
2348 	.open =		snd_ctl_open,
2349 	.release =	snd_ctl_release,
2350 	.poll =		snd_ctl_poll,
2351 	.unlocked_ioctl =	snd_ctl_ioctl,
2352 	.compat_ioctl =	snd_ctl_ioctl_compat,
2353 	.fasync =	snd_ctl_fasync,
2354 };
2355 
2356 /* call lops under rwsems; called from snd_ctl_dev_*() below() */
2357 #define call_snd_ctl_lops(_card, _op)				    \
2358 	do {							    \
2359 		struct snd_ctl_layer_ops *lops;			    \
2360 		guard(rwsem_read)(&(_card)->controls_rwsem);	    \
2361 		guard(rwsem_read)(&snd_ctl_layer_rwsem);	    \
2362 		for (lops = snd_ctl_layer; lops; lops = lops->next) \
2363 			lops->_op(_card);			    \
2364 	} while (0)
2365 
2366 /*
2367  * registration of the control device
2368  */
2369 static int snd_ctl_dev_register(struct snd_device *device)
2370 {
2371 	struct snd_card *card = device->device_data;
2372 	int err;
2373 
2374 	err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
2375 				  &snd_ctl_f_ops, card, card->ctl_dev);
2376 	if (err < 0)
2377 		return err;
2378 	call_snd_ctl_lops(card, lregister);
2379 	return 0;
2380 }
2381 
2382 /*
2383  * disconnection of the control device
2384  */
2385 static int snd_ctl_dev_disconnect(struct snd_device *device)
2386 {
2387 	struct snd_card *card = device->device_data;
2388 	struct snd_ctl_file *ctl;
2389 
2390 	scoped_guard(read_lock_irqsave, &card->controls_rwlock) {
2391 		list_for_each_entry(ctl, &card->ctl_files, list) {
2392 			wake_up(&ctl->change_sleep);
2393 			snd_kill_fasync(ctl->fasync, SIGIO, POLL_ERR);
2394 		}
2395 	}
2396 
2397 	call_snd_ctl_lops(card, ldisconnect);
2398 	return snd_unregister_device(card->ctl_dev);
2399 }
2400 
2401 /*
2402  * free all controls
2403  */
2404 static int snd_ctl_dev_free(struct snd_device *device)
2405 {
2406 	struct snd_card *card = device->device_data;
2407 	struct snd_kcontrol *control;
2408 
2409 	scoped_guard(rwsem_write, &card->controls_rwsem) {
2410 		while (!list_empty(&card->controls)) {
2411 			control = snd_kcontrol(card->controls.next);
2412 			__snd_ctl_remove(card, control, false);
2413 		}
2414 
2415 #ifdef CONFIG_SND_CTL_FAST_LOOKUP
2416 		xa_destroy(&card->ctl_numids);
2417 		xa_destroy(&card->ctl_hash);
2418 #endif
2419 	}
2420 	put_device(card->ctl_dev);
2421 	return 0;
2422 }
2423 
2424 /*
2425  * create control core:
2426  * called from init.c
2427  */
2428 int snd_ctl_create(struct snd_card *card)
2429 {
2430 	static const struct snd_device_ops ops = {
2431 		.dev_free = snd_ctl_dev_free,
2432 		.dev_register =	snd_ctl_dev_register,
2433 		.dev_disconnect = snd_ctl_dev_disconnect,
2434 	};
2435 	int err;
2436 
2437 	if (snd_BUG_ON(!card))
2438 		return -ENXIO;
2439 	if (snd_BUG_ON(card->number < 0 || card->number >= SNDRV_CARDS))
2440 		return -ENXIO;
2441 
2442 	err = snd_device_alloc(&card->ctl_dev, card);
2443 	if (err < 0)
2444 		return err;
2445 	dev_set_name(card->ctl_dev, "controlC%d", card->number);
2446 
2447 	err = snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
2448 	if (err < 0)
2449 		put_device(card->ctl_dev);
2450 	return err;
2451 }
2452 
2453 /*
2454  * Frequently used control callbacks/helpers
2455  */
2456 
2457 /**
2458  * snd_ctl_boolean_mono_info - Helper function for a standard boolean info
2459  * callback with a mono channel
2460  * @kcontrol: the kcontrol instance
2461  * @uinfo: info to store
2462  *
2463  * This is a function that can be used as info callback for a standard
2464  * boolean control with a single mono channel.
2465  *
2466  * Return: Zero (always successful)
2467  */
2468 int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
2469 			      struct snd_ctl_elem_info *uinfo)
2470 {
2471 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2472 	uinfo->count = 1;
2473 	uinfo->value.integer.min = 0;
2474 	uinfo->value.integer.max = 1;
2475 	return 0;
2476 }
2477 EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
2478 
2479 /**
2480  * snd_ctl_boolean_stereo_info - Helper function for a standard boolean info
2481  * callback with stereo two channels
2482  * @kcontrol: the kcontrol instance
2483  * @uinfo: info to store
2484  *
2485  * This is a function that can be used as info callback for a standard
2486  * boolean control with stereo two channels.
2487  *
2488  * Return: Zero (always successful)
2489  */
2490 int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
2491 				struct snd_ctl_elem_info *uinfo)
2492 {
2493 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2494 	uinfo->count = 2;
2495 	uinfo->value.integer.min = 0;
2496 	uinfo->value.integer.max = 1;
2497 	return 0;
2498 }
2499 EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
2500 
2501 /**
2502  * snd_ctl_enum_info - fills the info structure for an enumerated control
2503  * @info: the structure to be filled
2504  * @channels: the number of the control's channels; often one
2505  * @items: the number of control values; also the size of @names
2506  * @names: an array containing the names of all control values
2507  *
2508  * Sets all required fields in @info to their appropriate values.
2509  * If the control's accessibility is not the default (readable and writable),
2510  * the caller has to fill @info->access.
2511  *
2512  * Return: Zero (always successful)
2513  */
2514 int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
2515 		      unsigned int items, const char *const names[])
2516 {
2517 	info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2518 	info->count = channels;
2519 	info->value.enumerated.items = items;
2520 	if (!items)
2521 		return 0;
2522 	if (info->value.enumerated.item >= items)
2523 		info->value.enumerated.item = items - 1;
2524 	WARN(strlen(names[info->value.enumerated.item]) >= sizeof(info->value.enumerated.name),
2525 	     "ALSA: too long item name '%s'\n",
2526 	     names[info->value.enumerated.item]);
2527 	strscpy(info->value.enumerated.name,
2528 		names[info->value.enumerated.item],
2529 		sizeof(info->value.enumerated.name));
2530 	return 0;
2531 }
2532 EXPORT_SYMBOL(snd_ctl_enum_info);
2533