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 <linux/sched/signal.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
36 #define MAX_CONTROL_COUNT 1028
38 struct snd_kctl_ioctl
{
39 struct list_head list
; /* list of all ioctls */
40 snd_kctl_ioctl_func_t fioctl
;
43 static DECLARE_RWSEM(snd_ioctl_rwsem
);
44 static LIST_HEAD(snd_control_ioctls
);
46 static LIST_HEAD(snd_control_compat_ioctls
);
49 static int snd_ctl_open(struct inode
*inode
, struct file
*file
)
52 struct snd_card
*card
;
53 struct snd_ctl_file
*ctl
;
56 err
= nonseekable_open(inode
, file
);
60 card
= snd_lookup_minor_data(iminor(inode
), SNDRV_DEVICE_TYPE_CONTROL
);
65 err
= snd_card_file_add(card
, file
);
70 if (!try_module_get(card
->module
)) {
74 ctl
= kzalloc(sizeof(*ctl
), GFP_KERNEL
);
79 INIT_LIST_HEAD(&ctl
->events
);
80 init_waitqueue_head(&ctl
->change_sleep
);
81 spin_lock_init(&ctl
->read_lock
);
83 for (i
= 0; i
< SND_CTL_SUBDEV_ITEMS
; i
++)
84 ctl
->preferred_subdevice
[i
] = -1;
85 ctl
->pid
= get_pid(task_pid(current
));
86 file
->private_data
= ctl
;
87 write_lock_irqsave(&card
->ctl_files_rwlock
, flags
);
88 list_add_tail(&ctl
->list
, &card
->ctl_files
);
89 write_unlock_irqrestore(&card
->ctl_files_rwlock
, flags
);
94 module_put(card
->module
);
96 snd_card_file_remove(card
, file
);
103 static void snd_ctl_empty_read_queue(struct snd_ctl_file
* ctl
)
106 struct snd_kctl_event
*cread
;
108 spin_lock_irqsave(&ctl
->read_lock
, flags
);
109 while (!list_empty(&ctl
->events
)) {
110 cread
= snd_kctl_event(ctl
->events
.next
);
111 list_del(&cread
->list
);
114 spin_unlock_irqrestore(&ctl
->read_lock
, flags
);
117 static int snd_ctl_release(struct inode
*inode
, struct file
*file
)
120 struct snd_card
*card
;
121 struct snd_ctl_file
*ctl
;
122 struct snd_kcontrol
*control
;
125 ctl
= file
->private_data
;
126 file
->private_data
= NULL
;
128 write_lock_irqsave(&card
->ctl_files_rwlock
, flags
);
129 list_del(&ctl
->list
);
130 write_unlock_irqrestore(&card
->ctl_files_rwlock
, flags
);
131 down_write(&card
->controls_rwsem
);
132 list_for_each_entry(control
, &card
->controls
, list
)
133 for (idx
= 0; idx
< control
->count
; idx
++)
134 if (control
->vd
[idx
].owner
== ctl
)
135 control
->vd
[idx
].owner
= NULL
;
136 up_write(&card
->controls_rwsem
);
137 snd_ctl_empty_read_queue(ctl
);
140 module_put(card
->module
);
141 snd_card_file_remove(card
, file
);
146 * snd_ctl_notify - Send notification to user-space for a control change
147 * @card: the card to send notification
148 * @mask: the event mask, SNDRV_CTL_EVENT_*
149 * @id: the ctl element id to send notification
151 * This function adds an event record with the given id and mask, appends
152 * to the list and wakes up the user-space for notification. This can be
153 * called in the atomic context.
155 void snd_ctl_notify(struct snd_card
*card
, unsigned int mask
,
156 struct snd_ctl_elem_id
*id
)
159 struct snd_ctl_file
*ctl
;
160 struct snd_kctl_event
*ev
;
162 if (snd_BUG_ON(!card
|| !id
))
166 read_lock(&card
->ctl_files_rwlock
);
167 #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
168 card
->mixer_oss_change_count
++;
170 list_for_each_entry(ctl
, &card
->ctl_files
, list
) {
171 if (!ctl
->subscribed
)
173 spin_lock_irqsave(&ctl
->read_lock
, flags
);
174 list_for_each_entry(ev
, &ctl
->events
, list
) {
175 if (ev
->id
.numid
== id
->numid
) {
180 ev
= kzalloc(sizeof(*ev
), GFP_ATOMIC
);
184 list_add_tail(&ev
->list
, &ctl
->events
);
186 dev_err(card
->dev
, "No memory available to allocate event\n");
189 wake_up(&ctl
->change_sleep
);
190 spin_unlock_irqrestore(&ctl
->read_lock
, flags
);
191 kill_fasync(&ctl
->fasync
, SIGIO
, POLL_IN
);
193 read_unlock(&card
->ctl_files_rwlock
);
195 EXPORT_SYMBOL(snd_ctl_notify
);
198 * snd_ctl_new - create a new control instance with some elements
199 * @kctl: the pointer to store new control instance
200 * @count: the number of elements in this control
201 * @access: the default access flags for elements in this control
202 * @file: given when locking these elements
204 * Allocates a memory object for a new control instance. The instance has
205 * elements as many as the given number (@count). Each element has given
206 * access permissions (@access). Each element is locked when @file is given.
208 * Return: 0 on success, error code on failure
210 static int snd_ctl_new(struct snd_kcontrol
**kctl
, unsigned int count
,
211 unsigned int access
, struct snd_ctl_file
*file
)
216 if (count
== 0 || count
> MAX_CONTROL_COUNT
)
219 size
= sizeof(struct snd_kcontrol
);
220 size
+= sizeof(struct snd_kcontrol_volatile
) * count
;
222 *kctl
= kzalloc(size
, GFP_KERNEL
);
226 for (idx
= 0; idx
< count
; idx
++) {
227 (*kctl
)->vd
[idx
].access
= access
;
228 (*kctl
)->vd
[idx
].owner
= file
;
230 (*kctl
)->count
= count
;
236 * snd_ctl_new1 - create a control instance from the template
237 * @ncontrol: the initialization record
238 * @private_data: the private data to set
240 * Allocates a new struct snd_kcontrol instance and initialize from the given
241 * template. When the access field of ncontrol is 0, it's assumed as
242 * READWRITE access. When the count field is 0, it's assumes as one.
244 * Return: The pointer of the newly generated instance, or %NULL on failure.
246 struct snd_kcontrol
*snd_ctl_new1(const struct snd_kcontrol_new
*ncontrol
,
249 struct snd_kcontrol
*kctl
;
254 if (snd_BUG_ON(!ncontrol
|| !ncontrol
->info
))
257 count
= ncontrol
->count
;
261 access
= ncontrol
->access
;
263 access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
;
264 access
&= (SNDRV_CTL_ELEM_ACCESS_READWRITE
|
265 SNDRV_CTL_ELEM_ACCESS_VOLATILE
|
266 SNDRV_CTL_ELEM_ACCESS_INACTIVE
|
267 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
|
268 SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND
|
269 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
);
271 err
= snd_ctl_new(&kctl
, count
, access
, NULL
);
275 /* The 'numid' member is decided when calling snd_ctl_add(). */
276 kctl
->id
.iface
= ncontrol
->iface
;
277 kctl
->id
.device
= ncontrol
->device
;
278 kctl
->id
.subdevice
= ncontrol
->subdevice
;
279 if (ncontrol
->name
) {
280 strlcpy(kctl
->id
.name
, ncontrol
->name
, sizeof(kctl
->id
.name
));
281 if (strcmp(ncontrol
->name
, kctl
->id
.name
) != 0)
282 pr_warn("ALSA: Control name '%s' truncated to '%s'\n",
283 ncontrol
->name
, kctl
->id
.name
);
285 kctl
->id
.index
= ncontrol
->index
;
287 kctl
->info
= ncontrol
->info
;
288 kctl
->get
= ncontrol
->get
;
289 kctl
->put
= ncontrol
->put
;
290 kctl
->tlv
.p
= ncontrol
->tlv
.p
;
292 kctl
->private_value
= ncontrol
->private_value
;
293 kctl
->private_data
= private_data
;
297 EXPORT_SYMBOL(snd_ctl_new1
);
300 * snd_ctl_free_one - release the control instance
301 * @kcontrol: the control instance
303 * Releases the control instance created via snd_ctl_new()
305 * Don't call this after the control was added to the card.
307 void snd_ctl_free_one(struct snd_kcontrol
*kcontrol
)
310 if (kcontrol
->private_free
)
311 kcontrol
->private_free(kcontrol
);
315 EXPORT_SYMBOL(snd_ctl_free_one
);
317 static bool snd_ctl_remove_numid_conflict(struct snd_card
*card
,
320 struct snd_kcontrol
*kctl
;
322 /* Make sure that the ids assigned to the control do not wrap around */
323 if (card
->last_numid
>= UINT_MAX
- count
)
324 card
->last_numid
= 0;
326 list_for_each_entry(kctl
, &card
->controls
, list
) {
327 if (kctl
->id
.numid
< card
->last_numid
+ 1 + count
&&
328 kctl
->id
.numid
+ kctl
->count
> card
->last_numid
+ 1) {
329 card
->last_numid
= kctl
->id
.numid
+ kctl
->count
- 1;
336 static int snd_ctl_find_hole(struct snd_card
*card
, unsigned int count
)
338 unsigned int iter
= 100000;
340 while (snd_ctl_remove_numid_conflict(card
, count
)) {
342 /* this situation is very unlikely */
343 dev_err(card
->dev
, "unable to allocate new control numid\n");
351 * snd_ctl_add - add the control instance to the card
352 * @card: the card instance
353 * @kcontrol: the control instance to add
355 * Adds the control instance created via snd_ctl_new() or
356 * snd_ctl_new1() to the given card. Assigns also an unique
357 * numid used for fast search.
359 * It frees automatically the control which cannot be added.
361 * Return: Zero if successful, or a negative error code on failure.
364 int snd_ctl_add(struct snd_card
*card
, struct snd_kcontrol
*kcontrol
)
366 struct snd_ctl_elem_id id
;
373 if (snd_BUG_ON(!card
|| !kcontrol
->info
))
376 if (id
.index
> UINT_MAX
- kcontrol
->count
)
379 down_write(&card
->controls_rwsem
);
380 if (snd_ctl_find_id(card
, &id
)) {
381 up_write(&card
->controls_rwsem
);
382 dev_err(card
->dev
, "control %i:%i:%i:%s:%i is already present\n",
391 if (snd_ctl_find_hole(card
, kcontrol
->count
) < 0) {
392 up_write(&card
->controls_rwsem
);
396 list_add_tail(&kcontrol
->list
, &card
->controls
);
397 card
->controls_count
+= kcontrol
->count
;
398 kcontrol
->id
.numid
= card
->last_numid
+ 1;
399 card
->last_numid
+= kcontrol
->count
;
401 count
= kcontrol
->count
;
402 up_write(&card
->controls_rwsem
);
403 for (idx
= 0; idx
< count
; idx
++, id
.index
++, id
.numid
++)
404 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_ADD
, &id
);
408 snd_ctl_free_one(kcontrol
);
411 EXPORT_SYMBOL(snd_ctl_add
);
414 * snd_ctl_replace - replace the control instance of the card
415 * @card: the card instance
416 * @kcontrol: the control instance to replace
417 * @add_on_replace: add the control if not already added
419 * Replaces the given control. If the given control does not exist
420 * and the add_on_replace flag is set, the control is added. If the
421 * control exists, it is destroyed first.
423 * It frees automatically the control which cannot be added or replaced.
425 * Return: Zero if successful, or a negative error code on failure.
427 int snd_ctl_replace(struct snd_card
*card
, struct snd_kcontrol
*kcontrol
,
430 struct snd_ctl_elem_id id
;
433 struct snd_kcontrol
*old
;
438 if (snd_BUG_ON(!card
|| !kcontrol
->info
)) {
443 down_write(&card
->controls_rwsem
);
444 old
= snd_ctl_find_id(card
, &id
);
448 up_write(&card
->controls_rwsem
);
452 ret
= snd_ctl_remove(card
, old
);
454 up_write(&card
->controls_rwsem
);
458 if (snd_ctl_find_hole(card
, kcontrol
->count
) < 0) {
459 up_write(&card
->controls_rwsem
);
463 list_add_tail(&kcontrol
->list
, &card
->controls
);
464 card
->controls_count
+= kcontrol
->count
;
465 kcontrol
->id
.numid
= card
->last_numid
+ 1;
466 card
->last_numid
+= kcontrol
->count
;
468 count
= kcontrol
->count
;
469 up_write(&card
->controls_rwsem
);
470 for (idx
= 0; idx
< count
; idx
++, id
.index
++, id
.numid
++)
471 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_ADD
, &id
);
475 snd_ctl_free_one(kcontrol
);
478 EXPORT_SYMBOL(snd_ctl_replace
);
481 * snd_ctl_remove - remove the control from the card and release it
482 * @card: the card instance
483 * @kcontrol: the control instance to remove
485 * Removes the control from the card and then releases the instance.
486 * You don't need to call snd_ctl_free_one(). You must be in
487 * the write lock - down_write(&card->controls_rwsem).
489 * Return: 0 if successful, or a negative error code on failure.
491 int snd_ctl_remove(struct snd_card
*card
, struct snd_kcontrol
*kcontrol
)
493 struct snd_ctl_elem_id id
;
496 if (snd_BUG_ON(!card
|| !kcontrol
))
498 list_del(&kcontrol
->list
);
499 card
->controls_count
-= kcontrol
->count
;
501 for (idx
= 0; idx
< kcontrol
->count
; idx
++, id
.index
++, id
.numid
++)
502 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_REMOVE
, &id
);
503 snd_ctl_free_one(kcontrol
);
506 EXPORT_SYMBOL(snd_ctl_remove
);
509 * snd_ctl_remove_id - remove the control of the given id and release it
510 * @card: the card instance
511 * @id: the control id to remove
513 * Finds the control instance with the given id, removes it from the
514 * card list and releases it.
516 * Return: 0 if successful, or a negative error code on failure.
518 int snd_ctl_remove_id(struct snd_card
*card
, struct snd_ctl_elem_id
*id
)
520 struct snd_kcontrol
*kctl
;
523 down_write(&card
->controls_rwsem
);
524 kctl
= snd_ctl_find_id(card
, id
);
526 up_write(&card
->controls_rwsem
);
529 ret
= snd_ctl_remove(card
, kctl
);
530 up_write(&card
->controls_rwsem
);
533 EXPORT_SYMBOL(snd_ctl_remove_id
);
536 * snd_ctl_remove_user_ctl - remove and release the unlocked user control
537 * @file: active control handle
538 * @id: the control id to remove
540 * Finds the control instance with the given id, removes it from the
541 * card list and releases it.
543 * Return: 0 if successful, or a negative error code on failure.
545 static int snd_ctl_remove_user_ctl(struct snd_ctl_file
* file
,
546 struct snd_ctl_elem_id
*id
)
548 struct snd_card
*card
= file
->card
;
549 struct snd_kcontrol
*kctl
;
552 down_write(&card
->controls_rwsem
);
553 kctl
= snd_ctl_find_id(card
, id
);
558 if (!(kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_USER
)) {
562 for (idx
= 0; idx
< kctl
->count
; idx
++)
563 if (kctl
->vd
[idx
].owner
!= NULL
&& kctl
->vd
[idx
].owner
!= file
) {
567 ret
= snd_ctl_remove(card
, kctl
);
570 card
->user_ctl_count
--;
572 up_write(&card
->controls_rwsem
);
577 * snd_ctl_activate_id - activate/inactivate the control of the given id
578 * @card: the card instance
579 * @id: the control id to activate/inactivate
580 * @active: non-zero to activate
582 * Finds the control instance with the given id, and activate or
583 * inactivate the control together with notification, if changed.
584 * The given ID data is filled with full information.
586 * Return: 0 if unchanged, 1 if changed, or a negative error code on failure.
588 int snd_ctl_activate_id(struct snd_card
*card
, struct snd_ctl_elem_id
*id
,
591 struct snd_kcontrol
*kctl
;
592 struct snd_kcontrol_volatile
*vd
;
593 unsigned int index_offset
;
596 down_write(&card
->controls_rwsem
);
597 kctl
= snd_ctl_find_id(card
, id
);
602 index_offset
= snd_ctl_get_ioff(kctl
, id
);
603 vd
= &kctl
->vd
[index_offset
];
606 if (!(vd
->access
& SNDRV_CTL_ELEM_ACCESS_INACTIVE
))
608 vd
->access
&= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE
;
610 if (vd
->access
& SNDRV_CTL_ELEM_ACCESS_INACTIVE
)
612 vd
->access
|= SNDRV_CTL_ELEM_ACCESS_INACTIVE
;
614 snd_ctl_build_ioff(id
, kctl
, index_offset
);
617 up_write(&card
->controls_rwsem
);
619 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_INFO
, id
);
622 EXPORT_SYMBOL_GPL(snd_ctl_activate_id
);
625 * snd_ctl_rename_id - replace the id of a control on the card
626 * @card: the card instance
627 * @src_id: the old id
628 * @dst_id: the new id
630 * Finds the control with the old id from the card, and replaces the
631 * id with the new one.
633 * Return: Zero if successful, or a negative error code on failure.
635 int snd_ctl_rename_id(struct snd_card
*card
, struct snd_ctl_elem_id
*src_id
,
636 struct snd_ctl_elem_id
*dst_id
)
638 struct snd_kcontrol
*kctl
;
640 down_write(&card
->controls_rwsem
);
641 kctl
= snd_ctl_find_id(card
, src_id
);
643 up_write(&card
->controls_rwsem
);
647 kctl
->id
.numid
= card
->last_numid
+ 1;
648 card
->last_numid
+= kctl
->count
;
649 up_write(&card
->controls_rwsem
);
652 EXPORT_SYMBOL(snd_ctl_rename_id
);
655 * snd_ctl_find_numid - find the control instance with the given number-id
656 * @card: the card instance
657 * @numid: the number-id to search
659 * Finds the control instance with the given number-id from the card.
661 * The caller must down card->controls_rwsem before calling this function
662 * (if the race condition can happen).
664 * Return: The pointer of the instance if found, or %NULL if not.
667 struct snd_kcontrol
*snd_ctl_find_numid(struct snd_card
*card
, unsigned int numid
)
669 struct snd_kcontrol
*kctl
;
671 if (snd_BUG_ON(!card
|| !numid
))
673 list_for_each_entry(kctl
, &card
->controls
, list
) {
674 if (kctl
->id
.numid
<= numid
&& kctl
->id
.numid
+ kctl
->count
> numid
)
679 EXPORT_SYMBOL(snd_ctl_find_numid
);
682 * snd_ctl_find_id - find the control instance with the given id
683 * @card: the card instance
684 * @id: the id to search
686 * Finds the control instance with the given id from the card.
688 * The caller must down card->controls_rwsem before calling this function
689 * (if the race condition can happen).
691 * Return: The pointer of the instance if found, or %NULL if not.
694 struct snd_kcontrol
*snd_ctl_find_id(struct snd_card
*card
,
695 struct snd_ctl_elem_id
*id
)
697 struct snd_kcontrol
*kctl
;
699 if (snd_BUG_ON(!card
|| !id
))
702 return snd_ctl_find_numid(card
, id
->numid
);
703 list_for_each_entry(kctl
, &card
->controls
, list
) {
704 if (kctl
->id
.iface
!= id
->iface
)
706 if (kctl
->id
.device
!= id
->device
)
708 if (kctl
->id
.subdevice
!= id
->subdevice
)
710 if (strncmp(kctl
->id
.name
, id
->name
, sizeof(kctl
->id
.name
)))
712 if (kctl
->id
.index
> id
->index
)
714 if (kctl
->id
.index
+ kctl
->count
<= id
->index
)
720 EXPORT_SYMBOL(snd_ctl_find_id
);
722 static int snd_ctl_card_info(struct snd_card
*card
, struct snd_ctl_file
* ctl
,
723 unsigned int cmd
, void __user
*arg
)
725 struct snd_ctl_card_info
*info
;
727 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
730 down_read(&snd_ioctl_rwsem
);
731 info
->card
= card
->number
;
732 strlcpy(info
->id
, card
->id
, sizeof(info
->id
));
733 strlcpy(info
->driver
, card
->driver
, sizeof(info
->driver
));
734 strlcpy(info
->name
, card
->shortname
, sizeof(info
->name
));
735 strlcpy(info
->longname
, card
->longname
, sizeof(info
->longname
));
736 strlcpy(info
->mixername
, card
->mixername
, sizeof(info
->mixername
));
737 strlcpy(info
->components
, card
->components
, sizeof(info
->components
));
738 up_read(&snd_ioctl_rwsem
);
739 if (copy_to_user(arg
, info
, sizeof(struct snd_ctl_card_info
))) {
747 static int snd_ctl_elem_list(struct snd_card
*card
,
748 struct snd_ctl_elem_list __user
*_list
)
750 struct snd_ctl_elem_list list
;
751 struct snd_kcontrol
*kctl
;
752 struct snd_ctl_elem_id id
;
753 unsigned int offset
, space
, jidx
;
756 if (copy_from_user(&list
, _list
, sizeof(list
)))
758 offset
= list
.offset
;
761 down_read(&card
->controls_rwsem
);
762 list
.count
= card
->controls_count
;
765 list_for_each_entry(kctl
, &card
->controls
, list
) {
766 if (offset
>= kctl
->count
) {
767 offset
-= kctl
->count
;
770 for (jidx
= offset
; jidx
< kctl
->count
; jidx
++) {
771 snd_ctl_build_ioff(&id
, kctl
, jidx
);
772 if (copy_to_user(list
.pids
+ list
.used
, &id
,
785 up_read(&card
->controls_rwsem
);
786 if (!err
&& copy_to_user(_list
, &list
, sizeof(list
)))
791 static bool validate_element_member_dimension(struct snd_ctl_elem_info
*info
)
793 unsigned int members
;
796 if (info
->dimen
.d
[0] == 0)
800 for (i
= 0; i
< ARRAY_SIZE(info
->dimen
.d
); ++i
) {
801 if (info
->dimen
.d
[i
] == 0)
803 members
*= info
->dimen
.d
[i
];
806 * info->count should be validated in advance, to guarantee
807 * calculation soundness.
809 if (members
> info
->count
)
813 for (++i
; i
< ARRAY_SIZE(info
->dimen
.d
); ++i
) {
814 if (info
->dimen
.d
[i
] > 0)
818 return members
== info
->count
;
821 static int snd_ctl_elem_info(struct snd_ctl_file
*ctl
,
822 struct snd_ctl_elem_info
*info
)
824 struct snd_card
*card
= ctl
->card
;
825 struct snd_kcontrol
*kctl
;
826 struct snd_kcontrol_volatile
*vd
;
827 unsigned int index_offset
;
830 down_read(&card
->controls_rwsem
);
831 kctl
= snd_ctl_find_id(card
, &info
->id
);
833 up_read(&card
->controls_rwsem
);
836 #ifdef CONFIG_SND_DEBUG
839 result
= kctl
->info(kctl
, info
);
841 snd_BUG_ON(info
->access
);
842 index_offset
= snd_ctl_get_ioff(kctl
, &info
->id
);
843 vd
= &kctl
->vd
[index_offset
];
844 snd_ctl_build_ioff(&info
->id
, kctl
, index_offset
);
845 info
->access
= vd
->access
;
847 info
->access
|= SNDRV_CTL_ELEM_ACCESS_LOCK
;
848 if (vd
->owner
== ctl
)
849 info
->access
|= SNDRV_CTL_ELEM_ACCESS_OWNER
;
850 info
->owner
= pid_vnr(vd
->owner
->pid
);
855 up_read(&card
->controls_rwsem
);
859 static int snd_ctl_elem_info_user(struct snd_ctl_file
*ctl
,
860 struct snd_ctl_elem_info __user
*_info
)
862 struct snd_ctl_elem_info info
;
865 if (copy_from_user(&info
, _info
, sizeof(info
)))
867 result
= snd_power_wait(ctl
->card
, SNDRV_CTL_POWER_D0
);
870 result
= snd_ctl_elem_info(ctl
, &info
);
873 if (copy_to_user(_info
, &info
, sizeof(info
)))
878 static int snd_ctl_elem_read(struct snd_card
*card
,
879 struct snd_ctl_elem_value
*control
)
881 struct snd_kcontrol
*kctl
;
882 struct snd_kcontrol_volatile
*vd
;
883 unsigned int index_offset
;
885 kctl
= snd_ctl_find_id(card
, &control
->id
);
889 index_offset
= snd_ctl_get_ioff(kctl
, &control
->id
);
890 vd
= &kctl
->vd
[index_offset
];
891 if (!(vd
->access
& SNDRV_CTL_ELEM_ACCESS_READ
) && kctl
->get
== NULL
)
894 snd_ctl_build_ioff(&control
->id
, kctl
, index_offset
);
895 return kctl
->get(kctl
, control
);
898 static int snd_ctl_elem_read_user(struct snd_card
*card
,
899 struct snd_ctl_elem_value __user
*_control
)
901 struct snd_ctl_elem_value
*control
;
904 control
= memdup_user(_control
, sizeof(*control
));
906 return PTR_ERR(control
);
908 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
912 down_read(&card
->controls_rwsem
);
913 result
= snd_ctl_elem_read(card
, control
);
914 up_read(&card
->controls_rwsem
);
918 if (copy_to_user(_control
, control
, sizeof(*control
)))
925 static int snd_ctl_elem_write(struct snd_card
*card
, struct snd_ctl_file
*file
,
926 struct snd_ctl_elem_value
*control
)
928 struct snd_kcontrol
*kctl
;
929 struct snd_kcontrol_volatile
*vd
;
930 unsigned int index_offset
;
933 kctl
= snd_ctl_find_id(card
, &control
->id
);
937 index_offset
= snd_ctl_get_ioff(kctl
, &control
->id
);
938 vd
= &kctl
->vd
[index_offset
];
939 if (!(vd
->access
& SNDRV_CTL_ELEM_ACCESS_WRITE
) || kctl
->put
== NULL
||
940 (file
&& vd
->owner
&& vd
->owner
!= file
)) {
944 snd_ctl_build_ioff(&control
->id
, kctl
, index_offset
);
945 result
= kctl
->put(kctl
, control
);
950 struct snd_ctl_elem_id id
= control
->id
;
951 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_VALUE
, &id
);
957 static int snd_ctl_elem_write_user(struct snd_ctl_file
*file
,
958 struct snd_ctl_elem_value __user
*_control
)
960 struct snd_ctl_elem_value
*control
;
961 struct snd_card
*card
;
964 control
= memdup_user(_control
, sizeof(*control
));
966 return PTR_ERR(control
);
969 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
973 down_write(&card
->controls_rwsem
);
974 result
= snd_ctl_elem_write(card
, file
, control
);
975 up_write(&card
->controls_rwsem
);
979 if (copy_to_user(_control
, control
, sizeof(*control
)))
986 static int snd_ctl_elem_lock(struct snd_ctl_file
*file
,
987 struct snd_ctl_elem_id __user
*_id
)
989 struct snd_card
*card
= file
->card
;
990 struct snd_ctl_elem_id id
;
991 struct snd_kcontrol
*kctl
;
992 struct snd_kcontrol_volatile
*vd
;
995 if (copy_from_user(&id
, _id
, sizeof(id
)))
997 down_write(&card
->controls_rwsem
);
998 kctl
= snd_ctl_find_id(card
, &id
);
1002 vd
= &kctl
->vd
[snd_ctl_get_ioff(kctl
, &id
)];
1003 if (vd
->owner
!= NULL
)
1010 up_write(&card
->controls_rwsem
);
1014 static int snd_ctl_elem_unlock(struct snd_ctl_file
*file
,
1015 struct snd_ctl_elem_id __user
*_id
)
1017 struct snd_card
*card
= file
->card
;
1018 struct snd_ctl_elem_id id
;
1019 struct snd_kcontrol
*kctl
;
1020 struct snd_kcontrol_volatile
*vd
;
1023 if (copy_from_user(&id
, _id
, sizeof(id
)))
1025 down_write(&card
->controls_rwsem
);
1026 kctl
= snd_ctl_find_id(card
, &id
);
1030 vd
= &kctl
->vd
[snd_ctl_get_ioff(kctl
, &id
)];
1031 if (vd
->owner
== NULL
)
1033 else if (vd
->owner
!= file
)
1040 up_write(&card
->controls_rwsem
);
1044 struct user_element
{
1045 struct snd_ctl_elem_info info
;
1046 struct snd_card
*card
;
1047 char *elem_data
; /* element data */
1048 unsigned long elem_data_size
; /* size of element data in bytes */
1049 void *tlv_data
; /* TLV data */
1050 unsigned long tlv_data_size
; /* TLV data size */
1051 void *priv_data
; /* private data (like strings for enumerated type) */
1054 static int snd_ctl_elem_user_info(struct snd_kcontrol
*kcontrol
,
1055 struct snd_ctl_elem_info
*uinfo
)
1057 struct user_element
*ue
= kcontrol
->private_data
;
1058 unsigned int offset
;
1060 offset
= snd_ctl_get_ioff(kcontrol
, &uinfo
->id
);
1062 snd_ctl_build_ioff(&uinfo
->id
, kcontrol
, offset
);
1067 static int snd_ctl_elem_user_enum_info(struct snd_kcontrol
*kcontrol
,
1068 struct snd_ctl_elem_info
*uinfo
)
1070 struct user_element
*ue
= kcontrol
->private_data
;
1073 unsigned int offset
;
1075 item
= uinfo
->value
.enumerated
.item
;
1077 offset
= snd_ctl_get_ioff(kcontrol
, &uinfo
->id
);
1079 snd_ctl_build_ioff(&uinfo
->id
, kcontrol
, offset
);
1081 item
= min(item
, uinfo
->value
.enumerated
.items
- 1);
1082 uinfo
->value
.enumerated
.item
= item
;
1084 names
= ue
->priv_data
;
1085 for (; item
> 0; --item
)
1086 names
+= strlen(names
) + 1;
1087 strcpy(uinfo
->value
.enumerated
.name
, names
);
1092 static int snd_ctl_elem_user_get(struct snd_kcontrol
*kcontrol
,
1093 struct snd_ctl_elem_value
*ucontrol
)
1095 struct user_element
*ue
= kcontrol
->private_data
;
1096 unsigned int size
= ue
->elem_data_size
;
1097 char *src
= ue
->elem_data
+
1098 snd_ctl_get_ioff(kcontrol
, &ucontrol
->id
) * size
;
1100 memcpy(&ucontrol
->value
, src
, size
);
1104 static int snd_ctl_elem_user_put(struct snd_kcontrol
*kcontrol
,
1105 struct snd_ctl_elem_value
*ucontrol
)
1108 struct user_element
*ue
= kcontrol
->private_data
;
1109 unsigned int size
= ue
->elem_data_size
;
1110 char *dst
= ue
->elem_data
+
1111 snd_ctl_get_ioff(kcontrol
, &ucontrol
->id
) * size
;
1113 change
= memcmp(&ucontrol
->value
, dst
, size
) != 0;
1115 memcpy(dst
, &ucontrol
->value
, size
);
1119 static int replace_user_tlv(struct snd_kcontrol
*kctl
, unsigned int __user
*buf
,
1122 struct user_element
*ue
= kctl
->private_data
;
1123 unsigned int *container
;
1124 struct snd_ctl_elem_id id
;
1125 unsigned int mask
= 0;
1129 if (size
> 1024 * 128) /* sane value */
1132 container
= memdup_user(buf
, size
);
1133 if (IS_ERR(container
))
1134 return PTR_ERR(container
);
1136 change
= ue
->tlv_data_size
!= size
;
1138 change
= memcmp(ue
->tlv_data
, container
, size
) != 0;
1144 if (ue
->tlv_data
== NULL
) {
1145 /* Now TLV data is available. */
1146 for (i
= 0; i
< kctl
->count
; ++i
)
1147 kctl
->vd
[i
].access
|= SNDRV_CTL_ELEM_ACCESS_TLV_READ
;
1148 mask
= SNDRV_CTL_EVENT_MASK_INFO
;
1151 kfree(ue
->tlv_data
);
1152 ue
->tlv_data
= container
;
1153 ue
->tlv_data_size
= size
;
1155 mask
|= SNDRV_CTL_EVENT_MASK_TLV
;
1156 for (i
= 0; i
< kctl
->count
; ++i
) {
1157 snd_ctl_build_ioff(&id
, kctl
, i
);
1158 snd_ctl_notify(ue
->card
, mask
, &id
);
1164 static int read_user_tlv(struct snd_kcontrol
*kctl
, unsigned int __user
*buf
,
1167 struct user_element
*ue
= kctl
->private_data
;
1169 if (ue
->tlv_data_size
== 0 || ue
->tlv_data
== NULL
)
1172 if (size
< ue
->tlv_data_size
)
1175 if (copy_to_user(buf
, ue
->tlv_data
, ue
->tlv_data_size
))
1181 static int snd_ctl_elem_user_tlv(struct snd_kcontrol
*kctl
, int op_flag
,
1182 unsigned int size
, unsigned int __user
*buf
)
1184 if (op_flag
== SNDRV_CTL_TLV_OP_WRITE
)
1185 return replace_user_tlv(kctl
, buf
, size
);
1187 return read_user_tlv(kctl
, buf
, size
);
1190 static int snd_ctl_elem_init_enum_names(struct user_element
*ue
)
1193 size_t buf_len
, name_len
;
1195 const uintptr_t user_ptrval
= ue
->info
.value
.enumerated
.names_ptr
;
1197 if (ue
->info
.value
.enumerated
.names_length
> 64 * 1024)
1200 names
= memdup_user((const void __user
*)user_ptrval
,
1201 ue
->info
.value
.enumerated
.names_length
);
1203 return PTR_ERR(names
);
1205 /* check that there are enough valid names */
1206 buf_len
= ue
->info
.value
.enumerated
.names_length
;
1208 for (i
= 0; i
< ue
->info
.value
.enumerated
.items
; ++i
) {
1209 name_len
= strnlen(p
, buf_len
);
1210 if (name_len
== 0 || name_len
>= 64 || name_len
== buf_len
) {
1215 buf_len
-= name_len
+ 1;
1218 ue
->priv_data
= names
;
1219 ue
->info
.value
.enumerated
.names_ptr
= 0;
1224 static void snd_ctl_elem_user_free(struct snd_kcontrol
*kcontrol
)
1226 struct user_element
*ue
= kcontrol
->private_data
;
1228 kfree(ue
->tlv_data
);
1229 kfree(ue
->priv_data
);
1233 static int snd_ctl_elem_add(struct snd_ctl_file
*file
,
1234 struct snd_ctl_elem_info
*info
, int replace
)
1236 /* The capacity of struct snd_ctl_elem_value.value.*/
1237 static const unsigned int value_sizes
[] = {
1238 [SNDRV_CTL_ELEM_TYPE_BOOLEAN
] = sizeof(long),
1239 [SNDRV_CTL_ELEM_TYPE_INTEGER
] = sizeof(long),
1240 [SNDRV_CTL_ELEM_TYPE_ENUMERATED
] = sizeof(unsigned int),
1241 [SNDRV_CTL_ELEM_TYPE_BYTES
] = sizeof(unsigned char),
1242 [SNDRV_CTL_ELEM_TYPE_IEC958
] = sizeof(struct snd_aes_iec958
),
1243 [SNDRV_CTL_ELEM_TYPE_INTEGER64
] = sizeof(long long),
1245 static const unsigned int max_value_counts
[] = {
1246 [SNDRV_CTL_ELEM_TYPE_BOOLEAN
] = 128,
1247 [SNDRV_CTL_ELEM_TYPE_INTEGER
] = 128,
1248 [SNDRV_CTL_ELEM_TYPE_ENUMERATED
] = 128,
1249 [SNDRV_CTL_ELEM_TYPE_BYTES
] = 512,
1250 [SNDRV_CTL_ELEM_TYPE_IEC958
] = 1,
1251 [SNDRV_CTL_ELEM_TYPE_INTEGER64
] = 64,
1253 struct snd_card
*card
= file
->card
;
1254 struct snd_kcontrol
*kctl
;
1256 unsigned int access
;
1258 struct user_element
*ue
;
1259 unsigned int offset
;
1262 if (!*info
->id
.name
)
1264 if (strnlen(info
->id
.name
, sizeof(info
->id
.name
)) >= sizeof(info
->id
.name
))
1267 /* Delete a control to replace them if needed. */
1270 err
= snd_ctl_remove_user_ctl(file
, &info
->id
);
1276 * The number of userspace controls are counted control by control,
1277 * not element by element.
1279 if (card
->user_ctl_count
+ 1 > MAX_USER_CONTROLS
)
1282 /* Check the number of elements for this userspace control. */
1283 count
= info
->owner
;
1287 /* Arrange access permissions if needed. */
1288 access
= info
->access
;
1290 access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
;
1291 access
&= (SNDRV_CTL_ELEM_ACCESS_READWRITE
|
1292 SNDRV_CTL_ELEM_ACCESS_INACTIVE
|
1293 SNDRV_CTL_ELEM_ACCESS_TLV_WRITE
);
1295 /* In initial state, nothing is available as TLV container. */
1296 if (access
& SNDRV_CTL_ELEM_ACCESS_TLV_WRITE
)
1297 access
|= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
;
1298 access
|= SNDRV_CTL_ELEM_ACCESS_USER
;
1301 * Check information and calculate the size of data specific to
1302 * this userspace control.
1304 if (info
->type
< SNDRV_CTL_ELEM_TYPE_BOOLEAN
||
1305 info
->type
> SNDRV_CTL_ELEM_TYPE_INTEGER64
)
1307 if (info
->type
== SNDRV_CTL_ELEM_TYPE_ENUMERATED
&&
1308 info
->value
.enumerated
.items
== 0)
1310 if (info
->count
< 1 ||
1311 info
->count
> max_value_counts
[info
->type
])
1313 if (!validate_element_member_dimension(info
))
1315 private_size
= value_sizes
[info
->type
] * info
->count
;
1318 * Keep memory object for this userspace control. After passing this
1319 * code block, the instance should be freed by snd_ctl_free_one().
1321 * Note that these elements in this control are locked.
1323 err
= snd_ctl_new(&kctl
, count
, access
, file
);
1326 memcpy(&kctl
->id
, &info
->id
, sizeof(kctl
->id
));
1327 kctl
->private_data
= kzalloc(sizeof(struct user_element
) + private_size
* count
,
1329 if (kctl
->private_data
== NULL
) {
1333 kctl
->private_free
= snd_ctl_elem_user_free
;
1335 /* Set private data for this userspace control. */
1336 ue
= (struct user_element
*)kctl
->private_data
;
1339 ue
->info
.access
= 0;
1340 ue
->elem_data
= (char *)ue
+ sizeof(*ue
);
1341 ue
->elem_data_size
= private_size
;
1342 if (ue
->info
.type
== SNDRV_CTL_ELEM_TYPE_ENUMERATED
) {
1343 err
= snd_ctl_elem_init_enum_names(ue
);
1345 snd_ctl_free_one(kctl
);
1350 /* Set callback functions. */
1351 if (info
->type
== SNDRV_CTL_ELEM_TYPE_ENUMERATED
)
1352 kctl
->info
= snd_ctl_elem_user_enum_info
;
1354 kctl
->info
= snd_ctl_elem_user_info
;
1355 if (access
& SNDRV_CTL_ELEM_ACCESS_READ
)
1356 kctl
->get
= snd_ctl_elem_user_get
;
1357 if (access
& SNDRV_CTL_ELEM_ACCESS_WRITE
)
1358 kctl
->put
= snd_ctl_elem_user_put
;
1359 if (access
& SNDRV_CTL_ELEM_ACCESS_TLV_WRITE
)
1360 kctl
->tlv
.c
= snd_ctl_elem_user_tlv
;
1362 /* This function manage to free the instance on failure. */
1363 err
= snd_ctl_add(card
, kctl
);
1366 offset
= snd_ctl_get_ioff(kctl
, &info
->id
);
1367 snd_ctl_build_ioff(&info
->id
, kctl
, offset
);
1369 * Here we cannot fill any field for the number of elements added by
1370 * this operation because there're no specific fields. The usage of
1371 * 'owner' field for this purpose may cause any bugs to userspace
1372 * applications because the field originally means PID of a process
1373 * which locks the element.
1376 down_write(&card
->controls_rwsem
);
1377 card
->user_ctl_count
++;
1378 up_write(&card
->controls_rwsem
);
1383 static int snd_ctl_elem_add_user(struct snd_ctl_file
*file
,
1384 struct snd_ctl_elem_info __user
*_info
, int replace
)
1386 struct snd_ctl_elem_info info
;
1389 if (copy_from_user(&info
, _info
, sizeof(info
)))
1391 err
= snd_ctl_elem_add(file
, &info
, replace
);
1394 if (copy_to_user(_info
, &info
, sizeof(info
))) {
1395 snd_ctl_remove_user_ctl(file
, &info
.id
);
1402 static int snd_ctl_elem_remove(struct snd_ctl_file
*file
,
1403 struct snd_ctl_elem_id __user
*_id
)
1405 struct snd_ctl_elem_id id
;
1407 if (copy_from_user(&id
, _id
, sizeof(id
)))
1409 return snd_ctl_remove_user_ctl(file
, &id
);
1412 static int snd_ctl_subscribe_events(struct snd_ctl_file
*file
, int __user
*ptr
)
1415 if (get_user(subscribe
, ptr
))
1417 if (subscribe
< 0) {
1418 subscribe
= file
->subscribed
;
1419 if (put_user(subscribe
, ptr
))
1424 file
->subscribed
= 1;
1426 } else if (file
->subscribed
) {
1427 snd_ctl_empty_read_queue(file
);
1428 file
->subscribed
= 0;
1433 static int call_tlv_handler(struct snd_ctl_file
*file
, int op_flag
,
1434 struct snd_kcontrol
*kctl
,
1435 struct snd_ctl_elem_id
*id
,
1436 unsigned int __user
*buf
, unsigned int size
)
1438 static const struct {
1442 {SNDRV_CTL_TLV_OP_READ
, SNDRV_CTL_ELEM_ACCESS_TLV_READ
},
1443 {SNDRV_CTL_TLV_OP_WRITE
, SNDRV_CTL_ELEM_ACCESS_TLV_WRITE
},
1444 {SNDRV_CTL_TLV_OP_CMD
, SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND
},
1446 struct snd_kcontrol_volatile
*vd
= &kctl
->vd
[snd_ctl_get_ioff(kctl
, id
)];
1449 /* Check support of the request for this element. */
1450 for (i
= 0; i
< ARRAY_SIZE(pairs
); ++i
) {
1451 if (op_flag
== pairs
[i
].op
&& (vd
->access
& pairs
[i
].perm
))
1454 if (i
== ARRAY_SIZE(pairs
))
1457 if (kctl
->tlv
.c
== NULL
)
1460 /* When locked, this is unavailable. */
1461 if (vd
->owner
!= NULL
&& vd
->owner
!= file
)
1464 return kctl
->tlv
.c(kctl
, op_flag
, size
, buf
);
1467 static int read_tlv_buf(struct snd_kcontrol
*kctl
, struct snd_ctl_elem_id
*id
,
1468 unsigned int __user
*buf
, unsigned int size
)
1470 struct snd_kcontrol_volatile
*vd
= &kctl
->vd
[snd_ctl_get_ioff(kctl
, id
)];
1473 if (!(vd
->access
& SNDRV_CTL_ELEM_ACCESS_TLV_READ
))
1476 if (kctl
->tlv
.p
== NULL
)
1479 len
= sizeof(unsigned int) * 2 + kctl
->tlv
.p
[1];
1483 if (copy_to_user(buf
, kctl
->tlv
.p
, len
))
1489 static int snd_ctl_tlv_ioctl(struct snd_ctl_file
*file
,
1490 struct snd_ctl_tlv __user
*buf
,
1493 struct snd_ctl_tlv header
;
1494 unsigned int *container
;
1495 unsigned int container_size
;
1496 struct snd_kcontrol
*kctl
;
1497 struct snd_ctl_elem_id id
;
1498 struct snd_kcontrol_volatile
*vd
;
1500 if (copy_from_user(&header
, buf
, sizeof(header
)))
1503 /* In design of control core, numerical ID starts at 1. */
1504 if (header
.numid
== 0)
1507 /* At least, container should include type and length fields. */
1508 if (header
.length
< sizeof(unsigned int) * 2)
1510 container_size
= header
.length
;
1511 container
= buf
->tlv
;
1513 kctl
= snd_ctl_find_numid(file
->card
, header
.numid
);
1517 /* Calculate index of the element in this set. */
1519 snd_ctl_build_ioff(&id
, kctl
, header
.numid
- id
.numid
);
1520 vd
= &kctl
->vd
[snd_ctl_get_ioff(kctl
, &id
)];
1522 if (vd
->access
& SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
) {
1523 return call_tlv_handler(file
, op_flag
, kctl
, &id
, container
,
1526 if (op_flag
== SNDRV_CTL_TLV_OP_READ
) {
1527 return read_tlv_buf(kctl
, &id
, container
,
1532 /* Not supported. */
1536 static long snd_ctl_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1538 struct snd_ctl_file
*ctl
;
1539 struct snd_card
*card
;
1540 struct snd_kctl_ioctl
*p
;
1541 void __user
*argp
= (void __user
*)arg
;
1542 int __user
*ip
= argp
;
1545 ctl
= file
->private_data
;
1547 if (snd_BUG_ON(!card
))
1550 case SNDRV_CTL_IOCTL_PVERSION
:
1551 return put_user(SNDRV_CTL_VERSION
, ip
) ? -EFAULT
: 0;
1552 case SNDRV_CTL_IOCTL_CARD_INFO
:
1553 return snd_ctl_card_info(card
, ctl
, cmd
, argp
);
1554 case SNDRV_CTL_IOCTL_ELEM_LIST
:
1555 return snd_ctl_elem_list(card
, argp
);
1556 case SNDRV_CTL_IOCTL_ELEM_INFO
:
1557 return snd_ctl_elem_info_user(ctl
, argp
);
1558 case SNDRV_CTL_IOCTL_ELEM_READ
:
1559 return snd_ctl_elem_read_user(card
, argp
);
1560 case SNDRV_CTL_IOCTL_ELEM_WRITE
:
1561 return snd_ctl_elem_write_user(ctl
, argp
);
1562 case SNDRV_CTL_IOCTL_ELEM_LOCK
:
1563 return snd_ctl_elem_lock(ctl
, argp
);
1564 case SNDRV_CTL_IOCTL_ELEM_UNLOCK
:
1565 return snd_ctl_elem_unlock(ctl
, argp
);
1566 case SNDRV_CTL_IOCTL_ELEM_ADD
:
1567 return snd_ctl_elem_add_user(ctl
, argp
, 0);
1568 case SNDRV_CTL_IOCTL_ELEM_REPLACE
:
1569 return snd_ctl_elem_add_user(ctl
, argp
, 1);
1570 case SNDRV_CTL_IOCTL_ELEM_REMOVE
:
1571 return snd_ctl_elem_remove(ctl
, argp
);
1572 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS
:
1573 return snd_ctl_subscribe_events(ctl
, ip
);
1574 case SNDRV_CTL_IOCTL_TLV_READ
:
1575 down_read(&ctl
->card
->controls_rwsem
);
1576 err
= snd_ctl_tlv_ioctl(ctl
, argp
, SNDRV_CTL_TLV_OP_READ
);
1577 up_read(&ctl
->card
->controls_rwsem
);
1579 case SNDRV_CTL_IOCTL_TLV_WRITE
:
1580 down_write(&ctl
->card
->controls_rwsem
);
1581 err
= snd_ctl_tlv_ioctl(ctl
, argp
, SNDRV_CTL_TLV_OP_WRITE
);
1582 up_write(&ctl
->card
->controls_rwsem
);
1584 case SNDRV_CTL_IOCTL_TLV_COMMAND
:
1585 down_write(&ctl
->card
->controls_rwsem
);
1586 err
= snd_ctl_tlv_ioctl(ctl
, argp
, SNDRV_CTL_TLV_OP_CMD
);
1587 up_write(&ctl
->card
->controls_rwsem
);
1589 case SNDRV_CTL_IOCTL_POWER
:
1590 return -ENOPROTOOPT
;
1591 case SNDRV_CTL_IOCTL_POWER_STATE
:
1593 return put_user(card
->power_state
, ip
) ? -EFAULT
: 0;
1595 return put_user(SNDRV_CTL_POWER_D0
, ip
) ? -EFAULT
: 0;
1598 down_read(&snd_ioctl_rwsem
);
1599 list_for_each_entry(p
, &snd_control_ioctls
, list
) {
1600 err
= p
->fioctl(card
, ctl
, cmd
, arg
);
1601 if (err
!= -ENOIOCTLCMD
) {
1602 up_read(&snd_ioctl_rwsem
);
1606 up_read(&snd_ioctl_rwsem
);
1607 dev_dbg(card
->dev
, "unknown ioctl = 0x%x\n", cmd
);
1611 static ssize_t
snd_ctl_read(struct file
*file
, char __user
*buffer
,
1612 size_t count
, loff_t
* offset
)
1614 struct snd_ctl_file
*ctl
;
1618 ctl
= file
->private_data
;
1619 if (snd_BUG_ON(!ctl
|| !ctl
->card
))
1621 if (!ctl
->subscribed
)
1623 if (count
< sizeof(struct snd_ctl_event
))
1625 spin_lock_irq(&ctl
->read_lock
);
1626 while (count
>= sizeof(struct snd_ctl_event
)) {
1627 struct snd_ctl_event ev
;
1628 struct snd_kctl_event
*kev
;
1629 while (list_empty(&ctl
->events
)) {
1630 wait_queue_entry_t wait
;
1631 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
1635 init_waitqueue_entry(&wait
, current
);
1636 add_wait_queue(&ctl
->change_sleep
, &wait
);
1637 set_current_state(TASK_INTERRUPTIBLE
);
1638 spin_unlock_irq(&ctl
->read_lock
);
1640 remove_wait_queue(&ctl
->change_sleep
, &wait
);
1641 if (ctl
->card
->shutdown
)
1643 if (signal_pending(current
))
1644 return -ERESTARTSYS
;
1645 spin_lock_irq(&ctl
->read_lock
);
1647 kev
= snd_kctl_event(ctl
->events
.next
);
1648 ev
.type
= SNDRV_CTL_EVENT_ELEM
;
1649 ev
.data
.elem
.mask
= kev
->mask
;
1650 ev
.data
.elem
.id
= kev
->id
;
1651 list_del(&kev
->list
);
1652 spin_unlock_irq(&ctl
->read_lock
);
1654 if (copy_to_user(buffer
, &ev
, sizeof(struct snd_ctl_event
))) {
1658 spin_lock_irq(&ctl
->read_lock
);
1659 buffer
+= sizeof(struct snd_ctl_event
);
1660 count
-= sizeof(struct snd_ctl_event
);
1661 result
+= sizeof(struct snd_ctl_event
);
1664 spin_unlock_irq(&ctl
->read_lock
);
1666 return result
> 0 ? result
: err
;
1669 static unsigned int snd_ctl_poll(struct file
*file
, poll_table
* wait
)
1672 struct snd_ctl_file
*ctl
;
1674 ctl
= file
->private_data
;
1675 if (!ctl
->subscribed
)
1677 poll_wait(file
, &ctl
->change_sleep
, wait
);
1680 if (!list_empty(&ctl
->events
))
1681 mask
|= POLLIN
| POLLRDNORM
;
1687 * register the device-specific control-ioctls.
1688 * called from each device manager like pcm.c, hwdep.c, etc.
1690 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn
, struct list_head
*lists
)
1692 struct snd_kctl_ioctl
*pn
;
1694 pn
= kzalloc(sizeof(struct snd_kctl_ioctl
), GFP_KERNEL
);
1698 down_write(&snd_ioctl_rwsem
);
1699 list_add_tail(&pn
->list
, lists
);
1700 up_write(&snd_ioctl_rwsem
);
1705 * snd_ctl_register_ioctl - register the device-specific control-ioctls
1706 * @fcn: ioctl callback function
1708 * called from each device manager like pcm.c, hwdep.c, etc.
1710 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn
)
1712 return _snd_ctl_register_ioctl(fcn
, &snd_control_ioctls
);
1714 EXPORT_SYMBOL(snd_ctl_register_ioctl
);
1716 #ifdef CONFIG_COMPAT
1718 * snd_ctl_register_ioctl_compat - register the device-specific 32bit compat
1720 * @fcn: ioctl callback function
1722 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn
)
1724 return _snd_ctl_register_ioctl(fcn
, &snd_control_compat_ioctls
);
1726 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat
);
1730 * de-register the device-specific control-ioctls.
1732 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn
,
1733 struct list_head
*lists
)
1735 struct snd_kctl_ioctl
*p
;
1737 if (snd_BUG_ON(!fcn
))
1739 down_write(&snd_ioctl_rwsem
);
1740 list_for_each_entry(p
, lists
, list
) {
1741 if (p
->fioctl
== fcn
) {
1743 up_write(&snd_ioctl_rwsem
);
1748 up_write(&snd_ioctl_rwsem
);
1754 * snd_ctl_unregister_ioctl - de-register the device-specific control-ioctls
1755 * @fcn: ioctl callback function to unregister
1757 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn
)
1759 return _snd_ctl_unregister_ioctl(fcn
, &snd_control_ioctls
);
1761 EXPORT_SYMBOL(snd_ctl_unregister_ioctl
);
1763 #ifdef CONFIG_COMPAT
1765 * snd_ctl_unregister_ioctl - de-register the device-specific compat 32bit
1767 * @fcn: ioctl callback function to unregister
1769 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn
)
1771 return _snd_ctl_unregister_ioctl(fcn
, &snd_control_compat_ioctls
);
1773 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat
);
1776 static int snd_ctl_fasync(int fd
, struct file
* file
, int on
)
1778 struct snd_ctl_file
*ctl
;
1780 ctl
= file
->private_data
;
1781 return fasync_helper(fd
, file
, on
, &ctl
->fasync
);
1784 /* return the preferred subdevice number if already assigned;
1785 * otherwise return -1
1787 int snd_ctl_get_preferred_subdevice(struct snd_card
*card
, int type
)
1789 struct snd_ctl_file
*kctl
;
1792 read_lock(&card
->ctl_files_rwlock
);
1793 list_for_each_entry(kctl
, &card
->ctl_files
, list
) {
1794 if (kctl
->pid
== task_pid(current
)) {
1795 subdevice
= kctl
->preferred_subdevice
[type
];
1796 if (subdevice
!= -1)
1800 read_unlock(&card
->ctl_files_rwlock
);
1803 EXPORT_SYMBOL_GPL(snd_ctl_get_preferred_subdevice
);
1808 #ifdef CONFIG_COMPAT
1809 #include "control_compat.c"
1811 #define snd_ctl_ioctl_compat NULL
1818 static const struct file_operations snd_ctl_f_ops
=
1820 .owner
= THIS_MODULE
,
1821 .read
= snd_ctl_read
,
1822 .open
= snd_ctl_open
,
1823 .release
= snd_ctl_release
,
1824 .llseek
= no_llseek
,
1825 .poll
= snd_ctl_poll
,
1826 .unlocked_ioctl
= snd_ctl_ioctl
,
1827 .compat_ioctl
= snd_ctl_ioctl_compat
,
1828 .fasync
= snd_ctl_fasync
,
1832 * registration of the control device
1834 static int snd_ctl_dev_register(struct snd_device
*device
)
1836 struct snd_card
*card
= device
->device_data
;
1838 return snd_register_device(SNDRV_DEVICE_TYPE_CONTROL
, card
, -1,
1839 &snd_ctl_f_ops
, card
, &card
->ctl_dev
);
1843 * disconnection of the control device
1845 static int snd_ctl_dev_disconnect(struct snd_device
*device
)
1847 struct snd_card
*card
= device
->device_data
;
1848 struct snd_ctl_file
*ctl
;
1850 read_lock(&card
->ctl_files_rwlock
);
1851 list_for_each_entry(ctl
, &card
->ctl_files
, list
) {
1852 wake_up(&ctl
->change_sleep
);
1853 kill_fasync(&ctl
->fasync
, SIGIO
, POLL_ERR
);
1855 read_unlock(&card
->ctl_files_rwlock
);
1857 return snd_unregister_device(&card
->ctl_dev
);
1863 static int snd_ctl_dev_free(struct snd_device
*device
)
1865 struct snd_card
*card
= device
->device_data
;
1866 struct snd_kcontrol
*control
;
1868 down_write(&card
->controls_rwsem
);
1869 while (!list_empty(&card
->controls
)) {
1870 control
= snd_kcontrol(card
->controls
.next
);
1871 snd_ctl_remove(card
, control
);
1873 up_write(&card
->controls_rwsem
);
1874 put_device(&card
->ctl_dev
);
1879 * create control core:
1880 * called from init.c
1882 int snd_ctl_create(struct snd_card
*card
)
1884 static struct snd_device_ops ops
= {
1885 .dev_free
= snd_ctl_dev_free
,
1886 .dev_register
= snd_ctl_dev_register
,
1887 .dev_disconnect
= snd_ctl_dev_disconnect
,
1891 if (snd_BUG_ON(!card
))
1893 if (snd_BUG_ON(card
->number
< 0 || card
->number
>= SNDRV_CARDS
))
1896 snd_device_initialize(&card
->ctl_dev
, card
);
1897 dev_set_name(&card
->ctl_dev
, "controlC%d", card
->number
);
1899 err
= snd_device_new(card
, SNDRV_DEV_CONTROL
, card
, &ops
);
1901 put_device(&card
->ctl_dev
);
1906 * Frequently used control callbacks/helpers
1910 * snd_ctl_boolean_mono_info - Helper function for a standard boolean info
1911 * callback with a mono channel
1912 * @kcontrol: the kcontrol instance
1913 * @uinfo: info to store
1915 * This is a function that can be used as info callback for a standard
1916 * boolean control with a single mono channel.
1918 int snd_ctl_boolean_mono_info(struct snd_kcontrol
*kcontrol
,
1919 struct snd_ctl_elem_info
*uinfo
)
1921 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1923 uinfo
->value
.integer
.min
= 0;
1924 uinfo
->value
.integer
.max
= 1;
1927 EXPORT_SYMBOL(snd_ctl_boolean_mono_info
);
1930 * snd_ctl_boolean_stereo_info - Helper function for a standard boolean info
1931 * callback with stereo two channels
1932 * @kcontrol: the kcontrol instance
1933 * @uinfo: info to store
1935 * This is a function that can be used as info callback for a standard
1936 * boolean control with stereo two channels.
1938 int snd_ctl_boolean_stereo_info(struct snd_kcontrol
*kcontrol
,
1939 struct snd_ctl_elem_info
*uinfo
)
1941 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1943 uinfo
->value
.integer
.min
= 0;
1944 uinfo
->value
.integer
.max
= 1;
1947 EXPORT_SYMBOL(snd_ctl_boolean_stereo_info
);
1950 * snd_ctl_enum_info - fills the info structure for an enumerated control
1951 * @info: the structure to be filled
1952 * @channels: the number of the control's channels; often one
1953 * @items: the number of control values; also the size of @names
1954 * @names: an array containing the names of all control values
1956 * Sets all required fields in @info to their appropriate values.
1957 * If the control's accessibility is not the default (readable and writable),
1958 * the caller has to fill @info->access.
1962 int snd_ctl_enum_info(struct snd_ctl_elem_info
*info
, unsigned int channels
,
1963 unsigned int items
, const char *const names
[])
1965 info
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1966 info
->count
= channels
;
1967 info
->value
.enumerated
.items
= items
;
1970 if (info
->value
.enumerated
.item
>= items
)
1971 info
->value
.enumerated
.item
= items
- 1;
1972 WARN(strlen(names
[info
->value
.enumerated
.item
]) >= sizeof(info
->value
.enumerated
.name
),
1973 "ALSA: too long item name '%s'\n",
1974 names
[info
->value
.enumerated
.item
]);
1975 strlcpy(info
->value
.enumerated
.name
,
1976 names
[info
->value
.enumerated
.item
],
1977 sizeof(info
->value
.enumerated
.name
));
1980 EXPORT_SYMBOL(snd_ctl_enum_info
);