blob: e9c8854d2f7b47f6d4d781cb4f62090e15bb75df [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 :
239 (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|SNDRV_CTL_ELEM_ACCESS_INACTIVE|
240 SNDRV_CTL_ELEM_ACCESS_DINDIRECT|SNDRV_CTL_ELEM_ACCESS_INDIRECT));
241 kctl.info = ncontrol->info;
242 kctl.get = ncontrol->get;
243 kctl.put = ncontrol->put;
Jaroslav Kysela42750b02006-06-01 18:34:01 +0200244 kctl.tlv = ncontrol->tlv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 kctl.private_value = ncontrol->private_value;
246 kctl.private_data = private_data;
247 return snd_ctl_new(&kctl, access);
248}
249
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200250EXPORT_SYMBOL(snd_ctl_new1);
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252/**
253 * snd_ctl_free_one - release the control instance
254 * @kcontrol: the control instance
255 *
256 * Releases the control instance created via snd_ctl_new()
257 * or snd_ctl_new1().
258 * Don't call this after the control was added to the card.
259 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100260void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 if (kcontrol) {
263 if (kcontrol->private_free)
264 kcontrol->private_free(kcontrol);
265 kfree(kcontrol);
266 }
267}
268
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200269EXPORT_SYMBOL(snd_ctl_free_one);
270
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100271static unsigned int snd_ctl_hole_check(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 unsigned int count)
273{
274 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100275 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 list_for_each(list, &card->controls) {
278 kctl = snd_kcontrol(list);
279 if ((kctl->id.numid <= card->last_numid &&
280 kctl->id.numid + kctl->count > card->last_numid) ||
281 (kctl->id.numid <= card->last_numid + count - 1 &&
282 kctl->id.numid + kctl->count > card->last_numid + count - 1))
283 return card->last_numid = kctl->id.numid + kctl->count - 1;
284 }
285 return card->last_numid;
286}
287
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100288static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 unsigned int last_numid, iter = 100000;
291
292 last_numid = card->last_numid;
293 while (last_numid != snd_ctl_hole_check(card, count)) {
294 if (--iter == 0) {
295 /* this situation is very unlikely */
296 snd_printk(KERN_ERR "unable to allocate new control numid\n");
297 return -ENOMEM;
298 }
299 last_numid = card->last_numid;
300 }
301 return 0;
302}
303
304/**
305 * snd_ctl_add - add the control instance to the card
306 * @card: the card instance
307 * @kcontrol: the control instance to add
308 *
309 * Adds the control instance created via snd_ctl_new() or
310 * snd_ctl_new1() to the given card. Assigns also an unique
311 * numid used for fast search.
312 *
313 * Returns zero if successful, or a negative error code on failure.
314 *
315 * It frees automatically the control which cannot be added.
316 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100317int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100319 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 unsigned int idx;
Takashi Iwaic6077b32006-03-21 16:07:13 +0100321 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100323 if (! kcontrol)
Takashi Iwaic6077b32006-03-21 16:07:13 +0100324 return err;
325 snd_assert(card != NULL, goto error);
326 snd_assert(kcontrol->info != NULL, goto error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 id = kcontrol->id;
328 down_write(&card->controls_rwsem);
329 if (snd_ctl_find_id(card, &id)) {
330 up_write(&card->controls_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
332 id.iface,
333 id.device,
334 id.subdevice,
335 id.name,
336 id.index);
Takashi Iwaic6077b32006-03-21 16:07:13 +0100337 err = -EBUSY;
338 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
341 up_write(&card->controls_rwsem);
Takashi Iwaic6077b32006-03-21 16:07:13 +0100342 err = -ENOMEM;
343 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345 list_add_tail(&kcontrol->list, &card->controls);
346 card->controls_count += kcontrol->count;
347 kcontrol->id.numid = card->last_numid + 1;
348 card->last_numid += kcontrol->count;
349 up_write(&card->controls_rwsem);
350 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
351 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
352 return 0;
Takashi Iwaic6077b32006-03-21 16:07:13 +0100353
354 error:
355 snd_ctl_free_one(kcontrol);
356 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200359EXPORT_SYMBOL(snd_ctl_add);
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361/**
362 * snd_ctl_remove - remove the control from the card and release it
363 * @card: the card instance
364 * @kcontrol: the control instance to remove
365 *
366 * Removes the control from the card and then releases the instance.
367 * You don't need to call snd_ctl_free_one(). You must be in
368 * the write lock - down_write(&card->controls_rwsem).
369 *
370 * Returns 0 if successful, or a negative error code on failure.
371 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100372int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100374 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 unsigned int idx;
376
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200377 snd_assert(card != NULL && kcontrol != NULL, return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 list_del(&kcontrol->list);
379 card->controls_count -= kcontrol->count;
380 id = kcontrol->id;
381 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
382 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
383 snd_ctl_free_one(kcontrol);
384 return 0;
385}
386
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200387EXPORT_SYMBOL(snd_ctl_remove);
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389/**
390 * snd_ctl_remove_id - remove the control of the given id and release it
391 * @card: the card instance
392 * @id: the control id to remove
393 *
394 * Finds the control instance with the given id, removes it from the
395 * card list and releases it.
396 *
397 * Returns 0 if successful, or a negative error code on failure.
398 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100399int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100401 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 int ret;
403
404 down_write(&card->controls_rwsem);
405 kctl = snd_ctl_find_id(card, id);
406 if (kctl == NULL) {
407 up_write(&card->controls_rwsem);
408 return -ENOENT;
409 }
410 ret = snd_ctl_remove(card, kctl);
411 up_write(&card->controls_rwsem);
412 return ret;
413}
414
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200415EXPORT_SYMBOL(snd_ctl_remove_id);
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417/**
418 * snd_ctl_remove_unlocked_id - remove the unlocked control of the given id and release it
419 * @file: active control handle
420 * @id: the control id to remove
421 *
422 * Finds the control instance with the given id, removes it from the
423 * card list and releases it.
424 *
425 * Returns 0 if successful, or a negative error code on failure.
426 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100427static int snd_ctl_remove_unlocked_id(struct snd_ctl_file * file,
428 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100430 struct snd_card *card = file->card;
431 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 int idx, ret;
433
434 down_write(&card->controls_rwsem);
435 kctl = snd_ctl_find_id(card, id);
436 if (kctl == NULL) {
437 up_write(&card->controls_rwsem);
438 return -ENOENT;
439 }
440 for (idx = 0; idx < kctl->count; idx++)
441 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
442 up_write(&card->controls_rwsem);
443 return -EBUSY;
444 }
445 ret = snd_ctl_remove(card, kctl);
446 up_write(&card->controls_rwsem);
447 return ret;
448}
449
450/**
451 * snd_ctl_rename_id - replace the id of a control on the card
452 * @card: the card instance
453 * @src_id: the old id
454 * @dst_id: the new id
455 *
456 * Finds the control with the old id from the card, and replaces the
457 * id with the new one.
458 *
459 * Returns zero if successful, or a negative error code on failure.
460 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100461int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
462 struct snd_ctl_elem_id *dst_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100464 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 down_write(&card->controls_rwsem);
467 kctl = snd_ctl_find_id(card, src_id);
468 if (kctl == NULL) {
469 up_write(&card->controls_rwsem);
470 return -ENOENT;
471 }
472 kctl->id = *dst_id;
473 kctl->id.numid = card->last_numid + 1;
474 card->last_numid += kctl->count;
475 up_write(&card->controls_rwsem);
476 return 0;
477}
478
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200479EXPORT_SYMBOL(snd_ctl_rename_id);
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481/**
482 * snd_ctl_find_numid - find the control instance with the given number-id
483 * @card: the card instance
484 * @numid: the number-id to search
485 *
486 * Finds the control instance with the given number-id from the card.
487 *
488 * Returns the pointer of the instance if found, or NULL if not.
489 *
490 * The caller must down card->controls_rwsem before calling this function
491 * (if the race condition can happen).
492 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100493struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100496 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200498 snd_assert(card != NULL && numid != 0, return NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 list_for_each(list, &card->controls) {
500 kctl = snd_kcontrol(list);
501 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
502 return kctl;
503 }
504 return NULL;
505}
506
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200507EXPORT_SYMBOL(snd_ctl_find_numid);
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509/**
510 * snd_ctl_find_id - find the control instance with the given id
511 * @card: the card instance
512 * @id: the id to search
513 *
514 * Finds the control instance with the given id from the card.
515 *
516 * Returns the pointer of the instance if found, or NULL if not.
517 *
518 * The caller must down card->controls_rwsem before calling this function
519 * (if the race condition can happen).
520 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100521struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
522 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
524 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100525 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200527 snd_assert(card != NULL && id != NULL, return NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (id->numid != 0)
529 return snd_ctl_find_numid(card, id->numid);
530 list_for_each(list, &card->controls) {
531 kctl = snd_kcontrol(list);
532 if (kctl->id.iface != id->iface)
533 continue;
534 if (kctl->id.device != id->device)
535 continue;
536 if (kctl->id.subdevice != id->subdevice)
537 continue;
538 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
539 continue;
540 if (kctl->id.index > id->index)
541 continue;
542 if (kctl->id.index + kctl->count <= id->index)
543 continue;
544 return kctl;
545 }
546 return NULL;
547}
548
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200549EXPORT_SYMBOL(snd_ctl_find_id);
550
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100551static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 unsigned int cmd, void __user *arg)
553{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100554 struct snd_ctl_card_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Takashi Iwaica2c0962005-09-09 14:20:23 +0200556 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (! info)
558 return -ENOMEM;
559 down_read(&snd_ioctl_rwsem);
560 info->card = card->number;
561 strlcpy(info->id, card->id, sizeof(info->id));
562 strlcpy(info->driver, card->driver, sizeof(info->driver));
563 strlcpy(info->name, card->shortname, sizeof(info->name));
564 strlcpy(info->longname, card->longname, sizeof(info->longname));
565 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
566 strlcpy(info->components, card->components, sizeof(info->components));
567 up_read(&snd_ioctl_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100568 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 kfree(info);
570 return -EFAULT;
571 }
572 kfree(info);
573 return 0;
574}
575
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100576static int snd_ctl_elem_list(struct snd_card *card,
577 struct snd_ctl_elem_list __user *_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 struct list_head *plist;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100580 struct snd_ctl_elem_list list;
581 struct snd_kcontrol *kctl;
582 struct snd_ctl_elem_id *dst, *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 unsigned int offset, space, first, jidx;
584
585 if (copy_from_user(&list, _list, sizeof(list)))
586 return -EFAULT;
587 offset = list.offset;
588 space = list.space;
589 first = 0;
590 /* try limit maximum space */
591 if (space > 16384)
592 return -ENOMEM;
593 if (space > 0) {
594 /* allocate temporary buffer for atomic operation */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100595 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (dst == NULL)
597 return -ENOMEM;
598 down_read(&card->controls_rwsem);
599 list.count = card->controls_count;
600 plist = card->controls.next;
601 while (plist != &card->controls) {
602 if (offset == 0)
603 break;
604 kctl = snd_kcontrol(plist);
605 if (offset < kctl->count)
606 break;
607 offset -= kctl->count;
608 plist = plist->next;
609 }
610 list.used = 0;
611 id = dst;
612 while (space > 0 && plist != &card->controls) {
613 kctl = snd_kcontrol(plist);
614 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
615 snd_ctl_build_ioff(id, kctl, jidx);
616 id++;
617 space--;
618 list.used++;
619 }
620 plist = plist->next;
621 offset = 0;
622 }
623 up_read(&card->controls_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100624 if (list.used > 0 &&
625 copy_to_user(list.pids, dst,
626 list.used * sizeof(struct snd_ctl_elem_id))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 vfree(dst);
628 return -EFAULT;
629 }
630 vfree(dst);
631 } else {
632 down_read(&card->controls_rwsem);
633 list.count = card->controls_count;
634 up_read(&card->controls_rwsem);
635 }
636 if (copy_to_user(_list, &list, sizeof(list)))
637 return -EFAULT;
638 return 0;
639}
640
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100641static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
642 struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100644 struct snd_card *card = ctl->card;
645 struct snd_kcontrol *kctl;
646 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 unsigned int index_offset;
648 int result;
649
650 down_read(&card->controls_rwsem);
651 kctl = snd_ctl_find_id(card, &info->id);
652 if (kctl == NULL) {
653 up_read(&card->controls_rwsem);
654 return -ENOENT;
655 }
656#ifdef CONFIG_SND_DEBUG
657 info->access = 0;
658#endif
659 result = kctl->info(kctl, info);
660 if (result >= 0) {
661 snd_assert(info->access == 0, );
662 index_offset = snd_ctl_get_ioff(kctl, &info->id);
663 vd = &kctl->vd[index_offset];
664 snd_ctl_build_ioff(&info->id, kctl, index_offset);
665 info->access = vd->access;
666 if (vd->owner) {
667 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
668 if (vd->owner == ctl)
669 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
670 info->owner = vd->owner_pid;
671 } else {
672 info->owner = -1;
673 }
674 }
675 up_read(&card->controls_rwsem);
676 return result;
677}
678
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100679static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
680 struct snd_ctl_elem_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100682 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 int result;
684
685 if (copy_from_user(&info, _info, sizeof(info)))
686 return -EFAULT;
Giuliano Pochini64649402006-03-13 14:11:11 +0100687 snd_power_lock(ctl->card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200688 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100689 if (result >= 0)
690 result = snd_ctl_elem_info(ctl, &info);
691 snd_power_unlock(ctl->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (result >= 0)
693 if (copy_to_user(_info, &info, sizeof(info)))
694 return -EFAULT;
695 return result;
696}
697
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100698int snd_ctl_elem_read(struct snd_card *card, struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100700 struct snd_kcontrol *kctl;
701 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 unsigned int index_offset;
703 int result, indirect;
704
705 down_read(&card->controls_rwsem);
706 kctl = snd_ctl_find_id(card, &control->id);
707 if (kctl == NULL) {
708 result = -ENOENT;
709 } else {
710 index_offset = snd_ctl_get_ioff(kctl, &control->id);
711 vd = &kctl->vd[index_offset];
712 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
713 if (control->indirect != indirect) {
714 result = -EACCES;
715 } else {
716 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) && kctl->get != NULL) {
717 snd_ctl_build_ioff(&control->id, kctl, index_offset);
718 result = kctl->get(kctl, control);
719 } else {
720 result = -EPERM;
721 }
722 }
723 }
724 up_read(&card->controls_rwsem);
725 return result;
726}
727
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200728EXPORT_SYMBOL(snd_ctl_elem_read);
729
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100730static int snd_ctl_elem_read_user(struct snd_card *card,
731 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100733 struct snd_ctl_elem_value *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 int result;
735
736 control = kmalloc(sizeof(*control), GFP_KERNEL);
737 if (control == NULL)
738 return -ENOMEM;
739 if (copy_from_user(control, _control, sizeof(*control))) {
740 kfree(control);
741 return -EFAULT;
742 }
Giuliano Pochini64649402006-03-13 14:11:11 +0100743 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200744 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100745 if (result >= 0)
746 result = snd_ctl_elem_read(card, control);
747 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (result >= 0)
749 if (copy_to_user(_control, control, sizeof(*control)))
750 result = -EFAULT;
751 kfree(control);
752 return result;
753}
754
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100755int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
756 struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100758 struct snd_kcontrol *kctl;
759 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 unsigned int index_offset;
761 int result, indirect;
762
763 down_read(&card->controls_rwsem);
764 kctl = snd_ctl_find_id(card, &control->id);
765 if (kctl == NULL) {
766 result = -ENOENT;
767 } else {
768 index_offset = snd_ctl_get_ioff(kctl, &control->id);
769 vd = &kctl->vd[index_offset];
770 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
771 if (control->indirect != indirect) {
772 result = -EACCES;
773 } else {
774 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
775 kctl->put == NULL ||
776 (file && vd->owner != NULL && vd->owner != file)) {
777 result = -EPERM;
778 } else {
779 snd_ctl_build_ioff(&control->id, kctl, index_offset);
780 result = kctl->put(kctl, control);
781 }
782 if (result > 0) {
783 up_read(&card->controls_rwsem);
784 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &control->id);
785 return 0;
786 }
787 }
788 }
789 up_read(&card->controls_rwsem);
790 return result;
791}
792
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200793EXPORT_SYMBOL(snd_ctl_elem_write);
794
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100795static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
796 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100798 struct snd_ctl_elem_value *control;
Giuliano Pochini64649402006-03-13 14:11:11 +0100799 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 int result;
801
802 control = kmalloc(sizeof(*control), GFP_KERNEL);
803 if (control == NULL)
804 return -ENOMEM;
805 if (copy_from_user(control, _control, sizeof(*control))) {
806 kfree(control);
807 return -EFAULT;
808 }
Giuliano Pochini64649402006-03-13 14:11:11 +0100809 card = file->card;
810 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200811 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100812 if (result >= 0)
813 result = snd_ctl_elem_write(card, file, control);
814 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (result >= 0)
816 if (copy_to_user(_control, control, sizeof(*control)))
817 result = -EFAULT;
818 kfree(control);
819 return result;
820}
821
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100822static int snd_ctl_elem_lock(struct snd_ctl_file *file,
823 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100825 struct snd_card *card = file->card;
826 struct snd_ctl_elem_id id;
827 struct snd_kcontrol *kctl;
828 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 int result;
830
831 if (copy_from_user(&id, _id, sizeof(id)))
832 return -EFAULT;
833 down_write(&card->controls_rwsem);
834 kctl = snd_ctl_find_id(card, &id);
835 if (kctl == NULL) {
836 result = -ENOENT;
837 } else {
838 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
839 if (vd->owner != NULL)
840 result = -EBUSY;
841 else {
842 vd->owner = file;
843 vd->owner_pid = current->pid;
844 result = 0;
845 }
846 }
847 up_write(&card->controls_rwsem);
848 return result;
849}
850
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100851static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
852 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100854 struct snd_card *card = file->card;
855 struct snd_ctl_elem_id id;
856 struct snd_kcontrol *kctl;
857 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 int result;
859
860 if (copy_from_user(&id, _id, sizeof(id)))
861 return -EFAULT;
862 down_write(&card->controls_rwsem);
863 kctl = snd_ctl_find_id(card, &id);
864 if (kctl == NULL) {
865 result = -ENOENT;
866 } else {
867 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
868 if (vd->owner == NULL)
869 result = -EINVAL;
870 else if (vd->owner != file)
871 result = -EPERM;
872 else {
873 vd->owner = NULL;
874 vd->owner_pid = 0;
875 result = 0;
876 }
877 }
878 up_write(&card->controls_rwsem);
879 return result;
880}
881
882struct user_element {
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100883 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 void *elem_data; /* element data */
885 unsigned long elem_data_size; /* size of element data in bytes */
886 void *priv_data; /* private data (like strings for enumerated type) */
887 unsigned long priv_data_size; /* size of private data in bytes */
888};
889
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100890static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
891 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 struct user_element *ue = kcontrol->private_data;
894
895 *uinfo = ue->info;
896 return 0;
897}
898
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100899static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
900 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 struct user_element *ue = kcontrol->private_data;
903
904 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
905 return 0;
906}
907
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100908static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
909 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 int change;
912 struct user_element *ue = kcontrol->private_data;
913
914 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
915 if (change)
916 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
917 return change;
918}
919
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100920static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922 kfree(kcontrol->private_data);
923}
924
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100925static int snd_ctl_elem_add(struct snd_ctl_file *file,
926 struct snd_ctl_elem_info *info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100928 struct snd_card *card = file->card;
929 struct snd_kcontrol kctl, *_kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 unsigned int access;
931 long private_size;
932 struct user_element *ue;
933 int idx, err;
934
935 if (card->user_ctl_count >= MAX_USER_CONTROLS)
936 return -ENOMEM;
937 if (info->count > 1024)
938 return -EINVAL;
939 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100940 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
941 SNDRV_CTL_ELEM_ACCESS_INACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 info->id.numid = 0;
943 memset(&kctl, 0, sizeof(kctl));
944 down_write(&card->controls_rwsem);
945 _kctl = snd_ctl_find_id(card, &info->id);
946 err = 0;
947 if (_kctl) {
948 if (replace)
949 err = snd_ctl_remove(card, _kctl);
950 else
951 err = -EBUSY;
952 } else {
953 if (replace)
954 err = -ENOENT;
955 }
956 up_write(&card->controls_rwsem);
957 if (err < 0)
958 return err;
959 memcpy(&kctl.id, &info->id, sizeof(info->id));
960 kctl.count = info->owner ? info->owner : 1;
961 access |= SNDRV_CTL_ELEM_ACCESS_USER;
962 kctl.info = snd_ctl_elem_user_info;
963 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
964 kctl.get = snd_ctl_elem_user_get;
965 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
966 kctl.put = snd_ctl_elem_user_put;
967 switch (info->type) {
968 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
969 private_size = sizeof(char);
970 if (info->count > 128)
971 return -EINVAL;
972 break;
973 case SNDRV_CTL_ELEM_TYPE_INTEGER:
974 private_size = sizeof(long);
975 if (info->count > 128)
976 return -EINVAL;
977 break;
978 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
979 private_size = sizeof(long long);
980 if (info->count > 64)
981 return -EINVAL;
982 break;
983 case SNDRV_CTL_ELEM_TYPE_BYTES:
984 private_size = sizeof(unsigned char);
985 if (info->count > 512)
986 return -EINVAL;
987 break;
988 case SNDRV_CTL_ELEM_TYPE_IEC958:
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100989 private_size = sizeof(struct snd_aes_iec958);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (info->count != 1)
991 return -EINVAL;
992 break;
993 default:
994 return -EINVAL;
995 }
996 private_size *= info->count;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200997 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (ue == NULL)
999 return -ENOMEM;
1000 ue->info = *info;
1001 ue->elem_data = (char *)ue + sizeof(*ue);
1002 ue->elem_data_size = private_size;
1003 kctl.private_free = snd_ctl_elem_user_free;
1004 _kctl = snd_ctl_new(&kctl, access);
1005 if (_kctl == NULL) {
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001006 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return -ENOMEM;
1008 }
1009 _kctl->private_data = ue;
1010 for (idx = 0; idx < _kctl->count; idx++)
1011 _kctl->vd[idx].owner = file;
1012 err = snd_ctl_add(card, _kctl);
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001013 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 down_write(&card->controls_rwsem);
1017 card->user_ctl_count++;
1018 up_write(&card->controls_rwsem);
1019
1020 return 0;
1021}
1022
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001023static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1024 struct snd_ctl_elem_info __user *_info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001026 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (copy_from_user(&info, _info, sizeof(info)))
1028 return -EFAULT;
1029 return snd_ctl_elem_add(file, &info, replace);
1030}
1031
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001032static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1033 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001035 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 int err;
1037
1038 if (copy_from_user(&id, _id, sizeof(id)))
1039 return -EFAULT;
1040 err = snd_ctl_remove_unlocked_id(file, &id);
1041 if (! err) {
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001042 struct snd_card *card = file->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 down_write(&card->controls_rwsem);
1044 card->user_ctl_count--;
1045 up_write(&card->controls_rwsem);
1046 }
1047 return err;
1048}
1049
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001050static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 int subscribe;
1053 if (get_user(subscribe, ptr))
1054 return -EFAULT;
1055 if (subscribe < 0) {
1056 subscribe = file->subscribed;
1057 if (put_user(subscribe, ptr))
1058 return -EFAULT;
1059 return 0;
1060 }
1061 if (subscribe) {
1062 file->subscribed = 1;
1063 return 0;
1064 } else if (file->subscribed) {
1065 snd_ctl_empty_read_queue(file);
1066 file->subscribed = 0;
1067 }
1068 return 0;
1069}
1070
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001071static int snd_ctl_tlv_read(struct snd_card *card,
1072 struct snd_ctl_tlv __user *_tlv)
1073{
1074 struct snd_ctl_tlv tlv;
1075 struct snd_kcontrol *kctl;
1076 unsigned int len;
1077 int err = 0;
1078
1079 if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
1080 return -EFAULT;
1081 if (tlv.length < sizeof(unsigned int) * 3)
1082 return -EINVAL;
1083 down_read(&card->controls_rwsem);
1084 kctl = snd_ctl_find_numid(card, tlv.numid);
1085 if (kctl == NULL) {
1086 err = -ENOENT;
1087 goto __kctl_end;
1088 }
1089 if (kctl->tlv == NULL) {
1090 err = -ENXIO;
1091 goto __kctl_end;
1092 }
1093 len = kctl->tlv[1] + 2 * sizeof(unsigned int);
1094 if (tlv.length < len) {
1095 err = -ENOMEM;
1096 goto __kctl_end;
1097 }
1098 if (copy_to_user(_tlv->tlv, kctl->tlv, len))
1099 err = -EFAULT;
1100 __kctl_end:
1101 up_read(&card->controls_rwsem);
1102 return err;
1103}
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1106{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001107 struct snd_ctl_file *ctl;
1108 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001110 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 void __user *argp = (void __user *)arg;
1112 int __user *ip = argp;
1113 int err;
1114
1115 ctl = file->private_data;
1116 card = ctl->card;
1117 snd_assert(card != NULL, return -ENXIO);
1118 switch (cmd) {
1119 case SNDRV_CTL_IOCTL_PVERSION:
1120 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1121 case SNDRV_CTL_IOCTL_CARD_INFO:
1122 return snd_ctl_card_info(card, ctl, cmd, argp);
1123 case SNDRV_CTL_IOCTL_ELEM_LIST:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001124 return snd_ctl_elem_list(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 case SNDRV_CTL_IOCTL_ELEM_INFO:
1126 return snd_ctl_elem_info_user(ctl, argp);
1127 case SNDRV_CTL_IOCTL_ELEM_READ:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001128 return snd_ctl_elem_read_user(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1130 return snd_ctl_elem_write_user(ctl, argp);
1131 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1132 return snd_ctl_elem_lock(ctl, argp);
1133 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1134 return snd_ctl_elem_unlock(ctl, argp);
1135 case SNDRV_CTL_IOCTL_ELEM_ADD:
1136 return snd_ctl_elem_add_user(ctl, argp, 0);
1137 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1138 return snd_ctl_elem_add_user(ctl, argp, 1);
1139 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1140 return snd_ctl_elem_remove(ctl, argp);
1141 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1142 return snd_ctl_subscribe_events(ctl, ip);
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001143 case SNDRV_CTL_IOCTL_TLV_READ:
1144 return snd_ctl_tlv_read(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 case SNDRV_CTL_IOCTL_POWER:
Takashi Iwaia381a7a2005-11-17 15:55:49 +01001146 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 case SNDRV_CTL_IOCTL_POWER_STATE:
1148#ifdef CONFIG_PM
1149 return put_user(card->power_state, ip) ? -EFAULT : 0;
1150#else
1151 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1152#endif
1153 }
1154 down_read(&snd_ioctl_rwsem);
1155 list_for_each(list, &snd_control_ioctls) {
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001156 p = list_entry(list, struct snd_kctl_ioctl, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 err = p->fioctl(card, ctl, cmd, arg);
1158 if (err != -ENOIOCTLCMD) {
1159 up_read(&snd_ioctl_rwsem);
1160 return err;
1161 }
1162 }
1163 up_read(&snd_ioctl_rwsem);
Takashi Iwai6d85be62005-05-15 14:32:50 +02001164 snd_printdd("unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return -ENOTTY;
1166}
1167
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001168static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1169 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001171 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 int err = 0;
1173 ssize_t result = 0;
1174
1175 ctl = file->private_data;
1176 snd_assert(ctl != NULL && ctl->card != NULL, return -ENXIO);
1177 if (!ctl->subscribed)
1178 return -EBADFD;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001179 if (count < sizeof(struct snd_ctl_event))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return -EINVAL;
1181 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001182 while (count >= sizeof(struct snd_ctl_event)) {
1183 struct snd_ctl_event ev;
1184 struct snd_kctl_event *kev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 while (list_empty(&ctl->events)) {
1186 wait_queue_t wait;
1187 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1188 err = -EAGAIN;
1189 goto __end_lock;
1190 }
1191 init_waitqueue_entry(&wait, current);
1192 add_wait_queue(&ctl->change_sleep, &wait);
1193 set_current_state(TASK_INTERRUPTIBLE);
1194 spin_unlock_irq(&ctl->read_lock);
1195 schedule();
1196 remove_wait_queue(&ctl->change_sleep, &wait);
1197 if (signal_pending(current))
1198 return result > 0 ? result : -ERESTARTSYS;
1199 spin_lock_irq(&ctl->read_lock);
1200 }
1201 kev = snd_kctl_event(ctl->events.next);
1202 ev.type = SNDRV_CTL_EVENT_ELEM;
1203 ev.data.elem.mask = kev->mask;
1204 ev.data.elem.id = kev->id;
1205 list_del(&kev->list);
1206 spin_unlock_irq(&ctl->read_lock);
1207 kfree(kev);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001208 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 err = -EFAULT;
1210 goto __end;
1211 }
1212 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001213 buffer += sizeof(struct snd_ctl_event);
1214 count -= sizeof(struct snd_ctl_event);
1215 result += sizeof(struct snd_ctl_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217 __end_lock:
1218 spin_unlock_irq(&ctl->read_lock);
1219 __end:
1220 return result > 0 ? result : err;
1221}
1222
1223static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1224{
1225 unsigned int mask;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001226 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 ctl = file->private_data;
1229 if (!ctl->subscribed)
1230 return 0;
1231 poll_wait(file, &ctl->change_sleep, wait);
1232
1233 mask = 0;
1234 if (!list_empty(&ctl->events))
1235 mask |= POLLIN | POLLRDNORM;
1236
1237 return mask;
1238}
1239
1240/*
1241 * register the device-specific control-ioctls.
1242 * called from each device manager like pcm.c, hwdep.c, etc.
1243 */
1244static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1245{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001246 struct snd_kctl_ioctl *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001248 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (pn == NULL)
1250 return -ENOMEM;
1251 pn->fioctl = fcn;
1252 down_write(&snd_ioctl_rwsem);
1253 list_add_tail(&pn->list, lists);
1254 up_write(&snd_ioctl_rwsem);
1255 return 0;
1256}
1257
1258int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1259{
1260 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1261}
1262
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001263EXPORT_SYMBOL(snd_ctl_register_ioctl);
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265#ifdef CONFIG_COMPAT
1266int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1267{
1268 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1269}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001270
1271EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272#endif
1273
1274/*
1275 * de-register the device-specific control-ioctls.
1276 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001277static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1278 struct list_head *lists)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001281 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Takashi Iwai7c22f1a2005-10-10 11:46:31 +02001283 snd_assert(fcn != NULL, return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 down_write(&snd_ioctl_rwsem);
1285 list_for_each(list, lists) {
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001286 p = list_entry(list, struct snd_kctl_ioctl, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (p->fioctl == fcn) {
1288 list_del(&p->list);
1289 up_write(&snd_ioctl_rwsem);
1290 kfree(p);
1291 return 0;
1292 }
1293 }
1294 up_write(&snd_ioctl_rwsem);
1295 snd_BUG();
1296 return -EINVAL;
1297}
1298
1299int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1300{
1301 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1302}
1303
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001304EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306#ifdef CONFIG_COMPAT
1307int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1308{
1309 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1310}
1311
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001312EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313#endif
1314
1315static int snd_ctl_fasync(int fd, struct file * file, int on)
1316{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001317 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 int err;
1319 ctl = file->private_data;
1320 err = fasync_helper(fd, file, on, &ctl->fasync);
1321 if (err < 0)
1322 return err;
1323 return 0;
1324}
1325
1326/*
1327 * ioctl32 compat
1328 */
1329#ifdef CONFIG_COMPAT
1330#include "control_compat.c"
1331#else
1332#define snd_ctl_ioctl_compat NULL
1333#endif
1334
1335/*
1336 * INIT PART
1337 */
1338
1339static struct file_operations snd_ctl_f_ops =
1340{
1341 .owner = THIS_MODULE,
1342 .read = snd_ctl_read,
1343 .open = snd_ctl_open,
1344 .release = snd_ctl_release,
1345 .poll = snd_ctl_poll,
1346 .unlocked_ioctl = snd_ctl_ioctl,
1347 .compat_ioctl = snd_ctl_ioctl_compat,
1348 .fasync = snd_ctl_fasync,
1349};
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351/*
1352 * registration of the control device
1353 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001354static int snd_ctl_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001356 struct snd_card *card = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 int err, cardnum;
1358 char name[16];
1359
1360 snd_assert(card != NULL, return -ENXIO);
1361 cardnum = card->number;
1362 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1363 sprintf(name, "controlC%i", cardnum);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001364 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1365 &snd_ctl_f_ops, card, name)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return err;
1367 return 0;
1368}
1369
1370/*
1371 * disconnection of the control device
1372 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001373static int snd_ctl_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001375 struct snd_card *card = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 struct list_head *flist;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001377 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 down_read(&card->controls_rwsem);
1380 list_for_each(flist, &card->ctl_files) {
1381 ctl = snd_ctl_file(flist);
1382 wake_up(&ctl->change_sleep);
1383 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1384 }
1385 up_read(&card->controls_rwsem);
1386 return 0;
1387}
1388
1389/*
1390 * free all controls
1391 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001392static int snd_ctl_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001394 struct snd_card *card = device->device_data;
1395 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 down_write(&card->controls_rwsem);
1398 while (!list_empty(&card->controls)) {
1399 control = snd_kcontrol(card->controls.next);
1400 snd_ctl_remove(card, control);
1401 }
1402 up_write(&card->controls_rwsem);
1403 return 0;
1404}
1405
1406/*
1407 * de-registration of the control device
1408 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001409static int snd_ctl_dev_unregister(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001411 struct snd_card *card = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 int err, cardnum;
1413
1414 snd_assert(card != NULL, return -ENXIO);
1415 cardnum = card->number;
1416 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
Clemens Ladisch2af677f2005-11-20 14:03:48 +01001417 if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
1418 card, -1)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 return err;
1420 return snd_ctl_dev_free(device);
1421}
1422
1423/*
1424 * create control core:
1425 * called from init.c
1426 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001427int snd_ctl_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001429 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 .dev_free = snd_ctl_dev_free,
1431 .dev_register = snd_ctl_dev_register,
1432 .dev_disconnect = snd_ctl_dev_disconnect,
1433 .dev_unregister = snd_ctl_dev_unregister
1434 };
1435
1436 snd_assert(card != NULL, return -ENXIO);
1437 return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
1438}