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 typedef struct _snd_kctl_ioctl
{
38 struct list_head list
; /* list of all ioctls */
39 snd_kctl_ioctl_func_t fioctl
;
42 #define snd_kctl_ioctl(n) list_entry(n, snd_kctl_ioctl_t, list)
44 static DECLARE_RWSEM(snd_ioctl_rwsem
);
45 static LIST_HEAD(snd_control_ioctls
);
47 static LIST_HEAD(snd_control_compat_ioctls
);
50 static int snd_ctl_open(struct inode
*inode
, struct file
*file
)
52 int cardnum
= SNDRV_MINOR_CARD(iminor(inode
));
58 card
= snd_cards
[cardnum
];
63 err
= snd_card_file_add(card
, file
);
68 if (!try_module_get(card
->module
)) {
72 ctl
= kcalloc(1, sizeof(*ctl
), GFP_KERNEL
);
77 INIT_LIST_HEAD(&ctl
->events
);
78 init_waitqueue_head(&ctl
->change_sleep
);
79 spin_lock_init(&ctl
->read_lock
);
81 ctl
->pid
= current
->pid
;
82 file
->private_data
= ctl
;
83 write_lock_irqsave(&card
->ctl_files_rwlock
, flags
);
84 list_add_tail(&ctl
->list
, &card
->ctl_files
);
85 write_unlock_irqrestore(&card
->ctl_files_rwlock
, flags
);
89 module_put(card
->module
);
91 snd_card_file_remove(card
, file
);
96 static void snd_ctl_empty_read_queue(snd_ctl_file_t
* ctl
)
98 snd_kctl_event_t
*cread
;
100 spin_lock(&ctl
->read_lock
);
101 while (!list_empty(&ctl
->events
)) {
102 cread
= snd_kctl_event(ctl
->events
.next
);
103 list_del(&cread
->list
);
106 spin_unlock(&ctl
->read_lock
);
109 static int snd_ctl_release(struct inode
*inode
, struct file
*file
)
112 struct list_head
*list
;
115 snd_kcontrol_t
*control
;
118 ctl
= file
->private_data
;
119 fasync_helper(-1, file
, 0, &ctl
->fasync
);
120 file
->private_data
= NULL
;
122 write_lock_irqsave(&card
->ctl_files_rwlock
, flags
);
123 list_del(&ctl
->list
);
124 write_unlock_irqrestore(&card
->ctl_files_rwlock
, flags
);
125 down_write(&card
->controls_rwsem
);
126 list_for_each(list
, &card
->controls
) {
127 control
= snd_kcontrol(list
);
128 for (idx
= 0; idx
< control
->count
; idx
++)
129 if (control
->vd
[idx
].owner
== ctl
)
130 control
->vd
[idx
].owner
= NULL
;
132 up_write(&card
->controls_rwsem
);
133 snd_ctl_empty_read_queue(ctl
);
135 module_put(card
->module
);
136 snd_card_file_remove(card
, file
);
140 void snd_ctl_notify(snd_card_t
*card
, unsigned int mask
, snd_ctl_elem_id_t
*id
)
143 struct list_head
*flist
;
145 snd_kctl_event_t
*ev
;
147 snd_runtime_check(card
!= NULL
&& id
!= NULL
, return);
148 read_lock(&card
->ctl_files_rwlock
);
149 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
150 card
->mixer_oss_change_count
++;
152 list_for_each(flist
, &card
->ctl_files
) {
153 struct list_head
*elist
;
154 ctl
= snd_ctl_file(flist
);
155 if (!ctl
->subscribed
)
157 spin_lock_irqsave(&ctl
->read_lock
, flags
);
158 list_for_each(elist
, &ctl
->events
) {
159 ev
= snd_kctl_event(elist
);
160 if (ev
->id
.numid
== id
->numid
) {
165 ev
= kcalloc(1, sizeof(*ev
), GFP_ATOMIC
);
169 list_add_tail(&ev
->list
, &ctl
->events
);
171 snd_printk(KERN_ERR
"No memory available to allocate event\n");
174 wake_up(&ctl
->change_sleep
);
175 spin_unlock_irqrestore(&ctl
->read_lock
, flags
);
176 kill_fasync(&ctl
->fasync
, SIGIO
, POLL_IN
);
178 read_unlock(&card
->ctl_files_rwlock
);
182 * snd_ctl_new - create a control instance from the template
183 * @control: the control template
184 * @access: the default control access
186 * Allocates a new snd_kcontrol_t instance and copies the given template
187 * to the new instance. It does not copy volatile data (access).
189 * Returns the pointer of the new instance, or NULL on failure.
191 snd_kcontrol_t
*snd_ctl_new(snd_kcontrol_t
* control
, unsigned int access
)
193 snd_kcontrol_t
*kctl
;
196 snd_runtime_check(control
!= NULL
, return NULL
);
197 snd_runtime_check(control
->count
> 0, return NULL
);
198 kctl
= kcalloc(1, sizeof(*kctl
) + sizeof(snd_kcontrol_volatile_t
) * control
->count
, GFP_KERNEL
);
202 for (idx
= 0; idx
< kctl
->count
; idx
++)
203 kctl
->vd
[idx
].access
= access
;
208 * snd_ctl_new1 - create a control instance from the template
209 * @ncontrol: the initialization record
210 * @private_data: the private data to set
212 * Allocates a new snd_kcontrol_t instance and initialize from the given
213 * template. When the access field of ncontrol is 0, it's assumed as
214 * READWRITE access. When the count field is 0, it's assumes as one.
216 * Returns the pointer of the newly generated instance, or NULL on failure.
218 snd_kcontrol_t
*snd_ctl_new1(snd_kcontrol_new_t
* ncontrol
, void *private_data
)
223 snd_runtime_check(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
;
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()
250 * Don't call this after the control was added to the card.
252 void snd_ctl_free_one(snd_kcontrol_t
* kcontrol
)
255 if (kcontrol
->private_free
)
256 kcontrol
->private_free(kcontrol
);
261 static unsigned int snd_ctl_hole_check(snd_card_t
* card
,
264 struct list_head
*list
;
265 snd_kcontrol_t
*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(snd_card_t
* 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
)) {
285 /* this situation is very unlikely */
286 snd_printk(KERN_ERR
"unable to allocate new control numid\n");
289 last_numid
= card
->last_numid
;
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(snd_card_t
* card
, snd_kcontrol_t
* kcontrol
)
309 snd_ctl_elem_id_t id
;
312 snd_runtime_check(card
!= NULL
&& kcontrol
!= NULL
, return -EINVAL
);
313 snd_assert(kcontrol
->info
!= NULL
, return -EINVAL
);
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",
327 if (snd_ctl_find_hole(card
, kcontrol
->count
) < 0) {
328 up_write(&card
->controls_rwsem
);
329 snd_ctl_free_one(kcontrol
);
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
);
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(snd_card_t
* card
, snd_kcontrol_t
* kcontrol
)
355 snd_ctl_elem_id_t id
;
358 snd_runtime_check(card
!= NULL
&& kcontrol
!= NULL
, return -EINVAL
);
359 list_del(&kcontrol
->list
);
360 card
->controls_count
-= kcontrol
->count
;
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
);
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(snd_card_t
* card
, snd_ctl_elem_id_t
*id
)
380 snd_kcontrol_t
*kctl
;
383 down_write(&card
->controls_rwsem
);
384 kctl
= snd_ctl_find_id(card
, id
);
386 up_write(&card
->controls_rwsem
);
389 ret
= snd_ctl_remove(card
, kctl
);
390 up_write(&card
->controls_rwsem
);
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(snd_ctl_file_t
* file
, snd_ctl_elem_id_t
*id
)
406 snd_card_t
*card
= file
->card
;
407 snd_kcontrol_t
*kctl
;
410 down_write(&card
->controls_rwsem
);
411 kctl
= snd_ctl_find_id(card
, id
);
413 up_write(&card
->controls_rwsem
);
416 for (idx
= 0; idx
< kctl
->count
; idx
++)
417 if (kctl
->vd
[idx
].owner
!= NULL
&& kctl
->vd
[idx
].owner
!= file
) {
418 up_write(&card
->controls_rwsem
);
421 ret
= snd_ctl_remove(card
, kctl
);
422 up_write(&card
->controls_rwsem
);
427 * snd_ctl_rename_id - replace the id of a control on the card
428 * @card: the card instance
429 * @src_id: the old id
430 * @dst_id: the new id
432 * Finds the control with the old id from the card, and replaces the
433 * id with the new one.
435 * Returns zero if successful, or a negative error code on failure.
437 int snd_ctl_rename_id(snd_card_t
* card
, snd_ctl_elem_id_t
*src_id
, snd_ctl_elem_id_t
*dst_id
)
439 snd_kcontrol_t
*kctl
;
441 down_write(&card
->controls_rwsem
);
442 kctl
= snd_ctl_find_id(card
, src_id
);
444 up_write(&card
->controls_rwsem
);
448 kctl
->id
.numid
= card
->last_numid
+ 1;
449 card
->last_numid
+= kctl
->count
;
450 up_write(&card
->controls_rwsem
);
455 * snd_ctl_find_numid - find the control instance with the given number-id
456 * @card: the card instance
457 * @numid: the number-id to search
459 * Finds the control instance with the given number-id from the card.
461 * Returns the pointer of the instance if found, or NULL if not.
463 * The caller must down card->controls_rwsem before calling this function
464 * (if the race condition can happen).
466 snd_kcontrol_t
*snd_ctl_find_numid(snd_card_t
* card
, unsigned int numid
)
468 struct list_head
*list
;
469 snd_kcontrol_t
*kctl
;
471 snd_runtime_check(card
!= NULL
&& numid
!= 0, return NULL
);
472 list_for_each(list
, &card
->controls
) {
473 kctl
= snd_kcontrol(list
);
474 if (kctl
->id
.numid
<= numid
&& kctl
->id
.numid
+ kctl
->count
> numid
)
481 * snd_ctl_find_id - find the control instance with the given id
482 * @card: the card instance
483 * @id: the id to search
485 * Finds the control instance with the given id from the card.
487 * Returns the pointer of the instance if found, or NULL if not.
489 * The caller must down card->controls_rwsem before calling this function
490 * (if the race condition can happen).
492 snd_kcontrol_t
*snd_ctl_find_id(snd_card_t
* card
, snd_ctl_elem_id_t
*id
)
494 struct list_head
*list
;
495 snd_kcontrol_t
*kctl
;
497 snd_runtime_check(card
!= NULL
&& id
!= NULL
, return NULL
);
499 return snd_ctl_find_numid(card
, id
->numid
);
500 list_for_each(list
, &card
->controls
) {
501 kctl
= snd_kcontrol(list
);
502 if (kctl
->id
.iface
!= id
->iface
)
504 if (kctl
->id
.device
!= id
->device
)
506 if (kctl
->id
.subdevice
!= id
->subdevice
)
508 if (strncmp(kctl
->id
.name
, id
->name
, sizeof(kctl
->id
.name
)))
510 if (kctl
->id
.index
> id
->index
)
512 if (kctl
->id
.index
+ kctl
->count
<= id
->index
)
519 static int snd_ctl_card_info(snd_card_t
* card
, snd_ctl_file_t
* ctl
,
520 unsigned int cmd
, void __user
*arg
)
522 snd_ctl_card_info_t
*info
;
524 info
= kcalloc(1, sizeof(*info
), GFP_KERNEL
);
527 down_read(&snd_ioctl_rwsem
);
528 info
->card
= card
->number
;
529 strlcpy(info
->id
, card
->id
, sizeof(info
->id
));
530 strlcpy(info
->driver
, card
->driver
, sizeof(info
->driver
));
531 strlcpy(info
->name
, card
->shortname
, sizeof(info
->name
));
532 strlcpy(info
->longname
, card
->longname
, sizeof(info
->longname
));
533 strlcpy(info
->mixername
, card
->mixername
, sizeof(info
->mixername
));
534 strlcpy(info
->components
, card
->components
, sizeof(info
->components
));
535 up_read(&snd_ioctl_rwsem
);
536 if (copy_to_user(arg
, info
, sizeof(snd_ctl_card_info_t
))) {
544 static int snd_ctl_elem_list(snd_card_t
*card
, snd_ctl_elem_list_t __user
*_list
)
546 struct list_head
*plist
;
547 snd_ctl_elem_list_t list
;
548 snd_kcontrol_t
*kctl
;
549 snd_ctl_elem_id_t
*dst
, *id
;
550 unsigned int offset
, space
, first
, jidx
;
552 if (copy_from_user(&list
, _list
, sizeof(list
)))
554 offset
= list
.offset
;
557 /* try limit maximum space */
561 /* allocate temporary buffer for atomic operation */
562 dst
= vmalloc(space
* sizeof(snd_ctl_elem_id_t
));
565 down_read(&card
->controls_rwsem
);
566 list
.count
= card
->controls_count
;
567 plist
= card
->controls
.next
;
568 while (plist
!= &card
->controls
) {
571 kctl
= snd_kcontrol(plist
);
572 if (offset
< kctl
->count
)
574 offset
-= kctl
->count
;
579 while (space
> 0 && plist
!= &card
->controls
) {
580 kctl
= snd_kcontrol(plist
);
581 for (jidx
= offset
; space
> 0 && jidx
< kctl
->count
; jidx
++) {
582 snd_ctl_build_ioff(id
, kctl
, jidx
);
590 up_read(&card
->controls_rwsem
);
591 if (list
.used
> 0 && copy_to_user(list
.pids
, dst
, list
.used
* sizeof(snd_ctl_elem_id_t
))) {
597 down_read(&card
->controls_rwsem
);
598 list
.count
= card
->controls_count
;
599 up_read(&card
->controls_rwsem
);
601 if (copy_to_user(_list
, &list
, sizeof(list
)))
606 static int snd_ctl_elem_info(snd_ctl_file_t
*ctl
, snd_ctl_elem_info_t
*info
)
608 snd_card_t
*card
= ctl
->card
;
609 snd_kcontrol_t
*kctl
;
610 snd_kcontrol_volatile_t
*vd
;
611 unsigned int index_offset
;
614 down_read(&card
->controls_rwsem
);
615 kctl
= snd_ctl_find_id(card
, &info
->id
);
617 up_read(&card
->controls_rwsem
);
620 #ifdef CONFIG_SND_DEBUG
623 result
= kctl
->info(kctl
, info
);
625 snd_assert(info
->access
== 0, );
626 index_offset
= snd_ctl_get_ioff(kctl
, &info
->id
);
627 vd
= &kctl
->vd
[index_offset
];
628 snd_ctl_build_ioff(&info
->id
, kctl
, index_offset
);
629 info
->access
= vd
->access
;
631 info
->access
|= SNDRV_CTL_ELEM_ACCESS_LOCK
;
632 if (vd
->owner
== ctl
)
633 info
->access
|= SNDRV_CTL_ELEM_ACCESS_OWNER
;
634 info
->owner
= vd
->owner_pid
;
639 up_read(&card
->controls_rwsem
);
643 static int snd_ctl_elem_info_user(snd_ctl_file_t
*ctl
, snd_ctl_elem_info_t __user
*_info
)
645 snd_ctl_elem_info_t info
;
648 if (copy_from_user(&info
, _info
, sizeof(info
)))
650 result
= snd_ctl_elem_info(ctl
, &info
);
652 if (copy_to_user(_info
, &info
, sizeof(info
)))
657 int snd_ctl_elem_read(snd_card_t
*card
, snd_ctl_elem_value_t
*control
)
659 snd_kcontrol_t
*kctl
;
660 snd_kcontrol_volatile_t
*vd
;
661 unsigned int index_offset
;
662 int result
, indirect
;
664 down_read(&card
->controls_rwsem
);
665 kctl
= snd_ctl_find_id(card
, &control
->id
);
669 index_offset
= snd_ctl_get_ioff(kctl
, &control
->id
);
670 vd
= &kctl
->vd
[index_offset
];
671 indirect
= vd
->access
& SNDRV_CTL_ELEM_ACCESS_INDIRECT
? 1 : 0;
672 if (control
->indirect
!= indirect
) {
675 if ((vd
->access
& SNDRV_CTL_ELEM_ACCESS_READ
) && kctl
->get
!= NULL
) {
676 snd_ctl_build_ioff(&control
->id
, kctl
, index_offset
);
677 result
= kctl
->get(kctl
, control
);
683 up_read(&card
->controls_rwsem
);
687 static int snd_ctl_elem_read_user(snd_card_t
*card
, snd_ctl_elem_value_t __user
*_control
)
689 snd_ctl_elem_value_t
*control
;
692 control
= kmalloc(sizeof(*control
), GFP_KERNEL
);
695 if (copy_from_user(control
, _control
, sizeof(*control
))) {
699 result
= snd_ctl_elem_read(card
, control
);
701 if (copy_to_user(_control
, control
, sizeof(*control
)))
707 int snd_ctl_elem_write(snd_card_t
*card
, snd_ctl_file_t
*file
, snd_ctl_elem_value_t
*control
)
709 snd_kcontrol_t
*kctl
;
710 snd_kcontrol_volatile_t
*vd
;
711 unsigned int index_offset
;
712 int result
, indirect
;
714 down_read(&card
->controls_rwsem
);
715 kctl
= snd_ctl_find_id(card
, &control
->id
);
719 index_offset
= snd_ctl_get_ioff(kctl
, &control
->id
);
720 vd
= &kctl
->vd
[index_offset
];
721 indirect
= vd
->access
& SNDRV_CTL_ELEM_ACCESS_INDIRECT
? 1 : 0;
722 if (control
->indirect
!= indirect
) {
725 if (!(vd
->access
& SNDRV_CTL_ELEM_ACCESS_WRITE
) ||
727 (file
&& vd
->owner
!= NULL
&& vd
->owner
!= file
)) {
730 snd_ctl_build_ioff(&control
->id
, kctl
, index_offset
);
731 result
= kctl
->put(kctl
, control
);
734 up_read(&card
->controls_rwsem
);
735 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_VALUE
, &control
->id
);
740 up_read(&card
->controls_rwsem
);
744 static int snd_ctl_elem_write_user(snd_ctl_file_t
*file
, snd_ctl_elem_value_t __user
*_control
)
746 snd_ctl_elem_value_t
*control
;
749 control
= kmalloc(sizeof(*control
), GFP_KERNEL
);
752 if (copy_from_user(control
, _control
, sizeof(*control
))) {
756 result
= snd_ctl_elem_write(file
->card
, file
, control
);
758 if (copy_to_user(_control
, control
, sizeof(*control
)))
764 static int snd_ctl_elem_lock(snd_ctl_file_t
*file
, snd_ctl_elem_id_t __user
*_id
)
766 snd_card_t
*card
= file
->card
;
767 snd_ctl_elem_id_t id
;
768 snd_kcontrol_t
*kctl
;
769 snd_kcontrol_volatile_t
*vd
;
772 if (copy_from_user(&id
, _id
, sizeof(id
)))
774 down_write(&card
->controls_rwsem
);
775 kctl
= snd_ctl_find_id(card
, &id
);
779 vd
= &kctl
->vd
[snd_ctl_get_ioff(kctl
, &id
)];
780 if (vd
->owner
!= NULL
)
784 vd
->owner_pid
= current
->pid
;
788 up_write(&card
->controls_rwsem
);
792 static int snd_ctl_elem_unlock(snd_ctl_file_t
*file
, snd_ctl_elem_id_t __user
*_id
)
794 snd_card_t
*card
= file
->card
;
795 snd_ctl_elem_id_t id
;
796 snd_kcontrol_t
*kctl
;
797 snd_kcontrol_volatile_t
*vd
;
800 if (copy_from_user(&id
, _id
, sizeof(id
)))
802 down_write(&card
->controls_rwsem
);
803 kctl
= snd_ctl_find_id(card
, &id
);
807 vd
= &kctl
->vd
[snd_ctl_get_ioff(kctl
, &id
)];
808 if (vd
->owner
== NULL
)
810 else if (vd
->owner
!= file
)
818 up_write(&card
->controls_rwsem
);
822 struct user_element
{
823 snd_ctl_elem_info_t info
;
824 void *elem_data
; /* element data */
825 unsigned long elem_data_size
; /* size of element data in bytes */
826 void *priv_data
; /* private data (like strings for enumerated type) */
827 unsigned long priv_data_size
; /* size of private data in bytes */
830 static int snd_ctl_elem_user_info(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_info_t
*uinfo
)
832 struct user_element
*ue
= kcontrol
->private_data
;
838 static int snd_ctl_elem_user_get(snd_kcontrol_t
* kcontrol
, snd_ctl_elem_value_t
* ucontrol
)
840 struct user_element
*ue
= kcontrol
->private_data
;
842 memcpy(&ucontrol
->value
, ue
->elem_data
, ue
->elem_data_size
);
846 static int snd_ctl_elem_user_put(snd_kcontrol_t
* kcontrol
, snd_ctl_elem_value_t
* ucontrol
)
849 struct user_element
*ue
= kcontrol
->private_data
;
851 change
= memcmp(&ucontrol
->value
, ue
->elem_data
, ue
->elem_data_size
) != 0;
853 memcpy(ue
->elem_data
, &ucontrol
->value
, ue
->elem_data_size
);
857 static void snd_ctl_elem_user_free(snd_kcontrol_t
* kcontrol
)
859 kfree(kcontrol
->private_data
);
862 static int snd_ctl_elem_add(snd_ctl_file_t
*file
, snd_ctl_elem_info_t
*info
, int replace
)
864 snd_card_t
*card
= file
->card
;
865 snd_kcontrol_t kctl
, *_kctl
;
868 struct user_element
*ue
;
871 if (card
->user_ctl_count
>= MAX_USER_CONTROLS
)
873 if (info
->count
> 1024)
875 access
= info
->access
== 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE
:
876 (info
->access
& (SNDRV_CTL_ELEM_ACCESS_READWRITE
|SNDRV_CTL_ELEM_ACCESS_INACTIVE
));
878 memset(&kctl
, 0, sizeof(kctl
));
879 down_write(&card
->controls_rwsem
);
880 _kctl
= snd_ctl_find_id(card
, &info
->id
);
884 err
= snd_ctl_remove(card
, _kctl
);
891 up_write(&card
->controls_rwsem
);
894 memcpy(&kctl
.id
, &info
->id
, sizeof(info
->id
));
895 kctl
.count
= info
->owner
? info
->owner
: 1;
896 access
|= SNDRV_CTL_ELEM_ACCESS_USER
;
897 kctl
.info
= snd_ctl_elem_user_info
;
898 if (access
& SNDRV_CTL_ELEM_ACCESS_READ
)
899 kctl
.get
= snd_ctl_elem_user_get
;
900 if (access
& SNDRV_CTL_ELEM_ACCESS_WRITE
)
901 kctl
.put
= snd_ctl_elem_user_put
;
902 switch (info
->type
) {
903 case SNDRV_CTL_ELEM_TYPE_BOOLEAN
:
904 private_size
= sizeof(char);
905 if (info
->count
> 128)
908 case SNDRV_CTL_ELEM_TYPE_INTEGER
:
909 private_size
= sizeof(long);
910 if (info
->count
> 128)
913 case SNDRV_CTL_ELEM_TYPE_INTEGER64
:
914 private_size
= sizeof(long long);
915 if (info
->count
> 64)
918 case SNDRV_CTL_ELEM_TYPE_BYTES
:
919 private_size
= sizeof(unsigned char);
920 if (info
->count
> 512)
923 case SNDRV_CTL_ELEM_TYPE_IEC958
:
924 private_size
= sizeof(struct sndrv_aes_iec958
);
925 if (info
->count
!= 1)
931 private_size
*= info
->count
;
932 ue
= kcalloc(1, sizeof(struct user_element
) + private_size
, GFP_KERNEL
);
936 ue
->elem_data
= (char *)ue
+ sizeof(*ue
);
937 ue
->elem_data_size
= private_size
;
938 kctl
.private_free
= snd_ctl_elem_user_free
;
939 _kctl
= snd_ctl_new(&kctl
, access
);
941 kfree(_kctl
->private_data
);
944 _kctl
->private_data
= ue
;
945 for (idx
= 0; idx
< _kctl
->count
; idx
++)
946 _kctl
->vd
[idx
].owner
= file
;
947 err
= snd_ctl_add(card
, _kctl
);
949 snd_ctl_free_one(_kctl
);
953 down_write(&card
->controls_rwsem
);
954 card
->user_ctl_count
++;
955 up_write(&card
->controls_rwsem
);
960 static int snd_ctl_elem_add_user(snd_ctl_file_t
*file
, snd_ctl_elem_info_t __user
*_info
, int replace
)
962 snd_ctl_elem_info_t info
;
963 if (copy_from_user(&info
, _info
, sizeof(info
)))
965 return snd_ctl_elem_add(file
, &info
, replace
);
968 static int snd_ctl_elem_remove(snd_ctl_file_t
*file
, snd_ctl_elem_id_t __user
*_id
)
970 snd_ctl_elem_id_t id
;
973 if (copy_from_user(&id
, _id
, sizeof(id
)))
975 err
= snd_ctl_remove_unlocked_id(file
, &id
);
977 snd_card_t
*card
= file
->card
;
978 down_write(&card
->controls_rwsem
);
979 card
->user_ctl_count
--;
980 up_write(&card
->controls_rwsem
);
985 static int snd_ctl_subscribe_events(snd_ctl_file_t
*file
, int __user
*ptr
)
988 if (get_user(subscribe
, ptr
))
991 subscribe
= file
->subscribed
;
992 if (put_user(subscribe
, ptr
))
997 file
->subscribed
= 1;
999 } else if (file
->subscribed
) {
1000 snd_ctl_empty_read_queue(file
);
1001 file
->subscribed
= 0;
1008 * change the power state
1010 static int snd_ctl_set_power_state(snd_card_t
*card
, unsigned int power_state
)
1012 switch (power_state
) {
1013 case SNDRV_CTL_POWER_D0
:
1014 if (card
->power_state
!= power_state
) {
1015 card
->pm_resume(card
);
1016 snd_power_change_state(card
, power_state
);
1019 case SNDRV_CTL_POWER_D3hot
:
1020 if (card
->power_state
!= power_state
) {
1021 card
->pm_suspend(card
, PMSG_SUSPEND
);
1022 snd_power_change_state(card
, power_state
);
1025 case SNDRV_CTL_POWER_D1
:
1026 case SNDRV_CTL_POWER_D2
:
1027 case SNDRV_CTL_POWER_D3cold
:
1028 /* not supported yet */
1036 static long snd_ctl_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1038 snd_ctl_file_t
*ctl
;
1040 struct list_head
*list
;
1041 snd_kctl_ioctl_t
*p
;
1042 void __user
*argp
= (void __user
*)arg
;
1043 int __user
*ip
= argp
;
1046 ctl
= file
->private_data
;
1048 snd_assert(card
!= NULL
, return -ENXIO
);
1050 case SNDRV_CTL_IOCTL_PVERSION
:
1051 return put_user(SNDRV_CTL_VERSION
, ip
) ? -EFAULT
: 0;
1052 case SNDRV_CTL_IOCTL_CARD_INFO
:
1053 return snd_ctl_card_info(card
, ctl
, cmd
, argp
);
1054 case SNDRV_CTL_IOCTL_ELEM_LIST
:
1055 return snd_ctl_elem_list(ctl
->card
, argp
);
1056 case SNDRV_CTL_IOCTL_ELEM_INFO
:
1057 return snd_ctl_elem_info_user(ctl
, argp
);
1058 case SNDRV_CTL_IOCTL_ELEM_READ
:
1059 return snd_ctl_elem_read_user(ctl
->card
, argp
);
1060 case SNDRV_CTL_IOCTL_ELEM_WRITE
:
1061 return snd_ctl_elem_write_user(ctl
, argp
);
1062 case SNDRV_CTL_IOCTL_ELEM_LOCK
:
1063 return snd_ctl_elem_lock(ctl
, argp
);
1064 case SNDRV_CTL_IOCTL_ELEM_UNLOCK
:
1065 return snd_ctl_elem_unlock(ctl
, argp
);
1066 case SNDRV_CTL_IOCTL_ELEM_ADD
:
1067 return snd_ctl_elem_add_user(ctl
, argp
, 0);
1068 case SNDRV_CTL_IOCTL_ELEM_REPLACE
:
1069 return snd_ctl_elem_add_user(ctl
, argp
, 1);
1070 case SNDRV_CTL_IOCTL_ELEM_REMOVE
:
1071 return snd_ctl_elem_remove(ctl
, argp
);
1072 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS
:
1073 return snd_ctl_subscribe_events(ctl
, ip
);
1074 case SNDRV_CTL_IOCTL_POWER
:
1075 if (get_user(err
, ip
))
1077 if (!capable(CAP_SYS_ADMIN
))
1080 if (card
->pm_suspend
&& card
->pm_resume
) {
1081 snd_power_lock(card
);
1082 err
= snd_ctl_set_power_state(card
, err
);
1083 snd_power_unlock(card
);
1088 case SNDRV_CTL_IOCTL_POWER_STATE
:
1090 return put_user(card
->power_state
, ip
) ? -EFAULT
: 0;
1092 return put_user(SNDRV_CTL_POWER_D0
, ip
) ? -EFAULT
: 0;
1095 down_read(&snd_ioctl_rwsem
);
1096 list_for_each(list
, &snd_control_ioctls
) {
1097 p
= list_entry(list
, snd_kctl_ioctl_t
, list
);
1098 err
= p
->fioctl(card
, ctl
, cmd
, arg
);
1099 if (err
!= -ENOIOCTLCMD
) {
1100 up_read(&snd_ioctl_rwsem
);
1104 up_read(&snd_ioctl_rwsem
);
1105 snd_printd("unknown ioctl = 0x%x\n", cmd
);
1109 static ssize_t
snd_ctl_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
* offset
)
1111 snd_ctl_file_t
*ctl
;
1115 ctl
= file
->private_data
;
1116 snd_assert(ctl
!= NULL
&& ctl
->card
!= NULL
, return -ENXIO
);
1117 if (!ctl
->subscribed
)
1119 if (count
< sizeof(snd_ctl_event_t
))
1121 spin_lock_irq(&ctl
->read_lock
);
1122 while (count
>= sizeof(snd_ctl_event_t
)) {
1124 snd_kctl_event_t
*kev
;
1125 while (list_empty(&ctl
->events
)) {
1127 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
1131 init_waitqueue_entry(&wait
, current
);
1132 add_wait_queue(&ctl
->change_sleep
, &wait
);
1133 set_current_state(TASK_INTERRUPTIBLE
);
1134 spin_unlock_irq(&ctl
->read_lock
);
1136 remove_wait_queue(&ctl
->change_sleep
, &wait
);
1137 if (signal_pending(current
))
1138 return result
> 0 ? result
: -ERESTARTSYS
;
1139 spin_lock_irq(&ctl
->read_lock
);
1141 kev
= snd_kctl_event(ctl
->events
.next
);
1142 ev
.type
= SNDRV_CTL_EVENT_ELEM
;
1143 ev
.data
.elem
.mask
= kev
->mask
;
1144 ev
.data
.elem
.id
= kev
->id
;
1145 list_del(&kev
->list
);
1146 spin_unlock_irq(&ctl
->read_lock
);
1148 if (copy_to_user(buffer
, &ev
, sizeof(snd_ctl_event_t
))) {
1152 spin_lock_irq(&ctl
->read_lock
);
1153 buffer
+= sizeof(snd_ctl_event_t
);
1154 count
-= sizeof(snd_ctl_event_t
);
1155 result
+= sizeof(snd_ctl_event_t
);
1158 spin_unlock_irq(&ctl
->read_lock
);
1160 return result
> 0 ? result
: err
;
1163 static unsigned int snd_ctl_poll(struct file
*file
, poll_table
* wait
)
1166 snd_ctl_file_t
*ctl
;
1168 ctl
= file
->private_data
;
1169 if (!ctl
->subscribed
)
1171 poll_wait(file
, &ctl
->change_sleep
, wait
);
1174 if (!list_empty(&ctl
->events
))
1175 mask
|= POLLIN
| POLLRDNORM
;
1181 * register the device-specific control-ioctls.
1182 * called from each device manager like pcm.c, hwdep.c, etc.
1184 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn
, struct list_head
*lists
)
1186 snd_kctl_ioctl_t
*pn
;
1188 pn
= kcalloc(1, sizeof(snd_kctl_ioctl_t
), GFP_KERNEL
);
1192 down_write(&snd_ioctl_rwsem
);
1193 list_add_tail(&pn
->list
, lists
);
1194 up_write(&snd_ioctl_rwsem
);
1198 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn
)
1200 return _snd_ctl_register_ioctl(fcn
, &snd_control_ioctls
);
1203 #ifdef CONFIG_COMPAT
1204 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn
)
1206 return _snd_ctl_register_ioctl(fcn
, &snd_control_compat_ioctls
);
1211 * de-register the device-specific control-ioctls.
1213 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn
, struct list_head
*lists
)
1215 struct list_head
*list
;
1216 snd_kctl_ioctl_t
*p
;
1218 snd_runtime_check(fcn
!= NULL
, return -EINVAL
);
1219 down_write(&snd_ioctl_rwsem
);
1220 list_for_each(list
, lists
) {
1221 p
= list_entry(list
, snd_kctl_ioctl_t
, list
);
1222 if (p
->fioctl
== fcn
) {
1224 up_write(&snd_ioctl_rwsem
);
1229 up_write(&snd_ioctl_rwsem
);
1234 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn
)
1236 return _snd_ctl_unregister_ioctl(fcn
, &snd_control_ioctls
);
1239 #ifdef CONFIG_COMPAT
1240 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn
)
1242 return _snd_ctl_unregister_ioctl(fcn
, &snd_control_compat_ioctls
);
1247 static int snd_ctl_fasync(int fd
, struct file
* file
, int on
)
1249 snd_ctl_file_t
*ctl
;
1251 ctl
= file
->private_data
;
1252 err
= fasync_helper(fd
, file
, on
, &ctl
->fasync
);
1261 #ifdef CONFIG_COMPAT
1262 #include "control_compat.c"
1264 #define snd_ctl_ioctl_compat NULL
1271 static struct file_operations snd_ctl_f_ops
=
1273 .owner
= THIS_MODULE
,
1274 .read
= snd_ctl_read
,
1275 .open
= snd_ctl_open
,
1276 .release
= snd_ctl_release
,
1277 .poll
= snd_ctl_poll
,
1278 .unlocked_ioctl
= snd_ctl_ioctl
,
1279 .compat_ioctl
= snd_ctl_ioctl_compat
,
1280 .fasync
= snd_ctl_fasync
,
1283 static snd_minor_t snd_ctl_reg
=
1286 .f_ops
= &snd_ctl_f_ops
,
1290 * registration of the control device
1292 static int snd_ctl_dev_register(snd_device_t
*device
)
1294 snd_card_t
*card
= device
->device_data
;
1298 snd_assert(card
!= NULL
, return -ENXIO
);
1299 cardnum
= card
->number
;
1300 snd_assert(cardnum
>= 0 && cardnum
< SNDRV_CARDS
, return -ENXIO
);
1301 sprintf(name
, "controlC%i", cardnum
);
1302 if ((err
= snd_register_device(SNDRV_DEVICE_TYPE_CONTROL
,
1303 card
, 0, &snd_ctl_reg
, name
)) < 0)
1309 * disconnection of the control device
1311 static int snd_ctl_dev_disconnect(snd_device_t
*device
)
1313 snd_card_t
*card
= device
->device_data
;
1314 struct list_head
*flist
;
1315 snd_ctl_file_t
*ctl
;
1317 down_read(&card
->controls_rwsem
);
1318 list_for_each(flist
, &card
->ctl_files
) {
1319 ctl
= snd_ctl_file(flist
);
1320 wake_up(&ctl
->change_sleep
);
1321 kill_fasync(&ctl
->fasync
, SIGIO
, POLL_ERR
);
1323 up_read(&card
->controls_rwsem
);
1330 static int snd_ctl_dev_free(snd_device_t
*device
)
1332 snd_card_t
*card
= device
->device_data
;
1333 snd_kcontrol_t
*control
;
1335 down_write(&card
->controls_rwsem
);
1336 while (!list_empty(&card
->controls
)) {
1337 control
= snd_kcontrol(card
->controls
.next
);
1338 snd_ctl_remove(card
, control
);
1340 up_write(&card
->controls_rwsem
);
1345 * de-registration of the control device
1347 static int snd_ctl_dev_unregister(snd_device_t
*device
)
1349 snd_card_t
*card
= device
->device_data
;
1352 snd_assert(card
!= NULL
, return -ENXIO
);
1353 cardnum
= card
->number
;
1354 snd_assert(cardnum
>= 0 && cardnum
< SNDRV_CARDS
, return -ENXIO
);
1355 if ((err
= snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL
, card
, 0)) < 0)
1357 return snd_ctl_dev_free(device
);
1361 * create control core:
1362 * called from init.c
1364 int snd_ctl_create(snd_card_t
*card
)
1366 static snd_device_ops_t ops
= {
1367 .dev_free
= snd_ctl_dev_free
,
1368 .dev_register
= snd_ctl_dev_register
,
1369 .dev_disconnect
= snd_ctl_dev_disconnect
,
1370 .dev_unregister
= snd_ctl_dev_unregister
1373 snd_assert(card
!= NULL
, return -ENXIO
);
1374 return snd_device_new(card
, SNDRV_DEV_CONTROL
, card
, &ops
);