3493465ad3bcbc3de6d85f79e1f1a7cd00c34709
[profile/ivi/pulseaudio.git] / 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-sink.h"
41 #include "alsa-source.h"
42 #include "module-alsa-card-symdef.h"
43
44 PA_MODULE_AUTHOR("Lennart Poettering");
45 PA_MODULE_DESCRIPTION("ALSA Card");
46 PA_MODULE_VERSION(PACKAGE_VERSION);
47 PA_MODULE_LOAD_ONCE(FALSE);
48 PA_MODULE_USAGE(
49         "name=<name for the card/sink/source, to be prefixed> "
50         "card_name=<name for the card> "
51         "card_properties=<properties for the card> "
52         "sink_name=<name for the sink> "
53         "sink_properties=<properties for the sink> "
54         "source_name=<name for the source> "
55         "source_properties=<properties for the source> "
56         "namereg_fail=<when false attempt to synthesise new names if they are already taken> "
57         "device_id=<ALSA card index> "
58         "format=<sample format> "
59         "rate=<sample rate> "
60         "fragments=<number of fragments> "
61         "fragment_size=<fragment size> "
62         "mmap=<enable memory mapping?> "
63         "tsched=<enable system timer based scheduling mode?> "
64         "tsched_buffer_size=<buffer size when using timer based scheduling> "
65         "tsched_buffer_watermark=<lower fill watermark> "
66         "profile=<profile name> "
67         "fixed_latency_range=<disable latency range changes on underrun?> "
68         "ignore_dB=<ignore dB information from the device?> "
69         "deferred_volume=<Synchronize software and hardware volume changes to avoid momentary jumps?> "
70         "profile_set=<profile set configuration file> "
71         "paths_dir=<directory containing the path configuration files> "
72 );
73
74 static const char* const valid_modargs[] = {
75     "name",
76     "card_name",
77     "card_properties",
78     "sink_name",
79     "sink_properties",
80     "source_name",
81     "source_properties",
82     "namereg_fail",
83     "device_id",
84     "format",
85     "rate",
86     "fragments",
87     "fragment_size",
88     "mmap",
89     "tsched",
90     "tsched_buffer_size",
91     "tsched_buffer_watermark",
92     "fixed_latency_range",
93     "profile",
94     "ignore_dB",
95     "deferred_volume",
96     "profile_set",
97     "paths_dir",
98     NULL
99 };
100
101 #define DEFAULT_DEVICE_ID "0"
102
103 struct userdata {
104     pa_core *core;
105     pa_module *module;
106
107     char *device_id;
108     int alsa_card_index;
109
110     snd_mixer_t *mixer_handle;
111     snd_hctl_t *hctl_handle;
112     pa_hashmap *jacks;
113     pa_alsa_fdlist *mixer_fdl;
114
115     pa_card *card;
116
117     pa_modargs *modargs;
118
119     pa_alsa_profile_set *profile_set;
120 };
121
122 struct profile_data {
123     pa_alsa_profile *profile;
124 };
125
126 static void add_profiles(struct userdata *u, pa_hashmap *h, pa_hashmap *ports) {
127     pa_alsa_profile *ap;
128     void *state;
129
130     pa_assert(u);
131     pa_assert(h);
132
133     PA_HASHMAP_FOREACH(ap, u->profile_set->profiles, state) {
134         struct profile_data *d;
135         pa_card_profile *cp;
136         pa_alsa_mapping *m;
137         uint32_t idx;
138
139         cp = pa_card_profile_new(ap->name, ap->description, sizeof(struct profile_data));
140         cp->priority = ap->priority;
141
142         if (ap->output_mappings) {
143             cp->n_sinks = pa_idxset_size(ap->output_mappings);
144
145             PA_IDXSET_FOREACH(m, ap->output_mappings, idx) {
146                 pa_alsa_path_set_add_ports(m->output_path_set, cp, ports, NULL, u->core);
147                 if (m->channel_map.channels > cp->max_sink_channels)
148                     cp->max_sink_channels = m->channel_map.channels;
149             }
150         }
151
152         if (ap->input_mappings) {
153             cp->n_sources = pa_idxset_size(ap->input_mappings);
154
155             PA_IDXSET_FOREACH(m, ap->input_mappings, idx) {
156                 pa_alsa_path_set_add_ports(m->input_path_set, cp, ports, NULL, u->core);
157                 if (m->channel_map.channels > cp->max_source_channels)
158                     cp->max_source_channels = m->channel_map.channels;
159             }
160         }
161
162         d = PA_CARD_PROFILE_DATA(cp);
163         d->profile = ap;
164
165         pa_hashmap_put(h, cp->name, cp);
166     }
167 }
168
169 static void add_disabled_profile(pa_hashmap *profiles) {
170     pa_card_profile *p;
171     struct profile_data *d;
172
173     p = pa_card_profile_new("off", _("Off"), sizeof(struct profile_data));
174
175     d = PA_CARD_PROFILE_DATA(p);
176     d->profile = NULL;
177
178     pa_hashmap_put(profiles, p->name, p);
179 }
180
181 static int card_set_profile(pa_card *c, pa_card_profile *new_profile) {
182     struct userdata *u;
183     struct profile_data *nd, *od;
184     uint32_t idx;
185     pa_alsa_mapping *am;
186     pa_queue *sink_inputs = NULL, *source_outputs = NULL;
187
188     pa_assert(c);
189     pa_assert(new_profile);
190     pa_assert_se(u = c->userdata);
191
192     nd = PA_CARD_PROFILE_DATA(new_profile);
193     od = PA_CARD_PROFILE_DATA(c->active_profile);
194
195     if (od->profile && od->profile->output_mappings)
196         PA_IDXSET_FOREACH(am, od->profile->output_mappings, idx) {
197             if (!am->sink)
198                 continue;
199
200             if (nd->profile &&
201                 nd->profile->output_mappings &&
202                 pa_idxset_get_by_data(nd->profile->output_mappings, am, NULL))
203                 continue;
204
205             sink_inputs = pa_sink_move_all_start(am->sink, sink_inputs);
206             pa_alsa_sink_free(am->sink);
207             am->sink = NULL;
208         }
209
210     if (od->profile && od->profile->input_mappings)
211         PA_IDXSET_FOREACH(am, od->profile->input_mappings, idx) {
212             if (!am->source)
213                 continue;
214
215             if (nd->profile &&
216                 nd->profile->input_mappings &&
217                 pa_idxset_get_by_data(nd->profile->input_mappings, am, NULL))
218                 continue;
219
220             source_outputs = pa_source_move_all_start(am->source, source_outputs);
221             pa_alsa_source_free(am->source);
222             am->source = NULL;
223         }
224
225     if (nd->profile && nd->profile->output_mappings)
226         PA_IDXSET_FOREACH(am, nd->profile->output_mappings, idx) {
227
228             if (!am->sink)
229                 am->sink = pa_alsa_sink_new(c->module, u->modargs, __FILE__, c, am);
230
231             if (sink_inputs && am->sink) {
232                 pa_sink_move_all_finish(am->sink, sink_inputs, FALSE);
233                 sink_inputs = NULL;
234             }
235         }
236
237     if (nd->profile && nd->profile->input_mappings)
238         PA_IDXSET_FOREACH(am, nd->profile->input_mappings, idx) {
239
240             if (!am->source)
241                 am->source = pa_alsa_source_new(c->module, u->modargs, __FILE__, c, am);
242
243             if (source_outputs && am->source) {
244                 pa_source_move_all_finish(am->source, source_outputs, FALSE);
245                 source_outputs = NULL;
246             }
247         }
248
249     if (sink_inputs)
250         pa_sink_move_all_fail(sink_inputs);
251
252     if (source_outputs)
253         pa_source_move_all_fail(source_outputs);
254
255     return 0;
256 }
257
258 static void init_profile(struct userdata *u) {
259     uint32_t idx;
260     pa_alsa_mapping *am;
261     struct profile_data *d;
262
263     pa_assert(u);
264
265     d = PA_CARD_PROFILE_DATA(u->card->active_profile);
266
267     if (d->profile && d->profile->output_mappings)
268         PA_IDXSET_FOREACH(am, d->profile->output_mappings, idx)
269             am->sink = pa_alsa_sink_new(u->module, u->modargs, __FILE__, u->card, am);
270
271     if (d->profile && d->profile->input_mappings)
272         PA_IDXSET_FOREACH(am, d->profile->input_mappings, idx)
273             am->source = pa_alsa_source_new(u->module, u->modargs, __FILE__, u->card, am);
274 }
275
276 static void report_port_state(pa_device_port *p, struct userdata *u)
277 {
278     void *state;
279     pa_alsa_jack *jack;
280     pa_port_available_t pa = PA_PORT_AVAILABLE_UNKNOWN;
281
282     PA_HASHMAP_FOREACH(jack, u->jacks, state) {
283         pa_port_available_t cpa;
284
285         if (!jack->path)
286             continue;
287
288         if (p != jack->path->port)
289             continue;
290
291         cpa = jack->plugged_in ? jack->state_plugged : jack->state_unplugged;
292
293         /* "Yes" and "no" trumphs "unknown" if we have more than one jack */
294         if (cpa == PA_PORT_AVAILABLE_UNKNOWN)
295             continue;
296
297         if ((cpa == PA_PORT_AVAILABLE_NO && pa == PA_PORT_AVAILABLE_YES) ||
298             (pa == PA_PORT_AVAILABLE_NO && cpa == PA_PORT_AVAILABLE_YES))
299             pa_log_warn("Availability of port '%s' is inconsistent!", p->name);
300         else
301             pa = cpa;
302     }
303
304     pa_device_port_set_available(p, pa);
305 }
306
307 static int report_jack_state(snd_hctl_elem_t *elem, unsigned int mask)
308 {
309     struct userdata *u = snd_hctl_elem_get_callback_private(elem);
310     snd_ctl_elem_value_t *elem_value;
311     pa_bool_t plugged_in;
312     void *state;
313     pa_alsa_jack *jack;
314
315     pa_assert(u);
316
317     if (mask == SND_CTL_EVENT_MASK_REMOVE)
318         return 0;
319
320     snd_ctl_elem_value_alloca(&elem_value);
321     if (snd_hctl_elem_read(elem, elem_value) < 0) {
322         pa_log_warn("Failed to read jack detection from '%s'", pa_strnull(snd_hctl_elem_get_name(elem)));
323         return 0;
324     }
325
326     plugged_in = !!snd_ctl_elem_value_get_boolean(elem_value, 0);
327
328     pa_log_debug("Jack '%s' is now %s", pa_strnull(snd_hctl_elem_get_name(elem)), plugged_in ? "plugged in" : "unplugged");
329
330     PA_HASHMAP_FOREACH(jack, u->jacks, state)
331         if (jack->hctl_elem == elem) {
332             jack->plugged_in = plugged_in;
333             pa_assert(jack->path && jack->path->port);
334             report_port_state(jack->path->port, u);
335         }
336     return 0;
337 }
338
339 static void init_jacks(struct userdata *u) {
340     void *state;
341     pa_alsa_path* path;
342     pa_alsa_jack* jack;
343
344     u->jacks = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
345
346     /* See if we have any jacks */
347     if (u->profile_set->output_paths)
348         PA_HASHMAP_FOREACH(path, u->profile_set->output_paths, state)
349             PA_LLIST_FOREACH(jack, path->jacks)
350                 if (jack->has_control)
351                     pa_hashmap_put(u->jacks, jack, jack);
352
353     if (u->profile_set->input_paths)
354         PA_HASHMAP_FOREACH(path, u->profile_set->input_paths, state)
355             PA_LLIST_FOREACH(jack, path->jacks)
356                 if (jack->has_control)
357                     pa_hashmap_put(u->jacks, jack, jack);
358
359     pa_log_debug("Found %d jacks.", pa_hashmap_size(u->jacks));
360
361     if (pa_hashmap_size(u->jacks) == 0)
362         return;
363
364     u->mixer_fdl = pa_alsa_fdlist_new();
365
366     u->mixer_handle = pa_alsa_open_mixer(u->alsa_card_index, NULL, &u->hctl_handle);
367     if (u->mixer_handle && pa_alsa_fdlist_set_handle(u->mixer_fdl, NULL, u->hctl_handle, u->core->mainloop) >= 0) {
368         PA_HASHMAP_FOREACH(jack, u->jacks, state) {
369             jack->hctl_elem = pa_alsa_find_jack(u->hctl_handle, jack->alsa_name);
370             if (!jack->hctl_elem) {
371                 pa_log_warn("Jack '%s' seems to have disappeared.", jack->alsa_name);
372                 jack->has_control = FALSE;
373                 continue;
374             }
375             snd_hctl_elem_set_callback_private(jack->hctl_elem, u);
376             snd_hctl_elem_set_callback(jack->hctl_elem, report_jack_state);
377             report_jack_state(jack->hctl_elem, 0);
378         }
379
380     } else
381         pa_log("Failed to open hctl/mixer for jack detection");
382
383 }
384
385 static void set_card_name(pa_card_new_data *data, pa_modargs *ma, const char *device_id) {
386     char *t;
387     const char *n;
388
389     pa_assert(data);
390     pa_assert(ma);
391     pa_assert(device_id);
392
393     if ((n = pa_modargs_get_value(ma, "card_name", NULL))) {
394         pa_card_new_data_set_name(data, n);
395         data->namereg_fail = TRUE;
396         return;
397     }
398
399     if ((n = pa_modargs_get_value(ma, "name", NULL)))
400         data->namereg_fail = TRUE;
401     else {
402         n = device_id;
403         data->namereg_fail = FALSE;
404     }
405
406     t = pa_sprintf_malloc("alsa_card.%s", n);
407     pa_card_new_data_set_name(data, t);
408     pa_xfree(t);
409 }
410
411 int pa__init(pa_module *m) {
412     pa_card_new_data data;
413     pa_modargs *ma;
414     pa_bool_t ignore_dB = FALSE;
415     struct userdata *u;
416     pa_reserve_wrapper *reserve = NULL;
417     const char *description;
418     const char *profile = NULL;
419     char *fn = NULL;
420     pa_bool_t namereg_fail = FALSE;
421
422     pa_alsa_refcnt_inc();
423
424     pa_assert(m);
425
426     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
427         pa_log("Failed to parse module arguments");
428         goto fail;
429     }
430
431     if (pa_modargs_get_value_boolean(ma, "ignore_dB", &ignore_dB) < 0) {
432         pa_log("Failed to parse ignore_dB argument.");
433         goto fail;
434     }
435
436     m->userdata = u = pa_xnew0(struct userdata, 1);
437     u->core = m->core;
438     u->module = m;
439     u->device_id = pa_xstrdup(pa_modargs_get_value(ma, "device_id", DEFAULT_DEVICE_ID));
440     u->modargs = ma;
441
442     if ((u->alsa_card_index = snd_card_get_index(u->device_id)) < 0) {
443         pa_log("Card '%s' doesn't exist: %s", u->device_id, pa_alsa_strerror(u->alsa_card_index));
444         goto fail;
445     }
446
447     if (!pa_in_system_mode()) {
448         char *rname;
449
450         if ((rname = pa_alsa_get_reserve_name(u->device_id))) {
451             reserve = pa_reserve_wrapper_get(m->core, rname);
452             pa_xfree(rname);
453
454             if (!reserve)
455                 goto fail;
456         }
457     }
458
459 #ifdef HAVE_UDEV
460     fn = pa_udev_get_property(u->alsa_card_index, "PULSE_PROFILE_SET");
461 #endif
462
463     if (pa_modargs_get_value(ma, "profile_set", NULL)) {
464         pa_xfree(fn);
465         fn = pa_xstrdup(pa_modargs_get_value(ma, "profile_set", NULL));
466     }
467
468     u->profile_set = pa_alsa_profile_set_new(fn, &u->core->default_channel_map);
469     pa_xfree(fn);
470
471     u->profile_set->ignore_dB = ignore_dB;
472
473     if (!u->profile_set)
474         goto fail;
475
476     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);
477     pa_alsa_profile_set_dump(u->profile_set);
478
479     pa_card_new_data_init(&data);
480     data.driver = __FILE__;
481     data.module = m;
482
483     pa_alsa_init_proplist_card(m->core, data.proplist, u->alsa_card_index);
484
485     pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_id);
486     pa_alsa_init_description(data.proplist);
487     set_card_name(&data, ma, u->device_id);
488
489     /* We need to give pa_modargs_get_value_boolean() a pointer to a local
490      * variable instead of using &data.namereg_fail directly, because
491      * data.namereg_fail is a bitfield and taking the address of a bitfield
492      * variable is impossible. */
493     namereg_fail = data.namereg_fail;
494     if (pa_modargs_get_value_boolean(ma, "namereg_fail", &namereg_fail) < 0) {
495         pa_log("Failed to parse namereg_fail argument.");
496         pa_card_new_data_done(&data);
497         goto fail;
498     }
499     data.namereg_fail = namereg_fail;
500
501     if (reserve)
502         if ((description = pa_proplist_gets(data.proplist, PA_PROP_DEVICE_DESCRIPTION)))
503             pa_reserve_wrapper_set_application_device_name(reserve, description);
504
505     data.profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
506     add_profiles(u, data.profiles, data.ports);
507
508     if (pa_hashmap_isempty(data.profiles)) {
509         pa_log("Failed to find a working profile.");
510         pa_card_new_data_done(&data);
511         goto fail;
512     }
513
514     add_disabled_profile(data.profiles);
515
516     if (pa_modargs_get_proplist(ma, "card_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
517         pa_log("Invalid properties");
518         pa_card_new_data_done(&data);
519         goto fail;
520     }
521
522     if ((profile = pa_modargs_get_value(ma, "profile", NULL)))
523         pa_card_new_data_set_profile(&data, profile);
524
525     u->card = pa_card_new(m->core, &data);
526     pa_card_new_data_done(&data);
527
528     if (!u->card)
529         goto fail;
530
531     u->card->userdata = u;
532     u->card->set_profile = card_set_profile;
533
534     init_profile(u);
535     init_jacks(u);
536
537     if (reserve)
538         pa_reserve_wrapper_unref(reserve);
539
540     if (!pa_hashmap_isempty(u->profile_set->decibel_fixes))
541         pa_log_warn("Card %s uses decibel fixes (i.e. overrides the decibel information for some alsa volume elements). "
542                     "Please note that this feature is meant just as a help for figuring out the correct decibel values. "
543                     "Pulseaudio is not the correct place to maintain the decibel mappings! The fixed decibel values "
544                     "should be sent to ALSA developers so that they can fix the driver. If it turns out that this feature "
545                     "is abused (i.e. fixes are not pushed to ALSA), the decibel fix feature may be removed in some future "
546                     "Pulseaudio version.", u->card->name);
547
548     return 0;
549
550 fail:
551     if (reserve)
552         pa_reserve_wrapper_unref(reserve);
553
554     pa__done(m);
555
556     return -1;
557 }
558
559 int pa__get_n_used(pa_module *m) {
560     struct userdata *u;
561     int n = 0;
562     uint32_t idx;
563     pa_sink *sink;
564     pa_source *source;
565
566     pa_assert(m);
567     pa_assert_se(u = m->userdata);
568     pa_assert(u->card);
569
570     PA_IDXSET_FOREACH(sink, u->card->sinks, idx)
571         n += pa_sink_linked_by(sink);
572
573     PA_IDXSET_FOREACH(source, u->card->sources, idx)
574         n += pa_source_linked_by(source);
575
576     return n;
577 }
578
579 void pa__done(pa_module*m) {
580     struct userdata *u;
581
582     pa_assert(m);
583
584     if (!(u = m->userdata))
585         goto finish;
586
587     if (u->mixer_fdl)
588         pa_alsa_fdlist_free(u->mixer_fdl);
589     if (u->mixer_handle)
590         snd_mixer_close(u->mixer_handle);
591     if (u->jacks)
592         pa_hashmap_free(u->jacks, NULL, NULL);
593
594     if (u->card && u->card->sinks) {
595         pa_sink *s;
596
597         while ((s = pa_idxset_steal_first(u->card->sinks, NULL)))
598             pa_alsa_sink_free(s);
599     }
600
601     if (u->card && u->card->sources) {
602         pa_source *s;
603
604         while ((s = pa_idxset_steal_first(u->card->sources, NULL)))
605             pa_alsa_source_free(s);
606     }
607
608     if (u->card)
609         pa_card_free(u->card);
610
611     if (u->modargs)
612         pa_modargs_free(u->modargs);
613
614     if (u->profile_set)
615         pa_alsa_profile_set_free(u->profile_set);
616
617     pa_xfree(u->device_id);
618     pa_xfree(u);
619
620 finish:
621     pa_alsa_refcnt_dec();
622 }