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