blob: 175610bfa8c8b90279ba2ecc5f15f89c4739f4ef [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
27#define SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK (1 << 27)
28#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
Takashi Iwai0cea76f2014-07-15 16:31:01 +020035enum {
36 SNDRV_CTL_TLV_OP_READ = 0,
37 SNDRV_CTL_TLV_OP_WRITE = 1,
38 SNDRV_CTL_TLV_OP_CMD = -1,
39};
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Takashi Iwai82e9bae2005-11-17 13:53:23 +010041struct snd_kcontrol_new {
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 snd_ctl_elem_iface_t iface; /* interface identifier */
43 unsigned int device; /* device/client number */
44 unsigned int subdevice; /* subdevice (substream) number */
Arnd Bergmann2a6eca12020-10-26 17:52:18 +010045 const char *name; /* ASCII name of item */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 unsigned int index; /* index of item */
47 unsigned int access; /* access rights */
48 unsigned int count; /* count of same elements */
49 snd_kcontrol_info_t *info;
50 snd_kcontrol_get_t *get;
51 snd_kcontrol_put_t *put;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +020052 union {
53 snd_kcontrol_tlv_rw_t *c;
Takashi Iwai0cb29ea2007-01-29 15:33:49 +010054 const unsigned int *p;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +020055 } tlv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 unsigned long private_value;
Takashi Iwai82e9bae2005-11-17 13:53:23 +010057};
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Takashi Iwai82e9bae2005-11-17 13:53:23 +010059struct snd_kcontrol_volatile {
60 struct snd_ctl_file *owner; /* locked */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 unsigned int access; /* access rights */
Takashi Iwai82e9bae2005-11-17 13:53:23 +010062};
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Takashi Iwai82e9bae2005-11-17 13:53:23 +010064struct snd_kcontrol {
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 struct list_head list; /* list of controls */
Takashi Iwai82e9bae2005-11-17 13:53:23 +010066 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 unsigned int count; /* count of same elements */
68 snd_kcontrol_info_t *info;
69 snd_kcontrol_get_t *get;
70 snd_kcontrol_put_t *put;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +020071 union {
72 snd_kcontrol_tlv_rw_t *c;
Takashi Iwai0cb29ea2007-01-29 15:33:49 +010073 const unsigned int *p;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +020074 } tlv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 unsigned long private_value;
76 void *private_data;
Takashi Iwai82e9bae2005-11-17 13:53:23 +010077 void (*private_free)(struct snd_kcontrol *kcontrol);
Gustavo A. R. Silva9ad06eb2020-05-07 14:22:23 -050078 struct snd_kcontrol_volatile vd[]; /* volatile data */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
Takashi Iwai82e9bae2005-11-17 13:53:23 +010081#define snd_kcontrol(n) list_entry(n, struct snd_kcontrol, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Takashi Iwai82e9bae2005-11-17 13:53:23 +010083struct snd_kctl_event {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 struct list_head list; /* list of events */
Takashi Iwai82e9bae2005-11-17 13:53:23 +010085 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 unsigned int mask;
Takashi Iwai82e9bae2005-11-17 13:53:23 +010087};
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Takashi Iwai82e9bae2005-11-17 13:53:23 +010089#define snd_kctl_event(n) list_entry(n, struct snd_kctl_event, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Clemens Ladisch25d27ed2009-11-02 09:35:44 +010091struct pid;
92
Takashi Iwai23c18d42014-02-19 14:30:29 +010093enum {
94 SND_CTL_SUBDEV_PCM,
95 SND_CTL_SUBDEV_RAWMIDI,
96 SND_CTL_SUBDEV_ITEMS,
97};
98
Takashi Iwai82e9bae2005-11-17 13:53:23 +010099struct snd_ctl_file {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 struct list_head list; /* list of all control files */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100101 struct snd_card *card;
Clemens Ladisch25d27ed2009-11-02 09:35:44 +0100102 struct pid *pid;
Takashi Iwai23c18d42014-02-19 14:30:29 +0100103 int preferred_subdevice[SND_CTL_SUBDEV_ITEMS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 wait_queue_head_t change_sleep;
105 spinlock_t read_lock;
106 struct fasync_struct *fasync;
107 int subscribed; /* read interface is activated */
108 struct list_head events; /* waiting events for read */
109};
110
Jaroslav Kysela3f0638a2021-03-17 18:29:41 +0100111struct snd_ctl_layer_ops {
112 struct snd_ctl_layer_ops *next;
113 const char *module_name;
114 void (*lregister)(struct snd_card *card);
115 void (*ldisconnect)(struct snd_card *card);
116 void (*lnotify)(struct snd_card *card, unsigned int mask, struct snd_kcontrol *kctl, unsigned int ioff);
117};
118
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100119#define snd_ctl_file(n) list_entry(n, struct snd_ctl_file, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100121typedef int (*snd_kctl_ioctl_func_t) (struct snd_card * card,
122 struct snd_ctl_file * control,
123 unsigned int cmd, unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100125void snd_ctl_notify(struct snd_card * card, unsigned int mask, struct snd_ctl_elem_id * id);
Jaroslav Kysela1fa44452021-03-17 18:29:40 +0100126void 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 -0700127
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100128struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new * kcontrolnew, void * private_data);
129void snd_ctl_free_one(struct snd_kcontrol * kcontrol);
130int snd_ctl_add(struct snd_card * card, struct snd_kcontrol * kcontrol);
131int snd_ctl_remove(struct snd_card * card, struct snd_kcontrol * kcontrol);
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000132int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol, bool add_on_replace);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100133int snd_ctl_remove_id(struct snd_card * card, struct snd_ctl_elem_id *id);
134int 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 +0100135int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id, int active);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100136struct snd_kcontrol *snd_ctl_find_numid(struct snd_card * card, unsigned int numid);
137struct snd_kcontrol *snd_ctl_find_id(struct snd_card * card, struct snd_ctl_elem_id *id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100139int snd_ctl_create(struct snd_card *card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn);
142int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn);
143#ifdef CONFIG_COMPAT
144int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn);
145int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn);
146#else
147#define snd_ctl_register_ioctl_compat(fcn)
148#define snd_ctl_unregister_ioctl_compat(fcn)
149#endif
150
Jaroslav Kysela3f0638a2021-03-17 18:29:41 +0100151int snd_ctl_request_layer(const char *module_name);
152void snd_ctl_register_layer(struct snd_ctl_layer_ops *lops);
153void snd_ctl_disconnect_layer(struct snd_ctl_layer_ops *lops);
154
Takashi Iwai23c18d42014-02-19 14:30:29 +0100155int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type);
156
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100157static inline unsigned int snd_ctl_get_ioffnum(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Takashi Iwai088e8612018-04-24 07:45:56 +0200159 unsigned int ioff = id->numid - kctl->id.numid;
160 return array_index_nospec(ioff, kctl->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100163static inline unsigned int snd_ctl_get_ioffidx(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->index - kctl->id.index;
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_ioff(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 if (id->numid) {
172 return snd_ctl_get_ioffnum(kctl, id);
173 } else {
174 return snd_ctl_get_ioffidx(kctl, id);
175 }
176}
177
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100178static inline struct snd_ctl_elem_id *snd_ctl_build_ioff(struct snd_ctl_elem_id *dst_id,
179 struct snd_kcontrol *src_kctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 unsigned int offset)
181{
182 *dst_id = src_kctl->id;
183 dst_id->index += offset;
184 dst_id->numid += offset;
185 return dst_id;
186}
187
Takashi Iwaib9ed4f22007-07-23 15:41:34 +0200188/*
Clemens Ladisch96007322011-01-10 16:25:44 +0100189 * Frequently used control callbacks/helpers
Takashi Iwaib9ed4f22007-07-23 15:41:34 +0200190 */
191int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
192 struct snd_ctl_elem_info *uinfo);
193int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
194 struct snd_ctl_elem_info *uinfo);
Clemens Ladisch96007322011-01-10 16:25:44 +0100195int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
196 unsigned int items, const char *const names[]);
Takashi Iwaib9ed4f22007-07-23 15:41:34 +0200197
Takashi Iwaie922b002008-02-18 13:03:13 +0100198/*
199 * virtual master control
200 */
201struct snd_kcontrol *snd_ctl_make_virtual_master(char *name,
202 const unsigned int *tlv);
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200203int _snd_ctl_add_follower(struct snd_kcontrol *master,
204 struct snd_kcontrol *follower,
205 unsigned int flags);
206/* optional flags for follower */
207#define SND_CTL_FOLLOWER_NEED_UPDATE (1 << 0)
Takashi Iwaif5b1db62009-01-16 18:15:22 +0100208
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100209/**
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200210 * snd_ctl_add_follower - Add a virtual follower control
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100211 * @master: vmaster element
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200212 * @follower: follower element to add
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100213 *
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200214 * Add a virtual follower control to the given master element created via
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100215 * snd_ctl_create_virtual_master() beforehand.
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100216 *
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200217 * All followers must be the same type (returning the same information
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300218 * via info callback). The function doesn't check it, so it's your
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100219 * responsibility.
220 *
221 * Also, some additional limitations:
222 * at most two channels,
223 * logarithmic volume control (dB level) thus no linear volume,
224 * master can only attenuate the volume without gain
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100225 *
226 * Return: Zero if successful or a negative error code.
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100227 */
Takashi Iwaif5b1db62009-01-16 18:15:22 +0100228static inline int
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200229snd_ctl_add_follower(struct snd_kcontrol *master, struct snd_kcontrol *follower)
Takashi Iwaif5b1db62009-01-16 18:15:22 +0100230{
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200231 return _snd_ctl_add_follower(master, follower, 0);
Takashi Iwaif5b1db62009-01-16 18:15:22 +0100232}
233
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100234/**
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200235 * snd_ctl_add_follower_uncached - Add a virtual follower control
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100236 * @master: vmaster element
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200237 * @follower: follower element to add
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100238 *
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200239 * Add a virtual follower control to the given master.
240 * Unlike snd_ctl_add_follower(), the element added via this function
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100241 * is supposed to have volatile values, and get callback is called
Masanari Iida1a6ab462015-03-04 10:56:13 +0900242 * at each time queried from the master.
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100243 *
244 * When the control peeks the hardware values directly and the value
245 * can be changed by other means than the put callback of the element,
246 * this function should be used to keep the value always up-to-date.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100247 *
248 * Return: Zero if successful or a negative error code.
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100249 */
Takashi Iwaif5b1db62009-01-16 18:15:22 +0100250static inline int
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200251snd_ctl_add_follower_uncached(struct snd_kcontrol *master,
252 struct snd_kcontrol *follower)
Takashi Iwaif5b1db62009-01-16 18:15:22 +0100253{
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200254 return _snd_ctl_add_follower(master, follower, SND_CTL_FOLLOWER_NEED_UPDATE);
Takashi Iwaif5b1db62009-01-16 18:15:22 +0100255}
256
Takashi Iwai2ad787e2012-03-12 12:18:37 +0100257int snd_ctl_add_vmaster_hook(struct snd_kcontrol *kctl,
258 void (*hook)(void *private_data, int),
259 void *private_data);
Takashi Iwai1ca2f2e2013-06-24 15:51:54 +0200260void snd_ctl_sync_vmaster(struct snd_kcontrol *kctl, bool hook_only);
261#define snd_ctl_sync_vmaster_hook(kctl) snd_ctl_sync_vmaster(kctl, true)
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200262int snd_ctl_apply_vmaster_followers(struct snd_kcontrol *kctl,
263 int (*func)(struct snd_kcontrol *vfollower,
264 struct snd_kcontrol *follower,
265 void *arg),
266 void *arg);
Takashi Iwai2ad787e2012-03-12 12:18:37 +0100267
Takashi Iwai35be5442011-11-02 08:36:06 +0100268/*
269 * Helper functions for jack-detection controls
270 */
271struct snd_kcontrol *
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800272snd_kctl_jack_new(const char *name, struct snd_card *card);
Takashi Iwai35be5442011-11-02 08:36:06 +0100273void snd_kctl_jack_report(struct snd_card *card,
274 struct snd_kcontrol *kctl, bool status);
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276#endif /* __SOUND_CONTROL_H */