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/slab.h>
11 #include <linux/vmalloc.h>
12 #include <linux/time.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>
21 /* max number of user-defined controls */
22 #define MAX_USER_CONTROLS 32
23 #define MAX_CONTROL_COUNT 1028
25 struct snd_kctl_ioctl {
26 struct list_head list; /* list of all ioctls */
27 snd_kctl_ioctl_func_t fioctl;
30 static DECLARE_RWSEM(snd_ioctl_rwsem);
31 static LIST_HEAD(snd_control_ioctls);
33 static LIST_HEAD(snd_control_compat_ioctls);
36 static int snd_ctl_open(struct inode *inode, struct file *file)
39 struct snd_card *card;
40 struct snd_ctl_file *ctl;
43 err = stream_open(inode, file);
47 card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
52 err = snd_card_file_add(card, file);
57 if (!try_module_get(card->module)) {
61 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
66 INIT_LIST_HEAD(&ctl->events);
67 init_waitqueue_head(&ctl->change_sleep);
68 spin_lock_init(&ctl->read_lock);
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);
81 module_put(card->module);
83 snd_card_file_remove(card, file);
90 static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
93 struct snd_kctl_event *cread;
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);
101 spin_unlock_irqrestore(&ctl->read_lock, flags);
104 static int snd_ctl_release(struct inode *inode, struct file *file)
107 struct snd_card *card;
108 struct snd_ctl_file *ctl;
109 struct snd_kcontrol *control;
112 ctl = file->private_data;
113 file->private_data = NULL;
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_ctl_empty_read_queue(ctl);
127 module_put(card->module);
128 snd_card_file_remove(card, file);
133 * snd_ctl_notify - Send notification to user-space for a control change
134 * @card: the card to send notification
135 * @mask: the event mask, SNDRV_CTL_EVENT_*
136 * @id: the ctl element id to send notification
138 * This function adds an event record with the given id and mask, appends
139 * to the list and wakes up the user-space for notification. This can be
140 * called in the atomic context.
142 void snd_ctl_notify(struct snd_card *card, unsigned int mask,
143 struct snd_ctl_elem_id *id)
146 struct snd_ctl_file *ctl;
147 struct snd_kctl_event *ev;
149 if (snd_BUG_ON(!card || !id))
153 read_lock_irqsave(&card->ctl_files_rwlock, flags);
154 #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
155 card->mixer_oss_change_count++;
157 list_for_each_entry(ctl, &card->ctl_files, list) {
158 if (!ctl->subscribed)
160 spin_lock(&ctl->read_lock);
161 list_for_each_entry(ev, &ctl->events, list) {
162 if (ev->id.numid == id->numid) {
167 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
171 list_add_tail(&ev->list, &ctl->events);
173 dev_err(card->dev, "No memory available to allocate event\n");
176 wake_up(&ctl->change_sleep);
177 spin_unlock(&ctl->read_lock);
178 kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
180 read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
182 EXPORT_SYMBOL(snd_ctl_notify);
185 * snd_ctl_notify_one - Send notification to user-space for a control change
186 * @card: the card to send notification
187 * @mask: the event mask, SNDRV_CTL_EVENT_*
188 * @kctl: the pointer with the control instance
189 * @ioff: the additional offset to the control index
191 * This function calls snd_ctl_notify() and does additional jobs
192 * like LED state changes.
194 void snd_ctl_notify_one(struct snd_card *card, unsigned int mask,
195 struct snd_kcontrol *kctl, unsigned int ioff)
197 struct snd_ctl_elem_id id = kctl->id;
201 snd_ctl_notify(card, mask, &id);
203 EXPORT_SYMBOL(snd_ctl_notify_one);
206 * snd_ctl_new - create a new control instance with some elements
207 * @kctl: the pointer to store new control instance
208 * @count: the number of elements in this control
209 * @access: the default access flags for elements in this control
210 * @file: given when locking these elements
212 * Allocates a memory object for a new control instance. The instance has
213 * elements as many as the given number (@count). Each element has given
214 * access permissions (@access). Each element is locked when @file is given.
216 * Return: 0 on success, error code on failure
218 static int snd_ctl_new(struct snd_kcontrol **kctl, unsigned int count,
219 unsigned int access, struct snd_ctl_file *file)
223 if (count == 0 || count > MAX_CONTROL_COUNT)
226 *kctl = kzalloc(struct_size(*kctl, vd, count), GFP_KERNEL);
230 for (idx = 0; idx < count; idx++) {
231 (*kctl)->vd[idx].access = access;
232 (*kctl)->vd[idx].owner = file;
234 (*kctl)->count = count;
240 * snd_ctl_new1 - create a control instance from the template
241 * @ncontrol: the initialization record
242 * @private_data: the private data to set
244 * Allocates a new struct snd_kcontrol instance and initialize from the given
245 * template. When the access field of ncontrol is 0, it's assumed as
246 * READWRITE access. When the count field is 0, it's assumes as one.
248 * Return: The pointer of the newly generated instance, or %NULL on failure.
250 struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
253 struct snd_kcontrol *kctl;
258 if (snd_BUG_ON(!ncontrol || !ncontrol->info))
261 count = ncontrol->count;
265 access = ncontrol->access;
267 access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
268 access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
269 SNDRV_CTL_ELEM_ACCESS_VOLATILE |
270 SNDRV_CTL_ELEM_ACCESS_INACTIVE |
271 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE |
272 SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND |
273 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK |
274 SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK);
276 err = snd_ctl_new(&kctl, count, access, NULL);
280 /* The 'numid' member is decided when calling snd_ctl_add(). */
281 kctl->id.iface = ncontrol->iface;
282 kctl->id.device = ncontrol->device;
283 kctl->id.subdevice = ncontrol->subdevice;
284 if (ncontrol->name) {
285 strscpy(kctl->id.name, ncontrol->name, sizeof(kctl->id.name));
286 if (strcmp(ncontrol->name, kctl->id.name) != 0)
287 pr_warn("ALSA: Control name '%s' truncated to '%s'\n",
288 ncontrol->name, kctl->id.name);
290 kctl->id.index = ncontrol->index;
292 kctl->info = ncontrol->info;
293 kctl->get = ncontrol->get;
294 kctl->put = ncontrol->put;
295 kctl->tlv.p = ncontrol->tlv.p;
297 kctl->private_value = ncontrol->private_value;
298 kctl->private_data = private_data;
302 EXPORT_SYMBOL(snd_ctl_new1);
305 * snd_ctl_free_one - release the control instance
306 * @kcontrol: the control instance
308 * Releases the control instance created via snd_ctl_new()
310 * Don't call this after the control was added to the card.
312 void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
315 if (kcontrol->private_free)
316 kcontrol->private_free(kcontrol);
320 EXPORT_SYMBOL(snd_ctl_free_one);
322 static bool snd_ctl_remove_numid_conflict(struct snd_card *card,
325 struct snd_kcontrol *kctl;
327 /* Make sure that the ids assigned to the control do not wrap around */
328 if (card->last_numid >= UINT_MAX - count)
329 card->last_numid = 0;
331 list_for_each_entry(kctl, &card->controls, list) {
332 if (kctl->id.numid < card->last_numid + 1 + count &&
333 kctl->id.numid + kctl->count > card->last_numid + 1) {
334 card->last_numid = kctl->id.numid + kctl->count - 1;
341 static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
343 unsigned int iter = 100000;
345 while (snd_ctl_remove_numid_conflict(card, count)) {
347 /* this situation is very unlikely */
348 dev_err(card->dev, "unable to allocate new control numid\n");
355 enum snd_ctl_add_mode {
356 CTL_ADD_EXCLUSIVE, CTL_REPLACE, CTL_ADD_ON_REPLACE,
359 /* add/replace a new kcontrol object; call with card->controls_rwsem locked */
360 static int __snd_ctl_add_replace(struct snd_card *card,
361 struct snd_kcontrol *kcontrol,
362 enum snd_ctl_add_mode mode)
364 struct snd_ctl_elem_id id;
366 struct snd_kcontrol *old;
370 if (id.index > UINT_MAX - kcontrol->count)
373 old = snd_ctl_find_id(card, &id);
375 if (mode == CTL_REPLACE)
378 if (mode == CTL_ADD_EXCLUSIVE) {
380 "control %i:%i:%i:%s:%i is already present\n",
381 id.iface, id.device, id.subdevice, id.name,
386 err = snd_ctl_remove(card, old);
391 if (snd_ctl_find_hole(card, kcontrol->count) < 0)
394 list_add_tail(&kcontrol->list, &card->controls);
395 card->controls_count += kcontrol->count;
396 kcontrol->id.numid = card->last_numid + 1;
397 card->last_numid += kcontrol->count;
399 for (idx = 0; idx < kcontrol->count; idx++)
400 snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_ADD, kcontrol, idx);
405 static int snd_ctl_add_replace(struct snd_card *card,
406 struct snd_kcontrol *kcontrol,
407 enum snd_ctl_add_mode mode)
413 if (snd_BUG_ON(!card || !kcontrol->info))
416 down_write(&card->controls_rwsem);
417 err = __snd_ctl_add_replace(card, kcontrol, mode);
418 up_write(&card->controls_rwsem);
424 snd_ctl_free_one(kcontrol);
429 * snd_ctl_add - add the control instance to the card
430 * @card: the card instance
431 * @kcontrol: the control instance to add
433 * Adds the control instance created via snd_ctl_new() or
434 * snd_ctl_new1() to the given card. Assigns also an unique
435 * numid used for fast search.
437 * It frees automatically the control which cannot be added.
439 * Return: Zero if successful, or a negative error code on failure.
442 int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
444 return snd_ctl_add_replace(card, kcontrol, CTL_ADD_EXCLUSIVE);
446 EXPORT_SYMBOL(snd_ctl_add);
449 * snd_ctl_replace - replace the control instance of the card
450 * @card: the card instance
451 * @kcontrol: the control instance to replace
452 * @add_on_replace: add the control if not already added
454 * Replaces the given control. If the given control does not exist
455 * and the add_on_replace flag is set, the control is added. If the
456 * control exists, it is destroyed first.
458 * It frees automatically the control which cannot be added or replaced.
460 * Return: Zero if successful, or a negative error code on failure.
462 int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol,
465 return snd_ctl_add_replace(card, kcontrol,
466 add_on_replace ? CTL_ADD_ON_REPLACE : CTL_REPLACE);
468 EXPORT_SYMBOL(snd_ctl_replace);
471 * snd_ctl_remove - remove the control from the card and release it
472 * @card: the card instance
473 * @kcontrol: the control instance to remove
475 * Removes the control from the card and then releases the instance.
476 * You don't need to call snd_ctl_free_one(). You must be in
477 * the write lock - down_write(&card->controls_rwsem).
479 * Return: 0 if successful, or a negative error code on failure.
481 int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
485 if (snd_BUG_ON(!card || !kcontrol))
487 list_del(&kcontrol->list);
488 card->controls_count -= kcontrol->count;
489 for (idx = 0; idx < kcontrol->count; idx++)
490 snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_REMOVE, kcontrol, idx);
491 snd_ctl_free_one(kcontrol);
494 EXPORT_SYMBOL(snd_ctl_remove);
497 * snd_ctl_remove_id - remove the control of the given id and release it
498 * @card: the card instance
499 * @id: the control id to remove
501 * Finds the control instance with the given id, removes it from the
502 * card list and releases it.
504 * Return: 0 if successful, or a negative error code on failure.
506 int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
508 struct snd_kcontrol *kctl;
511 down_write(&card->controls_rwsem);
512 kctl = snd_ctl_find_id(card, id);
514 up_write(&card->controls_rwsem);
517 ret = snd_ctl_remove(card, kctl);
518 up_write(&card->controls_rwsem);
521 EXPORT_SYMBOL(snd_ctl_remove_id);
524 * snd_ctl_remove_user_ctl - remove and release the unlocked user control
525 * @file: active control handle
526 * @id: the control id to remove
528 * Finds the control instance with the given id, removes it from the
529 * card list and releases it.
531 * Return: 0 if successful, or a negative error code on failure.
533 static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
534 struct snd_ctl_elem_id *id)
536 struct snd_card *card = file->card;
537 struct snd_kcontrol *kctl;
540 down_write(&card->controls_rwsem);
541 kctl = snd_ctl_find_id(card, id);
546 if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
550 for (idx = 0; idx < kctl->count; idx++)
551 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
555 ret = snd_ctl_remove(card, kctl);
558 card->user_ctl_count--;
560 up_write(&card->controls_rwsem);
565 * snd_ctl_activate_id - activate/inactivate the control of the given id
566 * @card: the card instance
567 * @id: the control id to activate/inactivate
568 * @active: non-zero to activate
570 * Finds the control instance with the given id, and activate or
571 * inactivate the control together with notification, if changed.
572 * The given ID data is filled with full information.
574 * Return: 0 if unchanged, 1 if changed, or a negative error code on failure.
576 int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
579 struct snd_kcontrol *kctl;
580 struct snd_kcontrol_volatile *vd;
581 unsigned int index_offset;
584 down_write(&card->controls_rwsem);
585 kctl = snd_ctl_find_id(card, id);
590 index_offset = snd_ctl_get_ioff(kctl, id);
591 vd = &kctl->vd[index_offset];
594 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
596 vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
598 if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)
600 vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
602 snd_ctl_build_ioff(id, kctl, index_offset);
603 downgrade_write(&card->controls_rwsem);
604 snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_INFO, kctl, index_offset);
605 up_read(&card->controls_rwsem);
609 up_write(&card->controls_rwsem);
612 EXPORT_SYMBOL_GPL(snd_ctl_activate_id);
615 * snd_ctl_rename_id - replace the id of a control on the card
616 * @card: the card instance
617 * @src_id: the old id
618 * @dst_id: the new id
620 * Finds the control with the old id from the card, and replaces the
621 * id with the new one.
623 * Return: Zero if successful, or a negative error code on failure.
625 int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
626 struct snd_ctl_elem_id *dst_id)
628 struct snd_kcontrol *kctl;
630 down_write(&card->controls_rwsem);
631 kctl = snd_ctl_find_id(card, src_id);
633 up_write(&card->controls_rwsem);
637 kctl->id.numid = card->last_numid + 1;
638 card->last_numid += kctl->count;
639 up_write(&card->controls_rwsem);
642 EXPORT_SYMBOL(snd_ctl_rename_id);
645 * snd_ctl_find_numid - find the control instance with the given number-id
646 * @card: the card instance
647 * @numid: the number-id to search
649 * Finds the control instance with the given number-id from the card.
651 * The caller must down card->controls_rwsem before calling this function
652 * (if the race condition can happen).
654 * Return: The pointer of the instance if found, or %NULL if not.
657 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
659 struct snd_kcontrol *kctl;
661 if (snd_BUG_ON(!card || !numid))
663 list_for_each_entry(kctl, &card->controls, list) {
664 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
669 EXPORT_SYMBOL(snd_ctl_find_numid);
672 * snd_ctl_find_id - find the control instance with the given id
673 * @card: the card instance
674 * @id: the id to search
676 * Finds the control instance with the given id from the card.
678 * The caller must down card->controls_rwsem before calling this function
679 * (if the race condition can happen).
681 * Return: The pointer of the instance if found, or %NULL if not.
684 struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
685 struct snd_ctl_elem_id *id)
687 struct snd_kcontrol *kctl;
689 if (snd_BUG_ON(!card || !id))
692 return snd_ctl_find_numid(card, id->numid);
693 list_for_each_entry(kctl, &card->controls, list) {
694 if (kctl->id.iface != id->iface)
696 if (kctl->id.device != id->device)
698 if (kctl->id.subdevice != id->subdevice)
700 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
702 if (kctl->id.index > id->index)
704 if (kctl->id.index + kctl->count <= id->index)
710 EXPORT_SYMBOL(snd_ctl_find_id);
712 static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
713 unsigned int cmd, void __user *arg)
715 struct snd_ctl_card_info *info;
717 info = kzalloc(sizeof(*info), GFP_KERNEL);
720 down_read(&snd_ioctl_rwsem);
721 info->card = card->number;
722 strscpy(info->id, card->id, sizeof(info->id));
723 strscpy(info->driver, card->driver, sizeof(info->driver));
724 strscpy(info->name, card->shortname, sizeof(info->name));
725 strscpy(info->longname, card->longname, sizeof(info->longname));
726 strscpy(info->mixername, card->mixername, sizeof(info->mixername));
727 strscpy(info->components, card->components, sizeof(info->components));
728 up_read(&snd_ioctl_rwsem);
729 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
737 static int snd_ctl_elem_list(struct snd_card *card,
738 struct snd_ctl_elem_list *list)
740 struct snd_kcontrol *kctl;
741 struct snd_ctl_elem_id id;
742 unsigned int offset, space, jidx;
745 offset = list->offset;
748 down_read(&card->controls_rwsem);
749 list->count = card->controls_count;
752 list_for_each_entry(kctl, &card->controls, list) {
753 if (offset >= kctl->count) {
754 offset -= kctl->count;
757 for (jidx = offset; jidx < kctl->count; jidx++) {
758 snd_ctl_build_ioff(&id, kctl, jidx);
759 if (copy_to_user(list->pids + list->used, &id,
772 up_read(&card->controls_rwsem);
776 static int snd_ctl_elem_list_user(struct snd_card *card,
777 struct snd_ctl_elem_list __user *_list)
779 struct snd_ctl_elem_list list;
782 if (copy_from_user(&list, _list, sizeof(list)))
784 err = snd_ctl_elem_list(card, &list);
787 if (copy_to_user(_list, &list, sizeof(list)))
793 /* Check whether the given kctl info is valid */
794 static int snd_ctl_check_elem_info(struct snd_card *card,
795 const struct snd_ctl_elem_info *info)
797 static const unsigned int max_value_counts[] = {
798 [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = 128,
799 [SNDRV_CTL_ELEM_TYPE_INTEGER] = 128,
800 [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = 128,
801 [SNDRV_CTL_ELEM_TYPE_BYTES] = 512,
802 [SNDRV_CTL_ELEM_TYPE_IEC958] = 1,
803 [SNDRV_CTL_ELEM_TYPE_INTEGER64] = 64,
806 if (info->type < SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
807 info->type > SNDRV_CTL_ELEM_TYPE_INTEGER64) {
810 "control %i:%i:%i:%s:%i: invalid type %d\n",
811 info->id.iface, info->id.device,
812 info->id.subdevice, info->id.name,
813 info->id.index, info->type);
816 if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED &&
817 info->value.enumerated.items == 0) {
820 "control %i:%i:%i:%s:%i: zero enum items\n",
821 info->id.iface, info->id.device,
822 info->id.subdevice, info->id.name,
826 if (info->count > max_value_counts[info->type]) {
829 "control %i:%i:%i:%s:%i: invalid count %d\n",
830 info->id.iface, info->id.device,
831 info->id.subdevice, info->id.name,
832 info->id.index, info->count);
839 /* The capacity of struct snd_ctl_elem_value.value.*/
840 static const unsigned int value_sizes[] = {
841 [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = sizeof(long),
842 [SNDRV_CTL_ELEM_TYPE_INTEGER] = sizeof(long),
843 [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = sizeof(unsigned int),
844 [SNDRV_CTL_ELEM_TYPE_BYTES] = sizeof(unsigned char),
845 [SNDRV_CTL_ELEM_TYPE_IEC958] = sizeof(struct snd_aes_iec958),
846 [SNDRV_CTL_ELEM_TYPE_INTEGER64] = sizeof(long long),
849 #ifdef CONFIG_SND_CTL_VALIDATION
850 /* fill the remaining snd_ctl_elem_value data with the given pattern */
851 static void fill_remaining_elem_value(struct snd_ctl_elem_value *control,
852 struct snd_ctl_elem_info *info,
855 size_t offset = value_sizes[info->type] * info->count;
857 offset = DIV_ROUND_UP(offset, sizeof(u32));
858 memset32((u32 *)control->value.bytes.data + offset, pattern,
859 sizeof(control->value) / sizeof(u32) - offset);
862 /* check whether the given integer ctl value is valid */
863 static int sanity_check_int_value(struct snd_card *card,
864 const struct snd_ctl_elem_value *control,
865 const struct snd_ctl_elem_info *info,
868 long long lval, lmin, lmax, lstep;
871 switch (info->type) {
873 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
874 lval = control->value.integer.value[i];
879 case SNDRV_CTL_ELEM_TYPE_INTEGER:
880 lval = control->value.integer.value[i];
881 lmin = info->value.integer.min;
882 lmax = info->value.integer.max;
883 lstep = info->value.integer.step;
885 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
886 lval = control->value.integer64.value[i];
887 lmin = info->value.integer64.min;
888 lmax = info->value.integer64.max;
889 lstep = info->value.integer64.step;
891 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
892 lval = control->value.enumerated.item[i];
894 lmax = info->value.enumerated.items - 1;
899 if (lval < lmin || lval > lmax) {
901 "control %i:%i:%i:%s:%i: value out of range %lld (%lld/%lld) at count %i\n",
902 control->id.iface, control->id.device,
903 control->id.subdevice, control->id.name,
904 control->id.index, lval, lmin, lmax, i);
908 div64_u64_rem(lval, lstep, &rem);
911 "control %i:%i:%i:%s:%i: unaligned value %lld (step %lld) at count %i\n",
912 control->id.iface, control->id.device,
913 control->id.subdevice, control->id.name,
914 control->id.index, lval, lstep, i);
922 /* perform sanity checks to the given snd_ctl_elem_value object */
923 static int sanity_check_elem_value(struct snd_card *card,
924 const struct snd_ctl_elem_value *control,
925 const struct snd_ctl_elem_info *info,
932 switch (info->type) {
933 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
934 case SNDRV_CTL_ELEM_TYPE_INTEGER:
935 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
936 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
937 for (i = 0; i < info->count; i++) {
938 ret = sanity_check_int_value(card, control, info, i);
947 /* check whether the remaining area kept untouched */
948 offset = value_sizes[info->type] * info->count;
949 offset = DIV_ROUND_UP(offset, sizeof(u32));
950 p = (u32 *)control->value.bytes.data + offset;
951 for (; offset < sizeof(control->value) / sizeof(u32); offset++, p++) {
956 *p = 0; /* clear the checked area */
962 static inline void fill_remaining_elem_value(struct snd_ctl_elem_value *control,
963 struct snd_ctl_elem_info *info,
968 static inline int sanity_check_elem_value(struct snd_card *card,
969 struct snd_ctl_elem_value *control,
970 struct snd_ctl_elem_info *info,
977 static int __snd_ctl_elem_info(struct snd_card *card,
978 struct snd_kcontrol *kctl,
979 struct snd_ctl_elem_info *info,
980 struct snd_ctl_file *ctl)
982 struct snd_kcontrol_volatile *vd;
983 unsigned int index_offset;
986 #ifdef CONFIG_SND_DEBUG
989 result = kctl->info(kctl, info);
991 snd_BUG_ON(info->access);
992 index_offset = snd_ctl_get_ioff(kctl, &info->id);
993 vd = &kctl->vd[index_offset];
994 snd_ctl_build_ioff(&info->id, kctl, index_offset);
995 info->access = vd->access;
997 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
998 if (vd->owner == ctl)
999 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
1000 info->owner = pid_vnr(vd->owner->pid);
1004 if (!snd_ctl_skip_validation(info) &&
1005 snd_ctl_check_elem_info(card, info) < 0)
1011 static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
1012 struct snd_ctl_elem_info *info)
1014 struct snd_card *card = ctl->card;
1015 struct snd_kcontrol *kctl;
1018 down_read(&card->controls_rwsem);
1019 kctl = snd_ctl_find_id(card, &info->id);
1023 result = __snd_ctl_elem_info(card, kctl, info, ctl);
1024 up_read(&card->controls_rwsem);
1028 static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
1029 struct snd_ctl_elem_info __user *_info)
1031 struct snd_ctl_elem_info info;
1034 if (copy_from_user(&info, _info, sizeof(info)))
1036 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
1039 result = snd_ctl_elem_info(ctl, &info);
1042 /* drop internal access flags */
1043 info.access &= ~SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK;
1044 if (copy_to_user(_info, &info, sizeof(info)))
1049 static int snd_ctl_elem_read(struct snd_card *card,
1050 struct snd_ctl_elem_value *control)
1052 struct snd_kcontrol *kctl;
1053 struct snd_kcontrol_volatile *vd;
1054 unsigned int index_offset;
1055 struct snd_ctl_elem_info info;
1056 const u32 pattern = 0xdeadbeef;
1059 kctl = snd_ctl_find_id(card, &control->id);
1063 index_offset = snd_ctl_get_ioff(kctl, &control->id);
1064 vd = &kctl->vd[index_offset];
1065 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_READ) || kctl->get == NULL)
1068 snd_ctl_build_ioff(&control->id, kctl, index_offset);
1070 #ifdef CONFIG_SND_CTL_VALIDATION
1071 /* info is needed only for validation */
1072 memset(&info, 0, sizeof(info));
1073 info.id = control->id;
1074 ret = __snd_ctl_elem_info(card, kctl, &info, NULL);
1079 if (!snd_ctl_skip_validation(&info))
1080 fill_remaining_elem_value(control, &info, pattern);
1081 ret = kctl->get(kctl, control);
1084 if (!snd_ctl_skip_validation(&info) &&
1085 sanity_check_elem_value(card, control, &info, pattern) < 0) {
1087 "control %i:%i:%i:%s:%i: access overflow\n",
1088 control->id.iface, control->id.device,
1089 control->id.subdevice, control->id.name,
1096 static int snd_ctl_elem_read_user(struct snd_card *card,
1097 struct snd_ctl_elem_value __user *_control)
1099 struct snd_ctl_elem_value *control;
1102 control = memdup_user(_control, sizeof(*control));
1103 if (IS_ERR(control))
1104 return PTR_ERR(control);
1106 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1110 down_read(&card->controls_rwsem);
1111 result = snd_ctl_elem_read(card, control);
1112 up_read(&card->controls_rwsem);
1116 if (copy_to_user(_control, control, sizeof(*control)))
1123 static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
1124 struct snd_ctl_elem_value *control)
1126 struct snd_kcontrol *kctl;
1127 struct snd_kcontrol_volatile *vd;
1128 unsigned int index_offset;
1131 down_write(&card->controls_rwsem);
1132 kctl = snd_ctl_find_id(card, &control->id);
1134 up_write(&card->controls_rwsem);
1138 index_offset = snd_ctl_get_ioff(kctl, &control->id);
1139 vd = &kctl->vd[index_offset];
1140 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) || kctl->put == NULL ||
1141 (file && vd->owner && vd->owner != file)) {
1142 up_write(&card->controls_rwsem);
1146 snd_ctl_build_ioff(&control->id, kctl, index_offset);
1147 result = kctl->put(kctl, control);
1149 up_write(&card->controls_rwsem);
1154 downgrade_write(&card->controls_rwsem);
1155 snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_VALUE, kctl, index_offset);
1156 up_read(&card->controls_rwsem);
1158 up_write(&card->controls_rwsem);
1164 static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
1165 struct snd_ctl_elem_value __user *_control)
1167 struct snd_ctl_elem_value *control;
1168 struct snd_card *card;
1171 control = memdup_user(_control, sizeof(*control));
1172 if (IS_ERR(control))
1173 return PTR_ERR(control);
1176 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1180 result = snd_ctl_elem_write(card, file, control);
1184 if (copy_to_user(_control, control, sizeof(*control)))
1191 static int snd_ctl_elem_lock(struct snd_ctl_file *file,
1192 struct snd_ctl_elem_id __user *_id)
1194 struct snd_card *card = file->card;
1195 struct snd_ctl_elem_id id;
1196 struct snd_kcontrol *kctl;
1197 struct snd_kcontrol_volatile *vd;
1200 if (copy_from_user(&id, _id, sizeof(id)))
1202 down_write(&card->controls_rwsem);
1203 kctl = snd_ctl_find_id(card, &id);
1207 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1208 if (vd->owner != NULL)
1215 up_write(&card->controls_rwsem);
1219 static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
1220 struct snd_ctl_elem_id __user *_id)
1222 struct snd_card *card = file->card;
1223 struct snd_ctl_elem_id id;
1224 struct snd_kcontrol *kctl;
1225 struct snd_kcontrol_volatile *vd;
1228 if (copy_from_user(&id, _id, sizeof(id)))
1230 down_write(&card->controls_rwsem);
1231 kctl = snd_ctl_find_id(card, &id);
1235 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1236 if (vd->owner == NULL)
1238 else if (vd->owner != file)
1245 up_write(&card->controls_rwsem);
1249 struct user_element {
1250 struct snd_ctl_elem_info info;
1251 struct snd_card *card;
1252 char *elem_data; /* element data */
1253 unsigned long elem_data_size; /* size of element data in bytes */
1254 void *tlv_data; /* TLV data */
1255 unsigned long tlv_data_size; /* TLV data size */
1256 void *priv_data; /* private data (like strings for enumerated type) */
1259 static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
1260 struct snd_ctl_elem_info *uinfo)
1262 struct user_element *ue = kcontrol->private_data;
1263 unsigned int offset;
1265 offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
1267 snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
1272 static int snd_ctl_elem_user_enum_info(struct snd_kcontrol *kcontrol,
1273 struct snd_ctl_elem_info *uinfo)
1275 struct user_element *ue = kcontrol->private_data;
1278 unsigned int offset;
1280 item = uinfo->value.enumerated.item;
1282 offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
1284 snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
1286 item = min(item, uinfo->value.enumerated.items - 1);
1287 uinfo->value.enumerated.item = item;
1289 names = ue->priv_data;
1290 for (; item > 0; --item)
1291 names += strlen(names) + 1;
1292 strcpy(uinfo->value.enumerated.name, names);
1297 static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
1298 struct snd_ctl_elem_value *ucontrol)
1300 struct user_element *ue = kcontrol->private_data;
1301 unsigned int size = ue->elem_data_size;
1302 char *src = ue->elem_data +
1303 snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
1305 memcpy(&ucontrol->value, src, size);
1309 static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
1310 struct snd_ctl_elem_value *ucontrol)
1313 struct user_element *ue = kcontrol->private_data;
1314 unsigned int size = ue->elem_data_size;
1315 char *dst = ue->elem_data +
1316 snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
1318 change = memcmp(&ucontrol->value, dst, size) != 0;
1320 memcpy(dst, &ucontrol->value, size);
1324 static int replace_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
1327 struct user_element *ue = kctl->private_data;
1328 unsigned int *container;
1329 unsigned int mask = 0;
1333 if (size > 1024 * 128) /* sane value */
1336 container = vmemdup_user(buf, size);
1337 if (IS_ERR(container))
1338 return PTR_ERR(container);
1340 change = ue->tlv_data_size != size;
1342 change = memcmp(ue->tlv_data, container, size) != 0;
1348 if (ue->tlv_data == NULL) {
1349 /* Now TLV data is available. */
1350 for (i = 0; i < kctl->count; ++i)
1351 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1352 mask = SNDRV_CTL_EVENT_MASK_INFO;
1355 kvfree(ue->tlv_data);
1356 ue->tlv_data = container;
1357 ue->tlv_data_size = size;
1359 mask |= SNDRV_CTL_EVENT_MASK_TLV;
1360 for (i = 0; i < kctl->count; ++i)
1361 snd_ctl_notify_one(ue->card, mask, kctl, i);
1366 static int read_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
1369 struct user_element *ue = kctl->private_data;
1371 if (ue->tlv_data_size == 0 || ue->tlv_data == NULL)
1374 if (size < ue->tlv_data_size)
1377 if (copy_to_user(buf, ue->tlv_data, ue->tlv_data_size))
1383 static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kctl, int op_flag,
1384 unsigned int size, unsigned int __user *buf)
1386 if (op_flag == SNDRV_CTL_TLV_OP_WRITE)
1387 return replace_user_tlv(kctl, buf, size);
1389 return read_user_tlv(kctl, buf, size);
1392 static int snd_ctl_elem_init_enum_names(struct user_element *ue)
1395 size_t buf_len, name_len;
1397 const uintptr_t user_ptrval = ue->info.value.enumerated.names_ptr;
1399 if (ue->info.value.enumerated.names_length > 64 * 1024)
1402 names = vmemdup_user((const void __user *)user_ptrval,
1403 ue->info.value.enumerated.names_length);
1405 return PTR_ERR(names);
1407 /* check that there are enough valid names */
1408 buf_len = ue->info.value.enumerated.names_length;
1410 for (i = 0; i < ue->info.value.enumerated.items; ++i) {
1411 name_len = strnlen(p, buf_len);
1412 if (name_len == 0 || name_len >= 64 || name_len == buf_len) {
1417 buf_len -= name_len + 1;
1420 ue->priv_data = names;
1421 ue->info.value.enumerated.names_ptr = 0;
1426 static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
1428 struct user_element *ue = kcontrol->private_data;
1430 kvfree(ue->tlv_data);
1431 kvfree(ue->priv_data);
1435 static int snd_ctl_elem_add(struct snd_ctl_file *file,
1436 struct snd_ctl_elem_info *info, int replace)
1438 struct snd_card *card = file->card;
1439 struct snd_kcontrol *kctl;
1441 unsigned int access;
1443 struct user_element *ue;
1444 unsigned int offset;
1447 if (!*info->id.name)
1449 if (strnlen(info->id.name, sizeof(info->id.name)) >= sizeof(info->id.name))
1452 /* Delete a control to replace them if needed. */
1455 err = snd_ctl_remove_user_ctl(file, &info->id);
1461 * The number of userspace controls are counted control by control,
1462 * not element by element.
1464 if (card->user_ctl_count + 1 > MAX_USER_CONTROLS)
1467 /* Check the number of elements for this userspace control. */
1468 count = info->owner;
1472 /* Arrange access permissions if needed. */
1473 access = info->access;
1475 access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1476 access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1477 SNDRV_CTL_ELEM_ACCESS_INACTIVE |
1478 SNDRV_CTL_ELEM_ACCESS_TLV_WRITE);
1480 /* In initial state, nothing is available as TLV container. */
1481 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
1482 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1483 access |= SNDRV_CTL_ELEM_ACCESS_USER;
1486 * Check information and calculate the size of data specific to
1487 * this userspace control.
1489 /* pass NULL to card for suppressing error messages */
1490 err = snd_ctl_check_elem_info(NULL, info);
1493 /* user-space control doesn't allow zero-size data */
1494 if (info->count < 1)
1496 private_size = value_sizes[info->type] * info->count;
1499 * Keep memory object for this userspace control. After passing this
1500 * code block, the instance should be freed by snd_ctl_free_one().
1502 * Note that these elements in this control are locked.
1504 err = snd_ctl_new(&kctl, count, access, file);
1507 memcpy(&kctl->id, &info->id, sizeof(kctl->id));
1508 kctl->private_data = kzalloc(sizeof(struct user_element) + private_size * count,
1510 if (kctl->private_data == NULL) {
1514 kctl->private_free = snd_ctl_elem_user_free;
1516 /* Set private data for this userspace control. */
1517 ue = (struct user_element *)kctl->private_data;
1520 ue->info.access = 0;
1521 ue->elem_data = (char *)ue + sizeof(*ue);
1522 ue->elem_data_size = private_size;
1523 if (ue->info.type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
1524 err = snd_ctl_elem_init_enum_names(ue);
1526 snd_ctl_free_one(kctl);
1531 /* Set callback functions. */
1532 if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED)
1533 kctl->info = snd_ctl_elem_user_enum_info;
1535 kctl->info = snd_ctl_elem_user_info;
1536 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1537 kctl->get = snd_ctl_elem_user_get;
1538 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1539 kctl->put = snd_ctl_elem_user_put;
1540 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
1541 kctl->tlv.c = snd_ctl_elem_user_tlv;
1543 /* This function manage to free the instance on failure. */
1544 down_write(&card->controls_rwsem);
1545 err = __snd_ctl_add_replace(card, kctl, CTL_ADD_EXCLUSIVE);
1547 snd_ctl_free_one(kctl);
1550 offset = snd_ctl_get_ioff(kctl, &info->id);
1551 snd_ctl_build_ioff(&info->id, kctl, offset);
1553 * Here we cannot fill any field for the number of elements added by
1554 * this operation because there're no specific fields. The usage of
1555 * 'owner' field for this purpose may cause any bugs to userspace
1556 * applications because the field originally means PID of a process
1557 * which locks the element.
1560 card->user_ctl_count++;
1563 up_write(&card->controls_rwsem);
1567 static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1568 struct snd_ctl_elem_info __user *_info, int replace)
1570 struct snd_ctl_elem_info info;
1573 if (copy_from_user(&info, _info, sizeof(info)))
1575 err = snd_ctl_elem_add(file, &info, replace);
1578 if (copy_to_user(_info, &info, sizeof(info))) {
1579 snd_ctl_remove_user_ctl(file, &info.id);
1586 static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1587 struct snd_ctl_elem_id __user *_id)
1589 struct snd_ctl_elem_id id;
1591 if (copy_from_user(&id, _id, sizeof(id)))
1593 return snd_ctl_remove_user_ctl(file, &id);
1596 static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
1599 if (get_user(subscribe, ptr))
1601 if (subscribe < 0) {
1602 subscribe = file->subscribed;
1603 if (put_user(subscribe, ptr))
1608 file->subscribed = 1;
1610 } else if (file->subscribed) {
1611 snd_ctl_empty_read_queue(file);
1612 file->subscribed = 0;
1617 static int call_tlv_handler(struct snd_ctl_file *file, int op_flag,
1618 struct snd_kcontrol *kctl,
1619 struct snd_ctl_elem_id *id,
1620 unsigned int __user *buf, unsigned int size)
1622 static const struct {
1626 {SNDRV_CTL_TLV_OP_READ, SNDRV_CTL_ELEM_ACCESS_TLV_READ},
1627 {SNDRV_CTL_TLV_OP_WRITE, SNDRV_CTL_ELEM_ACCESS_TLV_WRITE},
1628 {SNDRV_CTL_TLV_OP_CMD, SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND},
1630 struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
1633 /* Check support of the request for this element. */
1634 for (i = 0; i < ARRAY_SIZE(pairs); ++i) {
1635 if (op_flag == pairs[i].op && (vd->access & pairs[i].perm))
1638 if (i == ARRAY_SIZE(pairs))
1641 if (kctl->tlv.c == NULL)
1644 /* Write and command operations are not allowed for locked element. */
1645 if (op_flag != SNDRV_CTL_TLV_OP_READ &&
1646 vd->owner != NULL && vd->owner != file)
1649 return kctl->tlv.c(kctl, op_flag, size, buf);
1652 static int read_tlv_buf(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id,
1653 unsigned int __user *buf, unsigned int size)
1655 struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
1658 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ))
1661 if (kctl->tlv.p == NULL)
1664 len = sizeof(unsigned int) * 2 + kctl->tlv.p[1];
1668 if (copy_to_user(buf, kctl->tlv.p, len))
1674 static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1675 struct snd_ctl_tlv __user *buf,
1678 struct snd_ctl_tlv header;
1679 unsigned int __user *container;
1680 unsigned int container_size;
1681 struct snd_kcontrol *kctl;
1682 struct snd_ctl_elem_id id;
1683 struct snd_kcontrol_volatile *vd;
1685 if (copy_from_user(&header, buf, sizeof(header)))
1688 /* In design of control core, numerical ID starts at 1. */
1689 if (header.numid == 0)
1692 /* At least, container should include type and length fields. */
1693 if (header.length < sizeof(unsigned int) * 2)
1695 container_size = header.length;
1696 container = buf->tlv;
1698 kctl = snd_ctl_find_numid(file->card, header.numid);
1702 /* Calculate index of the element in this set. */
1704 snd_ctl_build_ioff(&id, kctl, header.numid - id.numid);
1705 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1707 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1708 return call_tlv_handler(file, op_flag, kctl, &id, container,
1711 if (op_flag == SNDRV_CTL_TLV_OP_READ) {
1712 return read_tlv_buf(kctl, &id, container,
1717 /* Not supported. */
1721 static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1723 struct snd_ctl_file *ctl;
1724 struct snd_card *card;
1725 struct snd_kctl_ioctl *p;
1726 void __user *argp = (void __user *)arg;
1727 int __user *ip = argp;
1730 ctl = file->private_data;
1732 if (snd_BUG_ON(!card))
1735 case SNDRV_CTL_IOCTL_PVERSION:
1736 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1737 case SNDRV_CTL_IOCTL_CARD_INFO:
1738 return snd_ctl_card_info(card, ctl, cmd, argp);
1739 case SNDRV_CTL_IOCTL_ELEM_LIST:
1740 return snd_ctl_elem_list_user(card, argp);
1741 case SNDRV_CTL_IOCTL_ELEM_INFO:
1742 return snd_ctl_elem_info_user(ctl, argp);
1743 case SNDRV_CTL_IOCTL_ELEM_READ:
1744 return snd_ctl_elem_read_user(card, argp);
1745 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1746 return snd_ctl_elem_write_user(ctl, argp);
1747 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1748 return snd_ctl_elem_lock(ctl, argp);
1749 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1750 return snd_ctl_elem_unlock(ctl, argp);
1751 case SNDRV_CTL_IOCTL_ELEM_ADD:
1752 return snd_ctl_elem_add_user(ctl, argp, 0);
1753 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1754 return snd_ctl_elem_add_user(ctl, argp, 1);
1755 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1756 return snd_ctl_elem_remove(ctl, argp);
1757 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1758 return snd_ctl_subscribe_events(ctl, ip);
1759 case SNDRV_CTL_IOCTL_TLV_READ:
1760 down_read(&ctl->card->controls_rwsem);
1761 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_READ);
1762 up_read(&ctl->card->controls_rwsem);
1764 case SNDRV_CTL_IOCTL_TLV_WRITE:
1765 down_write(&ctl->card->controls_rwsem);
1766 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_WRITE);
1767 up_write(&ctl->card->controls_rwsem);
1769 case SNDRV_CTL_IOCTL_TLV_COMMAND:
1770 down_write(&ctl->card->controls_rwsem);
1771 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_CMD);
1772 up_write(&ctl->card->controls_rwsem);
1774 case SNDRV_CTL_IOCTL_POWER:
1775 return -ENOPROTOOPT;
1776 case SNDRV_CTL_IOCTL_POWER_STATE:
1778 return put_user(card->power_state, ip) ? -EFAULT : 0;
1780 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1783 down_read(&snd_ioctl_rwsem);
1784 list_for_each_entry(p, &snd_control_ioctls, list) {
1785 err = p->fioctl(card, ctl, cmd, arg);
1786 if (err != -ENOIOCTLCMD) {
1787 up_read(&snd_ioctl_rwsem);
1791 up_read(&snd_ioctl_rwsem);
1792 dev_dbg(card->dev, "unknown ioctl = 0x%x\n", cmd);
1796 static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1797 size_t count, loff_t * offset)
1799 struct snd_ctl_file *ctl;
1803 ctl = file->private_data;
1804 if (snd_BUG_ON(!ctl || !ctl->card))
1806 if (!ctl->subscribed)
1808 if (count < sizeof(struct snd_ctl_event))
1810 spin_lock_irq(&ctl->read_lock);
1811 while (count >= sizeof(struct snd_ctl_event)) {
1812 struct snd_ctl_event ev;
1813 struct snd_kctl_event *kev;
1814 while (list_empty(&ctl->events)) {
1815 wait_queue_entry_t wait;
1816 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1820 init_waitqueue_entry(&wait, current);
1821 add_wait_queue(&ctl->change_sleep, &wait);
1822 set_current_state(TASK_INTERRUPTIBLE);
1823 spin_unlock_irq(&ctl->read_lock);
1825 remove_wait_queue(&ctl->change_sleep, &wait);
1826 if (ctl->card->shutdown)
1828 if (signal_pending(current))
1829 return -ERESTARTSYS;
1830 spin_lock_irq(&ctl->read_lock);
1832 kev = snd_kctl_event(ctl->events.next);
1833 ev.type = SNDRV_CTL_EVENT_ELEM;
1834 ev.data.elem.mask = kev->mask;
1835 ev.data.elem.id = kev->id;
1836 list_del(&kev->list);
1837 spin_unlock_irq(&ctl->read_lock);
1839 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
1843 spin_lock_irq(&ctl->read_lock);
1844 buffer += sizeof(struct snd_ctl_event);
1845 count -= sizeof(struct snd_ctl_event);
1846 result += sizeof(struct snd_ctl_event);
1849 spin_unlock_irq(&ctl->read_lock);
1851 return result > 0 ? result : err;
1854 static __poll_t snd_ctl_poll(struct file *file, poll_table * wait)
1857 struct snd_ctl_file *ctl;
1859 ctl = file->private_data;
1860 if (!ctl->subscribed)
1862 poll_wait(file, &ctl->change_sleep, wait);
1865 if (!list_empty(&ctl->events))
1866 mask |= EPOLLIN | EPOLLRDNORM;
1872 * register the device-specific control-ioctls.
1873 * called from each device manager like pcm.c, hwdep.c, etc.
1875 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1877 struct snd_kctl_ioctl *pn;
1879 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
1883 down_write(&snd_ioctl_rwsem);
1884 list_add_tail(&pn->list, lists);
1885 up_write(&snd_ioctl_rwsem);
1890 * snd_ctl_register_ioctl - register the device-specific control-ioctls
1891 * @fcn: ioctl callback function
1893 * called from each device manager like pcm.c, hwdep.c, etc.
1895 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1897 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1899 EXPORT_SYMBOL(snd_ctl_register_ioctl);
1901 #ifdef CONFIG_COMPAT
1903 * snd_ctl_register_ioctl_compat - register the device-specific 32bit compat
1905 * @fcn: ioctl callback function
1907 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1909 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1911 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
1915 * de-register the device-specific control-ioctls.
1917 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1918 struct list_head *lists)
1920 struct snd_kctl_ioctl *p;
1922 if (snd_BUG_ON(!fcn))
1924 down_write(&snd_ioctl_rwsem);
1925 list_for_each_entry(p, lists, list) {
1926 if (p->fioctl == fcn) {
1928 up_write(&snd_ioctl_rwsem);
1933 up_write(&snd_ioctl_rwsem);
1939 * snd_ctl_unregister_ioctl - de-register the device-specific control-ioctls
1940 * @fcn: ioctl callback function to unregister
1942 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1944 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1946 EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1948 #ifdef CONFIG_COMPAT
1950 * snd_ctl_unregister_ioctl_compat - de-register the device-specific compat
1951 * 32bit control-ioctls
1952 * @fcn: ioctl callback function to unregister
1954 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1956 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1958 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
1961 static int snd_ctl_fasync(int fd, struct file * file, int on)
1963 struct snd_ctl_file *ctl;
1965 ctl = file->private_data;
1966 return fasync_helper(fd, file, on, &ctl->fasync);
1969 /* return the preferred subdevice number if already assigned;
1970 * otherwise return -1
1972 int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type)
1974 struct snd_ctl_file *kctl;
1976 unsigned long flags;
1978 read_lock_irqsave(&card->ctl_files_rwlock, flags);
1979 list_for_each_entry(kctl, &card->ctl_files, list) {
1980 if (kctl->pid == task_pid(current)) {
1981 subdevice = kctl->preferred_subdevice[type];
1982 if (subdevice != -1)
1986 read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
1989 EXPORT_SYMBOL_GPL(snd_ctl_get_preferred_subdevice);
1994 #ifdef CONFIG_COMPAT
1995 #include "control_compat.c"
1997 #define snd_ctl_ioctl_compat NULL
2004 static const struct file_operations snd_ctl_f_ops =
2006 .owner = THIS_MODULE,
2007 .read = snd_ctl_read,
2008 .open = snd_ctl_open,
2009 .release = snd_ctl_release,
2010 .llseek = no_llseek,
2011 .poll = snd_ctl_poll,
2012 .unlocked_ioctl = snd_ctl_ioctl,
2013 .compat_ioctl = snd_ctl_ioctl_compat,
2014 .fasync = snd_ctl_fasync,
2018 * registration of the control device
2020 static int snd_ctl_dev_register(struct snd_device *device)
2022 struct snd_card *card = device->device_data;
2024 return snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
2025 &snd_ctl_f_ops, card, &card->ctl_dev);
2029 * disconnection of the control device
2031 static int snd_ctl_dev_disconnect(struct snd_device *device)
2033 struct snd_card *card = device->device_data;
2034 struct snd_ctl_file *ctl;
2035 unsigned long flags;
2037 read_lock_irqsave(&card->ctl_files_rwlock, flags);
2038 list_for_each_entry(ctl, &card->ctl_files, list) {
2039 wake_up(&ctl->change_sleep);
2040 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
2042 read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
2044 return snd_unregister_device(&card->ctl_dev);
2050 static int snd_ctl_dev_free(struct snd_device *device)
2052 struct snd_card *card = device->device_data;
2053 struct snd_kcontrol *control;
2055 down_write(&card->controls_rwsem);
2056 while (!list_empty(&card->controls)) {
2057 control = snd_kcontrol(card->controls.next);
2058 snd_ctl_remove(card, control);
2060 up_write(&card->controls_rwsem);
2061 put_device(&card->ctl_dev);
2066 * create control core:
2067 * called from init.c
2069 int snd_ctl_create(struct snd_card *card)
2071 static const struct snd_device_ops ops = {
2072 .dev_free = snd_ctl_dev_free,
2073 .dev_register = snd_ctl_dev_register,
2074 .dev_disconnect = snd_ctl_dev_disconnect,
2078 if (snd_BUG_ON(!card))
2080 if (snd_BUG_ON(card->number < 0 || card->number >= SNDRV_CARDS))
2083 snd_device_initialize(&card->ctl_dev, card);
2084 dev_set_name(&card->ctl_dev, "controlC%d", card->number);
2086 err = snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
2088 put_device(&card->ctl_dev);
2093 * Frequently used control callbacks/helpers
2097 * snd_ctl_boolean_mono_info - Helper function for a standard boolean info
2098 * callback with a mono channel
2099 * @kcontrol: the kcontrol instance
2100 * @uinfo: info to store
2102 * This is a function that can be used as info callback for a standard
2103 * boolean control with a single mono channel.
2105 int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
2106 struct snd_ctl_elem_info *uinfo)
2108 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2110 uinfo->value.integer.min = 0;
2111 uinfo->value.integer.max = 1;
2114 EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
2117 * snd_ctl_boolean_stereo_info - Helper function for a standard boolean info
2118 * callback with stereo two channels
2119 * @kcontrol: the kcontrol instance
2120 * @uinfo: info to store
2122 * This is a function that can be used as info callback for a standard
2123 * boolean control with stereo two channels.
2125 int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
2126 struct snd_ctl_elem_info *uinfo)
2128 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2130 uinfo->value.integer.min = 0;
2131 uinfo->value.integer.max = 1;
2134 EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
2137 * snd_ctl_enum_info - fills the info structure for an enumerated control
2138 * @info: the structure to be filled
2139 * @channels: the number of the control's channels; often one
2140 * @items: the number of control values; also the size of @names
2141 * @names: an array containing the names of all control values
2143 * Sets all required fields in @info to their appropriate values.
2144 * If the control's accessibility is not the default (readable and writable),
2145 * the caller has to fill @info->access.
2149 int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
2150 unsigned int items, const char *const names[])
2152 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2153 info->count = channels;
2154 info->value.enumerated.items = items;
2157 if (info->value.enumerated.item >= items)
2158 info->value.enumerated.item = items - 1;
2159 WARN(strlen(names[info->value.enumerated.item]) >= sizeof(info->value.enumerated.name),
2160 "ALSA: too long item name '%s'\n",
2161 names[info->value.enumerated.item]);
2162 strscpy(info->value.enumerated.name,
2163 names[info->value.enumerated.item],
2164 sizeof(info->value.enumerated.name));
2167 EXPORT_SYMBOL(snd_ctl_enum_info);