]> code.delx.au - pulseaudio/blob - src/modules/alsa/module-alsa-card.c
alsa: Use card description in default sink/source prefix when available
[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 bool 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 int ret = 0;
208
209 pa_assert(c);
210 pa_assert(new_profile);
211 pa_assert_se(u = c->userdata);
212
213 nd = PA_CARD_PROFILE_DATA(new_profile);
214 od = PA_CARD_PROFILE_DATA(c->active_profile);
215
216 if (od->profile && od->profile->output_mappings)
217 PA_IDXSET_FOREACH(am, od->profile->output_mappings, idx) {
218 if (!am->sink)
219 continue;
220
221 if (nd->profile &&
222 nd->profile->output_mappings &&
223 pa_idxset_get_by_data(nd->profile->output_mappings, am, NULL))
224 continue;
225
226 sink_inputs = pa_sink_move_all_start(am->sink, sink_inputs);
227 pa_alsa_sink_free(am->sink);
228 am->sink = NULL;
229 }
230
231 if (od->profile && od->profile->input_mappings)
232 PA_IDXSET_FOREACH(am, od->profile->input_mappings, idx) {
233 if (!am->source)
234 continue;
235
236 if (nd->profile &&
237 nd->profile->input_mappings &&
238 pa_idxset_get_by_data(nd->profile->input_mappings, am, NULL))
239 continue;
240
241 source_outputs = pa_source_move_all_start(am->source, source_outputs);
242 pa_alsa_source_free(am->source);
243 am->source = NULL;
244 }
245
246 /* if UCM is available for this card then update the verb */
247 if (u->use_ucm) {
248 if (pa_alsa_ucm_set_profile(&u->ucm, nd->profile ? nd->profile->name : NULL,
249 od->profile ? od->profile->name : NULL) < 0) {
250 ret = -1;
251 goto finish;
252 }
253 }
254
255 if (nd->profile && nd->profile->output_mappings)
256 PA_IDXSET_FOREACH(am, nd->profile->output_mappings, idx) {
257
258 if (!am->sink)
259 am->sink = pa_alsa_sink_new(c->module, u->modargs, __FILE__, c, am);
260
261 if (sink_inputs && am->sink) {
262 pa_sink_move_all_finish(am->sink, sink_inputs, false);
263 sink_inputs = NULL;
264 }
265 }
266
267 if (nd->profile && nd->profile->input_mappings)
268 PA_IDXSET_FOREACH(am, nd->profile->input_mappings, idx) {
269
270 if (!am->source)
271 am->source = pa_alsa_source_new(c->module, u->modargs, __FILE__, c, am);
272
273 if (source_outputs && am->source) {
274 pa_source_move_all_finish(am->source, source_outputs, false);
275 source_outputs = NULL;
276 }
277 }
278
279 finish:
280 if (sink_inputs)
281 pa_sink_move_all_fail(sink_inputs);
282
283 if (source_outputs)
284 pa_source_move_all_fail(source_outputs);
285
286 return ret;
287 }
288
289 static void init_profile(struct userdata *u) {
290 uint32_t idx;
291 pa_alsa_mapping *am;
292 struct profile_data *d;
293 pa_alsa_ucm_config *ucm = &u->ucm;
294
295 pa_assert(u);
296
297 d = PA_CARD_PROFILE_DATA(u->card->active_profile);
298
299 if (d->profile && u->use_ucm) {
300 /* Set initial verb */
301 if (pa_alsa_ucm_set_profile(ucm, d->profile->name, NULL) < 0) {
302 pa_log("Failed to set ucm profile %s", d->profile->name);
303 return;
304 }
305 }
306
307 if (d->profile && d->profile->output_mappings)
308 PA_IDXSET_FOREACH(am, d->profile->output_mappings, idx)
309 am->sink = pa_alsa_sink_new(u->module, u->modargs, __FILE__, u->card, am);
310
311 if (d->profile && d->profile->input_mappings)
312 PA_IDXSET_FOREACH(am, d->profile->input_mappings, idx)
313 am->source = pa_alsa_source_new(u->module, u->modargs, __FILE__, u->card, am);
314 }
315
316 static void report_port_state(pa_device_port *p, struct userdata *u) {
317 void *state;
318 pa_alsa_jack *jack;
319 pa_available_t pa = PA_AVAILABLE_UNKNOWN;
320 pa_device_port *port;
321
322 PA_HASHMAP_FOREACH(jack, u->jacks, state) {
323 pa_available_t cpa;
324
325 if (u->use_ucm)
326 port = pa_hashmap_get(u->card->ports, jack->name);
327 else {
328 if (jack->path)
329 port = jack->path->port;
330 else
331 continue;
332 }
333
334 if (p != port)
335 continue;
336
337 cpa = jack->plugged_in ? jack->state_plugged : jack->state_unplugged;
338
339 /* "Yes" and "no" trumphs "unknown" if we have more than one jack */
340 if (cpa == PA_AVAILABLE_UNKNOWN)
341 continue;
342
343 if ((cpa == PA_AVAILABLE_NO && pa == PA_AVAILABLE_YES) ||
344 (pa == PA_AVAILABLE_NO && cpa == PA_AVAILABLE_YES))
345 pa_log_warn("Availability of port '%s' is inconsistent!", p->name);
346 else
347 pa = cpa;
348 }
349
350 pa_device_port_set_available(p, pa);
351 }
352
353 static int report_jack_state(snd_hctl_elem_t *elem, unsigned int mask) {
354 struct userdata *u = snd_hctl_elem_get_callback_private(elem);
355 snd_ctl_elem_value_t *elem_value;
356 bool plugged_in;
357 void *state;
358 pa_alsa_jack *jack;
359 pa_device_port *port;
360
361 pa_assert(u);
362
363 if (mask == SND_CTL_EVENT_MASK_REMOVE)
364 return 0;
365
366 snd_ctl_elem_value_alloca(&elem_value);
367 if (snd_hctl_elem_read(elem, elem_value) < 0) {
368 pa_log_warn("Failed to read jack detection from '%s'", pa_strnull(snd_hctl_elem_get_name(elem)));
369 return 0;
370 }
371
372 plugged_in = !!snd_ctl_elem_value_get_boolean(elem_value, 0);
373
374 pa_log_debug("Jack '%s' is now %s", pa_strnull(snd_hctl_elem_get_name(elem)), plugged_in ? "plugged in" : "unplugged");
375
376 PA_HASHMAP_FOREACH(jack, u->jacks, state)
377 if (jack->hctl_elem == elem) {
378 jack->plugged_in = plugged_in;
379 if (u->use_ucm) {
380 pa_assert(u->card->ports);
381 port = pa_hashmap_get(u->card->ports, jack->name);
382 pa_assert(port);
383 }
384 else {
385 pa_assert(jack->path);
386 pa_assert_se(port = jack->path->port);
387 }
388 report_port_state(port, u);
389 }
390 return 0;
391 }
392
393 static pa_device_port* find_port_with_eld_device(pa_hashmap *ports, int device) {
394 void *state;
395 pa_device_port *p;
396
397 PA_HASHMAP_FOREACH(p, ports, state) {
398 pa_alsa_port_data *data = PA_DEVICE_PORT_DATA(p);
399 pa_assert(data->path);
400 if (device == data->path->eld_device)
401 return p;
402 }
403 return NULL;
404 }
405
406 static int hdmi_eld_changed(snd_hctl_elem_t *elem, unsigned int mask) {
407 struct userdata *u = snd_hctl_elem_get_callback_private(elem);
408 int device = snd_hctl_elem_get_device(elem);
409 const char *old_monitor_name;
410 pa_device_port *p;
411 pa_hdmi_eld eld;
412 bool changed = false;
413
414 if (mask == SND_CTL_EVENT_MASK_REMOVE)
415 return 0;
416
417 p = find_port_with_eld_device(u->card->ports, device);
418 if (p == NULL) {
419 pa_log_error("Invalid device changed in ALSA: %d", device);
420 return 0;
421 }
422
423 if (pa_alsa_get_hdmi_eld(u->hctl_handle, device, &eld) < 0)
424 memset(&eld, 0, sizeof(eld));
425
426 old_monitor_name = pa_proplist_gets(p->proplist, PA_PROP_DEVICE_PRODUCT_NAME);
427 if (eld.monitor_name[0] == '\0') {
428 changed |= old_monitor_name != NULL;
429 pa_proplist_unset(p->proplist, PA_PROP_DEVICE_PRODUCT_NAME);
430 } else {
431 changed |= (old_monitor_name == NULL) || (strcmp(old_monitor_name, eld.monitor_name) != 0);
432 pa_proplist_sets(p->proplist, PA_PROP_DEVICE_PRODUCT_NAME, eld.monitor_name);
433 }
434
435 if (changed && mask != 0)
436 pa_subscription_post(u->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE, u->card->index);
437
438 return 0;
439 }
440
441 static void init_eld_ctls(struct userdata *u) {
442 void *state;
443 pa_device_port *port;
444
445 if (!u->hctl_handle)
446 return;
447
448 PA_HASHMAP_FOREACH(port, u->card->ports, state) {
449 pa_alsa_port_data *data = PA_DEVICE_PORT_DATA(port);
450 snd_hctl_elem_t* hctl_elem;
451 int device;
452
453 pa_assert(data->path);
454 device = data->path->eld_device;
455 if (device < 0)
456 continue;
457
458 hctl_elem = pa_alsa_find_eld_ctl(u->hctl_handle, device);
459 if (!hctl_elem) {
460 pa_log_debug("No ELD device found for port %s.", port->name);
461 continue;
462 }
463
464 snd_hctl_elem_set_callback_private(hctl_elem, u);
465 snd_hctl_elem_set_callback(hctl_elem, hdmi_eld_changed);
466 hdmi_eld_changed(hctl_elem, 0);
467 }
468 }
469
470 static void init_jacks(struct userdata *u) {
471 void *state;
472 pa_alsa_path* path;
473 pa_alsa_jack* jack;
474
475 u->jacks = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
476
477 if (u->use_ucm) {
478 PA_LLIST_FOREACH(jack, u->ucm.jacks)
479 if (jack->has_control)
480 pa_hashmap_put(u->jacks, jack, jack);
481 } else {
482 /* See if we have any jacks */
483 if (u->profile_set->output_paths)
484 PA_HASHMAP_FOREACH(path, u->profile_set->output_paths, state)
485 PA_LLIST_FOREACH(jack, path->jacks)
486 if (jack->has_control)
487 pa_hashmap_put(u->jacks, jack, jack);
488
489 if (u->profile_set->input_paths)
490 PA_HASHMAP_FOREACH(path, u->profile_set->input_paths, state)
491 PA_LLIST_FOREACH(jack, path->jacks)
492 if (jack->has_control)
493 pa_hashmap_put(u->jacks, jack, jack);
494 }
495
496 pa_log_debug("Found %d jacks.", pa_hashmap_size(u->jacks));
497
498 if (pa_hashmap_size(u->jacks) == 0)
499 return;
500
501 u->mixer_fdl = pa_alsa_fdlist_new();
502
503 u->mixer_handle = pa_alsa_open_mixer(u->alsa_card_index, NULL, &u->hctl_handle);
504 if (u->mixer_handle && pa_alsa_fdlist_set_handle(u->mixer_fdl, NULL, u->hctl_handle, u->core->mainloop) >= 0) {
505 PA_HASHMAP_FOREACH(jack, u->jacks, state) {
506 jack->hctl_elem = pa_alsa_find_jack(u->hctl_handle, jack->alsa_name);
507 if (!jack->hctl_elem) {
508 pa_log_warn("Jack '%s' seems to have disappeared.", jack->alsa_name);
509 jack->has_control = false;
510 continue;
511 }
512 snd_hctl_elem_set_callback_private(jack->hctl_elem, u);
513 snd_hctl_elem_set_callback(jack->hctl_elem, report_jack_state);
514 report_jack_state(jack->hctl_elem, 0);
515 }
516
517 } else
518 pa_log("Failed to open hctl/mixer for jack detection");
519
520 }
521
522 static void set_card_name(pa_card_new_data *data, pa_modargs *ma, const char *device_id) {
523 char *t;
524 const char *n;
525
526 pa_assert(data);
527 pa_assert(ma);
528 pa_assert(device_id);
529
530 if ((n = pa_modargs_get_value(ma, "card_name", NULL))) {
531 pa_card_new_data_set_name(data, n);
532 data->namereg_fail = true;
533 return;
534 }
535
536 if ((n = pa_modargs_get_value(ma, "name", NULL)))
537 data->namereg_fail = true;
538 else {
539 n = device_id;
540 data->namereg_fail = false;
541 }
542
543 t = pa_sprintf_malloc("alsa_card.%s", n);
544 pa_card_new_data_set_name(data, t);
545 pa_xfree(t);
546 }
547
548 static pa_hook_result_t sink_input_put_hook_callback(pa_core *c, pa_sink_input *sink_input, struct userdata *u) {
549 const char *role;
550 pa_sink *sink = sink_input->sink;
551
552 pa_assert(sink);
553
554 role = pa_proplist_gets(sink_input->proplist, PA_PROP_MEDIA_ROLE);
555
556 /* new sink input linked to sink of this card */
557 if (role && sink->card == u->card)
558 pa_alsa_ucm_roled_stream_begin(&u->ucm, role, PA_DIRECTION_OUTPUT);
559
560 return PA_HOOK_OK;
561 }
562
563 static pa_hook_result_t source_output_put_hook_callback(pa_core *c, pa_source_output *source_output, struct userdata *u) {
564 const char *role;
565 pa_source *source = source_output->source;
566
567 pa_assert(source);
568
569 role = pa_proplist_gets(source_output->proplist, PA_PROP_MEDIA_ROLE);
570
571 /* new source output linked to source of this card */
572 if (role && source->card == u->card)
573 pa_alsa_ucm_roled_stream_begin(&u->ucm, role, PA_DIRECTION_INPUT);
574
575 return PA_HOOK_OK;
576 }
577
578 static pa_hook_result_t sink_input_unlink_hook_callback(pa_core *c, pa_sink_input *sink_input, struct userdata *u) {
579 const char *role;
580 pa_sink *sink = sink_input->sink;
581
582 pa_assert(sink);
583
584 role = pa_proplist_gets(sink_input->proplist, PA_PROP_MEDIA_ROLE);
585
586 /* new sink input unlinked from sink of this card */
587 if (role && sink->card == u->card)
588 pa_alsa_ucm_roled_stream_end(&u->ucm, role, PA_DIRECTION_OUTPUT);
589
590 return PA_HOOK_OK;
591 }
592
593 static pa_hook_result_t source_output_unlink_hook_callback(pa_core *c, pa_source_output *source_output, struct userdata *u) {
594 const char *role;
595 pa_source *source = source_output->source;
596
597 pa_assert(source);
598
599 role = pa_proplist_gets(source_output->proplist, PA_PROP_MEDIA_ROLE);
600
601 /* new source output unlinked from source of this card */
602 if (role && source->card == u->card)
603 pa_alsa_ucm_roled_stream_end(&u->ucm, role, PA_DIRECTION_INPUT);
604
605 return PA_HOOK_OK;
606 }
607
608 int pa__init(pa_module *m) {
609 pa_card_new_data data;
610 bool ignore_dB = false;
611 struct userdata *u;
612 pa_reserve_wrapper *reserve = NULL;
613 const char *description;
614 const char *profile = NULL;
615 char *fn = NULL;
616 bool namereg_fail = false;
617
618 pa_alsa_refcnt_inc();
619
620 pa_assert(m);
621
622 m->userdata = u = pa_xnew0(struct userdata, 1);
623 u->core = m->core;
624 u->module = m;
625 u->use_ucm = true;
626 u->ucm.core = m->core;
627
628 if (!(u->modargs = pa_modargs_new(m->argument, valid_modargs))) {
629 pa_log("Failed to parse module arguments.");
630 goto fail;
631 }
632
633 u->device_id = pa_xstrdup(pa_modargs_get_value(u->modargs, "device_id", DEFAULT_DEVICE_ID));
634
635 if ((u->alsa_card_index = snd_card_get_index(u->device_id)) < 0) {
636 pa_log("Card '%s' doesn't exist: %s", u->device_id, pa_alsa_strerror(u->alsa_card_index));
637 goto fail;
638 }
639
640 if (pa_modargs_get_value_boolean(u->modargs, "ignore_dB", &ignore_dB) < 0) {
641 pa_log("Failed to parse ignore_dB argument.");
642 goto fail;
643 }
644
645 if (!pa_in_system_mode()) {
646 char *rname;
647
648 if ((rname = pa_alsa_get_reserve_name(u->device_id))) {
649 reserve = pa_reserve_wrapper_get(m->core, rname);
650 pa_xfree(rname);
651
652 if (!reserve)
653 goto fail;
654 }
655 }
656
657 pa_modargs_get_value_boolean(u->modargs, "use_ucm", &u->use_ucm);
658 if (u->use_ucm && !pa_alsa_ucm_query_profiles(&u->ucm, u->alsa_card_index)) {
659 pa_log_info("Found UCM profiles");
660
661 u->profile_set = pa_alsa_ucm_add_profile_set(&u->ucm, &u->core->default_channel_map);
662
663 /* hook start of sink input/source output to enable modifiers */
664 /* A little bit later than module-role-cork */
665 u->sink_input_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_PUT], PA_HOOK_LATE+10,
666 (pa_hook_cb_t) sink_input_put_hook_callback, u);
667 u->source_output_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PUT], PA_HOOK_LATE+10,
668 (pa_hook_cb_t) source_output_put_hook_callback, u);
669
670 /* hook end of sink input/source output to disable modifiers */
671 /* A little bit later than module-role-cork */
672 u->sink_input_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_UNLINK], PA_HOOK_LATE+10,
673 (pa_hook_cb_t) sink_input_unlink_hook_callback, u);
674 u->source_output_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK], PA_HOOK_LATE+10,
675 (pa_hook_cb_t) source_output_unlink_hook_callback, u);
676 }
677 else {
678 u->use_ucm = false;
679 #ifdef HAVE_UDEV
680 fn = pa_udev_get_property(u->alsa_card_index, "PULSE_PROFILE_SET");
681 #endif
682
683 if (pa_modargs_get_value(u->modargs, "profile_set", NULL)) {
684 pa_xfree(fn);
685 fn = pa_xstrdup(pa_modargs_get_value(u->modargs, "profile_set", NULL));
686 }
687
688 u->profile_set = pa_alsa_profile_set_new(fn, &u->core->default_channel_map);
689 pa_xfree(fn);
690 }
691
692 if (!u->profile_set)
693 goto fail;
694
695 u->profile_set->ignore_dB = ignore_dB;
696
697 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);
698 pa_alsa_profile_set_dump(u->profile_set);
699
700 pa_card_new_data_init(&data);
701 data.driver = __FILE__;
702 data.module = m;
703
704 pa_alsa_init_proplist_card(m->core, data.proplist, u->alsa_card_index);
705
706 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_id);
707 pa_alsa_init_description(data.proplist, NULL);
708 set_card_name(&data, u->modargs, u->device_id);
709
710 /* We need to give pa_modargs_get_value_boolean() a pointer to a local
711 * variable instead of using &data.namereg_fail directly, because
712 * data.namereg_fail is a bitfield and taking the address of a bitfield
713 * variable is impossible. */
714 namereg_fail = data.namereg_fail;
715 if (pa_modargs_get_value_boolean(u->modargs, "namereg_fail", &namereg_fail) < 0) {
716 pa_log("Failed to parse namereg_fail argument.");
717 pa_card_new_data_done(&data);
718 goto fail;
719 }
720 data.namereg_fail = namereg_fail;
721
722 if (reserve)
723 if ((description = pa_proplist_gets(data.proplist, PA_PROP_DEVICE_DESCRIPTION)))
724 pa_reserve_wrapper_set_application_device_name(reserve, description);
725
726 add_profiles(u, data.profiles, data.ports);
727
728 if (pa_hashmap_isempty(data.profiles)) {
729 pa_log("Failed to find a working profile.");
730 pa_card_new_data_done(&data);
731 goto fail;
732 }
733
734 add_disabled_profile(data.profiles);
735
736 if (pa_modargs_get_proplist(u->modargs, "card_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
737 pa_log("Invalid properties");
738 pa_card_new_data_done(&data);
739 goto fail;
740 }
741
742 if ((profile = pa_modargs_get_value(u->modargs, "profile", NULL)))
743 pa_card_new_data_set_profile(&data, profile);
744
745 u->card = pa_card_new(m->core, &data);
746 pa_card_new_data_done(&data);
747
748 if (!u->card)
749 goto fail;
750
751 u->card->userdata = u;
752 u->card->set_profile = card_set_profile;
753
754 init_jacks(u);
755 init_profile(u);
756 init_eld_ctls(u);
757
758 if (reserve)
759 pa_reserve_wrapper_unref(reserve);
760
761 if (!pa_hashmap_isempty(u->profile_set->decibel_fixes))
762 pa_log_warn("Card %s uses decibel fixes (i.e. overrides the decibel information for some alsa volume elements). "
763 "Please note that this feature is meant just as a help for figuring out the correct decibel values. "
764 "PulseAudio is not the correct place to maintain the decibel mappings! The fixed decibel values "
765 "should be sent to ALSA developers so that they can fix the driver. If it turns out that this feature "
766 "is abused (i.e. fixes are not pushed to ALSA), the decibel fix feature may be removed in some future "
767 "PulseAudio version.", u->card->name);
768
769 return 0;
770
771 fail:
772 if (reserve)
773 pa_reserve_wrapper_unref(reserve);
774
775 pa__done(m);
776
777 return -1;
778 }
779
780 int pa__get_n_used(pa_module *m) {
781 struct userdata *u;
782 int n = 0;
783 uint32_t idx;
784 pa_sink *sink;
785 pa_source *source;
786
787 pa_assert(m);
788 pa_assert_se(u = m->userdata);
789 pa_assert(u->card);
790
791 PA_IDXSET_FOREACH(sink, u->card->sinks, idx)
792 n += pa_sink_linked_by(sink);
793
794 PA_IDXSET_FOREACH(source, u->card->sources, idx)
795 n += pa_source_linked_by(source);
796
797 return n;
798 }
799
800 void pa__done(pa_module*m) {
801 struct userdata *u;
802
803 pa_assert(m);
804
805 if (!(u = m->userdata))
806 goto finish;
807
808 if (u->sink_input_put_hook_slot)
809 pa_hook_slot_free(u->sink_input_put_hook_slot);
810
811 if (u->sink_input_unlink_hook_slot)
812 pa_hook_slot_free(u->sink_input_unlink_hook_slot);
813
814 if (u->source_output_put_hook_slot)
815 pa_hook_slot_free(u->source_output_put_hook_slot);
816
817 if (u->source_output_unlink_hook_slot)
818 pa_hook_slot_free(u->source_output_unlink_hook_slot);
819
820 if (u->mixer_fdl)
821 pa_alsa_fdlist_free(u->mixer_fdl);
822 if (u->mixer_handle)
823 snd_mixer_close(u->mixer_handle);
824 if (u->jacks)
825 pa_hashmap_free(u->jacks);
826
827 if (u->card && u->card->sinks)
828 pa_idxset_remove_all(u->card->sinks, (pa_free_cb_t) pa_alsa_sink_free);
829
830 if (u->card && u->card->sources)
831 pa_idxset_remove_all(u->card->sources, (pa_free_cb_t) pa_alsa_source_free);
832
833 if (u->card)
834 pa_card_free(u->card);
835
836 if (u->modargs)
837 pa_modargs_free(u->modargs);
838
839 if (u->profile_set)
840 pa_alsa_profile_set_free(u->profile_set);
841
842 pa_alsa_ucm_free(&u->ucm);
843
844 pa_xfree(u->device_id);
845 pa_xfree(u);
846
847 finish:
848 pa_alsa_refcnt_dec();
849 }