blob: 985c51a8fb7487fa1453f39fc5b9d6b0d88fd390 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef __SOUND_CONTROL_H
3#define __SOUND_CONTROL_H
4
5/*
6 * Header file for control interface
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02007 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Ingo Molnarb69339ba2017-02-05 16:15:03 +010010#include <linux/wait.h>
Takashi Iwai088e8612018-04-24 07:45:56 +020011#include <linux/nospec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <sound/asound.h>
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#define snd_kcontrol_chip(kcontrol) ((kcontrol)->private_data)
15
Takashi Iwai82e9bae2005-11-17 13:53:23 +010016struct snd_kcontrol;
17typedef int (snd_kcontrol_info_t) (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_info * uinfo);
18typedef int (snd_kcontrol_get_t) (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol);
19typedef int (snd_kcontrol_put_t) (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +020020typedef int (snd_kcontrol_tlv_rw_t)(struct snd_kcontrol *kcontrol,
Takashi Iwai0cea76f2014-07-15 16:31:01 +020021 int op_flag, /* SNDRV_CTL_TLV_OP_XXX */
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +020022 unsigned int size,
23 unsigned int __user *tlv);
24
Takashi Iwaifbd3eb72020-01-04 09:35:56 +010025/* internal flag for skipping validations */
26#ifdef CONFIG_SND_CTL_VALIDATION
Jaroslav Kysela22d8de62021-03-17 18:29:42 +010027#define SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK (1 << 24)
Takashi Iwaifbd3eb72020-01-04 09:35:56 +010028#define snd_ctl_skip_validation(info) \
29 ((info)->access & SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK)
30#else
31#define SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK 0
32#define snd_ctl_skip_validation(info) true
33#endif
34
Jaroslav Kysela22d8de62021-03-17 18:29:42 +010035/* kernel only - LED bits */
36#define SNDRV_CTL_ELEM_ACCESS_LED_SHIFT 25
37#define SNDRV_CTL_ELEM_ACCESS_LED_MASK (7<<25) /* kernel three bits - LED group */
38#define SNDRV_CTL_ELEM_ACCESS_SPK_LED (1<<25) /* kernel speaker (output) LED flag */
39#define SNDRV_CTL_ELEM_ACCESS_MIC_LED (2<<25) /* kernel microphone (input) LED flag */
40
Takashi Iwai0cea76f2014-07-15 16:31:01 +020041enum {
42 SNDRV_CTL_TLV_OP_READ = 0,
43 SNDRV_CTL_TLV_OP_WRITE = 1,
44 SNDRV_CTL_TLV_OP_CMD = -1,
45};
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Takashi Iwai82e9bae2005-11-17 13:53:23 +010047struct snd_kcontrol_new {
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 snd_ctl_elem_iface_t iface; /* interface identifier */
49 unsigned int device; /* device/client number */
50 unsigned int subdevice; /* subdevice (substream) number */
Arnd Bergmann2a6eca12020-10-26 17:52:18 +010051 const char *name; /* ASCII name of item */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 unsigned int index; /* index of item */
53 unsigned int access; /* access rights */
54 unsigned int count; /* count of same elements */
55 snd_kcontrol_info_t *info;
56 snd_kcontrol_get_t *get;
57 snd_kcontrol_put_t *put;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +020058 union {
59 snd_kcontrol_tlv_rw_t *c;
Takashi Iwai0cb29ea2007-01-29 15:33:49 +010060 const unsigned int *p;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +020061 } tlv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 unsigned long private_value;
Takashi Iwai82e9bae2005-11-17 13:53:23 +010063};
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Takashi Iwai82e9bae2005-11-17 13:53:23 +010065struct snd_kcontrol_volatile {
66 struct snd_ctl_file *owner; /* locked */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 unsigned int access; /* access rights */
Takashi Iwai82e9bae2005-11-17 13:53:23 +010068};
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Takashi Iwai82e9bae2005-11-17 13:53:23 +010070struct snd_kcontrol {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 struct list_head list; /* list of controls */
Takashi Iwai82e9bae2005-11-17 13:53:23 +010072 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 unsigned int count; /* count of same elements */
74 snd_kcontrol_info_t *info;
75 snd_kcontrol_get_t *get;
76 snd_kcontrol_put_t *put;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +020077 union {
78 snd_kcontrol_tlv_rw_t *c;
Takashi Iwai0cb29ea2007-01-29 15:33:49 +010079 const unsigned int *p;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +020080 } tlv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 unsigned long private_value;
82 void *private_data;
Takashi Iwai82e9bae2005-11-17 13:53:23 +010083 void (*private_free)(struct snd_kcontrol *kcontrol);
Gustavo A. R. Silva9ad06eb2020-05-07 14:22:23 -050084 struct snd_kcontrol_volatile vd[]; /* volatile data */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085};
86
Takashi Iwai82e9bae2005-11-17 13:53:23 +010087#define snd_kcontrol(n) list_entry(n, struct snd_kcontrol, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Takashi Iwai82e9bae2005-11-17 13:53:23 +010089struct snd_kctl_event {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 struct list_head list; /* list of events */
Takashi Iwai82e9bae2005-11-17 13:53:23 +010091 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 unsigned int mask;
Takashi Iwai82e9bae2005-11-17 13:53:23 +010093};
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Takashi Iwai82e9bae2005-11-17 13:53:23 +010095#define snd_kctl_event(n) list_entry(n, struct snd_kctl_event, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Clemens Ladisch25d27ed2009-11-02 09:35:44 +010097struct pid;
98
Takashi Iwai23c18d42014-02-19 14:30:29 +010099enum {
100 SND_CTL_SUBDEV_PCM,
101 SND_CTL_SUBDEV_RAWMIDI,
102 SND_CTL_SUBDEV_ITEMS,
103};
104
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100105struct snd_ctl_file {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct list_head list; /* list of all control files */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100107 struct snd_card *card;
Clemens Ladisch25d27ed2009-11-02 09:35:44 +0100108 struct pid *pid;
Takashi Iwai23c18d42014-02-19 14:30:29 +0100109 int preferred_subdevice[SND_CTL_SUBDEV_ITEMS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 wait_queue_head_t change_sleep;
111 spinlock_t read_lock;
112 struct fasync_struct *fasync;
113 int subscribed; /* read interface is activated */
114 struct list_head events; /* waiting events for read */
115};
116
Jaroslav Kysela3f0638a2021-03-17 18:29:41 +0100117struct snd_ctl_layer_ops {
118 struct snd_ctl_layer_ops *next;
119 const char *module_name;
120 void (*lregister)(struct snd_card *card);
121 void (*ldisconnect)(struct snd_card *card);
122 void (*lnotify)(struct snd_card *card, unsigned int mask, struct snd_kcontrol *kctl, unsigned int ioff);
123};
124
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100125#define snd_ctl_file(n) list_entry(n, struct snd_ctl_file, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100127typedef int (*snd_kctl_ioctl_func_t) (struct snd_card * card,
128 struct snd_ctl_file * control,
129 unsigned int cmd, unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100131void snd_ctl_notify(struct snd_card * card, unsigned int mask, struct snd_ctl_elem_id * id);
Jaroslav Kysela1fa44452021-03-17 18:29:40 +0100132void snd_ctl_notify_one(struct snd_card * card, unsigned int mask, struct snd_kcontrol * kctl, unsigned int ioff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100134struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new * kcontrolnew, void * private_data);
135void snd_ctl_free_one(struct snd_kcontrol * kcontrol);
136int snd_ctl_add(struct snd_card * card, struct snd_kcontrol * kcontrol);
137int snd_ctl_remove(struct snd_card * card, struct snd_kcontrol * kcontrol);
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000138int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol, bool add_on_replace);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100139int snd_ctl_remove_id(struct snd_card * card, struct snd_ctl_elem_id *id);
140int snd_ctl_rename_id(struct snd_card * card, struct snd_ctl_elem_id *src_id, struct snd_ctl_elem_id *dst_id);
Jaroslav Kysela1fa44452021-03-17 18:29:40 +0100141int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id, int active);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100142struct snd_kcontrol *snd_ctl_find_numid(struct snd_card * card, unsigned int numid);
143struct snd_kcontrol *snd_ctl_find_id(struct snd_card * card, struct snd_ctl_elem_id *id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100145int snd_ctl_create(struct snd_card *card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn);
148int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn);
149#ifdef CONFIG_COMPAT
150int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn);
151int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn);
152#else
153#define snd_ctl_register_ioctl_compat(fcn)
154#define snd_ctl_unregister_ioctl_compat(fcn)
155#endif
156
Jaroslav Kysela3f0638a2021-03-17 18:29:41 +0100157int snd_ctl_request_layer(const char *module_name);
158void snd_ctl_register_layer(struct snd_ctl_layer_ops *lops);
159void snd_ctl_disconnect_layer(struct snd_ctl_layer_ops *lops);
160
Takashi Iwai23c18d42014-02-19 14:30:29 +0100161int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type);
162
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100163static inline unsigned int snd_ctl_get_ioffnum(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Takashi Iwai088e8612018-04-24 07:45:56 +0200165 unsigned int ioff = id->numid - kctl->id.numid;
166 return array_index_nospec(ioff, kctl->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100169static inline unsigned int snd_ctl_get_ioffidx(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Takashi Iwai088e8612018-04-24 07:45:56 +0200171 unsigned int ioff = id->index - kctl->id.index;
172 return array_index_nospec(ioff, kctl->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100175static inline unsigned int snd_ctl_get_ioff(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 if (id->numid) {
178 return snd_ctl_get_ioffnum(kctl, id);
179 } else {
180 return snd_ctl_get_ioffidx(kctl, id);
181 }
182}
183
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100184static inline struct snd_ctl_elem_id *snd_ctl_build_ioff(struct snd_ctl_elem_id *dst_id,
185 struct snd_kcontrol *src_kctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 unsigned int offset)
187{
188 *dst_id = src_kctl->id;
189 dst_id->index += offset;
190 dst_id->numid += offset;
191 return dst_id;
192}
193
Takashi Iwaib9ed4f22007-07-23 15:41:34 +0200194/*
Clemens Ladisch96007322011-01-10 16:25:44 +0100195 * Frequently used control callbacks/helpers
Takashi Iwaib9ed4f22007-07-23 15:41:34 +0200196 */
197int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
198 struct snd_ctl_elem_info *uinfo);
199int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
200 struct snd_ctl_elem_info *uinfo);
Clemens Ladisch96007322011-01-10 16:25:44 +0100201int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
202 unsigned int items, const char *const names[]);
Takashi Iwaib9ed4f22007-07-23 15:41:34 +0200203
Takashi Iwaie922b002008-02-18 13:03:13 +0100204/*
205 * virtual master control
206 */
207struct snd_kcontrol *snd_ctl_make_virtual_master(char *name,
208 const unsigned int *tlv);
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200209int _snd_ctl_add_follower(struct snd_kcontrol *master,
210 struct snd_kcontrol *follower,
211 unsigned int flags);
212/* optional flags for follower */
213#define SND_CTL_FOLLOWER_NEED_UPDATE (1 << 0)
Takashi Iwaif5b1db62009-01-16 18:15:22 +0100214
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100215/**
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200216 * snd_ctl_add_follower - Add a virtual follower control
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100217 * @master: vmaster element
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200218 * @follower: follower element to add
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100219 *
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200220 * Add a virtual follower control to the given master element created via
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100221 * snd_ctl_create_virtual_master() beforehand.
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100222 *
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200223 * All followers must be the same type (returning the same information
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300224 * via info callback). The function doesn't check it, so it's your
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100225 * responsibility.
226 *
227 * Also, some additional limitations:
228 * at most two channels,
229 * logarithmic volume control (dB level) thus no linear volume,
230 * master can only attenuate the volume without gain
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100231 *
232 * Return: Zero if successful or a negative error code.
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100233 */
Takashi Iwaif5b1db62009-01-16 18:15:22 +0100234static inline int
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200235snd_ctl_add_follower(struct snd_kcontrol *master, struct snd_kcontrol *follower)
Takashi Iwaif5b1db62009-01-16 18:15:22 +0100236{
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200237 return _snd_ctl_add_follower(master, follower, 0);
Takashi Iwaif5b1db62009-01-16 18:15:22 +0100238}
239
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100240/**
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200241 * snd_ctl_add_follower_uncached - Add a virtual follower control
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100242 * @master: vmaster element
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200243 * @follower: follower element to add
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100244 *
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200245 * Add a virtual follower control to the given master.
246 * Unlike snd_ctl_add_follower(), the element added via this function
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100247 * is supposed to have volatile values, and get callback is called
Masanari Iida1a6ab462015-03-04 10:56:13 +0900248 * at each time queried from the master.
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100249 *
250 * When the control peeks the hardware values directly and the value
251 * can be changed by other means than the put callback of the element,
252 * this function should be used to keep the value always up-to-date.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100253 *
254 * Return: Zero if successful or a negative error code.
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100255 */
Takashi Iwaif5b1db62009-01-16 18:15:22 +0100256static inline int
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200257snd_ctl_add_follower_uncached(struct snd_kcontrol *master,
258 struct snd_kcontrol *follower)
Takashi Iwaif5b1db62009-01-16 18:15:22 +0100259{
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200260 return _snd_ctl_add_follower(master, follower, SND_CTL_FOLLOWER_NEED_UPDATE);
Takashi Iwaif5b1db62009-01-16 18:15:22 +0100261}
262
Takashi Iwai2ad787e2012-03-12 12:18:37 +0100263int snd_ctl_add_vmaster_hook(struct snd_kcontrol *kctl,
264 void (*hook)(void *private_data, int),
265 void *private_data);
Takashi Iwai1ca2f2e2013-06-24 15:51:54 +0200266void snd_ctl_sync_vmaster(struct snd_kcontrol *kctl, bool hook_only);
267#define snd_ctl_sync_vmaster_hook(kctl) snd_ctl_sync_vmaster(kctl, true)
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200268int snd_ctl_apply_vmaster_followers(struct snd_kcontrol *kctl,
269 int (*func)(struct snd_kcontrol *vfollower,
270 struct snd_kcontrol *follower,
271 void *arg),
272 void *arg);
Takashi Iwai2ad787e2012-03-12 12:18:37 +0100273
Takashi Iwai35be5442011-11-02 08:36:06 +0100274/*
Jaroslav Kysela22d8de62021-03-17 18:29:42 +0100275 * Control LED trigger layer
276 */
277#define SND_CTL_LAYER_MODULE_LED "snd-ctl-led"
278
279#if IS_MODULE(CONFIG_SND_CTL_LED)
280static inline int snd_ctl_led_request(void) { return snd_ctl_request_layer(SND_CTL_LAYER_MODULE_LED); }
281#else
282static inline int snd_ctl_led_request(void) { return 0; }
283#endif
284
285/*
Takashi Iwai35be5442011-11-02 08:36:06 +0100286 * Helper functions for jack-detection controls
287 */
288struct snd_kcontrol *
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800289snd_kctl_jack_new(const char *name, struct snd_card *card);
Takashi Iwai35be5442011-11-02 08:36:06 +0100290void snd_kctl_jack_report(struct snd_card *card,
291 struct snd_kcontrol *kctl, bool status);
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293#endif /* __SOUND_CONTROL_H */