1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2009 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <pulse/xmalloc.h>
29 #include <pulse/util.h>
30
31 #include <pulsecore/log.h>
32 #include <pulsecore/macro.h>
33 #include <pulsecore/core-util.h>
34 #include <pulsecore/namereg.h>
35 #include <pulsecore/device-port.h>
36
37 #include "card.h"
38
pa_available_to_string(pa_available_t available)39 const char *pa_available_to_string(pa_available_t available) {
40 switch (available) {
41 case PA_AVAILABLE_UNKNOWN:
42 return "unknown";
43 case PA_AVAILABLE_NO:
44 return "no";
45 case PA_AVAILABLE_YES:
46 return "yes";
47 default:
48 pa_assert_not_reached();
49 }
50 }
51
pa_card_profile_new(const char * name,const char * description,size_t extra)52 pa_card_profile *pa_card_profile_new(const char *name, const char *description, size_t extra) {
53 pa_card_profile *c;
54
55 pa_assert(name);
56
57 c = pa_xmalloc0(PA_ALIGN(sizeof(pa_card_profile)) + extra);
58 c->name = pa_xstrdup(name);
59 c->description = pa_xstrdup(description);
60 c->available = PA_AVAILABLE_UNKNOWN;
61
62 return c;
63 }
64
pa_card_profile_free(pa_card_profile * c)65 void pa_card_profile_free(pa_card_profile *c) {
66 pa_assert(c);
67
68 pa_xfree(c->input_name);
69 pa_xfree(c->output_name);
70 pa_xfree(c->name);
71 pa_xfree(c->description);
72 pa_xfree(c);
73 }
74
pa_card_profile_set_available(pa_card_profile * c,pa_available_t available)75 void pa_card_profile_set_available(pa_card_profile *c, pa_available_t available) {
76 pa_core *core;
77
78 pa_assert(c);
79 pa_assert(c->card); /* Modify member variable directly during creation instead of using this function */
80
81 if (c->available == available)
82 return;
83
84 c->available = available;
85 pa_log_debug("Setting card %s profile %s to availability status %s", c->card->name, c->name,
86 pa_available_to_string(available));
87
88 /* Post subscriptions to the card which owns us */
89 pa_assert_se(core = c->card->core);
90 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE, c->card->index);
91
92 if (c->card->linked)
93 pa_hook_fire(&core->hooks[PA_CORE_HOOK_CARD_PROFILE_AVAILABLE_CHANGED], c);
94 }
95
pa_card_new_data_init(pa_card_new_data * data)96 pa_card_new_data* pa_card_new_data_init(pa_card_new_data *data) {
97 pa_assert(data);
98
99 memset(data, 0, sizeof(*data));
100 data->proplist = pa_proplist_new();
101 data->profiles = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, (pa_free_cb_t) pa_card_profile_free);
102 data->ports = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, (pa_free_cb_t) pa_device_port_unref);
103 return data;
104 }
105
pa_card_new_data_set_name(pa_card_new_data * data,const char * name)106 void pa_card_new_data_set_name(pa_card_new_data *data, const char *name) {
107 pa_assert(data);
108
109 pa_xfree(data->name);
110 data->name = pa_xstrdup(name);
111 }
112
pa_card_new_data_set_preferred_port(pa_card_new_data * data,pa_direction_t direction,pa_device_port * port)113 void pa_card_new_data_set_preferred_port(pa_card_new_data *data, pa_direction_t direction, pa_device_port *port) {
114 pa_assert(data);
115
116 if (direction == PA_DIRECTION_INPUT)
117 data->preferred_input_port = port;
118 else
119 data->preferred_output_port = port;
120 }
121
pa_card_new_data_done(pa_card_new_data * data)122 void pa_card_new_data_done(pa_card_new_data *data) {
123
124 pa_assert(data);
125
126 pa_proplist_free(data->proplist);
127
128 if (data->profiles)
129 pa_hashmap_free(data->profiles);
130
131 if (data->ports)
132 pa_hashmap_free(data->ports);
133
134 pa_xfree(data->name);
135 }
136
pa_card_new(pa_core * core,pa_card_new_data * data)137 pa_card *pa_card_new(pa_core *core, pa_card_new_data *data) {
138 pa_card *c;
139 const char *name;
140 void *state;
141 pa_card_profile *profile;
142 pa_device_port *port;
143
144 pa_core_assert_ref(core);
145 pa_assert(data);
146 pa_assert(data->name);
147 pa_assert(data->profiles);
148 pa_assert(!pa_hashmap_isempty(data->profiles));
149
150 c = pa_xnew0(pa_card, 1);
151
152 if (!(name = pa_namereg_register(core, data->name, PA_NAMEREG_CARD, c, data->namereg_fail))) {
153 pa_xfree(c);
154 return NULL;
155 }
156
157 pa_card_new_data_set_name(data, name);
158 pa_hook_fire(&core->hooks[PA_CORE_HOOK_CARD_NEW], data);
159
160 c->core = core;
161 c->name = pa_xstrdup(data->name);
162 c->proplist = pa_proplist_copy(data->proplist);
163 c->driver = pa_xstrdup(pa_path_get_filename(data->driver));
164 c->module = data->module;
165
166 c->sinks = pa_idxset_new(NULL, NULL);
167 c->sources = pa_idxset_new(NULL, NULL);
168
169 /* As a minor optimization we just steal the list instead of
170 * copying it here */
171 pa_assert_se(c->profiles = data->profiles);
172 data->profiles = NULL;
173 pa_assert_se(c->ports = data->ports);
174 data->ports = NULL;
175
176 PA_HASHMAP_FOREACH(profile, c->profiles, state)
177 profile->card = c;
178
179 PA_HASHMAP_FOREACH(port, c->ports, state)
180 port->card = c;
181
182 c->preferred_input_port = data->preferred_input_port;
183 c->preferred_output_port = data->preferred_output_port;
184
185 pa_device_init_description(c->proplist, c);
186 pa_device_init_icon(c->proplist, true);
187 pa_device_init_intended_roles(c->proplist);
188
189 return c;
190 }
191
pa_card_choose_initial_profile(pa_card * card)192 void pa_card_choose_initial_profile(pa_card *card) {
193 pa_card_profile *profile;
194 void *state;
195 pa_card_profile *best = NULL;
196
197 pa_assert(card);
198
199 /* By default, pick the highest priority profile that is not unavailable,
200 * or if all profiles are unavailable, pick the profile with the highest
201 * priority regardless of its availability. */
202
203 pa_log_debug("Looking for initial profile for card %s", card->name);
204 PA_HASHMAP_FOREACH(profile, card->profiles, state) {
205 pa_log_debug("%s availability %s", profile->name, pa_available_to_string(profile->available));
206 if (profile->available == PA_AVAILABLE_NO)
207 continue;
208
209 if (!best || profile->priority > best->priority)
210 best = profile;
211 }
212
213 if (!best) {
214 PA_HASHMAP_FOREACH(profile, card->profiles, state) {
215 if (!best || profile->priority > best->priority)
216 best = profile;
217 }
218 }
219 pa_assert(best);
220
221 card->active_profile = best;
222 card->save_profile = false;
223 pa_log_info("%s: active_profile: %s", card->name, card->active_profile->name);
224
225 /* Let policy modules override the default. */
226 pa_hook_fire(&card->core->hooks[PA_CORE_HOOK_CARD_CHOOSE_INITIAL_PROFILE], card);
227 }
228
pa_card_put(pa_card * card)229 void pa_card_put(pa_card *card) {
230 pa_assert(card);
231
232 pa_assert_se(pa_idxset_put(card->core->cards, card, &card->index) >= 0);
233 card->linked = true;
234
235 pa_log_info("Created %u \"%s\"", card->index, card->name);
236 pa_hook_fire(&card->core->hooks[PA_CORE_HOOK_CARD_PUT], card);
237 pa_subscription_post(card->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_NEW, card->index);
238 }
239
pa_card_free(pa_card * c)240 void pa_card_free(pa_card *c) {
241 pa_core *core;
242
243 pa_assert(c);
244 pa_assert(c->core);
245
246 core = c->core;
247
248 if (c->linked) {
249 pa_hook_fire(&core->hooks[PA_CORE_HOOK_CARD_UNLINK], c);
250
251 pa_idxset_remove_by_data(c->core->cards, c, NULL);
252 pa_log_info("Freed %u \"%s\"", c->index, c->name);
253 pa_subscription_post(c->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_REMOVE, c->index);
254 }
255
256 pa_namereg_unregister(core, c->name);
257
258 pa_assert(pa_idxset_isempty(c->sinks));
259 pa_idxset_free(c->sinks, NULL);
260 pa_assert(pa_idxset_isempty(c->sources));
261 pa_idxset_free(c->sources, NULL);
262
263 pa_hashmap_free(c->ports);
264
265 if (c->profiles)
266 pa_hashmap_free(c->profiles);
267
268 pa_proplist_free(c->proplist);
269 pa_xfree(c->driver);
270 pa_xfree(c->name);
271 pa_xfree(c);
272 }
273
pa_card_add_profile(pa_card * c,pa_card_profile * profile)274 void pa_card_add_profile(pa_card *c, pa_card_profile *profile) {
275 pa_assert(c);
276 pa_assert(profile);
277
278 /* take ownership of the profile */
279 pa_assert_se(pa_hashmap_put(c->profiles, profile->name, profile) >= 0);
280 profile->card = c;
281
282 pa_subscription_post(c->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE, c->index);
283
284 pa_hook_fire(&c->core->hooks[PA_CORE_HOOK_CARD_PROFILE_ADDED], profile);
285 }
286
profile_name_for_dir(pa_card_profile * cp,pa_direction_t dir)287 static const char* profile_name_for_dir(pa_card_profile *cp, pa_direction_t dir) {
288 if (dir == PA_DIRECTION_OUTPUT && cp->output_name)
289 return cp->output_name;
290 if (dir == PA_DIRECTION_INPUT && cp->input_name)
291 return cp->input_name;
292 return cp->name;
293 }
294
update_port_preferred_profile(pa_card * c)295 static void update_port_preferred_profile(pa_card *c) {
296 pa_sink *sink;
297 pa_source *source;
298 uint32_t state;
299
300 PA_IDXSET_FOREACH(sink, c->sinks, state)
301 if (sink->active_port)
302 pa_device_port_set_preferred_profile(sink->active_port, profile_name_for_dir(c->active_profile, PA_DIRECTION_OUTPUT));
303 PA_IDXSET_FOREACH(source, c->sources, state)
304 if (source->active_port)
305 pa_device_port_set_preferred_profile(source->active_port, profile_name_for_dir(c->active_profile, PA_DIRECTION_INPUT));
306 }
307
pa_card_set_profile(pa_card * c,pa_card_profile * profile,bool save)308 int pa_card_set_profile(pa_card *c, pa_card_profile *profile, bool save) {
309 int r;
310
311 pa_assert(c);
312 pa_assert(profile);
313 pa_assert(profile->card == c);
314
315 if (!c->set_profile) {
316 pa_log_debug("set_profile() operation not implemented for card %u \"%s\"", c->index, c->name);
317 return -PA_ERR_NOTIMPLEMENTED;
318 }
319
320 if (c->active_profile == profile) {
321 if (save && !c->save_profile) {
322 update_port_preferred_profile(c);
323 c->save_profile = true;
324 }
325 return 0;
326 }
327
328 /* If we're setting the initial profile, we shouldn't call set_profile(),
329 * because the implementations don't expect that (for historical reasons).
330 * We should just set c->active_profile, and the implementations will
331 * properly set up that profile after pa_card_put() has returned. It would
332 * be probably good to change this so that also the initial profile can be
333 * set up in set_profile(), but if set_profile() fails, that would need
334 * some better handling than what we do here currently. */
335 if (c->linked && (r = c->set_profile(c, profile)) < 0)
336 return r;
337
338 pa_log_debug("%s: active_profile: %s -> %s", c->name, c->active_profile->name, profile->name);
339 c->active_profile = profile;
340 c->save_profile = save;
341
342 if (save)
343 update_port_preferred_profile(c);
344
345 if (c->linked) {
346 pa_hook_fire(&c->core->hooks[PA_CORE_HOOK_CARD_PROFILE_CHANGED], c);
347 pa_subscription_post(c->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE, c->index);
348 }
349
350 return 0;
351 }
352
pa_card_set_preferred_port(pa_card * c,pa_direction_t direction,pa_device_port * port)353 void pa_card_set_preferred_port(pa_card *c, pa_direction_t direction, pa_device_port *port) {
354 pa_device_port *old_port;
355 const char *old_port_str;
356 const char *new_port_str;
357 pa_card_preferred_port_changed_hook_data data;
358
359 pa_assert(c);
360
361 if (direction == PA_DIRECTION_INPUT) {
362 old_port = c->preferred_input_port;
363 old_port_str = c->preferred_input_port ? c->preferred_input_port->name : "(unset)";
364 } else {
365 old_port = c->preferred_output_port;
366 old_port_str = c->preferred_output_port ? c->preferred_output_port->name : "(unset)";
367 }
368
369 if (port == old_port)
370 return;
371
372 new_port_str = port ? port->name : "(unset)";
373
374 if (direction == PA_DIRECTION_INPUT) {
375 c->preferred_input_port = port;
376 pa_log_debug("%s: preferred_input_port: %s -> %s", c->name, old_port_str, new_port_str);
377 } else {
378 c->preferred_output_port = port;
379 pa_log_debug("%s: preferred_output_port: %s -> %s", c->name, old_port_str, new_port_str);
380 }
381
382 data.card = c;
383 data.direction = direction;
384 pa_hook_fire(&c->core->hooks[PA_CORE_HOOK_CARD_PREFERRED_PORT_CHANGED], &data);
385 }
386
pa_card_suspend(pa_card * c,bool suspend,pa_suspend_cause_t cause)387 int pa_card_suspend(pa_card *c, bool suspend, pa_suspend_cause_t cause) {
388 pa_sink *sink;
389 pa_source *source;
390 pa_suspend_cause_t suspend_cause;
391 uint32_t idx;
392 int ret = 0;
393
394 pa_assert(c);
395 pa_assert(cause != 0);
396
397 suspend_cause = c->suspend_cause;
398
399 if (suspend)
400 suspend_cause |= cause;
401 else
402 suspend_cause &= ~cause;
403
404 if (c->suspend_cause != suspend_cause) {
405 pa_log_debug("Card suspend causes/state changed");
406 c->suspend_cause = suspend_cause;
407 pa_hook_fire(&c->core->hooks[PA_CORE_HOOK_CARD_SUSPEND_CHANGED], c);
408 }
409
410 PA_IDXSET_FOREACH(sink, c->sinks, idx) {
411 int r;
412
413 if ((r = pa_sink_suspend(sink, suspend, cause)) < 0)
414 ret = r;
415 }
416
417 PA_IDXSET_FOREACH(source, c->sources, idx) {
418 int r;
419
420 if ((r = pa_source_suspend(source, suspend, cause)) < 0)
421 ret = r;
422 }
423
424 return ret;
425 }
426