2 * Routines for driver control interface
3 * Copyright (c) by Jaroslav Kysela <perex@perex.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 <linux/threads.h>
23 #include <linux/interrupt.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
27 #include <linux/time.h>
28 #include <sound/core.h>
29 #include <sound/minors.h>
30 #include <sound/info.h>
31 #include <sound/control.h>
33 /* max number of user-defined controls */
34 #define MAX_USER_CONTROLS 32
35 #define MAX_CONTROL_COUNT 1028
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
)
51 struct snd_card
*card
;
52 struct snd_ctl_file
*ctl
;
55 err
= nonseekable_open(inode
, file
);
59 card
= snd_lookup_minor_data(iminor(inode
), SNDRV_DEVICE_TYPE_CONTROL
);
64 err
= snd_card_file_add(card
, file
);
69 if (!try_module_get(card
->module
)) {
73 ctl
= kzalloc(sizeof(*ctl
), GFP_KERNEL
);
78 INIT_LIST_HEAD(&ctl
->events
);
79 init_waitqueue_head(&ctl
->change_sleep
);
80 spin_lock_init(&ctl
->read_lock
);
82 ctl
->prefer_pcm_subdevice
= -1;
83 ctl
->prefer_rawmidi_subdevice
= -1;
84 ctl
->pid
= get_pid(task_pid(current
));
85 file
->private_data
= ctl
;
86 write_lock_irqsave(&card
->ctl_files_rwlock
, flags
);
87 list_add_tail(&ctl
->list
, &card
->ctl_files
);
88 write_unlock_irqrestore(&card
->ctl_files_rwlock
, flags
);
93 module_put(card
->module
);
95 snd_card_file_remove(card
, file
);
102 static void snd_ctl_empty_read_queue(struct snd_ctl_file
* ctl
)
105 struct snd_kctl_event
*cread
;
107 spin_lock_irqsave(&ctl
->read_lock
, flags
);
108 while (!list_empty(&ctl
->events
)) {
109 cread
= snd_kctl_event(ctl
->events
.next
);
110 list_del(&cread
->list
);
113 spin_unlock_irqrestore(&ctl
->read_lock
, flags
);
116 static int snd_ctl_release(struct inode
*inode
, struct file
*file
)
119 struct snd_card
*card
;
120 struct snd_ctl_file
*ctl
;
121 struct snd_kcontrol
*control
;
124 ctl
= file
->private_data
;
125 file
->private_data
= NULL
;
127 write_lock_irqsave(&card
->ctl_files_rwlock
, flags
);
128 list_del(&ctl
->list
);
129 write_unlock_irqrestore(&card
->ctl_files_rwlock
, flags
);
130 down_write(&card
->controls_rwsem
);
131 list_for_each_entry(control
, &card
->controls
, list
)
132 for (idx
= 0; idx
< control
->count
; idx
++)
133 if (control
->vd
[idx
].owner
== ctl
)
134 control
->vd
[idx
].owner
= NULL
;
135 up_write(&card
->controls_rwsem
);
136 snd_ctl_empty_read_queue(ctl
);
139 module_put(card
->module
);
140 snd_card_file_remove(card
, file
);
144 void snd_ctl_notify(struct snd_card
*card
, unsigned int mask
,
145 struct snd_ctl_elem_id
*id
)
148 struct snd_ctl_file
*ctl
;
149 struct snd_kctl_event
*ev
;
151 if (snd_BUG_ON(!card
|| !id
))
155 read_lock(&card
->ctl_files_rwlock
);
156 #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
157 card
->mixer_oss_change_count
++;
159 list_for_each_entry(ctl
, &card
->ctl_files
, list
) {
160 if (!ctl
->subscribed
)
162 spin_lock_irqsave(&ctl
->read_lock
, flags
);
163 list_for_each_entry(ev
, &ctl
->events
, list
) {
164 if (ev
->id
.numid
== id
->numid
) {
169 ev
= kzalloc(sizeof(*ev
), GFP_ATOMIC
);
173 list_add_tail(&ev
->list
, &ctl
->events
);
175 dev_err(card
->dev
, "No memory available to allocate event\n");
178 wake_up(&ctl
->change_sleep
);
179 spin_unlock_irqrestore(&ctl
->read_lock
, flags
);
180 kill_fasync(&ctl
->fasync
, SIGIO
, POLL_IN
);
182 read_unlock(&card
->ctl_files_rwlock
);
185 EXPORT_SYMBOL(snd_ctl_notify
);
188 * snd_ctl_new - create a control instance from the template
189 * @control: the control template
190 * @access: the default control access
192 * Allocates a new struct snd_kcontrol instance and copies the given template
193 * to the new instance. It does not copy volatile data (access).
195 * Return: The pointer of the new instance, or %NULL on failure.
197 static struct snd_kcontrol
*snd_ctl_new(struct snd_kcontrol
*control
,
200 struct snd_kcontrol
*kctl
;
203 if (snd_BUG_ON(!control
|| !control
->count
))
206 if (control
->count
> MAX_CONTROL_COUNT
)
209 kctl
= kzalloc(sizeof(*kctl
) + sizeof(struct snd_kcontrol_volatile
) * control
->count
, GFP_KERNEL
);
211 pr_err("ALSA: Cannot allocate control instance\n");
215 for (idx
= 0; idx
< kctl
->count
; idx
++)
216 kctl
->vd
[idx
].access
= access
;
221 * snd_ctl_new1 - create a control instance from the template
222 * @ncontrol: the initialization record
223 * @private_data: the private data to set
225 * Allocates a new struct snd_kcontrol instance and initialize from the given
226 * template. When the access field of ncontrol is 0, it's assumed as
227 * READWRITE access. When the count field is 0, it's assumes as one.
229 * Return: The pointer of the newly generated instance, or %NULL on failure.
231 struct snd_kcontrol
*snd_ctl_new1(const struct snd_kcontrol_new
*ncontrol
,
234 struct snd_kcontrol kctl
;
237 if (snd_BUG_ON(!ncontrol
|| !ncontrol
->info
))
239 memset(&kctl
, 0, sizeof(kctl
));
240 kctl
.id
.iface
= ncontrol
->iface
;
241 kctl
.id
.device
= ncontrol
->device
;
242 kctl
.id
.subdevice
= ncontrol
->subdevice
;
243 if (ncontrol
->name
) {
244 strlcpy(kctl
.id
.name
, ncontrol
->name
, sizeof(kctl
.id
.name
));
245 if (strcmp(ncontrol
->name
, kctl
.id
.name
) != 0)
246 pr_warn("ALSA: Control name '%s' truncated to '%s'\n",
247 ncontrol
->name
, kctl
.id
.name
);
249 kctl
.id
.index
= ncontrol
->index
;
250 kctl
.count
= ncontrol
->count
? ncontrol
->count
: 1;
251 access
= ncontrol
->access
== 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE
:
252 (ncontrol
->access
& (SNDRV_CTL_ELEM_ACCESS_READWRITE
|
253 SNDRV_CTL_ELEM_ACCESS_VOLATILE
|
254 SNDRV_CTL_ELEM_ACCESS_INACTIVE
|
255 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
|
256 SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND
|
257 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
));
258 kctl
.info
= ncontrol
->info
;
259 kctl
.get
= ncontrol
->get
;
260 kctl
.put
= ncontrol
->put
;
261 kctl
.tlv
.p
= ncontrol
->tlv
.p
;
262 kctl
.private_value
= ncontrol
->private_value
;
263 kctl
.private_data
= private_data
;
264 return snd_ctl_new(&kctl
, access
);
267 EXPORT_SYMBOL(snd_ctl_new1
);
270 * snd_ctl_free_one - release the control instance
271 * @kcontrol: the control instance
273 * Releases the control instance created via snd_ctl_new()
275 * Don't call this after the control was added to the card.
277 void snd_ctl_free_one(struct snd_kcontrol
*kcontrol
)
280 if (kcontrol
->private_free
)
281 kcontrol
->private_free(kcontrol
);
286 EXPORT_SYMBOL(snd_ctl_free_one
);
288 static bool snd_ctl_remove_numid_conflict(struct snd_card
*card
,
291 struct snd_kcontrol
*kctl
;
293 /* Make sure that the ids assigned to the control do not wrap around */
294 if (card
->last_numid
>= UINT_MAX
- count
)
295 card
->last_numid
= 0;
297 list_for_each_entry(kctl
, &card
->controls
, list
) {
298 if (kctl
->id
.numid
< card
->last_numid
+ 1 + count
&&
299 kctl
->id
.numid
+ kctl
->count
> card
->last_numid
+ 1) {
300 card
->last_numid
= kctl
->id
.numid
+ kctl
->count
- 1;
307 static int snd_ctl_find_hole(struct snd_card
*card
, unsigned int count
)
309 unsigned int iter
= 100000;
311 while (snd_ctl_remove_numid_conflict(card
, count
)) {
313 /* this situation is very unlikely */
314 dev_err(card
->dev
, "unable to allocate new control numid\n");
322 * snd_ctl_add - add the control instance to the card
323 * @card: the card instance
324 * @kcontrol: the control instance to add
326 * Adds the control instance created via snd_ctl_new() or
327 * snd_ctl_new1() to the given card. Assigns also an unique
328 * numid used for fast search.
330 * It frees automatically the control which cannot be added.
332 * Return: Zero if successful, or a negative error code on failure.
335 int snd_ctl_add(struct snd_card
*card
, struct snd_kcontrol
*kcontrol
)
337 struct snd_ctl_elem_id id
;
344 if (snd_BUG_ON(!card
|| !kcontrol
->info
))
347 if (id
.index
> UINT_MAX
- kcontrol
->count
)
350 down_write(&card
->controls_rwsem
);
351 if (snd_ctl_find_id(card
, &id
)) {
352 up_write(&card
->controls_rwsem
);
353 dev_err(card
->dev
, "control %i:%i:%i:%s:%i is already present\n",
362 if (snd_ctl_find_hole(card
, kcontrol
->count
) < 0) {
363 up_write(&card
->controls_rwsem
);
367 list_add_tail(&kcontrol
->list
, &card
->controls
);
368 card
->controls_count
+= kcontrol
->count
;
369 kcontrol
->id
.numid
= card
->last_numid
+ 1;
370 card
->last_numid
+= kcontrol
->count
;
371 count
= kcontrol
->count
;
372 up_write(&card
->controls_rwsem
);
373 for (idx
= 0; idx
< count
; idx
++, id
.index
++, id
.numid
++)
374 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_ADD
, &id
);
378 snd_ctl_free_one(kcontrol
);
382 EXPORT_SYMBOL(snd_ctl_add
);
385 * snd_ctl_replace - replace the control instance of the card
386 * @card: the card instance
387 * @kcontrol: the control instance to replace
388 * @add_on_replace: add the control if not already added
390 * Replaces the given control. If the given control does not exist
391 * and the add_on_replace flag is set, the control is added. If the
392 * control exists, it is destroyed first.
394 * It frees automatically the control which cannot be added or replaced.
396 * Return: Zero if successful, or a negative error code on failure.
398 int snd_ctl_replace(struct snd_card
*card
, struct snd_kcontrol
*kcontrol
,
401 struct snd_ctl_elem_id id
;
404 struct snd_kcontrol
*old
;
409 if (snd_BUG_ON(!card
|| !kcontrol
->info
)) {
414 down_write(&card
->controls_rwsem
);
415 old
= snd_ctl_find_id(card
, &id
);
419 up_write(&card
->controls_rwsem
);
423 ret
= snd_ctl_remove(card
, old
);
425 up_write(&card
->controls_rwsem
);
429 if (snd_ctl_find_hole(card
, kcontrol
->count
) < 0) {
430 up_write(&card
->controls_rwsem
);
434 list_add_tail(&kcontrol
->list
, &card
->controls
);
435 card
->controls_count
+= kcontrol
->count
;
436 kcontrol
->id
.numid
= card
->last_numid
+ 1;
437 card
->last_numid
+= kcontrol
->count
;
438 count
= kcontrol
->count
;
439 up_write(&card
->controls_rwsem
);
440 for (idx
= 0; idx
< count
; idx
++, id
.index
++, id
.numid
++)
441 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_ADD
, &id
);
445 snd_ctl_free_one(kcontrol
);
448 EXPORT_SYMBOL(snd_ctl_replace
);
451 * snd_ctl_remove - remove the control from the card and release it
452 * @card: the card instance
453 * @kcontrol: the control instance to remove
455 * Removes the control from the card and then releases the instance.
456 * You don't need to call snd_ctl_free_one(). You must be in
457 * the write lock - down_write(&card->controls_rwsem).
459 * Return: 0 if successful, or a negative error code on failure.
461 int snd_ctl_remove(struct snd_card
*card
, struct snd_kcontrol
*kcontrol
)
463 struct snd_ctl_elem_id id
;
466 if (snd_BUG_ON(!card
|| !kcontrol
))
468 list_del(&kcontrol
->list
);
469 card
->controls_count
-= kcontrol
->count
;
471 for (idx
= 0; idx
< kcontrol
->count
; idx
++, id
.index
++, id
.numid
++)
472 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_REMOVE
, &id
);
473 snd_ctl_free_one(kcontrol
);
477 EXPORT_SYMBOL(snd_ctl_remove
);
480 * snd_ctl_remove_id - remove the control of the given id and release it
481 * @card: the card instance
482 * @id: the control id to remove
484 * Finds the control instance with the given id, removes it from the
485 * card list and releases it.
487 * Return: 0 if successful, or a negative error code on failure.
489 int snd_ctl_remove_id(struct snd_card
*card
, struct snd_ctl_elem_id
*id
)
491 struct snd_kcontrol
*kctl
;
494 down_write(&card
->controls_rwsem
);
495 kctl
= snd_ctl_find_id(card
, id
);
497 up_write(&card
->controls_rwsem
);
500 ret
= snd_ctl_remove(card
, kctl
);
501 up_write(&card
->controls_rwsem
);
505 EXPORT_SYMBOL(snd_ctl_remove_id
);
508 * snd_ctl_remove_user_ctl - remove and release the unlocked user control
509 * @file: active control handle
510 * @id: the control id to remove
512 * Finds the control instance with the given id, removes it from the
513 * card list and releases it.
515 * Return: 0 if successful, or a negative error code on failure.
517 static int snd_ctl_remove_user_ctl(struct snd_ctl_file
* file
,
518 struct snd_ctl_elem_id
*id
)
520 struct snd_card
*card
= file
->card
;
521 struct snd_kcontrol
*kctl
;
524 down_write(&card
->controls_rwsem
);
525 kctl
= snd_ctl_find_id(card
, id
);
530 if (!(kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_USER
)) {
534 for (idx
= 0; idx
< kctl
->count
; idx
++)
535 if (kctl
->vd
[idx
].owner
!= NULL
&& kctl
->vd
[idx
].owner
!= file
) {
539 ret
= snd_ctl_remove(card
, kctl
);
542 card
->user_ctl_count
--;
544 up_write(&card
->controls_rwsem
);
549 * snd_ctl_activate_id - activate/inactivate the control of the given id
550 * @card: the card instance
551 * @id: the control id to activate/inactivate
552 * @active: non-zero to activate
554 * Finds the control instance with the given id, and activate or
555 * inactivate the control together with notification, if changed.
557 * Return: 0 if unchanged, 1 if changed, or a negative error code on failure.
559 int snd_ctl_activate_id(struct snd_card
*card
, struct snd_ctl_elem_id
*id
,
562 struct snd_kcontrol
*kctl
;
563 struct snd_kcontrol_volatile
*vd
;
564 unsigned int index_offset
;
567 down_write(&card
->controls_rwsem
);
568 kctl
= snd_ctl_find_id(card
, id
);
573 index_offset
= snd_ctl_get_ioff(kctl
, &kctl
->id
);
574 vd
= &kctl
->vd
[index_offset
];
577 if (!(vd
->access
& SNDRV_CTL_ELEM_ACCESS_INACTIVE
))
579 vd
->access
&= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE
;
581 if (vd
->access
& SNDRV_CTL_ELEM_ACCESS_INACTIVE
)
583 vd
->access
|= SNDRV_CTL_ELEM_ACCESS_INACTIVE
;
587 up_write(&card
->controls_rwsem
);
589 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_INFO
, id
);
592 EXPORT_SYMBOL_GPL(snd_ctl_activate_id
);
595 * snd_ctl_rename_id - replace the id of a control on the card
596 * @card: the card instance
597 * @src_id: the old id
598 * @dst_id: the new id
600 * Finds the control with the old id from the card, and replaces the
601 * id with the new one.
603 * Return: Zero if successful, or a negative error code on failure.
605 int snd_ctl_rename_id(struct snd_card
*card
, struct snd_ctl_elem_id
*src_id
,
606 struct snd_ctl_elem_id
*dst_id
)
608 struct snd_kcontrol
*kctl
;
610 down_write(&card
->controls_rwsem
);
611 kctl
= snd_ctl_find_id(card
, src_id
);
613 up_write(&card
->controls_rwsem
);
617 kctl
->id
.numid
= card
->last_numid
+ 1;
618 card
->last_numid
+= kctl
->count
;
619 up_write(&card
->controls_rwsem
);
623 EXPORT_SYMBOL(snd_ctl_rename_id
);
626 * snd_ctl_find_numid - find the control instance with the given number-id
627 * @card: the card instance
628 * @numid: the number-id to search
630 * Finds the control instance with the given number-id from the card.
632 * The caller must down card->controls_rwsem before calling this function
633 * (if the race condition can happen).
635 * Return: The pointer of the instance if found, or %NULL if not.
638 struct snd_kcontrol
*snd_ctl_find_numid(struct snd_card
*card
, unsigned int numid
)
640 struct snd_kcontrol
*kctl
;
642 if (snd_BUG_ON(!card
|| !numid
))
644 list_for_each_entry(kctl
, &card
->controls
, list
) {
645 if (kctl
->id
.numid
<= numid
&& kctl
->id
.numid
+ kctl
->count
> numid
)
651 EXPORT_SYMBOL(snd_ctl_find_numid
);
654 * snd_ctl_find_id - find the control instance with the given id
655 * @card: the card instance
656 * @id: the id to search
658 * Finds the control instance with the given id from the card.
660 * The caller must down card->controls_rwsem before calling this function
661 * (if the race condition can happen).
663 * Return: The pointer of the instance if found, or %NULL if not.
666 struct snd_kcontrol
*snd_ctl_find_id(struct snd_card
*card
,
667 struct snd_ctl_elem_id
*id
)
669 struct snd_kcontrol
*kctl
;
671 if (snd_BUG_ON(!card
|| !id
))
674 return snd_ctl_find_numid(card
, id
->numid
);
675 list_for_each_entry(kctl
, &card
->controls
, list
) {
676 if (kctl
->id
.iface
!= id
->iface
)
678 if (kctl
->id
.device
!= id
->device
)
680 if (kctl
->id
.subdevice
!= id
->subdevice
)
682 if (strncmp(kctl
->id
.name
, id
->name
, sizeof(kctl
->id
.name
)))
684 if (kctl
->id
.index
> id
->index
)
686 if (kctl
->id
.index
+ kctl
->count
<= id
->index
)
693 EXPORT_SYMBOL(snd_ctl_find_id
);
695 static int snd_ctl_card_info(struct snd_card
*card
, struct snd_ctl_file
* ctl
,
696 unsigned int cmd
, void __user
*arg
)
698 struct snd_ctl_card_info
*info
;
700 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
703 down_read(&snd_ioctl_rwsem
);
704 info
->card
= card
->number
;
705 strlcpy(info
->id
, card
->id
, sizeof(info
->id
));
706 strlcpy(info
->driver
, card
->driver
, sizeof(info
->driver
));
707 strlcpy(info
->name
, card
->shortname
, sizeof(info
->name
));
708 strlcpy(info
->longname
, card
->longname
, sizeof(info
->longname
));
709 strlcpy(info
->mixername
, card
->mixername
, sizeof(info
->mixername
));
710 strlcpy(info
->components
, card
->components
, sizeof(info
->components
));
711 up_read(&snd_ioctl_rwsem
);
712 if (copy_to_user(arg
, info
, sizeof(struct snd_ctl_card_info
))) {
720 static int snd_ctl_elem_list(struct snd_card
*card
,
721 struct snd_ctl_elem_list __user
*_list
)
723 struct list_head
*plist
;
724 struct snd_ctl_elem_list list
;
725 struct snd_kcontrol
*kctl
;
726 struct snd_ctl_elem_id
*dst
, *id
;
727 unsigned int offset
, space
, jidx
;
729 if (copy_from_user(&list
, _list
, sizeof(list
)))
731 offset
= list
.offset
;
733 /* try limit maximum space */
737 /* allocate temporary buffer for atomic operation */
738 dst
= vmalloc(space
* sizeof(struct snd_ctl_elem_id
));
741 down_read(&card
->controls_rwsem
);
742 list
.count
= card
->controls_count
;
743 plist
= card
->controls
.next
;
744 while (plist
!= &card
->controls
) {
747 kctl
= snd_kcontrol(plist
);
748 if (offset
< kctl
->count
)
750 offset
-= kctl
->count
;
755 while (space
> 0 && plist
!= &card
->controls
) {
756 kctl
= snd_kcontrol(plist
);
757 for (jidx
= offset
; space
> 0 && jidx
< kctl
->count
; jidx
++) {
758 snd_ctl_build_ioff(id
, kctl
, jidx
);
766 up_read(&card
->controls_rwsem
);
768 copy_to_user(list
.pids
, dst
,
769 list
.used
* sizeof(struct snd_ctl_elem_id
))) {
775 down_read(&card
->controls_rwsem
);
776 list
.count
= card
->controls_count
;
777 up_read(&card
->controls_rwsem
);
779 if (copy_to_user(_list
, &list
, sizeof(list
)))
784 static int snd_ctl_elem_info(struct snd_ctl_file
*ctl
,
785 struct snd_ctl_elem_info
*info
)
787 struct snd_card
*card
= ctl
->card
;
788 struct snd_kcontrol
*kctl
;
789 struct snd_kcontrol_volatile
*vd
;
790 unsigned int index_offset
;
793 down_read(&card
->controls_rwsem
);
794 kctl
= snd_ctl_find_id(card
, &info
->id
);
796 up_read(&card
->controls_rwsem
);
799 #ifdef CONFIG_SND_DEBUG
802 result
= kctl
->info(kctl
, info
);
804 snd_BUG_ON(info
->access
);
805 index_offset
= snd_ctl_get_ioff(kctl
, &info
->id
);
806 vd
= &kctl
->vd
[index_offset
];
807 snd_ctl_build_ioff(&info
->id
, kctl
, index_offset
);
808 info
->access
= vd
->access
;
810 info
->access
|= SNDRV_CTL_ELEM_ACCESS_LOCK
;
811 if (vd
->owner
== ctl
)
812 info
->access
|= SNDRV_CTL_ELEM_ACCESS_OWNER
;
813 info
->owner
= pid_vnr(vd
->owner
->pid
);
818 up_read(&card
->controls_rwsem
);
822 static int snd_ctl_elem_info_user(struct snd_ctl_file
*ctl
,
823 struct snd_ctl_elem_info __user
*_info
)
825 struct snd_ctl_elem_info info
;
828 if (copy_from_user(&info
, _info
, sizeof(info
)))
830 snd_power_lock(ctl
->card
);
831 result
= snd_power_wait(ctl
->card
, SNDRV_CTL_POWER_D0
);
833 result
= snd_ctl_elem_info(ctl
, &info
);
834 snd_power_unlock(ctl
->card
);
836 if (copy_to_user(_info
, &info
, sizeof(info
)))
841 static int snd_ctl_elem_read(struct snd_card
*card
,
842 struct snd_ctl_elem_value
*control
)
844 struct snd_kcontrol
*kctl
;
845 struct snd_kcontrol_volatile
*vd
;
846 unsigned int index_offset
;
849 down_read(&card
->controls_rwsem
);
850 kctl
= snd_ctl_find_id(card
, &control
->id
);
854 index_offset
= snd_ctl_get_ioff(kctl
, &control
->id
);
855 vd
= &kctl
->vd
[index_offset
];
856 if ((vd
->access
& SNDRV_CTL_ELEM_ACCESS_READ
) &&
858 snd_ctl_build_ioff(&control
->id
, kctl
, index_offset
);
859 result
= kctl
->get(kctl
, control
);
863 up_read(&card
->controls_rwsem
);
867 static int snd_ctl_elem_read_user(struct snd_card
*card
,
868 struct snd_ctl_elem_value __user
*_control
)
870 struct snd_ctl_elem_value
*control
;
873 control
= memdup_user(_control
, sizeof(*control
));
875 return PTR_ERR(control
);
877 snd_power_lock(card
);
878 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
880 result
= snd_ctl_elem_read(card
, control
);
881 snd_power_unlock(card
);
883 if (copy_to_user(_control
, control
, sizeof(*control
)))
889 static int snd_ctl_elem_write(struct snd_card
*card
, struct snd_ctl_file
*file
,
890 struct snd_ctl_elem_value
*control
)
892 struct snd_kcontrol
*kctl
;
893 struct snd_kcontrol_volatile
*vd
;
894 unsigned int index_offset
;
897 down_read(&card
->controls_rwsem
);
898 kctl
= snd_ctl_find_id(card
, &control
->id
);
902 index_offset
= snd_ctl_get_ioff(kctl
, &control
->id
);
903 vd
= &kctl
->vd
[index_offset
];
904 if (!(vd
->access
& SNDRV_CTL_ELEM_ACCESS_WRITE
) ||
906 (file
&& vd
->owner
&& vd
->owner
!= file
)) {
909 snd_ctl_build_ioff(&control
->id
, kctl
, index_offset
);
910 result
= kctl
->put(kctl
, control
);
913 struct snd_ctl_elem_id id
= control
->id
;
914 up_read(&card
->controls_rwsem
);
915 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_VALUE
, &id
);
919 up_read(&card
->controls_rwsem
);
923 static int snd_ctl_elem_write_user(struct snd_ctl_file
*file
,
924 struct snd_ctl_elem_value __user
*_control
)
926 struct snd_ctl_elem_value
*control
;
927 struct snd_card
*card
;
930 control
= memdup_user(_control
, sizeof(*control
));
932 return PTR_ERR(control
);
935 snd_power_lock(card
);
936 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
938 result
= snd_ctl_elem_write(card
, file
, control
);
939 snd_power_unlock(card
);
941 if (copy_to_user(_control
, control
, sizeof(*control
)))
947 static int snd_ctl_elem_lock(struct snd_ctl_file
*file
,
948 struct snd_ctl_elem_id __user
*_id
)
950 struct snd_card
*card
= file
->card
;
951 struct snd_ctl_elem_id id
;
952 struct snd_kcontrol
*kctl
;
953 struct snd_kcontrol_volatile
*vd
;
956 if (copy_from_user(&id
, _id
, sizeof(id
)))
958 down_write(&card
->controls_rwsem
);
959 kctl
= snd_ctl_find_id(card
, &id
);
963 vd
= &kctl
->vd
[snd_ctl_get_ioff(kctl
, &id
)];
964 if (vd
->owner
!= NULL
)
971 up_write(&card
->controls_rwsem
);
975 static int snd_ctl_elem_unlock(struct snd_ctl_file
*file
,
976 struct snd_ctl_elem_id __user
*_id
)
978 struct snd_card
*card
= file
->card
;
979 struct snd_ctl_elem_id id
;
980 struct snd_kcontrol
*kctl
;
981 struct snd_kcontrol_volatile
*vd
;
984 if (copy_from_user(&id
, _id
, sizeof(id
)))
986 down_write(&card
->controls_rwsem
);
987 kctl
= snd_ctl_find_id(card
, &id
);
991 vd
= &kctl
->vd
[snd_ctl_get_ioff(kctl
, &id
)];
992 if (vd
->owner
== NULL
)
994 else if (vd
->owner
!= file
)
1001 up_write(&card
->controls_rwsem
);
1005 struct user_element
{
1006 struct snd_ctl_elem_info info
;
1007 struct snd_card
*card
;
1008 void *elem_data
; /* element data */
1009 unsigned long elem_data_size
; /* size of element data in bytes */
1010 void *tlv_data
; /* TLV data */
1011 unsigned long tlv_data_size
; /* TLV data size */
1012 void *priv_data
; /* private data (like strings for enumerated type) */
1015 static int snd_ctl_elem_user_info(struct snd_kcontrol
*kcontrol
,
1016 struct snd_ctl_elem_info
*uinfo
)
1018 struct user_element
*ue
= kcontrol
->private_data
;
1024 static int snd_ctl_elem_user_enum_info(struct snd_kcontrol
*kcontrol
,
1025 struct snd_ctl_elem_info
*uinfo
)
1027 struct user_element
*ue
= kcontrol
->private_data
;
1031 item
= uinfo
->value
.enumerated
.item
;
1035 item
= min(item
, uinfo
->value
.enumerated
.items
- 1);
1036 uinfo
->value
.enumerated
.item
= item
;
1038 names
= ue
->priv_data
;
1039 for (; item
> 0; --item
)
1040 names
+= strlen(names
) + 1;
1041 strcpy(uinfo
->value
.enumerated
.name
, names
);
1046 static int snd_ctl_elem_user_get(struct snd_kcontrol
*kcontrol
,
1047 struct snd_ctl_elem_value
*ucontrol
)
1049 struct user_element
*ue
= kcontrol
->private_data
;
1051 mutex_lock(&ue
->card
->user_ctl_lock
);
1052 memcpy(&ucontrol
->value
, ue
->elem_data
, ue
->elem_data_size
);
1053 mutex_unlock(&ue
->card
->user_ctl_lock
);
1057 static int snd_ctl_elem_user_put(struct snd_kcontrol
*kcontrol
,
1058 struct snd_ctl_elem_value
*ucontrol
)
1061 struct user_element
*ue
= kcontrol
->private_data
;
1063 mutex_lock(&ue
->card
->user_ctl_lock
);
1064 change
= memcmp(&ucontrol
->value
, ue
->elem_data
, ue
->elem_data_size
) != 0;
1066 memcpy(ue
->elem_data
, &ucontrol
->value
, ue
->elem_data_size
);
1067 mutex_unlock(&ue
->card
->user_ctl_lock
);
1071 static int snd_ctl_elem_user_tlv(struct snd_kcontrol
*kcontrol
,
1074 unsigned int __user
*tlv
)
1076 struct user_element
*ue
= kcontrol
->private_data
;
1081 if (size
> 1024 * 128) /* sane value */
1084 new_data
= memdup_user(tlv
, size
);
1085 if (IS_ERR(new_data
))
1086 return PTR_ERR(new_data
);
1087 mutex_lock(&ue
->card
->user_ctl_lock
);
1088 change
= ue
->tlv_data_size
!= size
;
1090 change
= memcmp(ue
->tlv_data
, new_data
, size
) != 0;
1091 kfree(ue
->tlv_data
);
1092 ue
->tlv_data
= new_data
;
1093 ue
->tlv_data_size
= size
;
1094 mutex_unlock(&ue
->card
->user_ctl_lock
);
1098 mutex_lock(&ue
->card
->user_ctl_lock
);
1099 if (!ue
->tlv_data_size
|| !ue
->tlv_data
) {
1103 if (size
< ue
->tlv_data_size
) {
1107 if (copy_to_user(tlv
, ue
->tlv_data
, ue
->tlv_data_size
))
1110 mutex_unlock(&ue
->card
->user_ctl_lock
);
1117 static int snd_ctl_elem_init_enum_names(struct user_element
*ue
)
1120 size_t buf_len
, name_len
;
1122 const uintptr_t user_ptrval
= ue
->info
.value
.enumerated
.names_ptr
;
1124 if (ue
->info
.value
.enumerated
.names_length
> 64 * 1024)
1127 names
= memdup_user((const void __user
*)user_ptrval
,
1128 ue
->info
.value
.enumerated
.names_length
);
1130 return PTR_ERR(names
);
1132 /* check that there are enough valid names */
1133 buf_len
= ue
->info
.value
.enumerated
.names_length
;
1135 for (i
= 0; i
< ue
->info
.value
.enumerated
.items
; ++i
) {
1136 name_len
= strnlen(p
, buf_len
);
1137 if (name_len
== 0 || name_len
>= 64 || name_len
== buf_len
) {
1142 buf_len
-= name_len
+ 1;
1145 ue
->priv_data
= names
;
1146 ue
->info
.value
.enumerated
.names_ptr
= 0;
1151 static void snd_ctl_elem_user_free(struct snd_kcontrol
*kcontrol
)
1153 struct user_element
*ue
= kcontrol
->private_data
;
1155 kfree(ue
->tlv_data
);
1156 kfree(ue
->priv_data
);
1160 static int snd_ctl_elem_add(struct snd_ctl_file
*file
,
1161 struct snd_ctl_elem_info
*info
, int replace
)
1163 struct snd_card
*card
= file
->card
;
1164 struct snd_kcontrol kctl
, *_kctl
;
1165 unsigned int access
;
1167 struct user_element
*ue
;
1170 if (info
->count
< 1)
1172 if (!*info
->id
.name
)
1174 if (strnlen(info
->id
.name
, sizeof(info
->id
.name
)) >= sizeof(info
->id
.name
))
1176 access
= info
->access
== 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE
:
1177 (info
->access
& (SNDRV_CTL_ELEM_ACCESS_READWRITE
|
1178 SNDRV_CTL_ELEM_ACCESS_INACTIVE
|
1179 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
));
1181 memset(&kctl
, 0, sizeof(kctl
));
1184 err
= snd_ctl_remove_user_ctl(file
, &info
->id
);
1189 if (card
->user_ctl_count
>= MAX_USER_CONTROLS
)
1192 memcpy(&kctl
.id
, &info
->id
, sizeof(info
->id
));
1193 kctl
.count
= info
->owner
? info
->owner
: 1;
1194 access
|= SNDRV_CTL_ELEM_ACCESS_USER
;
1195 if (info
->type
== SNDRV_CTL_ELEM_TYPE_ENUMERATED
)
1196 kctl
.info
= snd_ctl_elem_user_enum_info
;
1198 kctl
.info
= snd_ctl_elem_user_info
;
1199 if (access
& SNDRV_CTL_ELEM_ACCESS_READ
)
1200 kctl
.get
= snd_ctl_elem_user_get
;
1201 if (access
& SNDRV_CTL_ELEM_ACCESS_WRITE
)
1202 kctl
.put
= snd_ctl_elem_user_put
;
1203 if (access
& SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
) {
1204 kctl
.tlv
.c
= snd_ctl_elem_user_tlv
;
1205 access
|= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
;
1207 switch (info
->type
) {
1208 case SNDRV_CTL_ELEM_TYPE_BOOLEAN
:
1209 case SNDRV_CTL_ELEM_TYPE_INTEGER
:
1210 private_size
= sizeof(long);
1211 if (info
->count
> 128)
1214 case SNDRV_CTL_ELEM_TYPE_INTEGER64
:
1215 private_size
= sizeof(long long);
1216 if (info
->count
> 64)
1219 case SNDRV_CTL_ELEM_TYPE_ENUMERATED
:
1220 private_size
= sizeof(unsigned int);
1221 if (info
->count
> 128 || info
->value
.enumerated
.items
== 0)
1224 case SNDRV_CTL_ELEM_TYPE_BYTES
:
1225 private_size
= sizeof(unsigned char);
1226 if (info
->count
> 512)
1229 case SNDRV_CTL_ELEM_TYPE_IEC958
:
1230 private_size
= sizeof(struct snd_aes_iec958
);
1231 if (info
->count
!= 1)
1237 private_size
*= info
->count
;
1238 ue
= kzalloc(sizeof(struct user_element
) + private_size
, GFP_KERNEL
);
1243 ue
->info
.access
= 0;
1244 ue
->elem_data
= (char *)ue
+ sizeof(*ue
);
1245 ue
->elem_data_size
= private_size
;
1246 if (ue
->info
.type
== SNDRV_CTL_ELEM_TYPE_ENUMERATED
) {
1247 err
= snd_ctl_elem_init_enum_names(ue
);
1253 kctl
.private_free
= snd_ctl_elem_user_free
;
1254 _kctl
= snd_ctl_new(&kctl
, access
);
1255 if (_kctl
== NULL
) {
1256 kfree(ue
->priv_data
);
1260 _kctl
->private_data
= ue
;
1261 for (idx
= 0; idx
< _kctl
->count
; idx
++)
1262 _kctl
->vd
[idx
].owner
= file
;
1263 err
= snd_ctl_add(card
, _kctl
);
1267 down_write(&card
->controls_rwsem
);
1268 card
->user_ctl_count
++;
1269 up_write(&card
->controls_rwsem
);
1274 static int snd_ctl_elem_add_user(struct snd_ctl_file
*file
,
1275 struct snd_ctl_elem_info __user
*_info
, int replace
)
1277 struct snd_ctl_elem_info info
;
1278 if (copy_from_user(&info
, _info
, sizeof(info
)))
1280 return snd_ctl_elem_add(file
, &info
, replace
);
1283 static int snd_ctl_elem_remove(struct snd_ctl_file
*file
,
1284 struct snd_ctl_elem_id __user
*_id
)
1286 struct snd_ctl_elem_id id
;
1288 if (copy_from_user(&id
, _id
, sizeof(id
)))
1290 return snd_ctl_remove_user_ctl(file
, &id
);
1293 static int snd_ctl_subscribe_events(struct snd_ctl_file
*file
, int __user
*ptr
)
1296 if (get_user(subscribe
, ptr
))
1298 if (subscribe
< 0) {
1299 subscribe
= file
->subscribed
;
1300 if (put_user(subscribe
, ptr
))
1305 file
->subscribed
= 1;
1307 } else if (file
->subscribed
) {
1308 snd_ctl_empty_read_queue(file
);
1309 file
->subscribed
= 0;
1314 static int snd_ctl_tlv_ioctl(struct snd_ctl_file
*file
,
1315 struct snd_ctl_tlv __user
*_tlv
,
1318 struct snd_card
*card
= file
->card
;
1319 struct snd_ctl_tlv tlv
;
1320 struct snd_kcontrol
*kctl
;
1321 struct snd_kcontrol_volatile
*vd
;
1325 if (copy_from_user(&tlv
, _tlv
, sizeof(tlv
)))
1327 if (tlv
.length
< sizeof(unsigned int) * 2)
1331 down_read(&card
->controls_rwsem
);
1332 kctl
= snd_ctl_find_numid(card
, tlv
.numid
);
1337 if (kctl
->tlv
.p
== NULL
) {
1341 vd
= &kctl
->vd
[tlv
.numid
- kctl
->id
.numid
];
1342 if ((op_flag
== 0 && (vd
->access
& SNDRV_CTL_ELEM_ACCESS_TLV_READ
) == 0) ||
1343 (op_flag
> 0 && (vd
->access
& SNDRV_CTL_ELEM_ACCESS_TLV_WRITE
) == 0) ||
1344 (op_flag
< 0 && (vd
->access
& SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND
) == 0)) {
1348 if (vd
->access
& SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
) {
1349 if (vd
->owner
!= NULL
&& vd
->owner
!= file
) {
1353 err
= kctl
->tlv
.c(kctl
, op_flag
, tlv
.length
, _tlv
->tlv
);
1355 struct snd_ctl_elem_id id
= kctl
->id
;
1356 up_read(&card
->controls_rwsem
);
1357 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_TLV
, &id
);
1365 len
= kctl
->tlv
.p
[1] + 2 * sizeof(unsigned int);
1366 if (tlv
.length
< len
) {
1370 if (copy_to_user(_tlv
->tlv
, kctl
->tlv
.p
, len
))
1374 up_read(&card
->controls_rwsem
);
1378 static long snd_ctl_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1380 struct snd_ctl_file
*ctl
;
1381 struct snd_card
*card
;
1382 struct snd_kctl_ioctl
*p
;
1383 void __user
*argp
= (void __user
*)arg
;
1384 int __user
*ip
= argp
;
1387 ctl
= file
->private_data
;
1389 if (snd_BUG_ON(!card
))
1392 case SNDRV_CTL_IOCTL_PVERSION
:
1393 return put_user(SNDRV_CTL_VERSION
, ip
) ? -EFAULT
: 0;
1394 case SNDRV_CTL_IOCTL_CARD_INFO
:
1395 return snd_ctl_card_info(card
, ctl
, cmd
, argp
);
1396 case SNDRV_CTL_IOCTL_ELEM_LIST
:
1397 return snd_ctl_elem_list(card
, argp
);
1398 case SNDRV_CTL_IOCTL_ELEM_INFO
:
1399 return snd_ctl_elem_info_user(ctl
, argp
);
1400 case SNDRV_CTL_IOCTL_ELEM_READ
:
1401 return snd_ctl_elem_read_user(card
, argp
);
1402 case SNDRV_CTL_IOCTL_ELEM_WRITE
:
1403 return snd_ctl_elem_write_user(ctl
, argp
);
1404 case SNDRV_CTL_IOCTL_ELEM_LOCK
:
1405 return snd_ctl_elem_lock(ctl
, argp
);
1406 case SNDRV_CTL_IOCTL_ELEM_UNLOCK
:
1407 return snd_ctl_elem_unlock(ctl
, argp
);
1408 case SNDRV_CTL_IOCTL_ELEM_ADD
:
1409 return snd_ctl_elem_add_user(ctl
, argp
, 0);
1410 case SNDRV_CTL_IOCTL_ELEM_REPLACE
:
1411 return snd_ctl_elem_add_user(ctl
, argp
, 1);
1412 case SNDRV_CTL_IOCTL_ELEM_REMOVE
:
1413 return snd_ctl_elem_remove(ctl
, argp
);
1414 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS
:
1415 return snd_ctl_subscribe_events(ctl
, ip
);
1416 case SNDRV_CTL_IOCTL_TLV_READ
:
1417 return snd_ctl_tlv_ioctl(ctl
, argp
, 0);
1418 case SNDRV_CTL_IOCTL_TLV_WRITE
:
1419 return snd_ctl_tlv_ioctl(ctl
, argp
, 1);
1420 case SNDRV_CTL_IOCTL_TLV_COMMAND
:
1421 return snd_ctl_tlv_ioctl(ctl
, argp
, -1);
1422 case SNDRV_CTL_IOCTL_POWER
:
1423 return -ENOPROTOOPT
;
1424 case SNDRV_CTL_IOCTL_POWER_STATE
:
1426 return put_user(card
->power_state
, ip
) ? -EFAULT
: 0;
1428 return put_user(SNDRV_CTL_POWER_D0
, ip
) ? -EFAULT
: 0;
1431 down_read(&snd_ioctl_rwsem
);
1432 list_for_each_entry(p
, &snd_control_ioctls
, list
) {
1433 err
= p
->fioctl(card
, ctl
, cmd
, arg
);
1434 if (err
!= -ENOIOCTLCMD
) {
1435 up_read(&snd_ioctl_rwsem
);
1439 up_read(&snd_ioctl_rwsem
);
1440 dev_dbg(card
->dev
, "unknown ioctl = 0x%x\n", cmd
);
1444 static ssize_t
snd_ctl_read(struct file
*file
, char __user
*buffer
,
1445 size_t count
, loff_t
* offset
)
1447 struct snd_ctl_file
*ctl
;
1451 ctl
= file
->private_data
;
1452 if (snd_BUG_ON(!ctl
|| !ctl
->card
))
1454 if (!ctl
->subscribed
)
1456 if (count
< sizeof(struct snd_ctl_event
))
1458 spin_lock_irq(&ctl
->read_lock
);
1459 while (count
>= sizeof(struct snd_ctl_event
)) {
1460 struct snd_ctl_event ev
;
1461 struct snd_kctl_event
*kev
;
1462 while (list_empty(&ctl
->events
)) {
1464 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
1468 init_waitqueue_entry(&wait
, current
);
1469 add_wait_queue(&ctl
->change_sleep
, &wait
);
1470 set_current_state(TASK_INTERRUPTIBLE
);
1471 spin_unlock_irq(&ctl
->read_lock
);
1473 remove_wait_queue(&ctl
->change_sleep
, &wait
);
1474 if (ctl
->card
->shutdown
)
1476 if (signal_pending(current
))
1477 return -ERESTARTSYS
;
1478 spin_lock_irq(&ctl
->read_lock
);
1480 kev
= snd_kctl_event(ctl
->events
.next
);
1481 ev
.type
= SNDRV_CTL_EVENT_ELEM
;
1482 ev
.data
.elem
.mask
= kev
->mask
;
1483 ev
.data
.elem
.id
= kev
->id
;
1484 list_del(&kev
->list
);
1485 spin_unlock_irq(&ctl
->read_lock
);
1487 if (copy_to_user(buffer
, &ev
, sizeof(struct snd_ctl_event
))) {
1491 spin_lock_irq(&ctl
->read_lock
);
1492 buffer
+= sizeof(struct snd_ctl_event
);
1493 count
-= sizeof(struct snd_ctl_event
);
1494 result
+= sizeof(struct snd_ctl_event
);
1497 spin_unlock_irq(&ctl
->read_lock
);
1499 return result
> 0 ? result
: err
;
1502 static unsigned int snd_ctl_poll(struct file
*file
, poll_table
* wait
)
1505 struct snd_ctl_file
*ctl
;
1507 ctl
= file
->private_data
;
1508 if (!ctl
->subscribed
)
1510 poll_wait(file
, &ctl
->change_sleep
, wait
);
1513 if (!list_empty(&ctl
->events
))
1514 mask
|= POLLIN
| POLLRDNORM
;
1520 * register the device-specific control-ioctls.
1521 * called from each device manager like pcm.c, hwdep.c, etc.
1523 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn
, struct list_head
*lists
)
1525 struct snd_kctl_ioctl
*pn
;
1527 pn
= kzalloc(sizeof(struct snd_kctl_ioctl
), GFP_KERNEL
);
1531 down_write(&snd_ioctl_rwsem
);
1532 list_add_tail(&pn
->list
, lists
);
1533 up_write(&snd_ioctl_rwsem
);
1537 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn
)
1539 return _snd_ctl_register_ioctl(fcn
, &snd_control_ioctls
);
1542 EXPORT_SYMBOL(snd_ctl_register_ioctl
);
1544 #ifdef CONFIG_COMPAT
1545 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn
)
1547 return _snd_ctl_register_ioctl(fcn
, &snd_control_compat_ioctls
);
1550 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat
);
1554 * de-register the device-specific control-ioctls.
1556 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn
,
1557 struct list_head
*lists
)
1559 struct snd_kctl_ioctl
*p
;
1561 if (snd_BUG_ON(!fcn
))
1563 down_write(&snd_ioctl_rwsem
);
1564 list_for_each_entry(p
, lists
, list
) {
1565 if (p
->fioctl
== fcn
) {
1567 up_write(&snd_ioctl_rwsem
);
1572 up_write(&snd_ioctl_rwsem
);
1577 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn
)
1579 return _snd_ctl_unregister_ioctl(fcn
, &snd_control_ioctls
);
1582 EXPORT_SYMBOL(snd_ctl_unregister_ioctl
);
1584 #ifdef CONFIG_COMPAT
1585 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn
)
1587 return _snd_ctl_unregister_ioctl(fcn
, &snd_control_compat_ioctls
);
1590 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat
);
1593 static int snd_ctl_fasync(int fd
, struct file
* file
, int on
)
1595 struct snd_ctl_file
*ctl
;
1597 ctl
= file
->private_data
;
1598 return fasync_helper(fd
, file
, on
, &ctl
->fasync
);
1604 #ifdef CONFIG_COMPAT
1605 #include "control_compat.c"
1607 #define snd_ctl_ioctl_compat NULL
1614 static const struct file_operations snd_ctl_f_ops
=
1616 .owner
= THIS_MODULE
,
1617 .read
= snd_ctl_read
,
1618 .open
= snd_ctl_open
,
1619 .release
= snd_ctl_release
,
1620 .llseek
= no_llseek
,
1621 .poll
= snd_ctl_poll
,
1622 .unlocked_ioctl
= snd_ctl_ioctl
,
1623 .compat_ioctl
= snd_ctl_ioctl_compat
,
1624 .fasync
= snd_ctl_fasync
,
1628 * registration of the control device
1630 static int snd_ctl_dev_register(struct snd_device
*device
)
1632 struct snd_card
*card
= device
->device_data
;
1636 if (snd_BUG_ON(!card
))
1638 cardnum
= card
->number
;
1639 if (snd_BUG_ON(cardnum
< 0 || cardnum
>= SNDRV_CARDS
))
1641 sprintf(name
, "controlC%i", cardnum
);
1642 if ((err
= snd_register_device(SNDRV_DEVICE_TYPE_CONTROL
, card
, -1,
1643 &snd_ctl_f_ops
, card
, name
)) < 0)
1649 * disconnection of the control device
1651 static int snd_ctl_dev_disconnect(struct snd_device
*device
)
1653 struct snd_card
*card
= device
->device_data
;
1654 struct snd_ctl_file
*ctl
;
1657 if (snd_BUG_ON(!card
))
1659 cardnum
= card
->number
;
1660 if (snd_BUG_ON(cardnum
< 0 || cardnum
>= SNDRV_CARDS
))
1663 read_lock(&card
->ctl_files_rwlock
);
1664 list_for_each_entry(ctl
, &card
->ctl_files
, list
) {
1665 wake_up(&ctl
->change_sleep
);
1666 kill_fasync(&ctl
->fasync
, SIGIO
, POLL_ERR
);
1668 read_unlock(&card
->ctl_files_rwlock
);
1670 if ((err
= snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL
,
1679 static int snd_ctl_dev_free(struct snd_device
*device
)
1681 struct snd_card
*card
= device
->device_data
;
1682 struct snd_kcontrol
*control
;
1684 down_write(&card
->controls_rwsem
);
1685 while (!list_empty(&card
->controls
)) {
1686 control
= snd_kcontrol(card
->controls
.next
);
1687 snd_ctl_remove(card
, control
);
1689 up_write(&card
->controls_rwsem
);
1694 * create control core:
1695 * called from init.c
1697 int snd_ctl_create(struct snd_card
*card
)
1699 static struct snd_device_ops ops
= {
1700 .dev_free
= snd_ctl_dev_free
,
1701 .dev_register
= snd_ctl_dev_register
,
1702 .dev_disconnect
= snd_ctl_dev_disconnect
,
1705 if (snd_BUG_ON(!card
))
1707 return snd_device_new(card
, SNDRV_DEV_CONTROL
, card
, &ops
);
1711 * Frequently used control callbacks/helpers
1713 int snd_ctl_boolean_mono_info(struct snd_kcontrol
*kcontrol
,
1714 struct snd_ctl_elem_info
*uinfo
)
1716 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1718 uinfo
->value
.integer
.min
= 0;
1719 uinfo
->value
.integer
.max
= 1;
1723 EXPORT_SYMBOL(snd_ctl_boolean_mono_info
);
1725 int snd_ctl_boolean_stereo_info(struct snd_kcontrol
*kcontrol
,
1726 struct snd_ctl_elem_info
*uinfo
)
1728 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1730 uinfo
->value
.integer
.min
= 0;
1731 uinfo
->value
.integer
.max
= 1;
1735 EXPORT_SYMBOL(snd_ctl_boolean_stereo_info
);
1738 * snd_ctl_enum_info - fills the info structure for an enumerated control
1739 * @info: the structure to be filled
1740 * @channels: the number of the control's channels; often one
1741 * @items: the number of control values; also the size of @names
1742 * @names: an array containing the names of all control values
1744 * Sets all required fields in @info to their appropriate values.
1745 * If the control's accessibility is not the default (readable and writable),
1746 * the caller has to fill @info->access.
1750 int snd_ctl_enum_info(struct snd_ctl_elem_info
*info
, unsigned int channels
,
1751 unsigned int items
, const char *const names
[])
1753 info
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1754 info
->count
= channels
;
1755 info
->value
.enumerated
.items
= items
;
1756 if (info
->value
.enumerated
.item
>= items
)
1757 info
->value
.enumerated
.item
= items
- 1;
1758 strlcpy(info
->value
.enumerated
.name
,
1759 names
[info
->value
.enumerated
.item
],
1760 sizeof(info
->value
.enumerated
.name
));
1763 EXPORT_SYMBOL(snd_ctl_enum_info
);