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|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()
257 * Don't call this after the control was added to the card.
259 void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
262 if (kcontrol->private_free)
263 kcontrol->private_free(kcontrol);
268 EXPORT_SYMBOL(snd_ctl_free_one);
270 static unsigned int snd_ctl_hole_check(struct snd_card *card,
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)) {
294 /* this situation is very unlikely */
295 snd_printk(KERN_ERR "unable to allocate new control numid\n");
298 last_numid = card->last_numid;
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;
324 snd_assert(card != NULL, goto error);
325 snd_assert(kcontrol->info != NULL, goto error);
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",
339 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
340 up_write(&card->controls_rwsem);
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);
354 snd_ctl_free_one(kcontrol);
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;
376 snd_assert(card != NULL && kcontrol != NULL, return -EINVAL);
377 list_del(&kcontrol->list);
378 card->controls_count -= kcontrol->count;
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);
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;
403 down_write(&card->controls_rwsem);
404 kctl = snd_ctl_find_id(card, id);
406 up_write(&card->controls_rwsem);
409 ret = snd_ctl_remove(card, kctl);
410 up_write(&card->controls_rwsem);
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;
433 down_write(&card->controls_rwsem);
434 kctl = snd_ctl_find_id(card, id);
436 up_write(&card->controls_rwsem);
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);
444 ret = snd_ctl_remove(card, kctl);
445 up_write(&card->controls_rwsem);
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);
468 up_write(&card->controls_rwsem);
472 kctl->id.numid = card->last_numid + 1;
473 card->last_numid += kctl->count;
474 up_write(&card->controls_rwsem);
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)
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);
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)
533 if (kctl->id.device != id->device)
535 if (kctl->id.subdevice != id->subdevice)
537 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
539 if (kctl->id.index > id->index)
541 if (kctl->id.index + kctl->count <= id->index)
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);
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))) {
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)))
586 offset = list.offset;
589 /* try limit maximum space */
593 /* allocate temporary buffer for atomic operation */
594 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
597 down_read(&card->controls_rwsem);
598 list.count = card->controls_count;
599 plist = card->controls.next;
600 while (plist != &card->controls) {
603 kctl = snd_kcontrol(plist);
604 if (offset < kctl->count)
606 offset -= kctl->count;
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);
622 up_read(&card->controls_rwsem);
624 copy_to_user(list.pids, dst,
625 list.used * sizeof(struct snd_ctl_elem_id))) {
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)))
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;
649 down_read(&card->controls_rwsem);
650 kctl = snd_ctl_find_id(card, &info->id);
652 up_read(&card->controls_rwsem);
655 #ifdef CONFIG_SND_DEBUG
658 result = kctl->info(kctl, info);
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;
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;
674 up_read(&card->controls_rwsem);
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;
684 if (copy_from_user(&info, _info, sizeof(info)))
686 snd_power_lock(ctl->card);
687 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
689 result = snd_ctl_elem_info(ctl, &info);
690 snd_power_unlock(ctl->card);
692 if (copy_to_user(_info, &info, sizeof(info)))
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);
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) {
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);
723 up_read(&card->controls_rwsem);
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;
735 control = kmalloc(sizeof(*control), GFP_KERNEL);
738 if (copy_from_user(control, _control, sizeof(*control))) {
742 snd_power_lock(card);
743 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
745 result = snd_ctl_elem_read(card, control);
746 snd_power_unlock(card);
748 if (copy_to_user(_control, control, sizeof(*control)))
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);
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) {
773 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
775 (file && vd->owner != NULL && vd->owner != file)) {
778 snd_ctl_build_ioff(&control->id, kctl, index_offset);
779 result = kctl->put(kctl, control);
782 up_read(&card->controls_rwsem);
783 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &control->id);
788 up_read(&card->controls_rwsem);
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;
801 control = kmalloc(sizeof(*control), GFP_KERNEL);
804 if (copy_from_user(control, _control, sizeof(*control))) {
809 snd_power_lock(card);
810 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
812 result = snd_ctl_elem_write(card, file, control);
813 snd_power_unlock(card);
815 if (copy_to_user(_control, control, sizeof(*control)))
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;
830 if (copy_from_user(&id, _id, sizeof(id)))
832 down_write(&card->controls_rwsem);
833 kctl = snd_ctl_find_id(card, &id);
837 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
838 if (vd->owner != NULL)
842 vd->owner_pid = current->pid;
846 up_write(&card->controls_rwsem);
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;
859 if (copy_from_user(&id, _id, sizeof(id)))
861 down_write(&card->controls_rwsem);
862 kctl = snd_ctl_find_id(card, &id);
866 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
867 if (vd->owner == NULL)
869 else if (vd->owner != file)
877 up_write(&card->controls_rwsem);
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;
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);
907 static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
908 struct snd_ctl_elem_value *ucontrol)
911 struct user_element *ue = kcontrol->private_data;
913 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
915 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
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;
931 struct user_element *ue;
934 if (card->user_ctl_count >= MAX_USER_CONTROLS)
936 if (info->count > 1024)
938 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
939 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
940 SNDRV_CTL_ELEM_ACCESS_INACTIVE));
942 memset(&kctl, 0, sizeof(kctl));
943 down_write(&card->controls_rwsem);
944 _kctl = snd_ctl_find_id(card, &info->id);
948 err = snd_ctl_remove(card, _kctl);
955 up_write(&card->controls_rwsem);
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)
972 case SNDRV_CTL_ELEM_TYPE_INTEGER:
973 private_size = sizeof(long);
974 if (info->count > 128)
977 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
978 private_size = sizeof(long long);
979 if (info->count > 64)
982 case SNDRV_CTL_ELEM_TYPE_BYTES:
983 private_size = sizeof(unsigned char);
984 if (info->count > 512)
987 case SNDRV_CTL_ELEM_TYPE_IEC958:
988 private_size = sizeof(struct snd_aes_iec958);
989 if (info->count != 1)
995 private_size *= info->count;
996 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
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) {
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);
1015 down_write(&card->controls_rwsem);
1016 card->user_ctl_count++;
1017 up_write(&card->controls_rwsem);
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)))
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;
1037 if (copy_from_user(&id, _id, sizeof(id)))
1039 err = snd_ctl_remove_unlocked_id(file, &id);
1041 struct snd_card *card = file->card;
1042 down_write(&card->controls_rwsem);
1043 card->user_ctl_count--;
1044 up_write(&card->controls_rwsem);
1049 static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
1052 if (get_user(subscribe, ptr))
1054 if (subscribe < 0) {
1055 subscribe = file->subscribed;
1056 if (put_user(subscribe, ptr))
1061 file->subscribed = 1;
1063 } else if (file->subscribed) {
1064 snd_ctl_empty_read_queue(file);
1065 file->subscribed = 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;
1080 ctl = file->private_data;
1082 snd_assert(card != NULL, return -ENXIO);
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:
1112 return put_user(card->power_state, ip) ? -EFAULT : 0;
1114 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
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);
1126 up_read(&snd_ioctl_rwsem);
1127 snd_printdd("unknown ioctl = 0x%x\n", cmd);
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;
1138 ctl = file->private_data;
1139 snd_assert(ctl != NULL && ctl->card != NULL, return -ENXIO);
1140 if (!ctl->subscribed)
1142 if (count < sizeof(struct snd_ctl_event))
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)) {
1150 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
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);
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);
1171 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
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);
1181 spin_unlock_irq(&ctl->read_lock);
1183 return result > 0 ? result : err;
1186 static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1189 struct snd_ctl_file *ctl;
1191 ctl = file->private_data;
1192 if (!ctl->subscribed)
1194 poll_wait(file, &ctl->change_sleep, wait);
1197 if (!list_empty(&ctl->events))
1198 mask |= POLLIN | POLLRDNORM;
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);
1215 down_write(&snd_ioctl_rwsem);
1216 list_add_tail(&pn->list, lists);
1217 up_write(&snd_ioctl_rwsem);
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);
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) {
1252 up_write(&snd_ioctl_rwsem);
1257 up_write(&snd_ioctl_rwsem);
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);
1278 static int snd_ctl_fasync(int fd, struct file * file, int on)
1280 struct snd_ctl_file *ctl;
1282 ctl = file->private_data;
1283 err = fasync_helper(fd, file, on, &ctl->fasync);
1292 #ifdef CONFIG_COMPAT
1293 #include "control_compat.c"
1295 #define snd_ctl_ioctl_compat NULL
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;
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)
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);
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);
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;
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,
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);