blob: 7e86a5b9f3b572f9c97f351027ed4e1b9b7b75b3 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 int err;
54
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 Iwai2529bba2006-08-04 12:57:19 +020082 ctl->prefer_pcm_subdevice = -1;
83 ctl->prefer_rawmidi_subdevice = -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);
89 return 0;
90
91 __error:
92 module_put(card->module);
93 __error2:
94 snd_card_file_remove(card, file);
95 __error1:
96 return err;
97}
98
Takashi Iwai82e9bae2005-11-17 13:53:23 +010099static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Borislav Petkov7507e8d2007-10-22 17:11:09 +0200101 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100102 struct snd_kctl_event *cread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Borislav Petkov7507e8d2007-10-22 17:11:09 +0200104 spin_lock_irqsave(&ctl->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 while (!list_empty(&ctl->events)) {
106 cread = snd_kctl_event(ctl->events.next);
107 list_del(&cread->list);
108 kfree(cread);
109 }
Borislav Petkov7507e8d2007-10-22 17:11:09 +0200110 spin_unlock_irqrestore(&ctl->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113static int snd_ctl_release(struct inode *inode, struct file *file)
114{
115 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100116 struct snd_card *card;
117 struct snd_ctl_file *ctl;
118 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 unsigned int idx;
120
121 ctl = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 file->private_data = NULL;
123 card = ctl->card;
124 write_lock_irqsave(&card->ctl_files_rwlock, flags);
125 list_del(&ctl->list);
126 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
127 down_write(&card->controls_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200128 list_for_each_entry(control, &card->controls, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 for (idx = 0; idx < control->count; idx++)
130 if (control->vd[idx].owner == ctl)
131 control->vd[idx].owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 up_write(&card->controls_rwsem);
133 snd_ctl_empty_read_queue(ctl);
Clemens Ladisch25d27ed2009-11-02 09:35:44 +0100134 put_pid(ctl->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 kfree(ctl);
136 module_put(card->module);
137 snd_card_file_remove(card, file);
138 return 0;
139}
140
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100141void snd_ctl_notify(struct snd_card *card, unsigned int mask,
142 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100145 struct snd_ctl_file *ctl;
146 struct snd_kctl_event *ev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200148 if (snd_BUG_ON(!card || !id))
149 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 read_lock(&card->ctl_files_rwlock);
151#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
152 card->mixer_oss_change_count++;
153#endif
Johannes Berg9244b2c2006-10-05 16:02:22 +0200154 list_for_each_entry(ctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (!ctl->subscribed)
156 continue;
157 spin_lock_irqsave(&ctl->read_lock, flags);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200158 list_for_each_entry(ev, &ctl->events, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (ev->id.numid == id->numid) {
160 ev->mask |= mask;
161 goto _found;
162 }
163 }
Takashi Iwaica2c0962005-09-09 14:20:23 +0200164 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (ev) {
166 ev->id = *id;
167 ev->mask = mask;
168 list_add_tail(&ev->list, &ctl->events);
169 } else {
170 snd_printk(KERN_ERR "No memory available to allocate event\n");
171 }
172 _found:
173 wake_up(&ctl->change_sleep);
174 spin_unlock_irqrestore(&ctl->read_lock, flags);
175 kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
176 }
177 read_unlock(&card->ctl_files_rwlock);
178}
179
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200180EXPORT_SYMBOL(snd_ctl_notify);
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/**
183 * snd_ctl_new - create a control instance from the template
184 * @control: the control template
185 * @access: the default control access
186 *
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100187 * Allocates a new struct snd_kcontrol instance and copies the given template
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * to the new instance. It does not copy volatile data (access).
189 *
190 * Returns the pointer of the new instance, or NULL on failure.
191 */
Adrian Bunk0b51ba02006-11-20 17:50:17 +0100192static struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control,
193 unsigned int access)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100195 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 unsigned int idx;
197
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200198 if (snd_BUG_ON(!control || !control->count))
199 return NULL;
Dan Rosenberg5591bf02010-09-28 14:18:20 -0400200
201 if (control->count > MAX_CONTROL_COUNT)
202 return NULL;
203
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100204 kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100205 if (kctl == NULL) {
206 snd_printk(KERN_ERR "Cannot allocate control instance\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return NULL;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 *kctl = *control;
210 for (idx = 0; idx < kctl->count; idx++)
211 kctl->vd[idx].access = access;
212 return kctl;
213}
214
215/**
216 * snd_ctl_new1 - create a control instance from the template
217 * @ncontrol: the initialization record
218 * @private_data: the private data to set
219 *
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100220 * Allocates a new struct snd_kcontrol instance and initialize from the given
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * template. When the access field of ncontrol is 0, it's assumed as
222 * READWRITE access. When the count field is 0, it's assumes as one.
223 *
224 * Returns the pointer of the newly generated instance, or NULL on failure.
225 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100226struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
227 void *private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100229 struct snd_kcontrol kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 unsigned int access;
231
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200232 if (snd_BUG_ON(!ncontrol || !ncontrol->info))
233 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 memset(&kctl, 0, sizeof(kctl));
235 kctl.id.iface = ncontrol->iface;
236 kctl.id.device = ncontrol->device;
237 kctl.id.subdevice = ncontrol->subdevice;
Mark Brown366840d2008-10-29 14:40:30 +0000238 if (ncontrol->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
Mark Brown366840d2008-10-29 14:40:30 +0000240 if (strcmp(ncontrol->name, kctl.id.name) != 0)
241 snd_printk(KERN_WARNING
242 "Control name '%s' truncated to '%s'\n",
243 ncontrol->name, kctl.id.name);
244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 kctl.id.index = ncontrol->index;
246 kctl.count = ncontrol->count ? ncontrol->count : 1;
247 access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200248 (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
Takashi Iwaia8d372f2012-07-30 13:47:07 +0200249 SNDRV_CTL_ELEM_ACCESS_VOLATILE|
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200250 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
Clemens Ladischa75d7a42010-02-01 13:29:50 +0100251 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE|
252 SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND|
253 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 kctl.info = ncontrol->info;
255 kctl.get = ncontrol->get;
256 kctl.put = ncontrol->put;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200257 kctl.tlv.p = ncontrol->tlv.p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 kctl.private_value = ncontrol->private_value;
259 kctl.private_data = private_data;
260 return snd_ctl_new(&kctl, access);
261}
262
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200263EXPORT_SYMBOL(snd_ctl_new1);
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/**
266 * snd_ctl_free_one - release the control instance
267 * @kcontrol: the control instance
268 *
269 * Releases the control instance created via snd_ctl_new()
270 * or snd_ctl_new1().
271 * Don't call this after the control was added to the card.
272 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100273void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 if (kcontrol) {
276 if (kcontrol->private_free)
277 kcontrol->private_free(kcontrol);
278 kfree(kcontrol);
279 }
280}
281
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200282EXPORT_SYMBOL(snd_ctl_free_one);
283
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100284static bool snd_ctl_remove_numid_conflict(struct snd_card *card,
285 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100287 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Johannes Berg9244b2c2006-10-05 16:02:22 +0200289 list_for_each_entry(kctl, &card->controls, list) {
Clemens Ladisch7c733582011-03-07 13:22:50 +0100290 if (kctl->id.numid < card->last_numid + 1 + count &&
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100291 kctl->id.numid + kctl->count > card->last_numid + 1) {
292 card->last_numid = kctl->id.numid + kctl->count - 1;
293 return true;
294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100296 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100299static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100301 unsigned int iter = 100000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100303 while (snd_ctl_remove_numid_conflict(card, count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (--iter == 0) {
305 /* this situation is very unlikely */
306 snd_printk(KERN_ERR "unable to allocate new control numid\n");
307 return -ENOMEM;
308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310 return 0;
311}
312
313/**
314 * snd_ctl_add - add the control instance to the card
315 * @card: the card instance
316 * @kcontrol: the control instance to add
317 *
318 * Adds the control instance created via snd_ctl_new() or
319 * snd_ctl_new1() to the given card. Assigns also an unique
320 * numid used for fast search.
321 *
322 * Returns zero if successful, or a negative error code on failure.
323 *
324 * It frees automatically the control which cannot be added.
325 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100326int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100328 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 unsigned int idx;
Takashi Iwaic6077b32006-03-21 16:07:13 +0100330 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100332 if (! kcontrol)
Takashi Iwaic6077b32006-03-21 16:07:13 +0100333 return err;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200334 if (snd_BUG_ON(!card || !kcontrol->info))
335 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 id = kcontrol->id;
337 down_write(&card->controls_rwsem);
338 if (snd_ctl_find_id(card, &id)) {
339 up_write(&card->controls_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
341 id.iface,
342 id.device,
343 id.subdevice,
344 id.name,
345 id.index);
Takashi Iwaic6077b32006-03-21 16:07:13 +0100346 err = -EBUSY;
347 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
350 up_write(&card->controls_rwsem);
Takashi Iwaic6077b32006-03-21 16:07:13 +0100351 err = -ENOMEM;
352 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 list_add_tail(&kcontrol->list, &card->controls);
355 card->controls_count += kcontrol->count;
356 kcontrol->id.numid = card->last_numid + 1;
357 card->last_numid += kcontrol->count;
358 up_write(&card->controls_rwsem);
359 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
360 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
361 return 0;
Takashi Iwaic6077b32006-03-21 16:07:13 +0100362
363 error:
364 snd_ctl_free_one(kcontrol);
365 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200368EXPORT_SYMBOL(snd_ctl_add);
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370/**
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000371 * snd_ctl_replace - replace the control instance of the card
372 * @card: the card instance
373 * @kcontrol: the control instance to replace
374 * @add_on_replace: add the control if not already added
375 *
376 * Replaces the given control. If the given control does not exist
377 * and the add_on_replace flag is set, the control is added. If the
378 * control exists, it is destroyed first.
379 *
380 * Returns zero if successful, or a negative error code on failure.
381 *
382 * It frees automatically the control which cannot be added or replaced.
383 */
384int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol,
385 bool add_on_replace)
386{
387 struct snd_ctl_elem_id id;
388 unsigned int idx;
389 struct snd_kcontrol *old;
390 int ret;
391
392 if (!kcontrol)
393 return -EINVAL;
394 if (snd_BUG_ON(!card || !kcontrol->info)) {
395 ret = -EINVAL;
396 goto error;
397 }
398 id = kcontrol->id;
399 down_write(&card->controls_rwsem);
400 old = snd_ctl_find_id(card, &id);
401 if (!old) {
402 if (add_on_replace)
403 goto add;
404 up_write(&card->controls_rwsem);
405 ret = -EINVAL;
406 goto error;
407 }
408 ret = snd_ctl_remove(card, old);
409 if (ret < 0) {
410 up_write(&card->controls_rwsem);
411 goto error;
412 }
413add:
414 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
415 up_write(&card->controls_rwsem);
416 ret = -ENOMEM;
417 goto error;
418 }
419 list_add_tail(&kcontrol->list, &card->controls);
420 card->controls_count += kcontrol->count;
421 kcontrol->id.numid = card->last_numid + 1;
422 card->last_numid += kcontrol->count;
423 up_write(&card->controls_rwsem);
424 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
425 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
426 return 0;
427
428error:
429 snd_ctl_free_one(kcontrol);
430 return ret;
431}
432EXPORT_SYMBOL(snd_ctl_replace);
433
434/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 * snd_ctl_remove - remove the control from the card and release it
436 * @card: the card instance
437 * @kcontrol: the control instance to remove
438 *
439 * Removes the control from the card and then releases the instance.
440 * You don't need to call snd_ctl_free_one(). You must be in
441 * the write lock - down_write(&card->controls_rwsem).
442 *
443 * Returns 0 if successful, or a negative error code on failure.
444 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100445int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100447 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 unsigned int idx;
449
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200450 if (snd_BUG_ON(!card || !kcontrol))
451 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 list_del(&kcontrol->list);
453 card->controls_count -= kcontrol->count;
454 id = kcontrol->id;
455 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
456 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
457 snd_ctl_free_one(kcontrol);
458 return 0;
459}
460
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200461EXPORT_SYMBOL(snd_ctl_remove);
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463/**
464 * snd_ctl_remove_id - remove the control of the given id and release it
465 * @card: the card instance
466 * @id: the control id to remove
467 *
468 * Finds the control instance with the given id, removes it from the
469 * card list and releases it.
470 *
471 * Returns 0 if successful, or a negative error code on failure.
472 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100473int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100475 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 int ret;
477
478 down_write(&card->controls_rwsem);
479 kctl = snd_ctl_find_id(card, id);
480 if (kctl == NULL) {
481 up_write(&card->controls_rwsem);
482 return -ENOENT;
483 }
484 ret = snd_ctl_remove(card, kctl);
485 up_write(&card->controls_rwsem);
486 return ret;
487}
488
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200489EXPORT_SYMBOL(snd_ctl_remove_id);
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491/**
Clemens Ladischf217ac52009-08-17 12:27:22 +0200492 * snd_ctl_remove_user_ctl - remove and release the unlocked user control
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 * @file: active control handle
494 * @id: the control id to remove
495 *
496 * Finds the control instance with the given id, removes it from the
497 * card list and releases it.
498 *
499 * Returns 0 if successful, or a negative error code on failure.
500 */
Clemens Ladischf217ac52009-08-17 12:27:22 +0200501static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
502 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100504 struct snd_card *card = file->card;
505 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 int idx, ret;
507
508 down_write(&card->controls_rwsem);
509 kctl = snd_ctl_find_id(card, id);
510 if (kctl == NULL) {
Clemens Ladisch317b8082009-08-17 12:26:34 +0200511 ret = -ENOENT;
512 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
Clemens Ladisch18dd0aa2009-08-17 12:28:09 +0200514 if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
515 ret = -EINVAL;
516 goto error;
517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 for (idx = 0; idx < kctl->count; idx++)
519 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
Clemens Ladisch317b8082009-08-17 12:26:34 +0200520 ret = -EBUSY;
521 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523 ret = snd_ctl_remove(card, kctl);
Clemens Ladischf217ac52009-08-17 12:27:22 +0200524 if (ret < 0)
525 goto error;
526 card->user_ctl_count--;
Clemens Ladisch317b8082009-08-17 12:26:34 +0200527error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 up_write(&card->controls_rwsem);
529 return ret;
530}
531
532/**
Takashi Iwai3cbdd752008-08-29 16:09:01 +0200533 * snd_ctl_activate_id - activate/inactivate the control of the given id
534 * @card: the card instance
535 * @id: the control id to activate/inactivate
536 * @active: non-zero to activate
537 *
538 * Finds the control instance with the given id, and activate or
539 * inactivate the control together with notification, if changed.
540 *
541 * Returns 0 if unchanged, 1 if changed, or a negative error code on failure.
542 */
543int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
544 int active)
545{
546 struct snd_kcontrol *kctl;
547 struct snd_kcontrol_volatile *vd;
548 unsigned int index_offset;
549 int ret;
550
551 down_write(&card->controls_rwsem);
552 kctl = snd_ctl_find_id(card, id);
553 if (kctl == NULL) {
554 ret = -ENOENT;
555 goto unlock;
556 }
557 index_offset = snd_ctl_get_ioff(kctl, &kctl->id);
558 vd = &kctl->vd[index_offset];
559 ret = 0;
560 if (active) {
561 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
562 goto unlock;
563 vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
564 } else {
565 if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)
566 goto unlock;
567 vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
568 }
569 ret = 1;
570 unlock:
571 up_write(&card->controls_rwsem);
572 if (ret > 0)
573 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, id);
574 return ret;
575}
576EXPORT_SYMBOL_GPL(snd_ctl_activate_id);
577
578/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 * snd_ctl_rename_id - replace the id of a control on the card
580 * @card: the card instance
581 * @src_id: the old id
582 * @dst_id: the new id
583 *
584 * Finds the control with the old id from the card, and replaces the
585 * id with the new one.
586 *
587 * Returns zero if successful, or a negative error code on failure.
588 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100589int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
590 struct snd_ctl_elem_id *dst_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100592 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 down_write(&card->controls_rwsem);
595 kctl = snd_ctl_find_id(card, src_id);
596 if (kctl == NULL) {
597 up_write(&card->controls_rwsem);
598 return -ENOENT;
599 }
600 kctl->id = *dst_id;
601 kctl->id.numid = card->last_numid + 1;
602 card->last_numid += kctl->count;
603 up_write(&card->controls_rwsem);
604 return 0;
605}
606
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200607EXPORT_SYMBOL(snd_ctl_rename_id);
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609/**
610 * snd_ctl_find_numid - find the control instance with the given number-id
611 * @card: the card instance
612 * @numid: the number-id to search
613 *
614 * Finds the control instance with the given number-id from the card.
615 *
616 * Returns the pointer of the instance if found, or NULL if not.
617 *
618 * The caller must down card->controls_rwsem before calling this function
619 * (if the race condition can happen).
620 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100621struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100623 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200625 if (snd_BUG_ON(!card || !numid))
626 return NULL;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200627 list_for_each_entry(kctl, &card->controls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
629 return kctl;
630 }
631 return NULL;
632}
633
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200634EXPORT_SYMBOL(snd_ctl_find_numid);
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636/**
637 * snd_ctl_find_id - find the control instance with the given id
638 * @card: the card instance
639 * @id: the id to search
640 *
641 * Finds the control instance with the given id from the card.
642 *
643 * Returns the pointer of the instance if found, or NULL if not.
644 *
645 * The caller must down card->controls_rwsem before calling this function
646 * (if the race condition can happen).
647 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100648struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
649 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100651 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200653 if (snd_BUG_ON(!card || !id))
654 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (id->numid != 0)
656 return snd_ctl_find_numid(card, id->numid);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200657 list_for_each_entry(kctl, &card->controls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (kctl->id.iface != id->iface)
659 continue;
660 if (kctl->id.device != id->device)
661 continue;
662 if (kctl->id.subdevice != id->subdevice)
663 continue;
664 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
665 continue;
666 if (kctl->id.index > id->index)
667 continue;
668 if (kctl->id.index + kctl->count <= id->index)
669 continue;
670 return kctl;
671 }
672 return NULL;
673}
674
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200675EXPORT_SYMBOL(snd_ctl_find_id);
676
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100677static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 unsigned int cmd, void __user *arg)
679{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100680 struct snd_ctl_card_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Takashi Iwaica2c0962005-09-09 14:20:23 +0200682 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (! info)
684 return -ENOMEM;
685 down_read(&snd_ioctl_rwsem);
686 info->card = card->number;
687 strlcpy(info->id, card->id, sizeof(info->id));
688 strlcpy(info->driver, card->driver, sizeof(info->driver));
689 strlcpy(info->name, card->shortname, sizeof(info->name));
690 strlcpy(info->longname, card->longname, sizeof(info->longname));
691 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
692 strlcpy(info->components, card->components, sizeof(info->components));
693 up_read(&snd_ioctl_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100694 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 kfree(info);
696 return -EFAULT;
697 }
698 kfree(info);
699 return 0;
700}
701
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100702static int snd_ctl_elem_list(struct snd_card *card,
703 struct snd_ctl_elem_list __user *_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 struct list_head *plist;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100706 struct snd_ctl_elem_list list;
707 struct snd_kcontrol *kctl;
708 struct snd_ctl_elem_id *dst, *id;
Luca Tettamanti78fa2c42011-05-25 22:43:27 +0200709 unsigned int offset, space, jidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 if (copy_from_user(&list, _list, sizeof(list)))
712 return -EFAULT;
713 offset = list.offset;
714 space = list.space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 /* try limit maximum space */
716 if (space > 16384)
717 return -ENOMEM;
718 if (space > 0) {
719 /* allocate temporary buffer for atomic operation */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100720 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (dst == NULL)
722 return -ENOMEM;
723 down_read(&card->controls_rwsem);
724 list.count = card->controls_count;
725 plist = card->controls.next;
726 while (plist != &card->controls) {
727 if (offset == 0)
728 break;
729 kctl = snd_kcontrol(plist);
730 if (offset < kctl->count)
731 break;
732 offset -= kctl->count;
733 plist = plist->next;
734 }
735 list.used = 0;
736 id = dst;
737 while (space > 0 && plist != &card->controls) {
738 kctl = snd_kcontrol(plist);
739 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
740 snd_ctl_build_ioff(id, kctl, jidx);
741 id++;
742 space--;
743 list.used++;
744 }
745 plist = plist->next;
746 offset = 0;
747 }
748 up_read(&card->controls_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100749 if (list.used > 0 &&
750 copy_to_user(list.pids, dst,
751 list.used * sizeof(struct snd_ctl_elem_id))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 vfree(dst);
753 return -EFAULT;
754 }
755 vfree(dst);
756 } else {
757 down_read(&card->controls_rwsem);
758 list.count = card->controls_count;
759 up_read(&card->controls_rwsem);
760 }
761 if (copy_to_user(_list, &list, sizeof(list)))
762 return -EFAULT;
763 return 0;
764}
765
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100766static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
767 struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100769 struct snd_card *card = ctl->card;
770 struct snd_kcontrol *kctl;
771 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 unsigned int index_offset;
773 int result;
774
775 down_read(&card->controls_rwsem);
776 kctl = snd_ctl_find_id(card, &info->id);
777 if (kctl == NULL) {
778 up_read(&card->controls_rwsem);
779 return -ENOENT;
780 }
781#ifdef CONFIG_SND_DEBUG
782 info->access = 0;
783#endif
784 result = kctl->info(kctl, info);
785 if (result >= 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200786 snd_BUG_ON(info->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 index_offset = snd_ctl_get_ioff(kctl, &info->id);
788 vd = &kctl->vd[index_offset];
789 snd_ctl_build_ioff(&info->id, kctl, index_offset);
790 info->access = vd->access;
791 if (vd->owner) {
792 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
793 if (vd->owner == ctl)
794 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
Clemens Ladisch25d27ed2009-11-02 09:35:44 +0100795 info->owner = pid_vnr(vd->owner->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 } else {
797 info->owner = -1;
798 }
799 }
800 up_read(&card->controls_rwsem);
801 return result;
802}
803
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100804static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
805 struct snd_ctl_elem_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100807 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 int result;
809
810 if (copy_from_user(&info, _info, sizeof(info)))
811 return -EFAULT;
Giuliano Pochini64649402006-03-13 14:11:11 +0100812 snd_power_lock(ctl->card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200813 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100814 if (result >= 0)
815 result = snd_ctl_elem_info(ctl, &info);
816 snd_power_unlock(ctl->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (result >= 0)
818 if (copy_to_user(_info, &info, sizeof(info)))
819 return -EFAULT;
820 return result;
821}
822
Takashi Iwaid3bd67c2008-06-12 18:17:26 +0200823static int snd_ctl_elem_read(struct snd_card *card,
824 struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100826 struct snd_kcontrol *kctl;
827 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 unsigned int index_offset;
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100829 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 down_read(&card->controls_rwsem);
832 kctl = snd_ctl_find_id(card, &control->id);
833 if (kctl == NULL) {
834 result = -ENOENT;
835 } else {
836 index_offset = snd_ctl_get_ioff(kctl, &control->id);
837 vd = &kctl->vd[index_offset];
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100838 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) &&
839 kctl->get != NULL) {
840 snd_ctl_build_ioff(&control->id, kctl, index_offset);
841 result = kctl->get(kctl, control);
842 } else
843 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845 up_read(&card->controls_rwsem);
846 return result;
847}
848
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100849static int snd_ctl_elem_read_user(struct snd_card *card,
850 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100852 struct snd_ctl_elem_value *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 int result;
Li Zefanef44a1e2009-04-10 09:43:08 +0800854
855 control = memdup_user(_control, sizeof(*control));
856 if (IS_ERR(control))
857 return PTR_ERR(control);
858
Giuliano Pochini64649402006-03-13 14:11:11 +0100859 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200860 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100861 if (result >= 0)
862 result = snd_ctl_elem_read(card, control);
863 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (result >= 0)
865 if (copy_to_user(_control, control, sizeof(*control)))
866 result = -EFAULT;
867 kfree(control);
868 return result;
869}
870
Takashi Iwaid3bd67c2008-06-12 18:17:26 +0200871static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
872 struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100874 struct snd_kcontrol *kctl;
875 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 unsigned int index_offset;
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100877 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 down_read(&card->controls_rwsem);
880 kctl = snd_ctl_find_id(card, &control->id);
881 if (kctl == NULL) {
882 result = -ENOENT;
883 } else {
884 index_offset = snd_ctl_get_ioff(kctl, &control->id);
885 vd = &kctl->vd[index_offset];
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100886 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
887 kctl->put == NULL ||
888 (file && vd->owner && vd->owner != file)) {
889 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 } else {
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100891 snd_ctl_build_ioff(&control->id, kctl, index_offset);
892 result = kctl->put(kctl, control);
893 }
894 if (result > 0) {
895 up_read(&card->controls_rwsem);
896 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
897 &control->id);
898 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900 }
901 up_read(&card->controls_rwsem);
902 return result;
903}
904
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100905static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
906 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100908 struct snd_ctl_elem_value *control;
Giuliano Pochini64649402006-03-13 14:11:11 +0100909 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 int result;
911
Li Zefanef44a1e2009-04-10 09:43:08 +0800912 control = memdup_user(_control, sizeof(*control));
913 if (IS_ERR(control))
914 return PTR_ERR(control);
915
Giuliano Pochini64649402006-03-13 14:11:11 +0100916 card = file->card;
917 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200918 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100919 if (result >= 0)
920 result = snd_ctl_elem_write(card, file, control);
921 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (result >= 0)
923 if (copy_to_user(_control, control, sizeof(*control)))
924 result = -EFAULT;
925 kfree(control);
926 return result;
927}
928
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100929static int snd_ctl_elem_lock(struct snd_ctl_file *file,
930 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100932 struct snd_card *card = file->card;
933 struct snd_ctl_elem_id id;
934 struct snd_kcontrol *kctl;
935 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 int result;
937
938 if (copy_from_user(&id, _id, sizeof(id)))
939 return -EFAULT;
940 down_write(&card->controls_rwsem);
941 kctl = snd_ctl_find_id(card, &id);
942 if (kctl == NULL) {
943 result = -ENOENT;
944 } else {
945 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
946 if (vd->owner != NULL)
947 result = -EBUSY;
948 else {
949 vd->owner = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 result = 0;
951 }
952 }
953 up_write(&card->controls_rwsem);
954 return result;
955}
956
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100957static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
958 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100960 struct snd_card *card = file->card;
961 struct snd_ctl_elem_id id;
962 struct snd_kcontrol *kctl;
963 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 int result;
965
966 if (copy_from_user(&id, _id, sizeof(id)))
967 return -EFAULT;
968 down_write(&card->controls_rwsem);
969 kctl = snd_ctl_find_id(card, &id);
970 if (kctl == NULL) {
971 result = -ENOENT;
972 } else {
973 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
974 if (vd->owner == NULL)
975 result = -EINVAL;
976 else if (vd->owner != file)
977 result = -EPERM;
978 else {
979 vd->owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 result = 0;
981 }
982 }
983 up_write(&card->controls_rwsem);
984 return result;
985}
986
987struct user_element {
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100988 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 void *elem_data; /* element data */
990 unsigned long elem_data_size; /* size of element data in bytes */
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200991 void *tlv_data; /* TLV data */
992 unsigned long tlv_data_size; /* TLV data size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 void *priv_data; /* private data (like strings for enumerated type) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994};
995
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100996static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
997 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 struct user_element *ue = kcontrol->private_data;
1000
1001 *uinfo = ue->info;
1002 return 0;
1003}
1004
Clemens Ladisch8d448162011-10-07 22:38:59 +02001005static int snd_ctl_elem_user_enum_info(struct snd_kcontrol *kcontrol,
1006 struct snd_ctl_elem_info *uinfo)
1007{
1008 struct user_element *ue = kcontrol->private_data;
1009 const char *names;
1010 unsigned int item;
1011
1012 item = uinfo->value.enumerated.item;
1013
1014 *uinfo = ue->info;
1015
1016 item = min(item, uinfo->value.enumerated.items - 1);
1017 uinfo->value.enumerated.item = item;
1018
1019 names = ue->priv_data;
1020 for (; item > 0; --item)
1021 names += strlen(names) + 1;
1022 strcpy(uinfo->value.enumerated.name, names);
1023
1024 return 0;
1025}
1026
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001027static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
1028 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 struct user_element *ue = kcontrol->private_data;
1031
1032 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
1033 return 0;
1034}
1035
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001036static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
1037 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
1039 int change;
1040 struct user_element *ue = kcontrol->private_data;
1041
1042 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
1043 if (change)
1044 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
1045 return change;
1046}
1047
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001048static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol,
1049 int op_flag,
1050 unsigned int size,
1051 unsigned int __user *tlv)
1052{
1053 struct user_element *ue = kcontrol->private_data;
1054 int change = 0;
1055 void *new_data;
1056
1057 if (op_flag > 0) {
1058 if (size > 1024 * 128) /* sane value */
1059 return -EINVAL;
Li Zefanef44a1e2009-04-10 09:43:08 +08001060
1061 new_data = memdup_user(tlv, size);
1062 if (IS_ERR(new_data))
1063 return PTR_ERR(new_data);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001064 change = ue->tlv_data_size != size;
1065 if (!change)
1066 change = memcmp(ue->tlv_data, new_data, size);
1067 kfree(ue->tlv_data);
1068 ue->tlv_data = new_data;
1069 ue->tlv_data_size = size;
1070 } else {
Takashi Iwai18c1c3f2006-08-25 11:39:34 +02001071 if (! ue->tlv_data_size || ! ue->tlv_data)
1072 return -ENXIO;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001073 if (size < ue->tlv_data_size)
1074 return -ENOSPC;
1075 if (copy_to_user(tlv, ue->tlv_data, ue->tlv_data_size))
1076 return -EFAULT;
1077 }
1078 return change;
1079}
1080
Clemens Ladisch8d448162011-10-07 22:38:59 +02001081static int snd_ctl_elem_init_enum_names(struct user_element *ue)
1082{
1083 char *names, *p;
1084 size_t buf_len, name_len;
1085 unsigned int i;
Olof Johansson447c6f92011-11-05 22:51:54 +01001086 const uintptr_t user_ptrval = ue->info.value.enumerated.names_ptr;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001087
1088 if (ue->info.value.enumerated.names_length > 64 * 1024)
1089 return -EINVAL;
1090
Olof Johansson447c6f92011-11-05 22:51:54 +01001091 names = memdup_user((const void __user *)user_ptrval,
Clemens Ladisch8d448162011-10-07 22:38:59 +02001092 ue->info.value.enumerated.names_length);
1093 if (IS_ERR(names))
1094 return PTR_ERR(names);
1095
1096 /* check that there are enough valid names */
1097 buf_len = ue->info.value.enumerated.names_length;
1098 p = names;
1099 for (i = 0; i < ue->info.value.enumerated.items; ++i) {
1100 name_len = strnlen(p, buf_len);
1101 if (name_len == 0 || name_len >= 64 || name_len == buf_len) {
1102 kfree(names);
1103 return -EINVAL;
1104 }
1105 p += name_len + 1;
1106 buf_len -= name_len + 1;
1107 }
1108
1109 ue->priv_data = names;
1110 ue->info.value.enumerated.names_ptr = 0;
1111
1112 return 0;
1113}
1114
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001115static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001117 struct user_element *ue = kcontrol->private_data;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001118
1119 kfree(ue->tlv_data);
1120 kfree(ue->priv_data);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001121 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001124static int snd_ctl_elem_add(struct snd_ctl_file *file,
1125 struct snd_ctl_elem_info *info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001127 struct snd_card *card = file->card;
1128 struct snd_kcontrol kctl, *_kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 unsigned int access;
1130 long private_size;
1131 struct user_element *ue;
1132 int idx, err;
Lu Guanqun983929c2011-08-24 11:12:34 +08001133
Lu Guanqun08ede032011-08-24 14:45:10 +08001134 if (!replace && card->user_ctl_count >= MAX_USER_CONTROLS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return -ENOMEM;
Clemens Ladisch2a031ae2009-08-17 12:25:52 +02001136 if (info->count < 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 return -EINVAL;
1138 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001139 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001140 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
1141 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 info->id.numid = 0;
1143 memset(&kctl, 0, sizeof(kctl));
1144 down_write(&card->controls_rwsem);
1145 _kctl = snd_ctl_find_id(card, &info->id);
1146 err = 0;
1147 if (_kctl) {
1148 if (replace)
1149 err = snd_ctl_remove(card, _kctl);
1150 else
1151 err = -EBUSY;
1152 } else {
1153 if (replace)
1154 err = -ENOENT;
1155 }
1156 up_write(&card->controls_rwsem);
1157 if (err < 0)
1158 return err;
1159 memcpy(&kctl.id, &info->id, sizeof(info->id));
1160 kctl.count = info->owner ? info->owner : 1;
1161 access |= SNDRV_CTL_ELEM_ACCESS_USER;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001162 if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED)
1163 kctl.info = snd_ctl_elem_user_enum_info;
1164 else
1165 kctl.info = snd_ctl_elem_user_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1167 kctl.get = snd_ctl_elem_user_get;
1168 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1169 kctl.put = snd_ctl_elem_user_put;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001170 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
1171 kctl.tlv.c = snd_ctl_elem_user_tlv;
1172 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 switch (info->type) {
1175 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 case SNDRV_CTL_ELEM_TYPE_INTEGER:
1177 private_size = sizeof(long);
1178 if (info->count > 128)
1179 return -EINVAL;
1180 break;
1181 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
1182 private_size = sizeof(long long);
1183 if (info->count > 64)
1184 return -EINVAL;
1185 break;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001186 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
1187 private_size = sizeof(unsigned int);
1188 if (info->count > 128 || info->value.enumerated.items == 0)
1189 return -EINVAL;
1190 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 case SNDRV_CTL_ELEM_TYPE_BYTES:
1192 private_size = sizeof(unsigned char);
1193 if (info->count > 512)
1194 return -EINVAL;
1195 break;
1196 case SNDRV_CTL_ELEM_TYPE_IEC958:
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001197 private_size = sizeof(struct snd_aes_iec958);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (info->count != 1)
1199 return -EINVAL;
1200 break;
1201 default:
1202 return -EINVAL;
1203 }
1204 private_size *= info->count;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001205 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (ue == NULL)
1207 return -ENOMEM;
1208 ue->info = *info;
Takashi Iwai86148e82006-08-24 12:36:36 +02001209 ue->info.access = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 ue->elem_data = (char *)ue + sizeof(*ue);
1211 ue->elem_data_size = private_size;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001212 if (ue->info.type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
1213 err = snd_ctl_elem_init_enum_names(ue);
1214 if (err < 0) {
1215 kfree(ue);
1216 return err;
1217 }
1218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 kctl.private_free = snd_ctl_elem_user_free;
1220 _kctl = snd_ctl_new(&kctl, access);
1221 if (_kctl == NULL) {
Clemens Ladisch8d448162011-10-07 22:38:59 +02001222 kfree(ue->priv_data);
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001223 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 return -ENOMEM;
1225 }
1226 _kctl->private_data = ue;
1227 for (idx = 0; idx < _kctl->count; idx++)
1228 _kctl->vd[idx].owner = file;
1229 err = snd_ctl_add(card, _kctl);
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001230 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 down_write(&card->controls_rwsem);
1234 card->user_ctl_count++;
1235 up_write(&card->controls_rwsem);
1236
1237 return 0;
1238}
1239
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001240static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1241 struct snd_ctl_elem_info __user *_info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001243 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (copy_from_user(&info, _info, sizeof(info)))
1245 return -EFAULT;
1246 return snd_ctl_elem_add(file, &info, replace);
1247}
1248
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001249static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1250 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001252 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 if (copy_from_user(&id, _id, sizeof(id)))
1255 return -EFAULT;
Clemens Ladischf217ac52009-08-17 12:27:22 +02001256 return snd_ctl_remove_user_ctl(file, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001259static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
1261 int subscribe;
1262 if (get_user(subscribe, ptr))
1263 return -EFAULT;
1264 if (subscribe < 0) {
1265 subscribe = file->subscribed;
1266 if (put_user(subscribe, ptr))
1267 return -EFAULT;
1268 return 0;
1269 }
1270 if (subscribe) {
1271 file->subscribed = 1;
1272 return 0;
1273 } else if (file->subscribed) {
1274 snd_ctl_empty_read_queue(file);
1275 file->subscribed = 0;
1276 }
1277 return 0;
1278}
1279
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001280static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1281 struct snd_ctl_tlv __user *_tlv,
1282 int op_flag)
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001283{
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001284 struct snd_card *card = file->card;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001285 struct snd_ctl_tlv tlv;
1286 struct snd_kcontrol *kctl;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001287 struct snd_kcontrol_volatile *vd;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001288 unsigned int len;
1289 int err = 0;
1290
1291 if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
1292 return -EFAULT;
Clemens Ladisch61236372010-02-01 13:30:56 +01001293 if (tlv.length < sizeof(unsigned int) * 2)
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001294 return -EINVAL;
1295 down_read(&card->controls_rwsem);
1296 kctl = snd_ctl_find_numid(card, tlv.numid);
1297 if (kctl == NULL) {
1298 err = -ENOENT;
1299 goto __kctl_end;
1300 }
1301 if (kctl->tlv.p == NULL) {
1302 err = -ENXIO;
1303 goto __kctl_end;
1304 }
1305 vd = &kctl->vd[tlv.numid - kctl->id.numid];
1306 if ((op_flag == 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) == 0) ||
1307 (op_flag > 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) == 0) ||
1308 (op_flag < 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND) == 0)) {
1309 err = -ENXIO;
1310 goto __kctl_end;
1311 }
1312 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
Dan Carpenterbec145a2009-11-18 10:31:57 +02001313 if (vd->owner != NULL && vd->owner != file) {
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001314 err = -EPERM;
1315 goto __kctl_end;
1316 }
Jeffrin Josebd483d42012-03-07 22:57:39 +05301317 err = kctl->tlv.c(kctl, op_flag, tlv.length, _tlv->tlv);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001318 if (err > 0) {
1319 up_read(&card->controls_rwsem);
1320 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &kctl->id);
1321 return 0;
1322 }
1323 } else {
1324 if (op_flag) {
1325 err = -ENXIO;
1326 goto __kctl_end;
1327 }
1328 len = kctl->tlv.p[1] + 2 * sizeof(unsigned int);
1329 if (tlv.length < len) {
1330 err = -ENOMEM;
1331 goto __kctl_end;
1332 }
1333 if (copy_to_user(_tlv->tlv, kctl->tlv.p, len))
1334 err = -EFAULT;
1335 }
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001336 __kctl_end:
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001337 up_read(&card->controls_rwsem);
1338 return err;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001339}
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1342{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001343 struct snd_ctl_file *ctl;
1344 struct snd_card *card;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001345 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 void __user *argp = (void __user *)arg;
1347 int __user *ip = argp;
1348 int err;
1349
1350 ctl = file->private_data;
1351 card = ctl->card;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001352 if (snd_BUG_ON(!card))
1353 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 switch (cmd) {
1355 case SNDRV_CTL_IOCTL_PVERSION:
1356 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1357 case SNDRV_CTL_IOCTL_CARD_INFO:
1358 return snd_ctl_card_info(card, ctl, cmd, argp);
1359 case SNDRV_CTL_IOCTL_ELEM_LIST:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001360 return snd_ctl_elem_list(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 case SNDRV_CTL_IOCTL_ELEM_INFO:
1362 return snd_ctl_elem_info_user(ctl, argp);
1363 case SNDRV_CTL_IOCTL_ELEM_READ:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001364 return snd_ctl_elem_read_user(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1366 return snd_ctl_elem_write_user(ctl, argp);
1367 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1368 return snd_ctl_elem_lock(ctl, argp);
1369 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1370 return snd_ctl_elem_unlock(ctl, argp);
1371 case SNDRV_CTL_IOCTL_ELEM_ADD:
1372 return snd_ctl_elem_add_user(ctl, argp, 0);
1373 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1374 return snd_ctl_elem_add_user(ctl, argp, 1);
1375 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1376 return snd_ctl_elem_remove(ctl, argp);
1377 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1378 return snd_ctl_subscribe_events(ctl, ip);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001379 case SNDRV_CTL_IOCTL_TLV_READ:
1380 return snd_ctl_tlv_ioctl(ctl, argp, 0);
1381 case SNDRV_CTL_IOCTL_TLV_WRITE:
1382 return snd_ctl_tlv_ioctl(ctl, argp, 1);
1383 case SNDRV_CTL_IOCTL_TLV_COMMAND:
1384 return snd_ctl_tlv_ioctl(ctl, argp, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 case SNDRV_CTL_IOCTL_POWER:
Takashi Iwaia381a7a2005-11-17 15:55:49 +01001386 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 case SNDRV_CTL_IOCTL_POWER_STATE:
1388#ifdef CONFIG_PM
1389 return put_user(card->power_state, ip) ? -EFAULT : 0;
1390#else
1391 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1392#endif
1393 }
1394 down_read(&snd_ioctl_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001395 list_for_each_entry(p, &snd_control_ioctls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 err = p->fioctl(card, ctl, cmd, arg);
1397 if (err != -ENOIOCTLCMD) {
1398 up_read(&snd_ioctl_rwsem);
1399 return err;
1400 }
1401 }
1402 up_read(&snd_ioctl_rwsem);
Takashi Iwai6d85be62005-05-15 14:32:50 +02001403 snd_printdd("unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 return -ENOTTY;
1405}
1406
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001407static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1408 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001410 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 int err = 0;
1412 ssize_t result = 0;
1413
1414 ctl = file->private_data;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001415 if (snd_BUG_ON(!ctl || !ctl->card))
1416 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (!ctl->subscribed)
1418 return -EBADFD;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001419 if (count < sizeof(struct snd_ctl_event))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return -EINVAL;
1421 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001422 while (count >= sizeof(struct snd_ctl_event)) {
1423 struct snd_ctl_event ev;
1424 struct snd_kctl_event *kev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 while (list_empty(&ctl->events)) {
1426 wait_queue_t wait;
1427 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1428 err = -EAGAIN;
1429 goto __end_lock;
1430 }
1431 init_waitqueue_entry(&wait, current);
1432 add_wait_queue(&ctl->change_sleep, &wait);
1433 set_current_state(TASK_INTERRUPTIBLE);
1434 spin_unlock_irq(&ctl->read_lock);
1435 schedule();
1436 remove_wait_queue(&ctl->change_sleep, &wait);
1437 if (signal_pending(current))
Adrian Bunk0e5d7202006-11-07 13:42:54 +01001438 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 spin_lock_irq(&ctl->read_lock);
1440 }
1441 kev = snd_kctl_event(ctl->events.next);
1442 ev.type = SNDRV_CTL_EVENT_ELEM;
1443 ev.data.elem.mask = kev->mask;
1444 ev.data.elem.id = kev->id;
1445 list_del(&kev->list);
1446 spin_unlock_irq(&ctl->read_lock);
1447 kfree(kev);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001448 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 err = -EFAULT;
1450 goto __end;
1451 }
1452 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001453 buffer += sizeof(struct snd_ctl_event);
1454 count -= sizeof(struct snd_ctl_event);
1455 result += sizeof(struct snd_ctl_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
1457 __end_lock:
1458 spin_unlock_irq(&ctl->read_lock);
1459 __end:
1460 return result > 0 ? result : err;
1461}
1462
1463static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1464{
1465 unsigned int mask;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001466 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 ctl = file->private_data;
1469 if (!ctl->subscribed)
1470 return 0;
1471 poll_wait(file, &ctl->change_sleep, wait);
1472
1473 mask = 0;
1474 if (!list_empty(&ctl->events))
1475 mask |= POLLIN | POLLRDNORM;
1476
1477 return mask;
1478}
1479
1480/*
1481 * register the device-specific control-ioctls.
1482 * called from each device manager like pcm.c, hwdep.c, etc.
1483 */
1484static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1485{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001486 struct snd_kctl_ioctl *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001488 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 if (pn == NULL)
1490 return -ENOMEM;
1491 pn->fioctl = fcn;
1492 down_write(&snd_ioctl_rwsem);
1493 list_add_tail(&pn->list, lists);
1494 up_write(&snd_ioctl_rwsem);
1495 return 0;
1496}
1497
1498int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1499{
1500 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1501}
1502
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001503EXPORT_SYMBOL(snd_ctl_register_ioctl);
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505#ifdef CONFIG_COMPAT
1506int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1507{
1508 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1509}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001510
1511EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512#endif
1513
1514/*
1515 * de-register the device-specific control-ioctls.
1516 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001517static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1518 struct list_head *lists)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001520 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001522 if (snd_BUG_ON(!fcn))
1523 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 down_write(&snd_ioctl_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001525 list_for_each_entry(p, lists, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (p->fioctl == fcn) {
1527 list_del(&p->list);
1528 up_write(&snd_ioctl_rwsem);
1529 kfree(p);
1530 return 0;
1531 }
1532 }
1533 up_write(&snd_ioctl_rwsem);
1534 snd_BUG();
1535 return -EINVAL;
1536}
1537
1538int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1539{
1540 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1541}
1542
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001543EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545#ifdef CONFIG_COMPAT
1546int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1547{
1548 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1549}
1550
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001551EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552#endif
1553
1554static int snd_ctl_fasync(int fd, struct file * file, int on)
1555{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001556 struct snd_ctl_file *ctl;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 ctl = file->private_data;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001559 return fasync_helper(fd, file, on, &ctl->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
1562/*
1563 * ioctl32 compat
1564 */
1565#ifdef CONFIG_COMPAT
1566#include "control_compat.c"
1567#else
1568#define snd_ctl_ioctl_compat NULL
1569#endif
1570
1571/*
1572 * INIT PART
1573 */
1574
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001575static const struct file_operations snd_ctl_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
1577 .owner = THIS_MODULE,
1578 .read = snd_ctl_read,
1579 .open = snd_ctl_open,
1580 .release = snd_ctl_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02001581 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 .poll = snd_ctl_poll,
1583 .unlocked_ioctl = snd_ctl_ioctl,
1584 .compat_ioctl = snd_ctl_ioctl_compat,
1585 .fasync = snd_ctl_fasync,
1586};
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588/*
1589 * registration of the control device
1590 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001591static int snd_ctl_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001593 struct snd_card *card = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 int err, cardnum;
1595 char name[16];
1596
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001597 if (snd_BUG_ON(!card))
1598 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 cardnum = card->number;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001600 if (snd_BUG_ON(cardnum < 0 || cardnum >= SNDRV_CARDS))
1601 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 sprintf(name, "controlC%i", cardnum);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001603 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1604 &snd_ctl_f_ops, card, name)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 return err;
1606 return 0;
1607}
1608
1609/*
1610 * disconnection of the control device
1611 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001612static int snd_ctl_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001614 struct snd_card *card = device->device_data;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001615 struct snd_ctl_file *ctl;
Takashi Iwaic4614822006-06-23 14:38:23 +02001616 int err, cardnum;
1617
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001618 if (snd_BUG_ON(!card))
1619 return -ENXIO;
Takashi Iwaic4614822006-06-23 14:38:23 +02001620 cardnum = card->number;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001621 if (snd_BUG_ON(cardnum < 0 || cardnum >= SNDRV_CARDS))
1622 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Takashi Iwaid8009882008-09-07 12:51:13 +02001624 read_lock(&card->ctl_files_rwlock);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001625 list_for_each_entry(ctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 wake_up(&ctl->change_sleep);
1627 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1628 }
Takashi Iwaid8009882008-09-07 12:51:13 +02001629 read_unlock(&card->ctl_files_rwlock);
Takashi Iwaic4614822006-06-23 14:38:23 +02001630
1631 if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
1632 card, -1)) < 0)
1633 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 return 0;
1635}
1636
1637/*
1638 * free all controls
1639 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001640static int snd_ctl_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001642 struct snd_card *card = device->device_data;
1643 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 down_write(&card->controls_rwsem);
1646 while (!list_empty(&card->controls)) {
1647 control = snd_kcontrol(card->controls.next);
1648 snd_ctl_remove(card, control);
1649 }
1650 up_write(&card->controls_rwsem);
1651 return 0;
1652}
1653
1654/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 * create control core:
1656 * called from init.c
1657 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001658int snd_ctl_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001660 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 .dev_free = snd_ctl_dev_free,
1662 .dev_register = snd_ctl_dev_register,
1663 .dev_disconnect = snd_ctl_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 };
1665
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001666 if (snd_BUG_ON(!card))
1667 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
1669}
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001670
1671/*
Clemens Ladisch96007322011-01-10 16:25:44 +01001672 * Frequently used control callbacks/helpers
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001673 */
1674int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
1675 struct snd_ctl_elem_info *uinfo)
1676{
1677 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1678 uinfo->count = 1;
1679 uinfo->value.integer.min = 0;
1680 uinfo->value.integer.max = 1;
1681 return 0;
1682}
1683
1684EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
1685
1686int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
1687 struct snd_ctl_elem_info *uinfo)
1688{
1689 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1690 uinfo->count = 2;
1691 uinfo->value.integer.min = 0;
1692 uinfo->value.integer.max = 1;
1693 return 0;
1694}
1695
1696EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
Clemens Ladisch96007322011-01-10 16:25:44 +01001697
1698/**
1699 * snd_ctl_enum_info - fills the info structure for an enumerated control
1700 * @info: the structure to be filled
1701 * @channels: the number of the control's channels; often one
1702 * @items: the number of control values; also the size of @names
1703 * @names: an array containing the names of all control values
1704 *
1705 * Sets all required fields in @info to their appropriate values.
1706 * If the control's accessibility is not the default (readable and writable),
1707 * the caller has to fill @info->access.
1708 */
1709int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
1710 unsigned int items, const char *const names[])
1711{
1712 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1713 info->count = channels;
1714 info->value.enumerated.items = items;
1715 if (info->value.enumerated.item >= items)
1716 info->value.enumerated.item = items - 1;
1717 strlcpy(info->value.enumerated.name,
1718 names[info->value.enumerated.item],
1719 sizeof(info->value.enumerated.name));
1720 return 0;
1721}
1722EXPORT_SYMBOL(snd_ctl_enum_info);