blob: 3030aaa6d2c5cd14d9be4113e7098755c6da4d06 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines for driver control interface
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4 *
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>
25#include <linux/smp_lock.h>
26#include <linux/slab.h>
27#include <linux/vmalloc.h>
28#include <linux/time.h>
29#include <sound/core.h>
30#include <sound/minors.h>
31#include <sound/info.h>
32#include <sound/control.h>
33
34/* max number of user-defined controls */
35#define MAX_USER_CONTROLS 32
36
Takashi Iwai82e9bae2005-11-17 13:53:23 +010037struct snd_kctl_ioctl {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct list_head list; /* list of all ioctls */
39 snd_kctl_ioctl_func_t fioctl;
Takashi Iwai82e9bae2005-11-17 13:53:23 +010040};
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42static DECLARE_RWSEM(snd_ioctl_rwsem);
43static LIST_HEAD(snd_control_ioctls);
44#ifdef CONFIG_COMPAT
45static LIST_HEAD(snd_control_compat_ioctls);
46#endif
47
48static int snd_ctl_open(struct inode *inode, struct file *file)
49{
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +010051 struct snd_card *card;
52 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 int err;
54
Clemens Ladischf87135f2005-11-20 14:06:59 +010055 card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 if (!card) {
57 err = -ENODEV;
58 goto __error1;
59 }
60 err = snd_card_file_add(card, file);
61 if (err < 0) {
62 err = -ENODEV;
63 goto __error1;
64 }
65 if (!try_module_get(card->module)) {
66 err = -EFAULT;
67 goto __error2;
68 }
Takashi Iwaica2c0962005-09-09 14:20:23 +020069 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 if (ctl == NULL) {
71 err = -ENOMEM;
72 goto __error;
73 }
74 INIT_LIST_HEAD(&ctl->events);
75 init_waitqueue_head(&ctl->change_sleep);
76 spin_lock_init(&ctl->read_lock);
77 ctl->card = card;
Takashi Iwai2529bba2006-08-04 12:57:19 +020078 ctl->prefer_pcm_subdevice = -1;
79 ctl->prefer_rawmidi_subdevice = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 ctl->pid = current->pid;
81 file->private_data = ctl;
82 write_lock_irqsave(&card->ctl_files_rwlock, flags);
83 list_add_tail(&ctl->list, &card->ctl_files);
84 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
85 return 0;
86
87 __error:
88 module_put(card->module);
89 __error2:
90 snd_card_file_remove(card, file);
91 __error1:
92 return err;
93}
94
Takashi Iwai82e9bae2005-11-17 13:53:23 +010095static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Takashi Iwai82e9bae2005-11-17 13:53:23 +010097 struct snd_kctl_event *cread;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 spin_lock(&ctl->read_lock);
100 while (!list_empty(&ctl->events)) {
101 cread = snd_kctl_event(ctl->events.next);
102 list_del(&cread->list);
103 kfree(cread);
104 }
105 spin_unlock(&ctl->read_lock);
106}
107
108static int snd_ctl_release(struct inode *inode, struct file *file)
109{
110 unsigned long flags;
111 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100112 struct snd_card *card;
113 struct snd_ctl_file *ctl;
114 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 unsigned int idx;
116
117 ctl = file->private_data;
118 fasync_helper(-1, file, 0, &ctl->fasync);
119 file->private_data = NULL;
120 card = ctl->card;
121 write_lock_irqsave(&card->ctl_files_rwlock, flags);
122 list_del(&ctl->list);
123 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
124 down_write(&card->controls_rwsem);
125 list_for_each(list, &card->controls) {
126 control = snd_kcontrol(list);
127 for (idx = 0; idx < control->count; idx++)
128 if (control->vd[idx].owner == ctl)
129 control->vd[idx].owner = NULL;
130 }
131 up_write(&card->controls_rwsem);
132 snd_ctl_empty_read_queue(ctl);
133 kfree(ctl);
134 module_put(card->module);
135 snd_card_file_remove(card, file);
136 return 0;
137}
138
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100139void snd_ctl_notify(struct snd_card *card, unsigned int mask,
140 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 unsigned long flags;
143 struct list_head *flist;
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 Iwai7c22f1a2005-10-10 11:46:31 +0200147 snd_assert(card != NULL && id != NULL, return);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 read_lock(&card->ctl_files_rwlock);
149#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
150 card->mixer_oss_change_count++;
151#endif
152 list_for_each(flist, &card->ctl_files) {
153 struct list_head *elist;
154 ctl = snd_ctl_file(flist);
155 if (!ctl->subscribed)
156 continue;
157 spin_lock_irqsave(&ctl->read_lock, flags);
158 list_for_each(elist, &ctl->events) {
159 ev = snd_kctl_event(elist);
160 if (ev->id.numid == id->numid) {
161 ev->mask |= mask;
162 goto _found;
163 }
164 }
Takashi Iwaica2c0962005-09-09 14:20:23 +0200165 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (ev) {
167 ev->id = *id;
168 ev->mask = mask;
169 list_add_tail(&ev->list, &ctl->events);
170 } else {
171 snd_printk(KERN_ERR "No memory available to allocate event\n");
172 }
173 _found:
174 wake_up(&ctl->change_sleep);
175 spin_unlock_irqrestore(&ctl->read_lock, flags);
176 kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
177 }
178 read_unlock(&card->ctl_files_rwlock);
179}
180
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200181EXPORT_SYMBOL(snd_ctl_notify);
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/**
184 * snd_ctl_new - create a control instance from the template
185 * @control: the control template
186 * @access: the default control access
187 *
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100188 * Allocates a new struct snd_kcontrol instance and copies the given template
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * to the new instance. It does not copy volatile data (access).
190 *
191 * Returns the pointer of the new instance, or NULL on failure.
192 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100193struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control, unsigned int access)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100195 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 unsigned int idx;
197
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200198 snd_assert(control != NULL, return NULL);
199 snd_assert(control->count > 0, return NULL);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100200 kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100201 if (kctl == NULL) {
202 snd_printk(KERN_ERR "Cannot allocate control instance\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return NULL;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 *kctl = *control;
206 for (idx = 0; idx < kctl->count; idx++)
207 kctl->vd[idx].access = access;
208 return kctl;
209}
210
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200211EXPORT_SYMBOL(snd_ctl_new);
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/**
214 * snd_ctl_new1 - create a control instance from the template
215 * @ncontrol: the initialization record
216 * @private_data: the private data to set
217 *
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100218 * Allocates a new struct snd_kcontrol instance and initialize from the given
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * template. When the access field of ncontrol is 0, it's assumed as
220 * READWRITE access. When the count field is 0, it's assumes as one.
221 *
222 * Returns the pointer of the newly generated instance, or NULL on failure.
223 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100224struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
225 void *private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100227 struct snd_kcontrol kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 unsigned int access;
229
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200230 snd_assert(ncontrol != NULL, return NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 snd_assert(ncontrol->info != NULL, return NULL);
232 memset(&kctl, 0, sizeof(kctl));
233 kctl.id.iface = ncontrol->iface;
234 kctl.id.device = ncontrol->device;
235 kctl.id.subdevice = ncontrol->subdevice;
236 if (ncontrol->name)
237 strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
238 kctl.id.index = ncontrol->index;
239 kctl.count = ncontrol->count ? ncontrol->count : 1;
240 access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200241 (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
242 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
243 SNDRV_CTL_ELEM_ACCESS_DINDIRECT|
244 SNDRV_CTL_ELEM_ACCESS_INDIRECT|
245 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE|
246 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 kctl.info = ncontrol->info;
248 kctl.get = ncontrol->get;
249 kctl.put = ncontrol->put;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200250 kctl.tlv.p = ncontrol->tlv.p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 kctl.private_value = ncontrol->private_value;
252 kctl.private_data = private_data;
253 return snd_ctl_new(&kctl, access);
254}
255
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200256EXPORT_SYMBOL(snd_ctl_new1);
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258/**
259 * snd_ctl_free_one - release the control instance
260 * @kcontrol: the control instance
261 *
262 * Releases the control instance created via snd_ctl_new()
263 * or snd_ctl_new1().
264 * Don't call this after the control was added to the card.
265 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100266void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 if (kcontrol) {
269 if (kcontrol->private_free)
270 kcontrol->private_free(kcontrol);
271 kfree(kcontrol);
272 }
273}
274
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200275EXPORT_SYMBOL(snd_ctl_free_one);
276
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100277static unsigned int snd_ctl_hole_check(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 unsigned int count)
279{
280 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100281 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 list_for_each(list, &card->controls) {
284 kctl = snd_kcontrol(list);
285 if ((kctl->id.numid <= card->last_numid &&
286 kctl->id.numid + kctl->count > card->last_numid) ||
287 (kctl->id.numid <= card->last_numid + count - 1 &&
288 kctl->id.numid + kctl->count > card->last_numid + count - 1))
289 return card->last_numid = kctl->id.numid + kctl->count - 1;
290 }
291 return card->last_numid;
292}
293
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100294static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 unsigned int last_numid, iter = 100000;
297
298 last_numid = card->last_numid;
299 while (last_numid != snd_ctl_hole_check(card, count)) {
300 if (--iter == 0) {
301 /* this situation is very unlikely */
302 snd_printk(KERN_ERR "unable to allocate new control numid\n");
303 return -ENOMEM;
304 }
305 last_numid = card->last_numid;
306 }
307 return 0;
308}
309
310/**
311 * snd_ctl_add - add the control instance to the card
312 * @card: the card instance
313 * @kcontrol: the control instance to add
314 *
315 * Adds the control instance created via snd_ctl_new() or
316 * snd_ctl_new1() to the given card. Assigns also an unique
317 * numid used for fast search.
318 *
319 * Returns zero if successful, or a negative error code on failure.
320 *
321 * It frees automatically the control which cannot be added.
322 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100323int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100325 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 unsigned int idx;
Takashi Iwaic6077b32006-03-21 16:07:13 +0100327 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100329 if (! kcontrol)
Takashi Iwaic6077b32006-03-21 16:07:13 +0100330 return err;
331 snd_assert(card != NULL, goto error);
332 snd_assert(kcontrol->info != NULL, goto error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 id = kcontrol->id;
334 down_write(&card->controls_rwsem);
335 if (snd_ctl_find_id(card, &id)) {
336 up_write(&card->controls_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
338 id.iface,
339 id.device,
340 id.subdevice,
341 id.name,
342 id.index);
Takashi Iwaic6077b32006-03-21 16:07:13 +0100343 err = -EBUSY;
344 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
347 up_write(&card->controls_rwsem);
Takashi Iwaic6077b32006-03-21 16:07:13 +0100348 err = -ENOMEM;
349 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351 list_add_tail(&kcontrol->list, &card->controls);
352 card->controls_count += kcontrol->count;
353 kcontrol->id.numid = card->last_numid + 1;
354 card->last_numid += kcontrol->count;
355 up_write(&card->controls_rwsem);
356 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
357 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
358 return 0;
Takashi Iwaic6077b32006-03-21 16:07:13 +0100359
360 error:
361 snd_ctl_free_one(kcontrol);
362 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200365EXPORT_SYMBOL(snd_ctl_add);
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367/**
368 * snd_ctl_remove - remove the control from the card and release it
369 * @card: the card instance
370 * @kcontrol: the control instance to remove
371 *
372 * Removes the control from the card and then releases the instance.
373 * You don't need to call snd_ctl_free_one(). You must be in
374 * the write lock - down_write(&card->controls_rwsem).
375 *
376 * Returns 0 if successful, or a negative error code on failure.
377 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100378int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100380 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 unsigned int idx;
382
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200383 snd_assert(card != NULL && kcontrol != NULL, return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 list_del(&kcontrol->list);
385 card->controls_count -= kcontrol->count;
386 id = kcontrol->id;
387 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
388 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
389 snd_ctl_free_one(kcontrol);
390 return 0;
391}
392
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200393EXPORT_SYMBOL(snd_ctl_remove);
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395/**
396 * snd_ctl_remove_id - remove the control of the given id and release it
397 * @card: the card instance
398 * @id: the control id to remove
399 *
400 * Finds the control instance with the given id, removes it from the
401 * card list and releases it.
402 *
403 * Returns 0 if successful, or a negative error code on failure.
404 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100405int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100407 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 int ret;
409
410 down_write(&card->controls_rwsem);
411 kctl = snd_ctl_find_id(card, id);
412 if (kctl == NULL) {
413 up_write(&card->controls_rwsem);
414 return -ENOENT;
415 }
416 ret = snd_ctl_remove(card, kctl);
417 up_write(&card->controls_rwsem);
418 return ret;
419}
420
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200421EXPORT_SYMBOL(snd_ctl_remove_id);
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423/**
424 * snd_ctl_remove_unlocked_id - remove the unlocked control of the given id and release it
425 * @file: active control handle
426 * @id: the control id to remove
427 *
428 * Finds the control instance with the given id, removes it from the
429 * card list and releases it.
430 *
431 * Returns 0 if successful, or a negative error code on failure.
432 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100433static int snd_ctl_remove_unlocked_id(struct snd_ctl_file * file,
434 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100436 struct snd_card *card = file->card;
437 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 int idx, ret;
439
440 down_write(&card->controls_rwsem);
441 kctl = snd_ctl_find_id(card, id);
442 if (kctl == NULL) {
443 up_write(&card->controls_rwsem);
444 return -ENOENT;
445 }
446 for (idx = 0; idx < kctl->count; idx++)
447 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
448 up_write(&card->controls_rwsem);
449 return -EBUSY;
450 }
451 ret = snd_ctl_remove(card, kctl);
452 up_write(&card->controls_rwsem);
453 return ret;
454}
455
456/**
457 * snd_ctl_rename_id - replace the id of a control on the card
458 * @card: the card instance
459 * @src_id: the old id
460 * @dst_id: the new id
461 *
462 * Finds the control with the old id from the card, and replaces the
463 * id with the new one.
464 *
465 * Returns zero if successful, or a negative error code on failure.
466 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100467int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
468 struct snd_ctl_elem_id *dst_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100470 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 down_write(&card->controls_rwsem);
473 kctl = snd_ctl_find_id(card, src_id);
474 if (kctl == NULL) {
475 up_write(&card->controls_rwsem);
476 return -ENOENT;
477 }
478 kctl->id = *dst_id;
479 kctl->id.numid = card->last_numid + 1;
480 card->last_numid += kctl->count;
481 up_write(&card->controls_rwsem);
482 return 0;
483}
484
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200485EXPORT_SYMBOL(snd_ctl_rename_id);
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487/**
488 * snd_ctl_find_numid - find the control instance with the given number-id
489 * @card: the card instance
490 * @numid: the number-id to search
491 *
492 * Finds the control instance with the given number-id from the card.
493 *
494 * Returns the pointer of the instance if found, or NULL if not.
495 *
496 * The caller must down card->controls_rwsem before calling this function
497 * (if the race condition can happen).
498 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100499struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100502 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200504 snd_assert(card != NULL && numid != 0, return NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 list_for_each(list, &card->controls) {
506 kctl = snd_kcontrol(list);
507 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
508 return kctl;
509 }
510 return NULL;
511}
512
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200513EXPORT_SYMBOL(snd_ctl_find_numid);
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515/**
516 * snd_ctl_find_id - find the control instance with the given id
517 * @card: the card instance
518 * @id: the id to search
519 *
520 * Finds the control instance with the given id from the card.
521 *
522 * Returns the pointer of the instance if found, or NULL if not.
523 *
524 * The caller must down card->controls_rwsem before calling this function
525 * (if the race condition can happen).
526 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100527struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
528 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100531 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200533 snd_assert(card != NULL && id != NULL, return NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (id->numid != 0)
535 return snd_ctl_find_numid(card, id->numid);
536 list_for_each(list, &card->controls) {
537 kctl = snd_kcontrol(list);
538 if (kctl->id.iface != id->iface)
539 continue;
540 if (kctl->id.device != id->device)
541 continue;
542 if (kctl->id.subdevice != id->subdevice)
543 continue;
544 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
545 continue;
546 if (kctl->id.index > id->index)
547 continue;
548 if (kctl->id.index + kctl->count <= id->index)
549 continue;
550 return kctl;
551 }
552 return NULL;
553}
554
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200555EXPORT_SYMBOL(snd_ctl_find_id);
556
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100557static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 unsigned int cmd, void __user *arg)
559{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100560 struct snd_ctl_card_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Takashi Iwaica2c0962005-09-09 14:20:23 +0200562 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (! info)
564 return -ENOMEM;
565 down_read(&snd_ioctl_rwsem);
566 info->card = card->number;
567 strlcpy(info->id, card->id, sizeof(info->id));
568 strlcpy(info->driver, card->driver, sizeof(info->driver));
569 strlcpy(info->name, card->shortname, sizeof(info->name));
570 strlcpy(info->longname, card->longname, sizeof(info->longname));
571 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
572 strlcpy(info->components, card->components, sizeof(info->components));
573 up_read(&snd_ioctl_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100574 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 kfree(info);
576 return -EFAULT;
577 }
578 kfree(info);
579 return 0;
580}
581
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100582static int snd_ctl_elem_list(struct snd_card *card,
583 struct snd_ctl_elem_list __user *_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 struct list_head *plist;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100586 struct snd_ctl_elem_list list;
587 struct snd_kcontrol *kctl;
588 struct snd_ctl_elem_id *dst, *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 unsigned int offset, space, first, jidx;
590
591 if (copy_from_user(&list, _list, sizeof(list)))
592 return -EFAULT;
593 offset = list.offset;
594 space = list.space;
595 first = 0;
596 /* try limit maximum space */
597 if (space > 16384)
598 return -ENOMEM;
599 if (space > 0) {
600 /* allocate temporary buffer for atomic operation */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100601 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (dst == NULL)
603 return -ENOMEM;
604 down_read(&card->controls_rwsem);
605 list.count = card->controls_count;
606 plist = card->controls.next;
607 while (plist != &card->controls) {
608 if (offset == 0)
609 break;
610 kctl = snd_kcontrol(plist);
611 if (offset < kctl->count)
612 break;
613 offset -= kctl->count;
614 plist = plist->next;
615 }
616 list.used = 0;
617 id = dst;
618 while (space > 0 && plist != &card->controls) {
619 kctl = snd_kcontrol(plist);
620 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
621 snd_ctl_build_ioff(id, kctl, jidx);
622 id++;
623 space--;
624 list.used++;
625 }
626 plist = plist->next;
627 offset = 0;
628 }
629 up_read(&card->controls_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100630 if (list.used > 0 &&
631 copy_to_user(list.pids, dst,
632 list.used * sizeof(struct snd_ctl_elem_id))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 vfree(dst);
634 return -EFAULT;
635 }
636 vfree(dst);
637 } else {
638 down_read(&card->controls_rwsem);
639 list.count = card->controls_count;
640 up_read(&card->controls_rwsem);
641 }
642 if (copy_to_user(_list, &list, sizeof(list)))
643 return -EFAULT;
644 return 0;
645}
646
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100647static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
648 struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100650 struct snd_card *card = ctl->card;
651 struct snd_kcontrol *kctl;
652 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 unsigned int index_offset;
654 int result;
655
656 down_read(&card->controls_rwsem);
657 kctl = snd_ctl_find_id(card, &info->id);
658 if (kctl == NULL) {
659 up_read(&card->controls_rwsem);
660 return -ENOENT;
661 }
662#ifdef CONFIG_SND_DEBUG
663 info->access = 0;
664#endif
665 result = kctl->info(kctl, info);
666 if (result >= 0) {
667 snd_assert(info->access == 0, );
668 index_offset = snd_ctl_get_ioff(kctl, &info->id);
669 vd = &kctl->vd[index_offset];
670 snd_ctl_build_ioff(&info->id, kctl, index_offset);
671 info->access = vd->access;
672 if (vd->owner) {
673 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
674 if (vd->owner == ctl)
675 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
676 info->owner = vd->owner_pid;
677 } else {
678 info->owner = -1;
679 }
680 }
681 up_read(&card->controls_rwsem);
682 return result;
683}
684
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100685static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
686 struct snd_ctl_elem_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100688 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 int result;
690
691 if (copy_from_user(&info, _info, sizeof(info)))
692 return -EFAULT;
Giuliano Pochini64649402006-03-13 14:11:11 +0100693 snd_power_lock(ctl->card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200694 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100695 if (result >= 0)
696 result = snd_ctl_elem_info(ctl, &info);
697 snd_power_unlock(ctl->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (result >= 0)
699 if (copy_to_user(_info, &info, sizeof(info)))
700 return -EFAULT;
701 return result;
702}
703
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100704int snd_ctl_elem_read(struct snd_card *card, struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100706 struct snd_kcontrol *kctl;
707 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 unsigned int index_offset;
709 int result, indirect;
710
711 down_read(&card->controls_rwsem);
712 kctl = snd_ctl_find_id(card, &control->id);
713 if (kctl == NULL) {
714 result = -ENOENT;
715 } else {
716 index_offset = snd_ctl_get_ioff(kctl, &control->id);
717 vd = &kctl->vd[index_offset];
718 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
719 if (control->indirect != indirect) {
720 result = -EACCES;
721 } else {
722 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) && kctl->get != NULL) {
723 snd_ctl_build_ioff(&control->id, kctl, index_offset);
724 result = kctl->get(kctl, control);
725 } else {
726 result = -EPERM;
727 }
728 }
729 }
730 up_read(&card->controls_rwsem);
731 return result;
732}
733
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200734EXPORT_SYMBOL(snd_ctl_elem_read);
735
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100736static int snd_ctl_elem_read_user(struct snd_card *card,
737 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100739 struct snd_ctl_elem_value *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 int result;
741
742 control = kmalloc(sizeof(*control), GFP_KERNEL);
743 if (control == NULL)
744 return -ENOMEM;
745 if (copy_from_user(control, _control, sizeof(*control))) {
746 kfree(control);
747 return -EFAULT;
748 }
Giuliano Pochini64649402006-03-13 14:11:11 +0100749 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200750 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100751 if (result >= 0)
752 result = snd_ctl_elem_read(card, control);
753 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (result >= 0)
755 if (copy_to_user(_control, control, sizeof(*control)))
756 result = -EFAULT;
757 kfree(control);
758 return result;
759}
760
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100761int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
762 struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100764 struct snd_kcontrol *kctl;
765 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 unsigned int index_offset;
767 int result, indirect;
768
769 down_read(&card->controls_rwsem);
770 kctl = snd_ctl_find_id(card, &control->id);
771 if (kctl == NULL) {
772 result = -ENOENT;
773 } else {
774 index_offset = snd_ctl_get_ioff(kctl, &control->id);
775 vd = &kctl->vd[index_offset];
776 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
777 if (control->indirect != indirect) {
778 result = -EACCES;
779 } else {
780 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
781 kctl->put == NULL ||
782 (file && vd->owner != NULL && vd->owner != file)) {
783 result = -EPERM;
784 } else {
785 snd_ctl_build_ioff(&control->id, kctl, index_offset);
786 result = kctl->put(kctl, control);
787 }
788 if (result > 0) {
789 up_read(&card->controls_rwsem);
790 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &control->id);
791 return 0;
792 }
793 }
794 }
795 up_read(&card->controls_rwsem);
796 return result;
797}
798
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200799EXPORT_SYMBOL(snd_ctl_elem_write);
800
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100801static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
802 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100804 struct snd_ctl_elem_value *control;
Giuliano Pochini64649402006-03-13 14:11:11 +0100805 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 int result;
807
808 control = kmalloc(sizeof(*control), GFP_KERNEL);
809 if (control == NULL)
810 return -ENOMEM;
811 if (copy_from_user(control, _control, sizeof(*control))) {
812 kfree(control);
813 return -EFAULT;
814 }
Giuliano Pochini64649402006-03-13 14:11:11 +0100815 card = file->card;
816 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200817 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100818 if (result >= 0)
819 result = snd_ctl_elem_write(card, file, control);
820 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (result >= 0)
822 if (copy_to_user(_control, control, sizeof(*control)))
823 result = -EFAULT;
824 kfree(control);
825 return result;
826}
827
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100828static int snd_ctl_elem_lock(struct snd_ctl_file *file,
829 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100831 struct snd_card *card = file->card;
832 struct snd_ctl_elem_id id;
833 struct snd_kcontrol *kctl;
834 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 int result;
836
837 if (copy_from_user(&id, _id, sizeof(id)))
838 return -EFAULT;
839 down_write(&card->controls_rwsem);
840 kctl = snd_ctl_find_id(card, &id);
841 if (kctl == NULL) {
842 result = -ENOENT;
843 } else {
844 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
845 if (vd->owner != NULL)
846 result = -EBUSY;
847 else {
848 vd->owner = file;
849 vd->owner_pid = current->pid;
850 result = 0;
851 }
852 }
853 up_write(&card->controls_rwsem);
854 return result;
855}
856
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100857static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
858 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100860 struct snd_card *card = file->card;
861 struct snd_ctl_elem_id id;
862 struct snd_kcontrol *kctl;
863 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 int result;
865
866 if (copy_from_user(&id, _id, sizeof(id)))
867 return -EFAULT;
868 down_write(&card->controls_rwsem);
869 kctl = snd_ctl_find_id(card, &id);
870 if (kctl == NULL) {
871 result = -ENOENT;
872 } else {
873 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
874 if (vd->owner == NULL)
875 result = -EINVAL;
876 else if (vd->owner != file)
877 result = -EPERM;
878 else {
879 vd->owner = NULL;
880 vd->owner_pid = 0;
881 result = 0;
882 }
883 }
884 up_write(&card->controls_rwsem);
885 return result;
886}
887
888struct user_element {
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100889 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 void *elem_data; /* element data */
891 unsigned long elem_data_size; /* size of element data in bytes */
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200892 void *tlv_data; /* TLV data */
893 unsigned long tlv_data_size; /* TLV data size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 void *priv_data; /* private data (like strings for enumerated type) */
895 unsigned long priv_data_size; /* size of private data in bytes */
896};
897
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100898static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
899 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 struct user_element *ue = kcontrol->private_data;
902
903 *uinfo = ue->info;
904 return 0;
905}
906
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100907static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
908 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
910 struct user_element *ue = kcontrol->private_data;
911
912 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
913 return 0;
914}
915
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100916static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
917 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 int change;
920 struct user_element *ue = kcontrol->private_data;
921
922 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
923 if (change)
924 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
925 return change;
926}
927
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200928static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol,
929 int op_flag,
930 unsigned int size,
931 unsigned int __user *tlv)
932{
933 struct user_element *ue = kcontrol->private_data;
934 int change = 0;
935 void *new_data;
936
937 if (op_flag > 0) {
938 if (size > 1024 * 128) /* sane value */
939 return -EINVAL;
940 new_data = kmalloc(size, GFP_KERNEL);
941 if (new_data == NULL)
942 return -ENOMEM;
943 if (copy_from_user(new_data, tlv, size)) {
944 kfree(new_data);
945 return -EFAULT;
946 }
947 change = ue->tlv_data_size != size;
948 if (!change)
949 change = memcmp(ue->tlv_data, new_data, size);
950 kfree(ue->tlv_data);
951 ue->tlv_data = new_data;
952 ue->tlv_data_size = size;
953 } else {
954 if (size < ue->tlv_data_size)
955 return -ENOSPC;
956 if (copy_to_user(tlv, ue->tlv_data, ue->tlv_data_size))
957 return -EFAULT;
958 }
959 return change;
960}
961
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100962static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200964 struct user_element *ue = kcontrol->private_data;
965 if (ue->tlv_data)
966 kfree(ue->tlv_data);
967 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100970static int snd_ctl_elem_add(struct snd_ctl_file *file,
971 struct snd_ctl_elem_info *info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100973 struct snd_card *card = file->card;
974 struct snd_kcontrol kctl, *_kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 unsigned int access;
976 long private_size;
977 struct user_element *ue;
978 int idx, err;
979
980 if (card->user_ctl_count >= MAX_USER_CONTROLS)
981 return -ENOMEM;
982 if (info->count > 1024)
983 return -EINVAL;
984 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100985 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200986 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
987 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 info->id.numid = 0;
989 memset(&kctl, 0, sizeof(kctl));
990 down_write(&card->controls_rwsem);
991 _kctl = snd_ctl_find_id(card, &info->id);
992 err = 0;
993 if (_kctl) {
994 if (replace)
995 err = snd_ctl_remove(card, _kctl);
996 else
997 err = -EBUSY;
998 } else {
999 if (replace)
1000 err = -ENOENT;
1001 }
1002 up_write(&card->controls_rwsem);
1003 if (err < 0)
1004 return err;
1005 memcpy(&kctl.id, &info->id, sizeof(info->id));
1006 kctl.count = info->owner ? info->owner : 1;
1007 access |= SNDRV_CTL_ELEM_ACCESS_USER;
1008 kctl.info = snd_ctl_elem_user_info;
1009 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1010 kctl.get = snd_ctl_elem_user_get;
1011 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1012 kctl.put = snd_ctl_elem_user_put;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001013 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
1014 kctl.tlv.c = snd_ctl_elem_user_tlv;
1015 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 switch (info->type) {
1018 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
1019 private_size = sizeof(char);
1020 if (info->count > 128)
1021 return -EINVAL;
1022 break;
1023 case SNDRV_CTL_ELEM_TYPE_INTEGER:
1024 private_size = sizeof(long);
1025 if (info->count > 128)
1026 return -EINVAL;
1027 break;
1028 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
1029 private_size = sizeof(long long);
1030 if (info->count > 64)
1031 return -EINVAL;
1032 break;
1033 case SNDRV_CTL_ELEM_TYPE_BYTES:
1034 private_size = sizeof(unsigned char);
1035 if (info->count > 512)
1036 return -EINVAL;
1037 break;
1038 case SNDRV_CTL_ELEM_TYPE_IEC958:
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001039 private_size = sizeof(struct snd_aes_iec958);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (info->count != 1)
1041 return -EINVAL;
1042 break;
1043 default:
1044 return -EINVAL;
1045 }
1046 private_size *= info->count;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001047 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (ue == NULL)
1049 return -ENOMEM;
1050 ue->info = *info;
Takashi Iwai86148e82006-08-24 12:36:36 +02001051 ue->info.access = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 ue->elem_data = (char *)ue + sizeof(*ue);
1053 ue->elem_data_size = private_size;
1054 kctl.private_free = snd_ctl_elem_user_free;
1055 _kctl = snd_ctl_new(&kctl, access);
1056 if (_kctl == NULL) {
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001057 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return -ENOMEM;
1059 }
1060 _kctl->private_data = ue;
1061 for (idx = 0; idx < _kctl->count; idx++)
1062 _kctl->vd[idx].owner = file;
1063 err = snd_ctl_add(card, _kctl);
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001064 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 down_write(&card->controls_rwsem);
1068 card->user_ctl_count++;
1069 up_write(&card->controls_rwsem);
1070
1071 return 0;
1072}
1073
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001074static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1075 struct snd_ctl_elem_info __user *_info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001077 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (copy_from_user(&info, _info, sizeof(info)))
1079 return -EFAULT;
1080 return snd_ctl_elem_add(file, &info, replace);
1081}
1082
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001083static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1084 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001086 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 int err;
1088
1089 if (copy_from_user(&id, _id, sizeof(id)))
1090 return -EFAULT;
1091 err = snd_ctl_remove_unlocked_id(file, &id);
1092 if (! err) {
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001093 struct snd_card *card = file->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 down_write(&card->controls_rwsem);
1095 card->user_ctl_count--;
1096 up_write(&card->controls_rwsem);
1097 }
1098 return err;
1099}
1100
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001101static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 int subscribe;
1104 if (get_user(subscribe, ptr))
1105 return -EFAULT;
1106 if (subscribe < 0) {
1107 subscribe = file->subscribed;
1108 if (put_user(subscribe, ptr))
1109 return -EFAULT;
1110 return 0;
1111 }
1112 if (subscribe) {
1113 file->subscribed = 1;
1114 return 0;
1115 } else if (file->subscribed) {
1116 snd_ctl_empty_read_queue(file);
1117 file->subscribed = 0;
1118 }
1119 return 0;
1120}
1121
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001122static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1123 struct snd_ctl_tlv __user *_tlv,
1124 int op_flag)
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001125{
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001126 struct snd_card *card = file->card;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001127 struct snd_ctl_tlv tlv;
1128 struct snd_kcontrol *kctl;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001129 struct snd_kcontrol_volatile *vd;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001130 unsigned int len;
1131 int err = 0;
1132
1133 if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
1134 return -EFAULT;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001135 if (tlv.length < sizeof(unsigned int) * 3)
1136 return -EINVAL;
1137 down_read(&card->controls_rwsem);
1138 kctl = snd_ctl_find_numid(card, tlv.numid);
1139 if (kctl == NULL) {
1140 err = -ENOENT;
1141 goto __kctl_end;
1142 }
1143 if (kctl->tlv.p == NULL) {
1144 err = -ENXIO;
1145 goto __kctl_end;
1146 }
1147 vd = &kctl->vd[tlv.numid - kctl->id.numid];
1148 if ((op_flag == 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) == 0) ||
1149 (op_flag > 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) == 0) ||
1150 (op_flag < 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND) == 0)) {
1151 err = -ENXIO;
1152 goto __kctl_end;
1153 }
1154 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1155 if (file && vd->owner != NULL && vd->owner != file) {
1156 err = -EPERM;
1157 goto __kctl_end;
1158 }
1159 err = kctl->tlv.c(kctl, op_flag, tlv.length, _tlv->tlv);
1160 if (err > 0) {
1161 up_read(&card->controls_rwsem);
1162 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &kctl->id);
1163 return 0;
1164 }
1165 } else {
1166 if (op_flag) {
1167 err = -ENXIO;
1168 goto __kctl_end;
1169 }
1170 len = kctl->tlv.p[1] + 2 * sizeof(unsigned int);
1171 if (tlv.length < len) {
1172 err = -ENOMEM;
1173 goto __kctl_end;
1174 }
1175 if (copy_to_user(_tlv->tlv, kctl->tlv.p, len))
1176 err = -EFAULT;
1177 }
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001178 __kctl_end:
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001179 up_read(&card->controls_rwsem);
1180 return err;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001181}
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1184{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001185 struct snd_ctl_file *ctl;
1186 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001188 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 void __user *argp = (void __user *)arg;
1190 int __user *ip = argp;
1191 int err;
1192
1193 ctl = file->private_data;
1194 card = ctl->card;
1195 snd_assert(card != NULL, return -ENXIO);
1196 switch (cmd) {
1197 case SNDRV_CTL_IOCTL_PVERSION:
1198 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1199 case SNDRV_CTL_IOCTL_CARD_INFO:
1200 return snd_ctl_card_info(card, ctl, cmd, argp);
1201 case SNDRV_CTL_IOCTL_ELEM_LIST:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001202 return snd_ctl_elem_list(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 case SNDRV_CTL_IOCTL_ELEM_INFO:
1204 return snd_ctl_elem_info_user(ctl, argp);
1205 case SNDRV_CTL_IOCTL_ELEM_READ:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001206 return snd_ctl_elem_read_user(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1208 return snd_ctl_elem_write_user(ctl, argp);
1209 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1210 return snd_ctl_elem_lock(ctl, argp);
1211 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1212 return snd_ctl_elem_unlock(ctl, argp);
1213 case SNDRV_CTL_IOCTL_ELEM_ADD:
1214 return snd_ctl_elem_add_user(ctl, argp, 0);
1215 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1216 return snd_ctl_elem_add_user(ctl, argp, 1);
1217 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1218 return snd_ctl_elem_remove(ctl, argp);
1219 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1220 return snd_ctl_subscribe_events(ctl, ip);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001221 case SNDRV_CTL_IOCTL_TLV_READ:
1222 return snd_ctl_tlv_ioctl(ctl, argp, 0);
1223 case SNDRV_CTL_IOCTL_TLV_WRITE:
1224 return snd_ctl_tlv_ioctl(ctl, argp, 1);
1225 case SNDRV_CTL_IOCTL_TLV_COMMAND:
1226 return snd_ctl_tlv_ioctl(ctl, argp, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 case SNDRV_CTL_IOCTL_POWER:
Takashi Iwaia381a7a2005-11-17 15:55:49 +01001228 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 case SNDRV_CTL_IOCTL_POWER_STATE:
1230#ifdef CONFIG_PM
1231 return put_user(card->power_state, ip) ? -EFAULT : 0;
1232#else
1233 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1234#endif
1235 }
1236 down_read(&snd_ioctl_rwsem);
1237 list_for_each(list, &snd_control_ioctls) {
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001238 p = list_entry(list, struct snd_kctl_ioctl, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 err = p->fioctl(card, ctl, cmd, arg);
1240 if (err != -ENOIOCTLCMD) {
1241 up_read(&snd_ioctl_rwsem);
1242 return err;
1243 }
1244 }
1245 up_read(&snd_ioctl_rwsem);
Takashi Iwai6d85be62005-05-15 14:32:50 +02001246 snd_printdd("unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return -ENOTTY;
1248}
1249
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001250static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1251 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001253 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 int err = 0;
1255 ssize_t result = 0;
1256
1257 ctl = file->private_data;
1258 snd_assert(ctl != NULL && ctl->card != NULL, return -ENXIO);
1259 if (!ctl->subscribed)
1260 return -EBADFD;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001261 if (count < sizeof(struct snd_ctl_event))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 return -EINVAL;
1263 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001264 while (count >= sizeof(struct snd_ctl_event)) {
1265 struct snd_ctl_event ev;
1266 struct snd_kctl_event *kev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 while (list_empty(&ctl->events)) {
1268 wait_queue_t wait;
1269 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1270 err = -EAGAIN;
1271 goto __end_lock;
1272 }
1273 init_waitqueue_entry(&wait, current);
1274 add_wait_queue(&ctl->change_sleep, &wait);
1275 set_current_state(TASK_INTERRUPTIBLE);
1276 spin_unlock_irq(&ctl->read_lock);
1277 schedule();
1278 remove_wait_queue(&ctl->change_sleep, &wait);
1279 if (signal_pending(current))
1280 return result > 0 ? result : -ERESTARTSYS;
1281 spin_lock_irq(&ctl->read_lock);
1282 }
1283 kev = snd_kctl_event(ctl->events.next);
1284 ev.type = SNDRV_CTL_EVENT_ELEM;
1285 ev.data.elem.mask = kev->mask;
1286 ev.data.elem.id = kev->id;
1287 list_del(&kev->list);
1288 spin_unlock_irq(&ctl->read_lock);
1289 kfree(kev);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001290 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 err = -EFAULT;
1292 goto __end;
1293 }
1294 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001295 buffer += sizeof(struct snd_ctl_event);
1296 count -= sizeof(struct snd_ctl_event);
1297 result += sizeof(struct snd_ctl_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
1299 __end_lock:
1300 spin_unlock_irq(&ctl->read_lock);
1301 __end:
1302 return result > 0 ? result : err;
1303}
1304
1305static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1306{
1307 unsigned int mask;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001308 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 ctl = file->private_data;
1311 if (!ctl->subscribed)
1312 return 0;
1313 poll_wait(file, &ctl->change_sleep, wait);
1314
1315 mask = 0;
1316 if (!list_empty(&ctl->events))
1317 mask |= POLLIN | POLLRDNORM;
1318
1319 return mask;
1320}
1321
1322/*
1323 * register the device-specific control-ioctls.
1324 * called from each device manager like pcm.c, hwdep.c, etc.
1325 */
1326static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1327{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001328 struct snd_kctl_ioctl *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001330 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (pn == NULL)
1332 return -ENOMEM;
1333 pn->fioctl = fcn;
1334 down_write(&snd_ioctl_rwsem);
1335 list_add_tail(&pn->list, lists);
1336 up_write(&snd_ioctl_rwsem);
1337 return 0;
1338}
1339
1340int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1341{
1342 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1343}
1344
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001345EXPORT_SYMBOL(snd_ctl_register_ioctl);
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347#ifdef CONFIG_COMPAT
1348int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1349{
1350 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1351}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001352
1353EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354#endif
1355
1356/*
1357 * de-register the device-specific control-ioctls.
1358 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001359static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1360 struct list_head *lists)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001363 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Takashi Iwai7c22f1a2005-10-10 11:46:31 +02001365 snd_assert(fcn != NULL, return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 down_write(&snd_ioctl_rwsem);
1367 list_for_each(list, lists) {
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001368 p = list_entry(list, struct snd_kctl_ioctl, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (p->fioctl == fcn) {
1370 list_del(&p->list);
1371 up_write(&snd_ioctl_rwsem);
1372 kfree(p);
1373 return 0;
1374 }
1375 }
1376 up_write(&snd_ioctl_rwsem);
1377 snd_BUG();
1378 return -EINVAL;
1379}
1380
1381int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1382{
1383 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1384}
1385
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001386EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388#ifdef CONFIG_COMPAT
1389int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1390{
1391 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1392}
1393
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001394EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395#endif
1396
1397static int snd_ctl_fasync(int fd, struct file * file, int on)
1398{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001399 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 int err;
1401 ctl = file->private_data;
1402 err = fasync_helper(fd, file, on, &ctl->fasync);
1403 if (err < 0)
1404 return err;
1405 return 0;
1406}
1407
1408/*
1409 * ioctl32 compat
1410 */
1411#ifdef CONFIG_COMPAT
1412#include "control_compat.c"
1413#else
1414#define snd_ctl_ioctl_compat NULL
1415#endif
1416
1417/*
1418 * INIT PART
1419 */
1420
1421static struct file_operations snd_ctl_f_ops =
1422{
1423 .owner = THIS_MODULE,
1424 .read = snd_ctl_read,
1425 .open = snd_ctl_open,
1426 .release = snd_ctl_release,
1427 .poll = snd_ctl_poll,
1428 .unlocked_ioctl = snd_ctl_ioctl,
1429 .compat_ioctl = snd_ctl_ioctl_compat,
1430 .fasync = snd_ctl_fasync,
1431};
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433/*
1434 * registration of the control device
1435 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001436static int snd_ctl_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001438 struct snd_card *card = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 int err, cardnum;
1440 char name[16];
1441
1442 snd_assert(card != NULL, return -ENXIO);
1443 cardnum = card->number;
1444 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1445 sprintf(name, "controlC%i", cardnum);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001446 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1447 &snd_ctl_f_ops, card, name)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return err;
1449 return 0;
1450}
1451
1452/*
1453 * disconnection of the control device
1454 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001455static int snd_ctl_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001457 struct snd_card *card = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 struct list_head *flist;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001459 struct snd_ctl_file *ctl;
Takashi Iwaic4614822006-06-23 14:38:23 +02001460 int err, cardnum;
1461
1462 snd_assert(card != NULL, return -ENXIO);
1463 cardnum = card->number;
1464 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 down_read(&card->controls_rwsem);
1467 list_for_each(flist, &card->ctl_files) {
1468 ctl = snd_ctl_file(flist);
1469 wake_up(&ctl->change_sleep);
1470 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1471 }
1472 up_read(&card->controls_rwsem);
Takashi Iwaic4614822006-06-23 14:38:23 +02001473
1474 if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
1475 card, -1)) < 0)
1476 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return 0;
1478}
1479
1480/*
1481 * free all controls
1482 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001483static int snd_ctl_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001485 struct snd_card *card = device->device_data;
1486 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 down_write(&card->controls_rwsem);
1489 while (!list_empty(&card->controls)) {
1490 control = snd_kcontrol(card->controls.next);
1491 snd_ctl_remove(card, control);
1492 }
1493 up_write(&card->controls_rwsem);
1494 return 0;
1495}
1496
1497/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 * create control core:
1499 * called from init.c
1500 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001501int snd_ctl_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001503 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 .dev_free = snd_ctl_dev_free,
1505 .dev_register = snd_ctl_dev_register,
1506 .dev_disconnect = snd_ctl_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 };
1508
1509 snd_assert(card != NULL, return -ENXIO);
1510 return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
1511}