blob: 0b85cbc27e4d2eb9afa7531cdb15eb7851386171 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines for driver control interface
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/threads.h>
23#include <linux/interrupt.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040024#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/slab.h>
26#include <linux/vmalloc.h>
27#include <linux/time.h>
28#include <sound/core.h>
29#include <sound/minors.h>
30#include <sound/info.h>
31#include <sound/control.h>
32
33/* max number of user-defined controls */
34#define MAX_USER_CONTROLS 32
Dan Rosenberg5591bf02010-09-28 14:18:20 -040035#define MAX_CONTROL_COUNT 1028
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Takashi Iwai82e9bae2005-11-17 13:53:23 +010037struct snd_kctl_ioctl {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct list_head list; /* list of all ioctls */
39 snd_kctl_ioctl_func_t fioctl;
Takashi Iwai82e9bae2005-11-17 13:53:23 +010040};
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42static DECLARE_RWSEM(snd_ioctl_rwsem);
43static LIST_HEAD(snd_control_ioctls);
44#ifdef CONFIG_COMPAT
45static LIST_HEAD(snd_control_compat_ioctls);
46#endif
47
48static int snd_ctl_open(struct inode *inode, struct file *file)
49{
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +010051 struct snd_card *card;
52 struct snd_ctl_file *ctl;
Takashi Iwai23c18d42014-02-19 14:30:29 +010053 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Takashi Iwai02f48652010-04-13 11:49:04 +020055 err = nonseekable_open(inode, file);
56 if (err < 0)
57 return err;
58
Clemens Ladischf87135f2005-11-20 14:06:59 +010059 card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 if (!card) {
61 err = -ENODEV;
62 goto __error1;
63 }
64 err = snd_card_file_add(card, file);
65 if (err < 0) {
66 err = -ENODEV;
67 goto __error1;
68 }
69 if (!try_module_get(card->module)) {
70 err = -EFAULT;
71 goto __error2;
72 }
Takashi Iwaica2c0962005-09-09 14:20:23 +020073 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (ctl == NULL) {
75 err = -ENOMEM;
76 goto __error;
77 }
78 INIT_LIST_HEAD(&ctl->events);
79 init_waitqueue_head(&ctl->change_sleep);
80 spin_lock_init(&ctl->read_lock);
81 ctl->card = card;
Takashi Iwai23c18d42014-02-19 14:30:29 +010082 for (i = 0; i < SND_CTL_SUBDEV_ITEMS; i++)
83 ctl->preferred_subdevice[i] = -1;
Clemens Ladisch25d27ed2009-11-02 09:35:44 +010084 ctl->pid = get_pid(task_pid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 file->private_data = ctl;
86 write_lock_irqsave(&card->ctl_files_rwlock, flags);
87 list_add_tail(&ctl->list, &card->ctl_files);
88 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
Takashi Iwaia0830db2012-10-16 13:05:59 +020089 snd_card_unref(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return 0;
91
92 __error:
93 module_put(card->module);
94 __error2:
95 snd_card_file_remove(card, file);
96 __error1:
Takashi Iwaia0830db2012-10-16 13:05:59 +020097 if (card)
98 snd_card_unref(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return err;
100}
101
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100102static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Borislav Petkov7507e8d2007-10-22 17:11:09 +0200104 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100105 struct snd_kctl_event *cread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Borislav Petkov7507e8d2007-10-22 17:11:09 +0200107 spin_lock_irqsave(&ctl->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 while (!list_empty(&ctl->events)) {
109 cread = snd_kctl_event(ctl->events.next);
110 list_del(&cread->list);
111 kfree(cread);
112 }
Borislav Petkov7507e8d2007-10-22 17:11:09 +0200113 spin_unlock_irqrestore(&ctl->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116static int snd_ctl_release(struct inode *inode, struct file *file)
117{
118 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100119 struct snd_card *card;
120 struct snd_ctl_file *ctl;
121 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 unsigned int idx;
123
124 ctl = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 file->private_data = NULL;
126 card = ctl->card;
127 write_lock_irqsave(&card->ctl_files_rwlock, flags);
128 list_del(&ctl->list);
129 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
130 down_write(&card->controls_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200131 list_for_each_entry(control, &card->controls, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 for (idx = 0; idx < control->count; idx++)
133 if (control->vd[idx].owner == ctl)
134 control->vd[idx].owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 up_write(&card->controls_rwsem);
136 snd_ctl_empty_read_queue(ctl);
Clemens Ladisch25d27ed2009-11-02 09:35:44 +0100137 put_pid(ctl->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 kfree(ctl);
139 module_put(card->module);
140 snd_card_file_remove(card, file);
141 return 0;
142}
143
Takashi Iwai12cddbd2014-10-30 13:44:34 +0100144/**
145 * snd_ctl_notify - Send notification to user-space for a control change
146 * @card: the card to send notification
147 * @mask: the event mask, SNDRV_CTL_EVENT_*
148 * @id: the ctl element id to send notification
149 *
150 * This function adds an event record with the given id and mask, appends
151 * to the list and wakes up the user-space for notification. This can be
152 * called in the atomic context.
153 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100154void snd_ctl_notify(struct snd_card *card, unsigned int mask,
155 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100158 struct snd_ctl_file *ctl;
159 struct snd_kctl_event *ev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200161 if (snd_BUG_ON(!card || !id))
162 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 read_lock(&card->ctl_files_rwlock);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100164#if IS_ENABLED(CONFIG_SND_MIXER_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 card->mixer_oss_change_count++;
166#endif
Johannes Berg9244b2c2006-10-05 16:02:22 +0200167 list_for_each_entry(ctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (!ctl->subscribed)
169 continue;
170 spin_lock_irqsave(&ctl->read_lock, flags);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200171 list_for_each_entry(ev, &ctl->events, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (ev->id.numid == id->numid) {
173 ev->mask |= mask;
174 goto _found;
175 }
176 }
Takashi Iwaica2c0962005-09-09 14:20:23 +0200177 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (ev) {
179 ev->id = *id;
180 ev->mask = mask;
181 list_add_tail(&ev->list, &ctl->events);
182 } else {
Takashi Iwaibb009452014-02-04 18:18:16 +0100183 dev_err(card->dev, "No memory available to allocate event\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185 _found:
186 wake_up(&ctl->change_sleep);
187 spin_unlock_irqrestore(&ctl->read_lock, flags);
188 kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
189 }
190 read_unlock(&card->ctl_files_rwlock);
191}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200192EXPORT_SYMBOL(snd_ctl_notify);
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194/**
195 * snd_ctl_new - create a control instance from the template
196 * @control: the control template
197 * @access: the default control access
198 *
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100199 * Allocates a new struct snd_kcontrol instance and copies the given template
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * to the new instance. It does not copy volatile data (access).
201 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100202 * Return: The pointer of the new instance, or %NULL on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 */
Adrian Bunk0b51ba02006-11-20 17:50:17 +0100204static struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control,
205 unsigned int access)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100207 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 unsigned int idx;
209
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200210 if (snd_BUG_ON(!control || !control->count))
211 return NULL;
Dan Rosenberg5591bf02010-09-28 14:18:20 -0400212
213 if (control->count > MAX_CONTROL_COUNT)
214 return NULL;
215
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100216 kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100217 if (kctl == NULL) {
Takashi Iwaibb009452014-02-04 18:18:16 +0100218 pr_err("ALSA: Cannot allocate control instance\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return NULL;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 *kctl = *control;
222 for (idx = 0; idx < kctl->count; idx++)
223 kctl->vd[idx].access = access;
224 return kctl;
225}
226
227/**
228 * snd_ctl_new1 - create a control instance from the template
229 * @ncontrol: the initialization record
230 * @private_data: the private data to set
231 *
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100232 * Allocates a new struct snd_kcontrol instance and initialize from the given
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 * template. When the access field of ncontrol is 0, it's assumed as
234 * READWRITE access. When the count field is 0, it's assumes as one.
235 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100236 * Return: The pointer of the newly generated instance, or %NULL on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100238struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
239 void *private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100241 struct snd_kcontrol kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 unsigned int access;
243
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200244 if (snd_BUG_ON(!ncontrol || !ncontrol->info))
245 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 memset(&kctl, 0, sizeof(kctl));
247 kctl.id.iface = ncontrol->iface;
248 kctl.id.device = ncontrol->device;
249 kctl.id.subdevice = ncontrol->subdevice;
Mark Brown366840d2008-10-29 14:40:30 +0000250 if (ncontrol->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
Mark Brown366840d2008-10-29 14:40:30 +0000252 if (strcmp(ncontrol->name, kctl.id.name) != 0)
Takashi Iwaibb009452014-02-04 18:18:16 +0100253 pr_warn("ALSA: Control name '%s' truncated to '%s'\n",
254 ncontrol->name, kctl.id.name);
Mark Brown366840d2008-10-29 14:40:30 +0000255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 kctl.id.index = ncontrol->index;
257 kctl.count = ncontrol->count ? ncontrol->count : 1;
258 access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200259 (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
Takashi Iwaia8d372f2012-07-30 13:47:07 +0200260 SNDRV_CTL_ELEM_ACCESS_VOLATILE|
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200261 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
Clemens Ladischa75d7a42010-02-01 13:29:50 +0100262 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE|
263 SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND|
264 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 kctl.info = ncontrol->info;
266 kctl.get = ncontrol->get;
267 kctl.put = ncontrol->put;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200268 kctl.tlv.p = ncontrol->tlv.p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 kctl.private_value = ncontrol->private_value;
270 kctl.private_data = private_data;
271 return snd_ctl_new(&kctl, access);
272}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200273EXPORT_SYMBOL(snd_ctl_new1);
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275/**
276 * snd_ctl_free_one - release the control instance
277 * @kcontrol: the control instance
278 *
279 * Releases the control instance created via snd_ctl_new()
280 * or snd_ctl_new1().
281 * Don't call this after the control was added to the card.
282 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100283void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 if (kcontrol) {
286 if (kcontrol->private_free)
287 kcontrol->private_free(kcontrol);
288 kfree(kcontrol);
289 }
290}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200291EXPORT_SYMBOL(snd_ctl_free_one);
292
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100293static bool snd_ctl_remove_numid_conflict(struct snd_card *card,
294 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100296 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Lars-Peter Clausenac902c12014-06-18 13:32:34 +0200298 /* Make sure that the ids assigned to the control do not wrap around */
299 if (card->last_numid >= UINT_MAX - count)
300 card->last_numid = 0;
301
Johannes Berg9244b2c2006-10-05 16:02:22 +0200302 list_for_each_entry(kctl, &card->controls, list) {
Clemens Ladisch7c733582011-03-07 13:22:50 +0100303 if (kctl->id.numid < card->last_numid + 1 + count &&
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100304 kctl->id.numid + kctl->count > card->last_numid + 1) {
305 card->last_numid = kctl->id.numid + kctl->count - 1;
306 return true;
307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100309 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100312static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100314 unsigned int iter = 100000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100316 while (snd_ctl_remove_numid_conflict(card, count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (--iter == 0) {
318 /* this situation is very unlikely */
Takashi Iwaibb009452014-02-04 18:18:16 +0100319 dev_err(card->dev, "unable to allocate new control numid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return -ENOMEM;
321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323 return 0;
324}
325
326/**
327 * snd_ctl_add - add the control instance to the card
328 * @card: the card instance
329 * @kcontrol: the control instance to add
330 *
331 * Adds the control instance created via snd_ctl_new() or
332 * snd_ctl_new1() to the given card. Assigns also an unique
333 * numid used for fast search.
334 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 * It frees automatically the control which cannot be added.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100336 *
337 * Return: Zero if successful, or a negative error code on failure.
338 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100340int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100342 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 unsigned int idx;
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200344 unsigned int count;
Takashi Iwaic6077b32006-03-21 16:07:13 +0100345 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100347 if (! kcontrol)
Takashi Iwaic6077b32006-03-21 16:07:13 +0100348 return err;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200349 if (snd_BUG_ON(!card || !kcontrol->info))
350 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 id = kcontrol->id;
Lars-Peter Clausen883a1d42014-06-18 13:32:35 +0200352 if (id.index > UINT_MAX - kcontrol->count)
353 goto error;
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 down_write(&card->controls_rwsem);
356 if (snd_ctl_find_id(card, &id)) {
357 up_write(&card->controls_rwsem);
Takashi Iwaibb009452014-02-04 18:18:16 +0100358 dev_err(card->dev, "control %i:%i:%i:%s:%i is already present\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 id.iface,
360 id.device,
361 id.subdevice,
362 id.name,
363 id.index);
Takashi Iwaic6077b32006-03-21 16:07:13 +0100364 err = -EBUSY;
365 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
368 up_write(&card->controls_rwsem);
Takashi Iwaic6077b32006-03-21 16:07:13 +0100369 err = -ENOMEM;
370 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372 list_add_tail(&kcontrol->list, &card->controls);
373 card->controls_count += kcontrol->count;
374 kcontrol->id.numid = card->last_numid + 1;
375 card->last_numid += kcontrol->count;
Takashi Sakamotod34890c2015-02-08 22:39:44 +0900376 id = kcontrol->id;
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200377 count = kcontrol->count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 up_write(&card->controls_rwsem);
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200379 for (idx = 0; idx < count; idx++, id.index++, id.numid++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
381 return 0;
Takashi Iwaic6077b32006-03-21 16:07:13 +0100382
383 error:
384 snd_ctl_free_one(kcontrol);
385 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200387EXPORT_SYMBOL(snd_ctl_add);
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389/**
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000390 * snd_ctl_replace - replace the control instance of the card
391 * @card: the card instance
392 * @kcontrol: the control instance to replace
393 * @add_on_replace: add the control if not already added
394 *
395 * Replaces the given control. If the given control does not exist
396 * and the add_on_replace flag is set, the control is added. If the
397 * control exists, it is destroyed first.
398 *
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000399 * It frees automatically the control which cannot be added or replaced.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100400 *
401 * Return: Zero if successful, or a negative error code on failure.
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000402 */
403int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol,
404 bool add_on_replace)
405{
406 struct snd_ctl_elem_id id;
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200407 unsigned int count;
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000408 unsigned int idx;
409 struct snd_kcontrol *old;
410 int ret;
411
412 if (!kcontrol)
413 return -EINVAL;
414 if (snd_BUG_ON(!card || !kcontrol->info)) {
415 ret = -EINVAL;
416 goto error;
417 }
418 id = kcontrol->id;
419 down_write(&card->controls_rwsem);
420 old = snd_ctl_find_id(card, &id);
421 if (!old) {
422 if (add_on_replace)
423 goto add;
424 up_write(&card->controls_rwsem);
425 ret = -EINVAL;
426 goto error;
427 }
428 ret = snd_ctl_remove(card, old);
429 if (ret < 0) {
430 up_write(&card->controls_rwsem);
431 goto error;
432 }
433add:
434 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
435 up_write(&card->controls_rwsem);
436 ret = -ENOMEM;
437 goto error;
438 }
439 list_add_tail(&kcontrol->list, &card->controls);
440 card->controls_count += kcontrol->count;
441 kcontrol->id.numid = card->last_numid + 1;
442 card->last_numid += kcontrol->count;
Takashi Sakamotoe6ff3842015-02-10 00:01:45 +0900443 id = kcontrol->id;
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200444 count = kcontrol->count;
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000445 up_write(&card->controls_rwsem);
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200446 for (idx = 0; idx < count; idx++, id.index++, id.numid++)
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000447 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
448 return 0;
449
450error:
451 snd_ctl_free_one(kcontrol);
452 return ret;
453}
454EXPORT_SYMBOL(snd_ctl_replace);
455
456/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 * snd_ctl_remove - remove the control from the card and release it
458 * @card: the card instance
459 * @kcontrol: the control instance to remove
460 *
461 * Removes the control from the card and then releases the instance.
462 * You don't need to call snd_ctl_free_one(). You must be in
463 * the write lock - down_write(&card->controls_rwsem).
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100464 *
465 * Return: 0 if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100467int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100469 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 unsigned int idx;
471
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200472 if (snd_BUG_ON(!card || !kcontrol))
473 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 list_del(&kcontrol->list);
475 card->controls_count -= kcontrol->count;
476 id = kcontrol->id;
477 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
478 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
479 snd_ctl_free_one(kcontrol);
480 return 0;
481}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200482EXPORT_SYMBOL(snd_ctl_remove);
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/**
485 * snd_ctl_remove_id - remove the control of the given id and release it
486 * @card: the card instance
487 * @id: the control id to remove
488 *
489 * Finds the control instance with the given id, removes it from the
490 * card list and releases it.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100491 *
492 * Return: 0 if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100494int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100496 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 int ret;
498
499 down_write(&card->controls_rwsem);
500 kctl = snd_ctl_find_id(card, id);
501 if (kctl == NULL) {
502 up_write(&card->controls_rwsem);
503 return -ENOENT;
504 }
505 ret = snd_ctl_remove(card, kctl);
506 up_write(&card->controls_rwsem);
507 return ret;
508}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200509EXPORT_SYMBOL(snd_ctl_remove_id);
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511/**
Clemens Ladischf217ac52009-08-17 12:27:22 +0200512 * snd_ctl_remove_user_ctl - remove and release the unlocked user control
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 * @file: active control handle
514 * @id: the control id to remove
515 *
516 * Finds the control instance with the given id, removes it from the
517 * card list and releases it.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100518 *
519 * Return: 0 if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 */
Clemens Ladischf217ac52009-08-17 12:27:22 +0200521static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
522 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100524 struct snd_card *card = file->card;
525 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 int idx, ret;
527
528 down_write(&card->controls_rwsem);
529 kctl = snd_ctl_find_id(card, id);
530 if (kctl == NULL) {
Clemens Ladisch317b8082009-08-17 12:26:34 +0200531 ret = -ENOENT;
532 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
Clemens Ladisch18dd0aa2009-08-17 12:28:09 +0200534 if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
535 ret = -EINVAL;
536 goto error;
537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 for (idx = 0; idx < kctl->count; idx++)
539 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
Clemens Ladisch317b8082009-08-17 12:26:34 +0200540 ret = -EBUSY;
541 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543 ret = snd_ctl_remove(card, kctl);
Clemens Ladischf217ac52009-08-17 12:27:22 +0200544 if (ret < 0)
545 goto error;
546 card->user_ctl_count--;
Clemens Ladisch317b8082009-08-17 12:26:34 +0200547error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 up_write(&card->controls_rwsem);
549 return ret;
550}
551
552/**
Takashi Iwai3cbdd752008-08-29 16:09:01 +0200553 * snd_ctl_activate_id - activate/inactivate the control of the given id
554 * @card: the card instance
555 * @id: the control id to activate/inactivate
556 * @active: non-zero to activate
557 *
558 * Finds the control instance with the given id, and activate or
559 * inactivate the control together with notification, if changed.
560 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100561 * Return: 0 if unchanged, 1 if changed, or a negative error code on failure.
Takashi Iwai3cbdd752008-08-29 16:09:01 +0200562 */
563int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
564 int active)
565{
566 struct snd_kcontrol *kctl;
567 struct snd_kcontrol_volatile *vd;
568 unsigned int index_offset;
569 int ret;
570
571 down_write(&card->controls_rwsem);
572 kctl = snd_ctl_find_id(card, id);
573 if (kctl == NULL) {
574 ret = -ENOENT;
575 goto unlock;
576 }
Lars-Peter Clausen31584ed2014-11-07 14:12:34 +0100577 index_offset = snd_ctl_get_ioff(kctl, id);
Takashi Iwai3cbdd752008-08-29 16:09:01 +0200578 vd = &kctl->vd[index_offset];
579 ret = 0;
580 if (active) {
581 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
582 goto unlock;
583 vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
584 } else {
585 if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)
586 goto unlock;
587 vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
588 }
589 ret = 1;
590 unlock:
591 up_write(&card->controls_rwsem);
592 if (ret > 0)
593 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, id);
594 return ret;
595}
596EXPORT_SYMBOL_GPL(snd_ctl_activate_id);
597
598/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 * snd_ctl_rename_id - replace the id of a control on the card
600 * @card: the card instance
601 * @src_id: the old id
602 * @dst_id: the new id
603 *
604 * Finds the control with the old id from the card, and replaces the
605 * id with the new one.
606 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100607 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100609int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
610 struct snd_ctl_elem_id *dst_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100612 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 down_write(&card->controls_rwsem);
615 kctl = snd_ctl_find_id(card, src_id);
616 if (kctl == NULL) {
617 up_write(&card->controls_rwsem);
618 return -ENOENT;
619 }
620 kctl->id = *dst_id;
621 kctl->id.numid = card->last_numid + 1;
622 card->last_numid += kctl->count;
623 up_write(&card->controls_rwsem);
624 return 0;
625}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200626EXPORT_SYMBOL(snd_ctl_rename_id);
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628/**
629 * snd_ctl_find_numid - find the control instance with the given number-id
630 * @card: the card instance
631 * @numid: the number-id to search
632 *
633 * Finds the control instance with the given number-id from the card.
634 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 * The caller must down card->controls_rwsem before calling this function
636 * (if the race condition can happen).
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100637 *
638 * Return: The pointer of the instance if found, or %NULL if not.
639 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100641struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100643 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200645 if (snd_BUG_ON(!card || !numid))
646 return NULL;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200647 list_for_each_entry(kctl, &card->controls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
649 return kctl;
650 }
651 return NULL;
652}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200653EXPORT_SYMBOL(snd_ctl_find_numid);
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655/**
656 * snd_ctl_find_id - find the control instance with the given id
657 * @card: the card instance
658 * @id: the id to search
659 *
660 * Finds the control instance with the given id from the card.
661 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 * The caller must down card->controls_rwsem before calling this function
663 * (if the race condition can happen).
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100664 *
665 * Return: The pointer of the instance if found, or %NULL if not.
666 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100668struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
669 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100671 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200673 if (snd_BUG_ON(!card || !id))
674 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (id->numid != 0)
676 return snd_ctl_find_numid(card, id->numid);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200677 list_for_each_entry(kctl, &card->controls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (kctl->id.iface != id->iface)
679 continue;
680 if (kctl->id.device != id->device)
681 continue;
682 if (kctl->id.subdevice != id->subdevice)
683 continue;
684 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
685 continue;
686 if (kctl->id.index > id->index)
687 continue;
688 if (kctl->id.index + kctl->count <= id->index)
689 continue;
690 return kctl;
691 }
692 return NULL;
693}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200694EXPORT_SYMBOL(snd_ctl_find_id);
695
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100696static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 unsigned int cmd, void __user *arg)
698{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100699 struct snd_ctl_card_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Takashi Iwaica2c0962005-09-09 14:20:23 +0200701 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (! info)
703 return -ENOMEM;
704 down_read(&snd_ioctl_rwsem);
705 info->card = card->number;
706 strlcpy(info->id, card->id, sizeof(info->id));
707 strlcpy(info->driver, card->driver, sizeof(info->driver));
708 strlcpy(info->name, card->shortname, sizeof(info->name));
709 strlcpy(info->longname, card->longname, sizeof(info->longname));
710 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
711 strlcpy(info->components, card->components, sizeof(info->components));
712 up_read(&snd_ioctl_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100713 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 kfree(info);
715 return -EFAULT;
716 }
717 kfree(info);
718 return 0;
719}
720
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100721static int snd_ctl_elem_list(struct snd_card *card,
722 struct snd_ctl_elem_list __user *_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 struct list_head *plist;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100725 struct snd_ctl_elem_list list;
726 struct snd_kcontrol *kctl;
727 struct snd_ctl_elem_id *dst, *id;
Luca Tettamanti78fa2c42011-05-25 22:43:27 +0200728 unsigned int offset, space, jidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 if (copy_from_user(&list, _list, sizeof(list)))
731 return -EFAULT;
732 offset = list.offset;
733 space = list.space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /* try limit maximum space */
735 if (space > 16384)
736 return -ENOMEM;
737 if (space > 0) {
738 /* allocate temporary buffer for atomic operation */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100739 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (dst == NULL)
741 return -ENOMEM;
742 down_read(&card->controls_rwsem);
743 list.count = card->controls_count;
744 plist = card->controls.next;
745 while (plist != &card->controls) {
746 if (offset == 0)
747 break;
748 kctl = snd_kcontrol(plist);
749 if (offset < kctl->count)
750 break;
751 offset -= kctl->count;
752 plist = plist->next;
753 }
754 list.used = 0;
755 id = dst;
756 while (space > 0 && plist != &card->controls) {
757 kctl = snd_kcontrol(plist);
758 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
759 snd_ctl_build_ioff(id, kctl, jidx);
760 id++;
761 space--;
762 list.used++;
763 }
764 plist = plist->next;
765 offset = 0;
766 }
767 up_read(&card->controls_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100768 if (list.used > 0 &&
769 copy_to_user(list.pids, dst,
770 list.used * sizeof(struct snd_ctl_elem_id))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 vfree(dst);
772 return -EFAULT;
773 }
774 vfree(dst);
775 } else {
776 down_read(&card->controls_rwsem);
777 list.count = card->controls_count;
778 up_read(&card->controls_rwsem);
779 }
780 if (copy_to_user(_list, &list, sizeof(list)))
781 return -EFAULT;
782 return 0;
783}
784
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100785static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
786 struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100788 struct snd_card *card = ctl->card;
789 struct snd_kcontrol *kctl;
790 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 unsigned int index_offset;
792 int result;
793
794 down_read(&card->controls_rwsem);
795 kctl = snd_ctl_find_id(card, &info->id);
796 if (kctl == NULL) {
797 up_read(&card->controls_rwsem);
798 return -ENOENT;
799 }
800#ifdef CONFIG_SND_DEBUG
801 info->access = 0;
802#endif
803 result = kctl->info(kctl, info);
804 if (result >= 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200805 snd_BUG_ON(info->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 index_offset = snd_ctl_get_ioff(kctl, &info->id);
807 vd = &kctl->vd[index_offset];
808 snd_ctl_build_ioff(&info->id, kctl, index_offset);
809 info->access = vd->access;
810 if (vd->owner) {
811 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
812 if (vd->owner == ctl)
813 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
Clemens Ladisch25d27ed2009-11-02 09:35:44 +0100814 info->owner = pid_vnr(vd->owner->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 } else {
816 info->owner = -1;
817 }
818 }
819 up_read(&card->controls_rwsem);
820 return result;
821}
822
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100823static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
824 struct snd_ctl_elem_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100826 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 int result;
828
829 if (copy_from_user(&info, _info, sizeof(info)))
830 return -EFAULT;
Giuliano Pochini64649402006-03-13 14:11:11 +0100831 snd_power_lock(ctl->card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200832 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100833 if (result >= 0)
834 result = snd_ctl_elem_info(ctl, &info);
835 snd_power_unlock(ctl->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (result >= 0)
837 if (copy_to_user(_info, &info, sizeof(info)))
838 return -EFAULT;
839 return result;
840}
841
Takashi Iwaid3bd67c2008-06-12 18:17:26 +0200842static int snd_ctl_elem_read(struct snd_card *card,
843 struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100845 struct snd_kcontrol *kctl;
846 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 unsigned int index_offset;
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100848 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 down_read(&card->controls_rwsem);
851 kctl = snd_ctl_find_id(card, &control->id);
852 if (kctl == NULL) {
853 result = -ENOENT;
854 } else {
855 index_offset = snd_ctl_get_ioff(kctl, &control->id);
856 vd = &kctl->vd[index_offset];
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100857 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) &&
858 kctl->get != NULL) {
859 snd_ctl_build_ioff(&control->id, kctl, index_offset);
860 result = kctl->get(kctl, control);
861 } else
862 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864 up_read(&card->controls_rwsem);
865 return result;
866}
867
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100868static int snd_ctl_elem_read_user(struct snd_card *card,
869 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100871 struct snd_ctl_elem_value *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 int result;
Li Zefanef44a1e2009-04-10 09:43:08 +0800873
874 control = memdup_user(_control, sizeof(*control));
875 if (IS_ERR(control))
876 return PTR_ERR(control);
877
Giuliano Pochini64649402006-03-13 14:11:11 +0100878 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200879 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100880 if (result >= 0)
881 result = snd_ctl_elem_read(card, control);
882 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (result >= 0)
884 if (copy_to_user(_control, control, sizeof(*control)))
885 result = -EFAULT;
886 kfree(control);
887 return result;
888}
889
Takashi Iwaid3bd67c2008-06-12 18:17:26 +0200890static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
891 struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100893 struct snd_kcontrol *kctl;
894 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 unsigned int index_offset;
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100896 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 down_read(&card->controls_rwsem);
899 kctl = snd_ctl_find_id(card, &control->id);
900 if (kctl == NULL) {
901 result = -ENOENT;
902 } else {
903 index_offset = snd_ctl_get_ioff(kctl, &control->id);
904 vd = &kctl->vd[index_offset];
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100905 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
906 kctl->put == NULL ||
907 (file && vd->owner && vd->owner != file)) {
908 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 } else {
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100910 snd_ctl_build_ioff(&control->id, kctl, index_offset);
911 result = kctl->put(kctl, control);
912 }
913 if (result > 0) {
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200914 struct snd_ctl_elem_id id = control->id;
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100915 up_read(&card->controls_rwsem);
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200916 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &id);
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100917 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
919 }
920 up_read(&card->controls_rwsem);
921 return result;
922}
923
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100924static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
925 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100927 struct snd_ctl_elem_value *control;
Giuliano Pochini64649402006-03-13 14:11:11 +0100928 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 int result;
930
Li Zefanef44a1e2009-04-10 09:43:08 +0800931 control = memdup_user(_control, sizeof(*control));
932 if (IS_ERR(control))
933 return PTR_ERR(control);
934
Giuliano Pochini64649402006-03-13 14:11:11 +0100935 card = file->card;
936 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200937 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100938 if (result >= 0)
939 result = snd_ctl_elem_write(card, file, control);
940 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (result >= 0)
942 if (copy_to_user(_control, control, sizeof(*control)))
943 result = -EFAULT;
944 kfree(control);
945 return result;
946}
947
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100948static int snd_ctl_elem_lock(struct snd_ctl_file *file,
949 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100951 struct snd_card *card = file->card;
952 struct snd_ctl_elem_id id;
953 struct snd_kcontrol *kctl;
954 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 int result;
956
957 if (copy_from_user(&id, _id, sizeof(id)))
958 return -EFAULT;
959 down_write(&card->controls_rwsem);
960 kctl = snd_ctl_find_id(card, &id);
961 if (kctl == NULL) {
962 result = -ENOENT;
963 } else {
964 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
965 if (vd->owner != NULL)
966 result = -EBUSY;
967 else {
968 vd->owner = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 result = 0;
970 }
971 }
972 up_write(&card->controls_rwsem);
973 return result;
974}
975
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100976static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
977 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100979 struct snd_card *card = file->card;
980 struct snd_ctl_elem_id id;
981 struct snd_kcontrol *kctl;
982 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 int result;
984
985 if (copy_from_user(&id, _id, sizeof(id)))
986 return -EFAULT;
987 down_write(&card->controls_rwsem);
988 kctl = snd_ctl_find_id(card, &id);
989 if (kctl == NULL) {
990 result = -ENOENT;
991 } else {
992 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
993 if (vd->owner == NULL)
994 result = -EINVAL;
995 else if (vd->owner != file)
996 result = -EPERM;
997 else {
998 vd->owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 result = 0;
1000 }
1001 }
1002 up_write(&card->controls_rwsem);
1003 return result;
1004}
1005
1006struct user_element {
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001007 struct snd_ctl_elem_info info;
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001008 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 void *elem_data; /* element data */
1010 unsigned long elem_data_size; /* size of element data in bytes */
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001011 void *tlv_data; /* TLV data */
1012 unsigned long tlv_data_size; /* TLV data size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 void *priv_data; /* private data (like strings for enumerated type) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014};
1015
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001016static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
1017 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 struct user_element *ue = kcontrol->private_data;
1020
1021 *uinfo = ue->info;
1022 return 0;
1023}
1024
Clemens Ladisch8d448162011-10-07 22:38:59 +02001025static int snd_ctl_elem_user_enum_info(struct snd_kcontrol *kcontrol,
1026 struct snd_ctl_elem_info *uinfo)
1027{
1028 struct user_element *ue = kcontrol->private_data;
1029 const char *names;
1030 unsigned int item;
1031
1032 item = uinfo->value.enumerated.item;
1033
1034 *uinfo = ue->info;
1035
1036 item = min(item, uinfo->value.enumerated.items - 1);
1037 uinfo->value.enumerated.item = item;
1038
1039 names = ue->priv_data;
1040 for (; item > 0; --item)
1041 names += strlen(names) + 1;
1042 strcpy(uinfo->value.enumerated.name, names);
1043
1044 return 0;
1045}
1046
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001047static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
1048 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
1050 struct user_element *ue = kcontrol->private_data;
1051
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001052 mutex_lock(&ue->card->user_ctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001054 mutex_unlock(&ue->card->user_ctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return 0;
1056}
1057
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001058static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
1059 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 int change;
1062 struct user_element *ue = kcontrol->private_data;
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001063
1064 mutex_lock(&ue->card->user_ctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
1066 if (change)
1067 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001068 mutex_unlock(&ue->card->user_ctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return change;
1070}
1071
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001072static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol,
1073 int op_flag,
1074 unsigned int size,
1075 unsigned int __user *tlv)
1076{
1077 struct user_element *ue = kcontrol->private_data;
1078 int change = 0;
1079 void *new_data;
1080
1081 if (op_flag > 0) {
1082 if (size > 1024 * 128) /* sane value */
1083 return -EINVAL;
Li Zefanef44a1e2009-04-10 09:43:08 +08001084
1085 new_data = memdup_user(tlv, size);
1086 if (IS_ERR(new_data))
1087 return PTR_ERR(new_data);
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001088 mutex_lock(&ue->card->user_ctl_lock);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001089 change = ue->tlv_data_size != size;
1090 if (!change)
1091 change = memcmp(ue->tlv_data, new_data, size);
1092 kfree(ue->tlv_data);
1093 ue->tlv_data = new_data;
1094 ue->tlv_data_size = size;
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001095 mutex_unlock(&ue->card->user_ctl_lock);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001096 } else {
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001097 int ret = 0;
1098
1099 mutex_lock(&ue->card->user_ctl_lock);
1100 if (!ue->tlv_data_size || !ue->tlv_data) {
1101 ret = -ENXIO;
1102 goto err_unlock;
1103 }
1104 if (size < ue->tlv_data_size) {
1105 ret = -ENOSPC;
1106 goto err_unlock;
1107 }
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001108 if (copy_to_user(tlv, ue->tlv_data, ue->tlv_data_size))
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001109 ret = -EFAULT;
1110err_unlock:
1111 mutex_unlock(&ue->card->user_ctl_lock);
1112 if (ret)
1113 return ret;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001114 }
1115 return change;
1116}
1117
Clemens Ladisch8d448162011-10-07 22:38:59 +02001118static int snd_ctl_elem_init_enum_names(struct user_element *ue)
1119{
1120 char *names, *p;
1121 size_t buf_len, name_len;
1122 unsigned int i;
Olof Johansson447c6f92011-11-05 22:51:54 +01001123 const uintptr_t user_ptrval = ue->info.value.enumerated.names_ptr;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001124
1125 if (ue->info.value.enumerated.names_length > 64 * 1024)
1126 return -EINVAL;
1127
Olof Johansson447c6f92011-11-05 22:51:54 +01001128 names = memdup_user((const void __user *)user_ptrval,
Clemens Ladisch8d448162011-10-07 22:38:59 +02001129 ue->info.value.enumerated.names_length);
1130 if (IS_ERR(names))
1131 return PTR_ERR(names);
1132
1133 /* check that there are enough valid names */
1134 buf_len = ue->info.value.enumerated.names_length;
1135 p = names;
1136 for (i = 0; i < ue->info.value.enumerated.items; ++i) {
1137 name_len = strnlen(p, buf_len);
1138 if (name_len == 0 || name_len >= 64 || name_len == buf_len) {
1139 kfree(names);
1140 return -EINVAL;
1141 }
1142 p += name_len + 1;
1143 buf_len -= name_len + 1;
1144 }
1145
1146 ue->priv_data = names;
1147 ue->info.value.enumerated.names_ptr = 0;
1148
1149 return 0;
1150}
1151
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001152static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001154 struct user_element *ue = kcontrol->private_data;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001155
1156 kfree(ue->tlv_data);
1157 kfree(ue->priv_data);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001158 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001161static int snd_ctl_elem_add(struct snd_ctl_file *file,
1162 struct snd_ctl_elem_info *info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Takashi Sakamoto4ed56662015-03-10 22:13:30 +09001164 /* The capacity of struct snd_ctl_elem_value.value.*/
1165 static const unsigned int value_sizes[] = {
1166 [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = sizeof(long),
1167 [SNDRV_CTL_ELEM_TYPE_INTEGER] = sizeof(long),
1168 [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = sizeof(unsigned int),
1169 [SNDRV_CTL_ELEM_TYPE_BYTES] = sizeof(unsigned char),
1170 [SNDRV_CTL_ELEM_TYPE_IEC958] = sizeof(struct snd_aes_iec958),
1171 [SNDRV_CTL_ELEM_TYPE_INTEGER64] = sizeof(long long),
1172 };
1173 static const unsigned int max_value_counts[] = {
1174 [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = 128,
1175 [SNDRV_CTL_ELEM_TYPE_INTEGER] = 128,
1176 [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = 128,
1177 [SNDRV_CTL_ELEM_TYPE_BYTES] = 512,
1178 [SNDRV_CTL_ELEM_TYPE_IEC958] = 1,
1179 [SNDRV_CTL_ELEM_TYPE_INTEGER64] = 64,
1180 };
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001181 struct snd_card *card = file->card;
1182 struct snd_kcontrol kctl, *_kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 unsigned int access;
1184 long private_size;
1185 struct user_element *ue;
1186 int idx, err;
Lu Guanqun983929c2011-08-24 11:12:34 +08001187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001189 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001190 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
1191 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 info->id.numid = 0;
1193 memset(&kctl, 0, sizeof(kctl));
Lars-Peter Clausen82262a462014-06-18 13:32:32 +02001194
1195 if (replace) {
1196 err = snd_ctl_remove_user_ctl(file, &info->id);
1197 if (err)
1198 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
Lars-Peter Clausen82262a462014-06-18 13:32:32 +02001200
1201 if (card->user_ctl_count >= MAX_USER_CONTROLS)
1202 return -ENOMEM;
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 memcpy(&kctl.id, &info->id, sizeof(info->id));
1205 kctl.count = info->owner ? info->owner : 1;
1206 access |= SNDRV_CTL_ELEM_ACCESS_USER;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001207 if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED)
1208 kctl.info = snd_ctl_elem_user_enum_info;
1209 else
1210 kctl.info = snd_ctl_elem_user_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1212 kctl.get = snd_ctl_elem_user_get;
1213 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1214 kctl.put = snd_ctl_elem_user_put;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001215 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
1216 kctl.tlv.c = snd_ctl_elem_user_tlv;
1217 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1218 }
Takashi Sakamoto4ed56662015-03-10 22:13:30 +09001219
1220 if (info->type < SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
1221 info->type > SNDRV_CTL_ELEM_TYPE_INTEGER64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 return -EINVAL;
Takashi Sakamoto4ed56662015-03-10 22:13:30 +09001223 if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED &&
1224 info->value.enumerated.items == 0)
1225 return -EINVAL;
1226 if (info->count < 1 ||
1227 info->count > max_value_counts[info->type])
1228 return -EINVAL;
1229
1230 private_size = value_sizes[info->type] * info->count;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001231 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (ue == NULL)
1233 return -ENOMEM;
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001234 ue->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 ue->info = *info;
Takashi Iwai86148e82006-08-24 12:36:36 +02001236 ue->info.access = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 ue->elem_data = (char *)ue + sizeof(*ue);
1238 ue->elem_data_size = private_size;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001239 if (ue->info.type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
1240 err = snd_ctl_elem_init_enum_names(ue);
1241 if (err < 0) {
1242 kfree(ue);
1243 return err;
1244 }
1245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 kctl.private_free = snd_ctl_elem_user_free;
1247 _kctl = snd_ctl_new(&kctl, access);
1248 if (_kctl == NULL) {
Clemens Ladisch8d448162011-10-07 22:38:59 +02001249 kfree(ue->priv_data);
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001250 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return -ENOMEM;
1252 }
1253 _kctl->private_data = ue;
1254 for (idx = 0; idx < _kctl->count; idx++)
1255 _kctl->vd[idx].owner = file;
1256 err = snd_ctl_add(card, _kctl);
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001257 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 down_write(&card->controls_rwsem);
1261 card->user_ctl_count++;
1262 up_write(&card->controls_rwsem);
1263
1264 return 0;
1265}
1266
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001267static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1268 struct snd_ctl_elem_info __user *_info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001270 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (copy_from_user(&info, _info, sizeof(info)))
1272 return -EFAULT;
1273 return snd_ctl_elem_add(file, &info, replace);
1274}
1275
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001276static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1277 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001279 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 if (copy_from_user(&id, _id, sizeof(id)))
1282 return -EFAULT;
Clemens Ladischf217ac52009-08-17 12:27:22 +02001283 return snd_ctl_remove_user_ctl(file, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001286static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
1288 int subscribe;
1289 if (get_user(subscribe, ptr))
1290 return -EFAULT;
1291 if (subscribe < 0) {
1292 subscribe = file->subscribed;
1293 if (put_user(subscribe, ptr))
1294 return -EFAULT;
1295 return 0;
1296 }
1297 if (subscribe) {
1298 file->subscribed = 1;
1299 return 0;
1300 } else if (file->subscribed) {
1301 snd_ctl_empty_read_queue(file);
1302 file->subscribed = 0;
1303 }
1304 return 0;
1305}
1306
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001307static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1308 struct snd_ctl_tlv __user *_tlv,
1309 int op_flag)
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001310{
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001311 struct snd_card *card = file->card;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001312 struct snd_ctl_tlv tlv;
1313 struct snd_kcontrol *kctl;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001314 struct snd_kcontrol_volatile *vd;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001315 unsigned int len;
1316 int err = 0;
1317
1318 if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
1319 return -EFAULT;
Clemens Ladisch61236372010-02-01 13:30:56 +01001320 if (tlv.length < sizeof(unsigned int) * 2)
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001321 return -EINVAL;
1322 down_read(&card->controls_rwsem);
1323 kctl = snd_ctl_find_numid(card, tlv.numid);
1324 if (kctl == NULL) {
1325 err = -ENOENT;
1326 goto __kctl_end;
1327 }
1328 if (kctl->tlv.p == NULL) {
1329 err = -ENXIO;
1330 goto __kctl_end;
1331 }
1332 vd = &kctl->vd[tlv.numid - kctl->id.numid];
1333 if ((op_flag == 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) == 0) ||
1334 (op_flag > 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) == 0) ||
1335 (op_flag < 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND) == 0)) {
1336 err = -ENXIO;
1337 goto __kctl_end;
1338 }
1339 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
Dan Carpenterbec145a2009-11-18 10:31:57 +02001340 if (vd->owner != NULL && vd->owner != file) {
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001341 err = -EPERM;
1342 goto __kctl_end;
1343 }
Jeffrin Josebd483d42012-03-07 22:57:39 +05301344 err = kctl->tlv.c(kctl, op_flag, tlv.length, _tlv->tlv);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001345 if (err > 0) {
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +02001346 struct snd_ctl_elem_id id = kctl->id;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001347 up_read(&card->controls_rwsem);
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +02001348 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &id);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001349 return 0;
1350 }
1351 } else {
1352 if (op_flag) {
1353 err = -ENXIO;
1354 goto __kctl_end;
1355 }
1356 len = kctl->tlv.p[1] + 2 * sizeof(unsigned int);
1357 if (tlv.length < len) {
1358 err = -ENOMEM;
1359 goto __kctl_end;
1360 }
1361 if (copy_to_user(_tlv->tlv, kctl->tlv.p, len))
1362 err = -EFAULT;
1363 }
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001364 __kctl_end:
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001365 up_read(&card->controls_rwsem);
1366 return err;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001367}
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1370{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001371 struct snd_ctl_file *ctl;
1372 struct snd_card *card;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001373 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 void __user *argp = (void __user *)arg;
1375 int __user *ip = argp;
1376 int err;
1377
1378 ctl = file->private_data;
1379 card = ctl->card;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001380 if (snd_BUG_ON(!card))
1381 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 switch (cmd) {
1383 case SNDRV_CTL_IOCTL_PVERSION:
1384 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1385 case SNDRV_CTL_IOCTL_CARD_INFO:
1386 return snd_ctl_card_info(card, ctl, cmd, argp);
1387 case SNDRV_CTL_IOCTL_ELEM_LIST:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001388 return snd_ctl_elem_list(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 case SNDRV_CTL_IOCTL_ELEM_INFO:
1390 return snd_ctl_elem_info_user(ctl, argp);
1391 case SNDRV_CTL_IOCTL_ELEM_READ:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001392 return snd_ctl_elem_read_user(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1394 return snd_ctl_elem_write_user(ctl, argp);
1395 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1396 return snd_ctl_elem_lock(ctl, argp);
1397 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1398 return snd_ctl_elem_unlock(ctl, argp);
1399 case SNDRV_CTL_IOCTL_ELEM_ADD:
1400 return snd_ctl_elem_add_user(ctl, argp, 0);
1401 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1402 return snd_ctl_elem_add_user(ctl, argp, 1);
1403 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1404 return snd_ctl_elem_remove(ctl, argp);
1405 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1406 return snd_ctl_subscribe_events(ctl, ip);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001407 case SNDRV_CTL_IOCTL_TLV_READ:
Takashi Iwai0cea76f2014-07-15 16:31:01 +02001408 return snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_READ);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001409 case SNDRV_CTL_IOCTL_TLV_WRITE:
Takashi Iwai0cea76f2014-07-15 16:31:01 +02001410 return snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_WRITE);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001411 case SNDRV_CTL_IOCTL_TLV_COMMAND:
Takashi Iwai0cea76f2014-07-15 16:31:01 +02001412 return snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 case SNDRV_CTL_IOCTL_POWER:
Takashi Iwaia381a7a2005-11-17 15:55:49 +01001414 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 case SNDRV_CTL_IOCTL_POWER_STATE:
1416#ifdef CONFIG_PM
1417 return put_user(card->power_state, ip) ? -EFAULT : 0;
1418#else
1419 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1420#endif
1421 }
1422 down_read(&snd_ioctl_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001423 list_for_each_entry(p, &snd_control_ioctls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 err = p->fioctl(card, ctl, cmd, arg);
1425 if (err != -ENOIOCTLCMD) {
1426 up_read(&snd_ioctl_rwsem);
1427 return err;
1428 }
1429 }
1430 up_read(&snd_ioctl_rwsem);
Takashi Iwaibb009452014-02-04 18:18:16 +01001431 dev_dbg(card->dev, "unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return -ENOTTY;
1433}
1434
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001435static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1436 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001438 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 int err = 0;
1440 ssize_t result = 0;
1441
1442 ctl = file->private_data;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001443 if (snd_BUG_ON(!ctl || !ctl->card))
1444 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (!ctl->subscribed)
1446 return -EBADFD;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001447 if (count < sizeof(struct snd_ctl_event))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return -EINVAL;
1449 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001450 while (count >= sizeof(struct snd_ctl_event)) {
1451 struct snd_ctl_event ev;
1452 struct snd_kctl_event *kev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 while (list_empty(&ctl->events)) {
1454 wait_queue_t wait;
1455 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1456 err = -EAGAIN;
1457 goto __end_lock;
1458 }
1459 init_waitqueue_entry(&wait, current);
1460 add_wait_queue(&ctl->change_sleep, &wait);
1461 set_current_state(TASK_INTERRUPTIBLE);
1462 spin_unlock_irq(&ctl->read_lock);
1463 schedule();
1464 remove_wait_queue(&ctl->change_sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001465 if (ctl->card->shutdown)
1466 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (signal_pending(current))
Adrian Bunk0e5d7202006-11-07 13:42:54 +01001468 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 spin_lock_irq(&ctl->read_lock);
1470 }
1471 kev = snd_kctl_event(ctl->events.next);
1472 ev.type = SNDRV_CTL_EVENT_ELEM;
1473 ev.data.elem.mask = kev->mask;
1474 ev.data.elem.id = kev->id;
1475 list_del(&kev->list);
1476 spin_unlock_irq(&ctl->read_lock);
1477 kfree(kev);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001478 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 err = -EFAULT;
1480 goto __end;
1481 }
1482 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001483 buffer += sizeof(struct snd_ctl_event);
1484 count -= sizeof(struct snd_ctl_event);
1485 result += sizeof(struct snd_ctl_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
1487 __end_lock:
1488 spin_unlock_irq(&ctl->read_lock);
1489 __end:
1490 return result > 0 ? result : err;
1491}
1492
1493static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1494{
1495 unsigned int mask;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001496 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 ctl = file->private_data;
1499 if (!ctl->subscribed)
1500 return 0;
1501 poll_wait(file, &ctl->change_sleep, wait);
1502
1503 mask = 0;
1504 if (!list_empty(&ctl->events))
1505 mask |= POLLIN | POLLRDNORM;
1506
1507 return mask;
1508}
1509
1510/*
1511 * register the device-specific control-ioctls.
1512 * called from each device manager like pcm.c, hwdep.c, etc.
1513 */
1514static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1515{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001516 struct snd_kctl_ioctl *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001518 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (pn == NULL)
1520 return -ENOMEM;
1521 pn->fioctl = fcn;
1522 down_write(&snd_ioctl_rwsem);
1523 list_add_tail(&pn->list, lists);
1524 up_write(&snd_ioctl_rwsem);
1525 return 0;
1526}
1527
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001528/**
1529 * snd_ctl_register_ioctl - register the device-specific control-ioctls
1530 * @fcn: ioctl callback function
1531 *
1532 * called from each device manager like pcm.c, hwdep.c, etc.
1533 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1535{
1536 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1537}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001538EXPORT_SYMBOL(snd_ctl_register_ioctl);
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540#ifdef CONFIG_COMPAT
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001541/**
1542 * snd_ctl_register_ioctl_compat - register the device-specific 32bit compat
1543 * control-ioctls
1544 * @fcn: ioctl callback function
1545 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1547{
1548 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1549}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001550EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551#endif
1552
1553/*
1554 * de-register the device-specific control-ioctls.
1555 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001556static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1557 struct list_head *lists)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001559 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001561 if (snd_BUG_ON(!fcn))
1562 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 down_write(&snd_ioctl_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001564 list_for_each_entry(p, lists, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (p->fioctl == fcn) {
1566 list_del(&p->list);
1567 up_write(&snd_ioctl_rwsem);
1568 kfree(p);
1569 return 0;
1570 }
1571 }
1572 up_write(&snd_ioctl_rwsem);
1573 snd_BUG();
1574 return -EINVAL;
1575}
1576
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001577/**
1578 * snd_ctl_unregister_ioctl - de-register the device-specific control-ioctls
1579 * @fcn: ioctl callback function to unregister
1580 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1582{
1583 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1584}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001585EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587#ifdef CONFIG_COMPAT
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001588/**
1589 * snd_ctl_unregister_ioctl - de-register the device-specific compat 32bit
1590 * control-ioctls
1591 * @fcn: ioctl callback function to unregister
1592 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1594{
1595 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1596}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001597EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598#endif
1599
1600static int snd_ctl_fasync(int fd, struct file * file, int on)
1601{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001602 struct snd_ctl_file *ctl;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 ctl = file->private_data;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001605 return fasync_helper(fd, file, on, &ctl->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
Takashi Iwai23c18d42014-02-19 14:30:29 +01001608/* return the preferred subdevice number if already assigned;
1609 * otherwise return -1
1610 */
1611int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type)
1612{
1613 struct snd_ctl_file *kctl;
1614 int subdevice = -1;
1615
1616 read_lock(&card->ctl_files_rwlock);
1617 list_for_each_entry(kctl, &card->ctl_files, list) {
1618 if (kctl->pid == task_pid(current)) {
1619 subdevice = kctl->preferred_subdevice[type];
1620 if (subdevice != -1)
1621 break;
1622 }
1623 }
1624 read_unlock(&card->ctl_files_rwlock);
1625 return subdevice;
1626}
1627EXPORT_SYMBOL_GPL(snd_ctl_get_preferred_subdevice);
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629/*
1630 * ioctl32 compat
1631 */
1632#ifdef CONFIG_COMPAT
1633#include "control_compat.c"
1634#else
1635#define snd_ctl_ioctl_compat NULL
1636#endif
1637
1638/*
1639 * INIT PART
1640 */
1641
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001642static const struct file_operations snd_ctl_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
1644 .owner = THIS_MODULE,
1645 .read = snd_ctl_read,
1646 .open = snd_ctl_open,
1647 .release = snd_ctl_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02001648 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 .poll = snd_ctl_poll,
1650 .unlocked_ioctl = snd_ctl_ioctl,
1651 .compat_ioctl = snd_ctl_ioctl_compat,
1652 .fasync = snd_ctl_fasync,
1653};
1654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655/*
1656 * registration of the control device
1657 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001658static int snd_ctl_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001660 struct snd_card *card = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Takashi Iwai40a4b262015-01-30 08:34:58 +01001662 return snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1663 &snd_ctl_f_ops, card, &card->ctl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
1666/*
1667 * disconnection of the control device
1668 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001669static int snd_ctl_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001671 struct snd_card *card = device->device_data;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001672 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Takashi Iwaid8009882008-09-07 12:51:13 +02001674 read_lock(&card->ctl_files_rwlock);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001675 list_for_each_entry(ctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 wake_up(&ctl->change_sleep);
1677 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1678 }
Takashi Iwaid8009882008-09-07 12:51:13 +02001679 read_unlock(&card->ctl_files_rwlock);
Takashi Iwaic4614822006-06-23 14:38:23 +02001680
Takashi Iwai40a4b262015-01-30 08:34:58 +01001681 return snd_unregister_device(&card->ctl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682}
1683
1684/*
1685 * free all controls
1686 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001687static int snd_ctl_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001689 struct snd_card *card = device->device_data;
1690 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 down_write(&card->controls_rwsem);
1693 while (!list_empty(&card->controls)) {
1694 control = snd_kcontrol(card->controls.next);
1695 snd_ctl_remove(card, control);
1696 }
1697 up_write(&card->controls_rwsem);
Takashi Iwai0fcd9f42015-01-29 16:41:27 +01001698 put_device(&card->ctl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return 0;
1700}
1701
1702/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 * create control core:
1704 * called from init.c
1705 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001706int snd_ctl_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001708 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 .dev_free = snd_ctl_dev_free,
1710 .dev_register = snd_ctl_dev_register,
1711 .dev_disconnect = snd_ctl_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 };
Takashi Iwai0fcd9f42015-01-29 16:41:27 +01001713 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001715 if (snd_BUG_ON(!card))
1716 return -ENXIO;
Takashi Iwai0fcd9f42015-01-29 16:41:27 +01001717 if (snd_BUG_ON(card->number < 0 || card->number >= SNDRV_CARDS))
1718 return -ENXIO;
1719
1720 snd_device_initialize(&card->ctl_dev, card);
1721 dev_set_name(&card->ctl_dev, "controlC%d", card->number);
1722
1723 err = snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
1724 if (err < 0)
1725 put_device(&card->ctl_dev);
1726 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001728
1729/*
Clemens Ladisch96007322011-01-10 16:25:44 +01001730 * Frequently used control callbacks/helpers
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001731 */
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001732
1733/**
1734 * snd_ctl_boolean_mono_info - Helper function for a standard boolean info
1735 * callback with a mono channel
1736 * @kcontrol: the kcontrol instance
1737 * @uinfo: info to store
1738 *
1739 * This is a function that can be used as info callback for a standard
1740 * boolean control with a single mono channel.
1741 */
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001742int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
1743 struct snd_ctl_elem_info *uinfo)
1744{
1745 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1746 uinfo->count = 1;
1747 uinfo->value.integer.min = 0;
1748 uinfo->value.integer.max = 1;
1749 return 0;
1750}
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001751EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
1752
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001753/**
1754 * snd_ctl_boolean_stereo_info - Helper function for a standard boolean info
1755 * callback with stereo two channels
1756 * @kcontrol: the kcontrol instance
1757 * @uinfo: info to store
1758 *
1759 * This is a function that can be used as info callback for a standard
1760 * boolean control with stereo two channels.
1761 */
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001762int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
1763 struct snd_ctl_elem_info *uinfo)
1764{
1765 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1766 uinfo->count = 2;
1767 uinfo->value.integer.min = 0;
1768 uinfo->value.integer.max = 1;
1769 return 0;
1770}
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001771EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
Clemens Ladisch96007322011-01-10 16:25:44 +01001772
1773/**
1774 * snd_ctl_enum_info - fills the info structure for an enumerated control
1775 * @info: the structure to be filled
1776 * @channels: the number of the control's channels; often one
1777 * @items: the number of control values; also the size of @names
1778 * @names: an array containing the names of all control values
1779 *
1780 * Sets all required fields in @info to their appropriate values.
1781 * If the control's accessibility is not the default (readable and writable),
1782 * the caller has to fill @info->access.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001783 *
1784 * Return: Zero.
Clemens Ladisch96007322011-01-10 16:25:44 +01001785 */
1786int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
1787 unsigned int items, const char *const names[])
1788{
1789 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1790 info->count = channels;
1791 info->value.enumerated.items = items;
Takashi Iwaia7e6fb92014-10-20 18:08:50 +02001792 if (!items)
1793 return 0;
Clemens Ladisch96007322011-01-10 16:25:44 +01001794 if (info->value.enumerated.item >= items)
1795 info->value.enumerated.item = items - 1;
Takashi Iwaidf803e12014-10-20 18:07:21 +02001796 WARN(strlen(names[info->value.enumerated.item]) >= sizeof(info->value.enumerated.name),
1797 "ALSA: too long item name '%s'\n",
1798 names[info->value.enumerated.item]);
Clemens Ladisch96007322011-01-10 16:25:44 +01001799 strlcpy(info->value.enumerated.name,
1800 names[info->value.enumerated.item],
1801 sizeof(info->value.enumerated.name));
1802 return 0;
1803}
1804EXPORT_SYMBOL(snd_ctl_enum_info);