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);
45 static LIST_HEAD(snd_control_compat_ioctls);
48 static int snd_ctl_open(struct inode *inode, struct file *file)
51 struct snd_card *card;
52 struct snd_ctl_file *ctl;
55 card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
60 err = snd_card_file_add(card, file);
65 if (!try_module_get(card->module)) {
69 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
74 INIT_LIST_HEAD(&ctl->events);
75 init_waitqueue_head(&ctl->change_sleep);
76 spin_lock_init(&ctl->read_lock);
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);
86 module_put(card->module);
88 snd_card_file_remove(card, file);
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);
103 spin_unlock(&ctl->read_lock);
106 static int snd_ctl_release(struct inode *inode, struct file *file)
109 struct list_head *list;
110 struct snd_card *card;
111 struct snd_ctl_file *ctl;
112 struct snd_kcontrol *control;
115 ctl = file->private_data;
116 fasync_helper(-1, file, 0, &ctl->fasync);
117 file->private_data = NULL;
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);
132 module_put(card->module);
133 snd_card_file_remove(card, file);
137 void snd_ctl_notify(struct snd_card *card, unsigned int mask,
138 struct snd_ctl_elem_id *id)
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++;
150 list_for_each(flist, &card->ctl_files) {
151 struct list_head *elist;
152 ctl = snd_ctl_file(flist);
153 if (!ctl->subscribed)
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) {
163 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
167 list_add_tail(&ev->list, &ctl->events);
169 snd_printk(KERN_ERR "No memory available to allocate event\n");
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;
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);
200 snd_printk(KERN_ERR "Cannot allocate control instance\n");
204 for (idx = 0; idx < kctl->count; idx++)
205 kctl->vd[idx].access = access;
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,
225 struct snd_kcontrol kctl;
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;
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|
240 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
241 SNDRV_CTL_ELEM_ACCESS_DINDIRECT|
242 SNDRV_CTL_ELEM_ACCESS_INDIRECT|
243 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE|
244 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK));
245 kctl.info = ncontrol->info;
246 kctl.get = ncontrol->get;
247 kctl.put = ncontrol->put;
248 kctl.tlv.p = ncontrol->tlv.p;
249 kctl.private_value = ncontrol->private_value;
250 kctl.private_data = private_data;
251 return snd_ctl_new(&kctl, access);
254 EXPORT_SYMBOL(snd_ctl_new1);
257 * snd_ctl_free_one - release the control instance
258 * @kcontrol: the control instance
260 * Releases the control instance created via snd_ctl_new()
262 * Don't call this after the control was added to the card.
264 void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
267 if (kcontrol->private_free)
268 kcontrol->private_free(kcontrol);
273 EXPORT_SYMBOL(snd_ctl_free_one);
275 static unsigned int snd_ctl_hole_check(struct snd_card *card,
278 struct list_head *list;
279 struct snd_kcontrol *kctl;
281 list_for_each(list, &card->controls) {
282 kctl = snd_kcontrol(list);
283 if ((kctl->id.numid <= card->last_numid &&
284 kctl->id.numid + kctl->count > card->last_numid) ||
285 (kctl->id.numid <= card->last_numid + count - 1 &&
286 kctl->id.numid + kctl->count > card->last_numid + count - 1))
287 return card->last_numid = kctl->id.numid + kctl->count - 1;
289 return card->last_numid;
292 static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
294 unsigned int last_numid, iter = 100000;
296 last_numid = card->last_numid;
297 while (last_numid != snd_ctl_hole_check(card, count)) {
299 /* this situation is very unlikely */
300 snd_printk(KERN_ERR "unable to allocate new control numid\n");
303 last_numid = card->last_numid;
309 * snd_ctl_add - add the control instance to the card
310 * @card: the card instance
311 * @kcontrol: the control instance to add
313 * Adds the control instance created via snd_ctl_new() or
314 * snd_ctl_new1() to the given card. Assigns also an unique
315 * numid used for fast search.
317 * Returns zero if successful, or a negative error code on failure.
319 * It frees automatically the control which cannot be added.
321 int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
323 struct snd_ctl_elem_id id;
329 snd_assert(card != NULL, goto error);
330 snd_assert(kcontrol->info != NULL, goto error);
332 down_write(&card->controls_rwsem);
333 if (snd_ctl_find_id(card, &id)) {
334 up_write(&card->controls_rwsem);
335 snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
344 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
345 up_write(&card->controls_rwsem);
349 list_add_tail(&kcontrol->list, &card->controls);
350 card->controls_count += kcontrol->count;
351 kcontrol->id.numid = card->last_numid + 1;
352 card->last_numid += kcontrol->count;
353 up_write(&card->controls_rwsem);
354 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
355 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
359 snd_ctl_free_one(kcontrol);
363 EXPORT_SYMBOL(snd_ctl_add);
366 * snd_ctl_remove - remove the control from the card and release it
367 * @card: the card instance
368 * @kcontrol: the control instance to remove
370 * Removes the control from the card and then releases the instance.
371 * You don't need to call snd_ctl_free_one(). You must be in
372 * the write lock - down_write(&card->controls_rwsem).
374 * Returns 0 if successful, or a negative error code on failure.
376 int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
378 struct snd_ctl_elem_id id;
381 snd_assert(card != NULL && kcontrol != NULL, return -EINVAL);
382 list_del(&kcontrol->list);
383 card->controls_count -= kcontrol->count;
385 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
386 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
387 snd_ctl_free_one(kcontrol);
391 EXPORT_SYMBOL(snd_ctl_remove);
394 * snd_ctl_remove_id - remove the control of the given id and release it
395 * @card: the card instance
396 * @id: the control id to remove
398 * Finds the control instance with the given id, removes it from the
399 * card list and releases it.
401 * Returns 0 if successful, or a negative error code on failure.
403 int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
405 struct snd_kcontrol *kctl;
408 down_write(&card->controls_rwsem);
409 kctl = snd_ctl_find_id(card, id);
411 up_write(&card->controls_rwsem);
414 ret = snd_ctl_remove(card, kctl);
415 up_write(&card->controls_rwsem);
419 EXPORT_SYMBOL(snd_ctl_remove_id);
422 * snd_ctl_remove_unlocked_id - remove the unlocked control of the given id and release it
423 * @file: active control handle
424 * @id: the control id to remove
426 * Finds the control instance with the given id, removes it from the
427 * card list and releases it.
429 * Returns 0 if successful, or a negative error code on failure.
431 static int snd_ctl_remove_unlocked_id(struct snd_ctl_file * file,
432 struct snd_ctl_elem_id *id)
434 struct snd_card *card = file->card;
435 struct snd_kcontrol *kctl;
438 down_write(&card->controls_rwsem);
439 kctl = snd_ctl_find_id(card, id);
441 up_write(&card->controls_rwsem);
444 for (idx = 0; idx < kctl->count; idx++)
445 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
446 up_write(&card->controls_rwsem);
449 ret = snd_ctl_remove(card, kctl);
450 up_write(&card->controls_rwsem);
455 * snd_ctl_rename_id - replace the id of a control on the card
456 * @card: the card instance
457 * @src_id: the old id
458 * @dst_id: the new id
460 * Finds the control with the old id from the card, and replaces the
461 * id with the new one.
463 * Returns zero if successful, or a negative error code on failure.
465 int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
466 struct snd_ctl_elem_id *dst_id)
468 struct snd_kcontrol *kctl;
470 down_write(&card->controls_rwsem);
471 kctl = snd_ctl_find_id(card, src_id);
473 up_write(&card->controls_rwsem);
477 kctl->id.numid = card->last_numid + 1;
478 card->last_numid += kctl->count;
479 up_write(&card->controls_rwsem);
483 EXPORT_SYMBOL(snd_ctl_rename_id);
486 * snd_ctl_find_numid - find the control instance with the given number-id
487 * @card: the card instance
488 * @numid: the number-id to search
490 * Finds the control instance with the given number-id from the card.
492 * Returns the pointer of the instance if found, or NULL if not.
494 * The caller must down card->controls_rwsem before calling this function
495 * (if the race condition can happen).
497 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
499 struct list_head *list;
500 struct snd_kcontrol *kctl;
502 snd_assert(card != NULL && numid != 0, return NULL);
503 list_for_each(list, &card->controls) {
504 kctl = snd_kcontrol(list);
505 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
511 EXPORT_SYMBOL(snd_ctl_find_numid);
514 * snd_ctl_find_id - find the control instance with the given id
515 * @card: the card instance
516 * @id: the id to search
518 * Finds the control instance with the given id from the card.
520 * Returns the pointer of the instance if found, or NULL if not.
522 * The caller must down card->controls_rwsem before calling this function
523 * (if the race condition can happen).
525 struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
526 struct snd_ctl_elem_id *id)
528 struct list_head *list;
529 struct snd_kcontrol *kctl;
531 snd_assert(card != NULL && id != NULL, return NULL);
533 return snd_ctl_find_numid(card, id->numid);
534 list_for_each(list, &card->controls) {
535 kctl = snd_kcontrol(list);
536 if (kctl->id.iface != id->iface)
538 if (kctl->id.device != id->device)
540 if (kctl->id.subdevice != id->subdevice)
542 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
544 if (kctl->id.index > id->index)
546 if (kctl->id.index + kctl->count <= id->index)
553 EXPORT_SYMBOL(snd_ctl_find_id);
555 static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
556 unsigned int cmd, void __user *arg)
558 struct snd_ctl_card_info *info;
560 info = kzalloc(sizeof(*info), GFP_KERNEL);
563 down_read(&snd_ioctl_rwsem);
564 info->card = card->number;
565 strlcpy(info->id, card->id, sizeof(info->id));
566 strlcpy(info->driver, card->driver, sizeof(info->driver));
567 strlcpy(info->name, card->shortname, sizeof(info->name));
568 strlcpy(info->longname, card->longname, sizeof(info->longname));
569 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
570 strlcpy(info->components, card->components, sizeof(info->components));
571 up_read(&snd_ioctl_rwsem);
572 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
580 static int snd_ctl_elem_list(struct snd_card *card,
581 struct snd_ctl_elem_list __user *_list)
583 struct list_head *plist;
584 struct snd_ctl_elem_list list;
585 struct snd_kcontrol *kctl;
586 struct snd_ctl_elem_id *dst, *id;
587 unsigned int offset, space, first, jidx;
589 if (copy_from_user(&list, _list, sizeof(list)))
591 offset = list.offset;
594 /* try limit maximum space */
598 /* allocate temporary buffer for atomic operation */
599 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
602 down_read(&card->controls_rwsem);
603 list.count = card->controls_count;
604 plist = card->controls.next;
605 while (plist != &card->controls) {
608 kctl = snd_kcontrol(plist);
609 if (offset < kctl->count)
611 offset -= kctl->count;
616 while (space > 0 && plist != &card->controls) {
617 kctl = snd_kcontrol(plist);
618 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
619 snd_ctl_build_ioff(id, kctl, jidx);
627 up_read(&card->controls_rwsem);
629 copy_to_user(list.pids, dst,
630 list.used * sizeof(struct snd_ctl_elem_id))) {
636 down_read(&card->controls_rwsem);
637 list.count = card->controls_count;
638 up_read(&card->controls_rwsem);
640 if (copy_to_user(_list, &list, sizeof(list)))
645 static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
646 struct snd_ctl_elem_info *info)
648 struct snd_card *card = ctl->card;
649 struct snd_kcontrol *kctl;
650 struct snd_kcontrol_volatile *vd;
651 unsigned int index_offset;
654 down_read(&card->controls_rwsem);
655 kctl = snd_ctl_find_id(card, &info->id);
657 up_read(&card->controls_rwsem);
660 #ifdef CONFIG_SND_DEBUG
663 result = kctl->info(kctl, info);
665 snd_assert(info->access == 0, );
666 index_offset = snd_ctl_get_ioff(kctl, &info->id);
667 vd = &kctl->vd[index_offset];
668 snd_ctl_build_ioff(&info->id, kctl, index_offset);
669 info->access = vd->access;
671 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
672 if (vd->owner == ctl)
673 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
674 info->owner = vd->owner_pid;
679 up_read(&card->controls_rwsem);
683 static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
684 struct snd_ctl_elem_info __user *_info)
686 struct snd_ctl_elem_info info;
689 if (copy_from_user(&info, _info, sizeof(info)))
691 snd_power_lock(ctl->card);
692 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
694 result = snd_ctl_elem_info(ctl, &info);
695 snd_power_unlock(ctl->card);
697 if (copy_to_user(_info, &info, sizeof(info)))
702 int snd_ctl_elem_read(struct snd_card *card, struct snd_ctl_elem_value *control)
704 struct snd_kcontrol *kctl;
705 struct snd_kcontrol_volatile *vd;
706 unsigned int index_offset;
707 int result, indirect;
709 down_read(&card->controls_rwsem);
710 kctl = snd_ctl_find_id(card, &control->id);
714 index_offset = snd_ctl_get_ioff(kctl, &control->id);
715 vd = &kctl->vd[index_offset];
716 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
717 if (control->indirect != indirect) {
720 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) && kctl->get != NULL) {
721 snd_ctl_build_ioff(&control->id, kctl, index_offset);
722 result = kctl->get(kctl, control);
728 up_read(&card->controls_rwsem);
732 EXPORT_SYMBOL(snd_ctl_elem_read);
734 static int snd_ctl_elem_read_user(struct snd_card *card,
735 struct snd_ctl_elem_value __user *_control)
737 struct snd_ctl_elem_value *control;
740 control = kmalloc(sizeof(*control), GFP_KERNEL);
743 if (copy_from_user(control, _control, sizeof(*control))) {
747 snd_power_lock(card);
748 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
750 result = snd_ctl_elem_read(card, control);
751 snd_power_unlock(card);
753 if (copy_to_user(_control, control, sizeof(*control)))
759 int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
760 struct snd_ctl_elem_value *control)
762 struct snd_kcontrol *kctl;
763 struct snd_kcontrol_volatile *vd;
764 unsigned int index_offset;
765 int result, indirect;
767 down_read(&card->controls_rwsem);
768 kctl = snd_ctl_find_id(card, &control->id);
772 index_offset = snd_ctl_get_ioff(kctl, &control->id);
773 vd = &kctl->vd[index_offset];
774 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
775 if (control->indirect != indirect) {
778 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
780 (file && vd->owner != NULL && vd->owner != file)) {
783 snd_ctl_build_ioff(&control->id, kctl, index_offset);
784 result = kctl->put(kctl, control);
787 up_read(&card->controls_rwsem);
788 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &control->id);
793 up_read(&card->controls_rwsem);
797 EXPORT_SYMBOL(snd_ctl_elem_write);
799 static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
800 struct snd_ctl_elem_value __user *_control)
802 struct snd_ctl_elem_value *control;
803 struct snd_card *card;
806 control = kmalloc(sizeof(*control), GFP_KERNEL);
809 if (copy_from_user(control, _control, sizeof(*control))) {
814 snd_power_lock(card);
815 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
817 result = snd_ctl_elem_write(card, file, control);
818 snd_power_unlock(card);
820 if (copy_to_user(_control, control, sizeof(*control)))
826 static int snd_ctl_elem_lock(struct snd_ctl_file *file,
827 struct snd_ctl_elem_id __user *_id)
829 struct snd_card *card = file->card;
830 struct snd_ctl_elem_id id;
831 struct snd_kcontrol *kctl;
832 struct snd_kcontrol_volatile *vd;
835 if (copy_from_user(&id, _id, sizeof(id)))
837 down_write(&card->controls_rwsem);
838 kctl = snd_ctl_find_id(card, &id);
842 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
843 if (vd->owner != NULL)
847 vd->owner_pid = current->pid;
851 up_write(&card->controls_rwsem);
855 static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
856 struct snd_ctl_elem_id __user *_id)
858 struct snd_card *card = file->card;
859 struct snd_ctl_elem_id id;
860 struct snd_kcontrol *kctl;
861 struct snd_kcontrol_volatile *vd;
864 if (copy_from_user(&id, _id, sizeof(id)))
866 down_write(&card->controls_rwsem);
867 kctl = snd_ctl_find_id(card, &id);
871 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
872 if (vd->owner == NULL)
874 else if (vd->owner != file)
882 up_write(&card->controls_rwsem);
886 struct user_element {
887 struct snd_ctl_elem_info info;
888 void *elem_data; /* element data */
889 unsigned long elem_data_size; /* size of element data in bytes */
890 void *tlv_data; /* TLV data */
891 unsigned long tlv_data_size; /* TLV data size */
892 void *priv_data; /* private data (like strings for enumerated type) */
893 unsigned long priv_data_size; /* size of private data in bytes */
896 static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
897 struct snd_ctl_elem_info *uinfo)
899 struct user_element *ue = kcontrol->private_data;
905 static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
906 struct snd_ctl_elem_value *ucontrol)
908 struct user_element *ue = kcontrol->private_data;
910 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
914 static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
915 struct snd_ctl_elem_value *ucontrol)
918 struct user_element *ue = kcontrol->private_data;
920 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
922 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
926 static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol,
929 unsigned int __user *tlv)
931 struct user_element *ue = kcontrol->private_data;
936 if (size > 1024 * 128) /* sane value */
938 new_data = kmalloc(size, GFP_KERNEL);
939 if (new_data == NULL)
941 if (copy_from_user(new_data, tlv, size)) {
945 change = ue->tlv_data_size != size;
947 change = memcmp(ue->tlv_data, new_data, size);
949 ue->tlv_data = new_data;
950 ue->tlv_data_size = size;
952 if (size < ue->tlv_data_size)
954 if (copy_to_user(tlv, ue->tlv_data, ue->tlv_data_size))
960 static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
962 struct user_element *ue = kcontrol->private_data;
968 static int snd_ctl_elem_add(struct snd_ctl_file *file,
969 struct snd_ctl_elem_info *info, int replace)
971 struct snd_card *card = file->card;
972 struct snd_kcontrol kctl, *_kctl;
975 struct user_element *ue;
978 if (card->user_ctl_count >= MAX_USER_CONTROLS)
980 if (info->count > 1024)
982 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
983 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
984 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
985 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE));
987 memset(&kctl, 0, sizeof(kctl));
988 down_write(&card->controls_rwsem);
989 _kctl = snd_ctl_find_id(card, &info->id);
993 err = snd_ctl_remove(card, _kctl);
1000 up_write(&card->controls_rwsem);
1003 memcpy(&kctl.id, &info->id, sizeof(info->id));
1004 kctl.count = info->owner ? info->owner : 1;
1005 access |= SNDRV_CTL_ELEM_ACCESS_USER;
1006 kctl.info = snd_ctl_elem_user_info;
1007 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1008 kctl.get = snd_ctl_elem_user_get;
1009 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1010 kctl.put = snd_ctl_elem_user_put;
1011 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
1012 kctl.tlv.c = snd_ctl_elem_user_tlv;
1013 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1015 switch (info->type) {
1016 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
1017 private_size = sizeof(char);
1018 if (info->count > 128)
1021 case SNDRV_CTL_ELEM_TYPE_INTEGER:
1022 private_size = sizeof(long);
1023 if (info->count > 128)
1026 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
1027 private_size = sizeof(long long);
1028 if (info->count > 64)
1031 case SNDRV_CTL_ELEM_TYPE_BYTES:
1032 private_size = sizeof(unsigned char);
1033 if (info->count > 512)
1036 case SNDRV_CTL_ELEM_TYPE_IEC958:
1037 private_size = sizeof(struct snd_aes_iec958);
1038 if (info->count != 1)
1044 private_size *= info->count;
1045 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
1049 ue->elem_data = (char *)ue + sizeof(*ue);
1050 ue->elem_data_size = private_size;
1051 kctl.private_free = snd_ctl_elem_user_free;
1052 _kctl = snd_ctl_new(&kctl, access);
1053 if (_kctl == NULL) {
1057 _kctl->private_data = ue;
1058 for (idx = 0; idx < _kctl->count; idx++)
1059 _kctl->vd[idx].owner = file;
1060 err = snd_ctl_add(card, _kctl);
1064 down_write(&card->controls_rwsem);
1065 card->user_ctl_count++;
1066 up_write(&card->controls_rwsem);
1071 static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1072 struct snd_ctl_elem_info __user *_info, int replace)
1074 struct snd_ctl_elem_info info;
1075 if (copy_from_user(&info, _info, sizeof(info)))
1077 return snd_ctl_elem_add(file, &info, replace);
1080 static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1081 struct snd_ctl_elem_id __user *_id)
1083 struct snd_ctl_elem_id id;
1086 if (copy_from_user(&id, _id, sizeof(id)))
1088 err = snd_ctl_remove_unlocked_id(file, &id);
1090 struct snd_card *card = file->card;
1091 down_write(&card->controls_rwsem);
1092 card->user_ctl_count--;
1093 up_write(&card->controls_rwsem);
1098 static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
1101 if (get_user(subscribe, ptr))
1103 if (subscribe < 0) {
1104 subscribe = file->subscribed;
1105 if (put_user(subscribe, ptr))
1110 file->subscribed = 1;
1112 } else if (file->subscribed) {
1113 snd_ctl_empty_read_queue(file);
1114 file->subscribed = 0;
1119 static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1120 struct snd_ctl_tlv __user *_tlv,
1123 struct snd_card *card = file->card;
1124 struct snd_ctl_tlv tlv;
1125 struct snd_kcontrol *kctl;
1126 struct snd_kcontrol_volatile *vd;
1130 if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
1132 if (tlv.length < sizeof(unsigned int) * 3)
1134 down_read(&card->controls_rwsem);
1135 kctl = snd_ctl_find_numid(card, tlv.numid);
1140 if (kctl->tlv.p == NULL) {
1144 vd = &kctl->vd[tlv.numid - kctl->id.numid];
1145 if ((op_flag == 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) == 0) ||
1146 (op_flag > 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) == 0) ||
1147 (op_flag < 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND) == 0)) {
1151 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1152 if (file && vd->owner != NULL && vd->owner != file) {
1156 err = kctl->tlv.c(kctl, op_flag, tlv.length, _tlv->tlv);
1158 up_read(&card->controls_rwsem);
1159 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &kctl->id);
1167 len = kctl->tlv.p[1] + 2 * sizeof(unsigned int);
1168 if (tlv.length < len) {
1172 if (copy_to_user(_tlv->tlv, kctl->tlv.p, len))
1176 up_read(&card->controls_rwsem);
1180 static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1182 struct snd_ctl_file *ctl;
1183 struct snd_card *card;
1184 struct list_head *list;
1185 struct snd_kctl_ioctl *p;
1186 void __user *argp = (void __user *)arg;
1187 int __user *ip = argp;
1190 ctl = file->private_data;
1192 snd_assert(card != NULL, return -ENXIO);
1194 case SNDRV_CTL_IOCTL_PVERSION:
1195 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1196 case SNDRV_CTL_IOCTL_CARD_INFO:
1197 return snd_ctl_card_info(card, ctl, cmd, argp);
1198 case SNDRV_CTL_IOCTL_ELEM_LIST:
1199 return snd_ctl_elem_list(card, argp);
1200 case SNDRV_CTL_IOCTL_ELEM_INFO:
1201 return snd_ctl_elem_info_user(ctl, argp);
1202 case SNDRV_CTL_IOCTL_ELEM_READ:
1203 return snd_ctl_elem_read_user(card, argp);
1204 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1205 return snd_ctl_elem_write_user(ctl, argp);
1206 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1207 return snd_ctl_elem_lock(ctl, argp);
1208 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1209 return snd_ctl_elem_unlock(ctl, argp);
1210 case SNDRV_CTL_IOCTL_ELEM_ADD:
1211 return snd_ctl_elem_add_user(ctl, argp, 0);
1212 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1213 return snd_ctl_elem_add_user(ctl, argp, 1);
1214 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1215 return snd_ctl_elem_remove(ctl, argp);
1216 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1217 return snd_ctl_subscribe_events(ctl, ip);
1218 case SNDRV_CTL_IOCTL_TLV_READ:
1219 return snd_ctl_tlv_ioctl(ctl, argp, 0);
1220 case SNDRV_CTL_IOCTL_TLV_WRITE:
1221 return snd_ctl_tlv_ioctl(ctl, argp, 1);
1222 case SNDRV_CTL_IOCTL_TLV_COMMAND:
1223 return snd_ctl_tlv_ioctl(ctl, argp, -1);
1224 case SNDRV_CTL_IOCTL_POWER:
1225 return -ENOPROTOOPT;
1226 case SNDRV_CTL_IOCTL_POWER_STATE:
1228 return put_user(card->power_state, ip) ? -EFAULT : 0;
1230 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1233 down_read(&snd_ioctl_rwsem);
1234 list_for_each(list, &snd_control_ioctls) {
1235 p = list_entry(list, struct snd_kctl_ioctl, list);
1236 err = p->fioctl(card, ctl, cmd, arg);
1237 if (err != -ENOIOCTLCMD) {
1238 up_read(&snd_ioctl_rwsem);
1242 up_read(&snd_ioctl_rwsem);
1243 snd_printdd("unknown ioctl = 0x%x\n", cmd);
1247 static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1248 size_t count, loff_t * offset)
1250 struct snd_ctl_file *ctl;
1254 ctl = file->private_data;
1255 snd_assert(ctl != NULL && ctl->card != NULL, return -ENXIO);
1256 if (!ctl->subscribed)
1258 if (count < sizeof(struct snd_ctl_event))
1260 spin_lock_irq(&ctl->read_lock);
1261 while (count >= sizeof(struct snd_ctl_event)) {
1262 struct snd_ctl_event ev;
1263 struct snd_kctl_event *kev;
1264 while (list_empty(&ctl->events)) {
1266 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1270 init_waitqueue_entry(&wait, current);
1271 add_wait_queue(&ctl->change_sleep, &wait);
1272 set_current_state(TASK_INTERRUPTIBLE);
1273 spin_unlock_irq(&ctl->read_lock);
1275 remove_wait_queue(&ctl->change_sleep, &wait);
1276 if (signal_pending(current))
1277 return result > 0 ? result : -ERESTARTSYS;
1278 spin_lock_irq(&ctl->read_lock);
1280 kev = snd_kctl_event(ctl->events.next);
1281 ev.type = SNDRV_CTL_EVENT_ELEM;
1282 ev.data.elem.mask = kev->mask;
1283 ev.data.elem.id = kev->id;
1284 list_del(&kev->list);
1285 spin_unlock_irq(&ctl->read_lock);
1287 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
1291 spin_lock_irq(&ctl->read_lock);
1292 buffer += sizeof(struct snd_ctl_event);
1293 count -= sizeof(struct snd_ctl_event);
1294 result += sizeof(struct snd_ctl_event);
1297 spin_unlock_irq(&ctl->read_lock);
1299 return result > 0 ? result : err;
1302 static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1305 struct snd_ctl_file *ctl;
1307 ctl = file->private_data;
1308 if (!ctl->subscribed)
1310 poll_wait(file, &ctl->change_sleep, wait);
1313 if (!list_empty(&ctl->events))
1314 mask |= POLLIN | POLLRDNORM;
1320 * register the device-specific control-ioctls.
1321 * called from each device manager like pcm.c, hwdep.c, etc.
1323 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1325 struct snd_kctl_ioctl *pn;
1327 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
1331 down_write(&snd_ioctl_rwsem);
1332 list_add_tail(&pn->list, lists);
1333 up_write(&snd_ioctl_rwsem);
1337 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1339 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1342 EXPORT_SYMBOL(snd_ctl_register_ioctl);
1344 #ifdef CONFIG_COMPAT
1345 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1347 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1350 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
1354 * de-register the device-specific control-ioctls.
1356 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1357 struct list_head *lists)
1359 struct list_head *list;
1360 struct snd_kctl_ioctl *p;
1362 snd_assert(fcn != NULL, return -EINVAL);
1363 down_write(&snd_ioctl_rwsem);
1364 list_for_each(list, lists) {
1365 p = list_entry(list, struct snd_kctl_ioctl, list);
1366 if (p->fioctl == fcn) {
1368 up_write(&snd_ioctl_rwsem);
1373 up_write(&snd_ioctl_rwsem);
1378 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1380 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1383 EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1385 #ifdef CONFIG_COMPAT
1386 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1388 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1391 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
1394 static int snd_ctl_fasync(int fd, struct file * file, int on)
1396 struct snd_ctl_file *ctl;
1398 ctl = file->private_data;
1399 err = fasync_helper(fd, file, on, &ctl->fasync);
1408 #ifdef CONFIG_COMPAT
1409 #include "control_compat.c"
1411 #define snd_ctl_ioctl_compat NULL
1418 static struct file_operations snd_ctl_f_ops =
1420 .owner = THIS_MODULE,
1421 .read = snd_ctl_read,
1422 .open = snd_ctl_open,
1423 .release = snd_ctl_release,
1424 .poll = snd_ctl_poll,
1425 .unlocked_ioctl = snd_ctl_ioctl,
1426 .compat_ioctl = snd_ctl_ioctl_compat,
1427 .fasync = snd_ctl_fasync,
1431 * registration of the control device
1433 static int snd_ctl_dev_register(struct snd_device *device)
1435 struct snd_card *card = device->device_data;
1439 snd_assert(card != NULL, return -ENXIO);
1440 cardnum = card->number;
1441 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1442 sprintf(name, "controlC%i", cardnum);
1443 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1444 &snd_ctl_f_ops, card, name)) < 0)
1450 * disconnection of the control device
1452 static int snd_ctl_dev_disconnect(struct snd_device *device)
1454 struct snd_card *card = device->device_data;
1455 struct list_head *flist;
1456 struct snd_ctl_file *ctl;
1459 snd_assert(card != NULL, return -ENXIO);
1460 cardnum = card->number;
1461 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1463 down_read(&card->controls_rwsem);
1464 list_for_each(flist, &card->ctl_files) {
1465 ctl = snd_ctl_file(flist);
1466 wake_up(&ctl->change_sleep);
1467 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1469 up_read(&card->controls_rwsem);
1471 if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
1480 static int snd_ctl_dev_free(struct snd_device *device)
1482 struct snd_card *card = device->device_data;
1483 struct snd_kcontrol *control;
1485 down_write(&card->controls_rwsem);
1486 while (!list_empty(&card->controls)) {
1487 control = snd_kcontrol(card->controls.next);
1488 snd_ctl_remove(card, control);
1490 up_write(&card->controls_rwsem);
1495 * create control core:
1496 * called from init.c
1498 int snd_ctl_create(struct snd_card *card)
1500 static struct snd_device_ops ops = {
1501 .dev_free = snd_ctl_dev_free,
1502 .dev_register = snd_ctl_dev_register,
1503 .dev_disconnect = snd_ctl_dev_disconnect,
1506 snd_assert(card != NULL, return -ENXIO);
1507 return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);