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 for (i
= 0; i
< SND_CTL_SUBDEV_ITEMS
; i
++)
83 ctl
->preferred_subdevice
[i
] = -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
);
145 * snd_ctl_notify - Send notification to user-space for a control change
146 * @card: the card to send notification
147 * @mask: the event mask, SNDRV_CTL_EVENT_*
148 * @id: the ctl element id to send notification
150 * This function adds an event record with the given id and mask, appends
151 * to the list and wakes up the user-space for notification. This can be
152 * called in the atomic context.
154 void snd_ctl_notify(struct snd_card
*card
, unsigned int mask
,
155 struct snd_ctl_elem_id
*id
)
158 struct snd_ctl_file
*ctl
;
159 struct snd_kctl_event
*ev
;
161 if (snd_BUG_ON(!card
|| !id
))
163 read_lock(&card
->ctl_files_rwlock
);
164 #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
165 card
->mixer_oss_change_count
++;
167 list_for_each_entry(ctl
, &card
->ctl_files
, list
) {
168 if (!ctl
->subscribed
)
170 spin_lock_irqsave(&ctl
->read_lock
, flags
);
171 list_for_each_entry(ev
, &ctl
->events
, list
) {
172 if (ev
->id
.numid
== id
->numid
) {
177 ev
= kzalloc(sizeof(*ev
), GFP_ATOMIC
);
181 list_add_tail(&ev
->list
, &ctl
->events
);
183 dev_err(card
->dev
, "No memory available to allocate event\n");
186 wake_up(&ctl
->change_sleep
);
187 spin_unlock_irqrestore(&ctl
->read_lock
, flags
);
188 kill_fasync(&ctl
->fasync
, SIGIO
, POLL_IN
);
190 read_unlock(&card
->ctl_files_rwlock
);
192 EXPORT_SYMBOL(snd_ctl_notify
);
195 * snd_ctl_new - create a control instance from the template
196 * @control: the control template
197 * @access: the default control access
199 * Allocates a new struct snd_kcontrol instance and copies the given template
200 * to the new instance. It does not copy volatile data (access).
202 * Return: The pointer of the new instance, or %NULL on failure.
204 static struct snd_kcontrol
*snd_ctl_new(struct snd_kcontrol
*control
,
207 struct snd_kcontrol
*kctl
;
210 if (snd_BUG_ON(!control
|| !control
->count
))
213 if (control
->count
> MAX_CONTROL_COUNT
)
216 kctl
= kzalloc(sizeof(*kctl
) + sizeof(struct snd_kcontrol_volatile
) * control
->count
, GFP_KERNEL
);
218 pr_err("ALSA: Cannot allocate control instance\n");
222 for (idx
= 0; idx
< kctl
->count
; idx
++)
223 kctl
->vd
[idx
].access
= access
;
228 * snd_ctl_new1 - create a control instance from the template
229 * @ncontrol: the initialization record
230 * @private_data: the private data to set
232 * Allocates a new struct snd_kcontrol instance and initialize from the given
233 * template. When the access field of ncontrol is 0, it's assumed as
234 * READWRITE access. When the count field is 0, it's assumes as one.
236 * Return: The pointer of the newly generated instance, or %NULL on failure.
238 struct snd_kcontrol
*snd_ctl_new1(const struct snd_kcontrol_new
*ncontrol
,
241 struct snd_kcontrol kctl
;
244 if (snd_BUG_ON(!ncontrol
|| !ncontrol
->info
))
246 memset(&kctl
, 0, sizeof(kctl
));
247 kctl
.id
.iface
= ncontrol
->iface
;
248 kctl
.id
.device
= ncontrol
->device
;
249 kctl
.id
.subdevice
= ncontrol
->subdevice
;
250 if (ncontrol
->name
) {
251 strlcpy(kctl
.id
.name
, ncontrol
->name
, sizeof(kctl
.id
.name
));
252 if (strcmp(ncontrol
->name
, kctl
.id
.name
) != 0)
253 pr_warn("ALSA: Control name '%s' truncated to '%s'\n",
254 ncontrol
->name
, kctl
.id
.name
);
256 kctl
.id
.index
= ncontrol
->index
;
257 kctl
.count
= ncontrol
->count
? ncontrol
->count
: 1;
258 access
= ncontrol
->access
== 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE
:
259 (ncontrol
->access
& (SNDRV_CTL_ELEM_ACCESS_READWRITE
|
260 SNDRV_CTL_ELEM_ACCESS_VOLATILE
|
261 SNDRV_CTL_ELEM_ACCESS_INACTIVE
|
262 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
|
263 SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND
|
264 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
));
265 kctl
.info
= ncontrol
->info
;
266 kctl
.get
= ncontrol
->get
;
267 kctl
.put
= ncontrol
->put
;
268 kctl
.tlv
.p
= ncontrol
->tlv
.p
;
269 kctl
.private_value
= ncontrol
->private_value
;
270 kctl
.private_data
= private_data
;
271 return snd_ctl_new(&kctl
, access
);
273 EXPORT_SYMBOL(snd_ctl_new1
);
276 * snd_ctl_free_one - release the control instance
277 * @kcontrol: the control instance
279 * Releases the control instance created via snd_ctl_new()
281 * Don't call this after the control was added to the card.
283 void snd_ctl_free_one(struct snd_kcontrol
*kcontrol
)
286 if (kcontrol
->private_free
)
287 kcontrol
->private_free(kcontrol
);
291 EXPORT_SYMBOL(snd_ctl_free_one
);
293 static bool snd_ctl_remove_numid_conflict(struct snd_card
*card
,
296 struct snd_kcontrol
*kctl
;
298 /* Make sure that the ids assigned to the control do not wrap around */
299 if (card
->last_numid
>= UINT_MAX
- count
)
300 card
->last_numid
= 0;
302 list_for_each_entry(kctl
, &card
->controls
, list
) {
303 if (kctl
->id
.numid
< card
->last_numid
+ 1 + count
&&
304 kctl
->id
.numid
+ kctl
->count
> card
->last_numid
+ 1) {
305 card
->last_numid
= kctl
->id
.numid
+ kctl
->count
- 1;
312 static int snd_ctl_find_hole(struct snd_card
*card
, unsigned int count
)
314 unsigned int iter
= 100000;
316 while (snd_ctl_remove_numid_conflict(card
, count
)) {
318 /* this situation is very unlikely */
319 dev_err(card
->dev
, "unable to allocate new control numid\n");
327 * snd_ctl_add - add the control instance to the card
328 * @card: the card instance
329 * @kcontrol: the control instance to add
331 * Adds the control instance created via snd_ctl_new() or
332 * snd_ctl_new1() to the given card. Assigns also an unique
333 * numid used for fast search.
335 * It frees automatically the control which cannot be added.
337 * Return: Zero if successful, or a negative error code on failure.
340 int snd_ctl_add(struct snd_card
*card
, struct snd_kcontrol
*kcontrol
)
342 struct snd_ctl_elem_id id
;
349 if (snd_BUG_ON(!card
|| !kcontrol
->info
))
352 if (id
.index
> UINT_MAX
- kcontrol
->count
)
355 down_write(&card
->controls_rwsem
);
356 if (snd_ctl_find_id(card
, &id
)) {
357 up_write(&card
->controls_rwsem
);
358 dev_err(card
->dev
, "control %i:%i:%i:%s:%i is already present\n",
367 if (snd_ctl_find_hole(card
, kcontrol
->count
) < 0) {
368 up_write(&card
->controls_rwsem
);
372 list_add_tail(&kcontrol
->list
, &card
->controls
);
373 card
->controls_count
+= kcontrol
->count
;
374 kcontrol
->id
.numid
= card
->last_numid
+ 1;
375 card
->last_numid
+= kcontrol
->count
;
377 count
= kcontrol
->count
;
378 up_write(&card
->controls_rwsem
);
379 for (idx
= 0; idx
< count
; idx
++, id
.index
++, id
.numid
++)
380 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_ADD
, &id
);
384 snd_ctl_free_one(kcontrol
);
387 EXPORT_SYMBOL(snd_ctl_add
);
390 * snd_ctl_replace - replace the control instance of the card
391 * @card: the card instance
392 * @kcontrol: the control instance to replace
393 * @add_on_replace: add the control if not already added
395 * Replaces the given control. If the given control does not exist
396 * and the add_on_replace flag is set, the control is added. If the
397 * control exists, it is destroyed first.
399 * It frees automatically the control which cannot be added or replaced.
401 * Return: Zero if successful, or a negative error code on failure.
403 int snd_ctl_replace(struct snd_card
*card
, struct snd_kcontrol
*kcontrol
,
406 struct snd_ctl_elem_id id
;
409 struct snd_kcontrol
*old
;
414 if (snd_BUG_ON(!card
|| !kcontrol
->info
)) {
419 down_write(&card
->controls_rwsem
);
420 old
= snd_ctl_find_id(card
, &id
);
424 up_write(&card
->controls_rwsem
);
428 ret
= snd_ctl_remove(card
, old
);
430 up_write(&card
->controls_rwsem
);
434 if (snd_ctl_find_hole(card
, kcontrol
->count
) < 0) {
435 up_write(&card
->controls_rwsem
);
439 list_add_tail(&kcontrol
->list
, &card
->controls
);
440 card
->controls_count
+= kcontrol
->count
;
441 kcontrol
->id
.numid
= card
->last_numid
+ 1;
442 card
->last_numid
+= kcontrol
->count
;
444 count
= kcontrol
->count
;
445 up_write(&card
->controls_rwsem
);
446 for (idx
= 0; idx
< count
; idx
++, id
.index
++, id
.numid
++)
447 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_ADD
, &id
);
451 snd_ctl_free_one(kcontrol
);
454 EXPORT_SYMBOL(snd_ctl_replace
);
457 * snd_ctl_remove - remove the control from the card and release it
458 * @card: the card instance
459 * @kcontrol: the control instance to remove
461 * Removes the control from the card and then releases the instance.
462 * You don't need to call snd_ctl_free_one(). You must be in
463 * the write lock - down_write(&card->controls_rwsem).
465 * Return: 0 if successful, or a negative error code on failure.
467 int snd_ctl_remove(struct snd_card
*card
, struct snd_kcontrol
*kcontrol
)
469 struct snd_ctl_elem_id id
;
472 if (snd_BUG_ON(!card
|| !kcontrol
))
474 list_del(&kcontrol
->list
);
475 card
->controls_count
-= kcontrol
->count
;
477 for (idx
= 0; idx
< kcontrol
->count
; idx
++, id
.index
++, id
.numid
++)
478 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_REMOVE
, &id
);
479 snd_ctl_free_one(kcontrol
);
482 EXPORT_SYMBOL(snd_ctl_remove
);
485 * snd_ctl_remove_id - remove the control of the given id and release it
486 * @card: the card instance
487 * @id: the control id to remove
489 * Finds the control instance with the given id, removes it from the
490 * card list and releases it.
492 * Return: 0 if successful, or a negative error code on failure.
494 int snd_ctl_remove_id(struct snd_card
*card
, struct snd_ctl_elem_id
*id
)
496 struct snd_kcontrol
*kctl
;
499 down_write(&card
->controls_rwsem
);
500 kctl
= snd_ctl_find_id(card
, id
);
502 up_write(&card
->controls_rwsem
);
505 ret
= snd_ctl_remove(card
, kctl
);
506 up_write(&card
->controls_rwsem
);
509 EXPORT_SYMBOL(snd_ctl_remove_id
);
512 * snd_ctl_remove_user_ctl - remove and release the unlocked user control
513 * @file: active control handle
514 * @id: the control id to remove
516 * Finds the control instance with the given id, removes it from the
517 * card list and releases it.
519 * Return: 0 if successful, or a negative error code on failure.
521 static int snd_ctl_remove_user_ctl(struct snd_ctl_file
* file
,
522 struct snd_ctl_elem_id
*id
)
524 struct snd_card
*card
= file
->card
;
525 struct snd_kcontrol
*kctl
;
528 down_write(&card
->controls_rwsem
);
529 kctl
= snd_ctl_find_id(card
, id
);
534 if (!(kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_USER
)) {
538 for (idx
= 0; idx
< kctl
->count
; idx
++)
539 if (kctl
->vd
[idx
].owner
!= NULL
&& kctl
->vd
[idx
].owner
!= file
) {
543 ret
= snd_ctl_remove(card
, kctl
);
546 card
->user_ctl_count
--;
548 up_write(&card
->controls_rwsem
);
553 * snd_ctl_activate_id - activate/inactivate the control of the given id
554 * @card: the card instance
555 * @id: the control id to activate/inactivate
556 * @active: non-zero to activate
558 * Finds the control instance with the given id, and activate or
559 * inactivate the control together with notification, if changed.
561 * Return: 0 if unchanged, 1 if changed, or a negative error code on failure.
563 int snd_ctl_activate_id(struct snd_card
*card
, struct snd_ctl_elem_id
*id
,
566 struct snd_kcontrol
*kctl
;
567 struct snd_kcontrol_volatile
*vd
;
568 unsigned int index_offset
;
571 down_write(&card
->controls_rwsem
);
572 kctl
= snd_ctl_find_id(card
, id
);
577 index_offset
= snd_ctl_get_ioff(kctl
, id
);
578 vd
= &kctl
->vd
[index_offset
];
581 if (!(vd
->access
& SNDRV_CTL_ELEM_ACCESS_INACTIVE
))
583 vd
->access
&= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE
;
585 if (vd
->access
& SNDRV_CTL_ELEM_ACCESS_INACTIVE
)
587 vd
->access
|= SNDRV_CTL_ELEM_ACCESS_INACTIVE
;
591 up_write(&card
->controls_rwsem
);
593 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_INFO
, id
);
596 EXPORT_SYMBOL_GPL(snd_ctl_activate_id
);
599 * snd_ctl_rename_id - replace the id of a control on the card
600 * @card: the card instance
601 * @src_id: the old id
602 * @dst_id: the new id
604 * Finds the control with the old id from the card, and replaces the
605 * id with the new one.
607 * Return: Zero if successful, or a negative error code on failure.
609 int snd_ctl_rename_id(struct snd_card
*card
, struct snd_ctl_elem_id
*src_id
,
610 struct snd_ctl_elem_id
*dst_id
)
612 struct snd_kcontrol
*kctl
;
614 down_write(&card
->controls_rwsem
);
615 kctl
= snd_ctl_find_id(card
, src_id
);
617 up_write(&card
->controls_rwsem
);
621 kctl
->id
.numid
= card
->last_numid
+ 1;
622 card
->last_numid
+= kctl
->count
;
623 up_write(&card
->controls_rwsem
);
626 EXPORT_SYMBOL(snd_ctl_rename_id
);
629 * snd_ctl_find_numid - find the control instance with the given number-id
630 * @card: the card instance
631 * @numid: the number-id to search
633 * Finds the control instance with the given number-id from the card.
635 * The caller must down card->controls_rwsem before calling this function
636 * (if the race condition can happen).
638 * Return: The pointer of the instance if found, or %NULL if not.
641 struct snd_kcontrol
*snd_ctl_find_numid(struct snd_card
*card
, unsigned int numid
)
643 struct snd_kcontrol
*kctl
;
645 if (snd_BUG_ON(!card
|| !numid
))
647 list_for_each_entry(kctl
, &card
->controls
, list
) {
648 if (kctl
->id
.numid
<= numid
&& kctl
->id
.numid
+ kctl
->count
> numid
)
653 EXPORT_SYMBOL(snd_ctl_find_numid
);
656 * snd_ctl_find_id - find the control instance with the given id
657 * @card: the card instance
658 * @id: the id to search
660 * Finds the control instance with the given id from the card.
662 * The caller must down card->controls_rwsem before calling this function
663 * (if the race condition can happen).
665 * Return: The pointer of the instance if found, or %NULL if not.
668 struct snd_kcontrol
*snd_ctl_find_id(struct snd_card
*card
,
669 struct snd_ctl_elem_id
*id
)
671 struct snd_kcontrol
*kctl
;
673 if (snd_BUG_ON(!card
|| !id
))
676 return snd_ctl_find_numid(card
, id
->numid
);
677 list_for_each_entry(kctl
, &card
->controls
, list
) {
678 if (kctl
->id
.iface
!= id
->iface
)
680 if (kctl
->id
.device
!= id
->device
)
682 if (kctl
->id
.subdevice
!= id
->subdevice
)
684 if (strncmp(kctl
->id
.name
, id
->name
, sizeof(kctl
->id
.name
)))
686 if (kctl
->id
.index
> id
->index
)
688 if (kctl
->id
.index
+ kctl
->count
<= id
->index
)
694 EXPORT_SYMBOL(snd_ctl_find_id
);
696 static int snd_ctl_card_info(struct snd_card
*card
, struct snd_ctl_file
* ctl
,
697 unsigned int cmd
, void __user
*arg
)
699 struct snd_ctl_card_info
*info
;
701 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
704 down_read(&snd_ioctl_rwsem
);
705 info
->card
= card
->number
;
706 strlcpy(info
->id
, card
->id
, sizeof(info
->id
));
707 strlcpy(info
->driver
, card
->driver
, sizeof(info
->driver
));
708 strlcpy(info
->name
, card
->shortname
, sizeof(info
->name
));
709 strlcpy(info
->longname
, card
->longname
, sizeof(info
->longname
));
710 strlcpy(info
->mixername
, card
->mixername
, sizeof(info
->mixername
));
711 strlcpy(info
->components
, card
->components
, sizeof(info
->components
));
712 up_read(&snd_ioctl_rwsem
);
713 if (copy_to_user(arg
, info
, sizeof(struct snd_ctl_card_info
))) {
721 static int snd_ctl_elem_list(struct snd_card
*card
,
722 struct snd_ctl_elem_list __user
*_list
)
724 struct list_head
*plist
;
725 struct snd_ctl_elem_list list
;
726 struct snd_kcontrol
*kctl
;
727 struct snd_ctl_elem_id
*dst
, *id
;
728 unsigned int offset
, space
, jidx
;
730 if (copy_from_user(&list
, _list
, sizeof(list
)))
732 offset
= list
.offset
;
734 /* try limit maximum space */
738 /* allocate temporary buffer for atomic operation */
739 dst
= vmalloc(space
* sizeof(struct snd_ctl_elem_id
));
742 down_read(&card
->controls_rwsem
);
743 list
.count
= card
->controls_count
;
744 plist
= card
->controls
.next
;
745 while (plist
!= &card
->controls
) {
748 kctl
= snd_kcontrol(plist
);
749 if (offset
< kctl
->count
)
751 offset
-= kctl
->count
;
756 while (space
> 0 && plist
!= &card
->controls
) {
757 kctl
= snd_kcontrol(plist
);
758 for (jidx
= offset
; space
> 0 && jidx
< kctl
->count
; jidx
++) {
759 snd_ctl_build_ioff(id
, kctl
, jidx
);
767 up_read(&card
->controls_rwsem
);
769 copy_to_user(list
.pids
, dst
,
770 list
.used
* sizeof(struct snd_ctl_elem_id
))) {
776 down_read(&card
->controls_rwsem
);
777 list
.count
= card
->controls_count
;
778 up_read(&card
->controls_rwsem
);
780 if (copy_to_user(_list
, &list
, sizeof(list
)))
785 static int snd_ctl_elem_info(struct snd_ctl_file
*ctl
,
786 struct snd_ctl_elem_info
*info
)
788 struct snd_card
*card
= ctl
->card
;
789 struct snd_kcontrol
*kctl
;
790 struct snd_kcontrol_volatile
*vd
;
791 unsigned int index_offset
;
794 down_read(&card
->controls_rwsem
);
795 kctl
= snd_ctl_find_id(card
, &info
->id
);
797 up_read(&card
->controls_rwsem
);
800 #ifdef CONFIG_SND_DEBUG
803 result
= kctl
->info(kctl
, info
);
805 snd_BUG_ON(info
->access
);
806 index_offset
= snd_ctl_get_ioff(kctl
, &info
->id
);
807 vd
= &kctl
->vd
[index_offset
];
808 snd_ctl_build_ioff(&info
->id
, kctl
, index_offset
);
809 info
->access
= vd
->access
;
811 info
->access
|= SNDRV_CTL_ELEM_ACCESS_LOCK
;
812 if (vd
->owner
== ctl
)
813 info
->access
|= SNDRV_CTL_ELEM_ACCESS_OWNER
;
814 info
->owner
= pid_vnr(vd
->owner
->pid
);
819 up_read(&card
->controls_rwsem
);
823 static int snd_ctl_elem_info_user(struct snd_ctl_file
*ctl
,
824 struct snd_ctl_elem_info __user
*_info
)
826 struct snd_ctl_elem_info info
;
829 if (copy_from_user(&info
, _info
, sizeof(info
)))
831 snd_power_lock(ctl
->card
);
832 result
= snd_power_wait(ctl
->card
, SNDRV_CTL_POWER_D0
);
834 result
= snd_ctl_elem_info(ctl
, &info
);
835 snd_power_unlock(ctl
->card
);
837 if (copy_to_user(_info
, &info
, sizeof(info
)))
842 static int snd_ctl_elem_read(struct snd_card
*card
,
843 struct snd_ctl_elem_value
*control
)
845 struct snd_kcontrol
*kctl
;
846 struct snd_kcontrol_volatile
*vd
;
847 unsigned int index_offset
;
850 down_read(&card
->controls_rwsem
);
851 kctl
= snd_ctl_find_id(card
, &control
->id
);
855 index_offset
= snd_ctl_get_ioff(kctl
, &control
->id
);
856 vd
= &kctl
->vd
[index_offset
];
857 if ((vd
->access
& SNDRV_CTL_ELEM_ACCESS_READ
) &&
859 snd_ctl_build_ioff(&control
->id
, kctl
, index_offset
);
860 result
= kctl
->get(kctl
, control
);
864 up_read(&card
->controls_rwsem
);
868 static int snd_ctl_elem_read_user(struct snd_card
*card
,
869 struct snd_ctl_elem_value __user
*_control
)
871 struct snd_ctl_elem_value
*control
;
874 control
= memdup_user(_control
, sizeof(*control
));
876 return PTR_ERR(control
);
878 snd_power_lock(card
);
879 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
881 result
= snd_ctl_elem_read(card
, control
);
882 snd_power_unlock(card
);
884 if (copy_to_user(_control
, control
, sizeof(*control
)))
890 static int snd_ctl_elem_write(struct snd_card
*card
, struct snd_ctl_file
*file
,
891 struct snd_ctl_elem_value
*control
)
893 struct snd_kcontrol
*kctl
;
894 struct snd_kcontrol_volatile
*vd
;
895 unsigned int index_offset
;
898 down_read(&card
->controls_rwsem
);
899 kctl
= snd_ctl_find_id(card
, &control
->id
);
903 index_offset
= snd_ctl_get_ioff(kctl
, &control
->id
);
904 vd
= &kctl
->vd
[index_offset
];
905 if (!(vd
->access
& SNDRV_CTL_ELEM_ACCESS_WRITE
) ||
907 (file
&& vd
->owner
&& vd
->owner
!= file
)) {
910 snd_ctl_build_ioff(&control
->id
, kctl
, index_offset
);
911 result
= kctl
->put(kctl
, control
);
914 struct snd_ctl_elem_id id
= control
->id
;
915 up_read(&card
->controls_rwsem
);
916 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_VALUE
, &id
);
920 up_read(&card
->controls_rwsem
);
924 static int snd_ctl_elem_write_user(struct snd_ctl_file
*file
,
925 struct snd_ctl_elem_value __user
*_control
)
927 struct snd_ctl_elem_value
*control
;
928 struct snd_card
*card
;
931 control
= memdup_user(_control
, sizeof(*control
));
933 return PTR_ERR(control
);
936 snd_power_lock(card
);
937 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
939 result
= snd_ctl_elem_write(card
, file
, control
);
940 snd_power_unlock(card
);
942 if (copy_to_user(_control
, control
, sizeof(*control
)))
948 static int snd_ctl_elem_lock(struct snd_ctl_file
*file
,
949 struct snd_ctl_elem_id __user
*_id
)
951 struct snd_card
*card
= file
->card
;
952 struct snd_ctl_elem_id id
;
953 struct snd_kcontrol
*kctl
;
954 struct snd_kcontrol_volatile
*vd
;
957 if (copy_from_user(&id
, _id
, sizeof(id
)))
959 down_write(&card
->controls_rwsem
);
960 kctl
= snd_ctl_find_id(card
, &id
);
964 vd
= &kctl
->vd
[snd_ctl_get_ioff(kctl
, &id
)];
965 if (vd
->owner
!= NULL
)
972 up_write(&card
->controls_rwsem
);
976 static int snd_ctl_elem_unlock(struct snd_ctl_file
*file
,
977 struct snd_ctl_elem_id __user
*_id
)
979 struct snd_card
*card
= file
->card
;
980 struct snd_ctl_elem_id id
;
981 struct snd_kcontrol
*kctl
;
982 struct snd_kcontrol_volatile
*vd
;
985 if (copy_from_user(&id
, _id
, sizeof(id
)))
987 down_write(&card
->controls_rwsem
);
988 kctl
= snd_ctl_find_id(card
, &id
);
992 vd
= &kctl
->vd
[snd_ctl_get_ioff(kctl
, &id
)];
993 if (vd
->owner
== NULL
)
995 else if (vd
->owner
!= file
)
1002 up_write(&card
->controls_rwsem
);
1006 struct user_element
{
1007 struct snd_ctl_elem_info info
;
1008 struct snd_card
*card
;
1009 void *elem_data
; /* element data */
1010 unsigned long elem_data_size
; /* size of element data in bytes */
1011 void *tlv_data
; /* TLV data */
1012 unsigned long tlv_data_size
; /* TLV data size */
1013 void *priv_data
; /* private data (like strings for enumerated type) */
1016 static int snd_ctl_elem_user_info(struct snd_kcontrol
*kcontrol
,
1017 struct snd_ctl_elem_info
*uinfo
)
1019 struct user_element
*ue
= kcontrol
->private_data
;
1025 static int snd_ctl_elem_user_enum_info(struct snd_kcontrol
*kcontrol
,
1026 struct snd_ctl_elem_info
*uinfo
)
1028 struct user_element
*ue
= kcontrol
->private_data
;
1032 item
= uinfo
->value
.enumerated
.item
;
1036 item
= min(item
, uinfo
->value
.enumerated
.items
- 1);
1037 uinfo
->value
.enumerated
.item
= item
;
1039 names
= ue
->priv_data
;
1040 for (; item
> 0; --item
)
1041 names
+= strlen(names
) + 1;
1042 strcpy(uinfo
->value
.enumerated
.name
, names
);
1047 static int snd_ctl_elem_user_get(struct snd_kcontrol
*kcontrol
,
1048 struct snd_ctl_elem_value
*ucontrol
)
1050 struct user_element
*ue
= kcontrol
->private_data
;
1052 mutex_lock(&ue
->card
->user_ctl_lock
);
1053 memcpy(&ucontrol
->value
, ue
->elem_data
, ue
->elem_data_size
);
1054 mutex_unlock(&ue
->card
->user_ctl_lock
);
1058 static int snd_ctl_elem_user_put(struct snd_kcontrol
*kcontrol
,
1059 struct snd_ctl_elem_value
*ucontrol
)
1062 struct user_element
*ue
= kcontrol
->private_data
;
1064 mutex_lock(&ue
->card
->user_ctl_lock
);
1065 change
= memcmp(&ucontrol
->value
, ue
->elem_data
, ue
->elem_data_size
) != 0;
1067 memcpy(ue
->elem_data
, &ucontrol
->value
, ue
->elem_data_size
);
1068 mutex_unlock(&ue
->card
->user_ctl_lock
);
1072 static int snd_ctl_elem_user_tlv(struct snd_kcontrol
*kcontrol
,
1075 unsigned int __user
*tlv
)
1077 struct user_element
*ue
= kcontrol
->private_data
;
1082 if (size
> 1024 * 128) /* sane value */
1085 new_data
= memdup_user(tlv
, size
);
1086 if (IS_ERR(new_data
))
1087 return PTR_ERR(new_data
);
1088 mutex_lock(&ue
->card
->user_ctl_lock
);
1089 change
= ue
->tlv_data_size
!= size
;
1091 change
= memcmp(ue
->tlv_data
, new_data
, size
);
1092 kfree(ue
->tlv_data
);
1093 ue
->tlv_data
= new_data
;
1094 ue
->tlv_data_size
= size
;
1095 mutex_unlock(&ue
->card
->user_ctl_lock
);
1099 mutex_lock(&ue
->card
->user_ctl_lock
);
1100 if (!ue
->tlv_data_size
|| !ue
->tlv_data
) {
1104 if (size
< ue
->tlv_data_size
) {
1108 if (copy_to_user(tlv
, ue
->tlv_data
, ue
->tlv_data_size
))
1111 mutex_unlock(&ue
->card
->user_ctl_lock
);
1118 static int snd_ctl_elem_init_enum_names(struct user_element
*ue
)
1121 size_t buf_len
, name_len
;
1123 const uintptr_t user_ptrval
= ue
->info
.value
.enumerated
.names_ptr
;
1125 if (ue
->info
.value
.enumerated
.names_length
> 64 * 1024)
1128 names
= memdup_user((const void __user
*)user_ptrval
,
1129 ue
->info
.value
.enumerated
.names_length
);
1131 return PTR_ERR(names
);
1133 /* check that there are enough valid names */
1134 buf_len
= ue
->info
.value
.enumerated
.names_length
;
1136 for (i
= 0; i
< ue
->info
.value
.enumerated
.items
; ++i
) {
1137 name_len
= strnlen(p
, buf_len
);
1138 if (name_len
== 0 || name_len
>= 64 || name_len
== buf_len
) {
1143 buf_len
-= name_len
+ 1;
1146 ue
->priv_data
= names
;
1147 ue
->info
.value
.enumerated
.names_ptr
= 0;
1152 static void snd_ctl_elem_user_free(struct snd_kcontrol
*kcontrol
)
1154 struct user_element
*ue
= kcontrol
->private_data
;
1156 kfree(ue
->tlv_data
);
1157 kfree(ue
->priv_data
);
1161 static int snd_ctl_elem_add(struct snd_ctl_file
*file
,
1162 struct snd_ctl_elem_info
*info
, int replace
)
1164 struct snd_card
*card
= file
->card
;
1165 struct snd_kcontrol kctl
, *_kctl
;
1166 unsigned int access
;
1168 struct user_element
*ue
;
1171 if (info
->count
< 1)
1173 if (!*info
->id
.name
)
1175 if (strnlen(info
->id
.name
, sizeof(info
->id
.name
)) >= sizeof(info
->id
.name
))
1177 access
= info
->access
== 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE
:
1178 (info
->access
& (SNDRV_CTL_ELEM_ACCESS_READWRITE
|
1179 SNDRV_CTL_ELEM_ACCESS_INACTIVE
|
1180 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
));
1182 memset(&kctl
, 0, sizeof(kctl
));
1185 err
= snd_ctl_remove_user_ctl(file
, &info
->id
);
1190 if (card
->user_ctl_count
>= MAX_USER_CONTROLS
)
1193 memcpy(&kctl
.id
, &info
->id
, sizeof(info
->id
));
1194 kctl
.count
= info
->owner
? info
->owner
: 1;
1195 access
|= SNDRV_CTL_ELEM_ACCESS_USER
;
1196 if (info
->type
== SNDRV_CTL_ELEM_TYPE_ENUMERATED
)
1197 kctl
.info
= snd_ctl_elem_user_enum_info
;
1199 kctl
.info
= snd_ctl_elem_user_info
;
1200 if (access
& SNDRV_CTL_ELEM_ACCESS_READ
)
1201 kctl
.get
= snd_ctl_elem_user_get
;
1202 if (access
& SNDRV_CTL_ELEM_ACCESS_WRITE
)
1203 kctl
.put
= snd_ctl_elem_user_put
;
1204 if (access
& SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
) {
1205 kctl
.tlv
.c
= snd_ctl_elem_user_tlv
;
1206 access
|= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
;
1208 switch (info
->type
) {
1209 case SNDRV_CTL_ELEM_TYPE_BOOLEAN
:
1210 case SNDRV_CTL_ELEM_TYPE_INTEGER
:
1211 private_size
= sizeof(long);
1212 if (info
->count
> 128)
1215 case SNDRV_CTL_ELEM_TYPE_INTEGER64
:
1216 private_size
= sizeof(long long);
1217 if (info
->count
> 64)
1220 case SNDRV_CTL_ELEM_TYPE_ENUMERATED
:
1221 private_size
= sizeof(unsigned int);
1222 if (info
->count
> 128 || info
->value
.enumerated
.items
== 0)
1225 case SNDRV_CTL_ELEM_TYPE_BYTES
:
1226 private_size
= sizeof(unsigned char);
1227 if (info
->count
> 512)
1230 case SNDRV_CTL_ELEM_TYPE_IEC958
:
1231 private_size
= sizeof(struct snd_aes_iec958
);
1232 if (info
->count
!= 1)
1238 private_size
*= info
->count
;
1239 ue
= kzalloc(sizeof(struct user_element
) + private_size
, GFP_KERNEL
);
1244 ue
->info
.access
= 0;
1245 ue
->elem_data
= (char *)ue
+ sizeof(*ue
);
1246 ue
->elem_data_size
= private_size
;
1247 if (ue
->info
.type
== SNDRV_CTL_ELEM_TYPE_ENUMERATED
) {
1248 err
= snd_ctl_elem_init_enum_names(ue
);
1254 kctl
.private_free
= snd_ctl_elem_user_free
;
1255 _kctl
= snd_ctl_new(&kctl
, access
);
1256 if (_kctl
== NULL
) {
1257 kfree(ue
->priv_data
);
1261 _kctl
->private_data
= ue
;
1262 for (idx
= 0; idx
< _kctl
->count
; idx
++)
1263 _kctl
->vd
[idx
].owner
= file
;
1264 err
= snd_ctl_add(card
, _kctl
);
1268 down_write(&card
->controls_rwsem
);
1269 card
->user_ctl_count
++;
1270 up_write(&card
->controls_rwsem
);
1275 static int snd_ctl_elem_add_user(struct snd_ctl_file
*file
,
1276 struct snd_ctl_elem_info __user
*_info
, int replace
)
1278 struct snd_ctl_elem_info info
;
1279 if (copy_from_user(&info
, _info
, sizeof(info
)))
1281 return snd_ctl_elem_add(file
, &info
, replace
);
1284 static int snd_ctl_elem_remove(struct snd_ctl_file
*file
,
1285 struct snd_ctl_elem_id __user
*_id
)
1287 struct snd_ctl_elem_id id
;
1289 if (copy_from_user(&id
, _id
, sizeof(id
)))
1291 return snd_ctl_remove_user_ctl(file
, &id
);
1294 static int snd_ctl_subscribe_events(struct snd_ctl_file
*file
, int __user
*ptr
)
1297 if (get_user(subscribe
, ptr
))
1299 if (subscribe
< 0) {
1300 subscribe
= file
->subscribed
;
1301 if (put_user(subscribe
, ptr
))
1306 file
->subscribed
= 1;
1308 } else if (file
->subscribed
) {
1309 snd_ctl_empty_read_queue(file
);
1310 file
->subscribed
= 0;
1315 static int snd_ctl_tlv_ioctl(struct snd_ctl_file
*file
,
1316 struct snd_ctl_tlv __user
*_tlv
,
1319 struct snd_card
*card
= file
->card
;
1320 struct snd_ctl_tlv tlv
;
1321 struct snd_kcontrol
*kctl
;
1322 struct snd_kcontrol_volatile
*vd
;
1326 if (copy_from_user(&tlv
, _tlv
, sizeof(tlv
)))
1328 if (tlv
.length
< sizeof(unsigned int) * 2)
1330 down_read(&card
->controls_rwsem
);
1331 kctl
= snd_ctl_find_numid(card
, tlv
.numid
);
1336 if (kctl
->tlv
.p
== NULL
) {
1340 vd
= &kctl
->vd
[tlv
.numid
- kctl
->id
.numid
];
1341 if ((op_flag
== 0 && (vd
->access
& SNDRV_CTL_ELEM_ACCESS_TLV_READ
) == 0) ||
1342 (op_flag
> 0 && (vd
->access
& SNDRV_CTL_ELEM_ACCESS_TLV_WRITE
) == 0) ||
1343 (op_flag
< 0 && (vd
->access
& SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND
) == 0)) {
1347 if (vd
->access
& SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
) {
1348 if (vd
->owner
!= NULL
&& vd
->owner
!= file
) {
1352 err
= kctl
->tlv
.c(kctl
, op_flag
, tlv
.length
, _tlv
->tlv
);
1354 struct snd_ctl_elem_id id
= kctl
->id
;
1355 up_read(&card
->controls_rwsem
);
1356 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_TLV
, &id
);
1364 len
= kctl
->tlv
.p
[1] + 2 * sizeof(unsigned int);
1365 if (tlv
.length
< len
) {
1369 if (copy_to_user(_tlv
->tlv
, kctl
->tlv
.p
, len
))
1373 up_read(&card
->controls_rwsem
);
1377 static long snd_ctl_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1379 struct snd_ctl_file
*ctl
;
1380 struct snd_card
*card
;
1381 struct snd_kctl_ioctl
*p
;
1382 void __user
*argp
= (void __user
*)arg
;
1383 int __user
*ip
= argp
;
1386 ctl
= file
->private_data
;
1388 if (snd_BUG_ON(!card
))
1391 case SNDRV_CTL_IOCTL_PVERSION
:
1392 return put_user(SNDRV_CTL_VERSION
, ip
) ? -EFAULT
: 0;
1393 case SNDRV_CTL_IOCTL_CARD_INFO
:
1394 return snd_ctl_card_info(card
, ctl
, cmd
, argp
);
1395 case SNDRV_CTL_IOCTL_ELEM_LIST
:
1396 return snd_ctl_elem_list(card
, argp
);
1397 case SNDRV_CTL_IOCTL_ELEM_INFO
:
1398 return snd_ctl_elem_info_user(ctl
, argp
);
1399 case SNDRV_CTL_IOCTL_ELEM_READ
:
1400 return snd_ctl_elem_read_user(card
, argp
);
1401 case SNDRV_CTL_IOCTL_ELEM_WRITE
:
1402 return snd_ctl_elem_write_user(ctl
, argp
);
1403 case SNDRV_CTL_IOCTL_ELEM_LOCK
:
1404 return snd_ctl_elem_lock(ctl
, argp
);
1405 case SNDRV_CTL_IOCTL_ELEM_UNLOCK
:
1406 return snd_ctl_elem_unlock(ctl
, argp
);
1407 case SNDRV_CTL_IOCTL_ELEM_ADD
:
1408 return snd_ctl_elem_add_user(ctl
, argp
, 0);
1409 case SNDRV_CTL_IOCTL_ELEM_REPLACE
:
1410 return snd_ctl_elem_add_user(ctl
, argp
, 1);
1411 case SNDRV_CTL_IOCTL_ELEM_REMOVE
:
1412 return snd_ctl_elem_remove(ctl
, argp
);
1413 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS
:
1414 return snd_ctl_subscribe_events(ctl
, ip
);
1415 case SNDRV_CTL_IOCTL_TLV_READ
:
1416 return snd_ctl_tlv_ioctl(ctl
, argp
, SNDRV_CTL_TLV_OP_READ
);
1417 case SNDRV_CTL_IOCTL_TLV_WRITE
:
1418 return snd_ctl_tlv_ioctl(ctl
, argp
, SNDRV_CTL_TLV_OP_WRITE
);
1419 case SNDRV_CTL_IOCTL_TLV_COMMAND
:
1420 return snd_ctl_tlv_ioctl(ctl
, argp
, SNDRV_CTL_TLV_OP_CMD
);
1421 case SNDRV_CTL_IOCTL_POWER
:
1422 return -ENOPROTOOPT
;
1423 case SNDRV_CTL_IOCTL_POWER_STATE
:
1425 return put_user(card
->power_state
, ip
) ? -EFAULT
: 0;
1427 return put_user(SNDRV_CTL_POWER_D0
, ip
) ? -EFAULT
: 0;
1430 down_read(&snd_ioctl_rwsem
);
1431 list_for_each_entry(p
, &snd_control_ioctls
, list
) {
1432 err
= p
->fioctl(card
, ctl
, cmd
, arg
);
1433 if (err
!= -ENOIOCTLCMD
) {
1434 up_read(&snd_ioctl_rwsem
);
1438 up_read(&snd_ioctl_rwsem
);
1439 dev_dbg(card
->dev
, "unknown ioctl = 0x%x\n", cmd
);
1443 static ssize_t
snd_ctl_read(struct file
*file
, char __user
*buffer
,
1444 size_t count
, loff_t
* offset
)
1446 struct snd_ctl_file
*ctl
;
1450 ctl
= file
->private_data
;
1451 if (snd_BUG_ON(!ctl
|| !ctl
->card
))
1453 if (!ctl
->subscribed
)
1455 if (count
< sizeof(struct snd_ctl_event
))
1457 spin_lock_irq(&ctl
->read_lock
);
1458 while (count
>= sizeof(struct snd_ctl_event
)) {
1459 struct snd_ctl_event ev
;
1460 struct snd_kctl_event
*kev
;
1461 while (list_empty(&ctl
->events
)) {
1463 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
1467 init_waitqueue_entry(&wait
, current
);
1468 add_wait_queue(&ctl
->change_sleep
, &wait
);
1469 set_current_state(TASK_INTERRUPTIBLE
);
1470 spin_unlock_irq(&ctl
->read_lock
);
1472 remove_wait_queue(&ctl
->change_sleep
, &wait
);
1473 if (ctl
->card
->shutdown
)
1475 if (signal_pending(current
))
1476 return -ERESTARTSYS
;
1477 spin_lock_irq(&ctl
->read_lock
);
1479 kev
= snd_kctl_event(ctl
->events
.next
);
1480 ev
.type
= SNDRV_CTL_EVENT_ELEM
;
1481 ev
.data
.elem
.mask
= kev
->mask
;
1482 ev
.data
.elem
.id
= kev
->id
;
1483 list_del(&kev
->list
);
1484 spin_unlock_irq(&ctl
->read_lock
);
1486 if (copy_to_user(buffer
, &ev
, sizeof(struct snd_ctl_event
))) {
1490 spin_lock_irq(&ctl
->read_lock
);
1491 buffer
+= sizeof(struct snd_ctl_event
);
1492 count
-= sizeof(struct snd_ctl_event
);
1493 result
+= sizeof(struct snd_ctl_event
);
1496 spin_unlock_irq(&ctl
->read_lock
);
1498 return result
> 0 ? result
: err
;
1501 static unsigned int snd_ctl_poll(struct file
*file
, poll_table
* wait
)
1504 struct snd_ctl_file
*ctl
;
1506 ctl
= file
->private_data
;
1507 if (!ctl
->subscribed
)
1509 poll_wait(file
, &ctl
->change_sleep
, wait
);
1512 if (!list_empty(&ctl
->events
))
1513 mask
|= POLLIN
| POLLRDNORM
;
1519 * register the device-specific control-ioctls.
1520 * called from each device manager like pcm.c, hwdep.c, etc.
1522 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn
, struct list_head
*lists
)
1524 struct snd_kctl_ioctl
*pn
;
1526 pn
= kzalloc(sizeof(struct snd_kctl_ioctl
), GFP_KERNEL
);
1530 down_write(&snd_ioctl_rwsem
);
1531 list_add_tail(&pn
->list
, lists
);
1532 up_write(&snd_ioctl_rwsem
);
1537 * snd_ctl_register_ioctl - register the device-specific control-ioctls
1538 * @fcn: ioctl callback function
1540 * called from each device manager like pcm.c, hwdep.c, etc.
1542 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn
)
1544 return _snd_ctl_register_ioctl(fcn
, &snd_control_ioctls
);
1546 EXPORT_SYMBOL(snd_ctl_register_ioctl
);
1548 #ifdef CONFIG_COMPAT
1550 * snd_ctl_register_ioctl_compat - register the device-specific 32bit compat
1552 * @fcn: ioctl callback function
1554 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn
)
1556 return _snd_ctl_register_ioctl(fcn
, &snd_control_compat_ioctls
);
1558 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat
);
1562 * de-register the device-specific control-ioctls.
1564 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn
,
1565 struct list_head
*lists
)
1567 struct snd_kctl_ioctl
*p
;
1569 if (snd_BUG_ON(!fcn
))
1571 down_write(&snd_ioctl_rwsem
);
1572 list_for_each_entry(p
, lists
, list
) {
1573 if (p
->fioctl
== fcn
) {
1575 up_write(&snd_ioctl_rwsem
);
1580 up_write(&snd_ioctl_rwsem
);
1586 * snd_ctl_unregister_ioctl - de-register the device-specific control-ioctls
1587 * @fcn: ioctl callback function to unregister
1589 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn
)
1591 return _snd_ctl_unregister_ioctl(fcn
, &snd_control_ioctls
);
1593 EXPORT_SYMBOL(snd_ctl_unregister_ioctl
);
1595 #ifdef CONFIG_COMPAT
1597 * snd_ctl_unregister_ioctl - de-register the device-specific compat 32bit
1599 * @fcn: ioctl callback function to unregister
1601 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn
)
1603 return _snd_ctl_unregister_ioctl(fcn
, &snd_control_compat_ioctls
);
1605 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat
);
1608 static int snd_ctl_fasync(int fd
, struct file
* file
, int on
)
1610 struct snd_ctl_file
*ctl
;
1612 ctl
= file
->private_data
;
1613 return fasync_helper(fd
, file
, on
, &ctl
->fasync
);
1616 /* return the preferred subdevice number if already assigned;
1617 * otherwise return -1
1619 int snd_ctl_get_preferred_subdevice(struct snd_card
*card
, int type
)
1621 struct snd_ctl_file
*kctl
;
1624 read_lock(&card
->ctl_files_rwlock
);
1625 list_for_each_entry(kctl
, &card
->ctl_files
, list
) {
1626 if (kctl
->pid
== task_pid(current
)) {
1627 subdevice
= kctl
->preferred_subdevice
[type
];
1628 if (subdevice
!= -1)
1632 read_unlock(&card
->ctl_files_rwlock
);
1635 EXPORT_SYMBOL_GPL(snd_ctl_get_preferred_subdevice
);
1640 #ifdef CONFIG_COMPAT
1641 #include "control_compat.c"
1643 #define snd_ctl_ioctl_compat NULL
1650 static const struct file_operations snd_ctl_f_ops
=
1652 .owner
= THIS_MODULE
,
1653 .read
= snd_ctl_read
,
1654 .open
= snd_ctl_open
,
1655 .release
= snd_ctl_release
,
1656 .llseek
= no_llseek
,
1657 .poll
= snd_ctl_poll
,
1658 .unlocked_ioctl
= snd_ctl_ioctl
,
1659 .compat_ioctl
= snd_ctl_ioctl_compat
,
1660 .fasync
= snd_ctl_fasync
,
1664 * registration of the control device
1666 static int snd_ctl_dev_register(struct snd_device
*device
)
1668 struct snd_card
*card
= device
->device_data
;
1670 return snd_register_device(SNDRV_DEVICE_TYPE_CONTROL
, card
, -1,
1671 &snd_ctl_f_ops
, card
, &card
->ctl_dev
);
1675 * disconnection of the control device
1677 static int snd_ctl_dev_disconnect(struct snd_device
*device
)
1679 struct snd_card
*card
= device
->device_data
;
1680 struct snd_ctl_file
*ctl
;
1682 read_lock(&card
->ctl_files_rwlock
);
1683 list_for_each_entry(ctl
, &card
->ctl_files
, list
) {
1684 wake_up(&ctl
->change_sleep
);
1685 kill_fasync(&ctl
->fasync
, SIGIO
, POLL_ERR
);
1687 read_unlock(&card
->ctl_files_rwlock
);
1689 return snd_unregister_device(&card
->ctl_dev
);
1695 static int snd_ctl_dev_free(struct snd_device
*device
)
1697 struct snd_card
*card
= device
->device_data
;
1698 struct snd_kcontrol
*control
;
1700 down_write(&card
->controls_rwsem
);
1701 while (!list_empty(&card
->controls
)) {
1702 control
= snd_kcontrol(card
->controls
.next
);
1703 snd_ctl_remove(card
, control
);
1705 up_write(&card
->controls_rwsem
);
1706 put_device(&card
->ctl_dev
);
1711 * create control core:
1712 * called from init.c
1714 int snd_ctl_create(struct snd_card
*card
)
1716 static struct snd_device_ops ops
= {
1717 .dev_free
= snd_ctl_dev_free
,
1718 .dev_register
= snd_ctl_dev_register
,
1719 .dev_disconnect
= snd_ctl_dev_disconnect
,
1723 if (snd_BUG_ON(!card
))
1725 if (snd_BUG_ON(card
->number
< 0 || card
->number
>= SNDRV_CARDS
))
1728 snd_device_initialize(&card
->ctl_dev
, card
);
1729 dev_set_name(&card
->ctl_dev
, "controlC%d", card
->number
);
1731 err
= snd_device_new(card
, SNDRV_DEV_CONTROL
, card
, &ops
);
1733 put_device(&card
->ctl_dev
);
1738 * Frequently used control callbacks/helpers
1742 * snd_ctl_boolean_mono_info - Helper function for a standard boolean info
1743 * callback with a mono channel
1744 * @kcontrol: the kcontrol instance
1745 * @uinfo: info to store
1747 * This is a function that can be used as info callback for a standard
1748 * boolean control with a single mono channel.
1750 int snd_ctl_boolean_mono_info(struct snd_kcontrol
*kcontrol
,
1751 struct snd_ctl_elem_info
*uinfo
)
1753 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1755 uinfo
->value
.integer
.min
= 0;
1756 uinfo
->value
.integer
.max
= 1;
1759 EXPORT_SYMBOL(snd_ctl_boolean_mono_info
);
1762 * snd_ctl_boolean_stereo_info - Helper function for a standard boolean info
1763 * callback with stereo two channels
1764 * @kcontrol: the kcontrol instance
1765 * @uinfo: info to store
1767 * This is a function that can be used as info callback for a standard
1768 * boolean control with stereo two channels.
1770 int snd_ctl_boolean_stereo_info(struct snd_kcontrol
*kcontrol
,
1771 struct snd_ctl_elem_info
*uinfo
)
1773 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1775 uinfo
->value
.integer
.min
= 0;
1776 uinfo
->value
.integer
.max
= 1;
1779 EXPORT_SYMBOL(snd_ctl_boolean_stereo_info
);
1782 * snd_ctl_enum_info - fills the info structure for an enumerated control
1783 * @info: the structure to be filled
1784 * @channels: the number of the control's channels; often one
1785 * @items: the number of control values; also the size of @names
1786 * @names: an array containing the names of all control values
1788 * Sets all required fields in @info to their appropriate values.
1789 * If the control's accessibility is not the default (readable and writable),
1790 * the caller has to fill @info->access.
1794 int snd_ctl_enum_info(struct snd_ctl_elem_info
*info
, unsigned int channels
,
1795 unsigned int items
, const char *const names
[])
1797 info
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1798 info
->count
= channels
;
1799 info
->value
.enumerated
.items
= items
;
1802 if (info
->value
.enumerated
.item
>= items
)
1803 info
->value
.enumerated
.item
= items
- 1;
1804 WARN(strlen(names
[info
->value
.enumerated
.item
]) >= sizeof(info
->value
.enumerated
.name
),
1805 "ALSA: too long item name '%s'\n",
1806 names
[info
->value
.enumerated
.item
]);
1807 strlcpy(info
->value
.enumerated
.name
,
1808 names
[info
->value
.enumerated
.item
],
1809 sizeof(info
->value
.enumerated
.name
));
1812 EXPORT_SYMBOL(snd_ctl_enum_info
);