blob: 31ad58154c06c8959ed19079237543a2d4f55b0c [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;
78 ctl->pid = current->pid;
79 file->private_data = ctl;
80 write_lock_irqsave(&card->ctl_files_rwlock, flags);
81 list_add_tail(&ctl->list, &card->ctl_files);
82 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
83 return 0;
84
85 __error:
86 module_put(card->module);
87 __error2:
88 snd_card_file_remove(card, file);
89 __error1:
90 return err;
91}
92
Takashi Iwai82e9bae2005-11-17 13:53:23 +010093static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Takashi Iwai82e9bae2005-11-17 13:53:23 +010095 struct snd_kctl_event *cread;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 spin_lock(&ctl->read_lock);
98 while (!list_empty(&ctl->events)) {
99 cread = snd_kctl_event(ctl->events.next);
100 list_del(&cread->list);
101 kfree(cread);
102 }
103 spin_unlock(&ctl->read_lock);
104}
105
106static int snd_ctl_release(struct inode *inode, struct file *file)
107{
108 unsigned long flags;
109 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100110 struct snd_card *card;
111 struct snd_ctl_file *ctl;
112 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 unsigned int idx;
114
115 ctl = file->private_data;
116 fasync_helper(-1, file, 0, &ctl->fasync);
117 file->private_data = NULL;
118 card = ctl->card;
119 write_lock_irqsave(&card->ctl_files_rwlock, flags);
120 list_del(&ctl->list);
121 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
122 down_write(&card->controls_rwsem);
123 list_for_each(list, &card->controls) {
124 control = snd_kcontrol(list);
125 for (idx = 0; idx < control->count; idx++)
126 if (control->vd[idx].owner == ctl)
127 control->vd[idx].owner = NULL;
128 }
129 up_write(&card->controls_rwsem);
130 snd_ctl_empty_read_queue(ctl);
131 kfree(ctl);
132 module_put(card->module);
133 snd_card_file_remove(card, file);
134 return 0;
135}
136
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100137void snd_ctl_notify(struct snd_card *card, unsigned int mask,
138 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 unsigned long flags;
141 struct list_head *flist;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100142 struct snd_ctl_file *ctl;
143 struct snd_kctl_event *ev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200145 snd_assert(card != NULL && id != NULL, return);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 read_lock(&card->ctl_files_rwlock);
147#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
148 card->mixer_oss_change_count++;
149#endif
150 list_for_each(flist, &card->ctl_files) {
151 struct list_head *elist;
152 ctl = snd_ctl_file(flist);
153 if (!ctl->subscribed)
154 continue;
155 spin_lock_irqsave(&ctl->read_lock, flags);
156 list_for_each(elist, &ctl->events) {
157 ev = snd_kctl_event(elist);
158 if (ev->id.numid == id->numid) {
159 ev->mask |= mask;
160 goto _found;
161 }
162 }
Takashi Iwaica2c0962005-09-09 14:20:23 +0200163 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (ev) {
165 ev->id = *id;
166 ev->mask = mask;
167 list_add_tail(&ev->list, &ctl->events);
168 } else {
169 snd_printk(KERN_ERR "No memory available to allocate event\n");
170 }
171 _found:
172 wake_up(&ctl->change_sleep);
173 spin_unlock_irqrestore(&ctl->read_lock, flags);
174 kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
175 }
176 read_unlock(&card->ctl_files_rwlock);
177}
178
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200179EXPORT_SYMBOL(snd_ctl_notify);
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/**
182 * snd_ctl_new - create a control instance from the template
183 * @control: the control template
184 * @access: the default control access
185 *
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100186 * Allocates a new struct snd_kcontrol instance and copies the given template
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * to the new instance. It does not copy volatile data (access).
188 *
189 * Returns the pointer of the new instance, or NULL on failure.
190 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100191struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control, unsigned int access)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100193 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 unsigned int idx;
195
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200196 snd_assert(control != NULL, return NULL);
197 snd_assert(control->count > 0, return NULL);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100198 kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100199 if (kctl == NULL) {
200 snd_printk(KERN_ERR "Cannot allocate control instance\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return NULL;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 *kctl = *control;
204 for (idx = 0; idx < kctl->count; idx++)
205 kctl->vd[idx].access = access;
206 return kctl;
207}
208
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200209EXPORT_SYMBOL(snd_ctl_new);
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/**
212 * snd_ctl_new1 - create a control instance from the template
213 * @ncontrol: the initialization record
214 * @private_data: the private data to set
215 *
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100216 * Allocates a new struct snd_kcontrol instance and initialize from the given
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 * template. When the access field of ncontrol is 0, it's assumed as
218 * READWRITE access. When the count field is 0, it's assumes as one.
219 *
220 * Returns the pointer of the newly generated instance, or NULL on failure.
221 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100222struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
223 void *private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100225 struct snd_kcontrol kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 unsigned int access;
227
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200228 snd_assert(ncontrol != NULL, return NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 snd_assert(ncontrol->info != NULL, return NULL);
230 memset(&kctl, 0, sizeof(kctl));
231 kctl.id.iface = ncontrol->iface;
232 kctl.id.device = ncontrol->device;
233 kctl.id.subdevice = ncontrol->subdevice;
234 if (ncontrol->name)
235 strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
236 kctl.id.index = ncontrol->index;
237 kctl.count = ncontrol->count ? ncontrol->count : 1;
238 access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200239 (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
240 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
241 SNDRV_CTL_ELEM_ACCESS_DINDIRECT|
242 SNDRV_CTL_ELEM_ACCESS_INDIRECT|
243 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE|
244 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 kctl.info = ncontrol->info;
246 kctl.get = ncontrol->get;
247 kctl.put = ncontrol->put;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200248 kctl.tlv.p = ncontrol->tlv.p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 kctl.private_value = ncontrol->private_value;
250 kctl.private_data = private_data;
251 return snd_ctl_new(&kctl, access);
252}
253
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200254EXPORT_SYMBOL(snd_ctl_new1);
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/**
257 * snd_ctl_free_one - release the control instance
258 * @kcontrol: the control instance
259 *
260 * Releases the control instance created via snd_ctl_new()
261 * or snd_ctl_new1().
262 * Don't call this after the control was added to the card.
263 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100264void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 if (kcontrol) {
267 if (kcontrol->private_free)
268 kcontrol->private_free(kcontrol);
269 kfree(kcontrol);
270 }
271}
272
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200273EXPORT_SYMBOL(snd_ctl_free_one);
274
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100275static unsigned int snd_ctl_hole_check(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 unsigned int count)
277{
278 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100279 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 list_for_each(list, &card->controls) {
282 kctl = snd_kcontrol(list);
283 if ((kctl->id.numid <= card->last_numid &&
284 kctl->id.numid + kctl->count > card->last_numid) ||
285 (kctl->id.numid <= card->last_numid + count - 1 &&
286 kctl->id.numid + kctl->count > card->last_numid + count - 1))
287 return card->last_numid = kctl->id.numid + kctl->count - 1;
288 }
289 return card->last_numid;
290}
291
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100292static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 unsigned int last_numid, iter = 100000;
295
296 last_numid = card->last_numid;
297 while (last_numid != snd_ctl_hole_check(card, count)) {
298 if (--iter == 0) {
299 /* this situation is very unlikely */
300 snd_printk(KERN_ERR "unable to allocate new control numid\n");
301 return -ENOMEM;
302 }
303 last_numid = card->last_numid;
304 }
305 return 0;
306}
307
308/**
309 * snd_ctl_add - add the control instance to the card
310 * @card: the card instance
311 * @kcontrol: the control instance to add
312 *
313 * Adds the control instance created via snd_ctl_new() or
314 * snd_ctl_new1() to the given card. Assigns also an unique
315 * numid used for fast search.
316 *
317 * Returns zero if successful, or a negative error code on failure.
318 *
319 * It frees automatically the control which cannot be added.
320 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100321int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100323 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 unsigned int idx;
Takashi Iwaic6077b32006-03-21 16:07:13 +0100325 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100327 if (! kcontrol)
Takashi Iwaic6077b32006-03-21 16:07:13 +0100328 return err;
329 snd_assert(card != NULL, goto error);
330 snd_assert(kcontrol->info != NULL, goto error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 id = kcontrol->id;
332 down_write(&card->controls_rwsem);
333 if (snd_ctl_find_id(card, &id)) {
334 up_write(&card->controls_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
336 id.iface,
337 id.device,
338 id.subdevice,
339 id.name,
340 id.index);
Takashi Iwaic6077b32006-03-21 16:07:13 +0100341 err = -EBUSY;
342 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
345 up_write(&card->controls_rwsem);
Takashi Iwaic6077b32006-03-21 16:07:13 +0100346 err = -ENOMEM;
347 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349 list_add_tail(&kcontrol->list, &card->controls);
350 card->controls_count += kcontrol->count;
351 kcontrol->id.numid = card->last_numid + 1;
352 card->last_numid += kcontrol->count;
353 up_write(&card->controls_rwsem);
354 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
355 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
356 return 0;
Takashi Iwaic6077b32006-03-21 16:07:13 +0100357
358 error:
359 snd_ctl_free_one(kcontrol);
360 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200363EXPORT_SYMBOL(snd_ctl_add);
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365/**
366 * snd_ctl_remove - remove the control from the card and release it
367 * @card: the card instance
368 * @kcontrol: the control instance to remove
369 *
370 * Removes the control from the card and then releases the instance.
371 * You don't need to call snd_ctl_free_one(). You must be in
372 * the write lock - down_write(&card->controls_rwsem).
373 *
374 * Returns 0 if successful, or a negative error code on failure.
375 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100376int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100378 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 unsigned int idx;
380
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200381 snd_assert(card != NULL && kcontrol != NULL, return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 list_del(&kcontrol->list);
383 card->controls_count -= kcontrol->count;
384 id = kcontrol->id;
385 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
386 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
387 snd_ctl_free_one(kcontrol);
388 return 0;
389}
390
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200391EXPORT_SYMBOL(snd_ctl_remove);
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393/**
394 * snd_ctl_remove_id - remove the control of the given id and release it
395 * @card: the card instance
396 * @id: the control id to remove
397 *
398 * Finds the control instance with the given id, removes it from the
399 * card list and releases it.
400 *
401 * Returns 0 if successful, or a negative error code on failure.
402 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100403int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100405 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 int ret;
407
408 down_write(&card->controls_rwsem);
409 kctl = snd_ctl_find_id(card, id);
410 if (kctl == NULL) {
411 up_write(&card->controls_rwsem);
412 return -ENOENT;
413 }
414 ret = snd_ctl_remove(card, kctl);
415 up_write(&card->controls_rwsem);
416 return ret;
417}
418
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200419EXPORT_SYMBOL(snd_ctl_remove_id);
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421/**
422 * snd_ctl_remove_unlocked_id - remove the unlocked control of the given id and release it
423 * @file: active control handle
424 * @id: the control id to remove
425 *
426 * Finds the control instance with the given id, removes it from the
427 * card list and releases it.
428 *
429 * Returns 0 if successful, or a negative error code on failure.
430 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100431static int snd_ctl_remove_unlocked_id(struct snd_ctl_file * file,
432 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100434 struct snd_card *card = file->card;
435 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 int idx, ret;
437
438 down_write(&card->controls_rwsem);
439 kctl = snd_ctl_find_id(card, id);
440 if (kctl == NULL) {
441 up_write(&card->controls_rwsem);
442 return -ENOENT;
443 }
444 for (idx = 0; idx < kctl->count; idx++)
445 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
446 up_write(&card->controls_rwsem);
447 return -EBUSY;
448 }
449 ret = snd_ctl_remove(card, kctl);
450 up_write(&card->controls_rwsem);
451 return ret;
452}
453
454/**
455 * snd_ctl_rename_id - replace the id of a control on the card
456 * @card: the card instance
457 * @src_id: the old id
458 * @dst_id: the new id
459 *
460 * Finds the control with the old id from the card, and replaces the
461 * id with the new one.
462 *
463 * Returns zero if successful, or a negative error code on failure.
464 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100465int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
466 struct snd_ctl_elem_id *dst_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100468 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 down_write(&card->controls_rwsem);
471 kctl = snd_ctl_find_id(card, src_id);
472 if (kctl == NULL) {
473 up_write(&card->controls_rwsem);
474 return -ENOENT;
475 }
476 kctl->id = *dst_id;
477 kctl->id.numid = card->last_numid + 1;
478 card->last_numid += kctl->count;
479 up_write(&card->controls_rwsem);
480 return 0;
481}
482
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200483EXPORT_SYMBOL(snd_ctl_rename_id);
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485/**
486 * snd_ctl_find_numid - find the control instance with the given number-id
487 * @card: the card instance
488 * @numid: the number-id to search
489 *
490 * Finds the control instance with the given number-id from the card.
491 *
492 * Returns the pointer of the instance if found, or NULL if not.
493 *
494 * The caller must down card->controls_rwsem before calling this function
495 * (if the race condition can happen).
496 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100497struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100500 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200502 snd_assert(card != NULL && numid != 0, return NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 list_for_each(list, &card->controls) {
504 kctl = snd_kcontrol(list);
505 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
506 return kctl;
507 }
508 return NULL;
509}
510
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200511EXPORT_SYMBOL(snd_ctl_find_numid);
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513/**
514 * snd_ctl_find_id - find the control instance with the given id
515 * @card: the card instance
516 * @id: the id to search
517 *
518 * Finds the control instance with the given id from the card.
519 *
520 * Returns the pointer of the instance if found, or NULL if not.
521 *
522 * The caller must down card->controls_rwsem before calling this function
523 * (if the race condition can happen).
524 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100525struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
526 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100529 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200531 snd_assert(card != NULL && id != NULL, return NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (id->numid != 0)
533 return snd_ctl_find_numid(card, id->numid);
534 list_for_each(list, &card->controls) {
535 kctl = snd_kcontrol(list);
536 if (kctl->id.iface != id->iface)
537 continue;
538 if (kctl->id.device != id->device)
539 continue;
540 if (kctl->id.subdevice != id->subdevice)
541 continue;
542 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
543 continue;
544 if (kctl->id.index > id->index)
545 continue;
546 if (kctl->id.index + kctl->count <= id->index)
547 continue;
548 return kctl;
549 }
550 return NULL;
551}
552
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200553EXPORT_SYMBOL(snd_ctl_find_id);
554
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100555static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 unsigned int cmd, void __user *arg)
557{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100558 struct snd_ctl_card_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Takashi Iwaica2c0962005-09-09 14:20:23 +0200560 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (! info)
562 return -ENOMEM;
563 down_read(&snd_ioctl_rwsem);
564 info->card = card->number;
565 strlcpy(info->id, card->id, sizeof(info->id));
566 strlcpy(info->driver, card->driver, sizeof(info->driver));
567 strlcpy(info->name, card->shortname, sizeof(info->name));
568 strlcpy(info->longname, card->longname, sizeof(info->longname));
569 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
570 strlcpy(info->components, card->components, sizeof(info->components));
571 up_read(&snd_ioctl_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100572 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 kfree(info);
574 return -EFAULT;
575 }
576 kfree(info);
577 return 0;
578}
579
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100580static int snd_ctl_elem_list(struct snd_card *card,
581 struct snd_ctl_elem_list __user *_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 struct list_head *plist;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100584 struct snd_ctl_elem_list list;
585 struct snd_kcontrol *kctl;
586 struct snd_ctl_elem_id *dst, *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 unsigned int offset, space, first, jidx;
588
589 if (copy_from_user(&list, _list, sizeof(list)))
590 return -EFAULT;
591 offset = list.offset;
592 space = list.space;
593 first = 0;
594 /* try limit maximum space */
595 if (space > 16384)
596 return -ENOMEM;
597 if (space > 0) {
598 /* allocate temporary buffer for atomic operation */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100599 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (dst == NULL)
601 return -ENOMEM;
602 down_read(&card->controls_rwsem);
603 list.count = card->controls_count;
604 plist = card->controls.next;
605 while (plist != &card->controls) {
606 if (offset == 0)
607 break;
608 kctl = snd_kcontrol(plist);
609 if (offset < kctl->count)
610 break;
611 offset -= kctl->count;
612 plist = plist->next;
613 }
614 list.used = 0;
615 id = dst;
616 while (space > 0 && plist != &card->controls) {
617 kctl = snd_kcontrol(plist);
618 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
619 snd_ctl_build_ioff(id, kctl, jidx);
620 id++;
621 space--;
622 list.used++;
623 }
624 plist = plist->next;
625 offset = 0;
626 }
627 up_read(&card->controls_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100628 if (list.used > 0 &&
629 copy_to_user(list.pids, dst,
630 list.used * sizeof(struct snd_ctl_elem_id))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 vfree(dst);
632 return -EFAULT;
633 }
634 vfree(dst);
635 } else {
636 down_read(&card->controls_rwsem);
637 list.count = card->controls_count;
638 up_read(&card->controls_rwsem);
639 }
640 if (copy_to_user(_list, &list, sizeof(list)))
641 return -EFAULT;
642 return 0;
643}
644
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100645static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
646 struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100648 struct snd_card *card = ctl->card;
649 struct snd_kcontrol *kctl;
650 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 unsigned int index_offset;
652 int result;
653
654 down_read(&card->controls_rwsem);
655 kctl = snd_ctl_find_id(card, &info->id);
656 if (kctl == NULL) {
657 up_read(&card->controls_rwsem);
658 return -ENOENT;
659 }
660#ifdef CONFIG_SND_DEBUG
661 info->access = 0;
662#endif
663 result = kctl->info(kctl, info);
664 if (result >= 0) {
665 snd_assert(info->access == 0, );
666 index_offset = snd_ctl_get_ioff(kctl, &info->id);
667 vd = &kctl->vd[index_offset];
668 snd_ctl_build_ioff(&info->id, kctl, index_offset);
669 info->access = vd->access;
670 if (vd->owner) {
671 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
672 if (vd->owner == ctl)
673 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
674 info->owner = vd->owner_pid;
675 } else {
676 info->owner = -1;
677 }
678 }
679 up_read(&card->controls_rwsem);
680 return result;
681}
682
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100683static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
684 struct snd_ctl_elem_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100686 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 int result;
688
689 if (copy_from_user(&info, _info, sizeof(info)))
690 return -EFAULT;
Giuliano Pochini64649402006-03-13 14:11:11 +0100691 snd_power_lock(ctl->card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200692 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100693 if (result >= 0)
694 result = snd_ctl_elem_info(ctl, &info);
695 snd_power_unlock(ctl->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (result >= 0)
697 if (copy_to_user(_info, &info, sizeof(info)))
698 return -EFAULT;
699 return result;
700}
701
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100702int snd_ctl_elem_read(struct snd_card *card, struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100704 struct snd_kcontrol *kctl;
705 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 unsigned int index_offset;
707 int result, indirect;
708
709 down_read(&card->controls_rwsem);
710 kctl = snd_ctl_find_id(card, &control->id);
711 if (kctl == NULL) {
712 result = -ENOENT;
713 } else {
714 index_offset = snd_ctl_get_ioff(kctl, &control->id);
715 vd = &kctl->vd[index_offset];
716 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
717 if (control->indirect != indirect) {
718 result = -EACCES;
719 } else {
720 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) && kctl->get != NULL) {
721 snd_ctl_build_ioff(&control->id, kctl, index_offset);
722 result = kctl->get(kctl, control);
723 } else {
724 result = -EPERM;
725 }
726 }
727 }
728 up_read(&card->controls_rwsem);
729 return result;
730}
731
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200732EXPORT_SYMBOL(snd_ctl_elem_read);
733
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100734static int snd_ctl_elem_read_user(struct snd_card *card,
735 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100737 struct snd_ctl_elem_value *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 int result;
739
740 control = kmalloc(sizeof(*control), GFP_KERNEL);
741 if (control == NULL)
742 return -ENOMEM;
743 if (copy_from_user(control, _control, sizeof(*control))) {
744 kfree(control);
745 return -EFAULT;
746 }
Giuliano Pochini64649402006-03-13 14:11:11 +0100747 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200748 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100749 if (result >= 0)
750 result = snd_ctl_elem_read(card, control);
751 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (result >= 0)
753 if (copy_to_user(_control, control, sizeof(*control)))
754 result = -EFAULT;
755 kfree(control);
756 return result;
757}
758
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100759int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
760 struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100762 struct snd_kcontrol *kctl;
763 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 unsigned int index_offset;
765 int result, indirect;
766
767 down_read(&card->controls_rwsem);
768 kctl = snd_ctl_find_id(card, &control->id);
769 if (kctl == NULL) {
770 result = -ENOENT;
771 } else {
772 index_offset = snd_ctl_get_ioff(kctl, &control->id);
773 vd = &kctl->vd[index_offset];
774 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
775 if (control->indirect != indirect) {
776 result = -EACCES;
777 } else {
778 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
779 kctl->put == NULL ||
780 (file && vd->owner != NULL && vd->owner != file)) {
781 result = -EPERM;
782 } else {
783 snd_ctl_build_ioff(&control->id, kctl, index_offset);
784 result = kctl->put(kctl, control);
785 }
786 if (result > 0) {
787 up_read(&card->controls_rwsem);
788 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &control->id);
789 return 0;
790 }
791 }
792 }
793 up_read(&card->controls_rwsem);
794 return result;
795}
796
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200797EXPORT_SYMBOL(snd_ctl_elem_write);
798
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100799static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
800 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100802 struct snd_ctl_elem_value *control;
Giuliano Pochini64649402006-03-13 14:11:11 +0100803 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 int result;
805
806 control = kmalloc(sizeof(*control), GFP_KERNEL);
807 if (control == NULL)
808 return -ENOMEM;
809 if (copy_from_user(control, _control, sizeof(*control))) {
810 kfree(control);
811 return -EFAULT;
812 }
Giuliano Pochini64649402006-03-13 14:11:11 +0100813 card = file->card;
814 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200815 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100816 if (result >= 0)
817 result = snd_ctl_elem_write(card, file, control);
818 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (result >= 0)
820 if (copy_to_user(_control, control, sizeof(*control)))
821 result = -EFAULT;
822 kfree(control);
823 return result;
824}
825
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100826static int snd_ctl_elem_lock(struct snd_ctl_file *file,
827 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100829 struct snd_card *card = file->card;
830 struct snd_ctl_elem_id id;
831 struct snd_kcontrol *kctl;
832 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 int result;
834
835 if (copy_from_user(&id, _id, sizeof(id)))
836 return -EFAULT;
837 down_write(&card->controls_rwsem);
838 kctl = snd_ctl_find_id(card, &id);
839 if (kctl == NULL) {
840 result = -ENOENT;
841 } else {
842 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
843 if (vd->owner != NULL)
844 result = -EBUSY;
845 else {
846 vd->owner = file;
847 vd->owner_pid = current->pid;
848 result = 0;
849 }
850 }
851 up_write(&card->controls_rwsem);
852 return result;
853}
854
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100855static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
856 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100858 struct snd_card *card = file->card;
859 struct snd_ctl_elem_id id;
860 struct snd_kcontrol *kctl;
861 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 int result;
863
864 if (copy_from_user(&id, _id, sizeof(id)))
865 return -EFAULT;
866 down_write(&card->controls_rwsem);
867 kctl = snd_ctl_find_id(card, &id);
868 if (kctl == NULL) {
869 result = -ENOENT;
870 } else {
871 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
872 if (vd->owner == NULL)
873 result = -EINVAL;
874 else if (vd->owner != file)
875 result = -EPERM;
876 else {
877 vd->owner = NULL;
878 vd->owner_pid = 0;
879 result = 0;
880 }
881 }
882 up_write(&card->controls_rwsem);
883 return result;
884}
885
886struct user_element {
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100887 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 void *elem_data; /* element data */
889 unsigned long elem_data_size; /* size of element data in bytes */
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200890 void *tlv_data; /* TLV data */
891 unsigned long tlv_data_size; /* TLV data size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 void *priv_data; /* private data (like strings for enumerated type) */
893 unsigned long priv_data_size; /* size of private data in bytes */
894};
895
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100896static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
897 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
899 struct user_element *ue = kcontrol->private_data;
900
901 *uinfo = ue->info;
902 return 0;
903}
904
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100905static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
906 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
908 struct user_element *ue = kcontrol->private_data;
909
910 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
911 return 0;
912}
913
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100914static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
915 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 int change;
918 struct user_element *ue = kcontrol->private_data;
919
920 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
921 if (change)
922 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
923 return change;
924}
925
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200926static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol,
927 int op_flag,
928 unsigned int size,
929 unsigned int __user *tlv)
930{
931 struct user_element *ue = kcontrol->private_data;
932 int change = 0;
933 void *new_data;
934
935 if (op_flag > 0) {
936 if (size > 1024 * 128) /* sane value */
937 return -EINVAL;
938 new_data = kmalloc(size, GFP_KERNEL);
939 if (new_data == NULL)
940 return -ENOMEM;
941 if (copy_from_user(new_data, tlv, size)) {
942 kfree(new_data);
943 return -EFAULT;
944 }
945 change = ue->tlv_data_size != size;
946 if (!change)
947 change = memcmp(ue->tlv_data, new_data, size);
948 kfree(ue->tlv_data);
949 ue->tlv_data = new_data;
950 ue->tlv_data_size = size;
951 } else {
952 if (size < ue->tlv_data_size)
953 return -ENOSPC;
954 if (copy_to_user(tlv, ue->tlv_data, ue->tlv_data_size))
955 return -EFAULT;
956 }
957 return change;
958}
959
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100960static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200962 struct user_element *ue = kcontrol->private_data;
963 if (ue->tlv_data)
964 kfree(ue->tlv_data);
965 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100968static int snd_ctl_elem_add(struct snd_ctl_file *file,
969 struct snd_ctl_elem_info *info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100971 struct snd_card *card = file->card;
972 struct snd_kcontrol kctl, *_kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 unsigned int access;
974 long private_size;
975 struct user_element *ue;
976 int idx, err;
977
978 if (card->user_ctl_count >= MAX_USER_CONTROLS)
979 return -ENOMEM;
980 if (info->count > 1024)
981 return -EINVAL;
982 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100983 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200984 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
985 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 info->id.numid = 0;
987 memset(&kctl, 0, sizeof(kctl));
988 down_write(&card->controls_rwsem);
989 _kctl = snd_ctl_find_id(card, &info->id);
990 err = 0;
991 if (_kctl) {
992 if (replace)
993 err = snd_ctl_remove(card, _kctl);
994 else
995 err = -EBUSY;
996 } else {
997 if (replace)
998 err = -ENOENT;
999 }
1000 up_write(&card->controls_rwsem);
1001 if (err < 0)
1002 return err;
1003 memcpy(&kctl.id, &info->id, sizeof(info->id));
1004 kctl.count = info->owner ? info->owner : 1;
1005 access |= SNDRV_CTL_ELEM_ACCESS_USER;
1006 kctl.info = snd_ctl_elem_user_info;
1007 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1008 kctl.get = snd_ctl_elem_user_get;
1009 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1010 kctl.put = snd_ctl_elem_user_put;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001011 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
1012 kctl.tlv.c = snd_ctl_elem_user_tlv;
1013 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 switch (info->type) {
1016 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
1017 private_size = sizeof(char);
1018 if (info->count > 128)
1019 return -EINVAL;
1020 break;
1021 case SNDRV_CTL_ELEM_TYPE_INTEGER:
1022 private_size = sizeof(long);
1023 if (info->count > 128)
1024 return -EINVAL;
1025 break;
1026 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
1027 private_size = sizeof(long long);
1028 if (info->count > 64)
1029 return -EINVAL;
1030 break;
1031 case SNDRV_CTL_ELEM_TYPE_BYTES:
1032 private_size = sizeof(unsigned char);
1033 if (info->count > 512)
1034 return -EINVAL;
1035 break;
1036 case SNDRV_CTL_ELEM_TYPE_IEC958:
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001037 private_size = sizeof(struct snd_aes_iec958);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (info->count != 1)
1039 return -EINVAL;
1040 break;
1041 default:
1042 return -EINVAL;
1043 }
1044 private_size *= info->count;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001045 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (ue == NULL)
1047 return -ENOMEM;
1048 ue->info = *info;
1049 ue->elem_data = (char *)ue + sizeof(*ue);
1050 ue->elem_data_size = private_size;
1051 kctl.private_free = snd_ctl_elem_user_free;
1052 _kctl = snd_ctl_new(&kctl, access);
1053 if (_kctl == NULL) {
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001054 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return -ENOMEM;
1056 }
1057 _kctl->private_data = ue;
1058 for (idx = 0; idx < _kctl->count; idx++)
1059 _kctl->vd[idx].owner = file;
1060 err = snd_ctl_add(card, _kctl);
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001061 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 down_write(&card->controls_rwsem);
1065 card->user_ctl_count++;
1066 up_write(&card->controls_rwsem);
1067
1068 return 0;
1069}
1070
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001071static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1072 struct snd_ctl_elem_info __user *_info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001074 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (copy_from_user(&info, _info, sizeof(info)))
1076 return -EFAULT;
1077 return snd_ctl_elem_add(file, &info, replace);
1078}
1079
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001080static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1081 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001083 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 int err;
1085
1086 if (copy_from_user(&id, _id, sizeof(id)))
1087 return -EFAULT;
1088 err = snd_ctl_remove_unlocked_id(file, &id);
1089 if (! err) {
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001090 struct snd_card *card = file->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 down_write(&card->controls_rwsem);
1092 card->user_ctl_count--;
1093 up_write(&card->controls_rwsem);
1094 }
1095 return err;
1096}
1097
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001098static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
1100 int subscribe;
1101 if (get_user(subscribe, ptr))
1102 return -EFAULT;
1103 if (subscribe < 0) {
1104 subscribe = file->subscribed;
1105 if (put_user(subscribe, ptr))
1106 return -EFAULT;
1107 return 0;
1108 }
1109 if (subscribe) {
1110 file->subscribed = 1;
1111 return 0;
1112 } else if (file->subscribed) {
1113 snd_ctl_empty_read_queue(file);
1114 file->subscribed = 0;
1115 }
1116 return 0;
1117}
1118
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001119static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1120 struct snd_ctl_tlv __user *_tlv,
1121 int op_flag)
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001122{
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001123 struct snd_card *card = file->card;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001124 struct snd_ctl_tlv tlv;
1125 struct snd_kcontrol *kctl;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001126 struct snd_kcontrol_volatile *vd;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001127 unsigned int len;
1128 int err = 0;
1129
1130 if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
1131 return -EFAULT;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001132 if (tlv.length < sizeof(unsigned int) * 3)
1133 return -EINVAL;
1134 down_read(&card->controls_rwsem);
1135 kctl = snd_ctl_find_numid(card, tlv.numid);
1136 if (kctl == NULL) {
1137 err = -ENOENT;
1138 goto __kctl_end;
1139 }
1140 if (kctl->tlv.p == NULL) {
1141 err = -ENXIO;
1142 goto __kctl_end;
1143 }
1144 vd = &kctl->vd[tlv.numid - kctl->id.numid];
1145 if ((op_flag == 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) == 0) ||
1146 (op_flag > 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) == 0) ||
1147 (op_flag < 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND) == 0)) {
1148 err = -ENXIO;
1149 goto __kctl_end;
1150 }
1151 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1152 if (file && vd->owner != NULL && vd->owner != file) {
1153 err = -EPERM;
1154 goto __kctl_end;
1155 }
1156 err = kctl->tlv.c(kctl, op_flag, tlv.length, _tlv->tlv);
1157 if (err > 0) {
1158 up_read(&card->controls_rwsem);
1159 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &kctl->id);
1160 return 0;
1161 }
1162 } else {
1163 if (op_flag) {
1164 err = -ENXIO;
1165 goto __kctl_end;
1166 }
1167 len = kctl->tlv.p[1] + 2 * sizeof(unsigned int);
1168 if (tlv.length < len) {
1169 err = -ENOMEM;
1170 goto __kctl_end;
1171 }
1172 if (copy_to_user(_tlv->tlv, kctl->tlv.p, len))
1173 err = -EFAULT;
1174 }
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001175 __kctl_end:
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001176 up_read(&card->controls_rwsem);
1177 return err;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001178}
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1181{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001182 struct snd_ctl_file *ctl;
1183 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001185 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 void __user *argp = (void __user *)arg;
1187 int __user *ip = argp;
1188 int err;
1189
1190 ctl = file->private_data;
1191 card = ctl->card;
1192 snd_assert(card != NULL, return -ENXIO);
1193 switch (cmd) {
1194 case SNDRV_CTL_IOCTL_PVERSION:
1195 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1196 case SNDRV_CTL_IOCTL_CARD_INFO:
1197 return snd_ctl_card_info(card, ctl, cmd, argp);
1198 case SNDRV_CTL_IOCTL_ELEM_LIST:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001199 return snd_ctl_elem_list(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 case SNDRV_CTL_IOCTL_ELEM_INFO:
1201 return snd_ctl_elem_info_user(ctl, argp);
1202 case SNDRV_CTL_IOCTL_ELEM_READ:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001203 return snd_ctl_elem_read_user(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1205 return snd_ctl_elem_write_user(ctl, argp);
1206 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1207 return snd_ctl_elem_lock(ctl, argp);
1208 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1209 return snd_ctl_elem_unlock(ctl, argp);
1210 case SNDRV_CTL_IOCTL_ELEM_ADD:
1211 return snd_ctl_elem_add_user(ctl, argp, 0);
1212 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1213 return snd_ctl_elem_add_user(ctl, argp, 1);
1214 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1215 return snd_ctl_elem_remove(ctl, argp);
1216 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1217 return snd_ctl_subscribe_events(ctl, ip);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001218 case SNDRV_CTL_IOCTL_TLV_READ:
1219 return snd_ctl_tlv_ioctl(ctl, argp, 0);
1220 case SNDRV_CTL_IOCTL_TLV_WRITE:
1221 return snd_ctl_tlv_ioctl(ctl, argp, 1);
1222 case SNDRV_CTL_IOCTL_TLV_COMMAND:
1223 return snd_ctl_tlv_ioctl(ctl, argp, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 case SNDRV_CTL_IOCTL_POWER:
Takashi Iwaia381a7a2005-11-17 15:55:49 +01001225 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 case SNDRV_CTL_IOCTL_POWER_STATE:
1227#ifdef CONFIG_PM
1228 return put_user(card->power_state, ip) ? -EFAULT : 0;
1229#else
1230 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1231#endif
1232 }
1233 down_read(&snd_ioctl_rwsem);
1234 list_for_each(list, &snd_control_ioctls) {
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001235 p = list_entry(list, struct snd_kctl_ioctl, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 err = p->fioctl(card, ctl, cmd, arg);
1237 if (err != -ENOIOCTLCMD) {
1238 up_read(&snd_ioctl_rwsem);
1239 return err;
1240 }
1241 }
1242 up_read(&snd_ioctl_rwsem);
Takashi Iwai6d85be62005-05-15 14:32:50 +02001243 snd_printdd("unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return -ENOTTY;
1245}
1246
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001247static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1248 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001250 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 int err = 0;
1252 ssize_t result = 0;
1253
1254 ctl = file->private_data;
1255 snd_assert(ctl != NULL && ctl->card != NULL, return -ENXIO);
1256 if (!ctl->subscribed)
1257 return -EBADFD;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001258 if (count < sizeof(struct snd_ctl_event))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return -EINVAL;
1260 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001261 while (count >= sizeof(struct snd_ctl_event)) {
1262 struct snd_ctl_event ev;
1263 struct snd_kctl_event *kev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 while (list_empty(&ctl->events)) {
1265 wait_queue_t wait;
1266 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1267 err = -EAGAIN;
1268 goto __end_lock;
1269 }
1270 init_waitqueue_entry(&wait, current);
1271 add_wait_queue(&ctl->change_sleep, &wait);
1272 set_current_state(TASK_INTERRUPTIBLE);
1273 spin_unlock_irq(&ctl->read_lock);
1274 schedule();
1275 remove_wait_queue(&ctl->change_sleep, &wait);
1276 if (signal_pending(current))
1277 return result > 0 ? result : -ERESTARTSYS;
1278 spin_lock_irq(&ctl->read_lock);
1279 }
1280 kev = snd_kctl_event(ctl->events.next);
1281 ev.type = SNDRV_CTL_EVENT_ELEM;
1282 ev.data.elem.mask = kev->mask;
1283 ev.data.elem.id = kev->id;
1284 list_del(&kev->list);
1285 spin_unlock_irq(&ctl->read_lock);
1286 kfree(kev);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001287 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 err = -EFAULT;
1289 goto __end;
1290 }
1291 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001292 buffer += sizeof(struct snd_ctl_event);
1293 count -= sizeof(struct snd_ctl_event);
1294 result += sizeof(struct snd_ctl_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296 __end_lock:
1297 spin_unlock_irq(&ctl->read_lock);
1298 __end:
1299 return result > 0 ? result : err;
1300}
1301
1302static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1303{
1304 unsigned int mask;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001305 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 ctl = file->private_data;
1308 if (!ctl->subscribed)
1309 return 0;
1310 poll_wait(file, &ctl->change_sleep, wait);
1311
1312 mask = 0;
1313 if (!list_empty(&ctl->events))
1314 mask |= POLLIN | POLLRDNORM;
1315
1316 return mask;
1317}
1318
1319/*
1320 * register the device-specific control-ioctls.
1321 * called from each device manager like pcm.c, hwdep.c, etc.
1322 */
1323static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1324{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001325 struct snd_kctl_ioctl *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001327 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (pn == NULL)
1329 return -ENOMEM;
1330 pn->fioctl = fcn;
1331 down_write(&snd_ioctl_rwsem);
1332 list_add_tail(&pn->list, lists);
1333 up_write(&snd_ioctl_rwsem);
1334 return 0;
1335}
1336
1337int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1338{
1339 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1340}
1341
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001342EXPORT_SYMBOL(snd_ctl_register_ioctl);
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344#ifdef CONFIG_COMPAT
1345int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1346{
1347 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1348}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001349
1350EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351#endif
1352
1353/*
1354 * de-register the device-specific control-ioctls.
1355 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001356static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1357 struct list_head *lists)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
1359 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001360 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Takashi Iwai7c22f1a2005-10-10 11:46:31 +02001362 snd_assert(fcn != NULL, return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 down_write(&snd_ioctl_rwsem);
1364 list_for_each(list, lists) {
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001365 p = list_entry(list, struct snd_kctl_ioctl, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (p->fioctl == fcn) {
1367 list_del(&p->list);
1368 up_write(&snd_ioctl_rwsem);
1369 kfree(p);
1370 return 0;
1371 }
1372 }
1373 up_write(&snd_ioctl_rwsem);
1374 snd_BUG();
1375 return -EINVAL;
1376}
1377
1378int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1379{
1380 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1381}
1382
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001383EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385#ifdef CONFIG_COMPAT
1386int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1387{
1388 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1389}
1390
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001391EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392#endif
1393
1394static int snd_ctl_fasync(int fd, struct file * file, int on)
1395{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001396 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 int err;
1398 ctl = file->private_data;
1399 err = fasync_helper(fd, file, on, &ctl->fasync);
1400 if (err < 0)
1401 return err;
1402 return 0;
1403}
1404
1405/*
1406 * ioctl32 compat
1407 */
1408#ifdef CONFIG_COMPAT
1409#include "control_compat.c"
1410#else
1411#define snd_ctl_ioctl_compat NULL
1412#endif
1413
1414/*
1415 * INIT PART
1416 */
1417
1418static struct file_operations snd_ctl_f_ops =
1419{
1420 .owner = THIS_MODULE,
1421 .read = snd_ctl_read,
1422 .open = snd_ctl_open,
1423 .release = snd_ctl_release,
1424 .poll = snd_ctl_poll,
1425 .unlocked_ioctl = snd_ctl_ioctl,
1426 .compat_ioctl = snd_ctl_ioctl_compat,
1427 .fasync = snd_ctl_fasync,
1428};
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430/*
1431 * registration of the control device
1432 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001433static int snd_ctl_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001435 struct snd_card *card = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 int err, cardnum;
1437 char name[16];
1438
1439 snd_assert(card != NULL, return -ENXIO);
1440 cardnum = card->number;
1441 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1442 sprintf(name, "controlC%i", cardnum);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001443 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1444 &snd_ctl_f_ops, card, name)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return err;
1446 return 0;
1447}
1448
1449/*
1450 * disconnection of the control device
1451 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001452static int snd_ctl_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001454 struct snd_card *card = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 struct list_head *flist;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001456 struct snd_ctl_file *ctl;
Takashi Iwaic4614822006-06-23 14:38:23 +02001457 int err, cardnum;
1458
1459 snd_assert(card != NULL, return -ENXIO);
1460 cardnum = card->number;
1461 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 down_read(&card->controls_rwsem);
1464 list_for_each(flist, &card->ctl_files) {
1465 ctl = snd_ctl_file(flist);
1466 wake_up(&ctl->change_sleep);
1467 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1468 }
1469 up_read(&card->controls_rwsem);
Takashi Iwaic4614822006-06-23 14:38:23 +02001470
1471 if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
1472 card, -1)) < 0)
1473 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return 0;
1475}
1476
1477/*
1478 * free all controls
1479 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001480static int snd_ctl_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001482 struct snd_card *card = device->device_data;
1483 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 down_write(&card->controls_rwsem);
1486 while (!list_empty(&card->controls)) {
1487 control = snd_kcontrol(card->controls.next);
1488 snd_ctl_remove(card, control);
1489 }
1490 up_write(&card->controls_rwsem);
1491 return 0;
1492}
1493
1494/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 * create control core:
1496 * called from init.c
1497 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001498int snd_ctl_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001500 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 .dev_free = snd_ctl_dev_free,
1502 .dev_register = snd_ctl_dev_register,
1503 .dev_disconnect = snd_ctl_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 };
1505
1506 snd_assert(card != NULL, return -ENXIO);
1507 return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
1508}