Merge commit 'origin/master-tx'
[platform/upstream/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 #include <pulse/i18n.h>
28
29 #include <pulsecore/core-util.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         "device_id=<ALSA card index> "
57         "format=<sample format> "
58         "rate=<sample rate> "
59         "fragments=<number of fragments> "
60         "fragment_size=<fragment size> "
61         "mmap=<enable memory mapping?> "
62         "tsched=<enable system timer based scheduling mode?> "
63         "tsched_buffer_size=<buffer size when using timer based scheduling> "
64         "tsched_buffer_watermark=<lower fill watermark> "
65         "profile=<profile name> "
66         "ignore_dB=<ignore dB information from the device?>");
67
68 static const char* const valid_modargs[] = {
69     "name",
70     "card_name",
71     "card_properties",
72     "sink_name",
73     "sink_properties",
74     "source_name",
75     "source_properties",
76     "device_id",
77     "format",
78     "rate",
79     "fragments",
80     "fragment_size",
81     "mmap",
82     "tsched",
83     "tsched_buffer_size",
84     "tsched_buffer_watermark",
85     "profile",
86     "ignore_dB",
87     NULL
88 };
89
90 #define DEFAULT_DEVICE_ID "0"
91
92 struct userdata {
93     pa_core *core;
94     pa_module *module;
95
96     char *device_id;
97
98     pa_card *card;
99
100     pa_modargs *modargs;
101
102     pa_alsa_profile_set *profile_set;
103 };
104
105 struct profile_data {
106     pa_alsa_profile *profile;
107 };
108
109 static void add_profiles(struct userdata *u, pa_hashmap *h) {
110     pa_alsa_profile *ap;
111     void *state;
112
113     pa_assert(u);
114     pa_assert(h);
115
116     PA_HASHMAP_FOREACH(ap, u->profile_set->profiles, state) {
117         struct profile_data *d;
118         pa_card_profile *cp;
119         pa_alsa_mapping *m;
120         uint32_t idx;
121
122         cp = pa_card_profile_new(ap->name, ap->description, sizeof(struct profile_data));
123         cp->priority = ap->priority;
124
125         if (ap->output_mappings) {
126             cp->n_sinks = pa_idxset_size(ap->output_mappings);
127
128             PA_IDXSET_FOREACH(m, ap->output_mappings, idx)
129                 if (m->channel_map.channels > cp->max_sink_channels)
130                     cp->max_sink_channels = m->channel_map.channels;
131         }
132
133         if (ap->input_mappings) {
134             cp->n_sources = pa_idxset_size(ap->input_mappings);
135
136             PA_IDXSET_FOREACH(m, ap->input_mappings, idx)
137                 if (m->channel_map.channels > cp->max_source_channels)
138                     cp->max_source_channels = m->channel_map.channels;
139         }
140
141         d = PA_CARD_PROFILE_DATA(cp);
142         d->profile = ap;
143
144         pa_hashmap_put(h, cp->name, cp);
145     }
146 }
147
148 static void add_disabled_profile(pa_hashmap *profiles) {
149     pa_card_profile *p;
150     struct profile_data *d;
151
152     p = pa_card_profile_new("off", _("Off"), sizeof(struct profile_data));
153
154     d = PA_CARD_PROFILE_DATA(p);
155     d->profile = NULL;
156
157     pa_hashmap_put(profiles, p->name, p);
158 }
159
160 static int card_set_profile(pa_card *c, pa_card_profile *new_profile) {
161     struct userdata *u;
162     struct profile_data *nd, *od;
163     uint32_t idx;
164     pa_alsa_mapping *am;
165     pa_queue *sink_inputs = NULL, *source_outputs = NULL;
166
167     pa_assert(c);
168     pa_assert(new_profile);
169     pa_assert_se(u = c->userdata);
170
171     nd = PA_CARD_PROFILE_DATA(new_profile);
172     od = PA_CARD_PROFILE_DATA(c->active_profile);
173
174     if (od->profile && od->profile->output_mappings)
175         PA_IDXSET_FOREACH(am, od->profile->output_mappings, idx) {
176             if (!am->sink)
177                 continue;
178
179             if (nd->profile &&
180                 nd->profile->output_mappings &&
181                 pa_idxset_get_by_data(nd->profile->output_mappings, am, NULL))
182                 continue;
183
184             sink_inputs = pa_sink_move_all_start(am->sink, sink_inputs);
185             pa_alsa_sink_free(am->sink);
186             am->sink = NULL;
187         }
188
189     if (od->profile && od->profile->input_mappings)
190         PA_IDXSET_FOREACH(am, od->profile->input_mappings, idx) {
191             if (!am->source)
192                 continue;
193
194             if (nd->profile &&
195                 nd->profile->input_mappings &&
196                 pa_idxset_get_by_data(nd->profile->input_mappings, am, NULL))
197                 continue;
198
199             source_outputs = pa_source_move_all_start(am->source, source_outputs);
200             pa_alsa_source_free(am->source);
201             am->source = NULL;
202         }
203
204     if (nd->profile && nd->profile->output_mappings)
205         PA_IDXSET_FOREACH(am, nd->profile->output_mappings, idx) {
206
207             if (!am->sink)
208                 am->sink = pa_alsa_sink_new(c->module, u->modargs, __FILE__, c, am);
209
210             if (sink_inputs && am->sink) {
211                 pa_sink_move_all_finish(am->sink, sink_inputs, FALSE);
212                 sink_inputs = NULL;
213             }
214         }
215
216     if (nd->profile && nd->profile->input_mappings)
217         PA_IDXSET_FOREACH(am, nd->profile->input_mappings, idx) {
218
219             if (!am->source)
220                 am->source = pa_alsa_source_new(c->module, u->modargs, __FILE__, c, am);
221
222             if (source_outputs && am->source) {
223                 pa_source_move_all_finish(am->source, source_outputs, FALSE);
224                 source_outputs = NULL;
225             }
226         }
227
228     if (sink_inputs)
229         pa_sink_move_all_fail(sink_inputs);
230
231     if (source_outputs)
232         pa_source_move_all_fail(source_outputs);
233
234     return 0;
235 }
236
237 static void init_profile(struct userdata *u) {
238     uint32_t idx;
239     pa_alsa_mapping *am;
240     struct profile_data *d;
241
242     pa_assert(u);
243
244     d = PA_CARD_PROFILE_DATA(u->card->active_profile);
245
246     if (d->profile && d->profile->output_mappings)
247         PA_IDXSET_FOREACH(am, d->profile->output_mappings, idx)
248             am->sink = pa_alsa_sink_new(u->module, u->modargs, __FILE__, u->card, am);
249
250     if (d->profile && d->profile->input_mappings)
251         PA_IDXSET_FOREACH(am, d->profile->input_mappings, idx)
252             am->source = pa_alsa_source_new(u->module, u->modargs, __FILE__, u->card, am);
253 }
254
255 static void set_card_name(pa_card_new_data *data, pa_modargs *ma, const char *device_id) {
256     char *t;
257     const char *n;
258
259     pa_assert(data);
260     pa_assert(ma);
261     pa_assert(device_id);
262
263     if ((n = pa_modargs_get_value(ma, "card_name", NULL))) {
264         pa_card_new_data_set_name(data, n);
265         data->namereg_fail = TRUE;
266         return;
267     }
268
269     if ((n = pa_modargs_get_value(ma, "name", NULL)))
270         data->namereg_fail = TRUE;
271     else {
272         n = device_id;
273         data->namereg_fail = FALSE;
274     }
275
276     t = pa_sprintf_malloc("alsa_card.%s", n);
277     pa_card_new_data_set_name(data, t);
278     pa_xfree(t);
279 }
280
281 int pa__init(pa_module *m) {
282     pa_card_new_data data;
283     pa_modargs *ma;
284     int alsa_card_index;
285     struct userdata *u;
286     pa_reserve_wrapper *reserve = NULL;
287     const char *description;
288     char *fn = NULL;
289
290     pa_alsa_redirect_errors_inc();
291     snd_config_update_free_global();
292
293     pa_assert(m);
294
295     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
296         pa_log("Failed to parse module arguments");
297         goto fail;
298     }
299
300     m->userdata = u = pa_xnew0(struct userdata, 1);
301     u->core = m->core;
302     u->module = m;
303     u->device_id = pa_xstrdup(pa_modargs_get_value(ma, "device_id", DEFAULT_DEVICE_ID));
304     u->modargs = ma;
305
306     if ((alsa_card_index = snd_card_get_index(u->device_id)) < 0) {
307         pa_log("Card '%s' doesn't exist: %s", u->device_id, pa_alsa_strerror(alsa_card_index));
308         goto fail;
309     }
310
311     if (!pa_in_system_mode()) {
312         char *rname;
313
314         if ((rname = pa_alsa_get_reserve_name(u->device_id))) {
315             reserve = pa_reserve_wrapper_get(m->core, rname);
316             pa_xfree(rname);
317
318             if (!reserve)
319                 goto fail;
320         }
321     }
322
323 #ifdef HAVE_UDEV
324     fn = pa_udev_get_property(alsa_card_index, "PULSE_PROFILE_SET");
325 #endif
326
327     u->profile_set = pa_alsa_profile_set_new(fn, &u->core->default_channel_map);
328     pa_xfree(fn);
329
330     if (!u->profile_set)
331         goto fail;
332
333     pa_alsa_profile_set_probe(u->profile_set, u->device_id, &m->core->default_sample_spec);
334
335     pa_card_new_data_init(&data);
336     data.driver = __FILE__;
337     data.module = m;
338
339     pa_alsa_init_proplist_card(m->core, data.proplist, alsa_card_index);
340
341     pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_id);
342     pa_alsa_init_description(data.proplist);
343     set_card_name(&data, ma, u->device_id);
344
345     if (reserve)
346         if ((description = pa_proplist_gets(data.proplist, PA_PROP_DEVICE_DESCRIPTION)))
347             pa_reserve_wrapper_set_application_device_name(reserve, description);
348
349     data.profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
350     add_profiles(u, data.profiles);
351
352     if (pa_hashmap_isempty(data.profiles)) {
353         pa_log("Failed to find a working profile.");
354         pa_card_new_data_done(&data);
355         goto fail;
356     }
357
358     add_disabled_profile(data.profiles);
359
360     if (pa_modargs_get_proplist(ma, "card_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
361         pa_log("Invalid properties");
362         pa_card_new_data_done(&data);
363         goto fail;
364     }
365
366     u->card = pa_card_new(m->core, &data);
367     pa_card_new_data_done(&data);
368
369     if (!u->card)
370         goto fail;
371
372     u->card->userdata = u;
373     u->card->set_profile = card_set_profile;
374
375     init_profile(u);
376
377     if (reserve)
378         pa_reserve_wrapper_unref(reserve);
379
380     return 0;
381
382 fail:
383     if (reserve)
384         pa_reserve_wrapper_unref(reserve);
385
386     pa__done(m);
387
388     return -1;
389 }
390
391 int pa__get_n_used(pa_module *m) {
392     struct userdata *u;
393     int n = 0;
394     uint32_t idx;
395     pa_sink *sink;
396     pa_source *source;
397
398     pa_assert(m);
399     pa_assert_se(u = m->userdata);
400     pa_assert(u->card);
401
402     PA_IDXSET_FOREACH(sink, u->card->sinks, idx)
403         n += pa_sink_linked_by(sink);
404
405     PA_IDXSET_FOREACH(source, u->card->sources, idx)
406         n += pa_source_linked_by(source);
407
408     return n;
409 }
410
411 void pa__done(pa_module*m) {
412     struct userdata *u;
413
414     pa_assert(m);
415
416     if (!(u = m->userdata))
417         goto finish;
418
419     if (u->card && u->card->sinks) {
420         pa_sink *s;
421
422         while ((s = pa_idxset_steal_first(u->card->sinks, NULL)))
423             pa_alsa_sink_free(s);
424     }
425
426     if (u->card && u->card->sources) {
427         pa_source *s;
428
429         while ((s = pa_idxset_steal_first(u->card->sources, NULL)))
430             pa_alsa_source_free(s);
431     }
432
433     if (u->card)
434         pa_card_free(u->card);
435
436     if (u->modargs)
437         pa_modargs_free(u->modargs);
438
439     if (u->profile_set)
440         pa_alsa_profile_set_free(u->profile_set);
441
442     pa_xfree(u->device_id);
443     pa_xfree(u);
444
445 finish:
446     snd_config_update_free_global();
447     pa_alsa_redirect_errors_dec();
448 }