blob: a08ad57c49b6adf48a6fcc516dd02d62c159f406 [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
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100282static bool snd_ctl_remove_numid_conflict(struct snd_card *card,
283 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
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) {
Clemens Ladisch7c733582011-03-07 13:22:50 +0100288 if (kctl->id.numid < card->last_numid + 1 + count &&
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100289 kctl->id.numid + kctl->count > card->last_numid + 1) {
290 card->last_numid = kctl->id.numid + kctl->count - 1;
291 return true;
292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100294 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
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{
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100299 unsigned int iter = 100000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100301 while (snd_ctl_remove_numid_conflict(card, count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (--iter == 0) {
303 /* this situation is very unlikely */
304 snd_printk(KERN_ERR "unable to allocate new control numid\n");
305 return -ENOMEM;
306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308 return 0;
309}
310
311/**
312 * snd_ctl_add - add the control instance to the card
313 * @card: the card instance
314 * @kcontrol: the control instance to add
315 *
316 * Adds the control instance created via snd_ctl_new() or
317 * snd_ctl_new1() to the given card. Assigns also an unique
318 * numid used for fast search.
319 *
320 * Returns zero if successful, or a negative error code on failure.
321 *
322 * It frees automatically the control which cannot be added.
323 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100324int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100326 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 unsigned int idx;
Takashi Iwaic6077b32006-03-21 16:07:13 +0100328 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100330 if (! kcontrol)
Takashi Iwaic6077b32006-03-21 16:07:13 +0100331 return err;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200332 if (snd_BUG_ON(!card || !kcontrol->info))
333 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 id = kcontrol->id;
335 down_write(&card->controls_rwsem);
336 if (snd_ctl_find_id(card, &id)) {
337 up_write(&card->controls_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
339 id.iface,
340 id.device,
341 id.subdevice,
342 id.name,
343 id.index);
Takashi Iwaic6077b32006-03-21 16:07:13 +0100344 err = -EBUSY;
345 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
348 up_write(&card->controls_rwsem);
Takashi Iwaic6077b32006-03-21 16:07:13 +0100349 err = -ENOMEM;
350 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352 list_add_tail(&kcontrol->list, &card->controls);
353 card->controls_count += kcontrol->count;
354 kcontrol->id.numid = card->last_numid + 1;
355 card->last_numid += kcontrol->count;
356 up_write(&card->controls_rwsem);
357 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
358 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
359 return 0;
Takashi Iwaic6077b32006-03-21 16:07:13 +0100360
361 error:
362 snd_ctl_free_one(kcontrol);
363 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200366EXPORT_SYMBOL(snd_ctl_add);
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368/**
369 * snd_ctl_remove - remove the control from the card and release it
370 * @card: the card instance
371 * @kcontrol: the control instance to remove
372 *
373 * Removes the control from the card and then releases the instance.
374 * You don't need to call snd_ctl_free_one(). You must be in
375 * the write lock - down_write(&card->controls_rwsem).
376 *
377 * Returns 0 if successful, or a negative error code on failure.
378 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100379int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100381 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 unsigned int idx;
383
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200384 if (snd_BUG_ON(!card || !kcontrol))
385 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 list_del(&kcontrol->list);
387 card->controls_count -= kcontrol->count;
388 id = kcontrol->id;
389 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
390 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
391 snd_ctl_free_one(kcontrol);
392 return 0;
393}
394
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200395EXPORT_SYMBOL(snd_ctl_remove);
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397/**
398 * snd_ctl_remove_id - remove the control of the given id and release it
399 * @card: the card instance
400 * @id: the control id to remove
401 *
402 * Finds the control instance with the given id, removes it from the
403 * card list and releases it.
404 *
405 * Returns 0 if successful, or a negative error code on failure.
406 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100407int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100409 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 int ret;
411
412 down_write(&card->controls_rwsem);
413 kctl = snd_ctl_find_id(card, id);
414 if (kctl == NULL) {
415 up_write(&card->controls_rwsem);
416 return -ENOENT;
417 }
418 ret = snd_ctl_remove(card, kctl);
419 up_write(&card->controls_rwsem);
420 return ret;
421}
422
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200423EXPORT_SYMBOL(snd_ctl_remove_id);
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425/**
Clemens Ladischf217ac52009-08-17 12:27:22 +0200426 * snd_ctl_remove_user_ctl - remove and release the unlocked user control
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 * @file: active control handle
428 * @id: the control id to remove
429 *
430 * Finds the control instance with the given id, removes it from the
431 * card list and releases it.
432 *
433 * Returns 0 if successful, or a negative error code on failure.
434 */
Clemens Ladischf217ac52009-08-17 12:27:22 +0200435static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
436 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100438 struct snd_card *card = file->card;
439 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 int idx, ret;
441
442 down_write(&card->controls_rwsem);
443 kctl = snd_ctl_find_id(card, id);
444 if (kctl == NULL) {
Clemens Ladisch317b8082009-08-17 12:26:34 +0200445 ret = -ENOENT;
446 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Clemens Ladisch18dd0aa2009-08-17 12:28:09 +0200448 if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
449 ret = -EINVAL;
450 goto error;
451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 for (idx = 0; idx < kctl->count; idx++)
453 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
Clemens Ladisch317b8082009-08-17 12:26:34 +0200454 ret = -EBUSY;
455 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457 ret = snd_ctl_remove(card, kctl);
Clemens Ladischf217ac52009-08-17 12:27:22 +0200458 if (ret < 0)
459 goto error;
460 card->user_ctl_count--;
Clemens Ladisch317b8082009-08-17 12:26:34 +0200461error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 up_write(&card->controls_rwsem);
463 return ret;
464}
465
466/**
Takashi Iwai3cbdd752008-08-29 16:09:01 +0200467 * snd_ctl_activate_id - activate/inactivate the control of the given id
468 * @card: the card instance
469 * @id: the control id to activate/inactivate
470 * @active: non-zero to activate
471 *
472 * Finds the control instance with the given id, and activate or
473 * inactivate the control together with notification, if changed.
474 *
475 * Returns 0 if unchanged, 1 if changed, or a negative error code on failure.
476 */
477int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
478 int active)
479{
480 struct snd_kcontrol *kctl;
481 struct snd_kcontrol_volatile *vd;
482 unsigned int index_offset;
483 int ret;
484
485 down_write(&card->controls_rwsem);
486 kctl = snd_ctl_find_id(card, id);
487 if (kctl == NULL) {
488 ret = -ENOENT;
489 goto unlock;
490 }
491 index_offset = snd_ctl_get_ioff(kctl, &kctl->id);
492 vd = &kctl->vd[index_offset];
493 ret = 0;
494 if (active) {
495 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
496 goto unlock;
497 vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
498 } else {
499 if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)
500 goto unlock;
501 vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
502 }
503 ret = 1;
504 unlock:
505 up_write(&card->controls_rwsem);
506 if (ret > 0)
507 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, id);
508 return ret;
509}
510EXPORT_SYMBOL_GPL(snd_ctl_activate_id);
511
512/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 * snd_ctl_rename_id - replace the id of a control on the card
514 * @card: the card instance
515 * @src_id: the old id
516 * @dst_id: the new id
517 *
518 * Finds the control with the old id from the card, and replaces the
519 * id with the new one.
520 *
521 * Returns zero if successful, or a negative error code on failure.
522 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100523int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
524 struct snd_ctl_elem_id *dst_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100526 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 down_write(&card->controls_rwsem);
529 kctl = snd_ctl_find_id(card, src_id);
530 if (kctl == NULL) {
531 up_write(&card->controls_rwsem);
532 return -ENOENT;
533 }
534 kctl->id = *dst_id;
535 kctl->id.numid = card->last_numid + 1;
536 card->last_numid += kctl->count;
537 up_write(&card->controls_rwsem);
538 return 0;
539}
540
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200541EXPORT_SYMBOL(snd_ctl_rename_id);
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543/**
544 * snd_ctl_find_numid - find the control instance with the given number-id
545 * @card: the card instance
546 * @numid: the number-id to search
547 *
548 * Finds the control instance with the given number-id from the card.
549 *
550 * Returns the pointer of the instance if found, or NULL if not.
551 *
552 * The caller must down card->controls_rwsem before calling this function
553 * (if the race condition can happen).
554 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100555struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100557 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200559 if (snd_BUG_ON(!card || !numid))
560 return NULL;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200561 list_for_each_entry(kctl, &card->controls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
563 return kctl;
564 }
565 return NULL;
566}
567
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200568EXPORT_SYMBOL(snd_ctl_find_numid);
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570/**
571 * snd_ctl_find_id - find the control instance with the given id
572 * @card: the card instance
573 * @id: the id to search
574 *
575 * Finds the control instance with the given id from the card.
576 *
577 * Returns the pointer of the instance if found, or NULL if not.
578 *
579 * The caller must down card->controls_rwsem before calling this function
580 * (if the race condition can happen).
581 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100582struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
583 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100585 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200587 if (snd_BUG_ON(!card || !id))
588 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (id->numid != 0)
590 return snd_ctl_find_numid(card, id->numid);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200591 list_for_each_entry(kctl, &card->controls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (kctl->id.iface != id->iface)
593 continue;
594 if (kctl->id.device != id->device)
595 continue;
596 if (kctl->id.subdevice != id->subdevice)
597 continue;
598 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
599 continue;
600 if (kctl->id.index > id->index)
601 continue;
602 if (kctl->id.index + kctl->count <= id->index)
603 continue;
604 return kctl;
605 }
606 return NULL;
607}
608
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200609EXPORT_SYMBOL(snd_ctl_find_id);
610
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100611static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 unsigned int cmd, void __user *arg)
613{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100614 struct snd_ctl_card_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Takashi Iwaica2c0962005-09-09 14:20:23 +0200616 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (! info)
618 return -ENOMEM;
619 down_read(&snd_ioctl_rwsem);
620 info->card = card->number;
621 strlcpy(info->id, card->id, sizeof(info->id));
622 strlcpy(info->driver, card->driver, sizeof(info->driver));
623 strlcpy(info->name, card->shortname, sizeof(info->name));
624 strlcpy(info->longname, card->longname, sizeof(info->longname));
625 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
626 strlcpy(info->components, card->components, sizeof(info->components));
627 up_read(&snd_ioctl_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100628 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 kfree(info);
630 return -EFAULT;
631 }
632 kfree(info);
633 return 0;
634}
635
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100636static int snd_ctl_elem_list(struct snd_card *card,
637 struct snd_ctl_elem_list __user *_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 struct list_head *plist;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100640 struct snd_ctl_elem_list list;
641 struct snd_kcontrol *kctl;
642 struct snd_ctl_elem_id *dst, *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 unsigned int offset, space, first, jidx;
644
645 if (copy_from_user(&list, _list, sizeof(list)))
646 return -EFAULT;
647 offset = list.offset;
648 space = list.space;
649 first = 0;
650 /* try limit maximum space */
651 if (space > 16384)
652 return -ENOMEM;
653 if (space > 0) {
654 /* allocate temporary buffer for atomic operation */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100655 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (dst == NULL)
657 return -ENOMEM;
658 down_read(&card->controls_rwsem);
659 list.count = card->controls_count;
660 plist = card->controls.next;
661 while (plist != &card->controls) {
662 if (offset == 0)
663 break;
664 kctl = snd_kcontrol(plist);
665 if (offset < kctl->count)
666 break;
667 offset -= kctl->count;
668 plist = plist->next;
669 }
670 list.used = 0;
671 id = dst;
672 while (space > 0 && plist != &card->controls) {
673 kctl = snd_kcontrol(plist);
674 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
675 snd_ctl_build_ioff(id, kctl, jidx);
676 id++;
677 space--;
678 list.used++;
679 }
680 plist = plist->next;
681 offset = 0;
682 }
683 up_read(&card->controls_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100684 if (list.used > 0 &&
685 copy_to_user(list.pids, dst,
686 list.used * sizeof(struct snd_ctl_elem_id))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 vfree(dst);
688 return -EFAULT;
689 }
690 vfree(dst);
691 } else {
692 down_read(&card->controls_rwsem);
693 list.count = card->controls_count;
694 up_read(&card->controls_rwsem);
695 }
696 if (copy_to_user(_list, &list, sizeof(list)))
697 return -EFAULT;
698 return 0;
699}
700
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100701static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
702 struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100704 struct snd_card *card = ctl->card;
705 struct snd_kcontrol *kctl;
706 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 unsigned int index_offset;
708 int result;
709
710 down_read(&card->controls_rwsem);
711 kctl = snd_ctl_find_id(card, &info->id);
712 if (kctl == NULL) {
713 up_read(&card->controls_rwsem);
714 return -ENOENT;
715 }
716#ifdef CONFIG_SND_DEBUG
717 info->access = 0;
718#endif
719 result = kctl->info(kctl, info);
720 if (result >= 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200721 snd_BUG_ON(info->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 index_offset = snd_ctl_get_ioff(kctl, &info->id);
723 vd = &kctl->vd[index_offset];
724 snd_ctl_build_ioff(&info->id, kctl, index_offset);
725 info->access = vd->access;
726 if (vd->owner) {
727 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
728 if (vd->owner == ctl)
729 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
Clemens Ladisch25d27ed2009-11-02 09:35:44 +0100730 info->owner = pid_vnr(vd->owner->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 } else {
732 info->owner = -1;
733 }
734 }
735 up_read(&card->controls_rwsem);
736 return result;
737}
738
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100739static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
740 struct snd_ctl_elem_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100742 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 int result;
744
745 if (copy_from_user(&info, _info, sizeof(info)))
746 return -EFAULT;
Giuliano Pochini64649402006-03-13 14:11:11 +0100747 snd_power_lock(ctl->card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200748 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100749 if (result >= 0)
750 result = snd_ctl_elem_info(ctl, &info);
751 snd_power_unlock(ctl->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (result >= 0)
753 if (copy_to_user(_info, &info, sizeof(info)))
754 return -EFAULT;
755 return result;
756}
757
Takashi Iwaid3bd67c2008-06-12 18:17:26 +0200758static int snd_ctl_elem_read(struct snd_card *card,
759 struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100761 struct snd_kcontrol *kctl;
762 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 unsigned int index_offset;
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100764 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 down_read(&card->controls_rwsem);
767 kctl = snd_ctl_find_id(card, &control->id);
768 if (kctl == NULL) {
769 result = -ENOENT;
770 } else {
771 index_offset = snd_ctl_get_ioff(kctl, &control->id);
772 vd = &kctl->vd[index_offset];
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100773 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) &&
774 kctl->get != NULL) {
775 snd_ctl_build_ioff(&control->id, kctl, index_offset);
776 result = kctl->get(kctl, control);
777 } else
778 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780 up_read(&card->controls_rwsem);
781 return result;
782}
783
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100784static int snd_ctl_elem_read_user(struct snd_card *card,
785 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100787 struct snd_ctl_elem_value *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 int result;
Li Zefanef44a1e2009-04-10 09:43:08 +0800789
790 control = memdup_user(_control, sizeof(*control));
791 if (IS_ERR(control))
792 return PTR_ERR(control);
793
Giuliano Pochini64649402006-03-13 14:11:11 +0100794 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200795 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100796 if (result >= 0)
797 result = snd_ctl_elem_read(card, control);
798 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (result >= 0)
800 if (copy_to_user(_control, control, sizeof(*control)))
801 result = -EFAULT;
802 kfree(control);
803 return result;
804}
805
Takashi Iwaid3bd67c2008-06-12 18:17:26 +0200806static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
807 struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100809 struct snd_kcontrol *kctl;
810 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 unsigned int index_offset;
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100812 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 down_read(&card->controls_rwsem);
815 kctl = snd_ctl_find_id(card, &control->id);
816 if (kctl == NULL) {
817 result = -ENOENT;
818 } else {
819 index_offset = snd_ctl_get_ioff(kctl, &control->id);
820 vd = &kctl->vd[index_offset];
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100821 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
822 kctl->put == NULL ||
823 (file && vd->owner && vd->owner != file)) {
824 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 } else {
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100826 snd_ctl_build_ioff(&control->id, kctl, index_offset);
827 result = kctl->put(kctl, control);
828 }
829 if (result > 0) {
830 up_read(&card->controls_rwsem);
831 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
832 &control->id);
833 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835 }
836 up_read(&card->controls_rwsem);
837 return result;
838}
839
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100840static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
841 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100843 struct snd_ctl_elem_value *control;
Giuliano Pochini64649402006-03-13 14:11:11 +0100844 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 int result;
846
Li Zefanef44a1e2009-04-10 09:43:08 +0800847 control = memdup_user(_control, sizeof(*control));
848 if (IS_ERR(control))
849 return PTR_ERR(control);
850
Giuliano Pochini64649402006-03-13 14:11:11 +0100851 card = file->card;
852 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200853 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100854 if (result >= 0)
855 result = snd_ctl_elem_write(card, file, control);
856 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (result >= 0)
858 if (copy_to_user(_control, control, sizeof(*control)))
859 result = -EFAULT;
860 kfree(control);
861 return result;
862}
863
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100864static int snd_ctl_elem_lock(struct snd_ctl_file *file,
865 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100867 struct snd_card *card = file->card;
868 struct snd_ctl_elem_id id;
869 struct snd_kcontrol *kctl;
870 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 int result;
872
873 if (copy_from_user(&id, _id, sizeof(id)))
874 return -EFAULT;
875 down_write(&card->controls_rwsem);
876 kctl = snd_ctl_find_id(card, &id);
877 if (kctl == NULL) {
878 result = -ENOENT;
879 } else {
880 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
881 if (vd->owner != NULL)
882 result = -EBUSY;
883 else {
884 vd->owner = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 result = 0;
886 }
887 }
888 up_write(&card->controls_rwsem);
889 return result;
890}
891
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100892static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
893 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100895 struct snd_card *card = file->card;
896 struct snd_ctl_elem_id id;
897 struct snd_kcontrol *kctl;
898 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 int result;
900
901 if (copy_from_user(&id, _id, sizeof(id)))
902 return -EFAULT;
903 down_write(&card->controls_rwsem);
904 kctl = snd_ctl_find_id(card, &id);
905 if (kctl == NULL) {
906 result = -ENOENT;
907 } else {
908 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
909 if (vd->owner == NULL)
910 result = -EINVAL;
911 else if (vd->owner != file)
912 result = -EPERM;
913 else {
914 vd->owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 result = 0;
916 }
917 }
918 up_write(&card->controls_rwsem);
919 return result;
920}
921
922struct user_element {
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100923 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 void *elem_data; /* element data */
925 unsigned long elem_data_size; /* size of element data in bytes */
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200926 void *tlv_data; /* TLV data */
927 unsigned long tlv_data_size; /* TLV data size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 void *priv_data; /* private data (like strings for enumerated type) */
929 unsigned long priv_data_size; /* size of private data in bytes */
930};
931
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100932static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
933 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
935 struct user_element *ue = kcontrol->private_data;
936
937 *uinfo = ue->info;
938 return 0;
939}
940
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100941static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
942 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 struct user_element *ue = kcontrol->private_data;
945
946 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
947 return 0;
948}
949
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100950static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
951 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953 int change;
954 struct user_element *ue = kcontrol->private_data;
955
956 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
957 if (change)
958 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
959 return change;
960}
961
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200962static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol,
963 int op_flag,
964 unsigned int size,
965 unsigned int __user *tlv)
966{
967 struct user_element *ue = kcontrol->private_data;
968 int change = 0;
969 void *new_data;
970
971 if (op_flag > 0) {
972 if (size > 1024 * 128) /* sane value */
973 return -EINVAL;
Li Zefanef44a1e2009-04-10 09:43:08 +0800974
975 new_data = memdup_user(tlv, size);
976 if (IS_ERR(new_data))
977 return PTR_ERR(new_data);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200978 change = ue->tlv_data_size != size;
979 if (!change)
980 change = memcmp(ue->tlv_data, new_data, size);
981 kfree(ue->tlv_data);
982 ue->tlv_data = new_data;
983 ue->tlv_data_size = size;
984 } else {
Takashi Iwai18c1c3f2006-08-25 11:39:34 +0200985 if (! ue->tlv_data_size || ! ue->tlv_data)
986 return -ENXIO;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200987 if (size < ue->tlv_data_size)
988 return -ENOSPC;
989 if (copy_to_user(tlv, ue->tlv_data, ue->tlv_data_size))
990 return -EFAULT;
991 }
992 return change;
993}
994
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100995static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200997 struct user_element *ue = kcontrol->private_data;
998 if (ue->tlv_data)
999 kfree(ue->tlv_data);
1000 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001003static int snd_ctl_elem_add(struct snd_ctl_file *file,
1004 struct snd_ctl_elem_info *info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001006 struct snd_card *card = file->card;
1007 struct snd_kcontrol kctl, *_kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 unsigned int access;
1009 long private_size;
1010 struct user_element *ue;
1011 int idx, err;
1012
1013 if (card->user_ctl_count >= MAX_USER_CONTROLS)
1014 return -ENOMEM;
Clemens Ladisch2a031ae2009-08-17 12:25:52 +02001015 if (info->count < 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return -EINVAL;
1017 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001018 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001019 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
1020 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 info->id.numid = 0;
1022 memset(&kctl, 0, sizeof(kctl));
1023 down_write(&card->controls_rwsem);
1024 _kctl = snd_ctl_find_id(card, &info->id);
1025 err = 0;
1026 if (_kctl) {
1027 if (replace)
1028 err = snd_ctl_remove(card, _kctl);
1029 else
1030 err = -EBUSY;
1031 } else {
1032 if (replace)
1033 err = -ENOENT;
1034 }
1035 up_write(&card->controls_rwsem);
1036 if (err < 0)
1037 return err;
1038 memcpy(&kctl.id, &info->id, sizeof(info->id));
1039 kctl.count = info->owner ? info->owner : 1;
1040 access |= SNDRV_CTL_ELEM_ACCESS_USER;
1041 kctl.info = snd_ctl_elem_user_info;
1042 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1043 kctl.get = snd_ctl_elem_user_get;
1044 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1045 kctl.put = snd_ctl_elem_user_put;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001046 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
1047 kctl.tlv.c = snd_ctl_elem_user_tlv;
1048 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 switch (info->type) {
1051 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 case SNDRV_CTL_ELEM_TYPE_INTEGER:
1053 private_size = sizeof(long);
1054 if (info->count > 128)
1055 return -EINVAL;
1056 break;
1057 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
1058 private_size = sizeof(long long);
1059 if (info->count > 64)
1060 return -EINVAL;
1061 break;
1062 case SNDRV_CTL_ELEM_TYPE_BYTES:
1063 private_size = sizeof(unsigned char);
1064 if (info->count > 512)
1065 return -EINVAL;
1066 break;
1067 case SNDRV_CTL_ELEM_TYPE_IEC958:
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001068 private_size = sizeof(struct snd_aes_iec958);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (info->count != 1)
1070 return -EINVAL;
1071 break;
1072 default:
1073 return -EINVAL;
1074 }
1075 private_size *= info->count;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001076 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (ue == NULL)
1078 return -ENOMEM;
1079 ue->info = *info;
Takashi Iwai86148e82006-08-24 12:36:36 +02001080 ue->info.access = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 ue->elem_data = (char *)ue + sizeof(*ue);
1082 ue->elem_data_size = private_size;
1083 kctl.private_free = snd_ctl_elem_user_free;
1084 _kctl = snd_ctl_new(&kctl, access);
1085 if (_kctl == NULL) {
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001086 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return -ENOMEM;
1088 }
1089 _kctl->private_data = ue;
1090 for (idx = 0; idx < _kctl->count; idx++)
1091 _kctl->vd[idx].owner = file;
1092 err = snd_ctl_add(card, _kctl);
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001093 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 down_write(&card->controls_rwsem);
1097 card->user_ctl_count++;
1098 up_write(&card->controls_rwsem);
1099
1100 return 0;
1101}
1102
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001103static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1104 struct snd_ctl_elem_info __user *_info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001106 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (copy_from_user(&info, _info, sizeof(info)))
1108 return -EFAULT;
1109 return snd_ctl_elem_add(file, &info, replace);
1110}
1111
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001112static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1113 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001115 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 if (copy_from_user(&id, _id, sizeof(id)))
1118 return -EFAULT;
Clemens Ladischf217ac52009-08-17 12:27:22 +02001119 return snd_ctl_remove_user_ctl(file, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001122static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124 int subscribe;
1125 if (get_user(subscribe, ptr))
1126 return -EFAULT;
1127 if (subscribe < 0) {
1128 subscribe = file->subscribed;
1129 if (put_user(subscribe, ptr))
1130 return -EFAULT;
1131 return 0;
1132 }
1133 if (subscribe) {
1134 file->subscribed = 1;
1135 return 0;
1136 } else if (file->subscribed) {
1137 snd_ctl_empty_read_queue(file);
1138 file->subscribed = 0;
1139 }
1140 return 0;
1141}
1142
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001143static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1144 struct snd_ctl_tlv __user *_tlv,
1145 int op_flag)
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001146{
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001147 struct snd_card *card = file->card;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001148 struct snd_ctl_tlv tlv;
1149 struct snd_kcontrol *kctl;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001150 struct snd_kcontrol_volatile *vd;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001151 unsigned int len;
1152 int err = 0;
1153
1154 if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
1155 return -EFAULT;
Clemens Ladisch61236372010-02-01 13:30:56 +01001156 if (tlv.length < sizeof(unsigned int) * 2)
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001157 return -EINVAL;
1158 down_read(&card->controls_rwsem);
1159 kctl = snd_ctl_find_numid(card, tlv.numid);
1160 if (kctl == NULL) {
1161 err = -ENOENT;
1162 goto __kctl_end;
1163 }
1164 if (kctl->tlv.p == NULL) {
1165 err = -ENXIO;
1166 goto __kctl_end;
1167 }
1168 vd = &kctl->vd[tlv.numid - kctl->id.numid];
1169 if ((op_flag == 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) == 0) ||
1170 (op_flag > 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) == 0) ||
1171 (op_flag < 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND) == 0)) {
1172 err = -ENXIO;
1173 goto __kctl_end;
1174 }
1175 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
Dan Carpenterbec145a2009-11-18 10:31:57 +02001176 if (vd->owner != NULL && vd->owner != file) {
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001177 err = -EPERM;
1178 goto __kctl_end;
1179 }
1180 err = kctl->tlv.c(kctl, op_flag, tlv.length, _tlv->tlv);
1181 if (err > 0) {
1182 up_read(&card->controls_rwsem);
1183 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &kctl->id);
1184 return 0;
1185 }
1186 } else {
1187 if (op_flag) {
1188 err = -ENXIO;
1189 goto __kctl_end;
1190 }
1191 len = kctl->tlv.p[1] + 2 * sizeof(unsigned int);
1192 if (tlv.length < len) {
1193 err = -ENOMEM;
1194 goto __kctl_end;
1195 }
1196 if (copy_to_user(_tlv->tlv, kctl->tlv.p, len))
1197 err = -EFAULT;
1198 }
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001199 __kctl_end:
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001200 up_read(&card->controls_rwsem);
1201 return err;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001202}
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1205{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001206 struct snd_ctl_file *ctl;
1207 struct snd_card *card;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001208 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 void __user *argp = (void __user *)arg;
1210 int __user *ip = argp;
1211 int err;
1212
1213 ctl = file->private_data;
1214 card = ctl->card;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001215 if (snd_BUG_ON(!card))
1216 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 switch (cmd) {
1218 case SNDRV_CTL_IOCTL_PVERSION:
1219 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1220 case SNDRV_CTL_IOCTL_CARD_INFO:
1221 return snd_ctl_card_info(card, ctl, cmd, argp);
1222 case SNDRV_CTL_IOCTL_ELEM_LIST:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001223 return snd_ctl_elem_list(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 case SNDRV_CTL_IOCTL_ELEM_INFO:
1225 return snd_ctl_elem_info_user(ctl, argp);
1226 case SNDRV_CTL_IOCTL_ELEM_READ:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001227 return snd_ctl_elem_read_user(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1229 return snd_ctl_elem_write_user(ctl, argp);
1230 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1231 return snd_ctl_elem_lock(ctl, argp);
1232 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1233 return snd_ctl_elem_unlock(ctl, argp);
1234 case SNDRV_CTL_IOCTL_ELEM_ADD:
1235 return snd_ctl_elem_add_user(ctl, argp, 0);
1236 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1237 return snd_ctl_elem_add_user(ctl, argp, 1);
1238 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1239 return snd_ctl_elem_remove(ctl, argp);
1240 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1241 return snd_ctl_subscribe_events(ctl, ip);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001242 case SNDRV_CTL_IOCTL_TLV_READ:
1243 return snd_ctl_tlv_ioctl(ctl, argp, 0);
1244 case SNDRV_CTL_IOCTL_TLV_WRITE:
1245 return snd_ctl_tlv_ioctl(ctl, argp, 1);
1246 case SNDRV_CTL_IOCTL_TLV_COMMAND:
1247 return snd_ctl_tlv_ioctl(ctl, argp, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 case SNDRV_CTL_IOCTL_POWER:
Takashi Iwaia381a7a2005-11-17 15:55:49 +01001249 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 case SNDRV_CTL_IOCTL_POWER_STATE:
1251#ifdef CONFIG_PM
1252 return put_user(card->power_state, ip) ? -EFAULT : 0;
1253#else
1254 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1255#endif
1256 }
1257 down_read(&snd_ioctl_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001258 list_for_each_entry(p, &snd_control_ioctls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 err = p->fioctl(card, ctl, cmd, arg);
1260 if (err != -ENOIOCTLCMD) {
1261 up_read(&snd_ioctl_rwsem);
1262 return err;
1263 }
1264 }
1265 up_read(&snd_ioctl_rwsem);
Takashi Iwai6d85be62005-05-15 14:32:50 +02001266 snd_printdd("unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return -ENOTTY;
1268}
1269
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001270static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1271 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001273 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 int err = 0;
1275 ssize_t result = 0;
1276
1277 ctl = file->private_data;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001278 if (snd_BUG_ON(!ctl || !ctl->card))
1279 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (!ctl->subscribed)
1281 return -EBADFD;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001282 if (count < sizeof(struct snd_ctl_event))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return -EINVAL;
1284 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001285 while (count >= sizeof(struct snd_ctl_event)) {
1286 struct snd_ctl_event ev;
1287 struct snd_kctl_event *kev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 while (list_empty(&ctl->events)) {
1289 wait_queue_t wait;
1290 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1291 err = -EAGAIN;
1292 goto __end_lock;
1293 }
1294 init_waitqueue_entry(&wait, current);
1295 add_wait_queue(&ctl->change_sleep, &wait);
1296 set_current_state(TASK_INTERRUPTIBLE);
1297 spin_unlock_irq(&ctl->read_lock);
1298 schedule();
1299 remove_wait_queue(&ctl->change_sleep, &wait);
1300 if (signal_pending(current))
Adrian Bunk0e5d7202006-11-07 13:42:54 +01001301 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 spin_lock_irq(&ctl->read_lock);
1303 }
1304 kev = snd_kctl_event(ctl->events.next);
1305 ev.type = SNDRV_CTL_EVENT_ELEM;
1306 ev.data.elem.mask = kev->mask;
1307 ev.data.elem.id = kev->id;
1308 list_del(&kev->list);
1309 spin_unlock_irq(&ctl->read_lock);
1310 kfree(kev);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001311 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 err = -EFAULT;
1313 goto __end;
1314 }
1315 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001316 buffer += sizeof(struct snd_ctl_event);
1317 count -= sizeof(struct snd_ctl_event);
1318 result += sizeof(struct snd_ctl_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320 __end_lock:
1321 spin_unlock_irq(&ctl->read_lock);
1322 __end:
1323 return result > 0 ? result : err;
1324}
1325
1326static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1327{
1328 unsigned int mask;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001329 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 ctl = file->private_data;
1332 if (!ctl->subscribed)
1333 return 0;
1334 poll_wait(file, &ctl->change_sleep, wait);
1335
1336 mask = 0;
1337 if (!list_empty(&ctl->events))
1338 mask |= POLLIN | POLLRDNORM;
1339
1340 return mask;
1341}
1342
1343/*
1344 * register the device-specific control-ioctls.
1345 * called from each device manager like pcm.c, hwdep.c, etc.
1346 */
1347static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1348{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001349 struct snd_kctl_ioctl *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001351 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (pn == NULL)
1353 return -ENOMEM;
1354 pn->fioctl = fcn;
1355 down_write(&snd_ioctl_rwsem);
1356 list_add_tail(&pn->list, lists);
1357 up_write(&snd_ioctl_rwsem);
1358 return 0;
1359}
1360
1361int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1362{
1363 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1364}
1365
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001366EXPORT_SYMBOL(snd_ctl_register_ioctl);
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368#ifdef CONFIG_COMPAT
1369int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1370{
1371 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1372}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001373
1374EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375#endif
1376
1377/*
1378 * de-register the device-specific control-ioctls.
1379 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001380static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1381 struct list_head *lists)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001383 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001385 if (snd_BUG_ON(!fcn))
1386 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 down_write(&snd_ioctl_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001388 list_for_each_entry(p, lists, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (p->fioctl == fcn) {
1390 list_del(&p->list);
1391 up_write(&snd_ioctl_rwsem);
1392 kfree(p);
1393 return 0;
1394 }
1395 }
1396 up_write(&snd_ioctl_rwsem);
1397 snd_BUG();
1398 return -EINVAL;
1399}
1400
1401int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1402{
1403 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1404}
1405
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001406EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408#ifdef CONFIG_COMPAT
1409int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1410{
1411 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1412}
1413
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001414EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415#endif
1416
1417static int snd_ctl_fasync(int fd, struct file * file, int on)
1418{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001419 struct snd_ctl_file *ctl;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 ctl = file->private_data;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001422 return fasync_helper(fd, file, on, &ctl->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
1425/*
1426 * ioctl32 compat
1427 */
1428#ifdef CONFIG_COMPAT
1429#include "control_compat.c"
1430#else
1431#define snd_ctl_ioctl_compat NULL
1432#endif
1433
1434/*
1435 * INIT PART
1436 */
1437
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001438static const struct file_operations snd_ctl_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 .owner = THIS_MODULE,
1441 .read = snd_ctl_read,
1442 .open = snd_ctl_open,
1443 .release = snd_ctl_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02001444 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 .poll = snd_ctl_poll,
1446 .unlocked_ioctl = snd_ctl_ioctl,
1447 .compat_ioctl = snd_ctl_ioctl_compat,
1448 .fasync = snd_ctl_fasync,
1449};
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451/*
1452 * registration of the control device
1453 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001454static int snd_ctl_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001456 struct snd_card *card = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 int err, cardnum;
1458 char name[16];
1459
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001460 if (snd_BUG_ON(!card))
1461 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 cardnum = card->number;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001463 if (snd_BUG_ON(cardnum < 0 || cardnum >= SNDRV_CARDS))
1464 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 sprintf(name, "controlC%i", cardnum);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001466 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1467 &snd_ctl_f_ops, card, name)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return err;
1469 return 0;
1470}
1471
1472/*
1473 * disconnection of the control device
1474 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001475static int snd_ctl_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001477 struct snd_card *card = device->device_data;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001478 struct snd_ctl_file *ctl;
Takashi Iwaic4614822006-06-23 14:38:23 +02001479 int err, cardnum;
1480
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001481 if (snd_BUG_ON(!card))
1482 return -ENXIO;
Takashi Iwaic4614822006-06-23 14:38:23 +02001483 cardnum = card->number;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001484 if (snd_BUG_ON(cardnum < 0 || cardnum >= SNDRV_CARDS))
1485 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Takashi Iwaid8009882008-09-07 12:51:13 +02001487 read_lock(&card->ctl_files_rwlock);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001488 list_for_each_entry(ctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 wake_up(&ctl->change_sleep);
1490 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1491 }
Takashi Iwaid8009882008-09-07 12:51:13 +02001492 read_unlock(&card->ctl_files_rwlock);
Takashi Iwaic4614822006-06-23 14:38:23 +02001493
1494 if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
1495 card, -1)) < 0)
1496 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return 0;
1498}
1499
1500/*
1501 * free all controls
1502 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001503static int snd_ctl_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001505 struct snd_card *card = device->device_data;
1506 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 down_write(&card->controls_rwsem);
1509 while (!list_empty(&card->controls)) {
1510 control = snd_kcontrol(card->controls.next);
1511 snd_ctl_remove(card, control);
1512 }
1513 up_write(&card->controls_rwsem);
1514 return 0;
1515}
1516
1517/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 * create control core:
1519 * called from init.c
1520 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001521int snd_ctl_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001523 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 .dev_free = snd_ctl_dev_free,
1525 .dev_register = snd_ctl_dev_register,
1526 .dev_disconnect = snd_ctl_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 };
1528
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001529 if (snd_BUG_ON(!card))
1530 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
1532}
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001533
1534/*
Clemens Ladisch96007322011-01-10 16:25:44 +01001535 * Frequently used control callbacks/helpers
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001536 */
1537int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
1538 struct snd_ctl_elem_info *uinfo)
1539{
1540 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1541 uinfo->count = 1;
1542 uinfo->value.integer.min = 0;
1543 uinfo->value.integer.max = 1;
1544 return 0;
1545}
1546
1547EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
1548
1549int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
1550 struct snd_ctl_elem_info *uinfo)
1551{
1552 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1553 uinfo->count = 2;
1554 uinfo->value.integer.min = 0;
1555 uinfo->value.integer.max = 1;
1556 return 0;
1557}
1558
1559EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
Clemens Ladisch96007322011-01-10 16:25:44 +01001560
1561/**
1562 * snd_ctl_enum_info - fills the info structure for an enumerated control
1563 * @info: the structure to be filled
1564 * @channels: the number of the control's channels; often one
1565 * @items: the number of control values; also the size of @names
1566 * @names: an array containing the names of all control values
1567 *
1568 * Sets all required fields in @info to their appropriate values.
1569 * If the control's accessibility is not the default (readable and writable),
1570 * the caller has to fill @info->access.
1571 */
1572int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
1573 unsigned int items, const char *const names[])
1574{
1575 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1576 info->count = channels;
1577 info->value.enumerated.items = items;
1578 if (info->value.enumerated.item >= items)
1579 info->value.enumerated.item = items - 1;
1580 strlcpy(info->value.enumerated.name,
1581 names[info->value.enumerated.item],
1582 sizeof(info->value.enumerated.name));
1583 return 0;
1584}
1585EXPORT_SYMBOL(snd_ctl_enum_info);