2 This file is part of PulseAudio.
4 Copyright 2009 Lennart Poettering
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.
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.
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
26 #include <pulse/xmalloc.h>
28 #include <pulsecore/core-util.h>
29 #include <pulsecore/i18n.h>
30 #include <pulsecore/modargs.h>
31 #include <pulsecore/queue.h>
33 #include <modules/reserve-wrap.h>
36 #include <modules/udev-util.h>
39 #include "alsa-util.h"
40 #include "alsa-sink.h"
41 #include "alsa-source.h"
42 #include "module-alsa-card-symdef.h"
44 PA_MODULE_AUTHOR("Lennart Poettering");
45 PA_MODULE_DESCRIPTION("ALSA Card");
46 PA_MODULE_VERSION(PACKAGE_VERSION
);
47 PA_MODULE_LOAD_ONCE(FALSE
);
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> "
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> "
74 static const char* const valid_modargs
[] = {
91 "tsched_buffer_watermark",
92 "fixed_latency_range",
101 #define DEFAULT_DEVICE_ID "0"
113 pa_alsa_profile_set
*profile_set
;
116 struct profile_data
{
117 pa_alsa_profile
*profile
;
120 static void add_profiles(struct userdata
*u
, pa_hashmap
*h
) {
127 PA_HASHMAP_FOREACH(ap
, u
->profile_set
->profiles
, state
) {
128 struct profile_data
*d
;
133 cp
= pa_card_profile_new(ap
->name
, ap
->description
, sizeof(struct profile_data
));
134 cp
->priority
= ap
->priority
;
136 if (ap
->output_mappings
) {
137 cp
->n_sinks
= pa_idxset_size(ap
->output_mappings
);
139 PA_IDXSET_FOREACH(m
, ap
->output_mappings
, idx
)
140 if (m
->channel_map
.channels
> cp
->max_sink_channels
)
141 cp
->max_sink_channels
= m
->channel_map
.channels
;
144 if (ap
->input_mappings
) {
145 cp
->n_sources
= pa_idxset_size(ap
->input_mappings
);
147 PA_IDXSET_FOREACH(m
, ap
->input_mappings
, idx
)
148 if (m
->channel_map
.channels
> cp
->max_source_channels
)
149 cp
->max_source_channels
= m
->channel_map
.channels
;
152 d
= PA_CARD_PROFILE_DATA(cp
);
155 pa_hashmap_put(h
, cp
->name
, cp
);
159 static void add_disabled_profile(pa_hashmap
*profiles
) {
161 struct profile_data
*d
;
163 p
= pa_card_profile_new("off", _("Off"), sizeof(struct profile_data
));
165 d
= PA_CARD_PROFILE_DATA(p
);
168 pa_hashmap_put(profiles
, p
->name
, p
);
171 static int card_set_profile(pa_card
*c
, pa_card_profile
*new_profile
) {
173 struct profile_data
*nd
, *od
;
176 pa_queue
*sink_inputs
= NULL
, *source_outputs
= NULL
;
179 pa_assert(new_profile
);
180 pa_assert_se(u
= c
->userdata
);
182 nd
= PA_CARD_PROFILE_DATA(new_profile
);
183 od
= PA_CARD_PROFILE_DATA(c
->active_profile
);
185 if (od
->profile
&& od
->profile
->output_mappings
)
186 PA_IDXSET_FOREACH(am
, od
->profile
->output_mappings
, idx
) {
191 nd
->profile
->output_mappings
&&
192 pa_idxset_get_by_data(nd
->profile
->output_mappings
, am
, NULL
))
195 sink_inputs
= pa_sink_move_all_start(am
->sink
, sink_inputs
);
196 pa_alsa_sink_free(am
->sink
);
200 if (od
->profile
&& od
->profile
->input_mappings
)
201 PA_IDXSET_FOREACH(am
, od
->profile
->input_mappings
, idx
) {
206 nd
->profile
->input_mappings
&&
207 pa_idxset_get_by_data(nd
->profile
->input_mappings
, am
, NULL
))
210 source_outputs
= pa_source_move_all_start(am
->source
, source_outputs
);
211 pa_alsa_source_free(am
->source
);
215 if (nd
->profile
&& nd
->profile
->output_mappings
)
216 PA_IDXSET_FOREACH(am
, nd
->profile
->output_mappings
, idx
) {
219 am
->sink
= pa_alsa_sink_new(c
->module
, u
->modargs
, __FILE__
, c
, am
);
221 if (sink_inputs
&& am
->sink
) {
222 pa_sink_move_all_finish(am
->sink
, sink_inputs
, FALSE
);
227 if (nd
->profile
&& nd
->profile
->input_mappings
)
228 PA_IDXSET_FOREACH(am
, nd
->profile
->input_mappings
, idx
) {
231 am
->source
= pa_alsa_source_new(c
->module
, u
->modargs
, __FILE__
, c
, am
);
233 if (source_outputs
&& am
->source
) {
234 pa_source_move_all_finish(am
->source
, source_outputs
, FALSE
);
235 source_outputs
= NULL
;
240 pa_sink_move_all_fail(sink_inputs
);
243 pa_source_move_all_fail(source_outputs
);
248 static void init_profile(struct userdata
*u
) {
251 struct profile_data
*d
;
255 d
= PA_CARD_PROFILE_DATA(u
->card
->active_profile
);
257 if (d
->profile
&& d
->profile
->output_mappings
)
258 PA_IDXSET_FOREACH(am
, d
->profile
->output_mappings
, idx
)
259 am
->sink
= pa_alsa_sink_new(u
->module
, u
->modargs
, __FILE__
, u
->card
, am
);
261 if (d
->profile
&& d
->profile
->input_mappings
)
262 PA_IDXSET_FOREACH(am
, d
->profile
->input_mappings
, idx
)
263 am
->source
= pa_alsa_source_new(u
->module
, u
->modargs
, __FILE__
, u
->card
, am
);
266 static void set_card_name(pa_card_new_data
*data
, pa_modargs
*ma
, const char *device_id
) {
272 pa_assert(device_id
);
274 if ((n
= pa_modargs_get_value(ma
, "card_name", NULL
))) {
275 pa_card_new_data_set_name(data
, n
);
276 data
->namereg_fail
= TRUE
;
280 if ((n
= pa_modargs_get_value(ma
, "name", NULL
)))
281 data
->namereg_fail
= TRUE
;
284 data
->namereg_fail
= FALSE
;
287 t
= pa_sprintf_malloc("alsa_card.%s", n
);
288 pa_card_new_data_set_name(data
, t
);
292 int pa__init(pa_module
*m
) {
293 pa_card_new_data data
;
297 pa_reserve_wrapper
*reserve
= NULL
;
298 const char *description
;
299 const char *profile
= NULL
;
301 pa_bool_t namereg_fail
= FALSE
;
303 pa_alsa_refcnt_inc();
307 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
308 pa_log("Failed to parse module arguments");
312 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
315 u
->device_id
= pa_xstrdup(pa_modargs_get_value(ma
, "device_id", DEFAULT_DEVICE_ID
));
318 if ((alsa_card_index
= snd_card_get_index(u
->device_id
)) < 0) {
319 pa_log("Card '%s' doesn't exist: %s", u
->device_id
, pa_alsa_strerror(alsa_card_index
));
323 if (!pa_in_system_mode()) {
326 if ((rname
= pa_alsa_get_reserve_name(u
->device_id
))) {
327 reserve
= pa_reserve_wrapper_get(m
->core
, rname
);
336 fn
= pa_udev_get_property(alsa_card_index
, "PULSE_PROFILE_SET");
339 if (pa_modargs_get_value(ma
, "profile_set", NULL
)) {
341 fn
= pa_xstrdup(pa_modargs_get_value(ma
, "profile_set", NULL
));
344 u
->profile_set
= pa_alsa_profile_set_new(fn
, &u
->core
->default_channel_map
);
350 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
);
351 pa_alsa_profile_set_dump(u
->profile_set
);
353 pa_card_new_data_init(&data
);
354 data
.driver
= __FILE__
;
357 pa_alsa_init_proplist_card(m
->core
, data
.proplist
, alsa_card_index
);
359 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_STRING
, u
->device_id
);
360 pa_alsa_init_description(data
.proplist
);
361 set_card_name(&data
, ma
, u
->device_id
);
363 /* We need to give pa_modargs_get_value_boolean() a pointer to a local
364 * variable instead of using &data.namereg_fail directly, because
365 * data.namereg_fail is a bitfield and taking the address of a bitfield
366 * variable is impossible. */
367 namereg_fail
= data
.namereg_fail
;
368 if (pa_modargs_get_value_boolean(ma
, "namereg_fail", &namereg_fail
) < 0) {
369 pa_log("Failed to parse namereg_fail argument.");
370 pa_card_new_data_done(&data
);
373 data
.namereg_fail
= namereg_fail
;
376 if ((description
= pa_proplist_gets(data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
)))
377 pa_reserve_wrapper_set_application_device_name(reserve
, description
);
379 data
.profiles
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
380 add_profiles(u
, data
.profiles
);
382 if (pa_hashmap_isempty(data
.profiles
)) {
383 pa_log("Failed to find a working profile.");
384 pa_card_new_data_done(&data
);
388 add_disabled_profile(data
.profiles
);
390 if (pa_modargs_get_proplist(ma
, "card_properties", data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
391 pa_log("Invalid properties");
392 pa_card_new_data_done(&data
);
396 if ((profile
= pa_modargs_get_value(ma
, "profile", NULL
)))
397 pa_card_new_data_set_profile(&data
, profile
);
399 u
->card
= pa_card_new(m
->core
, &data
);
400 pa_card_new_data_done(&data
);
405 u
->card
->userdata
= u
;
406 u
->card
->set_profile
= card_set_profile
;
411 pa_reserve_wrapper_unref(reserve
);
413 if (!pa_hashmap_isempty(u
->profile_set
->decibel_fixes
))
414 pa_log_warn("Card %s uses decibel fixes (i.e. overrides the decibel information for some alsa volume elements). "
415 "Please note that this feature is meant just as a help for figuring out the correct decibel values. "
416 "Pulseaudio is not the correct place to maintain the decibel mappings! The fixed decibel values "
417 "should be sent to ALSA developers so that they can fix the driver. If it turns out that this feature "
418 "is abused (i.e. fixes are not pushed to ALSA), the decibel fix feature may be removed in some future "
419 "Pulseaudio version.", u
->card
->name
);
425 pa_reserve_wrapper_unref(reserve
);
432 int pa__get_n_used(pa_module
*m
) {
440 pa_assert_se(u
= m
->userdata
);
443 PA_IDXSET_FOREACH(sink
, u
->card
->sinks
, idx
)
444 n
+= pa_sink_linked_by(sink
);
446 PA_IDXSET_FOREACH(source
, u
->card
->sources
, idx
)
447 n
+= pa_source_linked_by(source
);
452 void pa__done(pa_module
*m
) {
457 if (!(u
= m
->userdata
))
460 if (u
->card
&& u
->card
->sinks
) {
463 while ((s
= pa_idxset_steal_first(u
->card
->sinks
, NULL
)))
464 pa_alsa_sink_free(s
);
467 if (u
->card
&& u
->card
->sources
) {
470 while ((s
= pa_idxset_steal_first(u
->card
->sources
, NULL
)))
471 pa_alsa_source_free(s
);
475 pa_card_free(u
->card
);
478 pa_modargs_free(u
->modargs
);
481 pa_alsa_profile_set_free(u
->profile_set
);
483 pa_xfree(u
->device_id
);
487 pa_alsa_refcnt_dec();