1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Routines for driver control interface
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
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>
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>
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");
27 #define MAX_CONTROL_COUNT 1028
29 struct snd_kctl_ioctl {
30 struct list_head list; /* list of all ioctls */
31 snd_kctl_ioctl_func_t fioctl;
34 static DECLARE_RWSEM(snd_ioctl_rwsem);
35 static DECLARE_RWSEM(snd_ctl_layer_rwsem);
36 static LIST_HEAD(snd_control_ioctls);
38 static LIST_HEAD(snd_control_compat_ioctls);
40 static struct snd_ctl_layer_ops *snd_ctl_layer;
42 static int snd_ctl_open(struct inode *inode, struct file *file)
45 struct snd_card *card;
46 struct snd_ctl_file *ctl;
49 err = stream_open(inode, file);
53 card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
58 err = snd_card_file_add(card, file);
63 if (!try_module_get(card->module)) {
67 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
72 INIT_LIST_HEAD(&ctl->events);
73 init_waitqueue_head(&ctl->change_sleep);
74 spin_lock_init(&ctl->read_lock);
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);
87 module_put(card->module);
89 snd_card_file_remove(card, file);
96 static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
99 struct snd_kctl_event *cread;
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);
107 spin_unlock_irqrestore(&ctl->read_lock, flags);
110 static int snd_ctl_release(struct inode *inode, struct file *file)
113 struct snd_card *card;
114 struct snd_ctl_file *ctl;
115 struct snd_kcontrol *control;
118 ctl = file->private_data;
119 file->private_data = NULL;
121 write_lock_irqsave(&card->ctl_files_rwlock, flags);
122 list_del(&ctl->list);
123 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
124 down_write(&card->controls_rwsem);
125 list_for_each_entry(control, &card->controls, list)
126 for (idx = 0; idx < control->count; idx++)
127 if (control->vd[idx].owner == ctl)
128 control->vd[idx].owner = NULL;
129 up_write(&card->controls_rwsem);
130 snd_fasync_free(ctl->fasync);
131 snd_ctl_empty_read_queue(ctl);
134 module_put(card->module);
135 snd_card_file_remove(card, file);
140 * snd_ctl_notify - Send notification to user-space for a control change
141 * @card: the card to send notification
142 * @mask: the event mask, SNDRV_CTL_EVENT_*
143 * @id: the ctl element id to send notification
145 * This function adds an event record with the given id and mask, appends
146 * to the list and wakes up the user-space for notification. This can be
147 * called in the atomic context.
149 void snd_ctl_notify(struct snd_card *card, unsigned int mask,
150 struct snd_ctl_elem_id *id)
153 struct snd_ctl_file *ctl;
154 struct snd_kctl_event *ev;
156 if (snd_BUG_ON(!card || !id))
160 read_lock_irqsave(&card->ctl_files_rwlock, flags);
161 #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
162 card->mixer_oss_change_count++;
164 list_for_each_entry(ctl, &card->ctl_files, list) {
165 if (!ctl->subscribed)
167 spin_lock(&ctl->read_lock);
168 list_for_each_entry(ev, &ctl->events, list) {
169 if (ev->id.numid == id->numid) {
174 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
178 list_add_tail(&ev->list, &ctl->events);
180 dev_err(card->dev, "No memory available to allocate event\n");
183 wake_up(&ctl->change_sleep);
184 spin_unlock(&ctl->read_lock);
185 snd_kill_fasync(ctl->fasync, SIGIO, POLL_IN);
187 read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
189 EXPORT_SYMBOL(snd_ctl_notify);
192 * snd_ctl_notify_one - Send notification to user-space for a control change
193 * @card: the card to send notification
194 * @mask: the event mask, SNDRV_CTL_EVENT_*
195 * @kctl: the pointer with the control instance
196 * @ioff: the additional offset to the control index
198 * This function calls snd_ctl_notify() and does additional jobs
199 * like LED state changes.
201 void snd_ctl_notify_one(struct snd_card *card, unsigned int mask,
202 struct snd_kcontrol *kctl, unsigned int ioff)
204 struct snd_ctl_elem_id id = kctl->id;
205 struct snd_ctl_layer_ops *lops;
209 snd_ctl_notify(card, mask, &id);
210 down_read(&snd_ctl_layer_rwsem);
211 for (lops = snd_ctl_layer; lops; lops = lops->next)
212 lops->lnotify(card, mask, kctl, ioff);
213 up_read(&snd_ctl_layer_rwsem);
215 EXPORT_SYMBOL(snd_ctl_notify_one);
218 * snd_ctl_new - create a new control instance with some elements
219 * @kctl: the pointer to store new control instance
220 * @count: the number of elements in this control
221 * @access: the default access flags for elements in this control
222 * @file: given when locking these elements
224 * Allocates a memory object for a new control instance. The instance has
225 * elements as many as the given number (@count). Each element has given
226 * access permissions (@access). Each element is locked when @file is given.
228 * Return: 0 on success, error code on failure
230 static int snd_ctl_new(struct snd_kcontrol **kctl, unsigned int count,
231 unsigned int access, struct snd_ctl_file *file)
235 if (count == 0 || count > MAX_CONTROL_COUNT)
238 *kctl = kzalloc(struct_size(*kctl, vd, count), GFP_KERNEL);
242 for (idx = 0; idx < count; idx++) {
243 (*kctl)->vd[idx].access = access;
244 (*kctl)->vd[idx].owner = file;
246 (*kctl)->count = count;
252 * snd_ctl_new1 - create a control instance from the template
253 * @ncontrol: the initialization record
254 * @private_data: the private data to set
256 * Allocates a new struct snd_kcontrol instance and initialize from the given
257 * template. When the access field of ncontrol is 0, it's assumed as
258 * READWRITE access. When the count field is 0, it's assumes as one.
260 * Return: The pointer of the newly generated instance, or %NULL on failure.
262 struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
265 struct snd_kcontrol *kctl;
270 if (snd_BUG_ON(!ncontrol || !ncontrol->info))
273 count = ncontrol->count;
277 access = ncontrol->access;
279 access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
280 access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
281 SNDRV_CTL_ELEM_ACCESS_VOLATILE |
282 SNDRV_CTL_ELEM_ACCESS_INACTIVE |
283 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE |
284 SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND |
285 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK |
286 SNDRV_CTL_ELEM_ACCESS_LED_MASK |
287 SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK);
289 err = snd_ctl_new(&kctl, count, access, NULL);
293 /* The 'numid' member is decided when calling snd_ctl_add(). */
294 kctl->id.iface = ncontrol->iface;
295 kctl->id.device = ncontrol->device;
296 kctl->id.subdevice = ncontrol->subdevice;
297 if (ncontrol->name) {
298 strscpy(kctl->id.name, ncontrol->name, sizeof(kctl->id.name));
299 if (strcmp(ncontrol->name, kctl->id.name) != 0)
300 pr_warn("ALSA: Control name '%s' truncated to '%s'\n",
301 ncontrol->name, kctl->id.name);
303 kctl->id.index = ncontrol->index;
305 kctl->info = ncontrol->info;
306 kctl->get = ncontrol->get;
307 kctl->put = ncontrol->put;
308 kctl->tlv.p = ncontrol->tlv.p;
310 kctl->private_value = ncontrol->private_value;
311 kctl->private_data = private_data;
315 EXPORT_SYMBOL(snd_ctl_new1);
318 * snd_ctl_free_one - release the control instance
319 * @kcontrol: the control instance
321 * Releases the control instance created via snd_ctl_new()
323 * Don't call this after the control was added to the card.
325 void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
328 if (kcontrol->private_free)
329 kcontrol->private_free(kcontrol);
333 EXPORT_SYMBOL(snd_ctl_free_one);
335 static bool snd_ctl_remove_numid_conflict(struct snd_card *card,
338 struct snd_kcontrol *kctl;
340 /* Make sure that the ids assigned to the control do not wrap around */
341 if (card->last_numid >= UINT_MAX - count)
342 card->last_numid = 0;
344 list_for_each_entry(kctl, &card->controls, list) {
345 if (kctl->id.numid < card->last_numid + 1 + count &&
346 kctl->id.numid + kctl->count > card->last_numid + 1) {
347 card->last_numid = kctl->id.numid + kctl->count - 1;
354 static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
356 unsigned int iter = 100000;
358 while (snd_ctl_remove_numid_conflict(card, count)) {
360 /* this situation is very unlikely */
361 dev_err(card->dev, "unable to allocate new control numid\n");
368 enum snd_ctl_add_mode {
369 CTL_ADD_EXCLUSIVE, CTL_REPLACE, CTL_ADD_ON_REPLACE,
372 /* add/replace a new kcontrol object; call with card->controls_rwsem locked */
373 static int __snd_ctl_add_replace(struct snd_card *card,
374 struct snd_kcontrol *kcontrol,
375 enum snd_ctl_add_mode mode)
377 struct snd_ctl_elem_id id;
379 struct snd_kcontrol *old;
383 if (id.index > UINT_MAX - kcontrol->count)
386 old = snd_ctl_find_id(card, &id);
388 if (mode == CTL_REPLACE)
391 if (mode == CTL_ADD_EXCLUSIVE) {
393 "control %i:%i:%i:%s:%i is already present\n",
394 id.iface, id.device, id.subdevice, id.name,
399 err = snd_ctl_remove(card, old);
404 if (snd_ctl_find_hole(card, kcontrol->count) < 0)
407 list_add_tail(&kcontrol->list, &card->controls);
408 card->controls_count += kcontrol->count;
409 kcontrol->id.numid = card->last_numid + 1;
410 card->last_numid += kcontrol->count;
412 for (idx = 0; idx < kcontrol->count; idx++)
413 snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_ADD, kcontrol, idx);
418 static int snd_ctl_add_replace(struct snd_card *card,
419 struct snd_kcontrol *kcontrol,
420 enum snd_ctl_add_mode mode)
426 if (snd_BUG_ON(!card || !kcontrol->info))
429 down_write(&card->controls_rwsem);
430 err = __snd_ctl_add_replace(card, kcontrol, mode);
431 up_write(&card->controls_rwsem);
437 snd_ctl_free_one(kcontrol);
442 * snd_ctl_add - add the control instance to the card
443 * @card: the card instance
444 * @kcontrol: the control instance to add
446 * Adds the control instance created via snd_ctl_new() or
447 * snd_ctl_new1() to the given card. Assigns also an unique
448 * numid used for fast search.
450 * It frees automatically the control which cannot be added.
452 * Return: Zero if successful, or a negative error code on failure.
455 int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
457 return snd_ctl_add_replace(card, kcontrol, CTL_ADD_EXCLUSIVE);
459 EXPORT_SYMBOL(snd_ctl_add);
462 * snd_ctl_replace - replace the control instance of the card
463 * @card: the card instance
464 * @kcontrol: the control instance to replace
465 * @add_on_replace: add the control if not already added
467 * Replaces the given control. If the given control does not exist
468 * and the add_on_replace flag is set, the control is added. If the
469 * control exists, it is destroyed first.
471 * It frees automatically the control which cannot be added or replaced.
473 * Return: Zero if successful, or a negative error code on failure.
475 int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol,
478 return snd_ctl_add_replace(card, kcontrol,
479 add_on_replace ? CTL_ADD_ON_REPLACE : CTL_REPLACE);
481 EXPORT_SYMBOL(snd_ctl_replace);
484 * snd_ctl_remove - remove the control from the card and release it
485 * @card: the card instance
486 * @kcontrol: the control instance to remove
488 * Removes the control from the card and then releases the instance.
489 * You don't need to call snd_ctl_free_one(). You must be in
490 * the write lock - down_write(&card->controls_rwsem).
492 * Return: 0 if successful, or a negative error code on failure.
494 int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
498 if (snd_BUG_ON(!card || !kcontrol))
500 list_del(&kcontrol->list);
501 card->controls_count -= kcontrol->count;
502 for (idx = 0; idx < kcontrol->count; idx++)
503 snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_REMOVE, kcontrol, idx);
504 snd_ctl_free_one(kcontrol);
507 EXPORT_SYMBOL(snd_ctl_remove);
510 * snd_ctl_remove_id - remove the control of the given id and release it
511 * @card: the card instance
512 * @id: the control id to remove
514 * Finds the control instance with the given id, removes it from the
515 * card list and releases it.
517 * Return: 0 if successful, or a negative error code on failure.
519 int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
521 struct snd_kcontrol *kctl;
524 down_write(&card->controls_rwsem);
525 kctl = snd_ctl_find_id(card, id);
527 up_write(&card->controls_rwsem);
530 ret = snd_ctl_remove(card, kctl);
531 up_write(&card->controls_rwsem);
534 EXPORT_SYMBOL(snd_ctl_remove_id);
537 * snd_ctl_remove_user_ctl - remove and release the unlocked user control
538 * @file: active control handle
539 * @id: the control id to remove
541 * Finds the control instance with the given id, removes it from the
542 * card list and releases it.
544 * Return: 0 if successful, or a negative error code on failure.
546 static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
547 struct snd_ctl_elem_id *id)
549 struct snd_card *card = file->card;
550 struct snd_kcontrol *kctl;
553 down_write(&card->controls_rwsem);
554 kctl = snd_ctl_find_id(card, id);
559 if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
563 for (idx = 0; idx < kctl->count; idx++)
564 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
568 ret = snd_ctl_remove(card, kctl);
570 up_write(&card->controls_rwsem);
575 * snd_ctl_activate_id - activate/inactivate the control of the given id
576 * @card: the card instance
577 * @id: the control id to activate/inactivate
578 * @active: non-zero to activate
580 * Finds the control instance with the given id, and activate or
581 * inactivate the control together with notification, if changed.
582 * The given ID data is filled with full information.
584 * Return: 0 if unchanged, 1 if changed, or a negative error code on failure.
586 int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
589 struct snd_kcontrol *kctl;
590 struct snd_kcontrol_volatile *vd;
591 unsigned int index_offset;
594 down_write(&card->controls_rwsem);
595 kctl = snd_ctl_find_id(card, id);
600 index_offset = snd_ctl_get_ioff(kctl, id);
601 vd = &kctl->vd[index_offset];
604 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
606 vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
608 if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)
610 vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
612 snd_ctl_build_ioff(id, kctl, index_offset);
613 downgrade_write(&card->controls_rwsem);
614 snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_INFO, kctl, index_offset);
615 up_read(&card->controls_rwsem);
619 up_write(&card->controls_rwsem);
622 EXPORT_SYMBOL_GPL(snd_ctl_activate_id);
625 * snd_ctl_rename_id - replace the id of a control on the card
626 * @card: the card instance
627 * @src_id: the old id
628 * @dst_id: the new id
630 * Finds the control with the old id from the card, and replaces the
631 * id with the new one.
633 * Return: Zero if successful, or a negative error code on failure.
635 int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
636 struct snd_ctl_elem_id *dst_id)
638 struct snd_kcontrol *kctl;
640 down_write(&card->controls_rwsem);
641 kctl = snd_ctl_find_id(card, src_id);
643 up_write(&card->controls_rwsem);
647 kctl->id.numid = card->last_numid + 1;
648 card->last_numid += kctl->count;
649 up_write(&card->controls_rwsem);
652 EXPORT_SYMBOL(snd_ctl_rename_id);
655 * snd_ctl_find_numid - find the control instance with the given number-id
656 * @card: the card instance
657 * @numid: the number-id to search
659 * Finds the control instance with the given number-id from the card.
661 * The caller must down card->controls_rwsem before calling this function
662 * (if the race condition can happen).
664 * Return: The pointer of the instance if found, or %NULL if not.
667 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
669 struct snd_kcontrol *kctl;
671 if (snd_BUG_ON(!card || !numid))
673 list_for_each_entry(kctl, &card->controls, list) {
674 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
679 EXPORT_SYMBOL(snd_ctl_find_numid);
682 * snd_ctl_find_id - find the control instance with the given id
683 * @card: the card instance
684 * @id: the id to search
686 * Finds the control instance with the given id from the card.
688 * The caller must down card->controls_rwsem before calling this function
689 * (if the race condition can happen).
691 * Return: The pointer of the instance if found, or %NULL if not.
694 struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
695 struct snd_ctl_elem_id *id)
697 struct snd_kcontrol *kctl;
699 if (snd_BUG_ON(!card || !id))
702 return snd_ctl_find_numid(card, id->numid);
703 list_for_each_entry(kctl, &card->controls, list) {
704 if (kctl->id.iface != id->iface)
706 if (kctl->id.device != id->device)
708 if (kctl->id.subdevice != id->subdevice)
710 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
712 if (kctl->id.index > id->index)
714 if (kctl->id.index + kctl->count <= id->index)
720 EXPORT_SYMBOL(snd_ctl_find_id);
722 static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
723 unsigned int cmd, void __user *arg)
725 struct snd_ctl_card_info *info;
727 info = kzalloc(sizeof(*info), GFP_KERNEL);
730 down_read(&snd_ioctl_rwsem);
731 info->card = card->number;
732 strscpy(info->id, card->id, sizeof(info->id));
733 strscpy(info->driver, card->driver, sizeof(info->driver));
734 strscpy(info->name, card->shortname, sizeof(info->name));
735 strscpy(info->longname, card->longname, sizeof(info->longname));
736 strscpy(info->mixername, card->mixername, sizeof(info->mixername));
737 strscpy(info->components, card->components, sizeof(info->components));
738 up_read(&snd_ioctl_rwsem);
739 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
747 static int snd_ctl_elem_list(struct snd_card *card,
748 struct snd_ctl_elem_list *list)
750 struct snd_kcontrol *kctl;
751 struct snd_ctl_elem_id id;
752 unsigned int offset, space, jidx;
755 offset = list->offset;
758 down_read(&card->controls_rwsem);
759 list->count = card->controls_count;
762 list_for_each_entry(kctl, &card->controls, list) {
763 if (offset >= kctl->count) {
764 offset -= kctl->count;
767 for (jidx = offset; jidx < kctl->count; jidx++) {
768 snd_ctl_build_ioff(&id, kctl, jidx);
769 if (copy_to_user(list->pids + list->used, &id,
782 up_read(&card->controls_rwsem);
786 static int snd_ctl_elem_list_user(struct snd_card *card,
787 struct snd_ctl_elem_list __user *_list)
789 struct snd_ctl_elem_list list;
792 if (copy_from_user(&list, _list, sizeof(list)))
794 err = snd_ctl_elem_list(card, &list);
797 if (copy_to_user(_list, &list, sizeof(list)))
803 /* Check whether the given kctl info is valid */
804 static int snd_ctl_check_elem_info(struct snd_card *card,
805 const struct snd_ctl_elem_info *info)
807 static const unsigned int max_value_counts[] = {
808 [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = 128,
809 [SNDRV_CTL_ELEM_TYPE_INTEGER] = 128,
810 [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = 128,
811 [SNDRV_CTL_ELEM_TYPE_BYTES] = 512,
812 [SNDRV_CTL_ELEM_TYPE_IEC958] = 1,
813 [SNDRV_CTL_ELEM_TYPE_INTEGER64] = 64,
816 if (info->type < SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
817 info->type > SNDRV_CTL_ELEM_TYPE_INTEGER64) {
820 "control %i:%i:%i:%s:%i: invalid type %d\n",
821 info->id.iface, info->id.device,
822 info->id.subdevice, info->id.name,
823 info->id.index, info->type);
826 if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED &&
827 info->value.enumerated.items == 0) {
830 "control %i:%i:%i:%s:%i: zero enum items\n",
831 info->id.iface, info->id.device,
832 info->id.subdevice, info->id.name,
836 if (info->count > max_value_counts[info->type]) {
839 "control %i:%i:%i:%s:%i: invalid count %d\n",
840 info->id.iface, info->id.device,
841 info->id.subdevice, info->id.name,
842 info->id.index, info->count);
849 /* The capacity of struct snd_ctl_elem_value.value.*/
850 static const unsigned int value_sizes[] = {
851 [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = sizeof(long),
852 [SNDRV_CTL_ELEM_TYPE_INTEGER] = sizeof(long),
853 [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = sizeof(unsigned int),
854 [SNDRV_CTL_ELEM_TYPE_BYTES] = sizeof(unsigned char),
855 [SNDRV_CTL_ELEM_TYPE_IEC958] = sizeof(struct snd_aes_iec958),
856 [SNDRV_CTL_ELEM_TYPE_INTEGER64] = sizeof(long long),
859 #ifdef CONFIG_SND_CTL_VALIDATION
860 /* fill the remaining snd_ctl_elem_value data with the given pattern */
861 static void fill_remaining_elem_value(struct snd_ctl_elem_value *control,
862 struct snd_ctl_elem_info *info,
865 size_t offset = value_sizes[info->type] * info->count;
867 offset = DIV_ROUND_UP(offset, sizeof(u32));
868 memset32((u32 *)control->value.bytes.data + offset, pattern,
869 sizeof(control->value) / sizeof(u32) - offset);
872 /* check whether the given integer ctl value is valid */
873 static int sanity_check_int_value(struct snd_card *card,
874 const struct snd_ctl_elem_value *control,
875 const struct snd_ctl_elem_info *info,
878 long long lval, lmin, lmax, lstep;
881 switch (info->type) {
883 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
884 lval = control->value.integer.value[i];
889 case SNDRV_CTL_ELEM_TYPE_INTEGER:
890 lval = control->value.integer.value[i];
891 lmin = info->value.integer.min;
892 lmax = info->value.integer.max;
893 lstep = info->value.integer.step;
895 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
896 lval = control->value.integer64.value[i];
897 lmin = info->value.integer64.min;
898 lmax = info->value.integer64.max;
899 lstep = info->value.integer64.step;
901 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
902 lval = control->value.enumerated.item[i];
904 lmax = info->value.enumerated.items - 1;
909 if (lval < lmin || lval > lmax) {
911 "control %i:%i:%i:%s:%i: value out of range %lld (%lld/%lld) at count %i\n",
912 control->id.iface, control->id.device,
913 control->id.subdevice, control->id.name,
914 control->id.index, lval, lmin, lmax, i);
918 div64_u64_rem(lval, lstep, &rem);
921 "control %i:%i:%i:%s:%i: unaligned value %lld (step %lld) at count %i\n",
922 control->id.iface, control->id.device,
923 control->id.subdevice, control->id.name,
924 control->id.index, lval, lstep, i);
932 /* perform sanity checks to the given snd_ctl_elem_value object */
933 static int sanity_check_elem_value(struct snd_card *card,
934 const struct snd_ctl_elem_value *control,
935 const struct snd_ctl_elem_info *info,
942 switch (info->type) {
943 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
944 case SNDRV_CTL_ELEM_TYPE_INTEGER:
945 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
946 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
947 for (i = 0; i < info->count; i++) {
948 ret = sanity_check_int_value(card, control, info, i);
957 /* check whether the remaining area kept untouched */
958 offset = value_sizes[info->type] * info->count;
959 offset = DIV_ROUND_UP(offset, sizeof(u32));
960 p = (u32 *)control->value.bytes.data + offset;
961 for (; offset < sizeof(control->value) / sizeof(u32); offset++, p++) {
966 *p = 0; /* clear the checked area */
972 static inline void fill_remaining_elem_value(struct snd_ctl_elem_value *control,
973 struct snd_ctl_elem_info *info,
978 static inline int sanity_check_elem_value(struct snd_card *card,
979 struct snd_ctl_elem_value *control,
980 struct snd_ctl_elem_info *info,
987 static int __snd_ctl_elem_info(struct snd_card *card,
988 struct snd_kcontrol *kctl,
989 struct snd_ctl_elem_info *info,
990 struct snd_ctl_file *ctl)
992 struct snd_kcontrol_volatile *vd;
993 unsigned int index_offset;
996 #ifdef CONFIG_SND_DEBUG
999 result = snd_power_ref_and_wait(card);
1001 result = kctl->info(kctl, info);
1002 snd_power_unref(card);
1004 snd_BUG_ON(info->access);
1005 index_offset = snd_ctl_get_ioff(kctl, &info->id);
1006 vd = &kctl->vd[index_offset];
1007 snd_ctl_build_ioff(&info->id, kctl, index_offset);
1008 info->access = vd->access;
1010 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
1011 if (vd->owner == ctl)
1012 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
1013 info->owner = pid_vnr(vd->owner->pid);
1017 if (!snd_ctl_skip_validation(info) &&
1018 snd_ctl_check_elem_info(card, info) < 0)
1024 static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
1025 struct snd_ctl_elem_info *info)
1027 struct snd_card *card = ctl->card;
1028 struct snd_kcontrol *kctl;
1031 down_read(&card->controls_rwsem);
1032 kctl = snd_ctl_find_id(card, &info->id);
1036 result = __snd_ctl_elem_info(card, kctl, info, ctl);
1037 up_read(&card->controls_rwsem);
1041 static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
1042 struct snd_ctl_elem_info __user *_info)
1044 struct snd_ctl_elem_info info;
1047 if (copy_from_user(&info, _info, sizeof(info)))
1049 result = snd_ctl_elem_info(ctl, &info);
1052 /* drop internal access flags */
1053 info.access &= ~(SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK|
1054 SNDRV_CTL_ELEM_ACCESS_LED_MASK);
1055 if (copy_to_user(_info, &info, sizeof(info)))
1060 static int snd_ctl_elem_read(struct snd_card *card,
1061 struct snd_ctl_elem_value *control)
1063 struct snd_kcontrol *kctl;
1064 struct snd_kcontrol_volatile *vd;
1065 unsigned int index_offset;
1066 struct snd_ctl_elem_info info;
1067 const u32 pattern = 0xdeadbeef;
1070 kctl = snd_ctl_find_id(card, &control->id);
1074 index_offset = snd_ctl_get_ioff(kctl, &control->id);
1075 vd = &kctl->vd[index_offset];
1076 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_READ) || kctl->get == NULL)
1079 snd_ctl_build_ioff(&control->id, kctl, index_offset);
1081 #ifdef CONFIG_SND_CTL_VALIDATION
1082 /* info is needed only for validation */
1083 memset(&info, 0, sizeof(info));
1084 info.id = control->id;
1085 ret = __snd_ctl_elem_info(card, kctl, &info, NULL);
1090 if (!snd_ctl_skip_validation(&info))
1091 fill_remaining_elem_value(control, &info, pattern);
1092 ret = snd_power_ref_and_wait(card);
1094 ret = kctl->get(kctl, control);
1095 snd_power_unref(card);
1098 if (!snd_ctl_skip_validation(&info) &&
1099 sanity_check_elem_value(card, control, &info, pattern) < 0) {
1101 "control %i:%i:%i:%s:%i: access overflow\n",
1102 control->id.iface, control->id.device,
1103 control->id.subdevice, control->id.name,
1110 static int snd_ctl_elem_read_user(struct snd_card *card,
1111 struct snd_ctl_elem_value __user *_control)
1113 struct snd_ctl_elem_value *control;
1116 control = memdup_user(_control, sizeof(*control));
1117 if (IS_ERR(control))
1118 return PTR_ERR(control);
1120 down_read(&card->controls_rwsem);
1121 result = snd_ctl_elem_read(card, control);
1122 up_read(&card->controls_rwsem);
1126 if (copy_to_user(_control, control, sizeof(*control)))
1133 static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
1134 struct snd_ctl_elem_value *control)
1136 struct snd_kcontrol *kctl;
1137 struct snd_kcontrol_volatile *vd;
1138 unsigned int index_offset;
1141 down_write(&card->controls_rwsem);
1142 kctl = snd_ctl_find_id(card, &control->id);
1144 up_write(&card->controls_rwsem);
1148 index_offset = snd_ctl_get_ioff(kctl, &control->id);
1149 vd = &kctl->vd[index_offset];
1150 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) || kctl->put == NULL ||
1151 (file && vd->owner && vd->owner != file)) {
1152 up_write(&card->controls_rwsem);
1156 snd_ctl_build_ioff(&control->id, kctl, index_offset);
1157 result = snd_power_ref_and_wait(card);
1159 result = kctl->put(kctl, control);
1160 snd_power_unref(card);
1162 up_write(&card->controls_rwsem);
1167 downgrade_write(&card->controls_rwsem);
1168 snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_VALUE, kctl, index_offset);
1169 up_read(&card->controls_rwsem);
1171 up_write(&card->controls_rwsem);
1177 static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
1178 struct snd_ctl_elem_value __user *_control)
1180 struct snd_ctl_elem_value *control;
1181 struct snd_card *card;
1184 control = memdup_user(_control, sizeof(*control));
1185 if (IS_ERR(control))
1186 return PTR_ERR(control);
1189 result = snd_ctl_elem_write(card, file, control);
1193 if (copy_to_user(_control, control, sizeof(*control)))
1200 static int snd_ctl_elem_lock(struct snd_ctl_file *file,
1201 struct snd_ctl_elem_id __user *_id)
1203 struct snd_card *card = file->card;
1204 struct snd_ctl_elem_id id;
1205 struct snd_kcontrol *kctl;
1206 struct snd_kcontrol_volatile *vd;
1209 if (copy_from_user(&id, _id, sizeof(id)))
1211 down_write(&card->controls_rwsem);
1212 kctl = snd_ctl_find_id(card, &id);
1216 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1217 if (vd->owner != NULL)
1224 up_write(&card->controls_rwsem);
1228 static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
1229 struct snd_ctl_elem_id __user *_id)
1231 struct snd_card *card = file->card;
1232 struct snd_ctl_elem_id id;
1233 struct snd_kcontrol *kctl;
1234 struct snd_kcontrol_volatile *vd;
1237 if (copy_from_user(&id, _id, sizeof(id)))
1239 down_write(&card->controls_rwsem);
1240 kctl = snd_ctl_find_id(card, &id);
1244 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1245 if (vd->owner == NULL)
1247 else if (vd->owner != file)
1254 up_write(&card->controls_rwsem);
1258 struct user_element {
1259 struct snd_ctl_elem_info info;
1260 struct snd_card *card;
1261 char *elem_data; /* element data */
1262 unsigned long elem_data_size; /* size of element data in bytes */
1263 void *tlv_data; /* TLV data */
1264 unsigned long tlv_data_size; /* TLV data size */
1265 void *priv_data; /* private data (like strings for enumerated type) */
1268 // check whether the addition (in bytes) of user ctl element may overflow the limit.
1269 static bool check_user_elem_overflow(struct snd_card *card, ssize_t add)
1271 return (ssize_t)card->user_ctl_alloc_size + add > max_user_ctl_alloc_size;
1274 static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
1275 struct snd_ctl_elem_info *uinfo)
1277 struct user_element *ue = kcontrol->private_data;
1278 unsigned int offset;
1280 offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
1282 snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
1287 static int snd_ctl_elem_user_enum_info(struct snd_kcontrol *kcontrol,
1288 struct snd_ctl_elem_info *uinfo)
1290 struct user_element *ue = kcontrol->private_data;
1293 unsigned int offset;
1295 item = uinfo->value.enumerated.item;
1297 offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
1299 snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
1301 item = min(item, uinfo->value.enumerated.items - 1);
1302 uinfo->value.enumerated.item = item;
1304 names = ue->priv_data;
1305 for (; item > 0; --item)
1306 names += strlen(names) + 1;
1307 strcpy(uinfo->value.enumerated.name, names);
1312 static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
1313 struct snd_ctl_elem_value *ucontrol)
1315 struct user_element *ue = kcontrol->private_data;
1316 unsigned int size = ue->elem_data_size;
1317 char *src = ue->elem_data +
1318 snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
1320 memcpy(&ucontrol->value, src, size);
1324 static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
1325 struct snd_ctl_elem_value *ucontrol)
1328 struct user_element *ue = kcontrol->private_data;
1329 unsigned int size = ue->elem_data_size;
1330 char *dst = ue->elem_data +
1331 snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
1333 change = memcmp(&ucontrol->value, dst, size) != 0;
1335 memcpy(dst, &ucontrol->value, size);
1339 /* called in controls_rwsem write lock */
1340 static int replace_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
1343 struct user_element *ue = kctl->private_data;
1344 unsigned int *container;
1345 unsigned int mask = 0;
1349 if (size > 1024 * 128) /* sane value */
1352 // does the TLV size change cause overflow?
1353 if (check_user_elem_overflow(ue->card, (ssize_t)(size - ue->tlv_data_size)))
1356 container = vmemdup_user(buf, size);
1357 if (IS_ERR(container))
1358 return PTR_ERR(container);
1360 change = ue->tlv_data_size != size;
1362 change = memcmp(ue->tlv_data, container, size) != 0;
1368 if (ue->tlv_data == NULL) {
1369 /* Now TLV data is available. */
1370 for (i = 0; i < kctl->count; ++i)
1371 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1372 mask = SNDRV_CTL_EVENT_MASK_INFO;
1374 ue->card->user_ctl_alloc_size -= ue->tlv_data_size;
1375 ue->tlv_data_size = 0;
1376 kvfree(ue->tlv_data);
1379 ue->tlv_data = container;
1380 ue->tlv_data_size = size;
1381 // decremented at private_free.
1382 ue->card->user_ctl_alloc_size += size;
1384 mask |= SNDRV_CTL_EVENT_MASK_TLV;
1385 for (i = 0; i < kctl->count; ++i)
1386 snd_ctl_notify_one(ue->card, mask, kctl, i);
1391 static int read_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
1394 struct user_element *ue = kctl->private_data;
1396 if (ue->tlv_data_size == 0 || ue->tlv_data == NULL)
1399 if (size < ue->tlv_data_size)
1402 if (copy_to_user(buf, ue->tlv_data, ue->tlv_data_size))
1408 static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kctl, int op_flag,
1409 unsigned int size, unsigned int __user *buf)
1411 if (op_flag == SNDRV_CTL_TLV_OP_WRITE)
1412 return replace_user_tlv(kctl, buf, size);
1414 return read_user_tlv(kctl, buf, size);
1417 /* called in controls_rwsem write lock */
1418 static int snd_ctl_elem_init_enum_names(struct user_element *ue)
1421 size_t buf_len, name_len;
1423 const uintptr_t user_ptrval = ue->info.value.enumerated.names_ptr;
1425 buf_len = ue->info.value.enumerated.names_length;
1426 if (buf_len > 64 * 1024)
1429 if (check_user_elem_overflow(ue->card, buf_len))
1431 names = vmemdup_user((const void __user *)user_ptrval, buf_len);
1433 return PTR_ERR(names);
1435 /* check that there are enough valid names */
1437 for (i = 0; i < ue->info.value.enumerated.items; ++i) {
1438 name_len = strnlen(p, buf_len);
1439 if (name_len == 0 || name_len >= 64 || name_len == buf_len) {
1444 buf_len -= name_len + 1;
1447 ue->priv_data = names;
1448 ue->info.value.enumerated.names_ptr = 0;
1449 // increment the allocation size; decremented again at private_free.
1450 ue->card->user_ctl_alloc_size += ue->info.value.enumerated.names_length;
1455 static size_t compute_user_elem_size(size_t size, unsigned int count)
1457 return sizeof(struct user_element) + size * count;
1460 static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
1462 struct user_element *ue = kcontrol->private_data;
1464 // decrement the allocation size.
1465 ue->card->user_ctl_alloc_size -= compute_user_elem_size(ue->elem_data_size, kcontrol->count);
1466 ue->card->user_ctl_alloc_size -= ue->tlv_data_size;
1468 ue->card->user_ctl_alloc_size -= ue->info.value.enumerated.names_length;
1470 kvfree(ue->tlv_data);
1471 kvfree(ue->priv_data);
1475 static int snd_ctl_elem_add(struct snd_ctl_file *file,
1476 struct snd_ctl_elem_info *info, int replace)
1478 struct snd_card *card = file->card;
1479 struct snd_kcontrol *kctl;
1481 unsigned int access;
1484 struct user_element *ue;
1485 unsigned int offset;
1488 if (!*info->id.name)
1490 if (strnlen(info->id.name, sizeof(info->id.name)) >= sizeof(info->id.name))
1493 /* Delete a control to replace them if needed. */
1496 err = snd_ctl_remove_user_ctl(file, &info->id);
1501 /* Check the number of elements for this userspace control. */
1502 count = info->owner;
1506 /* Arrange access permissions if needed. */
1507 access = info->access;
1509 access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1510 access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1511 SNDRV_CTL_ELEM_ACCESS_INACTIVE |
1512 SNDRV_CTL_ELEM_ACCESS_TLV_WRITE);
1514 /* In initial state, nothing is available as TLV container. */
1515 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
1516 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1517 access |= SNDRV_CTL_ELEM_ACCESS_USER;
1520 * Check information and calculate the size of data specific to
1521 * this userspace control.
1523 /* pass NULL to card for suppressing error messages */
1524 err = snd_ctl_check_elem_info(NULL, info);
1527 /* user-space control doesn't allow zero-size data */
1528 if (info->count < 1)
1530 private_size = value_sizes[info->type] * info->count;
1531 alloc_size = compute_user_elem_size(private_size, count);
1533 down_write(&card->controls_rwsem);
1534 if (check_user_elem_overflow(card, alloc_size)) {
1540 * Keep memory object for this userspace control. After passing this
1541 * code block, the instance should be freed by snd_ctl_free_one().
1543 * Note that these elements in this control are locked.
1545 err = snd_ctl_new(&kctl, count, access, file);
1548 memcpy(&kctl->id, &info->id, sizeof(kctl->id));
1549 ue = kzalloc(alloc_size, GFP_KERNEL);
1555 kctl->private_data = ue;
1556 kctl->private_free = snd_ctl_elem_user_free;
1558 // increment the allocated size; decremented again at private_free.
1559 card->user_ctl_alloc_size += alloc_size;
1561 /* Set private data for this userspace control. */
1564 ue->info.access = 0;
1565 ue->elem_data = (char *)ue + sizeof(*ue);
1566 ue->elem_data_size = private_size;
1567 if (ue->info.type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
1568 err = snd_ctl_elem_init_enum_names(ue);
1570 snd_ctl_free_one(kctl);
1575 /* Set callback functions. */
1576 if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED)
1577 kctl->info = snd_ctl_elem_user_enum_info;
1579 kctl->info = snd_ctl_elem_user_info;
1580 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1581 kctl->get = snd_ctl_elem_user_get;
1582 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1583 kctl->put = snd_ctl_elem_user_put;
1584 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
1585 kctl->tlv.c = snd_ctl_elem_user_tlv;
1587 /* This function manage to free the instance on failure. */
1588 err = __snd_ctl_add_replace(card, kctl, CTL_ADD_EXCLUSIVE);
1590 snd_ctl_free_one(kctl);
1593 offset = snd_ctl_get_ioff(kctl, &info->id);
1594 snd_ctl_build_ioff(&info->id, kctl, offset);
1596 * Here we cannot fill any field for the number of elements added by
1597 * this operation because there're no specific fields. The usage of
1598 * 'owner' field for this purpose may cause any bugs to userspace
1599 * applications because the field originally means PID of a process
1600 * which locks the element.
1603 up_write(&card->controls_rwsem);
1607 static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1608 struct snd_ctl_elem_info __user *_info, int replace)
1610 struct snd_ctl_elem_info info;
1613 if (copy_from_user(&info, _info, sizeof(info)))
1615 err = snd_ctl_elem_add(file, &info, replace);
1618 if (copy_to_user(_info, &info, sizeof(info))) {
1619 snd_ctl_remove_user_ctl(file, &info.id);
1626 static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1627 struct snd_ctl_elem_id __user *_id)
1629 struct snd_ctl_elem_id id;
1631 if (copy_from_user(&id, _id, sizeof(id)))
1633 return snd_ctl_remove_user_ctl(file, &id);
1636 static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
1639 if (get_user(subscribe, ptr))
1641 if (subscribe < 0) {
1642 subscribe = file->subscribed;
1643 if (put_user(subscribe, ptr))
1648 file->subscribed = 1;
1650 } else if (file->subscribed) {
1651 snd_ctl_empty_read_queue(file);
1652 file->subscribed = 0;
1657 static int call_tlv_handler(struct snd_ctl_file *file, int op_flag,
1658 struct snd_kcontrol *kctl,
1659 struct snd_ctl_elem_id *id,
1660 unsigned int __user *buf, unsigned int size)
1662 static const struct {
1666 {SNDRV_CTL_TLV_OP_READ, SNDRV_CTL_ELEM_ACCESS_TLV_READ},
1667 {SNDRV_CTL_TLV_OP_WRITE, SNDRV_CTL_ELEM_ACCESS_TLV_WRITE},
1668 {SNDRV_CTL_TLV_OP_CMD, SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND},
1670 struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
1673 /* Check support of the request for this element. */
1674 for (i = 0; i < ARRAY_SIZE(pairs); ++i) {
1675 if (op_flag == pairs[i].op && (vd->access & pairs[i].perm))
1678 if (i == ARRAY_SIZE(pairs))
1681 if (kctl->tlv.c == NULL)
1684 /* Write and command operations are not allowed for locked element. */
1685 if (op_flag != SNDRV_CTL_TLV_OP_READ &&
1686 vd->owner != NULL && vd->owner != file)
1689 ret = snd_power_ref_and_wait(file->card);
1691 ret = kctl->tlv.c(kctl, op_flag, size, buf);
1692 snd_power_unref(file->card);
1696 static int read_tlv_buf(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id,
1697 unsigned int __user *buf, unsigned int size)
1699 struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
1702 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ))
1705 if (kctl->tlv.p == NULL)
1708 len = sizeof(unsigned int) * 2 + kctl->tlv.p[1];
1712 if (copy_to_user(buf, kctl->tlv.p, len))
1718 static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1719 struct snd_ctl_tlv __user *buf,
1722 struct snd_ctl_tlv header;
1723 unsigned int __user *container;
1724 unsigned int container_size;
1725 struct snd_kcontrol *kctl;
1726 struct snd_ctl_elem_id id;
1727 struct snd_kcontrol_volatile *vd;
1729 if (copy_from_user(&header, buf, sizeof(header)))
1732 /* In design of control core, numerical ID starts at 1. */
1733 if (header.numid == 0)
1736 /* At least, container should include type and length fields. */
1737 if (header.length < sizeof(unsigned int) * 2)
1739 container_size = header.length;
1740 container = buf->tlv;
1742 kctl = snd_ctl_find_numid(file->card, header.numid);
1746 /* Calculate index of the element in this set. */
1748 snd_ctl_build_ioff(&id, kctl, header.numid - id.numid);
1749 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1751 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1752 return call_tlv_handler(file, op_flag, kctl, &id, container,
1755 if (op_flag == SNDRV_CTL_TLV_OP_READ) {
1756 return read_tlv_buf(kctl, &id, container,
1761 /* Not supported. */
1765 static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1767 struct snd_ctl_file *ctl;
1768 struct snd_card *card;
1769 struct snd_kctl_ioctl *p;
1770 void __user *argp = (void __user *)arg;
1771 int __user *ip = argp;
1774 ctl = file->private_data;
1776 if (snd_BUG_ON(!card))
1779 case SNDRV_CTL_IOCTL_PVERSION:
1780 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1781 case SNDRV_CTL_IOCTL_CARD_INFO:
1782 return snd_ctl_card_info(card, ctl, cmd, argp);
1783 case SNDRV_CTL_IOCTL_ELEM_LIST:
1784 return snd_ctl_elem_list_user(card, argp);
1785 case SNDRV_CTL_IOCTL_ELEM_INFO:
1786 return snd_ctl_elem_info_user(ctl, argp);
1787 case SNDRV_CTL_IOCTL_ELEM_READ:
1788 return snd_ctl_elem_read_user(card, argp);
1789 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1790 return snd_ctl_elem_write_user(ctl, argp);
1791 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1792 return snd_ctl_elem_lock(ctl, argp);
1793 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1794 return snd_ctl_elem_unlock(ctl, argp);
1795 case SNDRV_CTL_IOCTL_ELEM_ADD:
1796 return snd_ctl_elem_add_user(ctl, argp, 0);
1797 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1798 return snd_ctl_elem_add_user(ctl, argp, 1);
1799 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1800 return snd_ctl_elem_remove(ctl, argp);
1801 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1802 return snd_ctl_subscribe_events(ctl, ip);
1803 case SNDRV_CTL_IOCTL_TLV_READ:
1804 down_read(&ctl->card->controls_rwsem);
1805 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_READ);
1806 up_read(&ctl->card->controls_rwsem);
1808 case SNDRV_CTL_IOCTL_TLV_WRITE:
1809 down_write(&ctl->card->controls_rwsem);
1810 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_WRITE);
1811 up_write(&ctl->card->controls_rwsem);
1813 case SNDRV_CTL_IOCTL_TLV_COMMAND:
1814 down_write(&ctl->card->controls_rwsem);
1815 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_CMD);
1816 up_write(&ctl->card->controls_rwsem);
1818 case SNDRV_CTL_IOCTL_POWER:
1819 return -ENOPROTOOPT;
1820 case SNDRV_CTL_IOCTL_POWER_STATE:
1821 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1823 down_read(&snd_ioctl_rwsem);
1824 list_for_each_entry(p, &snd_control_ioctls, list) {
1825 err = p->fioctl(card, ctl, cmd, arg);
1826 if (err != -ENOIOCTLCMD) {
1827 up_read(&snd_ioctl_rwsem);
1831 up_read(&snd_ioctl_rwsem);
1832 dev_dbg(card->dev, "unknown ioctl = 0x%x\n", cmd);
1836 static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1837 size_t count, loff_t * offset)
1839 struct snd_ctl_file *ctl;
1843 ctl = file->private_data;
1844 if (snd_BUG_ON(!ctl || !ctl->card))
1846 if (!ctl->subscribed)
1848 if (count < sizeof(struct snd_ctl_event))
1850 spin_lock_irq(&ctl->read_lock);
1851 while (count >= sizeof(struct snd_ctl_event)) {
1852 struct snd_ctl_event ev;
1853 struct snd_kctl_event *kev;
1854 while (list_empty(&ctl->events)) {
1855 wait_queue_entry_t wait;
1856 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1860 init_waitqueue_entry(&wait, current);
1861 add_wait_queue(&ctl->change_sleep, &wait);
1862 set_current_state(TASK_INTERRUPTIBLE);
1863 spin_unlock_irq(&ctl->read_lock);
1865 remove_wait_queue(&ctl->change_sleep, &wait);
1866 if (ctl->card->shutdown)
1868 if (signal_pending(current))
1869 return -ERESTARTSYS;
1870 spin_lock_irq(&ctl->read_lock);
1872 kev = snd_kctl_event(ctl->events.next);
1873 ev.type = SNDRV_CTL_EVENT_ELEM;
1874 ev.data.elem.mask = kev->mask;
1875 ev.data.elem.id = kev->id;
1876 list_del(&kev->list);
1877 spin_unlock_irq(&ctl->read_lock);
1879 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
1883 spin_lock_irq(&ctl->read_lock);
1884 buffer += sizeof(struct snd_ctl_event);
1885 count -= sizeof(struct snd_ctl_event);
1886 result += sizeof(struct snd_ctl_event);
1889 spin_unlock_irq(&ctl->read_lock);
1891 return result > 0 ? result : err;
1894 static __poll_t snd_ctl_poll(struct file *file, poll_table * wait)
1897 struct snd_ctl_file *ctl;
1899 ctl = file->private_data;
1900 if (!ctl->subscribed)
1902 poll_wait(file, &ctl->change_sleep, wait);
1905 if (!list_empty(&ctl->events))
1906 mask |= EPOLLIN | EPOLLRDNORM;
1912 * register the device-specific control-ioctls.
1913 * called from each device manager like pcm.c, hwdep.c, etc.
1915 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1917 struct snd_kctl_ioctl *pn;
1919 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
1923 down_write(&snd_ioctl_rwsem);
1924 list_add_tail(&pn->list, lists);
1925 up_write(&snd_ioctl_rwsem);
1930 * snd_ctl_register_ioctl - register the device-specific control-ioctls
1931 * @fcn: ioctl callback function
1933 * called from each device manager like pcm.c, hwdep.c, etc.
1935 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1937 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1939 EXPORT_SYMBOL(snd_ctl_register_ioctl);
1941 #ifdef CONFIG_COMPAT
1943 * snd_ctl_register_ioctl_compat - register the device-specific 32bit compat
1945 * @fcn: ioctl callback function
1947 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1949 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1951 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
1955 * de-register the device-specific control-ioctls.
1957 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1958 struct list_head *lists)
1960 struct snd_kctl_ioctl *p;
1962 if (snd_BUG_ON(!fcn))
1964 down_write(&snd_ioctl_rwsem);
1965 list_for_each_entry(p, lists, list) {
1966 if (p->fioctl == fcn) {
1968 up_write(&snd_ioctl_rwsem);
1973 up_write(&snd_ioctl_rwsem);
1979 * snd_ctl_unregister_ioctl - de-register the device-specific control-ioctls
1980 * @fcn: ioctl callback function to unregister
1982 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1984 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1986 EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1988 #ifdef CONFIG_COMPAT
1990 * snd_ctl_unregister_ioctl_compat - de-register the device-specific compat
1991 * 32bit control-ioctls
1992 * @fcn: ioctl callback function to unregister
1994 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1996 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1998 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
2001 static int snd_ctl_fasync(int fd, struct file * file, int on)
2003 struct snd_ctl_file *ctl;
2005 ctl = file->private_data;
2006 return snd_fasync_helper(fd, file, on, &ctl->fasync);
2009 /* return the preferred subdevice number if already assigned;
2010 * otherwise return -1
2012 int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type)
2014 struct snd_ctl_file *kctl;
2016 unsigned long flags;
2018 read_lock_irqsave(&card->ctl_files_rwlock, flags);
2019 list_for_each_entry(kctl, &card->ctl_files, list) {
2020 if (kctl->pid == task_pid(current)) {
2021 subdevice = kctl->preferred_subdevice[type];
2022 if (subdevice != -1)
2026 read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
2029 EXPORT_SYMBOL_GPL(snd_ctl_get_preferred_subdevice);
2034 #ifdef CONFIG_COMPAT
2035 #include "control_compat.c"
2037 #define snd_ctl_ioctl_compat NULL
2041 * control layers (audio LED etc.)
2045 * snd_ctl_request_layer - request to use the layer
2046 * @module_name: Name of the kernel module (NULL == build-in)
2048 * Return an error code when the module cannot be loaded.
2050 int snd_ctl_request_layer(const char *module_name)
2052 struct snd_ctl_layer_ops *lops;
2054 if (module_name == NULL)
2056 down_read(&snd_ctl_layer_rwsem);
2057 for (lops = snd_ctl_layer; lops; lops = lops->next)
2058 if (strcmp(lops->module_name, module_name) == 0)
2060 up_read(&snd_ctl_layer_rwsem);
2063 return request_module(module_name);
2065 EXPORT_SYMBOL_GPL(snd_ctl_request_layer);
2068 * snd_ctl_register_layer - register new control layer
2069 * @lops: operation structure
2071 * The new layer can track all control elements and do additional
2072 * operations on top (like audio LED handling).
2074 void snd_ctl_register_layer(struct snd_ctl_layer_ops *lops)
2076 struct snd_card *card;
2079 down_write(&snd_ctl_layer_rwsem);
2080 lops->next = snd_ctl_layer;
2081 snd_ctl_layer = lops;
2082 up_write(&snd_ctl_layer_rwsem);
2083 for (card_number = 0; card_number < SNDRV_CARDS; card_number++) {
2084 card = snd_card_ref(card_number);
2086 down_read(&card->controls_rwsem);
2087 lops->lregister(card);
2088 up_read(&card->controls_rwsem);
2089 snd_card_unref(card);
2093 EXPORT_SYMBOL_GPL(snd_ctl_register_layer);
2096 * snd_ctl_disconnect_layer - disconnect control layer
2097 * @lops: operation structure
2099 * It is expected that the information about tracked cards
2100 * is freed before this call (the disconnect callback is
2103 void snd_ctl_disconnect_layer(struct snd_ctl_layer_ops *lops)
2105 struct snd_ctl_layer_ops *lops2, *prev_lops2;
2107 down_write(&snd_ctl_layer_rwsem);
2108 for (lops2 = snd_ctl_layer, prev_lops2 = NULL; lops2; lops2 = lops2->next) {
2109 if (lops2 == lops) {
2111 snd_ctl_layer = lops->next;
2113 prev_lops2->next = lops->next;
2118 up_write(&snd_ctl_layer_rwsem);
2120 EXPORT_SYMBOL_GPL(snd_ctl_disconnect_layer);
2126 static const struct file_operations snd_ctl_f_ops =
2128 .owner = THIS_MODULE,
2129 .read = snd_ctl_read,
2130 .open = snd_ctl_open,
2131 .release = snd_ctl_release,
2132 .llseek = no_llseek,
2133 .poll = snd_ctl_poll,
2134 .unlocked_ioctl = snd_ctl_ioctl,
2135 .compat_ioctl = snd_ctl_ioctl_compat,
2136 .fasync = snd_ctl_fasync,
2140 * registration of the control device
2142 static int snd_ctl_dev_register(struct snd_device *device)
2144 struct snd_card *card = device->device_data;
2145 struct snd_ctl_layer_ops *lops;
2148 err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
2149 &snd_ctl_f_ops, card, &card->ctl_dev);
2152 down_read(&card->controls_rwsem);
2153 down_read(&snd_ctl_layer_rwsem);
2154 for (lops = snd_ctl_layer; lops; lops = lops->next)
2155 lops->lregister(card);
2156 up_read(&snd_ctl_layer_rwsem);
2157 up_read(&card->controls_rwsem);
2162 * disconnection of the control device
2164 static int snd_ctl_dev_disconnect(struct snd_device *device)
2166 struct snd_card *card = device->device_data;
2167 struct snd_ctl_file *ctl;
2168 struct snd_ctl_layer_ops *lops;
2169 unsigned long flags;
2171 read_lock_irqsave(&card->ctl_files_rwlock, flags);
2172 list_for_each_entry(ctl, &card->ctl_files, list) {
2173 wake_up(&ctl->change_sleep);
2174 snd_kill_fasync(ctl->fasync, SIGIO, POLL_ERR);
2176 read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
2178 down_read(&card->controls_rwsem);
2179 down_read(&snd_ctl_layer_rwsem);
2180 for (lops = snd_ctl_layer; lops; lops = lops->next)
2181 lops->ldisconnect(card);
2182 up_read(&snd_ctl_layer_rwsem);
2183 up_read(&card->controls_rwsem);
2185 return snd_unregister_device(&card->ctl_dev);
2191 static int snd_ctl_dev_free(struct snd_device *device)
2193 struct snd_card *card = device->device_data;
2194 struct snd_kcontrol *control;
2196 down_write(&card->controls_rwsem);
2197 while (!list_empty(&card->controls)) {
2198 control = snd_kcontrol(card->controls.next);
2199 snd_ctl_remove(card, control);
2201 up_write(&card->controls_rwsem);
2202 put_device(&card->ctl_dev);
2207 * create control core:
2208 * called from init.c
2210 int snd_ctl_create(struct snd_card *card)
2212 static const struct snd_device_ops ops = {
2213 .dev_free = snd_ctl_dev_free,
2214 .dev_register = snd_ctl_dev_register,
2215 .dev_disconnect = snd_ctl_dev_disconnect,
2219 if (snd_BUG_ON(!card))
2221 if (snd_BUG_ON(card->number < 0 || card->number >= SNDRV_CARDS))
2224 snd_device_initialize(&card->ctl_dev, card);
2225 dev_set_name(&card->ctl_dev, "controlC%d", card->number);
2227 err = snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
2229 put_device(&card->ctl_dev);
2234 * Frequently used control callbacks/helpers
2238 * snd_ctl_boolean_mono_info - Helper function for a standard boolean info
2239 * callback with a mono channel
2240 * @kcontrol: the kcontrol instance
2241 * @uinfo: info to store
2243 * This is a function that can be used as info callback for a standard
2244 * boolean control with a single mono channel.
2246 int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
2247 struct snd_ctl_elem_info *uinfo)
2249 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2251 uinfo->value.integer.min = 0;
2252 uinfo->value.integer.max = 1;
2255 EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
2258 * snd_ctl_boolean_stereo_info - Helper function for a standard boolean info
2259 * callback with stereo two channels
2260 * @kcontrol: the kcontrol instance
2261 * @uinfo: info to store
2263 * This is a function that can be used as info callback for a standard
2264 * boolean control with stereo two channels.
2266 int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
2267 struct snd_ctl_elem_info *uinfo)
2269 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2271 uinfo->value.integer.min = 0;
2272 uinfo->value.integer.max = 1;
2275 EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
2278 * snd_ctl_enum_info - fills the info structure for an enumerated control
2279 * @info: the structure to be filled
2280 * @channels: the number of the control's channels; often one
2281 * @items: the number of control values; also the size of @names
2282 * @names: an array containing the names of all control values
2284 * Sets all required fields in @info to their appropriate values.
2285 * If the control's accessibility is not the default (readable and writable),
2286 * the caller has to fill @info->access.
2290 int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
2291 unsigned int items, const char *const names[])
2293 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2294 info->count = channels;
2295 info->value.enumerated.items = items;
2298 if (info->value.enumerated.item >= items)
2299 info->value.enumerated.item = items - 1;
2300 WARN(strlen(names[info->value.enumerated.item]) >= sizeof(info->value.enumerated.name),
2301 "ALSA: too long item name '%s'\n",
2302 names[info->value.enumerated.item]);
2303 strscpy(info->value.enumerated.name,
2304 names[info->value.enumerated.item],
2305 sizeof(info->value.enumerated.name));
2308 EXPORT_SYMBOL(snd_ctl_enum_info);