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_fasync_free(ctl->fasync);
125 snd_ctl_empty_read_queue(ctl);
128 module_put(card->module);
129 snd_card_file_remove(card, file);
134 * snd_ctl_notify - Send notification to user-space for a control change
135 * @card: the card to send notification
136 * @mask: the event mask, SNDRV_CTL_EVENT_*
137 * @id: the ctl element id to send notification
139 * This function adds an event record with the given id and mask, appends
140 * to the list and wakes up the user-space for notification. This can be
141 * called in the atomic context.
143 void snd_ctl_notify(struct snd_card *card, unsigned int mask,
144 struct snd_ctl_elem_id *id)
147 struct snd_ctl_file *ctl;
148 struct snd_kctl_event *ev;
150 if (snd_BUG_ON(!card || !id))
154 read_lock_irqsave(&card->ctl_files_rwlock, flags);
155 #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
156 card->mixer_oss_change_count++;
158 list_for_each_entry(ctl, &card->ctl_files, list) {
159 if (!ctl->subscribed)
161 spin_lock(&ctl->read_lock);
162 list_for_each_entry(ev, &ctl->events, list) {
163 if (ev->id.numid == id->numid) {
168 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
172 list_add_tail(&ev->list, &ctl->events);
174 dev_err(card->dev, "No memory available to allocate event\n");
177 wake_up(&ctl->change_sleep);
178 spin_unlock(&ctl->read_lock);
179 snd_kill_fasync(ctl->fasync, SIGIO, POLL_IN);
181 read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
183 EXPORT_SYMBOL(snd_ctl_notify);
186 * snd_ctl_new - create a new control instance with some elements
187 * @kctl: the pointer to store new control instance
188 * @count: the number of elements in this control
189 * @access: the default access flags for elements in this control
190 * @file: given when locking these elements
192 * Allocates a memory object for a new control instance. The instance has
193 * elements as many as the given number (@count). Each element has given
194 * access permissions (@access). Each element is locked when @file is given.
196 * Return: 0 on success, error code on failure
198 static int snd_ctl_new(struct snd_kcontrol **kctl, unsigned int count,
199 unsigned int access, struct snd_ctl_file *file)
203 if (count == 0 || count > MAX_CONTROL_COUNT)
206 *kctl = kzalloc(struct_size(*kctl, vd, count), GFP_KERNEL);
210 for (idx = 0; idx < count; idx++) {
211 (*kctl)->vd[idx].access = access;
212 (*kctl)->vd[idx].owner = file;
214 (*kctl)->count = count;
220 * snd_ctl_new1 - create a control instance from the template
221 * @ncontrol: the initialization record
222 * @private_data: the private data to set
224 * Allocates a new struct snd_kcontrol instance and initialize from the given
225 * template. When the access field of ncontrol is 0, it's assumed as
226 * READWRITE access. When the count field is 0, it's assumes as one.
228 * Return: The pointer of the newly generated instance, or %NULL on failure.
230 struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
233 struct snd_kcontrol *kctl;
238 if (snd_BUG_ON(!ncontrol || !ncontrol->info))
241 count = ncontrol->count;
245 access = ncontrol->access;
247 access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
248 access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
249 SNDRV_CTL_ELEM_ACCESS_VOLATILE |
250 SNDRV_CTL_ELEM_ACCESS_INACTIVE |
251 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE |
252 SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND |
253 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK |
254 SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK);
256 err = snd_ctl_new(&kctl, count, access, NULL);
260 /* The 'numid' member is decided when calling snd_ctl_add(). */
261 kctl->id.iface = ncontrol->iface;
262 kctl->id.device = ncontrol->device;
263 kctl->id.subdevice = ncontrol->subdevice;
264 if (ncontrol->name) {
265 strlcpy(kctl->id.name, ncontrol->name, sizeof(kctl->id.name));
266 if (strcmp(ncontrol->name, kctl->id.name) != 0)
267 pr_warn("ALSA: Control name '%s' truncated to '%s'\n",
268 ncontrol->name, kctl->id.name);
270 kctl->id.index = ncontrol->index;
272 kctl->info = ncontrol->info;
273 kctl->get = ncontrol->get;
274 kctl->put = ncontrol->put;
275 kctl->tlv.p = ncontrol->tlv.p;
277 kctl->private_value = ncontrol->private_value;
278 kctl->private_data = private_data;
282 EXPORT_SYMBOL(snd_ctl_new1);
285 * snd_ctl_free_one - release the control instance
286 * @kcontrol: the control instance
288 * Releases the control instance created via snd_ctl_new()
290 * Don't call this after the control was added to the card.
292 void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
295 if (kcontrol->private_free)
296 kcontrol->private_free(kcontrol);
300 EXPORT_SYMBOL(snd_ctl_free_one);
302 static bool snd_ctl_remove_numid_conflict(struct snd_card *card,
305 struct snd_kcontrol *kctl;
307 /* Make sure that the ids assigned to the control do not wrap around */
308 if (card->last_numid >= UINT_MAX - count)
309 card->last_numid = 0;
311 list_for_each_entry(kctl, &card->controls, list) {
312 if (kctl->id.numid < card->last_numid + 1 + count &&
313 kctl->id.numid + kctl->count > card->last_numid + 1) {
314 card->last_numid = kctl->id.numid + kctl->count - 1;
321 static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
323 unsigned int iter = 100000;
325 while (snd_ctl_remove_numid_conflict(card, count)) {
327 /* this situation is very unlikely */
328 dev_err(card->dev, "unable to allocate new control numid\n");
335 enum snd_ctl_add_mode {
336 CTL_ADD_EXCLUSIVE, CTL_REPLACE, CTL_ADD_ON_REPLACE,
339 /* add/replace a new kcontrol object; call with card->controls_rwsem locked */
340 static int __snd_ctl_add_replace(struct snd_card *card,
341 struct snd_kcontrol *kcontrol,
342 enum snd_ctl_add_mode mode)
344 struct snd_ctl_elem_id id;
347 struct snd_kcontrol *old;
351 if (id.index > UINT_MAX - kcontrol->count)
354 old = snd_ctl_find_id(card, &id);
356 if (mode == CTL_REPLACE)
359 if (mode == CTL_ADD_EXCLUSIVE) {
361 "control %i:%i:%i:%s:%i is already present\n",
362 id.iface, id.device, id.subdevice, id.name,
367 err = snd_ctl_remove(card, old);
372 if (snd_ctl_find_hole(card, kcontrol->count) < 0)
375 list_add_tail(&kcontrol->list, &card->controls);
376 card->controls_count += kcontrol->count;
377 kcontrol->id.numid = card->last_numid + 1;
378 card->last_numid += kcontrol->count;
381 count = kcontrol->count;
382 for (idx = 0; idx < count; idx++, id.index++, id.numid++)
383 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
388 static int snd_ctl_add_replace(struct snd_card *card,
389 struct snd_kcontrol *kcontrol,
390 enum snd_ctl_add_mode mode)
396 if (snd_BUG_ON(!card || !kcontrol->info))
399 down_write(&card->controls_rwsem);
400 err = __snd_ctl_add_replace(card, kcontrol, mode);
401 up_write(&card->controls_rwsem);
407 snd_ctl_free_one(kcontrol);
412 * snd_ctl_add - add the control instance to the card
413 * @card: the card instance
414 * @kcontrol: the control instance to add
416 * Adds the control instance created via snd_ctl_new() or
417 * snd_ctl_new1() to the given card. Assigns also an unique
418 * numid used for fast search.
420 * It frees automatically the control which cannot be added.
422 * Return: Zero if successful, or a negative error code on failure.
425 int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
427 return snd_ctl_add_replace(card, kcontrol, CTL_ADD_EXCLUSIVE);
429 EXPORT_SYMBOL(snd_ctl_add);
432 * snd_ctl_replace - replace the control instance of the card
433 * @card: the card instance
434 * @kcontrol: the control instance to replace
435 * @add_on_replace: add the control if not already added
437 * Replaces the given control. If the given control does not exist
438 * and the add_on_replace flag is set, the control is added. If the
439 * control exists, it is destroyed first.
441 * It frees automatically the control which cannot be added or replaced.
443 * Return: Zero if successful, or a negative error code on failure.
445 int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol,
448 return snd_ctl_add_replace(card, kcontrol,
449 add_on_replace ? CTL_ADD_ON_REPLACE : CTL_REPLACE);
451 EXPORT_SYMBOL(snd_ctl_replace);
454 * snd_ctl_remove - remove the control from the card and release it
455 * @card: the card instance
456 * @kcontrol: the control instance to remove
458 * Removes the control from the card and then releases the instance.
459 * You don't need to call snd_ctl_free_one(). You must be in
460 * the write lock - down_write(&card->controls_rwsem).
462 * Return: 0 if successful, or a negative error code on failure.
464 int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
466 struct snd_ctl_elem_id id;
469 if (snd_BUG_ON(!card || !kcontrol))
471 list_del(&kcontrol->list);
472 card->controls_count -= kcontrol->count;
474 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
475 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
476 snd_ctl_free_one(kcontrol);
479 EXPORT_SYMBOL(snd_ctl_remove);
482 * snd_ctl_remove_id - remove the control of the given id and release it
483 * @card: the card instance
484 * @id: the control id to remove
486 * Finds the control instance with the given id, removes it from the
487 * card list and releases it.
489 * Return: 0 if successful, or a negative error code on failure.
491 int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
493 struct snd_kcontrol *kctl;
496 down_write(&card->controls_rwsem);
497 kctl = snd_ctl_find_id(card, id);
499 up_write(&card->controls_rwsem);
502 ret = snd_ctl_remove(card, kctl);
503 up_write(&card->controls_rwsem);
506 EXPORT_SYMBOL(snd_ctl_remove_id);
509 * snd_ctl_remove_user_ctl - remove and release the unlocked user control
510 * @file: active control handle
511 * @id: the control id to remove
513 * Finds the control instance with the given id, removes it from the
514 * card list and releases it.
516 * Return: 0 if successful, or a negative error code on failure.
518 static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
519 struct snd_ctl_elem_id *id)
521 struct snd_card *card = file->card;
522 struct snd_kcontrol *kctl;
525 down_write(&card->controls_rwsem);
526 kctl = snd_ctl_find_id(card, id);
531 if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
535 for (idx = 0; idx < kctl->count; idx++)
536 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
540 ret = snd_ctl_remove(card, kctl);
543 card->user_ctl_count--;
545 up_write(&card->controls_rwsem);
550 * snd_ctl_activate_id - activate/inactivate the control of the given id
551 * @card: the card instance
552 * @id: the control id to activate/inactivate
553 * @active: non-zero to activate
555 * Finds the control instance with the given id, and activate or
556 * inactivate the control together with notification, if changed.
557 * The given ID data is filled with full information.
559 * Return: 0 if unchanged, 1 if changed, or a negative error code on failure.
561 int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
564 struct snd_kcontrol *kctl;
565 struct snd_kcontrol_volatile *vd;
566 unsigned int index_offset;
569 down_write(&card->controls_rwsem);
570 kctl = snd_ctl_find_id(card, id);
575 index_offset = snd_ctl_get_ioff(kctl, id);
576 vd = &kctl->vd[index_offset];
579 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
581 vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
583 if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)
585 vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
587 snd_ctl_build_ioff(id, kctl, index_offset);
590 up_write(&card->controls_rwsem);
592 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, id);
595 EXPORT_SYMBOL_GPL(snd_ctl_activate_id);
598 * snd_ctl_rename_id - replace the id of a control on the card
599 * @card: the card instance
600 * @src_id: the old id
601 * @dst_id: the new id
603 * Finds the control with the old id from the card, and replaces the
604 * id with the new one.
606 * Return: Zero if successful, or a negative error code on failure.
608 int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
609 struct snd_ctl_elem_id *dst_id)
611 struct snd_kcontrol *kctl;
613 down_write(&card->controls_rwsem);
614 kctl = snd_ctl_find_id(card, src_id);
616 up_write(&card->controls_rwsem);
620 kctl->id.numid = card->last_numid + 1;
621 card->last_numid += kctl->count;
622 up_write(&card->controls_rwsem);
625 EXPORT_SYMBOL(snd_ctl_rename_id);
628 * snd_ctl_find_numid - find the control instance with the given number-id
629 * @card: the card instance
630 * @numid: the number-id to search
632 * Finds the control instance with the given number-id from the card.
634 * The caller must down card->controls_rwsem before calling this function
635 * (if the race condition can happen).
637 * Return: The pointer of the instance if found, or %NULL if not.
640 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
642 struct snd_kcontrol *kctl;
644 if (snd_BUG_ON(!card || !numid))
646 list_for_each_entry(kctl, &card->controls, list) {
647 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
652 EXPORT_SYMBOL(snd_ctl_find_numid);
655 * snd_ctl_find_id - find the control instance with the given id
656 * @card: the card instance
657 * @id: the id to search
659 * Finds the control instance with the given 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_id(struct snd_card *card,
668 struct snd_ctl_elem_id *id)
670 struct snd_kcontrol *kctl;
672 if (snd_BUG_ON(!card || !id))
675 return snd_ctl_find_numid(card, id->numid);
676 list_for_each_entry(kctl, &card->controls, list) {
677 if (kctl->id.iface != id->iface)
679 if (kctl->id.device != id->device)
681 if (kctl->id.subdevice != id->subdevice)
683 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
685 if (kctl->id.index > id->index)
687 if (kctl->id.index + kctl->count <= id->index)
693 EXPORT_SYMBOL(snd_ctl_find_id);
695 static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
696 unsigned int cmd, void __user *arg)
698 struct snd_ctl_card_info *info;
700 info = kzalloc(sizeof(*info), GFP_KERNEL);
703 down_read(&snd_ioctl_rwsem);
704 info->card = card->number;
705 strlcpy(info->id, card->id, sizeof(info->id));
706 strlcpy(info->driver, card->driver, sizeof(info->driver));
707 strlcpy(info->name, card->shortname, sizeof(info->name));
708 strlcpy(info->longname, card->longname, sizeof(info->longname));
709 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
710 strlcpy(info->components, card->components, sizeof(info->components));
711 up_read(&snd_ioctl_rwsem);
712 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
720 static int snd_ctl_elem_list(struct snd_card *card,
721 struct snd_ctl_elem_list *list)
723 struct snd_kcontrol *kctl;
724 struct snd_ctl_elem_id id;
725 unsigned int offset, space, jidx;
728 offset = list->offset;
731 down_read(&card->controls_rwsem);
732 list->count = card->controls_count;
735 list_for_each_entry(kctl, &card->controls, list) {
736 if (offset >= kctl->count) {
737 offset -= kctl->count;
740 for (jidx = offset; jidx < kctl->count; jidx++) {
741 snd_ctl_build_ioff(&id, kctl, jidx);
742 if (copy_to_user(list->pids + list->used, &id,
755 up_read(&card->controls_rwsem);
759 static int snd_ctl_elem_list_user(struct snd_card *card,
760 struct snd_ctl_elem_list __user *_list)
762 struct snd_ctl_elem_list list;
765 if (copy_from_user(&list, _list, sizeof(list)))
767 err = snd_ctl_elem_list(card, &list);
770 if (copy_to_user(_list, &list, sizeof(list)))
776 /* Check whether the given kctl info is valid */
777 static int snd_ctl_check_elem_info(struct snd_card *card,
778 const struct snd_ctl_elem_info *info)
780 static const unsigned int max_value_counts[] = {
781 [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = 128,
782 [SNDRV_CTL_ELEM_TYPE_INTEGER] = 128,
783 [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = 128,
784 [SNDRV_CTL_ELEM_TYPE_BYTES] = 512,
785 [SNDRV_CTL_ELEM_TYPE_IEC958] = 1,
786 [SNDRV_CTL_ELEM_TYPE_INTEGER64] = 64,
789 if (info->type < SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
790 info->type > SNDRV_CTL_ELEM_TYPE_INTEGER64) {
793 "control %i:%i:%i:%s:%i: invalid type %d\n",
794 info->id.iface, info->id.device,
795 info->id.subdevice, info->id.name,
796 info->id.index, info->type);
799 if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED &&
800 info->value.enumerated.items == 0) {
803 "control %i:%i:%i:%s:%i: zero enum items\n",
804 info->id.iface, info->id.device,
805 info->id.subdevice, info->id.name,
809 if (info->count > max_value_counts[info->type]) {
812 "control %i:%i:%i:%s:%i: invalid count %d\n",
813 info->id.iface, info->id.device,
814 info->id.subdevice, info->id.name,
815 info->id.index, info->count);
822 /* The capacity of struct snd_ctl_elem_value.value.*/
823 static const unsigned int value_sizes[] = {
824 [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = sizeof(long),
825 [SNDRV_CTL_ELEM_TYPE_INTEGER] = sizeof(long),
826 [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = sizeof(unsigned int),
827 [SNDRV_CTL_ELEM_TYPE_BYTES] = sizeof(unsigned char),
828 [SNDRV_CTL_ELEM_TYPE_IEC958] = sizeof(struct snd_aes_iec958),
829 [SNDRV_CTL_ELEM_TYPE_INTEGER64] = sizeof(long long),
832 #ifdef CONFIG_SND_CTL_VALIDATION
833 /* fill the remaining snd_ctl_elem_value data with the given pattern */
834 static void fill_remaining_elem_value(struct snd_ctl_elem_value *control,
835 struct snd_ctl_elem_info *info,
838 size_t offset = value_sizes[info->type] * info->count;
840 offset = (offset + sizeof(u32) - 1) / sizeof(u32);
841 memset32((u32 *)control->value.bytes.data + offset, pattern,
842 sizeof(control->value) / sizeof(u32) - offset);
845 /* check whether the given integer ctl value is valid */
846 static int sanity_check_int_value(struct snd_card *card,
847 const struct snd_ctl_elem_value *control,
848 const struct snd_ctl_elem_info *info,
851 long long lval, lmin, lmax, lstep;
854 switch (info->type) {
856 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
857 lval = control->value.integer.value[i];
862 case SNDRV_CTL_ELEM_TYPE_INTEGER:
863 lval = control->value.integer.value[i];
864 lmin = info->value.integer.min;
865 lmax = info->value.integer.max;
866 lstep = info->value.integer.step;
868 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
869 lval = control->value.integer64.value[i];
870 lmin = info->value.integer64.min;
871 lmax = info->value.integer64.max;
872 lstep = info->value.integer64.step;
874 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
875 lval = control->value.enumerated.item[i];
877 lmax = info->value.enumerated.items - 1;
882 if (lval < lmin || lval > lmax) {
884 "control %i:%i:%i:%s:%i: value out of range %lld (%lld/%lld) at count %i\n",
885 control->id.iface, control->id.device,
886 control->id.subdevice, control->id.name,
887 control->id.index, lval, lmin, lmax, i);
891 div64_u64_rem(lval, lstep, &rem);
894 "control %i:%i:%i:%s:%i: unaligned value %lld (step %lld) at count %i\n",
895 control->id.iface, control->id.device,
896 control->id.subdevice, control->id.name,
897 control->id.index, lval, lstep, i);
905 /* perform sanity checks to the given snd_ctl_elem_value object */
906 static int sanity_check_elem_value(struct snd_card *card,
907 const struct snd_ctl_elem_value *control,
908 const struct snd_ctl_elem_info *info,
915 switch (info->type) {
916 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
917 case SNDRV_CTL_ELEM_TYPE_INTEGER:
918 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
919 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
920 for (i = 0; i < info->count; i++) {
921 ret = sanity_check_int_value(card, control, info, i);
930 /* check whether the remaining area kept untouched */
931 offset = value_sizes[info->type] * info->count;
932 offset = (offset + sizeof(u32) - 1) / sizeof(u32);
933 p = (u32 *)control->value.bytes.data + offset;
934 for (; offset < sizeof(control->value) / sizeof(u32); offset++, p++) {
939 *p = 0; /* clear the checked area */
945 static inline void fill_remaining_elem_value(struct snd_ctl_elem_value *control,
946 struct snd_ctl_elem_info *info,
951 static inline int sanity_check_elem_value(struct snd_card *card,
952 struct snd_ctl_elem_value *control,
953 struct snd_ctl_elem_info *info,
960 static int __snd_ctl_elem_info(struct snd_card *card,
961 struct snd_kcontrol *kctl,
962 struct snd_ctl_elem_info *info,
963 struct snd_ctl_file *ctl)
965 struct snd_kcontrol_volatile *vd;
966 unsigned int index_offset;
969 #ifdef CONFIG_SND_DEBUG
972 result = kctl->info(kctl, info);
974 snd_BUG_ON(info->access);
975 index_offset = snd_ctl_get_ioff(kctl, &info->id);
976 vd = &kctl->vd[index_offset];
977 snd_ctl_build_ioff(&info->id, kctl, index_offset);
978 info->access = vd->access;
980 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
981 if (vd->owner == ctl)
982 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
983 info->owner = pid_vnr(vd->owner->pid);
987 if (!snd_ctl_skip_validation(info) &&
988 snd_ctl_check_elem_info(card, info) < 0)
994 static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
995 struct snd_ctl_elem_info *info)
997 struct snd_card *card = ctl->card;
998 struct snd_kcontrol *kctl;
1001 down_read(&card->controls_rwsem);
1002 kctl = snd_ctl_find_id(card, &info->id);
1006 result = __snd_ctl_elem_info(card, kctl, info, ctl);
1007 up_read(&card->controls_rwsem);
1011 static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
1012 struct snd_ctl_elem_info __user *_info)
1014 struct snd_ctl_elem_info info;
1017 if (copy_from_user(&info, _info, sizeof(info)))
1019 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
1022 result = snd_ctl_elem_info(ctl, &info);
1025 /* drop internal access flags */
1026 info.access &= ~SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK;
1027 if (copy_to_user(_info, &info, sizeof(info)))
1032 static int snd_ctl_elem_read(struct snd_card *card,
1033 struct snd_ctl_elem_value *control)
1035 struct snd_kcontrol *kctl;
1036 struct snd_kcontrol_volatile *vd;
1037 unsigned int index_offset;
1038 struct snd_ctl_elem_info info;
1039 const u32 pattern = 0xdeadbeef;
1042 kctl = snd_ctl_find_id(card, &control->id);
1046 index_offset = snd_ctl_get_ioff(kctl, &control->id);
1047 vd = &kctl->vd[index_offset];
1048 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_READ) || kctl->get == NULL)
1051 snd_ctl_build_ioff(&control->id, kctl, index_offset);
1053 #ifdef CONFIG_SND_CTL_VALIDATION
1054 /* info is needed only for validation */
1055 memset(&info, 0, sizeof(info));
1056 info.id = control->id;
1057 ret = __snd_ctl_elem_info(card, kctl, &info, NULL);
1062 if (!snd_ctl_skip_validation(&info))
1063 fill_remaining_elem_value(control, &info, pattern);
1064 ret = kctl->get(kctl, control);
1067 if (!snd_ctl_skip_validation(&info) &&
1068 sanity_check_elem_value(card, control, &info, pattern) < 0) {
1070 "control %i:%i:%i:%s:%i: access overflow\n",
1071 control->id.iface, control->id.device,
1072 control->id.subdevice, control->id.name,
1079 static int snd_ctl_elem_read_user(struct snd_card *card,
1080 struct snd_ctl_elem_value __user *_control)
1082 struct snd_ctl_elem_value *control;
1085 control = memdup_user(_control, sizeof(*control));
1086 if (IS_ERR(control))
1087 return PTR_ERR(control);
1089 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1093 down_read(&card->controls_rwsem);
1094 result = snd_ctl_elem_read(card, control);
1095 up_read(&card->controls_rwsem);
1099 if (copy_to_user(_control, control, sizeof(*control)))
1106 static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
1107 struct snd_ctl_elem_value *control)
1109 struct snd_kcontrol *kctl;
1110 struct snd_kcontrol_volatile *vd;
1111 unsigned int index_offset;
1114 kctl = snd_ctl_find_id(card, &control->id);
1118 index_offset = snd_ctl_get_ioff(kctl, &control->id);
1119 vd = &kctl->vd[index_offset];
1120 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) || kctl->put == NULL ||
1121 (file && vd->owner && vd->owner != file)) {
1125 snd_ctl_build_ioff(&control->id, kctl, index_offset);
1126 result = kctl->put(kctl, control);
1131 struct snd_ctl_elem_id id = control->id;
1132 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &id);
1138 static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
1139 struct snd_ctl_elem_value __user *_control)
1141 struct snd_ctl_elem_value *control;
1142 struct snd_card *card;
1145 control = memdup_user(_control, sizeof(*control));
1146 if (IS_ERR(control))
1147 return PTR_ERR(control);
1150 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1154 down_write(&card->controls_rwsem);
1155 result = snd_ctl_elem_write(card, file, control);
1156 up_write(&card->controls_rwsem);
1160 if (copy_to_user(_control, control, sizeof(*control)))
1167 static int snd_ctl_elem_lock(struct snd_ctl_file *file,
1168 struct snd_ctl_elem_id __user *_id)
1170 struct snd_card *card = file->card;
1171 struct snd_ctl_elem_id id;
1172 struct snd_kcontrol *kctl;
1173 struct snd_kcontrol_volatile *vd;
1176 if (copy_from_user(&id, _id, sizeof(id)))
1178 down_write(&card->controls_rwsem);
1179 kctl = snd_ctl_find_id(card, &id);
1183 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1184 if (vd->owner != NULL)
1191 up_write(&card->controls_rwsem);
1195 static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
1196 struct snd_ctl_elem_id __user *_id)
1198 struct snd_card *card = file->card;
1199 struct snd_ctl_elem_id id;
1200 struct snd_kcontrol *kctl;
1201 struct snd_kcontrol_volatile *vd;
1204 if (copy_from_user(&id, _id, sizeof(id)))
1206 down_write(&card->controls_rwsem);
1207 kctl = snd_ctl_find_id(card, &id);
1211 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1212 if (vd->owner == NULL)
1214 else if (vd->owner != file)
1221 up_write(&card->controls_rwsem);
1225 struct user_element {
1226 struct snd_ctl_elem_info info;
1227 struct snd_card *card;
1228 char *elem_data; /* element data */
1229 unsigned long elem_data_size; /* size of element data in bytes */
1230 void *tlv_data; /* TLV data */
1231 unsigned long tlv_data_size; /* TLV data size */
1232 void *priv_data; /* private data (like strings for enumerated type) */
1235 static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
1236 struct snd_ctl_elem_info *uinfo)
1238 struct user_element *ue = kcontrol->private_data;
1239 unsigned int offset;
1241 offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
1243 snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
1248 static int snd_ctl_elem_user_enum_info(struct snd_kcontrol *kcontrol,
1249 struct snd_ctl_elem_info *uinfo)
1251 struct user_element *ue = kcontrol->private_data;
1254 unsigned int offset;
1256 item = uinfo->value.enumerated.item;
1258 offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
1260 snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
1262 item = min(item, uinfo->value.enumerated.items - 1);
1263 uinfo->value.enumerated.item = item;
1265 names = ue->priv_data;
1266 for (; item > 0; --item)
1267 names += strlen(names) + 1;
1268 strcpy(uinfo->value.enumerated.name, names);
1273 static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
1274 struct snd_ctl_elem_value *ucontrol)
1276 struct user_element *ue = kcontrol->private_data;
1277 unsigned int size = ue->elem_data_size;
1278 char *src = ue->elem_data +
1279 snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
1281 memcpy(&ucontrol->value, src, size);
1285 static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
1286 struct snd_ctl_elem_value *ucontrol)
1289 struct user_element *ue = kcontrol->private_data;
1290 unsigned int size = ue->elem_data_size;
1291 char *dst = ue->elem_data +
1292 snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
1294 change = memcmp(&ucontrol->value, dst, size) != 0;
1296 memcpy(dst, &ucontrol->value, size);
1300 static int replace_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
1303 struct user_element *ue = kctl->private_data;
1304 unsigned int *container;
1305 struct snd_ctl_elem_id id;
1306 unsigned int mask = 0;
1310 if (size > 1024 * 128) /* sane value */
1313 container = vmemdup_user(buf, size);
1314 if (IS_ERR(container))
1315 return PTR_ERR(container);
1317 change = ue->tlv_data_size != size;
1319 change = memcmp(ue->tlv_data, container, size) != 0;
1325 if (ue->tlv_data == NULL) {
1326 /* Now TLV data is available. */
1327 for (i = 0; i < kctl->count; ++i)
1328 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1329 mask = SNDRV_CTL_EVENT_MASK_INFO;
1332 kvfree(ue->tlv_data);
1333 ue->tlv_data = container;
1334 ue->tlv_data_size = size;
1336 mask |= SNDRV_CTL_EVENT_MASK_TLV;
1337 for (i = 0; i < kctl->count; ++i) {
1338 snd_ctl_build_ioff(&id, kctl, i);
1339 snd_ctl_notify(ue->card, mask, &id);
1345 static int read_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
1348 struct user_element *ue = kctl->private_data;
1350 if (ue->tlv_data_size == 0 || ue->tlv_data == NULL)
1353 if (size < ue->tlv_data_size)
1356 if (copy_to_user(buf, ue->tlv_data, ue->tlv_data_size))
1362 static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kctl, int op_flag,
1363 unsigned int size, unsigned int __user *buf)
1365 if (op_flag == SNDRV_CTL_TLV_OP_WRITE)
1366 return replace_user_tlv(kctl, buf, size);
1368 return read_user_tlv(kctl, buf, size);
1371 static int snd_ctl_elem_init_enum_names(struct user_element *ue)
1374 size_t buf_len, name_len;
1376 const uintptr_t user_ptrval = ue->info.value.enumerated.names_ptr;
1378 if (ue->info.value.enumerated.names_length > 64 * 1024)
1381 names = vmemdup_user((const void __user *)user_ptrval,
1382 ue->info.value.enumerated.names_length);
1384 return PTR_ERR(names);
1386 /* check that there are enough valid names */
1387 buf_len = ue->info.value.enumerated.names_length;
1389 for (i = 0; i < ue->info.value.enumerated.items; ++i) {
1390 name_len = strnlen(p, buf_len);
1391 if (name_len == 0 || name_len >= 64 || name_len == buf_len) {
1396 buf_len -= name_len + 1;
1399 ue->priv_data = names;
1400 ue->info.value.enumerated.names_ptr = 0;
1405 static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
1407 struct user_element *ue = kcontrol->private_data;
1409 kvfree(ue->tlv_data);
1410 kvfree(ue->priv_data);
1414 static int snd_ctl_elem_add(struct snd_ctl_file *file,
1415 struct snd_ctl_elem_info *info, int replace)
1417 struct snd_card *card = file->card;
1418 struct snd_kcontrol *kctl;
1420 unsigned int access;
1422 struct user_element *ue;
1423 unsigned int offset;
1426 if (!*info->id.name)
1428 if (strnlen(info->id.name, sizeof(info->id.name)) >= sizeof(info->id.name))
1431 /* Delete a control to replace them if needed. */
1434 err = snd_ctl_remove_user_ctl(file, &info->id);
1440 * The number of userspace controls are counted control by control,
1441 * not element by element.
1443 if (card->user_ctl_count + 1 > MAX_USER_CONTROLS)
1446 /* Check the number of elements for this userspace control. */
1447 count = info->owner;
1451 /* Arrange access permissions if needed. */
1452 access = info->access;
1454 access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1455 access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1456 SNDRV_CTL_ELEM_ACCESS_INACTIVE |
1457 SNDRV_CTL_ELEM_ACCESS_TLV_WRITE);
1459 /* In initial state, nothing is available as TLV container. */
1460 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
1461 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1462 access |= SNDRV_CTL_ELEM_ACCESS_USER;
1465 * Check information and calculate the size of data specific to
1466 * this userspace control.
1468 /* pass NULL to card for suppressing error messages */
1469 err = snd_ctl_check_elem_info(NULL, info);
1472 /* user-space control doesn't allow zero-size data */
1473 if (info->count < 1)
1475 private_size = value_sizes[info->type] * info->count;
1478 * Keep memory object for this userspace control. After passing this
1479 * code block, the instance should be freed by snd_ctl_free_one().
1481 * Note that these elements in this control are locked.
1483 err = snd_ctl_new(&kctl, count, access, file);
1486 memcpy(&kctl->id, &info->id, sizeof(kctl->id));
1487 kctl->private_data = kzalloc(sizeof(struct user_element) + private_size * count,
1489 if (kctl->private_data == NULL) {
1493 kctl->private_free = snd_ctl_elem_user_free;
1495 /* Set private data for this userspace control. */
1496 ue = (struct user_element *)kctl->private_data;
1499 ue->info.access = 0;
1500 ue->elem_data = (char *)ue + sizeof(*ue);
1501 ue->elem_data_size = private_size;
1502 if (ue->info.type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
1503 err = snd_ctl_elem_init_enum_names(ue);
1505 snd_ctl_free_one(kctl);
1510 /* Set callback functions. */
1511 if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED)
1512 kctl->info = snd_ctl_elem_user_enum_info;
1514 kctl->info = snd_ctl_elem_user_info;
1515 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1516 kctl->get = snd_ctl_elem_user_get;
1517 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1518 kctl->put = snd_ctl_elem_user_put;
1519 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
1520 kctl->tlv.c = snd_ctl_elem_user_tlv;
1522 /* This function manage to free the instance on failure. */
1523 down_write(&card->controls_rwsem);
1524 err = __snd_ctl_add_replace(card, kctl, CTL_ADD_EXCLUSIVE);
1526 snd_ctl_free_one(kctl);
1529 offset = snd_ctl_get_ioff(kctl, &info->id);
1530 snd_ctl_build_ioff(&info->id, kctl, offset);
1532 * Here we cannot fill any field for the number of elements added by
1533 * this operation because there're no specific fields. The usage of
1534 * 'owner' field for this purpose may cause any bugs to userspace
1535 * applications because the field originally means PID of a process
1536 * which locks the element.
1539 card->user_ctl_count++;
1542 up_write(&card->controls_rwsem);
1546 static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1547 struct snd_ctl_elem_info __user *_info, int replace)
1549 struct snd_ctl_elem_info info;
1552 if (copy_from_user(&info, _info, sizeof(info)))
1554 err = snd_ctl_elem_add(file, &info, replace);
1557 if (copy_to_user(_info, &info, sizeof(info))) {
1558 snd_ctl_remove_user_ctl(file, &info.id);
1565 static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1566 struct snd_ctl_elem_id __user *_id)
1568 struct snd_ctl_elem_id id;
1570 if (copy_from_user(&id, _id, sizeof(id)))
1572 return snd_ctl_remove_user_ctl(file, &id);
1575 static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
1578 if (get_user(subscribe, ptr))
1580 if (subscribe < 0) {
1581 subscribe = file->subscribed;
1582 if (put_user(subscribe, ptr))
1587 file->subscribed = 1;
1589 } else if (file->subscribed) {
1590 snd_ctl_empty_read_queue(file);
1591 file->subscribed = 0;
1596 static int call_tlv_handler(struct snd_ctl_file *file, int op_flag,
1597 struct snd_kcontrol *kctl,
1598 struct snd_ctl_elem_id *id,
1599 unsigned int __user *buf, unsigned int size)
1601 static const struct {
1605 {SNDRV_CTL_TLV_OP_READ, SNDRV_CTL_ELEM_ACCESS_TLV_READ},
1606 {SNDRV_CTL_TLV_OP_WRITE, SNDRV_CTL_ELEM_ACCESS_TLV_WRITE},
1607 {SNDRV_CTL_TLV_OP_CMD, SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND},
1609 struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
1612 /* Check support of the request for this element. */
1613 for (i = 0; i < ARRAY_SIZE(pairs); ++i) {
1614 if (op_flag == pairs[i].op && (vd->access & pairs[i].perm))
1617 if (i == ARRAY_SIZE(pairs))
1620 if (kctl->tlv.c == NULL)
1623 /* Write and command operations are not allowed for locked element. */
1624 if (op_flag != SNDRV_CTL_TLV_OP_READ &&
1625 vd->owner != NULL && vd->owner != file)
1628 return kctl->tlv.c(kctl, op_flag, size, buf);
1631 static int read_tlv_buf(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id,
1632 unsigned int __user *buf, unsigned int size)
1634 struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
1637 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ))
1640 if (kctl->tlv.p == NULL)
1643 len = sizeof(unsigned int) * 2 + kctl->tlv.p[1];
1647 if (copy_to_user(buf, kctl->tlv.p, len))
1653 static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1654 struct snd_ctl_tlv __user *buf,
1657 struct snd_ctl_tlv header;
1658 unsigned int __user *container;
1659 unsigned int container_size;
1660 struct snd_kcontrol *kctl;
1661 struct snd_ctl_elem_id id;
1662 struct snd_kcontrol_volatile *vd;
1664 if (copy_from_user(&header, buf, sizeof(header)))
1667 /* In design of control core, numerical ID starts at 1. */
1668 if (header.numid == 0)
1671 /* At least, container should include type and length fields. */
1672 if (header.length < sizeof(unsigned int) * 2)
1674 container_size = header.length;
1675 container = buf->tlv;
1677 kctl = snd_ctl_find_numid(file->card, header.numid);
1681 /* Calculate index of the element in this set. */
1683 snd_ctl_build_ioff(&id, kctl, header.numid - id.numid);
1684 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1686 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1687 return call_tlv_handler(file, op_flag, kctl, &id, container,
1690 if (op_flag == SNDRV_CTL_TLV_OP_READ) {
1691 return read_tlv_buf(kctl, &id, container,
1696 /* Not supported. */
1700 static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1702 struct snd_ctl_file *ctl;
1703 struct snd_card *card;
1704 struct snd_kctl_ioctl *p;
1705 void __user *argp = (void __user *)arg;
1706 int __user *ip = argp;
1709 ctl = file->private_data;
1711 if (snd_BUG_ON(!card))
1714 case SNDRV_CTL_IOCTL_PVERSION:
1715 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1716 case SNDRV_CTL_IOCTL_CARD_INFO:
1717 return snd_ctl_card_info(card, ctl, cmd, argp);
1718 case SNDRV_CTL_IOCTL_ELEM_LIST:
1719 return snd_ctl_elem_list_user(card, argp);
1720 case SNDRV_CTL_IOCTL_ELEM_INFO:
1721 return snd_ctl_elem_info_user(ctl, argp);
1722 case SNDRV_CTL_IOCTL_ELEM_READ:
1723 return snd_ctl_elem_read_user(card, argp);
1724 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1725 return snd_ctl_elem_write_user(ctl, argp);
1726 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1727 return snd_ctl_elem_lock(ctl, argp);
1728 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1729 return snd_ctl_elem_unlock(ctl, argp);
1730 case SNDRV_CTL_IOCTL_ELEM_ADD:
1731 return snd_ctl_elem_add_user(ctl, argp, 0);
1732 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1733 return snd_ctl_elem_add_user(ctl, argp, 1);
1734 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1735 return snd_ctl_elem_remove(ctl, argp);
1736 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1737 return snd_ctl_subscribe_events(ctl, ip);
1738 case SNDRV_CTL_IOCTL_TLV_READ:
1739 down_read(&ctl->card->controls_rwsem);
1740 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_READ);
1741 up_read(&ctl->card->controls_rwsem);
1743 case SNDRV_CTL_IOCTL_TLV_WRITE:
1744 down_write(&ctl->card->controls_rwsem);
1745 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_WRITE);
1746 up_write(&ctl->card->controls_rwsem);
1748 case SNDRV_CTL_IOCTL_TLV_COMMAND:
1749 down_write(&ctl->card->controls_rwsem);
1750 err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_CMD);
1751 up_write(&ctl->card->controls_rwsem);
1753 case SNDRV_CTL_IOCTL_POWER:
1754 return -ENOPROTOOPT;
1755 case SNDRV_CTL_IOCTL_POWER_STATE:
1757 return put_user(card->power_state, ip) ? -EFAULT : 0;
1759 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1762 down_read(&snd_ioctl_rwsem);
1763 list_for_each_entry(p, &snd_control_ioctls, list) {
1764 err = p->fioctl(card, ctl, cmd, arg);
1765 if (err != -ENOIOCTLCMD) {
1766 up_read(&snd_ioctl_rwsem);
1770 up_read(&snd_ioctl_rwsem);
1771 dev_dbg(card->dev, "unknown ioctl = 0x%x\n", cmd);
1775 static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1776 size_t count, loff_t * offset)
1778 struct snd_ctl_file *ctl;
1782 ctl = file->private_data;
1783 if (snd_BUG_ON(!ctl || !ctl->card))
1785 if (!ctl->subscribed)
1787 if (count < sizeof(struct snd_ctl_event))
1789 spin_lock_irq(&ctl->read_lock);
1790 while (count >= sizeof(struct snd_ctl_event)) {
1791 struct snd_ctl_event ev;
1792 struct snd_kctl_event *kev;
1793 while (list_empty(&ctl->events)) {
1794 wait_queue_entry_t wait;
1795 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1799 init_waitqueue_entry(&wait, current);
1800 add_wait_queue(&ctl->change_sleep, &wait);
1801 set_current_state(TASK_INTERRUPTIBLE);
1802 spin_unlock_irq(&ctl->read_lock);
1804 remove_wait_queue(&ctl->change_sleep, &wait);
1805 if (ctl->card->shutdown)
1807 if (signal_pending(current))
1808 return -ERESTARTSYS;
1809 spin_lock_irq(&ctl->read_lock);
1811 kev = snd_kctl_event(ctl->events.next);
1812 ev.type = SNDRV_CTL_EVENT_ELEM;
1813 ev.data.elem.mask = kev->mask;
1814 ev.data.elem.id = kev->id;
1815 list_del(&kev->list);
1816 spin_unlock_irq(&ctl->read_lock);
1818 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
1822 spin_lock_irq(&ctl->read_lock);
1823 buffer += sizeof(struct snd_ctl_event);
1824 count -= sizeof(struct snd_ctl_event);
1825 result += sizeof(struct snd_ctl_event);
1828 spin_unlock_irq(&ctl->read_lock);
1830 return result > 0 ? result : err;
1833 static __poll_t snd_ctl_poll(struct file *file, poll_table * wait)
1836 struct snd_ctl_file *ctl;
1838 ctl = file->private_data;
1839 if (!ctl->subscribed)
1841 poll_wait(file, &ctl->change_sleep, wait);
1844 if (!list_empty(&ctl->events))
1845 mask |= EPOLLIN | EPOLLRDNORM;
1851 * register the device-specific control-ioctls.
1852 * called from each device manager like pcm.c, hwdep.c, etc.
1854 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1856 struct snd_kctl_ioctl *pn;
1858 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
1862 down_write(&snd_ioctl_rwsem);
1863 list_add_tail(&pn->list, lists);
1864 up_write(&snd_ioctl_rwsem);
1869 * snd_ctl_register_ioctl - register the device-specific control-ioctls
1870 * @fcn: ioctl callback function
1872 * called from each device manager like pcm.c, hwdep.c, etc.
1874 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1876 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1878 EXPORT_SYMBOL(snd_ctl_register_ioctl);
1880 #ifdef CONFIG_COMPAT
1882 * snd_ctl_register_ioctl_compat - register the device-specific 32bit compat
1884 * @fcn: ioctl callback function
1886 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1888 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1890 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
1894 * de-register the device-specific control-ioctls.
1896 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1897 struct list_head *lists)
1899 struct snd_kctl_ioctl *p;
1901 if (snd_BUG_ON(!fcn))
1903 down_write(&snd_ioctl_rwsem);
1904 list_for_each_entry(p, lists, list) {
1905 if (p->fioctl == fcn) {
1907 up_write(&snd_ioctl_rwsem);
1912 up_write(&snd_ioctl_rwsem);
1918 * snd_ctl_unregister_ioctl - de-register the device-specific control-ioctls
1919 * @fcn: ioctl callback function to unregister
1921 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1923 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1925 EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1927 #ifdef CONFIG_COMPAT
1929 * snd_ctl_unregister_ioctl_compat - de-register the device-specific compat
1930 * 32bit control-ioctls
1931 * @fcn: ioctl callback function to unregister
1933 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1935 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1937 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
1940 static int snd_ctl_fasync(int fd, struct file * file, int on)
1942 struct snd_ctl_file *ctl;
1944 ctl = file->private_data;
1945 return snd_fasync_helper(fd, file, on, &ctl->fasync);
1948 /* return the preferred subdevice number if already assigned;
1949 * otherwise return -1
1951 int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type)
1953 struct snd_ctl_file *kctl;
1955 unsigned long flags;
1957 read_lock_irqsave(&card->ctl_files_rwlock, flags);
1958 list_for_each_entry(kctl, &card->ctl_files, list) {
1959 if (kctl->pid == task_pid(current)) {
1960 subdevice = kctl->preferred_subdevice[type];
1961 if (subdevice != -1)
1965 read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
1968 EXPORT_SYMBOL_GPL(snd_ctl_get_preferred_subdevice);
1973 #ifdef CONFIG_COMPAT
1974 #include "control_compat.c"
1976 #define snd_ctl_ioctl_compat NULL
1983 static const struct file_operations snd_ctl_f_ops =
1985 .owner = THIS_MODULE,
1986 .read = snd_ctl_read,
1987 .open = snd_ctl_open,
1988 .release = snd_ctl_release,
1989 .llseek = no_llseek,
1990 .poll = snd_ctl_poll,
1991 .unlocked_ioctl = snd_ctl_ioctl,
1992 .compat_ioctl = snd_ctl_ioctl_compat,
1993 .fasync = snd_ctl_fasync,
1997 * registration of the control device
1999 static int snd_ctl_dev_register(struct snd_device *device)
2001 struct snd_card *card = device->device_data;
2003 return snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
2004 &snd_ctl_f_ops, card, &card->ctl_dev);
2008 * disconnection of the control device
2010 static int snd_ctl_dev_disconnect(struct snd_device *device)
2012 struct snd_card *card = device->device_data;
2013 struct snd_ctl_file *ctl;
2014 unsigned long flags;
2016 read_lock_irqsave(&card->ctl_files_rwlock, flags);
2017 list_for_each_entry(ctl, &card->ctl_files, list) {
2018 wake_up(&ctl->change_sleep);
2019 snd_kill_fasync(ctl->fasync, SIGIO, POLL_ERR);
2021 read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
2023 return snd_unregister_device(&card->ctl_dev);
2029 static int snd_ctl_dev_free(struct snd_device *device)
2031 struct snd_card *card = device->device_data;
2032 struct snd_kcontrol *control;
2034 down_write(&card->controls_rwsem);
2035 while (!list_empty(&card->controls)) {
2036 control = snd_kcontrol(card->controls.next);
2037 snd_ctl_remove(card, control);
2039 up_write(&card->controls_rwsem);
2040 put_device(&card->ctl_dev);
2045 * create control core:
2046 * called from init.c
2048 int snd_ctl_create(struct snd_card *card)
2050 static const struct snd_device_ops ops = {
2051 .dev_free = snd_ctl_dev_free,
2052 .dev_register = snd_ctl_dev_register,
2053 .dev_disconnect = snd_ctl_dev_disconnect,
2057 if (snd_BUG_ON(!card))
2059 if (snd_BUG_ON(card->number < 0 || card->number >= SNDRV_CARDS))
2062 snd_device_initialize(&card->ctl_dev, card);
2063 dev_set_name(&card->ctl_dev, "controlC%d", card->number);
2065 err = snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
2067 put_device(&card->ctl_dev);
2072 * Frequently used control callbacks/helpers
2076 * snd_ctl_boolean_mono_info - Helper function for a standard boolean info
2077 * callback with a mono channel
2078 * @kcontrol: the kcontrol instance
2079 * @uinfo: info to store
2081 * This is a function that can be used as info callback for a standard
2082 * boolean control with a single mono channel.
2084 int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
2085 struct snd_ctl_elem_info *uinfo)
2087 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2089 uinfo->value.integer.min = 0;
2090 uinfo->value.integer.max = 1;
2093 EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
2096 * snd_ctl_boolean_stereo_info - Helper function for a standard boolean info
2097 * callback with stereo two channels
2098 * @kcontrol: the kcontrol instance
2099 * @uinfo: info to store
2101 * This is a function that can be used as info callback for a standard
2102 * boolean control with stereo two channels.
2104 int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
2105 struct snd_ctl_elem_info *uinfo)
2107 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2109 uinfo->value.integer.min = 0;
2110 uinfo->value.integer.max = 1;
2113 EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
2116 * snd_ctl_enum_info - fills the info structure for an enumerated control
2117 * @info: the structure to be filled
2118 * @channels: the number of the control's channels; often one
2119 * @items: the number of control values; also the size of @names
2120 * @names: an array containing the names of all control values
2122 * Sets all required fields in @info to their appropriate values.
2123 * If the control's accessibility is not the default (readable and writable),
2124 * the caller has to fill @info->access.
2128 int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
2129 unsigned int items, const char *const names[])
2131 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2132 info->count = channels;
2133 info->value.enumerated.items = items;
2136 if (info->value.enumerated.item >= items)
2137 info->value.enumerated.item = items - 1;
2138 WARN(strlen(names[info->value.enumerated.item]) >= sizeof(info->value.enumerated.name),
2139 "ALSA: too long item name '%s'\n",
2140 names[info->value.enumerated.item]);
2141 strlcpy(info->value.enumerated.name,
2142 names[info->value.enumerated.item],
2143 sizeof(info->value.enumerated.name));
2146 EXPORT_SYMBOL(snd_ctl_enum_info);