blob: 67f09b8f85e401180e5d0340d4a7e29e06ce7566 [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;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100111 struct snd_card *card;
112 struct snd_ctl_file *ctl;
113 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 unsigned int idx;
115
116 ctl = file->private_data;
117 fasync_helper(-1, file, 0, &ctl->fasync);
118 file->private_data = NULL;
119 card = ctl->card;
120 write_lock_irqsave(&card->ctl_files_rwlock, flags);
121 list_del(&ctl->list);
122 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
123 down_write(&card->controls_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200124 list_for_each_entry(control, &card->controls, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 for (idx = 0; idx < control->count; idx++)
126 if (control->vd[idx].owner == ctl)
127 control->vd[idx].owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 up_write(&card->controls_rwsem);
129 snd_ctl_empty_read_queue(ctl);
130 kfree(ctl);
131 module_put(card->module);
132 snd_card_file_remove(card, file);
133 return 0;
134}
135
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100136void snd_ctl_notify(struct snd_card *card, unsigned int mask,
137 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100140 struct snd_ctl_file *ctl;
141 struct snd_kctl_event *ev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200143 snd_assert(card != NULL && id != NULL, return);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 read_lock(&card->ctl_files_rwlock);
145#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
146 card->mixer_oss_change_count++;
147#endif
Johannes Berg9244b2c2006-10-05 16:02:22 +0200148 list_for_each_entry(ctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (!ctl->subscribed)
150 continue;
151 spin_lock_irqsave(&ctl->read_lock, flags);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200152 list_for_each_entry(ev, &ctl->events, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (ev->id.numid == id->numid) {
154 ev->mask |= mask;
155 goto _found;
156 }
157 }
Takashi Iwaica2c0962005-09-09 14:20:23 +0200158 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (ev) {
160 ev->id = *id;
161 ev->mask = mask;
162 list_add_tail(&ev->list, &ctl->events);
163 } else {
164 snd_printk(KERN_ERR "No memory available to allocate event\n");
165 }
166 _found:
167 wake_up(&ctl->change_sleep);
168 spin_unlock_irqrestore(&ctl->read_lock, flags);
169 kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
170 }
171 read_unlock(&card->ctl_files_rwlock);
172}
173
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200174EXPORT_SYMBOL(snd_ctl_notify);
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/**
177 * snd_ctl_new - create a control instance from the template
178 * @control: the control template
179 * @access: the default control access
180 *
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100181 * Allocates a new struct snd_kcontrol instance and copies the given template
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * to the new instance. It does not copy volatile data (access).
183 *
184 * Returns the pointer of the new instance, or NULL on failure.
185 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100186struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control, unsigned int access)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100188 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 unsigned int idx;
190
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200191 snd_assert(control != NULL, return NULL);
192 snd_assert(control->count > 0, return NULL);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100193 kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100194 if (kctl == NULL) {
195 snd_printk(KERN_ERR "Cannot allocate control instance\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return NULL;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 *kctl = *control;
199 for (idx = 0; idx < kctl->count; idx++)
200 kctl->vd[idx].access = access;
201 return kctl;
202}
203
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200204EXPORT_SYMBOL(snd_ctl_new);
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/**
207 * snd_ctl_new1 - create a control instance from the template
208 * @ncontrol: the initialization record
209 * @private_data: the private data to set
210 *
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100211 * Allocates a new struct snd_kcontrol instance and initialize from the given
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 * template. When the access field of ncontrol is 0, it's assumed as
213 * READWRITE access. When the count field is 0, it's assumes as one.
214 *
215 * Returns the pointer of the newly generated instance, or NULL on failure.
216 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100217struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
218 void *private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100220 struct snd_kcontrol kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 unsigned int access;
222
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200223 snd_assert(ncontrol != NULL, return NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 snd_assert(ncontrol->info != NULL, return NULL);
225 memset(&kctl, 0, sizeof(kctl));
226 kctl.id.iface = ncontrol->iface;
227 kctl.id.device = ncontrol->device;
228 kctl.id.subdevice = ncontrol->subdevice;
229 if (ncontrol->name)
230 strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
231 kctl.id.index = ncontrol->index;
232 kctl.count = ncontrol->count ? ncontrol->count : 1;
233 access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200234 (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
235 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
236 SNDRV_CTL_ELEM_ACCESS_DINDIRECT|
237 SNDRV_CTL_ELEM_ACCESS_INDIRECT|
238 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE|
239 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 kctl.info = ncontrol->info;
241 kctl.get = ncontrol->get;
242 kctl.put = ncontrol->put;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200243 kctl.tlv.p = ncontrol->tlv.p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 kctl.private_value = ncontrol->private_value;
245 kctl.private_data = private_data;
246 return snd_ctl_new(&kctl, access);
247}
248
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200249EXPORT_SYMBOL(snd_ctl_new1);
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/**
252 * snd_ctl_free_one - release the control instance
253 * @kcontrol: the control instance
254 *
255 * Releases the control instance created via snd_ctl_new()
256 * or snd_ctl_new1().
257 * Don't call this after the control was added to the card.
258 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100259void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 if (kcontrol) {
262 if (kcontrol->private_free)
263 kcontrol->private_free(kcontrol);
264 kfree(kcontrol);
265 }
266}
267
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200268EXPORT_SYMBOL(snd_ctl_free_one);
269
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100270static unsigned int snd_ctl_hole_check(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 unsigned int count)
272{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100273 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Johannes Berg9244b2c2006-10-05 16:02:22 +0200275 list_for_each_entry(kctl, &card->controls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if ((kctl->id.numid <= card->last_numid &&
277 kctl->id.numid + kctl->count > card->last_numid) ||
278 (kctl->id.numid <= card->last_numid + count - 1 &&
279 kctl->id.numid + kctl->count > card->last_numid + count - 1))
280 return card->last_numid = kctl->id.numid + kctl->count - 1;
281 }
282 return card->last_numid;
283}
284
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100285static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 unsigned int last_numid, iter = 100000;
288
289 last_numid = card->last_numid;
290 while (last_numid != snd_ctl_hole_check(card, count)) {
291 if (--iter == 0) {
292 /* this situation is very unlikely */
293 snd_printk(KERN_ERR "unable to allocate new control numid\n");
294 return -ENOMEM;
295 }
296 last_numid = card->last_numid;
297 }
298 return 0;
299}
300
301/**
302 * snd_ctl_add - add the control instance to the card
303 * @card: the card instance
304 * @kcontrol: the control instance to add
305 *
306 * Adds the control instance created via snd_ctl_new() or
307 * snd_ctl_new1() to the given card. Assigns also an unique
308 * numid used for fast search.
309 *
310 * Returns zero if successful, or a negative error code on failure.
311 *
312 * It frees automatically the control which cannot be added.
313 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100314int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100316 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 unsigned int idx;
Takashi Iwaic6077b32006-03-21 16:07:13 +0100318 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100320 if (! kcontrol)
Takashi Iwaic6077b32006-03-21 16:07:13 +0100321 return err;
322 snd_assert(card != NULL, goto error);
323 snd_assert(kcontrol->info != NULL, goto error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 id = kcontrol->id;
325 down_write(&card->controls_rwsem);
326 if (snd_ctl_find_id(card, &id)) {
327 up_write(&card->controls_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
329 id.iface,
330 id.device,
331 id.subdevice,
332 id.name,
333 id.index);
Takashi Iwaic6077b32006-03-21 16:07:13 +0100334 err = -EBUSY;
335 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
338 up_write(&card->controls_rwsem);
Takashi Iwaic6077b32006-03-21 16:07:13 +0100339 err = -ENOMEM;
340 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342 list_add_tail(&kcontrol->list, &card->controls);
343 card->controls_count += kcontrol->count;
344 kcontrol->id.numid = card->last_numid + 1;
345 card->last_numid += kcontrol->count;
346 up_write(&card->controls_rwsem);
347 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
348 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
349 return 0;
Takashi Iwaic6077b32006-03-21 16:07:13 +0100350
351 error:
352 snd_ctl_free_one(kcontrol);
353 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200356EXPORT_SYMBOL(snd_ctl_add);
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358/**
359 * snd_ctl_remove - remove the control from the card and release it
360 * @card: the card instance
361 * @kcontrol: the control instance to remove
362 *
363 * Removes the control from the card and then releases the instance.
364 * You don't need to call snd_ctl_free_one(). You must be in
365 * the write lock - down_write(&card->controls_rwsem).
366 *
367 * Returns 0 if successful, or a negative error code on failure.
368 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100369int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100371 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 unsigned int idx;
373
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200374 snd_assert(card != NULL && kcontrol != NULL, return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 list_del(&kcontrol->list);
376 card->controls_count -= kcontrol->count;
377 id = kcontrol->id;
378 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
379 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
380 snd_ctl_free_one(kcontrol);
381 return 0;
382}
383
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200384EXPORT_SYMBOL(snd_ctl_remove);
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386/**
387 * snd_ctl_remove_id - remove the control of the given id and release it
388 * @card: the card instance
389 * @id: the control id to remove
390 *
391 * Finds the control instance with the given id, removes it from the
392 * card list and releases it.
393 *
394 * Returns 0 if successful, or a negative error code on failure.
395 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100396int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100398 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 int ret;
400
401 down_write(&card->controls_rwsem);
402 kctl = snd_ctl_find_id(card, id);
403 if (kctl == NULL) {
404 up_write(&card->controls_rwsem);
405 return -ENOENT;
406 }
407 ret = snd_ctl_remove(card, kctl);
408 up_write(&card->controls_rwsem);
409 return ret;
410}
411
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200412EXPORT_SYMBOL(snd_ctl_remove_id);
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414/**
415 * snd_ctl_remove_unlocked_id - remove the unlocked control of the given id and release it
416 * @file: active control handle
417 * @id: the control id to remove
418 *
419 * Finds the control instance with the given id, removes it from the
420 * card list and releases it.
421 *
422 * Returns 0 if successful, or a negative error code on failure.
423 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100424static int snd_ctl_remove_unlocked_id(struct snd_ctl_file * file,
425 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100427 struct snd_card *card = file->card;
428 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 int idx, ret;
430
431 down_write(&card->controls_rwsem);
432 kctl = snd_ctl_find_id(card, id);
433 if (kctl == NULL) {
434 up_write(&card->controls_rwsem);
435 return -ENOENT;
436 }
437 for (idx = 0; idx < kctl->count; idx++)
438 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
439 up_write(&card->controls_rwsem);
440 return -EBUSY;
441 }
442 ret = snd_ctl_remove(card, kctl);
443 up_write(&card->controls_rwsem);
444 return ret;
445}
446
447/**
448 * snd_ctl_rename_id - replace the id of a control on the card
449 * @card: the card instance
450 * @src_id: the old id
451 * @dst_id: the new id
452 *
453 * Finds the control with the old id from the card, and replaces the
454 * id with the new one.
455 *
456 * Returns zero if successful, or a negative error code on failure.
457 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100458int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
459 struct snd_ctl_elem_id *dst_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100461 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 down_write(&card->controls_rwsem);
464 kctl = snd_ctl_find_id(card, src_id);
465 if (kctl == NULL) {
466 up_write(&card->controls_rwsem);
467 return -ENOENT;
468 }
469 kctl->id = *dst_id;
470 kctl->id.numid = card->last_numid + 1;
471 card->last_numid += kctl->count;
472 up_write(&card->controls_rwsem);
473 return 0;
474}
475
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200476EXPORT_SYMBOL(snd_ctl_rename_id);
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478/**
479 * snd_ctl_find_numid - find the control instance with the given number-id
480 * @card: the card instance
481 * @numid: the number-id to search
482 *
483 * Finds the control instance with the given number-id from the card.
484 *
485 * Returns the pointer of the instance if found, or NULL if not.
486 *
487 * The caller must down card->controls_rwsem before calling this function
488 * (if the race condition can happen).
489 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100490struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100492 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200494 snd_assert(card != NULL && numid != 0, return NULL);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200495 list_for_each_entry(kctl, &card->controls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
497 return kctl;
498 }
499 return NULL;
500}
501
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200502EXPORT_SYMBOL(snd_ctl_find_numid);
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504/**
505 * snd_ctl_find_id - find the control instance with the given id
506 * @card: the card instance
507 * @id: the id to search
508 *
509 * Finds the control instance with the given id from the card.
510 *
511 * Returns the pointer of the instance if found, or NULL if not.
512 *
513 * The caller must down card->controls_rwsem before calling this function
514 * (if the race condition can happen).
515 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100516struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
517 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100519 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200521 snd_assert(card != NULL && id != NULL, return NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (id->numid != 0)
523 return snd_ctl_find_numid(card, id->numid);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200524 list_for_each_entry(kctl, &card->controls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (kctl->id.iface != id->iface)
526 continue;
527 if (kctl->id.device != id->device)
528 continue;
529 if (kctl->id.subdevice != id->subdevice)
530 continue;
531 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
532 continue;
533 if (kctl->id.index > id->index)
534 continue;
535 if (kctl->id.index + kctl->count <= id->index)
536 continue;
537 return kctl;
538 }
539 return NULL;
540}
541
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200542EXPORT_SYMBOL(snd_ctl_find_id);
543
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100544static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 unsigned int cmd, void __user *arg)
546{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100547 struct snd_ctl_card_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Takashi Iwaica2c0962005-09-09 14:20:23 +0200549 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (! info)
551 return -ENOMEM;
552 down_read(&snd_ioctl_rwsem);
553 info->card = card->number;
554 strlcpy(info->id, card->id, sizeof(info->id));
555 strlcpy(info->driver, card->driver, sizeof(info->driver));
556 strlcpy(info->name, card->shortname, sizeof(info->name));
557 strlcpy(info->longname, card->longname, sizeof(info->longname));
558 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
559 strlcpy(info->components, card->components, sizeof(info->components));
560 up_read(&snd_ioctl_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100561 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 kfree(info);
563 return -EFAULT;
564 }
565 kfree(info);
566 return 0;
567}
568
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100569static int snd_ctl_elem_list(struct snd_card *card,
570 struct snd_ctl_elem_list __user *_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 struct list_head *plist;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100573 struct snd_ctl_elem_list list;
574 struct snd_kcontrol *kctl;
575 struct snd_ctl_elem_id *dst, *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 unsigned int offset, space, first, jidx;
577
578 if (copy_from_user(&list, _list, sizeof(list)))
579 return -EFAULT;
580 offset = list.offset;
581 space = list.space;
582 first = 0;
583 /* try limit maximum space */
584 if (space > 16384)
585 return -ENOMEM;
586 if (space > 0) {
587 /* allocate temporary buffer for atomic operation */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100588 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (dst == NULL)
590 return -ENOMEM;
591 down_read(&card->controls_rwsem);
592 list.count = card->controls_count;
593 plist = card->controls.next;
594 while (plist != &card->controls) {
595 if (offset == 0)
596 break;
597 kctl = snd_kcontrol(plist);
598 if (offset < kctl->count)
599 break;
600 offset -= kctl->count;
601 plist = plist->next;
602 }
603 list.used = 0;
604 id = dst;
605 while (space > 0 && plist != &card->controls) {
606 kctl = snd_kcontrol(plist);
607 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
608 snd_ctl_build_ioff(id, kctl, jidx);
609 id++;
610 space--;
611 list.used++;
612 }
613 plist = plist->next;
614 offset = 0;
615 }
616 up_read(&card->controls_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100617 if (list.used > 0 &&
618 copy_to_user(list.pids, dst,
619 list.used * sizeof(struct snd_ctl_elem_id))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 vfree(dst);
621 return -EFAULT;
622 }
623 vfree(dst);
624 } else {
625 down_read(&card->controls_rwsem);
626 list.count = card->controls_count;
627 up_read(&card->controls_rwsem);
628 }
629 if (copy_to_user(_list, &list, sizeof(list)))
630 return -EFAULT;
631 return 0;
632}
633
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100634static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
635 struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100637 struct snd_card *card = ctl->card;
638 struct snd_kcontrol *kctl;
639 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 unsigned int index_offset;
641 int result;
642
643 down_read(&card->controls_rwsem);
644 kctl = snd_ctl_find_id(card, &info->id);
645 if (kctl == NULL) {
646 up_read(&card->controls_rwsem);
647 return -ENOENT;
648 }
649#ifdef CONFIG_SND_DEBUG
650 info->access = 0;
651#endif
652 result = kctl->info(kctl, info);
653 if (result >= 0) {
654 snd_assert(info->access == 0, );
655 index_offset = snd_ctl_get_ioff(kctl, &info->id);
656 vd = &kctl->vd[index_offset];
657 snd_ctl_build_ioff(&info->id, kctl, index_offset);
658 info->access = vd->access;
659 if (vd->owner) {
660 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
661 if (vd->owner == ctl)
662 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
663 info->owner = vd->owner_pid;
664 } else {
665 info->owner = -1;
666 }
667 }
668 up_read(&card->controls_rwsem);
669 return result;
670}
671
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100672static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
673 struct snd_ctl_elem_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100675 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 int result;
677
678 if (copy_from_user(&info, _info, sizeof(info)))
679 return -EFAULT;
Giuliano Pochini64649402006-03-13 14:11:11 +0100680 snd_power_lock(ctl->card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200681 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100682 if (result >= 0)
683 result = snd_ctl_elem_info(ctl, &info);
684 snd_power_unlock(ctl->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (result >= 0)
686 if (copy_to_user(_info, &info, sizeof(info)))
687 return -EFAULT;
688 return result;
689}
690
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100691int snd_ctl_elem_read(struct snd_card *card, struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100693 struct snd_kcontrol *kctl;
694 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 unsigned int index_offset;
696 int result, indirect;
697
698 down_read(&card->controls_rwsem);
699 kctl = snd_ctl_find_id(card, &control->id);
700 if (kctl == NULL) {
701 result = -ENOENT;
702 } else {
703 index_offset = snd_ctl_get_ioff(kctl, &control->id);
704 vd = &kctl->vd[index_offset];
705 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
706 if (control->indirect != indirect) {
707 result = -EACCES;
708 } else {
709 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) && kctl->get != NULL) {
710 snd_ctl_build_ioff(&control->id, kctl, index_offset);
711 result = kctl->get(kctl, control);
712 } else {
713 result = -EPERM;
714 }
715 }
716 }
717 up_read(&card->controls_rwsem);
718 return result;
719}
720
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200721EXPORT_SYMBOL(snd_ctl_elem_read);
722
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100723static int snd_ctl_elem_read_user(struct snd_card *card,
724 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100726 struct snd_ctl_elem_value *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 int result;
728
729 control = kmalloc(sizeof(*control), GFP_KERNEL);
730 if (control == NULL)
731 return -ENOMEM;
732 if (copy_from_user(control, _control, sizeof(*control))) {
733 kfree(control);
734 return -EFAULT;
735 }
Giuliano Pochini64649402006-03-13 14:11:11 +0100736 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200737 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100738 if (result >= 0)
739 result = snd_ctl_elem_read(card, control);
740 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (result >= 0)
742 if (copy_to_user(_control, control, sizeof(*control)))
743 result = -EFAULT;
744 kfree(control);
745 return result;
746}
747
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100748int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
749 struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100751 struct snd_kcontrol *kctl;
752 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 unsigned int index_offset;
754 int result, indirect;
755
756 down_read(&card->controls_rwsem);
757 kctl = snd_ctl_find_id(card, &control->id);
758 if (kctl == NULL) {
759 result = -ENOENT;
760 } else {
761 index_offset = snd_ctl_get_ioff(kctl, &control->id);
762 vd = &kctl->vd[index_offset];
763 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
764 if (control->indirect != indirect) {
765 result = -EACCES;
766 } else {
767 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
768 kctl->put == NULL ||
769 (file && vd->owner != NULL && vd->owner != file)) {
770 result = -EPERM;
771 } else {
772 snd_ctl_build_ioff(&control->id, kctl, index_offset);
773 result = kctl->put(kctl, control);
774 }
775 if (result > 0) {
776 up_read(&card->controls_rwsem);
777 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &control->id);
778 return 0;
779 }
780 }
781 }
782 up_read(&card->controls_rwsem);
783 return result;
784}
785
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200786EXPORT_SYMBOL(snd_ctl_elem_write);
787
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100788static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
789 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100791 struct snd_ctl_elem_value *control;
Giuliano Pochini64649402006-03-13 14:11:11 +0100792 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 int result;
794
795 control = kmalloc(sizeof(*control), GFP_KERNEL);
796 if (control == NULL)
797 return -ENOMEM;
798 if (copy_from_user(control, _control, sizeof(*control))) {
799 kfree(control);
800 return -EFAULT;
801 }
Giuliano Pochini64649402006-03-13 14:11:11 +0100802 card = file->card;
803 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200804 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100805 if (result >= 0)
806 result = snd_ctl_elem_write(card, file, control);
807 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (result >= 0)
809 if (copy_to_user(_control, control, sizeof(*control)))
810 result = -EFAULT;
811 kfree(control);
812 return result;
813}
814
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100815static int snd_ctl_elem_lock(struct snd_ctl_file *file,
816 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100818 struct snd_card *card = file->card;
819 struct snd_ctl_elem_id id;
820 struct snd_kcontrol *kctl;
821 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 int result;
823
824 if (copy_from_user(&id, _id, sizeof(id)))
825 return -EFAULT;
826 down_write(&card->controls_rwsem);
827 kctl = snd_ctl_find_id(card, &id);
828 if (kctl == NULL) {
829 result = -ENOENT;
830 } else {
831 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
832 if (vd->owner != NULL)
833 result = -EBUSY;
834 else {
835 vd->owner = file;
836 vd->owner_pid = current->pid;
837 result = 0;
838 }
839 }
840 up_write(&card->controls_rwsem);
841 return result;
842}
843
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100844static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
845 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100847 struct snd_card *card = file->card;
848 struct snd_ctl_elem_id id;
849 struct snd_kcontrol *kctl;
850 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 int result;
852
853 if (copy_from_user(&id, _id, sizeof(id)))
854 return -EFAULT;
855 down_write(&card->controls_rwsem);
856 kctl = snd_ctl_find_id(card, &id);
857 if (kctl == NULL) {
858 result = -ENOENT;
859 } else {
860 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
861 if (vd->owner == NULL)
862 result = -EINVAL;
863 else if (vd->owner != file)
864 result = -EPERM;
865 else {
866 vd->owner = NULL;
867 vd->owner_pid = 0;
868 result = 0;
869 }
870 }
871 up_write(&card->controls_rwsem);
872 return result;
873}
874
875struct user_element {
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100876 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 void *elem_data; /* element data */
878 unsigned long elem_data_size; /* size of element data in bytes */
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200879 void *tlv_data; /* TLV data */
880 unsigned long tlv_data_size; /* TLV data size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 void *priv_data; /* private data (like strings for enumerated type) */
882 unsigned long priv_data_size; /* size of private data in bytes */
883};
884
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100885static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
886 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888 struct user_element *ue = kcontrol->private_data;
889
890 *uinfo = ue->info;
891 return 0;
892}
893
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100894static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
895 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
897 struct user_element *ue = kcontrol->private_data;
898
899 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
900 return 0;
901}
902
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100903static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
904 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
906 int change;
907 struct user_element *ue = kcontrol->private_data;
908
909 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
910 if (change)
911 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
912 return change;
913}
914
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200915static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol,
916 int op_flag,
917 unsigned int size,
918 unsigned int __user *tlv)
919{
920 struct user_element *ue = kcontrol->private_data;
921 int change = 0;
922 void *new_data;
923
924 if (op_flag > 0) {
925 if (size > 1024 * 128) /* sane value */
926 return -EINVAL;
927 new_data = kmalloc(size, GFP_KERNEL);
928 if (new_data == NULL)
929 return -ENOMEM;
930 if (copy_from_user(new_data, tlv, size)) {
931 kfree(new_data);
932 return -EFAULT;
933 }
934 change = ue->tlv_data_size != size;
935 if (!change)
936 change = memcmp(ue->tlv_data, new_data, size);
937 kfree(ue->tlv_data);
938 ue->tlv_data = new_data;
939 ue->tlv_data_size = size;
940 } else {
Takashi Iwai18c1c3f2006-08-25 11:39:34 +0200941 if (! ue->tlv_data_size || ! ue->tlv_data)
942 return -ENXIO;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200943 if (size < ue->tlv_data_size)
944 return -ENOSPC;
945 if (copy_to_user(tlv, ue->tlv_data, ue->tlv_data_size))
946 return -EFAULT;
947 }
948 return change;
949}
950
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100951static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200953 struct user_element *ue = kcontrol->private_data;
954 if (ue->tlv_data)
955 kfree(ue->tlv_data);
956 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100959static int snd_ctl_elem_add(struct snd_ctl_file *file,
960 struct snd_ctl_elem_info *info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100962 struct snd_card *card = file->card;
963 struct snd_kcontrol kctl, *_kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 unsigned int access;
965 long private_size;
966 struct user_element *ue;
967 int idx, err;
968
969 if (card->user_ctl_count >= MAX_USER_CONTROLS)
970 return -ENOMEM;
971 if (info->count > 1024)
972 return -EINVAL;
973 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100974 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200975 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
976 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 info->id.numid = 0;
978 memset(&kctl, 0, sizeof(kctl));
979 down_write(&card->controls_rwsem);
980 _kctl = snd_ctl_find_id(card, &info->id);
981 err = 0;
982 if (_kctl) {
983 if (replace)
984 err = snd_ctl_remove(card, _kctl);
985 else
986 err = -EBUSY;
987 } else {
988 if (replace)
989 err = -ENOENT;
990 }
991 up_write(&card->controls_rwsem);
992 if (err < 0)
993 return err;
994 memcpy(&kctl.id, &info->id, sizeof(info->id));
995 kctl.count = info->owner ? info->owner : 1;
996 access |= SNDRV_CTL_ELEM_ACCESS_USER;
997 kctl.info = snd_ctl_elem_user_info;
998 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
999 kctl.get = snd_ctl_elem_user_get;
1000 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1001 kctl.put = snd_ctl_elem_user_put;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001002 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
1003 kctl.tlv.c = snd_ctl_elem_user_tlv;
1004 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 switch (info->type) {
1007 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 case SNDRV_CTL_ELEM_TYPE_INTEGER:
1009 private_size = sizeof(long);
1010 if (info->count > 128)
1011 return -EINVAL;
1012 break;
1013 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
1014 private_size = sizeof(long long);
1015 if (info->count > 64)
1016 return -EINVAL;
1017 break;
1018 case SNDRV_CTL_ELEM_TYPE_BYTES:
1019 private_size = sizeof(unsigned char);
1020 if (info->count > 512)
1021 return -EINVAL;
1022 break;
1023 case SNDRV_CTL_ELEM_TYPE_IEC958:
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001024 private_size = sizeof(struct snd_aes_iec958);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (info->count != 1)
1026 return -EINVAL;
1027 break;
1028 default:
1029 return -EINVAL;
1030 }
1031 private_size *= info->count;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001032 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (ue == NULL)
1034 return -ENOMEM;
1035 ue->info = *info;
Takashi Iwai86148e82006-08-24 12:36:36 +02001036 ue->info.access = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 ue->elem_data = (char *)ue + sizeof(*ue);
1038 ue->elem_data_size = private_size;
1039 kctl.private_free = snd_ctl_elem_user_free;
1040 _kctl = snd_ctl_new(&kctl, access);
1041 if (_kctl == NULL) {
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001042 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return -ENOMEM;
1044 }
1045 _kctl->private_data = ue;
1046 for (idx = 0; idx < _kctl->count; idx++)
1047 _kctl->vd[idx].owner = file;
1048 err = snd_ctl_add(card, _kctl);
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001049 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 down_write(&card->controls_rwsem);
1053 card->user_ctl_count++;
1054 up_write(&card->controls_rwsem);
1055
1056 return 0;
1057}
1058
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001059static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1060 struct snd_ctl_elem_info __user *_info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001062 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (copy_from_user(&info, _info, sizeof(info)))
1064 return -EFAULT;
1065 return snd_ctl_elem_add(file, &info, replace);
1066}
1067
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001068static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1069 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001071 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 int err;
1073
1074 if (copy_from_user(&id, _id, sizeof(id)))
1075 return -EFAULT;
1076 err = snd_ctl_remove_unlocked_id(file, &id);
1077 if (! err) {
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001078 struct snd_card *card = file->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 down_write(&card->controls_rwsem);
1080 card->user_ctl_count--;
1081 up_write(&card->controls_rwsem);
1082 }
1083 return err;
1084}
1085
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001086static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
1088 int subscribe;
1089 if (get_user(subscribe, ptr))
1090 return -EFAULT;
1091 if (subscribe < 0) {
1092 subscribe = file->subscribed;
1093 if (put_user(subscribe, ptr))
1094 return -EFAULT;
1095 return 0;
1096 }
1097 if (subscribe) {
1098 file->subscribed = 1;
1099 return 0;
1100 } else if (file->subscribed) {
1101 snd_ctl_empty_read_queue(file);
1102 file->subscribed = 0;
1103 }
1104 return 0;
1105}
1106
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001107static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1108 struct snd_ctl_tlv __user *_tlv,
1109 int op_flag)
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001110{
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001111 struct snd_card *card = file->card;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001112 struct snd_ctl_tlv tlv;
1113 struct snd_kcontrol *kctl;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001114 struct snd_kcontrol_volatile *vd;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001115 unsigned int len;
1116 int err = 0;
1117
1118 if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
1119 return -EFAULT;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001120 if (tlv.length < sizeof(unsigned int) * 3)
1121 return -EINVAL;
1122 down_read(&card->controls_rwsem);
1123 kctl = snd_ctl_find_numid(card, tlv.numid);
1124 if (kctl == NULL) {
1125 err = -ENOENT;
1126 goto __kctl_end;
1127 }
1128 if (kctl->tlv.p == NULL) {
1129 err = -ENXIO;
1130 goto __kctl_end;
1131 }
1132 vd = &kctl->vd[tlv.numid - kctl->id.numid];
1133 if ((op_flag == 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) == 0) ||
1134 (op_flag > 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) == 0) ||
1135 (op_flag < 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND) == 0)) {
1136 err = -ENXIO;
1137 goto __kctl_end;
1138 }
1139 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1140 if (file && vd->owner != NULL && vd->owner != file) {
1141 err = -EPERM;
1142 goto __kctl_end;
1143 }
1144 err = kctl->tlv.c(kctl, op_flag, tlv.length, _tlv->tlv);
1145 if (err > 0) {
1146 up_read(&card->controls_rwsem);
1147 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &kctl->id);
1148 return 0;
1149 }
1150 } else {
1151 if (op_flag) {
1152 err = -ENXIO;
1153 goto __kctl_end;
1154 }
1155 len = kctl->tlv.p[1] + 2 * sizeof(unsigned int);
1156 if (tlv.length < len) {
1157 err = -ENOMEM;
1158 goto __kctl_end;
1159 }
1160 if (copy_to_user(_tlv->tlv, kctl->tlv.p, len))
1161 err = -EFAULT;
1162 }
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001163 __kctl_end:
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001164 up_read(&card->controls_rwsem);
1165 return err;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001166}
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1169{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001170 struct snd_ctl_file *ctl;
1171 struct snd_card *card;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001172 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 void __user *argp = (void __user *)arg;
1174 int __user *ip = argp;
1175 int err;
1176
1177 ctl = file->private_data;
1178 card = ctl->card;
1179 snd_assert(card != NULL, return -ENXIO);
1180 switch (cmd) {
1181 case SNDRV_CTL_IOCTL_PVERSION:
1182 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1183 case SNDRV_CTL_IOCTL_CARD_INFO:
1184 return snd_ctl_card_info(card, ctl, cmd, argp);
1185 case SNDRV_CTL_IOCTL_ELEM_LIST:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001186 return snd_ctl_elem_list(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 case SNDRV_CTL_IOCTL_ELEM_INFO:
1188 return snd_ctl_elem_info_user(ctl, argp);
1189 case SNDRV_CTL_IOCTL_ELEM_READ:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001190 return snd_ctl_elem_read_user(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1192 return snd_ctl_elem_write_user(ctl, argp);
1193 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1194 return snd_ctl_elem_lock(ctl, argp);
1195 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1196 return snd_ctl_elem_unlock(ctl, argp);
1197 case SNDRV_CTL_IOCTL_ELEM_ADD:
1198 return snd_ctl_elem_add_user(ctl, argp, 0);
1199 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1200 return snd_ctl_elem_add_user(ctl, argp, 1);
1201 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1202 return snd_ctl_elem_remove(ctl, argp);
1203 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1204 return snd_ctl_subscribe_events(ctl, ip);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001205 case SNDRV_CTL_IOCTL_TLV_READ:
1206 return snd_ctl_tlv_ioctl(ctl, argp, 0);
1207 case SNDRV_CTL_IOCTL_TLV_WRITE:
1208 return snd_ctl_tlv_ioctl(ctl, argp, 1);
1209 case SNDRV_CTL_IOCTL_TLV_COMMAND:
1210 return snd_ctl_tlv_ioctl(ctl, argp, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 case SNDRV_CTL_IOCTL_POWER:
Takashi Iwaia381a7a2005-11-17 15:55:49 +01001212 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 case SNDRV_CTL_IOCTL_POWER_STATE:
1214#ifdef CONFIG_PM
1215 return put_user(card->power_state, ip) ? -EFAULT : 0;
1216#else
1217 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1218#endif
1219 }
1220 down_read(&snd_ioctl_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001221 list_for_each_entry(p, &snd_control_ioctls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 err = p->fioctl(card, ctl, cmd, arg);
1223 if (err != -ENOIOCTLCMD) {
1224 up_read(&snd_ioctl_rwsem);
1225 return err;
1226 }
1227 }
1228 up_read(&snd_ioctl_rwsem);
Takashi Iwai6d85be62005-05-15 14:32:50 +02001229 snd_printdd("unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return -ENOTTY;
1231}
1232
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001233static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1234 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001236 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 int err = 0;
1238 ssize_t result = 0;
1239
1240 ctl = file->private_data;
1241 snd_assert(ctl != NULL && ctl->card != NULL, return -ENXIO);
1242 if (!ctl->subscribed)
1243 return -EBADFD;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001244 if (count < sizeof(struct snd_ctl_event))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return -EINVAL;
1246 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001247 while (count >= sizeof(struct snd_ctl_event)) {
1248 struct snd_ctl_event ev;
1249 struct snd_kctl_event *kev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 while (list_empty(&ctl->events)) {
1251 wait_queue_t wait;
1252 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1253 err = -EAGAIN;
1254 goto __end_lock;
1255 }
1256 init_waitqueue_entry(&wait, current);
1257 add_wait_queue(&ctl->change_sleep, &wait);
1258 set_current_state(TASK_INTERRUPTIBLE);
1259 spin_unlock_irq(&ctl->read_lock);
1260 schedule();
1261 remove_wait_queue(&ctl->change_sleep, &wait);
1262 if (signal_pending(current))
Adrian Bunk0e5d7202006-11-07 13:42:54 +01001263 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 spin_lock_irq(&ctl->read_lock);
1265 }
1266 kev = snd_kctl_event(ctl->events.next);
1267 ev.type = SNDRV_CTL_EVENT_ELEM;
1268 ev.data.elem.mask = kev->mask;
1269 ev.data.elem.id = kev->id;
1270 list_del(&kev->list);
1271 spin_unlock_irq(&ctl->read_lock);
1272 kfree(kev);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001273 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 err = -EFAULT;
1275 goto __end;
1276 }
1277 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001278 buffer += sizeof(struct snd_ctl_event);
1279 count -= sizeof(struct snd_ctl_event);
1280 result += sizeof(struct snd_ctl_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
1282 __end_lock:
1283 spin_unlock_irq(&ctl->read_lock);
1284 __end:
1285 return result > 0 ? result : err;
1286}
1287
1288static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1289{
1290 unsigned int mask;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001291 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 ctl = file->private_data;
1294 if (!ctl->subscribed)
1295 return 0;
1296 poll_wait(file, &ctl->change_sleep, wait);
1297
1298 mask = 0;
1299 if (!list_empty(&ctl->events))
1300 mask |= POLLIN | POLLRDNORM;
1301
1302 return mask;
1303}
1304
1305/*
1306 * register the device-specific control-ioctls.
1307 * called from each device manager like pcm.c, hwdep.c, etc.
1308 */
1309static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1310{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001311 struct snd_kctl_ioctl *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001313 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (pn == NULL)
1315 return -ENOMEM;
1316 pn->fioctl = fcn;
1317 down_write(&snd_ioctl_rwsem);
1318 list_add_tail(&pn->list, lists);
1319 up_write(&snd_ioctl_rwsem);
1320 return 0;
1321}
1322
1323int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1324{
1325 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1326}
1327
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001328EXPORT_SYMBOL(snd_ctl_register_ioctl);
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330#ifdef CONFIG_COMPAT
1331int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1332{
1333 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1334}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001335
1336EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337#endif
1338
1339/*
1340 * de-register the device-specific control-ioctls.
1341 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001342static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1343 struct list_head *lists)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001345 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Takashi Iwai7c22f1a2005-10-10 11:46:31 +02001347 snd_assert(fcn != NULL, return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 down_write(&snd_ioctl_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001349 list_for_each_entry(p, lists, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (p->fioctl == fcn) {
1351 list_del(&p->list);
1352 up_write(&snd_ioctl_rwsem);
1353 kfree(p);
1354 return 0;
1355 }
1356 }
1357 up_write(&snd_ioctl_rwsem);
1358 snd_BUG();
1359 return -EINVAL;
1360}
1361
1362int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1363{
1364 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1365}
1366
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001367EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369#ifdef CONFIG_COMPAT
1370int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1371{
1372 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1373}
1374
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001375EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376#endif
1377
1378static int snd_ctl_fasync(int fd, struct file * file, int on)
1379{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001380 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 int err;
1382 ctl = file->private_data;
1383 err = fasync_helper(fd, file, on, &ctl->fasync);
1384 if (err < 0)
1385 return err;
1386 return 0;
1387}
1388
1389/*
1390 * ioctl32 compat
1391 */
1392#ifdef CONFIG_COMPAT
1393#include "control_compat.c"
1394#else
1395#define snd_ctl_ioctl_compat NULL
1396#endif
1397
1398/*
1399 * INIT PART
1400 */
1401
1402static struct file_operations snd_ctl_f_ops =
1403{
1404 .owner = THIS_MODULE,
1405 .read = snd_ctl_read,
1406 .open = snd_ctl_open,
1407 .release = snd_ctl_release,
1408 .poll = snd_ctl_poll,
1409 .unlocked_ioctl = snd_ctl_ioctl,
1410 .compat_ioctl = snd_ctl_ioctl_compat,
1411 .fasync = snd_ctl_fasync,
1412};
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414/*
1415 * registration of the control device
1416 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001417static int snd_ctl_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001419 struct snd_card *card = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 int err, cardnum;
1421 char name[16];
1422
1423 snd_assert(card != NULL, return -ENXIO);
1424 cardnum = card->number;
1425 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1426 sprintf(name, "controlC%i", cardnum);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001427 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1428 &snd_ctl_f_ops, card, name)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return err;
1430 return 0;
1431}
1432
1433/*
1434 * disconnection of the control device
1435 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001436static int snd_ctl_dev_disconnect(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;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001439 struct snd_ctl_file *ctl;
Takashi Iwaic4614822006-06-23 14:38:23 +02001440 int err, cardnum;
1441
1442 snd_assert(card != NULL, return -ENXIO);
1443 cardnum = card->number;
1444 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 down_read(&card->controls_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001447 list_for_each_entry(ctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 wake_up(&ctl->change_sleep);
1449 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1450 }
1451 up_read(&card->controls_rwsem);
Takashi Iwaic4614822006-06-23 14:38:23 +02001452
1453 if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
1454 card, -1)) < 0)
1455 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return 0;
1457}
1458
1459/*
1460 * free all controls
1461 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001462static int snd_ctl_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001464 struct snd_card *card = device->device_data;
1465 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 down_write(&card->controls_rwsem);
1468 while (!list_empty(&card->controls)) {
1469 control = snd_kcontrol(card->controls.next);
1470 snd_ctl_remove(card, control);
1471 }
1472 up_write(&card->controls_rwsem);
1473 return 0;
1474}
1475
1476/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 * create control core:
1478 * called from init.c
1479 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001480int snd_ctl_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001482 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 .dev_free = snd_ctl_dev_free,
1484 .dev_register = snd_ctl_dev_register,
1485 .dev_disconnect = snd_ctl_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 };
1487
1488 snd_assert(card != NULL, return -ENXIO);
1489 return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
1490}