]> code.delx.au - pulseaudio/blob - src/modules/alsa/module-alsa-card.c
alsa: Add UCM jack detection
[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
129 struct profile_data {
130 pa_alsa_profile *profile;
131 };
132
133 static void add_profiles(struct userdata *u, pa_hashmap *h, pa_hashmap *ports) {
134 pa_alsa_profile *ap;
135 void *state;
136
137 pa_assert(u);
138 pa_assert(h);
139
140 PA_HASHMAP_FOREACH(ap, u->profile_set->profiles, state) {
141 struct profile_data *d;
142 pa_card_profile *cp;
143 pa_alsa_mapping *m;
144 uint32_t idx;
145
146 cp = pa_card_profile_new(ap->name, ap->description, sizeof(struct profile_data));
147 cp->priority = ap->priority;
148
149 if (ap->output_mappings) {
150 cp->n_sinks = pa_idxset_size(ap->output_mappings);
151
152 PA_IDXSET_FOREACH(m, ap->output_mappings, idx) {
153 if (u->use_ucm)
154 pa_alsa_ucm_add_ports_combination(NULL, &m->ucm_context, TRUE, ports, cp, u->core);
155 else
156 pa_alsa_path_set_add_ports(m->output_path_set, cp, ports, NULL, u->core);
157 if (m->channel_map.channels > cp->max_sink_channels)
158 cp->max_sink_channels = m->channel_map.channels;
159 }
160 }
161
162 if (ap->input_mappings) {
163 cp->n_sources = pa_idxset_size(ap->input_mappings);
164
165 PA_IDXSET_FOREACH(m, ap->input_mappings, idx) {
166 if (u->use_ucm)
167 pa_alsa_ucm_add_ports_combination(NULL, &m->ucm_context, FALSE, ports, cp, u->core);
168 else
169 pa_alsa_path_set_add_ports(m->input_path_set, cp, ports, NULL, u->core);
170 if (m->channel_map.channels > cp->max_source_channels)
171 cp->max_source_channels = m->channel_map.channels;
172 }
173 }
174
175 d = PA_CARD_PROFILE_DATA(cp);
176 d->profile = ap;
177
178 pa_hashmap_put(h, cp->name, cp);
179 }
180 }
181
182 static void add_disabled_profile(pa_hashmap *profiles) {
183 pa_card_profile *p;
184 struct profile_data *d;
185
186 p = pa_card_profile_new("off", _("Off"), sizeof(struct profile_data));
187
188 d = PA_CARD_PROFILE_DATA(p);
189 d->profile = NULL;
190
191 pa_hashmap_put(profiles, p->name, p);
192 }
193
194 static int card_set_profile(pa_card *c, pa_card_profile *new_profile) {
195 struct userdata *u;
196 struct profile_data *nd, *od;
197 uint32_t idx;
198 pa_alsa_mapping *am;
199 pa_queue *sink_inputs = NULL, *source_outputs = NULL;
200
201 pa_assert(c);
202 pa_assert(new_profile);
203 pa_assert_se(u = c->userdata);
204
205 nd = PA_CARD_PROFILE_DATA(new_profile);
206 od = PA_CARD_PROFILE_DATA(c->active_profile);
207
208 if (od->profile && od->profile->output_mappings)
209 PA_IDXSET_FOREACH(am, od->profile->output_mappings, idx) {
210 if (!am->sink)
211 continue;
212
213 if (nd->profile &&
214 nd->profile->output_mappings &&
215 pa_idxset_get_by_data(nd->profile->output_mappings, am, NULL))
216 continue;
217
218 sink_inputs = pa_sink_move_all_start(am->sink, sink_inputs);
219 pa_alsa_sink_free(am->sink);
220 am->sink = NULL;
221 }
222
223 if (od->profile && od->profile->input_mappings)
224 PA_IDXSET_FOREACH(am, od->profile->input_mappings, idx) {
225 if (!am->source)
226 continue;
227
228 if (nd->profile &&
229 nd->profile->input_mappings &&
230 pa_idxset_get_by_data(nd->profile->input_mappings, am, NULL))
231 continue;
232
233 source_outputs = pa_source_move_all_start(am->source, source_outputs);
234 pa_alsa_source_free(am->source);
235 am->source = NULL;
236 }
237
238 /* if UCM is available for this card then update the verb */
239 if (u->use_ucm) {
240 if (pa_alsa_ucm_set_profile(&u->ucm, nd->profile ? nd->profile->name : NULL,
241 od->profile ? od->profile->name : NULL) < 0)
242 return -1;
243 }
244
245 if (nd->profile && nd->profile->output_mappings)
246 PA_IDXSET_FOREACH(am, nd->profile->output_mappings, idx) {
247
248 if (!am->sink)
249 am->sink = pa_alsa_sink_new(c->module, u->modargs, __FILE__, c, am);
250
251 if (sink_inputs && am->sink) {
252 pa_sink_move_all_finish(am->sink, sink_inputs, FALSE);
253 sink_inputs = NULL;
254 }
255 }
256
257 if (nd->profile && nd->profile->input_mappings)
258 PA_IDXSET_FOREACH(am, nd->profile->input_mappings, idx) {
259
260 if (!am->source)
261 am->source = pa_alsa_source_new(c->module, u->modargs, __FILE__, c, am);
262
263 if (source_outputs && am->source) {
264 pa_source_move_all_finish(am->source, source_outputs, FALSE);
265 source_outputs = NULL;
266 }
267 }
268
269 if (sink_inputs)
270 pa_sink_move_all_fail(sink_inputs);
271
272 if (source_outputs)
273 pa_source_move_all_fail(source_outputs);
274
275 return 0;
276 }
277
278 static void init_profile(struct userdata *u) {
279 uint32_t idx;
280 pa_alsa_mapping *am;
281 struct profile_data *d;
282 pa_alsa_ucm_config *ucm = &u->ucm;
283
284 pa_assert(u);
285
286 d = PA_CARD_PROFILE_DATA(u->card->active_profile);
287
288 if (d->profile && u->use_ucm) {
289 /* Set initial verb */
290 if (pa_alsa_ucm_set_profile(ucm, d->profile->name, NULL) < 0) {
291 pa_log("Failed to set ucm profile %s", d->profile->name);
292 return;
293 }
294 }
295
296 if (d->profile && d->profile->output_mappings)
297 PA_IDXSET_FOREACH(am, d->profile->output_mappings, idx)
298 am->sink = pa_alsa_sink_new(u->module, u->modargs, __FILE__, u->card, am);
299
300 if (d->profile && d->profile->input_mappings)
301 PA_IDXSET_FOREACH(am, d->profile->input_mappings, idx)
302 am->source = pa_alsa_source_new(u->module, u->modargs, __FILE__, u->card, am);
303 }
304
305 static void report_port_state(pa_device_port *p, struct userdata *u)
306 {
307 void *state;
308 pa_alsa_jack *jack;
309 pa_port_available_t pa = PA_PORT_AVAILABLE_UNKNOWN;
310 pa_device_port *port;
311
312 PA_HASHMAP_FOREACH(jack, u->jacks, state) {
313 pa_port_available_t cpa;
314
315 if (u->use_ucm)
316 port = pa_hashmap_get(u->card->ports, jack->name);
317 else {
318 if (jack->path)
319 port = jack->path->port;
320 else
321 continue;
322 }
323
324 if (p != port)
325 continue;
326
327 cpa = jack->plugged_in ? jack->state_plugged : jack->state_unplugged;
328
329 /* "Yes" and "no" trumphs "unknown" if we have more than one jack */
330 if (cpa == PA_PORT_AVAILABLE_UNKNOWN)
331 continue;
332
333 if ((cpa == PA_PORT_AVAILABLE_NO && pa == PA_PORT_AVAILABLE_YES) ||
334 (pa == PA_PORT_AVAILABLE_NO && cpa == PA_PORT_AVAILABLE_YES))
335 pa_log_warn("Availability of port '%s' is inconsistent!", p->name);
336 else
337 pa = cpa;
338 }
339
340 pa_device_port_set_available(p, pa);
341 }
342
343 static int report_jack_state(snd_hctl_elem_t *elem, unsigned int mask)
344 {
345 struct userdata *u = snd_hctl_elem_get_callback_private(elem);
346 snd_ctl_elem_value_t *elem_value;
347 pa_bool_t plugged_in;
348 void *state;
349 pa_alsa_jack *jack;
350 pa_device_port *port;
351
352 pa_assert(u);
353
354 if (mask == SND_CTL_EVENT_MASK_REMOVE)
355 return 0;
356
357 snd_ctl_elem_value_alloca(&elem_value);
358 if (snd_hctl_elem_read(elem, elem_value) < 0) {
359 pa_log_warn("Failed to read jack detection from '%s'", pa_strnull(snd_hctl_elem_get_name(elem)));
360 return 0;
361 }
362
363 plugged_in = !!snd_ctl_elem_value_get_boolean(elem_value, 0);
364
365 pa_log_debug("Jack '%s' is now %s", pa_strnull(snd_hctl_elem_get_name(elem)), plugged_in ? "plugged in" : "unplugged");
366
367 PA_HASHMAP_FOREACH(jack, u->jacks, state)
368 if (jack->hctl_elem == elem) {
369 jack->plugged_in = plugged_in;
370 if (u->use_ucm) {
371 pa_assert(u->card->ports);
372 port = pa_hashmap_get(u->card->ports, jack->name);
373 pa_assert(port);
374 }
375 else {
376 pa_assert(jack->path && jack->path->port);
377 port = jack->path->port;
378 }
379 report_port_state(port, u);
380 }
381 return 0;
382 }
383
384 static void init_jacks(struct userdata *u) {
385 void *state;
386 pa_alsa_path* path;
387 pa_alsa_jack* jack;
388
389 u->jacks = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
390
391 if (u->use_ucm) {
392 PA_LLIST_FOREACH(jack, u->ucm.jacks)
393 if (jack->has_control)
394 pa_hashmap_put(u->jacks, jack, jack);
395 } else {
396 /* See if we have any jacks */
397 if (u->profile_set->output_paths)
398 PA_HASHMAP_FOREACH(path, u->profile_set->output_paths, state)
399 PA_LLIST_FOREACH(jack, path->jacks)
400 if (jack->has_control)
401 pa_hashmap_put(u->jacks, jack, jack);
402
403 if (u->profile_set->input_paths)
404 PA_HASHMAP_FOREACH(path, u->profile_set->input_paths, state)
405 PA_LLIST_FOREACH(jack, path->jacks)
406 if (jack->has_control)
407 pa_hashmap_put(u->jacks, jack, jack);
408 }
409
410 pa_log_debug("Found %d jacks.", pa_hashmap_size(u->jacks));
411
412 if (pa_hashmap_size(u->jacks) == 0)
413 return;
414
415 u->mixer_fdl = pa_alsa_fdlist_new();
416
417 u->mixer_handle = pa_alsa_open_mixer(u->alsa_card_index, NULL, &u->hctl_handle);
418 if (u->mixer_handle && pa_alsa_fdlist_set_handle(u->mixer_fdl, NULL, u->hctl_handle, u->core->mainloop) >= 0) {
419 PA_HASHMAP_FOREACH(jack, u->jacks, state) {
420 jack->hctl_elem = pa_alsa_find_jack(u->hctl_handle, jack->alsa_name);
421 if (!jack->hctl_elem) {
422 pa_log_warn("Jack '%s' seems to have disappeared.", jack->alsa_name);
423 jack->has_control = FALSE;
424 continue;
425 }
426 snd_hctl_elem_set_callback_private(jack->hctl_elem, u);
427 snd_hctl_elem_set_callback(jack->hctl_elem, report_jack_state);
428 report_jack_state(jack->hctl_elem, 0);
429 }
430
431 } else
432 pa_log("Failed to open hctl/mixer for jack detection");
433
434 }
435
436 static void set_card_name(pa_card_new_data *data, pa_modargs *ma, const char *device_id) {
437 char *t;
438 const char *n;
439
440 pa_assert(data);
441 pa_assert(ma);
442 pa_assert(device_id);
443
444 if ((n = pa_modargs_get_value(ma, "card_name", NULL))) {
445 pa_card_new_data_set_name(data, n);
446 data->namereg_fail = TRUE;
447 return;
448 }
449
450 if ((n = pa_modargs_get_value(ma, "name", NULL)))
451 data->namereg_fail = TRUE;
452 else {
453 n = device_id;
454 data->namereg_fail = FALSE;
455 }
456
457 t = pa_sprintf_malloc("alsa_card.%s", n);
458 pa_card_new_data_set_name(data, t);
459 pa_xfree(t);
460 }
461
462 int pa__init(pa_module *m) {
463 pa_card_new_data data;
464 pa_modargs *ma;
465 pa_bool_t ignore_dB = FALSE;
466 struct userdata *u;
467 pa_reserve_wrapper *reserve = NULL;
468 const char *description;
469 const char *profile = NULL;
470 char *fn = NULL;
471 pa_bool_t namereg_fail = FALSE;
472
473 pa_alsa_refcnt_inc();
474
475 pa_assert(m);
476
477 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
478 pa_log("Failed to parse module arguments");
479 goto fail;
480 }
481
482 if (pa_modargs_get_value_boolean(ma, "ignore_dB", &ignore_dB) < 0) {
483 pa_log("Failed to parse ignore_dB argument.");
484 goto fail;
485 }
486
487 m->userdata = u = pa_xnew0(struct userdata, 1);
488 u->core = m->core;
489 u->module = m;
490 u->device_id = pa_xstrdup(pa_modargs_get_value(ma, "device_id", DEFAULT_DEVICE_ID));
491 u->modargs = ma;
492
493 u->use_ucm = TRUE;
494 u->ucm.core = m->core;
495
496 if ((u->alsa_card_index = snd_card_get_index(u->device_id)) < 0) {
497 pa_log("Card '%s' doesn't exist: %s", u->device_id, pa_alsa_strerror(u->alsa_card_index));
498 goto fail;
499 }
500
501 if (!pa_in_system_mode()) {
502 char *rname;
503
504 if ((rname = pa_alsa_get_reserve_name(u->device_id))) {
505 reserve = pa_reserve_wrapper_get(m->core, rname);
506 pa_xfree(rname);
507
508 if (!reserve)
509 goto fail;
510 }
511 }
512
513 pa_modargs_get_value_boolean(ma, "use_ucm", &u->use_ucm);
514 if (u->use_ucm && !pa_alsa_ucm_query_profiles(&u->ucm, u->alsa_card_index)) {
515 pa_log_info("Found UCM profiles");
516
517 u->profile_set = pa_alsa_ucm_add_profile_set(&u->ucm, &u->core->default_channel_map);
518 }
519 else {
520 u->use_ucm = FALSE;
521 #ifdef HAVE_UDEV
522 fn = pa_udev_get_property(u->alsa_card_index, "PULSE_PROFILE_SET");
523 #endif
524
525 if (pa_modargs_get_value(ma, "profile_set", NULL)) {
526 pa_xfree(fn);
527 fn = pa_xstrdup(pa_modargs_get_value(ma, "profile_set", NULL));
528 }
529
530 u->profile_set = pa_alsa_profile_set_new(fn, &u->core->default_channel_map);
531 pa_xfree(fn);
532 }
533
534 u->profile_set->ignore_dB = ignore_dB;
535
536 if (!u->profile_set)
537 goto fail;
538
539 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);
540 pa_alsa_profile_set_dump(u->profile_set);
541
542 pa_card_new_data_init(&data);
543 data.driver = __FILE__;
544 data.module = m;
545
546 pa_alsa_init_proplist_card(m->core, data.proplist, u->alsa_card_index);
547
548 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_id);
549 pa_alsa_init_description(data.proplist);
550 set_card_name(&data, ma, u->device_id);
551
552 /* We need to give pa_modargs_get_value_boolean() a pointer to a local
553 * variable instead of using &data.namereg_fail directly, because
554 * data.namereg_fail is a bitfield and taking the address of a bitfield
555 * variable is impossible. */
556 namereg_fail = data.namereg_fail;
557 if (pa_modargs_get_value_boolean(ma, "namereg_fail", &namereg_fail) < 0) {
558 pa_log("Failed to parse namereg_fail argument.");
559 pa_card_new_data_done(&data);
560 goto fail;
561 }
562 data.namereg_fail = namereg_fail;
563
564 if (reserve)
565 if ((description = pa_proplist_gets(data.proplist, PA_PROP_DEVICE_DESCRIPTION)))
566 pa_reserve_wrapper_set_application_device_name(reserve, description);
567
568 add_profiles(u, data.profiles, data.ports);
569
570 if (pa_hashmap_isempty(data.profiles)) {
571 pa_log("Failed to find a working profile.");
572 pa_card_new_data_done(&data);
573 goto fail;
574 }
575
576 add_disabled_profile(data.profiles);
577
578 if (pa_modargs_get_proplist(ma, "card_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
579 pa_log("Invalid properties");
580 pa_card_new_data_done(&data);
581 goto fail;
582 }
583
584 if ((profile = pa_modargs_get_value(ma, "profile", NULL)))
585 pa_card_new_data_set_profile(&data, profile);
586
587 u->card = pa_card_new(m->core, &data);
588 pa_card_new_data_done(&data);
589
590 if (!u->card)
591 goto fail;
592
593 u->card->userdata = u;
594 u->card->set_profile = card_set_profile;
595
596 init_profile(u);
597 init_jacks(u);
598
599 if (reserve)
600 pa_reserve_wrapper_unref(reserve);
601
602 if (!pa_hashmap_isempty(u->profile_set->decibel_fixes))
603 pa_log_warn("Card %s uses decibel fixes (i.e. overrides the decibel information for some alsa volume elements). "
604 "Please note that this feature is meant just as a help for figuring out the correct decibel values. "
605 "PulseAudio is not the correct place to maintain the decibel mappings! The fixed decibel values "
606 "should be sent to ALSA developers so that they can fix the driver. If it turns out that this feature "
607 "is abused (i.e. fixes are not pushed to ALSA), the decibel fix feature may be removed in some future "
608 "PulseAudio version.", u->card->name);
609
610 return 0;
611
612 fail:
613 if (reserve)
614 pa_reserve_wrapper_unref(reserve);
615
616 pa__done(m);
617
618 return -1;
619 }
620
621 int pa__get_n_used(pa_module *m) {
622 struct userdata *u;
623 int n = 0;
624 uint32_t idx;
625 pa_sink *sink;
626 pa_source *source;
627
628 pa_assert(m);
629 pa_assert_se(u = m->userdata);
630 pa_assert(u->card);
631
632 PA_IDXSET_FOREACH(sink, u->card->sinks, idx)
633 n += pa_sink_linked_by(sink);
634
635 PA_IDXSET_FOREACH(source, u->card->sources, idx)
636 n += pa_source_linked_by(source);
637
638 return n;
639 }
640
641 void pa__done(pa_module*m) {
642 struct userdata *u;
643
644 pa_assert(m);
645
646 if (!(u = m->userdata))
647 goto finish;
648
649 if (u->mixer_fdl)
650 pa_alsa_fdlist_free(u->mixer_fdl);
651 if (u->mixer_handle)
652 snd_mixer_close(u->mixer_handle);
653 if (u->jacks)
654 pa_hashmap_free(u->jacks, NULL, NULL);
655
656 if (u->card && u->card->sinks) {
657 pa_sink *s;
658
659 while ((s = pa_idxset_steal_first(u->card->sinks, NULL)))
660 pa_alsa_sink_free(s);
661 }
662
663 if (u->card && u->card->sources) {
664 pa_source *s;
665
666 while ((s = pa_idxset_steal_first(u->card->sources, NULL)))
667 pa_alsa_source_free(s);
668 }
669
670 if (u->card)
671 pa_card_free(u->card);
672
673 if (u->modargs)
674 pa_modargs_free(u->modargs);
675
676 if (u->profile_set)
677 pa_alsa_profile_set_free(u->profile_set);
678
679 pa_alsa_ucm_free(&u->ucm);
680
681 pa_xfree(u->device_id);
682 pa_xfree(u);
683
684 finish:
685 pa_alsa_refcnt_dec();
686 }