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 typedef struct _snd_kctl_ioctl {
35 struct list_head list; /* list of all ioctls */
36 snd_kctl_ioctl_func_t fioctl;
39 #define snd_kctl_ioctl(n) list_entry(n, snd_kctl_ioctl_t, list)
41 static DECLARE_RWSEM(snd_ioctl_rwsem);
42 static LIST_HEAD(snd_control_ioctls);
44 static int snd_ctl_open(struct inode *inode, struct file *file)
46 int cardnum = SNDRV_MINOR_CARD(iminor(inode));
52 card = snd_cards[cardnum];
57 err = snd_card_file_add(card, file);
62 if (!try_module_get(card->module)) {
66 ctl = kcalloc(1, sizeof(*ctl), GFP_KERNEL);
71 INIT_LIST_HEAD(&ctl->events);
72 init_waitqueue_head(&ctl->change_sleep);
73 spin_lock_init(&ctl->read_lock);
75 ctl->pid = current->pid;
76 file->private_data = ctl;
77 write_lock_irqsave(&card->ctl_files_rwlock, flags);
78 list_add_tail(&ctl->list, &card->ctl_files);
79 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
83 module_put(card->module);
85 snd_card_file_remove(card, file);
90 static void snd_ctl_empty_read_queue(snd_ctl_file_t * ctl)
92 snd_kctl_event_t *cread;
94 spin_lock(&ctl->read_lock);
95 while (!list_empty(&ctl->events)) {
96 cread = snd_kctl_event(ctl->events.next);
97 list_del(&cread->list);
100 spin_unlock(&ctl->read_lock);
103 static int snd_ctl_release(struct inode *inode, struct file *file)
106 struct list_head *list;
109 snd_kcontrol_t *control;
112 ctl = file->private_data;
113 fasync_helper(-1, file, 0, &ctl->fasync);
114 file->private_data = NULL;
116 write_lock_irqsave(&card->ctl_files_rwlock, flags);
117 list_del(&ctl->list);
118 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
119 down_write(&card->controls_rwsem);
120 list_for_each(list, &card->controls) {
121 control = snd_kcontrol(list);
122 for (idx = 0; idx < control->count; idx++)
123 if (control->vd[idx].owner == ctl)
124 control->vd[idx].owner = NULL;
126 up_write(&card->controls_rwsem);
127 snd_ctl_empty_read_queue(ctl);
129 module_put(card->module);
130 snd_card_file_remove(card, file);
134 void snd_ctl_notify(snd_card_t *card, unsigned int mask, snd_ctl_elem_id_t *id)
137 struct list_head *flist;
139 snd_kctl_event_t *ev;
141 snd_runtime_check(card != NULL && id != NULL, return);
142 read_lock(&card->ctl_files_rwlock);
143 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
144 card->mixer_oss_change_count++;
146 list_for_each(flist, &card->ctl_files) {
147 struct list_head *elist;
148 ctl = snd_ctl_file(flist);
149 if (!ctl->subscribed)
151 spin_lock_irqsave(&ctl->read_lock, flags);
152 list_for_each(elist, &ctl->events) {
153 ev = snd_kctl_event(elist);
154 if (ev->id.numid == id->numid) {
159 ev = kcalloc(1, sizeof(*ev), GFP_ATOMIC);
163 list_add_tail(&ev->list, &ctl->events);
165 snd_printk(KERN_ERR "No memory available to allocate event\n");
168 wake_up(&ctl->change_sleep);
169 spin_unlock_irqrestore(&ctl->read_lock, flags);
170 kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
172 read_unlock(&card->ctl_files_rwlock);
176 * snd_ctl_new - create a control instance from the template
177 * @control: the control template
178 * @access: the default control access
180 * Allocates a new snd_kcontrol_t instance and copies the given template
181 * to the new instance. It does not copy volatile data (access).
183 * Returns the pointer of the new instance, or NULL on failure.
185 snd_kcontrol_t *snd_ctl_new(snd_kcontrol_t * control, unsigned int access)
187 snd_kcontrol_t *kctl;
190 snd_runtime_check(control != NULL, return NULL);
191 snd_runtime_check(control->count > 0, return NULL);
192 kctl = kcalloc(1, sizeof(*kctl) + sizeof(snd_kcontrol_volatile_t) * control->count, GFP_KERNEL);
196 for (idx = 0; idx < kctl->count; idx++)
197 kctl->vd[idx].access = access;
202 * snd_ctl_new1 - create a control instance from the template
203 * @ncontrol: the initialization record
204 * @private_data: the private data to set
206 * Allocates a new snd_kcontrol_t instance and initialize from the given
207 * template. When the access field of ncontrol is 0, it's assumed as
208 * READWRITE access. When the count field is 0, it's assumes as one.
210 * Returns the pointer of the newly generated instance, or NULL on failure.
212 snd_kcontrol_t *snd_ctl_new1(snd_kcontrol_new_t * ncontrol, void *private_data)
217 snd_runtime_check(ncontrol != NULL, return NULL);
218 snd_assert(ncontrol->info != NULL, return NULL);
219 memset(&kctl, 0, sizeof(kctl));
220 kctl.id.iface = ncontrol->iface;
221 kctl.id.device = ncontrol->device;
222 kctl.id.subdevice = ncontrol->subdevice;
224 strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
225 kctl.id.index = ncontrol->index;
226 kctl.count = ncontrol->count ? ncontrol->count : 1;
227 access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
228 (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|SNDRV_CTL_ELEM_ACCESS_INACTIVE|
229 SNDRV_CTL_ELEM_ACCESS_DINDIRECT|SNDRV_CTL_ELEM_ACCESS_INDIRECT));
230 kctl.info = ncontrol->info;
231 kctl.get = ncontrol->get;
232 kctl.put = ncontrol->put;
233 kctl.private_value = ncontrol->private_value;
234 kctl.private_data = private_data;
235 return snd_ctl_new(&kctl, access);
239 * snd_ctl_free_one - release the control instance
240 * @kcontrol: the control instance
242 * Releases the control instance created via snd_ctl_new()
244 * Don't call this after the control was added to the card.
246 void snd_ctl_free_one(snd_kcontrol_t * kcontrol)
249 if (kcontrol->private_free)
250 kcontrol->private_free(kcontrol);
255 static unsigned int snd_ctl_hole_check(snd_card_t * card,
258 struct list_head *list;
259 snd_kcontrol_t *kctl;
261 list_for_each(list, &card->controls) {
262 kctl = snd_kcontrol(list);
263 if ((kctl->id.numid <= card->last_numid &&
264 kctl->id.numid + kctl->count > card->last_numid) ||
265 (kctl->id.numid <= card->last_numid + count - 1 &&
266 kctl->id.numid + kctl->count > card->last_numid + count - 1))
267 return card->last_numid = kctl->id.numid + kctl->count - 1;
269 return card->last_numid;
272 static int snd_ctl_find_hole(snd_card_t * card, unsigned int count)
274 unsigned int last_numid, iter = 100000;
276 last_numid = card->last_numid;
277 while (last_numid != snd_ctl_hole_check(card, count)) {
279 /* this situation is very unlikely */
280 snd_printk(KERN_ERR "unable to allocate new control numid\n");
283 last_numid = card->last_numid;
289 * snd_ctl_add - add the control instance to the card
290 * @card: the card instance
291 * @kcontrol: the control instance to add
293 * Adds the control instance created via snd_ctl_new() or
294 * snd_ctl_new1() to the given card. Assigns also an unique
295 * numid used for fast search.
297 * Returns zero if successful, or a negative error code on failure.
299 * It frees automatically the control which cannot be added.
301 int snd_ctl_add(snd_card_t * card, snd_kcontrol_t * kcontrol)
303 snd_ctl_elem_id_t id;
306 snd_runtime_check(card != NULL && kcontrol != NULL, return -EINVAL);
307 snd_assert(kcontrol->info != NULL, return -EINVAL);
309 down_write(&card->controls_rwsem);
310 if (snd_ctl_find_id(card, &id)) {
311 up_write(&card->controls_rwsem);
312 snd_ctl_free_one(kcontrol);
313 snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
321 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
322 up_write(&card->controls_rwsem);
323 snd_ctl_free_one(kcontrol);
326 list_add_tail(&kcontrol->list, &card->controls);
327 card->controls_count += kcontrol->count;
328 kcontrol->id.numid = card->last_numid + 1;
329 card->last_numid += kcontrol->count;
330 up_write(&card->controls_rwsem);
331 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
332 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
337 * snd_ctl_remove - remove the control from the card and release it
338 * @card: the card instance
339 * @kcontrol: the control instance to remove
341 * Removes the control from the card and then releases the instance.
342 * You don't need to call snd_ctl_free_one(). You must be in
343 * the write lock - down_write(&card->controls_rwsem).
345 * Returns 0 if successful, or a negative error code on failure.
347 int snd_ctl_remove(snd_card_t * card, snd_kcontrol_t * kcontrol)
349 snd_ctl_elem_id_t id;
352 snd_runtime_check(card != NULL && kcontrol != NULL, return -EINVAL);
353 list_del(&kcontrol->list);
354 card->controls_count -= kcontrol->count;
356 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
357 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
358 snd_ctl_free_one(kcontrol);
363 * snd_ctl_remove_id - remove the control of the given id and release it
364 * @card: the card instance
365 * @id: the control id to remove
367 * Finds the control instance with the given id, removes it from the
368 * card list and releases it.
370 * Returns 0 if successful, or a negative error code on failure.
372 int snd_ctl_remove_id(snd_card_t * card, snd_ctl_elem_id_t *id)
374 snd_kcontrol_t *kctl;
377 down_write(&card->controls_rwsem);
378 kctl = snd_ctl_find_id(card, id);
380 up_write(&card->controls_rwsem);
383 ret = snd_ctl_remove(card, kctl);
384 up_write(&card->controls_rwsem);
389 * snd_ctl_remove_unlocked_id - remove the unlocked control of the given id and release it
390 * @file: active control handle
391 * @id: the control id to remove
393 * Finds the control instance with the given id, removes it from the
394 * card list and releases it.
396 * Returns 0 if successful, or a negative error code on failure.
398 static int snd_ctl_remove_unlocked_id(snd_ctl_file_t * file, snd_ctl_elem_id_t *id)
400 snd_card_t *card = file->card;
401 snd_kcontrol_t *kctl;
404 down_write(&card->controls_rwsem);
405 kctl = snd_ctl_find_id(card, id);
407 up_write(&card->controls_rwsem);
410 for (idx = 0; idx < kctl->count; idx++)
411 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
412 up_write(&card->controls_rwsem);
415 ret = snd_ctl_remove(card, kctl);
416 up_write(&card->controls_rwsem);
421 * snd_ctl_rename_id - replace the id of a control on the card
422 * @card: the card instance
423 * @src_id: the old id
424 * @dst_id: the new id
426 * Finds the control with the old id from the card, and replaces the
427 * id with the new one.
429 * Returns zero if successful, or a negative error code on failure.
431 int snd_ctl_rename_id(snd_card_t * card, snd_ctl_elem_id_t *src_id, snd_ctl_elem_id_t *dst_id)
433 snd_kcontrol_t *kctl;
435 down_write(&card->controls_rwsem);
436 kctl = snd_ctl_find_id(card, src_id);
438 up_write(&card->controls_rwsem);
442 kctl->id.numid = card->last_numid + 1;
443 card->last_numid += kctl->count;
444 up_write(&card->controls_rwsem);
449 * snd_ctl_find_numid - find the control instance with the given number-id
450 * @card: the card instance
451 * @numid: the number-id to search
453 * Finds the control instance with the given number-id from the card.
455 * Returns the pointer of the instance if found, or NULL if not.
457 * The caller must down card->controls_rwsem before calling this function
458 * (if the race condition can happen).
460 snd_kcontrol_t *snd_ctl_find_numid(snd_card_t * card, unsigned int numid)
462 struct list_head *list;
463 snd_kcontrol_t *kctl;
465 snd_runtime_check(card != NULL && numid != 0, return NULL);
466 list_for_each(list, &card->controls) {
467 kctl = snd_kcontrol(list);
468 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
475 * snd_ctl_find_id - find the control instance with the given id
476 * @card: the card instance
477 * @id: the id to search
479 * Finds the control instance with the given id from the card.
481 * Returns the pointer of the instance if found, or NULL if not.
483 * The caller must down card->controls_rwsem before calling this function
484 * (if the race condition can happen).
486 snd_kcontrol_t *snd_ctl_find_id(snd_card_t * card, snd_ctl_elem_id_t *id)
488 struct list_head *list;
489 snd_kcontrol_t *kctl;
491 snd_runtime_check(card != NULL && id != NULL, return NULL);
493 return snd_ctl_find_numid(card, id->numid);
494 list_for_each(list, &card->controls) {
495 kctl = snd_kcontrol(list);
496 if (kctl->id.iface != id->iface)
498 if (kctl->id.device != id->device)
500 if (kctl->id.subdevice != id->subdevice)
502 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
504 if (kctl->id.index > id->index)
506 if (kctl->id.index + kctl->count <= id->index)
513 static int snd_ctl_card_info(snd_card_t * card, snd_ctl_file_t * ctl,
514 unsigned int cmd, void __user *arg)
516 snd_ctl_card_info_t info;
518 memset(&info, 0, sizeof(info));
519 down_read(&snd_ioctl_rwsem);
520 info.card = card->number;
521 strlcpy(info.id, card->id, sizeof(info.id));
522 strlcpy(info.driver, card->driver, sizeof(info.driver));
523 strlcpy(info.name, card->shortname, sizeof(info.name));
524 strlcpy(info.longname, card->longname, sizeof(info.longname));
525 strlcpy(info.mixername, card->mixername, sizeof(info.mixername));
526 strlcpy(info.components, card->components, sizeof(info.components));
527 up_read(&snd_ioctl_rwsem);
528 if (copy_to_user(arg, &info, sizeof(snd_ctl_card_info_t)))
533 static int snd_ctl_elem_list(snd_card_t *card, snd_ctl_elem_list_t __user *_list)
535 struct list_head *plist;
536 snd_ctl_elem_list_t list;
537 snd_kcontrol_t *kctl;
538 snd_ctl_elem_id_t *dst, *id;
539 unsigned int offset, space, first, jidx;
541 if (copy_from_user(&list, _list, sizeof(list)))
543 offset = list.offset;
546 /* try limit maximum space */
550 /* allocate temporary buffer for atomic operation */
551 dst = vmalloc(space * sizeof(snd_ctl_elem_id_t));
554 down_read(&card->controls_rwsem);
555 list.count = card->controls_count;
556 plist = card->controls.next;
557 while (plist != &card->controls) {
560 kctl = snd_kcontrol(plist);
561 if (offset < kctl->count)
563 offset -= kctl->count;
568 while (space > 0 && plist != &card->controls) {
569 kctl = snd_kcontrol(plist);
570 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
571 snd_ctl_build_ioff(id, kctl, jidx);
579 up_read(&card->controls_rwsem);
580 if (list.used > 0 && copy_to_user(list.pids, dst, list.used * sizeof(snd_ctl_elem_id_t))) {
586 down_read(&card->controls_rwsem);
587 list.count = card->controls_count;
588 up_read(&card->controls_rwsem);
590 if (copy_to_user(_list, &list, sizeof(list)))
595 static int snd_ctl_elem_info(snd_ctl_file_t *ctl, snd_ctl_elem_info_t __user *_info)
597 snd_card_t *card = ctl->card;
598 snd_ctl_elem_info_t info;
599 snd_kcontrol_t *kctl;
600 snd_kcontrol_volatile_t *vd;
601 unsigned int index_offset;
604 if (copy_from_user(&info, _info, sizeof(info)))
606 down_read(&card->controls_rwsem);
607 kctl = snd_ctl_find_id(card, &info.id);
609 up_read(&card->controls_rwsem);
612 #ifdef CONFIG_SND_DEBUG
615 result = kctl->info(kctl, &info);
617 snd_assert(info.access == 0, );
618 index_offset = snd_ctl_get_ioff(kctl, &info.id);
619 vd = &kctl->vd[index_offset];
620 snd_ctl_build_ioff(&info.id, kctl, index_offset);
621 info.access = vd->access;
623 info.access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
624 if (vd->owner == ctl)
625 info.access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
626 info.owner = vd->owner_pid;
631 up_read(&card->controls_rwsem);
633 if (copy_to_user(_info, &info, sizeof(info)))
638 static int snd_ctl_elem_read(snd_card_t *card, snd_ctl_elem_value_t __user *_control)
640 snd_ctl_elem_value_t *control;
641 snd_kcontrol_t *kctl;
642 snd_kcontrol_volatile_t *vd;
643 unsigned int index_offset;
644 int result, indirect;
646 control = kmalloc(sizeof(*control), GFP_KERNEL);
649 if (copy_from_user(control, _control, sizeof(*control)))
651 down_read(&card->controls_rwsem);
652 kctl = snd_ctl_find_id(card, &control->id);
656 index_offset = snd_ctl_get_ioff(kctl, &control->id);
657 vd = &kctl->vd[index_offset];
658 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
659 if (control->indirect != indirect) {
662 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) && kctl->get != NULL) {
663 snd_ctl_build_ioff(&control->id, kctl, index_offset);
664 result = kctl->get(kctl, control);
670 up_read(&card->controls_rwsem);
672 if (copy_to_user(_control, control, sizeof(*control)))
678 static int snd_ctl_elem_write(snd_ctl_file_t *file, snd_ctl_elem_value_t __user *_control)
680 snd_card_t *card = file->card;
681 snd_ctl_elem_value_t *control;
682 snd_kcontrol_t *kctl;
683 snd_kcontrol_volatile_t *vd;
684 unsigned int index_offset;
685 int result, indirect;
687 control = kmalloc(sizeof(*control), GFP_KERNEL);
690 if (copy_from_user(control, _control, sizeof(*control)))
692 down_read(&card->controls_rwsem);
693 kctl = snd_ctl_find_id(card, &control->id);
697 index_offset = snd_ctl_get_ioff(kctl, &control->id);
698 vd = &kctl->vd[index_offset];
699 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
700 if (control->indirect != indirect) {
703 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
705 (vd->owner != NULL && vd->owner != file)) {
708 snd_ctl_build_ioff(&control->id, kctl, index_offset);
709 result = kctl->put(kctl, control);
712 up_read(&card->controls_rwsem);
713 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &control->id);
719 up_read(&card->controls_rwsem);
722 if (copy_to_user(_control, control, sizeof(*control)))
728 static int snd_ctl_elem_lock(snd_ctl_file_t *file, snd_ctl_elem_id_t __user *_id)
730 snd_card_t *card = file->card;
731 snd_ctl_elem_id_t id;
732 snd_kcontrol_t *kctl;
733 snd_kcontrol_volatile_t *vd;
736 if (copy_from_user(&id, _id, sizeof(id)))
738 down_write(&card->controls_rwsem);
739 kctl = snd_ctl_find_id(card, &id);
743 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
744 if (vd->owner != NULL)
748 vd->owner_pid = current->pid;
752 up_write(&card->controls_rwsem);
756 static int snd_ctl_elem_unlock(snd_ctl_file_t *file, snd_ctl_elem_id_t __user *_id)
758 snd_card_t *card = file->card;
759 snd_ctl_elem_id_t id;
760 snd_kcontrol_t *kctl;
761 snd_kcontrol_volatile_t *vd;
764 if (copy_from_user(&id, _id, sizeof(id)))
766 down_write(&card->controls_rwsem);
767 kctl = snd_ctl_find_id(card, &id);
771 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
772 if (vd->owner == NULL)
774 else if (vd->owner != file)
782 up_write(&card->controls_rwsem);
786 struct user_element {
787 enum sndrv_ctl_elem_type type; /* element type */
788 unsigned int elem_count; /* count of elements */
794 void *elem_data; /* element data */
795 unsigned long elem_data_size; /* size of element data in bytes */
796 void *priv_data; /* private data (like strings for enumerated type) */
797 unsigned long priv_data_size; /* size of private data in bytes */
798 unsigned short dimen_count; /* count of dimensions */
799 unsigned short dimen[0]; /* array of dimensions */
802 static int snd_ctl_elem_user_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
804 struct user_element *ue = kcontrol->private_data;
806 uinfo->type = ue->type;
807 uinfo->count = ue->elem_count;
808 if (ue->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
809 uinfo->value.enumerated.items = ue->u.enumerated.items;
810 if (uinfo->value.enumerated.item >= ue->u.enumerated.items)
811 uinfo->value.enumerated.item = 0;
812 strlcpy(uinfo->value.enumerated.name,
813 (char *)ue->priv_data + uinfo->value.enumerated.item * 64,
819 static int snd_ctl_elem_user_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
821 struct user_element *ue = kcontrol->private_data;
823 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
827 static int snd_ctl_elem_user_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
830 struct user_element *ue = kcontrol->private_data;
832 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size);
833 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
837 static void snd_ctl_elem_user_free(snd_kcontrol_t * kcontrol)
839 kfree(kcontrol->private_data);
842 static int snd_ctl_elem_add(snd_ctl_file_t *file, snd_ctl_elem_info_t __user *_info, int replace)
844 snd_card_t *card = file->card;
845 snd_ctl_elem_info_t info;
846 snd_kcontrol_t kctl, *_kctl;
848 long private_size, dimen_size, extra_size;
849 struct user_element *ue;
852 if (copy_from_user(&info, _info, sizeof(info)))
854 if (info.count > 1024)
856 access = info.access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
857 (info.access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|SNDRV_CTL_ELEM_ACCESS_INACTIVE|
858 SNDRV_CTL_ELEM_ACCESS_DINDIRECT|SNDRV_CTL_ELEM_ACCESS_INDIRECT));
859 if (access & (SNDRV_CTL_ELEM_ACCESS_DINDIRECT | SNDRV_CTL_ELEM_ACCESS_INDIRECT))
862 memset(&kctl, 0, sizeof(kctl));
863 down_write(&card->controls_rwsem);
864 if (!!((_kctl = snd_ctl_find_id(card, &info.id)) != NULL) ^ replace) {
865 up_write(&card->controls_rwsem);
866 return !replace ? -EBUSY : -ENOENT;
869 err = snd_ctl_remove(card, _kctl);
871 up_write(&card->controls_rwsem);
875 up_write(&card->controls_rwsem);
876 memcpy(&kctl.id, &info.id, sizeof(info.id));
877 kctl.count = info.owner ? info.owner : 1;
878 access |= SNDRV_CTL_ELEM_ACCESS_USER;
879 kctl.info = snd_ctl_elem_user_info;
880 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
881 kctl.get = snd_ctl_elem_user_get;
882 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
883 kctl.put = snd_ctl_elem_user_put;
886 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
887 private_size = sizeof(char);
888 if (info.count > 128)
891 case SNDRV_CTL_ELEM_TYPE_INTEGER:
892 private_size = sizeof(long);
893 if (info.count > 128)
896 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
897 private_size = sizeof(long long);
901 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
902 private_size = sizeof(unsigned int);
903 if (info.count > 128)
905 if (info.value.enumerated.items > 1024)
907 extra_size = info.value.enumerated.items * 64;
909 case SNDRV_CTL_ELEM_TYPE_BYTES:
910 private_size = sizeof(unsigned char);
911 if (info.count > 512)
914 case SNDRV_CTL_ELEM_TYPE_IEC958:
915 private_size = sizeof(struct sndrv_aes_iec958);
922 private_size *= info.count;
923 if (private_size > 1024 * 1024)
926 if (!(info.access & SNDRV_CTL_ELEM_ACCESS_DINDIRECT))
927 for (idx = 0; idx < 4 && info.dimen.d[idx]; idx++)
928 dimen_size += sizeof(unsigned short);
929 ue = kcalloc(1, sizeof(struct user_element) + dimen_size + private_size + extra_size, GFP_KERNEL);
932 ue->type = info.type;
933 ue->elem_count = info.count;
934 if (!(info.access & SNDRV_CTL_ELEM_ACCESS_DINDIRECT)) {
935 for (idx = 0; idx < 4 && info.dimen.d[idx]; idx++)
936 ue->dimen[idx] = info.dimen.d[idx];
937 ue->dimen_count = dimen_size / sizeof(unsigned short);
939 ue->elem_data = (char *)ue + sizeof(ue) + dimen_size;
940 ue->elem_data_size = private_size;
942 ue->priv_data = (char *)ue + sizeof(ue) + dimen_size + private_size;
943 ue->priv_data_size = extra_size;
944 if (ue->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
945 if (copy_from_user(ue->priv_data, *(char **)info.value.enumerated.name, extra_size))
947 ue->u.enumerated.items = info.value.enumerated.items;
950 kctl.private_free = snd_ctl_elem_user_free;
951 _kctl = snd_ctl_new(&kctl, access);
953 kfree(_kctl->private_data);
956 _kctl->private_data = ue;
957 for (idx = 0; idx < _kctl->count; idx++)
958 _kctl->vd[idx].owner = file;
959 err = snd_ctl_add(card, _kctl);
961 snd_ctl_free_one(_kctl);
967 static int snd_ctl_elem_remove(snd_ctl_file_t *file, snd_ctl_elem_id_t __user *_id)
969 snd_ctl_elem_id_t id;
971 if (copy_from_user(&id, _id, sizeof(id)))
973 return snd_ctl_remove_unlocked_id(file, &id);
976 static int snd_ctl_subscribe_events(snd_ctl_file_t *file, int __user *ptr)
979 if (get_user(subscribe, ptr))
982 subscribe = file->subscribed;
983 if (put_user(subscribe, ptr))
988 file->subscribed = 1;
990 } else if (file->subscribed) {
991 snd_ctl_empty_read_queue(file);
992 file->subscribed = 0;
999 * change the power state
1001 static int snd_ctl_set_power_state(snd_card_t *card, unsigned int power_state)
1003 switch (power_state) {
1004 case SNDRV_CTL_POWER_D0:
1005 case SNDRV_CTL_POWER_D1:
1006 case SNDRV_CTL_POWER_D2:
1007 if (card->power_state != power_state)
1008 /* FIXME: pass the correct state value */
1009 card->pm_resume(card, 0);
1011 case SNDRV_CTL_POWER_D3hot:
1012 case SNDRV_CTL_POWER_D3cold:
1013 if (card->power_state != power_state)
1014 /* FIXME: pass the correct state value */
1015 card->pm_suspend(card, 0);
1024 static inline int _snd_ctl_ioctl(struct inode *inode, struct file *file,
1025 unsigned int cmd, unsigned long arg)
1027 snd_ctl_file_t *ctl;
1029 struct list_head *list;
1030 snd_kctl_ioctl_t *p;
1031 void __user *argp = (void __user *)arg;
1032 int __user *ip = argp;
1035 ctl = file->private_data;
1037 snd_assert(card != NULL, return -ENXIO);
1039 case SNDRV_CTL_IOCTL_PVERSION:
1040 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1041 case SNDRV_CTL_IOCTL_CARD_INFO:
1042 return snd_ctl_card_info(card, ctl, cmd, argp);
1043 case SNDRV_CTL_IOCTL_ELEM_LIST:
1044 return snd_ctl_elem_list(ctl->card, argp);
1045 case SNDRV_CTL_IOCTL_ELEM_INFO:
1046 return snd_ctl_elem_info(ctl, argp);
1047 case SNDRV_CTL_IOCTL_ELEM_READ:
1048 return snd_ctl_elem_read(ctl->card, argp);
1049 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1050 return snd_ctl_elem_write(ctl, argp);
1051 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1052 return snd_ctl_elem_lock(ctl, argp);
1053 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1054 return snd_ctl_elem_unlock(ctl, argp);
1055 case SNDRV_CTL_IOCTL_ELEM_ADD:
1056 return snd_ctl_elem_add(ctl, argp, 0);
1057 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1058 return snd_ctl_elem_add(ctl, argp, 1);
1059 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1060 return snd_ctl_elem_remove(ctl, argp);
1061 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1062 return snd_ctl_subscribe_events(ctl, ip);
1063 case SNDRV_CTL_IOCTL_POWER:
1064 if (get_user(err, ip))
1066 if (!capable(CAP_SYS_ADMIN))
1069 if (card->pm_suspend && card->pm_resume) {
1070 snd_power_lock(card);
1071 err = snd_ctl_set_power_state(card, err);
1072 snd_power_unlock(card);
1077 case SNDRV_CTL_IOCTL_POWER_STATE:
1079 return put_user(card->power_state, ip) ? -EFAULT : 0;
1081 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1084 down_read(&snd_ioctl_rwsem);
1085 list_for_each(list, &snd_control_ioctls) {
1086 p = list_entry(list, snd_kctl_ioctl_t, list);
1087 err = p->fioctl(card, ctl, cmd, arg);
1088 if (err != -ENOIOCTLCMD) {
1089 up_read(&snd_ioctl_rwsem);
1093 up_read(&snd_ioctl_rwsem);
1094 snd_printd("unknown ioctl = 0x%x\n", cmd);
1098 /* FIXME: need to unlock BKL to allow preemption */
1099 static int snd_ctl_ioctl(struct inode *inode, struct file *file,
1100 unsigned int cmd, unsigned long arg)
1104 err = _snd_ctl_ioctl(inode, file, cmd, arg);
1109 static ssize_t snd_ctl_read(struct file *file, char __user *buffer, size_t count, loff_t * offset)
1111 snd_ctl_file_t *ctl;
1115 ctl = file->private_data;
1116 snd_assert(ctl != NULL && ctl->card != NULL, return -ENXIO);
1117 if (!ctl->subscribed)
1119 if (count < sizeof(snd_ctl_event_t))
1121 spin_lock_irq(&ctl->read_lock);
1122 while (count >= sizeof(snd_ctl_event_t)) {
1124 snd_kctl_event_t *kev;
1125 while (list_empty(&ctl->events)) {
1127 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1131 init_waitqueue_entry(&wait, current);
1132 add_wait_queue(&ctl->change_sleep, &wait);
1133 set_current_state(TASK_INTERRUPTIBLE);
1134 spin_unlock_irq(&ctl->read_lock);
1136 remove_wait_queue(&ctl->change_sleep, &wait);
1137 if (signal_pending(current))
1138 return result > 0 ? result : -ERESTARTSYS;
1139 spin_lock_irq(&ctl->read_lock);
1141 kev = snd_kctl_event(ctl->events.next);
1142 ev.type = SNDRV_CTL_EVENT_ELEM;
1143 ev.data.elem.mask = kev->mask;
1144 ev.data.elem.id = kev->id;
1145 list_del(&kev->list);
1146 spin_unlock_irq(&ctl->read_lock);
1148 if (copy_to_user(buffer, &ev, sizeof(snd_ctl_event_t))) {
1152 spin_lock_irq(&ctl->read_lock);
1153 buffer += sizeof(snd_ctl_event_t);
1154 count -= sizeof(snd_ctl_event_t);
1155 result += sizeof(snd_ctl_event_t);
1158 spin_unlock_irq(&ctl->read_lock);
1160 return result > 0 ? result : err;
1163 static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1166 snd_ctl_file_t *ctl;
1168 ctl = file->private_data;
1169 if (!ctl->subscribed)
1171 poll_wait(file, &ctl->change_sleep, wait);
1174 if (!list_empty(&ctl->events))
1175 mask |= POLLIN | POLLRDNORM;
1181 * register the device-specific control-ioctls.
1182 * called from each device manager like pcm.c, hwdep.c, etc.
1184 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1186 snd_kctl_ioctl_t *pn;
1188 pn = kcalloc(1, sizeof(snd_kctl_ioctl_t), GFP_KERNEL);
1192 down_write(&snd_ioctl_rwsem);
1193 list_add_tail(&pn->list, &snd_control_ioctls);
1194 up_write(&snd_ioctl_rwsem);
1199 * de-register the device-specific control-ioctls.
1201 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1203 struct list_head *list;
1204 snd_kctl_ioctl_t *p;
1206 snd_runtime_check(fcn != NULL, return -EINVAL);
1207 down_write(&snd_ioctl_rwsem);
1208 list_for_each(list, &snd_control_ioctls) {
1209 p = list_entry(list, snd_kctl_ioctl_t, list);
1210 if (p->fioctl == fcn) {
1212 up_write(&snd_ioctl_rwsem);
1217 up_write(&snd_ioctl_rwsem);
1222 static int snd_ctl_fasync(int fd, struct file * file, int on)
1224 snd_ctl_file_t *ctl;
1226 ctl = file->private_data;
1227 err = fasync_helper(fd, file, on, &ctl->fasync);
1237 static struct file_operations snd_ctl_f_ops =
1239 .owner = THIS_MODULE,
1240 .read = snd_ctl_read,
1241 .open = snd_ctl_open,
1242 .release = snd_ctl_release,
1243 .poll = snd_ctl_poll,
1244 .ioctl = snd_ctl_ioctl,
1245 .fasync = snd_ctl_fasync,
1248 static snd_minor_t snd_ctl_reg =
1251 .f_ops = &snd_ctl_f_ops,
1255 * registration of the control device:
1256 * called from init.c
1258 int snd_ctl_register(snd_card_t *card)
1263 snd_assert(card != NULL, return -ENXIO);
1264 cardnum = card->number;
1265 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1266 sprintf(name, "controlC%i", cardnum);
1267 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL,
1268 card, 0, &snd_ctl_reg, name)) < 0)
1274 * disconnection of the control device:
1275 * called from init.c
1277 int snd_ctl_disconnect(snd_card_t *card)
1279 struct list_head *flist;
1280 snd_ctl_file_t *ctl;
1282 down_read(&card->controls_rwsem);
1283 list_for_each(flist, &card->ctl_files) {
1284 ctl = snd_ctl_file(flist);
1285 wake_up(&ctl->change_sleep);
1286 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1288 up_read(&card->controls_rwsem);
1293 * de-registration of the control device:
1294 * called from init.c
1296 int snd_ctl_unregister(snd_card_t *card)
1299 snd_kcontrol_t *control;
1301 snd_assert(card != NULL, return -ENXIO);
1302 cardnum = card->number;
1303 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1304 if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL, card, 0)) < 0)
1306 down_write(&card->controls_rwsem);
1307 while (!list_empty(&card->controls)) {
1308 control = snd_kcontrol(card->controls.next);
1309 snd_ctl_remove(card, control);
1311 up_write(&card->controls_rwsem);