[S390] cio: Use path verification to check for path state.
[linux-2.6/libata-dev.git] / sound / core / control.c
blob86de7258b76d04a0695251e94ecfcbc2e63c10a4
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 unsigned long flags;
51 struct snd_card *card;
52 struct snd_ctl_file *ctl;
53 int err;
55 card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
56 if (!card) {
57 err = -ENODEV;
58 goto __error1;
60 err = snd_card_file_add(card, file);
61 if (err < 0) {
62 err = -ENODEV;
63 goto __error1;
65 if (!try_module_get(card->module)) {
66 err = -EFAULT;
67 goto __error2;
69 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
70 if (ctl == NULL) {
71 err = -ENOMEM;
72 goto __error;
74 INIT_LIST_HEAD(&ctl->events);
75 init_waitqueue_head(&ctl->change_sleep);
76 spin_lock_init(&ctl->read_lock);
77 ctl->card = card;
78 ctl->prefer_pcm_subdevice = -1;
79 ctl->prefer_rawmidi_subdevice = -1;
80 ctl->pid = current->pid;
81 file->private_data = ctl;
82 write_lock_irqsave(&card->ctl_files_rwlock, flags);
83 list_add_tail(&ctl->list, &card->ctl_files);
84 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
85 return 0;
87 __error:
88 module_put(card->module);
89 __error2:
90 snd_card_file_remove(card, file);
91 __error1:
92 return err;
95 static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
97 struct snd_kctl_event *cread;
99 spin_lock(&ctl->read_lock);
100 while (!list_empty(&ctl->events)) {
101 cread = snd_kctl_event(ctl->events.next);
102 list_del(&cread->list);
103 kfree(cread);
105 spin_unlock(&ctl->read_lock);
108 static int snd_ctl_release(struct inode *inode, struct file *file)
110 unsigned long flags;
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_entry(control, &card->controls, list)
125 for (idx = 0; idx < control->count; idx++)
126 if (control->vd[idx].owner == ctl)
127 control->vd[idx].owner = NULL;
128 up_write(&card->controls_rwsem);
129 snd_ctl_empty_read_queue(ctl);
130 kfree(ctl);
131 module_put(card->module);
132 snd_card_file_remove(card, file);
133 return 0;
136 void snd_ctl_notify(struct snd_card *card, unsigned int mask,
137 struct snd_ctl_elem_id *id)
139 unsigned long flags;
140 struct snd_ctl_file *ctl;
141 struct snd_kctl_event *ev;
143 snd_assert(card != NULL && id != NULL, return);
144 read_lock(&card->ctl_files_rwlock);
145 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
146 card->mixer_oss_change_count++;
147 #endif
148 list_for_each_entry(ctl, &card->ctl_files, list) {
149 if (!ctl->subscribed)
150 continue;
151 spin_lock_irqsave(&ctl->read_lock, flags);
152 list_for_each_entry(ev, &ctl->events, list) {
153 if (ev->id.numid == id->numid) {
154 ev->mask |= mask;
155 goto _found;
158 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
159 if (ev) {
160 ev->id = *id;
161 ev->mask = mask;
162 list_add_tail(&ev->list, &ctl->events);
163 } else {
164 snd_printk(KERN_ERR "No memory available to allocate event\n");
166 _found:
167 wake_up(&ctl->change_sleep);
168 spin_unlock_irqrestore(&ctl->read_lock, flags);
169 kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
171 read_unlock(&card->ctl_files_rwlock);
174 EXPORT_SYMBOL(snd_ctl_notify);
177 * snd_ctl_new - create a control instance from the template
178 * @control: the control template
179 * @access: the default control access
181 * Allocates a new struct snd_kcontrol instance and copies the given template
182 * to the new instance. It does not copy volatile data (access).
184 * Returns the pointer of the new instance, or NULL on failure.
186 static struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control,
187 unsigned int access)
189 struct snd_kcontrol *kctl;
190 unsigned int idx;
192 snd_assert(control != NULL, return NULL);
193 snd_assert(control->count > 0, return NULL);
194 kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL);
195 if (kctl == NULL) {
196 snd_printk(KERN_ERR "Cannot allocate control instance\n");
197 return NULL;
199 *kctl = *control;
200 for (idx = 0; idx < kctl->count; idx++)
201 kctl->vd[idx].access = access;
202 return kctl;
206 * snd_ctl_new1 - create a control instance from the template
207 * @ncontrol: the initialization record
208 * @private_data: the private data to set
210 * Allocates a new struct snd_kcontrol instance and initialize from the given
211 * template. When the access field of ncontrol is 0, it's assumed as
212 * READWRITE access. When the count field is 0, it's assumes as one.
214 * Returns the pointer of the newly generated instance, or NULL on failure.
216 struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
217 void *private_data)
219 struct snd_kcontrol kctl;
220 unsigned int access;
222 snd_assert(ncontrol != NULL, return NULL);
223 snd_assert(ncontrol->info != NULL, return NULL);
224 memset(&kctl, 0, sizeof(kctl));
225 kctl.id.iface = ncontrol->iface;
226 kctl.id.device = ncontrol->device;
227 kctl.id.subdevice = ncontrol->subdevice;
228 if (ncontrol->name)
229 strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
230 kctl.id.index = ncontrol->index;
231 kctl.count = ncontrol->count ? ncontrol->count : 1;
232 access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
233 (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
234 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
235 SNDRV_CTL_ELEM_ACCESS_DINDIRECT|
236 SNDRV_CTL_ELEM_ACCESS_INDIRECT|
237 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE|
238 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK));
239 kctl.info = ncontrol->info;
240 kctl.get = ncontrol->get;
241 kctl.put = ncontrol->put;
242 kctl.tlv.p = ncontrol->tlv.p;
243 kctl.private_value = ncontrol->private_value;
244 kctl.private_data = private_data;
245 return snd_ctl_new(&kctl, access);
248 EXPORT_SYMBOL(snd_ctl_new1);
251 * snd_ctl_free_one - release the control instance
252 * @kcontrol: the control instance
254 * Releases the control instance created via snd_ctl_new()
255 * or snd_ctl_new1().
256 * Don't call this after the control was added to the card.
258 void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
260 if (kcontrol) {
261 if (kcontrol->private_free)
262 kcontrol->private_free(kcontrol);
263 kfree(kcontrol);
267 EXPORT_SYMBOL(snd_ctl_free_one);
269 static unsigned int snd_ctl_hole_check(struct snd_card *card,
270 unsigned int count)
272 struct snd_kcontrol *kctl;
274 list_for_each_entry(kctl, &card->controls, list) {
275 if ((kctl->id.numid <= card->last_numid &&
276 kctl->id.numid + kctl->count > card->last_numid) ||
277 (kctl->id.numid <= card->last_numid + count - 1 &&
278 kctl->id.numid + kctl->count > card->last_numid + count - 1))
279 return card->last_numid = kctl->id.numid + kctl->count - 1;
281 return card->last_numid;
284 static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
286 unsigned int last_numid, iter = 100000;
288 last_numid = card->last_numid;
289 while (last_numid != snd_ctl_hole_check(card, count)) {
290 if (--iter == 0) {
291 /* this situation is very unlikely */
292 snd_printk(KERN_ERR "unable to allocate new control numid\n");
293 return -ENOMEM;
295 last_numid = card->last_numid;
297 return 0;
301 * snd_ctl_add - add the control instance to the card
302 * @card: the card instance
303 * @kcontrol: the control instance to add
305 * Adds the control instance created via snd_ctl_new() or
306 * snd_ctl_new1() to the given card. Assigns also an unique
307 * numid used for fast search.
309 * Returns zero if successful, or a negative error code on failure.
311 * It frees automatically the control which cannot be added.
313 int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
315 struct snd_ctl_elem_id id;
316 unsigned int idx;
317 int err = -EINVAL;
319 if (! kcontrol)
320 return err;
321 snd_assert(card != NULL, goto error);
322 snd_assert(kcontrol->info != NULL, goto error);
323 id = kcontrol->id;
324 down_write(&card->controls_rwsem);
325 if (snd_ctl_find_id(card, &id)) {
326 up_write(&card->controls_rwsem);
327 snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
328 id.iface,
329 id.device,
330 id.subdevice,
331 id.name,
332 id.index);
333 err = -EBUSY;
334 goto error;
336 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
337 up_write(&card->controls_rwsem);
338 err = -ENOMEM;
339 goto error;
341 list_add_tail(&kcontrol->list, &card->controls);
342 card->controls_count += kcontrol->count;
343 kcontrol->id.numid = card->last_numid + 1;
344 card->last_numid += kcontrol->count;
345 up_write(&card->controls_rwsem);
346 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
347 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
348 return 0;
350 error:
351 snd_ctl_free_one(kcontrol);
352 return err;
355 EXPORT_SYMBOL(snd_ctl_add);
358 * snd_ctl_remove - remove the control from the card and release it
359 * @card: the card instance
360 * @kcontrol: the control instance to remove
362 * Removes the control from the card and then releases the instance.
363 * You don't need to call snd_ctl_free_one(). You must be in
364 * the write lock - down_write(&card->controls_rwsem).
366 * Returns 0 if successful, or a negative error code on failure.
368 int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
370 struct snd_ctl_elem_id id;
371 unsigned int idx;
373 snd_assert(card != NULL && kcontrol != NULL, return -EINVAL);
374 list_del(&kcontrol->list);
375 card->controls_count -= kcontrol->count;
376 id = kcontrol->id;
377 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
378 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
379 snd_ctl_free_one(kcontrol);
380 return 0;
383 EXPORT_SYMBOL(snd_ctl_remove);
386 * snd_ctl_remove_id - remove the control of the given id and release it
387 * @card: the card instance
388 * @id: the control id to remove
390 * Finds the control instance with the given id, removes it from the
391 * card list and releases it.
393 * Returns 0 if successful, or a negative error code on failure.
395 int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
397 struct snd_kcontrol *kctl;
398 int ret;
400 down_write(&card->controls_rwsem);
401 kctl = snd_ctl_find_id(card, id);
402 if (kctl == NULL) {
403 up_write(&card->controls_rwsem);
404 return -ENOENT;
406 ret = snd_ctl_remove(card, kctl);
407 up_write(&card->controls_rwsem);
408 return ret;
411 EXPORT_SYMBOL(snd_ctl_remove_id);
414 * snd_ctl_remove_unlocked_id - remove the unlocked control of the given id and release it
415 * @file: active control handle
416 * @id: the control id to remove
418 * Finds the control instance with the given id, removes it from the
419 * card list and releases it.
421 * Returns 0 if successful, or a negative error code on failure.
423 static int snd_ctl_remove_unlocked_id(struct snd_ctl_file * file,
424 struct snd_ctl_elem_id *id)
426 struct snd_card *card = file->card;
427 struct snd_kcontrol *kctl;
428 int idx, ret;
430 down_write(&card->controls_rwsem);
431 kctl = snd_ctl_find_id(card, id);
432 if (kctl == NULL) {
433 up_write(&card->controls_rwsem);
434 return -ENOENT;
436 for (idx = 0; idx < kctl->count; idx++)
437 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
438 up_write(&card->controls_rwsem);
439 return -EBUSY;
441 ret = snd_ctl_remove(card, kctl);
442 up_write(&card->controls_rwsem);
443 return ret;
447 * snd_ctl_rename_id - replace the id of a control on the card
448 * @card: the card instance
449 * @src_id: the old id
450 * @dst_id: the new id
452 * Finds the control with the old id from the card, and replaces the
453 * id with the new one.
455 * Returns zero if successful, or a negative error code on failure.
457 int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
458 struct snd_ctl_elem_id *dst_id)
460 struct snd_kcontrol *kctl;
462 down_write(&card->controls_rwsem);
463 kctl = snd_ctl_find_id(card, src_id);
464 if (kctl == NULL) {
465 up_write(&card->controls_rwsem);
466 return -ENOENT;
468 kctl->id = *dst_id;
469 kctl->id.numid = card->last_numid + 1;
470 card->last_numid += kctl->count;
471 up_write(&card->controls_rwsem);
472 return 0;
475 EXPORT_SYMBOL(snd_ctl_rename_id);
478 * snd_ctl_find_numid - find the control instance with the given number-id
479 * @card: the card instance
480 * @numid: the number-id to search
482 * Finds the control instance with the given number-id from the card.
484 * Returns the pointer of the instance if found, or NULL if not.
486 * The caller must down card->controls_rwsem before calling this function
487 * (if the race condition can happen).
489 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
491 struct snd_kcontrol *kctl;
493 snd_assert(card != NULL && numid != 0, return NULL);
494 list_for_each_entry(kctl, &card->controls, list) {
495 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
496 return kctl;
498 return NULL;
501 EXPORT_SYMBOL(snd_ctl_find_numid);
504 * snd_ctl_find_id - find the control instance with the given id
505 * @card: the card instance
506 * @id: the id to search
508 * Finds the control instance with the given id from the card.
510 * Returns the pointer of the instance if found, or NULL if not.
512 * The caller must down card->controls_rwsem before calling this function
513 * (if the race condition can happen).
515 struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
516 struct snd_ctl_elem_id *id)
518 struct snd_kcontrol *kctl;
520 snd_assert(card != NULL && id != NULL, return NULL);
521 if (id->numid != 0)
522 return snd_ctl_find_numid(card, id->numid);
523 list_for_each_entry(kctl, &card->controls, list) {
524 if (kctl->id.iface != id->iface)
525 continue;
526 if (kctl->id.device != id->device)
527 continue;
528 if (kctl->id.subdevice != id->subdevice)
529 continue;
530 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
531 continue;
532 if (kctl->id.index > id->index)
533 continue;
534 if (kctl->id.index + kctl->count <= id->index)
535 continue;
536 return kctl;
538 return NULL;
541 EXPORT_SYMBOL(snd_ctl_find_id);
543 static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
544 unsigned int cmd, void __user *arg)
546 struct snd_ctl_card_info *info;
548 info = kzalloc(sizeof(*info), GFP_KERNEL);
549 if (! info)
550 return -ENOMEM;
551 down_read(&snd_ioctl_rwsem);
552 info->card = card->number;
553 strlcpy(info->id, card->id, sizeof(info->id));
554 strlcpy(info->driver, card->driver, sizeof(info->driver));
555 strlcpy(info->name, card->shortname, sizeof(info->name));
556 strlcpy(info->longname, card->longname, sizeof(info->longname));
557 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
558 strlcpy(info->components, card->components, sizeof(info->components));
559 up_read(&snd_ioctl_rwsem);
560 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
561 kfree(info);
562 return -EFAULT;
564 kfree(info);
565 return 0;
568 static int snd_ctl_elem_list(struct snd_card *card,
569 struct snd_ctl_elem_list __user *_list)
571 struct list_head *plist;
572 struct snd_ctl_elem_list list;
573 struct snd_kcontrol *kctl;
574 struct snd_ctl_elem_id *dst, *id;
575 unsigned int offset, space, first, jidx;
577 if (copy_from_user(&list, _list, sizeof(list)))
578 return -EFAULT;
579 offset = list.offset;
580 space = list.space;
581 first = 0;
582 /* try limit maximum space */
583 if (space > 16384)
584 return -ENOMEM;
585 if (space > 0) {
586 /* allocate temporary buffer for atomic operation */
587 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
588 if (dst == NULL)
589 return -ENOMEM;
590 down_read(&card->controls_rwsem);
591 list.count = card->controls_count;
592 plist = card->controls.next;
593 while (plist != &card->controls) {
594 if (offset == 0)
595 break;
596 kctl = snd_kcontrol(plist);
597 if (offset < kctl->count)
598 break;
599 offset -= kctl->count;
600 plist = plist->next;
602 list.used = 0;
603 id = dst;
604 while (space > 0 && plist != &card->controls) {
605 kctl = snd_kcontrol(plist);
606 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
607 snd_ctl_build_ioff(id, kctl, jidx);
608 id++;
609 space--;
610 list.used++;
612 plist = plist->next;
613 offset = 0;
615 up_read(&card->controls_rwsem);
616 if (list.used > 0 &&
617 copy_to_user(list.pids, dst,
618 list.used * sizeof(struct snd_ctl_elem_id))) {
619 vfree(dst);
620 return -EFAULT;
622 vfree(dst);
623 } else {
624 down_read(&card->controls_rwsem);
625 list.count = card->controls_count;
626 up_read(&card->controls_rwsem);
628 if (copy_to_user(_list, &list, sizeof(list)))
629 return -EFAULT;
630 return 0;
633 static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
634 struct snd_ctl_elem_info *info)
636 struct snd_card *card = ctl->card;
637 struct snd_kcontrol *kctl;
638 struct snd_kcontrol_volatile *vd;
639 unsigned int index_offset;
640 int result;
642 down_read(&card->controls_rwsem);
643 kctl = snd_ctl_find_id(card, &info->id);
644 if (kctl == NULL) {
645 up_read(&card->controls_rwsem);
646 return -ENOENT;
648 #ifdef CONFIG_SND_DEBUG
649 info->access = 0;
650 #endif
651 result = kctl->info(kctl, info);
652 if (result >= 0) {
653 snd_assert(info->access == 0, );
654 index_offset = snd_ctl_get_ioff(kctl, &info->id);
655 vd = &kctl->vd[index_offset];
656 snd_ctl_build_ioff(&info->id, kctl, index_offset);
657 info->access = vd->access;
658 if (vd->owner) {
659 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
660 if (vd->owner == ctl)
661 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
662 info->owner = vd->owner_pid;
663 } else {
664 info->owner = -1;
667 up_read(&card->controls_rwsem);
668 return result;
671 static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
672 struct snd_ctl_elem_info __user *_info)
674 struct snd_ctl_elem_info info;
675 int result;
677 if (copy_from_user(&info, _info, sizeof(info)))
678 return -EFAULT;
679 snd_power_lock(ctl->card);
680 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
681 if (result >= 0)
682 result = snd_ctl_elem_info(ctl, &info);
683 snd_power_unlock(ctl->card);
684 if (result >= 0)
685 if (copy_to_user(_info, &info, sizeof(info)))
686 return -EFAULT;
687 return result;
690 int snd_ctl_elem_read(struct snd_card *card, struct snd_ctl_elem_value *control)
692 struct snd_kcontrol *kctl;
693 struct snd_kcontrol_volatile *vd;
694 unsigned int index_offset;
695 int result, indirect;
697 down_read(&card->controls_rwsem);
698 kctl = snd_ctl_find_id(card, &control->id);
699 if (kctl == NULL) {
700 result = -ENOENT;
701 } else {
702 index_offset = snd_ctl_get_ioff(kctl, &control->id);
703 vd = &kctl->vd[index_offset];
704 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
705 if (control->indirect != indirect) {
706 result = -EACCES;
707 } else {
708 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) && kctl->get != NULL) {
709 snd_ctl_build_ioff(&control->id, kctl, index_offset);
710 result = kctl->get(kctl, control);
711 } else {
712 result = -EPERM;
716 up_read(&card->controls_rwsem);
717 return result;
720 EXPORT_SYMBOL(snd_ctl_elem_read);
722 static int snd_ctl_elem_read_user(struct snd_card *card,
723 struct snd_ctl_elem_value __user *_control)
725 struct snd_ctl_elem_value *control;
726 int result;
728 control = kmalloc(sizeof(*control), GFP_KERNEL);
729 if (control == NULL)
730 return -ENOMEM;
731 if (copy_from_user(control, _control, sizeof(*control))) {
732 kfree(control);
733 return -EFAULT;
735 snd_power_lock(card);
736 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
737 if (result >= 0)
738 result = snd_ctl_elem_read(card, control);
739 snd_power_unlock(card);
740 if (result >= 0)
741 if (copy_to_user(_control, control, sizeof(*control)))
742 result = -EFAULT;
743 kfree(control);
744 return result;
747 int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
748 struct snd_ctl_elem_value *control)
750 struct snd_kcontrol *kctl;
751 struct snd_kcontrol_volatile *vd;
752 unsigned int index_offset;
753 int result, indirect;
755 down_read(&card->controls_rwsem);
756 kctl = snd_ctl_find_id(card, &control->id);
757 if (kctl == NULL) {
758 result = -ENOENT;
759 } else {
760 index_offset = snd_ctl_get_ioff(kctl, &control->id);
761 vd = &kctl->vd[index_offset];
762 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
763 if (control->indirect != indirect) {
764 result = -EACCES;
765 } else {
766 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
767 kctl->put == NULL ||
768 (file && vd->owner != NULL && vd->owner != file)) {
769 result = -EPERM;
770 } else {
771 snd_ctl_build_ioff(&control->id, kctl, index_offset);
772 result = kctl->put(kctl, control);
774 if (result > 0) {
775 up_read(&card->controls_rwsem);
776 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &control->id);
777 return 0;
781 up_read(&card->controls_rwsem);
782 return result;
785 EXPORT_SYMBOL(snd_ctl_elem_write);
787 static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
788 struct snd_ctl_elem_value __user *_control)
790 struct snd_ctl_elem_value *control;
791 struct snd_card *card;
792 int result;
794 control = kmalloc(sizeof(*control), GFP_KERNEL);
795 if (control == NULL)
796 return -ENOMEM;
797 if (copy_from_user(control, _control, sizeof(*control))) {
798 kfree(control);
799 return -EFAULT;
801 card = file->card;
802 snd_power_lock(card);
803 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
804 if (result >= 0)
805 result = snd_ctl_elem_write(card, file, control);
806 snd_power_unlock(card);
807 if (result >= 0)
808 if (copy_to_user(_control, control, sizeof(*control)))
809 result = -EFAULT;
810 kfree(control);
811 return result;
814 static int snd_ctl_elem_lock(struct snd_ctl_file *file,
815 struct snd_ctl_elem_id __user *_id)
817 struct snd_card *card = file->card;
818 struct snd_ctl_elem_id id;
819 struct snd_kcontrol *kctl;
820 struct snd_kcontrol_volatile *vd;
821 int result;
823 if (copy_from_user(&id, _id, sizeof(id)))
824 return -EFAULT;
825 down_write(&card->controls_rwsem);
826 kctl = snd_ctl_find_id(card, &id);
827 if (kctl == NULL) {
828 result = -ENOENT;
829 } else {
830 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
831 if (vd->owner != NULL)
832 result = -EBUSY;
833 else {
834 vd->owner = file;
835 vd->owner_pid = current->pid;
836 result = 0;
839 up_write(&card->controls_rwsem);
840 return result;
843 static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
844 struct snd_ctl_elem_id __user *_id)
846 struct snd_card *card = file->card;
847 struct snd_ctl_elem_id id;
848 struct snd_kcontrol *kctl;
849 struct snd_kcontrol_volatile *vd;
850 int result;
852 if (copy_from_user(&id, _id, sizeof(id)))
853 return -EFAULT;
854 down_write(&card->controls_rwsem);
855 kctl = snd_ctl_find_id(card, &id);
856 if (kctl == NULL) {
857 result = -ENOENT;
858 } else {
859 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
860 if (vd->owner == NULL)
861 result = -EINVAL;
862 else if (vd->owner != file)
863 result = -EPERM;
864 else {
865 vd->owner = NULL;
866 vd->owner_pid = 0;
867 result = 0;
870 up_write(&card->controls_rwsem);
871 return result;
874 struct user_element {
875 struct snd_ctl_elem_info info;
876 void *elem_data; /* element data */
877 unsigned long elem_data_size; /* size of element data in bytes */
878 void *tlv_data; /* TLV data */
879 unsigned long tlv_data_size; /* TLV data size */
880 void *priv_data; /* private data (like strings for enumerated type) */
881 unsigned long priv_data_size; /* size of private data in bytes */
884 static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
885 struct snd_ctl_elem_info *uinfo)
887 struct user_element *ue = kcontrol->private_data;
889 *uinfo = ue->info;
890 return 0;
893 static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
894 struct snd_ctl_elem_value *ucontrol)
896 struct user_element *ue = kcontrol->private_data;
898 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
899 return 0;
902 static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
903 struct snd_ctl_elem_value *ucontrol)
905 int change;
906 struct user_element *ue = kcontrol->private_data;
908 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
909 if (change)
910 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
911 return change;
914 static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol,
915 int op_flag,
916 unsigned int size,
917 unsigned int __user *tlv)
919 struct user_element *ue = kcontrol->private_data;
920 int change = 0;
921 void *new_data;
923 if (op_flag > 0) {
924 if (size > 1024 * 128) /* sane value */
925 return -EINVAL;
926 new_data = kmalloc(size, GFP_KERNEL);
927 if (new_data == NULL)
928 return -ENOMEM;
929 if (copy_from_user(new_data, tlv, size)) {
930 kfree(new_data);
931 return -EFAULT;
933 change = ue->tlv_data_size != size;
934 if (!change)
935 change = memcmp(ue->tlv_data, new_data, size);
936 kfree(ue->tlv_data);
937 ue->tlv_data = new_data;
938 ue->tlv_data_size = size;
939 } else {
940 if (! ue->tlv_data_size || ! ue->tlv_data)
941 return -ENXIO;
942 if (size < ue->tlv_data_size)
943 return -ENOSPC;
944 if (copy_to_user(tlv, ue->tlv_data, ue->tlv_data_size))
945 return -EFAULT;
947 return change;
950 static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
952 struct user_element *ue = kcontrol->private_data;
953 if (ue->tlv_data)
954 kfree(ue->tlv_data);
955 kfree(ue);
958 static int snd_ctl_elem_add(struct snd_ctl_file *file,
959 struct snd_ctl_elem_info *info, int replace)
961 struct snd_card *card = file->card;
962 struct snd_kcontrol kctl, *_kctl;
963 unsigned int access;
964 long private_size;
965 struct user_element *ue;
966 int idx, err;
968 if (card->user_ctl_count >= MAX_USER_CONTROLS)
969 return -ENOMEM;
970 if (info->count > 1024)
971 return -EINVAL;
972 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
973 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
974 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
975 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE));
976 info->id.numid = 0;
977 memset(&kctl, 0, sizeof(kctl));
978 down_write(&card->controls_rwsem);
979 _kctl = snd_ctl_find_id(card, &info->id);
980 err = 0;
981 if (_kctl) {
982 if (replace)
983 err = snd_ctl_remove(card, _kctl);
984 else
985 err = -EBUSY;
986 } else {
987 if (replace)
988 err = -ENOENT;
990 up_write(&card->controls_rwsem);
991 if (err < 0)
992 return err;
993 memcpy(&kctl.id, &info->id, sizeof(info->id));
994 kctl.count = info->owner ? info->owner : 1;
995 access |= SNDRV_CTL_ELEM_ACCESS_USER;
996 kctl.info = snd_ctl_elem_user_info;
997 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
998 kctl.get = snd_ctl_elem_user_get;
999 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1000 kctl.put = snd_ctl_elem_user_put;
1001 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
1002 kctl.tlv.c = snd_ctl_elem_user_tlv;
1003 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1005 switch (info->type) {
1006 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
1007 case SNDRV_CTL_ELEM_TYPE_INTEGER:
1008 private_size = sizeof(long);
1009 if (info->count > 128)
1010 return -EINVAL;
1011 break;
1012 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
1013 private_size = sizeof(long long);
1014 if (info->count > 64)
1015 return -EINVAL;
1016 break;
1017 case SNDRV_CTL_ELEM_TYPE_BYTES:
1018 private_size = sizeof(unsigned char);
1019 if (info->count > 512)
1020 return -EINVAL;
1021 break;
1022 case SNDRV_CTL_ELEM_TYPE_IEC958:
1023 private_size = sizeof(struct snd_aes_iec958);
1024 if (info->count != 1)
1025 return -EINVAL;
1026 break;
1027 default:
1028 return -EINVAL;
1030 private_size *= info->count;
1031 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
1032 if (ue == NULL)
1033 return -ENOMEM;
1034 ue->info = *info;
1035 ue->info.access = 0;
1036 ue->elem_data = (char *)ue + sizeof(*ue);
1037 ue->elem_data_size = private_size;
1038 kctl.private_free = snd_ctl_elem_user_free;
1039 _kctl = snd_ctl_new(&kctl, access);
1040 if (_kctl == NULL) {
1041 kfree(ue);
1042 return -ENOMEM;
1044 _kctl->private_data = ue;
1045 for (idx = 0; idx < _kctl->count; idx++)
1046 _kctl->vd[idx].owner = file;
1047 err = snd_ctl_add(card, _kctl);
1048 if (err < 0)
1049 return err;
1051 down_write(&card->controls_rwsem);
1052 card->user_ctl_count++;
1053 up_write(&card->controls_rwsem);
1055 return 0;
1058 static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1059 struct snd_ctl_elem_info __user *_info, int replace)
1061 struct snd_ctl_elem_info info;
1062 if (copy_from_user(&info, _info, sizeof(info)))
1063 return -EFAULT;
1064 return snd_ctl_elem_add(file, &info, replace);
1067 static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1068 struct snd_ctl_elem_id __user *_id)
1070 struct snd_ctl_elem_id id;
1071 int err;
1073 if (copy_from_user(&id, _id, sizeof(id)))
1074 return -EFAULT;
1075 err = snd_ctl_remove_unlocked_id(file, &id);
1076 if (! err) {
1077 struct snd_card *card = file->card;
1078 down_write(&card->controls_rwsem);
1079 card->user_ctl_count--;
1080 up_write(&card->controls_rwsem);
1082 return err;
1085 static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
1087 int subscribe;
1088 if (get_user(subscribe, ptr))
1089 return -EFAULT;
1090 if (subscribe < 0) {
1091 subscribe = file->subscribed;
1092 if (put_user(subscribe, ptr))
1093 return -EFAULT;
1094 return 0;
1096 if (subscribe) {
1097 file->subscribed = 1;
1098 return 0;
1099 } else if (file->subscribed) {
1100 snd_ctl_empty_read_queue(file);
1101 file->subscribed = 0;
1103 return 0;
1106 static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1107 struct snd_ctl_tlv __user *_tlv,
1108 int op_flag)
1110 struct snd_card *card = file->card;
1111 struct snd_ctl_tlv tlv;
1112 struct snd_kcontrol *kctl;
1113 struct snd_kcontrol_volatile *vd;
1114 unsigned int len;
1115 int err = 0;
1117 if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
1118 return -EFAULT;
1119 if (tlv.length < sizeof(unsigned int) * 3)
1120 return -EINVAL;
1121 down_read(&card->controls_rwsem);
1122 kctl = snd_ctl_find_numid(card, tlv.numid);
1123 if (kctl == NULL) {
1124 err = -ENOENT;
1125 goto __kctl_end;
1127 if (kctl->tlv.p == NULL) {
1128 err = -ENXIO;
1129 goto __kctl_end;
1131 vd = &kctl->vd[tlv.numid - kctl->id.numid];
1132 if ((op_flag == 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) == 0) ||
1133 (op_flag > 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) == 0) ||
1134 (op_flag < 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND) == 0)) {
1135 err = -ENXIO;
1136 goto __kctl_end;
1138 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1139 if (file && vd->owner != NULL && vd->owner != file) {
1140 err = -EPERM;
1141 goto __kctl_end;
1143 err = kctl->tlv.c(kctl, op_flag, tlv.length, _tlv->tlv);
1144 if (err > 0) {
1145 up_read(&card->controls_rwsem);
1146 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &kctl->id);
1147 return 0;
1149 } else {
1150 if (op_flag) {
1151 err = -ENXIO;
1152 goto __kctl_end;
1154 len = kctl->tlv.p[1] + 2 * sizeof(unsigned int);
1155 if (tlv.length < len) {
1156 err = -ENOMEM;
1157 goto __kctl_end;
1159 if (copy_to_user(_tlv->tlv, kctl->tlv.p, len))
1160 err = -EFAULT;
1162 __kctl_end:
1163 up_read(&card->controls_rwsem);
1164 return err;
1167 static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1169 struct snd_ctl_file *ctl;
1170 struct snd_card *card;
1171 struct snd_kctl_ioctl *p;
1172 void __user *argp = (void __user *)arg;
1173 int __user *ip = argp;
1174 int err;
1176 ctl = file->private_data;
1177 card = ctl->card;
1178 snd_assert(card != NULL, return -ENXIO);
1179 switch (cmd) {
1180 case SNDRV_CTL_IOCTL_PVERSION:
1181 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1182 case SNDRV_CTL_IOCTL_CARD_INFO:
1183 return snd_ctl_card_info(card, ctl, cmd, argp);
1184 case SNDRV_CTL_IOCTL_ELEM_LIST:
1185 return snd_ctl_elem_list(card, argp);
1186 case SNDRV_CTL_IOCTL_ELEM_INFO:
1187 return snd_ctl_elem_info_user(ctl, argp);
1188 case SNDRV_CTL_IOCTL_ELEM_READ:
1189 return snd_ctl_elem_read_user(card, argp);
1190 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1191 return snd_ctl_elem_write_user(ctl, argp);
1192 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1193 return snd_ctl_elem_lock(ctl, argp);
1194 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1195 return snd_ctl_elem_unlock(ctl, argp);
1196 case SNDRV_CTL_IOCTL_ELEM_ADD:
1197 return snd_ctl_elem_add_user(ctl, argp, 0);
1198 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1199 return snd_ctl_elem_add_user(ctl, argp, 1);
1200 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1201 return snd_ctl_elem_remove(ctl, argp);
1202 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1203 return snd_ctl_subscribe_events(ctl, ip);
1204 case SNDRV_CTL_IOCTL_TLV_READ:
1205 return snd_ctl_tlv_ioctl(ctl, argp, 0);
1206 case SNDRV_CTL_IOCTL_TLV_WRITE:
1207 return snd_ctl_tlv_ioctl(ctl, argp, 1);
1208 case SNDRV_CTL_IOCTL_TLV_COMMAND:
1209 return snd_ctl_tlv_ioctl(ctl, argp, -1);
1210 case SNDRV_CTL_IOCTL_POWER:
1211 return -ENOPROTOOPT;
1212 case SNDRV_CTL_IOCTL_POWER_STATE:
1213 #ifdef CONFIG_PM
1214 return put_user(card->power_state, ip) ? -EFAULT : 0;
1215 #else
1216 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1217 #endif
1219 down_read(&snd_ioctl_rwsem);
1220 list_for_each_entry(p, &snd_control_ioctls, list) {
1221 err = p->fioctl(card, ctl, cmd, arg);
1222 if (err != -ENOIOCTLCMD) {
1223 up_read(&snd_ioctl_rwsem);
1224 return err;
1227 up_read(&snd_ioctl_rwsem);
1228 snd_printdd("unknown ioctl = 0x%x\n", cmd);
1229 return -ENOTTY;
1232 static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1233 size_t count, loff_t * offset)
1235 struct snd_ctl_file *ctl;
1236 int err = 0;
1237 ssize_t result = 0;
1239 ctl = file->private_data;
1240 snd_assert(ctl != NULL && ctl->card != NULL, return -ENXIO);
1241 if (!ctl->subscribed)
1242 return -EBADFD;
1243 if (count < sizeof(struct snd_ctl_event))
1244 return -EINVAL;
1245 spin_lock_irq(&ctl->read_lock);
1246 while (count >= sizeof(struct snd_ctl_event)) {
1247 struct snd_ctl_event ev;
1248 struct snd_kctl_event *kev;
1249 while (list_empty(&ctl->events)) {
1250 wait_queue_t wait;
1251 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1252 err = -EAGAIN;
1253 goto __end_lock;
1255 init_waitqueue_entry(&wait, current);
1256 add_wait_queue(&ctl->change_sleep, &wait);
1257 set_current_state(TASK_INTERRUPTIBLE);
1258 spin_unlock_irq(&ctl->read_lock);
1259 schedule();
1260 remove_wait_queue(&ctl->change_sleep, &wait);
1261 if (signal_pending(current))
1262 return -ERESTARTSYS;
1263 spin_lock_irq(&ctl->read_lock);
1265 kev = snd_kctl_event(ctl->events.next);
1266 ev.type = SNDRV_CTL_EVENT_ELEM;
1267 ev.data.elem.mask = kev->mask;
1268 ev.data.elem.id = kev->id;
1269 list_del(&kev->list);
1270 spin_unlock_irq(&ctl->read_lock);
1271 kfree(kev);
1272 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
1273 err = -EFAULT;
1274 goto __end;
1276 spin_lock_irq(&ctl->read_lock);
1277 buffer += sizeof(struct snd_ctl_event);
1278 count -= sizeof(struct snd_ctl_event);
1279 result += sizeof(struct snd_ctl_event);
1281 __end_lock:
1282 spin_unlock_irq(&ctl->read_lock);
1283 __end:
1284 return result > 0 ? result : err;
1287 static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1289 unsigned int mask;
1290 struct snd_ctl_file *ctl;
1292 ctl = file->private_data;
1293 if (!ctl->subscribed)
1294 return 0;
1295 poll_wait(file, &ctl->change_sleep, wait);
1297 mask = 0;
1298 if (!list_empty(&ctl->events))
1299 mask |= POLLIN | POLLRDNORM;
1301 return mask;
1305 * register the device-specific control-ioctls.
1306 * called from each device manager like pcm.c, hwdep.c, etc.
1308 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1310 struct snd_kctl_ioctl *pn;
1312 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
1313 if (pn == NULL)
1314 return -ENOMEM;
1315 pn->fioctl = fcn;
1316 down_write(&snd_ioctl_rwsem);
1317 list_add_tail(&pn->list, lists);
1318 up_write(&snd_ioctl_rwsem);
1319 return 0;
1322 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1324 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1327 EXPORT_SYMBOL(snd_ctl_register_ioctl);
1329 #ifdef CONFIG_COMPAT
1330 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1332 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1335 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
1336 #endif
1339 * de-register the device-specific control-ioctls.
1341 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1342 struct list_head *lists)
1344 struct snd_kctl_ioctl *p;
1346 snd_assert(fcn != NULL, return -EINVAL);
1347 down_write(&snd_ioctl_rwsem);
1348 list_for_each_entry(p, lists, list) {
1349 if (p->fioctl == fcn) {
1350 list_del(&p->list);
1351 up_write(&snd_ioctl_rwsem);
1352 kfree(p);
1353 return 0;
1356 up_write(&snd_ioctl_rwsem);
1357 snd_BUG();
1358 return -EINVAL;
1361 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1363 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1366 EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1368 #ifdef CONFIG_COMPAT
1369 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1371 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1374 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
1375 #endif
1377 static int snd_ctl_fasync(int fd, struct file * file, int on)
1379 struct snd_ctl_file *ctl;
1380 int err;
1381 ctl = file->private_data;
1382 err = fasync_helper(fd, file, on, &ctl->fasync);
1383 if (err < 0)
1384 return err;
1385 return 0;
1389 * ioctl32 compat
1391 #ifdef CONFIG_COMPAT
1392 #include "control_compat.c"
1393 #else
1394 #define snd_ctl_ioctl_compat NULL
1395 #endif
1398 * INIT PART
1401 static const struct file_operations snd_ctl_f_ops =
1403 .owner = THIS_MODULE,
1404 .read = snd_ctl_read,
1405 .open = snd_ctl_open,
1406 .release = snd_ctl_release,
1407 .poll = snd_ctl_poll,
1408 .unlocked_ioctl = snd_ctl_ioctl,
1409 .compat_ioctl = snd_ctl_ioctl_compat,
1410 .fasync = snd_ctl_fasync,
1414 * registration of the control device
1416 static int snd_ctl_dev_register(struct snd_device *device)
1418 struct snd_card *card = device->device_data;
1419 int err, cardnum;
1420 char name[16];
1422 snd_assert(card != NULL, return -ENXIO);
1423 cardnum = card->number;
1424 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1425 sprintf(name, "controlC%i", cardnum);
1426 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1427 &snd_ctl_f_ops, card, name)) < 0)
1428 return err;
1429 return 0;
1433 * disconnection of the control device
1435 static int snd_ctl_dev_disconnect(struct snd_device *device)
1437 struct snd_card *card = device->device_data;
1438 struct snd_ctl_file *ctl;
1439 int err, cardnum;
1441 snd_assert(card != NULL, return -ENXIO);
1442 cardnum = card->number;
1443 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1445 down_read(&card->controls_rwsem);
1446 list_for_each_entry(ctl, &card->ctl_files, list) {
1447 wake_up(&ctl->change_sleep);
1448 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1450 up_read(&card->controls_rwsem);
1452 if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
1453 card, -1)) < 0)
1454 return err;
1455 return 0;
1459 * free all controls
1461 static int snd_ctl_dev_free(struct snd_device *device)
1463 struct snd_card *card = device->device_data;
1464 struct snd_kcontrol *control;
1466 down_write(&card->controls_rwsem);
1467 while (!list_empty(&card->controls)) {
1468 control = snd_kcontrol(card->controls.next);
1469 snd_ctl_remove(card, control);
1471 up_write(&card->controls_rwsem);
1472 return 0;
1476 * create control core:
1477 * called from init.c
1479 int snd_ctl_create(struct snd_card *card)
1481 static struct snd_device_ops ops = {
1482 .dev_free = snd_ctl_dev_free,
1483 .dev_register = snd_ctl_dev_register,
1484 .dev_disconnect = snd_ctl_dev_disconnect,
1487 snd_assert(card != NULL, return -ENXIO);
1488 return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);