blob: bb397eaa718793cb977687628eed02a9d4477907 [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;
244 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{
273 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100274 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 list_for_each(list, &card->controls) {
277 kctl = snd_kcontrol(list);
278 if ((kctl->id.numid <= card->last_numid &&
279 kctl->id.numid + kctl->count > card->last_numid) ||
280 (kctl->id.numid <= card->last_numid + count - 1 &&
281 kctl->id.numid + kctl->count > card->last_numid + count - 1))
282 return card->last_numid = kctl->id.numid + kctl->count - 1;
283 }
284 return card->last_numid;
285}
286
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100287static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 unsigned int last_numid, iter = 100000;
290
291 last_numid = card->last_numid;
292 while (last_numid != snd_ctl_hole_check(card, count)) {
293 if (--iter == 0) {
294 /* this situation is very unlikely */
295 snd_printk(KERN_ERR "unable to allocate new control numid\n");
296 return -ENOMEM;
297 }
298 last_numid = card->last_numid;
299 }
300 return 0;
301}
302
303/**
304 * snd_ctl_add - add the control instance to the card
305 * @card: the card instance
306 * @kcontrol: the control instance to add
307 *
308 * Adds the control instance created via snd_ctl_new() or
309 * snd_ctl_new1() to the given card. Assigns also an unique
310 * numid used for fast search.
311 *
312 * Returns zero if successful, or a negative error code on failure.
313 *
314 * It frees automatically the control which cannot be added.
315 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100316int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100318 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 unsigned int idx;
Takashi Iwaic6077b32006-03-21 16:07:13 +0100320 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100322 if (! kcontrol)
Takashi Iwaic6077b32006-03-21 16:07:13 +0100323 return err;
324 snd_assert(card != NULL, goto error);
325 snd_assert(kcontrol->info != NULL, goto error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 id = kcontrol->id;
327 down_write(&card->controls_rwsem);
328 if (snd_ctl_find_id(card, &id)) {
329 up_write(&card->controls_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
331 id.iface,
332 id.device,
333 id.subdevice,
334 id.name,
335 id.index);
Takashi Iwaic6077b32006-03-21 16:07:13 +0100336 err = -EBUSY;
337 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
340 up_write(&card->controls_rwsem);
Takashi Iwaic6077b32006-03-21 16:07:13 +0100341 err = -ENOMEM;
342 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344 list_add_tail(&kcontrol->list, &card->controls);
345 card->controls_count += kcontrol->count;
346 kcontrol->id.numid = card->last_numid + 1;
347 card->last_numid += kcontrol->count;
348 up_write(&card->controls_rwsem);
349 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
350 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
351 return 0;
Takashi Iwaic6077b32006-03-21 16:07:13 +0100352
353 error:
354 snd_ctl_free_one(kcontrol);
355 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200358EXPORT_SYMBOL(snd_ctl_add);
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/**
361 * snd_ctl_remove - remove the control from the card and release it
362 * @card: the card instance
363 * @kcontrol: the control instance to remove
364 *
365 * Removes the control from the card and then releases the instance.
366 * You don't need to call snd_ctl_free_one(). You must be in
367 * the write lock - down_write(&card->controls_rwsem).
368 *
369 * Returns 0 if successful, or a negative error code on failure.
370 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100371int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100373 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 unsigned int idx;
375
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200376 snd_assert(card != NULL && kcontrol != NULL, return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 list_del(&kcontrol->list);
378 card->controls_count -= kcontrol->count;
379 id = kcontrol->id;
380 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
381 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
382 snd_ctl_free_one(kcontrol);
383 return 0;
384}
385
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200386EXPORT_SYMBOL(snd_ctl_remove);
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388/**
389 * snd_ctl_remove_id - remove the control of the given id and release it
390 * @card: the card instance
391 * @id: the control id to remove
392 *
393 * Finds the control instance with the given id, removes it from the
394 * card list and releases it.
395 *
396 * Returns 0 if successful, or a negative error code on failure.
397 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100398int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100400 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 int ret;
402
403 down_write(&card->controls_rwsem);
404 kctl = snd_ctl_find_id(card, id);
405 if (kctl == NULL) {
406 up_write(&card->controls_rwsem);
407 return -ENOENT;
408 }
409 ret = snd_ctl_remove(card, kctl);
410 up_write(&card->controls_rwsem);
411 return ret;
412}
413
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200414EXPORT_SYMBOL(snd_ctl_remove_id);
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416/**
417 * snd_ctl_remove_unlocked_id - remove the unlocked control of the given id and release it
418 * @file: active control handle
419 * @id: the control id to remove
420 *
421 * Finds the control instance with the given id, removes it from the
422 * card list and releases it.
423 *
424 * Returns 0 if successful, or a negative error code on failure.
425 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100426static int snd_ctl_remove_unlocked_id(struct snd_ctl_file * file,
427 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100429 struct snd_card *card = file->card;
430 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 int idx, ret;
432
433 down_write(&card->controls_rwsem);
434 kctl = snd_ctl_find_id(card, id);
435 if (kctl == NULL) {
436 up_write(&card->controls_rwsem);
437 return -ENOENT;
438 }
439 for (idx = 0; idx < kctl->count; idx++)
440 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
441 up_write(&card->controls_rwsem);
442 return -EBUSY;
443 }
444 ret = snd_ctl_remove(card, kctl);
445 up_write(&card->controls_rwsem);
446 return ret;
447}
448
449/**
450 * snd_ctl_rename_id - replace the id of a control on the card
451 * @card: the card instance
452 * @src_id: the old id
453 * @dst_id: the new id
454 *
455 * Finds the control with the old id from the card, and replaces the
456 * id with the new one.
457 *
458 * Returns zero if successful, or a negative error code on failure.
459 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100460int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
461 struct snd_ctl_elem_id *dst_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100463 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 down_write(&card->controls_rwsem);
466 kctl = snd_ctl_find_id(card, src_id);
467 if (kctl == NULL) {
468 up_write(&card->controls_rwsem);
469 return -ENOENT;
470 }
471 kctl->id = *dst_id;
472 kctl->id.numid = card->last_numid + 1;
473 card->last_numid += kctl->count;
474 up_write(&card->controls_rwsem);
475 return 0;
476}
477
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200478EXPORT_SYMBOL(snd_ctl_rename_id);
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480/**
481 * snd_ctl_find_numid - find the control instance with the given number-id
482 * @card: the card instance
483 * @numid: the number-id to search
484 *
485 * Finds the control instance with the given number-id from the card.
486 *
487 * Returns the pointer of the instance if found, or NULL if not.
488 *
489 * The caller must down card->controls_rwsem before calling this function
490 * (if the race condition can happen).
491 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100492struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100495 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200497 snd_assert(card != NULL && numid != 0, return NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 list_for_each(list, &card->controls) {
499 kctl = snd_kcontrol(list);
500 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
501 return kctl;
502 }
503 return NULL;
504}
505
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200506EXPORT_SYMBOL(snd_ctl_find_numid);
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508/**
509 * snd_ctl_find_id - find the control instance with the given id
510 * @card: the card instance
511 * @id: the id to search
512 *
513 * Finds the control instance with the given id from the card.
514 *
515 * Returns the pointer of the instance if found, or NULL if not.
516 *
517 * The caller must down card->controls_rwsem before calling this function
518 * (if the race condition can happen).
519 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100520struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
521 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100524 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200526 snd_assert(card != NULL && id != NULL, return NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (id->numid != 0)
528 return snd_ctl_find_numid(card, id->numid);
529 list_for_each(list, &card->controls) {
530 kctl = snd_kcontrol(list);
531 if (kctl->id.iface != id->iface)
532 continue;
533 if (kctl->id.device != id->device)
534 continue;
535 if (kctl->id.subdevice != id->subdevice)
536 continue;
537 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
538 continue;
539 if (kctl->id.index > id->index)
540 continue;
541 if (kctl->id.index + kctl->count <= id->index)
542 continue;
543 return kctl;
544 }
545 return NULL;
546}
547
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200548EXPORT_SYMBOL(snd_ctl_find_id);
549
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100550static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 unsigned int cmd, void __user *arg)
552{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100553 struct snd_ctl_card_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Takashi Iwaica2c0962005-09-09 14:20:23 +0200555 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (! info)
557 return -ENOMEM;
558 down_read(&snd_ioctl_rwsem);
559 info->card = card->number;
560 strlcpy(info->id, card->id, sizeof(info->id));
561 strlcpy(info->driver, card->driver, sizeof(info->driver));
562 strlcpy(info->name, card->shortname, sizeof(info->name));
563 strlcpy(info->longname, card->longname, sizeof(info->longname));
564 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
565 strlcpy(info->components, card->components, sizeof(info->components));
566 up_read(&snd_ioctl_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100567 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 kfree(info);
569 return -EFAULT;
570 }
571 kfree(info);
572 return 0;
573}
574
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100575static int snd_ctl_elem_list(struct snd_card *card,
576 struct snd_ctl_elem_list __user *_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 struct list_head *plist;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100579 struct snd_ctl_elem_list list;
580 struct snd_kcontrol *kctl;
581 struct snd_ctl_elem_id *dst, *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 unsigned int offset, space, first, jidx;
583
584 if (copy_from_user(&list, _list, sizeof(list)))
585 return -EFAULT;
586 offset = list.offset;
587 space = list.space;
588 first = 0;
589 /* try limit maximum space */
590 if (space > 16384)
591 return -ENOMEM;
592 if (space > 0) {
593 /* allocate temporary buffer for atomic operation */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100594 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (dst == NULL)
596 return -ENOMEM;
597 down_read(&card->controls_rwsem);
598 list.count = card->controls_count;
599 plist = card->controls.next;
600 while (plist != &card->controls) {
601 if (offset == 0)
602 break;
603 kctl = snd_kcontrol(plist);
604 if (offset < kctl->count)
605 break;
606 offset -= kctl->count;
607 plist = plist->next;
608 }
609 list.used = 0;
610 id = dst;
611 while (space > 0 && plist != &card->controls) {
612 kctl = snd_kcontrol(plist);
613 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
614 snd_ctl_build_ioff(id, kctl, jidx);
615 id++;
616 space--;
617 list.used++;
618 }
619 plist = plist->next;
620 offset = 0;
621 }
622 up_read(&card->controls_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100623 if (list.used > 0 &&
624 copy_to_user(list.pids, dst,
625 list.used * sizeof(struct snd_ctl_elem_id))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 vfree(dst);
627 return -EFAULT;
628 }
629 vfree(dst);
630 } else {
631 down_read(&card->controls_rwsem);
632 list.count = card->controls_count;
633 up_read(&card->controls_rwsem);
634 }
635 if (copy_to_user(_list, &list, sizeof(list)))
636 return -EFAULT;
637 return 0;
638}
639
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100640static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
641 struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100643 struct snd_card *card = ctl->card;
644 struct snd_kcontrol *kctl;
645 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 unsigned int index_offset;
647 int result;
648
649 down_read(&card->controls_rwsem);
650 kctl = snd_ctl_find_id(card, &info->id);
651 if (kctl == NULL) {
652 up_read(&card->controls_rwsem);
653 return -ENOENT;
654 }
655#ifdef CONFIG_SND_DEBUG
656 info->access = 0;
657#endif
658 result = kctl->info(kctl, info);
659 if (result >= 0) {
660 snd_assert(info->access == 0, );
661 index_offset = snd_ctl_get_ioff(kctl, &info->id);
662 vd = &kctl->vd[index_offset];
663 snd_ctl_build_ioff(&info->id, kctl, index_offset);
664 info->access = vd->access;
665 if (vd->owner) {
666 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
667 if (vd->owner == ctl)
668 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
669 info->owner = vd->owner_pid;
670 } else {
671 info->owner = -1;
672 }
673 }
674 up_read(&card->controls_rwsem);
675 return result;
676}
677
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100678static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
679 struct snd_ctl_elem_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100681 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 int result;
683
684 if (copy_from_user(&info, _info, sizeof(info)))
685 return -EFAULT;
Giuliano Pochini64649402006-03-13 14:11:11 +0100686 snd_power_lock(ctl->card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200687 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100688 if (result >= 0)
689 result = snd_ctl_elem_info(ctl, &info);
690 snd_power_unlock(ctl->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (result >= 0)
692 if (copy_to_user(_info, &info, sizeof(info)))
693 return -EFAULT;
694 return result;
695}
696
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100697int snd_ctl_elem_read(struct snd_card *card, struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100699 struct snd_kcontrol *kctl;
700 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 unsigned int index_offset;
702 int result, indirect;
703
704 down_read(&card->controls_rwsem);
705 kctl = snd_ctl_find_id(card, &control->id);
706 if (kctl == NULL) {
707 result = -ENOENT;
708 } else {
709 index_offset = snd_ctl_get_ioff(kctl, &control->id);
710 vd = &kctl->vd[index_offset];
711 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
712 if (control->indirect != indirect) {
713 result = -EACCES;
714 } else {
715 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) && kctl->get != NULL) {
716 snd_ctl_build_ioff(&control->id, kctl, index_offset);
717 result = kctl->get(kctl, control);
718 } else {
719 result = -EPERM;
720 }
721 }
722 }
723 up_read(&card->controls_rwsem);
724 return result;
725}
726
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200727EXPORT_SYMBOL(snd_ctl_elem_read);
728
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100729static int snd_ctl_elem_read_user(struct snd_card *card,
730 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100732 struct snd_ctl_elem_value *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 int result;
734
735 control = kmalloc(sizeof(*control), GFP_KERNEL);
736 if (control == NULL)
737 return -ENOMEM;
738 if (copy_from_user(control, _control, sizeof(*control))) {
739 kfree(control);
740 return -EFAULT;
741 }
Giuliano Pochini64649402006-03-13 14:11:11 +0100742 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200743 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100744 if (result >= 0)
745 result = snd_ctl_elem_read(card, control);
746 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (result >= 0)
748 if (copy_to_user(_control, control, sizeof(*control)))
749 result = -EFAULT;
750 kfree(control);
751 return result;
752}
753
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100754int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
755 struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100757 struct snd_kcontrol *kctl;
758 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 unsigned int index_offset;
760 int result, indirect;
761
762 down_read(&card->controls_rwsem);
763 kctl = snd_ctl_find_id(card, &control->id);
764 if (kctl == NULL) {
765 result = -ENOENT;
766 } else {
767 index_offset = snd_ctl_get_ioff(kctl, &control->id);
768 vd = &kctl->vd[index_offset];
769 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
770 if (control->indirect != indirect) {
771 result = -EACCES;
772 } else {
773 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
774 kctl->put == NULL ||
775 (file && vd->owner != NULL && vd->owner != file)) {
776 result = -EPERM;
777 } else {
778 snd_ctl_build_ioff(&control->id, kctl, index_offset);
779 result = kctl->put(kctl, control);
780 }
781 if (result > 0) {
782 up_read(&card->controls_rwsem);
783 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &control->id);
784 return 0;
785 }
786 }
787 }
788 up_read(&card->controls_rwsem);
789 return result;
790}
791
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200792EXPORT_SYMBOL(snd_ctl_elem_write);
793
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100794static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
795 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100797 struct snd_ctl_elem_value *control;
Giuliano Pochini64649402006-03-13 14:11:11 +0100798 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 int result;
800
801 control = kmalloc(sizeof(*control), GFP_KERNEL);
802 if (control == NULL)
803 return -ENOMEM;
804 if (copy_from_user(control, _control, sizeof(*control))) {
805 kfree(control);
806 return -EFAULT;
807 }
Giuliano Pochini64649402006-03-13 14:11:11 +0100808 card = file->card;
809 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200810 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100811 if (result >= 0)
812 result = snd_ctl_elem_write(card, file, control);
813 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (result >= 0)
815 if (copy_to_user(_control, control, sizeof(*control)))
816 result = -EFAULT;
817 kfree(control);
818 return result;
819}
820
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100821static int snd_ctl_elem_lock(struct snd_ctl_file *file,
822 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100824 struct snd_card *card = file->card;
825 struct snd_ctl_elem_id id;
826 struct snd_kcontrol *kctl;
827 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 int result;
829
830 if (copy_from_user(&id, _id, sizeof(id)))
831 return -EFAULT;
832 down_write(&card->controls_rwsem);
833 kctl = snd_ctl_find_id(card, &id);
834 if (kctl == NULL) {
835 result = -ENOENT;
836 } else {
837 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
838 if (vd->owner != NULL)
839 result = -EBUSY;
840 else {
841 vd->owner = file;
842 vd->owner_pid = current->pid;
843 result = 0;
844 }
845 }
846 up_write(&card->controls_rwsem);
847 return result;
848}
849
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100850static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
851 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100853 struct snd_card *card = file->card;
854 struct snd_ctl_elem_id id;
855 struct snd_kcontrol *kctl;
856 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 int result;
858
859 if (copy_from_user(&id, _id, sizeof(id)))
860 return -EFAULT;
861 down_write(&card->controls_rwsem);
862 kctl = snd_ctl_find_id(card, &id);
863 if (kctl == NULL) {
864 result = -ENOENT;
865 } else {
866 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
867 if (vd->owner == NULL)
868 result = -EINVAL;
869 else if (vd->owner != file)
870 result = -EPERM;
871 else {
872 vd->owner = NULL;
873 vd->owner_pid = 0;
874 result = 0;
875 }
876 }
877 up_write(&card->controls_rwsem);
878 return result;
879}
880
881struct user_element {
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100882 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 void *elem_data; /* element data */
884 unsigned long elem_data_size; /* size of element data in bytes */
885 void *priv_data; /* private data (like strings for enumerated type) */
886 unsigned long priv_data_size; /* size of private data in bytes */
887};
888
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100889static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
890 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
892 struct user_element *ue = kcontrol->private_data;
893
894 *uinfo = ue->info;
895 return 0;
896}
897
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100898static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
899 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 struct user_element *ue = kcontrol->private_data;
902
903 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
904 return 0;
905}
906
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100907static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
908 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
910 int change;
911 struct user_element *ue = kcontrol->private_data;
912
913 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
914 if (change)
915 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
916 return change;
917}
918
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100919static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 kfree(kcontrol->private_data);
922}
923
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100924static int snd_ctl_elem_add(struct snd_ctl_file *file,
925 struct snd_ctl_elem_info *info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100927 struct snd_card *card = file->card;
928 struct snd_kcontrol kctl, *_kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 unsigned int access;
930 long private_size;
931 struct user_element *ue;
932 int idx, err;
933
934 if (card->user_ctl_count >= MAX_USER_CONTROLS)
935 return -ENOMEM;
936 if (info->count > 1024)
937 return -EINVAL;
938 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100939 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
940 SNDRV_CTL_ELEM_ACCESS_INACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 info->id.numid = 0;
942 memset(&kctl, 0, sizeof(kctl));
943 down_write(&card->controls_rwsem);
944 _kctl = snd_ctl_find_id(card, &info->id);
945 err = 0;
946 if (_kctl) {
947 if (replace)
948 err = snd_ctl_remove(card, _kctl);
949 else
950 err = -EBUSY;
951 } else {
952 if (replace)
953 err = -ENOENT;
954 }
955 up_write(&card->controls_rwsem);
956 if (err < 0)
957 return err;
958 memcpy(&kctl.id, &info->id, sizeof(info->id));
959 kctl.count = info->owner ? info->owner : 1;
960 access |= SNDRV_CTL_ELEM_ACCESS_USER;
961 kctl.info = snd_ctl_elem_user_info;
962 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
963 kctl.get = snd_ctl_elem_user_get;
964 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
965 kctl.put = snd_ctl_elem_user_put;
966 switch (info->type) {
967 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
968 private_size = sizeof(char);
969 if (info->count > 128)
970 return -EINVAL;
971 break;
972 case SNDRV_CTL_ELEM_TYPE_INTEGER:
973 private_size = sizeof(long);
974 if (info->count > 128)
975 return -EINVAL;
976 break;
977 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
978 private_size = sizeof(long long);
979 if (info->count > 64)
980 return -EINVAL;
981 break;
982 case SNDRV_CTL_ELEM_TYPE_BYTES:
983 private_size = sizeof(unsigned char);
984 if (info->count > 512)
985 return -EINVAL;
986 break;
987 case SNDRV_CTL_ELEM_TYPE_IEC958:
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100988 private_size = sizeof(struct snd_aes_iec958);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (info->count != 1)
990 return -EINVAL;
991 break;
992 default:
993 return -EINVAL;
994 }
995 private_size *= info->count;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200996 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (ue == NULL)
998 return -ENOMEM;
999 ue->info = *info;
1000 ue->elem_data = (char *)ue + sizeof(*ue);
1001 ue->elem_data_size = private_size;
1002 kctl.private_free = snd_ctl_elem_user_free;
1003 _kctl = snd_ctl_new(&kctl, access);
1004 if (_kctl == NULL) {
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001005 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return -ENOMEM;
1007 }
1008 _kctl->private_data = ue;
1009 for (idx = 0; idx < _kctl->count; idx++)
1010 _kctl->vd[idx].owner = file;
1011 err = snd_ctl_add(card, _kctl);
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001012 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 down_write(&card->controls_rwsem);
1016 card->user_ctl_count++;
1017 up_write(&card->controls_rwsem);
1018
1019 return 0;
1020}
1021
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001022static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1023 struct snd_ctl_elem_info __user *_info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001025 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (copy_from_user(&info, _info, sizeof(info)))
1027 return -EFAULT;
1028 return snd_ctl_elem_add(file, &info, replace);
1029}
1030
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001031static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1032 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001034 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 int err;
1036
1037 if (copy_from_user(&id, _id, sizeof(id)))
1038 return -EFAULT;
1039 err = snd_ctl_remove_unlocked_id(file, &id);
1040 if (! err) {
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001041 struct snd_card *card = file->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 down_write(&card->controls_rwsem);
1043 card->user_ctl_count--;
1044 up_write(&card->controls_rwsem);
1045 }
1046 return err;
1047}
1048
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001049static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
1051 int subscribe;
1052 if (get_user(subscribe, ptr))
1053 return -EFAULT;
1054 if (subscribe < 0) {
1055 subscribe = file->subscribed;
1056 if (put_user(subscribe, ptr))
1057 return -EFAULT;
1058 return 0;
1059 }
1060 if (subscribe) {
1061 file->subscribed = 1;
1062 return 0;
1063 } else if (file->subscribed) {
1064 snd_ctl_empty_read_queue(file);
1065 file->subscribed = 0;
1066 }
1067 return 0;
1068}
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1071{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001072 struct snd_ctl_file *ctl;
1073 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001075 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 void __user *argp = (void __user *)arg;
1077 int __user *ip = argp;
1078 int err;
1079
1080 ctl = file->private_data;
1081 card = ctl->card;
1082 snd_assert(card != NULL, return -ENXIO);
1083 switch (cmd) {
1084 case SNDRV_CTL_IOCTL_PVERSION:
1085 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1086 case SNDRV_CTL_IOCTL_CARD_INFO:
1087 return snd_ctl_card_info(card, ctl, cmd, argp);
1088 case SNDRV_CTL_IOCTL_ELEM_LIST:
1089 return snd_ctl_elem_list(ctl->card, argp);
1090 case SNDRV_CTL_IOCTL_ELEM_INFO:
1091 return snd_ctl_elem_info_user(ctl, argp);
1092 case SNDRV_CTL_IOCTL_ELEM_READ:
1093 return snd_ctl_elem_read_user(ctl->card, argp);
1094 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1095 return snd_ctl_elem_write_user(ctl, argp);
1096 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1097 return snd_ctl_elem_lock(ctl, argp);
1098 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1099 return snd_ctl_elem_unlock(ctl, argp);
1100 case SNDRV_CTL_IOCTL_ELEM_ADD:
1101 return snd_ctl_elem_add_user(ctl, argp, 0);
1102 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1103 return snd_ctl_elem_add_user(ctl, argp, 1);
1104 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1105 return snd_ctl_elem_remove(ctl, argp);
1106 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1107 return snd_ctl_subscribe_events(ctl, ip);
1108 case SNDRV_CTL_IOCTL_POWER:
Takashi Iwaia381a7a2005-11-17 15:55:49 +01001109 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 case SNDRV_CTL_IOCTL_POWER_STATE:
1111#ifdef CONFIG_PM
1112 return put_user(card->power_state, ip) ? -EFAULT : 0;
1113#else
1114 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1115#endif
1116 }
1117 down_read(&snd_ioctl_rwsem);
1118 list_for_each(list, &snd_control_ioctls) {
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001119 p = list_entry(list, struct snd_kctl_ioctl, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 err = p->fioctl(card, ctl, cmd, arg);
1121 if (err != -ENOIOCTLCMD) {
1122 up_read(&snd_ioctl_rwsem);
1123 return err;
1124 }
1125 }
1126 up_read(&snd_ioctl_rwsem);
Takashi Iwai6d85be62005-05-15 14:32:50 +02001127 snd_printdd("unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return -ENOTTY;
1129}
1130
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001131static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1132 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001134 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 int err = 0;
1136 ssize_t result = 0;
1137
1138 ctl = file->private_data;
1139 snd_assert(ctl != NULL && ctl->card != NULL, return -ENXIO);
1140 if (!ctl->subscribed)
1141 return -EBADFD;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001142 if (count < sizeof(struct snd_ctl_event))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return -EINVAL;
1144 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001145 while (count >= sizeof(struct snd_ctl_event)) {
1146 struct snd_ctl_event ev;
1147 struct snd_kctl_event *kev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 while (list_empty(&ctl->events)) {
1149 wait_queue_t wait;
1150 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1151 err = -EAGAIN;
1152 goto __end_lock;
1153 }
1154 init_waitqueue_entry(&wait, current);
1155 add_wait_queue(&ctl->change_sleep, &wait);
1156 set_current_state(TASK_INTERRUPTIBLE);
1157 spin_unlock_irq(&ctl->read_lock);
1158 schedule();
1159 remove_wait_queue(&ctl->change_sleep, &wait);
1160 if (signal_pending(current))
1161 return result > 0 ? result : -ERESTARTSYS;
1162 spin_lock_irq(&ctl->read_lock);
1163 }
1164 kev = snd_kctl_event(ctl->events.next);
1165 ev.type = SNDRV_CTL_EVENT_ELEM;
1166 ev.data.elem.mask = kev->mask;
1167 ev.data.elem.id = kev->id;
1168 list_del(&kev->list);
1169 spin_unlock_irq(&ctl->read_lock);
1170 kfree(kev);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001171 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 err = -EFAULT;
1173 goto __end;
1174 }
1175 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001176 buffer += sizeof(struct snd_ctl_event);
1177 count -= sizeof(struct snd_ctl_event);
1178 result += sizeof(struct snd_ctl_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
1180 __end_lock:
1181 spin_unlock_irq(&ctl->read_lock);
1182 __end:
1183 return result > 0 ? result : err;
1184}
1185
1186static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1187{
1188 unsigned int mask;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001189 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 ctl = file->private_data;
1192 if (!ctl->subscribed)
1193 return 0;
1194 poll_wait(file, &ctl->change_sleep, wait);
1195
1196 mask = 0;
1197 if (!list_empty(&ctl->events))
1198 mask |= POLLIN | POLLRDNORM;
1199
1200 return mask;
1201}
1202
1203/*
1204 * register the device-specific control-ioctls.
1205 * called from each device manager like pcm.c, hwdep.c, etc.
1206 */
1207static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1208{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001209 struct snd_kctl_ioctl *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001211 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (pn == NULL)
1213 return -ENOMEM;
1214 pn->fioctl = fcn;
1215 down_write(&snd_ioctl_rwsem);
1216 list_add_tail(&pn->list, lists);
1217 up_write(&snd_ioctl_rwsem);
1218 return 0;
1219}
1220
1221int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1222{
1223 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1224}
1225
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001226EXPORT_SYMBOL(snd_ctl_register_ioctl);
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228#ifdef CONFIG_COMPAT
1229int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1230{
1231 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1232}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001233
1234EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235#endif
1236
1237/*
1238 * de-register the device-specific control-ioctls.
1239 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001240static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1241 struct list_head *lists)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
1243 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001244 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Takashi Iwai7c22f1a2005-10-10 11:46:31 +02001246 snd_assert(fcn != NULL, return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 down_write(&snd_ioctl_rwsem);
1248 list_for_each(list, lists) {
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001249 p = list_entry(list, struct snd_kctl_ioctl, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (p->fioctl == fcn) {
1251 list_del(&p->list);
1252 up_write(&snd_ioctl_rwsem);
1253 kfree(p);
1254 return 0;
1255 }
1256 }
1257 up_write(&snd_ioctl_rwsem);
1258 snd_BUG();
1259 return -EINVAL;
1260}
1261
1262int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1263{
1264 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1265}
1266
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001267EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269#ifdef CONFIG_COMPAT
1270int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1271{
1272 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1273}
1274
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001275EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276#endif
1277
1278static int snd_ctl_fasync(int fd, struct file * file, int on)
1279{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001280 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 int err;
1282 ctl = file->private_data;
1283 err = fasync_helper(fd, file, on, &ctl->fasync);
1284 if (err < 0)
1285 return err;
1286 return 0;
1287}
1288
1289/*
1290 * ioctl32 compat
1291 */
1292#ifdef CONFIG_COMPAT
1293#include "control_compat.c"
1294#else
1295#define snd_ctl_ioctl_compat NULL
1296#endif
1297
1298/*
1299 * INIT PART
1300 */
1301
1302static struct file_operations snd_ctl_f_ops =
1303{
1304 .owner = THIS_MODULE,
1305 .read = snd_ctl_read,
1306 .open = snd_ctl_open,
1307 .release = snd_ctl_release,
1308 .poll = snd_ctl_poll,
1309 .unlocked_ioctl = snd_ctl_ioctl,
1310 .compat_ioctl = snd_ctl_ioctl_compat,
1311 .fasync = snd_ctl_fasync,
1312};
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314/*
1315 * registration of the control device
1316 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001317static int snd_ctl_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001319 struct snd_card *card = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 int err, cardnum;
1321 char name[16];
1322
1323 snd_assert(card != NULL, return -ENXIO);
1324 cardnum = card->number;
1325 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1326 sprintf(name, "controlC%i", cardnum);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001327 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1328 &snd_ctl_f_ops, card, name)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return err;
1330 return 0;
1331}
1332
1333/*
1334 * disconnection of the control device
1335 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001336static int snd_ctl_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001338 struct snd_card *card = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 struct list_head *flist;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001340 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 down_read(&card->controls_rwsem);
1343 list_for_each(flist, &card->ctl_files) {
1344 ctl = snd_ctl_file(flist);
1345 wake_up(&ctl->change_sleep);
1346 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1347 }
1348 up_read(&card->controls_rwsem);
1349 return 0;
1350}
1351
1352/*
1353 * free all controls
1354 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001355static int snd_ctl_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001357 struct snd_card *card = device->device_data;
1358 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 down_write(&card->controls_rwsem);
1361 while (!list_empty(&card->controls)) {
1362 control = snd_kcontrol(card->controls.next);
1363 snd_ctl_remove(card, control);
1364 }
1365 up_write(&card->controls_rwsem);
1366 return 0;
1367}
1368
1369/*
1370 * de-registration of the control device
1371 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001372static int snd_ctl_dev_unregister(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001374 struct snd_card *card = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 int err, cardnum;
1376
1377 snd_assert(card != NULL, return -ENXIO);
1378 cardnum = card->number;
1379 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
Clemens Ladisch2af677f2005-11-20 14:03:48 +01001380 if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
1381 card, -1)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return err;
1383 return snd_ctl_dev_free(device);
1384}
1385
1386/*
1387 * create control core:
1388 * called from init.c
1389 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001390int snd_ctl_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001392 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 .dev_free = snd_ctl_dev_free,
1394 .dev_register = snd_ctl_dev_register,
1395 .dev_disconnect = snd_ctl_dev_disconnect,
1396 .dev_unregister = snd_ctl_dev_unregister
1397 };
1398
1399 snd_assert(card != NULL, return -ENXIO);
1400 return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
1401}