[ALSA] Fix section mismatch errors in ALSA PCI drivers
[linux-2.6/linux-loongson.git] / sound / core / control.c
blobbb397eaa718793cb977687628eed02a9d4477907
1 /*
2 * Routines for driver control interface
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
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.
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.
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
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>
34 /* max number of user-defined controls */
35 #define MAX_USER_CONTROLS 32
37 struct snd_kctl_ioctl {
38 struct list_head list; /* list of all ioctls */
39 snd_kctl_ioctl_func_t fioctl;
42 static DECLARE_RWSEM(snd_ioctl_rwsem);
43 static LIST_HEAD(snd_control_ioctls);
44 #ifdef CONFIG_COMPAT
45 static LIST_HEAD(snd_control_compat_ioctls);
46 #endif
48 static int snd_ctl_open(struct inode *inode, struct file *file)
50 unsigned long flags;
51 struct snd_card *card;
52 struct snd_ctl_file *ctl;
53 int err;
55 card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
56 if (!card) {
57 err = -ENODEV;
58 goto __error1;
60 err = snd_card_file_add(card, file);
61 if (err < 0) {
62 err = -ENODEV;
63 goto __error1;
65 if (!try_module_get(card->module)) {
66 err = -EFAULT;
67 goto __error2;
69 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
70 if (ctl == NULL) {
71 err = -ENOMEM;
72 goto __error;
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;
85 __error:
86 module_put(card->module);
87 __error2:
88 snd_card_file_remove(card, file);
89 __error1:
90 return err;
93 static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
95 struct snd_kctl_event *cread;
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);
103 spin_unlock(&ctl->read_lock);
106 static int snd_ctl_release(struct inode *inode, struct file *file)
108 unsigned long flags;
109 struct list_head *list;
110 struct snd_card *card;
111 struct snd_ctl_file *ctl;
112 struct snd_kcontrol *control;
113 unsigned int idx;
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;
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;
137 void snd_ctl_notify(struct snd_card *card, unsigned int mask,
138 struct snd_ctl_elem_id *id)
140 unsigned long flags;
141 struct list_head *flist;
142 struct snd_ctl_file *ctl;
143 struct snd_kctl_event *ev;
145 snd_assert(card != NULL && id != NULL, return);
146 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;
163 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
164 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");
171 _found:
172 wake_up(&ctl->change_sleep);
173 spin_unlock_irqrestore(&ctl->read_lock, flags);
174 kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
176 read_unlock(&card->ctl_files_rwlock);
179 EXPORT_SYMBOL(snd_ctl_notify);
182 * snd_ctl_new - create a control instance from the template
183 * @control: the control template
184 * @access: the default control access
186 * Allocates a new struct snd_kcontrol instance and copies the given template
187 * to the new instance. It does not copy volatile data (access).
189 * Returns the pointer of the new instance, or NULL on failure.
191 struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control, unsigned int access)
193 struct snd_kcontrol *kctl;
194 unsigned int idx;
196 snd_assert(control != NULL, return NULL);
197 snd_assert(control->count > 0, return NULL);
198 kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL);
199 if (kctl == NULL) {
200 snd_printk(KERN_ERR "Cannot allocate control instance\n");
201 return NULL;
203 *kctl = *control;
204 for (idx = 0; idx < kctl->count; idx++)
205 kctl->vd[idx].access = access;
206 return kctl;
209 EXPORT_SYMBOL(snd_ctl_new);
212 * snd_ctl_new1 - create a control instance from the template
213 * @ncontrol: the initialization record
214 * @private_data: the private data to set
216 * Allocates a new struct snd_kcontrol instance and initialize from the given
217 * 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.
220 * Returns the pointer of the newly generated instance, or NULL on failure.
222 struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
223 void *private_data)
225 struct snd_kcontrol kctl;
226 unsigned int access;
228 snd_assert(ncontrol != NULL, return NULL);
229 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);
249 EXPORT_SYMBOL(snd_ctl_new1);
252 * snd_ctl_free_one - release the control instance
253 * @kcontrol: the control instance
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.
259 void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
261 if (kcontrol) {
262 if (kcontrol->private_free)
263 kcontrol->private_free(kcontrol);
264 kfree(kcontrol);
268 EXPORT_SYMBOL(snd_ctl_free_one);
270 static unsigned int snd_ctl_hole_check(struct snd_card *card,
271 unsigned int count)
273 struct list_head *list;
274 struct snd_kcontrol *kctl;
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;
284 return card->last_numid;
287 static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
289 unsigned int last_numid, iter = 100000;
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;
298 last_numid = card->last_numid;
300 return 0;
304 * snd_ctl_add - add the control instance to the card
305 * @card: the card instance
306 * @kcontrol: the control instance to add
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.
312 * Returns zero if successful, or a negative error code on failure.
314 * It frees automatically the control which cannot be added.
316 int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
318 struct snd_ctl_elem_id id;
319 unsigned int idx;
320 int err = -EINVAL;
322 if (! kcontrol)
323 return err;
324 snd_assert(card != NULL, goto error);
325 snd_assert(kcontrol->info != NULL, goto error);
326 id = kcontrol->id;
327 down_write(&card->controls_rwsem);
328 if (snd_ctl_find_id(card, &id)) {
329 up_write(&card->controls_rwsem);
330 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);
336 err = -EBUSY;
337 goto error;
339 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
340 up_write(&card->controls_rwsem);
341 err = -ENOMEM;
342 goto error;
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;
353 error:
354 snd_ctl_free_one(kcontrol);
355 return err;
358 EXPORT_SYMBOL(snd_ctl_add);
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
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).
369 * Returns 0 if successful, or a negative error code on failure.
371 int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
373 struct snd_ctl_elem_id id;
374 unsigned int idx;
376 snd_assert(card != NULL && kcontrol != NULL, return -EINVAL);
377 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;
386 EXPORT_SYMBOL(snd_ctl_remove);
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
393 * Finds the control instance with the given id, removes it from the
394 * card list and releases it.
396 * Returns 0 if successful, or a negative error code on failure.
398 int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
400 struct snd_kcontrol *kctl;
401 int ret;
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;
409 ret = snd_ctl_remove(card, kctl);
410 up_write(&card->controls_rwsem);
411 return ret;
414 EXPORT_SYMBOL(snd_ctl_remove_id);
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
421 * Finds the control instance with the given id, removes it from the
422 * card list and releases it.
424 * Returns 0 if successful, or a negative error code on failure.
426 static int snd_ctl_remove_unlocked_id(struct snd_ctl_file * file,
427 struct snd_ctl_elem_id *id)
429 struct snd_card *card = file->card;
430 struct snd_kcontrol *kctl;
431 int idx, ret;
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;
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;
444 ret = snd_ctl_remove(card, kctl);
445 up_write(&card->controls_rwsem);
446 return ret;
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
455 * Finds the control with the old id from the card, and replaces the
456 * id with the new one.
458 * Returns zero if successful, or a negative error code on failure.
460 int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
461 struct snd_ctl_elem_id *dst_id)
463 struct snd_kcontrol *kctl;
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;
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;
478 EXPORT_SYMBOL(snd_ctl_rename_id);
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
485 * Finds the control instance with the given number-id from the card.
487 * Returns the pointer of the instance if found, or NULL if not.
489 * The caller must down card->controls_rwsem before calling this function
490 * (if the race condition can happen).
492 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
494 struct list_head *list;
495 struct snd_kcontrol *kctl;
497 snd_assert(card != NULL && numid != 0, return NULL);
498 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;
503 return NULL;
506 EXPORT_SYMBOL(snd_ctl_find_numid);
509 * snd_ctl_find_id - find the control instance with the given id
510 * @card: the card instance
511 * @id: the id to search
513 * Finds the control instance with the given id from the card.
515 * Returns the pointer of the instance if found, or NULL if not.
517 * The caller must down card->controls_rwsem before calling this function
518 * (if the race condition can happen).
520 struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
521 struct snd_ctl_elem_id *id)
523 struct list_head *list;
524 struct snd_kcontrol *kctl;
526 snd_assert(card != NULL && id != NULL, return NULL);
527 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;
545 return NULL;
548 EXPORT_SYMBOL(snd_ctl_find_id);
550 static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
551 unsigned int cmd, void __user *arg)
553 struct snd_ctl_card_info *info;
555 info = kzalloc(sizeof(*info), GFP_KERNEL);
556 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);
567 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
568 kfree(info);
569 return -EFAULT;
571 kfree(info);
572 return 0;
575 static int snd_ctl_elem_list(struct snd_card *card,
576 struct snd_ctl_elem_list __user *_list)
578 struct list_head *plist;
579 struct snd_ctl_elem_list list;
580 struct snd_kcontrol *kctl;
581 struct snd_ctl_elem_id *dst, *id;
582 unsigned int offset, space, first, jidx;
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 */
594 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
595 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;
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++;
619 plist = plist->next;
620 offset = 0;
622 up_read(&card->controls_rwsem);
623 if (list.used > 0 &&
624 copy_to_user(list.pids, dst,
625 list.used * sizeof(struct snd_ctl_elem_id))) {
626 vfree(dst);
627 return -EFAULT;
629 vfree(dst);
630 } else {
631 down_read(&card->controls_rwsem);
632 list.count = card->controls_count;
633 up_read(&card->controls_rwsem);
635 if (copy_to_user(_list, &list, sizeof(list)))
636 return -EFAULT;
637 return 0;
640 static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
641 struct snd_ctl_elem_info *info)
643 struct snd_card *card = ctl->card;
644 struct snd_kcontrol *kctl;
645 struct snd_kcontrol_volatile *vd;
646 unsigned int index_offset;
647 int result;
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;
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;
674 up_read(&card->controls_rwsem);
675 return result;
678 static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
679 struct snd_ctl_elem_info __user *_info)
681 struct snd_ctl_elem_info info;
682 int result;
684 if (copy_from_user(&info, _info, sizeof(info)))
685 return -EFAULT;
686 snd_power_lock(ctl->card);
687 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
688 if (result >= 0)
689 result = snd_ctl_elem_info(ctl, &info);
690 snd_power_unlock(ctl->card);
691 if (result >= 0)
692 if (copy_to_user(_info, &info, sizeof(info)))
693 return -EFAULT;
694 return result;
697 int snd_ctl_elem_read(struct snd_card *card, struct snd_ctl_elem_value *control)
699 struct snd_kcontrol *kctl;
700 struct snd_kcontrol_volatile *vd;
701 unsigned int index_offset;
702 int result, indirect;
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;
723 up_read(&card->controls_rwsem);
724 return result;
727 EXPORT_SYMBOL(snd_ctl_elem_read);
729 static int snd_ctl_elem_read_user(struct snd_card *card,
730 struct snd_ctl_elem_value __user *_control)
732 struct snd_ctl_elem_value *control;
733 int result;
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;
742 snd_power_lock(card);
743 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
744 if (result >= 0)
745 result = snd_ctl_elem_read(card, control);
746 snd_power_unlock(card);
747 if (result >= 0)
748 if (copy_to_user(_control, control, sizeof(*control)))
749 result = -EFAULT;
750 kfree(control);
751 return result;
754 int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
755 struct snd_ctl_elem_value *control)
757 struct snd_kcontrol *kctl;
758 struct snd_kcontrol_volatile *vd;
759 unsigned int index_offset;
760 int result, indirect;
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);
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;
788 up_read(&card->controls_rwsem);
789 return result;
792 EXPORT_SYMBOL(snd_ctl_elem_write);
794 static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
795 struct snd_ctl_elem_value __user *_control)
797 struct snd_ctl_elem_value *control;
798 struct snd_card *card;
799 int result;
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;
808 card = file->card;
809 snd_power_lock(card);
810 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
811 if (result >= 0)
812 result = snd_ctl_elem_write(card, file, control);
813 snd_power_unlock(card);
814 if (result >= 0)
815 if (copy_to_user(_control, control, sizeof(*control)))
816 result = -EFAULT;
817 kfree(control);
818 return result;
821 static int snd_ctl_elem_lock(struct snd_ctl_file *file,
822 struct snd_ctl_elem_id __user *_id)
824 struct snd_card *card = file->card;
825 struct snd_ctl_elem_id id;
826 struct snd_kcontrol *kctl;
827 struct snd_kcontrol_volatile *vd;
828 int result;
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;
846 up_write(&card->controls_rwsem);
847 return result;
850 static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
851 struct snd_ctl_elem_id __user *_id)
853 struct snd_card *card = file->card;
854 struct snd_ctl_elem_id id;
855 struct snd_kcontrol *kctl;
856 struct snd_kcontrol_volatile *vd;
857 int result;
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;
877 up_write(&card->controls_rwsem);
878 return result;
881 struct user_element {
882 struct snd_ctl_elem_info info;
883 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 */
889 static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
890 struct snd_ctl_elem_info *uinfo)
892 struct user_element *ue = kcontrol->private_data;
894 *uinfo = ue->info;
895 return 0;
898 static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
899 struct snd_ctl_elem_value *ucontrol)
901 struct user_element *ue = kcontrol->private_data;
903 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
904 return 0;
907 static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
908 struct snd_ctl_elem_value *ucontrol)
910 int change;
911 struct user_element *ue = kcontrol->private_data;
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;
919 static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
921 kfree(kcontrol->private_data);
924 static int snd_ctl_elem_add(struct snd_ctl_file *file,
925 struct snd_ctl_elem_info *info, int replace)
927 struct snd_card *card = file->card;
928 struct snd_kcontrol kctl, *_kctl;
929 unsigned int access;
930 long private_size;
931 struct user_element *ue;
932 int idx, err;
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 :
939 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
940 SNDRV_CTL_ELEM_ACCESS_INACTIVE));
941 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;
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:
988 private_size = sizeof(struct snd_aes_iec958);
989 if (info->count != 1)
990 return -EINVAL;
991 break;
992 default:
993 return -EINVAL;
995 private_size *= info->count;
996 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
997 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) {
1005 kfree(ue);
1006 return -ENOMEM;
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);
1012 if (err < 0)
1013 return err;
1015 down_write(&card->controls_rwsem);
1016 card->user_ctl_count++;
1017 up_write(&card->controls_rwsem);
1019 return 0;
1022 static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1023 struct snd_ctl_elem_info __user *_info, int replace)
1025 struct snd_ctl_elem_info info;
1026 if (copy_from_user(&info, _info, sizeof(info)))
1027 return -EFAULT;
1028 return snd_ctl_elem_add(file, &info, replace);
1031 static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1032 struct snd_ctl_elem_id __user *_id)
1034 struct snd_ctl_elem_id id;
1035 int err;
1037 if (copy_from_user(&id, _id, sizeof(id)))
1038 return -EFAULT;
1039 err = snd_ctl_remove_unlocked_id(file, &id);
1040 if (! err) {
1041 struct snd_card *card = file->card;
1042 down_write(&card->controls_rwsem);
1043 card->user_ctl_count--;
1044 up_write(&card->controls_rwsem);
1046 return err;
1049 static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
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;
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;
1067 return 0;
1070 static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1072 struct snd_ctl_file *ctl;
1073 struct snd_card *card;
1074 struct list_head *list;
1075 struct snd_kctl_ioctl *p;
1076 void __user *argp = (void __user *)arg;
1077 int __user *ip = argp;
1078 int err;
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:
1109 return -ENOPROTOOPT;
1110 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
1117 down_read(&snd_ioctl_rwsem);
1118 list_for_each(list, &snd_control_ioctls) {
1119 p = list_entry(list, struct snd_kctl_ioctl, list);
1120 err = p->fioctl(card, ctl, cmd, arg);
1121 if (err != -ENOIOCTLCMD) {
1122 up_read(&snd_ioctl_rwsem);
1123 return err;
1126 up_read(&snd_ioctl_rwsem);
1127 snd_printdd("unknown ioctl = 0x%x\n", cmd);
1128 return -ENOTTY;
1131 static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1132 size_t count, loff_t * offset)
1134 struct snd_ctl_file *ctl;
1135 int err = 0;
1136 ssize_t result = 0;
1138 ctl = file->private_data;
1139 snd_assert(ctl != NULL && ctl->card != NULL, return -ENXIO);
1140 if (!ctl->subscribed)
1141 return -EBADFD;
1142 if (count < sizeof(struct snd_ctl_event))
1143 return -EINVAL;
1144 spin_lock_irq(&ctl->read_lock);
1145 while (count >= sizeof(struct snd_ctl_event)) {
1146 struct snd_ctl_event ev;
1147 struct snd_kctl_event *kev;
1148 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;
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);
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);
1171 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
1172 err = -EFAULT;
1173 goto __end;
1175 spin_lock_irq(&ctl->read_lock);
1176 buffer += sizeof(struct snd_ctl_event);
1177 count -= sizeof(struct snd_ctl_event);
1178 result += sizeof(struct snd_ctl_event);
1180 __end_lock:
1181 spin_unlock_irq(&ctl->read_lock);
1182 __end:
1183 return result > 0 ? result : err;
1186 static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1188 unsigned int mask;
1189 struct snd_ctl_file *ctl;
1191 ctl = file->private_data;
1192 if (!ctl->subscribed)
1193 return 0;
1194 poll_wait(file, &ctl->change_sleep, wait);
1196 mask = 0;
1197 if (!list_empty(&ctl->events))
1198 mask |= POLLIN | POLLRDNORM;
1200 return mask;
1204 * register the device-specific control-ioctls.
1205 * called from each device manager like pcm.c, hwdep.c, etc.
1207 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1209 struct snd_kctl_ioctl *pn;
1211 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
1212 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;
1221 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1223 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1226 EXPORT_SYMBOL(snd_ctl_register_ioctl);
1228 #ifdef CONFIG_COMPAT
1229 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1231 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1234 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
1235 #endif
1238 * de-register the device-specific control-ioctls.
1240 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1241 struct list_head *lists)
1243 struct list_head *list;
1244 struct snd_kctl_ioctl *p;
1246 snd_assert(fcn != NULL, return -EINVAL);
1247 down_write(&snd_ioctl_rwsem);
1248 list_for_each(list, lists) {
1249 p = list_entry(list, struct snd_kctl_ioctl, list);
1250 if (p->fioctl == fcn) {
1251 list_del(&p->list);
1252 up_write(&snd_ioctl_rwsem);
1253 kfree(p);
1254 return 0;
1257 up_write(&snd_ioctl_rwsem);
1258 snd_BUG();
1259 return -EINVAL;
1262 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1264 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1267 EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1269 #ifdef CONFIG_COMPAT
1270 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1272 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1275 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
1276 #endif
1278 static int snd_ctl_fasync(int fd, struct file * file, int on)
1280 struct snd_ctl_file *ctl;
1281 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;
1290 * ioctl32 compat
1292 #ifdef CONFIG_COMPAT
1293 #include "control_compat.c"
1294 #else
1295 #define snd_ctl_ioctl_compat NULL
1296 #endif
1299 * INIT PART
1302 static struct file_operations snd_ctl_f_ops =
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,
1315 * registration of the control device
1317 static int snd_ctl_dev_register(struct snd_device *device)
1319 struct snd_card *card = device->device_data;
1320 int err, cardnum;
1321 char name[16];
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);
1327 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1328 &snd_ctl_f_ops, card, name)) < 0)
1329 return err;
1330 return 0;
1334 * disconnection of the control device
1336 static int snd_ctl_dev_disconnect(struct snd_device *device)
1338 struct snd_card *card = device->device_data;
1339 struct list_head *flist;
1340 struct snd_ctl_file *ctl;
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);
1348 up_read(&card->controls_rwsem);
1349 return 0;
1353 * free all controls
1355 static int snd_ctl_dev_free(struct snd_device *device)
1357 struct snd_card *card = device->device_data;
1358 struct snd_kcontrol *control;
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);
1365 up_write(&card->controls_rwsem);
1366 return 0;
1370 * de-registration of the control device
1372 static int snd_ctl_dev_unregister(struct snd_device *device)
1374 struct snd_card *card = device->device_data;
1375 int err, cardnum;
1377 snd_assert(card != NULL, return -ENXIO);
1378 cardnum = card->number;
1379 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1380 if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
1381 card, -1)) < 0)
1382 return err;
1383 return snd_ctl_dev_free(device);
1387 * create control core:
1388 * called from init.c
1390 int snd_ctl_create(struct snd_card *card)
1392 static struct snd_device_ops ops = {
1393 .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
1399 snd_assert(card != NULL, return -ENXIO);
1400 return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);