[ALSA] Remove xxx_t typedefs: OSS-emulation
[linux-2.6.git] / sound / core / control.c
blob1a14338bd51623ad82d2edce18fd53fba1068384
1 /*
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);
44 #ifdef CONFIG_COMPAT
45 static LIST_HEAD(snd_control_compat_ioctls);
46 #endif
48 static int snd_ctl_open(struct inode *inode, struct file *file)
50 int cardnum = SNDRV_MINOR_CARD(iminor(inode));
51 unsigned long flags;
52 struct snd_card *card;
53 struct snd_ctl_file *ctl;
54 int err;
56 card = snd_cards[cardnum];
57 if (!card) {
58 err = -ENODEV;
59 goto __error1;
61 err = snd_card_file_add(card, file);
62 if (err < 0) {
63 err = -ENODEV;
64 goto __error1;
66 if (!try_module_get(card->module)) {
67 err = -EFAULT;
68 goto __error2;
70 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
71 if (ctl == NULL) {
72 err = -ENOMEM;
73 goto __error;
75 INIT_LIST_HEAD(&ctl->events);
76 init_waitqueue_head(&ctl->change_sleep);
77 spin_lock_init(&ctl->read_lock);
78 ctl->card = card;
79 ctl->pid = current->pid;
80 file->private_data = ctl;
81 write_lock_irqsave(&card->ctl_files_rwlock, flags);
82 list_add_tail(&ctl->list, &card->ctl_files);
83 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
84 return 0;
86 __error:
87 module_put(card->module);
88 __error2:
89 snd_card_file_remove(card, file);
90 __error1:
91 return err;
94 static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
96 struct snd_kctl_event *cread;
98 spin_lock(&ctl->read_lock);
99 while (!list_empty(&ctl->events)) {
100 cread = snd_kctl_event(ctl->events.next);
101 list_del(&cread->list);
102 kfree(cread);
104 spin_unlock(&ctl->read_lock);
107 static int snd_ctl_release(struct inode *inode, struct file *file)
109 unsigned long flags;
110 struct list_head *list;
111 struct snd_card *card;
112 struct snd_ctl_file *ctl;
113 struct snd_kcontrol *control;
114 unsigned int idx;
116 ctl = file->private_data;
117 fasync_helper(-1, file, 0, &ctl->fasync);
118 file->private_data = NULL;
119 card = ctl->card;
120 write_lock_irqsave(&card->ctl_files_rwlock, flags);
121 list_del(&ctl->list);
122 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
123 down_write(&card->controls_rwsem);
124 list_for_each(list, &card->controls) {
125 control = snd_kcontrol(list);
126 for (idx = 0; idx < control->count; idx++)
127 if (control->vd[idx].owner == ctl)
128 control->vd[idx].owner = NULL;
130 up_write(&card->controls_rwsem);
131 snd_ctl_empty_read_queue(ctl);
132 kfree(ctl);
133 module_put(card->module);
134 snd_card_file_remove(card, file);
135 return 0;
138 void snd_ctl_notify(struct snd_card *card, unsigned int mask,
139 struct snd_ctl_elem_id *id)
141 unsigned long flags;
142 struct list_head *flist;
143 struct snd_ctl_file *ctl;
144 struct snd_kctl_event *ev;
146 snd_assert(card != NULL && id != NULL, return);
147 read_lock(&card->ctl_files_rwlock);
148 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
149 card->mixer_oss_change_count++;
150 #endif
151 list_for_each(flist, &card->ctl_files) {
152 struct list_head *elist;
153 ctl = snd_ctl_file(flist);
154 if (!ctl->subscribed)
155 continue;
156 spin_lock_irqsave(&ctl->read_lock, flags);
157 list_for_each(elist, &ctl->events) {
158 ev = snd_kctl_event(elist);
159 if (ev->id.numid == id->numid) {
160 ev->mask |= mask;
161 goto _found;
164 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
165 if (ev) {
166 ev->id = *id;
167 ev->mask = mask;
168 list_add_tail(&ev->list, &ctl->events);
169 } else {
170 snd_printk(KERN_ERR "No memory available to allocate event\n");
172 _found:
173 wake_up(&ctl->change_sleep);
174 spin_unlock_irqrestore(&ctl->read_lock, flags);
175 kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
177 read_unlock(&card->ctl_files_rwlock);
181 * snd_ctl_new - create a control instance from the template
182 * @control: the control template
183 * @access: the default control access
185 * Allocates a new struct snd_kcontrol instance and copies the given template
186 * to the new instance. It does not copy volatile data (access).
188 * Returns the pointer of the new instance, or NULL on failure.
190 struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control, unsigned int access)
192 struct snd_kcontrol *kctl;
193 unsigned int idx;
195 snd_assert(control != NULL, return NULL);
196 snd_assert(control->count > 0, return NULL);
197 kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL);
198 if (kctl == NULL)
199 return NULL;
200 *kctl = *control;
201 for (idx = 0; idx < kctl->count; idx++)
202 kctl->vd[idx].access = access;
203 return kctl;
207 * snd_ctl_new1 - create a control instance from the template
208 * @ncontrol: the initialization record
209 * @private_data: the private data to set
211 * Allocates a new struct snd_kcontrol instance and initialize from the given
212 * template. When the access field of ncontrol is 0, it's assumed as
213 * READWRITE access. When the count field is 0, it's assumes as one.
215 * Returns the pointer of the newly generated instance, or NULL on failure.
217 struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
218 void *private_data)
220 struct snd_kcontrol kctl;
221 unsigned int access;
223 snd_assert(ncontrol != NULL, return NULL);
224 snd_assert(ncontrol->info != NULL, return NULL);
225 memset(&kctl, 0, sizeof(kctl));
226 kctl.id.iface = ncontrol->iface;
227 kctl.id.device = ncontrol->device;
228 kctl.id.subdevice = ncontrol->subdevice;
229 if (ncontrol->name)
230 strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
231 kctl.id.index = ncontrol->index;
232 kctl.count = ncontrol->count ? ncontrol->count : 1;
233 access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
234 (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|SNDRV_CTL_ELEM_ACCESS_INACTIVE|
235 SNDRV_CTL_ELEM_ACCESS_DINDIRECT|SNDRV_CTL_ELEM_ACCESS_INDIRECT));
236 kctl.info = ncontrol->info;
237 kctl.get = ncontrol->get;
238 kctl.put = ncontrol->put;
239 kctl.private_value = ncontrol->private_value;
240 kctl.private_data = private_data;
241 return snd_ctl_new(&kctl, access);
245 * snd_ctl_free_one - release the control instance
246 * @kcontrol: the control instance
248 * Releases the control instance created via snd_ctl_new()
249 * or snd_ctl_new1().
250 * Don't call this after the control was added to the card.
252 void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
254 if (kcontrol) {
255 if (kcontrol->private_free)
256 kcontrol->private_free(kcontrol);
257 kfree(kcontrol);
261 static unsigned int snd_ctl_hole_check(struct snd_card *card,
262 unsigned int count)
264 struct list_head *list;
265 struct snd_kcontrol *kctl;
267 list_for_each(list, &card->controls) {
268 kctl = snd_kcontrol(list);
269 if ((kctl->id.numid <= card->last_numid &&
270 kctl->id.numid + kctl->count > card->last_numid) ||
271 (kctl->id.numid <= card->last_numid + count - 1 &&
272 kctl->id.numid + kctl->count > card->last_numid + count - 1))
273 return card->last_numid = kctl->id.numid + kctl->count - 1;
275 return card->last_numid;
278 static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
280 unsigned int last_numid, iter = 100000;
282 last_numid = card->last_numid;
283 while (last_numid != snd_ctl_hole_check(card, count)) {
284 if (--iter == 0) {
285 /* this situation is very unlikely */
286 snd_printk(KERN_ERR "unable to allocate new control numid\n");
287 return -ENOMEM;
289 last_numid = card->last_numid;
291 return 0;
295 * snd_ctl_add - add the control instance to the card
296 * @card: the card instance
297 * @kcontrol: the control instance to add
299 * Adds the control instance created via snd_ctl_new() or
300 * snd_ctl_new1() to the given card. Assigns also an unique
301 * numid used for fast search.
303 * Returns zero if successful, or a negative error code on failure.
305 * It frees automatically the control which cannot be added.
307 int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
309 struct snd_ctl_elem_id id;
310 unsigned int idx;
312 snd_assert(card != NULL && kcontrol != NULL, return -EINVAL);
313 snd_assert(kcontrol->info != NULL, return -EINVAL);
314 id = kcontrol->id;
315 down_write(&card->controls_rwsem);
316 if (snd_ctl_find_id(card, &id)) {
317 up_write(&card->controls_rwsem);
318 snd_ctl_free_one(kcontrol);
319 snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
320 id.iface,
321 id.device,
322 id.subdevice,
323 id.name,
324 id.index);
325 return -EBUSY;
327 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
328 up_write(&card->controls_rwsem);
329 snd_ctl_free_one(kcontrol);
330 return -ENOMEM;
332 list_add_tail(&kcontrol->list, &card->controls);
333 card->controls_count += kcontrol->count;
334 kcontrol->id.numid = card->last_numid + 1;
335 card->last_numid += kcontrol->count;
336 up_write(&card->controls_rwsem);
337 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
338 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
339 return 0;
343 * snd_ctl_remove - remove the control from the card and release it
344 * @card: the card instance
345 * @kcontrol: the control instance to remove
347 * Removes the control from the card and then releases the instance.
348 * You don't need to call snd_ctl_free_one(). You must be in
349 * the write lock - down_write(&card->controls_rwsem).
351 * Returns 0 if successful, or a negative error code on failure.
353 int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
355 struct snd_ctl_elem_id id;
356 unsigned int idx;
358 snd_assert(card != NULL && kcontrol != NULL, return -EINVAL);
359 list_del(&kcontrol->list);
360 card->controls_count -= kcontrol->count;
361 id = kcontrol->id;
362 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
363 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
364 snd_ctl_free_one(kcontrol);
365 return 0;
369 * snd_ctl_remove_id - remove the control of the given id and release it
370 * @card: the card instance
371 * @id: the control id to remove
373 * Finds the control instance with the given id, removes it from the
374 * card list and releases it.
376 * Returns 0 if successful, or a negative error code on failure.
378 int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
380 struct snd_kcontrol *kctl;
381 int ret;
383 down_write(&card->controls_rwsem);
384 kctl = snd_ctl_find_id(card, id);
385 if (kctl == NULL) {
386 up_write(&card->controls_rwsem);
387 return -ENOENT;
389 ret = snd_ctl_remove(card, kctl);
390 up_write(&card->controls_rwsem);
391 return ret;
395 * snd_ctl_remove_unlocked_id - remove the unlocked control of the given id and release it
396 * @file: active control handle
397 * @id: the control id to remove
399 * Finds the control instance with the given id, removes it from the
400 * card list and releases it.
402 * Returns 0 if successful, or a negative error code on failure.
404 static int snd_ctl_remove_unlocked_id(struct snd_ctl_file * file,
405 struct snd_ctl_elem_id *id)
407 struct snd_card *card = file->card;
408 struct snd_kcontrol *kctl;
409 int idx, ret;
411 down_write(&card->controls_rwsem);
412 kctl = snd_ctl_find_id(card, id);
413 if (kctl == NULL) {
414 up_write(&card->controls_rwsem);
415 return -ENOENT;
417 for (idx = 0; idx < kctl->count; idx++)
418 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
419 up_write(&card->controls_rwsem);
420 return -EBUSY;
422 ret = snd_ctl_remove(card, kctl);
423 up_write(&card->controls_rwsem);
424 return ret;
428 * snd_ctl_rename_id - replace the id of a control on the card
429 * @card: the card instance
430 * @src_id: the old id
431 * @dst_id: the new id
433 * Finds the control with the old id from the card, and replaces the
434 * id with the new one.
436 * Returns zero if successful, or a negative error code on failure.
438 int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
439 struct snd_ctl_elem_id *dst_id)
441 struct snd_kcontrol *kctl;
443 down_write(&card->controls_rwsem);
444 kctl = snd_ctl_find_id(card, src_id);
445 if (kctl == NULL) {
446 up_write(&card->controls_rwsem);
447 return -ENOENT;
449 kctl->id = *dst_id;
450 kctl->id.numid = card->last_numid + 1;
451 card->last_numid += kctl->count;
452 up_write(&card->controls_rwsem);
453 return 0;
457 * snd_ctl_find_numid - find the control instance with the given number-id
458 * @card: the card instance
459 * @numid: the number-id to search
461 * Finds the control instance with the given number-id from the card.
463 * Returns the pointer of the instance if found, or NULL if not.
465 * The caller must down card->controls_rwsem before calling this function
466 * (if the race condition can happen).
468 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
470 struct list_head *list;
471 struct snd_kcontrol *kctl;
473 snd_assert(card != NULL && numid != 0, return NULL);
474 list_for_each(list, &card->controls) {
475 kctl = snd_kcontrol(list);
476 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
477 return kctl;
479 return NULL;
483 * snd_ctl_find_id - find the control instance with the given id
484 * @card: the card instance
485 * @id: the id to search
487 * Finds the control instance with the given id from the card.
489 * Returns the pointer of the instance if found, or NULL if not.
491 * The caller must down card->controls_rwsem before calling this function
492 * (if the race condition can happen).
494 struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
495 struct snd_ctl_elem_id *id)
497 struct list_head *list;
498 struct snd_kcontrol *kctl;
500 snd_assert(card != NULL && id != NULL, return NULL);
501 if (id->numid != 0)
502 return snd_ctl_find_numid(card, id->numid);
503 list_for_each(list, &card->controls) {
504 kctl = snd_kcontrol(list);
505 if (kctl->id.iface != id->iface)
506 continue;
507 if (kctl->id.device != id->device)
508 continue;
509 if (kctl->id.subdevice != id->subdevice)
510 continue;
511 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
512 continue;
513 if (kctl->id.index > id->index)
514 continue;
515 if (kctl->id.index + kctl->count <= id->index)
516 continue;
517 return kctl;
519 return NULL;
522 static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
523 unsigned int cmd, void __user *arg)
525 struct snd_ctl_card_info *info;
527 info = kzalloc(sizeof(*info), GFP_KERNEL);
528 if (! info)
529 return -ENOMEM;
530 down_read(&snd_ioctl_rwsem);
531 info->card = card->number;
532 strlcpy(info->id, card->id, sizeof(info->id));
533 strlcpy(info->driver, card->driver, sizeof(info->driver));
534 strlcpy(info->name, card->shortname, sizeof(info->name));
535 strlcpy(info->longname, card->longname, sizeof(info->longname));
536 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
537 strlcpy(info->components, card->components, sizeof(info->components));
538 up_read(&snd_ioctl_rwsem);
539 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
540 kfree(info);
541 return -EFAULT;
543 kfree(info);
544 return 0;
547 static int snd_ctl_elem_list(struct snd_card *card,
548 struct snd_ctl_elem_list __user *_list)
550 struct list_head *plist;
551 struct snd_ctl_elem_list list;
552 struct snd_kcontrol *kctl;
553 struct snd_ctl_elem_id *dst, *id;
554 unsigned int offset, space, first, jidx;
556 if (copy_from_user(&list, _list, sizeof(list)))
557 return -EFAULT;
558 offset = list.offset;
559 space = list.space;
560 first = 0;
561 /* try limit maximum space */
562 if (space > 16384)
563 return -ENOMEM;
564 if (space > 0) {
565 /* allocate temporary buffer for atomic operation */
566 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
567 if (dst == NULL)
568 return -ENOMEM;
569 down_read(&card->controls_rwsem);
570 list.count = card->controls_count;
571 plist = card->controls.next;
572 while (plist != &card->controls) {
573 if (offset == 0)
574 break;
575 kctl = snd_kcontrol(plist);
576 if (offset < kctl->count)
577 break;
578 offset -= kctl->count;
579 plist = plist->next;
581 list.used = 0;
582 id = dst;
583 while (space > 0 && plist != &card->controls) {
584 kctl = snd_kcontrol(plist);
585 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
586 snd_ctl_build_ioff(id, kctl, jidx);
587 id++;
588 space--;
589 list.used++;
591 plist = plist->next;
592 offset = 0;
594 up_read(&card->controls_rwsem);
595 if (list.used > 0 &&
596 copy_to_user(list.pids, dst,
597 list.used * sizeof(struct snd_ctl_elem_id))) {
598 vfree(dst);
599 return -EFAULT;
601 vfree(dst);
602 } else {
603 down_read(&card->controls_rwsem);
604 list.count = card->controls_count;
605 up_read(&card->controls_rwsem);
607 if (copy_to_user(_list, &list, sizeof(list)))
608 return -EFAULT;
609 return 0;
612 static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
613 struct snd_ctl_elem_info *info)
615 struct snd_card *card = ctl->card;
616 struct snd_kcontrol *kctl;
617 struct snd_kcontrol_volatile *vd;
618 unsigned int index_offset;
619 int result;
621 down_read(&card->controls_rwsem);
622 kctl = snd_ctl_find_id(card, &info->id);
623 if (kctl == NULL) {
624 up_read(&card->controls_rwsem);
625 return -ENOENT;
627 #ifdef CONFIG_SND_DEBUG
628 info->access = 0;
629 #endif
630 result = kctl->info(kctl, info);
631 if (result >= 0) {
632 snd_assert(info->access == 0, );
633 index_offset = snd_ctl_get_ioff(kctl, &info->id);
634 vd = &kctl->vd[index_offset];
635 snd_ctl_build_ioff(&info->id, kctl, index_offset);
636 info->access = vd->access;
637 if (vd->owner) {
638 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
639 if (vd->owner == ctl)
640 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
641 info->owner = vd->owner_pid;
642 } else {
643 info->owner = -1;
646 up_read(&card->controls_rwsem);
647 return result;
650 static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
651 struct snd_ctl_elem_info __user *_info)
653 struct snd_ctl_elem_info info;
654 int result;
656 if (copy_from_user(&info, _info, sizeof(info)))
657 return -EFAULT;
658 result = snd_ctl_elem_info(ctl, &info);
659 if (result >= 0)
660 if (copy_to_user(_info, &info, sizeof(info)))
661 return -EFAULT;
662 return result;
665 int snd_ctl_elem_read(struct snd_card *card, struct snd_ctl_elem_value *control)
667 struct snd_kcontrol *kctl;
668 struct snd_kcontrol_volatile *vd;
669 unsigned int index_offset;
670 int result, indirect;
672 down_read(&card->controls_rwsem);
673 kctl = snd_ctl_find_id(card, &control->id);
674 if (kctl == NULL) {
675 result = -ENOENT;
676 } else {
677 index_offset = snd_ctl_get_ioff(kctl, &control->id);
678 vd = &kctl->vd[index_offset];
679 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
680 if (control->indirect != indirect) {
681 result = -EACCES;
682 } else {
683 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) && kctl->get != NULL) {
684 snd_ctl_build_ioff(&control->id, kctl, index_offset);
685 result = kctl->get(kctl, control);
686 } else {
687 result = -EPERM;
691 up_read(&card->controls_rwsem);
692 return result;
695 static int snd_ctl_elem_read_user(struct snd_card *card,
696 struct snd_ctl_elem_value __user *_control)
698 struct snd_ctl_elem_value *control;
699 int result;
701 control = kmalloc(sizeof(*control), GFP_KERNEL);
702 if (control == NULL)
703 return -ENOMEM;
704 if (copy_from_user(control, _control, sizeof(*control))) {
705 kfree(control);
706 return -EFAULT;
708 result = snd_ctl_elem_read(card, control);
709 if (result >= 0)
710 if (copy_to_user(_control, control, sizeof(*control)))
711 result = -EFAULT;
712 kfree(control);
713 return result;
716 int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
717 struct snd_ctl_elem_value *control)
719 struct snd_kcontrol *kctl;
720 struct snd_kcontrol_volatile *vd;
721 unsigned int index_offset;
722 int result, indirect;
724 down_read(&card->controls_rwsem);
725 kctl = snd_ctl_find_id(card, &control->id);
726 if (kctl == NULL) {
727 result = -ENOENT;
728 } else {
729 index_offset = snd_ctl_get_ioff(kctl, &control->id);
730 vd = &kctl->vd[index_offset];
731 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
732 if (control->indirect != indirect) {
733 result = -EACCES;
734 } else {
735 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
736 kctl->put == NULL ||
737 (file && vd->owner != NULL && vd->owner != file)) {
738 result = -EPERM;
739 } else {
740 snd_ctl_build_ioff(&control->id, kctl, index_offset);
741 result = kctl->put(kctl, control);
743 if (result > 0) {
744 up_read(&card->controls_rwsem);
745 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &control->id);
746 return 0;
750 up_read(&card->controls_rwsem);
751 return result;
754 static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
755 struct snd_ctl_elem_value __user *_control)
757 struct snd_ctl_elem_value *control;
758 int result;
760 control = kmalloc(sizeof(*control), GFP_KERNEL);
761 if (control == NULL)
762 return -ENOMEM;
763 if (copy_from_user(control, _control, sizeof(*control))) {
764 kfree(control);
765 return -EFAULT;
767 result = snd_ctl_elem_write(file->card, file, control);
768 if (result >= 0)
769 if (copy_to_user(_control, control, sizeof(*control)))
770 result = -EFAULT;
771 kfree(control);
772 return result;
775 static int snd_ctl_elem_lock(struct snd_ctl_file *file,
776 struct snd_ctl_elem_id __user *_id)
778 struct snd_card *card = file->card;
779 struct snd_ctl_elem_id id;
780 struct snd_kcontrol *kctl;
781 struct snd_kcontrol_volatile *vd;
782 int result;
784 if (copy_from_user(&id, _id, sizeof(id)))
785 return -EFAULT;
786 down_write(&card->controls_rwsem);
787 kctl = snd_ctl_find_id(card, &id);
788 if (kctl == NULL) {
789 result = -ENOENT;
790 } else {
791 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
792 if (vd->owner != NULL)
793 result = -EBUSY;
794 else {
795 vd->owner = file;
796 vd->owner_pid = current->pid;
797 result = 0;
800 up_write(&card->controls_rwsem);
801 return result;
804 static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
805 struct snd_ctl_elem_id __user *_id)
807 struct snd_card *card = file->card;
808 struct snd_ctl_elem_id id;
809 struct snd_kcontrol *kctl;
810 struct snd_kcontrol_volatile *vd;
811 int result;
813 if (copy_from_user(&id, _id, sizeof(id)))
814 return -EFAULT;
815 down_write(&card->controls_rwsem);
816 kctl = snd_ctl_find_id(card, &id);
817 if (kctl == NULL) {
818 result = -ENOENT;
819 } else {
820 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
821 if (vd->owner == NULL)
822 result = -EINVAL;
823 else if (vd->owner != file)
824 result = -EPERM;
825 else {
826 vd->owner = NULL;
827 vd->owner_pid = 0;
828 result = 0;
831 up_write(&card->controls_rwsem);
832 return result;
835 struct user_element {
836 struct snd_ctl_elem_info info;
837 void *elem_data; /* element data */
838 unsigned long elem_data_size; /* size of element data in bytes */
839 void *priv_data; /* private data (like strings for enumerated type) */
840 unsigned long priv_data_size; /* size of private data in bytes */
843 static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
844 struct snd_ctl_elem_info *uinfo)
846 struct user_element *ue = kcontrol->private_data;
848 *uinfo = ue->info;
849 return 0;
852 static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
853 struct snd_ctl_elem_value *ucontrol)
855 struct user_element *ue = kcontrol->private_data;
857 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
858 return 0;
861 static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
862 struct snd_ctl_elem_value *ucontrol)
864 int change;
865 struct user_element *ue = kcontrol->private_data;
867 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
868 if (change)
869 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
870 return change;
873 static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
875 kfree(kcontrol->private_data);
878 static int snd_ctl_elem_add(struct snd_ctl_file *file,
879 struct snd_ctl_elem_info *info, int replace)
881 struct snd_card *card = file->card;
882 struct snd_kcontrol kctl, *_kctl;
883 unsigned int access;
884 long private_size;
885 struct user_element *ue;
886 int idx, err;
888 if (card->user_ctl_count >= MAX_USER_CONTROLS)
889 return -ENOMEM;
890 if (info->count > 1024)
891 return -EINVAL;
892 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
893 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
894 SNDRV_CTL_ELEM_ACCESS_INACTIVE));
895 info->id.numid = 0;
896 memset(&kctl, 0, sizeof(kctl));
897 down_write(&card->controls_rwsem);
898 _kctl = snd_ctl_find_id(card, &info->id);
899 err = 0;
900 if (_kctl) {
901 if (replace)
902 err = snd_ctl_remove(card, _kctl);
903 else
904 err = -EBUSY;
905 } else {
906 if (replace)
907 err = -ENOENT;
909 up_write(&card->controls_rwsem);
910 if (err < 0)
911 return err;
912 memcpy(&kctl.id, &info->id, sizeof(info->id));
913 kctl.count = info->owner ? info->owner : 1;
914 access |= SNDRV_CTL_ELEM_ACCESS_USER;
915 kctl.info = snd_ctl_elem_user_info;
916 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
917 kctl.get = snd_ctl_elem_user_get;
918 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
919 kctl.put = snd_ctl_elem_user_put;
920 switch (info->type) {
921 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
922 private_size = sizeof(char);
923 if (info->count > 128)
924 return -EINVAL;
925 break;
926 case SNDRV_CTL_ELEM_TYPE_INTEGER:
927 private_size = sizeof(long);
928 if (info->count > 128)
929 return -EINVAL;
930 break;
931 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
932 private_size = sizeof(long long);
933 if (info->count > 64)
934 return -EINVAL;
935 break;
936 case SNDRV_CTL_ELEM_TYPE_BYTES:
937 private_size = sizeof(unsigned char);
938 if (info->count > 512)
939 return -EINVAL;
940 break;
941 case SNDRV_CTL_ELEM_TYPE_IEC958:
942 private_size = sizeof(struct snd_aes_iec958);
943 if (info->count != 1)
944 return -EINVAL;
945 break;
946 default:
947 return -EINVAL;
949 private_size *= info->count;
950 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
951 if (ue == NULL)
952 return -ENOMEM;
953 ue->info = *info;
954 ue->elem_data = (char *)ue + sizeof(*ue);
955 ue->elem_data_size = private_size;
956 kctl.private_free = snd_ctl_elem_user_free;
957 _kctl = snd_ctl_new(&kctl, access);
958 if (_kctl == NULL) {
959 kfree(_kctl->private_data);
960 return -ENOMEM;
962 _kctl->private_data = ue;
963 for (idx = 0; idx < _kctl->count; idx++)
964 _kctl->vd[idx].owner = file;
965 err = snd_ctl_add(card, _kctl);
966 if (err < 0) {
967 snd_ctl_free_one(_kctl);
968 return err;
971 down_write(&card->controls_rwsem);
972 card->user_ctl_count++;
973 up_write(&card->controls_rwsem);
975 return 0;
978 static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
979 struct snd_ctl_elem_info __user *_info, int replace)
981 struct snd_ctl_elem_info info;
982 if (copy_from_user(&info, _info, sizeof(info)))
983 return -EFAULT;
984 return snd_ctl_elem_add(file, &info, replace);
987 static int snd_ctl_elem_remove(struct snd_ctl_file *file,
988 struct snd_ctl_elem_id __user *_id)
990 struct snd_ctl_elem_id id;
991 int err;
993 if (copy_from_user(&id, _id, sizeof(id)))
994 return -EFAULT;
995 err = snd_ctl_remove_unlocked_id(file, &id);
996 if (! err) {
997 struct snd_card *card = file->card;
998 down_write(&card->controls_rwsem);
999 card->user_ctl_count--;
1000 up_write(&card->controls_rwsem);
1002 return err;
1005 static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
1007 int subscribe;
1008 if (get_user(subscribe, ptr))
1009 return -EFAULT;
1010 if (subscribe < 0) {
1011 subscribe = file->subscribed;
1012 if (put_user(subscribe, ptr))
1013 return -EFAULT;
1014 return 0;
1016 if (subscribe) {
1017 file->subscribed = 1;
1018 return 0;
1019 } else if (file->subscribed) {
1020 snd_ctl_empty_read_queue(file);
1021 file->subscribed = 0;
1023 return 0;
1026 #ifdef CONFIG_PM
1028 * change the power state
1030 static int snd_ctl_set_power_state(struct snd_card *card, unsigned int power_state)
1032 switch (power_state) {
1033 case SNDRV_CTL_POWER_D0:
1034 if (card->power_state != power_state) {
1035 card->pm_resume(card);
1036 snd_power_change_state(card, power_state);
1038 break;
1039 case SNDRV_CTL_POWER_D3hot:
1040 if (card->power_state != power_state) {
1041 card->pm_suspend(card, PMSG_SUSPEND);
1042 snd_power_change_state(card, power_state);
1044 break;
1045 case SNDRV_CTL_POWER_D1:
1046 case SNDRV_CTL_POWER_D2:
1047 case SNDRV_CTL_POWER_D3cold:
1048 /* not supported yet */
1049 default:
1050 return -EINVAL;
1052 return 0;
1054 #endif
1056 static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1058 struct snd_ctl_file *ctl;
1059 struct snd_card *card;
1060 struct list_head *list;
1061 struct snd_kctl_ioctl *p;
1062 void __user *argp = (void __user *)arg;
1063 int __user *ip = argp;
1064 int err;
1066 ctl = file->private_data;
1067 card = ctl->card;
1068 snd_assert(card != NULL, return -ENXIO);
1069 switch (cmd) {
1070 case SNDRV_CTL_IOCTL_PVERSION:
1071 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1072 case SNDRV_CTL_IOCTL_CARD_INFO:
1073 return snd_ctl_card_info(card, ctl, cmd, argp);
1074 case SNDRV_CTL_IOCTL_ELEM_LIST:
1075 return snd_ctl_elem_list(ctl->card, argp);
1076 case SNDRV_CTL_IOCTL_ELEM_INFO:
1077 return snd_ctl_elem_info_user(ctl, argp);
1078 case SNDRV_CTL_IOCTL_ELEM_READ:
1079 return snd_ctl_elem_read_user(ctl->card, argp);
1080 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1081 return snd_ctl_elem_write_user(ctl, argp);
1082 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1083 return snd_ctl_elem_lock(ctl, argp);
1084 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1085 return snd_ctl_elem_unlock(ctl, argp);
1086 case SNDRV_CTL_IOCTL_ELEM_ADD:
1087 return snd_ctl_elem_add_user(ctl, argp, 0);
1088 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1089 return snd_ctl_elem_add_user(ctl, argp, 1);
1090 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1091 return snd_ctl_elem_remove(ctl, argp);
1092 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1093 return snd_ctl_subscribe_events(ctl, ip);
1094 case SNDRV_CTL_IOCTL_POWER:
1095 if (get_user(err, ip))
1096 return -EFAULT;
1097 if (!capable(CAP_SYS_ADMIN))
1098 return -EPERM;
1099 #ifdef CONFIG_PM
1100 if (card->pm_suspend && card->pm_resume) {
1101 snd_power_lock(card);
1102 err = snd_ctl_set_power_state(card, err);
1103 snd_power_unlock(card);
1104 } else
1105 #endif
1106 err = -ENOPROTOOPT;
1107 return err;
1108 case SNDRV_CTL_IOCTL_POWER_STATE:
1109 #ifdef CONFIG_PM
1110 return put_user(card->power_state, ip) ? -EFAULT : 0;
1111 #else
1112 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1113 #endif
1115 down_read(&snd_ioctl_rwsem);
1116 list_for_each(list, &snd_control_ioctls) {
1117 p = list_entry(list, struct snd_kctl_ioctl, list);
1118 err = p->fioctl(card, ctl, cmd, arg);
1119 if (err != -ENOIOCTLCMD) {
1120 up_read(&snd_ioctl_rwsem);
1121 return err;
1124 up_read(&snd_ioctl_rwsem);
1125 snd_printdd("unknown ioctl = 0x%x\n", cmd);
1126 return -ENOTTY;
1129 static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1130 size_t count, loff_t * offset)
1132 struct snd_ctl_file *ctl;
1133 int err = 0;
1134 ssize_t result = 0;
1136 ctl = file->private_data;
1137 snd_assert(ctl != NULL && ctl->card != NULL, return -ENXIO);
1138 if (!ctl->subscribed)
1139 return -EBADFD;
1140 if (count < sizeof(struct snd_ctl_event))
1141 return -EINVAL;
1142 spin_lock_irq(&ctl->read_lock);
1143 while (count >= sizeof(struct snd_ctl_event)) {
1144 struct snd_ctl_event ev;
1145 struct snd_kctl_event *kev;
1146 while (list_empty(&ctl->events)) {
1147 wait_queue_t wait;
1148 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1149 err = -EAGAIN;
1150 goto __end_lock;
1152 init_waitqueue_entry(&wait, current);
1153 add_wait_queue(&ctl->change_sleep, &wait);
1154 set_current_state(TASK_INTERRUPTIBLE);
1155 spin_unlock_irq(&ctl->read_lock);
1156 schedule();
1157 remove_wait_queue(&ctl->change_sleep, &wait);
1158 if (signal_pending(current))
1159 return result > 0 ? result : -ERESTARTSYS;
1160 spin_lock_irq(&ctl->read_lock);
1162 kev = snd_kctl_event(ctl->events.next);
1163 ev.type = SNDRV_CTL_EVENT_ELEM;
1164 ev.data.elem.mask = kev->mask;
1165 ev.data.elem.id = kev->id;
1166 list_del(&kev->list);
1167 spin_unlock_irq(&ctl->read_lock);
1168 kfree(kev);
1169 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
1170 err = -EFAULT;
1171 goto __end;
1173 spin_lock_irq(&ctl->read_lock);
1174 buffer += sizeof(struct snd_ctl_event);
1175 count -= sizeof(struct snd_ctl_event);
1176 result += sizeof(struct snd_ctl_event);
1178 __end_lock:
1179 spin_unlock_irq(&ctl->read_lock);
1180 __end:
1181 return result > 0 ? result : err;
1184 static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1186 unsigned int mask;
1187 struct snd_ctl_file *ctl;
1189 ctl = file->private_data;
1190 if (!ctl->subscribed)
1191 return 0;
1192 poll_wait(file, &ctl->change_sleep, wait);
1194 mask = 0;
1195 if (!list_empty(&ctl->events))
1196 mask |= POLLIN | POLLRDNORM;
1198 return mask;
1202 * register the device-specific control-ioctls.
1203 * called from each device manager like pcm.c, hwdep.c, etc.
1205 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1207 struct snd_kctl_ioctl *pn;
1209 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
1210 if (pn == NULL)
1211 return -ENOMEM;
1212 pn->fioctl = fcn;
1213 down_write(&snd_ioctl_rwsem);
1214 list_add_tail(&pn->list, lists);
1215 up_write(&snd_ioctl_rwsem);
1216 return 0;
1219 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1221 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1224 #ifdef CONFIG_COMPAT
1225 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1227 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1229 #endif
1232 * de-register the device-specific control-ioctls.
1234 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1235 struct list_head *lists)
1237 struct list_head *list;
1238 struct snd_kctl_ioctl *p;
1240 snd_assert(fcn != NULL, return -EINVAL);
1241 down_write(&snd_ioctl_rwsem);
1242 list_for_each(list, lists) {
1243 p = list_entry(list, struct snd_kctl_ioctl, list);
1244 if (p->fioctl == fcn) {
1245 list_del(&p->list);
1246 up_write(&snd_ioctl_rwsem);
1247 kfree(p);
1248 return 0;
1251 up_write(&snd_ioctl_rwsem);
1252 snd_BUG();
1253 return -EINVAL;
1256 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1258 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1261 #ifdef CONFIG_COMPAT
1262 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1264 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1267 #endif
1269 static int snd_ctl_fasync(int fd, struct file * file, int on)
1271 struct snd_ctl_file *ctl;
1272 int err;
1273 ctl = file->private_data;
1274 err = fasync_helper(fd, file, on, &ctl->fasync);
1275 if (err < 0)
1276 return err;
1277 return 0;
1281 * ioctl32 compat
1283 #ifdef CONFIG_COMPAT
1284 #include "control_compat.c"
1285 #else
1286 #define snd_ctl_ioctl_compat NULL
1287 #endif
1290 * INIT PART
1293 static struct file_operations snd_ctl_f_ops =
1295 .owner = THIS_MODULE,
1296 .read = snd_ctl_read,
1297 .open = snd_ctl_open,
1298 .release = snd_ctl_release,
1299 .poll = snd_ctl_poll,
1300 .unlocked_ioctl = snd_ctl_ioctl,
1301 .compat_ioctl = snd_ctl_ioctl_compat,
1302 .fasync = snd_ctl_fasync,
1305 static struct snd_minor snd_ctl_reg =
1307 .comment = "ctl",
1308 .f_ops = &snd_ctl_f_ops,
1312 * registration of the control device
1314 static int snd_ctl_dev_register(struct snd_device *device)
1316 struct snd_card *card = device->device_data;
1317 int err, cardnum;
1318 char name[16];
1320 snd_assert(card != NULL, return -ENXIO);
1321 cardnum = card->number;
1322 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1323 sprintf(name, "controlC%i", cardnum);
1324 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL,
1325 card, 0, &snd_ctl_reg, name)) < 0)
1326 return err;
1327 return 0;
1331 * disconnection of the control device
1333 static int snd_ctl_dev_disconnect(struct snd_device *device)
1335 struct snd_card *card = device->device_data;
1336 struct list_head *flist;
1337 struct snd_ctl_file *ctl;
1339 down_read(&card->controls_rwsem);
1340 list_for_each(flist, &card->ctl_files) {
1341 ctl = snd_ctl_file(flist);
1342 wake_up(&ctl->change_sleep);
1343 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1345 up_read(&card->controls_rwsem);
1346 return 0;
1350 * free all controls
1352 static int snd_ctl_dev_free(struct snd_device *device)
1354 struct snd_card *card = device->device_data;
1355 struct snd_kcontrol *control;
1357 down_write(&card->controls_rwsem);
1358 while (!list_empty(&card->controls)) {
1359 control = snd_kcontrol(card->controls.next);
1360 snd_ctl_remove(card, control);
1362 up_write(&card->controls_rwsem);
1363 return 0;
1367 * de-registration of the control device
1369 static int snd_ctl_dev_unregister(struct snd_device *device)
1371 struct snd_card *card = device->device_data;
1372 int err, cardnum;
1374 snd_assert(card != NULL, return -ENXIO);
1375 cardnum = card->number;
1376 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1377 if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL, card, 0)) < 0)
1378 return err;
1379 return snd_ctl_dev_free(device);
1383 * create control core:
1384 * called from init.c
1386 int snd_ctl_create(struct snd_card *card)
1388 static struct snd_device_ops ops = {
1389 .dev_free = snd_ctl_dev_free,
1390 .dev_register = snd_ctl_dev_register,
1391 .dev_disconnect = snd_ctl_dev_disconnect,
1392 .dev_unregister = snd_ctl_dev_unregister
1395 snd_assert(card != NULL, return -ENXIO);
1396 return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);