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
);
201 for (idx
= 0; idx
< kctl
->count
; idx
++)
202 kctl
->vd
[idx
].access
= access
;
207 * snd_ctl_new1 - create a control instance from the template
208 * @ncontrol: the initialization record
209 * @private_data: the private data to set
211 * Allocates a new struct snd_kcontrol instance and initialize from the given
212 * template. When the access field of ncontrol is 0, it's assumed as
213 * READWRITE access. When the count field is 0, it's assumes as one.
215 * Returns the pointer of the newly generated instance, or NULL on failure.
217 struct snd_kcontrol
*snd_ctl_new1(const struct snd_kcontrol_new
*ncontrol
,
220 struct snd_kcontrol kctl
;
223 snd_assert(ncontrol
!= NULL
, return NULL
);
224 snd_assert(ncontrol
->info
!= NULL
, return NULL
);
225 memset(&kctl
, 0, sizeof(kctl
));
226 kctl
.id
.iface
= ncontrol
->iface
;
227 kctl
.id
.device
= ncontrol
->device
;
228 kctl
.id
.subdevice
= ncontrol
->subdevice
;
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(struct snd_kcontrol
*kcontrol
)
255 if (kcontrol
->private_free
)
256 kcontrol
->private_free(kcontrol
);
261 static unsigned int snd_ctl_hole_check(struct snd_card
*card
,
264 struct list_head
*list
;
265 struct snd_kcontrol
*kctl
;
267 list_for_each(list
, &card
->controls
) {
268 kctl
= snd_kcontrol(list
);
269 if ((kctl
->id
.numid
<= card
->last_numid
&&
270 kctl
->id
.numid
+ kctl
->count
> card
->last_numid
) ||
271 (kctl
->id
.numid
<= card
->last_numid
+ count
- 1 &&
272 kctl
->id
.numid
+ kctl
->count
> card
->last_numid
+ count
- 1))
273 return card
->last_numid
= kctl
->id
.numid
+ kctl
->count
- 1;
275 return card
->last_numid
;
278 static int snd_ctl_find_hole(struct snd_card
*card
, unsigned int count
)
280 unsigned int last_numid
, iter
= 100000;
282 last_numid
= card
->last_numid
;
283 while (last_numid
!= snd_ctl_hole_check(card
, count
)) {
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(struct snd_card
*card
, struct snd_kcontrol
*kcontrol
)
309 struct snd_ctl_elem_id id
;
312 snd_assert(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(struct snd_card
*card
, struct snd_kcontrol
*kcontrol
)
355 struct snd_ctl_elem_id id
;
358 snd_assert(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(struct snd_card
*card
, struct snd_ctl_elem_id
*id
)
380 struct snd_kcontrol
*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(struct snd_ctl_file
* file
,
405 struct snd_ctl_elem_id
*id
)
407 struct snd_card
*card
= file
->card
;
408 struct snd_kcontrol
*kctl
;
411 down_write(&card
->controls_rwsem
);
412 kctl
= snd_ctl_find_id(card
, id
);
414 up_write(&card
->controls_rwsem
);
417 for (idx
= 0; idx
< kctl
->count
; idx
++)
418 if (kctl
->vd
[idx
].owner
!= NULL
&& kctl
->vd
[idx
].owner
!= file
) {
419 up_write(&card
->controls_rwsem
);
422 ret
= snd_ctl_remove(card
, kctl
);
423 up_write(&card
->controls_rwsem
);
428 * snd_ctl_rename_id - replace the id of a control on the card
429 * @card: the card instance
430 * @src_id: the old id
431 * @dst_id: the new id
433 * Finds the control with the old id from the card, and replaces the
434 * id with the new one.
436 * Returns zero if successful, or a negative error code on failure.
438 int snd_ctl_rename_id(struct snd_card
*card
, struct snd_ctl_elem_id
*src_id
,
439 struct snd_ctl_elem_id
*dst_id
)
441 struct snd_kcontrol
*kctl
;
443 down_write(&card
->controls_rwsem
);
444 kctl
= snd_ctl_find_id(card
, src_id
);
446 up_write(&card
->controls_rwsem
);
450 kctl
->id
.numid
= card
->last_numid
+ 1;
451 card
->last_numid
+= kctl
->count
;
452 up_write(&card
->controls_rwsem
);
457 * snd_ctl_find_numid - find the control instance with the given number-id
458 * @card: the card instance
459 * @numid: the number-id to search
461 * Finds the control instance with the given number-id from the card.
463 * Returns the pointer of the instance if found, or NULL if not.
465 * The caller must down card->controls_rwsem before calling this function
466 * (if the race condition can happen).
468 struct snd_kcontrol
*snd_ctl_find_numid(struct snd_card
*card
, unsigned int numid
)
470 struct list_head
*list
;
471 struct snd_kcontrol
*kctl
;
473 snd_assert(card
!= NULL
&& numid
!= 0, return NULL
);
474 list_for_each(list
, &card
->controls
) {
475 kctl
= snd_kcontrol(list
);
476 if (kctl
->id
.numid
<= numid
&& kctl
->id
.numid
+ kctl
->count
> numid
)
483 * snd_ctl_find_id - find the control instance with the given id
484 * @card: the card instance
485 * @id: the id to search
487 * Finds the control instance with the given id from the card.
489 * Returns the pointer of the instance if found, or NULL if not.
491 * The caller must down card->controls_rwsem before calling this function
492 * (if the race condition can happen).
494 struct snd_kcontrol
*snd_ctl_find_id(struct snd_card
*card
,
495 struct snd_ctl_elem_id
*id
)
497 struct list_head
*list
;
498 struct snd_kcontrol
*kctl
;
500 snd_assert(card
!= NULL
&& id
!= NULL
, return NULL
);
502 return snd_ctl_find_numid(card
, id
->numid
);
503 list_for_each(list
, &card
->controls
) {
504 kctl
= snd_kcontrol(list
);
505 if (kctl
->id
.iface
!= id
->iface
)
507 if (kctl
->id
.device
!= id
->device
)
509 if (kctl
->id
.subdevice
!= id
->subdevice
)
511 if (strncmp(kctl
->id
.name
, id
->name
, sizeof(kctl
->id
.name
)))
513 if (kctl
->id
.index
> id
->index
)
515 if (kctl
->id
.index
+ kctl
->count
<= id
->index
)
522 static int snd_ctl_card_info(struct snd_card
*card
, struct snd_ctl_file
* ctl
,
523 unsigned int cmd
, void __user
*arg
)
525 struct snd_ctl_card_info
*info
;
527 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
530 down_read(&snd_ioctl_rwsem
);
531 info
->card
= card
->number
;
532 strlcpy(info
->id
, card
->id
, sizeof(info
->id
));
533 strlcpy(info
->driver
, card
->driver
, sizeof(info
->driver
));
534 strlcpy(info
->name
, card
->shortname
, sizeof(info
->name
));
535 strlcpy(info
->longname
, card
->longname
, sizeof(info
->longname
));
536 strlcpy(info
->mixername
, card
->mixername
, sizeof(info
->mixername
));
537 strlcpy(info
->components
, card
->components
, sizeof(info
->components
));
538 up_read(&snd_ioctl_rwsem
);
539 if (copy_to_user(arg
, info
, sizeof(struct snd_ctl_card_info
))) {
547 static int snd_ctl_elem_list(struct snd_card
*card
,
548 struct snd_ctl_elem_list __user
*_list
)
550 struct list_head
*plist
;
551 struct snd_ctl_elem_list list
;
552 struct snd_kcontrol
*kctl
;
553 struct snd_ctl_elem_id
*dst
, *id
;
554 unsigned int offset
, space
, first
, jidx
;
556 if (copy_from_user(&list
, _list
, sizeof(list
)))
558 offset
= list
.offset
;
561 /* try limit maximum space */
565 /* allocate temporary buffer for atomic operation */
566 dst
= vmalloc(space
* sizeof(struct snd_ctl_elem_id
));
569 down_read(&card
->controls_rwsem
);
570 list
.count
= card
->controls_count
;
571 plist
= card
->controls
.next
;
572 while (plist
!= &card
->controls
) {
575 kctl
= snd_kcontrol(plist
);
576 if (offset
< kctl
->count
)
578 offset
-= kctl
->count
;
583 while (space
> 0 && plist
!= &card
->controls
) {
584 kctl
= snd_kcontrol(plist
);
585 for (jidx
= offset
; space
> 0 && jidx
< kctl
->count
; jidx
++) {
586 snd_ctl_build_ioff(id
, kctl
, jidx
);
594 up_read(&card
->controls_rwsem
);
596 copy_to_user(list
.pids
, dst
,
597 list
.used
* sizeof(struct snd_ctl_elem_id
))) {
603 down_read(&card
->controls_rwsem
);
604 list
.count
= card
->controls_count
;
605 up_read(&card
->controls_rwsem
);
607 if (copy_to_user(_list
, &list
, sizeof(list
)))
612 static int snd_ctl_elem_info(struct snd_ctl_file
*ctl
,
613 struct snd_ctl_elem_info
*info
)
615 struct snd_card
*card
= ctl
->card
;
616 struct snd_kcontrol
*kctl
;
617 struct snd_kcontrol_volatile
*vd
;
618 unsigned int index_offset
;
621 down_read(&card
->controls_rwsem
);
622 kctl
= snd_ctl_find_id(card
, &info
->id
);
624 up_read(&card
->controls_rwsem
);
627 #ifdef CONFIG_SND_DEBUG
630 result
= kctl
->info(kctl
, info
);
632 snd_assert(info
->access
== 0, );
633 index_offset
= snd_ctl_get_ioff(kctl
, &info
->id
);
634 vd
= &kctl
->vd
[index_offset
];
635 snd_ctl_build_ioff(&info
->id
, kctl
, index_offset
);
636 info
->access
= vd
->access
;
638 info
->access
|= SNDRV_CTL_ELEM_ACCESS_LOCK
;
639 if (vd
->owner
== ctl
)
640 info
->access
|= SNDRV_CTL_ELEM_ACCESS_OWNER
;
641 info
->owner
= vd
->owner_pid
;
646 up_read(&card
->controls_rwsem
);
650 static int snd_ctl_elem_info_user(struct snd_ctl_file
*ctl
,
651 struct snd_ctl_elem_info __user
*_info
)
653 struct snd_ctl_elem_info info
;
656 if (copy_from_user(&info
, _info
, sizeof(info
)))
658 result
= snd_ctl_elem_info(ctl
, &info
);
660 if (copy_to_user(_info
, &info
, sizeof(info
)))
665 int snd_ctl_elem_read(struct snd_card
*card
, struct snd_ctl_elem_value
*control
)
667 struct snd_kcontrol
*kctl
;
668 struct snd_kcontrol_volatile
*vd
;
669 unsigned int index_offset
;
670 int result
, indirect
;
672 down_read(&card
->controls_rwsem
);
673 kctl
= snd_ctl_find_id(card
, &control
->id
);
677 index_offset
= snd_ctl_get_ioff(kctl
, &control
->id
);
678 vd
= &kctl
->vd
[index_offset
];
679 indirect
= vd
->access
& SNDRV_CTL_ELEM_ACCESS_INDIRECT
? 1 : 0;
680 if (control
->indirect
!= indirect
) {
683 if ((vd
->access
& SNDRV_CTL_ELEM_ACCESS_READ
) && kctl
->get
!= NULL
) {
684 snd_ctl_build_ioff(&control
->id
, kctl
, index_offset
);
685 result
= kctl
->get(kctl
, control
);
691 up_read(&card
->controls_rwsem
);
695 static int snd_ctl_elem_read_user(struct snd_card
*card
,
696 struct snd_ctl_elem_value __user
*_control
)
698 struct snd_ctl_elem_value
*control
;
701 control
= kmalloc(sizeof(*control
), GFP_KERNEL
);
704 if (copy_from_user(control
, _control
, sizeof(*control
))) {
708 result
= snd_ctl_elem_read(card
, control
);
710 if (copy_to_user(_control
, control
, sizeof(*control
)))
716 int snd_ctl_elem_write(struct snd_card
*card
, struct snd_ctl_file
*file
,
717 struct snd_ctl_elem_value
*control
)
719 struct snd_kcontrol
*kctl
;
720 struct snd_kcontrol_volatile
*vd
;
721 unsigned int index_offset
;
722 int result
, indirect
;
724 down_read(&card
->controls_rwsem
);
725 kctl
= snd_ctl_find_id(card
, &control
->id
);
729 index_offset
= snd_ctl_get_ioff(kctl
, &control
->id
);
730 vd
= &kctl
->vd
[index_offset
];
731 indirect
= vd
->access
& SNDRV_CTL_ELEM_ACCESS_INDIRECT
? 1 : 0;
732 if (control
->indirect
!= indirect
) {
735 if (!(vd
->access
& SNDRV_CTL_ELEM_ACCESS_WRITE
) ||
737 (file
&& vd
->owner
!= NULL
&& vd
->owner
!= file
)) {
740 snd_ctl_build_ioff(&control
->id
, kctl
, index_offset
);
741 result
= kctl
->put(kctl
, control
);
744 up_read(&card
->controls_rwsem
);
745 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_VALUE
, &control
->id
);
750 up_read(&card
->controls_rwsem
);
754 static int snd_ctl_elem_write_user(struct snd_ctl_file
*file
,
755 struct snd_ctl_elem_value __user
*_control
)
757 struct snd_ctl_elem_value
*control
;
760 control
= kmalloc(sizeof(*control
), GFP_KERNEL
);
763 if (copy_from_user(control
, _control
, sizeof(*control
))) {
767 result
= snd_ctl_elem_write(file
->card
, file
, control
);
769 if (copy_to_user(_control
, control
, sizeof(*control
)))
775 static int snd_ctl_elem_lock(struct snd_ctl_file
*file
,
776 struct snd_ctl_elem_id __user
*_id
)
778 struct snd_card
*card
= file
->card
;
779 struct snd_ctl_elem_id id
;
780 struct snd_kcontrol
*kctl
;
781 struct snd_kcontrol_volatile
*vd
;
784 if (copy_from_user(&id
, _id
, sizeof(id
)))
786 down_write(&card
->controls_rwsem
);
787 kctl
= snd_ctl_find_id(card
, &id
);
791 vd
= &kctl
->vd
[snd_ctl_get_ioff(kctl
, &id
)];
792 if (vd
->owner
!= NULL
)
796 vd
->owner_pid
= current
->pid
;
800 up_write(&card
->controls_rwsem
);
804 static int snd_ctl_elem_unlock(struct snd_ctl_file
*file
,
805 struct snd_ctl_elem_id __user
*_id
)
807 struct snd_card
*card
= file
->card
;
808 struct snd_ctl_elem_id id
;
809 struct snd_kcontrol
*kctl
;
810 struct snd_kcontrol_volatile
*vd
;
813 if (copy_from_user(&id
, _id
, sizeof(id
)))
815 down_write(&card
->controls_rwsem
);
816 kctl
= snd_ctl_find_id(card
, &id
);
820 vd
= &kctl
->vd
[snd_ctl_get_ioff(kctl
, &id
)];
821 if (vd
->owner
== NULL
)
823 else if (vd
->owner
!= file
)
831 up_write(&card
->controls_rwsem
);
835 struct user_element
{
836 struct snd_ctl_elem_info info
;
837 void *elem_data
; /* element data */
838 unsigned long elem_data_size
; /* size of element data in bytes */
839 void *priv_data
; /* private data (like strings for enumerated type) */
840 unsigned long priv_data_size
; /* size of private data in bytes */
843 static int snd_ctl_elem_user_info(struct snd_kcontrol
*kcontrol
,
844 struct snd_ctl_elem_info
*uinfo
)
846 struct user_element
*ue
= kcontrol
->private_data
;
852 static int snd_ctl_elem_user_get(struct snd_kcontrol
*kcontrol
,
853 struct snd_ctl_elem_value
*ucontrol
)
855 struct user_element
*ue
= kcontrol
->private_data
;
857 memcpy(&ucontrol
->value
, ue
->elem_data
, ue
->elem_data_size
);
861 static int snd_ctl_elem_user_put(struct snd_kcontrol
*kcontrol
,
862 struct snd_ctl_elem_value
*ucontrol
)
865 struct user_element
*ue
= kcontrol
->private_data
;
867 change
= memcmp(&ucontrol
->value
, ue
->elem_data
, ue
->elem_data_size
) != 0;
869 memcpy(ue
->elem_data
, &ucontrol
->value
, ue
->elem_data_size
);
873 static void snd_ctl_elem_user_free(struct snd_kcontrol
*kcontrol
)
875 kfree(kcontrol
->private_data
);
878 static int snd_ctl_elem_add(struct snd_ctl_file
*file
,
879 struct snd_ctl_elem_info
*info
, int replace
)
881 struct snd_card
*card
= file
->card
;
882 struct snd_kcontrol kctl
, *_kctl
;
885 struct user_element
*ue
;
888 if (card
->user_ctl_count
>= MAX_USER_CONTROLS
)
890 if (info
->count
> 1024)
892 access
= info
->access
== 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE
:
893 (info
->access
& (SNDRV_CTL_ELEM_ACCESS_READWRITE
|
894 SNDRV_CTL_ELEM_ACCESS_INACTIVE
));
896 memset(&kctl
, 0, sizeof(kctl
));
897 down_write(&card
->controls_rwsem
);
898 _kctl
= snd_ctl_find_id(card
, &info
->id
);
902 err
= snd_ctl_remove(card
, _kctl
);
909 up_write(&card
->controls_rwsem
);
912 memcpy(&kctl
.id
, &info
->id
, sizeof(info
->id
));
913 kctl
.count
= info
->owner
? info
->owner
: 1;
914 access
|= SNDRV_CTL_ELEM_ACCESS_USER
;
915 kctl
.info
= snd_ctl_elem_user_info
;
916 if (access
& SNDRV_CTL_ELEM_ACCESS_READ
)
917 kctl
.get
= snd_ctl_elem_user_get
;
918 if (access
& SNDRV_CTL_ELEM_ACCESS_WRITE
)
919 kctl
.put
= snd_ctl_elem_user_put
;
920 switch (info
->type
) {
921 case SNDRV_CTL_ELEM_TYPE_BOOLEAN
:
922 private_size
= sizeof(char);
923 if (info
->count
> 128)
926 case SNDRV_CTL_ELEM_TYPE_INTEGER
:
927 private_size
= sizeof(long);
928 if (info
->count
> 128)
931 case SNDRV_CTL_ELEM_TYPE_INTEGER64
:
932 private_size
= sizeof(long long);
933 if (info
->count
> 64)
936 case SNDRV_CTL_ELEM_TYPE_BYTES
:
937 private_size
= sizeof(unsigned char);
938 if (info
->count
> 512)
941 case SNDRV_CTL_ELEM_TYPE_IEC958
:
942 private_size
= sizeof(struct snd_aes_iec958
);
943 if (info
->count
!= 1)
949 private_size
*= info
->count
;
950 ue
= kzalloc(sizeof(struct user_element
) + private_size
, GFP_KERNEL
);
954 ue
->elem_data
= (char *)ue
+ sizeof(*ue
);
955 ue
->elem_data_size
= private_size
;
956 kctl
.private_free
= snd_ctl_elem_user_free
;
957 _kctl
= snd_ctl_new(&kctl
, access
);
959 kfree(_kctl
->private_data
);
962 _kctl
->private_data
= ue
;
963 for (idx
= 0; idx
< _kctl
->count
; idx
++)
964 _kctl
->vd
[idx
].owner
= file
;
965 err
= snd_ctl_add(card
, _kctl
);
967 snd_ctl_free_one(_kctl
);
971 down_write(&card
->controls_rwsem
);
972 card
->user_ctl_count
++;
973 up_write(&card
->controls_rwsem
);
978 static int snd_ctl_elem_add_user(struct snd_ctl_file
*file
,
979 struct snd_ctl_elem_info __user
*_info
, int replace
)
981 struct snd_ctl_elem_info info
;
982 if (copy_from_user(&info
, _info
, sizeof(info
)))
984 return snd_ctl_elem_add(file
, &info
, replace
);
987 static int snd_ctl_elem_remove(struct snd_ctl_file
*file
,
988 struct snd_ctl_elem_id __user
*_id
)
990 struct snd_ctl_elem_id id
;
993 if (copy_from_user(&id
, _id
, sizeof(id
)))
995 err
= snd_ctl_remove_unlocked_id(file
, &id
);
997 struct snd_card
*card
= file
->card
;
998 down_write(&card
->controls_rwsem
);
999 card
->user_ctl_count
--;
1000 up_write(&card
->controls_rwsem
);
1005 static int snd_ctl_subscribe_events(struct snd_ctl_file
*file
, int __user
*ptr
)
1008 if (get_user(subscribe
, ptr
))
1010 if (subscribe
< 0) {
1011 subscribe
= file
->subscribed
;
1012 if (put_user(subscribe
, ptr
))
1017 file
->subscribed
= 1;
1019 } else if (file
->subscribed
) {
1020 snd_ctl_empty_read_queue(file
);
1021 file
->subscribed
= 0;
1028 * change the power state
1030 static int snd_ctl_set_power_state(struct snd_card
*card
, unsigned int power_state
)
1032 switch (power_state
) {
1033 case SNDRV_CTL_POWER_D0
:
1034 if (card
->power_state
!= power_state
) {
1035 card
->pm_resume(card
);
1036 snd_power_change_state(card
, power_state
);
1039 case SNDRV_CTL_POWER_D3hot
:
1040 if (card
->power_state
!= power_state
) {
1041 card
->pm_suspend(card
, PMSG_SUSPEND
);
1042 snd_power_change_state(card
, power_state
);
1045 case SNDRV_CTL_POWER_D1
:
1046 case SNDRV_CTL_POWER_D2
:
1047 case SNDRV_CTL_POWER_D3cold
:
1048 /* not supported yet */
1056 static long snd_ctl_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1058 struct snd_ctl_file
*ctl
;
1059 struct snd_card
*card
;
1060 struct list_head
*list
;
1061 struct snd_kctl_ioctl
*p
;
1062 void __user
*argp
= (void __user
*)arg
;
1063 int __user
*ip
= argp
;
1066 ctl
= file
->private_data
;
1068 snd_assert(card
!= NULL
, return -ENXIO
);
1070 case SNDRV_CTL_IOCTL_PVERSION
:
1071 return put_user(SNDRV_CTL_VERSION
, ip
) ? -EFAULT
: 0;
1072 case SNDRV_CTL_IOCTL_CARD_INFO
:
1073 return snd_ctl_card_info(card
, ctl
, cmd
, argp
);
1074 case SNDRV_CTL_IOCTL_ELEM_LIST
:
1075 return snd_ctl_elem_list(ctl
->card
, argp
);
1076 case SNDRV_CTL_IOCTL_ELEM_INFO
:
1077 return snd_ctl_elem_info_user(ctl
, argp
);
1078 case SNDRV_CTL_IOCTL_ELEM_READ
:
1079 return snd_ctl_elem_read_user(ctl
->card
, argp
);
1080 case SNDRV_CTL_IOCTL_ELEM_WRITE
:
1081 return snd_ctl_elem_write_user(ctl
, argp
);
1082 case SNDRV_CTL_IOCTL_ELEM_LOCK
:
1083 return snd_ctl_elem_lock(ctl
, argp
);
1084 case SNDRV_CTL_IOCTL_ELEM_UNLOCK
:
1085 return snd_ctl_elem_unlock(ctl
, argp
);
1086 case SNDRV_CTL_IOCTL_ELEM_ADD
:
1087 return snd_ctl_elem_add_user(ctl
, argp
, 0);
1088 case SNDRV_CTL_IOCTL_ELEM_REPLACE
:
1089 return snd_ctl_elem_add_user(ctl
, argp
, 1);
1090 case SNDRV_CTL_IOCTL_ELEM_REMOVE
:
1091 return snd_ctl_elem_remove(ctl
, argp
);
1092 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS
:
1093 return snd_ctl_subscribe_events(ctl
, ip
);
1094 case SNDRV_CTL_IOCTL_POWER
:
1095 if (get_user(err
, ip
))
1097 if (!capable(CAP_SYS_ADMIN
))
1100 if (card
->pm_suspend
&& card
->pm_resume
) {
1101 snd_power_lock(card
);
1102 err
= snd_ctl_set_power_state(card
, err
);
1103 snd_power_unlock(card
);
1108 case SNDRV_CTL_IOCTL_POWER_STATE
:
1110 return put_user(card
->power_state
, ip
) ? -EFAULT
: 0;
1112 return put_user(SNDRV_CTL_POWER_D0
, ip
) ? -EFAULT
: 0;
1115 down_read(&snd_ioctl_rwsem
);
1116 list_for_each(list
, &snd_control_ioctls
) {
1117 p
= list_entry(list
, struct snd_kctl_ioctl
, list
);
1118 err
= p
->fioctl(card
, ctl
, cmd
, arg
);
1119 if (err
!= -ENOIOCTLCMD
) {
1120 up_read(&snd_ioctl_rwsem
);
1124 up_read(&snd_ioctl_rwsem
);
1125 snd_printdd("unknown ioctl = 0x%x\n", cmd
);
1129 static ssize_t
snd_ctl_read(struct file
*file
, char __user
*buffer
,
1130 size_t count
, loff_t
* offset
)
1132 struct snd_ctl_file
*ctl
;
1136 ctl
= file
->private_data
;
1137 snd_assert(ctl
!= NULL
&& ctl
->card
!= NULL
, return -ENXIO
);
1138 if (!ctl
->subscribed
)
1140 if (count
< sizeof(struct snd_ctl_event
))
1142 spin_lock_irq(&ctl
->read_lock
);
1143 while (count
>= sizeof(struct snd_ctl_event
)) {
1144 struct snd_ctl_event ev
;
1145 struct snd_kctl_event
*kev
;
1146 while (list_empty(&ctl
->events
)) {
1148 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
1152 init_waitqueue_entry(&wait
, current
);
1153 add_wait_queue(&ctl
->change_sleep
, &wait
);
1154 set_current_state(TASK_INTERRUPTIBLE
);
1155 spin_unlock_irq(&ctl
->read_lock
);
1157 remove_wait_queue(&ctl
->change_sleep
, &wait
);
1158 if (signal_pending(current
))
1159 return result
> 0 ? result
: -ERESTARTSYS
;
1160 spin_lock_irq(&ctl
->read_lock
);
1162 kev
= snd_kctl_event(ctl
->events
.next
);
1163 ev
.type
= SNDRV_CTL_EVENT_ELEM
;
1164 ev
.data
.elem
.mask
= kev
->mask
;
1165 ev
.data
.elem
.id
= kev
->id
;
1166 list_del(&kev
->list
);
1167 spin_unlock_irq(&ctl
->read_lock
);
1169 if (copy_to_user(buffer
, &ev
, sizeof(struct snd_ctl_event
))) {
1173 spin_lock_irq(&ctl
->read_lock
);
1174 buffer
+= sizeof(struct snd_ctl_event
);
1175 count
-= sizeof(struct snd_ctl_event
);
1176 result
+= sizeof(struct snd_ctl_event
);
1179 spin_unlock_irq(&ctl
->read_lock
);
1181 return result
> 0 ? result
: err
;
1184 static unsigned int snd_ctl_poll(struct file
*file
, poll_table
* wait
)
1187 struct snd_ctl_file
*ctl
;
1189 ctl
= file
->private_data
;
1190 if (!ctl
->subscribed
)
1192 poll_wait(file
, &ctl
->change_sleep
, wait
);
1195 if (!list_empty(&ctl
->events
))
1196 mask
|= POLLIN
| POLLRDNORM
;
1202 * register the device-specific control-ioctls.
1203 * called from each device manager like pcm.c, hwdep.c, etc.
1205 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn
, struct list_head
*lists
)
1207 struct snd_kctl_ioctl
*pn
;
1209 pn
= kzalloc(sizeof(struct snd_kctl_ioctl
), GFP_KERNEL
);
1213 down_write(&snd_ioctl_rwsem
);
1214 list_add_tail(&pn
->list
, lists
);
1215 up_write(&snd_ioctl_rwsem
);
1219 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn
)
1221 return _snd_ctl_register_ioctl(fcn
, &snd_control_ioctls
);
1224 #ifdef CONFIG_COMPAT
1225 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn
)
1227 return _snd_ctl_register_ioctl(fcn
, &snd_control_compat_ioctls
);
1232 * de-register the device-specific control-ioctls.
1234 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn
,
1235 struct list_head
*lists
)
1237 struct list_head
*list
;
1238 struct snd_kctl_ioctl
*p
;
1240 snd_assert(fcn
!= NULL
, return -EINVAL
);
1241 down_write(&snd_ioctl_rwsem
);
1242 list_for_each(list
, lists
) {
1243 p
= list_entry(list
, struct snd_kctl_ioctl
, list
);
1244 if (p
->fioctl
== fcn
) {
1246 up_write(&snd_ioctl_rwsem
);
1251 up_write(&snd_ioctl_rwsem
);
1256 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn
)
1258 return _snd_ctl_unregister_ioctl(fcn
, &snd_control_ioctls
);
1261 #ifdef CONFIG_COMPAT
1262 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn
)
1264 return _snd_ctl_unregister_ioctl(fcn
, &snd_control_compat_ioctls
);
1269 static int snd_ctl_fasync(int fd
, struct file
* file
, int on
)
1271 struct snd_ctl_file
*ctl
;
1273 ctl
= file
->private_data
;
1274 err
= fasync_helper(fd
, file
, on
, &ctl
->fasync
);
1283 #ifdef CONFIG_COMPAT
1284 #include "control_compat.c"
1286 #define snd_ctl_ioctl_compat NULL
1293 static struct file_operations snd_ctl_f_ops
=
1295 .owner
= THIS_MODULE
,
1296 .read
= snd_ctl_read
,
1297 .open
= snd_ctl_open
,
1298 .release
= snd_ctl_release
,
1299 .poll
= snd_ctl_poll
,
1300 .unlocked_ioctl
= snd_ctl_ioctl
,
1301 .compat_ioctl
= snd_ctl_ioctl_compat
,
1302 .fasync
= snd_ctl_fasync
,
1305 static struct snd_minor snd_ctl_reg
=
1308 .f_ops
= &snd_ctl_f_ops
,
1312 * registration of the control device
1314 static int snd_ctl_dev_register(struct snd_device
*device
)
1316 struct snd_card
*card
= device
->device_data
;
1320 snd_assert(card
!= NULL
, return -ENXIO
);
1321 cardnum
= card
->number
;
1322 snd_assert(cardnum
>= 0 && cardnum
< SNDRV_CARDS
, return -ENXIO
);
1323 sprintf(name
, "controlC%i", cardnum
);
1324 if ((err
= snd_register_device(SNDRV_DEVICE_TYPE_CONTROL
,
1325 card
, 0, &snd_ctl_reg
, name
)) < 0)
1331 * disconnection of the control device
1333 static int snd_ctl_dev_disconnect(struct snd_device
*device
)
1335 struct snd_card
*card
= device
->device_data
;
1336 struct list_head
*flist
;
1337 struct snd_ctl_file
*ctl
;
1339 down_read(&card
->controls_rwsem
);
1340 list_for_each(flist
, &card
->ctl_files
) {
1341 ctl
= snd_ctl_file(flist
);
1342 wake_up(&ctl
->change_sleep
);
1343 kill_fasync(&ctl
->fasync
, SIGIO
, POLL_ERR
);
1345 up_read(&card
->controls_rwsem
);
1352 static int snd_ctl_dev_free(struct snd_device
*device
)
1354 struct snd_card
*card
= device
->device_data
;
1355 struct snd_kcontrol
*control
;
1357 down_write(&card
->controls_rwsem
);
1358 while (!list_empty(&card
->controls
)) {
1359 control
= snd_kcontrol(card
->controls
.next
);
1360 snd_ctl_remove(card
, control
);
1362 up_write(&card
->controls_rwsem
);
1367 * de-registration of the control device
1369 static int snd_ctl_dev_unregister(struct snd_device
*device
)
1371 struct snd_card
*card
= device
->device_data
;
1374 snd_assert(card
!= NULL
, return -ENXIO
);
1375 cardnum
= card
->number
;
1376 snd_assert(cardnum
>= 0 && cardnum
< SNDRV_CARDS
, return -ENXIO
);
1377 if ((err
= snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL
, card
, 0)) < 0)
1379 return snd_ctl_dev_free(device
);
1383 * create control core:
1384 * called from init.c
1386 int snd_ctl_create(struct snd_card
*card
)
1388 static struct snd_device_ops ops
= {
1389 .dev_free
= snd_ctl_dev_free
,
1390 .dev_register
= snd_ctl_dev_register
,
1391 .dev_disconnect
= snd_ctl_dev_disconnect
,
1392 .dev_unregister
= snd_ctl_dev_unregister
1395 snd_assert(card
!= NULL
, return -ENXIO
);
1396 return snd_device_new(card
, SNDRV_DEV_CONTROL
, card
, &ops
);