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