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
8 published by the Free Software Foundation; either version 2.1 of the
9 License, 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
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include <sys/inotify.h>
31 #include <pulsecore/modargs.h>
32 #include <pulsecore/core-error.h>
33 #include <pulsecore/core-util.h>
34 #include <pulsecore/namereg.h>
36 #include "module-udev-detect-symdef.h"
38 PA_MODULE_AUTHOR("Lennart Poettering");
39 PA_MODULE_DESCRIPTION("Detect available audio hardware and load matching drivers");
40 PA_MODULE_VERSION(PACKAGE_VERSION
);
41 PA_MODULE_LOAD_ONCE(TRUE
);
43 "tsched=<enable system timer based scheduling mode?> "
44 "ignore_dB=<ignore dB information from the device?>");
48 pa_bool_t accessible
:1;
49 pa_bool_t need_verify
:1;
59 pa_bool_t use_tsched
:1;
60 pa_bool_t ignore_dB
:1;
63 struct udev_monitor
*monitor
;
67 pa_io_event
*inotify_io
;
70 static const char* const valid_modargs
[] = {
76 static int setup_inotify(struct userdata
*u
);
78 static void device_free(struct device
*d
) {
82 pa_xfree(d
->card_name
);
87 static const char *path_get_card_id(const char *path
) {
93 if (!(e
= strrchr(path
, '/')))
96 if (!pa_startswith(e
, "/card"))
102 static void verify_access(struct userdata
*u
, struct device
*d
) {
109 cd
= pa_sprintf_malloc("%s/snd/controlC%s", udev_get_dev_path(u
->udev
), path_get_card_id(d
->path
));
110 d
->accessible
= access(cd
, R_OK
|W_OK
) >= 0;
112 pa_log_info("%s is accessible: %s", cd
, pa_yes_no(d
->accessible
));
115 if (d
->module
== PA_INVALID_INDEX
) {
117 /* If we not loaded, try to load */
122 pa_log_debug("Loading module-alsa-card with arguments '%s'", d
->args
);
123 m
= pa_module_load(u
->core
, "module-alsa-card", d
->args
);
126 d
->module
= m
->index
;
127 pa_log_info("Card %s (%s) module loaded.", d
->path
, d
->card_name
);
129 pa_log_info("Card %s (%s) failed to load module.", d
->path
, d
->card_name
);
134 /* If we are already loaded update suspend status with
135 * accessible boolean */
137 if ((card
= pa_namereg_get(u
->core
, d
->card_name
, PA_NAMEREG_CARD
)))
138 pa_card_suspend(card
, !d
->accessible
, PA_SUSPEND_SESSION
);
142 static void card_changed(struct userdata
*u
, struct udev_device
*dev
) {
151 /* Maybe /dev/snd is now available? */
154 path
= udev_device_get_devpath(dev
);
156 if ((d
= pa_hashmap_get(u
->devices
, path
))) {
161 d
= pa_xnew0(struct device
, 1);
162 d
->path
= pa_xstrdup(path
);
163 d
->accessible
= TRUE
;
164 d
->module
= PA_INVALID_INDEX
;
166 if (!(t
= udev_device_get_property_value(dev
, "PULSE_NAME")))
167 if (!(t
= udev_device_get_property_value(dev
, "ID_ID")))
168 if (!(t
= udev_device_get_property_value(dev
, "ID_PATH")))
169 t
= path_get_card_id(path
);
171 n
= pa_namereg_make_valid_name(t
);
172 d
->card_name
= pa_sprintf_malloc("alsa_card.%s", n
);
173 d
->args
= pa_sprintf_malloc("device_id=\"%s\" "
178 "card_properties=\"module-udev-detect.discovered=1\"",
179 path_get_card_id(path
),
182 pa_yes_no(u
->use_tsched
),
183 pa_yes_no(u
->ignore_dB
));
186 pa_hashmap_put(u
->devices
, d
->path
, d
);
191 static void remove_card(struct userdata
*u
, struct udev_device
*dev
) {
197 if (!(d
= pa_hashmap_remove(u
->devices
, udev_device_get_devpath(dev
))))
200 pa_log_info("Card %s removed.", d
->path
);
202 if (d
->module
!= PA_INVALID_INDEX
)
203 pa_module_unload_request_by_index(u
->core
, d
->module
, TRUE
);
208 static void process_device(struct userdata
*u
, struct udev_device
*dev
) {
209 const char *action
, *ff
;
214 if (udev_device_get_property_value(dev
, "PULSE_IGNORE")) {
215 pa_log_debug("Ignoring %s, because marked so.", udev_device_get_devpath(dev
));
219 if ((ff
= udev_device_get_property_value(dev
, "SOUND_FORM_FACTOR")) &&
220 pa_streq(ff
, "modem")) {
221 pa_log_debug("Ignoring %s, because it is a modem.", udev_device_get_devpath(dev
));
225 action
= udev_device_get_action(dev
);
227 if (action
&& pa_streq(action
, "remove"))
229 else if ((!action
|| pa_streq(action
, "change")) &&
230 udev_device_get_property_value(dev
, "SOUND_INITIALIZED"))
231 card_changed(u
, dev
);
233 /* For an explanation why we don't look for 'add' events here
234 * have a look into /lib/udev/rules.d/78-sound-card.rules! */
237 static void process_path(struct userdata
*u
, const char *path
) {
238 struct udev_device
*dev
;
240 if (!path_get_card_id(path
))
243 if (!(dev
= udev_device_new_from_syspath(u
->udev
, path
))) {
244 pa_log("Failed to get udev device object from udev.");
248 process_device(u
, dev
);
249 udev_device_unref(dev
);
252 static void monitor_cb(
256 pa_io_event_flags_t events
,
259 struct userdata
*u
= userdata
;
260 struct udev_device
*dev
;
264 if (!(dev
= udev_monitor_receive_device(u
->monitor
))) {
265 pa_log("Failed to get udev device object from monitor.");
269 if (!path_get_card_id(udev_device_get_devpath(dev
)))
272 process_device(u
, dev
);
273 udev_device_unref(dev
);
277 a
->io_free(u
->udev_io
);
281 static pa_bool_t
pcm_node_belongs_to_device(
288 cd
= pa_sprintf_malloc("pcmC%sD", path_get_card_id(d
->path
));
289 b
= pa_startswith(node
, cd
);
295 static pa_bool_t
control_node_belongs_to_device(
302 cd
= pa_sprintf_malloc("controlC%s", path_get_card_id(d
->path
));
303 b
= pa_streq(node
, cd
);
309 static void inotify_cb(
313 pa_io_event_flags_t events
,
317 struct inotify_event e
;
320 struct userdata
*u
= userdata
;
322 pa_bool_t deleted
= FALSE
;
330 if ((r
= pa_read(fd
, &buf
, sizeof(buf
), &type
)) <= 0) {
332 if (r
< 0 && errno
== EAGAIN
)
335 pa_log("read() from inotify failed: %s", r
< 0 ? pa_cstrerror(errno
) : "EOF");
339 /* From udev we get the guarantee that the control
340 * device's ACL is changes last. To avoid races when ACLs
341 * are changed we hence watch only the control device */
342 if (((buf
.e
.mask
& IN_ATTRIB
) && pa_startswith(buf
.e
.name
, "controlC")))
343 PA_HASHMAP_FOREACH(d
, u
->devices
, state
)
344 if (control_node_belongs_to_device(d
, buf
.e
.name
))
345 d
->need_verify
= TRUE
;
347 /* ALSA doesn't really give us any guarantee on the closing
348 * order, so let's simply hope */
349 if (((buf
.e
.mask
& IN_CLOSE_WRITE
) && pa_startswith(buf
.e
.name
, "pcmC")))
350 PA_HASHMAP_FOREACH(d
, u
->devices
, state
)
351 if (pcm_node_belongs_to_device(d
, buf
.e
.name
))
352 d
->need_verify
= TRUE
;
354 if ((buf
.e
.mask
& (IN_DELETE_SELF
|IN_MOVE_SELF
)))
358 PA_HASHMAP_FOREACH(d
, u
->devices
, state
)
359 if (d
->need_verify
) {
360 d
->need_verify
= FALSE
;
369 a
->io_free(u
->inotify_io
);
370 u
->inotify_io
= NULL
;
373 if (u
->inotify_fd
>= 0) {
374 pa_close(u
->inotify_fd
);
379 static int setup_inotify(struct userdata
*u
) {
383 if (u
->inotify_fd
>= 0)
386 if ((u
->inotify_fd
= inotify_init1(IN_CLOEXEC
|IN_NONBLOCK
)) < 0) {
387 pa_log("inotify_init1() failed: %s", pa_cstrerror(errno
));
391 dev_snd
= pa_sprintf_malloc("%s/snd", udev_get_dev_path(u
->udev
));
392 r
= inotify_add_watch(u
->inotify_fd
, dev_snd
, IN_ATTRIB
|IN_CLOSE_WRITE
|IN_DELETE_SELF
|IN_MOVE_SELF
);
396 int saved_errno
= errno
;
398 pa_close(u
->inotify_fd
);
401 if (saved_errno
== ENOENT
) {
402 pa_log_debug("/dev/snd/ is apparently not existing yet, retrying to create inotify watch later.");
406 if (saved_errno
== ENOSPC
) {
407 pa_log("You apparently ran out of inotify watches, probably because Tracker/Beagle took them all away. "
408 "I wished people would do their homework first and fix inotify before using it for watching whole "
409 "directory trees which is something the current inotify is certainly not useful for. "
410 "Please make sure to drop the Tracker/Beagle guys a line complaining about their broken use of inotify.");
414 pa_log("inotify_add_watch() failed: %s", pa_cstrerror(saved_errno
));
418 pa_assert_se(u
->inotify_io
= u
->core
->mainloop
->io_new(u
->core
->mainloop
, u
->inotify_fd
, PA_IO_EVENT_INPUT
, inotify_cb
, u
));
423 int pa__init(pa_module
*m
) {
424 struct userdata
*u
= NULL
;
426 struct udev_enumerate
*enumerate
= NULL
;
427 struct udev_list_entry
*item
= NULL
, *first
= NULL
;
429 pa_bool_t use_tsched
= TRUE
, ignore_dB
= FALSE
;
433 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
434 pa_log("Failed to parse module arguments");
438 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
440 u
->devices
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
443 if (pa_modargs_get_value_boolean(ma
, "tsched", &use_tsched
) < 0) {
444 pa_log("Failed to parse tsched= argument.");
447 u
->use_tsched
= use_tsched
;
449 if (pa_modargs_get_value_boolean(ma
, "ignore_dB", &ignore_dB
) < 0) {
450 pa_log("Failed to parse ignore_dB= argument.");
453 u
->ignore_dB
= ignore_dB
;
455 if (!(u
->udev
= udev_new())) {
456 pa_log("Failed to initialize udev library.");
460 if (setup_inotify(u
) < 0)
463 if (!(u
->monitor
= udev_monitor_new_from_netlink(u
->udev
, "udev"))) {
464 pa_log("Failed to initialize monitor.");
469 if (udev_monitor_enable_receiving(u
->monitor
) < 0) {
470 pa_log("Failed to enable monitor: %s", pa_cstrerror(errno
));
472 pa_log_info("Most likely your kernel is simply too old and "
473 "allows only priviliged processes to listen to device events. "
474 "Please upgrade your kernel to at least 2.6.30.");
478 if ((fd
= udev_monitor_get_fd(u
->monitor
)) < 0) {
479 pa_log("Failed to get udev monitor fd.");
483 pa_assert_se(u
->udev_io
= u
->core
->mainloop
->io_new(u
->core
->mainloop
, fd
, PA_IO_EVENT_INPUT
, monitor_cb
, u
));
485 if (!(enumerate
= udev_enumerate_new(u
->udev
))) {
486 pa_log("Failed to initialize udev enumerator.");
490 if (udev_enumerate_add_match_subsystem(enumerate
, "sound") < 0) {
491 pa_log("Failed to match to subsystem.");
495 if (udev_enumerate_scan_devices(enumerate
) < 0) {
496 pa_log("Failed to scan for devices.");
500 first
= udev_enumerate_get_list_entry(enumerate
);
501 udev_list_entry_foreach(item
, first
)
502 process_path(u
, udev_list_entry_get_name(item
));
504 udev_enumerate_unref(enumerate
);
506 pa_log_info("Found %u cards.", pa_hashmap_size(u
->devices
));
515 udev_enumerate_unref(enumerate
);
525 void pa__done(pa_module
*m
) {
530 if (!(u
= m
->userdata
))
534 m
->core
->mainloop
->io_free(u
->udev_io
);
537 udev_monitor_unref(u
->monitor
);
543 m
->core
->mainloop
->io_free(u
->inotify_io
);
545 if (u
->inotify_fd
>= 0)
546 pa_close(u
->inotify_fd
);
551 while ((d
= pa_hashmap_steal_first(u
->devices
)))
554 pa_hashmap_free(u
->devices
, NULL
, NULL
);