]> code.delx.au - pulseaudio/blob - src/modules/alsa/module-alsa-card.c
alsa: Initialize ports before sinks/sources
[pulseaudio] / src / modules / alsa / module-alsa-card.c
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, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <pulse/xmalloc.h>
27
28 #include <pulsecore/core-util.h>
29 #include <pulsecore/i18n.h>
30 #include <pulsecore/modargs.h>
31 #include <pulsecore/queue.h>
32
33 #include <modules/reserve-wrap.h>
34
35 #ifdef HAVE_UDEV
36 #include <modules/udev-util.h>
37 #endif
38
39 #include "alsa-util.h"
40 #include "alsa-ucm.h"
41 #include "alsa-sink.h"
42 #include "alsa-source.h"
43 #include "module-alsa-card-symdef.h"
44
45 PA_MODULE_AUTHOR("Lennart Poettering");
46 PA_MODULE_DESCRIPTION("ALSA Card");
47 PA_MODULE_VERSION(PACKAGE_VERSION);
48 PA_MODULE_LOAD_ONCE(FALSE);
49 PA_MODULE_USAGE(
50 "name=<name for the card/sink/source, to be prefixed> "
51 "card_name=<name for the card> "
52 "card_properties=<properties for the card> "
53 "sink_name=<name for the sink> "
54 "sink_properties=<properties for the sink> "
55 "source_name=<name for the source> "
56 "source_properties=<properties for the source> "
57 "namereg_fail=<when false attempt to synthesise new names if they are already taken> "
58 "device_id=<ALSA card index> "
59 "format=<sample format> "
60 "rate=<sample rate> "
61 "fragments=<number of fragments> "
62 "fragment_size=<fragment size> "
63 "mmap=<enable memory mapping?> "
64 "tsched=<enable system timer based scheduling mode?> "
65 "tsched_buffer_size=<buffer size when using timer based scheduling> "
66 "tsched_buffer_watermark=<lower fill watermark> "
67 "profile=<profile name> "
68 "fixed_latency_range=<disable latency range changes on underrun?> "
69 "ignore_dB=<ignore dB information from the device?> "
70 "deferred_volume=<Synchronize software and hardware volume changes to avoid momentary jumps?> "
71 "profile_set=<profile set configuration file> "
72 "paths_dir=<directory containing the path configuration files> "
73 "use_ucm=<load use case manager> "
74 );
75
76 static const char* const valid_modargs[] = {
77 "name",
78 "card_name",
79 "card_properties",
80 "sink_name",
81 "sink_properties",
82 "source_name",
83 "source_properties",
84 "namereg_fail",
85 "device_id",
86 "format",
87 "rate",
88 "fragments",
89 "fragment_size",
90 "mmap",
91 "tsched",
92 "tsched_buffer_size",
93 "tsched_buffer_watermark",
94 "fixed_latency_range",
95 "profile",
96 "ignore_dB",
97 "deferred_volume",
98 "profile_set",
99 "paths_dir",
100 "use_ucm",
101 NULL
102 };
103
104 #define DEFAULT_DEVICE_ID "0"
105
106 struct userdata {
107 pa_core *core;
108 pa_module *module;
109
110 char *device_id;
111 int alsa_card_index;
112
113 snd_mixer_t *mixer_handle;
114 snd_hctl_t *hctl_handle;
115 pa_hashmap *jacks;
116 pa_alsa_fdlist *mixer_fdl;
117
118 pa_card *card;
119
120 pa_modargs *modargs;
121
122 pa_alsa_profile_set *profile_set;
123
124 /* ucm stuffs */
125 pa_bool_t use_ucm;
126 pa_alsa_ucm_config ucm;
127
128 /* hooks for modifier action */
129 pa_hook_slot
130 *sink_input_put_hook_slot,
131 *source_output_put_hook_slot,
132 *sink_input_unlink_hook_slot,
133 *source_output_unlink_hook_slot;
134 };
135
136 struct profile_data {
137 pa_alsa_profile *profile;
138 };
139
140 static void add_profiles(struct userdata *u, pa_hashmap *h, pa_hashmap *ports) {
141 pa_alsa_profile *ap;
142 void *state;
143
144 pa_assert(u);
145 pa_assert(h);
146
147 PA_HASHMAP_FOREACH(ap, u->profile_set->profiles, state) {
148 struct profile_data *d;
149 pa_card_profile *cp;
150 pa_alsa_mapping *m;
151 uint32_t idx;
152
153 cp = pa_card_profile_new(ap->name, ap->description, sizeof(struct profile_data));
154 cp->priority = ap->priority;
155
156 if (ap->output_mappings) {
157 cp->n_sinks = pa_idxset_size(ap->output_mappings);
158
159 PA_IDXSET_FOREACH(m, ap->output_mappings, idx) {
160 if (u->use_ucm)
161 pa_alsa_ucm_add_ports_combination(NULL, &m->ucm_context, TRUE, ports, cp, u->core);
162 else
163 pa_alsa_path_set_add_ports(m->output_path_set, cp, ports, NULL, u->core);
164 if (m->channel_map.channels > cp->max_sink_channels)
165 cp->max_sink_channels = m->channel_map.channels;
166 }
167 }
168
169 if (ap->input_mappings) {
170 cp->n_sources = pa_idxset_size(ap->input_mappings);
171
172 PA_IDXSET_FOREACH(m, ap->input_mappings, idx) {
173 if (u->use_ucm)
174 pa_alsa_ucm_add_ports_combination(NULL, &m->ucm_context, FALSE, ports, cp, u->core);
175 else
176 pa_alsa_path_set_add_ports(m->input_path_set, cp, ports, NULL, u->core);
177 if (m->channel_map.channels > cp->max_source_channels)
178 cp->max_source_channels = m->channel_map.channels;
179 }
180 }
181
182 d = PA_CARD_PROFILE_DATA(cp);
183 d->profile = ap;
184
185 pa_hashmap_put(h, cp->name, cp);
186 }
187 }
188
189 static void add_disabled_profile(pa_hashmap *profiles) {
190 pa_card_profile *p;
191 struct profile_data *d;
192
193 p = pa_card_profile_new("off", _("Off"), sizeof(struct profile_data));
194
195 d = PA_CARD_PROFILE_DATA(p);
196 d->profile = NULL;
197
198 pa_hashmap_put(profiles, p->name, p);
199 }
200
201 static int card_set_profile(pa_card *c, pa_card_profile *new_profile) {
202 struct userdata *u;
203 struct profile_data *nd, *od;
204 uint32_t idx;
205 pa_alsa_mapping *am;
206 pa_queue *sink_inputs = NULL, *source_outputs = NULL;
207
208 pa_assert(c);
209 pa_assert(new_profile);
210 pa_assert_se(u = c->userdata);
211
212 nd = PA_CARD_PROFILE_DATA(new_profile);
213 od = PA_CARD_PROFILE_DATA(c->active_profile);
214
215 if (od->profile && od->profile->output_mappings)
216 PA_IDXSET_FOREACH(am, od->profile->output_mappings, idx) {
217 if (!am->sink)
218 continue;
219
220 if (nd->profile &&
221 nd->profile->output_mappings &&
222 pa_idxset_get_by_data(nd->profile->output_mappings, am, NULL))
223 continue;
224
225 sink_inputs = pa_sink_move_all_start(am->sink, sink_inputs);
226 pa_alsa_sink_free(am->sink);
227 am->sink = NULL;
228 }
229
230 if (od->profile && od->profile->input_mappings)
231 PA_IDXSET_FOREACH(am, od->profile->input_mappings, idx) {
232 if (!am->source)
233 continue;
234
235 if (nd->profile &&
236 nd->profile->input_mappings &&
237 pa_idxset_get_by_data(nd->profile->input_mappings, am, NULL))
238 continue;
239
240 source_outputs = pa_source_move_all_start(am->source, source_outputs);
241 pa_alsa_source_free(am->source);
242 am->source = NULL;
243 }
244
245 /* if UCM is available for this card then update the verb */
246 if (u->use_ucm) {
247 if (pa_alsa_ucm_set_profile(&u->ucm, nd->profile ? nd->profile->name : NULL,
248 od->profile ? od->profile->name : NULL) < 0)
249 return -1;
250 }
251
252 if (nd->profile && nd->profile->output_mappings)
253 PA_IDXSET_FOREACH(am, nd->profile->output_mappings, idx) {
254
255 if (!am->sink)
256 am->sink = pa_alsa_sink_new(c->module, u->modargs, __FILE__, c, am);
257
258 if (sink_inputs && am->sink) {
259 pa_sink_move_all_finish(am->sink, sink_inputs, FALSE);
260 sink_inputs = NULL;
261 }
262 }
263
264 if (nd->profile && nd->profile->input_mappings)
265 PA_IDXSET_FOREACH(am, nd->profile->input_mappings, idx) {
266
267 if (!am->source)
268 am->source = pa_alsa_source_new(c->module, u->modargs, __FILE__, c, am);
269
270 if (source_outputs && am->source) {
271 pa_source_move_all_finish(am->source, source_outputs, FALSE);
272 source_outputs = NULL;
273 }
274 }
275
276 if (sink_inputs)
277 pa_sink_move_all_fail(sink_inputs);
278
279 if (source_outputs)
280 pa_source_move_all_fail(source_outputs);
281
282 return 0;
283 }
284
285 static void init_profile(struct userdata *u) {
286 uint32_t idx;
287 pa_alsa_mapping *am;
288 struct profile_data *d;
289 pa_alsa_ucm_config *ucm = &u->ucm;
290
291 pa_assert(u);
292
293 d = PA_CARD_PROFILE_DATA(u->card->active_profile);
294
295 if (d->profile && u->use_ucm) {
296 /* Set initial verb */
297 if (pa_alsa_ucm_set_profile(ucm, d->profile->name, NULL) < 0) {
298 pa_log("Failed to set ucm profile %s", d->profile->name);
299 return;
300 }
301 }
302
303 if (d->profile && d->profile->output_mappings)
304 PA_IDXSET_FOREACH(am, d->profile->output_mappings, idx)
305 am->sink = pa_alsa_sink_new(u->module, u->modargs, __FILE__, u->card, am);
306
307 if (d->profile && d->profile->input_mappings)
308 PA_IDXSET_FOREACH(am, d->profile->input_mappings, idx)
309 am->source = pa_alsa_source_new(u->module, u->modargs, __FILE__, u->card, am);
310 }
311
312 static void report_port_state(pa_device_port *p, struct userdata *u)
313 {
314 void *state;
315 pa_alsa_jack *jack;
316 pa_available_t pa = PA_AVAILABLE_UNKNOWN;
317 pa_device_port *port;
318
319 PA_HASHMAP_FOREACH(jack, u->jacks, state) {
320 pa_available_t cpa;
321
322 if (u->use_ucm)
323 port = pa_hashmap_get(u->card->ports, jack->name);
324 else {
325 if (jack->path)
326 port = jack->path->port;
327 else
328 continue;
329 }
330
331 if (p != port)
332 continue;
333
334 cpa = jack->plugged_in ? jack->state_plugged : jack->state_unplugged;
335
336 /* "Yes" and "no" trumphs "unknown" if we have more than one jack */
337 if (cpa == PA_AVAILABLE_UNKNOWN)
338 continue;
339
340 if ((cpa == PA_AVAILABLE_NO && pa == PA_AVAILABLE_YES) ||
341 (pa == PA_AVAILABLE_NO && cpa == PA_AVAILABLE_YES))
342 pa_log_warn("Availability of port '%s' is inconsistent!", p->name);
343 else
344 pa = cpa;
345 }
346
347 pa_device_port_set_available(p, pa);
348 }
349
350 static int report_jack_state(snd_hctl_elem_t *elem, unsigned int mask)
351 {
352 struct userdata *u = snd_hctl_elem_get_callback_private(elem);
353 snd_ctl_elem_value_t *elem_value;
354 pa_bool_t plugged_in;
355 void *state;
356 pa_alsa_jack *jack;
357 pa_device_port *port;
358
359 pa_assert(u);
360
361 if (mask == SND_CTL_EVENT_MASK_REMOVE)
362 return 0;
363
364 snd_ctl_elem_value_alloca(&elem_value);
365 if (snd_hctl_elem_read(elem, elem_value) < 0) {
366 pa_log_warn("Failed to read jack detection from '%s'", pa_strnull(snd_hctl_elem_get_name(elem)));
367 return 0;
368 }
369
370 plugged_in = !!snd_ctl_elem_value_get_boolean(elem_value, 0);
371
372 pa_log_debug("Jack '%s' is now %s", pa_strnull(snd_hctl_elem_get_name(elem)), plugged_in ? "plugged in" : "unplugged");
373
374 PA_HASHMAP_FOREACH(jack, u->jacks, state)
375 if (jack->hctl_elem == elem) {
376 jack->plugged_in = plugged_in;
377 if (u->use_ucm) {
378 pa_assert(u->card->ports);
379 port = pa_hashmap_get(u->card->ports, jack->name);
380 pa_assert(port);
381 }
382 else {
383 pa_assert(jack->path && jack->path->port);
384 port = jack->path->port;
385 }
386 report_port_state(port, u);
387 }
388 return 0;
389 }
390
391 static pa_device_port* find_port_with_eld_device(pa_hashmap *ports, int device) {
392 void *state;
393 pa_device_port *p;
394
395 PA_HASHMAP_FOREACH(p, ports, state) {
396 pa_alsa_port_data *data = PA_DEVICE_PORT_DATA(p);
397 pa_assert(data->path);
398 if (device == data->path->eld_device)
399 return p;
400 }
401 return NULL;
402 }
403
404 static int hdmi_eld_changed(snd_hctl_elem_t *elem, unsigned int mask) {
405 struct userdata *u = snd_hctl_elem_get_callback_private(elem);
406 int device = snd_hctl_elem_get_device(elem);
407 const char *old_monitor_name;
408 pa_device_port *p;
409 pa_hdmi_eld eld;
410 bool changed = false;
411
412 p = find_port_with_eld_device(u->card->ports, device);
413 if (p == NULL) {
414 pa_log_error("Invalid device changed in ALSA: %d", device);
415 return 0;
416 }
417
418 if (pa_alsa_get_hdmi_eld(u->hctl_handle, device, &eld) < 0)
419 memset(&eld, 0, sizeof(eld));
420
421 old_monitor_name = pa_proplist_gets(p->proplist, PA_PROP_DEVICE_PRODUCT_NAME);
422 if (eld.monitor_name[0] == '\0') {
423 changed |= old_monitor_name != NULL;
424 pa_proplist_unset(p->proplist, PA_PROP_DEVICE_PRODUCT_NAME);
425 } else {
426 changed |= (old_monitor_name == NULL) || (strcmp(old_monitor_name, eld.monitor_name) != 0);
427 pa_proplist_sets(p->proplist, PA_PROP_DEVICE_PRODUCT_NAME, eld.monitor_name);
428 }
429
430 if (changed && mask != 0)
431 pa_subscription_post(u->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE, u->card->index);
432
433 return 0;
434 }
435
436 static void init_eld_ctls(struct userdata *u) {
437 void *state;
438 pa_device_port *port;
439
440 if (!u->hctl_handle)
441 return;
442
443 PA_HASHMAP_FOREACH(port, u->card->ports, state) {
444 pa_alsa_port_data *data = PA_DEVICE_PORT_DATA(port);
445 snd_hctl_elem_t* hctl_elem;
446 int device;
447
448 pa_assert(data->path);
449 device = data->path->eld_device;
450 if (device < 0)
451 continue;
452
453 hctl_elem = pa_alsa_find_eld_ctl(u->hctl_handle, device);
454 if (!hctl_elem) {
455 pa_log_debug("No ELD device found for port %s.", port->name);
456 continue;
457 }
458
459 snd_hctl_elem_set_callback_private(hctl_elem, u);
460 snd_hctl_elem_set_callback(hctl_elem, hdmi_eld_changed);
461 hdmi_eld_changed(hctl_elem, 0);
462 }
463 }
464
465 static void init_jacks(struct userdata *u) {
466 void *state;
467 pa_alsa_path* path;
468 pa_alsa_jack* jack;
469
470 u->jacks = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
471
472 if (u->use_ucm) {
473 PA_LLIST_FOREACH(jack, u->ucm.jacks)
474 if (jack->has_control)
475 pa_hashmap_put(u->jacks, jack, jack);
476 } else {
477 /* See if we have any jacks */
478 if (u->profile_set->output_paths)
479 PA_HASHMAP_FOREACH(path, u->profile_set->output_paths, state)
480 PA_LLIST_FOREACH(jack, path->jacks)
481 if (jack->has_control)
482 pa_hashmap_put(u->jacks, jack, jack);
483
484 if (u->profile_set->input_paths)
485 PA_HASHMAP_FOREACH(path, u->profile_set->input_paths, state)
486 PA_LLIST_FOREACH(jack, path->jacks)
487 if (jack->has_control)
488 pa_hashmap_put(u->jacks, jack, jack);
489 }
490
491 pa_log_debug("Found %d jacks.", pa_hashmap_size(u->jacks));
492
493 if (pa_hashmap_size(u->jacks) == 0)
494 return;
495
496 u->mixer_fdl = pa_alsa_fdlist_new();
497
498 u->mixer_handle = pa_alsa_open_mixer(u->alsa_card_index, NULL, &u->hctl_handle);
499 if (u->mixer_handle && pa_alsa_fdlist_set_handle(u->mixer_fdl, NULL, u->hctl_handle, u->core->mainloop) >= 0) {
500 PA_HASHMAP_FOREACH(jack, u->jacks, state) {
501 jack->hctl_elem = pa_alsa_find_jack(u->hctl_handle, jack->alsa_name);
502 if (!jack->hctl_elem) {
503 pa_log_warn("Jack '%s' seems to have disappeared.", jack->alsa_name);
504 jack->has_control = FALSE;
505 continue;
506 }
507 snd_hctl_elem_set_callback_private(jack->hctl_elem, u);
508 snd_hctl_elem_set_callback(jack->hctl_elem, report_jack_state);
509 report_jack_state(jack->hctl_elem, 0);
510 }
511
512 } else
513 pa_log("Failed to open hctl/mixer for jack detection");
514
515 }
516
517 static void set_card_name(pa_card_new_data *data, pa_modargs *ma, const char *device_id) {
518 char *t;
519 const char *n;
520
521 pa_assert(data);
522 pa_assert(ma);
523 pa_assert(device_id);
524
525 if ((n = pa_modargs_get_value(ma, "card_name", NULL))) {
526 pa_card_new_data_set_name(data, n);
527 data->namereg_fail = TRUE;
528 return;
529 }
530
531 if ((n = pa_modargs_get_value(ma, "name", NULL)))
532 data->namereg_fail = TRUE;
533 else {
534 n = device_id;
535 data->namereg_fail = FALSE;
536 }
537
538 t = pa_sprintf_malloc("alsa_card.%s", n);
539 pa_card_new_data_set_name(data, t);
540 pa_xfree(t);
541 }
542
543 static pa_hook_result_t sink_input_put_hook_callback(pa_core *c, pa_sink_input *sink_input, struct userdata *u) {
544 const char *role;
545 pa_sink *sink = sink_input->sink;
546
547 pa_assert(sink);
548
549 role = pa_proplist_gets(sink_input->proplist, PA_PROP_MEDIA_ROLE);
550
551 /* new sink input linked to sink of this card */
552 if (role && sink->card == u->card)
553 pa_alsa_ucm_roled_stream_begin(&u->ucm, role, PA_DIRECTION_OUTPUT);
554
555 return PA_HOOK_OK;
556 }
557
558 static pa_hook_result_t source_output_put_hook_callback(pa_core *c, pa_source_output *source_output, struct userdata *u) {
559 const char *role;
560 pa_source *source = source_output->source;
561
562 pa_assert(source);
563
564 role = pa_proplist_gets(source_output->proplist, PA_PROP_MEDIA_ROLE);
565
566 /* new source output linked to source of this card */
567 if (role && source->card == u->card)
568 pa_alsa_ucm_roled_stream_begin(&u->ucm, role, PA_DIRECTION_INPUT);
569
570 return PA_HOOK_OK;
571 }
572
573 static pa_hook_result_t sink_input_unlink_hook_callback(pa_core *c, pa_sink_input *sink_input, struct userdata *u) {
574 const char *role;
575 pa_sink *sink = sink_input->sink;
576
577 pa_assert(sink);
578
579 role = pa_proplist_gets(sink_input->proplist, PA_PROP_MEDIA_ROLE);
580
581 /* new sink input unlinked from sink of this card */
582 if (role && sink->card == u->card)
583 pa_alsa_ucm_roled_stream_end(&u->ucm, role, PA_DIRECTION_OUTPUT);
584
585 return PA_HOOK_OK;
586 }
587
588 static pa_hook_result_t source_output_unlink_hook_callback(pa_core *c, pa_source_output *source_output, struct userdata *u) {
589 const char *role;
590 pa_source *source = source_output->source;
591
592 pa_assert(source);
593
594 role = pa_proplist_gets(source_output->proplist, PA_PROP_MEDIA_ROLE);
595
596 /* new source output unlinked from source of this card */
597 if (role && source->card == u->card)
598 pa_alsa_ucm_roled_stream_end(&u->ucm, role, PA_DIRECTION_INPUT);
599
600 return PA_HOOK_OK;
601 }
602
603 int pa__init(pa_module *m) {
604 pa_card_new_data data;
605 pa_modargs *ma;
606 pa_bool_t ignore_dB = FALSE;
607 struct userdata *u;
608 pa_reserve_wrapper *reserve = NULL;
609 const char *description;
610 const char *profile = NULL;
611 char *fn = NULL;
612 pa_bool_t namereg_fail = FALSE;
613
614 pa_alsa_refcnt_inc();
615
616 pa_assert(m);
617
618 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
619 pa_log("Failed to parse module arguments");
620 goto fail;
621 }
622
623 if (pa_modargs_get_value_boolean(ma, "ignore_dB", &ignore_dB) < 0) {
624 pa_log("Failed to parse ignore_dB argument.");
625 goto fail;
626 }
627
628 m->userdata = u = pa_xnew0(struct userdata, 1);
629 u->core = m->core;
630 u->module = m;
631 u->device_id = pa_xstrdup(pa_modargs_get_value(ma, "device_id", DEFAULT_DEVICE_ID));
632 u->modargs = ma;
633
634 u->use_ucm = TRUE;
635 u->ucm.core = m->core;
636
637 if ((u->alsa_card_index = snd_card_get_index(u->device_id)) < 0) {
638 pa_log("Card '%s' doesn't exist: %s", u->device_id, pa_alsa_strerror(u->alsa_card_index));
639 goto fail;
640 }
641
642 if (!pa_in_system_mode()) {
643 char *rname;
644
645 if ((rname = pa_alsa_get_reserve_name(u->device_id))) {
646 reserve = pa_reserve_wrapper_get(m->core, rname);
647 pa_xfree(rname);
648
649 if (!reserve)
650 goto fail;
651 }
652 }
653
654 pa_modargs_get_value_boolean(ma, "use_ucm", &u->use_ucm);
655 if (u->use_ucm && !pa_alsa_ucm_query_profiles(&u->ucm, u->alsa_card_index)) {
656 pa_log_info("Found UCM profiles");
657
658 u->profile_set = pa_alsa_ucm_add_profile_set(&u->ucm, &u->core->default_channel_map);
659
660 /* hook start of sink input/source output to enable modifiers */
661 /* A little bit later than module-role-cork */
662 u->sink_input_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_PUT], PA_HOOK_LATE+10,
663 (pa_hook_cb_t) sink_input_put_hook_callback, u);
664 u->source_output_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PUT], PA_HOOK_LATE+10,
665 (pa_hook_cb_t) source_output_put_hook_callback, u);
666
667 /* hook end of sink input/source output to disable modifiers */
668 /* A little bit later than module-role-cork */
669 u->sink_input_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_UNLINK], PA_HOOK_LATE+10,
670 (pa_hook_cb_t) sink_input_unlink_hook_callback, u);
671 u->source_output_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK], PA_HOOK_LATE+10,
672 (pa_hook_cb_t) source_output_unlink_hook_callback, u);
673 }
674 else {
675 u->use_ucm = FALSE;
676 #ifdef HAVE_UDEV
677 fn = pa_udev_get_property(u->alsa_card_index, "PULSE_PROFILE_SET");
678 #endif
679
680 if (pa_modargs_get_value(ma, "profile_set", NULL)) {
681 pa_xfree(fn);
682 fn = pa_xstrdup(pa_modargs_get_value(ma, "profile_set", NULL));
683 }
684
685 u->profile_set = pa_alsa_profile_set_new(fn, &u->core->default_channel_map);
686 pa_xfree(fn);
687 }
688
689 u->profile_set->ignore_dB = ignore_dB;
690
691 if (!u->profile_set)
692 goto fail;
693
694 pa_alsa_profile_set_probe(u->profile_set, u->device_id, &m->core->default_sample_spec, m->core->default_n_fragments, m->core->default_fragment_size_msec);
695 pa_alsa_profile_set_dump(u->profile_set);
696
697 pa_card_new_data_init(&data);
698 data.driver = __FILE__;
699 data.module = m;
700
701 pa_alsa_init_proplist_card(m->core, data.proplist, u->alsa_card_index);
702
703 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_id);
704 pa_alsa_init_description(data.proplist);
705 set_card_name(&data, ma, u->device_id);
706
707 /* We need to give pa_modargs_get_value_boolean() a pointer to a local
708 * variable instead of using &data.namereg_fail directly, because
709 * data.namereg_fail is a bitfield and taking the address of a bitfield
710 * variable is impossible. */
711 namereg_fail = data.namereg_fail;
712 if (pa_modargs_get_value_boolean(ma, "namereg_fail", &namereg_fail) < 0) {
713 pa_log("Failed to parse namereg_fail argument.");
714 pa_card_new_data_done(&data);
715 goto fail;
716 }
717 data.namereg_fail = namereg_fail;
718
719 if (reserve)
720 if ((description = pa_proplist_gets(data.proplist, PA_PROP_DEVICE_DESCRIPTION)))
721 pa_reserve_wrapper_set_application_device_name(reserve, description);
722
723 add_profiles(u, data.profiles, data.ports);
724
725 if (pa_hashmap_isempty(data.profiles)) {
726 pa_log("Failed to find a working profile.");
727 pa_card_new_data_done(&data);
728 goto fail;
729 }
730
731 add_disabled_profile(data.profiles);
732
733 if (pa_modargs_get_proplist(ma, "card_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
734 pa_log("Invalid properties");
735 pa_card_new_data_done(&data);
736 goto fail;
737 }
738
739 if ((profile = pa_modargs_get_value(ma, "profile", NULL)))
740 pa_card_new_data_set_profile(&data, profile);
741
742 u->card = pa_card_new(m->core, &data);
743 pa_card_new_data_done(&data);
744
745 if (!u->card)
746 goto fail;
747
748 u->card->userdata = u;
749 u->card->set_profile = card_set_profile;
750
751 init_jacks(u);
752 init_profile(u);
753 init_eld_ctls(u);
754
755 if (reserve)
756 pa_reserve_wrapper_unref(reserve);
757
758 if (!pa_hashmap_isempty(u->profile_set->decibel_fixes))
759 pa_log_warn("Card %s uses decibel fixes (i.e. overrides the decibel information for some alsa volume elements). "
760 "Please note that this feature is meant just as a help for figuring out the correct decibel values. "
761 "PulseAudio is not the correct place to maintain the decibel mappings! The fixed decibel values "
762 "should be sent to ALSA developers so that they can fix the driver. If it turns out that this feature "
763 "is abused (i.e. fixes are not pushed to ALSA), the decibel fix feature may be removed in some future "
764 "PulseAudio version.", u->card->name);
765
766 return 0;
767
768 fail:
769 if (reserve)
770 pa_reserve_wrapper_unref(reserve);
771
772 pa__done(m);
773
774 return -1;
775 }
776
777 int pa__get_n_used(pa_module *m) {
778 struct userdata *u;
779 int n = 0;
780 uint32_t idx;
781 pa_sink *sink;
782 pa_source *source;
783
784 pa_assert(m);
785 pa_assert_se(u = m->userdata);
786 pa_assert(u->card);
787
788 PA_IDXSET_FOREACH(sink, u->card->sinks, idx)
789 n += pa_sink_linked_by(sink);
790
791 PA_IDXSET_FOREACH(source, u->card->sources, idx)
792 n += pa_source_linked_by(source);
793
794 return n;
795 }
796
797 void pa__done(pa_module*m) {
798 struct userdata *u;
799
800 pa_assert(m);
801
802 if (!(u = m->userdata))
803 goto finish;
804
805 if (u->sink_input_put_hook_slot)
806 pa_hook_slot_free(u->sink_input_put_hook_slot);
807
808 if (u->sink_input_unlink_hook_slot)
809 pa_hook_slot_free(u->sink_input_unlink_hook_slot);
810
811 if (u->source_output_put_hook_slot)
812 pa_hook_slot_free(u->source_output_put_hook_slot);
813
814 if (u->source_output_unlink_hook_slot)
815 pa_hook_slot_free(u->source_output_unlink_hook_slot);
816
817 if (u->mixer_fdl)
818 pa_alsa_fdlist_free(u->mixer_fdl);
819 if (u->mixer_handle)
820 snd_mixer_close(u->mixer_handle);
821 if (u->jacks)
822 pa_hashmap_free(u->jacks, NULL);
823
824 if (u->card && u->card->sinks)
825 pa_idxset_remove_all(u->card->sinks, (pa_free_cb_t) pa_alsa_sink_free);
826
827 if (u->card && u->card->sources)
828 pa_idxset_remove_all(u->card->sources, (pa_free_cb_t) pa_alsa_source_free);
829
830 if (u->card)
831 pa_card_free(u->card);
832
833 if (u->modargs)
834 pa_modargs_free(u->modargs);
835
836 if (u->profile_set)
837 pa_alsa_profile_set_free(u->profile_set);
838
839 pa_alsa_ucm_free(&u->ucm);
840
841 pa_xfree(u->device_id);
842 pa_xfree(u);
843
844 finish:
845 pa_alsa_refcnt_dec();
846 }