1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Routines for driver control interface
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
7 #include <linux/threads.h>
8 #include <linux/interrupt.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/vmalloc.h>
12 #include <linux/time.h>
14 #include <linux/sched/signal.h>
15 #include <sound/core.h>
16 #include <sound/minors.h>
17 #include <sound/info.h>
18 #include <sound/control.h>
20 /* max number of user-defined controls */
21 #define MAX_USER_CONTROLS 32
22 #define MAX_CONTROL_COUNT 1028
24 struct snd_kctl_ioctl
{
25 struct list_head list
; /* list of all ioctls */
26 snd_kctl_ioctl_func_t fioctl
;
29 static DECLARE_RWSEM(snd_ioctl_rwsem
);
30 static LIST_HEAD(snd_control_ioctls
);
32 static LIST_HEAD(snd_control_compat_ioctls
);
35 static int snd_ctl_open(struct inode
*inode
, struct file
*file
)
38 struct snd_card
*card
;
39 struct snd_ctl_file
*ctl
;
42 err
= stream_open(inode
, file
);
46 card
= snd_lookup_minor_data(iminor(inode
), SNDRV_DEVICE_TYPE_CONTROL
);
51 err
= snd_card_file_add(card
, file
);
56 if (!try_module_get(card
->module
)) {
60 ctl
= kzalloc(sizeof(*ctl
), GFP_KERNEL
);
65 INIT_LIST_HEAD(&ctl
->events
);
66 init_waitqueue_head(&ctl
->change_sleep
);
67 spin_lock_init(&ctl
->read_lock
);
69 for (i
= 0; i
< SND_CTL_SUBDEV_ITEMS
; i
++)
70 ctl
->preferred_subdevice
[i
] = -1;
71 ctl
->pid
= get_pid(task_pid(current
));
72 file
->private_data
= ctl
;
73 write_lock_irqsave(&card
->ctl_files_rwlock
, flags
);
74 list_add_tail(&ctl
->list
, &card
->ctl_files
);
75 write_unlock_irqrestore(&card
->ctl_files_rwlock
, flags
);
80 module_put(card
->module
);
82 snd_card_file_remove(card
, file
);
89 static void snd_ctl_empty_read_queue(struct snd_ctl_file
* ctl
)
92 struct snd_kctl_event
*cread
;
94 spin_lock_irqsave(&ctl
->read_lock
, flags
);
95 while (!list_empty(&ctl
->events
)) {
96 cread
= snd_kctl_event(ctl
->events
.next
);
97 list_del(&cread
->list
);
100 spin_unlock_irqrestore(&ctl
->read_lock
, flags
);
103 static int snd_ctl_release(struct inode
*inode
, struct file
*file
)
106 struct snd_card
*card
;
107 struct snd_ctl_file
*ctl
;
108 struct snd_kcontrol
*control
;
111 ctl
= file
->private_data
;
112 file
->private_data
= NULL
;
114 write_lock_irqsave(&card
->ctl_files_rwlock
, flags
);
115 list_del(&ctl
->list
);
116 write_unlock_irqrestore(&card
->ctl_files_rwlock
, flags
);
117 down_write(&card
->controls_rwsem
);
118 list_for_each_entry(control
, &card
->controls
, list
)
119 for (idx
= 0; idx
< control
->count
; idx
++)
120 if (control
->vd
[idx
].owner
== ctl
)
121 control
->vd
[idx
].owner
= NULL
;
122 up_write(&card
->controls_rwsem
);
123 snd_ctl_empty_read_queue(ctl
);
126 module_put(card
->module
);
127 snd_card_file_remove(card
, file
);
132 * snd_ctl_notify - Send notification to user-space for a control change
133 * @card: the card to send notification
134 * @mask: the event mask, SNDRV_CTL_EVENT_*
135 * @id: the ctl element id to send notification
137 * This function adds an event record with the given id and mask, appends
138 * to the list and wakes up the user-space for notification. This can be
139 * called in the atomic context.
141 void snd_ctl_notify(struct snd_card
*card
, unsigned int mask
,
142 struct snd_ctl_elem_id
*id
)
145 struct snd_ctl_file
*ctl
;
146 struct snd_kctl_event
*ev
;
148 if (snd_BUG_ON(!card
|| !id
))
152 read_lock(&card
->ctl_files_rwlock
);
153 #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
154 card
->mixer_oss_change_count
++;
156 list_for_each_entry(ctl
, &card
->ctl_files
, list
) {
157 if (!ctl
->subscribed
)
159 spin_lock_irqsave(&ctl
->read_lock
, flags
);
160 list_for_each_entry(ev
, &ctl
->events
, list
) {
161 if (ev
->id
.numid
== id
->numid
) {
166 ev
= kzalloc(sizeof(*ev
), GFP_ATOMIC
);
170 list_add_tail(&ev
->list
, &ctl
->events
);
172 dev_err(card
->dev
, "No memory available to allocate event\n");
175 wake_up(&ctl
->change_sleep
);
176 spin_unlock_irqrestore(&ctl
->read_lock
, flags
);
177 kill_fasync(&ctl
->fasync
, SIGIO
, POLL_IN
);
179 read_unlock(&card
->ctl_files_rwlock
);
181 EXPORT_SYMBOL(snd_ctl_notify
);
184 * snd_ctl_new - create a new control instance with some elements
185 * @kctl: the pointer to store new control instance
186 * @count: the number of elements in this control
187 * @access: the default access flags for elements in this control
188 * @file: given when locking these elements
190 * Allocates a memory object for a new control instance. The instance has
191 * elements as many as the given number (@count). Each element has given
192 * access permissions (@access). Each element is locked when @file is given.
194 * Return: 0 on success, error code on failure
196 static int snd_ctl_new(struct snd_kcontrol
**kctl
, unsigned int count
,
197 unsigned int access
, struct snd_ctl_file
*file
)
202 if (count
== 0 || count
> MAX_CONTROL_COUNT
)
205 size
= sizeof(struct snd_kcontrol
);
206 size
+= sizeof(struct snd_kcontrol_volatile
) * count
;
208 *kctl
= kzalloc(size
, GFP_KERNEL
);
212 for (idx
= 0; idx
< count
; idx
++) {
213 (*kctl
)->vd
[idx
].access
= access
;
214 (*kctl
)->vd
[idx
].owner
= file
;
216 (*kctl
)->count
= count
;
222 * snd_ctl_new1 - create a control instance from the template
223 * @ncontrol: the initialization record
224 * @private_data: the private data to set
226 * Allocates a new struct snd_kcontrol instance and initialize from the given
227 * template. When the access field of ncontrol is 0, it's assumed as
228 * READWRITE access. When the count field is 0, it's assumes as one.
230 * Return: The pointer of the newly generated instance, or %NULL on failure.
232 struct snd_kcontrol
*snd_ctl_new1(const struct snd_kcontrol_new
*ncontrol
,
235 struct snd_kcontrol
*kctl
;
240 if (snd_BUG_ON(!ncontrol
|| !ncontrol
->info
))
243 count
= ncontrol
->count
;
247 access
= ncontrol
->access
;
249 access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
;
250 access
&= (SNDRV_CTL_ELEM_ACCESS_READWRITE
|
251 SNDRV_CTL_ELEM_ACCESS_VOLATILE
|
252 SNDRV_CTL_ELEM_ACCESS_INACTIVE
|
253 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
|
254 SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND
|
255 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
);
257 err
= snd_ctl_new(&kctl
, count
, access
, NULL
);
261 /* The 'numid' member is decided when calling snd_ctl_add(). */
262 kctl
->id
.iface
= ncontrol
->iface
;
263 kctl
->id
.device
= ncontrol
->device
;
264 kctl
->id
.subdevice
= ncontrol
->subdevice
;
265 if (ncontrol
->name
) {
266 strlcpy(kctl
->id
.name
, ncontrol
->name
, sizeof(kctl
->id
.name
));
267 if (strcmp(ncontrol
->name
, kctl
->id
.name
) != 0)
268 pr_warn("ALSA: Control name '%s' truncated to '%s'\n",
269 ncontrol
->name
, kctl
->id
.name
);
271 kctl
->id
.index
= ncontrol
->index
;
273 kctl
->info
= ncontrol
->info
;
274 kctl
->get
= ncontrol
->get
;
275 kctl
->put
= ncontrol
->put
;
276 kctl
->tlv
.p
= ncontrol
->tlv
.p
;
278 kctl
->private_value
= ncontrol
->private_value
;
279 kctl
->private_data
= private_data
;
283 EXPORT_SYMBOL(snd_ctl_new1
);
286 * snd_ctl_free_one - release the control instance
287 * @kcontrol: the control instance
289 * Releases the control instance created via snd_ctl_new()
291 * Don't call this after the control was added to the card.
293 void snd_ctl_free_one(struct snd_kcontrol
*kcontrol
)
296 if (kcontrol
->private_free
)
297 kcontrol
->private_free(kcontrol
);
301 EXPORT_SYMBOL(snd_ctl_free_one
);
303 static bool snd_ctl_remove_numid_conflict(struct snd_card
*card
,
306 struct snd_kcontrol
*kctl
;
308 /* Make sure that the ids assigned to the control do not wrap around */
309 if (card
->last_numid
>= UINT_MAX
- count
)
310 card
->last_numid
= 0;
312 list_for_each_entry(kctl
, &card
->controls
, list
) {
313 if (kctl
->id
.numid
< card
->last_numid
+ 1 + count
&&
314 kctl
->id
.numid
+ kctl
->count
> card
->last_numid
+ 1) {
315 card
->last_numid
= kctl
->id
.numid
+ kctl
->count
- 1;
322 static int snd_ctl_find_hole(struct snd_card
*card
, unsigned int count
)
324 unsigned int iter
= 100000;
326 while (snd_ctl_remove_numid_conflict(card
, count
)) {
328 /* this situation is very unlikely */
329 dev_err(card
->dev
, "unable to allocate new control numid\n");
336 enum snd_ctl_add_mode
{
337 CTL_ADD_EXCLUSIVE
, CTL_REPLACE
, CTL_ADD_ON_REPLACE
,
340 /* add/replace a new kcontrol object; call with card->controls_rwsem locked */
341 static int __snd_ctl_add_replace(struct snd_card
*card
,
342 struct snd_kcontrol
*kcontrol
,
343 enum snd_ctl_add_mode mode
)
345 struct snd_ctl_elem_id id
;
348 struct snd_kcontrol
*old
;
352 if (id
.index
> UINT_MAX
- kcontrol
->count
)
355 old
= snd_ctl_find_id(card
, &id
);
357 if (mode
== CTL_REPLACE
)
360 if (mode
== CTL_ADD_EXCLUSIVE
) {
362 "control %i:%i:%i:%s:%i is already present\n",
363 id
.iface
, id
.device
, id
.subdevice
, id
.name
,
368 err
= snd_ctl_remove(card
, old
);
373 if (snd_ctl_find_hole(card
, kcontrol
->count
) < 0)
376 list_add_tail(&kcontrol
->list
, &card
->controls
);
377 card
->controls_count
+= kcontrol
->count
;
378 kcontrol
->id
.numid
= card
->last_numid
+ 1;
379 card
->last_numid
+= kcontrol
->count
;
382 count
= kcontrol
->count
;
383 for (idx
= 0; idx
< count
; idx
++, id
.index
++, id
.numid
++)
384 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_ADD
, &id
);
389 static int snd_ctl_add_replace(struct snd_card
*card
,
390 struct snd_kcontrol
*kcontrol
,
391 enum snd_ctl_add_mode mode
)
397 if (snd_BUG_ON(!card
|| !kcontrol
->info
))
400 down_write(&card
->controls_rwsem
);
401 err
= __snd_ctl_add_replace(card
, kcontrol
, mode
);
402 up_write(&card
->controls_rwsem
);
408 snd_ctl_free_one(kcontrol
);
413 * snd_ctl_add - add the control instance to the card
414 * @card: the card instance
415 * @kcontrol: the control instance to add
417 * Adds the control instance created via snd_ctl_new() or
418 * snd_ctl_new1() to the given card. Assigns also an unique
419 * numid used for fast search.
421 * It frees automatically the control which cannot be added.
423 * Return: Zero if successful, or a negative error code on failure.
426 int snd_ctl_add(struct snd_card
*card
, struct snd_kcontrol
*kcontrol
)
428 return snd_ctl_add_replace(card
, kcontrol
, CTL_ADD_EXCLUSIVE
);
430 EXPORT_SYMBOL(snd_ctl_add
);
433 * snd_ctl_replace - replace the control instance of the card
434 * @card: the card instance
435 * @kcontrol: the control instance to replace
436 * @add_on_replace: add the control if not already added
438 * Replaces the given control. If the given control does not exist
439 * and the add_on_replace flag is set, the control is added. If the
440 * control exists, it is destroyed first.
442 * It frees automatically the control which cannot be added or replaced.
444 * Return: Zero if successful, or a negative error code on failure.
446 int snd_ctl_replace(struct snd_card
*card
, struct snd_kcontrol
*kcontrol
,
449 return snd_ctl_add_replace(card
, kcontrol
,
450 add_on_replace
? CTL_ADD_ON_REPLACE
: CTL_REPLACE
);
452 EXPORT_SYMBOL(snd_ctl_replace
);
455 * snd_ctl_remove - remove the control from the card and release it
456 * @card: the card instance
457 * @kcontrol: the control instance to remove
459 * Removes the control from the card and then releases the instance.
460 * You don't need to call snd_ctl_free_one(). You must be in
461 * the write lock - down_write(&card->controls_rwsem).
463 * Return: 0 if successful, or a negative error code on failure.
465 int snd_ctl_remove(struct snd_card
*card
, struct snd_kcontrol
*kcontrol
)
467 struct snd_ctl_elem_id id
;
470 if (snd_BUG_ON(!card
|| !kcontrol
))
472 list_del(&kcontrol
->list
);
473 card
->controls_count
-= kcontrol
->count
;
475 for (idx
= 0; idx
< kcontrol
->count
; idx
++, id
.index
++, id
.numid
++)
476 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_REMOVE
, &id
);
477 snd_ctl_free_one(kcontrol
);
480 EXPORT_SYMBOL(snd_ctl_remove
);
483 * snd_ctl_remove_id - remove the control of the given id and release it
484 * @card: the card instance
485 * @id: the control id to remove
487 * Finds the control instance with the given id, removes it from the
488 * card list and releases it.
490 * Return: 0 if successful, or a negative error code on failure.
492 int snd_ctl_remove_id(struct snd_card
*card
, struct snd_ctl_elem_id
*id
)
494 struct snd_kcontrol
*kctl
;
497 down_write(&card
->controls_rwsem
);
498 kctl
= snd_ctl_find_id(card
, id
);
500 up_write(&card
->controls_rwsem
);
503 ret
= snd_ctl_remove(card
, kctl
);
504 up_write(&card
->controls_rwsem
);
507 EXPORT_SYMBOL(snd_ctl_remove_id
);
510 * snd_ctl_remove_user_ctl - remove and release the unlocked user control
511 * @file: active control handle
512 * @id: the control id to remove
514 * Finds the control instance with the given id, removes it from the
515 * card list and releases it.
517 * Return: 0 if successful, or a negative error code on failure.
519 static int snd_ctl_remove_user_ctl(struct snd_ctl_file
* file
,
520 struct snd_ctl_elem_id
*id
)
522 struct snd_card
*card
= file
->card
;
523 struct snd_kcontrol
*kctl
;
526 down_write(&card
->controls_rwsem
);
527 kctl
= snd_ctl_find_id(card
, id
);
532 if (!(kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_USER
)) {
536 for (idx
= 0; idx
< kctl
->count
; idx
++)
537 if (kctl
->vd
[idx
].owner
!= NULL
&& kctl
->vd
[idx
].owner
!= file
) {
541 ret
= snd_ctl_remove(card
, kctl
);
544 card
->user_ctl_count
--;
546 up_write(&card
->controls_rwsem
);
551 * snd_ctl_activate_id - activate/inactivate the control of the given id
552 * @card: the card instance
553 * @id: the control id to activate/inactivate
554 * @active: non-zero to activate
556 * Finds the control instance with the given id, and activate or
557 * inactivate the control together with notification, if changed.
558 * The given ID data is filled with full information.
560 * Return: 0 if unchanged, 1 if changed, or a negative error code on failure.
562 int snd_ctl_activate_id(struct snd_card
*card
, struct snd_ctl_elem_id
*id
,
565 struct snd_kcontrol
*kctl
;
566 struct snd_kcontrol_volatile
*vd
;
567 unsigned int index_offset
;
570 down_write(&card
->controls_rwsem
);
571 kctl
= snd_ctl_find_id(card
, id
);
576 index_offset
= snd_ctl_get_ioff(kctl
, id
);
577 vd
= &kctl
->vd
[index_offset
];
580 if (!(vd
->access
& SNDRV_CTL_ELEM_ACCESS_INACTIVE
))
582 vd
->access
&= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE
;
584 if (vd
->access
& SNDRV_CTL_ELEM_ACCESS_INACTIVE
)
586 vd
->access
|= SNDRV_CTL_ELEM_ACCESS_INACTIVE
;
588 snd_ctl_build_ioff(id
, kctl
, index_offset
);
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 snd_ctl_elem_list list
;
725 struct snd_kcontrol
*kctl
;
726 struct snd_ctl_elem_id id
;
727 unsigned int offset
, space
, jidx
;
730 if (copy_from_user(&list
, _list
, sizeof(list
)))
732 offset
= list
.offset
;
735 down_read(&card
->controls_rwsem
);
736 list
.count
= card
->controls_count
;
739 list_for_each_entry(kctl
, &card
->controls
, list
) {
740 if (offset
>= kctl
->count
) {
741 offset
-= kctl
->count
;
744 for (jidx
= offset
; jidx
< kctl
->count
; jidx
++) {
745 snd_ctl_build_ioff(&id
, kctl
, jidx
);
746 if (copy_to_user(list
.pids
+ list
.used
, &id
,
759 up_read(&card
->controls_rwsem
);
760 if (!err
&& copy_to_user(_list
, &list
, sizeof(list
)))
765 static bool validate_element_member_dimension(struct snd_ctl_elem_info
*info
)
767 unsigned int members
;
770 if (info
->dimen
.d
[0] == 0)
774 for (i
= 0; i
< ARRAY_SIZE(info
->dimen
.d
); ++i
) {
775 if (info
->dimen
.d
[i
] == 0)
777 members
*= info
->dimen
.d
[i
];
780 * info->count should be validated in advance, to guarantee
781 * calculation soundness.
783 if (members
> info
->count
)
787 for (++i
; i
< ARRAY_SIZE(info
->dimen
.d
); ++i
) {
788 if (info
->dimen
.d
[i
] > 0)
792 return members
== info
->count
;
795 static int snd_ctl_elem_info(struct snd_ctl_file
*ctl
,
796 struct snd_ctl_elem_info
*info
)
798 struct snd_card
*card
= ctl
->card
;
799 struct snd_kcontrol
*kctl
;
800 struct snd_kcontrol_volatile
*vd
;
801 unsigned int index_offset
;
804 down_read(&card
->controls_rwsem
);
805 kctl
= snd_ctl_find_id(card
, &info
->id
);
807 up_read(&card
->controls_rwsem
);
810 #ifdef CONFIG_SND_DEBUG
813 result
= kctl
->info(kctl
, info
);
815 snd_BUG_ON(info
->access
);
816 index_offset
= snd_ctl_get_ioff(kctl
, &info
->id
);
817 vd
= &kctl
->vd
[index_offset
];
818 snd_ctl_build_ioff(&info
->id
, kctl
, index_offset
);
819 info
->access
= vd
->access
;
821 info
->access
|= SNDRV_CTL_ELEM_ACCESS_LOCK
;
822 if (vd
->owner
== ctl
)
823 info
->access
|= SNDRV_CTL_ELEM_ACCESS_OWNER
;
824 info
->owner
= pid_vnr(vd
->owner
->pid
);
829 up_read(&card
->controls_rwsem
);
833 static int snd_ctl_elem_info_user(struct snd_ctl_file
*ctl
,
834 struct snd_ctl_elem_info __user
*_info
)
836 struct snd_ctl_elem_info info
;
839 if (copy_from_user(&info
, _info
, sizeof(info
)))
841 result
= snd_power_wait(ctl
->card
, SNDRV_CTL_POWER_D0
);
844 result
= snd_ctl_elem_info(ctl
, &info
);
847 if (copy_to_user(_info
, &info
, sizeof(info
)))
852 static int snd_ctl_elem_read(struct snd_card
*card
,
853 struct snd_ctl_elem_value
*control
)
855 struct snd_kcontrol
*kctl
;
856 struct snd_kcontrol_volatile
*vd
;
857 unsigned int index_offset
;
859 kctl
= snd_ctl_find_id(card
, &control
->id
);
863 index_offset
= snd_ctl_get_ioff(kctl
, &control
->id
);
864 vd
= &kctl
->vd
[index_offset
];
865 if (!(vd
->access
& SNDRV_CTL_ELEM_ACCESS_READ
) || kctl
->get
== NULL
)
868 snd_ctl_build_ioff(&control
->id
, kctl
, index_offset
);
869 return kctl
->get(kctl
, control
);
872 static int snd_ctl_elem_read_user(struct snd_card
*card
,
873 struct snd_ctl_elem_value __user
*_control
)
875 struct snd_ctl_elem_value
*control
;
878 control
= memdup_user(_control
, sizeof(*control
));
880 return PTR_ERR(control
);
882 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
886 down_read(&card
->controls_rwsem
);
887 result
= snd_ctl_elem_read(card
, control
);
888 up_read(&card
->controls_rwsem
);
892 if (copy_to_user(_control
, control
, sizeof(*control
)))
899 static int snd_ctl_elem_write(struct snd_card
*card
, struct snd_ctl_file
*file
,
900 struct snd_ctl_elem_value
*control
)
902 struct snd_kcontrol
*kctl
;
903 struct snd_kcontrol_volatile
*vd
;
904 unsigned int index_offset
;
907 kctl
= snd_ctl_find_id(card
, &control
->id
);
911 index_offset
= snd_ctl_get_ioff(kctl
, &control
->id
);
912 vd
= &kctl
->vd
[index_offset
];
913 if (!(vd
->access
& SNDRV_CTL_ELEM_ACCESS_WRITE
) || kctl
->put
== NULL
||
914 (file
&& vd
->owner
&& vd
->owner
!= file
)) {
918 snd_ctl_build_ioff(&control
->id
, kctl
, index_offset
);
919 result
= kctl
->put(kctl
, control
);
924 struct snd_ctl_elem_id id
= control
->id
;
925 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_VALUE
, &id
);
931 static int snd_ctl_elem_write_user(struct snd_ctl_file
*file
,
932 struct snd_ctl_elem_value __user
*_control
)
934 struct snd_ctl_elem_value
*control
;
935 struct snd_card
*card
;
938 control
= memdup_user(_control
, sizeof(*control
));
940 return PTR_ERR(control
);
943 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
947 down_write(&card
->controls_rwsem
);
948 result
= snd_ctl_elem_write(card
, file
, control
);
949 up_write(&card
->controls_rwsem
);
953 if (copy_to_user(_control
, control
, sizeof(*control
)))
960 static int snd_ctl_elem_lock(struct snd_ctl_file
*file
,
961 struct snd_ctl_elem_id __user
*_id
)
963 struct snd_card
*card
= file
->card
;
964 struct snd_ctl_elem_id id
;
965 struct snd_kcontrol
*kctl
;
966 struct snd_kcontrol_volatile
*vd
;
969 if (copy_from_user(&id
, _id
, sizeof(id
)))
971 down_write(&card
->controls_rwsem
);
972 kctl
= snd_ctl_find_id(card
, &id
);
976 vd
= &kctl
->vd
[snd_ctl_get_ioff(kctl
, &id
)];
977 if (vd
->owner
!= NULL
)
984 up_write(&card
->controls_rwsem
);
988 static int snd_ctl_elem_unlock(struct snd_ctl_file
*file
,
989 struct snd_ctl_elem_id __user
*_id
)
991 struct snd_card
*card
= file
->card
;
992 struct snd_ctl_elem_id id
;
993 struct snd_kcontrol
*kctl
;
994 struct snd_kcontrol_volatile
*vd
;
997 if (copy_from_user(&id
, _id
, sizeof(id
)))
999 down_write(&card
->controls_rwsem
);
1000 kctl
= snd_ctl_find_id(card
, &id
);
1004 vd
= &kctl
->vd
[snd_ctl_get_ioff(kctl
, &id
)];
1005 if (vd
->owner
== NULL
)
1007 else if (vd
->owner
!= file
)
1014 up_write(&card
->controls_rwsem
);
1018 struct user_element
{
1019 struct snd_ctl_elem_info info
;
1020 struct snd_card
*card
;
1021 char *elem_data
; /* element data */
1022 unsigned long elem_data_size
; /* size of element data in bytes */
1023 void *tlv_data
; /* TLV data */
1024 unsigned long tlv_data_size
; /* TLV data size */
1025 void *priv_data
; /* private data (like strings for enumerated type) */
1028 static int snd_ctl_elem_user_info(struct snd_kcontrol
*kcontrol
,
1029 struct snd_ctl_elem_info
*uinfo
)
1031 struct user_element
*ue
= kcontrol
->private_data
;
1032 unsigned int offset
;
1034 offset
= snd_ctl_get_ioff(kcontrol
, &uinfo
->id
);
1036 snd_ctl_build_ioff(&uinfo
->id
, kcontrol
, offset
);
1041 static int snd_ctl_elem_user_enum_info(struct snd_kcontrol
*kcontrol
,
1042 struct snd_ctl_elem_info
*uinfo
)
1044 struct user_element
*ue
= kcontrol
->private_data
;
1047 unsigned int offset
;
1049 item
= uinfo
->value
.enumerated
.item
;
1051 offset
= snd_ctl_get_ioff(kcontrol
, &uinfo
->id
);
1053 snd_ctl_build_ioff(&uinfo
->id
, kcontrol
, offset
);
1055 item
= min(item
, uinfo
->value
.enumerated
.items
- 1);
1056 uinfo
->value
.enumerated
.item
= item
;
1058 names
= ue
->priv_data
;
1059 for (; item
> 0; --item
)
1060 names
+= strlen(names
) + 1;
1061 strcpy(uinfo
->value
.enumerated
.name
, names
);
1066 static int snd_ctl_elem_user_get(struct snd_kcontrol
*kcontrol
,
1067 struct snd_ctl_elem_value
*ucontrol
)
1069 struct user_element
*ue
= kcontrol
->private_data
;
1070 unsigned int size
= ue
->elem_data_size
;
1071 char *src
= ue
->elem_data
+
1072 snd_ctl_get_ioff(kcontrol
, &ucontrol
->id
) * size
;
1074 memcpy(&ucontrol
->value
, src
, size
);
1078 static int snd_ctl_elem_user_put(struct snd_kcontrol
*kcontrol
,
1079 struct snd_ctl_elem_value
*ucontrol
)
1082 struct user_element
*ue
= kcontrol
->private_data
;
1083 unsigned int size
= ue
->elem_data_size
;
1084 char *dst
= ue
->elem_data
+
1085 snd_ctl_get_ioff(kcontrol
, &ucontrol
->id
) * size
;
1087 change
= memcmp(&ucontrol
->value
, dst
, size
) != 0;
1089 memcpy(dst
, &ucontrol
->value
, size
);
1093 static int replace_user_tlv(struct snd_kcontrol
*kctl
, unsigned int __user
*buf
,
1096 struct user_element
*ue
= kctl
->private_data
;
1097 unsigned int *container
;
1098 struct snd_ctl_elem_id id
;
1099 unsigned int mask
= 0;
1103 if (size
> 1024 * 128) /* sane value */
1106 container
= vmemdup_user(buf
, size
);
1107 if (IS_ERR(container
))
1108 return PTR_ERR(container
);
1110 change
= ue
->tlv_data_size
!= size
;
1112 change
= memcmp(ue
->tlv_data
, container
, size
) != 0;
1118 if (ue
->tlv_data
== NULL
) {
1119 /* Now TLV data is available. */
1120 for (i
= 0; i
< kctl
->count
; ++i
)
1121 kctl
->vd
[i
].access
|= SNDRV_CTL_ELEM_ACCESS_TLV_READ
;
1122 mask
= SNDRV_CTL_EVENT_MASK_INFO
;
1125 kvfree(ue
->tlv_data
);
1126 ue
->tlv_data
= container
;
1127 ue
->tlv_data_size
= size
;
1129 mask
|= SNDRV_CTL_EVENT_MASK_TLV
;
1130 for (i
= 0; i
< kctl
->count
; ++i
) {
1131 snd_ctl_build_ioff(&id
, kctl
, i
);
1132 snd_ctl_notify(ue
->card
, mask
, &id
);
1138 static int read_user_tlv(struct snd_kcontrol
*kctl
, unsigned int __user
*buf
,
1141 struct user_element
*ue
= kctl
->private_data
;
1143 if (ue
->tlv_data_size
== 0 || ue
->tlv_data
== NULL
)
1146 if (size
< ue
->tlv_data_size
)
1149 if (copy_to_user(buf
, ue
->tlv_data
, ue
->tlv_data_size
))
1155 static int snd_ctl_elem_user_tlv(struct snd_kcontrol
*kctl
, int op_flag
,
1156 unsigned int size
, unsigned int __user
*buf
)
1158 if (op_flag
== SNDRV_CTL_TLV_OP_WRITE
)
1159 return replace_user_tlv(kctl
, buf
, size
);
1161 return read_user_tlv(kctl
, buf
, size
);
1164 static int snd_ctl_elem_init_enum_names(struct user_element
*ue
)
1167 size_t buf_len
, name_len
;
1169 const uintptr_t user_ptrval
= ue
->info
.value
.enumerated
.names_ptr
;
1171 if (ue
->info
.value
.enumerated
.names_length
> 64 * 1024)
1174 names
= vmemdup_user((const void __user
*)user_ptrval
,
1175 ue
->info
.value
.enumerated
.names_length
);
1177 return PTR_ERR(names
);
1179 /* check that there are enough valid names */
1180 buf_len
= ue
->info
.value
.enumerated
.names_length
;
1182 for (i
= 0; i
< ue
->info
.value
.enumerated
.items
; ++i
) {
1183 name_len
= strnlen(p
, buf_len
);
1184 if (name_len
== 0 || name_len
>= 64 || name_len
== buf_len
) {
1189 buf_len
-= name_len
+ 1;
1192 ue
->priv_data
= names
;
1193 ue
->info
.value
.enumerated
.names_ptr
= 0;
1198 static void snd_ctl_elem_user_free(struct snd_kcontrol
*kcontrol
)
1200 struct user_element
*ue
= kcontrol
->private_data
;
1202 kvfree(ue
->tlv_data
);
1203 kvfree(ue
->priv_data
);
1207 static int snd_ctl_elem_add(struct snd_ctl_file
*file
,
1208 struct snd_ctl_elem_info
*info
, int replace
)
1210 /* The capacity of struct snd_ctl_elem_value.value.*/
1211 static const unsigned int value_sizes
[] = {
1212 [SNDRV_CTL_ELEM_TYPE_BOOLEAN
] = sizeof(long),
1213 [SNDRV_CTL_ELEM_TYPE_INTEGER
] = sizeof(long),
1214 [SNDRV_CTL_ELEM_TYPE_ENUMERATED
] = sizeof(unsigned int),
1215 [SNDRV_CTL_ELEM_TYPE_BYTES
] = sizeof(unsigned char),
1216 [SNDRV_CTL_ELEM_TYPE_IEC958
] = sizeof(struct snd_aes_iec958
),
1217 [SNDRV_CTL_ELEM_TYPE_INTEGER64
] = sizeof(long long),
1219 static const unsigned int max_value_counts
[] = {
1220 [SNDRV_CTL_ELEM_TYPE_BOOLEAN
] = 128,
1221 [SNDRV_CTL_ELEM_TYPE_INTEGER
] = 128,
1222 [SNDRV_CTL_ELEM_TYPE_ENUMERATED
] = 128,
1223 [SNDRV_CTL_ELEM_TYPE_BYTES
] = 512,
1224 [SNDRV_CTL_ELEM_TYPE_IEC958
] = 1,
1225 [SNDRV_CTL_ELEM_TYPE_INTEGER64
] = 64,
1227 struct snd_card
*card
= file
->card
;
1228 struct snd_kcontrol
*kctl
;
1230 unsigned int access
;
1232 struct user_element
*ue
;
1233 unsigned int offset
;
1236 if (!*info
->id
.name
)
1238 if (strnlen(info
->id
.name
, sizeof(info
->id
.name
)) >= sizeof(info
->id
.name
))
1241 /* Delete a control to replace them if needed. */
1244 err
= snd_ctl_remove_user_ctl(file
, &info
->id
);
1250 * The number of userspace controls are counted control by control,
1251 * not element by element.
1253 if (card
->user_ctl_count
+ 1 > MAX_USER_CONTROLS
)
1256 /* Check the number of elements for this userspace control. */
1257 count
= info
->owner
;
1261 /* Arrange access permissions if needed. */
1262 access
= info
->access
;
1264 access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
;
1265 access
&= (SNDRV_CTL_ELEM_ACCESS_READWRITE
|
1266 SNDRV_CTL_ELEM_ACCESS_INACTIVE
|
1267 SNDRV_CTL_ELEM_ACCESS_TLV_WRITE
);
1269 /* In initial state, nothing is available as TLV container. */
1270 if (access
& SNDRV_CTL_ELEM_ACCESS_TLV_WRITE
)
1271 access
|= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
;
1272 access
|= SNDRV_CTL_ELEM_ACCESS_USER
;
1275 * Check information and calculate the size of data specific to
1276 * this userspace control.
1278 if (info
->type
< SNDRV_CTL_ELEM_TYPE_BOOLEAN
||
1279 info
->type
> SNDRV_CTL_ELEM_TYPE_INTEGER64
)
1281 if (info
->type
== SNDRV_CTL_ELEM_TYPE_ENUMERATED
&&
1282 info
->value
.enumerated
.items
== 0)
1284 if (info
->count
< 1 ||
1285 info
->count
> max_value_counts
[info
->type
])
1287 if (!validate_element_member_dimension(info
))
1289 private_size
= value_sizes
[info
->type
] * info
->count
;
1292 * Keep memory object for this userspace control. After passing this
1293 * code block, the instance should be freed by snd_ctl_free_one().
1295 * Note that these elements in this control are locked.
1297 err
= snd_ctl_new(&kctl
, count
, access
, file
);
1300 memcpy(&kctl
->id
, &info
->id
, sizeof(kctl
->id
));
1301 kctl
->private_data
= kzalloc(sizeof(struct user_element
) + private_size
* count
,
1303 if (kctl
->private_data
== NULL
) {
1307 kctl
->private_free
= snd_ctl_elem_user_free
;
1309 /* Set private data for this userspace control. */
1310 ue
= (struct user_element
*)kctl
->private_data
;
1313 ue
->info
.access
= 0;
1314 ue
->elem_data
= (char *)ue
+ sizeof(*ue
);
1315 ue
->elem_data_size
= private_size
;
1316 if (ue
->info
.type
== SNDRV_CTL_ELEM_TYPE_ENUMERATED
) {
1317 err
= snd_ctl_elem_init_enum_names(ue
);
1319 snd_ctl_free_one(kctl
);
1324 /* Set callback functions. */
1325 if (info
->type
== SNDRV_CTL_ELEM_TYPE_ENUMERATED
)
1326 kctl
->info
= snd_ctl_elem_user_enum_info
;
1328 kctl
->info
= snd_ctl_elem_user_info
;
1329 if (access
& SNDRV_CTL_ELEM_ACCESS_READ
)
1330 kctl
->get
= snd_ctl_elem_user_get
;
1331 if (access
& SNDRV_CTL_ELEM_ACCESS_WRITE
)
1332 kctl
->put
= snd_ctl_elem_user_put
;
1333 if (access
& SNDRV_CTL_ELEM_ACCESS_TLV_WRITE
)
1334 kctl
->tlv
.c
= snd_ctl_elem_user_tlv
;
1336 /* This function manage to free the instance on failure. */
1337 down_write(&card
->controls_rwsem
);
1338 err
= __snd_ctl_add_replace(card
, kctl
, CTL_ADD_EXCLUSIVE
);
1340 snd_ctl_free_one(kctl
);
1343 offset
= snd_ctl_get_ioff(kctl
, &info
->id
);
1344 snd_ctl_build_ioff(&info
->id
, kctl
, offset
);
1346 * Here we cannot fill any field for the number of elements added by
1347 * this operation because there're no specific fields. The usage of
1348 * 'owner' field for this purpose may cause any bugs to userspace
1349 * applications because the field originally means PID of a process
1350 * which locks the element.
1353 card
->user_ctl_count
++;
1356 up_write(&card
->controls_rwsem
);
1360 static int snd_ctl_elem_add_user(struct snd_ctl_file
*file
,
1361 struct snd_ctl_elem_info __user
*_info
, int replace
)
1363 struct snd_ctl_elem_info info
;
1366 if (copy_from_user(&info
, _info
, sizeof(info
)))
1368 err
= snd_ctl_elem_add(file
, &info
, replace
);
1371 if (copy_to_user(_info
, &info
, sizeof(info
))) {
1372 snd_ctl_remove_user_ctl(file
, &info
.id
);
1379 static int snd_ctl_elem_remove(struct snd_ctl_file
*file
,
1380 struct snd_ctl_elem_id __user
*_id
)
1382 struct snd_ctl_elem_id id
;
1384 if (copy_from_user(&id
, _id
, sizeof(id
)))
1386 return snd_ctl_remove_user_ctl(file
, &id
);
1389 static int snd_ctl_subscribe_events(struct snd_ctl_file
*file
, int __user
*ptr
)
1392 if (get_user(subscribe
, ptr
))
1394 if (subscribe
< 0) {
1395 subscribe
= file
->subscribed
;
1396 if (put_user(subscribe
, ptr
))
1401 file
->subscribed
= 1;
1403 } else if (file
->subscribed
) {
1404 snd_ctl_empty_read_queue(file
);
1405 file
->subscribed
= 0;
1410 static int call_tlv_handler(struct snd_ctl_file
*file
, int op_flag
,
1411 struct snd_kcontrol
*kctl
,
1412 struct snd_ctl_elem_id
*id
,
1413 unsigned int __user
*buf
, unsigned int size
)
1415 static const struct {
1419 {SNDRV_CTL_TLV_OP_READ
, SNDRV_CTL_ELEM_ACCESS_TLV_READ
},
1420 {SNDRV_CTL_TLV_OP_WRITE
, SNDRV_CTL_ELEM_ACCESS_TLV_WRITE
},
1421 {SNDRV_CTL_TLV_OP_CMD
, SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND
},
1423 struct snd_kcontrol_volatile
*vd
= &kctl
->vd
[snd_ctl_get_ioff(kctl
, id
)];
1426 /* Check support of the request for this element. */
1427 for (i
= 0; i
< ARRAY_SIZE(pairs
); ++i
) {
1428 if (op_flag
== pairs
[i
].op
&& (vd
->access
& pairs
[i
].perm
))
1431 if (i
== ARRAY_SIZE(pairs
))
1434 if (kctl
->tlv
.c
== NULL
)
1437 /* When locked, this is unavailable. */
1438 if (vd
->owner
!= NULL
&& vd
->owner
!= file
)
1441 return kctl
->tlv
.c(kctl
, op_flag
, size
, buf
);
1444 static int read_tlv_buf(struct snd_kcontrol
*kctl
, struct snd_ctl_elem_id
*id
,
1445 unsigned int __user
*buf
, unsigned int size
)
1447 struct snd_kcontrol_volatile
*vd
= &kctl
->vd
[snd_ctl_get_ioff(kctl
, id
)];
1450 if (!(vd
->access
& SNDRV_CTL_ELEM_ACCESS_TLV_READ
))
1453 if (kctl
->tlv
.p
== NULL
)
1456 len
= sizeof(unsigned int) * 2 + kctl
->tlv
.p
[1];
1460 if (copy_to_user(buf
, kctl
->tlv
.p
, len
))
1466 static int snd_ctl_tlv_ioctl(struct snd_ctl_file
*file
,
1467 struct snd_ctl_tlv __user
*buf
,
1470 struct snd_ctl_tlv header
;
1471 unsigned int __user
*container
;
1472 unsigned int container_size
;
1473 struct snd_kcontrol
*kctl
;
1474 struct snd_ctl_elem_id id
;
1475 struct snd_kcontrol_volatile
*vd
;
1477 if (copy_from_user(&header
, buf
, sizeof(header
)))
1480 /* In design of control core, numerical ID starts at 1. */
1481 if (header
.numid
== 0)
1484 /* At least, container should include type and length fields. */
1485 if (header
.length
< sizeof(unsigned int) * 2)
1487 container_size
= header
.length
;
1488 container
= buf
->tlv
;
1490 kctl
= snd_ctl_find_numid(file
->card
, header
.numid
);
1494 /* Calculate index of the element in this set. */
1496 snd_ctl_build_ioff(&id
, kctl
, header
.numid
- id
.numid
);
1497 vd
= &kctl
->vd
[snd_ctl_get_ioff(kctl
, &id
)];
1499 if (vd
->access
& SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
) {
1500 return call_tlv_handler(file
, op_flag
, kctl
, &id
, container
,
1503 if (op_flag
== SNDRV_CTL_TLV_OP_READ
) {
1504 return read_tlv_buf(kctl
, &id
, container
,
1509 /* Not supported. */
1513 static long snd_ctl_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1515 struct snd_ctl_file
*ctl
;
1516 struct snd_card
*card
;
1517 struct snd_kctl_ioctl
*p
;
1518 void __user
*argp
= (void __user
*)arg
;
1519 int __user
*ip
= argp
;
1522 ctl
= file
->private_data
;
1524 if (snd_BUG_ON(!card
))
1527 case SNDRV_CTL_IOCTL_PVERSION
:
1528 return put_user(SNDRV_CTL_VERSION
, ip
) ? -EFAULT
: 0;
1529 case SNDRV_CTL_IOCTL_CARD_INFO
:
1530 return snd_ctl_card_info(card
, ctl
, cmd
, argp
);
1531 case SNDRV_CTL_IOCTL_ELEM_LIST
:
1532 return snd_ctl_elem_list(card
, argp
);
1533 case SNDRV_CTL_IOCTL_ELEM_INFO
:
1534 return snd_ctl_elem_info_user(ctl
, argp
);
1535 case SNDRV_CTL_IOCTL_ELEM_READ
:
1536 return snd_ctl_elem_read_user(card
, argp
);
1537 case SNDRV_CTL_IOCTL_ELEM_WRITE
:
1538 return snd_ctl_elem_write_user(ctl
, argp
);
1539 case SNDRV_CTL_IOCTL_ELEM_LOCK
:
1540 return snd_ctl_elem_lock(ctl
, argp
);
1541 case SNDRV_CTL_IOCTL_ELEM_UNLOCK
:
1542 return snd_ctl_elem_unlock(ctl
, argp
);
1543 case SNDRV_CTL_IOCTL_ELEM_ADD
:
1544 return snd_ctl_elem_add_user(ctl
, argp
, 0);
1545 case SNDRV_CTL_IOCTL_ELEM_REPLACE
:
1546 return snd_ctl_elem_add_user(ctl
, argp
, 1);
1547 case SNDRV_CTL_IOCTL_ELEM_REMOVE
:
1548 return snd_ctl_elem_remove(ctl
, argp
);
1549 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS
:
1550 return snd_ctl_subscribe_events(ctl
, ip
);
1551 case SNDRV_CTL_IOCTL_TLV_READ
:
1552 down_read(&ctl
->card
->controls_rwsem
);
1553 err
= snd_ctl_tlv_ioctl(ctl
, argp
, SNDRV_CTL_TLV_OP_READ
);
1554 up_read(&ctl
->card
->controls_rwsem
);
1556 case SNDRV_CTL_IOCTL_TLV_WRITE
:
1557 down_write(&ctl
->card
->controls_rwsem
);
1558 err
= snd_ctl_tlv_ioctl(ctl
, argp
, SNDRV_CTL_TLV_OP_WRITE
);
1559 up_write(&ctl
->card
->controls_rwsem
);
1561 case SNDRV_CTL_IOCTL_TLV_COMMAND
:
1562 down_write(&ctl
->card
->controls_rwsem
);
1563 err
= snd_ctl_tlv_ioctl(ctl
, argp
, SNDRV_CTL_TLV_OP_CMD
);
1564 up_write(&ctl
->card
->controls_rwsem
);
1566 case SNDRV_CTL_IOCTL_POWER
:
1567 return -ENOPROTOOPT
;
1568 case SNDRV_CTL_IOCTL_POWER_STATE
:
1570 return put_user(card
->power_state
, ip
) ? -EFAULT
: 0;
1572 return put_user(SNDRV_CTL_POWER_D0
, ip
) ? -EFAULT
: 0;
1575 down_read(&snd_ioctl_rwsem
);
1576 list_for_each_entry(p
, &snd_control_ioctls
, list
) {
1577 err
= p
->fioctl(card
, ctl
, cmd
, arg
);
1578 if (err
!= -ENOIOCTLCMD
) {
1579 up_read(&snd_ioctl_rwsem
);
1583 up_read(&snd_ioctl_rwsem
);
1584 dev_dbg(card
->dev
, "unknown ioctl = 0x%x\n", cmd
);
1588 static ssize_t
snd_ctl_read(struct file
*file
, char __user
*buffer
,
1589 size_t count
, loff_t
* offset
)
1591 struct snd_ctl_file
*ctl
;
1595 ctl
= file
->private_data
;
1596 if (snd_BUG_ON(!ctl
|| !ctl
->card
))
1598 if (!ctl
->subscribed
)
1600 if (count
< sizeof(struct snd_ctl_event
))
1602 spin_lock_irq(&ctl
->read_lock
);
1603 while (count
>= sizeof(struct snd_ctl_event
)) {
1604 struct snd_ctl_event ev
;
1605 struct snd_kctl_event
*kev
;
1606 while (list_empty(&ctl
->events
)) {
1607 wait_queue_entry_t wait
;
1608 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
1612 init_waitqueue_entry(&wait
, current
);
1613 add_wait_queue(&ctl
->change_sleep
, &wait
);
1614 set_current_state(TASK_INTERRUPTIBLE
);
1615 spin_unlock_irq(&ctl
->read_lock
);
1617 remove_wait_queue(&ctl
->change_sleep
, &wait
);
1618 if (ctl
->card
->shutdown
)
1620 if (signal_pending(current
))
1621 return -ERESTARTSYS
;
1622 spin_lock_irq(&ctl
->read_lock
);
1624 kev
= snd_kctl_event(ctl
->events
.next
);
1625 ev
.type
= SNDRV_CTL_EVENT_ELEM
;
1626 ev
.data
.elem
.mask
= kev
->mask
;
1627 ev
.data
.elem
.id
= kev
->id
;
1628 list_del(&kev
->list
);
1629 spin_unlock_irq(&ctl
->read_lock
);
1631 if (copy_to_user(buffer
, &ev
, sizeof(struct snd_ctl_event
))) {
1635 spin_lock_irq(&ctl
->read_lock
);
1636 buffer
+= sizeof(struct snd_ctl_event
);
1637 count
-= sizeof(struct snd_ctl_event
);
1638 result
+= sizeof(struct snd_ctl_event
);
1641 spin_unlock_irq(&ctl
->read_lock
);
1643 return result
> 0 ? result
: err
;
1646 static __poll_t
snd_ctl_poll(struct file
*file
, poll_table
* wait
)
1649 struct snd_ctl_file
*ctl
;
1651 ctl
= file
->private_data
;
1652 if (!ctl
->subscribed
)
1654 poll_wait(file
, &ctl
->change_sleep
, wait
);
1657 if (!list_empty(&ctl
->events
))
1658 mask
|= EPOLLIN
| EPOLLRDNORM
;
1664 * register the device-specific control-ioctls.
1665 * called from each device manager like pcm.c, hwdep.c, etc.
1667 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn
, struct list_head
*lists
)
1669 struct snd_kctl_ioctl
*pn
;
1671 pn
= kzalloc(sizeof(struct snd_kctl_ioctl
), GFP_KERNEL
);
1675 down_write(&snd_ioctl_rwsem
);
1676 list_add_tail(&pn
->list
, lists
);
1677 up_write(&snd_ioctl_rwsem
);
1682 * snd_ctl_register_ioctl - register the device-specific control-ioctls
1683 * @fcn: ioctl callback function
1685 * called from each device manager like pcm.c, hwdep.c, etc.
1687 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn
)
1689 return _snd_ctl_register_ioctl(fcn
, &snd_control_ioctls
);
1691 EXPORT_SYMBOL(snd_ctl_register_ioctl
);
1693 #ifdef CONFIG_COMPAT
1695 * snd_ctl_register_ioctl_compat - register the device-specific 32bit compat
1697 * @fcn: ioctl callback function
1699 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn
)
1701 return _snd_ctl_register_ioctl(fcn
, &snd_control_compat_ioctls
);
1703 EXPORT_SYMBOL(snd_ctl_register_ioctl_compat
);
1707 * de-register the device-specific control-ioctls.
1709 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn
,
1710 struct list_head
*lists
)
1712 struct snd_kctl_ioctl
*p
;
1714 if (snd_BUG_ON(!fcn
))
1716 down_write(&snd_ioctl_rwsem
);
1717 list_for_each_entry(p
, lists
, list
) {
1718 if (p
->fioctl
== fcn
) {
1720 up_write(&snd_ioctl_rwsem
);
1725 up_write(&snd_ioctl_rwsem
);
1731 * snd_ctl_unregister_ioctl - de-register the device-specific control-ioctls
1732 * @fcn: ioctl callback function to unregister
1734 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn
)
1736 return _snd_ctl_unregister_ioctl(fcn
, &snd_control_ioctls
);
1738 EXPORT_SYMBOL(snd_ctl_unregister_ioctl
);
1740 #ifdef CONFIG_COMPAT
1742 * snd_ctl_unregister_ioctl - de-register the device-specific compat 32bit
1744 * @fcn: ioctl callback function to unregister
1746 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn
)
1748 return _snd_ctl_unregister_ioctl(fcn
, &snd_control_compat_ioctls
);
1750 EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat
);
1753 static int snd_ctl_fasync(int fd
, struct file
* file
, int on
)
1755 struct snd_ctl_file
*ctl
;
1757 ctl
= file
->private_data
;
1758 return fasync_helper(fd
, file
, on
, &ctl
->fasync
);
1761 /* return the preferred subdevice number if already assigned;
1762 * otherwise return -1
1764 int snd_ctl_get_preferred_subdevice(struct snd_card
*card
, int type
)
1766 struct snd_ctl_file
*kctl
;
1769 read_lock(&card
->ctl_files_rwlock
);
1770 list_for_each_entry(kctl
, &card
->ctl_files
, list
) {
1771 if (kctl
->pid
== task_pid(current
)) {
1772 subdevice
= kctl
->preferred_subdevice
[type
];
1773 if (subdevice
!= -1)
1777 read_unlock(&card
->ctl_files_rwlock
);
1780 EXPORT_SYMBOL_GPL(snd_ctl_get_preferred_subdevice
);
1785 #ifdef CONFIG_COMPAT
1786 #include "control_compat.c"
1788 #define snd_ctl_ioctl_compat NULL
1795 static const struct file_operations snd_ctl_f_ops
=
1797 .owner
= THIS_MODULE
,
1798 .read
= snd_ctl_read
,
1799 .open
= snd_ctl_open
,
1800 .release
= snd_ctl_release
,
1801 .llseek
= no_llseek
,
1802 .poll
= snd_ctl_poll
,
1803 .unlocked_ioctl
= snd_ctl_ioctl
,
1804 .compat_ioctl
= snd_ctl_ioctl_compat
,
1805 .fasync
= snd_ctl_fasync
,
1809 * registration of the control device
1811 static int snd_ctl_dev_register(struct snd_device
*device
)
1813 struct snd_card
*card
= device
->device_data
;
1815 return snd_register_device(SNDRV_DEVICE_TYPE_CONTROL
, card
, -1,
1816 &snd_ctl_f_ops
, card
, &card
->ctl_dev
);
1820 * disconnection of the control device
1822 static int snd_ctl_dev_disconnect(struct snd_device
*device
)
1824 struct snd_card
*card
= device
->device_data
;
1825 struct snd_ctl_file
*ctl
;
1827 read_lock(&card
->ctl_files_rwlock
);
1828 list_for_each_entry(ctl
, &card
->ctl_files
, list
) {
1829 wake_up(&ctl
->change_sleep
);
1830 kill_fasync(&ctl
->fasync
, SIGIO
, POLL_ERR
);
1832 read_unlock(&card
->ctl_files_rwlock
);
1834 return snd_unregister_device(&card
->ctl_dev
);
1840 static int snd_ctl_dev_free(struct snd_device
*device
)
1842 struct snd_card
*card
= device
->device_data
;
1843 struct snd_kcontrol
*control
;
1845 down_write(&card
->controls_rwsem
);
1846 while (!list_empty(&card
->controls
)) {
1847 control
= snd_kcontrol(card
->controls
.next
);
1848 snd_ctl_remove(card
, control
);
1850 up_write(&card
->controls_rwsem
);
1851 put_device(&card
->ctl_dev
);
1856 * create control core:
1857 * called from init.c
1859 int snd_ctl_create(struct snd_card
*card
)
1861 static struct snd_device_ops ops
= {
1862 .dev_free
= snd_ctl_dev_free
,
1863 .dev_register
= snd_ctl_dev_register
,
1864 .dev_disconnect
= snd_ctl_dev_disconnect
,
1868 if (snd_BUG_ON(!card
))
1870 if (snd_BUG_ON(card
->number
< 0 || card
->number
>= SNDRV_CARDS
))
1873 snd_device_initialize(&card
->ctl_dev
, card
);
1874 dev_set_name(&card
->ctl_dev
, "controlC%d", card
->number
);
1876 err
= snd_device_new(card
, SNDRV_DEV_CONTROL
, card
, &ops
);
1878 put_device(&card
->ctl_dev
);
1883 * Frequently used control callbacks/helpers
1887 * snd_ctl_boolean_mono_info - Helper function for a standard boolean info
1888 * callback with a mono channel
1889 * @kcontrol: the kcontrol instance
1890 * @uinfo: info to store
1892 * This is a function that can be used as info callback for a standard
1893 * boolean control with a single mono channel.
1895 int snd_ctl_boolean_mono_info(struct snd_kcontrol
*kcontrol
,
1896 struct snd_ctl_elem_info
*uinfo
)
1898 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1900 uinfo
->value
.integer
.min
= 0;
1901 uinfo
->value
.integer
.max
= 1;
1904 EXPORT_SYMBOL(snd_ctl_boolean_mono_info
);
1907 * snd_ctl_boolean_stereo_info - Helper function for a standard boolean info
1908 * callback with stereo two channels
1909 * @kcontrol: the kcontrol instance
1910 * @uinfo: info to store
1912 * This is a function that can be used as info callback for a standard
1913 * boolean control with stereo two channels.
1915 int snd_ctl_boolean_stereo_info(struct snd_kcontrol
*kcontrol
,
1916 struct snd_ctl_elem_info
*uinfo
)
1918 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1920 uinfo
->value
.integer
.min
= 0;
1921 uinfo
->value
.integer
.max
= 1;
1924 EXPORT_SYMBOL(snd_ctl_boolean_stereo_info
);
1927 * snd_ctl_enum_info - fills the info structure for an enumerated control
1928 * @info: the structure to be filled
1929 * @channels: the number of the control's channels; often one
1930 * @items: the number of control values; also the size of @names
1931 * @names: an array containing the names of all control values
1933 * Sets all required fields in @info to their appropriate values.
1934 * If the control's accessibility is not the default (readable and writable),
1935 * the caller has to fill @info->access.
1939 int snd_ctl_enum_info(struct snd_ctl_elem_info
*info
, unsigned int channels
,
1940 unsigned int items
, const char *const names
[])
1942 info
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1943 info
->count
= channels
;
1944 info
->value
.enumerated
.items
= items
;
1947 if (info
->value
.enumerated
.item
>= items
)
1948 info
->value
.enumerated
.item
= items
- 1;
1949 WARN(strlen(names
[info
->value
.enumerated
.item
]) >= sizeof(info
->value
.enumerated
.name
),
1950 "ALSA: too long item name '%s'\n",
1951 names
[info
->value
.enumerated
.item
]);
1952 strlcpy(info
->value
.enumerated
.name
,
1953 names
[info
->value
.enumerated
.item
],
1954 sizeof(info
->value
.enumerated
.name
));
1957 EXPORT_SYMBOL(snd_ctl_enum_info
);