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