blob: 8aae6eaa3564f773022db871abc32a1a46842a98 [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;
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200443 count = kcontrol->count;
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000444 up_write(&card->controls_rwsem);
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200445 for (idx = 0; idx < count; idx++, id.index++, id.numid++)
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000446 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
447 return 0;
448
449error:
450 snd_ctl_free_one(kcontrol);
451 return ret;
452}
453EXPORT_SYMBOL(snd_ctl_replace);
454
455/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * snd_ctl_remove - remove the control from the card and release it
457 * @card: the card instance
458 * @kcontrol: the control instance to remove
459 *
460 * Removes the control from the card and then releases the instance.
461 * You don't need to call snd_ctl_free_one(). You must be in
462 * the write lock - down_write(&card->controls_rwsem).
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100463 *
464 * Return: 0 if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100466int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100468 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 unsigned int idx;
470
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200471 if (snd_BUG_ON(!card || !kcontrol))
472 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 list_del(&kcontrol->list);
474 card->controls_count -= kcontrol->count;
475 id = kcontrol->id;
476 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
477 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
478 snd_ctl_free_one(kcontrol);
479 return 0;
480}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200481EXPORT_SYMBOL(snd_ctl_remove);
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483/**
484 * snd_ctl_remove_id - remove the control of the given id and release it
485 * @card: the card instance
486 * @id: the control id to remove
487 *
488 * Finds the control instance with the given id, removes it from the
489 * card list and releases it.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100490 *
491 * Return: 0 if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100493int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100495 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 int ret;
497
498 down_write(&card->controls_rwsem);
499 kctl = snd_ctl_find_id(card, id);
500 if (kctl == NULL) {
501 up_write(&card->controls_rwsem);
502 return -ENOENT;
503 }
504 ret = snd_ctl_remove(card, kctl);
505 up_write(&card->controls_rwsem);
506 return ret;
507}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200508EXPORT_SYMBOL(snd_ctl_remove_id);
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510/**
Clemens Ladischf217ac52009-08-17 12:27:22 +0200511 * snd_ctl_remove_user_ctl - remove and release the unlocked user control
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 * @file: active control handle
513 * @id: the control id to remove
514 *
515 * Finds the control instance with the given id, removes it from the
516 * card list and releases it.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100517 *
518 * Return: 0 if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 */
Clemens Ladischf217ac52009-08-17 12:27:22 +0200520static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
521 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100523 struct snd_card *card = file->card;
524 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 int idx, ret;
526
527 down_write(&card->controls_rwsem);
528 kctl = snd_ctl_find_id(card, id);
529 if (kctl == NULL) {
Clemens Ladisch317b8082009-08-17 12:26:34 +0200530 ret = -ENOENT;
531 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
Clemens Ladisch18dd0aa2009-08-17 12:28:09 +0200533 if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
534 ret = -EINVAL;
535 goto error;
536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 for (idx = 0; idx < kctl->count; idx++)
538 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
Clemens Ladisch317b8082009-08-17 12:26:34 +0200539 ret = -EBUSY;
540 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
542 ret = snd_ctl_remove(card, kctl);
Clemens Ladischf217ac52009-08-17 12:27:22 +0200543 if (ret < 0)
544 goto error;
545 card->user_ctl_count--;
Clemens Ladisch317b8082009-08-17 12:26:34 +0200546error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 up_write(&card->controls_rwsem);
548 return ret;
549}
550
551/**
Takashi Iwai3cbdd752008-08-29 16:09:01 +0200552 * snd_ctl_activate_id - activate/inactivate the control of the given id
553 * @card: the card instance
554 * @id: the control id to activate/inactivate
555 * @active: non-zero to activate
556 *
557 * Finds the control instance with the given id, and activate or
558 * inactivate the control together with notification, if changed.
559 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100560 * Return: 0 if unchanged, 1 if changed, or a negative error code on failure.
Takashi Iwai3cbdd752008-08-29 16:09:01 +0200561 */
562int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
563 int active)
564{
565 struct snd_kcontrol *kctl;
566 struct snd_kcontrol_volatile *vd;
567 unsigned int index_offset;
568 int ret;
569
570 down_write(&card->controls_rwsem);
571 kctl = snd_ctl_find_id(card, id);
572 if (kctl == NULL) {
573 ret = -ENOENT;
574 goto unlock;
575 }
Lars-Peter Clausen31584ed2014-11-07 14:12:34 +0100576 index_offset = snd_ctl_get_ioff(kctl, id);
Takashi Iwai3cbdd752008-08-29 16:09:01 +0200577 vd = &kctl->vd[index_offset];
578 ret = 0;
579 if (active) {
580 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
581 goto unlock;
582 vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
583 } else {
584 if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)
585 goto unlock;
586 vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
587 }
588 ret = 1;
589 unlock:
590 up_write(&card->controls_rwsem);
591 if (ret > 0)
592 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, id);
593 return ret;
594}
595EXPORT_SYMBOL_GPL(snd_ctl_activate_id);
596
597/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 * snd_ctl_rename_id - replace the id of a control on the card
599 * @card: the card instance
600 * @src_id: the old id
601 * @dst_id: the new id
602 *
603 * Finds the control with the old id from the card, and replaces the
604 * id with the new one.
605 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100606 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100608int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
609 struct snd_ctl_elem_id *dst_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100611 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 down_write(&card->controls_rwsem);
614 kctl = snd_ctl_find_id(card, src_id);
615 if (kctl == NULL) {
616 up_write(&card->controls_rwsem);
617 return -ENOENT;
618 }
619 kctl->id = *dst_id;
620 kctl->id.numid = card->last_numid + 1;
621 card->last_numid += kctl->count;
622 up_write(&card->controls_rwsem);
623 return 0;
624}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200625EXPORT_SYMBOL(snd_ctl_rename_id);
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627/**
628 * snd_ctl_find_numid - find the control instance with the given number-id
629 * @card: the card instance
630 * @numid: the number-id to search
631 *
632 * Finds the control instance with the given number-id from the card.
633 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 * The caller must down card->controls_rwsem before calling this function
635 * (if the race condition can happen).
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100636 *
637 * Return: The pointer of the instance if found, or %NULL if not.
638 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100640struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100642 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200644 if (snd_BUG_ON(!card || !numid))
645 return NULL;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200646 list_for_each_entry(kctl, &card->controls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
648 return kctl;
649 }
650 return NULL;
651}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200652EXPORT_SYMBOL(snd_ctl_find_numid);
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654/**
655 * snd_ctl_find_id - find the control instance with the given id
656 * @card: the card instance
657 * @id: the id to search
658 *
659 * Finds the control instance with the given id from the card.
660 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 * The caller must down card->controls_rwsem before calling this function
662 * (if the race condition can happen).
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100663 *
664 * Return: The pointer of the instance if found, or %NULL if not.
665 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100667struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
668 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100670 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200672 if (snd_BUG_ON(!card || !id))
673 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (id->numid != 0)
675 return snd_ctl_find_numid(card, id->numid);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200676 list_for_each_entry(kctl, &card->controls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (kctl->id.iface != id->iface)
678 continue;
679 if (kctl->id.device != id->device)
680 continue;
681 if (kctl->id.subdevice != id->subdevice)
682 continue;
683 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
684 continue;
685 if (kctl->id.index > id->index)
686 continue;
687 if (kctl->id.index + kctl->count <= id->index)
688 continue;
689 return kctl;
690 }
691 return NULL;
692}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200693EXPORT_SYMBOL(snd_ctl_find_id);
694
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100695static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 unsigned int cmd, void __user *arg)
697{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100698 struct snd_ctl_card_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Takashi Iwaica2c0962005-09-09 14:20:23 +0200700 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (! info)
702 return -ENOMEM;
703 down_read(&snd_ioctl_rwsem);
704 info->card = card->number;
705 strlcpy(info->id, card->id, sizeof(info->id));
706 strlcpy(info->driver, card->driver, sizeof(info->driver));
707 strlcpy(info->name, card->shortname, sizeof(info->name));
708 strlcpy(info->longname, card->longname, sizeof(info->longname));
709 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
710 strlcpy(info->components, card->components, sizeof(info->components));
711 up_read(&snd_ioctl_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100712 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 kfree(info);
714 return -EFAULT;
715 }
716 kfree(info);
717 return 0;
718}
719
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100720static int snd_ctl_elem_list(struct snd_card *card,
721 struct snd_ctl_elem_list __user *_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 struct list_head *plist;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100724 struct snd_ctl_elem_list list;
725 struct snd_kcontrol *kctl;
726 struct snd_ctl_elem_id *dst, *id;
Luca Tettamanti78fa2c42011-05-25 22:43:27 +0200727 unsigned int offset, space, jidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 if (copy_from_user(&list, _list, sizeof(list)))
730 return -EFAULT;
731 offset = list.offset;
732 space = list.space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /* try limit maximum space */
734 if (space > 16384)
735 return -ENOMEM;
736 if (space > 0) {
737 /* allocate temporary buffer for atomic operation */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100738 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (dst == NULL)
740 return -ENOMEM;
741 down_read(&card->controls_rwsem);
742 list.count = card->controls_count;
743 plist = card->controls.next;
744 while (plist != &card->controls) {
745 if (offset == 0)
746 break;
747 kctl = snd_kcontrol(plist);
748 if (offset < kctl->count)
749 break;
750 offset -= kctl->count;
751 plist = plist->next;
752 }
753 list.used = 0;
754 id = dst;
755 while (space > 0 && plist != &card->controls) {
756 kctl = snd_kcontrol(plist);
757 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
758 snd_ctl_build_ioff(id, kctl, jidx);
759 id++;
760 space--;
761 list.used++;
762 }
763 plist = plist->next;
764 offset = 0;
765 }
766 up_read(&card->controls_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100767 if (list.used > 0 &&
768 copy_to_user(list.pids, dst,
769 list.used * sizeof(struct snd_ctl_elem_id))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 vfree(dst);
771 return -EFAULT;
772 }
773 vfree(dst);
774 } else {
775 down_read(&card->controls_rwsem);
776 list.count = card->controls_count;
777 up_read(&card->controls_rwsem);
778 }
779 if (copy_to_user(_list, &list, sizeof(list)))
780 return -EFAULT;
781 return 0;
782}
783
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100784static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
785 struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100787 struct snd_card *card = ctl->card;
788 struct snd_kcontrol *kctl;
789 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 unsigned int index_offset;
791 int result;
792
793 down_read(&card->controls_rwsem);
794 kctl = snd_ctl_find_id(card, &info->id);
795 if (kctl == NULL) {
796 up_read(&card->controls_rwsem);
797 return -ENOENT;
798 }
799#ifdef CONFIG_SND_DEBUG
800 info->access = 0;
801#endif
802 result = kctl->info(kctl, info);
803 if (result >= 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200804 snd_BUG_ON(info->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 index_offset = snd_ctl_get_ioff(kctl, &info->id);
806 vd = &kctl->vd[index_offset];
807 snd_ctl_build_ioff(&info->id, kctl, index_offset);
808 info->access = vd->access;
809 if (vd->owner) {
810 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
811 if (vd->owner == ctl)
812 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
Clemens Ladisch25d27ed2009-11-02 09:35:44 +0100813 info->owner = pid_vnr(vd->owner->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 } else {
815 info->owner = -1;
816 }
817 }
818 up_read(&card->controls_rwsem);
819 return result;
820}
821
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100822static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
823 struct snd_ctl_elem_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100825 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 int result;
827
828 if (copy_from_user(&info, _info, sizeof(info)))
829 return -EFAULT;
Giuliano Pochini64649402006-03-13 14:11:11 +0100830 snd_power_lock(ctl->card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200831 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100832 if (result >= 0)
833 result = snd_ctl_elem_info(ctl, &info);
834 snd_power_unlock(ctl->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (result >= 0)
836 if (copy_to_user(_info, &info, sizeof(info)))
837 return -EFAULT;
838 return result;
839}
840
Takashi Iwaid3bd67c2008-06-12 18:17:26 +0200841static int snd_ctl_elem_read(struct snd_card *card,
842 struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100844 struct snd_kcontrol *kctl;
845 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 unsigned int index_offset;
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100847 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 down_read(&card->controls_rwsem);
850 kctl = snd_ctl_find_id(card, &control->id);
851 if (kctl == NULL) {
852 result = -ENOENT;
853 } else {
854 index_offset = snd_ctl_get_ioff(kctl, &control->id);
855 vd = &kctl->vd[index_offset];
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100856 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) &&
857 kctl->get != NULL) {
858 snd_ctl_build_ioff(&control->id, kctl, index_offset);
859 result = kctl->get(kctl, control);
860 } else
861 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863 up_read(&card->controls_rwsem);
864 return result;
865}
866
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100867static int snd_ctl_elem_read_user(struct snd_card *card,
868 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100870 struct snd_ctl_elem_value *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 int result;
Li Zefanef44a1e2009-04-10 09:43:08 +0800872
873 control = memdup_user(_control, sizeof(*control));
874 if (IS_ERR(control))
875 return PTR_ERR(control);
876
Giuliano Pochini64649402006-03-13 14:11:11 +0100877 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200878 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100879 if (result >= 0)
880 result = snd_ctl_elem_read(card, control);
881 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (result >= 0)
883 if (copy_to_user(_control, control, sizeof(*control)))
884 result = -EFAULT;
885 kfree(control);
886 return result;
887}
888
Takashi Iwaid3bd67c2008-06-12 18:17:26 +0200889static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
890 struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100892 struct snd_kcontrol *kctl;
893 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 unsigned int index_offset;
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100895 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 down_read(&card->controls_rwsem);
898 kctl = snd_ctl_find_id(card, &control->id);
899 if (kctl == NULL) {
900 result = -ENOENT;
901 } else {
902 index_offset = snd_ctl_get_ioff(kctl, &control->id);
903 vd = &kctl->vd[index_offset];
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100904 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
905 kctl->put == NULL ||
906 (file && vd->owner && vd->owner != file)) {
907 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 } else {
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100909 snd_ctl_build_ioff(&control->id, kctl, index_offset);
910 result = kctl->put(kctl, control);
911 }
912 if (result > 0) {
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200913 struct snd_ctl_elem_id id = control->id;
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100914 up_read(&card->controls_rwsem);
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200915 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &id);
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100916 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918 }
919 up_read(&card->controls_rwsem);
920 return result;
921}
922
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100923static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
924 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100926 struct snd_ctl_elem_value *control;
Giuliano Pochini64649402006-03-13 14:11:11 +0100927 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 int result;
929
Li Zefanef44a1e2009-04-10 09:43:08 +0800930 control = memdup_user(_control, sizeof(*control));
931 if (IS_ERR(control))
932 return PTR_ERR(control);
933
Giuliano Pochini64649402006-03-13 14:11:11 +0100934 card = file->card;
935 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200936 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100937 if (result >= 0)
938 result = snd_ctl_elem_write(card, file, control);
939 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (result >= 0)
941 if (copy_to_user(_control, control, sizeof(*control)))
942 result = -EFAULT;
943 kfree(control);
944 return result;
945}
946
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100947static int snd_ctl_elem_lock(struct snd_ctl_file *file,
948 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100950 struct snd_card *card = file->card;
951 struct snd_ctl_elem_id id;
952 struct snd_kcontrol *kctl;
953 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 int result;
955
956 if (copy_from_user(&id, _id, sizeof(id)))
957 return -EFAULT;
958 down_write(&card->controls_rwsem);
959 kctl = snd_ctl_find_id(card, &id);
960 if (kctl == NULL) {
961 result = -ENOENT;
962 } else {
963 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
964 if (vd->owner != NULL)
965 result = -EBUSY;
966 else {
967 vd->owner = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 result = 0;
969 }
970 }
971 up_write(&card->controls_rwsem);
972 return result;
973}
974
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100975static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
976 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100978 struct snd_card *card = file->card;
979 struct snd_ctl_elem_id id;
980 struct snd_kcontrol *kctl;
981 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 int result;
983
984 if (copy_from_user(&id, _id, sizeof(id)))
985 return -EFAULT;
986 down_write(&card->controls_rwsem);
987 kctl = snd_ctl_find_id(card, &id);
988 if (kctl == NULL) {
989 result = -ENOENT;
990 } else {
991 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
992 if (vd->owner == NULL)
993 result = -EINVAL;
994 else if (vd->owner != file)
995 result = -EPERM;
996 else {
997 vd->owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 result = 0;
999 }
1000 }
1001 up_write(&card->controls_rwsem);
1002 return result;
1003}
1004
1005struct user_element {
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001006 struct snd_ctl_elem_info info;
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001007 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 void *elem_data; /* element data */
1009 unsigned long elem_data_size; /* size of element data in bytes */
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001010 void *tlv_data; /* TLV data */
1011 unsigned long tlv_data_size; /* TLV data size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 void *priv_data; /* private data (like strings for enumerated type) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013};
1014
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001015static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
1016 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
1018 struct user_element *ue = kcontrol->private_data;
1019
1020 *uinfo = ue->info;
1021 return 0;
1022}
1023
Clemens Ladisch8d448162011-10-07 22:38:59 +02001024static int snd_ctl_elem_user_enum_info(struct snd_kcontrol *kcontrol,
1025 struct snd_ctl_elem_info *uinfo)
1026{
1027 struct user_element *ue = kcontrol->private_data;
1028 const char *names;
1029 unsigned int item;
1030
1031 item = uinfo->value.enumerated.item;
1032
1033 *uinfo = ue->info;
1034
1035 item = min(item, uinfo->value.enumerated.items - 1);
1036 uinfo->value.enumerated.item = item;
1037
1038 names = ue->priv_data;
1039 for (; item > 0; --item)
1040 names += strlen(names) + 1;
1041 strcpy(uinfo->value.enumerated.name, names);
1042
1043 return 0;
1044}
1045
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001046static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
1047 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
1049 struct user_element *ue = kcontrol->private_data;
1050
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001051 mutex_lock(&ue->card->user_ctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001053 mutex_unlock(&ue->card->user_ctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return 0;
1055}
1056
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001057static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
1058 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
1060 int change;
1061 struct user_element *ue = kcontrol->private_data;
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001062
1063 mutex_lock(&ue->card->user_ctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
1065 if (change)
1066 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001067 mutex_unlock(&ue->card->user_ctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return change;
1069}
1070
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001071static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol,
1072 int op_flag,
1073 unsigned int size,
1074 unsigned int __user *tlv)
1075{
1076 struct user_element *ue = kcontrol->private_data;
1077 int change = 0;
1078 void *new_data;
1079
1080 if (op_flag > 0) {
1081 if (size > 1024 * 128) /* sane value */
1082 return -EINVAL;
Li Zefanef44a1e2009-04-10 09:43:08 +08001083
1084 new_data = memdup_user(tlv, size);
1085 if (IS_ERR(new_data))
1086 return PTR_ERR(new_data);
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001087 mutex_lock(&ue->card->user_ctl_lock);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001088 change = ue->tlv_data_size != size;
1089 if (!change)
1090 change = memcmp(ue->tlv_data, new_data, size);
1091 kfree(ue->tlv_data);
1092 ue->tlv_data = new_data;
1093 ue->tlv_data_size = size;
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001094 mutex_unlock(&ue->card->user_ctl_lock);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001095 } else {
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001096 int ret = 0;
1097
1098 mutex_lock(&ue->card->user_ctl_lock);
1099 if (!ue->tlv_data_size || !ue->tlv_data) {
1100 ret = -ENXIO;
1101 goto err_unlock;
1102 }
1103 if (size < ue->tlv_data_size) {
1104 ret = -ENOSPC;
1105 goto err_unlock;
1106 }
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001107 if (copy_to_user(tlv, ue->tlv_data, ue->tlv_data_size))
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001108 ret = -EFAULT;
1109err_unlock:
1110 mutex_unlock(&ue->card->user_ctl_lock);
1111 if (ret)
1112 return ret;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001113 }
1114 return change;
1115}
1116
Clemens Ladisch8d448162011-10-07 22:38:59 +02001117static int snd_ctl_elem_init_enum_names(struct user_element *ue)
1118{
1119 char *names, *p;
1120 size_t buf_len, name_len;
1121 unsigned int i;
Olof Johansson447c6f92011-11-05 22:51:54 +01001122 const uintptr_t user_ptrval = ue->info.value.enumerated.names_ptr;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001123
1124 if (ue->info.value.enumerated.names_length > 64 * 1024)
1125 return -EINVAL;
1126
Olof Johansson447c6f92011-11-05 22:51:54 +01001127 names = memdup_user((const void __user *)user_ptrval,
Clemens Ladisch8d448162011-10-07 22:38:59 +02001128 ue->info.value.enumerated.names_length);
1129 if (IS_ERR(names))
1130 return PTR_ERR(names);
1131
1132 /* check that there are enough valid names */
1133 buf_len = ue->info.value.enumerated.names_length;
1134 p = names;
1135 for (i = 0; i < ue->info.value.enumerated.items; ++i) {
1136 name_len = strnlen(p, buf_len);
1137 if (name_len == 0 || name_len >= 64 || name_len == buf_len) {
1138 kfree(names);
1139 return -EINVAL;
1140 }
1141 p += name_len + 1;
1142 buf_len -= name_len + 1;
1143 }
1144
1145 ue->priv_data = names;
1146 ue->info.value.enumerated.names_ptr = 0;
1147
1148 return 0;
1149}
1150
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001151static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001153 struct user_element *ue = kcontrol->private_data;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001154
1155 kfree(ue->tlv_data);
1156 kfree(ue->priv_data);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001157 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
1159
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001160static int snd_ctl_elem_add(struct snd_ctl_file *file,
1161 struct snd_ctl_elem_info *info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001163 struct snd_card *card = file->card;
1164 struct snd_kcontrol kctl, *_kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 unsigned int access;
1166 long private_size;
1167 struct user_element *ue;
1168 int idx, err;
Lu Guanqun983929c2011-08-24 11:12:34 +08001169
Clemens Ladisch2a031ae2009-08-17 12:25:52 +02001170 if (info->count < 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return -EINVAL;
1172 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001173 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001174 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
1175 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 info->id.numid = 0;
1177 memset(&kctl, 0, sizeof(kctl));
Lars-Peter Clausen82262a462014-06-18 13:32:32 +02001178
1179 if (replace) {
1180 err = snd_ctl_remove_user_ctl(file, &info->id);
1181 if (err)
1182 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
Lars-Peter Clausen82262a462014-06-18 13:32:32 +02001184
1185 if (card->user_ctl_count >= MAX_USER_CONTROLS)
1186 return -ENOMEM;
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 memcpy(&kctl.id, &info->id, sizeof(info->id));
1189 kctl.count = info->owner ? info->owner : 1;
1190 access |= SNDRV_CTL_ELEM_ACCESS_USER;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001191 if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED)
1192 kctl.info = snd_ctl_elem_user_enum_info;
1193 else
1194 kctl.info = snd_ctl_elem_user_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1196 kctl.get = snd_ctl_elem_user_get;
1197 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1198 kctl.put = snd_ctl_elem_user_put;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001199 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
1200 kctl.tlv.c = snd_ctl_elem_user_tlv;
1201 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 switch (info->type) {
1204 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 case SNDRV_CTL_ELEM_TYPE_INTEGER:
1206 private_size = sizeof(long);
1207 if (info->count > 128)
1208 return -EINVAL;
1209 break;
1210 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
1211 private_size = sizeof(long long);
1212 if (info->count > 64)
1213 return -EINVAL;
1214 break;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001215 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
1216 private_size = sizeof(unsigned int);
1217 if (info->count > 128 || info->value.enumerated.items == 0)
1218 return -EINVAL;
1219 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 case SNDRV_CTL_ELEM_TYPE_BYTES:
1221 private_size = sizeof(unsigned char);
1222 if (info->count > 512)
1223 return -EINVAL;
1224 break;
1225 case SNDRV_CTL_ELEM_TYPE_IEC958:
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001226 private_size = sizeof(struct snd_aes_iec958);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (info->count != 1)
1228 return -EINVAL;
1229 break;
1230 default:
1231 return -EINVAL;
1232 }
1233 private_size *= info->count;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001234 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (ue == NULL)
1236 return -ENOMEM;
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001237 ue->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 ue->info = *info;
Takashi Iwai86148e82006-08-24 12:36:36 +02001239 ue->info.access = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 ue->elem_data = (char *)ue + sizeof(*ue);
1241 ue->elem_data_size = private_size;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001242 if (ue->info.type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
1243 err = snd_ctl_elem_init_enum_names(ue);
1244 if (err < 0) {
1245 kfree(ue);
1246 return err;
1247 }
1248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 kctl.private_free = snd_ctl_elem_user_free;
1250 _kctl = snd_ctl_new(&kctl, access);
1251 if (_kctl == NULL) {
Clemens Ladisch8d448162011-10-07 22:38:59 +02001252 kfree(ue->priv_data);
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001253 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return -ENOMEM;
1255 }
1256 _kctl->private_data = ue;
1257 for (idx = 0; idx < _kctl->count; idx++)
1258 _kctl->vd[idx].owner = file;
1259 err = snd_ctl_add(card, _kctl);
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001260 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 down_write(&card->controls_rwsem);
1264 card->user_ctl_count++;
1265 up_write(&card->controls_rwsem);
1266
1267 return 0;
1268}
1269
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001270static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1271 struct snd_ctl_elem_info __user *_info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001273 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (copy_from_user(&info, _info, sizeof(info)))
1275 return -EFAULT;
1276 return snd_ctl_elem_add(file, &info, replace);
1277}
1278
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001279static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1280 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001282 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 if (copy_from_user(&id, _id, sizeof(id)))
1285 return -EFAULT;
Clemens Ladischf217ac52009-08-17 12:27:22 +02001286 return snd_ctl_remove_user_ctl(file, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001289static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 int subscribe;
1292 if (get_user(subscribe, ptr))
1293 return -EFAULT;
1294 if (subscribe < 0) {
1295 subscribe = file->subscribed;
1296 if (put_user(subscribe, ptr))
1297 return -EFAULT;
1298 return 0;
1299 }
1300 if (subscribe) {
1301 file->subscribed = 1;
1302 return 0;
1303 } else if (file->subscribed) {
1304 snd_ctl_empty_read_queue(file);
1305 file->subscribed = 0;
1306 }
1307 return 0;
1308}
1309
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001310static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1311 struct snd_ctl_tlv __user *_tlv,
1312 int op_flag)
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001313{
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001314 struct snd_card *card = file->card;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001315 struct snd_ctl_tlv tlv;
1316 struct snd_kcontrol *kctl;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001317 struct snd_kcontrol_volatile *vd;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001318 unsigned int len;
1319 int err = 0;
1320
1321 if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
1322 return -EFAULT;
Clemens Ladisch61236372010-02-01 13:30:56 +01001323 if (tlv.length < sizeof(unsigned int) * 2)
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001324 return -EINVAL;
1325 down_read(&card->controls_rwsem);
1326 kctl = snd_ctl_find_numid(card, tlv.numid);
1327 if (kctl == NULL) {
1328 err = -ENOENT;
1329 goto __kctl_end;
1330 }
1331 if (kctl->tlv.p == NULL) {
1332 err = -ENXIO;
1333 goto __kctl_end;
1334 }
1335 vd = &kctl->vd[tlv.numid - kctl->id.numid];
1336 if ((op_flag == 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) == 0) ||
1337 (op_flag > 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) == 0) ||
1338 (op_flag < 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND) == 0)) {
1339 err = -ENXIO;
1340 goto __kctl_end;
1341 }
1342 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
Dan Carpenterbec145a2009-11-18 10:31:57 +02001343 if (vd->owner != NULL && vd->owner != file) {
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001344 err = -EPERM;
1345 goto __kctl_end;
1346 }
Jeffrin Josebd483d42012-03-07 22:57:39 +05301347 err = kctl->tlv.c(kctl, op_flag, tlv.length, _tlv->tlv);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001348 if (err > 0) {
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +02001349 struct snd_ctl_elem_id id = kctl->id;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001350 up_read(&card->controls_rwsem);
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +02001351 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &id);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001352 return 0;
1353 }
1354 } else {
1355 if (op_flag) {
1356 err = -ENXIO;
1357 goto __kctl_end;
1358 }
1359 len = kctl->tlv.p[1] + 2 * sizeof(unsigned int);
1360 if (tlv.length < len) {
1361 err = -ENOMEM;
1362 goto __kctl_end;
1363 }
1364 if (copy_to_user(_tlv->tlv, kctl->tlv.p, len))
1365 err = -EFAULT;
1366 }
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001367 __kctl_end:
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001368 up_read(&card->controls_rwsem);
1369 return err;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001370}
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1373{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001374 struct snd_ctl_file *ctl;
1375 struct snd_card *card;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001376 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 void __user *argp = (void __user *)arg;
1378 int __user *ip = argp;
1379 int err;
1380
1381 ctl = file->private_data;
1382 card = ctl->card;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001383 if (snd_BUG_ON(!card))
1384 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 switch (cmd) {
1386 case SNDRV_CTL_IOCTL_PVERSION:
1387 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1388 case SNDRV_CTL_IOCTL_CARD_INFO:
1389 return snd_ctl_card_info(card, ctl, cmd, argp);
1390 case SNDRV_CTL_IOCTL_ELEM_LIST:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001391 return snd_ctl_elem_list(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 case SNDRV_CTL_IOCTL_ELEM_INFO:
1393 return snd_ctl_elem_info_user(ctl, argp);
1394 case SNDRV_CTL_IOCTL_ELEM_READ:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001395 return snd_ctl_elem_read_user(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1397 return snd_ctl_elem_write_user(ctl, argp);
1398 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1399 return snd_ctl_elem_lock(ctl, argp);
1400 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1401 return snd_ctl_elem_unlock(ctl, argp);
1402 case SNDRV_CTL_IOCTL_ELEM_ADD:
1403 return snd_ctl_elem_add_user(ctl, argp, 0);
1404 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1405 return snd_ctl_elem_add_user(ctl, argp, 1);
1406 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1407 return snd_ctl_elem_remove(ctl, argp);
1408 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1409 return snd_ctl_subscribe_events(ctl, ip);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001410 case SNDRV_CTL_IOCTL_TLV_READ:
Takashi Iwai0cea76f2014-07-15 16:31:01 +02001411 return snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_READ);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001412 case SNDRV_CTL_IOCTL_TLV_WRITE:
Takashi Iwai0cea76f2014-07-15 16:31:01 +02001413 return snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_WRITE);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001414 case SNDRV_CTL_IOCTL_TLV_COMMAND:
Takashi Iwai0cea76f2014-07-15 16:31:01 +02001415 return snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 case SNDRV_CTL_IOCTL_POWER:
Takashi Iwaia381a7a2005-11-17 15:55:49 +01001417 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 case SNDRV_CTL_IOCTL_POWER_STATE:
1419#ifdef CONFIG_PM
1420 return put_user(card->power_state, ip) ? -EFAULT : 0;
1421#else
1422 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1423#endif
1424 }
1425 down_read(&snd_ioctl_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001426 list_for_each_entry(p, &snd_control_ioctls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 err = p->fioctl(card, ctl, cmd, arg);
1428 if (err != -ENOIOCTLCMD) {
1429 up_read(&snd_ioctl_rwsem);
1430 return err;
1431 }
1432 }
1433 up_read(&snd_ioctl_rwsem);
Takashi Iwaibb009452014-02-04 18:18:16 +01001434 dev_dbg(card->dev, "unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return -ENOTTY;
1436}
1437
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001438static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1439 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001441 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 int err = 0;
1443 ssize_t result = 0;
1444
1445 ctl = file->private_data;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001446 if (snd_BUG_ON(!ctl || !ctl->card))
1447 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (!ctl->subscribed)
1449 return -EBADFD;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001450 if (count < sizeof(struct snd_ctl_event))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return -EINVAL;
1452 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001453 while (count >= sizeof(struct snd_ctl_event)) {
1454 struct snd_ctl_event ev;
1455 struct snd_kctl_event *kev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 while (list_empty(&ctl->events)) {
1457 wait_queue_t wait;
1458 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1459 err = -EAGAIN;
1460 goto __end_lock;
1461 }
1462 init_waitqueue_entry(&wait, current);
1463 add_wait_queue(&ctl->change_sleep, &wait);
1464 set_current_state(TASK_INTERRUPTIBLE);
1465 spin_unlock_irq(&ctl->read_lock);
1466 schedule();
1467 remove_wait_queue(&ctl->change_sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001468 if (ctl->card->shutdown)
1469 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (signal_pending(current))
Adrian Bunk0e5d7202006-11-07 13:42:54 +01001471 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 spin_lock_irq(&ctl->read_lock);
1473 }
1474 kev = snd_kctl_event(ctl->events.next);
1475 ev.type = SNDRV_CTL_EVENT_ELEM;
1476 ev.data.elem.mask = kev->mask;
1477 ev.data.elem.id = kev->id;
1478 list_del(&kev->list);
1479 spin_unlock_irq(&ctl->read_lock);
1480 kfree(kev);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001481 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 err = -EFAULT;
1483 goto __end;
1484 }
1485 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001486 buffer += sizeof(struct snd_ctl_event);
1487 count -= sizeof(struct snd_ctl_event);
1488 result += sizeof(struct snd_ctl_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
1490 __end_lock:
1491 spin_unlock_irq(&ctl->read_lock);
1492 __end:
1493 return result > 0 ? result : err;
1494}
1495
1496static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1497{
1498 unsigned int mask;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001499 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 ctl = file->private_data;
1502 if (!ctl->subscribed)
1503 return 0;
1504 poll_wait(file, &ctl->change_sleep, wait);
1505
1506 mask = 0;
1507 if (!list_empty(&ctl->events))
1508 mask |= POLLIN | POLLRDNORM;
1509
1510 return mask;
1511}
1512
1513/*
1514 * register the device-specific control-ioctls.
1515 * called from each device manager like pcm.c, hwdep.c, etc.
1516 */
1517static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1518{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001519 struct snd_kctl_ioctl *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001521 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 if (pn == NULL)
1523 return -ENOMEM;
1524 pn->fioctl = fcn;
1525 down_write(&snd_ioctl_rwsem);
1526 list_add_tail(&pn->list, lists);
1527 up_write(&snd_ioctl_rwsem);
1528 return 0;
1529}
1530
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001531/**
1532 * snd_ctl_register_ioctl - register the device-specific control-ioctls
1533 * @fcn: ioctl callback function
1534 *
1535 * called from each device manager like pcm.c, hwdep.c, etc.
1536 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1538{
1539 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1540}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001541EXPORT_SYMBOL(snd_ctl_register_ioctl);
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543#ifdef CONFIG_COMPAT
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001544/**
1545 * snd_ctl_register_ioctl_compat - register the device-specific 32bit compat
1546 * control-ioctls
1547 * @fcn: ioctl callback function
1548 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1550{
1551 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1552}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001553EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554#endif
1555
1556/*
1557 * de-register the device-specific control-ioctls.
1558 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001559static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1560 struct list_head *lists)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001562 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001564 if (snd_BUG_ON(!fcn))
1565 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 down_write(&snd_ioctl_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001567 list_for_each_entry(p, lists, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (p->fioctl == fcn) {
1569 list_del(&p->list);
1570 up_write(&snd_ioctl_rwsem);
1571 kfree(p);
1572 return 0;
1573 }
1574 }
1575 up_write(&snd_ioctl_rwsem);
1576 snd_BUG();
1577 return -EINVAL;
1578}
1579
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001580/**
1581 * snd_ctl_unregister_ioctl - de-register the device-specific control-ioctls
1582 * @fcn: ioctl callback function to unregister
1583 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1585{
1586 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1587}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001588EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590#ifdef CONFIG_COMPAT
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001591/**
1592 * snd_ctl_unregister_ioctl - de-register the device-specific compat 32bit
1593 * control-ioctls
1594 * @fcn: ioctl callback function to unregister
1595 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1597{
1598 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1599}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001600EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601#endif
1602
1603static int snd_ctl_fasync(int fd, struct file * file, int on)
1604{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001605 struct snd_ctl_file *ctl;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 ctl = file->private_data;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001608 return fasync_helper(fd, file, on, &ctl->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
1610
Takashi Iwai23c18d42014-02-19 14:30:29 +01001611/* return the preferred subdevice number if already assigned;
1612 * otherwise return -1
1613 */
1614int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type)
1615{
1616 struct snd_ctl_file *kctl;
1617 int subdevice = -1;
1618
1619 read_lock(&card->ctl_files_rwlock);
1620 list_for_each_entry(kctl, &card->ctl_files, list) {
1621 if (kctl->pid == task_pid(current)) {
1622 subdevice = kctl->preferred_subdevice[type];
1623 if (subdevice != -1)
1624 break;
1625 }
1626 }
1627 read_unlock(&card->ctl_files_rwlock);
1628 return subdevice;
1629}
1630EXPORT_SYMBOL_GPL(snd_ctl_get_preferred_subdevice);
1631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632/*
1633 * ioctl32 compat
1634 */
1635#ifdef CONFIG_COMPAT
1636#include "control_compat.c"
1637#else
1638#define snd_ctl_ioctl_compat NULL
1639#endif
1640
1641/*
1642 * INIT PART
1643 */
1644
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001645static const struct file_operations snd_ctl_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
1647 .owner = THIS_MODULE,
1648 .read = snd_ctl_read,
1649 .open = snd_ctl_open,
1650 .release = snd_ctl_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02001651 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 .poll = snd_ctl_poll,
1653 .unlocked_ioctl = snd_ctl_ioctl,
1654 .compat_ioctl = snd_ctl_ioctl_compat,
1655 .fasync = snd_ctl_fasync,
1656};
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658/*
1659 * registration of the control device
1660 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001661static int snd_ctl_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001663 struct snd_card *card = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Takashi Iwai40a4b262015-01-30 08:34:58 +01001665 return snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1666 &snd_ctl_f_ops, card, &card->ctl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
1668
1669/*
1670 * disconnection of the control device
1671 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001672static int snd_ctl_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001674 struct snd_card *card = device->device_data;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001675 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Takashi Iwaid8009882008-09-07 12:51:13 +02001677 read_lock(&card->ctl_files_rwlock);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001678 list_for_each_entry(ctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 wake_up(&ctl->change_sleep);
1680 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1681 }
Takashi Iwaid8009882008-09-07 12:51:13 +02001682 read_unlock(&card->ctl_files_rwlock);
Takashi Iwaic4614822006-06-23 14:38:23 +02001683
Takashi Iwai40a4b262015-01-30 08:34:58 +01001684 return snd_unregister_device(&card->ctl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
1687/*
1688 * free all controls
1689 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001690static int snd_ctl_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001692 struct snd_card *card = device->device_data;
1693 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 down_write(&card->controls_rwsem);
1696 while (!list_empty(&card->controls)) {
1697 control = snd_kcontrol(card->controls.next);
1698 snd_ctl_remove(card, control);
1699 }
1700 up_write(&card->controls_rwsem);
Takashi Iwai0fcd9f42015-01-29 16:41:27 +01001701 put_device(&card->ctl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return 0;
1703}
1704
1705/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 * create control core:
1707 * called from init.c
1708 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001709int snd_ctl_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001711 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 .dev_free = snd_ctl_dev_free,
1713 .dev_register = snd_ctl_dev_register,
1714 .dev_disconnect = snd_ctl_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 };
Takashi Iwai0fcd9f42015-01-29 16:41:27 +01001716 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001718 if (snd_BUG_ON(!card))
1719 return -ENXIO;
Takashi Iwai0fcd9f42015-01-29 16:41:27 +01001720 if (snd_BUG_ON(card->number < 0 || card->number >= SNDRV_CARDS))
1721 return -ENXIO;
1722
1723 snd_device_initialize(&card->ctl_dev, card);
1724 dev_set_name(&card->ctl_dev, "controlC%d", card->number);
1725
1726 err = snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
1727 if (err < 0)
1728 put_device(&card->ctl_dev);
1729 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001731
1732/*
Clemens Ladisch96007322011-01-10 16:25:44 +01001733 * Frequently used control callbacks/helpers
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001734 */
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001735
1736/**
1737 * snd_ctl_boolean_mono_info - Helper function for a standard boolean info
1738 * callback with a mono channel
1739 * @kcontrol: the kcontrol instance
1740 * @uinfo: info to store
1741 *
1742 * This is a function that can be used as info callback for a standard
1743 * boolean control with a single mono channel.
1744 */
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001745int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
1746 struct snd_ctl_elem_info *uinfo)
1747{
1748 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1749 uinfo->count = 1;
1750 uinfo->value.integer.min = 0;
1751 uinfo->value.integer.max = 1;
1752 return 0;
1753}
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001754EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
1755
Takashi Iwai12cddbd2014-10-30 13:44:34 +01001756/**
1757 * snd_ctl_boolean_stereo_info - Helper function for a standard boolean info
1758 * callback with stereo two channels
1759 * @kcontrol: the kcontrol instance
1760 * @uinfo: info to store
1761 *
1762 * This is a function that can be used as info callback for a standard
1763 * boolean control with stereo two channels.
1764 */
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001765int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
1766 struct snd_ctl_elem_info *uinfo)
1767{
1768 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1769 uinfo->count = 2;
1770 uinfo->value.integer.min = 0;
1771 uinfo->value.integer.max = 1;
1772 return 0;
1773}
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001774EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
Clemens Ladisch96007322011-01-10 16:25:44 +01001775
1776/**
1777 * snd_ctl_enum_info - fills the info structure for an enumerated control
1778 * @info: the structure to be filled
1779 * @channels: the number of the control's channels; often one
1780 * @items: the number of control values; also the size of @names
1781 * @names: an array containing the names of all control values
1782 *
1783 * Sets all required fields in @info to their appropriate values.
1784 * If the control's accessibility is not the default (readable and writable),
1785 * the caller has to fill @info->access.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001786 *
1787 * Return: Zero.
Clemens Ladisch96007322011-01-10 16:25:44 +01001788 */
1789int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
1790 unsigned int items, const char *const names[])
1791{
1792 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1793 info->count = channels;
1794 info->value.enumerated.items = items;
Takashi Iwaia7e6fb92014-10-20 18:08:50 +02001795 if (!items)
1796 return 0;
Clemens Ladisch96007322011-01-10 16:25:44 +01001797 if (info->value.enumerated.item >= items)
1798 info->value.enumerated.item = items - 1;
Takashi Iwaidf803e12014-10-20 18:07:21 +02001799 WARN(strlen(names[info->value.enumerated.item]) >= sizeof(info->value.enumerated.name),
1800 "ALSA: too long item name '%s'\n",
1801 names[info->value.enumerated.item]);
Clemens Ladisch96007322011-01-10 16:25:44 +01001802 strlcpy(info->value.enumerated.name,
1803 names[info->value.enumerated.item],
1804 sizeof(info->value.enumerated.name));
1805 return 0;
1806}
1807EXPORT_SYMBOL(snd_ctl_enum_info);