blob: df0774c76f6fc4eba16eb5abc0863ac01bbcedbc [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
22#include <sound/driver.h>
23#include <linux/threads.h>
24#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/slab.h>
26#include <linux/vmalloc.h>
27#include <linux/time.h>
28#include <sound/core.h>
29#include <sound/minors.h>
30#include <sound/info.h>
31#include <sound/control.h>
32
33/* max number of user-defined controls */
34#define MAX_USER_CONTROLS 32
35
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
Clemens Ladischf87135f2005-11-20 14:06:59 +010054 card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (!card) {
56 err = -ENODEV;
57 goto __error1;
58 }
59 err = snd_card_file_add(card, file);
60 if (err < 0) {
61 err = -ENODEV;
62 goto __error1;
63 }
64 if (!try_module_get(card->module)) {
65 err = -EFAULT;
66 goto __error2;
67 }
Takashi Iwaica2c0962005-09-09 14:20:23 +020068 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (ctl == NULL) {
70 err = -ENOMEM;
71 goto __error;
72 }
73 INIT_LIST_HEAD(&ctl->events);
74 init_waitqueue_head(&ctl->change_sleep);
75 spin_lock_init(&ctl->read_lock);
76 ctl->card = card;
Takashi Iwai2529bba2006-08-04 12:57:19 +020077 ctl->prefer_pcm_subdevice = -1;
78 ctl->prefer_rawmidi_subdevice = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 ctl->pid = current->pid;
80 file->private_data = ctl;
81 write_lock_irqsave(&card->ctl_files_rwlock, flags);
82 list_add_tail(&ctl->list, &card->ctl_files);
83 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
84 return 0;
85
86 __error:
87 module_put(card->module);
88 __error2:
89 snd_card_file_remove(card, file);
90 __error1:
91 return err;
92}
93
Takashi Iwai82e9bae2005-11-17 13:53:23 +010094static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Borislav Petkov7507e8d2007-10-22 17:11:09 +020096 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +010097 struct snd_kctl_event *cread;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Borislav Petkov7507e8d2007-10-22 17:11:09 +020099 spin_lock_irqsave(&ctl->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 while (!list_empty(&ctl->events)) {
101 cread = snd_kctl_event(ctl->events.next);
102 list_del(&cread->list);
103 kfree(cread);
104 }
Borislav Petkov7507e8d2007-10-22 17:11:09 +0200105 spin_unlock_irqrestore(&ctl->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108static int snd_ctl_release(struct inode *inode, struct file *file)
109{
110 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100111 struct snd_card *card;
112 struct snd_ctl_file *ctl;
113 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 unsigned int idx;
115
116 ctl = file->private_data;
117 fasync_helper(-1, file, 0, &ctl->fasync);
118 file->private_data = NULL;
119 card = ctl->card;
120 write_lock_irqsave(&card->ctl_files_rwlock, flags);
121 list_del(&ctl->list);
122 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
123 down_write(&card->controls_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200124 list_for_each_entry(control, &card->controls, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 for (idx = 0; idx < control->count; idx++)
126 if (control->vd[idx].owner == ctl)
127 control->vd[idx].owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 up_write(&card->controls_rwsem);
129 snd_ctl_empty_read_queue(ctl);
130 kfree(ctl);
131 module_put(card->module);
132 snd_card_file_remove(card, file);
133 return 0;
134}
135
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100136void snd_ctl_notify(struct snd_card *card, unsigned int mask,
137 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100140 struct snd_ctl_file *ctl;
141 struct snd_kctl_event *ev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200143 snd_assert(card != NULL && id != NULL, return);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 read_lock(&card->ctl_files_rwlock);
145#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
146 card->mixer_oss_change_count++;
147#endif
Johannes Berg9244b2c2006-10-05 16:02:22 +0200148 list_for_each_entry(ctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (!ctl->subscribed)
150 continue;
151 spin_lock_irqsave(&ctl->read_lock, flags);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200152 list_for_each_entry(ev, &ctl->events, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (ev->id.numid == id->numid) {
154 ev->mask |= mask;
155 goto _found;
156 }
157 }
Takashi Iwaica2c0962005-09-09 14:20:23 +0200158 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (ev) {
160 ev->id = *id;
161 ev->mask = mask;
162 list_add_tail(&ev->list, &ctl->events);
163 } else {
164 snd_printk(KERN_ERR "No memory available to allocate event\n");
165 }
166 _found:
167 wake_up(&ctl->change_sleep);
168 spin_unlock_irqrestore(&ctl->read_lock, flags);
169 kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
170 }
171 read_unlock(&card->ctl_files_rwlock);
172}
173
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200174EXPORT_SYMBOL(snd_ctl_notify);
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/**
177 * snd_ctl_new - create a control instance from the template
178 * @control: the control template
179 * @access: the default control access
180 *
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100181 * Allocates a new struct snd_kcontrol instance and copies the given template
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * to the new instance. It does not copy volatile data (access).
183 *
184 * Returns the pointer of the new instance, or NULL on failure.
185 */
Adrian Bunk0b51ba02006-11-20 17:50:17 +0100186static struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control,
187 unsigned int access)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100189 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 unsigned int idx;
191
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200192 snd_assert(control != NULL, return NULL);
193 snd_assert(control->count > 0, return NULL);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100194 kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100195 if (kctl == NULL) {
196 snd_printk(KERN_ERR "Cannot allocate control instance\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return NULL;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 *kctl = *control;
200 for (idx = 0; idx < kctl->count; idx++)
201 kctl->vd[idx].access = access;
202 return kctl;
203}
204
205/**
206 * snd_ctl_new1 - create a control instance from the template
207 * @ncontrol: the initialization record
208 * @private_data: the private data to set
209 *
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100210 * Allocates a new struct snd_kcontrol instance and initialize from the given
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * template. When the access field of ncontrol is 0, it's assumed as
212 * READWRITE access. When the count field is 0, it's assumes as one.
213 *
214 * Returns the pointer of the newly generated instance, or NULL on failure.
215 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100216struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
217 void *private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100219 struct snd_kcontrol kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 unsigned int access;
221
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200222 snd_assert(ncontrol != NULL, return NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 snd_assert(ncontrol->info != NULL, return NULL);
224 memset(&kctl, 0, sizeof(kctl));
225 kctl.id.iface = ncontrol->iface;
226 kctl.id.device = ncontrol->device;
227 kctl.id.subdevice = ncontrol->subdevice;
228 if (ncontrol->name)
229 strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
230 kctl.id.index = ncontrol->index;
231 kctl.count = ncontrol->count ? ncontrol->count : 1;
232 access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200233 (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
234 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
235 SNDRV_CTL_ELEM_ACCESS_DINDIRECT|
236 SNDRV_CTL_ELEM_ACCESS_INDIRECT|
237 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE|
238 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 kctl.info = ncontrol->info;
240 kctl.get = ncontrol->get;
241 kctl.put = ncontrol->put;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200242 kctl.tlv.p = ncontrol->tlv.p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 kctl.private_value = ncontrol->private_value;
244 kctl.private_data = private_data;
245 return snd_ctl_new(&kctl, access);
246}
247
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200248EXPORT_SYMBOL(snd_ctl_new1);
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250/**
251 * snd_ctl_free_one - release the control instance
252 * @kcontrol: the control instance
253 *
254 * Releases the control instance created via snd_ctl_new()
255 * or snd_ctl_new1().
256 * Don't call this after the control was added to the card.
257 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100258void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 if (kcontrol) {
261 if (kcontrol->private_free)
262 kcontrol->private_free(kcontrol);
263 kfree(kcontrol);
264 }
265}
266
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200267EXPORT_SYMBOL(snd_ctl_free_one);
268
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100269static unsigned int snd_ctl_hole_check(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 unsigned int count)
271{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100272 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Johannes Berg9244b2c2006-10-05 16:02:22 +0200274 list_for_each_entry(kctl, &card->controls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if ((kctl->id.numid <= card->last_numid &&
276 kctl->id.numid + kctl->count > card->last_numid) ||
277 (kctl->id.numid <= card->last_numid + count - 1 &&
278 kctl->id.numid + kctl->count > card->last_numid + count - 1))
279 return card->last_numid = kctl->id.numid + kctl->count - 1;
280 }
281 return card->last_numid;
282}
283
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100284static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 unsigned int last_numid, iter = 100000;
287
288 last_numid = card->last_numid;
289 while (last_numid != snd_ctl_hole_check(card, count)) {
290 if (--iter == 0) {
291 /* this situation is very unlikely */
292 snd_printk(KERN_ERR "unable to allocate new control numid\n");
293 return -ENOMEM;
294 }
295 last_numid = card->last_numid;
296 }
297 return 0;
298}
299
300/**
301 * snd_ctl_add - add the control instance to the card
302 * @card: the card instance
303 * @kcontrol: the control instance to add
304 *
305 * Adds the control instance created via snd_ctl_new() or
306 * snd_ctl_new1() to the given card. Assigns also an unique
307 * numid used for fast search.
308 *
309 * Returns zero if successful, or a negative error code on failure.
310 *
311 * It frees automatically the control which cannot be added.
312 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100313int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100315 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 unsigned int idx;
Takashi Iwaic6077b32006-03-21 16:07:13 +0100317 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100319 if (! kcontrol)
Takashi Iwaic6077b32006-03-21 16:07:13 +0100320 return err;
321 snd_assert(card != NULL, goto error);
322 snd_assert(kcontrol->info != NULL, goto error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 id = kcontrol->id;
324 down_write(&card->controls_rwsem);
325 if (snd_ctl_find_id(card, &id)) {
326 up_write(&card->controls_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
328 id.iface,
329 id.device,
330 id.subdevice,
331 id.name,
332 id.index);
Takashi Iwaic6077b32006-03-21 16:07:13 +0100333 err = -EBUSY;
334 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
337 up_write(&card->controls_rwsem);
Takashi Iwaic6077b32006-03-21 16:07:13 +0100338 err = -ENOMEM;
339 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341 list_add_tail(&kcontrol->list, &card->controls);
342 card->controls_count += kcontrol->count;
343 kcontrol->id.numid = card->last_numid + 1;
344 card->last_numid += kcontrol->count;
345 up_write(&card->controls_rwsem);
346 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
347 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
348 return 0;
Takashi Iwaic6077b32006-03-21 16:07:13 +0100349
350 error:
351 snd_ctl_free_one(kcontrol);
352 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200355EXPORT_SYMBOL(snd_ctl_add);
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357/**
358 * snd_ctl_remove - remove the control from the card and release it
359 * @card: the card instance
360 * @kcontrol: the control instance to remove
361 *
362 * Removes the control from the card and then releases the instance.
363 * You don't need to call snd_ctl_free_one(). You must be in
364 * the write lock - down_write(&card->controls_rwsem).
365 *
366 * Returns 0 if successful, or a negative error code on failure.
367 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100368int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100370 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 unsigned int idx;
372
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200373 snd_assert(card != NULL && kcontrol != NULL, return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 list_del(&kcontrol->list);
375 card->controls_count -= kcontrol->count;
376 id = kcontrol->id;
377 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
378 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
379 snd_ctl_free_one(kcontrol);
380 return 0;
381}
382
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200383EXPORT_SYMBOL(snd_ctl_remove);
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385/**
386 * snd_ctl_remove_id - remove the control of the given id and release it
387 * @card: the card instance
388 * @id: the control id to remove
389 *
390 * Finds the control instance with the given id, removes it from the
391 * card list and releases it.
392 *
393 * Returns 0 if successful, or a negative error code on failure.
394 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100395int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100397 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 int ret;
399
400 down_write(&card->controls_rwsem);
401 kctl = snd_ctl_find_id(card, id);
402 if (kctl == NULL) {
403 up_write(&card->controls_rwsem);
404 return -ENOENT;
405 }
406 ret = snd_ctl_remove(card, kctl);
407 up_write(&card->controls_rwsem);
408 return ret;
409}
410
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200411EXPORT_SYMBOL(snd_ctl_remove_id);
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413/**
414 * snd_ctl_remove_unlocked_id - remove the unlocked control of the given id and release it
415 * @file: active control handle
416 * @id: the control id to remove
417 *
418 * Finds the control instance with the given id, removes it from the
419 * card list and releases it.
420 *
421 * Returns 0 if successful, or a negative error code on failure.
422 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100423static int snd_ctl_remove_unlocked_id(struct snd_ctl_file * file,
424 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100426 struct snd_card *card = file->card;
427 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 int idx, ret;
429
430 down_write(&card->controls_rwsem);
431 kctl = snd_ctl_find_id(card, id);
432 if (kctl == NULL) {
433 up_write(&card->controls_rwsem);
434 return -ENOENT;
435 }
436 for (idx = 0; idx < kctl->count; idx++)
437 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
438 up_write(&card->controls_rwsem);
439 return -EBUSY;
440 }
441 ret = snd_ctl_remove(card, kctl);
442 up_write(&card->controls_rwsem);
443 return ret;
444}
445
446/**
447 * snd_ctl_rename_id - replace the id of a control on the card
448 * @card: the card instance
449 * @src_id: the old id
450 * @dst_id: the new id
451 *
452 * Finds the control with the old id from the card, and replaces the
453 * id with the new one.
454 *
455 * Returns zero if successful, or a negative error code on failure.
456 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100457int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
458 struct snd_ctl_elem_id *dst_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100460 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 down_write(&card->controls_rwsem);
463 kctl = snd_ctl_find_id(card, src_id);
464 if (kctl == NULL) {
465 up_write(&card->controls_rwsem);
466 return -ENOENT;
467 }
468 kctl->id = *dst_id;
469 kctl->id.numid = card->last_numid + 1;
470 card->last_numid += kctl->count;
471 up_write(&card->controls_rwsem);
472 return 0;
473}
474
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200475EXPORT_SYMBOL(snd_ctl_rename_id);
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477/**
478 * snd_ctl_find_numid - find the control instance with the given number-id
479 * @card: the card instance
480 * @numid: the number-id to search
481 *
482 * Finds the control instance with the given number-id from the card.
483 *
484 * Returns the pointer of the instance if found, or NULL if not.
485 *
486 * The caller must down card->controls_rwsem before calling this function
487 * (if the race condition can happen).
488 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100489struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100491 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200493 snd_assert(card != NULL && numid != 0, return NULL);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200494 list_for_each_entry(kctl, &card->controls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
496 return kctl;
497 }
498 return NULL;
499}
500
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200501EXPORT_SYMBOL(snd_ctl_find_numid);
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503/**
504 * snd_ctl_find_id - find the control instance with the given id
505 * @card: the card instance
506 * @id: the id to search
507 *
508 * Finds the control instance with the given id from the card.
509 *
510 * Returns the pointer of the instance if found, or NULL if not.
511 *
512 * The caller must down card->controls_rwsem before calling this function
513 * (if the race condition can happen).
514 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100515struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
516 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100518 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200520 snd_assert(card != NULL && id != NULL, return NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (id->numid != 0)
522 return snd_ctl_find_numid(card, id->numid);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200523 list_for_each_entry(kctl, &card->controls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (kctl->id.iface != id->iface)
525 continue;
526 if (kctl->id.device != id->device)
527 continue;
528 if (kctl->id.subdevice != id->subdevice)
529 continue;
530 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
531 continue;
532 if (kctl->id.index > id->index)
533 continue;
534 if (kctl->id.index + kctl->count <= id->index)
535 continue;
536 return kctl;
537 }
538 return NULL;
539}
540
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200541EXPORT_SYMBOL(snd_ctl_find_id);
542
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100543static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 unsigned int cmd, void __user *arg)
545{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100546 struct snd_ctl_card_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Takashi Iwaica2c0962005-09-09 14:20:23 +0200548 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (! info)
550 return -ENOMEM;
551 down_read(&snd_ioctl_rwsem);
552 info->card = card->number;
553 strlcpy(info->id, card->id, sizeof(info->id));
554 strlcpy(info->driver, card->driver, sizeof(info->driver));
555 strlcpy(info->name, card->shortname, sizeof(info->name));
556 strlcpy(info->longname, card->longname, sizeof(info->longname));
557 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
558 strlcpy(info->components, card->components, sizeof(info->components));
559 up_read(&snd_ioctl_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100560 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 kfree(info);
562 return -EFAULT;
563 }
564 kfree(info);
565 return 0;
566}
567
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100568static int snd_ctl_elem_list(struct snd_card *card,
569 struct snd_ctl_elem_list __user *_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 struct list_head *plist;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100572 struct snd_ctl_elem_list list;
573 struct snd_kcontrol *kctl;
574 struct snd_ctl_elem_id *dst, *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 unsigned int offset, space, first, jidx;
576
577 if (copy_from_user(&list, _list, sizeof(list)))
578 return -EFAULT;
579 offset = list.offset;
580 space = list.space;
581 first = 0;
582 /* try limit maximum space */
583 if (space > 16384)
584 return -ENOMEM;
585 if (space > 0) {
586 /* allocate temporary buffer for atomic operation */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100587 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (dst == NULL)
589 return -ENOMEM;
590 down_read(&card->controls_rwsem);
591 list.count = card->controls_count;
592 plist = card->controls.next;
593 while (plist != &card->controls) {
594 if (offset == 0)
595 break;
596 kctl = snd_kcontrol(plist);
597 if (offset < kctl->count)
598 break;
599 offset -= kctl->count;
600 plist = plist->next;
601 }
602 list.used = 0;
603 id = dst;
604 while (space > 0 && plist != &card->controls) {
605 kctl = snd_kcontrol(plist);
606 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
607 snd_ctl_build_ioff(id, kctl, jidx);
608 id++;
609 space--;
610 list.used++;
611 }
612 plist = plist->next;
613 offset = 0;
614 }
615 up_read(&card->controls_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100616 if (list.used > 0 &&
617 copy_to_user(list.pids, dst,
618 list.used * sizeof(struct snd_ctl_elem_id))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 vfree(dst);
620 return -EFAULT;
621 }
622 vfree(dst);
623 } else {
624 down_read(&card->controls_rwsem);
625 list.count = card->controls_count;
626 up_read(&card->controls_rwsem);
627 }
628 if (copy_to_user(_list, &list, sizeof(list)))
629 return -EFAULT;
630 return 0;
631}
632
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100633static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
634 struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100636 struct snd_card *card = ctl->card;
637 struct snd_kcontrol *kctl;
638 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 unsigned int index_offset;
640 int result;
641
642 down_read(&card->controls_rwsem);
643 kctl = snd_ctl_find_id(card, &info->id);
644 if (kctl == NULL) {
645 up_read(&card->controls_rwsem);
646 return -ENOENT;
647 }
648#ifdef CONFIG_SND_DEBUG
649 info->access = 0;
650#endif
651 result = kctl->info(kctl, info);
652 if (result >= 0) {
653 snd_assert(info->access == 0, );
654 index_offset = snd_ctl_get_ioff(kctl, &info->id);
655 vd = &kctl->vd[index_offset];
656 snd_ctl_build_ioff(&info->id, kctl, index_offset);
657 info->access = vd->access;
658 if (vd->owner) {
659 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
660 if (vd->owner == ctl)
661 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
662 info->owner = vd->owner_pid;
663 } else {
664 info->owner = -1;
665 }
666 }
667 up_read(&card->controls_rwsem);
668 return result;
669}
670
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100671static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
672 struct snd_ctl_elem_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100674 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 int result;
676
677 if (copy_from_user(&info, _info, sizeof(info)))
678 return -EFAULT;
Giuliano Pochini64649402006-03-13 14:11:11 +0100679 snd_power_lock(ctl->card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200680 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100681 if (result >= 0)
682 result = snd_ctl_elem_info(ctl, &info);
683 snd_power_unlock(ctl->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (result >= 0)
685 if (copy_to_user(_info, &info, sizeof(info)))
686 return -EFAULT;
687 return result;
688}
689
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100690int snd_ctl_elem_read(struct snd_card *card, struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100692 struct snd_kcontrol *kctl;
693 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 unsigned int index_offset;
695 int result, indirect;
696
697 down_read(&card->controls_rwsem);
698 kctl = snd_ctl_find_id(card, &control->id);
699 if (kctl == NULL) {
700 result = -ENOENT;
701 } else {
702 index_offset = snd_ctl_get_ioff(kctl, &control->id);
703 vd = &kctl->vd[index_offset];
704 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
705 if (control->indirect != indirect) {
706 result = -EACCES;
707 } else {
708 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) && kctl->get != NULL) {
709 snd_ctl_build_ioff(&control->id, kctl, index_offset);
710 result = kctl->get(kctl, control);
711 } else {
712 result = -EPERM;
713 }
714 }
715 }
716 up_read(&card->controls_rwsem);
717 return result;
718}
719
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100720static int snd_ctl_elem_read_user(struct snd_card *card,
721 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100723 struct snd_ctl_elem_value *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 int result;
725
726 control = kmalloc(sizeof(*control), GFP_KERNEL);
727 if (control == NULL)
728 return -ENOMEM;
729 if (copy_from_user(control, _control, sizeof(*control))) {
730 kfree(control);
731 return -EFAULT;
732 }
Giuliano Pochini64649402006-03-13 14:11:11 +0100733 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200734 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100735 if (result >= 0)
736 result = snd_ctl_elem_read(card, control);
737 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (result >= 0)
739 if (copy_to_user(_control, control, sizeof(*control)))
740 result = -EFAULT;
741 kfree(control);
742 return result;
743}
744
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100745int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
746 struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100748 struct snd_kcontrol *kctl;
749 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 unsigned int index_offset;
751 int result, indirect;
752
753 down_read(&card->controls_rwsem);
754 kctl = snd_ctl_find_id(card, &control->id);
755 if (kctl == NULL) {
756 result = -ENOENT;
757 } else {
758 index_offset = snd_ctl_get_ioff(kctl, &control->id);
759 vd = &kctl->vd[index_offset];
760 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
761 if (control->indirect != indirect) {
762 result = -EACCES;
763 } else {
764 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
765 kctl->put == NULL ||
766 (file && vd->owner != NULL && vd->owner != file)) {
767 result = -EPERM;
768 } else {
769 snd_ctl_build_ioff(&control->id, kctl, index_offset);
770 result = kctl->put(kctl, control);
771 }
772 if (result > 0) {
773 up_read(&card->controls_rwsem);
774 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &control->id);
775 return 0;
776 }
777 }
778 }
779 up_read(&card->controls_rwsem);
780 return result;
781}
782
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100783static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
784 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100786 struct snd_ctl_elem_value *control;
Giuliano Pochini64649402006-03-13 14:11:11 +0100787 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 int result;
789
790 control = kmalloc(sizeof(*control), GFP_KERNEL);
791 if (control == NULL)
792 return -ENOMEM;
793 if (copy_from_user(control, _control, sizeof(*control))) {
794 kfree(control);
795 return -EFAULT;
796 }
Giuliano Pochini64649402006-03-13 14:11:11 +0100797 card = file->card;
798 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200799 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100800 if (result >= 0)
801 result = snd_ctl_elem_write(card, file, control);
802 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (result >= 0)
804 if (copy_to_user(_control, control, sizeof(*control)))
805 result = -EFAULT;
806 kfree(control);
807 return result;
808}
809
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100810static int snd_ctl_elem_lock(struct snd_ctl_file *file,
811 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100813 struct snd_card *card = file->card;
814 struct snd_ctl_elem_id id;
815 struct snd_kcontrol *kctl;
816 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 int result;
818
819 if (copy_from_user(&id, _id, sizeof(id)))
820 return -EFAULT;
821 down_write(&card->controls_rwsem);
822 kctl = snd_ctl_find_id(card, &id);
823 if (kctl == NULL) {
824 result = -ENOENT;
825 } else {
826 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
827 if (vd->owner != NULL)
828 result = -EBUSY;
829 else {
830 vd->owner = file;
831 vd->owner_pid = current->pid;
832 result = 0;
833 }
834 }
835 up_write(&card->controls_rwsem);
836 return result;
837}
838
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100839static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
840 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100842 struct snd_card *card = file->card;
843 struct snd_ctl_elem_id id;
844 struct snd_kcontrol *kctl;
845 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 int result;
847
848 if (copy_from_user(&id, _id, sizeof(id)))
849 return -EFAULT;
850 down_write(&card->controls_rwsem);
851 kctl = snd_ctl_find_id(card, &id);
852 if (kctl == NULL) {
853 result = -ENOENT;
854 } else {
855 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
856 if (vd->owner == NULL)
857 result = -EINVAL;
858 else if (vd->owner != file)
859 result = -EPERM;
860 else {
861 vd->owner = NULL;
862 vd->owner_pid = 0;
863 result = 0;
864 }
865 }
866 up_write(&card->controls_rwsem);
867 return result;
868}
869
870struct user_element {
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100871 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 void *elem_data; /* element data */
873 unsigned long elem_data_size; /* size of element data in bytes */
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200874 void *tlv_data; /* TLV data */
875 unsigned long tlv_data_size; /* TLV data size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 void *priv_data; /* private data (like strings for enumerated type) */
877 unsigned long priv_data_size; /* size of private data in bytes */
878};
879
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100880static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
881 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
883 struct user_element *ue = kcontrol->private_data;
884
885 *uinfo = ue->info;
886 return 0;
887}
888
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100889static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
890 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
892 struct user_element *ue = kcontrol->private_data;
893
894 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
895 return 0;
896}
897
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100898static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
899 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 int change;
902 struct user_element *ue = kcontrol->private_data;
903
904 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
905 if (change)
906 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
907 return change;
908}
909
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200910static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol,
911 int op_flag,
912 unsigned int size,
913 unsigned int __user *tlv)
914{
915 struct user_element *ue = kcontrol->private_data;
916 int change = 0;
917 void *new_data;
918
919 if (op_flag > 0) {
920 if (size > 1024 * 128) /* sane value */
921 return -EINVAL;
922 new_data = kmalloc(size, GFP_KERNEL);
923 if (new_data == NULL)
924 return -ENOMEM;
925 if (copy_from_user(new_data, tlv, size)) {
926 kfree(new_data);
927 return -EFAULT;
928 }
929 change = ue->tlv_data_size != size;
930 if (!change)
931 change = memcmp(ue->tlv_data, new_data, size);
932 kfree(ue->tlv_data);
933 ue->tlv_data = new_data;
934 ue->tlv_data_size = size;
935 } else {
Takashi Iwai18c1c3f2006-08-25 11:39:34 +0200936 if (! ue->tlv_data_size || ! ue->tlv_data)
937 return -ENXIO;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200938 if (size < ue->tlv_data_size)
939 return -ENOSPC;
940 if (copy_to_user(tlv, ue->tlv_data, ue->tlv_data_size))
941 return -EFAULT;
942 }
943 return change;
944}
945
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100946static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200948 struct user_element *ue = kcontrol->private_data;
949 if (ue->tlv_data)
950 kfree(ue->tlv_data);
951 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100954static int snd_ctl_elem_add(struct snd_ctl_file *file,
955 struct snd_ctl_elem_info *info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100957 struct snd_card *card = file->card;
958 struct snd_kcontrol kctl, *_kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 unsigned int access;
960 long private_size;
961 struct user_element *ue;
962 int idx, err;
963
964 if (card->user_ctl_count >= MAX_USER_CONTROLS)
965 return -ENOMEM;
966 if (info->count > 1024)
967 return -EINVAL;
968 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100969 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200970 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
971 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 info->id.numid = 0;
973 memset(&kctl, 0, sizeof(kctl));
974 down_write(&card->controls_rwsem);
975 _kctl = snd_ctl_find_id(card, &info->id);
976 err = 0;
977 if (_kctl) {
978 if (replace)
979 err = snd_ctl_remove(card, _kctl);
980 else
981 err = -EBUSY;
982 } else {
983 if (replace)
984 err = -ENOENT;
985 }
986 up_write(&card->controls_rwsem);
987 if (err < 0)
988 return err;
989 memcpy(&kctl.id, &info->id, sizeof(info->id));
990 kctl.count = info->owner ? info->owner : 1;
991 access |= SNDRV_CTL_ELEM_ACCESS_USER;
992 kctl.info = snd_ctl_elem_user_info;
993 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
994 kctl.get = snd_ctl_elem_user_get;
995 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
996 kctl.put = snd_ctl_elem_user_put;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200997 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
998 kctl.tlv.c = snd_ctl_elem_user_tlv;
999 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 switch (info->type) {
1002 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 case SNDRV_CTL_ELEM_TYPE_INTEGER:
1004 private_size = sizeof(long);
1005 if (info->count > 128)
1006 return -EINVAL;
1007 break;
1008 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
1009 private_size = sizeof(long long);
1010 if (info->count > 64)
1011 return -EINVAL;
1012 break;
1013 case SNDRV_CTL_ELEM_TYPE_BYTES:
1014 private_size = sizeof(unsigned char);
1015 if (info->count > 512)
1016 return -EINVAL;
1017 break;
1018 case SNDRV_CTL_ELEM_TYPE_IEC958:
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001019 private_size = sizeof(struct snd_aes_iec958);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (info->count != 1)
1021 return -EINVAL;
1022 break;
1023 default:
1024 return -EINVAL;
1025 }
1026 private_size *= info->count;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001027 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (ue == NULL)
1029 return -ENOMEM;
1030 ue->info = *info;
Takashi Iwai86148e82006-08-24 12:36:36 +02001031 ue->info.access = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 ue->elem_data = (char *)ue + sizeof(*ue);
1033 ue->elem_data_size = private_size;
1034 kctl.private_free = snd_ctl_elem_user_free;
1035 _kctl = snd_ctl_new(&kctl, access);
1036 if (_kctl == NULL) {
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001037 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return -ENOMEM;
1039 }
1040 _kctl->private_data = ue;
1041 for (idx = 0; idx < _kctl->count; idx++)
1042 _kctl->vd[idx].owner = file;
1043 err = snd_ctl_add(card, _kctl);
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001044 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 down_write(&card->controls_rwsem);
1048 card->user_ctl_count++;
1049 up_write(&card->controls_rwsem);
1050
1051 return 0;
1052}
1053
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001054static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1055 struct snd_ctl_elem_info __user *_info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001057 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (copy_from_user(&info, _info, sizeof(info)))
1059 return -EFAULT;
1060 return snd_ctl_elem_add(file, &info, replace);
1061}
1062
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001063static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1064 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001066 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 int err;
1068
1069 if (copy_from_user(&id, _id, sizeof(id)))
1070 return -EFAULT;
1071 err = snd_ctl_remove_unlocked_id(file, &id);
1072 if (! err) {
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001073 struct snd_card *card = file->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 down_write(&card->controls_rwsem);
1075 card->user_ctl_count--;
1076 up_write(&card->controls_rwsem);
1077 }
1078 return err;
1079}
1080
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001081static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 int subscribe;
1084 if (get_user(subscribe, ptr))
1085 return -EFAULT;
1086 if (subscribe < 0) {
1087 subscribe = file->subscribed;
1088 if (put_user(subscribe, ptr))
1089 return -EFAULT;
1090 return 0;
1091 }
1092 if (subscribe) {
1093 file->subscribed = 1;
1094 return 0;
1095 } else if (file->subscribed) {
1096 snd_ctl_empty_read_queue(file);
1097 file->subscribed = 0;
1098 }
1099 return 0;
1100}
1101
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001102static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1103 struct snd_ctl_tlv __user *_tlv,
1104 int op_flag)
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001105{
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001106 struct snd_card *card = file->card;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001107 struct snd_ctl_tlv tlv;
1108 struct snd_kcontrol *kctl;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001109 struct snd_kcontrol_volatile *vd;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001110 unsigned int len;
1111 int err = 0;
1112
1113 if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
1114 return -EFAULT;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001115 if (tlv.length < sizeof(unsigned int) * 3)
1116 return -EINVAL;
1117 down_read(&card->controls_rwsem);
1118 kctl = snd_ctl_find_numid(card, tlv.numid);
1119 if (kctl == NULL) {
1120 err = -ENOENT;
1121 goto __kctl_end;
1122 }
1123 if (kctl->tlv.p == NULL) {
1124 err = -ENXIO;
1125 goto __kctl_end;
1126 }
1127 vd = &kctl->vd[tlv.numid - kctl->id.numid];
1128 if ((op_flag == 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) == 0) ||
1129 (op_flag > 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) == 0) ||
1130 (op_flag < 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND) == 0)) {
1131 err = -ENXIO;
1132 goto __kctl_end;
1133 }
1134 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1135 if (file && vd->owner != NULL && vd->owner != file) {
1136 err = -EPERM;
1137 goto __kctl_end;
1138 }
1139 err = kctl->tlv.c(kctl, op_flag, tlv.length, _tlv->tlv);
1140 if (err > 0) {
1141 up_read(&card->controls_rwsem);
1142 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &kctl->id);
1143 return 0;
1144 }
1145 } else {
1146 if (op_flag) {
1147 err = -ENXIO;
1148 goto __kctl_end;
1149 }
1150 len = kctl->tlv.p[1] + 2 * sizeof(unsigned int);
1151 if (tlv.length < len) {
1152 err = -ENOMEM;
1153 goto __kctl_end;
1154 }
1155 if (copy_to_user(_tlv->tlv, kctl->tlv.p, len))
1156 err = -EFAULT;
1157 }
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001158 __kctl_end:
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001159 up_read(&card->controls_rwsem);
1160 return err;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001161}
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1164{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001165 struct snd_ctl_file *ctl;
1166 struct snd_card *card;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001167 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 void __user *argp = (void __user *)arg;
1169 int __user *ip = argp;
1170 int err;
1171
1172 ctl = file->private_data;
1173 card = ctl->card;
1174 snd_assert(card != NULL, return -ENXIO);
1175 switch (cmd) {
1176 case SNDRV_CTL_IOCTL_PVERSION:
1177 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1178 case SNDRV_CTL_IOCTL_CARD_INFO:
1179 return snd_ctl_card_info(card, ctl, cmd, argp);
1180 case SNDRV_CTL_IOCTL_ELEM_LIST:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001181 return snd_ctl_elem_list(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 case SNDRV_CTL_IOCTL_ELEM_INFO:
1183 return snd_ctl_elem_info_user(ctl, argp);
1184 case SNDRV_CTL_IOCTL_ELEM_READ:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001185 return snd_ctl_elem_read_user(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1187 return snd_ctl_elem_write_user(ctl, argp);
1188 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1189 return snd_ctl_elem_lock(ctl, argp);
1190 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1191 return snd_ctl_elem_unlock(ctl, argp);
1192 case SNDRV_CTL_IOCTL_ELEM_ADD:
1193 return snd_ctl_elem_add_user(ctl, argp, 0);
1194 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1195 return snd_ctl_elem_add_user(ctl, argp, 1);
1196 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1197 return snd_ctl_elem_remove(ctl, argp);
1198 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1199 return snd_ctl_subscribe_events(ctl, ip);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001200 case SNDRV_CTL_IOCTL_TLV_READ:
1201 return snd_ctl_tlv_ioctl(ctl, argp, 0);
1202 case SNDRV_CTL_IOCTL_TLV_WRITE:
1203 return snd_ctl_tlv_ioctl(ctl, argp, 1);
1204 case SNDRV_CTL_IOCTL_TLV_COMMAND:
1205 return snd_ctl_tlv_ioctl(ctl, argp, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 case SNDRV_CTL_IOCTL_POWER:
Takashi Iwaia381a7a2005-11-17 15:55:49 +01001207 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 case SNDRV_CTL_IOCTL_POWER_STATE:
1209#ifdef CONFIG_PM
1210 return put_user(card->power_state, ip) ? -EFAULT : 0;
1211#else
1212 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1213#endif
1214 }
1215 down_read(&snd_ioctl_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001216 list_for_each_entry(p, &snd_control_ioctls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 err = p->fioctl(card, ctl, cmd, arg);
1218 if (err != -ENOIOCTLCMD) {
1219 up_read(&snd_ioctl_rwsem);
1220 return err;
1221 }
1222 }
1223 up_read(&snd_ioctl_rwsem);
Takashi Iwai6d85be62005-05-15 14:32:50 +02001224 snd_printdd("unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return -ENOTTY;
1226}
1227
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001228static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1229 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001231 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 int err = 0;
1233 ssize_t result = 0;
1234
1235 ctl = file->private_data;
1236 snd_assert(ctl != NULL && ctl->card != NULL, return -ENXIO);
1237 if (!ctl->subscribed)
1238 return -EBADFD;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001239 if (count < sizeof(struct snd_ctl_event))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return -EINVAL;
1241 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001242 while (count >= sizeof(struct snd_ctl_event)) {
1243 struct snd_ctl_event ev;
1244 struct snd_kctl_event *kev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 while (list_empty(&ctl->events)) {
1246 wait_queue_t wait;
1247 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1248 err = -EAGAIN;
1249 goto __end_lock;
1250 }
1251 init_waitqueue_entry(&wait, current);
1252 add_wait_queue(&ctl->change_sleep, &wait);
1253 set_current_state(TASK_INTERRUPTIBLE);
1254 spin_unlock_irq(&ctl->read_lock);
1255 schedule();
1256 remove_wait_queue(&ctl->change_sleep, &wait);
1257 if (signal_pending(current))
Adrian Bunk0e5d7202006-11-07 13:42:54 +01001258 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 spin_lock_irq(&ctl->read_lock);
1260 }
1261 kev = snd_kctl_event(ctl->events.next);
1262 ev.type = SNDRV_CTL_EVENT_ELEM;
1263 ev.data.elem.mask = kev->mask;
1264 ev.data.elem.id = kev->id;
1265 list_del(&kev->list);
1266 spin_unlock_irq(&ctl->read_lock);
1267 kfree(kev);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001268 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 err = -EFAULT;
1270 goto __end;
1271 }
1272 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001273 buffer += sizeof(struct snd_ctl_event);
1274 count -= sizeof(struct snd_ctl_event);
1275 result += sizeof(struct snd_ctl_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
1277 __end_lock:
1278 spin_unlock_irq(&ctl->read_lock);
1279 __end:
1280 return result > 0 ? result : err;
1281}
1282
1283static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1284{
1285 unsigned int mask;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001286 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 ctl = file->private_data;
1289 if (!ctl->subscribed)
1290 return 0;
1291 poll_wait(file, &ctl->change_sleep, wait);
1292
1293 mask = 0;
1294 if (!list_empty(&ctl->events))
1295 mask |= POLLIN | POLLRDNORM;
1296
1297 return mask;
1298}
1299
1300/*
1301 * register the device-specific control-ioctls.
1302 * called from each device manager like pcm.c, hwdep.c, etc.
1303 */
1304static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1305{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001306 struct snd_kctl_ioctl *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001308 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if (pn == NULL)
1310 return -ENOMEM;
1311 pn->fioctl = fcn;
1312 down_write(&snd_ioctl_rwsem);
1313 list_add_tail(&pn->list, lists);
1314 up_write(&snd_ioctl_rwsem);
1315 return 0;
1316}
1317
1318int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1319{
1320 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1321}
1322
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001323EXPORT_SYMBOL(snd_ctl_register_ioctl);
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325#ifdef CONFIG_COMPAT
1326int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1327{
1328 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1329}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001330
1331EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332#endif
1333
1334/*
1335 * de-register the device-specific control-ioctls.
1336 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001337static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1338 struct list_head *lists)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001340 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Takashi Iwai7c22f1a2005-10-10 11:46:31 +02001342 snd_assert(fcn != NULL, return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 down_write(&snd_ioctl_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001344 list_for_each_entry(p, lists, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (p->fioctl == fcn) {
1346 list_del(&p->list);
1347 up_write(&snd_ioctl_rwsem);
1348 kfree(p);
1349 return 0;
1350 }
1351 }
1352 up_write(&snd_ioctl_rwsem);
1353 snd_BUG();
1354 return -EINVAL;
1355}
1356
1357int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1358{
1359 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1360}
1361
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001362EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364#ifdef CONFIG_COMPAT
1365int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1366{
1367 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1368}
1369
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001370EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371#endif
1372
1373static int snd_ctl_fasync(int fd, struct file * file, int on)
1374{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001375 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 int err;
1377 ctl = file->private_data;
1378 err = fasync_helper(fd, file, on, &ctl->fasync);
1379 if (err < 0)
1380 return err;
1381 return 0;
1382}
1383
1384/*
1385 * ioctl32 compat
1386 */
1387#ifdef CONFIG_COMPAT
1388#include "control_compat.c"
1389#else
1390#define snd_ctl_ioctl_compat NULL
1391#endif
1392
1393/*
1394 * INIT PART
1395 */
1396
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001397static const struct file_operations snd_ctl_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
1399 .owner = THIS_MODULE,
1400 .read = snd_ctl_read,
1401 .open = snd_ctl_open,
1402 .release = snd_ctl_release,
1403 .poll = snd_ctl_poll,
1404 .unlocked_ioctl = snd_ctl_ioctl,
1405 .compat_ioctl = snd_ctl_ioctl_compat,
1406 .fasync = snd_ctl_fasync,
1407};
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409/*
1410 * registration of the control device
1411 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001412static int snd_ctl_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001414 struct snd_card *card = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 int err, cardnum;
1416 char name[16];
1417
1418 snd_assert(card != NULL, return -ENXIO);
1419 cardnum = card->number;
1420 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1421 sprintf(name, "controlC%i", cardnum);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001422 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1423 &snd_ctl_f_ops, card, name)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return err;
1425 return 0;
1426}
1427
1428/*
1429 * disconnection of the control device
1430 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001431static int snd_ctl_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001433 struct snd_card *card = device->device_data;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001434 struct snd_ctl_file *ctl;
Takashi Iwaic4614822006-06-23 14:38:23 +02001435 int err, cardnum;
1436
1437 snd_assert(card != NULL, return -ENXIO);
1438 cardnum = card->number;
1439 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 down_read(&card->controls_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001442 list_for_each_entry(ctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 wake_up(&ctl->change_sleep);
1444 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1445 }
1446 up_read(&card->controls_rwsem);
Takashi Iwaic4614822006-06-23 14:38:23 +02001447
1448 if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
1449 card, -1)) < 0)
1450 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return 0;
1452}
1453
1454/*
1455 * free all controls
1456 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001457static int snd_ctl_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001459 struct snd_card *card = device->device_data;
1460 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 down_write(&card->controls_rwsem);
1463 while (!list_empty(&card->controls)) {
1464 control = snd_kcontrol(card->controls.next);
1465 snd_ctl_remove(card, control);
1466 }
1467 up_write(&card->controls_rwsem);
1468 return 0;
1469}
1470
1471/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 * create control core:
1473 * called from init.c
1474 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001475int snd_ctl_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001477 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 .dev_free = snd_ctl_dev_free,
1479 .dev_register = snd_ctl_dev_register,
1480 .dev_disconnect = snd_ctl_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 };
1482
1483 snd_assert(card != NULL, return -ENXIO);
1484 return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
1485}
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001486
1487/*
1488 * Frequently used control callbacks
1489 */
1490int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
1491 struct snd_ctl_elem_info *uinfo)
1492{
1493 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1494 uinfo->count = 1;
1495 uinfo->value.integer.min = 0;
1496 uinfo->value.integer.max = 1;
1497 return 0;
1498}
1499
1500EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
1501
1502int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
1503 struct snd_ctl_elem_info *uinfo)
1504{
1505 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1506 uinfo->count = 2;
1507 uinfo->value.integer.min = 0;
1508 uinfo->value.integer.max = 1;
1509 return 0;
1510}
1511
1512EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);