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