2 * Routines for driver control interface
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sound/driver.h>
23 #include <linux/threads.h>
24 #include <linux/interrupt.h>
25 #include <linux/smp_lock.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/time.h>
29 #include <sound/core.h>
30 #include <sound/minors.h>
31 #include <sound/info.h>
32 #include <sound/control.h>
34 /* max number of user-defined controls */
35 #define MAX_USER_CONTROLS 32
37 struct snd_kctl_ioctl
{
38 struct list_head list
; /* list of all ioctls */
39 snd_kctl_ioctl_func_t fioctl
;
42 static DECLARE_RWSEM(snd_ioctl_rwsem
);
43 static LIST_HEAD(snd_control_ioctls
);
45 static LIST_HEAD(snd_control_compat_ioctls
);
48 static int snd_ctl_open(struct inode
*inode
, struct file
*file
)
50 int cardnum
= SNDRV_MINOR_CARD(iminor(inode
));
52 struct snd_card
*card
;
53 struct snd_ctl_file
*ctl
;
56 card
= snd_cards
[cardnum
];
61 err
= snd_card_file_add(card
, file
);
66 if (!try_module_get(card
->module
)) {
70 ctl
= kzalloc(sizeof(*ctl
), GFP_KERNEL
);
75 INIT_LIST_HEAD(&ctl
->events
);
76 init_waitqueue_head(&ctl
->change_sleep
);
77 spin_lock_init(&ctl
->read_lock
);
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
);
87 module_put(card
->module
);
89 snd_card_file_remove(card
, file
);
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
);
104 spin_unlock(&ctl
->read_lock
);
107 static int snd_ctl_release(struct inode
*inode
, struct file
*file
)
110 struct list_head
*list
;
111 struct snd_card
*card
;
112 struct snd_ctl_file
*ctl
;
113 struct snd_kcontrol
*control
;
116 ctl
= file
->private_data
;
117 fasync_helper(-1, file
, 0, &ctl
->fasync
);
118 file
->private_data
= NULL
;
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
);
133 module_put(card
->module
);
134 snd_card_file_remove(card
, file
);
138 void snd_ctl_notify(struct snd_card
*card
, unsigned int mask
,
139 struct snd_ctl_elem_id
*id
)
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
++;
151 list_for_each(flist
, &card
->ctl_files
) {
152 struct list_head
*elist
;
153 ctl
= snd_ctl_file(flist
);
154 if (!ctl
->subscribed
)
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
) {
164 ev
= kzalloc(sizeof(*ev
), GFP_ATOMIC
);
168 list_add_tail(&ev
->list
, &ctl
->events
);
170 snd_printk(KERN_ERR
"No memory available to allocate event\n");
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
;
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
);
199 snd_printk(KERN_ERR
"Cannot allocate control instance\n");
203 for (idx
= 0; idx
< kctl
->count
; idx
++)
204 kctl
->vd
[idx
].access
= access
;
209 * snd_ctl_new1 - create a control instance from the template
210 * @ncontrol: the initialization record
211 * @private_data: the private data to set
213 * Allocates a new struct snd_kcontrol instance and initialize from the given
214 * template. When the access field of ncontrol is 0, it's assumed as
215 * READWRITE access. When the count field is 0, it's assumes as one.
217 * Returns the pointer of the newly generated instance, or NULL on failure.
219 struct snd_kcontrol
*snd_ctl_new1(const struct snd_kcontrol_new
*ncontrol
,
222 struct snd_kcontrol kctl
;
225 snd_assert(ncontrol
!= NULL
, return NULL
);
226 snd_assert(ncontrol
->info
!= NULL
, return NULL
);
227 memset(&kctl
, 0, sizeof(kctl
));
228 kctl
.id
.iface
= ncontrol
->iface
;
229 kctl
.id
.device
= ncontrol
->device
;
230 kctl
.id
.subdevice
= ncontrol
->subdevice
;
232 strlcpy(kctl
.id
.name
, ncontrol
->name
, sizeof(kctl
.id
.name
));
233 kctl
.id
.index
= ncontrol
->index
;
234 kctl
.count
= ncontrol
->count
? ncontrol
->count
: 1;
235 access
= ncontrol
->access
== 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE
:
236 (ncontrol
->access
& (SNDRV_CTL_ELEM_ACCESS_READWRITE
|SNDRV_CTL_ELEM_ACCESS_INACTIVE
|
237 SNDRV_CTL_ELEM_ACCESS_DINDIRECT
|SNDRV_CTL_ELEM_ACCESS_INDIRECT
));
238 kctl
.info
= ncontrol
->info
;
239 kctl
.get
= ncontrol
->get
;
240 kctl
.put
= ncontrol
->put
;
241 kctl
.private_value
= ncontrol
->private_value
;
242 kctl
.private_data
= private_data
;
243 return snd_ctl_new(&kctl
, access
);
247 * snd_ctl_free_one - release the control instance
248 * @kcontrol: the control instance
250 * Releases the control instance created via snd_ctl_new()
252 * Don't call this after the control was added to the card.
254 void snd_ctl_free_one(struct snd_kcontrol
*kcontrol
)
257 if (kcontrol
->private_free
)
258 kcontrol
->private_free(kcontrol
);
263 static unsigned int snd_ctl_hole_check(struct snd_card
*card
,
266 struct list_head
*list
;
267 struct snd_kcontrol
*kctl
;
269 list_for_each(list
, &card
->controls
) {
270 kctl
= snd_kcontrol(list
);
271 if ((kctl
->id
.numid
<= card
->last_numid
&&
272 kctl
->id
.numid
+ kctl
->count
> card
->last_numid
) ||
273 (kctl
->id
.numid
<= card
->last_numid
+ count
- 1 &&
274 kctl
->id
.numid
+ kctl
->count
> card
->last_numid
+ count
- 1))
275 return card
->last_numid
= kctl
->id
.numid
+ kctl
->count
- 1;
277 return card
->last_numid
;
280 static int snd_ctl_find_hole(struct snd_card
*card
, unsigned int count
)
282 unsigned int last_numid
, iter
= 100000;
284 last_numid
= card
->last_numid
;
285 while (last_numid
!= snd_ctl_hole_check(card
, count
)) {
287 /* this situation is very unlikely */
288 snd_printk(KERN_ERR
"unable to allocate new control numid\n");
291 last_numid
= card
->last_numid
;
297 * snd_ctl_add - add the control instance to the card
298 * @card: the card instance
299 * @kcontrol: the control instance to add
301 * Adds the control instance created via snd_ctl_new() or
302 * snd_ctl_new1() to the given card. Assigns also an unique
303 * numid used for fast search.
305 * Returns zero if successful, or a negative error code on failure.
307 * It frees automatically the control which cannot be added.
309 int snd_ctl_add(struct snd_card
*card
, struct snd_kcontrol
*kcontrol
)
311 struct snd_ctl_elem_id id
;
314 snd_assert(card
!= NULL
, return -EINVAL
);
317 snd_assert(kcontrol
->info
!= NULL
, return -EINVAL
);
319 down_write(&card
->controls_rwsem
);
320 if (snd_ctl_find_id(card
, &id
)) {
321 up_write(&card
->controls_rwsem
);
322 snd_ctl_free_one(kcontrol
);
323 snd_printd(KERN_ERR
"control %i:%i:%i:%s:%i is already present\n",
331 if (snd_ctl_find_hole(card
, kcontrol
->count
) < 0) {
332 up_write(&card
->controls_rwsem
);
333 snd_ctl_free_one(kcontrol
);
336 list_add_tail(&kcontrol
->list
, &card
->controls
);
337 card
->controls_count
+= kcontrol
->count
;
338 kcontrol
->id
.numid
= card
->last_numid
+ 1;
339 card
->last_numid
+= kcontrol
->count
;
340 up_write(&card
->controls_rwsem
);
341 for (idx
= 0; idx
< kcontrol
->count
; idx
++, id
.index
++, id
.numid
++)
342 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_ADD
, &id
);
347 * snd_ctl_remove - remove the control from the card and release it
348 * @card: the card instance
349 * @kcontrol: the control instance to remove
351 * Removes the control from the card and then releases the instance.
352 * You don't need to call snd_ctl_free_one(). You must be in
353 * the write lock - down_write(&card->controls_rwsem).
355 * Returns 0 if successful, or a negative error code on failure.
357 int snd_ctl_remove(struct snd_card
*card
, struct snd_kcontrol
*kcontrol
)
359 struct snd_ctl_elem_id id
;
362 snd_assert(card
!= NULL
&& kcontrol
!= NULL
, return -EINVAL
);
363 list_del(&kcontrol
->list
);
364 card
->controls_count
-= kcontrol
->count
;
366 for (idx
= 0; idx
< kcontrol
->count
; idx
++, id
.index
++, id
.numid
++)
367 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_REMOVE
, &id
);
368 snd_ctl_free_one(kcontrol
);
373 * snd_ctl_remove_id - remove the control of the given id and release it
374 * @card: the card instance
375 * @id: the control id to remove
377 * Finds the control instance with the given id, removes it from the
378 * card list and releases it.
380 * Returns 0 if successful, or a negative error code on failure.
382 int snd_ctl_remove_id(struct snd_card
*card
, struct snd_ctl_elem_id
*id
)
384 struct snd_kcontrol
*kctl
;
387 down_write(&card
->controls_rwsem
);
388 kctl
= snd_ctl_find_id(card
, id
);
390 up_write(&card
->controls_rwsem
);
393 ret
= snd_ctl_remove(card
, kctl
);
394 up_write(&card
->controls_rwsem
);
399 * snd_ctl_remove_unlocked_id - remove the unlocked control of the given id and release it
400 * @file: active control handle
401 * @id: the control id to remove
403 * Finds the control instance with the given id, removes it from the
404 * card list and releases it.
406 * Returns 0 if successful, or a negative error code on failure.
408 static int snd_ctl_remove_unlocked_id(struct snd_ctl_file
* file
,
409 struct snd_ctl_elem_id
*id
)
411 struct snd_card
*card
= file
->card
;
412 struct snd_kcontrol
*kctl
;
415 down_write(&card
->controls_rwsem
);
416 kctl
= snd_ctl_find_id(card
, id
);
418 up_write(&card
->controls_rwsem
);
421 for (idx
= 0; idx
< kctl
->count
; idx
++)
422 if (kctl
->vd
[idx
].owner
!= NULL
&& kctl
->vd
[idx
].owner
!= file
) {
423 up_write(&card
->controls_rwsem
);
426 ret
= snd_ctl_remove(card
, kctl
);
427 up_write(&card
->controls_rwsem
);
432 * snd_ctl_rename_id - replace the id of a control on the card
433 * @card: the card instance
434 * @src_id: the old id
435 * @dst_id: the new id
437 * Finds the control with the old id from the card, and replaces the
438 * id with the new one.
440 * Returns zero if successful, or a negative error code on failure.
442 int snd_ctl_rename_id(struct snd_card
*card
, struct snd_ctl_elem_id
*src_id
,
443 struct snd_ctl_elem_id
*dst_id
)
445 struct snd_kcontrol
*kctl
;
447 down_write(&card
->controls_rwsem
);
448 kctl
= snd_ctl_find_id(card
, src_id
);
450 up_write(&card
->controls_rwsem
);
454 kctl
->id
.numid
= card
->last_numid
+ 1;
455 card
->last_numid
+= kctl
->count
;
456 up_write(&card
->controls_rwsem
);
461 * snd_ctl_find_numid - find the control instance with the given number-id
462 * @card: the card instance
463 * @numid: the number-id to search
465 * Finds the control instance with the given number-id from the card.
467 * Returns the pointer of the instance if found, or NULL if not.
469 * The caller must down card->controls_rwsem before calling this function
470 * (if the race condition can happen).
472 struct snd_kcontrol
*snd_ctl_find_numid(struct snd_card
*card
, unsigned int numid
)
474 struct list_head
*list
;
475 struct snd_kcontrol
*kctl
;
477 snd_assert(card
!= NULL
&& numid
!= 0, return NULL
);
478 list_for_each(list
, &card
->controls
) {
479 kctl
= snd_kcontrol(list
);
480 if (kctl
->id
.numid
<= numid
&& kctl
->id
.numid
+ kctl
->count
> numid
)
487 * snd_ctl_find_id - find the control instance with the given id
488 * @card: the card instance
489 * @id: the id to search
491 * Finds the control instance with the given id from the card.
493 * Returns the pointer of the instance if found, or NULL if not.
495 * The caller must down card->controls_rwsem before calling this function
496 * (if the race condition can happen).
498 struct snd_kcontrol
*snd_ctl_find_id(struct snd_card
*card
,
499 struct snd_ctl_elem_id
*id
)
501 struct list_head
*list
;
502 struct snd_kcontrol
*kctl
;
504 snd_assert(card
!= NULL
&& id
!= NULL
, return NULL
);
506 return snd_ctl_find_numid(card
, id
->numid
);
507 list_for_each(list
, &card
->controls
) {
508 kctl
= snd_kcontrol(list
);
509 if (kctl
->id
.iface
!= id
->iface
)
511 if (kctl
->id
.device
!= id
->device
)
513 if (kctl
->id
.subdevice
!= id
->subdevice
)
515 if (strncmp(kctl
->id
.name
, id
->name
, sizeof(kctl
->id
.name
)))
517 if (kctl
->id
.index
> id
->index
)
519 if (kctl
->id
.index
+ kctl
->count
<= id
->index
)
526 static int snd_ctl_card_info(struct snd_card
*card
, struct snd_ctl_file
* ctl
,
527 unsigned int cmd
, void __user
*arg
)
529 struct snd_ctl_card_info
*info
;
531 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
534 down_read(&snd_ioctl_rwsem
);
535 info
->card
= card
->number
;
536 strlcpy(info
->id
, card
->id
, sizeof(info
->id
));
537 strlcpy(info
->driver
, card
->driver
, sizeof(info
->driver
));
538 strlcpy(info
->name
, card
->shortname
, sizeof(info
->name
));
539 strlcpy(info
->longname
, card
->longname
, sizeof(info
->longname
));
540 strlcpy(info
->mixername
, card
->mixername
, sizeof(info
->mixername
));
541 strlcpy(info
->components
, card
->components
, sizeof(info
->components
));
542 up_read(&snd_ioctl_rwsem
);
543 if (copy_to_user(arg
, info
, sizeof(struct snd_ctl_card_info
))) {
551 static int snd_ctl_elem_list(struct snd_card
*card
,
552 struct snd_ctl_elem_list __user
*_list
)
554 struct list_head
*plist
;
555 struct snd_ctl_elem_list list
;
556 struct snd_kcontrol
*kctl
;
557 struct snd_ctl_elem_id
*dst
, *id
;
558 unsigned int offset
, space
, first
, jidx
;
560 if (copy_from_user(&list
, _list
, sizeof(list
)))
562 offset
= list
.offset
;
565 /* try limit maximum space */
569 /* allocate temporary buffer for atomic operation */
570 dst
= vmalloc(space
* sizeof(struct snd_ctl_elem_id
));
573 down_read(&card
->controls_rwsem
);
574 list
.count
= card
->controls_count
;
575 plist
= card
->controls
.next
;
576 while (plist
!= &card
->controls
) {
579 kctl
= snd_kcontrol(plist
);
580 if (offset
< kctl
->count
)
582 offset
-= kctl
->count
;
587 while (space
> 0 && plist
!= &card
->controls
) {
588 kctl
= snd_kcontrol(plist
);
589 for (jidx
= offset
; space
> 0 && jidx
< kctl
->count
; jidx
++) {
590 snd_ctl_build_ioff(id
, kctl
, jidx
);
598 up_read(&card
->controls_rwsem
);
600 copy_to_user(list
.pids
, dst
,
601 list
.used
* sizeof(struct snd_ctl_elem_id
))) {
607 down_read(&card
->controls_rwsem
);
608 list
.count
= card
->controls_count
;
609 up_read(&card
->controls_rwsem
);
611 if (copy_to_user(_list
, &list
, sizeof(list
)))
616 static int snd_ctl_elem_info(struct snd_ctl_file
*ctl
,
617 struct snd_ctl_elem_info
*info
)
619 struct snd_card
*card
= ctl
->card
;
620 struct snd_kcontrol
*kctl
;
621 struct snd_kcontrol_volatile
*vd
;
622 unsigned int index_offset
;
625 down_read(&card
->controls_rwsem
);
626 kctl
= snd_ctl_find_id(card
, &info
->id
);
628 up_read(&card
->controls_rwsem
);
631 #ifdef CONFIG_SND_DEBUG
634 result
= kctl
->info(kctl
, info
);
636 snd_assert(info
->access
== 0, );
637 index_offset
= snd_ctl_get_ioff(kctl
, &info
->id
);
638 vd
= &kctl
->vd
[index_offset
];
639 snd_ctl_build_ioff(&info
->id
, kctl
, index_offset
);
640 info
->access
= vd
->access
;
642 info
->access
|= SNDRV_CTL_ELEM_ACCESS_LOCK
;
643 if (vd
->owner
== ctl
)
644 info
->access
|= SNDRV_CTL_ELEM_ACCESS_OWNER
;
645 info
->owner
= vd
->owner_pid
;
650 up_read(&card
->controls_rwsem
);
654 static int snd_ctl_elem_info_user(struct snd_ctl_file
*ctl
,
655 struct snd_ctl_elem_info __user
*_info
)
657 struct snd_ctl_elem_info info
;
660 if (copy_from_user(&info
, _info
, sizeof(info
)))
662 result
= snd_ctl_elem_info(ctl
, &info
);
664 if (copy_to_user(_info
, &info
, sizeof(info
)))
669 int snd_ctl_elem_read(struct snd_card
*card
, struct snd_ctl_elem_value
*control
)
671 struct snd_kcontrol
*kctl
;
672 struct snd_kcontrol_volatile
*vd
;
673 unsigned int index_offset
;
674 int result
, indirect
;
676 down_read(&card
->controls_rwsem
);
677 kctl
= snd_ctl_find_id(card
, &control
->id
);
681 index_offset
= snd_ctl_get_ioff(kctl
, &control
->id
);
682 vd
= &kctl
->vd
[index_offset
];
683 indirect
= vd
->access
& SNDRV_CTL_ELEM_ACCESS_INDIRECT
? 1 : 0;
684 if (control
->indirect
!= indirect
) {
687 if ((vd
->access
& SNDRV_CTL_ELEM_ACCESS_READ
) && kctl
->get
!= NULL
) {
688 snd_ctl_build_ioff(&control
->id
, kctl
, index_offset
);
689 result
= kctl
->get(kctl
, control
);
695 up_read(&card
->controls_rwsem
);
699 static int snd_ctl_elem_read_user(struct snd_card
*card
,
700 struct snd_ctl_elem_value __user
*_control
)
702 struct snd_ctl_elem_value
*control
;
705 control
= kmalloc(sizeof(*control
), GFP_KERNEL
);
708 if (copy_from_user(control
, _control
, sizeof(*control
))) {
712 result
= snd_ctl_elem_read(card
, control
);
714 if (copy_to_user(_control
, control
, sizeof(*control
)))
720 int snd_ctl_elem_write(struct snd_card
*card
, struct snd_ctl_file
*file
,
721 struct snd_ctl_elem_value
*control
)
723 struct snd_kcontrol
*kctl
;
724 struct snd_kcontrol_volatile
*vd
;
725 unsigned int index_offset
;
726 int result
, indirect
;
728 down_read(&card
->controls_rwsem
);
729 kctl
= snd_ctl_find_id(card
, &control
->id
);
733 index_offset
= snd_ctl_get_ioff(kctl
, &control
->id
);
734 vd
= &kctl
->vd
[index_offset
];
735 indirect
= vd
->access
& SNDRV_CTL_ELEM_ACCESS_INDIRECT
? 1 : 0;
736 if (control
->indirect
!= indirect
) {
739 if (!(vd
->access
& SNDRV_CTL_ELEM_ACCESS_WRITE
) ||
741 (file
&& vd
->owner
!= NULL
&& vd
->owner
!= file
)) {
744 snd_ctl_build_ioff(&control
->id
, kctl
, index_offset
);
745 result
= kctl
->put(kctl
, control
);
748 up_read(&card
->controls_rwsem
);
749 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_VALUE
, &control
->id
);
754 up_read(&card
->controls_rwsem
);
758 static int snd_ctl_elem_write_user(struct snd_ctl_file
*file
,
759 struct snd_ctl_elem_value __user
*_control
)
761 struct snd_ctl_elem_value
*control
;
764 control
= kmalloc(sizeof(*control
), GFP_KERNEL
);
767 if (copy_from_user(control
, _control
, sizeof(*control
))) {
771 result
= snd_ctl_elem_write(file
->card
, file
, control
);
773 if (copy_to_user(_control
, control
, sizeof(*control
)))
779 static int snd_ctl_elem_lock(struct snd_ctl_file
*file
,
780 struct snd_ctl_elem_id __user
*_id
)
782 struct snd_card
*card
= file
->card
;
783 struct snd_ctl_elem_id id
;
784 struct snd_kcontrol
*kctl
;
785 struct snd_kcontrol_volatile
*vd
;
788 if (copy_from_user(&id
, _id
, sizeof(id
)))
790 down_write(&card
->controls_rwsem
);
791 kctl
= snd_ctl_find_id(card
, &id
);
795 vd
= &kctl
->vd
[snd_ctl_get_ioff(kctl
, &id
)];
796 if (vd
->owner
!= NULL
)
800 vd
->owner_pid
= current
->pid
;
804 up_write(&card
->controls_rwsem
);
808 static int snd_ctl_elem_unlock(struct snd_ctl_file
*file
,
809 struct snd_ctl_elem_id __user
*_id
)
811 struct snd_card
*card
= file
->card
;
812 struct snd_ctl_elem_id id
;
813 struct snd_kcontrol
*kctl
;
814 struct snd_kcontrol_volatile
*vd
;
817 if (copy_from_user(&id
, _id
, sizeof(id
)))
819 down_write(&card
->controls_rwsem
);
820 kctl
= snd_ctl_find_id(card
, &id
);
824 vd
= &kctl
->vd
[snd_ctl_get_ioff(kctl
, &id
)];
825 if (vd
->owner
== NULL
)
827 else if (vd
->owner
!= file
)
835 up_write(&card
->controls_rwsem
);
839 struct user_element
{
840 struct snd_ctl_elem_info info
;
841 void *elem_data
; /* element data */
842 unsigned long elem_data_size
; /* size of element data in bytes */
843 void *priv_data
; /* private data (like strings for enumerated type) */
844 unsigned long priv_data_size
; /* size of private data in bytes */
847 static int snd_ctl_elem_user_info(struct snd_kcontrol
*kcontrol
,
848 struct snd_ctl_elem_info
*uinfo
)
850 struct user_element
*ue
= kcontrol
->private_data
;
856 static int snd_ctl_elem_user_get(struct snd_kcontrol
*kcontrol
,
857 struct snd_ctl_elem_value
*ucontrol
)
859 struct user_element
*ue
= kcontrol
->private_data
;
861 memcpy(&ucontrol
->value
, ue
->elem_data
, ue
->elem_data_size
);
865 static int snd_ctl_elem_user_put(struct snd_kcontrol
*kcontrol
,
866 struct snd_ctl_elem_value
*ucontrol
)
869 struct user_element
*ue
= kcontrol
->private_data
;
871 change
= memcmp(&ucontrol
->value
, ue
->elem_data
, ue
->elem_data_size
) != 0;
873 memcpy(ue
->elem_data
, &ucontrol
->value
, ue
->elem_data_size
);
877 static void snd_ctl_elem_user_free(struct snd_kcontrol
*kcontrol
)
879 kfree(kcontrol
->private_data
);
882 static int snd_ctl_elem_add(struct snd_ctl_file
*file
,
883 struct snd_ctl_elem_info
*info
, int replace
)
885 struct snd_card
*card
= file
->card
;
886 struct snd_kcontrol kctl
, *_kctl
;
889 struct user_element
*ue
;
892 if (card
->user_ctl_count
>= MAX_USER_CONTROLS
)
894 if (info
->count
> 1024)
896 access
= info
->access
== 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE
:
897 (info
->access
& (SNDRV_CTL_ELEM_ACCESS_READWRITE
|
898 SNDRV_CTL_ELEM_ACCESS_INACTIVE
));
900 memset(&kctl
, 0, sizeof(kctl
));
901 down_write(&card
->controls_rwsem
);
902 _kctl
= snd_ctl_find_id(card
, &info
->id
);
906 err
= snd_ctl_remove(card
, _kctl
);
913 up_write(&card
->controls_rwsem
);
916 memcpy(&kctl
.id
, &info
->id
, sizeof(info
->id
));
917 kctl
.count
= info
->owner
? info
->owner
: 1;
918 access
|= SNDRV_CTL_ELEM_ACCESS_USER
;
919 kctl
.info
= snd_ctl_elem_user_info
;
920 if (access
& SNDRV_CTL_ELEM_ACCESS_READ
)
921 kctl
.get
= snd_ctl_elem_user_get
;
922 if (access
& SNDRV_CTL_ELEM_ACCESS_WRITE
)
923 kctl
.put
= snd_ctl_elem_user_put
;
924 switch (info
->type
) {
925 case SNDRV_CTL_ELEM_TYPE_BOOLEAN
:
926 private_size
= sizeof(char);
927 if (info
->count
> 128)
930 case SNDRV_CTL_ELEM_TYPE_INTEGER
:
931 private_size
= sizeof(long);
932 if (info
->count
> 128)
935 case SNDRV_CTL_ELEM_TYPE_INTEGER64
:
936 private_size
= sizeof(long long);
937 if (info
->count
> 64)
940 case SNDRV_CTL_ELEM_TYPE_BYTES
:
941 private_size
= sizeof(unsigned char);
942 if (info
->count
> 512)
945 case SNDRV_CTL_ELEM_TYPE_IEC958
:
946 private_size
= sizeof(struct snd_aes_iec958
);
947 if (info
->count
!= 1)
953 private_size
*= info
->count
;
954 ue
= kzalloc(sizeof(struct user_element
) + private_size
, GFP_KERNEL
);
958 ue
->elem_data
= (char *)ue
+ sizeof(*ue
);
959 ue
->elem_data_size
= private_size
;
960 kctl
.private_free
= snd_ctl_elem_user_free
;
961 _kctl
= snd_ctl_new(&kctl
, access
);
963 kfree(_kctl
->private_data
);
966 _kctl
->private_data
= ue
;
967 for (idx
= 0; idx
< _kctl
->count
; idx
++)
968 _kctl
->vd
[idx
].owner
= file
;
969 err
= snd_ctl_add(card
, _kctl
);
971 snd_ctl_free_one(_kctl
);
975 down_write(&card
->controls_rwsem
);
976 card
->user_ctl_count
++;
977 up_write(&card
->controls_rwsem
);
982 static int snd_ctl_elem_add_user(struct snd_ctl_file
*file
,
983 struct snd_ctl_elem_info __user
*_info
, int replace
)
985 struct snd_ctl_elem_info info
;
986 if (copy_from_user(&info
, _info
, sizeof(info
)))
988 return snd_ctl_elem_add(file
, &info
, replace
);
991 static int snd_ctl_elem_remove(struct snd_ctl_file
*file
,
992 struct snd_ctl_elem_id __user
*_id
)
994 struct snd_ctl_elem_id id
;
997 if (copy_from_user(&id
, _id
, sizeof(id
)))
999 err
= snd_ctl_remove_unlocked_id(file
, &id
);
1001 struct snd_card
*card
= file
->card
;
1002 down_write(&card
->controls_rwsem
);
1003 card
->user_ctl_count
--;
1004 up_write(&card
->controls_rwsem
);
1009 static int snd_ctl_subscribe_events(struct snd_ctl_file
*file
, int __user
*ptr
)
1012 if (get_user(subscribe
, ptr
))
1014 if (subscribe
< 0) {
1015 subscribe
= file
->subscribed
;
1016 if (put_user(subscribe
, ptr
))
1021 file
->subscribed
= 1;
1023 } else if (file
->subscribed
) {
1024 snd_ctl_empty_read_queue(file
);
1025 file
->subscribed
= 0;
1030 static long snd_ctl_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1032 struct snd_ctl_file
*ctl
;
1033 struct snd_card
*card
;
1034 struct list_head
*list
;
1035 struct snd_kctl_ioctl
*p
;
1036 void __user
*argp
= (void __user
*)arg
;
1037 int __user
*ip
= argp
;
1040 ctl
= file
->private_data
;
1042 snd_assert(card
!= NULL
, return -ENXIO
);
1044 case SNDRV_CTL_IOCTL_PVERSION
:
1045 return put_user(SNDRV_CTL_VERSION
, ip
) ? -EFAULT
: 0;
1046 case SNDRV_CTL_IOCTL_CARD_INFO
:
1047 return snd_ctl_card_info(card
, ctl
, cmd
, argp
);
1048 case SNDRV_CTL_IOCTL_ELEM_LIST
:
1049 return snd_ctl_elem_list(ctl
->card
, argp
);
1050 case SNDRV_CTL_IOCTL_ELEM_INFO
:
1051 return snd_ctl_elem_info_user(ctl
, argp
);
1052 case SNDRV_CTL_IOCTL_ELEM_READ
:
1053 return snd_ctl_elem_read_user(ctl
->card
, argp
);
1054 case SNDRV_CTL_IOCTL_ELEM_WRITE
:
1055 return snd_ctl_elem_write_user(ctl
, argp
);
1056 case SNDRV_CTL_IOCTL_ELEM_LOCK
:
1057 return snd_ctl_elem_lock(ctl
, argp
);
1058 case SNDRV_CTL_IOCTL_ELEM_UNLOCK
:
1059 return snd_ctl_elem_unlock(ctl
, argp
);
1060 case SNDRV_CTL_IOCTL_ELEM_ADD
:
1061 return snd_ctl_elem_add_user(ctl
, argp
, 0);
1062 case SNDRV_CTL_IOCTL_ELEM_REPLACE
:
1063 return snd_ctl_elem_add_user(ctl
, argp
, 1);
1064 case SNDRV_CTL_IOCTL_ELEM_REMOVE
:
1065 return snd_ctl_elem_remove(ctl
, argp
);
1066 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS
:
1067 return snd_ctl_subscribe_events(ctl
, ip
);
1068 case SNDRV_CTL_IOCTL_POWER
:
1069 return -ENOPROTOOPT
;
1070 case SNDRV_CTL_IOCTL_POWER_STATE
:
1072 return put_user(card
->power_state
, ip
) ? -EFAULT
: 0;
1074 return put_user(SNDRV_CTL_POWER_D0
, ip
) ? -EFAULT
: 0;
1077 down_read(&snd_ioctl_rwsem
);
1078 list_for_each(list
, &snd_control_ioctls
) {
1079 p
= list_entry(list
, struct snd_kctl_ioctl
, list
);
1080 err
= p
->fioctl(card
, ctl
, cmd
, arg
);
1081 if (err
!= -ENOIOCTLCMD
) {
1082 up_read(&snd_ioctl_rwsem
);
1086 up_read(&snd_ioctl_rwsem
);
1087 snd_printdd("unknown ioctl = 0x%x\n", cmd
);
1091 static ssize_t
snd_ctl_read(struct file
*file
, char __user
*buffer
,
1092 size_t count
, loff_t
* offset
)
1094 struct snd_ctl_file
*ctl
;
1098 ctl
= file
->private_data
;
1099 snd_assert(ctl
!= NULL
&& ctl
->card
!= NULL
, return -ENXIO
);
1100 if (!ctl
->subscribed
)
1102 if (count
< sizeof(struct snd_ctl_event
))
1104 spin_lock_irq(&ctl
->read_lock
);
1105 while (count
>= sizeof(struct snd_ctl_event
)) {
1106 struct snd_ctl_event ev
;
1107 struct snd_kctl_event
*kev
;
1108 while (list_empty(&ctl
->events
)) {
1110 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
1114 init_waitqueue_entry(&wait
, current
);
1115 add_wait_queue(&ctl
->change_sleep
, &wait
);
1116 set_current_state(TASK_INTERRUPTIBLE
);
1117 spin_unlock_irq(&ctl
->read_lock
);
1119 remove_wait_queue(&ctl
->change_sleep
, &wait
);
1120 if (signal_pending(current
))
1121 return result
> 0 ? result
: -ERESTARTSYS
;
1122 spin_lock_irq(&ctl
->read_lock
);
1124 kev
= snd_kctl_event(ctl
->events
.next
);
1125 ev
.type
= SNDRV_CTL_EVENT_ELEM
;
1126 ev
.data
.elem
.mask
= kev
->mask
;
1127 ev
.data
.elem
.id
= kev
->id
;
1128 list_del(&kev
->list
);
1129 spin_unlock_irq(&ctl
->read_lock
);
1131 if (copy_to_user(buffer
, &ev
, sizeof(struct snd_ctl_event
))) {
1135 spin_lock_irq(&ctl
->read_lock
);
1136 buffer
+= sizeof(struct snd_ctl_event
);
1137 count
-= sizeof(struct snd_ctl_event
);
1138 result
+= sizeof(struct snd_ctl_event
);
1141 spin_unlock_irq(&ctl
->read_lock
);
1143 return result
> 0 ? result
: err
;
1146 static unsigned int snd_ctl_poll(struct file
*file
, poll_table
* wait
)
1149 struct snd_ctl_file
*ctl
;
1151 ctl
= file
->private_data
;
1152 if (!ctl
->subscribed
)
1154 poll_wait(file
, &ctl
->change_sleep
, wait
);
1157 if (!list_empty(&ctl
->events
))
1158 mask
|= POLLIN
| POLLRDNORM
;
1164 * register the device-specific control-ioctls.
1165 * called from each device manager like pcm.c, hwdep.c, etc.
1167 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn
, struct list_head
*lists
)
1169 struct snd_kctl_ioctl
*pn
;
1171 pn
= kzalloc(sizeof(struct snd_kctl_ioctl
), GFP_KERNEL
);
1175 down_write(&snd_ioctl_rwsem
);
1176 list_add_tail(&pn
->list
, lists
);
1177 up_write(&snd_ioctl_rwsem
);
1181 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn
)
1183 return _snd_ctl_register_ioctl(fcn
, &snd_control_ioctls
);
1186 #ifdef CONFIG_COMPAT
1187 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn
)
1189 return _snd_ctl_register_ioctl(fcn
, &snd_control_compat_ioctls
);
1194 * de-register the device-specific control-ioctls.
1196 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn
,
1197 struct list_head
*lists
)
1199 struct list_head
*list
;
1200 struct snd_kctl_ioctl
*p
;
1202 snd_assert(fcn
!= NULL
, return -EINVAL
);
1203 down_write(&snd_ioctl_rwsem
);
1204 list_for_each(list
, lists
) {
1205 p
= list_entry(list
, struct snd_kctl_ioctl
, list
);
1206 if (p
->fioctl
== fcn
) {
1208 up_write(&snd_ioctl_rwsem
);
1213 up_write(&snd_ioctl_rwsem
);
1218 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn
)
1220 return _snd_ctl_unregister_ioctl(fcn
, &snd_control_ioctls
);
1223 #ifdef CONFIG_COMPAT
1224 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn
)
1226 return _snd_ctl_unregister_ioctl(fcn
, &snd_control_compat_ioctls
);
1231 static int snd_ctl_fasync(int fd
, struct file
* file
, int on
)
1233 struct snd_ctl_file
*ctl
;
1235 ctl
= file
->private_data
;
1236 err
= fasync_helper(fd
, file
, on
, &ctl
->fasync
);
1245 #ifdef CONFIG_COMPAT
1246 #include "control_compat.c"
1248 #define snd_ctl_ioctl_compat NULL
1255 static struct file_operations snd_ctl_f_ops
=
1257 .owner
= THIS_MODULE
,
1258 .read
= snd_ctl_read
,
1259 .open
= snd_ctl_open
,
1260 .release
= snd_ctl_release
,
1261 .poll
= snd_ctl_poll
,
1262 .unlocked_ioctl
= snd_ctl_ioctl
,
1263 .compat_ioctl
= snd_ctl_ioctl_compat
,
1264 .fasync
= snd_ctl_fasync
,
1268 * registration of the control device
1270 static int snd_ctl_dev_register(struct snd_device
*device
)
1272 struct snd_card
*card
= device
->device_data
;
1276 snd_assert(card
!= NULL
, return -ENXIO
);
1277 cardnum
= card
->number
;
1278 snd_assert(cardnum
>= 0 && cardnum
< SNDRV_CARDS
, return -ENXIO
);
1279 sprintf(name
, "controlC%i", cardnum
);
1280 if ((err
= snd_register_device(SNDRV_DEVICE_TYPE_CONTROL
,
1281 card
, -1, &snd_ctl_f_ops
, name
)) < 0)
1287 * disconnection of the control device
1289 static int snd_ctl_dev_disconnect(struct snd_device
*device
)
1291 struct snd_card
*card
= device
->device_data
;
1292 struct list_head
*flist
;
1293 struct snd_ctl_file
*ctl
;
1295 down_read(&card
->controls_rwsem
);
1296 list_for_each(flist
, &card
->ctl_files
) {
1297 ctl
= snd_ctl_file(flist
);
1298 wake_up(&ctl
->change_sleep
);
1299 kill_fasync(&ctl
->fasync
, SIGIO
, POLL_ERR
);
1301 up_read(&card
->controls_rwsem
);
1308 static int snd_ctl_dev_free(struct snd_device
*device
)
1310 struct snd_card
*card
= device
->device_data
;
1311 struct snd_kcontrol
*control
;
1313 down_write(&card
->controls_rwsem
);
1314 while (!list_empty(&card
->controls
)) {
1315 control
= snd_kcontrol(card
->controls
.next
);
1316 snd_ctl_remove(card
, control
);
1318 up_write(&card
->controls_rwsem
);
1323 * de-registration of the control device
1325 static int snd_ctl_dev_unregister(struct snd_device
*device
)
1327 struct snd_card
*card
= device
->device_data
;
1330 snd_assert(card
!= NULL
, return -ENXIO
);
1331 cardnum
= card
->number
;
1332 snd_assert(cardnum
>= 0 && cardnum
< SNDRV_CARDS
, return -ENXIO
);
1333 if ((err
= snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL
,
1336 return snd_ctl_dev_free(device
);
1340 * create control core:
1341 * called from init.c
1343 int snd_ctl_create(struct snd_card
*card
)
1345 static struct snd_device_ops ops
= {
1346 .dev_free
= snd_ctl_dev_free
,
1347 .dev_register
= snd_ctl_dev_register
,
1348 .dev_disconnect
= snd_ctl_dev_disconnect
,
1349 .dev_unregister
= snd_ctl_dev_unregister
1352 snd_assert(card
!= NULL
, return -ENXIO
);
1353 return snd_device_new(card
, SNDRV_DEV_CONTROL
, card
, &ops
);