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.tlv = ncontrol->tlv;
245 kctl.private_value = ncontrol->private_value;
246 kctl.private_data = private_data;
247 return snd_ctl_new(&kctl, access);
250 EXPORT_SYMBOL(snd_ctl_new1);
253 * snd_ctl_free_one - release the control instance
254 * @kcontrol: the control instance
256 * Releases the control instance created via snd_ctl_new()
258 * Don't call this after the control was added to the card.
260 void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
263 if (kcontrol->private_free)
264 kcontrol->private_free(kcontrol);
269 EXPORT_SYMBOL(snd_ctl_free_one);
271 static unsigned int snd_ctl_hole_check(struct snd_card *card,
274 struct list_head *list;
275 struct snd_kcontrol *kctl;
277 list_for_each(list, &card->controls) {
278 kctl = snd_kcontrol(list);
279 if ((kctl->id.numid <= card->last_numid &&
280 kctl->id.numid + kctl->count > card->last_numid) ||
281 (kctl->id.numid <= card->last_numid + count - 1 &&
282 kctl->id.numid + kctl->count > card->last_numid + count - 1))
283 return card->last_numid = kctl->id.numid + kctl->count - 1;
285 return card->last_numid;
288 static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
290 unsigned int last_numid, iter = 100000;
292 last_numid = card->last_numid;
293 while (last_numid != snd_ctl_hole_check(card, count)) {
295 /* this situation is very unlikely */
296 snd_printk(KERN_ERR "unable to allocate new control numid\n");
299 last_numid = card->last_numid;
305 * snd_ctl_add - add the control instance to the card
306 * @card: the card instance
307 * @kcontrol: the control instance to add
309 * Adds the control instance created via snd_ctl_new() or
310 * snd_ctl_new1() to the given card. Assigns also an unique
311 * numid used for fast search.
313 * Returns zero if successful, or a negative error code on failure.
315 * It frees automatically the control which cannot be added.
317 int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
319 struct snd_ctl_elem_id id;
325 snd_assert(card != NULL, goto error);
326 snd_assert(kcontrol->info != NULL, goto error);
328 down_write(&card->controls_rwsem);
329 if (snd_ctl_find_id(card, &id)) {
330 up_write(&card->controls_rwsem);
331 snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
340 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
341 up_write(&card->controls_rwsem);
345 list_add_tail(&kcontrol->list, &card->controls);
346 card->controls_count += kcontrol->count;
347 kcontrol->id.numid = card->last_numid + 1;
348 card->last_numid += kcontrol->count;
349 up_write(&card->controls_rwsem);
350 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
351 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
355 snd_ctl_free_one(kcontrol);
359 EXPORT_SYMBOL(snd_ctl_add);
362 * snd_ctl_remove - remove the control from the card and release it
363 * @card: the card instance
364 * @kcontrol: the control instance to remove
366 * Removes the control from the card and then releases the instance.
367 * You don't need to call snd_ctl_free_one(). You must be in
368 * the write lock - down_write(&card->controls_rwsem).
370 * Returns 0 if successful, or a negative error code on failure.
372 int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
374 struct snd_ctl_elem_id id;
377 snd_assert(card != NULL && kcontrol != NULL, return -EINVAL);
378 list_del(&kcontrol->list);
379 card->controls_count -= kcontrol->count;
381 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
382 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
383 snd_ctl_free_one(kcontrol);
387 EXPORT_SYMBOL(snd_ctl_remove);
390 * snd_ctl_remove_id - remove the control of the given id and release it
391 * @card: the card instance
392 * @id: the control id to remove
394 * Finds the control instance with the given id, removes it from the
395 * card list and releases it.
397 * Returns 0 if successful, or a negative error code on failure.
399 int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
401 struct snd_kcontrol *kctl;
404 down_write(&card->controls_rwsem);
405 kctl = snd_ctl_find_id(card, id);
407 up_write(&card->controls_rwsem);
410 ret = snd_ctl_remove(card, kctl);
411 up_write(&card->controls_rwsem);
415 EXPORT_SYMBOL(snd_ctl_remove_id);
418 * snd_ctl_remove_unlocked_id - remove the unlocked control of the given id and release it
419 * @file: active control handle
420 * @id: the control id to remove
422 * Finds the control instance with the given id, removes it from the
423 * card list and releases it.
425 * Returns 0 if successful, or a negative error code on failure.
427 static int snd_ctl_remove_unlocked_id(struct snd_ctl_file * file,
428 struct snd_ctl_elem_id *id)
430 struct snd_card *card = file->card;
431 struct snd_kcontrol *kctl;
434 down_write(&card->controls_rwsem);
435 kctl = snd_ctl_find_id(card, id);
437 up_write(&card->controls_rwsem);
440 for (idx = 0; idx < kctl->count; idx++)
441 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
442 up_write(&card->controls_rwsem);
445 ret = snd_ctl_remove(card, kctl);
446 up_write(&card->controls_rwsem);
451 * snd_ctl_rename_id - replace the id of a control on the card
452 * @card: the card instance
453 * @src_id: the old id
454 * @dst_id: the new id
456 * Finds the control with the old id from the card, and replaces the
457 * id with the new one.
459 * Returns zero if successful, or a negative error code on failure.
461 int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
462 struct snd_ctl_elem_id *dst_id)
464 struct snd_kcontrol *kctl;
466 down_write(&card->controls_rwsem);
467 kctl = snd_ctl_find_id(card, src_id);
469 up_write(&card->controls_rwsem);
473 kctl->id.numid = card->last_numid + 1;
474 card->last_numid += kctl->count;
475 up_write(&card->controls_rwsem);
479 EXPORT_SYMBOL(snd_ctl_rename_id);
482 * snd_ctl_find_numid - find the control instance with the given number-id
483 * @card: the card instance
484 * @numid: the number-id to search
486 * Finds the control instance with the given number-id from the card.
488 * Returns the pointer of the instance if found, or NULL if not.
490 * The caller must down card->controls_rwsem before calling this function
491 * (if the race condition can happen).
493 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
495 struct list_head *list;
496 struct snd_kcontrol *kctl;
498 snd_assert(card != NULL && numid != 0, return NULL);
499 list_for_each(list, &card->controls) {
500 kctl = snd_kcontrol(list);
501 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
507 EXPORT_SYMBOL(snd_ctl_find_numid);
510 * snd_ctl_find_id - find the control instance with the given id
511 * @card: the card instance
512 * @id: the id to search
514 * Finds the control instance with the given id from the card.
516 * Returns the pointer of the instance if found, or NULL if not.
518 * The caller must down card->controls_rwsem before calling this function
519 * (if the race condition can happen).
521 struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
522 struct snd_ctl_elem_id *id)
524 struct list_head *list;
525 struct snd_kcontrol *kctl;
527 snd_assert(card != NULL && id != NULL, return NULL);
529 return snd_ctl_find_numid(card, id->numid);
530 list_for_each(list, &card->controls) {
531 kctl = snd_kcontrol(list);
532 if (kctl->id.iface != id->iface)
534 if (kctl->id.device != id->device)
536 if (kctl->id.subdevice != id->subdevice)
538 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
540 if (kctl->id.index > id->index)
542 if (kctl->id.index + kctl->count <= id->index)
549 EXPORT_SYMBOL(snd_ctl_find_id);
551 static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
552 unsigned int cmd, void __user *arg)
554 struct snd_ctl_card_info *info;
556 info = kzalloc(sizeof(*info), GFP_KERNEL);
559 down_read(&snd_ioctl_rwsem);
560 info->card = card->number;
561 strlcpy(info->id, card->id, sizeof(info->id));
562 strlcpy(info->driver, card->driver, sizeof(info->driver));
563 strlcpy(info->name, card->shortname, sizeof(info->name));
564 strlcpy(info->longname, card->longname, sizeof(info->longname));
565 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
566 strlcpy(info->components, card->components, sizeof(info->components));
567 up_read(&snd_ioctl_rwsem);
568 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
576 static int snd_ctl_elem_list(struct snd_card *card,
577 struct snd_ctl_elem_list __user *_list)
579 struct list_head *plist;
580 struct snd_ctl_elem_list list;
581 struct snd_kcontrol *kctl;
582 struct snd_ctl_elem_id *dst, *id;
583 unsigned int offset, space, first, jidx;
585 if (copy_from_user(&list, _list, sizeof(list)))
587 offset = list.offset;
590 /* try limit maximum space */
594 /* allocate temporary buffer for atomic operation */
595 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
598 down_read(&card->controls_rwsem);
599 list.count = card->controls_count;
600 plist = card->controls.next;
601 while (plist != &card->controls) {
604 kctl = snd_kcontrol(plist);
605 if (offset < kctl->count)
607 offset -= kctl->count;
612 while (space > 0 && plist != &card->controls) {
613 kctl = snd_kcontrol(plist);
614 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
615 snd_ctl_build_ioff(id, kctl, jidx);
623 up_read(&card->controls_rwsem);
625 copy_to_user(list.pids, dst,
626 list.used * sizeof(struct snd_ctl_elem_id))) {
632 down_read(&card->controls_rwsem);
633 list.count = card->controls_count;
634 up_read(&card->controls_rwsem);
636 if (copy_to_user(_list, &list, sizeof(list)))
641 static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
642 struct snd_ctl_elem_info *info)
644 struct snd_card *card = ctl->card;
645 struct snd_kcontrol *kctl;
646 struct snd_kcontrol_volatile *vd;
647 unsigned int index_offset;
650 down_read(&card->controls_rwsem);
651 kctl = snd_ctl_find_id(card, &info->id);
653 up_read(&card->controls_rwsem);
656 #ifdef CONFIG_SND_DEBUG
659 result = kctl->info(kctl, info);
661 snd_assert(info->access == 0, );
662 index_offset = snd_ctl_get_ioff(kctl, &info->id);
663 vd = &kctl->vd[index_offset];
664 snd_ctl_build_ioff(&info->id, kctl, index_offset);
665 info->access = vd->access;
667 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
668 if (vd->owner == ctl)
669 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
670 info->owner = vd->owner_pid;
675 up_read(&card->controls_rwsem);
679 static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
680 struct snd_ctl_elem_info __user *_info)
682 struct snd_ctl_elem_info info;
685 if (copy_from_user(&info, _info, sizeof(info)))
687 snd_power_lock(ctl->card);
688 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
690 result = snd_ctl_elem_info(ctl, &info);
691 snd_power_unlock(ctl->card);
693 if (copy_to_user(_info, &info, sizeof(info)))
698 int snd_ctl_elem_read(struct snd_card *card, struct snd_ctl_elem_value *control)
700 struct snd_kcontrol *kctl;
701 struct snd_kcontrol_volatile *vd;
702 unsigned int index_offset;
703 int result, indirect;
705 down_read(&card->controls_rwsem);
706 kctl = snd_ctl_find_id(card, &control->id);
710 index_offset = snd_ctl_get_ioff(kctl, &control->id);
711 vd = &kctl->vd[index_offset];
712 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
713 if (control->indirect != indirect) {
716 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) && kctl->get != NULL) {
717 snd_ctl_build_ioff(&control->id, kctl, index_offset);
718 result = kctl->get(kctl, control);
724 up_read(&card->controls_rwsem);
728 EXPORT_SYMBOL(snd_ctl_elem_read);
730 static int snd_ctl_elem_read_user(struct snd_card *card,
731 struct snd_ctl_elem_value __user *_control)
733 struct snd_ctl_elem_value *control;
736 control = kmalloc(sizeof(*control), GFP_KERNEL);
739 if (copy_from_user(control, _control, sizeof(*control))) {
743 snd_power_lock(card);
744 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
746 result = snd_ctl_elem_read(card, control);
747 snd_power_unlock(card);
749 if (copy_to_user(_control, control, sizeof(*control)))
755 int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
756 struct snd_ctl_elem_value *control)
758 struct snd_kcontrol *kctl;
759 struct snd_kcontrol_volatile *vd;
760 unsigned int index_offset;
761 int result, indirect;
763 down_read(&card->controls_rwsem);
764 kctl = snd_ctl_find_id(card, &control->id);
768 index_offset = snd_ctl_get_ioff(kctl, &control->id);
769 vd = &kctl->vd[index_offset];
770 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
771 if (control->indirect != indirect) {
774 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
776 (file && vd->owner != NULL && vd->owner != file)) {
779 snd_ctl_build_ioff(&control->id, kctl, index_offset);
780 result = kctl->put(kctl, control);
783 up_read(&card->controls_rwsem);
784 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &control->id);
789 up_read(&card->controls_rwsem);
793 EXPORT_SYMBOL(snd_ctl_elem_write);
795 static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
796 struct snd_ctl_elem_value __user *_control)
798 struct snd_ctl_elem_value *control;
799 struct snd_card *card;
802 control = kmalloc(sizeof(*control), GFP_KERNEL);
805 if (copy_from_user(control, _control, sizeof(*control))) {
810 snd_power_lock(card);
811 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
813 result = snd_ctl_elem_write(card, file, control);
814 snd_power_unlock(card);
816 if (copy_to_user(_control, control, sizeof(*control)))
822 static int snd_ctl_elem_lock(struct snd_ctl_file *file,
823 struct snd_ctl_elem_id __user *_id)
825 struct snd_card *card = file->card;
826 struct snd_ctl_elem_id id;
827 struct snd_kcontrol *kctl;
828 struct snd_kcontrol_volatile *vd;
831 if (copy_from_user(&id, _id, sizeof(id)))
833 down_write(&card->controls_rwsem);
834 kctl = snd_ctl_find_id(card, &id);
838 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
839 if (vd->owner != NULL)
843 vd->owner_pid = current->pid;
847 up_write(&card->controls_rwsem);
851 static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
852 struct snd_ctl_elem_id __user *_id)
854 struct snd_card *card = file->card;
855 struct snd_ctl_elem_id id;
856 struct snd_kcontrol *kctl;
857 struct snd_kcontrol_volatile *vd;
860 if (copy_from_user(&id, _id, sizeof(id)))
862 down_write(&card->controls_rwsem);
863 kctl = snd_ctl_find_id(card, &id);
867 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
868 if (vd->owner == NULL)
870 else if (vd->owner != file)
878 up_write(&card->controls_rwsem);
882 struct user_element {
883 struct snd_ctl_elem_info info;
884 void *elem_data; /* element data */
885 unsigned long elem_data_size; /* size of element data in bytes */
886 void *priv_data; /* private data (like strings for enumerated type) */
887 unsigned long priv_data_size; /* size of private data in bytes */
890 static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
891 struct snd_ctl_elem_info *uinfo)
893 struct user_element *ue = kcontrol->private_data;
899 static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
900 struct snd_ctl_elem_value *ucontrol)
902 struct user_element *ue = kcontrol->private_data;
904 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
908 static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
909 struct snd_ctl_elem_value *ucontrol)
912 struct user_element *ue = kcontrol->private_data;
914 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
916 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
920 static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
922 kfree(kcontrol->private_data);
925 static int snd_ctl_elem_add(struct snd_ctl_file *file,
926 struct snd_ctl_elem_info *info, int replace)
928 struct snd_card *card = file->card;
929 struct snd_kcontrol kctl, *_kctl;
932 struct user_element *ue;
935 if (card->user_ctl_count >= MAX_USER_CONTROLS)
937 if (info->count > 1024)
939 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
940 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
941 SNDRV_CTL_ELEM_ACCESS_INACTIVE));
943 memset(&kctl, 0, sizeof(kctl));
944 down_write(&card->controls_rwsem);
945 _kctl = snd_ctl_find_id(card, &info->id);
949 err = snd_ctl_remove(card, _kctl);
956 up_write(&card->controls_rwsem);
959 memcpy(&kctl.id, &info->id, sizeof(info->id));
960 kctl.count = info->owner ? info->owner : 1;
961 access |= SNDRV_CTL_ELEM_ACCESS_USER;
962 kctl.info = snd_ctl_elem_user_info;
963 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
964 kctl.get = snd_ctl_elem_user_get;
965 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
966 kctl.put = snd_ctl_elem_user_put;
967 switch (info->type) {
968 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
969 private_size = sizeof(char);
970 if (info->count > 128)
973 case SNDRV_CTL_ELEM_TYPE_INTEGER:
974 private_size = sizeof(long);
975 if (info->count > 128)
978 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
979 private_size = sizeof(long long);
980 if (info->count > 64)
983 case SNDRV_CTL_ELEM_TYPE_BYTES:
984 private_size = sizeof(unsigned char);
985 if (info->count > 512)
988 case SNDRV_CTL_ELEM_TYPE_IEC958:
989 private_size = sizeof(struct snd_aes_iec958);
990 if (info->count != 1)
996 private_size *= info->count;
997 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
1001 ue->elem_data = (char *)ue + sizeof(*ue);
1002 ue->elem_data_size = private_size;
1003 kctl.private_free = snd_ctl_elem_user_free;
1004 _kctl = snd_ctl_new(&kctl, access);
1005 if (_kctl == NULL) {
1009 _kctl->private_data = ue;
1010 for (idx = 0; idx < _kctl->count; idx++)
1011 _kctl->vd[idx].owner = file;
1012 err = snd_ctl_add(card, _kctl);
1016 down_write(&card->controls_rwsem);
1017 card->user_ctl_count++;
1018 up_write(&card->controls_rwsem);
1023 static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1024 struct snd_ctl_elem_info __user *_info, int replace)
1026 struct snd_ctl_elem_info info;
1027 if (copy_from_user(&info, _info, sizeof(info)))
1029 return snd_ctl_elem_add(file, &info, replace);
1032 static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1033 struct snd_ctl_elem_id __user *_id)
1035 struct snd_ctl_elem_id id;
1038 if (copy_from_user(&id, _id, sizeof(id)))
1040 err = snd_ctl_remove_unlocked_id(file, &id);
1042 struct snd_card *card = file->card;
1043 down_write(&card->controls_rwsem);
1044 card->user_ctl_count--;
1045 up_write(&card->controls_rwsem);
1050 static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
1053 if (get_user(subscribe, ptr))
1055 if (subscribe < 0) {
1056 subscribe = file->subscribed;
1057 if (put_user(subscribe, ptr))
1062 file->subscribed = 1;
1064 } else if (file->subscribed) {
1065 snd_ctl_empty_read_queue(file);
1066 file->subscribed = 0;
1071 static int snd_ctl_tlv_read(struct snd_card *card,
1072 struct snd_ctl_tlv __user *_tlv)
1074 struct snd_ctl_tlv tlv;
1075 struct snd_kcontrol *kctl;
1079 if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
1081 if (tlv.length < sizeof(unsigned int) * 3)
1083 down_read(&card->controls_rwsem);
1084 kctl = snd_ctl_find_numid(card, tlv.numid);
1089 if (kctl->tlv == NULL) {
1093 len = kctl->tlv[1] + 2 * sizeof(unsigned int);
1094 if (tlv.length < len) {
1098 if (copy_to_user(_tlv->tlv, kctl->tlv, len))
1101 up_read(&card->controls_rwsem);
1105 static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1107 struct snd_ctl_file *ctl;
1108 struct snd_card *card;
1109 struct list_head *list;
1110 struct snd_kctl_ioctl *p;
1111 void __user *argp = (void __user *)arg;
1112 int __user *ip = argp;
1115 ctl = file->private_data;
1117 snd_assert(card != NULL, return -ENXIO);
1119 case SNDRV_CTL_IOCTL_PVERSION:
1120 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1121 case SNDRV_CTL_IOCTL_CARD_INFO:
1122 return snd_ctl_card_info(card, ctl, cmd, argp);
1123 case SNDRV_CTL_IOCTL_ELEM_LIST:
1124 return snd_ctl_elem_list(card, argp);
1125 case SNDRV_CTL_IOCTL_ELEM_INFO:
1126 return snd_ctl_elem_info_user(ctl, argp);
1127 case SNDRV_CTL_IOCTL_ELEM_READ:
1128 return snd_ctl_elem_read_user(card, argp);
1129 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1130 return snd_ctl_elem_write_user(ctl, argp);
1131 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1132 return snd_ctl_elem_lock(ctl, argp);
1133 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1134 return snd_ctl_elem_unlock(ctl, argp);
1135 case SNDRV_CTL_IOCTL_ELEM_ADD:
1136 return snd_ctl_elem_add_user(ctl, argp, 0);
1137 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1138 return snd_ctl_elem_add_user(ctl, argp, 1);
1139 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1140 return snd_ctl_elem_remove(ctl, argp);
1141 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1142 return snd_ctl_subscribe_events(ctl, ip);
1143 case SNDRV_CTL_IOCTL_TLV_READ:
1144 return snd_ctl_tlv_read(card, argp);
1145 case SNDRV_CTL_IOCTL_POWER:
1146 return -ENOPROTOOPT;
1147 case SNDRV_CTL_IOCTL_POWER_STATE:
1149 return put_user(card->power_state, ip) ? -EFAULT : 0;
1151 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1154 down_read(&snd_ioctl_rwsem);
1155 list_for_each(list, &snd_control_ioctls) {
1156 p = list_entry(list, struct snd_kctl_ioctl, list);
1157 err = p->fioctl(card, ctl, cmd, arg);
1158 if (err != -ENOIOCTLCMD) {
1159 up_read(&snd_ioctl_rwsem);
1163 up_read(&snd_ioctl_rwsem);
1164 snd_printdd("unknown ioctl = 0x%x\n", cmd);
1168 static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1169 size_t count, loff_t * offset)
1171 struct snd_ctl_file *ctl;
1175 ctl = file->private_data;
1176 snd_assert(ctl != NULL && ctl->card != NULL, return -ENXIO);
1177 if (!ctl->subscribed)
1179 if (count < sizeof(struct snd_ctl_event))
1181 spin_lock_irq(&ctl->read_lock);
1182 while (count >= sizeof(struct snd_ctl_event)) {
1183 struct snd_ctl_event ev;
1184 struct snd_kctl_event *kev;
1185 while (list_empty(&ctl->events)) {
1187 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1191 init_waitqueue_entry(&wait, current);
1192 add_wait_queue(&ctl->change_sleep, &wait);
1193 set_current_state(TASK_INTERRUPTIBLE);
1194 spin_unlock_irq(&ctl->read_lock);
1196 remove_wait_queue(&ctl->change_sleep, &wait);
1197 if (signal_pending(current))
1198 return result > 0 ? result : -ERESTARTSYS;
1199 spin_lock_irq(&ctl->read_lock);
1201 kev = snd_kctl_event(ctl->events.next);
1202 ev.type = SNDRV_CTL_EVENT_ELEM;
1203 ev.data.elem.mask = kev->mask;
1204 ev.data.elem.id = kev->id;
1205 list_del(&kev->list);
1206 spin_unlock_irq(&ctl->read_lock);
1208 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
1212 spin_lock_irq(&ctl->read_lock);
1213 buffer += sizeof(struct snd_ctl_event);
1214 count -= sizeof(struct snd_ctl_event);
1215 result += sizeof(struct snd_ctl_event);
1218 spin_unlock_irq(&ctl->read_lock);
1220 return result > 0 ? result : err;
1223 static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1226 struct snd_ctl_file *ctl;
1228 ctl = file->private_data;
1229 if (!ctl->subscribed)
1231 poll_wait(file, &ctl->change_sleep, wait);
1234 if (!list_empty(&ctl->events))
1235 mask |= POLLIN | POLLRDNORM;
1241 * register the device-specific control-ioctls.
1242 * called from each device manager like pcm.c, hwdep.c, etc.
1244 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1246 struct snd_kctl_ioctl *pn;
1248 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
1252 down_write(&snd_ioctl_rwsem);
1253 list_add_tail(&pn->list, lists);
1254 up_write(&snd_ioctl_rwsem);
1258 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1260 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1263 EXPORT_SYMBOL(snd_ctl_register_ioctl);
1265 #ifdef CONFIG_COMPAT
1266 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1268 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1271 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
1275 * de-register the device-specific control-ioctls.
1277 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1278 struct list_head *lists)
1280 struct list_head *list;
1281 struct snd_kctl_ioctl *p;
1283 snd_assert(fcn != NULL, return -EINVAL);
1284 down_write(&snd_ioctl_rwsem);
1285 list_for_each(list, lists) {
1286 p = list_entry(list, struct snd_kctl_ioctl, list);
1287 if (p->fioctl == fcn) {
1289 up_write(&snd_ioctl_rwsem);
1294 up_write(&snd_ioctl_rwsem);
1299 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1301 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1304 EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1306 #ifdef CONFIG_COMPAT
1307 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1309 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1312 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
1315 static int snd_ctl_fasync(int fd, struct file * file, int on)
1317 struct snd_ctl_file *ctl;
1319 ctl = file->private_data;
1320 err = fasync_helper(fd, file, on, &ctl->fasync);
1329 #ifdef CONFIG_COMPAT
1330 #include "control_compat.c"
1332 #define snd_ctl_ioctl_compat NULL
1339 static struct file_operations snd_ctl_f_ops =
1341 .owner = THIS_MODULE,
1342 .read = snd_ctl_read,
1343 .open = snd_ctl_open,
1344 .release = snd_ctl_release,
1345 .poll = snd_ctl_poll,
1346 .unlocked_ioctl = snd_ctl_ioctl,
1347 .compat_ioctl = snd_ctl_ioctl_compat,
1348 .fasync = snd_ctl_fasync,
1352 * registration of the control device
1354 static int snd_ctl_dev_register(struct snd_device *device)
1356 struct snd_card *card = device->device_data;
1360 snd_assert(card != NULL, return -ENXIO);
1361 cardnum = card->number;
1362 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1363 sprintf(name, "controlC%i", cardnum);
1364 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1365 &snd_ctl_f_ops, card, name)) < 0)
1371 * disconnection of the control device
1373 static int snd_ctl_dev_disconnect(struct snd_device *device)
1375 struct snd_card *card = device->device_data;
1376 struct list_head *flist;
1377 struct snd_ctl_file *ctl;
1379 down_read(&card->controls_rwsem);
1380 list_for_each(flist, &card->ctl_files) {
1381 ctl = snd_ctl_file(flist);
1382 wake_up(&ctl->change_sleep);
1383 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1385 up_read(&card->controls_rwsem);
1392 static int snd_ctl_dev_free(struct snd_device *device)
1394 struct snd_card *card = device->device_data;
1395 struct snd_kcontrol *control;
1397 down_write(&card->controls_rwsem);
1398 while (!list_empty(&card->controls)) {
1399 control = snd_kcontrol(card->controls.next);
1400 snd_ctl_remove(card, control);
1402 up_write(&card->controls_rwsem);
1407 * de-registration of the control device
1409 static int snd_ctl_dev_unregister(struct snd_device *device)
1411 struct snd_card *card = device->device_data;
1414 snd_assert(card != NULL, return -ENXIO);
1415 cardnum = card->number;
1416 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1417 if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
1420 return snd_ctl_dev_free(device);
1424 * create control core:
1425 * called from init.c
1427 int snd_ctl_create(struct snd_card *card)
1429 static struct snd_device_ops ops = {
1430 .dev_free = snd_ctl_dev_free,
1431 .dev_register = snd_ctl_dev_register,
1432 .dev_disconnect = snd_ctl_dev_disconnect,
1433 .dev_unregister = snd_ctl_dev_unregister
1436 snd_assert(card != NULL, return -ENXIO);
1437 return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);