2 * Routines for driver control interface
3 * Copyright (c) by Jaroslav Kysela <perex@perex.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 <linux/threads.h>
23 #include <linux/interrupt.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
26 #include <linux/time.h>
27 #include <sound/core.h>
28 #include <sound/minors.h>
29 #include <sound/info.h>
30 #include <sound/control.h>
32 /* max number of user-defined controls */
33 #define MAX_USER_CONTROLS 32
34 #define MAX_CONTROL_COUNT 1028
36 struct snd_kctl_ioctl {
37 struct list_head list; /* list of all ioctls */
38 snd_kctl_ioctl_func_t fioctl;
41 static DECLARE_RWSEM(snd_ioctl_rwsem);
42 static LIST_HEAD(snd_control_ioctls);
44 static LIST_HEAD(snd_control_compat_ioctls);
47 static int snd_ctl_open(struct inode *inode, struct file *file)
50 struct snd_card *card;
51 struct snd_ctl_file *ctl;
54 err = nonseekable_open(inode, file);
58 card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
63 err = snd_card_file_add(card, file);
68 if (!try_module_get(card->module)) {
72 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
77 INIT_LIST_HEAD(&ctl->events);
78 init_waitqueue_head(&ctl->change_sleep);
79 spin_lock_init(&ctl->read_lock);
81 ctl->prefer_pcm_subdevice = -1;
82 ctl->prefer_rawmidi_subdevice = -1;
83 ctl->pid = get_pid(task_pid(current));
84 file->private_data = ctl;
85 write_lock_irqsave(&card->ctl_files_rwlock, flags);
86 list_add_tail(&ctl->list, &card->ctl_files);
87 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
91 module_put(card->module);
93 snd_card_file_remove(card, file);
98 static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
101 struct snd_kctl_event *cread;
103 spin_lock_irqsave(&ctl->read_lock, flags);
104 while (!list_empty(&ctl->events)) {
105 cread = snd_kctl_event(ctl->events.next);
106 list_del(&cread->list);
109 spin_unlock_irqrestore(&ctl->read_lock, flags);
112 static int snd_ctl_release(struct inode *inode, struct file *file)
115 struct snd_card *card;
116 struct snd_ctl_file *ctl;
117 struct snd_kcontrol *control;
120 ctl = file->private_data;
121 file->private_data = NULL;
123 write_lock_irqsave(&card->ctl_files_rwlock, flags);
124 list_del(&ctl->list);
125 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
126 down_write(&card->controls_rwsem);
127 list_for_each_entry(control, &card->controls, list)
128 for (idx = 0; idx < control->count; idx++)
129 if (control->vd[idx].owner == ctl)
130 control->vd[idx].owner = NULL;
131 up_write(&card->controls_rwsem);
132 snd_ctl_empty_read_queue(ctl);
135 module_put(card->module);
136 snd_card_file_remove(card, file);
140 void snd_ctl_notify(struct snd_card *card, unsigned int mask,
141 struct snd_ctl_elem_id *id)
144 struct snd_ctl_file *ctl;
145 struct snd_kctl_event *ev;
147 if (snd_BUG_ON(!card || !id))
149 read_lock(&card->ctl_files_rwlock);
150 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
151 card->mixer_oss_change_count++;
153 list_for_each_entry(ctl, &card->ctl_files, list) {
154 if (!ctl->subscribed)
156 spin_lock_irqsave(&ctl->read_lock, flags);
157 list_for_each_entry(ev, &ctl->events, list) {
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 static struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control,
194 struct snd_kcontrol *kctl;
197 if (snd_BUG_ON(!control || !control->count))
200 if (control->count > MAX_CONTROL_COUNT)
203 kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL);
205 snd_printk(KERN_ERR "Cannot allocate control instance\n");
209 for (idx = 0; idx < kctl->count; idx++)
210 kctl->vd[idx].access = access;
215 * snd_ctl_new1 - create a control instance from the template
216 * @ncontrol: the initialization record
217 * @private_data: the private data to set
219 * Allocates a new struct snd_kcontrol instance and initialize from the given
220 * template. When the access field of ncontrol is 0, it's assumed as
221 * READWRITE access. When the count field is 0, it's assumes as one.
223 * Returns the pointer of the newly generated instance, or NULL on failure.
225 struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
228 struct snd_kcontrol kctl;
231 if (snd_BUG_ON(!ncontrol || !ncontrol->info))
233 memset(&kctl, 0, sizeof(kctl));
234 kctl.id.iface = ncontrol->iface;
235 kctl.id.device = ncontrol->device;
236 kctl.id.subdevice = ncontrol->subdevice;
237 if (ncontrol->name) {
238 strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
239 if (strcmp(ncontrol->name, kctl.id.name) != 0)
240 snd_printk(KERN_WARNING
241 "Control name '%s' truncated to '%s'\n",
242 ncontrol->name, kctl.id.name);
244 kctl.id.index = ncontrol->index;
245 kctl.count = ncontrol->count ? ncontrol->count : 1;
246 access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
247 (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
248 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
249 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE|
250 SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND|
251 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK));
252 kctl.info = ncontrol->info;
253 kctl.get = ncontrol->get;
254 kctl.put = ncontrol->put;
255 kctl.tlv.p = ncontrol->tlv.p;
256 kctl.private_value = ncontrol->private_value;
257 kctl.private_data = private_data;
258 return snd_ctl_new(&kctl, access);
261 EXPORT_SYMBOL(snd_ctl_new1);
264 * snd_ctl_free_one - release the control instance
265 * @kcontrol: the control instance
267 * Releases the control instance created via snd_ctl_new()
269 * Don't call this after the control was added to the card.
271 void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
274 if (kcontrol->private_free)
275 kcontrol->private_free(kcontrol);
280 EXPORT_SYMBOL(snd_ctl_free_one);
282 static unsigned int snd_ctl_hole_check(struct snd_card *card,
285 struct snd_kcontrol *kctl;
287 list_for_each_entry(kctl, &card->controls, list) {
288 if ((kctl->id.numid <= card->last_numid &&
289 kctl->id.numid + kctl->count > card->last_numid) ||
290 (kctl->id.numid <= card->last_numid + count - 1 &&
291 kctl->id.numid + kctl->count > card->last_numid + count - 1))
292 return card->last_numid = kctl->id.numid + kctl->count - 1;
294 return card->last_numid;
297 static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
299 unsigned int last_numid, iter = 100000;
301 last_numid = card->last_numid;
302 while (last_numid != snd_ctl_hole_check(card, count)) {
304 /* this situation is very unlikely */
305 snd_printk(KERN_ERR "unable to allocate new control numid\n");
308 last_numid = card->last_numid;
314 * snd_ctl_add - add the control instance to the card
315 * @card: the card instance
316 * @kcontrol: the control instance to add
318 * Adds the control instance created via snd_ctl_new() or
319 * snd_ctl_new1() to the given card. Assigns also an unique
320 * numid used for fast search.
322 * Returns zero if successful, or a negative error code on failure.
324 * It frees automatically the control which cannot be added.
326 int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
328 struct snd_ctl_elem_id id;
334 if (snd_BUG_ON(!card || !kcontrol->info))
337 down_write(&card->controls_rwsem);
338 if (snd_ctl_find_id(card, &id)) {
339 up_write(&card->controls_rwsem);
340 snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
349 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
350 up_write(&card->controls_rwsem);
354 list_add_tail(&kcontrol->list, &card->controls);
355 card->controls_count += kcontrol->count;
356 kcontrol->id.numid = card->last_numid + 1;
357 card->last_numid += kcontrol->count;
358 up_write(&card->controls_rwsem);
359 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
360 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
364 snd_ctl_free_one(kcontrol);
368 EXPORT_SYMBOL(snd_ctl_add);
371 * snd_ctl_remove - remove the control from the card and release it
372 * @card: the card instance
373 * @kcontrol: the control instance to remove
375 * Removes the control from the card and then releases the instance.
376 * You don't need to call snd_ctl_free_one(). You must be in
377 * the write lock - down_write(&card->controls_rwsem).
379 * Returns 0 if successful, or a negative error code on failure.
381 int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
383 struct snd_ctl_elem_id id;
386 if (snd_BUG_ON(!card || !kcontrol))
388 list_del(&kcontrol->list);
389 card->controls_count -= kcontrol->count;
391 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
392 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
393 snd_ctl_free_one(kcontrol);
397 EXPORT_SYMBOL(snd_ctl_remove);
400 * snd_ctl_remove_id - remove the control of the given id and release it
401 * @card: the card instance
402 * @id: the control id to remove
404 * Finds the control instance with the given id, removes it from the
405 * card list and releases it.
407 * Returns 0 if successful, or a negative error code on failure.
409 int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
411 struct snd_kcontrol *kctl;
414 down_write(&card->controls_rwsem);
415 kctl = snd_ctl_find_id(card, id);
417 up_write(&card->controls_rwsem);
420 ret = snd_ctl_remove(card, kctl);
421 up_write(&card->controls_rwsem);
425 EXPORT_SYMBOL(snd_ctl_remove_id);
428 * snd_ctl_remove_user_ctl - remove and release the unlocked user control
429 * @file: active control handle
430 * @id: the control id to remove
432 * Finds the control instance with the given id, removes it from the
433 * card list and releases it.
435 * Returns 0 if successful, or a negative error code on failure.
437 static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
438 struct snd_ctl_elem_id *id)
440 struct snd_card *card = file->card;
441 struct snd_kcontrol *kctl;
444 down_write(&card->controls_rwsem);
445 kctl = snd_ctl_find_id(card, id);
450 if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
454 for (idx = 0; idx < kctl->count; idx++)
455 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
459 ret = snd_ctl_remove(card, kctl);
462 card->user_ctl_count--;
464 up_write(&card->controls_rwsem);
469 * snd_ctl_activate_id - activate/inactivate the control of the given id
470 * @card: the card instance
471 * @id: the control id to activate/inactivate
472 * @active: non-zero to activate
474 * Finds the control instance with the given id, and activate or
475 * inactivate the control together with notification, if changed.
477 * Returns 0 if unchanged, 1 if changed, or a negative error code on failure.
479 int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
482 struct snd_kcontrol *kctl;
483 struct snd_kcontrol_volatile *vd;
484 unsigned int index_offset;
487 down_write(&card->controls_rwsem);
488 kctl = snd_ctl_find_id(card, id);
493 index_offset = snd_ctl_get_ioff(kctl, &kctl->id);
494 vd = &kctl->vd[index_offset];
497 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
499 vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
501 if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)
503 vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
507 up_write(&card->controls_rwsem);
509 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, id);
512 EXPORT_SYMBOL_GPL(snd_ctl_activate_id);
515 * snd_ctl_rename_id - replace the id of a control on the card
516 * @card: the card instance
517 * @src_id: the old id
518 * @dst_id: the new id
520 * Finds the control with the old id from the card, and replaces the
521 * id with the new one.
523 * Returns zero if successful, or a negative error code on failure.
525 int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
526 struct snd_ctl_elem_id *dst_id)
528 struct snd_kcontrol *kctl;
530 down_write(&card->controls_rwsem);
531 kctl = snd_ctl_find_id(card, src_id);
533 up_write(&card->controls_rwsem);
537 kctl->id.numid = card->last_numid + 1;
538 card->last_numid += kctl->count;
539 up_write(&card->controls_rwsem);
543 EXPORT_SYMBOL(snd_ctl_rename_id);
546 * snd_ctl_find_numid - find the control instance with the given number-id
547 * @card: the card instance
548 * @numid: the number-id to search
550 * Finds the control instance with the given number-id from the card.
552 * Returns the pointer of the instance if found, or NULL if not.
554 * The caller must down card->controls_rwsem before calling this function
555 * (if the race condition can happen).
557 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
559 struct snd_kcontrol *kctl;
561 if (snd_BUG_ON(!card || !numid))
563 list_for_each_entry(kctl, &card->controls, list) {
564 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
570 EXPORT_SYMBOL(snd_ctl_find_numid);
573 * snd_ctl_find_id - find the control instance with the given id
574 * @card: the card instance
575 * @id: the id to search
577 * Finds the control instance with the given id from the card.
579 * Returns the pointer of the instance if found, or NULL if not.
581 * The caller must down card->controls_rwsem before calling this function
582 * (if the race condition can happen).
584 struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
585 struct snd_ctl_elem_id *id)
587 struct snd_kcontrol *kctl;
589 if (snd_BUG_ON(!card || !id))
592 return snd_ctl_find_numid(card, id->numid);
593 list_for_each_entry(kctl, &card->controls, list) {
594 if (kctl->id.iface != id->iface)
596 if (kctl->id.device != id->device)
598 if (kctl->id.subdevice != id->subdevice)
600 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
602 if (kctl->id.index > id->index)
604 if (kctl->id.index + kctl->count <= id->index)
611 EXPORT_SYMBOL(snd_ctl_find_id);
613 static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
614 unsigned int cmd, void __user *arg)
616 struct snd_ctl_card_info *info;
618 info = kzalloc(sizeof(*info), GFP_KERNEL);
621 down_read(&snd_ioctl_rwsem);
622 info->card = card->number;
623 strlcpy(info->id, card->id, sizeof(info->id));
624 strlcpy(info->driver, card->driver, sizeof(info->driver));
625 strlcpy(info->name, card->shortname, sizeof(info->name));
626 strlcpy(info->longname, card->longname, sizeof(info->longname));
627 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
628 strlcpy(info->components, card->components, sizeof(info->components));
629 up_read(&snd_ioctl_rwsem);
630 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
638 static int snd_ctl_elem_list(struct snd_card *card,
639 struct snd_ctl_elem_list __user *_list)
641 struct list_head *plist;
642 struct snd_ctl_elem_list list;
643 struct snd_kcontrol *kctl;
644 struct snd_ctl_elem_id *dst, *id;
645 unsigned int offset, space, first, jidx;
647 if (copy_from_user(&list, _list, sizeof(list)))
649 offset = list.offset;
652 /* try limit maximum space */
656 /* allocate temporary buffer for atomic operation */
657 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
660 down_read(&card->controls_rwsem);
661 list.count = card->controls_count;
662 plist = card->controls.next;
663 while (plist != &card->controls) {
666 kctl = snd_kcontrol(plist);
667 if (offset < kctl->count)
669 offset -= kctl->count;
674 while (space > 0 && plist != &card->controls) {
675 kctl = snd_kcontrol(plist);
676 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
677 snd_ctl_build_ioff(id, kctl, jidx);
685 up_read(&card->controls_rwsem);
687 copy_to_user(list.pids, dst,
688 list.used * sizeof(struct snd_ctl_elem_id))) {
694 down_read(&card->controls_rwsem);
695 list.count = card->controls_count;
696 up_read(&card->controls_rwsem);
698 if (copy_to_user(_list, &list, sizeof(list)))
703 static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
704 struct snd_ctl_elem_info *info)
706 struct snd_card *card = ctl->card;
707 struct snd_kcontrol *kctl;
708 struct snd_kcontrol_volatile *vd;
709 unsigned int index_offset;
712 down_read(&card->controls_rwsem);
713 kctl = snd_ctl_find_id(card, &info->id);
715 up_read(&card->controls_rwsem);
718 #ifdef CONFIG_SND_DEBUG
721 result = kctl->info(kctl, info);
723 snd_BUG_ON(info->access);
724 index_offset = snd_ctl_get_ioff(kctl, &info->id);
725 vd = &kctl->vd[index_offset];
726 snd_ctl_build_ioff(&info->id, kctl, index_offset);
727 info->access = vd->access;
729 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
730 if (vd->owner == ctl)
731 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
732 info->owner = pid_vnr(vd->owner->pid);
737 up_read(&card->controls_rwsem);
741 static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
742 struct snd_ctl_elem_info __user *_info)
744 struct snd_ctl_elem_info info;
747 if (copy_from_user(&info, _info, sizeof(info)))
749 snd_power_lock(ctl->card);
750 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
752 result = snd_ctl_elem_info(ctl, &info);
753 snd_power_unlock(ctl->card);
755 if (copy_to_user(_info, &info, sizeof(info)))
760 static int snd_ctl_elem_read(struct snd_card *card,
761 struct snd_ctl_elem_value *control)
763 struct snd_kcontrol *kctl;
764 struct snd_kcontrol_volatile *vd;
765 unsigned int index_offset;
768 down_read(&card->controls_rwsem);
769 kctl = snd_ctl_find_id(card, &control->id);
773 index_offset = snd_ctl_get_ioff(kctl, &control->id);
774 vd = &kctl->vd[index_offset];
775 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) &&
777 snd_ctl_build_ioff(&control->id, kctl, index_offset);
778 result = kctl->get(kctl, control);
782 up_read(&card->controls_rwsem);
786 static int snd_ctl_elem_read_user(struct snd_card *card,
787 struct snd_ctl_elem_value __user *_control)
789 struct snd_ctl_elem_value *control;
792 control = memdup_user(_control, sizeof(*control));
794 return PTR_ERR(control);
796 snd_power_lock(card);
797 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
799 result = snd_ctl_elem_read(card, control);
800 snd_power_unlock(card);
802 if (copy_to_user(_control, control, sizeof(*control)))
808 static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
809 struct snd_ctl_elem_value *control)
811 struct snd_kcontrol *kctl;
812 struct snd_kcontrol_volatile *vd;
813 unsigned int index_offset;
816 down_read(&card->controls_rwsem);
817 kctl = snd_ctl_find_id(card, &control->id);
821 index_offset = snd_ctl_get_ioff(kctl, &control->id);
822 vd = &kctl->vd[index_offset];
823 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
825 (file && vd->owner && vd->owner != file)) {
828 snd_ctl_build_ioff(&control->id, kctl, index_offset);
829 result = kctl->put(kctl, control);
832 up_read(&card->controls_rwsem);
833 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
838 up_read(&card->controls_rwsem);
842 static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
843 struct snd_ctl_elem_value __user *_control)
845 struct snd_ctl_elem_value *control;
846 struct snd_card *card;
849 control = memdup_user(_control, sizeof(*control));
851 return PTR_ERR(control);
854 snd_power_lock(card);
855 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
857 result = snd_ctl_elem_write(card, file, control);
858 snd_power_unlock(card);
860 if (copy_to_user(_control, control, sizeof(*control)))
866 static int snd_ctl_elem_lock(struct snd_ctl_file *file,
867 struct snd_ctl_elem_id __user *_id)
869 struct snd_card *card = file->card;
870 struct snd_ctl_elem_id id;
871 struct snd_kcontrol *kctl;
872 struct snd_kcontrol_volatile *vd;
875 if (copy_from_user(&id, _id, sizeof(id)))
877 down_write(&card->controls_rwsem);
878 kctl = snd_ctl_find_id(card, &id);
882 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
883 if (vd->owner != NULL)
890 up_write(&card->controls_rwsem);
894 static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
895 struct snd_ctl_elem_id __user *_id)
897 struct snd_card *card = file->card;
898 struct snd_ctl_elem_id id;
899 struct snd_kcontrol *kctl;
900 struct snd_kcontrol_volatile *vd;
903 if (copy_from_user(&id, _id, sizeof(id)))
905 down_write(&card->controls_rwsem);
906 kctl = snd_ctl_find_id(card, &id);
910 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
911 if (vd->owner == NULL)
913 else if (vd->owner != file)
920 up_write(&card->controls_rwsem);
924 struct user_element {
925 struct snd_ctl_elem_info info;
926 void *elem_data; /* element data */
927 unsigned long elem_data_size; /* size of element data in bytes */
928 void *tlv_data; /* TLV data */
929 unsigned long tlv_data_size; /* TLV data size */
930 void *priv_data; /* private data (like strings for enumerated type) */
931 unsigned long priv_data_size; /* size of private data in bytes */
934 static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
935 struct snd_ctl_elem_info *uinfo)
937 struct user_element *ue = kcontrol->private_data;
943 static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
944 struct snd_ctl_elem_value *ucontrol)
946 struct user_element *ue = kcontrol->private_data;
948 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
952 static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
953 struct snd_ctl_elem_value *ucontrol)
956 struct user_element *ue = kcontrol->private_data;
958 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
960 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
964 static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol,
967 unsigned int __user *tlv)
969 struct user_element *ue = kcontrol->private_data;
974 if (size > 1024 * 128) /* sane value */
977 new_data = memdup_user(tlv, size);
978 if (IS_ERR(new_data))
979 return PTR_ERR(new_data);
980 change = ue->tlv_data_size != size;
982 change = memcmp(ue->tlv_data, new_data, size);
984 ue->tlv_data = new_data;
985 ue->tlv_data_size = size;
987 if (! ue->tlv_data_size || ! ue->tlv_data)
989 if (size < ue->tlv_data_size)
991 if (copy_to_user(tlv, ue->tlv_data, ue->tlv_data_size))
997 static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
999 struct user_element *ue = kcontrol->private_data;
1001 kfree(ue->tlv_data);
1005 static int snd_ctl_elem_add(struct snd_ctl_file *file,
1006 struct snd_ctl_elem_info *info, int replace)
1008 struct snd_card *card = file->card;
1009 struct snd_kcontrol kctl, *_kctl;
1010 unsigned int access;
1012 struct user_element *ue;
1015 if (card->user_ctl_count >= MAX_USER_CONTROLS)
1017 if (info->count < 1)
1019 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
1020 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
1021 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
1022 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE));
1024 memset(&kctl, 0, sizeof(kctl));
1025 down_write(&card->controls_rwsem);
1026 _kctl = snd_ctl_find_id(card, &info->id);
1030 err = snd_ctl_remove(card, _kctl);
1037 up_write(&card->controls_rwsem);
1040 memcpy(&kctl.id, &info->id, sizeof(info->id));
1041 kctl.count = info->owner ? info->owner : 1;
1042 access |= SNDRV_CTL_ELEM_ACCESS_USER;
1043 kctl.info = snd_ctl_elem_user_info;
1044 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1045 kctl.get = snd_ctl_elem_user_get;
1046 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1047 kctl.put = snd_ctl_elem_user_put;
1048 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
1049 kctl.tlv.c = snd_ctl_elem_user_tlv;
1050 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1052 switch (info->type) {
1053 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
1054 case SNDRV_CTL_ELEM_TYPE_INTEGER:
1055 private_size = sizeof(long);
1056 if (info->count > 128)
1059 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
1060 private_size = sizeof(long long);
1061 if (info->count > 64)
1064 case SNDRV_CTL_ELEM_TYPE_BYTES:
1065 private_size = sizeof(unsigned char);
1066 if (info->count > 512)
1069 case SNDRV_CTL_ELEM_TYPE_IEC958:
1070 private_size = sizeof(struct snd_aes_iec958);
1071 if (info->count != 1)
1077 private_size *= info->count;
1078 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
1082 ue->info.access = 0;
1083 ue->elem_data = (char *)ue + sizeof(*ue);
1084 ue->elem_data_size = private_size;
1085 kctl.private_free = snd_ctl_elem_user_free;
1086 _kctl = snd_ctl_new(&kctl, access);
1087 if (_kctl == NULL) {
1091 _kctl->private_data = ue;
1092 for (idx = 0; idx < _kctl->count; idx++)
1093 _kctl->vd[idx].owner = file;
1094 err = snd_ctl_add(card, _kctl);
1098 down_write(&card->controls_rwsem);
1099 card->user_ctl_count++;
1100 up_write(&card->controls_rwsem);
1105 static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1106 struct snd_ctl_elem_info __user *_info, int replace)
1108 struct snd_ctl_elem_info info;
1109 if (copy_from_user(&info, _info, sizeof(info)))
1111 return snd_ctl_elem_add(file, &info, replace);
1114 static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1115 struct snd_ctl_elem_id __user *_id)
1117 struct snd_ctl_elem_id id;
1119 if (copy_from_user(&id, _id, sizeof(id)))
1121 return snd_ctl_remove_user_ctl(file, &id);
1124 static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
1127 if (get_user(subscribe, ptr))
1129 if (subscribe < 0) {
1130 subscribe = file->subscribed;
1131 if (put_user(subscribe, ptr))
1136 file->subscribed = 1;
1138 } else if (file->subscribed) {
1139 snd_ctl_empty_read_queue(file);
1140 file->subscribed = 0;
1145 static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1146 struct snd_ctl_tlv __user *_tlv,
1149 struct snd_card *card = file->card;
1150 struct snd_ctl_tlv tlv;
1151 struct snd_kcontrol *kctl;
1152 struct snd_kcontrol_volatile *vd;
1156 if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
1158 if (tlv.length < sizeof(unsigned int) * 2)
1160 down_read(&card->controls_rwsem);
1161 kctl = snd_ctl_find_numid(card, tlv.numid);
1166 if (kctl->tlv.p == NULL) {
1170 vd = &kctl->vd[tlv.numid - kctl->id.numid];
1171 if ((op_flag == 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) == 0) ||
1172 (op_flag > 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) == 0) ||
1173 (op_flag < 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND) == 0)) {
1177 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1178 if (vd->owner != NULL && vd->owner != file) {
1182 err = kctl->tlv.c(kctl, op_flag, tlv.length, _tlv->tlv);
1184 up_read(&card->controls_rwsem);
1185 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &kctl->id);
1193 len = kctl->tlv.p[1] + 2 * sizeof(unsigned int);
1194 if (tlv.length < len) {
1198 if (copy_to_user(_tlv->tlv, kctl->tlv.p, len))
1202 up_read(&card->controls_rwsem);
1206 static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1208 struct snd_ctl_file *ctl;
1209 struct snd_card *card;
1210 struct snd_kctl_ioctl *p;
1211 void __user *argp = (void __user *)arg;
1212 int __user *ip = argp;
1215 ctl = file->private_data;
1217 if (snd_BUG_ON(!card))
1220 case SNDRV_CTL_IOCTL_PVERSION:
1221 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1222 case SNDRV_CTL_IOCTL_CARD_INFO:
1223 return snd_ctl_card_info(card, ctl, cmd, argp);
1224 case SNDRV_CTL_IOCTL_ELEM_LIST:
1225 return snd_ctl_elem_list(card, argp);
1226 case SNDRV_CTL_IOCTL_ELEM_INFO:
1227 return snd_ctl_elem_info_user(ctl, argp);
1228 case SNDRV_CTL_IOCTL_ELEM_READ:
1229 return snd_ctl_elem_read_user(card, argp);
1230 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1231 return snd_ctl_elem_write_user(ctl, argp);
1232 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1233 return snd_ctl_elem_lock(ctl, argp);
1234 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1235 return snd_ctl_elem_unlock(ctl, argp);
1236 case SNDRV_CTL_IOCTL_ELEM_ADD:
1237 return snd_ctl_elem_add_user(ctl, argp, 0);
1238 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1239 return snd_ctl_elem_add_user(ctl, argp, 1);
1240 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1241 return snd_ctl_elem_remove(ctl, argp);
1242 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1243 return snd_ctl_subscribe_events(ctl, ip);
1244 case SNDRV_CTL_IOCTL_TLV_READ:
1245 return snd_ctl_tlv_ioctl(ctl, argp, 0);
1246 case SNDRV_CTL_IOCTL_TLV_WRITE:
1247 return snd_ctl_tlv_ioctl(ctl, argp, 1);
1248 case SNDRV_CTL_IOCTL_TLV_COMMAND:
1249 return snd_ctl_tlv_ioctl(ctl, argp, -1);
1250 case SNDRV_CTL_IOCTL_POWER:
1251 return -ENOPROTOOPT;
1252 case SNDRV_CTL_IOCTL_POWER_STATE:
1254 return put_user(card->power_state, ip) ? -EFAULT : 0;
1256 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1259 down_read(&snd_ioctl_rwsem);
1260 list_for_each_entry(p, &snd_control_ioctls, list) {
1261 err = p->fioctl(card, ctl, cmd, arg);
1262 if (err != -ENOIOCTLCMD) {
1263 up_read(&snd_ioctl_rwsem);
1267 up_read(&snd_ioctl_rwsem);
1268 snd_printdd("unknown ioctl = 0x%x\n", cmd);
1272 static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1273 size_t count, loff_t * offset)
1275 struct snd_ctl_file *ctl;
1279 ctl = file->private_data;
1280 if (snd_BUG_ON(!ctl || !ctl->card))
1282 if (!ctl->subscribed)
1284 if (count < sizeof(struct snd_ctl_event))
1286 spin_lock_irq(&ctl->read_lock);
1287 while (count >= sizeof(struct snd_ctl_event)) {
1288 struct snd_ctl_event ev;
1289 struct snd_kctl_event *kev;
1290 while (list_empty(&ctl->events)) {
1292 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1296 init_waitqueue_entry(&wait, current);
1297 add_wait_queue(&ctl->change_sleep, &wait);
1298 set_current_state(TASK_INTERRUPTIBLE);
1299 spin_unlock_irq(&ctl->read_lock);
1301 remove_wait_queue(&ctl->change_sleep, &wait);
1302 if (signal_pending(current))
1303 return -ERESTARTSYS;
1304 spin_lock_irq(&ctl->read_lock);
1306 kev = snd_kctl_event(ctl->events.next);
1307 ev.type = SNDRV_CTL_EVENT_ELEM;
1308 ev.data.elem.mask = kev->mask;
1309 ev.data.elem.id = kev->id;
1310 list_del(&kev->list);
1311 spin_unlock_irq(&ctl->read_lock);
1313 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
1317 spin_lock_irq(&ctl->read_lock);
1318 buffer += sizeof(struct snd_ctl_event);
1319 count -= sizeof(struct snd_ctl_event);
1320 result += sizeof(struct snd_ctl_event);
1323 spin_unlock_irq(&ctl->read_lock);
1325 return result > 0 ? result : err;
1328 static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1331 struct snd_ctl_file *ctl;
1333 ctl = file->private_data;
1334 if (!ctl->subscribed)
1336 poll_wait(file, &ctl->change_sleep, wait);
1339 if (!list_empty(&ctl->events))
1340 mask |= POLLIN | POLLRDNORM;
1346 * register the device-specific control-ioctls.
1347 * called from each device manager like pcm.c, hwdep.c, etc.
1349 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1351 struct snd_kctl_ioctl *pn;
1353 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
1357 down_write(&snd_ioctl_rwsem);
1358 list_add_tail(&pn->list, lists);
1359 up_write(&snd_ioctl_rwsem);
1363 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1365 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1368 EXPORT_SYMBOL(snd_ctl_register_ioctl);
1370 #ifdef CONFIG_COMPAT
1371 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1373 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1376 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
1380 * de-register the device-specific control-ioctls.
1382 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1383 struct list_head *lists)
1385 struct snd_kctl_ioctl *p;
1387 if (snd_BUG_ON(!fcn))
1389 down_write(&snd_ioctl_rwsem);
1390 list_for_each_entry(p, lists, list) {
1391 if (p->fioctl == fcn) {
1393 up_write(&snd_ioctl_rwsem);
1398 up_write(&snd_ioctl_rwsem);
1403 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1405 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1408 EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1410 #ifdef CONFIG_COMPAT
1411 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1413 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1416 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
1419 static int snd_ctl_fasync(int fd, struct file * file, int on)
1421 struct snd_ctl_file *ctl;
1423 ctl = file->private_data;
1424 return fasync_helper(fd, file, on, &ctl->fasync);
1430 #ifdef CONFIG_COMPAT
1431 #include "control_compat.c"
1433 #define snd_ctl_ioctl_compat NULL
1440 static const struct file_operations snd_ctl_f_ops =
1442 .owner = THIS_MODULE,
1443 .read = snd_ctl_read,
1444 .open = snd_ctl_open,
1445 .release = snd_ctl_release,
1446 .llseek = no_llseek,
1447 .poll = snd_ctl_poll,
1448 .unlocked_ioctl = snd_ctl_ioctl,
1449 .compat_ioctl = snd_ctl_ioctl_compat,
1450 .fasync = snd_ctl_fasync,
1454 * registration of the control device
1456 static int snd_ctl_dev_register(struct snd_device *device)
1458 struct snd_card *card = device->device_data;
1462 if (snd_BUG_ON(!card))
1464 cardnum = card->number;
1465 if (snd_BUG_ON(cardnum < 0 || cardnum >= SNDRV_CARDS))
1467 sprintf(name, "controlC%i", cardnum);
1468 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1469 &snd_ctl_f_ops, card, name)) < 0)
1475 * disconnection of the control device
1477 static int snd_ctl_dev_disconnect(struct snd_device *device)
1479 struct snd_card *card = device->device_data;
1480 struct snd_ctl_file *ctl;
1483 if (snd_BUG_ON(!card))
1485 cardnum = card->number;
1486 if (snd_BUG_ON(cardnum < 0 || cardnum >= SNDRV_CARDS))
1489 read_lock(&card->ctl_files_rwlock);
1490 list_for_each_entry(ctl, &card->ctl_files, list) {
1491 wake_up(&ctl->change_sleep);
1492 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1494 read_unlock(&card->ctl_files_rwlock);
1496 if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
1505 static int snd_ctl_dev_free(struct snd_device *device)
1507 struct snd_card *card = device->device_data;
1508 struct snd_kcontrol *control;
1510 down_write(&card->controls_rwsem);
1511 while (!list_empty(&card->controls)) {
1512 control = snd_kcontrol(card->controls.next);
1513 snd_ctl_remove(card, control);
1515 up_write(&card->controls_rwsem);
1520 * create control core:
1521 * called from init.c
1523 int snd_ctl_create(struct snd_card *card)
1525 static struct snd_device_ops ops = {
1526 .dev_free = snd_ctl_dev_free,
1527 .dev_register = snd_ctl_dev_register,
1528 .dev_disconnect = snd_ctl_dev_disconnect,
1531 if (snd_BUG_ON(!card))
1533 return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
1537 * Frequently used control callbacks/helpers
1539 int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
1540 struct snd_ctl_elem_info *uinfo)
1542 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1544 uinfo->value.integer.min = 0;
1545 uinfo->value.integer.max = 1;
1549 EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
1551 int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
1552 struct snd_ctl_elem_info *uinfo)
1554 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1556 uinfo->value.integer.min = 0;
1557 uinfo->value.integer.max = 1;
1561 EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
1564 * snd_ctl_enum_info - fills the info structure for an enumerated control
1565 * @info: the structure to be filled
1566 * @channels: the number of the control's channels; often one
1567 * @items: the number of control values; also the size of @names
1568 * @names: an array containing the names of all control values
1570 * Sets all required fields in @info to their appropriate values.
1571 * If the control's accessibility is not the default (readable and writable),
1572 * the caller has to fill @info->access.
1574 int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
1575 unsigned int items, const char *const names[])
1577 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1578 info->count = channels;
1579 info->value.enumerated.items = items;
1580 if (info->value.enumerated.item >= items)
1581 info->value.enumerated.item = items - 1;
1582 strlcpy(info->value.enumerated.name,
1583 names[info->value.enumerated.item],
1584 sizeof(info->value.enumerated.name));
1587 EXPORT_SYMBOL(snd_ctl_enum_info);