blob: db51e4e6498438087aa9d8a35dcf26293799ad99 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/slab.h>
25#include <linux/vmalloc.h>
26#include <linux/time.h>
27#include <sound/core.h>
28#include <sound/minors.h>
29#include <sound/info.h>
30#include <sound/control.h>
31
32/* max number of user-defined controls */
33#define MAX_USER_CONTROLS 32
Dan Rosenberg5591bf02010-09-28 14:18:20 -040034#define MAX_CONTROL_COUNT 1028
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Takashi Iwai82e9bae2005-11-17 13:53:23 +010036struct snd_kctl_ioctl {
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 struct list_head list; /* list of all ioctls */
38 snd_kctl_ioctl_func_t fioctl;
Takashi Iwai82e9bae2005-11-17 13:53:23 +010039};
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41static DECLARE_RWSEM(snd_ioctl_rwsem);
42static LIST_HEAD(snd_control_ioctls);
43#ifdef CONFIG_COMPAT
44static LIST_HEAD(snd_control_compat_ioctls);
45#endif
46
47static int snd_ctl_open(struct inode *inode, struct file *file)
48{
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +010050 struct snd_card *card;
51 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 int err;
53
Takashi Iwai02f48652010-04-13 11:49:04 +020054 err = nonseekable_open(inode, file);
55 if (err < 0)
56 return err;
57
Clemens Ladischf87135f2005-11-20 14:06:59 +010058 card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (!card) {
60 err = -ENODEV;
61 goto __error1;
62 }
63 err = snd_card_file_add(card, file);
64 if (err < 0) {
65 err = -ENODEV;
66 goto __error1;
67 }
68 if (!try_module_get(card->module)) {
69 err = -EFAULT;
70 goto __error2;
71 }
Takashi Iwaica2c0962005-09-09 14:20:23 +020072 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (ctl == NULL) {
74 err = -ENOMEM;
75 goto __error;
76 }
77 INIT_LIST_HEAD(&ctl->events);
78 init_waitqueue_head(&ctl->change_sleep);
79 spin_lock_init(&ctl->read_lock);
80 ctl->card = card;
Takashi Iwai2529bba2006-08-04 12:57:19 +020081 ctl->prefer_pcm_subdevice = -1;
82 ctl->prefer_rawmidi_subdevice = -1;
Clemens Ladisch25d27ed2009-11-02 09:35:44 +010083 ctl->pid = get_pid(task_pid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 file->private_data = ctl;
85 write_lock_irqsave(&card->ctl_files_rwlock, flags);
86 list_add_tail(&ctl->list, &card->ctl_files);
87 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
88 return 0;
89
90 __error:
91 module_put(card->module);
92 __error2:
93 snd_card_file_remove(card, file);
94 __error1:
95 return err;
96}
97
Takashi Iwai82e9bae2005-11-17 13:53:23 +010098static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Borislav Petkov7507e8d2007-10-22 17:11:09 +0200100 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100101 struct snd_kctl_event *cread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Borislav Petkov7507e8d2007-10-22 17:11:09 +0200103 spin_lock_irqsave(&ctl->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 while (!list_empty(&ctl->events)) {
105 cread = snd_kctl_event(ctl->events.next);
106 list_del(&cread->list);
107 kfree(cread);
108 }
Borislav Petkov7507e8d2007-10-22 17:11:09 +0200109 spin_unlock_irqrestore(&ctl->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
112static int snd_ctl_release(struct inode *inode, struct file *file)
113{
114 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100115 struct snd_card *card;
116 struct snd_ctl_file *ctl;
117 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 unsigned int idx;
119
120 ctl = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 file->private_data = NULL;
122 card = ctl->card;
123 write_lock_irqsave(&card->ctl_files_rwlock, flags);
124 list_del(&ctl->list);
125 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
126 down_write(&card->controls_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200127 list_for_each_entry(control, &card->controls, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 for (idx = 0; idx < control->count; idx++)
129 if (control->vd[idx].owner == ctl)
130 control->vd[idx].owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 up_write(&card->controls_rwsem);
132 snd_ctl_empty_read_queue(ctl);
Clemens Ladisch25d27ed2009-11-02 09:35:44 +0100133 put_pid(ctl->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 kfree(ctl);
135 module_put(card->module);
136 snd_card_file_remove(card, file);
137 return 0;
138}
139
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100140void snd_ctl_notify(struct snd_card *card, unsigned int mask,
141 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100144 struct snd_ctl_file *ctl;
145 struct snd_kctl_event *ev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200147 if (snd_BUG_ON(!card || !id))
148 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 read_lock(&card->ctl_files_rwlock);
150#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
151 card->mixer_oss_change_count++;
152#endif
Johannes Berg9244b2c2006-10-05 16:02:22 +0200153 list_for_each_entry(ctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (!ctl->subscribed)
155 continue;
156 spin_lock_irqsave(&ctl->read_lock, flags);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200157 list_for_each_entry(ev, &ctl->events, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (ev->id.numid == id->numid) {
159 ev->mask |= mask;
160 goto _found;
161 }
162 }
Takashi Iwaica2c0962005-09-09 14:20:23 +0200163 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (ev) {
165 ev->id = *id;
166 ev->mask = mask;
167 list_add_tail(&ev->list, &ctl->events);
168 } else {
169 snd_printk(KERN_ERR "No memory available to allocate event\n");
170 }
171 _found:
172 wake_up(&ctl->change_sleep);
173 spin_unlock_irqrestore(&ctl->read_lock, flags);
174 kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
175 }
176 read_unlock(&card->ctl_files_rwlock);
177}
178
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200179EXPORT_SYMBOL(snd_ctl_notify);
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/**
182 * snd_ctl_new - create a control instance from the template
183 * @control: the control template
184 * @access: the default control access
185 *
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100186 * Allocates a new struct snd_kcontrol instance and copies the given template
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * to the new instance. It does not copy volatile data (access).
188 *
189 * Returns the pointer of the new instance, or NULL on failure.
190 */
Adrian Bunk0b51ba02006-11-20 17:50:17 +0100191static struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control,
192 unsigned int access)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100194 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 unsigned int idx;
196
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200197 if (snd_BUG_ON(!control || !control->count))
198 return NULL;
Dan Rosenberg5591bf02010-09-28 14:18:20 -0400199
200 if (control->count > MAX_CONTROL_COUNT)
201 return NULL;
202
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100203 kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100204 if (kctl == NULL) {
205 snd_printk(KERN_ERR "Cannot allocate control instance\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return NULL;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 *kctl = *control;
209 for (idx = 0; idx < kctl->count; idx++)
210 kctl->vd[idx].access = access;
211 return kctl;
212}
213
214/**
215 * snd_ctl_new1 - create a control instance from the template
216 * @ncontrol: the initialization record
217 * @private_data: the private data to set
218 *
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100219 * Allocates a new struct snd_kcontrol instance and initialize from the given
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 * template. When the access field of ncontrol is 0, it's assumed as
221 * READWRITE access. When the count field is 0, it's assumes as one.
222 *
223 * Returns the pointer of the newly generated instance, or NULL on failure.
224 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100225struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
226 void *private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100228 struct snd_kcontrol kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 unsigned int access;
230
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200231 if (snd_BUG_ON(!ncontrol || !ncontrol->info))
232 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 memset(&kctl, 0, sizeof(kctl));
234 kctl.id.iface = ncontrol->iface;
235 kctl.id.device = ncontrol->device;
236 kctl.id.subdevice = ncontrol->subdevice;
Mark Brown366840d2008-10-29 14:40:30 +0000237 if (ncontrol->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
Mark Brown366840d2008-10-29 14:40:30 +0000239 if (strcmp(ncontrol->name, kctl.id.name) != 0)
240 snd_printk(KERN_WARNING
241 "Control name '%s' truncated to '%s'\n",
242 ncontrol->name, kctl.id.name);
243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 kctl.id.index = ncontrol->index;
245 kctl.count = ncontrol->count ? ncontrol->count : 1;
246 access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200247 (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
248 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
Clemens Ladischa75d7a42010-02-01 13:29:50 +0100249 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE|
250 SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND|
251 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 kctl.info = ncontrol->info;
253 kctl.get = ncontrol->get;
254 kctl.put = ncontrol->put;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200255 kctl.tlv.p = ncontrol->tlv.p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 kctl.private_value = ncontrol->private_value;
257 kctl.private_data = private_data;
258 return snd_ctl_new(&kctl, access);
259}
260
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200261EXPORT_SYMBOL(snd_ctl_new1);
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263/**
264 * snd_ctl_free_one - release the control instance
265 * @kcontrol: the control instance
266 *
267 * Releases the control instance created via snd_ctl_new()
268 * or snd_ctl_new1().
269 * Don't call this after the control was added to the card.
270 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100271void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 if (kcontrol) {
274 if (kcontrol->private_free)
275 kcontrol->private_free(kcontrol);
276 kfree(kcontrol);
277 }
278}
279
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200280EXPORT_SYMBOL(snd_ctl_free_one);
281
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100282static unsigned int snd_ctl_hole_check(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 unsigned int count)
284{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100285 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Johannes Berg9244b2c2006-10-05 16:02:22 +0200287 list_for_each_entry(kctl, &card->controls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if ((kctl->id.numid <= card->last_numid &&
289 kctl->id.numid + kctl->count > card->last_numid) ||
290 (kctl->id.numid <= card->last_numid + count - 1 &&
291 kctl->id.numid + kctl->count > card->last_numid + count - 1))
292 return card->last_numid = kctl->id.numid + kctl->count - 1;
293 }
294 return card->last_numid;
295}
296
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100297static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 unsigned int last_numid, iter = 100000;
300
301 last_numid = card->last_numid;
302 while (last_numid != snd_ctl_hole_check(card, count)) {
303 if (--iter == 0) {
304 /* this situation is very unlikely */
305 snd_printk(KERN_ERR "unable to allocate new control numid\n");
306 return -ENOMEM;
307 }
308 last_numid = card->last_numid;
309 }
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/**
371 * snd_ctl_remove - remove the control from the card and release it
372 * @card: the card instance
373 * @kcontrol: the control instance to remove
374 *
375 * Removes the control from the card and then releases the instance.
376 * You don't need to call snd_ctl_free_one(). You must be in
377 * the write lock - down_write(&card->controls_rwsem).
378 *
379 * Returns 0 if successful, or a negative error code on failure.
380 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100381int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100383 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 unsigned int idx;
385
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200386 if (snd_BUG_ON(!card || !kcontrol))
387 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 list_del(&kcontrol->list);
389 card->controls_count -= kcontrol->count;
390 id = kcontrol->id;
391 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
392 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
393 snd_ctl_free_one(kcontrol);
394 return 0;
395}
396
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200397EXPORT_SYMBOL(snd_ctl_remove);
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399/**
400 * snd_ctl_remove_id - remove the control of the given id and release it
401 * @card: the card instance
402 * @id: the control id to remove
403 *
404 * Finds the control instance with the given id, removes it from the
405 * card list and releases it.
406 *
407 * Returns 0 if successful, or a negative error code on failure.
408 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100409int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100411 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 int ret;
413
414 down_write(&card->controls_rwsem);
415 kctl = snd_ctl_find_id(card, id);
416 if (kctl == NULL) {
417 up_write(&card->controls_rwsem);
418 return -ENOENT;
419 }
420 ret = snd_ctl_remove(card, kctl);
421 up_write(&card->controls_rwsem);
422 return ret;
423}
424
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200425EXPORT_SYMBOL(snd_ctl_remove_id);
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427/**
Clemens Ladischf217ac52009-08-17 12:27:22 +0200428 * snd_ctl_remove_user_ctl - remove and release the unlocked user control
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 * @file: active control handle
430 * @id: the control id to remove
431 *
432 * Finds the control instance with the given id, removes it from the
433 * card list and releases it.
434 *
435 * Returns 0 if successful, or a negative error code on failure.
436 */
Clemens Ladischf217ac52009-08-17 12:27:22 +0200437static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
438 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100440 struct snd_card *card = file->card;
441 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 int idx, ret;
443
444 down_write(&card->controls_rwsem);
445 kctl = snd_ctl_find_id(card, id);
446 if (kctl == NULL) {
Clemens Ladisch317b8082009-08-17 12:26:34 +0200447 ret = -ENOENT;
448 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
Clemens Ladisch18dd0aa2009-08-17 12:28:09 +0200450 if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
451 ret = -EINVAL;
452 goto error;
453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 for (idx = 0; idx < kctl->count; idx++)
455 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
Clemens Ladisch317b8082009-08-17 12:26:34 +0200456 ret = -EBUSY;
457 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459 ret = snd_ctl_remove(card, kctl);
Clemens Ladischf217ac52009-08-17 12:27:22 +0200460 if (ret < 0)
461 goto error;
462 card->user_ctl_count--;
Clemens Ladisch317b8082009-08-17 12:26:34 +0200463error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 up_write(&card->controls_rwsem);
465 return ret;
466}
467
468/**
Takashi Iwai3cbdd752008-08-29 16:09:01 +0200469 * snd_ctl_activate_id - activate/inactivate the control of the given id
470 * @card: the card instance
471 * @id: the control id to activate/inactivate
472 * @active: non-zero to activate
473 *
474 * Finds the control instance with the given id, and activate or
475 * inactivate the control together with notification, if changed.
476 *
477 * Returns 0 if unchanged, 1 if changed, or a negative error code on failure.
478 */
479int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
480 int active)
481{
482 struct snd_kcontrol *kctl;
483 struct snd_kcontrol_volatile *vd;
484 unsigned int index_offset;
485 int ret;
486
487 down_write(&card->controls_rwsem);
488 kctl = snd_ctl_find_id(card, id);
489 if (kctl == NULL) {
490 ret = -ENOENT;
491 goto unlock;
492 }
493 index_offset = snd_ctl_get_ioff(kctl, &kctl->id);
494 vd = &kctl->vd[index_offset];
495 ret = 0;
496 if (active) {
497 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
498 goto unlock;
499 vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
500 } else {
501 if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)
502 goto unlock;
503 vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
504 }
505 ret = 1;
506 unlock:
507 up_write(&card->controls_rwsem);
508 if (ret > 0)
509 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, id);
510 return ret;
511}
512EXPORT_SYMBOL_GPL(snd_ctl_activate_id);
513
514/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * snd_ctl_rename_id - replace the id of a control on the card
516 * @card: the card instance
517 * @src_id: the old id
518 * @dst_id: the new id
519 *
520 * Finds the control with the old id from the card, and replaces the
521 * id with the new one.
522 *
523 * Returns zero if successful, or a negative error code on failure.
524 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100525int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
526 struct snd_ctl_elem_id *dst_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100528 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 down_write(&card->controls_rwsem);
531 kctl = snd_ctl_find_id(card, src_id);
532 if (kctl == NULL) {
533 up_write(&card->controls_rwsem);
534 return -ENOENT;
535 }
536 kctl->id = *dst_id;
537 kctl->id.numid = card->last_numid + 1;
538 card->last_numid += kctl->count;
539 up_write(&card->controls_rwsem);
540 return 0;
541}
542
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200543EXPORT_SYMBOL(snd_ctl_rename_id);
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545/**
546 * snd_ctl_find_numid - find the control instance with the given number-id
547 * @card: the card instance
548 * @numid: the number-id to search
549 *
550 * Finds the control instance with the given number-id from the card.
551 *
552 * Returns the pointer of the instance if found, or NULL if not.
553 *
554 * The caller must down card->controls_rwsem before calling this function
555 * (if the race condition can happen).
556 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100557struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100559 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200561 if (snd_BUG_ON(!card || !numid))
562 return NULL;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200563 list_for_each_entry(kctl, &card->controls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
565 return kctl;
566 }
567 return NULL;
568}
569
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200570EXPORT_SYMBOL(snd_ctl_find_numid);
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572/**
573 * snd_ctl_find_id - find the control instance with the given id
574 * @card: the card instance
575 * @id: the id to search
576 *
577 * Finds the control instance with the given id from the card.
578 *
579 * Returns the pointer of the instance if found, or NULL if not.
580 *
581 * The caller must down card->controls_rwsem before calling this function
582 * (if the race condition can happen).
583 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100584struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
585 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100587 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200589 if (snd_BUG_ON(!card || !id))
590 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (id->numid != 0)
592 return snd_ctl_find_numid(card, id->numid);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200593 list_for_each_entry(kctl, &card->controls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (kctl->id.iface != id->iface)
595 continue;
596 if (kctl->id.device != id->device)
597 continue;
598 if (kctl->id.subdevice != id->subdevice)
599 continue;
600 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
601 continue;
602 if (kctl->id.index > id->index)
603 continue;
604 if (kctl->id.index + kctl->count <= id->index)
605 continue;
606 return kctl;
607 }
608 return NULL;
609}
610
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200611EXPORT_SYMBOL(snd_ctl_find_id);
612
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100613static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 unsigned int cmd, void __user *arg)
615{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100616 struct snd_ctl_card_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Takashi Iwaica2c0962005-09-09 14:20:23 +0200618 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (! info)
620 return -ENOMEM;
621 down_read(&snd_ioctl_rwsem);
622 info->card = card->number;
623 strlcpy(info->id, card->id, sizeof(info->id));
624 strlcpy(info->driver, card->driver, sizeof(info->driver));
625 strlcpy(info->name, card->shortname, sizeof(info->name));
626 strlcpy(info->longname, card->longname, sizeof(info->longname));
627 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
628 strlcpy(info->components, card->components, sizeof(info->components));
629 up_read(&snd_ioctl_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100630 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 kfree(info);
632 return -EFAULT;
633 }
634 kfree(info);
635 return 0;
636}
637
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100638static int snd_ctl_elem_list(struct snd_card *card,
639 struct snd_ctl_elem_list __user *_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 struct list_head *plist;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100642 struct snd_ctl_elem_list list;
643 struct snd_kcontrol *kctl;
644 struct snd_ctl_elem_id *dst, *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 unsigned int offset, space, first, jidx;
646
647 if (copy_from_user(&list, _list, sizeof(list)))
648 return -EFAULT;
649 offset = list.offset;
650 space = list.space;
651 first = 0;
652 /* try limit maximum space */
653 if (space > 16384)
654 return -ENOMEM;
655 if (space > 0) {
656 /* allocate temporary buffer for atomic operation */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100657 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (dst == NULL)
659 return -ENOMEM;
660 down_read(&card->controls_rwsem);
661 list.count = card->controls_count;
662 plist = card->controls.next;
663 while (plist != &card->controls) {
664 if (offset == 0)
665 break;
666 kctl = snd_kcontrol(plist);
667 if (offset < kctl->count)
668 break;
669 offset -= kctl->count;
670 plist = plist->next;
671 }
672 list.used = 0;
673 id = dst;
674 while (space > 0 && plist != &card->controls) {
675 kctl = snd_kcontrol(plist);
676 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
677 snd_ctl_build_ioff(id, kctl, jidx);
678 id++;
679 space--;
680 list.used++;
681 }
682 plist = plist->next;
683 offset = 0;
684 }
685 up_read(&card->controls_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100686 if (list.used > 0 &&
687 copy_to_user(list.pids, dst,
688 list.used * sizeof(struct snd_ctl_elem_id))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 vfree(dst);
690 return -EFAULT;
691 }
692 vfree(dst);
693 } else {
694 down_read(&card->controls_rwsem);
695 list.count = card->controls_count;
696 up_read(&card->controls_rwsem);
697 }
698 if (copy_to_user(_list, &list, sizeof(list)))
699 return -EFAULT;
700 return 0;
701}
702
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100703static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
704 struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100706 struct snd_card *card = ctl->card;
707 struct snd_kcontrol *kctl;
708 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 unsigned int index_offset;
710 int result;
711
712 down_read(&card->controls_rwsem);
713 kctl = snd_ctl_find_id(card, &info->id);
714 if (kctl == NULL) {
715 up_read(&card->controls_rwsem);
716 return -ENOENT;
717 }
718#ifdef CONFIG_SND_DEBUG
719 info->access = 0;
720#endif
721 result = kctl->info(kctl, info);
722 if (result >= 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200723 snd_BUG_ON(info->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 index_offset = snd_ctl_get_ioff(kctl, &info->id);
725 vd = &kctl->vd[index_offset];
726 snd_ctl_build_ioff(&info->id, kctl, index_offset);
727 info->access = vd->access;
728 if (vd->owner) {
729 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
730 if (vd->owner == ctl)
731 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
Clemens Ladisch25d27ed2009-11-02 09:35:44 +0100732 info->owner = pid_vnr(vd->owner->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 } else {
734 info->owner = -1;
735 }
736 }
737 up_read(&card->controls_rwsem);
738 return result;
739}
740
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100741static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
742 struct snd_ctl_elem_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100744 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 int result;
746
747 if (copy_from_user(&info, _info, sizeof(info)))
748 return -EFAULT;
Giuliano Pochini64649402006-03-13 14:11:11 +0100749 snd_power_lock(ctl->card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200750 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100751 if (result >= 0)
752 result = snd_ctl_elem_info(ctl, &info);
753 snd_power_unlock(ctl->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (result >= 0)
755 if (copy_to_user(_info, &info, sizeof(info)))
756 return -EFAULT;
757 return result;
758}
759
Takashi Iwaid3bd67c2008-06-12 18:17:26 +0200760static int snd_ctl_elem_read(struct snd_card *card,
761 struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100763 struct snd_kcontrol *kctl;
764 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 unsigned int index_offset;
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100766 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 down_read(&card->controls_rwsem);
769 kctl = snd_ctl_find_id(card, &control->id);
770 if (kctl == NULL) {
771 result = -ENOENT;
772 } else {
773 index_offset = snd_ctl_get_ioff(kctl, &control->id);
774 vd = &kctl->vd[index_offset];
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100775 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) &&
776 kctl->get != NULL) {
777 snd_ctl_build_ioff(&control->id, kctl, index_offset);
778 result = kctl->get(kctl, control);
779 } else
780 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782 up_read(&card->controls_rwsem);
783 return result;
784}
785
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100786static int snd_ctl_elem_read_user(struct snd_card *card,
787 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100789 struct snd_ctl_elem_value *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 int result;
Li Zefanef44a1e2009-04-10 09:43:08 +0800791
792 control = memdup_user(_control, sizeof(*control));
793 if (IS_ERR(control))
794 return PTR_ERR(control);
795
Giuliano Pochini64649402006-03-13 14:11:11 +0100796 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200797 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100798 if (result >= 0)
799 result = snd_ctl_elem_read(card, control);
800 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (result >= 0)
802 if (copy_to_user(_control, control, sizeof(*control)))
803 result = -EFAULT;
804 kfree(control);
805 return result;
806}
807
Takashi Iwaid3bd67c2008-06-12 18:17:26 +0200808static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
809 struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100811 struct snd_kcontrol *kctl;
812 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 unsigned int index_offset;
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100814 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 down_read(&card->controls_rwsem);
817 kctl = snd_ctl_find_id(card, &control->id);
818 if (kctl == NULL) {
819 result = -ENOENT;
820 } else {
821 index_offset = snd_ctl_get_ioff(kctl, &control->id);
822 vd = &kctl->vd[index_offset];
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100823 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
824 kctl->put == NULL ||
825 (file && vd->owner && vd->owner != file)) {
826 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 } else {
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100828 snd_ctl_build_ioff(&control->id, kctl, index_offset);
829 result = kctl->put(kctl, control);
830 }
831 if (result > 0) {
832 up_read(&card->controls_rwsem);
833 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
834 &control->id);
835 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837 }
838 up_read(&card->controls_rwsem);
839 return result;
840}
841
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100842static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
843 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100845 struct snd_ctl_elem_value *control;
Giuliano Pochini64649402006-03-13 14:11:11 +0100846 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 int result;
848
Li Zefanef44a1e2009-04-10 09:43:08 +0800849 control = memdup_user(_control, sizeof(*control));
850 if (IS_ERR(control))
851 return PTR_ERR(control);
852
Giuliano Pochini64649402006-03-13 14:11:11 +0100853 card = file->card;
854 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200855 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100856 if (result >= 0)
857 result = snd_ctl_elem_write(card, file, control);
858 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (result >= 0)
860 if (copy_to_user(_control, control, sizeof(*control)))
861 result = -EFAULT;
862 kfree(control);
863 return result;
864}
865
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100866static int snd_ctl_elem_lock(struct snd_ctl_file *file,
867 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100869 struct snd_card *card = file->card;
870 struct snd_ctl_elem_id id;
871 struct snd_kcontrol *kctl;
872 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 int result;
874
875 if (copy_from_user(&id, _id, sizeof(id)))
876 return -EFAULT;
877 down_write(&card->controls_rwsem);
878 kctl = snd_ctl_find_id(card, &id);
879 if (kctl == NULL) {
880 result = -ENOENT;
881 } else {
882 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
883 if (vd->owner != NULL)
884 result = -EBUSY;
885 else {
886 vd->owner = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 result = 0;
888 }
889 }
890 up_write(&card->controls_rwsem);
891 return result;
892}
893
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100894static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
895 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100897 struct snd_card *card = file->card;
898 struct snd_ctl_elem_id id;
899 struct snd_kcontrol *kctl;
900 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 int result;
902
903 if (copy_from_user(&id, _id, sizeof(id)))
904 return -EFAULT;
905 down_write(&card->controls_rwsem);
906 kctl = snd_ctl_find_id(card, &id);
907 if (kctl == NULL) {
908 result = -ENOENT;
909 } else {
910 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
911 if (vd->owner == NULL)
912 result = -EINVAL;
913 else if (vd->owner != file)
914 result = -EPERM;
915 else {
916 vd->owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 result = 0;
918 }
919 }
920 up_write(&card->controls_rwsem);
921 return result;
922}
923
924struct user_element {
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100925 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 void *elem_data; /* element data */
927 unsigned long elem_data_size; /* size of element data in bytes */
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200928 void *tlv_data; /* TLV data */
929 unsigned long tlv_data_size; /* TLV data size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 void *priv_data; /* private data (like strings for enumerated type) */
931 unsigned long priv_data_size; /* size of private data in bytes */
932};
933
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100934static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
935 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 struct user_element *ue = kcontrol->private_data;
938
939 *uinfo = ue->info;
940 return 0;
941}
942
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100943static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
944 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
946 struct user_element *ue = kcontrol->private_data;
947
948 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
949 return 0;
950}
951
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100952static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
953 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 int change;
956 struct user_element *ue = kcontrol->private_data;
957
958 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
959 if (change)
960 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
961 return change;
962}
963
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200964static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol,
965 int op_flag,
966 unsigned int size,
967 unsigned int __user *tlv)
968{
969 struct user_element *ue = kcontrol->private_data;
970 int change = 0;
971 void *new_data;
972
973 if (op_flag > 0) {
974 if (size > 1024 * 128) /* sane value */
975 return -EINVAL;
Li Zefanef44a1e2009-04-10 09:43:08 +0800976
977 new_data = memdup_user(tlv, size);
978 if (IS_ERR(new_data))
979 return PTR_ERR(new_data);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200980 change = ue->tlv_data_size != size;
981 if (!change)
982 change = memcmp(ue->tlv_data, new_data, size);
983 kfree(ue->tlv_data);
984 ue->tlv_data = new_data;
985 ue->tlv_data_size = size;
986 } else {
Takashi Iwai18c1c3f2006-08-25 11:39:34 +0200987 if (! ue->tlv_data_size || ! ue->tlv_data)
988 return -ENXIO;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200989 if (size < ue->tlv_data_size)
990 return -ENOSPC;
991 if (copy_to_user(tlv, ue->tlv_data, ue->tlv_data_size))
992 return -EFAULT;
993 }
994 return change;
995}
996
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100997static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200999 struct user_element *ue = kcontrol->private_data;
1000 if (ue->tlv_data)
1001 kfree(ue->tlv_data);
1002 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
1004
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001005static int snd_ctl_elem_add(struct snd_ctl_file *file,
1006 struct snd_ctl_elem_info *info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001008 struct snd_card *card = file->card;
1009 struct snd_kcontrol kctl, *_kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 unsigned int access;
1011 long private_size;
1012 struct user_element *ue;
1013 int idx, err;
1014
1015 if (card->user_ctl_count >= MAX_USER_CONTROLS)
1016 return -ENOMEM;
Clemens Ladisch2a031ae2009-08-17 12:25:52 +02001017 if (info->count < 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return -EINVAL;
1019 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001020 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001021 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
1022 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 info->id.numid = 0;
1024 memset(&kctl, 0, sizeof(kctl));
1025 down_write(&card->controls_rwsem);
1026 _kctl = snd_ctl_find_id(card, &info->id);
1027 err = 0;
1028 if (_kctl) {
1029 if (replace)
1030 err = snd_ctl_remove(card, _kctl);
1031 else
1032 err = -EBUSY;
1033 } else {
1034 if (replace)
1035 err = -ENOENT;
1036 }
1037 up_write(&card->controls_rwsem);
1038 if (err < 0)
1039 return err;
1040 memcpy(&kctl.id, &info->id, sizeof(info->id));
1041 kctl.count = info->owner ? info->owner : 1;
1042 access |= SNDRV_CTL_ELEM_ACCESS_USER;
1043 kctl.info = snd_ctl_elem_user_info;
1044 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1045 kctl.get = snd_ctl_elem_user_get;
1046 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1047 kctl.put = snd_ctl_elem_user_put;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001048 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
1049 kctl.tlv.c = snd_ctl_elem_user_tlv;
1050 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 switch (info->type) {
1053 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 case SNDRV_CTL_ELEM_TYPE_INTEGER:
1055 private_size = sizeof(long);
1056 if (info->count > 128)
1057 return -EINVAL;
1058 break;
1059 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
1060 private_size = sizeof(long long);
1061 if (info->count > 64)
1062 return -EINVAL;
1063 break;
1064 case SNDRV_CTL_ELEM_TYPE_BYTES:
1065 private_size = sizeof(unsigned char);
1066 if (info->count > 512)
1067 return -EINVAL;
1068 break;
1069 case SNDRV_CTL_ELEM_TYPE_IEC958:
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001070 private_size = sizeof(struct snd_aes_iec958);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (info->count != 1)
1072 return -EINVAL;
1073 break;
1074 default:
1075 return -EINVAL;
1076 }
1077 private_size *= info->count;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001078 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (ue == NULL)
1080 return -ENOMEM;
1081 ue->info = *info;
Takashi Iwai86148e82006-08-24 12:36:36 +02001082 ue->info.access = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 ue->elem_data = (char *)ue + sizeof(*ue);
1084 ue->elem_data_size = private_size;
1085 kctl.private_free = snd_ctl_elem_user_free;
1086 _kctl = snd_ctl_new(&kctl, access);
1087 if (_kctl == NULL) {
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001088 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return -ENOMEM;
1090 }
1091 _kctl->private_data = ue;
1092 for (idx = 0; idx < _kctl->count; idx++)
1093 _kctl->vd[idx].owner = file;
1094 err = snd_ctl_add(card, _kctl);
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001095 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 down_write(&card->controls_rwsem);
1099 card->user_ctl_count++;
1100 up_write(&card->controls_rwsem);
1101
1102 return 0;
1103}
1104
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001105static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1106 struct snd_ctl_elem_info __user *_info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001108 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (copy_from_user(&info, _info, sizeof(info)))
1110 return -EFAULT;
1111 return snd_ctl_elem_add(file, &info, replace);
1112}
1113
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001114static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1115 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001117 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 if (copy_from_user(&id, _id, sizeof(id)))
1120 return -EFAULT;
Clemens Ladischf217ac52009-08-17 12:27:22 +02001121 return snd_ctl_remove_user_ctl(file, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001124static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
1126 int subscribe;
1127 if (get_user(subscribe, ptr))
1128 return -EFAULT;
1129 if (subscribe < 0) {
1130 subscribe = file->subscribed;
1131 if (put_user(subscribe, ptr))
1132 return -EFAULT;
1133 return 0;
1134 }
1135 if (subscribe) {
1136 file->subscribed = 1;
1137 return 0;
1138 } else if (file->subscribed) {
1139 snd_ctl_empty_read_queue(file);
1140 file->subscribed = 0;
1141 }
1142 return 0;
1143}
1144
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001145static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1146 struct snd_ctl_tlv __user *_tlv,
1147 int op_flag)
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001148{
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001149 struct snd_card *card = file->card;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001150 struct snd_ctl_tlv tlv;
1151 struct snd_kcontrol *kctl;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001152 struct snd_kcontrol_volatile *vd;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001153 unsigned int len;
1154 int err = 0;
1155
1156 if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
1157 return -EFAULT;
Clemens Ladisch61236372010-02-01 13:30:56 +01001158 if (tlv.length < sizeof(unsigned int) * 2)
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001159 return -EINVAL;
1160 down_read(&card->controls_rwsem);
1161 kctl = snd_ctl_find_numid(card, tlv.numid);
1162 if (kctl == NULL) {
1163 err = -ENOENT;
1164 goto __kctl_end;
1165 }
1166 if (kctl->tlv.p == NULL) {
1167 err = -ENXIO;
1168 goto __kctl_end;
1169 }
1170 vd = &kctl->vd[tlv.numid - kctl->id.numid];
1171 if ((op_flag == 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) == 0) ||
1172 (op_flag > 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) == 0) ||
1173 (op_flag < 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND) == 0)) {
1174 err = -ENXIO;
1175 goto __kctl_end;
1176 }
1177 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
Dan Carpenterbec145a2009-11-18 10:31:57 +02001178 if (vd->owner != NULL && vd->owner != file) {
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001179 err = -EPERM;
1180 goto __kctl_end;
1181 }
1182 err = kctl->tlv.c(kctl, op_flag, tlv.length, _tlv->tlv);
1183 if (err > 0) {
1184 up_read(&card->controls_rwsem);
1185 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &kctl->id);
1186 return 0;
1187 }
1188 } else {
1189 if (op_flag) {
1190 err = -ENXIO;
1191 goto __kctl_end;
1192 }
1193 len = kctl->tlv.p[1] + 2 * sizeof(unsigned int);
1194 if (tlv.length < len) {
1195 err = -ENOMEM;
1196 goto __kctl_end;
1197 }
1198 if (copy_to_user(_tlv->tlv, kctl->tlv.p, len))
1199 err = -EFAULT;
1200 }
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001201 __kctl_end:
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001202 up_read(&card->controls_rwsem);
1203 return err;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001204}
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1207{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001208 struct snd_ctl_file *ctl;
1209 struct snd_card *card;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001210 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 void __user *argp = (void __user *)arg;
1212 int __user *ip = argp;
1213 int err;
1214
1215 ctl = file->private_data;
1216 card = ctl->card;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001217 if (snd_BUG_ON(!card))
1218 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 switch (cmd) {
1220 case SNDRV_CTL_IOCTL_PVERSION:
1221 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1222 case SNDRV_CTL_IOCTL_CARD_INFO:
1223 return snd_ctl_card_info(card, ctl, cmd, argp);
1224 case SNDRV_CTL_IOCTL_ELEM_LIST:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001225 return snd_ctl_elem_list(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 case SNDRV_CTL_IOCTL_ELEM_INFO:
1227 return snd_ctl_elem_info_user(ctl, argp);
1228 case SNDRV_CTL_IOCTL_ELEM_READ:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001229 return snd_ctl_elem_read_user(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1231 return snd_ctl_elem_write_user(ctl, argp);
1232 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1233 return snd_ctl_elem_lock(ctl, argp);
1234 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1235 return snd_ctl_elem_unlock(ctl, argp);
1236 case SNDRV_CTL_IOCTL_ELEM_ADD:
1237 return snd_ctl_elem_add_user(ctl, argp, 0);
1238 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1239 return snd_ctl_elem_add_user(ctl, argp, 1);
1240 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1241 return snd_ctl_elem_remove(ctl, argp);
1242 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1243 return snd_ctl_subscribe_events(ctl, ip);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001244 case SNDRV_CTL_IOCTL_TLV_READ:
1245 return snd_ctl_tlv_ioctl(ctl, argp, 0);
1246 case SNDRV_CTL_IOCTL_TLV_WRITE:
1247 return snd_ctl_tlv_ioctl(ctl, argp, 1);
1248 case SNDRV_CTL_IOCTL_TLV_COMMAND:
1249 return snd_ctl_tlv_ioctl(ctl, argp, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 case SNDRV_CTL_IOCTL_POWER:
Takashi Iwaia381a7a2005-11-17 15:55:49 +01001251 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 case SNDRV_CTL_IOCTL_POWER_STATE:
1253#ifdef CONFIG_PM
1254 return put_user(card->power_state, ip) ? -EFAULT : 0;
1255#else
1256 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1257#endif
1258 }
1259 down_read(&snd_ioctl_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001260 list_for_each_entry(p, &snd_control_ioctls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 err = p->fioctl(card, ctl, cmd, arg);
1262 if (err != -ENOIOCTLCMD) {
1263 up_read(&snd_ioctl_rwsem);
1264 return err;
1265 }
1266 }
1267 up_read(&snd_ioctl_rwsem);
Takashi Iwai6d85be62005-05-15 14:32:50 +02001268 snd_printdd("unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return -ENOTTY;
1270}
1271
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001272static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1273 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001275 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 int err = 0;
1277 ssize_t result = 0;
1278
1279 ctl = file->private_data;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001280 if (snd_BUG_ON(!ctl || !ctl->card))
1281 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (!ctl->subscribed)
1283 return -EBADFD;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001284 if (count < sizeof(struct snd_ctl_event))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 return -EINVAL;
1286 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001287 while (count >= sizeof(struct snd_ctl_event)) {
1288 struct snd_ctl_event ev;
1289 struct snd_kctl_event *kev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 while (list_empty(&ctl->events)) {
1291 wait_queue_t wait;
1292 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1293 err = -EAGAIN;
1294 goto __end_lock;
1295 }
1296 init_waitqueue_entry(&wait, current);
1297 add_wait_queue(&ctl->change_sleep, &wait);
1298 set_current_state(TASK_INTERRUPTIBLE);
1299 spin_unlock_irq(&ctl->read_lock);
1300 schedule();
1301 remove_wait_queue(&ctl->change_sleep, &wait);
1302 if (signal_pending(current))
Adrian Bunk0e5d7202006-11-07 13:42:54 +01001303 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 spin_lock_irq(&ctl->read_lock);
1305 }
1306 kev = snd_kctl_event(ctl->events.next);
1307 ev.type = SNDRV_CTL_EVENT_ELEM;
1308 ev.data.elem.mask = kev->mask;
1309 ev.data.elem.id = kev->id;
1310 list_del(&kev->list);
1311 spin_unlock_irq(&ctl->read_lock);
1312 kfree(kev);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001313 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 err = -EFAULT;
1315 goto __end;
1316 }
1317 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001318 buffer += sizeof(struct snd_ctl_event);
1319 count -= sizeof(struct snd_ctl_event);
1320 result += sizeof(struct snd_ctl_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
1322 __end_lock:
1323 spin_unlock_irq(&ctl->read_lock);
1324 __end:
1325 return result > 0 ? result : err;
1326}
1327
1328static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1329{
1330 unsigned int mask;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001331 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 ctl = file->private_data;
1334 if (!ctl->subscribed)
1335 return 0;
1336 poll_wait(file, &ctl->change_sleep, wait);
1337
1338 mask = 0;
1339 if (!list_empty(&ctl->events))
1340 mask |= POLLIN | POLLRDNORM;
1341
1342 return mask;
1343}
1344
1345/*
1346 * register the device-specific control-ioctls.
1347 * called from each device manager like pcm.c, hwdep.c, etc.
1348 */
1349static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1350{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001351 struct snd_kctl_ioctl *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001353 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (pn == NULL)
1355 return -ENOMEM;
1356 pn->fioctl = fcn;
1357 down_write(&snd_ioctl_rwsem);
1358 list_add_tail(&pn->list, lists);
1359 up_write(&snd_ioctl_rwsem);
1360 return 0;
1361}
1362
1363int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1364{
1365 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1366}
1367
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001368EXPORT_SYMBOL(snd_ctl_register_ioctl);
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370#ifdef CONFIG_COMPAT
1371int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1372{
1373 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1374}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001375
1376EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377#endif
1378
1379/*
1380 * de-register the device-specific control-ioctls.
1381 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001382static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1383 struct list_head *lists)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001385 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001387 if (snd_BUG_ON(!fcn))
1388 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 down_write(&snd_ioctl_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001390 list_for_each_entry(p, lists, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (p->fioctl == fcn) {
1392 list_del(&p->list);
1393 up_write(&snd_ioctl_rwsem);
1394 kfree(p);
1395 return 0;
1396 }
1397 }
1398 up_write(&snd_ioctl_rwsem);
1399 snd_BUG();
1400 return -EINVAL;
1401}
1402
1403int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1404{
1405 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1406}
1407
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001408EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410#ifdef CONFIG_COMPAT
1411int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1412{
1413 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1414}
1415
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001416EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417#endif
1418
1419static int snd_ctl_fasync(int fd, struct file * file, int on)
1420{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001421 struct snd_ctl_file *ctl;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 ctl = file->private_data;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001424 return fasync_helper(fd, file, on, &ctl->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
1426
1427/*
1428 * ioctl32 compat
1429 */
1430#ifdef CONFIG_COMPAT
1431#include "control_compat.c"
1432#else
1433#define snd_ctl_ioctl_compat NULL
1434#endif
1435
1436/*
1437 * INIT PART
1438 */
1439
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001440static const struct file_operations snd_ctl_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 .owner = THIS_MODULE,
1443 .read = snd_ctl_read,
1444 .open = snd_ctl_open,
1445 .release = snd_ctl_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02001446 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 .poll = snd_ctl_poll,
1448 .unlocked_ioctl = snd_ctl_ioctl,
1449 .compat_ioctl = snd_ctl_ioctl_compat,
1450 .fasync = snd_ctl_fasync,
1451};
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453/*
1454 * registration of the control device
1455 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001456static int snd_ctl_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001458 struct snd_card *card = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 int err, cardnum;
1460 char name[16];
1461
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001462 if (snd_BUG_ON(!card))
1463 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 cardnum = card->number;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001465 if (snd_BUG_ON(cardnum < 0 || cardnum >= SNDRV_CARDS))
1466 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 sprintf(name, "controlC%i", cardnum);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001468 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1469 &snd_ctl_f_ops, card, name)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return err;
1471 return 0;
1472}
1473
1474/*
1475 * disconnection of the control device
1476 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001477static int snd_ctl_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001479 struct snd_card *card = device->device_data;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001480 struct snd_ctl_file *ctl;
Takashi Iwaic4614822006-06-23 14:38:23 +02001481 int err, cardnum;
1482
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001483 if (snd_BUG_ON(!card))
1484 return -ENXIO;
Takashi Iwaic4614822006-06-23 14:38:23 +02001485 cardnum = card->number;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001486 if (snd_BUG_ON(cardnum < 0 || cardnum >= SNDRV_CARDS))
1487 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Takashi Iwaid8009882008-09-07 12:51:13 +02001489 read_lock(&card->ctl_files_rwlock);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001490 list_for_each_entry(ctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 wake_up(&ctl->change_sleep);
1492 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1493 }
Takashi Iwaid8009882008-09-07 12:51:13 +02001494 read_unlock(&card->ctl_files_rwlock);
Takashi Iwaic4614822006-06-23 14:38:23 +02001495
1496 if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
1497 card, -1)) < 0)
1498 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 return 0;
1500}
1501
1502/*
1503 * free all controls
1504 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001505static int snd_ctl_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001507 struct snd_card *card = device->device_data;
1508 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 down_write(&card->controls_rwsem);
1511 while (!list_empty(&card->controls)) {
1512 control = snd_kcontrol(card->controls.next);
1513 snd_ctl_remove(card, control);
1514 }
1515 up_write(&card->controls_rwsem);
1516 return 0;
1517}
1518
1519/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 * create control core:
1521 * called from init.c
1522 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001523int snd_ctl_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001525 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 .dev_free = snd_ctl_dev_free,
1527 .dev_register = snd_ctl_dev_register,
1528 .dev_disconnect = snd_ctl_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 };
1530
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001531 if (snd_BUG_ON(!card))
1532 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
1534}
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001535
1536/*
Clemens Ladisch96007322011-01-10 16:25:44 +01001537 * Frequently used control callbacks/helpers
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001538 */
1539int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
1540 struct snd_ctl_elem_info *uinfo)
1541{
1542 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1543 uinfo->count = 1;
1544 uinfo->value.integer.min = 0;
1545 uinfo->value.integer.max = 1;
1546 return 0;
1547}
1548
1549EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
1550
1551int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
1552 struct snd_ctl_elem_info *uinfo)
1553{
1554 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1555 uinfo->count = 2;
1556 uinfo->value.integer.min = 0;
1557 uinfo->value.integer.max = 1;
1558 return 0;
1559}
1560
1561EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
Clemens Ladisch96007322011-01-10 16:25:44 +01001562
1563/**
1564 * snd_ctl_enum_info - fills the info structure for an enumerated control
1565 * @info: the structure to be filled
1566 * @channels: the number of the control's channels; often one
1567 * @items: the number of control values; also the size of @names
1568 * @names: an array containing the names of all control values
1569 *
1570 * Sets all required fields in @info to their appropriate values.
1571 * If the control's accessibility is not the default (readable and writable),
1572 * the caller has to fill @info->access.
1573 */
1574int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
1575 unsigned int items, const char *const names[])
1576{
1577 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1578 info->count = channels;
1579 info->value.enumerated.items = items;
1580 if (info->value.enumerated.item >= items)
1581 info->value.enumerated.item = items - 1;
1582 strlcpy(info->value.enumerated.name,
1583 names[info->value.enumerated.item],
1584 sizeof(info->value.enumerated.name));
1585 return 0;
1586}
1587EXPORT_SYMBOL(snd_ctl_enum_info);