1 /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  */
5 
6 #ifndef _CRAS_ALSA_MIXER_H
7 #define _CRAS_ALSA_MIXER_H
8 
9 #include <alsa/asoundlib.h>
10 #include <iniparser.h>
11 
12 #include "cras_types.h"
13 
14 /* cras_alsa_mixer represents the alsa mixer interface for an alsa card.  It
15  * houses the volume and mute controls as well as playback switches for
16  * headphones and mic.
17  */
18 
19 struct mixer_control;
20 struct cras_alsa_mixer;
21 struct cras_volume_curve;
22 struct cras_card_config;
23 struct mixer_name;
24 struct ucm_section;
25 
26 /* Creates a cras_alsa_mixer instance for the given alsa device.
27  * Args:
28  *    card_name - Name of the card to open a mixer for.  This is an alsa name of
29  *      the form "hw:X" where X ranges from 0 to 31 inclusive.
30  * Returns:
31  *    A pointer to the newly created cras_alsa_mixer which must later be freed
32  *    by calling cras_alsa_mixer_destroy. The control in the mixer is not added
33  *    yet.
34  */
35 struct cras_alsa_mixer *cras_alsa_mixer_create(const char *card_name);
36 
37 /* Adds controls to a cras_alsa_mixer from the given UCM section.
38  * Args:
39  *    cmix - A pointer to cras_alsa_mixer.
40  *    section - A UCM section.
41  * Returns:
42  *    0 on success. Negative error code otherwise.
43  */
44 int cras_alsa_mixer_add_controls_in_section(
45 		struct cras_alsa_mixer *cmix,
46 		struct ucm_section *section);
47 
48 /* Adds controls to a cras_alsa_mixer instance by name matching.
49  * Args:
50  *    cmix - A pointer to cras_alsa_mixer.
51  *    extra_controls - A list array of extra mixer control names, always added.
52  *    coupled_controls - A list of coupled mixer control names.
53  * Returns:
54  *    0 on success. Other error code if error happens.
55  */
56 int cras_alsa_mixer_add_controls_by_name_matching(
57 		struct cras_alsa_mixer *cmix,
58 		struct mixer_name *extra_controls,
59 		struct mixer_name *coupled_controls);
60 
61 /* Destroys a cras_alsa_mixer that was returned from cras_alsa_mixer_create.
62  * Args:
63  *    cras_mixer - The cras_alsa_mixer pointer returned from
64  *        cras_alsa_mixer_create.
65  */
66 void cras_alsa_mixer_destroy(struct cras_alsa_mixer *cras_mixer);
67 
68 /* Returns if the mixer has any main volume control. */
69 int cras_alsa_mixer_has_main_volume(const struct cras_alsa_mixer *cras_mixer);
70 
71 /* Returns if the mixer control supports volume adjust. */
72 int cras_alsa_mixer_has_volume(const struct mixer_control *mixer_control);
73 
74 /* Sets the output volume for the device associated with this mixer.
75  * Args:
76  *    cras_mixer - The mixer to set the volume on.
77  *    dBFS - The volume level as dB * 100.  dB is a normally a negative quantity
78  *      specifying how much to attenuate.
79  *    mixer_output - The mixer output to set if not all attenuation can be
80  *      obtained from the main controls.  Can be null.
81  */
82 void cras_alsa_mixer_set_dBFS(struct cras_alsa_mixer *cras_mixer,
83 			      long dBFS,
84 			      struct mixer_control *mixer_output);
85 
86 /* Gets the volume range of the mixer in dB.
87  * Args:
88  *    cras_mixer - The mixer to get the volume range.
89  */
90 long cras_alsa_mixer_get_dB_range(struct cras_alsa_mixer *cras_mixer);
91 
92 /* Gets the volume range of the mixer output in dB.
93  * Args:
94  *    mixer_output - The mixer output to get the volume range.
95  */
96 long cras_alsa_mixer_get_output_dB_range(
97 		struct mixer_control *mixer_output);
98 
99 /* Sets the capture gain for the device associated with this mixer.
100  * Args:
101  *    cras_mixer - The mixer to set the volume on.
102  *    dBFS - The capture gain level as dB * 100.  dB can be a positive or a
103  *    negative quantity specifying how much gain or attenuation to apply.
104  *    mixer_input - The specific mixer control for input node, can be null.
105  */
106 void cras_alsa_mixer_set_capture_dBFS(struct cras_alsa_mixer *cras_mixer,
107 				      long dBFS,
108 				      struct mixer_control* mixer_input);
109 
110 /* Gets the minimum allowed setting for capture gain.
111  * Args:
112  *    cmix - The mixer to set the capture gain on.
113  *    mixer_input - The additional input mixer control, mainly specified
114  *      in ucm config. Can be null.
115  * Returns:
116  *    The minimum allowed capture gain in dBFS * 100.
117  */
118 long cras_alsa_mixer_get_minimum_capture_gain(
119                 struct cras_alsa_mixer *cmix,
120 		struct mixer_control *mixer_input);
121 
122 /* Gets the maximum allowed setting for capture gain.
123  * Args:
124  *    cmix - The mixer to set the capture gain on.
125  *    mixer_input - The additional input mixer control, mainly specified
126  *      in ucm config. Can be null.
127  * Returns:
128  *    The maximum allowed capture gain in dBFS * 100.
129  */
130 long cras_alsa_mixer_get_maximum_capture_gain(
131 		struct cras_alsa_mixer *cmix,
132 		struct mixer_control *mixer_input);
133 
134 /* Sets the playback switch for the device.
135  * Args:
136  *    cras_mixer - Mixer to set the playback switch.
137  *    muted - 1 if muted, 0 if not.
138  *    mixer_output - The output specific mixer control to mute/unmute. Pass NULL
139  *                   to skip it.
140  */
141 void cras_alsa_mixer_set_mute(struct cras_alsa_mixer *cras_mixer,
142 			      int muted,
143 			      struct mixer_control *mixer_output);
144 
145 /* Sets the capture switch for the device.
146  * Args:
147  *    cras_mixer - Mixer to set the volume in.
148  *    muted - 1 if muted, 0 if not.
149  *    mixer_input - The mixer input to mute if no master mute.
150  */
151 void cras_alsa_mixer_set_capture_mute(struct cras_alsa_mixer *cras_mixer,
152 				      int muted,
153 				      struct mixer_control *mixer_input);
154 
155 /* Invokes the provided callback once for each output (input).
156  * The callback will be provided with a reference to the control
157  * that can be queried to see what the control supports.
158  * Args:
159  *    cras_mixer - Mixer to set the volume in.
160  *    cb - Function to call for each output (input).
161  *    cb_arg - Argument to pass to cb.
162  */
163 typedef void (*cras_alsa_mixer_control_callback)(
164 		struct mixer_control *control, void *arg);
165 void cras_alsa_mixer_list_outputs(struct cras_alsa_mixer *cras_mixer,
166 				  cras_alsa_mixer_control_callback cb,
167 				  void *cb_arg);
168 
169 void cras_alsa_mixer_list_inputs(struct cras_alsa_mixer *cras_mixer,
170 				 cras_alsa_mixer_control_callback cb,
171 				 void *cb_arg);
172 
173 /* Gets the name of a given control. */
174 const char *cras_alsa_mixer_get_control_name(
175 		const struct mixer_control *control);
176 
177 /* Returns the mixer control matching the given direction and name.
178  * Args:
179  *    cras_mixer - Mixer to search for a control.
180  *    dir - Control's direction (OUTPUT or INPUT).
181  *    name - Name to search for.
182  *    create_missing - When non-zero, attempt to create a new control with
183  *		       the given name.
184  * Returns:
185  *    A pointer to the matching mixer control, or NULL if none found.
186  */
187 struct mixer_control *cras_alsa_mixer_get_control_matching_name(
188 		struct cras_alsa_mixer *cras_mixer,
189 		enum CRAS_STREAM_DIRECTION dir, const char *name,
190 		int create_missing);
191 
192 /* Returns the mixer control associated with the given section.
193  * The control is the one that matches 'mixer_name', or if that is not defined
194  * then it will be the control matching 'section->name', based on the
195  * coupled mixer controls.
196  * Args:
197  *    cras_mixer - Mixer to search for a control.
198  *    section - Associated UCM section.
199  * Returns:
200  *    A pointer to the associated mixer control, or NULL if none found.
201  */
202 struct mixer_control *cras_alsa_mixer_get_control_for_section(
203 		struct cras_alsa_mixer *cras_mixer,
204 		const struct ucm_section *section);
205 
206 /* Finds the output that matches the given string.  Used to match Jacks to Mixer
207  * elements.
208  * Args:
209  *    cras_mixer - Mixer to search for a control.
210  *    name - The name to match against the controls.
211  * Returns:
212  *    A pointer to the output with a mixer control that matches "name".
213  */
214 struct mixer_control *cras_alsa_mixer_get_output_matching_name(
215 		struct cras_alsa_mixer *cras_mixer,
216 		const char *name);
217 
218 /* Finds the mixer control for that matches the control name of input control
219  * name specified in ucm config.
220  * Args:
221  *    cras_mixer - Mixer to search for a control.
222  *    name - Name of the control to search for.
223  * Returns:
224  *    A pointer to the input with a mixer control that matches "name".
225  */
226 struct mixer_control *cras_alsa_mixer_get_input_matching_name(
227 		struct cras_alsa_mixer *cras_mixer,
228 		const char *name);
229 
230 /* Sets the given output active or inactive. */
231 int cras_alsa_mixer_set_output_active_state(
232 		struct mixer_control *output,
233 		int active);
234 
235 #endif /* _CRAS_ALSA_MIXER_H */
236