11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * Routines for driver control interface
4c1017a4cSJaroslav Kysela * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
51da177e4SLinus Torvalds */
61da177e4SLinus Torvalds
71da177e4SLinus Torvalds #include <linux/threads.h>
81da177e4SLinus Torvalds #include <linux/interrupt.h>
9da155d5bSPaul Gortmaker #include <linux/module.h>
1066c6d1efSTakashi Sakamoto #include <linux/moduleparam.h>
111da177e4SLinus Torvalds #include <linux/slab.h>
121da177e4SLinus Torvalds #include <linux/vmalloc.h>
131da177e4SLinus Torvalds #include <linux/time.h>
1488a89037SAl Viro #include <linux/mm.h>
15fbd3eb7fSTakashi Iwai #include <linux/math64.h>
16174cd4b1SIngo Molnar #include <linux/sched/signal.h>
171da177e4SLinus Torvalds #include <sound/core.h>
181da177e4SLinus Torvalds #include <sound/minors.h>
191da177e4SLinus Torvalds #include <sound/info.h>
201da177e4SLinus Torvalds #include <sound/control.h>
211da177e4SLinus Torvalds
2266c6d1efSTakashi Sakamoto // Max allocation size for user controls.
2366c6d1efSTakashi Sakamoto static int max_user_ctl_alloc_size = 8 * 1024 * 1024;
2466c6d1efSTakashi Sakamoto module_param_named(max_user_ctl_alloc_size, max_user_ctl_alloc_size, int, 0444);
2566c6d1efSTakashi Sakamoto MODULE_PARM_DESC(max_user_ctl_alloc_size, "Max allocation size for user controls");
2666c6d1efSTakashi Sakamoto
275591bf07SDan Rosenberg #define MAX_CONTROL_COUNT 1028
281da177e4SLinus Torvalds
2982e9bae6STakashi Iwai struct snd_kctl_ioctl {
301da177e4SLinus Torvalds struct list_head list; /* list of all ioctls */
311da177e4SLinus Torvalds snd_kctl_ioctl_func_t fioctl;
3282e9bae6STakashi Iwai };
331da177e4SLinus Torvalds
341da177e4SLinus Torvalds static DECLARE_RWSEM(snd_ioctl_rwsem);
353f0638a0SJaroslav Kysela static DECLARE_RWSEM(snd_ctl_layer_rwsem);
361da177e4SLinus Torvalds static LIST_HEAD(snd_control_ioctls);
371da177e4SLinus Torvalds #ifdef CONFIG_COMPAT
381da177e4SLinus Torvalds static LIST_HEAD(snd_control_compat_ioctls);
391da177e4SLinus Torvalds #endif
403f0638a0SJaroslav Kysela static struct snd_ctl_layer_ops *snd_ctl_layer;
411da177e4SLinus Torvalds
42192c4cccSTakashi Iwai static int snd_ctl_remove_locked(struct snd_card *card,
43192c4cccSTakashi Iwai struct snd_kcontrol *kcontrol);
44192c4cccSTakashi Iwai
snd_ctl_open(struct inode * inode,struct file * file)451da177e4SLinus Torvalds static int snd_ctl_open(struct inode *inode, struct file *file)
461da177e4SLinus Torvalds {
471da177e4SLinus Torvalds unsigned long flags;
4882e9bae6STakashi Iwai struct snd_card *card;
4982e9bae6STakashi Iwai struct snd_ctl_file *ctl;
5023c18d4bSTakashi Iwai int i, err;
511da177e4SLinus Torvalds
52c5bf68feSKirill Smelkov err = stream_open(inode, file);
5302f4865fSTakashi Iwai if (err < 0)
5402f4865fSTakashi Iwai return err;
5502f4865fSTakashi Iwai
56f87135f5SClemens Ladisch card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
571da177e4SLinus Torvalds if (!card) {
581da177e4SLinus Torvalds err = -ENODEV;
591da177e4SLinus Torvalds goto __error1;
601da177e4SLinus Torvalds }
611da177e4SLinus Torvalds err = snd_card_file_add(card, file);
621da177e4SLinus Torvalds if (err < 0) {
631da177e4SLinus Torvalds err = -ENODEV;
641da177e4SLinus Torvalds goto __error1;
651da177e4SLinus Torvalds }
661da177e4SLinus Torvalds if (!try_module_get(card->module)) {
671da177e4SLinus Torvalds err = -EFAULT;
681da177e4SLinus Torvalds goto __error2;
691da177e4SLinus Torvalds }
70ca2c0966STakashi Iwai ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
711da177e4SLinus Torvalds if (ctl == NULL) {
721da177e4SLinus Torvalds err = -ENOMEM;
731da177e4SLinus Torvalds goto __error;
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds INIT_LIST_HEAD(&ctl->events);
761da177e4SLinus Torvalds init_waitqueue_head(&ctl->change_sleep);
771da177e4SLinus Torvalds spin_lock_init(&ctl->read_lock);
781da177e4SLinus Torvalds ctl->card = card;
7923c18d4bSTakashi Iwai for (i = 0; i < SND_CTL_SUBDEV_ITEMS; i++)
8023c18d4bSTakashi Iwai ctl->preferred_subdevice[i] = -1;
8125d27edeSClemens Ladisch ctl->pid = get_pid(task_pid(current));
821da177e4SLinus Torvalds file->private_data = ctl;
831da177e4SLinus Torvalds write_lock_irqsave(&card->ctl_files_rwlock, flags);
841da177e4SLinus Torvalds list_add_tail(&ctl->list, &card->ctl_files);
851da177e4SLinus Torvalds write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
86a0830dbdSTakashi Iwai snd_card_unref(card);
871da177e4SLinus Torvalds return 0;
881da177e4SLinus Torvalds
891da177e4SLinus Torvalds __error:
901da177e4SLinus Torvalds module_put(card->module);
911da177e4SLinus Torvalds __error2:
921da177e4SLinus Torvalds snd_card_file_remove(card, file);
931da177e4SLinus Torvalds __error1:
94a0830dbdSTakashi Iwai if (card)
95a0830dbdSTakashi Iwai snd_card_unref(card);
961da177e4SLinus Torvalds return err;
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds
snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)9982e9bae6STakashi Iwai static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
1001da177e4SLinus Torvalds {
1017507e8daSBorislav Petkov unsigned long flags;
10282e9bae6STakashi Iwai struct snd_kctl_event *cread;
1031da177e4SLinus Torvalds
1047507e8daSBorislav Petkov spin_lock_irqsave(&ctl->read_lock, flags);
1051da177e4SLinus Torvalds while (!list_empty(&ctl->events)) {
1061da177e4SLinus Torvalds cread = snd_kctl_event(ctl->events.next);
1071da177e4SLinus Torvalds list_del(&cread->list);
1081da177e4SLinus Torvalds kfree(cread);
1091da177e4SLinus Torvalds }
1107507e8daSBorislav Petkov spin_unlock_irqrestore(&ctl->read_lock, flags);
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds
snd_ctl_release(struct inode * inode,struct file * file)1131da177e4SLinus Torvalds static int snd_ctl_release(struct inode *inode, struct file *file)
1141da177e4SLinus Torvalds {
1151da177e4SLinus Torvalds unsigned long flags;
11682e9bae6STakashi Iwai struct snd_card *card;
11782e9bae6STakashi Iwai struct snd_ctl_file *ctl;
11882e9bae6STakashi Iwai struct snd_kcontrol *control;
1191da177e4SLinus Torvalds unsigned int idx;
1201da177e4SLinus Torvalds
1211da177e4SLinus Torvalds ctl = file->private_data;
1221da177e4SLinus Torvalds file->private_data = NULL;
1231da177e4SLinus Torvalds card = ctl->card;
1241da177e4SLinus Torvalds write_lock_irqsave(&card->ctl_files_rwlock, flags);
1251da177e4SLinus Torvalds list_del(&ctl->list);
1261da177e4SLinus Torvalds write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
1271da177e4SLinus Torvalds down_write(&card->controls_rwsem);
1289244b2c3SJohannes Berg list_for_each_entry(control, &card->controls, list)
1291da177e4SLinus Torvalds for (idx = 0; idx < control->count; idx++)
1301da177e4SLinus Torvalds if (control->vd[idx].owner == ctl)
1311da177e4SLinus Torvalds control->vd[idx].owner = NULL;
1321da177e4SLinus Torvalds up_write(&card->controls_rwsem);
1334a971e84STakashi Iwai snd_fasync_free(ctl->fasync);
1341da177e4SLinus Torvalds snd_ctl_empty_read_queue(ctl);
13525d27edeSClemens Ladisch put_pid(ctl->pid);
1361da177e4SLinus Torvalds kfree(ctl);
1371da177e4SLinus Torvalds module_put(card->module);
1381da177e4SLinus Torvalds snd_card_file_remove(card, file);
1391da177e4SLinus Torvalds return 0;
1401da177e4SLinus Torvalds }
1411da177e4SLinus Torvalds
14212cddbd8STakashi Iwai /**
14312cddbd8STakashi Iwai * snd_ctl_notify - Send notification to user-space for a control change
14412cddbd8STakashi Iwai * @card: the card to send notification
14512cddbd8STakashi Iwai * @mask: the event mask, SNDRV_CTL_EVENT_*
14612cddbd8STakashi Iwai * @id: the ctl element id to send notification
14712cddbd8STakashi Iwai *
14812cddbd8STakashi Iwai * This function adds an event record with the given id and mask, appends
14912cddbd8STakashi Iwai * to the list and wakes up the user-space for notification. This can be
15012cddbd8STakashi Iwai * called in the atomic context.
15112cddbd8STakashi Iwai */
snd_ctl_notify(struct snd_card * card,unsigned int mask,struct snd_ctl_elem_id * id)15282e9bae6STakashi Iwai void snd_ctl_notify(struct snd_card *card, unsigned int mask,
15382e9bae6STakashi Iwai struct snd_ctl_elem_id *id)
1541da177e4SLinus Torvalds {
1551da177e4SLinus Torvalds unsigned long flags;
15682e9bae6STakashi Iwai struct snd_ctl_file *ctl;
15782e9bae6STakashi Iwai struct snd_kctl_event *ev;
1581da177e4SLinus Torvalds
1597eaa943cSTakashi Iwai if (snd_BUG_ON(!card || !id))
1607eaa943cSTakashi Iwai return;
161f388cdcdSTakashi Iwai if (card->shutdown)
162f388cdcdSTakashi Iwai return;
1636564d0adSTakashi Iwai read_lock_irqsave(&card->ctl_files_rwlock, flags);
1648eeaa2f9STakashi Iwai #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
1651da177e4SLinus Torvalds card->mixer_oss_change_count++;
1661da177e4SLinus Torvalds #endif
1679244b2c3SJohannes Berg list_for_each_entry(ctl, &card->ctl_files, list) {
1681da177e4SLinus Torvalds if (!ctl->subscribed)
1691da177e4SLinus Torvalds continue;
1706564d0adSTakashi Iwai spin_lock(&ctl->read_lock);
1719244b2c3SJohannes Berg list_for_each_entry(ev, &ctl->events, list) {
1721da177e4SLinus Torvalds if (ev->id.numid == id->numid) {
1731da177e4SLinus Torvalds ev->mask |= mask;
1741da177e4SLinus Torvalds goto _found;
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds }
177ca2c0966STakashi Iwai ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
1781da177e4SLinus Torvalds if (ev) {
1791da177e4SLinus Torvalds ev->id = *id;
1801da177e4SLinus Torvalds ev->mask = mask;
1811da177e4SLinus Torvalds list_add_tail(&ev->list, &ctl->events);
1821da177e4SLinus Torvalds } else {
183bb009457STakashi Iwai dev_err(card->dev, "No memory available to allocate event\n");
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds _found:
1861da177e4SLinus Torvalds wake_up(&ctl->change_sleep);
1876564d0adSTakashi Iwai spin_unlock(&ctl->read_lock);
1884a971e84STakashi Iwai snd_kill_fasync(ctl->fasync, SIGIO, POLL_IN);
1891da177e4SLinus Torvalds }
1906564d0adSTakashi Iwai read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
1911da177e4SLinus Torvalds }
192c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_ctl_notify);
193c0d3fb39STakashi Iwai
1941da177e4SLinus Torvalds /**
1951fa4445fSJaroslav Kysela * snd_ctl_notify_one - Send notification to user-space for a control change
1961fa4445fSJaroslav Kysela * @card: the card to send notification
1971fa4445fSJaroslav Kysela * @mask: the event mask, SNDRV_CTL_EVENT_*
1981fa4445fSJaroslav Kysela * @kctl: the pointer with the control instance
1991fa4445fSJaroslav Kysela * @ioff: the additional offset to the control index
2001fa4445fSJaroslav Kysela *
2011fa4445fSJaroslav Kysela * This function calls snd_ctl_notify() and does additional jobs
2021fa4445fSJaroslav Kysela * like LED state changes.
2031fa4445fSJaroslav Kysela */
snd_ctl_notify_one(struct snd_card * card,unsigned int mask,struct snd_kcontrol * kctl,unsigned int ioff)2041fa4445fSJaroslav Kysela void snd_ctl_notify_one(struct snd_card *card, unsigned int mask,
2051fa4445fSJaroslav Kysela struct snd_kcontrol *kctl, unsigned int ioff)
2061fa4445fSJaroslav Kysela {
2071fa4445fSJaroslav Kysela struct snd_ctl_elem_id id = kctl->id;
2083f0638a0SJaroslav Kysela struct snd_ctl_layer_ops *lops;
2091fa4445fSJaroslav Kysela
2101fa4445fSJaroslav Kysela id.index += ioff;
2111fa4445fSJaroslav Kysela id.numid += ioff;
2121fa4445fSJaroslav Kysela snd_ctl_notify(card, mask, &id);
2133f0638a0SJaroslav Kysela down_read(&snd_ctl_layer_rwsem);
2143f0638a0SJaroslav Kysela for (lops = snd_ctl_layer; lops; lops = lops->next)
2153f0638a0SJaroslav Kysela lops->lnotify(card, mask, kctl, ioff);
2163f0638a0SJaroslav Kysela up_read(&snd_ctl_layer_rwsem);
2171fa4445fSJaroslav Kysela }
2181fa4445fSJaroslav Kysela EXPORT_SYMBOL(snd_ctl_notify_one);
2191fa4445fSJaroslav Kysela
2201fa4445fSJaroslav Kysela /**
2212225e79bSTakashi Sakamoto * snd_ctl_new - create a new control instance with some elements
2222225e79bSTakashi Sakamoto * @kctl: the pointer to store new control instance
2232225e79bSTakashi Sakamoto * @count: the number of elements in this control
2242225e79bSTakashi Sakamoto * @access: the default access flags for elements in this control
2252225e79bSTakashi Sakamoto * @file: given when locking these elements
2261da177e4SLinus Torvalds *
2272225e79bSTakashi Sakamoto * Allocates a memory object for a new control instance. The instance has
2282225e79bSTakashi Sakamoto * elements as many as the given number (@count). Each element has given
2292225e79bSTakashi Sakamoto * access permissions (@access). Each element is locked when @file is given.
2301da177e4SLinus Torvalds *
2312225e79bSTakashi Sakamoto * Return: 0 on success, error code on failure
2321da177e4SLinus Torvalds */
snd_ctl_new(struct snd_kcontrol ** kctl,unsigned int count,unsigned int access,struct snd_ctl_file * file)2332225e79bSTakashi Sakamoto static int snd_ctl_new(struct snd_kcontrol **kctl, unsigned int count,
2342225e79bSTakashi Sakamoto unsigned int access, struct snd_ctl_file *file)
2351da177e4SLinus Torvalds {
2361da177e4SLinus Torvalds unsigned int idx;
2371da177e4SLinus Torvalds
2382225e79bSTakashi Sakamoto if (count == 0 || count > MAX_CONTROL_COUNT)
2392225e79bSTakashi Sakamoto return -EINVAL;
2405591bf07SDan Rosenberg
24165be9580STakashi Iwai *kctl = kzalloc(struct_size(*kctl, vd, count), GFP_KERNEL);
242ec0e9937STakashi Iwai if (!*kctl)
2432225e79bSTakashi Sakamoto return -ENOMEM;
2442225e79bSTakashi Sakamoto
2452225e79bSTakashi Sakamoto for (idx = 0; idx < count; idx++) {
2462225e79bSTakashi Sakamoto (*kctl)->vd[idx].access = access;
2472225e79bSTakashi Sakamoto (*kctl)->vd[idx].owner = file;
2482225e79bSTakashi Sakamoto }
2492225e79bSTakashi Sakamoto (*kctl)->count = count;
2502225e79bSTakashi Sakamoto
2512225e79bSTakashi Sakamoto return 0;
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds
2541da177e4SLinus Torvalds /**
2551da177e4SLinus Torvalds * snd_ctl_new1 - create a control instance from the template
2561da177e4SLinus Torvalds * @ncontrol: the initialization record
2571da177e4SLinus Torvalds * @private_data: the private data to set
2581da177e4SLinus Torvalds *
25982e9bae6STakashi Iwai * Allocates a new struct snd_kcontrol instance and initialize from the given
2601da177e4SLinus Torvalds * template. When the access field of ncontrol is 0, it's assumed as
2611da177e4SLinus Torvalds * READWRITE access. When the count field is 0, it's assumes as one.
2621da177e4SLinus Torvalds *
263eb7c06e8SYacine Belkadi * Return: The pointer of the newly generated instance, or %NULL on failure.
2641da177e4SLinus Torvalds */
snd_ctl_new1(const struct snd_kcontrol_new * ncontrol,void * private_data)26582e9bae6STakashi Iwai struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
26682e9bae6STakashi Iwai void *private_data)
2671da177e4SLinus Torvalds {
2682225e79bSTakashi Sakamoto struct snd_kcontrol *kctl;
2692225e79bSTakashi Sakamoto unsigned int count;
2701da177e4SLinus Torvalds unsigned int access;
2712225e79bSTakashi Sakamoto int err;
2721da177e4SLinus Torvalds
2737eaa943cSTakashi Iwai if (snd_BUG_ON(!ncontrol || !ncontrol->info))
2747eaa943cSTakashi Iwai return NULL;
2752225e79bSTakashi Sakamoto
2762225e79bSTakashi Sakamoto count = ncontrol->count;
2772225e79bSTakashi Sakamoto if (count == 0)
2782225e79bSTakashi Sakamoto count = 1;
2792225e79bSTakashi Sakamoto
2802225e79bSTakashi Sakamoto access = ncontrol->access;
2812225e79bSTakashi Sakamoto if (access == 0)
2822225e79bSTakashi Sakamoto access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2832225e79bSTakashi Sakamoto access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
284a8d372f1STakashi Iwai SNDRV_CTL_ELEM_ACCESS_VOLATILE |
2858aa9b586SJaroslav Kysela SNDRV_CTL_ELEM_ACCESS_INACTIVE |
2868aa9b586SJaroslav Kysela SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE |
287a75d7a4cSClemens Ladisch SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND |
288fbd3eb7fSTakashi Iwai SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK |
28922d8de62SJaroslav Kysela SNDRV_CTL_ELEM_ACCESS_LED_MASK |
290fbd3eb7fSTakashi Iwai SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK);
2912225e79bSTakashi Sakamoto
2922225e79bSTakashi Sakamoto err = snd_ctl_new(&kctl, count, access, NULL);
2932225e79bSTakashi Sakamoto if (err < 0)
2942225e79bSTakashi Sakamoto return NULL;
2952225e79bSTakashi Sakamoto
2962225e79bSTakashi Sakamoto /* The 'numid' member is decided when calling snd_ctl_add(). */
2972225e79bSTakashi Sakamoto kctl->id.iface = ncontrol->iface;
2982225e79bSTakashi Sakamoto kctl->id.device = ncontrol->device;
2992225e79bSTakashi Sakamoto kctl->id.subdevice = ncontrol->subdevice;
3002225e79bSTakashi Sakamoto if (ncontrol->name) {
30175b1a8f9SJoe Perches strscpy(kctl->id.name, ncontrol->name, sizeof(kctl->id.name));
3022225e79bSTakashi Sakamoto if (strcmp(ncontrol->name, kctl->id.name) != 0)
3032225e79bSTakashi Sakamoto pr_warn("ALSA: Control name '%s' truncated to '%s'\n",
3042225e79bSTakashi Sakamoto ncontrol->name, kctl->id.name);
3052225e79bSTakashi Sakamoto }
3062225e79bSTakashi Sakamoto kctl->id.index = ncontrol->index;
3072225e79bSTakashi Sakamoto
3082225e79bSTakashi Sakamoto kctl->info = ncontrol->info;
3092225e79bSTakashi Sakamoto kctl->get = ncontrol->get;
3102225e79bSTakashi Sakamoto kctl->put = ncontrol->put;
3112225e79bSTakashi Sakamoto kctl->tlv.p = ncontrol->tlv.p;
3122225e79bSTakashi Sakamoto
3132225e79bSTakashi Sakamoto kctl->private_value = ncontrol->private_value;
3142225e79bSTakashi Sakamoto kctl->private_data = private_data;
3152225e79bSTakashi Sakamoto
3162225e79bSTakashi Sakamoto return kctl;
3171da177e4SLinus Torvalds }
318c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_ctl_new1);
319c0d3fb39STakashi Iwai
3201da177e4SLinus Torvalds /**
3211da177e4SLinus Torvalds * snd_ctl_free_one - release the control instance
3221da177e4SLinus Torvalds * @kcontrol: the control instance
3231da177e4SLinus Torvalds *
3241da177e4SLinus Torvalds * Releases the control instance created via snd_ctl_new()
3251da177e4SLinus Torvalds * or snd_ctl_new1().
3261da177e4SLinus Torvalds * Don't call this after the control was added to the card.
3271da177e4SLinus Torvalds */
snd_ctl_free_one(struct snd_kcontrol * kcontrol)32882e9bae6STakashi Iwai void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
3291da177e4SLinus Torvalds {
3301da177e4SLinus Torvalds if (kcontrol) {
3311da177e4SLinus Torvalds if (kcontrol->private_free)
3321da177e4SLinus Torvalds kcontrol->private_free(kcontrol);
3331da177e4SLinus Torvalds kfree(kcontrol);
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds }
336c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_ctl_free_one);
337c0d3fb39STakashi Iwai
snd_ctl_remove_numid_conflict(struct snd_card * card,unsigned int count)3380e82e5faSClemens Ladisch static bool snd_ctl_remove_numid_conflict(struct snd_card *card,
3391da177e4SLinus Torvalds unsigned int count)
3401da177e4SLinus Torvalds {
34182e9bae6STakashi Iwai struct snd_kcontrol *kctl;
3421da177e4SLinus Torvalds
343ac902c11SLars-Peter Clausen /* Make sure that the ids assigned to the control do not wrap around */
344ac902c11SLars-Peter Clausen if (card->last_numid >= UINT_MAX - count)
345ac902c11SLars-Peter Clausen card->last_numid = 0;
346ac902c11SLars-Peter Clausen
3479244b2c3SJohannes Berg list_for_each_entry(kctl, &card->controls, list) {
3487c733587SClemens Ladisch if (kctl->id.numid < card->last_numid + 1 + count &&
3490e82e5faSClemens Ladisch kctl->id.numid + kctl->count > card->last_numid + 1) {
3500e82e5faSClemens Ladisch card->last_numid = kctl->id.numid + kctl->count - 1;
3510e82e5faSClemens Ladisch return true;
3521da177e4SLinus Torvalds }
3530e82e5faSClemens Ladisch }
3540e82e5faSClemens Ladisch return false;
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds
snd_ctl_find_hole(struct snd_card * card,unsigned int count)35782e9bae6STakashi Iwai static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
3581da177e4SLinus Torvalds {
3590e82e5faSClemens Ladisch unsigned int iter = 100000;
3601da177e4SLinus Torvalds
3610e82e5faSClemens Ladisch while (snd_ctl_remove_numid_conflict(card, count)) {
3621da177e4SLinus Torvalds if (--iter == 0) {
3631da177e4SLinus Torvalds /* this situation is very unlikely */
364bb009457STakashi Iwai dev_err(card->dev, "unable to allocate new control numid\n");
3651da177e4SLinus Torvalds return -ENOMEM;
3661da177e4SLinus Torvalds }
3671da177e4SLinus Torvalds }
3681da177e4SLinus Torvalds return 0;
3691da177e4SLinus Torvalds }
3701da177e4SLinus Torvalds
371c27e1efbSTakashi Iwai /* check whether the given id is contained in the given kctl */
elem_id_matches(const struct snd_kcontrol * kctl,const struct snd_ctl_elem_id * id)372c27e1efbSTakashi Iwai static bool elem_id_matches(const struct snd_kcontrol *kctl,
373c27e1efbSTakashi Iwai const struct snd_ctl_elem_id *id)
374c27e1efbSTakashi Iwai {
375c27e1efbSTakashi Iwai return kctl->id.iface == id->iface &&
376c27e1efbSTakashi Iwai kctl->id.device == id->device &&
377c27e1efbSTakashi Iwai kctl->id.subdevice == id->subdevice &&
378c27e1efbSTakashi Iwai !strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)) &&
379c27e1efbSTakashi Iwai kctl->id.index <= id->index &&
380c27e1efbSTakashi Iwai kctl->id.index + kctl->count > id->index;
381c27e1efbSTakashi Iwai }
382c27e1efbSTakashi Iwai
383c27e1efbSTakashi Iwai #ifdef CONFIG_SND_CTL_FAST_LOOKUP
384c27e1efbSTakashi Iwai /* Compute a hash key for the corresponding ctl id
385c27e1efbSTakashi Iwai * It's for the name lookup, hence the numid is excluded.
386c27e1efbSTakashi Iwai * The hash key is bound in LONG_MAX to be used for Xarray key.
387c27e1efbSTakashi Iwai */
388c27e1efbSTakashi Iwai #define MULTIPLIER 37
get_ctl_id_hash(const struct snd_ctl_elem_id * id)389c27e1efbSTakashi Iwai static unsigned long get_ctl_id_hash(const struct snd_ctl_elem_id *id)
390c27e1efbSTakashi Iwai {
3916ab55ec0SZheyu Ma int i;
392c27e1efbSTakashi Iwai unsigned long h;
393c27e1efbSTakashi Iwai
394c27e1efbSTakashi Iwai h = id->iface;
395c27e1efbSTakashi Iwai h = MULTIPLIER * h + id->device;
396c27e1efbSTakashi Iwai h = MULTIPLIER * h + id->subdevice;
3975934d9a0SDan Carpenter for (i = 0; i < SNDRV_CTL_ELEM_ID_NAME_MAXLEN && id->name[i]; i++)
3986ab55ec0SZheyu Ma h = MULTIPLIER * h + id->name[i];
399c27e1efbSTakashi Iwai h = MULTIPLIER * h + id->index;
400c27e1efbSTakashi Iwai h &= LONG_MAX;
401c27e1efbSTakashi Iwai return h;
402c27e1efbSTakashi Iwai }
403c27e1efbSTakashi Iwai
404c27e1efbSTakashi Iwai /* add hash entries to numid and ctl xarray tables */
add_hash_entries(struct snd_card * card,struct snd_kcontrol * kcontrol)405c27e1efbSTakashi Iwai static void add_hash_entries(struct snd_card *card,
406c27e1efbSTakashi Iwai struct snd_kcontrol *kcontrol)
407c27e1efbSTakashi Iwai {
408c27e1efbSTakashi Iwai struct snd_ctl_elem_id id = kcontrol->id;
409c27e1efbSTakashi Iwai int i;
410c27e1efbSTakashi Iwai
411c27e1efbSTakashi Iwai xa_store_range(&card->ctl_numids, kcontrol->id.numid,
412c27e1efbSTakashi Iwai kcontrol->id.numid + kcontrol->count - 1,
413c27e1efbSTakashi Iwai kcontrol, GFP_KERNEL);
414c27e1efbSTakashi Iwai
415c27e1efbSTakashi Iwai for (i = 0; i < kcontrol->count; i++) {
416c27e1efbSTakashi Iwai id.index = kcontrol->id.index + i;
417c27e1efbSTakashi Iwai if (xa_insert(&card->ctl_hash, get_ctl_id_hash(&id),
418c27e1efbSTakashi Iwai kcontrol, GFP_KERNEL)) {
419c27e1efbSTakashi Iwai /* skip hash for this entry, noting we had collision */
420c27e1efbSTakashi Iwai card->ctl_hash_collision = true;
421c27e1efbSTakashi Iwai dev_dbg(card->dev, "ctl_hash collision %d:%s:%d\n",
422c27e1efbSTakashi Iwai id.iface, id.name, id.index);
423c27e1efbSTakashi Iwai }
424c27e1efbSTakashi Iwai }
425c27e1efbSTakashi Iwai }
426c27e1efbSTakashi Iwai
427c27e1efbSTakashi Iwai /* remove hash entries that have been added */
remove_hash_entries(struct snd_card * card,struct snd_kcontrol * kcontrol)428c27e1efbSTakashi Iwai static void remove_hash_entries(struct snd_card *card,
429c27e1efbSTakashi Iwai struct snd_kcontrol *kcontrol)
430c27e1efbSTakashi Iwai {
431c27e1efbSTakashi Iwai struct snd_ctl_elem_id id = kcontrol->id;
432c27e1efbSTakashi Iwai struct snd_kcontrol *matched;
433c27e1efbSTakashi Iwai unsigned long h;
434c27e1efbSTakashi Iwai int i;
435c27e1efbSTakashi Iwai
436c27e1efbSTakashi Iwai for (i = 0; i < kcontrol->count; i++) {
437c27e1efbSTakashi Iwai xa_erase(&card->ctl_numids, id.numid);
438c27e1efbSTakashi Iwai h = get_ctl_id_hash(&id);
439c27e1efbSTakashi Iwai matched = xa_load(&card->ctl_hash, h);
440c27e1efbSTakashi Iwai if (matched && (matched == kcontrol ||
441c27e1efbSTakashi Iwai elem_id_matches(matched, &id)))
442c27e1efbSTakashi Iwai xa_erase(&card->ctl_hash, h);
443c27e1efbSTakashi Iwai id.index++;
444c27e1efbSTakashi Iwai id.numid++;
445c27e1efbSTakashi Iwai }
446c27e1efbSTakashi Iwai }
447c27e1efbSTakashi Iwai #else /* CONFIG_SND_CTL_FAST_LOOKUP */
add_hash_entries(struct snd_card * card,struct snd_kcontrol * kcontrol)448c27e1efbSTakashi Iwai static inline void add_hash_entries(struct snd_card *card,
449c27e1efbSTakashi Iwai struct snd_kcontrol *kcontrol)
450c27e1efbSTakashi Iwai {
451c27e1efbSTakashi Iwai }
remove_hash_entries(struct snd_card * card,struct snd_kcontrol * kcontrol)452c27e1efbSTakashi Iwai static inline void remove_hash_entries(struct snd_card *card,
453c27e1efbSTakashi Iwai struct snd_kcontrol *kcontrol)
454c27e1efbSTakashi Iwai {
455c27e1efbSTakashi Iwai }
456c27e1efbSTakashi Iwai #endif /* CONFIG_SND_CTL_FAST_LOOKUP */
457c27e1efbSTakashi Iwai
4583103c08fSTakashi Iwai enum snd_ctl_add_mode {
4593103c08fSTakashi Iwai CTL_ADD_EXCLUSIVE, CTL_REPLACE, CTL_ADD_ON_REPLACE,
4603103c08fSTakashi Iwai };
4613103c08fSTakashi Iwai
4623103c08fSTakashi Iwai /* add/replace a new kcontrol object; call with card->controls_rwsem locked */
__snd_ctl_add_replace(struct snd_card * card,struct snd_kcontrol * kcontrol,enum snd_ctl_add_mode mode)4633103c08fSTakashi Iwai static int __snd_ctl_add_replace(struct snd_card *card,
4643103c08fSTakashi Iwai struct snd_kcontrol *kcontrol,
4653103c08fSTakashi Iwai enum snd_ctl_add_mode mode)
466e1a7bfe3STakashi Iwai {
467e1a7bfe3STakashi Iwai struct snd_ctl_elem_id id;
468e1a7bfe3STakashi Iwai unsigned int idx;
4693103c08fSTakashi Iwai struct snd_kcontrol *old;
4703103c08fSTakashi Iwai int err;
471e1a7bfe3STakashi Iwai
4728320ba0cSTakashi Iwai lockdep_assert_held_write(&card->controls_rwsem);
4738320ba0cSTakashi Iwai
474e1a7bfe3STakashi Iwai id = kcontrol->id;
475e1a7bfe3STakashi Iwai if (id.index > UINT_MAX - kcontrol->count)
476e1a7bfe3STakashi Iwai return -EINVAL;
477e1a7bfe3STakashi Iwai
478b1e055f6STakashi Iwai old = snd_ctl_find_id_locked(card, &id);
4793103c08fSTakashi Iwai if (!old) {
4803103c08fSTakashi Iwai if (mode == CTL_REPLACE)
4813103c08fSTakashi Iwai return -EINVAL;
4823103c08fSTakashi Iwai } else {
4833103c08fSTakashi Iwai if (mode == CTL_ADD_EXCLUSIVE) {
484e1a7bfe3STakashi Iwai dev_err(card->dev,
485e1a7bfe3STakashi Iwai "control %i:%i:%i:%s:%i is already present\n",
4863103c08fSTakashi Iwai id.iface, id.device, id.subdevice, id.name,
4873103c08fSTakashi Iwai id.index);
488e1a7bfe3STakashi Iwai return -EBUSY;
489e1a7bfe3STakashi Iwai }
490e1a7bfe3STakashi Iwai
491192c4cccSTakashi Iwai err = snd_ctl_remove_locked(card, old);
4923103c08fSTakashi Iwai if (err < 0)
4933103c08fSTakashi Iwai return err;
4943103c08fSTakashi Iwai }
4953103c08fSTakashi Iwai
496e1a7bfe3STakashi Iwai if (snd_ctl_find_hole(card, kcontrol->count) < 0)
497e1a7bfe3STakashi Iwai return -ENOMEM;
498e1a7bfe3STakashi Iwai
499e1a7bfe3STakashi Iwai list_add_tail(&kcontrol->list, &card->controls);
500e1a7bfe3STakashi Iwai card->controls_count += kcontrol->count;
501e1a7bfe3STakashi Iwai kcontrol->id.numid = card->last_numid + 1;
502e1a7bfe3STakashi Iwai card->last_numid += kcontrol->count;
503e1a7bfe3STakashi Iwai
504c27e1efbSTakashi Iwai add_hash_entries(card, kcontrol);
505c27e1efbSTakashi Iwai
5061fa4445fSJaroslav Kysela for (idx = 0; idx < kcontrol->count; idx++)
5071fa4445fSJaroslav Kysela snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_ADD, kcontrol, idx);
508e1a7bfe3STakashi Iwai
509e1a7bfe3STakashi Iwai return 0;
510e1a7bfe3STakashi Iwai }
511e1a7bfe3STakashi Iwai
snd_ctl_add_replace(struct snd_card * card,struct snd_kcontrol * kcontrol,enum snd_ctl_add_mode mode)5123103c08fSTakashi Iwai static int snd_ctl_add_replace(struct snd_card *card,
5133103c08fSTakashi Iwai struct snd_kcontrol *kcontrol,
5143103c08fSTakashi Iwai enum snd_ctl_add_mode mode)
5153103c08fSTakashi Iwai {
5163103c08fSTakashi Iwai int err = -EINVAL;
5173103c08fSTakashi Iwai
5183103c08fSTakashi Iwai if (! kcontrol)
5193103c08fSTakashi Iwai return err;
5203103c08fSTakashi Iwai if (snd_BUG_ON(!card || !kcontrol->info))
5213103c08fSTakashi Iwai goto error;
5223103c08fSTakashi Iwai
5233103c08fSTakashi Iwai down_write(&card->controls_rwsem);
5243103c08fSTakashi Iwai err = __snd_ctl_add_replace(card, kcontrol, mode);
5253103c08fSTakashi Iwai up_write(&card->controls_rwsem);
5263103c08fSTakashi Iwai if (err < 0)
5273103c08fSTakashi Iwai goto error;
5283103c08fSTakashi Iwai return 0;
5293103c08fSTakashi Iwai
5303103c08fSTakashi Iwai error:
5313103c08fSTakashi Iwai snd_ctl_free_one(kcontrol);
5323103c08fSTakashi Iwai return err;
5333103c08fSTakashi Iwai }
5343103c08fSTakashi Iwai
5351da177e4SLinus Torvalds /**
5361da177e4SLinus Torvalds * snd_ctl_add - add the control instance to the card
5371da177e4SLinus Torvalds * @card: the card instance
5381da177e4SLinus Torvalds * @kcontrol: the control instance to add
5391da177e4SLinus Torvalds *
5401da177e4SLinus Torvalds * Adds the control instance created via snd_ctl_new() or
5411da177e4SLinus Torvalds * snd_ctl_new1() to the given card. Assigns also an unique
5421da177e4SLinus Torvalds * numid used for fast search.
5431da177e4SLinus Torvalds *
5441da177e4SLinus Torvalds * It frees automatically the control which cannot be added.
545eb7c06e8SYacine Belkadi *
546eb7c06e8SYacine Belkadi * Return: Zero if successful, or a negative error code on failure.
547eb7c06e8SYacine Belkadi *
5481da177e4SLinus Torvalds */
snd_ctl_add(struct snd_card * card,struct snd_kcontrol * kcontrol)54982e9bae6STakashi Iwai int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
5501da177e4SLinus Torvalds {
5513103c08fSTakashi Iwai return snd_ctl_add_replace(card, kcontrol, CTL_ADD_EXCLUSIVE);
5521da177e4SLinus Torvalds }
553c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_ctl_add);
554c0d3fb39STakashi Iwai
5551da177e4SLinus Torvalds /**
55666b5b972SDimitris Papastamos * snd_ctl_replace - replace the control instance of the card
55766b5b972SDimitris Papastamos * @card: the card instance
55866b5b972SDimitris Papastamos * @kcontrol: the control instance to replace
55966b5b972SDimitris Papastamos * @add_on_replace: add the control if not already added
56066b5b972SDimitris Papastamos *
56166b5b972SDimitris Papastamos * Replaces the given control. If the given control does not exist
56266b5b972SDimitris Papastamos * and the add_on_replace flag is set, the control is added. If the
56366b5b972SDimitris Papastamos * control exists, it is destroyed first.
56466b5b972SDimitris Papastamos *
56566b5b972SDimitris Papastamos * It frees automatically the control which cannot be added or replaced.
566eb7c06e8SYacine Belkadi *
567eb7c06e8SYacine Belkadi * Return: Zero if successful, or a negative error code on failure.
56866b5b972SDimitris Papastamos */
snd_ctl_replace(struct snd_card * card,struct snd_kcontrol * kcontrol,bool add_on_replace)56966b5b972SDimitris Papastamos int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol,
57066b5b972SDimitris Papastamos bool add_on_replace)
57166b5b972SDimitris Papastamos {
5723103c08fSTakashi Iwai return snd_ctl_add_replace(card, kcontrol,
5733103c08fSTakashi Iwai add_on_replace ? CTL_ADD_ON_REPLACE : CTL_REPLACE);
57466b5b972SDimitris Papastamos }
57566b5b972SDimitris Papastamos EXPORT_SYMBOL(snd_ctl_replace);
57666b5b972SDimitris Papastamos
__snd_ctl_remove(struct snd_card * card,struct snd_kcontrol * kcontrol,bool remove_hash)577c27e1efbSTakashi Iwai static int __snd_ctl_remove(struct snd_card *card,
578c27e1efbSTakashi Iwai struct snd_kcontrol *kcontrol,
579c27e1efbSTakashi Iwai bool remove_hash)
580c27e1efbSTakashi Iwai {
581c27e1efbSTakashi Iwai unsigned int idx;
582c27e1efbSTakashi Iwai
5838320ba0cSTakashi Iwai lockdep_assert_held_write(&card->controls_rwsem);
5848320ba0cSTakashi Iwai
585c27e1efbSTakashi Iwai if (snd_BUG_ON(!card || !kcontrol))
586c27e1efbSTakashi Iwai return -EINVAL;
587c27e1efbSTakashi Iwai list_del(&kcontrol->list);
588c27e1efbSTakashi Iwai
589c27e1efbSTakashi Iwai if (remove_hash)
590c27e1efbSTakashi Iwai remove_hash_entries(card, kcontrol);
591c27e1efbSTakashi Iwai
592c27e1efbSTakashi Iwai card->controls_count -= kcontrol->count;
593c27e1efbSTakashi Iwai for (idx = 0; idx < kcontrol->count; idx++)
594c27e1efbSTakashi Iwai snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_REMOVE, kcontrol, idx);
595c27e1efbSTakashi Iwai snd_ctl_free_one(kcontrol);
596c27e1efbSTakashi Iwai return 0;
597c27e1efbSTakashi Iwai }
598c27e1efbSTakashi Iwai
snd_ctl_remove_locked(struct snd_card * card,struct snd_kcontrol * kcontrol)599192c4cccSTakashi Iwai static inline int snd_ctl_remove_locked(struct snd_card *card,
600192c4cccSTakashi Iwai struct snd_kcontrol *kcontrol)
601192c4cccSTakashi Iwai {
602192c4cccSTakashi Iwai return __snd_ctl_remove(card, kcontrol, true);
603192c4cccSTakashi Iwai }
604192c4cccSTakashi Iwai
60566b5b972SDimitris Papastamos /**
6061da177e4SLinus Torvalds * snd_ctl_remove - remove the control from the card and release it
6071da177e4SLinus Torvalds * @card: the card instance
6081da177e4SLinus Torvalds * @kcontrol: the control instance to remove
6091da177e4SLinus Torvalds *
6101da177e4SLinus Torvalds * Removes the control from the card and then releases the instance.
611192c4cccSTakashi Iwai * You don't need to call snd_ctl_free_one().
6121da177e4SLinus Torvalds *
613eb7c06e8SYacine Belkadi * Return: 0 if successful, or a negative error code on failure.
614192c4cccSTakashi Iwai *
615192c4cccSTakashi Iwai * Note that this function takes card->controls_rwsem lock internally.
6161da177e4SLinus Torvalds */
snd_ctl_remove(struct snd_card * card,struct snd_kcontrol * kcontrol)61782e9bae6STakashi Iwai int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
6181da177e4SLinus Torvalds {
619192c4cccSTakashi Iwai int ret;
620192c4cccSTakashi Iwai
621192c4cccSTakashi Iwai down_write(&card->controls_rwsem);
622192c4cccSTakashi Iwai ret = snd_ctl_remove_locked(card, kcontrol);
623192c4cccSTakashi Iwai up_write(&card->controls_rwsem);
624192c4cccSTakashi Iwai return ret;
6251da177e4SLinus Torvalds }
626c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_ctl_remove);
627c0d3fb39STakashi Iwai
6281da177e4SLinus Torvalds /**
6291da177e4SLinus Torvalds * snd_ctl_remove_id - remove the control of the given id and release it
6301da177e4SLinus Torvalds * @card: the card instance
6311da177e4SLinus Torvalds * @id: the control id to remove
6321da177e4SLinus Torvalds *
6331da177e4SLinus Torvalds * Finds the control instance with the given id, removes it from the
6341da177e4SLinus Torvalds * card list and releases it.
6351da177e4SLinus Torvalds *
636eb7c06e8SYacine Belkadi * Return: 0 if successful, or a negative error code on failure.
6371da177e4SLinus Torvalds */
snd_ctl_remove_id(struct snd_card * card,struct snd_ctl_elem_id * id)63882e9bae6STakashi Iwai int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
6391da177e4SLinus Torvalds {
64082e9bae6STakashi Iwai struct snd_kcontrol *kctl;
6411da177e4SLinus Torvalds int ret;
6421da177e4SLinus Torvalds
6431da177e4SLinus Torvalds down_write(&card->controls_rwsem);
644b1e055f6STakashi Iwai kctl = snd_ctl_find_id_locked(card, id);
6451da177e4SLinus Torvalds if (kctl == NULL) {
6461da177e4SLinus Torvalds up_write(&card->controls_rwsem);
6471da177e4SLinus Torvalds return -ENOENT;
6481da177e4SLinus Torvalds }
649192c4cccSTakashi Iwai ret = snd_ctl_remove_locked(card, kctl);
6501da177e4SLinus Torvalds up_write(&card->controls_rwsem);
6511da177e4SLinus Torvalds return ret;
6521da177e4SLinus Torvalds }
653c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_ctl_remove_id);
654c0d3fb39STakashi Iwai
6551da177e4SLinus Torvalds /**
656f217ac59SClemens Ladisch * snd_ctl_remove_user_ctl - remove and release the unlocked user control
6571da177e4SLinus Torvalds * @file: active control handle
6581da177e4SLinus Torvalds * @id: the control id to remove
6591da177e4SLinus Torvalds *
6601da177e4SLinus Torvalds * Finds the control instance with the given id, removes it from the
6611da177e4SLinus Torvalds * card list and releases it.
6621da177e4SLinus Torvalds *
663eb7c06e8SYacine Belkadi * Return: 0 if successful, or a negative error code on failure.
6641da177e4SLinus Torvalds */
snd_ctl_remove_user_ctl(struct snd_ctl_file * file,struct snd_ctl_elem_id * id)665f217ac59SClemens Ladisch static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
66682e9bae6STakashi Iwai struct snd_ctl_elem_id *id)
6671da177e4SLinus Torvalds {
66882e9bae6STakashi Iwai struct snd_card *card = file->card;
66982e9bae6STakashi Iwai struct snd_kcontrol *kctl;
6701da177e4SLinus Torvalds int idx, ret;
6711da177e4SLinus Torvalds
6721da177e4SLinus Torvalds down_write(&card->controls_rwsem);
673b1e055f6STakashi Iwai kctl = snd_ctl_find_id_locked(card, id);
6741da177e4SLinus Torvalds if (kctl == NULL) {
675317b8081SClemens Ladisch ret = -ENOENT;
676317b8081SClemens Ladisch goto error;
6771da177e4SLinus Torvalds }
67818dd0aa5SClemens Ladisch if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
67918dd0aa5SClemens Ladisch ret = -EINVAL;
68018dd0aa5SClemens Ladisch goto error;
68118dd0aa5SClemens Ladisch }
6821da177e4SLinus Torvalds for (idx = 0; idx < kctl->count; idx++)
6831da177e4SLinus Torvalds if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
684317b8081SClemens Ladisch ret = -EBUSY;
685317b8081SClemens Ladisch goto error;
6861da177e4SLinus Torvalds }
687192c4cccSTakashi Iwai ret = snd_ctl_remove_locked(card, kctl);
688317b8081SClemens Ladisch error:
6891da177e4SLinus Torvalds up_write(&card->controls_rwsem);
6901da177e4SLinus Torvalds return ret;
6911da177e4SLinus Torvalds }
6921da177e4SLinus Torvalds
6931da177e4SLinus Torvalds /**
6943cbdd753STakashi Iwai * snd_ctl_activate_id - activate/inactivate the control of the given id
6953cbdd753STakashi Iwai * @card: the card instance
6963cbdd753STakashi Iwai * @id: the control id to activate/inactivate
6973cbdd753STakashi Iwai * @active: non-zero to activate
6983cbdd753STakashi Iwai *
6993cbdd753STakashi Iwai * Finds the control instance with the given id, and activate or
7003cbdd753STakashi Iwai * inactivate the control together with notification, if changed.
701c78497e0STakashi Sakamoto * The given ID data is filled with full information.
7023cbdd753STakashi Iwai *
703eb7c06e8SYacine Belkadi * Return: 0 if unchanged, 1 if changed, or a negative error code on failure.
7043cbdd753STakashi Iwai */
snd_ctl_activate_id(struct snd_card * card,struct snd_ctl_elem_id * id,int active)7053cbdd753STakashi Iwai int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
7063cbdd753STakashi Iwai int active)
7073cbdd753STakashi Iwai {
7083cbdd753STakashi Iwai struct snd_kcontrol *kctl;
7093cbdd753STakashi Iwai struct snd_kcontrol_volatile *vd;
7103cbdd753STakashi Iwai unsigned int index_offset;
7113cbdd753STakashi Iwai int ret;
7123cbdd753STakashi Iwai
7133cbdd753STakashi Iwai down_write(&card->controls_rwsem);
714b1e055f6STakashi Iwai kctl = snd_ctl_find_id_locked(card, id);
7153cbdd753STakashi Iwai if (kctl == NULL) {
7163cbdd753STakashi Iwai ret = -ENOENT;
7173cbdd753STakashi Iwai goto unlock;
7183cbdd753STakashi Iwai }
71931584ed1SLars-Peter Clausen index_offset = snd_ctl_get_ioff(kctl, id);
7203cbdd753STakashi Iwai vd = &kctl->vd[index_offset];
7213cbdd753STakashi Iwai ret = 0;
7223cbdd753STakashi Iwai if (active) {
7233cbdd753STakashi Iwai if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
7243cbdd753STakashi Iwai goto unlock;
7253cbdd753STakashi Iwai vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
7263cbdd753STakashi Iwai } else {
7273cbdd753STakashi Iwai if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)
7283cbdd753STakashi Iwai goto unlock;
7293cbdd753STakashi Iwai vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
7303cbdd753STakashi Iwai }
731c78497e0STakashi Sakamoto snd_ctl_build_ioff(id, kctl, index_offset);
7321fa4445fSJaroslav Kysela downgrade_write(&card->controls_rwsem);
7331fa4445fSJaroslav Kysela snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_INFO, kctl, index_offset);
7341fa4445fSJaroslav Kysela up_read(&card->controls_rwsem);
7351fa4445fSJaroslav Kysela return 1;
7361fa4445fSJaroslav Kysela
7373cbdd753STakashi Iwai unlock:
7383cbdd753STakashi Iwai up_write(&card->controls_rwsem);
7393cbdd753STakashi Iwai return ret;
7403cbdd753STakashi Iwai }
7413cbdd753STakashi Iwai EXPORT_SYMBOL_GPL(snd_ctl_activate_id);
7423cbdd753STakashi Iwai
7433cbdd753STakashi Iwai /**
7441da177e4SLinus Torvalds * snd_ctl_rename_id - replace the id of a control on the card
7451da177e4SLinus Torvalds * @card: the card instance
7461da177e4SLinus Torvalds * @src_id: the old id
7471da177e4SLinus Torvalds * @dst_id: the new id
7481da177e4SLinus Torvalds *
7491da177e4SLinus Torvalds * Finds the control with the old id from the card, and replaces the
7501da177e4SLinus Torvalds * id with the new one.
7511da177e4SLinus Torvalds *
752306f3f78STakashi Iwai * The function tries to keep the already assigned numid while replacing
753306f3f78STakashi Iwai * the rest.
754306f3f78STakashi Iwai *
755306f3f78STakashi Iwai * Note that this function should be used only in the card initialization
756306f3f78STakashi Iwai * phase. Calling after the card instantiation may cause issues with
757306f3f78STakashi Iwai * user-space expecting persistent numids.
758306f3f78STakashi Iwai *
759eb7c06e8SYacine Belkadi * Return: Zero if successful, or a negative error code on failure.
7601da177e4SLinus Torvalds */
snd_ctl_rename_id(struct snd_card * card,struct snd_ctl_elem_id * src_id,struct snd_ctl_elem_id * dst_id)76182e9bae6STakashi Iwai int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
76282e9bae6STakashi Iwai struct snd_ctl_elem_id *dst_id)
7631da177e4SLinus Torvalds {
76482e9bae6STakashi Iwai struct snd_kcontrol *kctl;
765306f3f78STakashi Iwai int saved_numid;
7661da177e4SLinus Torvalds
7671da177e4SLinus Torvalds down_write(&card->controls_rwsem);
768b1e055f6STakashi Iwai kctl = snd_ctl_find_id_locked(card, src_id);
7691da177e4SLinus Torvalds if (kctl == NULL) {
7701da177e4SLinus Torvalds up_write(&card->controls_rwsem);
7711da177e4SLinus Torvalds return -ENOENT;
7721da177e4SLinus Torvalds }
773306f3f78STakashi Iwai saved_numid = kctl->id.numid;
774c27e1efbSTakashi Iwai remove_hash_entries(card, kctl);
7751da177e4SLinus Torvalds kctl->id = *dst_id;
776306f3f78STakashi Iwai kctl->id.numid = saved_numid;
777c27e1efbSTakashi Iwai add_hash_entries(card, kctl);
7781da177e4SLinus Torvalds up_write(&card->controls_rwsem);
7791da177e4SLinus Torvalds return 0;
7801da177e4SLinus Torvalds }
781c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_ctl_rename_id);
782c0d3fb39STakashi Iwai
783966f015fSMaciej S. Szmigiero /**
784966f015fSMaciej S. Szmigiero * snd_ctl_rename - rename the control on the card
785966f015fSMaciej S. Szmigiero * @card: the card instance
786966f015fSMaciej S. Szmigiero * @kctl: the control to rename
787966f015fSMaciej S. Szmigiero * @name: the new name
788966f015fSMaciej S. Szmigiero *
789966f015fSMaciej S. Szmigiero * Renames the specified control on the card to the new name.
790966f015fSMaciej S. Szmigiero *
791d4e99962STakashi Iwai * Note that this function takes card->controls_rwsem lock internally.
792966f015fSMaciej S. Szmigiero */
snd_ctl_rename(struct snd_card * card,struct snd_kcontrol * kctl,const char * name)793966f015fSMaciej S. Szmigiero void snd_ctl_rename(struct snd_card *card, struct snd_kcontrol *kctl,
794966f015fSMaciej S. Szmigiero const char *name)
795966f015fSMaciej S. Szmigiero {
796d4e99962STakashi Iwai down_write(&card->controls_rwsem);
797966f015fSMaciej S. Szmigiero remove_hash_entries(card, kctl);
798966f015fSMaciej S. Szmigiero
799966f015fSMaciej S. Szmigiero if (strscpy(kctl->id.name, name, sizeof(kctl->id.name)) < 0)
800966f015fSMaciej S. Szmigiero pr_warn("ALSA: Renamed control new name '%s' truncated to '%s'\n",
801966f015fSMaciej S. Szmigiero name, kctl->id.name);
802966f015fSMaciej S. Szmigiero
803966f015fSMaciej S. Szmigiero add_hash_entries(card, kctl);
804d4e99962STakashi Iwai up_write(&card->controls_rwsem);
805966f015fSMaciej S. Szmigiero }
806966f015fSMaciej S. Szmigiero EXPORT_SYMBOL(snd_ctl_rename);
807966f015fSMaciej S. Szmigiero
808c27e1efbSTakashi Iwai #ifndef CONFIG_SND_CTL_FAST_LOOKUP
809c27e1efbSTakashi Iwai static struct snd_kcontrol *
snd_ctl_find_numid_slow(struct snd_card * card,unsigned int numid)810c27e1efbSTakashi Iwai snd_ctl_find_numid_slow(struct snd_card *card, unsigned int numid)
811c27e1efbSTakashi Iwai {
812c27e1efbSTakashi Iwai struct snd_kcontrol *kctl;
813c27e1efbSTakashi Iwai
814c27e1efbSTakashi Iwai list_for_each_entry(kctl, &card->controls, list) {
815c27e1efbSTakashi Iwai if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
816c27e1efbSTakashi Iwai return kctl;
817c27e1efbSTakashi Iwai }
818c27e1efbSTakashi Iwai return NULL;
819c27e1efbSTakashi Iwai }
820c27e1efbSTakashi Iwai #endif /* !CONFIG_SND_CTL_FAST_LOOKUP */
821c27e1efbSTakashi Iwai
8221da177e4SLinus Torvalds /**
823b1e055f6STakashi Iwai * snd_ctl_find_numid_locked - find the control instance with the given number-id
8241da177e4SLinus Torvalds * @card: the card instance
8251da177e4SLinus Torvalds * @numid: the number-id to search
8261da177e4SLinus Torvalds *
8271da177e4SLinus Torvalds * Finds the control instance with the given number-id from the card.
8281da177e4SLinus Torvalds *
8291da177e4SLinus Torvalds * The caller must down card->controls_rwsem before calling this function
8301da177e4SLinus Torvalds * (if the race condition can happen).
831eb7c06e8SYacine Belkadi *
832eb7c06e8SYacine Belkadi * Return: The pointer of the instance if found, or %NULL if not.
8331da177e4SLinus Torvalds */
834b1e055f6STakashi Iwai struct snd_kcontrol *
snd_ctl_find_numid_locked(struct snd_card * card,unsigned int numid)835b1e055f6STakashi Iwai snd_ctl_find_numid_locked(struct snd_card *card, unsigned int numid)
8361da177e4SLinus Torvalds {
8377eaa943cSTakashi Iwai if (snd_BUG_ON(!card || !numid))
8387eaa943cSTakashi Iwai return NULL;
8399c2cc565STakashi Iwai lockdep_assert_held(&card->controls_rwsem);
840c27e1efbSTakashi Iwai #ifdef CONFIG_SND_CTL_FAST_LOOKUP
841c27e1efbSTakashi Iwai return xa_load(&card->ctl_numids, numid);
842c27e1efbSTakashi Iwai #else
843c27e1efbSTakashi Iwai return snd_ctl_find_numid_slow(card, numid);
844c27e1efbSTakashi Iwai #endif
8451da177e4SLinus Torvalds }
846b1e055f6STakashi Iwai EXPORT_SYMBOL(snd_ctl_find_numid_locked);
847b1e055f6STakashi Iwai
848b1e055f6STakashi Iwai /**
849b1e055f6STakashi Iwai * snd_ctl_find_numid - find the control instance with the given number-id
850b1e055f6STakashi Iwai * @card: the card instance
851b1e055f6STakashi Iwai * @numid: the number-id to search
852b1e055f6STakashi Iwai *
853b1e055f6STakashi Iwai * Finds the control instance with the given number-id from the card.
854b1e055f6STakashi Iwai *
855b1e055f6STakashi Iwai * Return: The pointer of the instance if found, or %NULL if not.
8569c2cc565STakashi Iwai *
8579c2cc565STakashi Iwai * Note that this function takes card->controls_rwsem lock internally.
858b1e055f6STakashi Iwai */
snd_ctl_find_numid(struct snd_card * card,unsigned int numid)859b1e055f6STakashi Iwai struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card,
860b1e055f6STakashi Iwai unsigned int numid)
861b1e055f6STakashi Iwai {
8629c2cc565STakashi Iwai struct snd_kcontrol *kctl;
8639c2cc565STakashi Iwai
8649c2cc565STakashi Iwai down_read(&card->controls_rwsem);
8659c2cc565STakashi Iwai kctl = snd_ctl_find_numid_locked(card, numid);
8669c2cc565STakashi Iwai up_read(&card->controls_rwsem);
8679c2cc565STakashi Iwai return kctl;
868b1e055f6STakashi Iwai }
869c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_ctl_find_numid);
870c0d3fb39STakashi Iwai
8711da177e4SLinus Torvalds /**
872b1e055f6STakashi Iwai * snd_ctl_find_id_locked - find the control instance with the given id
8731da177e4SLinus Torvalds * @card: the card instance
8741da177e4SLinus Torvalds * @id: the id to search
8751da177e4SLinus Torvalds *
8761da177e4SLinus Torvalds * Finds the control instance with the given id from the card.
8771da177e4SLinus Torvalds *
8781da177e4SLinus Torvalds * The caller must down card->controls_rwsem before calling this function
8791da177e4SLinus Torvalds * (if the race condition can happen).
880eb7c06e8SYacine Belkadi *
881eb7c06e8SYacine Belkadi * Return: The pointer of the instance if found, or %NULL if not.
8821da177e4SLinus Torvalds */
snd_ctl_find_id_locked(struct snd_card * card,const struct snd_ctl_elem_id * id)883b1e055f6STakashi Iwai struct snd_kcontrol *snd_ctl_find_id_locked(struct snd_card *card,
8846723670aSTakashi Iwai const struct snd_ctl_elem_id *id)
8851da177e4SLinus Torvalds {
88682e9bae6STakashi Iwai struct snd_kcontrol *kctl;
8871da177e4SLinus Torvalds
8887eaa943cSTakashi Iwai if (snd_BUG_ON(!card || !id))
8897eaa943cSTakashi Iwai return NULL;
8909c2cc565STakashi Iwai lockdep_assert_held(&card->controls_rwsem);
8911da177e4SLinus Torvalds if (id->numid != 0)
892b1e055f6STakashi Iwai return snd_ctl_find_numid_locked(card, id->numid);
893c27e1efbSTakashi Iwai #ifdef CONFIG_SND_CTL_FAST_LOOKUP
894c27e1efbSTakashi Iwai kctl = xa_load(&card->ctl_hash, get_ctl_id_hash(id));
895c27e1efbSTakashi Iwai if (kctl && elem_id_matches(kctl, id))
8961da177e4SLinus Torvalds return kctl;
897c27e1efbSTakashi Iwai if (!card->ctl_hash_collision)
898c27e1efbSTakashi Iwai return NULL; /* we can rely on only hash table */
899c27e1efbSTakashi Iwai #endif
900c27e1efbSTakashi Iwai /* no matching in hash table - try all as the last resort */
901c27e1efbSTakashi Iwai list_for_each_entry(kctl, &card->controls, list)
902c27e1efbSTakashi Iwai if (elem_id_matches(kctl, id))
903c27e1efbSTakashi Iwai return kctl;
904c27e1efbSTakashi Iwai
9051da177e4SLinus Torvalds return NULL;
9061da177e4SLinus Torvalds }
907b1e055f6STakashi Iwai EXPORT_SYMBOL(snd_ctl_find_id_locked);
908b1e055f6STakashi Iwai
909b1e055f6STakashi Iwai /**
910b1e055f6STakashi Iwai * snd_ctl_find_id - find the control instance with the given id
911b1e055f6STakashi Iwai * @card: the card instance
912b1e055f6STakashi Iwai * @id: the id to search
913b1e055f6STakashi Iwai *
914b1e055f6STakashi Iwai * Finds the control instance with the given id from the card.
915b1e055f6STakashi Iwai *
916b1e055f6STakashi Iwai * Return: The pointer of the instance if found, or %NULL if not.
9179c2cc565STakashi Iwai *
9189c2cc565STakashi Iwai * Note that this function takes card->controls_rwsem lock internally.
919b1e055f6STakashi Iwai */
snd_ctl_find_id(struct snd_card * card,const struct snd_ctl_elem_id * id)920b1e055f6STakashi Iwai struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
921b1e055f6STakashi Iwai const struct snd_ctl_elem_id *id)
922b1e055f6STakashi Iwai {
9239c2cc565STakashi Iwai struct snd_kcontrol *kctl;
9249c2cc565STakashi Iwai
9259c2cc565STakashi Iwai down_read(&card->controls_rwsem);
9269c2cc565STakashi Iwai kctl = snd_ctl_find_id_locked(card, id);
9279c2cc565STakashi Iwai up_read(&card->controls_rwsem);
9289c2cc565STakashi Iwai return kctl;
929b1e055f6STakashi Iwai }
930c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_ctl_find_id);
931c0d3fb39STakashi Iwai
snd_ctl_card_info(struct snd_card * card,struct snd_ctl_file * ctl,unsigned int cmd,void __user * arg)93282e9bae6STakashi Iwai static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
9331da177e4SLinus Torvalds unsigned int cmd, void __user *arg)
9341da177e4SLinus Torvalds {
93582e9bae6STakashi Iwai struct snd_ctl_card_info *info;
9361da177e4SLinus Torvalds
937ca2c0966STakashi Iwai info = kzalloc(sizeof(*info), GFP_KERNEL);
9381da177e4SLinus Torvalds if (! info)
9391da177e4SLinus Torvalds return -ENOMEM;
9401da177e4SLinus Torvalds down_read(&snd_ioctl_rwsem);
9411da177e4SLinus Torvalds info->card = card->number;
94275b1a8f9SJoe Perches strscpy(info->id, card->id, sizeof(info->id));
94375b1a8f9SJoe Perches strscpy(info->driver, card->driver, sizeof(info->driver));
94475b1a8f9SJoe Perches strscpy(info->name, card->shortname, sizeof(info->name));
94575b1a8f9SJoe Perches strscpy(info->longname, card->longname, sizeof(info->longname));
94675b1a8f9SJoe Perches strscpy(info->mixername, card->mixername, sizeof(info->mixername));
94775b1a8f9SJoe Perches strscpy(info->components, card->components, sizeof(info->components));
9481da177e4SLinus Torvalds up_read(&snd_ioctl_rwsem);
94982e9bae6STakashi Iwai if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
9501da177e4SLinus Torvalds kfree(info);
9511da177e4SLinus Torvalds return -EFAULT;
9521da177e4SLinus Torvalds }
9531da177e4SLinus Torvalds kfree(info);
9541da177e4SLinus Torvalds return 0;
9551da177e4SLinus Torvalds }
9561da177e4SLinus Torvalds
snd_ctl_elem_list(struct snd_card * card,struct snd_ctl_elem_list * list)95782e9bae6STakashi Iwai static int snd_ctl_elem_list(struct snd_card *card,
95818d122c0SArnd Bergmann struct snd_ctl_elem_list *list)
9591da177e4SLinus Torvalds {
96082e9bae6STakashi Iwai struct snd_kcontrol *kctl;
96153e7bf45STakashi Iwai struct snd_ctl_elem_id id;
96278fa2c4dSLuca Tettamanti unsigned int offset, space, jidx;
96353e7bf45STakashi Iwai int err = 0;
9641da177e4SLinus Torvalds
96518d122c0SArnd Bergmann offset = list->offset;
96618d122c0SArnd Bergmann space = list->space;
9674e361d3cSTakashi Sakamoto
9681da177e4SLinus Torvalds down_read(&card->controls_rwsem);
96918d122c0SArnd Bergmann list->count = card->controls_count;
97018d122c0SArnd Bergmann list->used = 0;
97153e7bf45STakashi Iwai if (space > 0) {
97253e7bf45STakashi Iwai list_for_each_entry(kctl, &card->controls, list) {
97353e7bf45STakashi Iwai if (offset >= kctl->count) {
97453e7bf45STakashi Iwai offset -= kctl->count;
97553e7bf45STakashi Iwai continue;
9761da177e4SLinus Torvalds }
97753e7bf45STakashi Iwai for (jidx = offset; jidx < kctl->count; jidx++) {
97853e7bf45STakashi Iwai snd_ctl_build_ioff(&id, kctl, jidx);
97918d122c0SArnd Bergmann if (copy_to_user(list->pids + list->used, &id,
98053e7bf45STakashi Iwai sizeof(id))) {
98153e7bf45STakashi Iwai err = -EFAULT;
98253e7bf45STakashi Iwai goto out;
98353e7bf45STakashi Iwai }
98418d122c0SArnd Bergmann list->used++;
98553e7bf45STakashi Iwai if (!--space)
98653e7bf45STakashi Iwai goto out;
98753e7bf45STakashi Iwai }
9881da177e4SLinus Torvalds offset = 0;
9891da177e4SLinus Torvalds }
9901da177e4SLinus Torvalds }
99153e7bf45STakashi Iwai out:
9921da177e4SLinus Torvalds up_read(&card->controls_rwsem);
99353e7bf45STakashi Iwai return err;
9941da177e4SLinus Torvalds }
9951da177e4SLinus Torvalds
snd_ctl_elem_list_user(struct snd_card * card,struct snd_ctl_elem_list __user * _list)99618d122c0SArnd Bergmann static int snd_ctl_elem_list_user(struct snd_card *card,
99718d122c0SArnd Bergmann struct snd_ctl_elem_list __user *_list)
99818d122c0SArnd Bergmann {
99918d122c0SArnd Bergmann struct snd_ctl_elem_list list;
100018d122c0SArnd Bergmann int err;
100118d122c0SArnd Bergmann
100218d122c0SArnd Bergmann if (copy_from_user(&list, _list, sizeof(list)))
100318d122c0SArnd Bergmann return -EFAULT;
100418d122c0SArnd Bergmann err = snd_ctl_elem_list(card, &list);
100518d122c0SArnd Bergmann if (err)
100618d122c0SArnd Bergmann return err;
100718d122c0SArnd Bergmann if (copy_to_user(_list, &list, sizeof(list)))
100818d122c0SArnd Bergmann return -EFAULT;
100918d122c0SArnd Bergmann
101018d122c0SArnd Bergmann return 0;
101118d122c0SArnd Bergmann }
101218d122c0SArnd Bergmann
1013fbd3eb7fSTakashi Iwai /* Check whether the given kctl info is valid */
snd_ctl_check_elem_info(struct snd_card * card,const struct snd_ctl_elem_info * info)1014fbd3eb7fSTakashi Iwai static int snd_ctl_check_elem_info(struct snd_card *card,
1015fbd3eb7fSTakashi Iwai const struct snd_ctl_elem_info *info)
10161da177e4SLinus Torvalds {
1017fbd3eb7fSTakashi Iwai static const unsigned int max_value_counts[] = {
1018fbd3eb7fSTakashi Iwai [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = 128,
1019fbd3eb7fSTakashi Iwai [SNDRV_CTL_ELEM_TYPE_INTEGER] = 128,
1020fbd3eb7fSTakashi Iwai [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = 128,
1021fbd3eb7fSTakashi Iwai [SNDRV_CTL_ELEM_TYPE_BYTES] = 512,
1022fbd3eb7fSTakashi Iwai [SNDRV_CTL_ELEM_TYPE_IEC958] = 1,
1023fbd3eb7fSTakashi Iwai [SNDRV_CTL_ELEM_TYPE_INTEGER64] = 64,
1024fbd3eb7fSTakashi Iwai };
1025fbd3eb7fSTakashi Iwai
1026fbd3eb7fSTakashi Iwai if (info->type < SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
1027fbd3eb7fSTakashi Iwai info->type > SNDRV_CTL_ELEM_TYPE_INTEGER64) {
1028fbd3eb7fSTakashi Iwai if (card)
1029fbd3eb7fSTakashi Iwai dev_err(card->dev,
1030fbd3eb7fSTakashi Iwai "control %i:%i:%i:%s:%i: invalid type %d\n",
1031fbd3eb7fSTakashi Iwai info->id.iface, info->id.device,
1032fbd3eb7fSTakashi Iwai info->id.subdevice, info->id.name,
1033fbd3eb7fSTakashi Iwai info->id.index, info->type);
1034fbd3eb7fSTakashi Iwai return -EINVAL;
1035fbd3eb7fSTakashi Iwai }
1036fbd3eb7fSTakashi Iwai if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED &&
1037fbd3eb7fSTakashi Iwai info->value.enumerated.items == 0) {
1038fbd3eb7fSTakashi Iwai if (card)
1039fbd3eb7fSTakashi Iwai dev_err(card->dev,
1040fbd3eb7fSTakashi Iwai "control %i:%i:%i:%s:%i: zero enum items\n",
1041fbd3eb7fSTakashi Iwai info->id.iface, info->id.device,
1042fbd3eb7fSTakashi Iwai info->id.subdevice, info->id.name,
1043fbd3eb7fSTakashi Iwai info->id.index);
1044fbd3eb7fSTakashi Iwai return -EINVAL;
1045fbd3eb7fSTakashi Iwai }
1046fbd3eb7fSTakashi Iwai if (info->count > max_value_counts[info->type]) {
1047fbd3eb7fSTakashi Iwai if (card)
1048fbd3eb7fSTakashi Iwai dev_err(card->dev,
1049fbd3eb7fSTakashi Iwai "control %i:%i:%i:%s:%i: invalid count %d\n",
1050fbd3eb7fSTakashi Iwai info->id.iface, info->id.device,
1051fbd3eb7fSTakashi Iwai info->id.subdevice, info->id.name,
1052fbd3eb7fSTakashi Iwai info->id.index, info->count);
1053fbd3eb7fSTakashi Iwai return -EINVAL;
1054fbd3eb7fSTakashi Iwai }
1055fbd3eb7fSTakashi Iwai
1056fbd3eb7fSTakashi Iwai return 0;
1057fbd3eb7fSTakashi Iwai }
1058fbd3eb7fSTakashi Iwai
1059fbd3eb7fSTakashi Iwai /* The capacity of struct snd_ctl_elem_value.value.*/
1060fbd3eb7fSTakashi Iwai static const unsigned int value_sizes[] = {
1061fbd3eb7fSTakashi Iwai [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = sizeof(long),
1062fbd3eb7fSTakashi Iwai [SNDRV_CTL_ELEM_TYPE_INTEGER] = sizeof(long),
1063fbd3eb7fSTakashi Iwai [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = sizeof(unsigned int),
1064fbd3eb7fSTakashi Iwai [SNDRV_CTL_ELEM_TYPE_BYTES] = sizeof(unsigned char),
1065fbd3eb7fSTakashi Iwai [SNDRV_CTL_ELEM_TYPE_IEC958] = sizeof(struct snd_aes_iec958),
1066fbd3eb7fSTakashi Iwai [SNDRV_CTL_ELEM_TYPE_INTEGER64] = sizeof(long long),
1067fbd3eb7fSTakashi Iwai };
1068fbd3eb7fSTakashi Iwai
1069fbd3eb7fSTakashi Iwai /* fill the remaining snd_ctl_elem_value data with the given pattern */
fill_remaining_elem_value(struct snd_ctl_elem_value * control,struct snd_ctl_elem_info * info,u32 pattern)1070fbd3eb7fSTakashi Iwai static void fill_remaining_elem_value(struct snd_ctl_elem_value *control,
1071fbd3eb7fSTakashi Iwai struct snd_ctl_elem_info *info,
1072fbd3eb7fSTakashi Iwai u32 pattern)
1073fbd3eb7fSTakashi Iwai {
1074fbd3eb7fSTakashi Iwai size_t offset = value_sizes[info->type] * info->count;
1075fbd3eb7fSTakashi Iwai
1076afcfbcb3SLars-Peter Clausen offset = DIV_ROUND_UP(offset, sizeof(u32));
1077fbd3eb7fSTakashi Iwai memset32((u32 *)control->value.bytes.data + offset, pattern,
1078fbd3eb7fSTakashi Iwai sizeof(control->value) / sizeof(u32) - offset);
1079fbd3eb7fSTakashi Iwai }
1080fbd3eb7fSTakashi Iwai
1081fbd3eb7fSTakashi Iwai /* check whether the given integer ctl value is valid */
sanity_check_int_value(struct snd_card * card,const struct snd_ctl_elem_value * control,const struct snd_ctl_elem_info * info,int i,bool print_error)1082fbd3eb7fSTakashi Iwai static int sanity_check_int_value(struct snd_card *card,
1083fbd3eb7fSTakashi Iwai const struct snd_ctl_elem_value *control,
1084fbd3eb7fSTakashi Iwai const struct snd_ctl_elem_info *info,
1085f5e829f9STakashi Iwai int i, bool print_error)
1086fbd3eb7fSTakashi Iwai {
1087fbd3eb7fSTakashi Iwai long long lval, lmin, lmax, lstep;
1088fbd3eb7fSTakashi Iwai u64 rem;
1089fbd3eb7fSTakashi Iwai
1090fbd3eb7fSTakashi Iwai switch (info->type) {
1091fbd3eb7fSTakashi Iwai default:
1092fbd3eb7fSTakashi Iwai case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
1093fbd3eb7fSTakashi Iwai lval = control->value.integer.value[i];
1094fbd3eb7fSTakashi Iwai lmin = 0;
1095fbd3eb7fSTakashi Iwai lmax = 1;
1096fbd3eb7fSTakashi Iwai lstep = 0;
1097fbd3eb7fSTakashi Iwai break;
1098fbd3eb7fSTakashi Iwai case SNDRV_CTL_ELEM_TYPE_INTEGER:
1099fbd3eb7fSTakashi Iwai lval = control->value.integer.value[i];
1100fbd3eb7fSTakashi Iwai lmin = info->value.integer.min;
1101fbd3eb7fSTakashi Iwai lmax = info->value.integer.max;
1102fbd3eb7fSTakashi Iwai lstep = info->value.integer.step;
1103fbd3eb7fSTakashi Iwai break;
1104fbd3eb7fSTakashi Iwai case SNDRV_CTL_ELEM_TYPE_INTEGER64:
1105fbd3eb7fSTakashi Iwai lval = control->value.integer64.value[i];
1106fbd3eb7fSTakashi Iwai lmin = info->value.integer64.min;
1107fbd3eb7fSTakashi Iwai lmax = info->value.integer64.max;
1108fbd3eb7fSTakashi Iwai lstep = info->value.integer64.step;
1109fbd3eb7fSTakashi Iwai break;
1110fbd3eb7fSTakashi Iwai case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
1111fbd3eb7fSTakashi Iwai lval = control->value.enumerated.item[i];
1112fbd3eb7fSTakashi Iwai lmin = 0;
1113fbd3eb7fSTakashi Iwai lmax = info->value.enumerated.items - 1;
1114fbd3eb7fSTakashi Iwai lstep = 0;
1115fbd3eb7fSTakashi Iwai break;
1116fbd3eb7fSTakashi Iwai }
1117fbd3eb7fSTakashi Iwai
1118fbd3eb7fSTakashi Iwai if (lval < lmin || lval > lmax) {
1119f5e829f9STakashi Iwai if (print_error)
1120fbd3eb7fSTakashi Iwai dev_err(card->dev,
1121fbd3eb7fSTakashi Iwai "control %i:%i:%i:%s:%i: value out of range %lld (%lld/%lld) at count %i\n",
1122fbd3eb7fSTakashi Iwai control->id.iface, control->id.device,
1123fbd3eb7fSTakashi Iwai control->id.subdevice, control->id.name,
1124fbd3eb7fSTakashi Iwai control->id.index, lval, lmin, lmax, i);
1125fbd3eb7fSTakashi Iwai return -EINVAL;
1126fbd3eb7fSTakashi Iwai }
1127fbd3eb7fSTakashi Iwai if (lstep) {
1128fbd3eb7fSTakashi Iwai div64_u64_rem(lval, lstep, &rem);
1129fbd3eb7fSTakashi Iwai if (rem) {
1130f5e829f9STakashi Iwai if (print_error)
1131fbd3eb7fSTakashi Iwai dev_err(card->dev,
1132fbd3eb7fSTakashi Iwai "control %i:%i:%i:%s:%i: unaligned value %lld (step %lld) at count %i\n",
1133fbd3eb7fSTakashi Iwai control->id.iface, control->id.device,
1134fbd3eb7fSTakashi Iwai control->id.subdevice, control->id.name,
1135fbd3eb7fSTakashi Iwai control->id.index, lval, lstep, i);
1136fbd3eb7fSTakashi Iwai return -EINVAL;
1137fbd3eb7fSTakashi Iwai }
1138fbd3eb7fSTakashi Iwai }
1139fbd3eb7fSTakashi Iwai
1140fbd3eb7fSTakashi Iwai return 0;
1141fbd3eb7fSTakashi Iwai }
1142fbd3eb7fSTakashi Iwai
1143f5e829f9STakashi Iwai /* check whether the all input values are valid for the given elem value */
sanity_check_input_values(struct snd_card * card,const struct snd_ctl_elem_value * control,const struct snd_ctl_elem_info * info,bool print_error)1144f5e829f9STakashi Iwai static int sanity_check_input_values(struct snd_card *card,
1145fbd3eb7fSTakashi Iwai const struct snd_ctl_elem_value *control,
1146fbd3eb7fSTakashi Iwai const struct snd_ctl_elem_info *info,
1147f5e829f9STakashi Iwai bool print_error)
1148fbd3eb7fSTakashi Iwai {
1149f5e829f9STakashi Iwai int i, ret;
1150fbd3eb7fSTakashi Iwai
1151fbd3eb7fSTakashi Iwai switch (info->type) {
1152fbd3eb7fSTakashi Iwai case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
1153fbd3eb7fSTakashi Iwai case SNDRV_CTL_ELEM_TYPE_INTEGER:
1154fbd3eb7fSTakashi Iwai case SNDRV_CTL_ELEM_TYPE_INTEGER64:
1155fbd3eb7fSTakashi Iwai case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
1156fbd3eb7fSTakashi Iwai for (i = 0; i < info->count; i++) {
1157f5e829f9STakashi Iwai ret = sanity_check_int_value(card, control, info, i,
1158f5e829f9STakashi Iwai print_error);
1159fbd3eb7fSTakashi Iwai if (ret < 0)
1160fbd3eb7fSTakashi Iwai return ret;
1161fbd3eb7fSTakashi Iwai }
1162fbd3eb7fSTakashi Iwai break;
1163fbd3eb7fSTakashi Iwai default:
1164fbd3eb7fSTakashi Iwai break;
1165fbd3eb7fSTakashi Iwai }
1166fbd3eb7fSTakashi Iwai
1167f5e829f9STakashi Iwai return 0;
1168f5e829f9STakashi Iwai }
1169f5e829f9STakashi Iwai
1170f5e829f9STakashi Iwai /* perform sanity checks to the given snd_ctl_elem_value object */
sanity_check_elem_value(struct snd_card * card,const struct snd_ctl_elem_value * control,const struct snd_ctl_elem_info * info,u32 pattern)1171f5e829f9STakashi Iwai static int sanity_check_elem_value(struct snd_card *card,
1172f5e829f9STakashi Iwai const struct snd_ctl_elem_value *control,
1173f5e829f9STakashi Iwai const struct snd_ctl_elem_info *info,
1174f5e829f9STakashi Iwai u32 pattern)
1175f5e829f9STakashi Iwai {
1176f5e829f9STakashi Iwai size_t offset;
1177f5e829f9STakashi Iwai int ret;
1178f5e829f9STakashi Iwai u32 *p;
1179f5e829f9STakashi Iwai
1180f5e829f9STakashi Iwai ret = sanity_check_input_values(card, control, info, true);
1181f5e829f9STakashi Iwai if (ret < 0)
1182f5e829f9STakashi Iwai return ret;
1183f5e829f9STakashi Iwai
1184fbd3eb7fSTakashi Iwai /* check whether the remaining area kept untouched */
1185fbd3eb7fSTakashi Iwai offset = value_sizes[info->type] * info->count;
1186afcfbcb3SLars-Peter Clausen offset = DIV_ROUND_UP(offset, sizeof(u32));
1187fbd3eb7fSTakashi Iwai p = (u32 *)control->value.bytes.data + offset;
1188fbd3eb7fSTakashi Iwai for (; offset < sizeof(control->value) / sizeof(u32); offset++, p++) {
1189fbd3eb7fSTakashi Iwai if (*p != pattern) {
1190fbd3eb7fSTakashi Iwai ret = -EINVAL;
1191fbd3eb7fSTakashi Iwai break;
1192fbd3eb7fSTakashi Iwai }
1193fbd3eb7fSTakashi Iwai *p = 0; /* clear the checked area */
1194fbd3eb7fSTakashi Iwai }
1195fbd3eb7fSTakashi Iwai
1196fbd3eb7fSTakashi Iwai return ret;
1197fbd3eb7fSTakashi Iwai }
1198fbd3eb7fSTakashi Iwai
__snd_ctl_elem_info(struct snd_card * card,struct snd_kcontrol * kctl,struct snd_ctl_elem_info * info,struct snd_ctl_file * ctl)1199fbd3eb7fSTakashi Iwai static int __snd_ctl_elem_info(struct snd_card *card,
1200fbd3eb7fSTakashi Iwai struct snd_kcontrol *kctl,
1201fbd3eb7fSTakashi Iwai struct snd_ctl_elem_info *info,
1202fbd3eb7fSTakashi Iwai struct snd_ctl_file *ctl)
1203fbd3eb7fSTakashi Iwai {
120482e9bae6STakashi Iwai struct snd_kcontrol_volatile *vd;
12051da177e4SLinus Torvalds unsigned int index_offset;
12061da177e4SLinus Torvalds int result;
12071da177e4SLinus Torvalds
12081da177e4SLinus Torvalds #ifdef CONFIG_SND_DEBUG
12091da177e4SLinus Torvalds info->access = 0;
12101da177e4SLinus Torvalds #endif
1211e94fdbd7STakashi Iwai result = snd_power_ref_and_wait(card);
1212e94fdbd7STakashi Iwai if (!result)
12131da177e4SLinus Torvalds result = kctl->info(kctl, info);
1214e94fdbd7STakashi Iwai snd_power_unref(card);
12151da177e4SLinus Torvalds if (result >= 0) {
12167eaa943cSTakashi Iwai snd_BUG_ON(info->access);
12171da177e4SLinus Torvalds index_offset = snd_ctl_get_ioff(kctl, &info->id);
12181da177e4SLinus Torvalds vd = &kctl->vd[index_offset];
12191da177e4SLinus Torvalds snd_ctl_build_ioff(&info->id, kctl, index_offset);
12201da177e4SLinus Torvalds info->access = vd->access;
12211da177e4SLinus Torvalds if (vd->owner) {
12221da177e4SLinus Torvalds info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
12231da177e4SLinus Torvalds if (vd->owner == ctl)
12241da177e4SLinus Torvalds info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
122525d27edeSClemens Ladisch info->owner = pid_vnr(vd->owner->pid);
12261da177e4SLinus Torvalds } else {
12271da177e4SLinus Torvalds info->owner = -1;
12281da177e4SLinus Torvalds }
1229fbd3eb7fSTakashi Iwai if (!snd_ctl_skip_validation(info) &&
1230fbd3eb7fSTakashi Iwai snd_ctl_check_elem_info(card, info) < 0)
1231fbd3eb7fSTakashi Iwai result = -EINVAL;
12321da177e4SLinus Torvalds }
1233fbd3eb7fSTakashi Iwai return result;
1234fbd3eb7fSTakashi Iwai }
1235fbd3eb7fSTakashi Iwai
snd_ctl_elem_info(struct snd_ctl_file * ctl,struct snd_ctl_elem_info * info)1236fbd3eb7fSTakashi Iwai static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
1237fbd3eb7fSTakashi Iwai struct snd_ctl_elem_info *info)
1238fbd3eb7fSTakashi Iwai {
1239fbd3eb7fSTakashi Iwai struct snd_card *card = ctl->card;
1240fbd3eb7fSTakashi Iwai struct snd_kcontrol *kctl;
1241fbd3eb7fSTakashi Iwai int result;
1242fbd3eb7fSTakashi Iwai
1243fbd3eb7fSTakashi Iwai down_read(&card->controls_rwsem);
1244b1e055f6STakashi Iwai kctl = snd_ctl_find_id_locked(card, &info->id);
1245fbd3eb7fSTakashi Iwai if (kctl == NULL)
1246fbd3eb7fSTakashi Iwai result = -ENOENT;
1247fbd3eb7fSTakashi Iwai else
1248fbd3eb7fSTakashi Iwai result = __snd_ctl_elem_info(card, kctl, info, ctl);
12491da177e4SLinus Torvalds up_read(&card->controls_rwsem);
12501da177e4SLinus Torvalds return result;
12511da177e4SLinus Torvalds }
12521da177e4SLinus Torvalds
snd_ctl_elem_info_user(struct snd_ctl_file * ctl,struct snd_ctl_elem_info __user * _info)125382e9bae6STakashi Iwai static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
125482e9bae6STakashi Iwai struct snd_ctl_elem_info __user *_info)
12551da177e4SLinus Torvalds {
125682e9bae6STakashi Iwai struct snd_ctl_elem_info info;
12571da177e4SLinus Torvalds int result;
12581da177e4SLinus Torvalds
12591da177e4SLinus Torvalds if (copy_from_user(&info, _info, sizeof(info)))
12601da177e4SLinus Torvalds return -EFAULT;
12611da177e4SLinus Torvalds result = snd_ctl_elem_info(ctl, &info);
12627d8e8292STakashi Iwai if (result < 0)
12637d8e8292STakashi Iwai return result;
1264fbd3eb7fSTakashi Iwai /* drop internal access flags */
126522d8de62SJaroslav Kysela info.access &= ~(SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK|
126622d8de62SJaroslav Kysela SNDRV_CTL_ELEM_ACCESS_LED_MASK);
12671da177e4SLinus Torvalds if (copy_to_user(_info, &info, sizeof(info)))
12681da177e4SLinus Torvalds return -EFAULT;
12691da177e4SLinus Torvalds return result;
12701da177e4SLinus Torvalds }
12711da177e4SLinus Torvalds
snd_ctl_elem_read(struct snd_card * card,struct snd_ctl_elem_value * control)1272d3bd67cdSTakashi Iwai static int snd_ctl_elem_read(struct snd_card *card,
1273d3bd67cdSTakashi Iwai struct snd_ctl_elem_value *control)
12741da177e4SLinus Torvalds {
127582e9bae6STakashi Iwai struct snd_kcontrol *kctl;
127682e9bae6STakashi Iwai struct snd_kcontrol_volatile *vd;
12771da177e4SLinus Torvalds unsigned int index_offset;
1278fbd3eb7fSTakashi Iwai struct snd_ctl_elem_info info;
1279fbd3eb7fSTakashi Iwai const u32 pattern = 0xdeadbeef;
1280fbd3eb7fSTakashi Iwai int ret;
12811da177e4SLinus Torvalds
128256b88b50SClement Lecigne down_read(&card->controls_rwsem);
1283b1e055f6STakashi Iwai kctl = snd_ctl_find_id_locked(card, &control->id);
128456b88b50SClement Lecigne if (kctl == NULL) {
128556b88b50SClement Lecigne ret = -ENOENT;
128656b88b50SClement Lecigne goto unlock;
128756b88b50SClement Lecigne }
1288becf9e5dSTakashi Sakamoto
12891da177e4SLinus Torvalds index_offset = snd_ctl_get_ioff(kctl, &control->id);
12901da177e4SLinus Torvalds vd = &kctl->vd[index_offset];
129156b88b50SClement Lecigne if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_READ) || kctl->get == NULL) {
129256b88b50SClement Lecigne ret = -EPERM;
129356b88b50SClement Lecigne goto unlock;
129456b88b50SClement Lecigne }
1295becf9e5dSTakashi Sakamoto
12961da177e4SLinus Torvalds snd_ctl_build_ioff(&control->id, kctl, index_offset);
1297fbd3eb7fSTakashi Iwai
12981b7ec514STakashi Iwai #ifdef CONFIG_SND_CTL_DEBUG
1299fbd3eb7fSTakashi Iwai /* info is needed only for validation */
1300fbd3eb7fSTakashi Iwai memset(&info, 0, sizeof(info));
1301fbd3eb7fSTakashi Iwai info.id = control->id;
1302fbd3eb7fSTakashi Iwai ret = __snd_ctl_elem_info(card, kctl, &info, NULL);
1303fbd3eb7fSTakashi Iwai if (ret < 0)
130456b88b50SClement Lecigne goto unlock;
1305fbd3eb7fSTakashi Iwai #endif
1306fbd3eb7fSTakashi Iwai
1307fbd3eb7fSTakashi Iwai if (!snd_ctl_skip_validation(&info))
1308fbd3eb7fSTakashi Iwai fill_remaining_elem_value(control, &info, pattern);
1309e94fdbd7STakashi Iwai ret = snd_power_ref_and_wait(card);
1310e94fdbd7STakashi Iwai if (!ret)
1311fbd3eb7fSTakashi Iwai ret = kctl->get(kctl, control);
1312e94fdbd7STakashi Iwai snd_power_unref(card);
1313fbd3eb7fSTakashi Iwai if (ret < 0)
131456b88b50SClement Lecigne goto unlock;
1315fbd3eb7fSTakashi Iwai if (!snd_ctl_skip_validation(&info) &&
1316fbd3eb7fSTakashi Iwai sanity_check_elem_value(card, control, &info, pattern) < 0) {
1317fbd3eb7fSTakashi Iwai dev_err(card->dev,
1318fbd3eb7fSTakashi Iwai "control %i:%i:%i:%s:%i: access overflow\n",
1319fbd3eb7fSTakashi Iwai control->id.iface, control->id.device,
1320fbd3eb7fSTakashi Iwai control->id.subdevice, control->id.name,
1321fbd3eb7fSTakashi Iwai control->id.index);
132256b88b50SClement Lecigne ret = -EINVAL;
132356b88b50SClement Lecigne goto unlock;
1324fbd3eb7fSTakashi Iwai }
132556b88b50SClement Lecigne unlock:
132656b88b50SClement Lecigne up_read(&card->controls_rwsem);
1327fbd3eb7fSTakashi Iwai return ret;
13281da177e4SLinus Torvalds }
13291da177e4SLinus Torvalds
snd_ctl_elem_read_user(struct snd_card * card,struct snd_ctl_elem_value __user * _control)133082e9bae6STakashi Iwai static int snd_ctl_elem_read_user(struct snd_card *card,
133182e9bae6STakashi Iwai struct snd_ctl_elem_value __user *_control)
13321da177e4SLinus Torvalds {
133382e9bae6STakashi Iwai struct snd_ctl_elem_value *control;
13341da177e4SLinus Torvalds int result;
13351da177e4SLinus Torvalds
1336ef44a1ecSLi Zefan control = memdup_user(_control, sizeof(*control));
1337ef44a1ecSLi Zefan if (IS_ERR(control))
1338ef44a1ecSLi Zefan return PTR_ERR(control);
1339ef44a1ecSLi Zefan
13401da177e4SLinus Torvalds result = snd_ctl_elem_read(card, control);
13417d8e8292STakashi Iwai if (result < 0)
13427d8e8292STakashi Iwai goto error;
13437d8e8292STakashi Iwai
13441da177e4SLinus Torvalds if (copy_to_user(_control, control, sizeof(*control)))
13451da177e4SLinus Torvalds result = -EFAULT;
13467d8e8292STakashi Iwai error:
13471da177e4SLinus Torvalds kfree(control);
13481da177e4SLinus Torvalds return result;
13491da177e4SLinus Torvalds }
13501da177e4SLinus Torvalds
snd_ctl_elem_write(struct snd_card * card,struct snd_ctl_file * file,struct snd_ctl_elem_value * control)1351d3bd67cdSTakashi Iwai static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
135282e9bae6STakashi Iwai struct snd_ctl_elem_value *control)
13531da177e4SLinus Torvalds {
135482e9bae6STakashi Iwai struct snd_kcontrol *kctl;
135582e9bae6STakashi Iwai struct snd_kcontrol_volatile *vd;
13561da177e4SLinus Torvalds unsigned int index_offset;
13578ace4f3cSTakashi Iwai int result;
13581da177e4SLinus Torvalds
13591fa4445fSJaroslav Kysela down_write(&card->controls_rwsem);
1360b1e055f6STakashi Iwai kctl = snd_ctl_find_id_locked(card, &control->id);
13611fa4445fSJaroslav Kysela if (kctl == NULL) {
13621fa4445fSJaroslav Kysela up_write(&card->controls_rwsem);
1363becf9e5dSTakashi Sakamoto return -ENOENT;
13641fa4445fSJaroslav Kysela }
1365becf9e5dSTakashi Sakamoto
13661da177e4SLinus Torvalds index_offset = snd_ctl_get_ioff(kctl, &control->id);
13671da177e4SLinus Torvalds vd = &kctl->vd[index_offset];
1368becf9e5dSTakashi Sakamoto if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) || kctl->put == NULL ||
13698ace4f3cSTakashi Iwai (file && vd->owner && vd->owner != file)) {
13701fa4445fSJaroslav Kysela up_write(&card->controls_rwsem);
1371becf9e5dSTakashi Sakamoto return -EPERM;
1372becf9e5dSTakashi Sakamoto }
1373becf9e5dSTakashi Sakamoto
13741da177e4SLinus Torvalds snd_ctl_build_ioff(&control->id, kctl, index_offset);
1375e94fdbd7STakashi Iwai result = snd_power_ref_and_wait(card);
1376f5e829f9STakashi Iwai /* validate input values */
1377f5e829f9STakashi Iwai if (IS_ENABLED(CONFIG_SND_CTL_INPUT_VALIDATION) && !result) {
1378f5e829f9STakashi Iwai struct snd_ctl_elem_info info;
1379f5e829f9STakashi Iwai
1380f5e829f9STakashi Iwai memset(&info, 0, sizeof(info));
1381f5e829f9STakashi Iwai info.id = control->id;
1382f5e829f9STakashi Iwai result = __snd_ctl_elem_info(card, kctl, &info, NULL);
1383f5e829f9STakashi Iwai if (!result)
1384f5e829f9STakashi Iwai result = sanity_check_input_values(card, control, &info,
1385f5e829f9STakashi Iwai false);
1386f5e829f9STakashi Iwai }
1387e94fdbd7STakashi Iwai if (!result)
13881da177e4SLinus Torvalds result = kctl->put(kctl, control);
1389e94fdbd7STakashi Iwai snd_power_unref(card);
13901fa4445fSJaroslav Kysela if (result < 0) {
13911fa4445fSJaroslav Kysela up_write(&card->controls_rwsem);
1392becf9e5dSTakashi Sakamoto return result;
13931fa4445fSJaroslav Kysela }
1394becf9e5dSTakashi Sakamoto
13951da177e4SLinus Torvalds if (result > 0) {
13961fa4445fSJaroslav Kysela downgrade_write(&card->controls_rwsem);
13971fa4445fSJaroslav Kysela snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_VALUE, kctl, index_offset);
13981fa4445fSJaroslav Kysela up_read(&card->controls_rwsem);
13991fa4445fSJaroslav Kysela } else {
14001fa4445fSJaroslav Kysela up_write(&card->controls_rwsem);
14011da177e4SLinus Torvalds }
1402becf9e5dSTakashi Sakamoto
1403becf9e5dSTakashi Sakamoto return 0;
14041da177e4SLinus Torvalds }
14051da177e4SLinus Torvalds
snd_ctl_elem_write_user(struct snd_ctl_file * file,struct snd_ctl_elem_value __user * _control)140682e9bae6STakashi Iwai static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
140782e9bae6STakashi Iwai struct snd_ctl_elem_value __user *_control)
14081da177e4SLinus Torvalds {
140982e9bae6STakashi Iwai struct snd_ctl_elem_value *control;
141064649400SGiuliano Pochini struct snd_card *card;
14111da177e4SLinus Torvalds int result;
14121da177e4SLinus Torvalds
1413ef44a1ecSLi Zefan control = memdup_user(_control, sizeof(*control));
1414ef44a1ecSLi Zefan if (IS_ERR(control))
1415ef44a1ecSLi Zefan return PTR_ERR(control);
1416ef44a1ecSLi Zefan
141764649400SGiuliano Pochini card = file->card;
141864649400SGiuliano Pochini result = snd_ctl_elem_write(card, file, control);
14197d8e8292STakashi Iwai if (result < 0)
14207d8e8292STakashi Iwai goto error;
14217d8e8292STakashi Iwai
14221da177e4SLinus Torvalds if (copy_to_user(_control, control, sizeof(*control)))
14231da177e4SLinus Torvalds result = -EFAULT;
14247d8e8292STakashi Iwai error:
14251da177e4SLinus Torvalds kfree(control);
14261da177e4SLinus Torvalds return result;
14271da177e4SLinus Torvalds }
14281da177e4SLinus Torvalds
snd_ctl_elem_lock(struct snd_ctl_file * file,struct snd_ctl_elem_id __user * _id)142982e9bae6STakashi Iwai static int snd_ctl_elem_lock(struct snd_ctl_file *file,
143082e9bae6STakashi Iwai struct snd_ctl_elem_id __user *_id)
14311da177e4SLinus Torvalds {
143282e9bae6STakashi Iwai struct snd_card *card = file->card;
143382e9bae6STakashi Iwai struct snd_ctl_elem_id id;
143482e9bae6STakashi Iwai struct snd_kcontrol *kctl;
143582e9bae6STakashi Iwai struct snd_kcontrol_volatile *vd;
14361da177e4SLinus Torvalds int result;
14371da177e4SLinus Torvalds
14381da177e4SLinus Torvalds if (copy_from_user(&id, _id, sizeof(id)))
14391da177e4SLinus Torvalds return -EFAULT;
14401da177e4SLinus Torvalds down_write(&card->controls_rwsem);
1441b1e055f6STakashi Iwai kctl = snd_ctl_find_id_locked(card, &id);
14421da177e4SLinus Torvalds if (kctl == NULL) {
14431da177e4SLinus Torvalds result = -ENOENT;
14441da177e4SLinus Torvalds } else {
14451da177e4SLinus Torvalds vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
14461da177e4SLinus Torvalds if (vd->owner != NULL)
14471da177e4SLinus Torvalds result = -EBUSY;
14481da177e4SLinus Torvalds else {
14491da177e4SLinus Torvalds vd->owner = file;
14501da177e4SLinus Torvalds result = 0;
14511da177e4SLinus Torvalds }
14521da177e4SLinus Torvalds }
14531da177e4SLinus Torvalds up_write(&card->controls_rwsem);
14541da177e4SLinus Torvalds return result;
14551da177e4SLinus Torvalds }
14561da177e4SLinus Torvalds
snd_ctl_elem_unlock(struct snd_ctl_file * file,struct snd_ctl_elem_id __user * _id)145782e9bae6STakashi Iwai static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
145882e9bae6STakashi Iwai struct snd_ctl_elem_id __user *_id)
14591da177e4SLinus Torvalds {
146082e9bae6STakashi Iwai struct snd_card *card = file->card;
146182e9bae6STakashi Iwai struct snd_ctl_elem_id id;
146282e9bae6STakashi Iwai struct snd_kcontrol *kctl;
146382e9bae6STakashi Iwai struct snd_kcontrol_volatile *vd;
14641da177e4SLinus Torvalds int result;
14651da177e4SLinus Torvalds
14661da177e4SLinus Torvalds if (copy_from_user(&id, _id, sizeof(id)))
14671da177e4SLinus Torvalds return -EFAULT;
14681da177e4SLinus Torvalds down_write(&card->controls_rwsem);
1469b1e055f6STakashi Iwai kctl = snd_ctl_find_id_locked(card, &id);
14701da177e4SLinus Torvalds if (kctl == NULL) {
14711da177e4SLinus Torvalds result = -ENOENT;
14721da177e4SLinus Torvalds } else {
14731da177e4SLinus Torvalds vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
14741da177e4SLinus Torvalds if (vd->owner == NULL)
14751da177e4SLinus Torvalds result = -EINVAL;
14761da177e4SLinus Torvalds else if (vd->owner != file)
14771da177e4SLinus Torvalds result = -EPERM;
14781da177e4SLinus Torvalds else {
14791da177e4SLinus Torvalds vd->owner = NULL;
14801da177e4SLinus Torvalds result = 0;
14811da177e4SLinus Torvalds }
14821da177e4SLinus Torvalds }
14831da177e4SLinus Torvalds up_write(&card->controls_rwsem);
14841da177e4SLinus Torvalds return result;
14851da177e4SLinus Torvalds }
14861da177e4SLinus Torvalds
14871da177e4SLinus Torvalds struct user_element {
148882e9bae6STakashi Iwai struct snd_ctl_elem_info info;
148907f4d9d7SLars-Peter Clausen struct snd_card *card;
1490e1c78df1STakashi Sakamoto char *elem_data; /* element data */
14911da177e4SLinus Torvalds unsigned long elem_data_size; /* size of element data in bytes */
14928aa9b586SJaroslav Kysela void *tlv_data; /* TLV data */
14938aa9b586SJaroslav Kysela unsigned long tlv_data_size; /* TLV data size */
14941da177e4SLinus Torvalds void *priv_data; /* private data (like strings for enumerated type) */
14951da177e4SLinus Torvalds };
14961da177e4SLinus Torvalds
149766c6d1efSTakashi Sakamoto // check whether the addition (in bytes) of user ctl element may overflow the limit.
check_user_elem_overflow(struct snd_card * card,ssize_t add)149866c6d1efSTakashi Sakamoto static bool check_user_elem_overflow(struct snd_card *card, ssize_t add)
149966c6d1efSTakashi Sakamoto {
150066c6d1efSTakashi Sakamoto return (ssize_t)card->user_ctl_alloc_size + add > max_user_ctl_alloc_size;
150166c6d1efSTakashi Sakamoto }
150266c6d1efSTakashi Sakamoto
snd_ctl_elem_user_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)150382e9bae6STakashi Iwai static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
150482e9bae6STakashi Iwai struct snd_ctl_elem_info *uinfo)
15051da177e4SLinus Torvalds {
15061da177e4SLinus Torvalds struct user_element *ue = kcontrol->private_data;
1507c378c3b0STakashi Sakamoto unsigned int offset;
15081da177e4SLinus Torvalds
1509c378c3b0STakashi Sakamoto offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
15101da177e4SLinus Torvalds *uinfo = ue->info;
1511c378c3b0STakashi Sakamoto snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
1512c378c3b0STakashi Sakamoto
15131da177e4SLinus Torvalds return 0;
15141da177e4SLinus Torvalds }
15151da177e4SLinus Torvalds
snd_ctl_elem_user_enum_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)15168d448162SClemens Ladisch static int snd_ctl_elem_user_enum_info(struct snd_kcontrol *kcontrol,
15178d448162SClemens Ladisch struct snd_ctl_elem_info *uinfo)
15188d448162SClemens Ladisch {
15198d448162SClemens Ladisch struct user_element *ue = kcontrol->private_data;
15208d448162SClemens Ladisch const char *names;
15218d448162SClemens Ladisch unsigned int item;
1522c378c3b0STakashi Sakamoto unsigned int offset;
15238d448162SClemens Ladisch
15248d448162SClemens Ladisch item = uinfo->value.enumerated.item;
15258d448162SClemens Ladisch
1526c378c3b0STakashi Sakamoto offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
15278d448162SClemens Ladisch *uinfo = ue->info;
1528c378c3b0STakashi Sakamoto snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
15298d448162SClemens Ladisch
15308d448162SClemens Ladisch item = min(item, uinfo->value.enumerated.items - 1);
15318d448162SClemens Ladisch uinfo->value.enumerated.item = item;
15328d448162SClemens Ladisch
15338d448162SClemens Ladisch names = ue->priv_data;
15348d448162SClemens Ladisch for (; item > 0; --item)
15358d448162SClemens Ladisch names += strlen(names) + 1;
15368d448162SClemens Ladisch strcpy(uinfo->value.enumerated.name, names);
15378d448162SClemens Ladisch
15388d448162SClemens Ladisch return 0;
15398d448162SClemens Ladisch }
15408d448162SClemens Ladisch
snd_ctl_elem_user_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)154182e9bae6STakashi Iwai static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
154282e9bae6STakashi Iwai struct snd_ctl_elem_value *ucontrol)
15431da177e4SLinus Torvalds {
15441da177e4SLinus Torvalds struct user_element *ue = kcontrol->private_data;
1545e1c78df1STakashi Sakamoto unsigned int size = ue->elem_data_size;
1546e1c78df1STakashi Sakamoto char *src = ue->elem_data +
1547e1c78df1STakashi Sakamoto snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
15481da177e4SLinus Torvalds
1549e1c78df1STakashi Sakamoto memcpy(&ucontrol->value, src, size);
15501da177e4SLinus Torvalds return 0;
15511da177e4SLinus Torvalds }
15521da177e4SLinus Torvalds
snd_ctl_elem_user_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)155382e9bae6STakashi Iwai static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
155482e9bae6STakashi Iwai struct snd_ctl_elem_value *ucontrol)
15551da177e4SLinus Torvalds {
1556*4a67c7c0STakashi Iwai int err, change;
15571da177e4SLinus Torvalds struct user_element *ue = kcontrol->private_data;
1558e1c78df1STakashi Sakamoto unsigned int size = ue->elem_data_size;
1559e1c78df1STakashi Sakamoto char *dst = ue->elem_data +
1560e1c78df1STakashi Sakamoto snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
15611da177e4SLinus Torvalds
1562*4a67c7c0STakashi Iwai err = sanity_check_input_values(ue->card, ucontrol, &ue->info, false);
1563*4a67c7c0STakashi Iwai if (err < 0)
1564*4a67c7c0STakashi Iwai return err;
1565*4a67c7c0STakashi Iwai
1566e1c78df1STakashi Sakamoto change = memcmp(&ucontrol->value, dst, size) != 0;
15671da177e4SLinus Torvalds if (change)
1568e1c78df1STakashi Sakamoto memcpy(dst, &ucontrol->value, size);
15691da177e4SLinus Torvalds return change;
15701da177e4SLinus Torvalds }
15711da177e4SLinus Torvalds
1572998f26f4STakashi Iwai /* called in controls_rwsem write lock */
replace_user_tlv(struct snd_kcontrol * kctl,unsigned int __user * buf,unsigned int size)15736d4d41f0STakashi Sakamoto static int replace_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
15746d4d41f0STakashi Sakamoto unsigned int size)
15758aa9b586SJaroslav Kysela {
15766d4d41f0STakashi Sakamoto struct user_element *ue = kctl->private_data;
15776d4d41f0STakashi Sakamoto unsigned int *container;
1578b8e2204bSTakashi Sakamoto unsigned int mask = 0;
1579da428828STakashi Sakamoto int i;
158030d8340bSTakashi Sakamoto int change;
158130d8340bSTakashi Sakamoto
15828320ba0cSTakashi Iwai lockdep_assert_held_write(&ue->card->controls_rwsem);
15838320ba0cSTakashi Iwai
15848aa9b586SJaroslav Kysela if (size > 1024 * 128) /* sane value */
15858aa9b586SJaroslav Kysela return -EINVAL;
1586ef44a1ecSLi Zefan
158766c6d1efSTakashi Sakamoto // does the TLV size change cause overflow?
158866c6d1efSTakashi Sakamoto if (check_user_elem_overflow(ue->card, (ssize_t)(size - ue->tlv_data_size)))
158966c6d1efSTakashi Sakamoto return -ENOMEM;
159066c6d1efSTakashi Sakamoto
159188a89037SAl Viro container = vmemdup_user(buf, size);
15926d4d41f0STakashi Sakamoto if (IS_ERR(container))
15936d4d41f0STakashi Sakamoto return PTR_ERR(container);
15946d4d41f0STakashi Sakamoto
15958aa9b586SJaroslav Kysela change = ue->tlv_data_size != size;
15968aa9b586SJaroslav Kysela if (!change)
1597241bc82eSTakashi Iwai change = memcmp(ue->tlv_data, container, size) != 0;
15986d4d41f0STakashi Sakamoto if (!change) {
159988a89037SAl Viro kvfree(container);
16006d4d41f0STakashi Sakamoto return 0;
16016d4d41f0STakashi Sakamoto }
16026d4d41f0STakashi Sakamoto
1603b8e2204bSTakashi Sakamoto if (ue->tlv_data == NULL) {
1604b8e2204bSTakashi Sakamoto /* Now TLV data is available. */
1605b8e2204bSTakashi Sakamoto for (i = 0; i < kctl->count; ++i)
1606b8e2204bSTakashi Sakamoto kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1607b8e2204bSTakashi Sakamoto mask = SNDRV_CTL_EVENT_MASK_INFO;
160866c6d1efSTakashi Sakamoto } else {
160966c6d1efSTakashi Sakamoto ue->card->user_ctl_alloc_size -= ue->tlv_data_size;
161066c6d1efSTakashi Sakamoto ue->tlv_data_size = 0;
161166c6d1efSTakashi Sakamoto kvfree(ue->tlv_data);
1612b8e2204bSTakashi Sakamoto }
1613b8e2204bSTakashi Sakamoto
16146d4d41f0STakashi Sakamoto ue->tlv_data = container;
16158aa9b586SJaroslav Kysela ue->tlv_data_size = size;
161666c6d1efSTakashi Sakamoto // decremented at private_free.
161766c6d1efSTakashi Sakamoto ue->card->user_ctl_alloc_size += size;
161807f4d9d7SLars-Peter Clausen
1619b8e2204bSTakashi Sakamoto mask |= SNDRV_CTL_EVENT_MASK_TLV;
16201fa4445fSJaroslav Kysela for (i = 0; i < kctl->count; ++i)
16211fa4445fSJaroslav Kysela snd_ctl_notify_one(ue->card, mask, kctl, i);
1622fb8027ebSTakashi Sakamoto
16238aa9b586SJaroslav Kysela return change;
16246d4d41f0STakashi Sakamoto }
16256d4d41f0STakashi Sakamoto
read_user_tlv(struct snd_kcontrol * kctl,unsigned int __user * buf,unsigned int size)16266d4d41f0STakashi Sakamoto static int read_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
16276d4d41f0STakashi Sakamoto unsigned int size)
16286d4d41f0STakashi Sakamoto {
16296d4d41f0STakashi Sakamoto struct user_element *ue = kctl->private_data;
16306d4d41f0STakashi Sakamoto
16316d4d41f0STakashi Sakamoto if (ue->tlv_data_size == 0 || ue->tlv_data == NULL)
163230d8340bSTakashi Sakamoto return -ENXIO;
163330d8340bSTakashi Sakamoto
163430d8340bSTakashi Sakamoto if (size < ue->tlv_data_size)
163530d8340bSTakashi Sakamoto return -ENOSPC;
163630d8340bSTakashi Sakamoto
16376d4d41f0STakashi Sakamoto if (copy_to_user(buf, ue->tlv_data, ue->tlv_data_size))
163830d8340bSTakashi Sakamoto return -EFAULT;
163930d8340bSTakashi Sakamoto
164030d8340bSTakashi Sakamoto return 0;
164130d8340bSTakashi Sakamoto }
16426d4d41f0STakashi Sakamoto
snd_ctl_elem_user_tlv(struct snd_kcontrol * kctl,int op_flag,unsigned int size,unsigned int __user * buf)16436d4d41f0STakashi Sakamoto static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kctl, int op_flag,
16446d4d41f0STakashi Sakamoto unsigned int size, unsigned int __user *buf)
16456d4d41f0STakashi Sakamoto {
16466d4d41f0STakashi Sakamoto if (op_flag == SNDRV_CTL_TLV_OP_WRITE)
16476d4d41f0STakashi Sakamoto return replace_user_tlv(kctl, buf, size);
16486d4d41f0STakashi Sakamoto else
16496d4d41f0STakashi Sakamoto return read_user_tlv(kctl, buf, size);
16508aa9b586SJaroslav Kysela }
16518aa9b586SJaroslav Kysela
1652998f26f4STakashi Iwai /* called in controls_rwsem write lock */
snd_ctl_elem_init_enum_names(struct user_element * ue)16538d448162SClemens Ladisch static int snd_ctl_elem_init_enum_names(struct user_element *ue)
16548d448162SClemens Ladisch {
16558d448162SClemens Ladisch char *names, *p;
16568d448162SClemens Ladisch size_t buf_len, name_len;
16578d448162SClemens Ladisch unsigned int i;
1658447c6f93SOlof Johansson const uintptr_t user_ptrval = ue->info.value.enumerated.names_ptr;
16598d448162SClemens Ladisch
16608320ba0cSTakashi Iwai lockdep_assert_held_write(&ue->card->controls_rwsem);
16618320ba0cSTakashi Iwai
166266c6d1efSTakashi Sakamoto buf_len = ue->info.value.enumerated.names_length;
166366c6d1efSTakashi Sakamoto if (buf_len > 64 * 1024)
16648d448162SClemens Ladisch return -EINVAL;
16658d448162SClemens Ladisch
166666c6d1efSTakashi Sakamoto if (check_user_elem_overflow(ue->card, buf_len))
166766c6d1efSTakashi Sakamoto return -ENOMEM;
166866c6d1efSTakashi Sakamoto names = vmemdup_user((const void __user *)user_ptrval, buf_len);
16698d448162SClemens Ladisch if (IS_ERR(names))
16708d448162SClemens Ladisch return PTR_ERR(names);
16718d448162SClemens Ladisch
16728d448162SClemens Ladisch /* check that there are enough valid names */
16738d448162SClemens Ladisch p = names;
16748d448162SClemens Ladisch for (i = 0; i < ue->info.value.enumerated.items; ++i) {
16758d448162SClemens Ladisch name_len = strnlen(p, buf_len);
16768d448162SClemens Ladisch if (name_len == 0 || name_len >= 64 || name_len == buf_len) {
167759aeaf3fSAl Viro kvfree(names);
16788d448162SClemens Ladisch return -EINVAL;
16798d448162SClemens Ladisch }
16808d448162SClemens Ladisch p += name_len + 1;
16818d448162SClemens Ladisch buf_len -= name_len + 1;
16828d448162SClemens Ladisch }
16838d448162SClemens Ladisch
16848d448162SClemens Ladisch ue->priv_data = names;
16858d448162SClemens Ladisch ue->info.value.enumerated.names_ptr = 0;
168666c6d1efSTakashi Sakamoto // increment the allocation size; decremented again at private_free.
168766c6d1efSTakashi Sakamoto ue->card->user_ctl_alloc_size += ue->info.value.enumerated.names_length;
16888d448162SClemens Ladisch
16898d448162SClemens Ladisch return 0;
16908d448162SClemens Ladisch }
16918d448162SClemens Ladisch
compute_user_elem_size(size_t size,unsigned int count)169266c6d1efSTakashi Sakamoto static size_t compute_user_elem_size(size_t size, unsigned int count)
169366c6d1efSTakashi Sakamoto {
169466c6d1efSTakashi Sakamoto return sizeof(struct user_element) + size * count;
169566c6d1efSTakashi Sakamoto }
169666c6d1efSTakashi Sakamoto
snd_ctl_elem_user_free(struct snd_kcontrol * kcontrol)169782e9bae6STakashi Iwai static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
16981da177e4SLinus Torvalds {
16998aa9b586SJaroslav Kysela struct user_element *ue = kcontrol->private_data;
17008d448162SClemens Ladisch
170166c6d1efSTakashi Sakamoto // decrement the allocation size.
170266c6d1efSTakashi Sakamoto ue->card->user_ctl_alloc_size -= compute_user_elem_size(ue->elem_data_size, kcontrol->count);
170366c6d1efSTakashi Sakamoto ue->card->user_ctl_alloc_size -= ue->tlv_data_size;
170466c6d1efSTakashi Sakamoto if (ue->priv_data)
170566c6d1efSTakashi Sakamoto ue->card->user_ctl_alloc_size -= ue->info.value.enumerated.names_length;
170666c6d1efSTakashi Sakamoto
170788a89037SAl Viro kvfree(ue->tlv_data);
170859aeaf3fSAl Viro kvfree(ue->priv_data);
17098aa9b586SJaroslav Kysela kfree(ue);
17101da177e4SLinus Torvalds }
17111da177e4SLinus Torvalds
snd_ctl_elem_add(struct snd_ctl_file * file,struct snd_ctl_elem_info * info,int replace)171282e9bae6STakashi Iwai static int snd_ctl_elem_add(struct snd_ctl_file *file,
171382e9bae6STakashi Iwai struct snd_ctl_elem_info *info, int replace)
17141da177e4SLinus Torvalds {
171582e9bae6STakashi Iwai struct snd_card *card = file->card;
17162225e79bSTakashi Sakamoto struct snd_kcontrol *kctl;
17172225e79bSTakashi Sakamoto unsigned int count;
17181da177e4SLinus Torvalds unsigned int access;
17191da177e4SLinus Torvalds long private_size;
172066c6d1efSTakashi Sakamoto size_t alloc_size;
17211da177e4SLinus Torvalds struct user_element *ue;
1722cab2ed74STakashi Sakamoto unsigned int offset;
17232225e79bSTakashi Sakamoto int err;
17241da177e4SLinus Torvalds
1725be3bb823STakashi Iwai if (!*info->id.name)
1726be3bb823STakashi Iwai return -EINVAL;
1727be3bb823STakashi Iwai if (strnlen(info->id.name, sizeof(info->id.name)) >= sizeof(info->id.name))
1728be3bb823STakashi Iwai return -EINVAL;
172982262a46SLars-Peter Clausen
17302225e79bSTakashi Sakamoto /* Delete a control to replace them if needed. */
173182262a46SLars-Peter Clausen if (replace) {
17322225e79bSTakashi Sakamoto info->id.numid = 0;
173382262a46SLars-Peter Clausen err = snd_ctl_remove_user_ctl(file, &info->id);
173482262a46SLars-Peter Clausen if (err)
17351da177e4SLinus Torvalds return err;
173682262a46SLars-Peter Clausen }
173782262a46SLars-Peter Clausen
17382225e79bSTakashi Sakamoto /* Check the number of elements for this userspace control. */
17392225e79bSTakashi Sakamoto count = info->owner;
17402225e79bSTakashi Sakamoto if (count == 0)
17412225e79bSTakashi Sakamoto count = 1;
17424ed56666STakashi Sakamoto
17432225e79bSTakashi Sakamoto /* Arrange access permissions if needed. */
17442225e79bSTakashi Sakamoto access = info->access;
17452225e79bSTakashi Sakamoto if (access == 0)
17462225e79bSTakashi Sakamoto access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
17472225e79bSTakashi Sakamoto access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
17482225e79bSTakashi Sakamoto SNDRV_CTL_ELEM_ACCESS_INACTIVE |
1749b8e2204bSTakashi Sakamoto SNDRV_CTL_ELEM_ACCESS_TLV_WRITE);
1750b8e2204bSTakashi Sakamoto
1751b8e2204bSTakashi Sakamoto /* In initial state, nothing is available as TLV container. */
1752b8e2204bSTakashi Sakamoto if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
17532225e79bSTakashi Sakamoto access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
17542225e79bSTakashi Sakamoto access |= SNDRV_CTL_ELEM_ACCESS_USER;
17552225e79bSTakashi Sakamoto
17562225e79bSTakashi Sakamoto /*
17572225e79bSTakashi Sakamoto * Check information and calculate the size of data specific to
17582225e79bSTakashi Sakamoto * this userspace control.
17592225e79bSTakashi Sakamoto */
1760fbd3eb7fSTakashi Iwai /* pass NULL to card for suppressing error messages */
1761fbd3eb7fSTakashi Iwai err = snd_ctl_check_elem_info(NULL, info);
1762fbd3eb7fSTakashi Iwai if (err < 0)
1763fbd3eb7fSTakashi Iwai return err;
1764fbd3eb7fSTakashi Iwai /* user-space control doesn't allow zero-size data */
1765fbd3eb7fSTakashi Iwai if (info->count < 1)
17668d448162SClemens Ladisch return -EINVAL;
17674ed56666STakashi Sakamoto private_size = value_sizes[info->type] * info->count;
176866c6d1efSTakashi Sakamoto alloc_size = compute_user_elem_size(private_size, count);
176966c6d1efSTakashi Sakamoto
1770998f26f4STakashi Iwai down_write(&card->controls_rwsem);
1771998f26f4STakashi Iwai if (check_user_elem_overflow(card, alloc_size)) {
1772998f26f4STakashi Iwai err = -ENOMEM;
1773998f26f4STakashi Iwai goto unlock;
1774998f26f4STakashi Iwai }
17752225e79bSTakashi Sakamoto
17762225e79bSTakashi Sakamoto /*
17772225e79bSTakashi Sakamoto * Keep memory object for this userspace control. After passing this
17782225e79bSTakashi Sakamoto * code block, the instance should be freed by snd_ctl_free_one().
17792225e79bSTakashi Sakamoto *
17802225e79bSTakashi Sakamoto * Note that these elements in this control are locked.
17812225e79bSTakashi Sakamoto */
17822225e79bSTakashi Sakamoto err = snd_ctl_new(&kctl, count, access, file);
17832225e79bSTakashi Sakamoto if (err < 0)
1784998f26f4STakashi Iwai goto unlock;
1785e79d74abSTakashi Iwai memcpy(&kctl->id, &info->id, sizeof(kctl->id));
178666c6d1efSTakashi Sakamoto ue = kzalloc(alloc_size, GFP_KERNEL);
178766c6d1efSTakashi Sakamoto if (!ue) {
17882225e79bSTakashi Sakamoto kfree(kctl);
1789998f26f4STakashi Iwai err = -ENOMEM;
1790998f26f4STakashi Iwai goto unlock;
17912225e79bSTakashi Sakamoto }
179266c6d1efSTakashi Sakamoto kctl->private_data = ue;
17932225e79bSTakashi Sakamoto kctl->private_free = snd_ctl_elem_user_free;
17942225e79bSTakashi Sakamoto
179566c6d1efSTakashi Sakamoto // increment the allocated size; decremented again at private_free.
179666c6d1efSTakashi Sakamoto card->user_ctl_alloc_size += alloc_size;
179766c6d1efSTakashi Sakamoto
17982225e79bSTakashi Sakamoto /* Set private data for this userspace control. */
179907f4d9d7SLars-Peter Clausen ue->card = card;
18001da177e4SLinus Torvalds ue->info = *info;
180186148e84STakashi Iwai ue->info.access = 0;
18021da177e4SLinus Torvalds ue->elem_data = (char *)ue + sizeof(*ue);
18031da177e4SLinus Torvalds ue->elem_data_size = private_size;
18048d448162SClemens Ladisch if (ue->info.type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
18058d448162SClemens Ladisch err = snd_ctl_elem_init_enum_names(ue);
18068d448162SClemens Ladisch if (err < 0) {
18072225e79bSTakashi Sakamoto snd_ctl_free_one(kctl);
1808998f26f4STakashi Iwai goto unlock;
18098d448162SClemens Ladisch }
18108d448162SClemens Ladisch }
18112225e79bSTakashi Sakamoto
18122225e79bSTakashi Sakamoto /* Set callback functions. */
18132225e79bSTakashi Sakamoto if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED)
18142225e79bSTakashi Sakamoto kctl->info = snd_ctl_elem_user_enum_info;
18152225e79bSTakashi Sakamoto else
18162225e79bSTakashi Sakamoto kctl->info = snd_ctl_elem_user_info;
18172225e79bSTakashi Sakamoto if (access & SNDRV_CTL_ELEM_ACCESS_READ)
18182225e79bSTakashi Sakamoto kctl->get = snd_ctl_elem_user_get;
18192225e79bSTakashi Sakamoto if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
18202225e79bSTakashi Sakamoto kctl->put = snd_ctl_elem_user_put;
1821b8e2204bSTakashi Sakamoto if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
18222225e79bSTakashi Sakamoto kctl->tlv.c = snd_ctl_elem_user_tlv;
18232225e79bSTakashi Sakamoto
18242225e79bSTakashi Sakamoto /* This function manage to free the instance on failure. */
18253103c08fSTakashi Iwai err = __snd_ctl_add_replace(card, kctl, CTL_ADD_EXCLUSIVE);
1826e1a7bfe3STakashi Iwai if (err < 0) {
1827e1a7bfe3STakashi Iwai snd_ctl_free_one(kctl);
1828e1a7bfe3STakashi Iwai goto unlock;
1829e1a7bfe3STakashi Iwai }
1830cab2ed74STakashi Sakamoto offset = snd_ctl_get_ioff(kctl, &info->id);
1831cab2ed74STakashi Sakamoto snd_ctl_build_ioff(&info->id, kctl, offset);
1832cab2ed74STakashi Sakamoto /*
1833cab2ed74STakashi Sakamoto * Here we cannot fill any field for the number of elements added by
1834cab2ed74STakashi Sakamoto * this operation because there're no specific fields. The usage of
1835cab2ed74STakashi Sakamoto * 'owner' field for this purpose may cause any bugs to userspace
1836cab2ed74STakashi Sakamoto * applications because the field originally means PID of a process
1837cab2ed74STakashi Sakamoto * which locks the element.
1838cab2ed74STakashi Sakamoto */
1839e1a7bfe3STakashi Iwai unlock:
1840e1a7bfe3STakashi Iwai up_write(&card->controls_rwsem);
184195a793c3STakashi Sakamoto return err;
18421da177e4SLinus Torvalds }
18431da177e4SLinus Torvalds
snd_ctl_elem_add_user(struct snd_ctl_file * file,struct snd_ctl_elem_info __user * _info,int replace)184482e9bae6STakashi Iwai static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
184582e9bae6STakashi Iwai struct snd_ctl_elem_info __user *_info, int replace)
18461da177e4SLinus Torvalds {
184782e9bae6STakashi Iwai struct snd_ctl_elem_info info;
1848cab2ed74STakashi Sakamoto int err;
1849cab2ed74STakashi Sakamoto
18501da177e4SLinus Torvalds if (copy_from_user(&info, _info, sizeof(info)))
18511da177e4SLinus Torvalds return -EFAULT;
1852cab2ed74STakashi Sakamoto err = snd_ctl_elem_add(file, &info, replace);
1853cab2ed74STakashi Sakamoto if (err < 0)
1854cab2ed74STakashi Sakamoto return err;
1855cab2ed74STakashi Sakamoto if (copy_to_user(_info, &info, sizeof(info))) {
1856cab2ed74STakashi Sakamoto snd_ctl_remove_user_ctl(file, &info.id);
1857cab2ed74STakashi Sakamoto return -EFAULT;
1858cab2ed74STakashi Sakamoto }
1859cab2ed74STakashi Sakamoto
1860cab2ed74STakashi Sakamoto return 0;
18611da177e4SLinus Torvalds }
18621da177e4SLinus Torvalds
snd_ctl_elem_remove(struct snd_ctl_file * file,struct snd_ctl_elem_id __user * _id)186382e9bae6STakashi Iwai static int snd_ctl_elem_remove(struct snd_ctl_file *file,
186482e9bae6STakashi Iwai struct snd_ctl_elem_id __user *_id)
18651da177e4SLinus Torvalds {
186682e9bae6STakashi Iwai struct snd_ctl_elem_id id;
18671da177e4SLinus Torvalds
18681da177e4SLinus Torvalds if (copy_from_user(&id, _id, sizeof(id)))
18691da177e4SLinus Torvalds return -EFAULT;
1870f217ac59SClemens Ladisch return snd_ctl_remove_user_ctl(file, &id);
18711da177e4SLinus Torvalds }
18721da177e4SLinus Torvalds
snd_ctl_subscribe_events(struct snd_ctl_file * file,int __user * ptr)187382e9bae6STakashi Iwai static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
18741da177e4SLinus Torvalds {
18751da177e4SLinus Torvalds int subscribe;
18761da177e4SLinus Torvalds if (get_user(subscribe, ptr))
18771da177e4SLinus Torvalds return -EFAULT;
18781da177e4SLinus Torvalds if (subscribe < 0) {
18791da177e4SLinus Torvalds subscribe = file->subscribed;
18801da177e4SLinus Torvalds if (put_user(subscribe, ptr))
18811da177e4SLinus Torvalds return -EFAULT;
18821da177e4SLinus Torvalds return 0;
18831da177e4SLinus Torvalds }
18841da177e4SLinus Torvalds if (subscribe) {
18851da177e4SLinus Torvalds file->subscribed = 1;
18861da177e4SLinus Torvalds return 0;
18871da177e4SLinus Torvalds } else if (file->subscribed) {
18881da177e4SLinus Torvalds snd_ctl_empty_read_queue(file);
18891da177e4SLinus Torvalds file->subscribed = 0;
18901da177e4SLinus Torvalds }
18911da177e4SLinus Torvalds return 0;
18921da177e4SLinus Torvalds }
18931da177e4SLinus Torvalds
call_tlv_handler(struct snd_ctl_file * file,int op_flag,struct snd_kcontrol * kctl,struct snd_ctl_elem_id * id,unsigned int __user * buf,unsigned int size)1894450296f3STakashi Sakamoto static int call_tlv_handler(struct snd_ctl_file *file, int op_flag,
1895450296f3STakashi Sakamoto struct snd_kcontrol *kctl,
1896450296f3STakashi Sakamoto struct snd_ctl_elem_id *id,
1897450296f3STakashi Sakamoto unsigned int __user *buf, unsigned int size)
189842750b04SJaroslav Kysela {
1899450296f3STakashi Sakamoto static const struct {
1900450296f3STakashi Sakamoto int op;
1901450296f3STakashi Sakamoto int perm;
1902450296f3STakashi Sakamoto } pairs[] = {
1903450296f3STakashi Sakamoto {SNDRV_CTL_TLV_OP_READ, SNDRV_CTL_ELEM_ACCESS_TLV_READ},
1904450296f3STakashi Sakamoto {SNDRV_CTL_TLV_OP_WRITE, SNDRV_CTL_ELEM_ACCESS_TLV_WRITE},
1905450296f3STakashi Sakamoto {SNDRV_CTL_TLV_OP_CMD, SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND},
1906450296f3STakashi Sakamoto };
1907450296f3STakashi Sakamoto struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
1908e94fdbd7STakashi Iwai int i, ret;
1909450296f3STakashi Sakamoto
1910450296f3STakashi Sakamoto /* Check support of the request for this element. */
1911450296f3STakashi Sakamoto for (i = 0; i < ARRAY_SIZE(pairs); ++i) {
1912450296f3STakashi Sakamoto if (op_flag == pairs[i].op && (vd->access & pairs[i].perm))
1913450296f3STakashi Sakamoto break;
1914450296f3STakashi Sakamoto }
1915450296f3STakashi Sakamoto if (i == ARRAY_SIZE(pairs))
1916450296f3STakashi Sakamoto return -ENXIO;
1917450296f3STakashi Sakamoto
1918450296f3STakashi Sakamoto if (kctl->tlv.c == NULL)
1919450296f3STakashi Sakamoto return -ENXIO;
1920450296f3STakashi Sakamoto
1921d61fe22cSTakashi Sakamoto /* Write and command operations are not allowed for locked element. */
1922d61fe22cSTakashi Sakamoto if (op_flag != SNDRV_CTL_TLV_OP_READ &&
1923d61fe22cSTakashi Sakamoto vd->owner != NULL && vd->owner != file)
1924450296f3STakashi Sakamoto return -EPERM;
1925450296f3STakashi Sakamoto
1926e94fdbd7STakashi Iwai ret = snd_power_ref_and_wait(file->card);
1927e94fdbd7STakashi Iwai if (!ret)
1928e94fdbd7STakashi Iwai ret = kctl->tlv.c(kctl, op_flag, size, buf);
1929e94fdbd7STakashi Iwai snd_power_unref(file->card);
1930e94fdbd7STakashi Iwai return ret;
1931450296f3STakashi Sakamoto }
1932450296f3STakashi Sakamoto
read_tlv_buf(struct snd_kcontrol * kctl,struct snd_ctl_elem_id * id,unsigned int __user * buf,unsigned int size)1933450296f3STakashi Sakamoto static int read_tlv_buf(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id,
1934450296f3STakashi Sakamoto unsigned int __user *buf, unsigned int size)
1935450296f3STakashi Sakamoto {
1936450296f3STakashi Sakamoto struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
193742750b04SJaroslav Kysela unsigned int len;
193842750b04SJaroslav Kysela
1939450296f3STakashi Sakamoto if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ))
1940450296f3STakashi Sakamoto return -ENXIO;
19414c8099e9STakashi Sakamoto
19424c8099e9STakashi Sakamoto if (kctl->tlv.p == NULL)
19434c8099e9STakashi Sakamoto return -ENXIO;
19444c8099e9STakashi Sakamoto
1945450296f3STakashi Sakamoto len = sizeof(unsigned int) * 2 + kctl->tlv.p[1];
1946450296f3STakashi Sakamoto if (size < len)
19474c8099e9STakashi Sakamoto return -ENOMEM;
19484c8099e9STakashi Sakamoto
1949450296f3STakashi Sakamoto if (copy_to_user(buf, kctl->tlv.p, len))
19504c8099e9STakashi Sakamoto return -EFAULT;
19514c8099e9STakashi Sakamoto
19524c8099e9STakashi Sakamoto return 0;
195342750b04SJaroslav Kysela }
195442750b04SJaroslav Kysela
snd_ctl_tlv_ioctl(struct snd_ctl_file * file,struct snd_ctl_tlv __user * buf,int op_flag)1955450296f3STakashi Sakamoto static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1956450296f3STakashi Sakamoto struct snd_ctl_tlv __user *buf,
1957450296f3STakashi Sakamoto int op_flag)
1958450296f3STakashi Sakamoto {
1959450296f3STakashi Sakamoto struct snd_ctl_tlv header;
19601ba7862fSTakashi Iwai unsigned int __user *container;
1961450296f3STakashi Sakamoto unsigned int container_size;
1962450296f3STakashi Sakamoto struct snd_kcontrol *kctl;
1963450296f3STakashi Sakamoto struct snd_ctl_elem_id id;
1964450296f3STakashi Sakamoto struct snd_kcontrol_volatile *vd;
1965450296f3STakashi Sakamoto
19668320ba0cSTakashi Iwai lockdep_assert_held(&file->card->controls_rwsem);
19678320ba0cSTakashi Iwai
1968450296f3STakashi Sakamoto if (copy_from_user(&header, buf, sizeof(header)))
1969450296f3STakashi Sakamoto return -EFAULT;
1970450296f3STakashi Sakamoto
1971450296f3STakashi Sakamoto /* In design of control core, numerical ID starts at 1. */
1972450296f3STakashi Sakamoto if (header.numid == 0)
1973450296f3STakashi Sakamoto return -EINVAL;
1974450296f3STakashi Sakamoto
1975450296f3STakashi Sakamoto /* At least, container should include type and length fields. */
1976450296f3STakashi Sakamoto if (header.length < sizeof(unsigned int) * 2)
1977450296f3STakashi Sakamoto return -EINVAL;
1978450296f3STakashi Sakamoto container_size = header.length;
1979450296f3STakashi Sakamoto container = buf->tlv;
1980450296f3STakashi Sakamoto
1981b1e055f6STakashi Iwai kctl = snd_ctl_find_numid_locked(file->card, header.numid);
1982450296f3STakashi Sakamoto if (kctl == NULL)
1983450296f3STakashi Sakamoto return -ENOENT;
1984450296f3STakashi Sakamoto
1985450296f3STakashi Sakamoto /* Calculate index of the element in this set. */
1986450296f3STakashi Sakamoto id = kctl->id;
1987450296f3STakashi Sakamoto snd_ctl_build_ioff(&id, kctl, header.numid - id.numid);
1988450296f3STakashi Sakamoto vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
1989450296f3STakashi Sakamoto
1990450296f3STakashi Sakamoto if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1991450296f3STakashi Sakamoto return call_tlv_handler(file, op_flag, kctl, &id, container,
1992450296f3STakashi Sakamoto container_size);
1993450296f3STakashi Sakamoto } else {
1994450296f3STakashi Sakamoto if (op_flag == SNDRV_CTL_TLV_OP_READ) {
1995450296f3STakashi Sakamoto return read_tlv_buf(kctl, &id, container,
1996450296f3STakashi Sakamoto container_size);
1997450296f3STakashi Sakamoto }
1998450296f3STakashi Sakamoto }
1999450296f3STakashi Sakamoto
2000450296f3STakashi Sakamoto /* Not supported. */
2001450296f3STakashi Sakamoto return -ENXIO;
2002450296f3STakashi Sakamoto }
2003450296f3STakashi Sakamoto
snd_ctl_ioctl(struct file * file,unsigned int cmd,unsigned long arg)20041da177e4SLinus Torvalds static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
20051da177e4SLinus Torvalds {
200682e9bae6STakashi Iwai struct snd_ctl_file *ctl;
200782e9bae6STakashi Iwai struct snd_card *card;
200882e9bae6STakashi Iwai struct snd_kctl_ioctl *p;
20091da177e4SLinus Torvalds void __user *argp = (void __user *)arg;
20101da177e4SLinus Torvalds int __user *ip = argp;
20111da177e4SLinus Torvalds int err;
20121da177e4SLinus Torvalds
20131da177e4SLinus Torvalds ctl = file->private_data;
20141da177e4SLinus Torvalds card = ctl->card;
20157eaa943cSTakashi Iwai if (snd_BUG_ON(!card))
20167eaa943cSTakashi Iwai return -ENXIO;
20171da177e4SLinus Torvalds switch (cmd) {
20181da177e4SLinus Torvalds case SNDRV_CTL_IOCTL_PVERSION:
20191da177e4SLinus Torvalds return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
20201da177e4SLinus Torvalds case SNDRV_CTL_IOCTL_CARD_INFO:
20211da177e4SLinus Torvalds return snd_ctl_card_info(card, ctl, cmd, argp);
20221da177e4SLinus Torvalds case SNDRV_CTL_IOCTL_ELEM_LIST:
202318d122c0SArnd Bergmann return snd_ctl_elem_list_user(card, argp);
20241da177e4SLinus Torvalds case SNDRV_CTL_IOCTL_ELEM_INFO:
20251da177e4SLinus Torvalds return snd_ctl_elem_info_user(ctl, argp);
20261da177e4SLinus Torvalds case SNDRV_CTL_IOCTL_ELEM_READ:
202742750b04SJaroslav Kysela return snd_ctl_elem_read_user(card, argp);
20281da177e4SLinus Torvalds case SNDRV_CTL_IOCTL_ELEM_WRITE:
20291da177e4SLinus Torvalds return snd_ctl_elem_write_user(ctl, argp);
20301da177e4SLinus Torvalds case SNDRV_CTL_IOCTL_ELEM_LOCK:
20311da177e4SLinus Torvalds return snd_ctl_elem_lock(ctl, argp);
20321da177e4SLinus Torvalds case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
20331da177e4SLinus Torvalds return snd_ctl_elem_unlock(ctl, argp);
20341da177e4SLinus Torvalds case SNDRV_CTL_IOCTL_ELEM_ADD:
20351da177e4SLinus Torvalds return snd_ctl_elem_add_user(ctl, argp, 0);
20361da177e4SLinus Torvalds case SNDRV_CTL_IOCTL_ELEM_REPLACE:
20371da177e4SLinus Torvalds return snd_ctl_elem_add_user(ctl, argp, 1);
20381da177e4SLinus Torvalds case SNDRV_CTL_IOCTL_ELEM_REMOVE:
20391da177e4SLinus Torvalds return snd_ctl_elem_remove(ctl, argp);
20401da177e4SLinus Torvalds case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
20411da177e4SLinus Torvalds return snd_ctl_subscribe_events(ctl, ip);
204242750b04SJaroslav Kysela case SNDRV_CTL_IOCTL_TLV_READ:
20434c8099e9STakashi Sakamoto down_read(&ctl->card->controls_rwsem);
20444c8099e9STakashi Sakamoto err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_READ);
20454c8099e9STakashi Sakamoto up_read(&ctl->card->controls_rwsem);
20464c8099e9STakashi Sakamoto return err;
20478aa9b586SJaroslav Kysela case SNDRV_CTL_IOCTL_TLV_WRITE:
20484c8099e9STakashi Sakamoto down_write(&ctl->card->controls_rwsem);
20494c8099e9STakashi Sakamoto err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_WRITE);
20504c8099e9STakashi Sakamoto up_write(&ctl->card->controls_rwsem);
20514c8099e9STakashi Sakamoto return err;
20528aa9b586SJaroslav Kysela case SNDRV_CTL_IOCTL_TLV_COMMAND:
20534c8099e9STakashi Sakamoto down_write(&ctl->card->controls_rwsem);
20544c8099e9STakashi Sakamoto err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_CMD);
20554c8099e9STakashi Sakamoto up_write(&ctl->card->controls_rwsem);
20564c8099e9STakashi Sakamoto return err;
20571da177e4SLinus Torvalds case SNDRV_CTL_IOCTL_POWER:
2058a381a7a6STakashi Iwai return -ENOPROTOOPT;
20591da177e4SLinus Torvalds case SNDRV_CTL_IOCTL_POWER_STATE:
20601da177e4SLinus Torvalds return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
20611da177e4SLinus Torvalds }
20621da177e4SLinus Torvalds down_read(&snd_ioctl_rwsem);
20639244b2c3SJohannes Berg list_for_each_entry(p, &snd_control_ioctls, list) {
20641da177e4SLinus Torvalds err = p->fioctl(card, ctl, cmd, arg);
20651da177e4SLinus Torvalds if (err != -ENOIOCTLCMD) {
20661da177e4SLinus Torvalds up_read(&snd_ioctl_rwsem);
20671da177e4SLinus Torvalds return err;
20681da177e4SLinus Torvalds }
20691da177e4SLinus Torvalds }
20701da177e4SLinus Torvalds up_read(&snd_ioctl_rwsem);
2071bb009457STakashi Iwai dev_dbg(card->dev, "unknown ioctl = 0x%x\n", cmd);
20721da177e4SLinus Torvalds return -ENOTTY;
20731da177e4SLinus Torvalds }
20741da177e4SLinus Torvalds
snd_ctl_read(struct file * file,char __user * buffer,size_t count,loff_t * offset)207582e9bae6STakashi Iwai static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
207682e9bae6STakashi Iwai size_t count, loff_t * offset)
20771da177e4SLinus Torvalds {
207882e9bae6STakashi Iwai struct snd_ctl_file *ctl;
20791da177e4SLinus Torvalds int err = 0;
20801da177e4SLinus Torvalds ssize_t result = 0;
20811da177e4SLinus Torvalds
20821da177e4SLinus Torvalds ctl = file->private_data;
20837eaa943cSTakashi Iwai if (snd_BUG_ON(!ctl || !ctl->card))
20847eaa943cSTakashi Iwai return -ENXIO;
20851da177e4SLinus Torvalds if (!ctl->subscribed)
20861da177e4SLinus Torvalds return -EBADFD;
208782e9bae6STakashi Iwai if (count < sizeof(struct snd_ctl_event))
20881da177e4SLinus Torvalds return -EINVAL;
20891da177e4SLinus Torvalds spin_lock_irq(&ctl->read_lock);
209082e9bae6STakashi Iwai while (count >= sizeof(struct snd_ctl_event)) {
209182e9bae6STakashi Iwai struct snd_ctl_event ev;
209282e9bae6STakashi Iwai struct snd_kctl_event *kev;
20931da177e4SLinus Torvalds while (list_empty(&ctl->events)) {
2094ac6424b9SIngo Molnar wait_queue_entry_t wait;
20951da177e4SLinus Torvalds if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
20961da177e4SLinus Torvalds err = -EAGAIN;
20971da177e4SLinus Torvalds goto __end_lock;
20981da177e4SLinus Torvalds }
20991da177e4SLinus Torvalds init_waitqueue_entry(&wait, current);
21001da177e4SLinus Torvalds add_wait_queue(&ctl->change_sleep, &wait);
21011da177e4SLinus Torvalds set_current_state(TASK_INTERRUPTIBLE);
21021da177e4SLinus Torvalds spin_unlock_irq(&ctl->read_lock);
21031da177e4SLinus Torvalds schedule();
21041da177e4SLinus Torvalds remove_wait_queue(&ctl->change_sleep, &wait);
21050914f796STakashi Iwai if (ctl->card->shutdown)
21060914f796STakashi Iwai return -ENODEV;
21071da177e4SLinus Torvalds if (signal_pending(current))
21080e5d720cSAdrian Bunk return -ERESTARTSYS;
21091da177e4SLinus Torvalds spin_lock_irq(&ctl->read_lock);
21101da177e4SLinus Torvalds }
21111da177e4SLinus Torvalds kev = snd_kctl_event(ctl->events.next);
21121da177e4SLinus Torvalds ev.type = SNDRV_CTL_EVENT_ELEM;
21131da177e4SLinus Torvalds ev.data.elem.mask = kev->mask;
21141da177e4SLinus Torvalds ev.data.elem.id = kev->id;
21151da177e4SLinus Torvalds list_del(&kev->list);
21161da177e4SLinus Torvalds spin_unlock_irq(&ctl->read_lock);
21171da177e4SLinus Torvalds kfree(kev);
211882e9bae6STakashi Iwai if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
21191da177e4SLinus Torvalds err = -EFAULT;
21201da177e4SLinus Torvalds goto __end;
21211da177e4SLinus Torvalds }
21221da177e4SLinus Torvalds spin_lock_irq(&ctl->read_lock);
212382e9bae6STakashi Iwai buffer += sizeof(struct snd_ctl_event);
212482e9bae6STakashi Iwai count -= sizeof(struct snd_ctl_event);
212582e9bae6STakashi Iwai result += sizeof(struct snd_ctl_event);
21261da177e4SLinus Torvalds }
21271da177e4SLinus Torvalds __end_lock:
21281da177e4SLinus Torvalds spin_unlock_irq(&ctl->read_lock);
21291da177e4SLinus Torvalds __end:
21301da177e4SLinus Torvalds return result > 0 ? result : err;
21311da177e4SLinus Torvalds }
21321da177e4SLinus Torvalds
snd_ctl_poll(struct file * file,poll_table * wait)2133680ef72aSAl Viro static __poll_t snd_ctl_poll(struct file *file, poll_table * wait)
21341da177e4SLinus Torvalds {
2135680ef72aSAl Viro __poll_t mask;
213682e9bae6STakashi Iwai struct snd_ctl_file *ctl;
21371da177e4SLinus Torvalds
21381da177e4SLinus Torvalds ctl = file->private_data;
21391da177e4SLinus Torvalds if (!ctl->subscribed)
21401da177e4SLinus Torvalds return 0;
21411da177e4SLinus Torvalds poll_wait(file, &ctl->change_sleep, wait);
21421da177e4SLinus Torvalds
21431da177e4SLinus Torvalds mask = 0;
21441da177e4SLinus Torvalds if (!list_empty(&ctl->events))
2145a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM;
21461da177e4SLinus Torvalds
21471da177e4SLinus Torvalds return mask;
21481da177e4SLinus Torvalds }
21491da177e4SLinus Torvalds
21501da177e4SLinus Torvalds /*
21511da177e4SLinus Torvalds * register the device-specific control-ioctls.
21521da177e4SLinus Torvalds * called from each device manager like pcm.c, hwdep.c, etc.
21531da177e4SLinus Torvalds */
_snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn,struct list_head * lists)21541da177e4SLinus Torvalds static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
21551da177e4SLinus Torvalds {
215682e9bae6STakashi Iwai struct snd_kctl_ioctl *pn;
21571da177e4SLinus Torvalds
215882e9bae6STakashi Iwai pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
21591da177e4SLinus Torvalds if (pn == NULL)
21601da177e4SLinus Torvalds return -ENOMEM;
21611da177e4SLinus Torvalds pn->fioctl = fcn;
21621da177e4SLinus Torvalds down_write(&snd_ioctl_rwsem);
21631da177e4SLinus Torvalds list_add_tail(&pn->list, lists);
21641da177e4SLinus Torvalds up_write(&snd_ioctl_rwsem);
21651da177e4SLinus Torvalds return 0;
21661da177e4SLinus Torvalds }
21671da177e4SLinus Torvalds
216812cddbd8STakashi Iwai /**
216912cddbd8STakashi Iwai * snd_ctl_register_ioctl - register the device-specific control-ioctls
217012cddbd8STakashi Iwai * @fcn: ioctl callback function
217112cddbd8STakashi Iwai *
217212cddbd8STakashi Iwai * called from each device manager like pcm.c, hwdep.c, etc.
2173e8406ebcSTakashi Iwai *
2174e8406ebcSTakashi Iwai * Return: zero if successful, or a negative error code
217512cddbd8STakashi Iwai */
snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)21761da177e4SLinus Torvalds int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
21771da177e4SLinus Torvalds {
21781da177e4SLinus Torvalds return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
21791da177e4SLinus Torvalds }
2180c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_ctl_register_ioctl);
2181c0d3fb39STakashi Iwai
21821da177e4SLinus Torvalds #ifdef CONFIG_COMPAT
218312cddbd8STakashi Iwai /**
218412cddbd8STakashi Iwai * snd_ctl_register_ioctl_compat - register the device-specific 32bit compat
218512cddbd8STakashi Iwai * control-ioctls
218612cddbd8STakashi Iwai * @fcn: ioctl callback function
2187e8406ebcSTakashi Iwai *
2188e8406ebcSTakashi Iwai * Return: zero if successful, or a negative error code
218912cddbd8STakashi Iwai */
snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)21901da177e4SLinus Torvalds int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
21911da177e4SLinus Torvalds {
21921da177e4SLinus Torvalds return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
21931da177e4SLinus Torvalds }
2194c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
21951da177e4SLinus Torvalds #endif
21961da177e4SLinus Torvalds
21971da177e4SLinus Torvalds /*
21981da177e4SLinus Torvalds * de-register the device-specific control-ioctls.
21991da177e4SLinus Torvalds */
_snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,struct list_head * lists)220082e9bae6STakashi Iwai static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
220182e9bae6STakashi Iwai struct list_head *lists)
22021da177e4SLinus Torvalds {
220382e9bae6STakashi Iwai struct snd_kctl_ioctl *p;
22041da177e4SLinus Torvalds
22057eaa943cSTakashi Iwai if (snd_BUG_ON(!fcn))
22067eaa943cSTakashi Iwai return -EINVAL;
22071da177e4SLinus Torvalds down_write(&snd_ioctl_rwsem);
22089244b2c3SJohannes Berg list_for_each_entry(p, lists, list) {
22091da177e4SLinus Torvalds if (p->fioctl == fcn) {
22101da177e4SLinus Torvalds list_del(&p->list);
22111da177e4SLinus Torvalds up_write(&snd_ioctl_rwsem);
22121da177e4SLinus Torvalds kfree(p);
22131da177e4SLinus Torvalds return 0;
22141da177e4SLinus Torvalds }
22151da177e4SLinus Torvalds }
22161da177e4SLinus Torvalds up_write(&snd_ioctl_rwsem);
22171da177e4SLinus Torvalds snd_BUG();
22181da177e4SLinus Torvalds return -EINVAL;
22191da177e4SLinus Torvalds }
22201da177e4SLinus Torvalds
222112cddbd8STakashi Iwai /**
222212cddbd8STakashi Iwai * snd_ctl_unregister_ioctl - de-register the device-specific control-ioctls
222312cddbd8STakashi Iwai * @fcn: ioctl callback function to unregister
2224e8406ebcSTakashi Iwai *
2225e8406ebcSTakashi Iwai * Return: zero if successful, or a negative error code
222612cddbd8STakashi Iwai */
snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)22271da177e4SLinus Torvalds int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
22281da177e4SLinus Torvalds {
22291da177e4SLinus Torvalds return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
22301da177e4SLinus Torvalds }
2231c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
2232c0d3fb39STakashi Iwai
22331da177e4SLinus Torvalds #ifdef CONFIG_COMPAT
223412cddbd8STakashi Iwai /**
2235f7b6603cSMauro Carvalho Chehab * snd_ctl_unregister_ioctl_compat - de-register the device-specific compat
2236f7b6603cSMauro Carvalho Chehab * 32bit control-ioctls
223712cddbd8STakashi Iwai * @fcn: ioctl callback function to unregister
2238e8406ebcSTakashi Iwai *
2239e8406ebcSTakashi Iwai * Return: zero if successful, or a negative error code
224012cddbd8STakashi Iwai */
snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)22411da177e4SLinus Torvalds int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
22421da177e4SLinus Torvalds {
22431da177e4SLinus Torvalds return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
22441da177e4SLinus Torvalds }
2245c0d3fb39STakashi Iwai EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
22461da177e4SLinus Torvalds #endif
22471da177e4SLinus Torvalds
snd_ctl_fasync(int fd,struct file * file,int on)22481da177e4SLinus Torvalds static int snd_ctl_fasync(int fd, struct file * file, int on)
22491da177e4SLinus Torvalds {
225082e9bae6STakashi Iwai struct snd_ctl_file *ctl;
225160aa4924SJonathan Corbet
22521da177e4SLinus Torvalds ctl = file->private_data;
22534a971e84STakashi Iwai return snd_fasync_helper(fd, file, on, &ctl->fasync);
22541da177e4SLinus Torvalds }
22551da177e4SLinus Torvalds
225623c18d4bSTakashi Iwai /* return the preferred subdevice number if already assigned;
225723c18d4bSTakashi Iwai * otherwise return -1
225823c18d4bSTakashi Iwai */
snd_ctl_get_preferred_subdevice(struct snd_card * card,int type)225923c18d4bSTakashi Iwai int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type)
226023c18d4bSTakashi Iwai {
226123c18d4bSTakashi Iwai struct snd_ctl_file *kctl;
226223c18d4bSTakashi Iwai int subdevice = -1;
22636564d0adSTakashi Iwai unsigned long flags;
226423c18d4bSTakashi Iwai
22656564d0adSTakashi Iwai read_lock_irqsave(&card->ctl_files_rwlock, flags);
226623c18d4bSTakashi Iwai list_for_each_entry(kctl, &card->ctl_files, list) {
226723c18d4bSTakashi Iwai if (kctl->pid == task_pid(current)) {
226823c18d4bSTakashi Iwai subdevice = kctl->preferred_subdevice[type];
226923c18d4bSTakashi Iwai if (subdevice != -1)
227023c18d4bSTakashi Iwai break;
227123c18d4bSTakashi Iwai }
227223c18d4bSTakashi Iwai }
22736564d0adSTakashi Iwai read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
227423c18d4bSTakashi Iwai return subdevice;
227523c18d4bSTakashi Iwai }
227623c18d4bSTakashi Iwai EXPORT_SYMBOL_GPL(snd_ctl_get_preferred_subdevice);
227723c18d4bSTakashi Iwai
22781da177e4SLinus Torvalds /*
22791da177e4SLinus Torvalds * ioctl32 compat
22801da177e4SLinus Torvalds */
22811da177e4SLinus Torvalds #ifdef CONFIG_COMPAT
22821da177e4SLinus Torvalds #include "control_compat.c"
22831da177e4SLinus Torvalds #else
22841da177e4SLinus Torvalds #define snd_ctl_ioctl_compat NULL
22851da177e4SLinus Torvalds #endif
22861da177e4SLinus Torvalds
22871da177e4SLinus Torvalds /*
22883f0638a0SJaroslav Kysela * control layers (audio LED etc.)
22893f0638a0SJaroslav Kysela */
22903f0638a0SJaroslav Kysela
22913f0638a0SJaroslav Kysela /**
22923f0638a0SJaroslav Kysela * snd_ctl_request_layer - request to use the layer
22933f0638a0SJaroslav Kysela * @module_name: Name of the kernel module (NULL == build-in)
22943f0638a0SJaroslav Kysela *
2295e8406ebcSTakashi Iwai * Return: zero if successful, or an error code when the module cannot be loaded
22963f0638a0SJaroslav Kysela */
snd_ctl_request_layer(const char * module_name)22973f0638a0SJaroslav Kysela int snd_ctl_request_layer(const char *module_name)
22983f0638a0SJaroslav Kysela {
22993f0638a0SJaroslav Kysela struct snd_ctl_layer_ops *lops;
23003f0638a0SJaroslav Kysela
23013f0638a0SJaroslav Kysela if (module_name == NULL)
23023f0638a0SJaroslav Kysela return 0;
23033f0638a0SJaroslav Kysela down_read(&snd_ctl_layer_rwsem);
23043f0638a0SJaroslav Kysela for (lops = snd_ctl_layer; lops; lops = lops->next)
23053f0638a0SJaroslav Kysela if (strcmp(lops->module_name, module_name) == 0)
23063f0638a0SJaroslav Kysela break;
23073f0638a0SJaroslav Kysela up_read(&snd_ctl_layer_rwsem);
23083f0638a0SJaroslav Kysela if (lops)
23093f0638a0SJaroslav Kysela return 0;
23103f0638a0SJaroslav Kysela return request_module(module_name);
23113f0638a0SJaroslav Kysela }
23123f0638a0SJaroslav Kysela EXPORT_SYMBOL_GPL(snd_ctl_request_layer);
23133f0638a0SJaroslav Kysela
23143f0638a0SJaroslav Kysela /**
23153f0638a0SJaroslav Kysela * snd_ctl_register_layer - register new control layer
23163f0638a0SJaroslav Kysela * @lops: operation structure
23173f0638a0SJaroslav Kysela *
23183f0638a0SJaroslav Kysela * The new layer can track all control elements and do additional
23193f0638a0SJaroslav Kysela * operations on top (like audio LED handling).
23203f0638a0SJaroslav Kysela */
snd_ctl_register_layer(struct snd_ctl_layer_ops * lops)23213f0638a0SJaroslav Kysela void snd_ctl_register_layer(struct snd_ctl_layer_ops *lops)
23223f0638a0SJaroslav Kysela {
23233f0638a0SJaroslav Kysela struct snd_card *card;
23243f0638a0SJaroslav Kysela int card_number;
23253f0638a0SJaroslav Kysela
23263f0638a0SJaroslav Kysela down_write(&snd_ctl_layer_rwsem);
23273f0638a0SJaroslav Kysela lops->next = snd_ctl_layer;
23283f0638a0SJaroslav Kysela snd_ctl_layer = lops;
23293f0638a0SJaroslav Kysela up_write(&snd_ctl_layer_rwsem);
23303f0638a0SJaroslav Kysela for (card_number = 0; card_number < SNDRV_CARDS; card_number++) {
23313f0638a0SJaroslav Kysela card = snd_card_ref(card_number);
23323f0638a0SJaroslav Kysela if (card) {
23333f0638a0SJaroslav Kysela down_read(&card->controls_rwsem);
23343f0638a0SJaroslav Kysela lops->lregister(card);
23353f0638a0SJaroslav Kysela up_read(&card->controls_rwsem);
23363f0638a0SJaroslav Kysela snd_card_unref(card);
23373f0638a0SJaroslav Kysela }
23383f0638a0SJaroslav Kysela }
23393f0638a0SJaroslav Kysela }
23403f0638a0SJaroslav Kysela EXPORT_SYMBOL_GPL(snd_ctl_register_layer);
23413f0638a0SJaroslav Kysela
23423f0638a0SJaroslav Kysela /**
23433f0638a0SJaroslav Kysela * snd_ctl_disconnect_layer - disconnect control layer
23443f0638a0SJaroslav Kysela * @lops: operation structure
23453f0638a0SJaroslav Kysela *
23463f0638a0SJaroslav Kysela * It is expected that the information about tracked cards
23473f0638a0SJaroslav Kysela * is freed before this call (the disconnect callback is
23483f0638a0SJaroslav Kysela * not called here).
23493f0638a0SJaroslav Kysela */
snd_ctl_disconnect_layer(struct snd_ctl_layer_ops * lops)23503f0638a0SJaroslav Kysela void snd_ctl_disconnect_layer(struct snd_ctl_layer_ops *lops)
23513f0638a0SJaroslav Kysela {
23523f0638a0SJaroslav Kysela struct snd_ctl_layer_ops *lops2, *prev_lops2;
23533f0638a0SJaroslav Kysela
23543f0638a0SJaroslav Kysela down_write(&snd_ctl_layer_rwsem);
2355016c2050SJaroslav Kysela for (lops2 = snd_ctl_layer, prev_lops2 = NULL; lops2; lops2 = lops2->next) {
23563f0638a0SJaroslav Kysela if (lops2 == lops) {
23573f0638a0SJaroslav Kysela if (!prev_lops2)
23583f0638a0SJaroslav Kysela snd_ctl_layer = lops->next;
23593f0638a0SJaroslav Kysela else
23603f0638a0SJaroslav Kysela prev_lops2->next = lops->next;
23613f0638a0SJaroslav Kysela break;
23623f0638a0SJaroslav Kysela }
2363016c2050SJaroslav Kysela prev_lops2 = lops2;
2364016c2050SJaroslav Kysela }
23653f0638a0SJaroslav Kysela up_write(&snd_ctl_layer_rwsem);
23663f0638a0SJaroslav Kysela }
23673f0638a0SJaroslav Kysela EXPORT_SYMBOL_GPL(snd_ctl_disconnect_layer);
23683f0638a0SJaroslav Kysela
23693f0638a0SJaroslav Kysela /*
23701da177e4SLinus Torvalds * INIT PART
23711da177e4SLinus Torvalds */
23721da177e4SLinus Torvalds
23739c2e08c5SArjan van de Ven static const struct file_operations snd_ctl_f_ops =
23741da177e4SLinus Torvalds {
23751da177e4SLinus Torvalds .owner = THIS_MODULE,
23761da177e4SLinus Torvalds .read = snd_ctl_read,
23771da177e4SLinus Torvalds .open = snd_ctl_open,
23781da177e4SLinus Torvalds .release = snd_ctl_release,
237902f4865fSTakashi Iwai .llseek = no_llseek,
23801da177e4SLinus Torvalds .poll = snd_ctl_poll,
23811da177e4SLinus Torvalds .unlocked_ioctl = snd_ctl_ioctl,
23821da177e4SLinus Torvalds .compat_ioctl = snd_ctl_ioctl_compat,
23831da177e4SLinus Torvalds .fasync = snd_ctl_fasync,
23841da177e4SLinus Torvalds };
23851da177e4SLinus Torvalds
23861da177e4SLinus Torvalds /*
23871da177e4SLinus Torvalds * registration of the control device
23881da177e4SLinus Torvalds */
snd_ctl_dev_register(struct snd_device * device)238982e9bae6STakashi Iwai static int snd_ctl_dev_register(struct snd_device *device)
23901da177e4SLinus Torvalds {
239182e9bae6STakashi Iwai struct snd_card *card = device->device_data;
23923f0638a0SJaroslav Kysela struct snd_ctl_layer_ops *lops;
23933f0638a0SJaroslav Kysela int err;
23941da177e4SLinus Torvalds
23953f0638a0SJaroslav Kysela err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
23966a66b01dSTakashi Iwai &snd_ctl_f_ops, card, card->ctl_dev);
23973f0638a0SJaroslav Kysela if (err < 0)
23983f0638a0SJaroslav Kysela return err;
23993f0638a0SJaroslav Kysela down_read(&card->controls_rwsem);
24003f0638a0SJaroslav Kysela down_read(&snd_ctl_layer_rwsem);
24013f0638a0SJaroslav Kysela for (lops = snd_ctl_layer; lops; lops = lops->next)
24023f0638a0SJaroslav Kysela lops->lregister(card);
24033f0638a0SJaroslav Kysela up_read(&snd_ctl_layer_rwsem);
24043f0638a0SJaroslav Kysela up_read(&card->controls_rwsem);
24053f0638a0SJaroslav Kysela return 0;
24061da177e4SLinus Torvalds }
24071da177e4SLinus Torvalds
24081da177e4SLinus Torvalds /*
24091da177e4SLinus Torvalds * disconnection of the control device
24101da177e4SLinus Torvalds */
snd_ctl_dev_disconnect(struct snd_device * device)241182e9bae6STakashi Iwai static int snd_ctl_dev_disconnect(struct snd_device *device)
24121da177e4SLinus Torvalds {
241382e9bae6STakashi Iwai struct snd_card *card = device->device_data;
241482e9bae6STakashi Iwai struct snd_ctl_file *ctl;
24153f0638a0SJaroslav Kysela struct snd_ctl_layer_ops *lops;
24166564d0adSTakashi Iwai unsigned long flags;
24171da177e4SLinus Torvalds
24186564d0adSTakashi Iwai read_lock_irqsave(&card->ctl_files_rwlock, flags);
24199244b2c3SJohannes Berg list_for_each_entry(ctl, &card->ctl_files, list) {
24201da177e4SLinus Torvalds wake_up(&ctl->change_sleep);
24214a971e84STakashi Iwai snd_kill_fasync(ctl->fasync, SIGIO, POLL_ERR);
24221da177e4SLinus Torvalds }
24236564d0adSTakashi Iwai read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
2424c461482cSTakashi Iwai
24253f0638a0SJaroslav Kysela down_read(&card->controls_rwsem);
24263f0638a0SJaroslav Kysela down_read(&snd_ctl_layer_rwsem);
24273f0638a0SJaroslav Kysela for (lops = snd_ctl_layer; lops; lops = lops->next)
24283f0638a0SJaroslav Kysela lops->ldisconnect(card);
24293f0638a0SJaroslav Kysela up_read(&snd_ctl_layer_rwsem);
24303f0638a0SJaroslav Kysela up_read(&card->controls_rwsem);
24313f0638a0SJaroslav Kysela
24326a66b01dSTakashi Iwai return snd_unregister_device(card->ctl_dev);
24331da177e4SLinus Torvalds }
24341da177e4SLinus Torvalds
24351da177e4SLinus Torvalds /*
24361da177e4SLinus Torvalds * free all controls
24371da177e4SLinus Torvalds */
snd_ctl_dev_free(struct snd_device * device)243882e9bae6STakashi Iwai static int snd_ctl_dev_free(struct snd_device *device)
24391da177e4SLinus Torvalds {
244082e9bae6STakashi Iwai struct snd_card *card = device->device_data;
244182e9bae6STakashi Iwai struct snd_kcontrol *control;
24421da177e4SLinus Torvalds
24431da177e4SLinus Torvalds down_write(&card->controls_rwsem);
24441da177e4SLinus Torvalds while (!list_empty(&card->controls)) {
24451da177e4SLinus Torvalds control = snd_kcontrol(card->controls.next);
2446c27e1efbSTakashi Iwai __snd_ctl_remove(card, control, false);
24471da177e4SLinus Torvalds }
2448c27e1efbSTakashi Iwai
2449c27e1efbSTakashi Iwai #ifdef CONFIG_SND_CTL_FAST_LOOKUP
2450c27e1efbSTakashi Iwai xa_destroy(&card->ctl_numids);
2451c27e1efbSTakashi Iwai xa_destroy(&card->ctl_hash);
2452c27e1efbSTakashi Iwai #endif
24531da177e4SLinus Torvalds up_write(&card->controls_rwsem);
24546a66b01dSTakashi Iwai put_device(card->ctl_dev);
24551da177e4SLinus Torvalds return 0;
24561da177e4SLinus Torvalds }
24571da177e4SLinus Torvalds
24581da177e4SLinus Torvalds /*
24591da177e4SLinus Torvalds * create control core:
24601da177e4SLinus Torvalds * called from init.c
24611da177e4SLinus Torvalds */
snd_ctl_create(struct snd_card * card)246282e9bae6STakashi Iwai int snd_ctl_create(struct snd_card *card)
24631da177e4SLinus Torvalds {
2464f15ee210STakashi Iwai static const struct snd_device_ops ops = {
24651da177e4SLinus Torvalds .dev_free = snd_ctl_dev_free,
24661da177e4SLinus Torvalds .dev_register = snd_ctl_dev_register,
24671da177e4SLinus Torvalds .dev_disconnect = snd_ctl_dev_disconnect,
24681da177e4SLinus Torvalds };
24690fcd9f4bSTakashi Iwai int err;
24701da177e4SLinus Torvalds
24717eaa943cSTakashi Iwai if (snd_BUG_ON(!card))
24727eaa943cSTakashi Iwai return -ENXIO;
24730fcd9f4bSTakashi Iwai if (snd_BUG_ON(card->number < 0 || card->number >= SNDRV_CARDS))
24740fcd9f4bSTakashi Iwai return -ENXIO;
24750fcd9f4bSTakashi Iwai
24766a66b01dSTakashi Iwai err = snd_device_alloc(&card->ctl_dev, card);
24776a66b01dSTakashi Iwai if (err < 0)
24786a66b01dSTakashi Iwai return err;
24796a66b01dSTakashi Iwai dev_set_name(card->ctl_dev, "controlC%d", card->number);
24800fcd9f4bSTakashi Iwai
24810fcd9f4bSTakashi Iwai err = snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
24820fcd9f4bSTakashi Iwai if (err < 0)
24836a66b01dSTakashi Iwai put_device(card->ctl_dev);
24840fcd9f4bSTakashi Iwai return err;
24851da177e4SLinus Torvalds }
2486b9ed4f2bSTakashi Iwai
2487b9ed4f2bSTakashi Iwai /*
24889600732bSClemens Ladisch * Frequently used control callbacks/helpers
2489b9ed4f2bSTakashi Iwai */
249012cddbd8STakashi Iwai
249112cddbd8STakashi Iwai /**
249212cddbd8STakashi Iwai * snd_ctl_boolean_mono_info - Helper function for a standard boolean info
249312cddbd8STakashi Iwai * callback with a mono channel
249412cddbd8STakashi Iwai * @kcontrol: the kcontrol instance
249512cddbd8STakashi Iwai * @uinfo: info to store
249612cddbd8STakashi Iwai *
249712cddbd8STakashi Iwai * This is a function that can be used as info callback for a standard
249812cddbd8STakashi Iwai * boolean control with a single mono channel.
2499e8406ebcSTakashi Iwai *
2500e8406ebcSTakashi Iwai * Return: Zero (always successful)
250112cddbd8STakashi Iwai */
snd_ctl_boolean_mono_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2502b9ed4f2bSTakashi Iwai int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
2503b9ed4f2bSTakashi Iwai struct snd_ctl_elem_info *uinfo)
2504b9ed4f2bSTakashi Iwai {
2505b9ed4f2bSTakashi Iwai uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2506b9ed4f2bSTakashi Iwai uinfo->count = 1;
2507b9ed4f2bSTakashi Iwai uinfo->value.integer.min = 0;
2508b9ed4f2bSTakashi Iwai uinfo->value.integer.max = 1;
2509b9ed4f2bSTakashi Iwai return 0;
2510b9ed4f2bSTakashi Iwai }
2511b9ed4f2bSTakashi Iwai EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
2512b9ed4f2bSTakashi Iwai
251312cddbd8STakashi Iwai /**
251412cddbd8STakashi Iwai * snd_ctl_boolean_stereo_info - Helper function for a standard boolean info
251512cddbd8STakashi Iwai * callback with stereo two channels
251612cddbd8STakashi Iwai * @kcontrol: the kcontrol instance
251712cddbd8STakashi Iwai * @uinfo: info to store
251812cddbd8STakashi Iwai *
251912cddbd8STakashi Iwai * This is a function that can be used as info callback for a standard
252012cddbd8STakashi Iwai * boolean control with stereo two channels.
2521e8406ebcSTakashi Iwai *
2522e8406ebcSTakashi Iwai * Return: Zero (always successful)
252312cddbd8STakashi Iwai */
snd_ctl_boolean_stereo_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2524b9ed4f2bSTakashi Iwai int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
2525b9ed4f2bSTakashi Iwai struct snd_ctl_elem_info *uinfo)
2526b9ed4f2bSTakashi Iwai {
2527b9ed4f2bSTakashi Iwai uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2528b9ed4f2bSTakashi Iwai uinfo->count = 2;
2529b9ed4f2bSTakashi Iwai uinfo->value.integer.min = 0;
2530b9ed4f2bSTakashi Iwai uinfo->value.integer.max = 1;
2531b9ed4f2bSTakashi Iwai return 0;
2532b9ed4f2bSTakashi Iwai }
2533b9ed4f2bSTakashi Iwai EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
25349600732bSClemens Ladisch
25359600732bSClemens Ladisch /**
25369600732bSClemens Ladisch * snd_ctl_enum_info - fills the info structure for an enumerated control
25379600732bSClemens Ladisch * @info: the structure to be filled
25389600732bSClemens Ladisch * @channels: the number of the control's channels; often one
25399600732bSClemens Ladisch * @items: the number of control values; also the size of @names
25409600732bSClemens Ladisch * @names: an array containing the names of all control values
25419600732bSClemens Ladisch *
25429600732bSClemens Ladisch * Sets all required fields in @info to their appropriate values.
25439600732bSClemens Ladisch * If the control's accessibility is not the default (readable and writable),
25449600732bSClemens Ladisch * the caller has to fill @info->access.
2545eb7c06e8SYacine Belkadi *
2546e8406ebcSTakashi Iwai * Return: Zero (always successful)
25479600732bSClemens Ladisch */
snd_ctl_enum_info(struct snd_ctl_elem_info * info,unsigned int channels,unsigned int items,const char * const names[])25489600732bSClemens Ladisch int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
25499600732bSClemens Ladisch unsigned int items, const char *const names[])
25509600732bSClemens Ladisch {
25519600732bSClemens Ladisch info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
25529600732bSClemens Ladisch info->count = channels;
25539600732bSClemens Ladisch info->value.enumerated.items = items;
2554a7e6fb99STakashi Iwai if (!items)
2555a7e6fb99STakashi Iwai return 0;
25569600732bSClemens Ladisch if (info->value.enumerated.item >= items)
25579600732bSClemens Ladisch info->value.enumerated.item = items - 1;
2558df803e13STakashi Iwai WARN(strlen(names[info->value.enumerated.item]) >= sizeof(info->value.enumerated.name),
2559df803e13STakashi Iwai "ALSA: too long item name '%s'\n",
2560df803e13STakashi Iwai names[info->value.enumerated.item]);
256175b1a8f9SJoe Perches strscpy(info->value.enumerated.name,
25629600732bSClemens Ladisch names[info->value.enumerated.item],
25639600732bSClemens Ladisch sizeof(info->value.enumerated.name));
25649600732bSClemens Ladisch return 0;
25659600732bSClemens Ladisch }
25669600732bSClemens Ladisch EXPORT_SYMBOL(snd_ctl_enum_info);
2567