blob: 77d9fa10812d01dcc35f0b24b6fc5a66cbe9bb94 [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
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100111#define snd_ctl_file(n) list_entry(n, struct snd_ctl_file, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100113typedef int (*snd_kctl_ioctl_func_t) (struct snd_card * card,
114 struct snd_ctl_file * control,
115 unsigned int cmd, unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100117void snd_ctl_notify(struct snd_card * card, unsigned int mask, struct snd_ctl_elem_id * id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100119struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new * kcontrolnew, void * private_data);
120void snd_ctl_free_one(struct snd_kcontrol * kcontrol);
121int snd_ctl_add(struct snd_card * card, struct snd_kcontrol * kcontrol);
122int snd_ctl_remove(struct snd_card * card, struct snd_kcontrol * kcontrol);
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000123int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol, bool add_on_replace);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100124int snd_ctl_remove_id(struct snd_card * card, struct snd_ctl_elem_id *id);
125int snd_ctl_rename_id(struct snd_card * card, struct snd_ctl_elem_id *src_id, struct snd_ctl_elem_id *dst_id);
Takashi Iwai3cbdd752008-08-29 16:09:01 +0200126int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
127 int active);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100128struct snd_kcontrol *snd_ctl_find_numid(struct snd_card * card, unsigned int numid);
129struct snd_kcontrol *snd_ctl_find_id(struct snd_card * card, struct snd_ctl_elem_id *id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100131int snd_ctl_create(struct snd_card *card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn);
134int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn);
135#ifdef CONFIG_COMPAT
136int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn);
137int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn);
138#else
139#define snd_ctl_register_ioctl_compat(fcn)
140#define snd_ctl_unregister_ioctl_compat(fcn)
141#endif
142
Takashi Iwai23c18d42014-02-19 14:30:29 +0100143int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type);
144
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100145static inline unsigned int snd_ctl_get_ioffnum(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Takashi Iwai088e8612018-04-24 07:45:56 +0200147 unsigned int ioff = id->numid - kctl->id.numid;
148 return array_index_nospec(ioff, kctl->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100151static inline unsigned int snd_ctl_get_ioffidx(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Takashi Iwai088e8612018-04-24 07:45:56 +0200153 unsigned int ioff = id->index - kctl->id.index;
154 return array_index_nospec(ioff, kctl->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100157static inline unsigned int snd_ctl_get_ioff(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 if (id->numid) {
160 return snd_ctl_get_ioffnum(kctl, id);
161 } else {
162 return snd_ctl_get_ioffidx(kctl, id);
163 }
164}
165
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100166static inline struct snd_ctl_elem_id *snd_ctl_build_ioff(struct snd_ctl_elem_id *dst_id,
167 struct snd_kcontrol *src_kctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 unsigned int offset)
169{
170 *dst_id = src_kctl->id;
171 dst_id->index += offset;
172 dst_id->numid += offset;
173 return dst_id;
174}
175
Takashi Iwaib9ed4f22007-07-23 15:41:34 +0200176/*
Clemens Ladisch96007322011-01-10 16:25:44 +0100177 * Frequently used control callbacks/helpers
Takashi Iwaib9ed4f22007-07-23 15:41:34 +0200178 */
179int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
180 struct snd_ctl_elem_info *uinfo);
181int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
182 struct snd_ctl_elem_info *uinfo);
Clemens Ladisch96007322011-01-10 16:25:44 +0100183int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
184 unsigned int items, const char *const names[]);
Takashi Iwaib9ed4f22007-07-23 15:41:34 +0200185
Takashi Iwaie922b002008-02-18 13:03:13 +0100186/*
187 * virtual master control
188 */
189struct snd_kcontrol *snd_ctl_make_virtual_master(char *name,
190 const unsigned int *tlv);
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200191int _snd_ctl_add_follower(struct snd_kcontrol *master,
192 struct snd_kcontrol *follower,
193 unsigned int flags);
194/* optional flags for follower */
195#define SND_CTL_FOLLOWER_NEED_UPDATE (1 << 0)
Takashi Iwaif5b1db62009-01-16 18:15:22 +0100196
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100197/**
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200198 * snd_ctl_add_follower - Add a virtual follower control
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100199 * @master: vmaster element
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200200 * @follower: follower element to add
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100201 *
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200202 * Add a virtual follower control to the given master element created via
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100203 * snd_ctl_create_virtual_master() beforehand.
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100204 *
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200205 * All followers must be the same type (returning the same information
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300206 * via info callback). The function doesn't check it, so it's your
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100207 * responsibility.
208 *
209 * Also, some additional limitations:
210 * at most two channels,
211 * logarithmic volume control (dB level) thus no linear volume,
212 * master can only attenuate the volume without gain
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100213 *
214 * Return: Zero if successful or a negative error code.
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100215 */
Takashi Iwaif5b1db62009-01-16 18:15:22 +0100216static inline int
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200217snd_ctl_add_follower(struct snd_kcontrol *master, struct snd_kcontrol *follower)
Takashi Iwaif5b1db62009-01-16 18:15:22 +0100218{
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200219 return _snd_ctl_add_follower(master, follower, 0);
Takashi Iwaif5b1db62009-01-16 18:15:22 +0100220}
221
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100222/**
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200223 * snd_ctl_add_follower_uncached - Add a virtual follower control
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100224 * @master: vmaster element
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200225 * @follower: follower element to add
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100226 *
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200227 * Add a virtual follower control to the given master.
228 * Unlike snd_ctl_add_follower(), the element added via this function
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100229 * is supposed to have volatile values, and get callback is called
Masanari Iida1a6ab462015-03-04 10:56:13 +0900230 * at each time queried from the master.
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100231 *
232 * When the control peeks the hardware values directly and the value
233 * can be changed by other means than the put callback of the element,
234 * this function should be used to keep the value always up-to-date.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100235 *
236 * Return: Zero if successful or a negative error code.
Takashi Iwai79c7cdd2009-02-09 14:47:19 +0100237 */
Takashi Iwaif5b1db62009-01-16 18:15:22 +0100238static inline int
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200239snd_ctl_add_follower_uncached(struct snd_kcontrol *master,
240 struct snd_kcontrol *follower)
Takashi Iwaif5b1db62009-01-16 18:15:22 +0100241{
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200242 return _snd_ctl_add_follower(master, follower, SND_CTL_FOLLOWER_NEED_UPDATE);
Takashi Iwaif5b1db62009-01-16 18:15:22 +0100243}
244
Takashi Iwai2ad787e2012-03-12 12:18:37 +0100245int snd_ctl_add_vmaster_hook(struct snd_kcontrol *kctl,
246 void (*hook)(void *private_data, int),
247 void *private_data);
Takashi Iwai1ca2f2e2013-06-24 15:51:54 +0200248void snd_ctl_sync_vmaster(struct snd_kcontrol *kctl, bool hook_only);
249#define snd_ctl_sync_vmaster_hook(kctl) snd_ctl_sync_vmaster(kctl, true)
Takashi Iwai9ab0cb32020-07-17 17:45:17 +0200250int snd_ctl_apply_vmaster_followers(struct snd_kcontrol *kctl,
251 int (*func)(struct snd_kcontrol *vfollower,
252 struct snd_kcontrol *follower,
253 void *arg),
254 void *arg);
Takashi Iwai2ad787e2012-03-12 12:18:37 +0100255
Takashi Iwai35be5442011-11-02 08:36:06 +0100256/*
257 * Helper functions for jack-detection controls
258 */
259struct snd_kcontrol *
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800260snd_kctl_jack_new(const char *name, struct snd_card *card);
Takashi Iwai35be5442011-11-02 08:36:06 +0100261void snd_kctl_jack_report(struct snd_card *card,
262 struct snd_kcontrol *kctl, bool status);
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264#endif /* __SOUND_CONTROL_H */