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