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