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 list_head *list;
112 struct snd_card *card;
113 struct snd_ctl_file *ctl;
114 struct snd_kcontrol *control;
117 ctl = file->private_data;
118 fasync_helper(-1, file, 0, &ctl->fasync);
119 file->private_data = NULL;
121 write_lock_irqsave(&card->ctl_files_rwlock, flags);
122 list_del(&ctl->list);
123 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
124 down_write(&card->controls_rwsem);
125 list_for_each(list, &card->controls) {
126 control = snd_kcontrol(list);
127 for (idx = 0; idx < control->count; idx++)
128 if (control->vd[idx].owner == ctl)
129 control->vd[idx].owner = NULL;
131 up_write(&card->controls_rwsem);
132 snd_ctl_empty_read_queue(ctl);
134 module_put(card->module);
135 snd_card_file_remove(card, file);
139 void snd_ctl_notify(struct snd_card *card, unsigned int mask,
140 struct snd_ctl_elem_id *id)
143 struct list_head *flist;
144 struct snd_ctl_file *ctl;
145 struct snd_kctl_event *ev;
147 snd_assert(card != NULL && id != NULL, return);
148 read_lock(&card->ctl_files_rwlock);
149 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
150 card->mixer_oss_change_count++;
152 list_for_each(flist, &card->ctl_files) {
153 struct list_head *elist;
154 ctl = snd_ctl_file(flist);
155 if (!ctl->subscribed)
157 spin_lock_irqsave(&ctl->read_lock, flags);
158 list_for_each(elist, &ctl->events) {
159 ev = snd_kctl_event(elist);
160 if (ev->id.numid == id->numid) {
165 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
169 list_add_tail(&ev->list, &ctl->events);
171 snd_printk(KERN_ERR "No memory available to allocate event\n");
174 wake_up(&ctl->change_sleep);
175 spin_unlock_irqrestore(&ctl->read_lock, flags);
176 kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
178 read_unlock(&card->ctl_files_rwlock);
181 EXPORT_SYMBOL(snd_ctl_notify);
184 * snd_ctl_new - create a control instance from the template
185 * @control: the control template
186 * @access: the default control access
188 * Allocates a new struct snd_kcontrol instance and copies the given template
189 * to the new instance. It does not copy volatile data (access).
191 * Returns the pointer of the new instance, or NULL on failure.
193 struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control, unsigned int access)
195 struct snd_kcontrol *kctl;
198 snd_assert(control != NULL, return NULL);
199 snd_assert(control->count > 0, return NULL);
200 kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL);
202 snd_printk(KERN_ERR "Cannot allocate control instance\n");
206 for (idx = 0; idx < kctl->count; idx++)
207 kctl->vd[idx].access = access;
211 EXPORT_SYMBOL(snd_ctl_new);
214 * snd_ctl_new1 - create a control instance from the template
215 * @ncontrol: the initialization record
216 * @private_data: the private data to set
218 * Allocates a new struct snd_kcontrol instance and initialize from the given
219 * template. When the access field of ncontrol is 0, it's assumed as
220 * READWRITE access. When the count field is 0, it's assumes as one.
222 * Returns the pointer of the newly generated instance, or NULL on failure.
224 struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
227 struct snd_kcontrol kctl;
230 snd_assert(ncontrol != NULL, return NULL);
231 snd_assert(ncontrol->info != NULL, return NULL);
232 memset(&kctl, 0, sizeof(kctl));
233 kctl.id.iface = ncontrol->iface;
234 kctl.id.device = ncontrol->device;
235 kctl.id.subdevice = ncontrol->subdevice;
237 strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
238 kctl.id.index = ncontrol->index;
239 kctl.count = ncontrol->count ? ncontrol->count : 1;
240 access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
241 (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
242 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
243 SNDRV_CTL_ELEM_ACCESS_DINDIRECT|
244 SNDRV_CTL_ELEM_ACCESS_INDIRECT|
245 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE|
246 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK));
247 kctl.info = ncontrol->info;
248 kctl.get = ncontrol->get;
249 kctl.put = ncontrol->put;
250 kctl.tlv.p = ncontrol->tlv.p;
251 kctl.private_value = ncontrol->private_value;
252 kctl.private_data = private_data;
253 return snd_ctl_new(&kctl, access);
256 EXPORT_SYMBOL(snd_ctl_new1);
259 * snd_ctl_free_one - release the control instance
260 * @kcontrol: the control instance
262 * Releases the control instance created via snd_ctl_new()
264 * Don't call this after the control was added to the card.
266 void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
269 if (kcontrol->private_free)
270 kcontrol->private_free(kcontrol);
275 EXPORT_SYMBOL(snd_ctl_free_one);
277 static unsigned int snd_ctl_hole_check(struct snd_card *card,
280 struct list_head *list;
281 struct snd_kcontrol *kctl;
283 list_for_each(list, &card->controls) {
284 kctl = snd_kcontrol(list);
285 if ((kctl->id.numid <= card->last_numid &&
286 kctl->id.numid + kctl->count > card->last_numid) ||
287 (kctl->id.numid <= card->last_numid + count - 1 &&
288 kctl->id.numid + kctl->count > card->last_numid + count - 1))
289 return card->last_numid = kctl->id.numid + kctl->count - 1;
291 return card->last_numid;
294 static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
296 unsigned int last_numid, iter = 100000;
298 last_numid = card->last_numid;
299 while (last_numid != snd_ctl_hole_check(card, count)) {
301 /* this situation is very unlikely */
302 snd_printk(KERN_ERR "unable to allocate new control numid\n");
305 last_numid = card->last_numid;
311 * snd_ctl_add - add the control instance to the card
312 * @card: the card instance
313 * @kcontrol: the control instance to add
315 * Adds the control instance created via snd_ctl_new() or
316 * snd_ctl_new1() to the given card. Assigns also an unique
317 * numid used for fast search.
319 * Returns zero if successful, or a negative error code on failure.
321 * It frees automatically the control which cannot be added.
323 int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
325 struct snd_ctl_elem_id id;
331 snd_assert(card != NULL, goto error);
332 snd_assert(kcontrol->info != NULL, goto error);
334 down_write(&card->controls_rwsem);
335 if (snd_ctl_find_id(card, &id)) {
336 up_write(&card->controls_rwsem);
337 snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
346 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
347 up_write(&card->controls_rwsem);
351 list_add_tail(&kcontrol->list, &card->controls);
352 card->controls_count += kcontrol->count;
353 kcontrol->id.numid = card->last_numid + 1;
354 card->last_numid += kcontrol->count;
355 up_write(&card->controls_rwsem);
356 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
357 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
361 snd_ctl_free_one(kcontrol);
365 EXPORT_SYMBOL(snd_ctl_add);
368 * snd_ctl_remove - remove the control from the card and release it
369 * @card: the card instance
370 * @kcontrol: the control instance to remove
372 * Removes the control from the card and then releases the instance.
373 * You don't need to call snd_ctl_free_one(). You must be in
374 * the write lock - down_write(&card->controls_rwsem).
376 * Returns 0 if successful, or a negative error code on failure.
378 int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
380 struct snd_ctl_elem_id id;
383 snd_assert(card != NULL && kcontrol != NULL, return -EINVAL);
384 list_del(&kcontrol->list);
385 card->controls_count -= kcontrol->count;
387 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
388 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
389 snd_ctl_free_one(kcontrol);
393 EXPORT_SYMBOL(snd_ctl_remove);
396 * snd_ctl_remove_id - remove the control of the given id and release it
397 * @card: the card instance
398 * @id: the control id to remove
400 * Finds the control instance with the given id, removes it from the
401 * card list and releases it.
403 * Returns 0 if successful, or a negative error code on failure.
405 int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
407 struct snd_kcontrol *kctl;
410 down_write(&card->controls_rwsem);
411 kctl = snd_ctl_find_id(card, id);
413 up_write(&card->controls_rwsem);
416 ret = snd_ctl_remove(card, kctl);
417 up_write(&card->controls_rwsem);
421 EXPORT_SYMBOL(snd_ctl_remove_id);
424 * snd_ctl_remove_unlocked_id - remove the unlocked control of the given id and release it
425 * @file: active control handle
426 * @id: the control id to remove
428 * Finds the control instance with the given id, removes it from the
429 * card list and releases it.
431 * Returns 0 if successful, or a negative error code on failure.
433 static int snd_ctl_remove_unlocked_id(struct snd_ctl_file * file,
434 struct snd_ctl_elem_id *id)
436 struct snd_card *card = file->card;
437 struct snd_kcontrol *kctl;
440 down_write(&card->controls_rwsem);
441 kctl = snd_ctl_find_id(card, id);
443 up_write(&card->controls_rwsem);
446 for (idx = 0; idx < kctl->count; idx++)
447 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
448 up_write(&card->controls_rwsem);
451 ret = snd_ctl_remove(card, kctl);
452 up_write(&card->controls_rwsem);
457 * snd_ctl_rename_id - replace the id of a control on the card
458 * @card: the card instance
459 * @src_id: the old id
460 * @dst_id: the new id
462 * Finds the control with the old id from the card, and replaces the
463 * id with the new one.
465 * Returns zero if successful, or a negative error code on failure.
467 int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
468 struct snd_ctl_elem_id *dst_id)
470 struct snd_kcontrol *kctl;
472 down_write(&card->controls_rwsem);
473 kctl = snd_ctl_find_id(card, src_id);
475 up_write(&card->controls_rwsem);
479 kctl->id.numid = card->last_numid + 1;
480 card->last_numid += kctl->count;
481 up_write(&card->controls_rwsem);
485 EXPORT_SYMBOL(snd_ctl_rename_id);
488 * snd_ctl_find_numid - find the control instance with the given number-id
489 * @card: the card instance
490 * @numid: the number-id to search
492 * Finds the control instance with the given number-id from the card.
494 * Returns the pointer of the instance if found, or NULL if not.
496 * The caller must down card->controls_rwsem before calling this function
497 * (if the race condition can happen).
499 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
501 struct list_head *list;
502 struct snd_kcontrol *kctl;
504 snd_assert(card != NULL && numid != 0, return NULL);
505 list_for_each(list, &card->controls) {
506 kctl = snd_kcontrol(list);
507 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
513 EXPORT_SYMBOL(snd_ctl_find_numid);
516 * snd_ctl_find_id - find the control instance with the given id
517 * @card: the card instance
518 * @id: the id to search
520 * Finds the control instance with the given id from the card.
522 * Returns the pointer of the instance if found, or NULL if not.
524 * The caller must down card->controls_rwsem before calling this function
525 * (if the race condition can happen).
527 struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
528 struct snd_ctl_elem_id *id)
530 struct list_head *list;
531 struct snd_kcontrol *kctl;
533 snd_assert(card != NULL && id != NULL, return NULL);
535 return snd_ctl_find_numid(card, id->numid);
536 list_for_each(list, &card->controls) {
537 kctl = snd_kcontrol(list);
538 if (kctl->id.iface != id->iface)
540 if (kctl->id.device != id->device)
542 if (kctl->id.subdevice != id->subdevice)
544 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
546 if (kctl->id.index > id->index)
548 if (kctl->id.index + kctl->count <= id->index)
555 EXPORT_SYMBOL(snd_ctl_find_id);
557 static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
558 unsigned int cmd, void __user *arg)
560 struct snd_ctl_card_info *info;
562 info = kzalloc(sizeof(*info), GFP_KERNEL);
565 down_read(&snd_ioctl_rwsem);
566 info->card = card->number;
567 strlcpy(info->id, card->id, sizeof(info->id));
568 strlcpy(info->driver, card->driver, sizeof(info->driver));
569 strlcpy(info->name, card->shortname, sizeof(info->name));
570 strlcpy(info->longname, card->longname, sizeof(info->longname));
571 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
572 strlcpy(info->components, card->components, sizeof(info->components));
573 up_read(&snd_ioctl_rwsem);
574 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
582 static int snd_ctl_elem_list(struct snd_card *card,
583 struct snd_ctl_elem_list __user *_list)
585 struct list_head *plist;
586 struct snd_ctl_elem_list list;
587 struct snd_kcontrol *kctl;
588 struct snd_ctl_elem_id *dst, *id;
589 unsigned int offset, space, first, jidx;
591 if (copy_from_user(&list, _list, sizeof(list)))
593 offset = list.offset;
596 /* try limit maximum space */
600 /* allocate temporary buffer for atomic operation */
601 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
604 down_read(&card->controls_rwsem);
605 list.count = card->controls_count;
606 plist = card->controls.next;
607 while (plist != &card->controls) {
610 kctl = snd_kcontrol(plist);
611 if (offset < kctl->count)
613 offset -= kctl->count;
618 while (space > 0 && plist != &card->controls) {
619 kctl = snd_kcontrol(plist);
620 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
621 snd_ctl_build_ioff(id, kctl, jidx);
629 up_read(&card->controls_rwsem);
631 copy_to_user(list.pids, dst,
632 list.used * sizeof(struct snd_ctl_elem_id))) {
638 down_read(&card->controls_rwsem);
639 list.count = card->controls_count;
640 up_read(&card->controls_rwsem);
642 if (copy_to_user(_list, &list, sizeof(list)))
647 static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
648 struct snd_ctl_elem_info *info)
650 struct snd_card *card = ctl->card;
651 struct snd_kcontrol *kctl;
652 struct snd_kcontrol_volatile *vd;
653 unsigned int index_offset;
656 down_read(&card->controls_rwsem);
657 kctl = snd_ctl_find_id(card, &info->id);
659 up_read(&card->controls_rwsem);
662 #ifdef CONFIG_SND_DEBUG
665 result = kctl->info(kctl, info);
667 snd_assert(info->access == 0, );
668 index_offset = snd_ctl_get_ioff(kctl, &info->id);
669 vd = &kctl->vd[index_offset];
670 snd_ctl_build_ioff(&info->id, kctl, index_offset);
671 info->access = vd->access;
673 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
674 if (vd->owner == ctl)
675 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
676 info->owner = vd->owner_pid;
681 up_read(&card->controls_rwsem);
685 static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
686 struct snd_ctl_elem_info __user *_info)
688 struct snd_ctl_elem_info info;
691 if (copy_from_user(&info, _info, sizeof(info)))
693 snd_power_lock(ctl->card);
694 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
696 result = snd_ctl_elem_info(ctl, &info);
697 snd_power_unlock(ctl->card);
699 if (copy_to_user(_info, &info, sizeof(info)))
704 int snd_ctl_elem_read(struct snd_card *card, struct snd_ctl_elem_value *control)
706 struct snd_kcontrol *kctl;
707 struct snd_kcontrol_volatile *vd;
708 unsigned int index_offset;
709 int result, indirect;
711 down_read(&card->controls_rwsem);
712 kctl = snd_ctl_find_id(card, &control->id);
716 index_offset = snd_ctl_get_ioff(kctl, &control->id);
717 vd = &kctl->vd[index_offset];
718 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
719 if (control->indirect != indirect) {
722 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) && kctl->get != NULL) {
723 snd_ctl_build_ioff(&control->id, kctl, index_offset);
724 result = kctl->get(kctl, control);
730 up_read(&card->controls_rwsem);
734 EXPORT_SYMBOL(snd_ctl_elem_read);
736 static int snd_ctl_elem_read_user(struct snd_card *card,
737 struct snd_ctl_elem_value __user *_control)
739 struct snd_ctl_elem_value *control;
742 control = kmalloc(sizeof(*control), GFP_KERNEL);
745 if (copy_from_user(control, _control, sizeof(*control))) {
749 snd_power_lock(card);
750 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
752 result = snd_ctl_elem_read(card, control);
753 snd_power_unlock(card);
755 if (copy_to_user(_control, control, sizeof(*control)))
761 int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
762 struct snd_ctl_elem_value *control)
764 struct snd_kcontrol *kctl;
765 struct snd_kcontrol_volatile *vd;
766 unsigned int index_offset;
767 int result, indirect;
769 down_read(&card->controls_rwsem);
770 kctl = snd_ctl_find_id(card, &control->id);
774 index_offset = snd_ctl_get_ioff(kctl, &control->id);
775 vd = &kctl->vd[index_offset];
776 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
777 if (control->indirect != indirect) {
780 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
782 (file && vd->owner != NULL && vd->owner != file)) {
785 snd_ctl_build_ioff(&control->id, kctl, index_offset);
786 result = kctl->put(kctl, control);
789 up_read(&card->controls_rwsem);
790 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &control->id);
795 up_read(&card->controls_rwsem);
799 EXPORT_SYMBOL(snd_ctl_elem_write);
801 static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
802 struct snd_ctl_elem_value __user *_control)
804 struct snd_ctl_elem_value *control;
805 struct snd_card *card;
808 control = kmalloc(sizeof(*control), GFP_KERNEL);
811 if (copy_from_user(control, _control, sizeof(*control))) {
816 snd_power_lock(card);
817 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
819 result = snd_ctl_elem_write(card, file, control);
820 snd_power_unlock(card);
822 if (copy_to_user(_control, control, sizeof(*control)))
828 static int snd_ctl_elem_lock(struct snd_ctl_file *file,
829 struct snd_ctl_elem_id __user *_id)
831 struct snd_card *card = file->card;
832 struct snd_ctl_elem_id id;
833 struct snd_kcontrol *kctl;
834 struct snd_kcontrol_volatile *vd;
837 if (copy_from_user(&id, _id, sizeof(id)))
839 down_write(&card->controls_rwsem);
840 kctl = snd_ctl_find_id(card, &id);
844 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
845 if (vd->owner != NULL)
849 vd->owner_pid = current->pid;
853 up_write(&card->controls_rwsem);
857 static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
858 struct snd_ctl_elem_id __user *_id)
860 struct snd_card *card = file->card;
861 struct snd_ctl_elem_id id;
862 struct snd_kcontrol *kctl;
863 struct snd_kcontrol_volatile *vd;
866 if (copy_from_user(&id, _id, sizeof(id)))
868 down_write(&card->controls_rwsem);
869 kctl = snd_ctl_find_id(card, &id);
873 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
874 if (vd->owner == NULL)
876 else if (vd->owner != file)
884 up_write(&card->controls_rwsem);
888 struct user_element {
889 struct snd_ctl_elem_info info;
890 void *elem_data; /* element data */
891 unsigned long elem_data_size; /* size of element data in bytes */
892 void *tlv_data; /* TLV data */
893 unsigned long tlv_data_size; /* TLV data size */
894 void *priv_data; /* private data (like strings for enumerated type) */
895 unsigned long priv_data_size; /* size of private data in bytes */
898 static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
899 struct snd_ctl_elem_info *uinfo)
901 struct user_element *ue = kcontrol->private_data;
907 static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
908 struct snd_ctl_elem_value *ucontrol)
910 struct user_element *ue = kcontrol->private_data;
912 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
916 static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
917 struct snd_ctl_elem_value *ucontrol)
920 struct user_element *ue = kcontrol->private_data;
922 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
924 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
928 static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol,
931 unsigned int __user *tlv)
933 struct user_element *ue = kcontrol->private_data;
938 if (size > 1024 * 128) /* sane value */
940 new_data = kmalloc(size, GFP_KERNEL);
941 if (new_data == NULL)
943 if (copy_from_user(new_data, tlv, size)) {
947 change = ue->tlv_data_size != size;
949 change = memcmp(ue->tlv_data, new_data, size);
951 ue->tlv_data = new_data;
952 ue->tlv_data_size = size;
954 if (! ue->tlv_data_size || ! ue->tlv_data)
956 if (size < ue->tlv_data_size)
958 if (copy_to_user(tlv, ue->tlv_data, ue->tlv_data_size))
964 static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
966 struct user_element *ue = kcontrol->private_data;
972 static int snd_ctl_elem_add(struct snd_ctl_file *file,
973 struct snd_ctl_elem_info *info, int replace)
975 struct snd_card *card = file->card;
976 struct snd_kcontrol kctl, *_kctl;
979 struct user_element *ue;
982 if (card->user_ctl_count >= MAX_USER_CONTROLS)
984 if (info->count > 1024)
986 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
987 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
988 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
989 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE));
991 memset(&kctl, 0, sizeof(kctl));
992 down_write(&card->controls_rwsem);
993 _kctl = snd_ctl_find_id(card, &info->id);
997 err = snd_ctl_remove(card, _kctl);
1004 up_write(&card->controls_rwsem);
1007 memcpy(&kctl.id, &info->id, sizeof(info->id));
1008 kctl.count = info->owner ? info->owner : 1;
1009 access |= SNDRV_CTL_ELEM_ACCESS_USER;
1010 kctl.info = snd_ctl_elem_user_info;
1011 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1012 kctl.get = snd_ctl_elem_user_get;
1013 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1014 kctl.put = snd_ctl_elem_user_put;
1015 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
1016 kctl.tlv.c = snd_ctl_elem_user_tlv;
1017 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1019 switch (info->type) {
1020 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
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->info.access = 0;
1050 ue->elem_data = (char *)ue + sizeof(*ue);
1051 ue->elem_data_size = private_size;
1052 kctl.private_free = snd_ctl_elem_user_free;
1053 _kctl = snd_ctl_new(&kctl, access);
1054 if (_kctl == NULL) {
1058 _kctl->private_data = ue;
1059 for (idx = 0; idx < _kctl->count; idx++)
1060 _kctl->vd[idx].owner = file;
1061 err = snd_ctl_add(card, _kctl);
1065 down_write(&card->controls_rwsem);
1066 card->user_ctl_count++;
1067 up_write(&card->controls_rwsem);
1072 static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1073 struct snd_ctl_elem_info __user *_info, int replace)
1075 struct snd_ctl_elem_info info;
1076 if (copy_from_user(&info, _info, sizeof(info)))
1078 return snd_ctl_elem_add(file, &info, replace);
1081 static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1082 struct snd_ctl_elem_id __user *_id)
1084 struct snd_ctl_elem_id id;
1087 if (copy_from_user(&id, _id, sizeof(id)))
1089 err = snd_ctl_remove_unlocked_id(file, &id);
1091 struct snd_card *card = file->card;
1092 down_write(&card->controls_rwsem);
1093 card->user_ctl_count--;
1094 up_write(&card->controls_rwsem);
1099 static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
1102 if (get_user(subscribe, ptr))
1104 if (subscribe < 0) {
1105 subscribe = file->subscribed;
1106 if (put_user(subscribe, ptr))
1111 file->subscribed = 1;
1113 } else if (file->subscribed) {
1114 snd_ctl_empty_read_queue(file);
1115 file->subscribed = 0;
1120 static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1121 struct snd_ctl_tlv __user *_tlv,
1124 struct snd_card *card = file->card;
1125 struct snd_ctl_tlv tlv;
1126 struct snd_kcontrol *kctl;
1127 struct snd_kcontrol_volatile *vd;
1131 if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
1133 if (tlv.length < sizeof(unsigned int) * 3)
1135 down_read(&card->controls_rwsem);
1136 kctl = snd_ctl_find_numid(card, tlv.numid);
1141 if (kctl->tlv.p == NULL) {
1145 vd = &kctl->vd[tlv.numid - kctl->id.numid];
1146 if ((op_flag == 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) == 0) ||
1147 (op_flag > 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) == 0) ||
1148 (op_flag < 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND) == 0)) {
1152 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1153 if (file && vd->owner != NULL && vd->owner != file) {
1157 err = kctl->tlv.c(kctl, op_flag, tlv.length, _tlv->tlv);
1159 up_read(&card->controls_rwsem);
1160 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &kctl->id);
1168 len = kctl->tlv.p[1] + 2 * sizeof(unsigned int);
1169 if (tlv.length < len) {
1173 if (copy_to_user(_tlv->tlv, kctl->tlv.p, len))
1177 up_read(&card->controls_rwsem);
1181 static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1183 struct snd_ctl_file *ctl;
1184 struct snd_card *card;
1185 struct list_head *list;
1186 struct snd_kctl_ioctl *p;
1187 void __user *argp = (void __user *)arg;
1188 int __user *ip = argp;
1191 ctl = file->private_data;
1193 snd_assert(card != NULL, return -ENXIO);
1195 case SNDRV_CTL_IOCTL_PVERSION:
1196 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1197 case SNDRV_CTL_IOCTL_CARD_INFO:
1198 return snd_ctl_card_info(card, ctl, cmd, argp);
1199 case SNDRV_CTL_IOCTL_ELEM_LIST:
1200 return snd_ctl_elem_list(card, argp);
1201 case SNDRV_CTL_IOCTL_ELEM_INFO:
1202 return snd_ctl_elem_info_user(ctl, argp);
1203 case SNDRV_CTL_IOCTL_ELEM_READ:
1204 return snd_ctl_elem_read_user(card, argp);
1205 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1206 return snd_ctl_elem_write_user(ctl, argp);
1207 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1208 return snd_ctl_elem_lock(ctl, argp);
1209 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1210 return snd_ctl_elem_unlock(ctl, argp);
1211 case SNDRV_CTL_IOCTL_ELEM_ADD:
1212 return snd_ctl_elem_add_user(ctl, argp, 0);
1213 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1214 return snd_ctl_elem_add_user(ctl, argp, 1);
1215 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1216 return snd_ctl_elem_remove(ctl, argp);
1217 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1218 return snd_ctl_subscribe_events(ctl, ip);
1219 case SNDRV_CTL_IOCTL_TLV_READ:
1220 return snd_ctl_tlv_ioctl(ctl, argp, 0);
1221 case SNDRV_CTL_IOCTL_TLV_WRITE:
1222 return snd_ctl_tlv_ioctl(ctl, argp, 1);
1223 case SNDRV_CTL_IOCTL_TLV_COMMAND:
1224 return snd_ctl_tlv_ioctl(ctl, argp, -1);
1225 case SNDRV_CTL_IOCTL_POWER:
1226 return -ENOPROTOOPT;
1227 case SNDRV_CTL_IOCTL_POWER_STATE:
1229 return put_user(card->power_state, ip) ? -EFAULT : 0;
1231 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1234 down_read(&snd_ioctl_rwsem);
1235 list_for_each(list, &snd_control_ioctls) {
1236 p = list_entry(list, struct snd_kctl_ioctl, list);
1237 err = p->fioctl(card, ctl, cmd, arg);
1238 if (err != -ENOIOCTLCMD) {
1239 up_read(&snd_ioctl_rwsem);
1243 up_read(&snd_ioctl_rwsem);
1244 snd_printdd("unknown ioctl = 0x%x\n", cmd);
1248 static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1249 size_t count, loff_t * offset)
1251 struct snd_ctl_file *ctl;
1255 ctl = file->private_data;
1256 snd_assert(ctl != NULL && ctl->card != NULL, return -ENXIO);
1257 if (!ctl->subscribed)
1259 if (count < sizeof(struct snd_ctl_event))
1261 spin_lock_irq(&ctl->read_lock);
1262 while (count >= sizeof(struct snd_ctl_event)) {
1263 struct snd_ctl_event ev;
1264 struct snd_kctl_event *kev;
1265 while (list_empty(&ctl->events)) {
1267 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1271 init_waitqueue_entry(&wait, current);
1272 add_wait_queue(&ctl->change_sleep, &wait);
1273 set_current_state(TASK_INTERRUPTIBLE);
1274 spin_unlock_irq(&ctl->read_lock);
1276 remove_wait_queue(&ctl->change_sleep, &wait);
1277 if (signal_pending(current))
1278 return -ERESTARTSYS;
1279 spin_lock_irq(&ctl->read_lock);
1281 kev = snd_kctl_event(ctl->events.next);
1282 ev.type = SNDRV_CTL_EVENT_ELEM;
1283 ev.data.elem.mask = kev->mask;
1284 ev.data.elem.id = kev->id;
1285 list_del(&kev->list);
1286 spin_unlock_irq(&ctl->read_lock);
1288 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
1292 spin_lock_irq(&ctl->read_lock);
1293 buffer += sizeof(struct snd_ctl_event);
1294 count -= sizeof(struct snd_ctl_event);
1295 result += sizeof(struct snd_ctl_event);
1298 spin_unlock_irq(&ctl->read_lock);
1300 return result > 0 ? result : err;
1303 static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1306 struct snd_ctl_file *ctl;
1308 ctl = file->private_data;
1309 if (!ctl->subscribed)
1311 poll_wait(file, &ctl->change_sleep, wait);
1314 if (!list_empty(&ctl->events))
1315 mask |= POLLIN | POLLRDNORM;
1321 * register the device-specific control-ioctls.
1322 * called from each device manager like pcm.c, hwdep.c, etc.
1324 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1326 struct snd_kctl_ioctl *pn;
1328 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
1332 down_write(&snd_ioctl_rwsem);
1333 list_add_tail(&pn->list, lists);
1334 up_write(&snd_ioctl_rwsem);
1338 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1340 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1343 EXPORT_SYMBOL(snd_ctl_register_ioctl);
1345 #ifdef CONFIG_COMPAT
1346 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1348 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1351 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
1355 * de-register the device-specific control-ioctls.
1357 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1358 struct list_head *lists)
1360 struct list_head *list;
1361 struct snd_kctl_ioctl *p;
1363 snd_assert(fcn != NULL, return -EINVAL);
1364 down_write(&snd_ioctl_rwsem);
1365 list_for_each(list, lists) {
1366 p = list_entry(list, struct snd_kctl_ioctl, list);
1367 if (p->fioctl == fcn) {
1369 up_write(&snd_ioctl_rwsem);
1374 up_write(&snd_ioctl_rwsem);
1379 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1381 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1384 EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1386 #ifdef CONFIG_COMPAT
1387 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1389 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1392 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
1395 static int snd_ctl_fasync(int fd, struct file * file, int on)
1397 struct snd_ctl_file *ctl;
1399 ctl = file->private_data;
1400 err = fasync_helper(fd, file, on, &ctl->fasync);
1409 #ifdef CONFIG_COMPAT
1410 #include "control_compat.c"
1412 #define snd_ctl_ioctl_compat NULL
1419 static struct file_operations snd_ctl_f_ops =
1421 .owner = THIS_MODULE,
1422 .read = snd_ctl_read,
1423 .open = snd_ctl_open,
1424 .release = snd_ctl_release,
1425 .poll = snd_ctl_poll,
1426 .unlocked_ioctl = snd_ctl_ioctl,
1427 .compat_ioctl = snd_ctl_ioctl_compat,
1428 .fasync = snd_ctl_fasync,
1432 * registration of the control device
1434 static int snd_ctl_dev_register(struct snd_device *device)
1436 struct snd_card *card = device->device_data;
1440 snd_assert(card != NULL, return -ENXIO);
1441 cardnum = card->number;
1442 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1443 sprintf(name, "controlC%i", cardnum);
1444 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1445 &snd_ctl_f_ops, card, name)) < 0)
1451 * disconnection of the control device
1453 static int snd_ctl_dev_disconnect(struct snd_device *device)
1455 struct snd_card *card = device->device_data;
1456 struct list_head *flist;
1457 struct snd_ctl_file *ctl;
1460 snd_assert(card != NULL, return -ENXIO);
1461 cardnum = card->number;
1462 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1464 down_read(&card->controls_rwsem);
1465 list_for_each(flist, &card->ctl_files) {
1466 ctl = snd_ctl_file(flist);
1467 wake_up(&ctl->change_sleep);
1468 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1470 up_read(&card->controls_rwsem);
1472 if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
1481 static int snd_ctl_dev_free(struct snd_device *device)
1483 struct snd_card *card = device->device_data;
1484 struct snd_kcontrol *control;
1486 down_write(&card->controls_rwsem);
1487 while (!list_empty(&card->controls)) {
1488 control = snd_kcontrol(card->controls.next);
1489 snd_ctl_remove(card, control);
1491 up_write(&card->controls_rwsem);
1496 * create control core:
1497 * called from init.c
1499 int snd_ctl_create(struct snd_card *card)
1501 static struct snd_device_ops ops = {
1502 .dev_free = snd_ctl_dev_free,
1503 .dev_register = snd_ctl_dev_register,
1504 .dev_disconnect = snd_ctl_dev_disconnect,
1507 snd_assert(card != NULL, return -ENXIO);
1508 return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);