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);
180 * snd_ctl_new - create a control instance from the template
181 * @control: the control template
182 * @access: the default control access
184 * Allocates a new struct snd_kcontrol instance and copies the given template
185 * to the new instance. It does not copy volatile data (access).
187 * Returns the pointer of the new instance, or NULL on failure.
189 struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control, unsigned int access)
191 struct snd_kcontrol *kctl;
194 snd_assert(control != NULL, return NULL);
195 snd_assert(control->count > 0, return NULL);
196 kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL);
198 snd_printk(KERN_ERR "Cannot allocate control instance\n");
202 for (idx = 0; idx < kctl->count; idx++)
203 kctl->vd[idx].access = access;
208 * snd_ctl_new1 - create a control instance from the template
209 * @ncontrol: the initialization record
210 * @private_data: the private data to set
212 * Allocates a new struct snd_kcontrol instance and initialize from the given
213 * template. When the access field of ncontrol is 0, it's assumed as
214 * READWRITE access. When the count field is 0, it's assumes as one.
216 * Returns the pointer of the newly generated instance, or NULL on failure.
218 struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
221 struct snd_kcontrol kctl;
224 snd_assert(ncontrol != NULL, return NULL);
225 snd_assert(ncontrol->info != NULL, return NULL);
226 memset(&kctl, 0, sizeof(kctl));
227 kctl.id.iface = ncontrol->iface;
228 kctl.id.device = ncontrol->device;
229 kctl.id.subdevice = ncontrol->subdevice;
231 strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
232 kctl.id.index = ncontrol->index;
233 kctl.count = ncontrol->count ? ncontrol->count : 1;
234 access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
235 (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|SNDRV_CTL_ELEM_ACCESS_INACTIVE|
236 SNDRV_CTL_ELEM_ACCESS_DINDIRECT|SNDRV_CTL_ELEM_ACCESS_INDIRECT));
237 kctl.info = ncontrol->info;
238 kctl.get = ncontrol->get;
239 kctl.put = ncontrol->put;
240 kctl.private_value = ncontrol->private_value;
241 kctl.private_data = private_data;
242 return snd_ctl_new(&kctl, access);
246 * snd_ctl_free_one - release the control instance
247 * @kcontrol: the control instance
249 * Releases the control instance created via snd_ctl_new()
251 * Don't call this after the control was added to the card.
253 void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
256 if (kcontrol->private_free)
257 kcontrol->private_free(kcontrol);
262 static unsigned int snd_ctl_hole_check(struct snd_card *card,
265 struct list_head *list;
266 struct snd_kcontrol *kctl;
268 list_for_each(list, &card->controls) {
269 kctl = snd_kcontrol(list);
270 if ((kctl->id.numid <= card->last_numid &&
271 kctl->id.numid + kctl->count > card->last_numid) ||
272 (kctl->id.numid <= card->last_numid + count - 1 &&
273 kctl->id.numid + kctl->count > card->last_numid + count - 1))
274 return card->last_numid = kctl->id.numid + kctl->count - 1;
276 return card->last_numid;
279 static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
281 unsigned int last_numid, iter = 100000;
283 last_numid = card->last_numid;
284 while (last_numid != snd_ctl_hole_check(card, count)) {
286 /* this situation is very unlikely */
287 snd_printk(KERN_ERR "unable to allocate new control numid\n");
290 last_numid = card->last_numid;
296 * snd_ctl_add - add the control instance to the card
297 * @card: the card instance
298 * @kcontrol: the control instance to add
300 * Adds the control instance created via snd_ctl_new() or
301 * snd_ctl_new1() to the given card. Assigns also an unique
302 * numid used for fast search.
304 * Returns zero if successful, or a negative error code on failure.
306 * It frees automatically the control which cannot be added.
308 int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
310 struct snd_ctl_elem_id id;
313 snd_assert(card != NULL, return -EINVAL);
316 snd_assert(kcontrol->info != NULL, return -EINVAL);
318 down_write(&card->controls_rwsem);
319 if (snd_ctl_find_id(card, &id)) {
320 up_write(&card->controls_rwsem);
321 snd_ctl_free_one(kcontrol);
322 snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
330 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
331 up_write(&card->controls_rwsem);
332 snd_ctl_free_one(kcontrol);
335 list_add_tail(&kcontrol->list, &card->controls);
336 card->controls_count += kcontrol->count;
337 kcontrol->id.numid = card->last_numid + 1;
338 card->last_numid += kcontrol->count;
339 up_write(&card->controls_rwsem);
340 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
341 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
346 * snd_ctl_remove - remove the control from the card and release it
347 * @card: the card instance
348 * @kcontrol: the control instance to remove
350 * Removes the control from the card and then releases the instance.
351 * You don't need to call snd_ctl_free_one(). You must be in
352 * the write lock - down_write(&card->controls_rwsem).
354 * Returns 0 if successful, or a negative error code on failure.
356 int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
358 struct snd_ctl_elem_id id;
361 snd_assert(card != NULL && kcontrol != NULL, return -EINVAL);
362 list_del(&kcontrol->list);
363 card->controls_count -= kcontrol->count;
365 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
366 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
367 snd_ctl_free_one(kcontrol);
372 * snd_ctl_remove_id - remove the control of the given id and release it
373 * @card: the card instance
374 * @id: the control id to remove
376 * Finds the control instance with the given id, removes it from the
377 * card list and releases it.
379 * Returns 0 if successful, or a negative error code on failure.
381 int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
383 struct snd_kcontrol *kctl;
386 down_write(&card->controls_rwsem);
387 kctl = snd_ctl_find_id(card, id);
389 up_write(&card->controls_rwsem);
392 ret = snd_ctl_remove(card, kctl);
393 up_write(&card->controls_rwsem);
398 * snd_ctl_remove_unlocked_id - remove the unlocked control of the given id and release it
399 * @file: active control handle
400 * @id: the control id to remove
402 * Finds the control instance with the given id, removes it from the
403 * card list and releases it.
405 * Returns 0 if successful, or a negative error code on failure.
407 static int snd_ctl_remove_unlocked_id(struct snd_ctl_file * file,
408 struct snd_ctl_elem_id *id)
410 struct snd_card *card = file->card;
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 for (idx = 0; idx < kctl->count; idx++)
421 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
422 up_write(&card->controls_rwsem);
425 ret = snd_ctl_remove(card, kctl);
426 up_write(&card->controls_rwsem);
431 * snd_ctl_rename_id - replace the id of a control on the card
432 * @card: the card instance
433 * @src_id: the old id
434 * @dst_id: the new id
436 * Finds the control with the old id from the card, and replaces the
437 * id with the new one.
439 * Returns zero if successful, or a negative error code on failure.
441 int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
442 struct snd_ctl_elem_id *dst_id)
444 struct snd_kcontrol *kctl;
446 down_write(&card->controls_rwsem);
447 kctl = snd_ctl_find_id(card, src_id);
449 up_write(&card->controls_rwsem);
453 kctl->id.numid = card->last_numid + 1;
454 card->last_numid += kctl->count;
455 up_write(&card->controls_rwsem);
460 * snd_ctl_find_numid - find the control instance with the given number-id
461 * @card: the card instance
462 * @numid: the number-id to search
464 * Finds the control instance with the given number-id from the card.
466 * Returns the pointer of the instance if found, or NULL if not.
468 * The caller must down card->controls_rwsem before calling this function
469 * (if the race condition can happen).
471 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
473 struct list_head *list;
474 struct snd_kcontrol *kctl;
476 snd_assert(card != NULL && numid != 0, return NULL);
477 list_for_each(list, &card->controls) {
478 kctl = snd_kcontrol(list);
479 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
486 * snd_ctl_find_id - find the control instance with the given id
487 * @card: the card instance
488 * @id: the id to search
490 * Finds the control instance with the given id from the card.
492 * Returns the pointer of the instance if found, or NULL if not.
494 * The caller must down card->controls_rwsem before calling this function
495 * (if the race condition can happen).
497 struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
498 struct snd_ctl_elem_id *id)
500 struct list_head *list;
501 struct snd_kcontrol *kctl;
503 snd_assert(card != NULL && id != NULL, return NULL);
505 return snd_ctl_find_numid(card, id->numid);
506 list_for_each(list, &card->controls) {
507 kctl = snd_kcontrol(list);
508 if (kctl->id.iface != id->iface)
510 if (kctl->id.device != id->device)
512 if (kctl->id.subdevice != id->subdevice)
514 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
516 if (kctl->id.index > id->index)
518 if (kctl->id.index + kctl->count <= id->index)
525 static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
526 unsigned int cmd, void __user *arg)
528 struct snd_ctl_card_info *info;
530 info = kzalloc(sizeof(*info), GFP_KERNEL);
533 down_read(&snd_ioctl_rwsem);
534 info->card = card->number;
535 strlcpy(info->id, card->id, sizeof(info->id));
536 strlcpy(info->driver, card->driver, sizeof(info->driver));
537 strlcpy(info->name, card->shortname, sizeof(info->name));
538 strlcpy(info->longname, card->longname, sizeof(info->longname));
539 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
540 strlcpy(info->components, card->components, sizeof(info->components));
541 up_read(&snd_ioctl_rwsem);
542 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
550 static int snd_ctl_elem_list(struct snd_card *card,
551 struct snd_ctl_elem_list __user *_list)
553 struct list_head *plist;
554 struct snd_ctl_elem_list list;
555 struct snd_kcontrol *kctl;
556 struct snd_ctl_elem_id *dst, *id;
557 unsigned int offset, space, first, jidx;
559 if (copy_from_user(&list, _list, sizeof(list)))
561 offset = list.offset;
564 /* try limit maximum space */
568 /* allocate temporary buffer for atomic operation */
569 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
572 down_read(&card->controls_rwsem);
573 list.count = card->controls_count;
574 plist = card->controls.next;
575 while (plist != &card->controls) {
578 kctl = snd_kcontrol(plist);
579 if (offset < kctl->count)
581 offset -= kctl->count;
586 while (space > 0 && plist != &card->controls) {
587 kctl = snd_kcontrol(plist);
588 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
589 snd_ctl_build_ioff(id, kctl, jidx);
597 up_read(&card->controls_rwsem);
599 copy_to_user(list.pids, dst,
600 list.used * sizeof(struct snd_ctl_elem_id))) {
606 down_read(&card->controls_rwsem);
607 list.count = card->controls_count;
608 up_read(&card->controls_rwsem);
610 if (copy_to_user(_list, &list, sizeof(list)))
615 static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
616 struct snd_ctl_elem_info *info)
618 struct snd_card *card = ctl->card;
619 struct snd_kcontrol *kctl;
620 struct snd_kcontrol_volatile *vd;
621 unsigned int index_offset;
624 down_read(&card->controls_rwsem);
625 kctl = snd_ctl_find_id(card, &info->id);
627 up_read(&card->controls_rwsem);
630 #ifdef CONFIG_SND_DEBUG
633 result = kctl->info(kctl, info);
635 snd_assert(info->access == 0, );
636 index_offset = snd_ctl_get_ioff(kctl, &info->id);
637 vd = &kctl->vd[index_offset];
638 snd_ctl_build_ioff(&info->id, kctl, index_offset);
639 info->access = vd->access;
641 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
642 if (vd->owner == ctl)
643 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
644 info->owner = vd->owner_pid;
649 up_read(&card->controls_rwsem);
653 static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
654 struct snd_ctl_elem_info __user *_info)
656 struct snd_ctl_elem_info info;
659 if (copy_from_user(&info, _info, sizeof(info)))
661 result = snd_ctl_elem_info(ctl, &info);
663 if (copy_to_user(_info, &info, sizeof(info)))
668 int snd_ctl_elem_read(struct snd_card *card, struct snd_ctl_elem_value *control)
670 struct snd_kcontrol *kctl;
671 struct snd_kcontrol_volatile *vd;
672 unsigned int index_offset;
673 int result, indirect;
675 down_read(&card->controls_rwsem);
676 kctl = snd_ctl_find_id(card, &control->id);
680 index_offset = snd_ctl_get_ioff(kctl, &control->id);
681 vd = &kctl->vd[index_offset];
682 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
683 if (control->indirect != indirect) {
686 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) && kctl->get != NULL) {
687 snd_ctl_build_ioff(&control->id, kctl, index_offset);
688 result = kctl->get(kctl, control);
694 up_read(&card->controls_rwsem);
698 static int snd_ctl_elem_read_user(struct snd_card *card,
699 struct snd_ctl_elem_value __user *_control)
701 struct snd_ctl_elem_value *control;
704 control = kmalloc(sizeof(*control), GFP_KERNEL);
707 if (copy_from_user(control, _control, sizeof(*control))) {
711 result = snd_ctl_elem_read(card, control);
713 if (copy_to_user(_control, control, sizeof(*control)))
719 int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
720 struct snd_ctl_elem_value *control)
722 struct snd_kcontrol *kctl;
723 struct snd_kcontrol_volatile *vd;
724 unsigned int index_offset;
725 int result, indirect;
727 down_read(&card->controls_rwsem);
728 kctl = snd_ctl_find_id(card, &control->id);
732 index_offset = snd_ctl_get_ioff(kctl, &control->id);
733 vd = &kctl->vd[index_offset];
734 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
735 if (control->indirect != indirect) {
738 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
740 (file && vd->owner != NULL && vd->owner != file)) {
743 snd_ctl_build_ioff(&control->id, kctl, index_offset);
744 result = kctl->put(kctl, control);
747 up_read(&card->controls_rwsem);
748 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &control->id);
753 up_read(&card->controls_rwsem);
757 static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
758 struct snd_ctl_elem_value __user *_control)
760 struct snd_ctl_elem_value *control;
763 control = kmalloc(sizeof(*control), GFP_KERNEL);
766 if (copy_from_user(control, _control, sizeof(*control))) {
770 result = snd_ctl_elem_write(file->card, file, control);
772 if (copy_to_user(_control, control, sizeof(*control)))
778 static int snd_ctl_elem_lock(struct snd_ctl_file *file,
779 struct snd_ctl_elem_id __user *_id)
781 struct snd_card *card = file->card;
782 struct snd_ctl_elem_id id;
783 struct snd_kcontrol *kctl;
784 struct snd_kcontrol_volatile *vd;
787 if (copy_from_user(&id, _id, sizeof(id)))
789 down_write(&card->controls_rwsem);
790 kctl = snd_ctl_find_id(card, &id);
794 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
795 if (vd->owner != NULL)
799 vd->owner_pid = current->pid;
803 up_write(&card->controls_rwsem);
807 static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
808 struct snd_ctl_elem_id __user *_id)
810 struct snd_card *card = file->card;
811 struct snd_ctl_elem_id id;
812 struct snd_kcontrol *kctl;
813 struct snd_kcontrol_volatile *vd;
816 if (copy_from_user(&id, _id, sizeof(id)))
818 down_write(&card->controls_rwsem);
819 kctl = snd_ctl_find_id(card, &id);
823 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
824 if (vd->owner == NULL)
826 else if (vd->owner != file)
834 up_write(&card->controls_rwsem);
838 struct user_element {
839 struct snd_ctl_elem_info info;
840 void *elem_data; /* element data */
841 unsigned long elem_data_size; /* size of element data in bytes */
842 void *priv_data; /* private data (like strings for enumerated type) */
843 unsigned long priv_data_size; /* size of private data in bytes */
846 static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
847 struct snd_ctl_elem_info *uinfo)
849 struct user_element *ue = kcontrol->private_data;
855 static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
856 struct snd_ctl_elem_value *ucontrol)
858 struct user_element *ue = kcontrol->private_data;
860 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
864 static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
865 struct snd_ctl_elem_value *ucontrol)
868 struct user_element *ue = kcontrol->private_data;
870 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
872 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
876 static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
878 kfree(kcontrol->private_data);
881 static int snd_ctl_elem_add(struct snd_ctl_file *file,
882 struct snd_ctl_elem_info *info, int replace)
884 struct snd_card *card = file->card;
885 struct snd_kcontrol kctl, *_kctl;
888 struct user_element *ue;
891 if (card->user_ctl_count >= MAX_USER_CONTROLS)
893 if (info->count > 1024)
895 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
896 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
897 SNDRV_CTL_ELEM_ACCESS_INACTIVE));
899 memset(&kctl, 0, sizeof(kctl));
900 down_write(&card->controls_rwsem);
901 _kctl = snd_ctl_find_id(card, &info->id);
905 err = snd_ctl_remove(card, _kctl);
912 up_write(&card->controls_rwsem);
915 memcpy(&kctl.id, &info->id, sizeof(info->id));
916 kctl.count = info->owner ? info->owner : 1;
917 access |= SNDRV_CTL_ELEM_ACCESS_USER;
918 kctl.info = snd_ctl_elem_user_info;
919 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
920 kctl.get = snd_ctl_elem_user_get;
921 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
922 kctl.put = snd_ctl_elem_user_put;
923 switch (info->type) {
924 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
925 private_size = sizeof(char);
926 if (info->count > 128)
929 case SNDRV_CTL_ELEM_TYPE_INTEGER:
930 private_size = sizeof(long);
931 if (info->count > 128)
934 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
935 private_size = sizeof(long long);
936 if (info->count > 64)
939 case SNDRV_CTL_ELEM_TYPE_BYTES:
940 private_size = sizeof(unsigned char);
941 if (info->count > 512)
944 case SNDRV_CTL_ELEM_TYPE_IEC958:
945 private_size = sizeof(struct snd_aes_iec958);
946 if (info->count != 1)
952 private_size *= info->count;
953 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
957 ue->elem_data = (char *)ue + sizeof(*ue);
958 ue->elem_data_size = private_size;
959 kctl.private_free = snd_ctl_elem_user_free;
960 _kctl = snd_ctl_new(&kctl, access);
962 kfree(_kctl->private_data);
965 _kctl->private_data = ue;
966 for (idx = 0; idx < _kctl->count; idx++)
967 _kctl->vd[idx].owner = file;
968 err = snd_ctl_add(card, _kctl);
970 snd_ctl_free_one(_kctl);
974 down_write(&card->controls_rwsem);
975 card->user_ctl_count++;
976 up_write(&card->controls_rwsem);
981 static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
982 struct snd_ctl_elem_info __user *_info, int replace)
984 struct snd_ctl_elem_info info;
985 if (copy_from_user(&info, _info, sizeof(info)))
987 return snd_ctl_elem_add(file, &info, replace);
990 static int snd_ctl_elem_remove(struct snd_ctl_file *file,
991 struct snd_ctl_elem_id __user *_id)
993 struct snd_ctl_elem_id id;
996 if (copy_from_user(&id, _id, sizeof(id)))
998 err = snd_ctl_remove_unlocked_id(file, &id);
1000 struct snd_card *card = file->card;
1001 down_write(&card->controls_rwsem);
1002 card->user_ctl_count--;
1003 up_write(&card->controls_rwsem);
1008 static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
1011 if (get_user(subscribe, ptr))
1013 if (subscribe < 0) {
1014 subscribe = file->subscribed;
1015 if (put_user(subscribe, ptr))
1020 file->subscribed = 1;
1022 } else if (file->subscribed) {
1023 snd_ctl_empty_read_queue(file);
1024 file->subscribed = 0;
1029 static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1031 struct snd_ctl_file *ctl;
1032 struct snd_card *card;
1033 struct list_head *list;
1034 struct snd_kctl_ioctl *p;
1035 void __user *argp = (void __user *)arg;
1036 int __user *ip = argp;
1039 ctl = file->private_data;
1041 snd_assert(card != NULL, return -ENXIO);
1043 case SNDRV_CTL_IOCTL_PVERSION:
1044 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1045 case SNDRV_CTL_IOCTL_CARD_INFO:
1046 return snd_ctl_card_info(card, ctl, cmd, argp);
1047 case SNDRV_CTL_IOCTL_ELEM_LIST:
1048 return snd_ctl_elem_list(ctl->card, argp);
1049 case SNDRV_CTL_IOCTL_ELEM_INFO:
1050 return snd_ctl_elem_info_user(ctl, argp);
1051 case SNDRV_CTL_IOCTL_ELEM_READ:
1052 return snd_ctl_elem_read_user(ctl->card, argp);
1053 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1054 return snd_ctl_elem_write_user(ctl, argp);
1055 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1056 return snd_ctl_elem_lock(ctl, argp);
1057 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1058 return snd_ctl_elem_unlock(ctl, argp);
1059 case SNDRV_CTL_IOCTL_ELEM_ADD:
1060 return snd_ctl_elem_add_user(ctl, argp, 0);
1061 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1062 return snd_ctl_elem_add_user(ctl, argp, 1);
1063 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1064 return snd_ctl_elem_remove(ctl, argp);
1065 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1066 return snd_ctl_subscribe_events(ctl, ip);
1067 case SNDRV_CTL_IOCTL_POWER:
1068 return -ENOPROTOOPT;
1069 case SNDRV_CTL_IOCTL_POWER_STATE:
1071 return put_user(card->power_state, ip) ? -EFAULT : 0;
1073 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1076 down_read(&snd_ioctl_rwsem);
1077 list_for_each(list, &snd_control_ioctls) {
1078 p = list_entry(list, struct snd_kctl_ioctl, list);
1079 err = p->fioctl(card, ctl, cmd, arg);
1080 if (err != -ENOIOCTLCMD) {
1081 up_read(&snd_ioctl_rwsem);
1085 up_read(&snd_ioctl_rwsem);
1086 snd_printdd("unknown ioctl = 0x%x\n", cmd);
1090 static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1091 size_t count, loff_t * offset)
1093 struct snd_ctl_file *ctl;
1097 ctl = file->private_data;
1098 snd_assert(ctl != NULL && ctl->card != NULL, return -ENXIO);
1099 if (!ctl->subscribed)
1101 if (count < sizeof(struct snd_ctl_event))
1103 spin_lock_irq(&ctl->read_lock);
1104 while (count >= sizeof(struct snd_ctl_event)) {
1105 struct snd_ctl_event ev;
1106 struct snd_kctl_event *kev;
1107 while (list_empty(&ctl->events)) {
1109 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1113 init_waitqueue_entry(&wait, current);
1114 add_wait_queue(&ctl->change_sleep, &wait);
1115 set_current_state(TASK_INTERRUPTIBLE);
1116 spin_unlock_irq(&ctl->read_lock);
1118 remove_wait_queue(&ctl->change_sleep, &wait);
1119 if (signal_pending(current))
1120 return result > 0 ? result : -ERESTARTSYS;
1121 spin_lock_irq(&ctl->read_lock);
1123 kev = snd_kctl_event(ctl->events.next);
1124 ev.type = SNDRV_CTL_EVENT_ELEM;
1125 ev.data.elem.mask = kev->mask;
1126 ev.data.elem.id = kev->id;
1127 list_del(&kev->list);
1128 spin_unlock_irq(&ctl->read_lock);
1130 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
1134 spin_lock_irq(&ctl->read_lock);
1135 buffer += sizeof(struct snd_ctl_event);
1136 count -= sizeof(struct snd_ctl_event);
1137 result += sizeof(struct snd_ctl_event);
1140 spin_unlock_irq(&ctl->read_lock);
1142 return result > 0 ? result : err;
1145 static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1148 struct snd_ctl_file *ctl;
1150 ctl = file->private_data;
1151 if (!ctl->subscribed)
1153 poll_wait(file, &ctl->change_sleep, wait);
1156 if (!list_empty(&ctl->events))
1157 mask |= POLLIN | POLLRDNORM;
1163 * register the device-specific control-ioctls.
1164 * called from each device manager like pcm.c, hwdep.c, etc.
1166 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1168 struct snd_kctl_ioctl *pn;
1170 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
1174 down_write(&snd_ioctl_rwsem);
1175 list_add_tail(&pn->list, lists);
1176 up_write(&snd_ioctl_rwsem);
1180 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1182 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1185 #ifdef CONFIG_COMPAT
1186 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1188 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1193 * de-register the device-specific control-ioctls.
1195 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1196 struct list_head *lists)
1198 struct list_head *list;
1199 struct snd_kctl_ioctl *p;
1201 snd_assert(fcn != NULL, return -EINVAL);
1202 down_write(&snd_ioctl_rwsem);
1203 list_for_each(list, lists) {
1204 p = list_entry(list, struct snd_kctl_ioctl, list);
1205 if (p->fioctl == fcn) {
1207 up_write(&snd_ioctl_rwsem);
1212 up_write(&snd_ioctl_rwsem);
1217 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1219 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1222 #ifdef CONFIG_COMPAT
1223 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1225 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1230 static int snd_ctl_fasync(int fd, struct file * file, int on)
1232 struct snd_ctl_file *ctl;
1234 ctl = file->private_data;
1235 err = fasync_helper(fd, file, on, &ctl->fasync);
1244 #ifdef CONFIG_COMPAT
1245 #include "control_compat.c"
1247 #define snd_ctl_ioctl_compat NULL
1254 static struct file_operations snd_ctl_f_ops =
1256 .owner = THIS_MODULE,
1257 .read = snd_ctl_read,
1258 .open = snd_ctl_open,
1259 .release = snd_ctl_release,
1260 .poll = snd_ctl_poll,
1261 .unlocked_ioctl = snd_ctl_ioctl,
1262 .compat_ioctl = snd_ctl_ioctl_compat,
1263 .fasync = snd_ctl_fasync,
1267 * registration of the control device
1269 static int snd_ctl_dev_register(struct snd_device *device)
1271 struct snd_card *card = device->device_data;
1275 snd_assert(card != NULL, return -ENXIO);
1276 cardnum = card->number;
1277 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1278 sprintf(name, "controlC%i", cardnum);
1279 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1280 &snd_ctl_f_ops, card, name)) < 0)
1286 * disconnection of the control device
1288 static int snd_ctl_dev_disconnect(struct snd_device *device)
1290 struct snd_card *card = device->device_data;
1291 struct list_head *flist;
1292 struct snd_ctl_file *ctl;
1294 down_read(&card->controls_rwsem);
1295 list_for_each(flist, &card->ctl_files) {
1296 ctl = snd_ctl_file(flist);
1297 wake_up(&ctl->change_sleep);
1298 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1300 up_read(&card->controls_rwsem);
1307 static int snd_ctl_dev_free(struct snd_device *device)
1309 struct snd_card *card = device->device_data;
1310 struct snd_kcontrol *control;
1312 down_write(&card->controls_rwsem);
1313 while (!list_empty(&card->controls)) {
1314 control = snd_kcontrol(card->controls.next);
1315 snd_ctl_remove(card, control);
1317 up_write(&card->controls_rwsem);
1322 * de-registration of the control device
1324 static int snd_ctl_dev_unregister(struct snd_device *device)
1326 struct snd_card *card = device->device_data;
1329 snd_assert(card != NULL, return -ENXIO);
1330 cardnum = card->number;
1331 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1332 if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
1335 return snd_ctl_dev_free(device);
1339 * create control core:
1340 * called from init.c
1342 int snd_ctl_create(struct snd_card *card)
1344 static struct snd_device_ops ops = {
1345 .dev_free = snd_ctl_dev_free,
1346 .dev_register = snd_ctl_dev_register,
1347 .dev_disconnect = snd_ctl_dev_disconnect,
1348 .dev_unregister = snd_ctl_dev_unregister
1351 snd_assert(card != NULL, return -ENXIO);
1352 return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);