1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun #ifndef __SOUND_CONTROL_H
3*4882a593Smuzhiyun #define __SOUND_CONTROL_H
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun /*
6*4882a593Smuzhiyun * Header file for control interface
7*4882a593Smuzhiyun * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <linux/wait.h>
11*4882a593Smuzhiyun #include <linux/nospec.h>
12*4882a593Smuzhiyun #include <sound/asound.h>
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #define snd_kcontrol_chip(kcontrol) ((kcontrol)->private_data)
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun struct snd_kcontrol;
17*4882a593Smuzhiyun typedef int (snd_kcontrol_info_t) (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_info * uinfo);
18*4882a593Smuzhiyun typedef int (snd_kcontrol_get_t) (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol);
19*4882a593Smuzhiyun typedef int (snd_kcontrol_put_t) (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol);
20*4882a593Smuzhiyun typedef int (snd_kcontrol_tlv_rw_t)(struct snd_kcontrol *kcontrol,
21*4882a593Smuzhiyun int op_flag, /* SNDRV_CTL_TLV_OP_XXX */
22*4882a593Smuzhiyun unsigned int size,
23*4882a593Smuzhiyun unsigned int __user *tlv);
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun /* internal flag for skipping validations */
26*4882a593Smuzhiyun #ifdef CONFIG_SND_CTL_VALIDATION
27*4882a593Smuzhiyun #define SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK (1 << 27)
28*4882a593Smuzhiyun #define snd_ctl_skip_validation(info) \
29*4882a593Smuzhiyun ((info)->access & SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK)
30*4882a593Smuzhiyun #else
31*4882a593Smuzhiyun #define SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK 0
32*4882a593Smuzhiyun #define snd_ctl_skip_validation(info) true
33*4882a593Smuzhiyun #endif
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun enum {
36*4882a593Smuzhiyun SNDRV_CTL_TLV_OP_READ = 0,
37*4882a593Smuzhiyun SNDRV_CTL_TLV_OP_WRITE = 1,
38*4882a593Smuzhiyun SNDRV_CTL_TLV_OP_CMD = -1,
39*4882a593Smuzhiyun };
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun struct snd_kcontrol_new {
42*4882a593Smuzhiyun snd_ctl_elem_iface_t iface; /* interface identifier */
43*4882a593Smuzhiyun unsigned int device; /* device/client number */
44*4882a593Smuzhiyun unsigned int subdevice; /* subdevice (substream) number */
45*4882a593Smuzhiyun const char *name; /* ASCII name of item */
46*4882a593Smuzhiyun unsigned int index; /* index of item */
47*4882a593Smuzhiyun unsigned int access; /* access rights */
48*4882a593Smuzhiyun unsigned int count; /* count of same elements */
49*4882a593Smuzhiyun snd_kcontrol_info_t *info;
50*4882a593Smuzhiyun snd_kcontrol_get_t *get;
51*4882a593Smuzhiyun snd_kcontrol_put_t *put;
52*4882a593Smuzhiyun union {
53*4882a593Smuzhiyun snd_kcontrol_tlv_rw_t *c;
54*4882a593Smuzhiyun const unsigned int *p;
55*4882a593Smuzhiyun } tlv;
56*4882a593Smuzhiyun unsigned long private_value;
57*4882a593Smuzhiyun };
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun struct snd_kcontrol_volatile {
60*4882a593Smuzhiyun struct snd_ctl_file *owner; /* locked */
61*4882a593Smuzhiyun unsigned int access; /* access rights */
62*4882a593Smuzhiyun };
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun struct snd_kcontrol {
65*4882a593Smuzhiyun struct list_head list; /* list of controls */
66*4882a593Smuzhiyun struct snd_ctl_elem_id id;
67*4882a593Smuzhiyun unsigned int count; /* count of same elements */
68*4882a593Smuzhiyun snd_kcontrol_info_t *info;
69*4882a593Smuzhiyun snd_kcontrol_get_t *get;
70*4882a593Smuzhiyun snd_kcontrol_put_t *put;
71*4882a593Smuzhiyun union {
72*4882a593Smuzhiyun snd_kcontrol_tlv_rw_t *c;
73*4882a593Smuzhiyun const unsigned int *p;
74*4882a593Smuzhiyun } tlv;
75*4882a593Smuzhiyun unsigned long private_value;
76*4882a593Smuzhiyun void *private_data;
77*4882a593Smuzhiyun void (*private_free)(struct snd_kcontrol *kcontrol);
78*4882a593Smuzhiyun struct snd_kcontrol_volatile vd[]; /* volatile data */
79*4882a593Smuzhiyun };
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun #define snd_kcontrol(n) list_entry(n, struct snd_kcontrol, list)
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun struct snd_kctl_event {
84*4882a593Smuzhiyun struct list_head list; /* list of events */
85*4882a593Smuzhiyun struct snd_ctl_elem_id id;
86*4882a593Smuzhiyun unsigned int mask;
87*4882a593Smuzhiyun };
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun #define snd_kctl_event(n) list_entry(n, struct snd_kctl_event, list)
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun struct pid;
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun enum {
94*4882a593Smuzhiyun SND_CTL_SUBDEV_PCM,
95*4882a593Smuzhiyun SND_CTL_SUBDEV_RAWMIDI,
96*4882a593Smuzhiyun SND_CTL_SUBDEV_ITEMS,
97*4882a593Smuzhiyun };
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun struct snd_ctl_file {
100*4882a593Smuzhiyun struct list_head list; /* list of all control files */
101*4882a593Smuzhiyun struct snd_card *card;
102*4882a593Smuzhiyun struct pid *pid;
103*4882a593Smuzhiyun int preferred_subdevice[SND_CTL_SUBDEV_ITEMS];
104*4882a593Smuzhiyun wait_queue_head_t change_sleep;
105*4882a593Smuzhiyun spinlock_t read_lock;
106*4882a593Smuzhiyun struct fasync_struct *fasync;
107*4882a593Smuzhiyun int subscribed; /* read interface is activated */
108*4882a593Smuzhiyun struct list_head events; /* waiting events for read */
109*4882a593Smuzhiyun };
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun #define snd_ctl_file(n) list_entry(n, struct snd_ctl_file, list)
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun typedef int (*snd_kctl_ioctl_func_t) (struct snd_card * card,
114*4882a593Smuzhiyun struct snd_ctl_file * control,
115*4882a593Smuzhiyun unsigned int cmd, unsigned long arg);
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun void snd_ctl_notify(struct snd_card * card, unsigned int mask, struct snd_ctl_elem_id * id);
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new * kcontrolnew, void * private_data);
120*4882a593Smuzhiyun void snd_ctl_free_one(struct snd_kcontrol * kcontrol);
121*4882a593Smuzhiyun int snd_ctl_add(struct snd_card * card, struct snd_kcontrol * kcontrol);
122*4882a593Smuzhiyun int snd_ctl_remove(struct snd_card * card, struct snd_kcontrol * kcontrol);
123*4882a593Smuzhiyun int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol, bool add_on_replace);
124*4882a593Smuzhiyun int snd_ctl_remove_id(struct snd_card * card, struct snd_ctl_elem_id *id);
125*4882a593Smuzhiyun int snd_ctl_rename_id(struct snd_card * card, struct snd_ctl_elem_id *src_id, struct snd_ctl_elem_id *dst_id);
126*4882a593Smuzhiyun int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
127*4882a593Smuzhiyun int active);
128*4882a593Smuzhiyun struct snd_kcontrol *snd_ctl_find_numid(struct snd_card * card, unsigned int numid);
129*4882a593Smuzhiyun struct snd_kcontrol *snd_ctl_find_id(struct snd_card * card, struct snd_ctl_elem_id *id);
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun int snd_ctl_create(struct snd_card *card);
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn);
134*4882a593Smuzhiyun int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn);
135*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
136*4882a593Smuzhiyun int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn);
137*4882a593Smuzhiyun int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn);
138*4882a593Smuzhiyun #else
139*4882a593Smuzhiyun #define snd_ctl_register_ioctl_compat(fcn)
140*4882a593Smuzhiyun #define snd_ctl_unregister_ioctl_compat(fcn)
141*4882a593Smuzhiyun #endif
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type);
144*4882a593Smuzhiyun
snd_ctl_get_ioffnum(struct snd_kcontrol * kctl,struct snd_ctl_elem_id * id)145*4882a593Smuzhiyun static inline unsigned int snd_ctl_get_ioffnum(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun unsigned int ioff = id->numid - kctl->id.numid;
148*4882a593Smuzhiyun return array_index_nospec(ioff, kctl->count);
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
snd_ctl_get_ioffidx(struct snd_kcontrol * kctl,struct snd_ctl_elem_id * id)151*4882a593Smuzhiyun static inline unsigned int snd_ctl_get_ioffidx(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun unsigned int ioff = id->index - kctl->id.index;
154*4882a593Smuzhiyun return array_index_nospec(ioff, kctl->count);
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun
snd_ctl_get_ioff(struct snd_kcontrol * kctl,struct snd_ctl_elem_id * id)157*4882a593Smuzhiyun static inline unsigned int snd_ctl_get_ioff(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun if (id->numid) {
160*4882a593Smuzhiyun return snd_ctl_get_ioffnum(kctl, id);
161*4882a593Smuzhiyun } else {
162*4882a593Smuzhiyun return snd_ctl_get_ioffidx(kctl, id);
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun
snd_ctl_build_ioff(struct snd_ctl_elem_id * dst_id,struct snd_kcontrol * src_kctl,unsigned int offset)166*4882a593Smuzhiyun static inline struct snd_ctl_elem_id *snd_ctl_build_ioff(struct snd_ctl_elem_id *dst_id,
167*4882a593Smuzhiyun struct snd_kcontrol *src_kctl,
168*4882a593Smuzhiyun unsigned int offset)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun *dst_id = src_kctl->id;
171*4882a593Smuzhiyun dst_id->index += offset;
172*4882a593Smuzhiyun dst_id->numid += offset;
173*4882a593Smuzhiyun return dst_id;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun /*
177*4882a593Smuzhiyun * Frequently used control callbacks/helpers
178*4882a593Smuzhiyun */
179*4882a593Smuzhiyun int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
180*4882a593Smuzhiyun struct snd_ctl_elem_info *uinfo);
181*4882a593Smuzhiyun int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
182*4882a593Smuzhiyun struct snd_ctl_elem_info *uinfo);
183*4882a593Smuzhiyun int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
184*4882a593Smuzhiyun unsigned int items, const char *const names[]);
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun /*
187*4882a593Smuzhiyun * virtual master control
188*4882a593Smuzhiyun */
189*4882a593Smuzhiyun struct snd_kcontrol *snd_ctl_make_virtual_master(char *name,
190*4882a593Smuzhiyun const unsigned int *tlv);
191*4882a593Smuzhiyun int _snd_ctl_add_follower(struct snd_kcontrol *master,
192*4882a593Smuzhiyun struct snd_kcontrol *follower,
193*4882a593Smuzhiyun unsigned int flags);
194*4882a593Smuzhiyun /* optional flags for follower */
195*4882a593Smuzhiyun #define SND_CTL_FOLLOWER_NEED_UPDATE (1 << 0)
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun /**
198*4882a593Smuzhiyun * snd_ctl_add_follower - Add a virtual follower control
199*4882a593Smuzhiyun * @master: vmaster element
200*4882a593Smuzhiyun * @follower: follower element to add
201*4882a593Smuzhiyun *
202*4882a593Smuzhiyun * Add a virtual follower control to the given master element created via
203*4882a593Smuzhiyun * snd_ctl_create_virtual_master() beforehand.
204*4882a593Smuzhiyun *
205*4882a593Smuzhiyun * All followers must be the same type (returning the same information
206*4882a593Smuzhiyun * via info callback). The function doesn't check it, so it's your
207*4882a593Smuzhiyun * responsibility.
208*4882a593Smuzhiyun *
209*4882a593Smuzhiyun * Also, some additional limitations:
210*4882a593Smuzhiyun * at most two channels,
211*4882a593Smuzhiyun * logarithmic volume control (dB level) thus no linear volume,
212*4882a593Smuzhiyun * master can only attenuate the volume without gain
213*4882a593Smuzhiyun *
214*4882a593Smuzhiyun * Return: Zero if successful or a negative error code.
215*4882a593Smuzhiyun */
216*4882a593Smuzhiyun static inline int
snd_ctl_add_follower(struct snd_kcontrol * master,struct snd_kcontrol * follower)217*4882a593Smuzhiyun snd_ctl_add_follower(struct snd_kcontrol *master, struct snd_kcontrol *follower)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun return _snd_ctl_add_follower(master, follower, 0);
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun /**
223*4882a593Smuzhiyun * snd_ctl_add_follower_uncached - Add a virtual follower control
224*4882a593Smuzhiyun * @master: vmaster element
225*4882a593Smuzhiyun * @follower: follower element to add
226*4882a593Smuzhiyun *
227*4882a593Smuzhiyun * Add a virtual follower control to the given master.
228*4882a593Smuzhiyun * Unlike snd_ctl_add_follower(), the element added via this function
229*4882a593Smuzhiyun * is supposed to have volatile values, and get callback is called
230*4882a593Smuzhiyun * at each time queried from the master.
231*4882a593Smuzhiyun *
232*4882a593Smuzhiyun * When the control peeks the hardware values directly and the value
233*4882a593Smuzhiyun * can be changed by other means than the put callback of the element,
234*4882a593Smuzhiyun * this function should be used to keep the value always up-to-date.
235*4882a593Smuzhiyun *
236*4882a593Smuzhiyun * Return: Zero if successful or a negative error code.
237*4882a593Smuzhiyun */
238*4882a593Smuzhiyun static inline int
snd_ctl_add_follower_uncached(struct snd_kcontrol * master,struct snd_kcontrol * follower)239*4882a593Smuzhiyun snd_ctl_add_follower_uncached(struct snd_kcontrol *master,
240*4882a593Smuzhiyun struct snd_kcontrol *follower)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun return _snd_ctl_add_follower(master, follower, SND_CTL_FOLLOWER_NEED_UPDATE);
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun int snd_ctl_add_vmaster_hook(struct snd_kcontrol *kctl,
246*4882a593Smuzhiyun void (*hook)(void *private_data, int),
247*4882a593Smuzhiyun void *private_data);
248*4882a593Smuzhiyun void snd_ctl_sync_vmaster(struct snd_kcontrol *kctl, bool hook_only);
249*4882a593Smuzhiyun #define snd_ctl_sync_vmaster_hook(kctl) snd_ctl_sync_vmaster(kctl, true)
250*4882a593Smuzhiyun int snd_ctl_apply_vmaster_followers(struct snd_kcontrol *kctl,
251*4882a593Smuzhiyun int (*func)(struct snd_kcontrol *vfollower,
252*4882a593Smuzhiyun struct snd_kcontrol *follower,
253*4882a593Smuzhiyun void *arg),
254*4882a593Smuzhiyun void *arg);
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun /*
257*4882a593Smuzhiyun * Helper functions for jack-detection controls
258*4882a593Smuzhiyun */
259*4882a593Smuzhiyun struct snd_kcontrol *
260*4882a593Smuzhiyun snd_kctl_jack_new(const char *name, struct snd_card *card);
261*4882a593Smuzhiyun void snd_kctl_jack_report(struct snd_card *card,
262*4882a593Smuzhiyun struct snd_kcontrol *kctl, bool status);
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun #endif /* __SOUND_CONTROL_H */
265