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->prefer_pcm_subdevice = -1;
79 ctl->prefer_rawmidi_subdevice = -1;
80 ctl->pid = current->pid;
81 file->private_data = ctl;
82 write_lock_irqsave(&card->ctl_files_rwlock, flags);
83 list_add_tail(&ctl->list, &card->ctl_files);
84 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
88 module_put(card->module);
90 snd_card_file_remove(card, file);
95 static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
97 struct snd_kctl_event *cread;
99 spin_lock(&ctl->read_lock);
100 while (!list_empty(&ctl->events)) {
101 cread = snd_kctl_event(ctl->events.next);
102 list_del(&cread->list);
105 spin_unlock(&ctl->read_lock);
108 static int snd_ctl_release(struct inode *inode, struct file *file)
111 struct snd_card *card;
112 struct snd_ctl_file *ctl;
113 struct snd_kcontrol *control;
116 ctl = file->private_data;
117 fasync_helper(-1, file, 0, &ctl->fasync);
118 file->private_data = NULL;
120 write_lock_irqsave(&card->ctl_files_rwlock, flags);
121 list_del(&ctl->list);
122 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
123 down_write(&card->controls_rwsem);
124 list_for_each_entry(control, &card->controls, list)
125 for (idx = 0; idx < control->count; idx++)
126 if (control->vd[idx].owner == ctl)
127 control->vd[idx].owner = NULL;
128 up_write(&card->controls_rwsem);
129 snd_ctl_empty_read_queue(ctl);
131 module_put(card->module);
132 snd_card_file_remove(card, file);
136 void snd_ctl_notify(struct snd_card *card, unsigned int mask,
137 struct snd_ctl_elem_id *id)
140 struct snd_ctl_file *ctl;
141 struct snd_kctl_event *ev;
143 snd_assert(card != NULL && id != NULL, return);
144 read_lock(&card->ctl_files_rwlock);
145 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
146 card->mixer_oss_change_count++;
148 list_for_each_entry(ctl, &card->ctl_files, list) {
149 if (!ctl->subscribed)
151 spin_lock_irqsave(&ctl->read_lock, flags);
152 list_for_each_entry(ev, &ctl->events, list) {
153 if (ev->id.numid == id->numid) {
158 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
162 list_add_tail(&ev->list, &ctl->events);
164 snd_printk(KERN_ERR "No memory available to allocate event\n");
167 wake_up(&ctl->change_sleep);
168 spin_unlock_irqrestore(&ctl->read_lock, flags);
169 kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
171 read_unlock(&card->ctl_files_rwlock);
174 EXPORT_SYMBOL(snd_ctl_notify);
177 * snd_ctl_new - create a control instance from the template
178 * @control: the control template
179 * @access: the default control access
181 * Allocates a new struct snd_kcontrol instance and copies the given template
182 * to the new instance. It does not copy volatile data (access).
184 * Returns the pointer of the new instance, or NULL on failure.
186 struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control, unsigned int access)
188 struct snd_kcontrol *kctl;
191 snd_assert(control != NULL, return NULL);
192 snd_assert(control->count > 0, return NULL);
193 kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL);
195 snd_printk(KERN_ERR "Cannot allocate control instance\n");
199 for (idx = 0; idx < kctl->count; idx++)
200 kctl->vd[idx].access = access;
204 EXPORT_SYMBOL(snd_ctl_new);
207 * snd_ctl_new1 - create a control instance from the template
208 * @ncontrol: the initialization record
209 * @private_data: the private data to set
211 * Allocates a new struct snd_kcontrol instance and initialize from the given
212 * template. When the access field of ncontrol is 0, it's assumed as
213 * READWRITE access. When the count field is 0, it's assumes as one.
215 * Returns the pointer of the newly generated instance, or NULL on failure.
217 struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
220 struct snd_kcontrol kctl;
223 snd_assert(ncontrol != NULL, return NULL);
224 snd_assert(ncontrol->info != NULL, return NULL);
225 memset(&kctl, 0, sizeof(kctl));
226 kctl.id.iface = ncontrol->iface;
227 kctl.id.device = ncontrol->device;
228 kctl.id.subdevice = ncontrol->subdevice;
230 strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
231 kctl.id.index = ncontrol->index;
232 kctl.count = ncontrol->count ? ncontrol->count : 1;
233 access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
234 (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
235 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
236 SNDRV_CTL_ELEM_ACCESS_DINDIRECT|
237 SNDRV_CTL_ELEM_ACCESS_INDIRECT|
238 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE|
239 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK));
240 kctl.info = ncontrol->info;
241 kctl.get = ncontrol->get;
242 kctl.put = ncontrol->put;
243 kctl.tlv.p = ncontrol->tlv.p;
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 snd_kcontrol *kctl;
275 list_for_each_entry(kctl, &card->controls, list) {
276 if ((kctl->id.numid <= card->last_numid &&
277 kctl->id.numid + kctl->count > card->last_numid) ||
278 (kctl->id.numid <= card->last_numid + count - 1 &&
279 kctl->id.numid + kctl->count > card->last_numid + count - 1))
280 return card->last_numid = kctl->id.numid + kctl->count - 1;
282 return card->last_numid;
285 static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
287 unsigned int last_numid, iter = 100000;
289 last_numid = card->last_numid;
290 while (last_numid != snd_ctl_hole_check(card, count)) {
292 /* this situation is very unlikely */
293 snd_printk(KERN_ERR "unable to allocate new control numid\n");
296 last_numid = card->last_numid;
302 * snd_ctl_add - add the control instance to the card
303 * @card: the card instance
304 * @kcontrol: the control instance to add
306 * Adds the control instance created via snd_ctl_new() or
307 * snd_ctl_new1() to the given card. Assigns also an unique
308 * numid used for fast search.
310 * Returns zero if successful, or a negative error code on failure.
312 * It frees automatically the control which cannot be added.
314 int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
316 struct snd_ctl_elem_id id;
322 snd_assert(card != NULL, goto error);
323 snd_assert(kcontrol->info != NULL, goto error);
325 down_write(&card->controls_rwsem);
326 if (snd_ctl_find_id(card, &id)) {
327 up_write(&card->controls_rwsem);
328 snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
337 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
338 up_write(&card->controls_rwsem);
342 list_add_tail(&kcontrol->list, &card->controls);
343 card->controls_count += kcontrol->count;
344 kcontrol->id.numid = card->last_numid + 1;
345 card->last_numid += kcontrol->count;
346 up_write(&card->controls_rwsem);
347 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
348 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
352 snd_ctl_free_one(kcontrol);
356 EXPORT_SYMBOL(snd_ctl_add);
359 * snd_ctl_remove - remove the control from the card and release it
360 * @card: the card instance
361 * @kcontrol: the control instance to remove
363 * Removes the control from the card and then releases the instance.
364 * You don't need to call snd_ctl_free_one(). You must be in
365 * the write lock - down_write(&card->controls_rwsem).
367 * Returns 0 if successful, or a negative error code on failure.
369 int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
371 struct snd_ctl_elem_id id;
374 snd_assert(card != NULL && kcontrol != NULL, return -EINVAL);
375 list_del(&kcontrol->list);
376 card->controls_count -= kcontrol->count;
378 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
379 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
380 snd_ctl_free_one(kcontrol);
384 EXPORT_SYMBOL(snd_ctl_remove);
387 * snd_ctl_remove_id - remove the control of the given id and release it
388 * @card: the card instance
389 * @id: the control id to remove
391 * Finds the control instance with the given id, removes it from the
392 * card list and releases it.
394 * Returns 0 if successful, or a negative error code on failure.
396 int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
398 struct snd_kcontrol *kctl;
401 down_write(&card->controls_rwsem);
402 kctl = snd_ctl_find_id(card, id);
404 up_write(&card->controls_rwsem);
407 ret = snd_ctl_remove(card, kctl);
408 up_write(&card->controls_rwsem);
412 EXPORT_SYMBOL(snd_ctl_remove_id);
415 * snd_ctl_remove_unlocked_id - remove the unlocked control of the given id and release it
416 * @file: active control handle
417 * @id: the control id to remove
419 * Finds the control instance with the given id, removes it from the
420 * card list and releases it.
422 * Returns 0 if successful, or a negative error code on failure.
424 static int snd_ctl_remove_unlocked_id(struct snd_ctl_file * file,
425 struct snd_ctl_elem_id *id)
427 struct snd_card *card = file->card;
428 struct snd_kcontrol *kctl;
431 down_write(&card->controls_rwsem);
432 kctl = snd_ctl_find_id(card, id);
434 up_write(&card->controls_rwsem);
437 for (idx = 0; idx < kctl->count; idx++)
438 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
439 up_write(&card->controls_rwsem);
442 ret = snd_ctl_remove(card, kctl);
443 up_write(&card->controls_rwsem);
448 * snd_ctl_rename_id - replace the id of a control on the card
449 * @card: the card instance
450 * @src_id: the old id
451 * @dst_id: the new id
453 * Finds the control with the old id from the card, and replaces the
454 * id with the new one.
456 * Returns zero if successful, or a negative error code on failure.
458 int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
459 struct snd_ctl_elem_id *dst_id)
461 struct snd_kcontrol *kctl;
463 down_write(&card->controls_rwsem);
464 kctl = snd_ctl_find_id(card, src_id);
466 up_write(&card->controls_rwsem);
470 kctl->id.numid = card->last_numid + 1;
471 card->last_numid += kctl->count;
472 up_write(&card->controls_rwsem);
476 EXPORT_SYMBOL(snd_ctl_rename_id);
479 * snd_ctl_find_numid - find the control instance with the given number-id
480 * @card: the card instance
481 * @numid: the number-id to search
483 * Finds the control instance with the given number-id from the card.
485 * Returns the pointer of the instance if found, or NULL if not.
487 * The caller must down card->controls_rwsem before calling this function
488 * (if the race condition can happen).
490 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
492 struct snd_kcontrol *kctl;
494 snd_assert(card != NULL && numid != 0, return NULL);
495 list_for_each_entry(kctl, &card->controls, list) {
496 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
502 EXPORT_SYMBOL(snd_ctl_find_numid);
505 * snd_ctl_find_id - find the control instance with the given id
506 * @card: the card instance
507 * @id: the id to search
509 * Finds the control instance with the given id from the card.
511 * Returns the pointer of the instance if found, or NULL if not.
513 * The caller must down card->controls_rwsem before calling this function
514 * (if the race condition can happen).
516 struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
517 struct snd_ctl_elem_id *id)
519 struct snd_kcontrol *kctl;
521 snd_assert(card != NULL && id != NULL, return NULL);
523 return snd_ctl_find_numid(card, id->numid);
524 list_for_each_entry(kctl, &card->controls, list) {
525 if (kctl->id.iface != id->iface)
527 if (kctl->id.device != id->device)
529 if (kctl->id.subdevice != id->subdevice)
531 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
533 if (kctl->id.index > id->index)
535 if (kctl->id.index + kctl->count <= id->index)
542 EXPORT_SYMBOL(snd_ctl_find_id);
544 static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
545 unsigned int cmd, void __user *arg)
547 struct snd_ctl_card_info *info;
549 info = kzalloc(sizeof(*info), GFP_KERNEL);
552 down_read(&snd_ioctl_rwsem);
553 info->card = card->number;
554 strlcpy(info->id, card->id, sizeof(info->id));
555 strlcpy(info->driver, card->driver, sizeof(info->driver));
556 strlcpy(info->name, card->shortname, sizeof(info->name));
557 strlcpy(info->longname, card->longname, sizeof(info->longname));
558 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
559 strlcpy(info->components, card->components, sizeof(info->components));
560 up_read(&snd_ioctl_rwsem);
561 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
569 static int snd_ctl_elem_list(struct snd_card *card,
570 struct snd_ctl_elem_list __user *_list)
572 struct list_head *plist;
573 struct snd_ctl_elem_list list;
574 struct snd_kcontrol *kctl;
575 struct snd_ctl_elem_id *dst, *id;
576 unsigned int offset, space, first, jidx;
578 if (copy_from_user(&list, _list, sizeof(list)))
580 offset = list.offset;
583 /* try limit maximum space */
587 /* allocate temporary buffer for atomic operation */
588 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
591 down_read(&card->controls_rwsem);
592 list.count = card->controls_count;
593 plist = card->controls.next;
594 while (plist != &card->controls) {
597 kctl = snd_kcontrol(plist);
598 if (offset < kctl->count)
600 offset -= kctl->count;
605 while (space > 0 && plist != &card->controls) {
606 kctl = snd_kcontrol(plist);
607 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
608 snd_ctl_build_ioff(id, kctl, jidx);
616 up_read(&card->controls_rwsem);
618 copy_to_user(list.pids, dst,
619 list.used * sizeof(struct snd_ctl_elem_id))) {
625 down_read(&card->controls_rwsem);
626 list.count = card->controls_count;
627 up_read(&card->controls_rwsem);
629 if (copy_to_user(_list, &list, sizeof(list)))
634 static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
635 struct snd_ctl_elem_info *info)
637 struct snd_card *card = ctl->card;
638 struct snd_kcontrol *kctl;
639 struct snd_kcontrol_volatile *vd;
640 unsigned int index_offset;
643 down_read(&card->controls_rwsem);
644 kctl = snd_ctl_find_id(card, &info->id);
646 up_read(&card->controls_rwsem);
649 #ifdef CONFIG_SND_DEBUG
652 result = kctl->info(kctl, info);
654 snd_assert(info->access == 0, );
655 index_offset = snd_ctl_get_ioff(kctl, &info->id);
656 vd = &kctl->vd[index_offset];
657 snd_ctl_build_ioff(&info->id, kctl, index_offset);
658 info->access = vd->access;
660 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
661 if (vd->owner == ctl)
662 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
663 info->owner = vd->owner_pid;
668 up_read(&card->controls_rwsem);
672 static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
673 struct snd_ctl_elem_info __user *_info)
675 struct snd_ctl_elem_info info;
678 if (copy_from_user(&info, _info, sizeof(info)))
680 snd_power_lock(ctl->card);
681 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
683 result = snd_ctl_elem_info(ctl, &info);
684 snd_power_unlock(ctl->card);
686 if (copy_to_user(_info, &info, sizeof(info)))
691 int snd_ctl_elem_read(struct snd_card *card, struct snd_ctl_elem_value *control)
693 struct snd_kcontrol *kctl;
694 struct snd_kcontrol_volatile *vd;
695 unsigned int index_offset;
696 int result, indirect;
698 down_read(&card->controls_rwsem);
699 kctl = snd_ctl_find_id(card, &control->id);
703 index_offset = snd_ctl_get_ioff(kctl, &control->id);
704 vd = &kctl->vd[index_offset];
705 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
706 if (control->indirect != indirect) {
709 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) && kctl->get != NULL) {
710 snd_ctl_build_ioff(&control->id, kctl, index_offset);
711 result = kctl->get(kctl, control);
717 up_read(&card->controls_rwsem);
721 EXPORT_SYMBOL(snd_ctl_elem_read);
723 static int snd_ctl_elem_read_user(struct snd_card *card,
724 struct snd_ctl_elem_value __user *_control)
726 struct snd_ctl_elem_value *control;
729 control = kmalloc(sizeof(*control), GFP_KERNEL);
732 if (copy_from_user(control, _control, sizeof(*control))) {
736 snd_power_lock(card);
737 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
739 result = snd_ctl_elem_read(card, control);
740 snd_power_unlock(card);
742 if (copy_to_user(_control, control, sizeof(*control)))
748 int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
749 struct snd_ctl_elem_value *control)
751 struct snd_kcontrol *kctl;
752 struct snd_kcontrol_volatile *vd;
753 unsigned int index_offset;
754 int result, indirect;
756 down_read(&card->controls_rwsem);
757 kctl = snd_ctl_find_id(card, &control->id);
761 index_offset = snd_ctl_get_ioff(kctl, &control->id);
762 vd = &kctl->vd[index_offset];
763 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
764 if (control->indirect != indirect) {
767 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
769 (file && vd->owner != NULL && vd->owner != file)) {
772 snd_ctl_build_ioff(&control->id, kctl, index_offset);
773 result = kctl->put(kctl, control);
776 up_read(&card->controls_rwsem);
777 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &control->id);
782 up_read(&card->controls_rwsem);
786 EXPORT_SYMBOL(snd_ctl_elem_write);
788 static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
789 struct snd_ctl_elem_value __user *_control)
791 struct snd_ctl_elem_value *control;
792 struct snd_card *card;
795 control = kmalloc(sizeof(*control), GFP_KERNEL);
798 if (copy_from_user(control, _control, sizeof(*control))) {
803 snd_power_lock(card);
804 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
806 result = snd_ctl_elem_write(card, file, control);
807 snd_power_unlock(card);
809 if (copy_to_user(_control, control, sizeof(*control)))
815 static int snd_ctl_elem_lock(struct snd_ctl_file *file,
816 struct snd_ctl_elem_id __user *_id)
818 struct snd_card *card = file->card;
819 struct snd_ctl_elem_id id;
820 struct snd_kcontrol *kctl;
821 struct snd_kcontrol_volatile *vd;
824 if (copy_from_user(&id, _id, sizeof(id)))
826 down_write(&card->controls_rwsem);
827 kctl = snd_ctl_find_id(card, &id);
831 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
832 if (vd->owner != NULL)
836 vd->owner_pid = current->pid;
840 up_write(&card->controls_rwsem);
844 static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
845 struct snd_ctl_elem_id __user *_id)
847 struct snd_card *card = file->card;
848 struct snd_ctl_elem_id id;
849 struct snd_kcontrol *kctl;
850 struct snd_kcontrol_volatile *vd;
853 if (copy_from_user(&id, _id, sizeof(id)))
855 down_write(&card->controls_rwsem);
856 kctl = snd_ctl_find_id(card, &id);
860 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
861 if (vd->owner == NULL)
863 else if (vd->owner != file)
871 up_write(&card->controls_rwsem);
875 struct user_element {
876 struct snd_ctl_elem_info info;
877 void *elem_data; /* element data */
878 unsigned long elem_data_size; /* size of element data in bytes */
879 void *tlv_data; /* TLV data */
880 unsigned long tlv_data_size; /* TLV data size */
881 void *priv_data; /* private data (like strings for enumerated type) */
882 unsigned long priv_data_size; /* size of private data in bytes */
885 static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
886 struct snd_ctl_elem_info *uinfo)
888 struct user_element *ue = kcontrol->private_data;
894 static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
895 struct snd_ctl_elem_value *ucontrol)
897 struct user_element *ue = kcontrol->private_data;
899 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
903 static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
904 struct snd_ctl_elem_value *ucontrol)
907 struct user_element *ue = kcontrol->private_data;
909 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
911 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
915 static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol,
918 unsigned int __user *tlv)
920 struct user_element *ue = kcontrol->private_data;
925 if (size > 1024 * 128) /* sane value */
927 new_data = kmalloc(size, GFP_KERNEL);
928 if (new_data == NULL)
930 if (copy_from_user(new_data, tlv, size)) {
934 change = ue->tlv_data_size != size;
936 change = memcmp(ue->tlv_data, new_data, size);
938 ue->tlv_data = new_data;
939 ue->tlv_data_size = size;
941 if (! ue->tlv_data_size || ! ue->tlv_data)
943 if (size < ue->tlv_data_size)
945 if (copy_to_user(tlv, ue->tlv_data, ue->tlv_data_size))
951 static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
953 struct user_element *ue = kcontrol->private_data;
959 static int snd_ctl_elem_add(struct snd_ctl_file *file,
960 struct snd_ctl_elem_info *info, int replace)
962 struct snd_card *card = file->card;
963 struct snd_kcontrol kctl, *_kctl;
966 struct user_element *ue;
969 if (card->user_ctl_count >= MAX_USER_CONTROLS)
971 if (info->count > 1024)
973 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
974 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
975 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
976 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE));
978 memset(&kctl, 0, sizeof(kctl));
979 down_write(&card->controls_rwsem);
980 _kctl = snd_ctl_find_id(card, &info->id);
984 err = snd_ctl_remove(card, _kctl);
991 up_write(&card->controls_rwsem);
994 memcpy(&kctl.id, &info->id, sizeof(info->id));
995 kctl.count = info->owner ? info->owner : 1;
996 access |= SNDRV_CTL_ELEM_ACCESS_USER;
997 kctl.info = snd_ctl_elem_user_info;
998 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
999 kctl.get = snd_ctl_elem_user_get;
1000 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1001 kctl.put = snd_ctl_elem_user_put;
1002 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
1003 kctl.tlv.c = snd_ctl_elem_user_tlv;
1004 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1006 switch (info->type) {
1007 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
1008 case SNDRV_CTL_ELEM_TYPE_INTEGER:
1009 private_size = sizeof(long);
1010 if (info->count > 128)
1013 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
1014 private_size = sizeof(long long);
1015 if (info->count > 64)
1018 case SNDRV_CTL_ELEM_TYPE_BYTES:
1019 private_size = sizeof(unsigned char);
1020 if (info->count > 512)
1023 case SNDRV_CTL_ELEM_TYPE_IEC958:
1024 private_size = sizeof(struct snd_aes_iec958);
1025 if (info->count != 1)
1031 private_size *= info->count;
1032 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
1036 ue->info.access = 0;
1037 ue->elem_data = (char *)ue + sizeof(*ue);
1038 ue->elem_data_size = private_size;
1039 kctl.private_free = snd_ctl_elem_user_free;
1040 _kctl = snd_ctl_new(&kctl, access);
1041 if (_kctl == NULL) {
1045 _kctl->private_data = ue;
1046 for (idx = 0; idx < _kctl->count; idx++)
1047 _kctl->vd[idx].owner = file;
1048 err = snd_ctl_add(card, _kctl);
1052 down_write(&card->controls_rwsem);
1053 card->user_ctl_count++;
1054 up_write(&card->controls_rwsem);
1059 static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1060 struct snd_ctl_elem_info __user *_info, int replace)
1062 struct snd_ctl_elem_info info;
1063 if (copy_from_user(&info, _info, sizeof(info)))
1065 return snd_ctl_elem_add(file, &info, replace);
1068 static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1069 struct snd_ctl_elem_id __user *_id)
1071 struct snd_ctl_elem_id id;
1074 if (copy_from_user(&id, _id, sizeof(id)))
1076 err = snd_ctl_remove_unlocked_id(file, &id);
1078 struct snd_card *card = file->card;
1079 down_write(&card->controls_rwsem);
1080 card->user_ctl_count--;
1081 up_write(&card->controls_rwsem);
1086 static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
1089 if (get_user(subscribe, ptr))
1091 if (subscribe < 0) {
1092 subscribe = file->subscribed;
1093 if (put_user(subscribe, ptr))
1098 file->subscribed = 1;
1100 } else if (file->subscribed) {
1101 snd_ctl_empty_read_queue(file);
1102 file->subscribed = 0;
1107 static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1108 struct snd_ctl_tlv __user *_tlv,
1111 struct snd_card *card = file->card;
1112 struct snd_ctl_tlv tlv;
1113 struct snd_kcontrol *kctl;
1114 struct snd_kcontrol_volatile *vd;
1118 if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
1120 if (tlv.length < sizeof(unsigned int) * 3)
1122 down_read(&card->controls_rwsem);
1123 kctl = snd_ctl_find_numid(card, tlv.numid);
1128 if (kctl->tlv.p == NULL) {
1132 vd = &kctl->vd[tlv.numid - kctl->id.numid];
1133 if ((op_flag == 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) == 0) ||
1134 (op_flag > 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) == 0) ||
1135 (op_flag < 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND) == 0)) {
1139 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1140 if (file && vd->owner != NULL && vd->owner != file) {
1144 err = kctl->tlv.c(kctl, op_flag, tlv.length, _tlv->tlv);
1146 up_read(&card->controls_rwsem);
1147 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &kctl->id);
1155 len = kctl->tlv.p[1] + 2 * sizeof(unsigned int);
1156 if (tlv.length < len) {
1160 if (copy_to_user(_tlv->tlv, kctl->tlv.p, len))
1164 up_read(&card->controls_rwsem);
1168 static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1170 struct snd_ctl_file *ctl;
1171 struct snd_card *card;
1172 struct snd_kctl_ioctl *p;
1173 void __user *argp = (void __user *)arg;
1174 int __user *ip = argp;
1177 ctl = file->private_data;
1179 snd_assert(card != NULL, return -ENXIO);
1181 case SNDRV_CTL_IOCTL_PVERSION:
1182 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1183 case SNDRV_CTL_IOCTL_CARD_INFO:
1184 return snd_ctl_card_info(card, ctl, cmd, argp);
1185 case SNDRV_CTL_IOCTL_ELEM_LIST:
1186 return snd_ctl_elem_list(card, argp);
1187 case SNDRV_CTL_IOCTL_ELEM_INFO:
1188 return snd_ctl_elem_info_user(ctl, argp);
1189 case SNDRV_CTL_IOCTL_ELEM_READ:
1190 return snd_ctl_elem_read_user(card, argp);
1191 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1192 return snd_ctl_elem_write_user(ctl, argp);
1193 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1194 return snd_ctl_elem_lock(ctl, argp);
1195 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1196 return snd_ctl_elem_unlock(ctl, argp);
1197 case SNDRV_CTL_IOCTL_ELEM_ADD:
1198 return snd_ctl_elem_add_user(ctl, argp, 0);
1199 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1200 return snd_ctl_elem_add_user(ctl, argp, 1);
1201 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1202 return snd_ctl_elem_remove(ctl, argp);
1203 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1204 return snd_ctl_subscribe_events(ctl, ip);
1205 case SNDRV_CTL_IOCTL_TLV_READ:
1206 return snd_ctl_tlv_ioctl(ctl, argp, 0);
1207 case SNDRV_CTL_IOCTL_TLV_WRITE:
1208 return snd_ctl_tlv_ioctl(ctl, argp, 1);
1209 case SNDRV_CTL_IOCTL_TLV_COMMAND:
1210 return snd_ctl_tlv_ioctl(ctl, argp, -1);
1211 case SNDRV_CTL_IOCTL_POWER:
1212 return -ENOPROTOOPT;
1213 case SNDRV_CTL_IOCTL_POWER_STATE:
1215 return put_user(card->power_state, ip) ? -EFAULT : 0;
1217 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1220 down_read(&snd_ioctl_rwsem);
1221 list_for_each_entry(p, &snd_control_ioctls, list) {
1222 err = p->fioctl(card, ctl, cmd, arg);
1223 if (err != -ENOIOCTLCMD) {
1224 up_read(&snd_ioctl_rwsem);
1228 up_read(&snd_ioctl_rwsem);
1229 snd_printdd("unknown ioctl = 0x%x\n", cmd);
1233 static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1234 size_t count, loff_t * offset)
1236 struct snd_ctl_file *ctl;
1240 ctl = file->private_data;
1241 snd_assert(ctl != NULL && ctl->card != NULL, return -ENXIO);
1242 if (!ctl->subscribed)
1244 if (count < sizeof(struct snd_ctl_event))
1246 spin_lock_irq(&ctl->read_lock);
1247 while (count >= sizeof(struct snd_ctl_event)) {
1248 struct snd_ctl_event ev;
1249 struct snd_kctl_event *kev;
1250 while (list_empty(&ctl->events)) {
1252 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1256 init_waitqueue_entry(&wait, current);
1257 add_wait_queue(&ctl->change_sleep, &wait);
1258 set_current_state(TASK_INTERRUPTIBLE);
1259 spin_unlock_irq(&ctl->read_lock);
1261 remove_wait_queue(&ctl->change_sleep, &wait);
1262 if (signal_pending(current))
1263 return -ERESTARTSYS;
1264 spin_lock_irq(&ctl->read_lock);
1266 kev = snd_kctl_event(ctl->events.next);
1267 ev.type = SNDRV_CTL_EVENT_ELEM;
1268 ev.data.elem.mask = kev->mask;
1269 ev.data.elem.id = kev->id;
1270 list_del(&kev->list);
1271 spin_unlock_irq(&ctl->read_lock);
1273 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
1277 spin_lock_irq(&ctl->read_lock);
1278 buffer += sizeof(struct snd_ctl_event);
1279 count -= sizeof(struct snd_ctl_event);
1280 result += sizeof(struct snd_ctl_event);
1283 spin_unlock_irq(&ctl->read_lock);
1285 return result > 0 ? result : err;
1288 static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1291 struct snd_ctl_file *ctl;
1293 ctl = file->private_data;
1294 if (!ctl->subscribed)
1296 poll_wait(file, &ctl->change_sleep, wait);
1299 if (!list_empty(&ctl->events))
1300 mask |= POLLIN | POLLRDNORM;
1306 * register the device-specific control-ioctls.
1307 * called from each device manager like pcm.c, hwdep.c, etc.
1309 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1311 struct snd_kctl_ioctl *pn;
1313 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
1317 down_write(&snd_ioctl_rwsem);
1318 list_add_tail(&pn->list, lists);
1319 up_write(&snd_ioctl_rwsem);
1323 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1325 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1328 EXPORT_SYMBOL(snd_ctl_register_ioctl);
1330 #ifdef CONFIG_COMPAT
1331 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1333 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1336 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
1340 * de-register the device-specific control-ioctls.
1342 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1343 struct list_head *lists)
1345 struct snd_kctl_ioctl *p;
1347 snd_assert(fcn != NULL, return -EINVAL);
1348 down_write(&snd_ioctl_rwsem);
1349 list_for_each_entry(p, lists, list) {
1350 if (p->fioctl == fcn) {
1352 up_write(&snd_ioctl_rwsem);
1357 up_write(&snd_ioctl_rwsem);
1362 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1364 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1367 EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1369 #ifdef CONFIG_COMPAT
1370 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1372 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1375 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
1378 static int snd_ctl_fasync(int fd, struct file * file, int on)
1380 struct snd_ctl_file *ctl;
1382 ctl = file->private_data;
1383 err = fasync_helper(fd, file, on, &ctl->fasync);
1392 #ifdef CONFIG_COMPAT
1393 #include "control_compat.c"
1395 #define snd_ctl_ioctl_compat NULL
1402 static struct file_operations snd_ctl_f_ops =
1404 .owner = THIS_MODULE,
1405 .read = snd_ctl_read,
1406 .open = snd_ctl_open,
1407 .release = snd_ctl_release,
1408 .poll = snd_ctl_poll,
1409 .unlocked_ioctl = snd_ctl_ioctl,
1410 .compat_ioctl = snd_ctl_ioctl_compat,
1411 .fasync = snd_ctl_fasync,
1415 * registration of the control device
1417 static int snd_ctl_dev_register(struct snd_device *device)
1419 struct snd_card *card = device->device_data;
1423 snd_assert(card != NULL, return -ENXIO);
1424 cardnum = card->number;
1425 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1426 sprintf(name, "controlC%i", cardnum);
1427 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1428 &snd_ctl_f_ops, card, name)) < 0)
1434 * disconnection of the control device
1436 static int snd_ctl_dev_disconnect(struct snd_device *device)
1438 struct snd_card *card = device->device_data;
1439 struct snd_ctl_file *ctl;
1442 snd_assert(card != NULL, return -ENXIO);
1443 cardnum = card->number;
1444 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1446 down_read(&card->controls_rwsem);
1447 list_for_each_entry(ctl, &card->ctl_files, list) {
1448 wake_up(&ctl->change_sleep);
1449 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1451 up_read(&card->controls_rwsem);
1453 if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
1462 static int snd_ctl_dev_free(struct snd_device *device)
1464 struct snd_card *card = device->device_data;
1465 struct snd_kcontrol *control;
1467 down_write(&card->controls_rwsem);
1468 while (!list_empty(&card->controls)) {
1469 control = snd_kcontrol(card->controls.next);
1470 snd_ctl_remove(card, control);
1472 up_write(&card->controls_rwsem);
1477 * create control core:
1478 * called from init.c
1480 int snd_ctl_create(struct snd_card *card)
1482 static struct snd_device_ops ops = {
1483 .dev_free = snd_ctl_dev_free,
1484 .dev_register = snd_ctl_dev_register,
1485 .dev_disconnect = snd_ctl_dev_disconnect,
1488 snd_assert(card != NULL, return -ENXIO);
1489 return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);