]> code.delx.au - pulseaudio/blob - src/modules/alsa/module-alsa-card.c
alsa: add card ports and path probe cache
[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-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
109 pa_card *card;
110
111 pa_modargs *modargs;
112
113 pa_alsa_profile_set *profile_set;
114 };
115
116 struct profile_data {
117 pa_alsa_profile *profile;
118 };
119
120 static void add_profiles(struct userdata *u, pa_hashmap *h, pa_hashmap *ports) {
121 pa_alsa_profile *ap;
122 void *state;
123
124 pa_assert(u);
125 pa_assert(h);
126
127 PA_HASHMAP_FOREACH(ap, u->profile_set->profiles, state) {
128 struct profile_data *d;
129 pa_card_profile *cp;
130 pa_alsa_mapping *m;
131 uint32_t idx;
132
133 cp = pa_card_profile_new(ap->name, ap->description, sizeof(struct profile_data));
134 cp->priority = ap->priority;
135
136 if (ap->output_mappings) {
137 cp->n_sinks = pa_idxset_size(ap->output_mappings);
138
139 PA_IDXSET_FOREACH(m, ap->output_mappings, idx) {
140 pa_alsa_path_set_add_ports(m->output_path_set, cp, ports, NULL, u->core);
141 if (m->channel_map.channels > cp->max_sink_channels)
142 cp->max_sink_channels = m->channel_map.channels;
143 }
144 }
145
146 if (ap->input_mappings) {
147 cp->n_sources = pa_idxset_size(ap->input_mappings);
148
149 PA_IDXSET_FOREACH(m, ap->input_mappings, idx) {
150 pa_alsa_path_set_add_ports(m->input_path_set, cp, ports, NULL, u->core);
151 if (m->channel_map.channels > cp->max_source_channels)
152 cp->max_source_channels = m->channel_map.channels;
153 }
154 }
155
156 d = PA_CARD_PROFILE_DATA(cp);
157 d->profile = ap;
158
159 pa_hashmap_put(h, cp->name, cp);
160 }
161 }
162
163 static void add_disabled_profile(pa_hashmap *profiles) {
164 pa_card_profile *p;
165 struct profile_data *d;
166
167 p = pa_card_profile_new("off", _("Off"), sizeof(struct profile_data));
168
169 d = PA_CARD_PROFILE_DATA(p);
170 d->profile = NULL;
171
172 pa_hashmap_put(profiles, p->name, p);
173 }
174
175 static int card_set_profile(pa_card *c, pa_card_profile *new_profile) {
176 struct userdata *u;
177 struct profile_data *nd, *od;
178 uint32_t idx;
179 pa_alsa_mapping *am;
180 pa_queue *sink_inputs = NULL, *source_outputs = NULL;
181
182 pa_assert(c);
183 pa_assert(new_profile);
184 pa_assert_se(u = c->userdata);
185
186 nd = PA_CARD_PROFILE_DATA(new_profile);
187 od = PA_CARD_PROFILE_DATA(c->active_profile);
188
189 if (od->profile && od->profile->output_mappings)
190 PA_IDXSET_FOREACH(am, od->profile->output_mappings, idx) {
191 if (!am->sink)
192 continue;
193
194 if (nd->profile &&
195 nd->profile->output_mappings &&
196 pa_idxset_get_by_data(nd->profile->output_mappings, am, NULL))
197 continue;
198
199 sink_inputs = pa_sink_move_all_start(am->sink, sink_inputs);
200 pa_alsa_sink_free(am->sink);
201 am->sink = NULL;
202 }
203
204 if (od->profile && od->profile->input_mappings)
205 PA_IDXSET_FOREACH(am, od->profile->input_mappings, idx) {
206 if (!am->source)
207 continue;
208
209 if (nd->profile &&
210 nd->profile->input_mappings &&
211 pa_idxset_get_by_data(nd->profile->input_mappings, am, NULL))
212 continue;
213
214 source_outputs = pa_source_move_all_start(am->source, source_outputs);
215 pa_alsa_source_free(am->source);
216 am->source = NULL;
217 }
218
219 if (nd->profile && nd->profile->output_mappings)
220 PA_IDXSET_FOREACH(am, nd->profile->output_mappings, idx) {
221
222 if (!am->sink)
223 am->sink = pa_alsa_sink_new(c->module, u->modargs, __FILE__, c, am);
224
225 if (sink_inputs && am->sink) {
226 pa_sink_move_all_finish(am->sink, sink_inputs, FALSE);
227 sink_inputs = NULL;
228 }
229 }
230
231 if (nd->profile && nd->profile->input_mappings)
232 PA_IDXSET_FOREACH(am, nd->profile->input_mappings, idx) {
233
234 if (!am->source)
235 am->source = pa_alsa_source_new(c->module, u->modargs, __FILE__, c, am);
236
237 if (source_outputs && am->source) {
238 pa_source_move_all_finish(am->source, source_outputs, FALSE);
239 source_outputs = NULL;
240 }
241 }
242
243 if (sink_inputs)
244 pa_sink_move_all_fail(sink_inputs);
245
246 if (source_outputs)
247 pa_source_move_all_fail(source_outputs);
248
249 return 0;
250 }
251
252 static void init_profile(struct userdata *u) {
253 uint32_t idx;
254 pa_alsa_mapping *am;
255 struct profile_data *d;
256
257 pa_assert(u);
258
259 d = PA_CARD_PROFILE_DATA(u->card->active_profile);
260
261 if (d->profile && d->profile->output_mappings)
262 PA_IDXSET_FOREACH(am, d->profile->output_mappings, idx)
263 am->sink = pa_alsa_sink_new(u->module, u->modargs, __FILE__, u->card, am);
264
265 if (d->profile && d->profile->input_mappings)
266 PA_IDXSET_FOREACH(am, d->profile->input_mappings, idx)
267 am->source = pa_alsa_source_new(u->module, u->modargs, __FILE__, u->card, am);
268 }
269
270 static void set_card_name(pa_card_new_data *data, pa_modargs *ma, const char *device_id) {
271 char *t;
272 const char *n;
273
274 pa_assert(data);
275 pa_assert(ma);
276 pa_assert(device_id);
277
278 if ((n = pa_modargs_get_value(ma, "card_name", NULL))) {
279 pa_card_new_data_set_name(data, n);
280 data->namereg_fail = TRUE;
281 return;
282 }
283
284 if ((n = pa_modargs_get_value(ma, "name", NULL)))
285 data->namereg_fail = TRUE;
286 else {
287 n = device_id;
288 data->namereg_fail = FALSE;
289 }
290
291 t = pa_sprintf_malloc("alsa_card.%s", n);
292 pa_card_new_data_set_name(data, t);
293 pa_xfree(t);
294 }
295
296 int pa__init(pa_module *m) {
297 pa_card_new_data data;
298 pa_modargs *ma;
299 int alsa_card_index;
300 pa_bool_t ignore_dB = FALSE;
301 struct userdata *u;
302 pa_reserve_wrapper *reserve = NULL;
303 const char *description;
304 const char *profile = NULL;
305 char *fn = NULL;
306 pa_bool_t namereg_fail = FALSE;
307
308 pa_alsa_refcnt_inc();
309
310 pa_assert(m);
311
312 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
313 pa_log("Failed to parse module arguments");
314 goto fail;
315 }
316
317 if (pa_modargs_get_value_boolean(ma, "ignore_dB", &ignore_dB) < 0) {
318 pa_log("Failed to parse ignore_dB argument.");
319 goto fail;
320 }
321
322 m->userdata = u = pa_xnew0(struct userdata, 1);
323 u->core = m->core;
324 u->module = m;
325 u->device_id = pa_xstrdup(pa_modargs_get_value(ma, "device_id", DEFAULT_DEVICE_ID));
326 u->modargs = ma;
327
328 if ((alsa_card_index = snd_card_get_index(u->device_id)) < 0) {
329 pa_log("Card '%s' doesn't exist: %s", u->device_id, pa_alsa_strerror(alsa_card_index));
330 goto fail;
331 }
332
333 if (!pa_in_system_mode()) {
334 char *rname;
335
336 if ((rname = pa_alsa_get_reserve_name(u->device_id))) {
337 reserve = pa_reserve_wrapper_get(m->core, rname);
338 pa_xfree(rname);
339
340 if (!reserve)
341 goto fail;
342 }
343 }
344
345 #ifdef HAVE_UDEV
346 fn = pa_udev_get_property(alsa_card_index, "PULSE_PROFILE_SET");
347 #endif
348
349 if (pa_modargs_get_value(ma, "profile_set", NULL)) {
350 pa_xfree(fn);
351 fn = pa_xstrdup(pa_modargs_get_value(ma, "profile_set", NULL));
352 }
353
354 u->profile_set = pa_alsa_profile_set_new(fn, &u->core->default_channel_map);
355 pa_xfree(fn);
356
357 u->profile_set->ignore_dB = ignore_dB;
358
359 if (!u->profile_set)
360 goto fail;
361
362 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);
363 pa_alsa_profile_set_dump(u->profile_set);
364
365 pa_card_new_data_init(&data);
366 data.driver = __FILE__;
367 data.module = m;
368
369 pa_alsa_init_proplist_card(m->core, data.proplist, alsa_card_index);
370
371 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_id);
372 pa_alsa_init_description(data.proplist);
373 set_card_name(&data, ma, u->device_id);
374
375 /* We need to give pa_modargs_get_value_boolean() a pointer to a local
376 * variable instead of using &data.namereg_fail directly, because
377 * data.namereg_fail is a bitfield and taking the address of a bitfield
378 * variable is impossible. */
379 namereg_fail = data.namereg_fail;
380 if (pa_modargs_get_value_boolean(ma, "namereg_fail", &namereg_fail) < 0) {
381 pa_log("Failed to parse namereg_fail argument.");
382 pa_card_new_data_done(&data);
383 goto fail;
384 }
385 data.namereg_fail = namereg_fail;
386
387 if (reserve)
388 if ((description = pa_proplist_gets(data.proplist, PA_PROP_DEVICE_DESCRIPTION)))
389 pa_reserve_wrapper_set_application_device_name(reserve, description);
390
391 data.profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
392 add_profiles(u, data.profiles, data.ports);
393
394 if (pa_hashmap_isempty(data.profiles)) {
395 pa_log("Failed to find a working profile.");
396 pa_card_new_data_done(&data);
397 goto fail;
398 }
399
400 add_disabled_profile(data.profiles);
401
402 if (pa_modargs_get_proplist(ma, "card_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
403 pa_log("Invalid properties");
404 pa_card_new_data_done(&data);
405 goto fail;
406 }
407
408 if ((profile = pa_modargs_get_value(ma, "profile", NULL)))
409 pa_card_new_data_set_profile(&data, profile);
410
411 u->card = pa_card_new(m->core, &data);
412 pa_card_new_data_done(&data);
413
414 if (!u->card)
415 goto fail;
416
417 u->card->userdata = u;
418 u->card->set_profile = card_set_profile;
419
420 init_profile(u);
421
422 if (reserve)
423 pa_reserve_wrapper_unref(reserve);
424
425 if (!pa_hashmap_isempty(u->profile_set->decibel_fixes))
426 pa_log_warn("Card %s uses decibel fixes (i.e. overrides the decibel information for some alsa volume elements). "
427 "Please note that this feature is meant just as a help for figuring out the correct decibel values. "
428 "Pulseaudio is not the correct place to maintain the decibel mappings! The fixed decibel values "
429 "should be sent to ALSA developers so that they can fix the driver. If it turns out that this feature "
430 "is abused (i.e. fixes are not pushed to ALSA), the decibel fix feature may be removed in some future "
431 "Pulseaudio version.", u->card->name);
432
433 return 0;
434
435 fail:
436 if (reserve)
437 pa_reserve_wrapper_unref(reserve);
438
439 pa__done(m);
440
441 return -1;
442 }
443
444 int pa__get_n_used(pa_module *m) {
445 struct userdata *u;
446 int n = 0;
447 uint32_t idx;
448 pa_sink *sink;
449 pa_source *source;
450
451 pa_assert(m);
452 pa_assert_se(u = m->userdata);
453 pa_assert(u->card);
454
455 PA_IDXSET_FOREACH(sink, u->card->sinks, idx)
456 n += pa_sink_linked_by(sink);
457
458 PA_IDXSET_FOREACH(source, u->card->sources, idx)
459 n += pa_source_linked_by(source);
460
461 return n;
462 }
463
464 void pa__done(pa_module*m) {
465 struct userdata *u;
466
467 pa_assert(m);
468
469 if (!(u = m->userdata))
470 goto finish;
471
472 if (u->card && u->card->sinks) {
473 pa_sink *s;
474
475 while ((s = pa_idxset_steal_first(u->card->sinks, NULL)))
476 pa_alsa_sink_free(s);
477 }
478
479 if (u->card && u->card->sources) {
480 pa_source *s;
481
482 while ((s = pa_idxset_steal_first(u->card->sources, NULL)))
483 pa_alsa_source_free(s);
484 }
485
486 if (u->card)
487 pa_card_free(u->card);
488
489 if (u->modargs)
490 pa_modargs_free(u->modargs);
491
492 if (u->profile_set)
493 pa_alsa_profile_set_free(u->profile_set);
494
495 pa_xfree(u->device_id);
496 pa_xfree(u);
497
498 finish:
499 pa_alsa_refcnt_dec();
500 }