]> code.delx.au - pulseaudio/blob - src/modules/module-udev-detect.c
udev: Add tsched_buffer_size module argument
[pulseaudio] / src / modules / module-udev-detect.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
8 published by the Free Software Foundation; either version 2.1 of the
9 License, 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
17 License 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 <errno.h>
27 #include <limits.h>
28 #include <dirent.h>
29 #include <sys/inotify.h>
30 #include <libudev.h>
31
32 #include <pulse/timeval.h>
33
34 #include <pulsecore/modargs.h>
35 #include <pulsecore/core-error.h>
36 #include <pulsecore/core-util.h>
37 #include <pulsecore/namereg.h>
38 #include <pulsecore/ratelimit.h>
39 #include <pulsecore/strbuf.h>
40
41 #include "module-udev-detect-symdef.h"
42
43 PA_MODULE_AUTHOR("Lennart Poettering");
44 PA_MODULE_DESCRIPTION("Detect available audio hardware and load matching drivers");
45 PA_MODULE_VERSION(PACKAGE_VERSION);
46 PA_MODULE_LOAD_ONCE(TRUE);
47 PA_MODULE_USAGE(
48 "tsched=<enable system timer based scheduling mode?> "
49 "tsched_buffer_size=<buffer size when using timer based scheduling> "
50 "fixed_latency_range=<disable latency range changes on underrun?> "
51 "ignore_dB=<ignore dB information from the device?> "
52 "deferred_volume=<syncronize sw and hw volume changes in IO-thread?> "
53 "use_ucm=<use ALSA UCM for card configuration?>");
54
55 struct device {
56 char *path;
57 pa_bool_t need_verify;
58 char *card_name;
59 char *args;
60 uint32_t module;
61 pa_ratelimit ratelimit;
62 };
63
64 struct userdata {
65 pa_core *core;
66 pa_hashmap *devices;
67
68 pa_bool_t use_tsched:1;
69 bool tsched_buffer_size_valid:1;
70 pa_bool_t fixed_latency_range:1;
71 pa_bool_t ignore_dB:1;
72 pa_bool_t deferred_volume:1;
73 bool use_ucm:1;
74
75 uint32_t tsched_buffer_size;
76
77 struct udev* udev;
78 struct udev_monitor *monitor;
79 pa_io_event *udev_io;
80
81 int inotify_fd;
82 pa_io_event *inotify_io;
83 };
84
85 static const char* const valid_modargs[] = {
86 "tsched",
87 "tsched_buffer_size",
88 "fixed_latency_range",
89 "ignore_dB",
90 "deferred_volume",
91 "use_ucm",
92 NULL
93 };
94
95 static int setup_inotify(struct userdata *u);
96
97 static void device_free(struct device *d) {
98 pa_assert(d);
99
100 pa_xfree(d->path);
101 pa_xfree(d->card_name);
102 pa_xfree(d->args);
103 pa_xfree(d);
104 }
105
106 static const char *path_get_card_id(const char *path) {
107 const char *e;
108
109 if (!path)
110 return NULL;
111
112 if (!(e = strrchr(path, '/')))
113 return NULL;
114
115 if (!pa_startswith(e, "/card"))
116 return NULL;
117
118 return e + 5;
119 }
120
121 static char *card_get_sysattr(const char *card_idx, const char *name) {
122 struct udev *udev;
123 struct udev_device *card = NULL;
124 char *t, *r = NULL;
125 const char *v;
126
127 pa_assert(card_idx);
128 pa_assert(name);
129
130 if (!(udev = udev_new())) {
131 pa_log_error("Failed to allocate udev context.");
132 goto finish;
133 }
134
135 t = pa_sprintf_malloc("/sys/class/sound/card%s", card_idx);
136 card = udev_device_new_from_syspath(udev, t);
137 pa_xfree(t);
138
139 if (!card) {
140 pa_log_error("Failed to get card object.");
141 goto finish;
142 }
143
144 if ((v = udev_device_get_sysattr_value(card, name)) && *v)
145 r = pa_xstrdup(v);
146
147 finish:
148
149 if (card)
150 udev_device_unref(card);
151
152 if (udev)
153 udev_unref(udev);
154
155 return r;
156 }
157
158 static pa_bool_t pcm_is_modem(const char *card_idx, const char *pcm) {
159 char *sysfs_path, *pcm_class;
160 pa_bool_t is_modem;
161
162 pa_assert(card_idx);
163 pa_assert(pcm);
164
165 /* Check /sys/class/sound/card.../pcmC...../pcm_class. An HDA
166 * modem can be used simultaneously with generic
167 * playback/record. */
168
169 sysfs_path = pa_sprintf_malloc("pcmC%sD%s/pcm_class", card_idx, pcm);
170 pcm_class = card_get_sysattr(card_idx, sysfs_path);
171 is_modem = pcm_class && pa_streq(pcm_class, "modem");
172 pa_xfree(pcm_class);
173 pa_xfree(sysfs_path);
174
175 return is_modem;
176 }
177
178 static pa_bool_t is_card_busy(const char *id) {
179 char *card_path = NULL, *pcm_path = NULL, *sub_status = NULL;
180 DIR *card_dir = NULL, *pcm_dir = NULL;
181 FILE *status_file = NULL;
182 size_t len;
183 struct dirent *space = NULL, *de;
184 pa_bool_t busy = FALSE;
185 int r;
186
187 pa_assert(id);
188
189 /* This simply uses /proc/asound/card.../pcm.../sub.../status to
190 * check whether there is still a process using this audio device. */
191
192 card_path = pa_sprintf_malloc("/proc/asound/card%s", id);
193
194 if (!(card_dir = opendir(card_path))) {
195 pa_log_warn("Failed to open %s: %s", card_path, pa_cstrerror(errno));
196 goto fail;
197 }
198
199 len = offsetof(struct dirent, d_name) + fpathconf(dirfd(card_dir), _PC_NAME_MAX) + 1;
200 space = pa_xmalloc(len);
201
202 for (;;) {
203 de = NULL;
204
205 if ((r = readdir_r(card_dir, space, &de)) != 0) {
206 pa_log_warn("readdir_r() failed: %s", pa_cstrerror(r));
207 goto fail;
208 }
209
210 if (!de)
211 break;
212
213 if (!pa_startswith(de->d_name, "pcm"))
214 continue;
215
216 if (pcm_is_modem(id, de->d_name + 3))
217 continue;
218
219 pa_xfree(pcm_path);
220 pcm_path = pa_sprintf_malloc("%s/%s", card_path, de->d_name);
221
222 if (pcm_dir)
223 closedir(pcm_dir);
224
225 if (!(pcm_dir = opendir(pcm_path))) {
226 pa_log_warn("Failed to open %s: %s", pcm_path, pa_cstrerror(errno));
227 continue;
228 }
229
230 for (;;) {
231 char line[32];
232
233 if ((r = readdir_r(pcm_dir, space, &de)) != 0) {
234 pa_log_warn("readdir_r() failed: %s", pa_cstrerror(r));
235 goto fail;
236 }
237
238 if (!de)
239 break;
240
241 if (!pa_startswith(de->d_name, "sub"))
242 continue;
243
244 pa_xfree(sub_status);
245 sub_status = pa_sprintf_malloc("%s/%s/status", pcm_path, de->d_name);
246
247 if (status_file)
248 fclose(status_file);
249
250 if (!(status_file = pa_fopen_cloexec(sub_status, "r"))) {
251 pa_log_warn("Failed to open %s: %s", sub_status, pa_cstrerror(errno));
252 continue;
253 }
254
255 if (!(fgets(line, sizeof(line)-1, status_file))) {
256 pa_log_warn("Failed to read from %s: %s", sub_status, pa_cstrerror(errno));
257 continue;
258 }
259
260 if (!pa_streq(line, "closed\n")) {
261 busy = TRUE;
262 break;
263 }
264 }
265 }
266
267 fail:
268
269 pa_xfree(card_path);
270 pa_xfree(pcm_path);
271 pa_xfree(sub_status);
272 pa_xfree(space);
273
274 if (card_dir)
275 closedir(card_dir);
276
277 if (pcm_dir)
278 closedir(pcm_dir);
279
280 if (status_file)
281 fclose(status_file);
282
283 return busy;
284 }
285
286 static void verify_access(struct userdata *u, struct device *d) {
287 char *cd;
288 pa_card *card;
289 pa_bool_t accessible;
290
291 pa_assert(u);
292 pa_assert(d);
293
294 cd = pa_sprintf_malloc("/dev/snd/controlC%s", path_get_card_id(d->path));
295 accessible = access(cd, R_OK|W_OK) >= 0;
296 pa_log_debug("%s is accessible: %s", cd, pa_yes_no(accessible));
297
298 pa_xfree(cd);
299
300 if (d->module == PA_INVALID_INDEX) {
301
302 /* If we are not loaded, try to load */
303
304 if (accessible) {
305 pa_module *m;
306 pa_bool_t busy;
307
308 /* Check if any of the PCM devices that belong to this
309 * card are currently busy. If they are, don't try to load
310 * right now, to make sure the probing phase can
311 * successfully complete. When the current user of the
312 * device closes it we will get another notification via
313 * inotify and can then recheck. */
314
315 busy = is_card_busy(path_get_card_id(d->path));
316 pa_log_debug("%s is busy: %s", d->path, pa_yes_no(busy));
317
318 if (!busy) {
319
320 /* So, why do we rate limit here? It's certainly ugly,
321 * but there seems to be no other way. Problem is
322 * this: if we are unable to configure/probe an audio
323 * device after opening it we will close it again and
324 * the module initialization will fail. This will then
325 * cause an inotify event on the device node which
326 * will be forwarded to us. We then try to reopen the
327 * audio device again, practically entering a busy
328 * loop.
329 *
330 * A clean fix would be if we would be able to ignore
331 * our own inotify close events. However, inotify
332 * lacks such functionality. Also, during probing of
333 * the device we cannot really distinguish between
334 * other processes causing EBUSY or ourselves, which
335 * means we have no way to figure out if the probing
336 * during opening was canceled by a "try again"
337 * failure or a "fatal" failure. */
338
339 if (pa_ratelimit_test(&d->ratelimit, PA_LOG_DEBUG)) {
340 pa_log_debug("Loading module-alsa-card with arguments '%s'", d->args);
341 m = pa_module_load(u->core, "module-alsa-card", d->args);
342
343 if (m) {
344 d->module = m->index;
345 pa_log_info("Card %s (%s) module loaded.", d->path, d->card_name);
346 } else
347 pa_log_info("Card %s (%s) failed to load module.", d->path, d->card_name);
348 } else
349 pa_log_warn("Tried to configure %s (%s) more often than %u times in %llus",
350 d->path,
351 d->card_name,
352 d->ratelimit.burst,
353 (long long unsigned) (d->ratelimit.interval / PA_USEC_PER_SEC));
354 }
355 }
356
357 } else {
358
359 /* If we are already loaded update suspend status with
360 * accessible boolean */
361
362 if ((card = pa_namereg_get(u->core, d->card_name, PA_NAMEREG_CARD))) {
363 pa_log_debug("%s all sinks and sources of card %s.", accessible ? "Resuming" : "Suspending", d->card_name);
364 pa_card_suspend(card, !accessible, PA_SUSPEND_SESSION);
365 }
366 }
367 }
368
369 static void card_changed(struct userdata *u, struct udev_device *dev) {
370 struct device *d;
371 const char *path;
372 const char *t;
373 char *n;
374 pa_strbuf *args_buf;
375
376 pa_assert(u);
377 pa_assert(dev);
378
379 /* Maybe /dev/snd is now available? */
380 setup_inotify(u);
381
382 path = udev_device_get_devpath(dev);
383
384 if ((d = pa_hashmap_get(u->devices, path))) {
385 verify_access(u, d);
386 return;
387 }
388
389 d = pa_xnew0(struct device, 1);
390 d->path = pa_xstrdup(path);
391 d->module = PA_INVALID_INDEX;
392 PA_INIT_RATELIMIT(d->ratelimit, 10*PA_USEC_PER_SEC, 5);
393
394 if (!(t = udev_device_get_property_value(dev, "PULSE_NAME")))
395 if (!(t = udev_device_get_property_value(dev, "ID_ID")))
396 if (!(t = udev_device_get_property_value(dev, "ID_PATH")))
397 t = path_get_card_id(path);
398
399 n = pa_namereg_make_valid_name(t);
400 d->card_name = pa_sprintf_malloc("alsa_card.%s", n);
401 args_buf = pa_strbuf_new();
402 pa_strbuf_printf(args_buf,
403 "device_id=\"%s\" "
404 "name=\"%s\" "
405 "card_name=\"%s\" "
406 "namereg_fail=false "
407 "tsched=%s "
408 "fixed_latency_range=%s "
409 "ignore_dB=%s "
410 "deferred_volume=%s "
411 "use_ucm=%s "
412 "card_properties=\"module-udev-detect.discovered=1\"",
413 path_get_card_id(path),
414 n,
415 d->card_name,
416 pa_yes_no(u->use_tsched),
417 pa_yes_no(u->fixed_latency_range),
418 pa_yes_no(u->ignore_dB),
419 pa_yes_no(u->deferred_volume),
420 pa_yes_no(u->use_ucm));
421 pa_xfree(n);
422
423 if (u->tsched_buffer_size_valid)
424 pa_strbuf_printf(args_buf, " tsched_buffer_size=%" PRIu32, u->tsched_buffer_size);
425
426 d->args = pa_strbuf_tostring_free(args_buf);
427
428 pa_hashmap_put(u->devices, d->path, d);
429
430 verify_access(u, d);
431 }
432
433 static void remove_card(struct userdata *u, struct udev_device *dev) {
434 struct device *d;
435
436 pa_assert(u);
437 pa_assert(dev);
438
439 if (!(d = pa_hashmap_remove(u->devices, udev_device_get_devpath(dev))))
440 return;
441
442 pa_log_info("Card %s removed.", d->path);
443
444 if (d->module != PA_INVALID_INDEX)
445 pa_module_unload_request_by_index(u->core, d->module, TRUE);
446
447 device_free(d);
448 }
449
450 static void process_device(struct userdata *u, struct udev_device *dev) {
451 const char *action, *ff;
452
453 pa_assert(u);
454 pa_assert(dev);
455
456 if (udev_device_get_property_value(dev, "PULSE_IGNORE")) {
457 pa_log_debug("Ignoring %s, because marked so.", udev_device_get_devpath(dev));
458 return;
459 }
460
461 if ((ff = udev_device_get_property_value(dev, "SOUND_CLASS")) &&
462 pa_streq(ff, "modem")) {
463 pa_log_debug("Ignoring %s, because it is a modem.", udev_device_get_devpath(dev));
464 return;
465 }
466
467 action = udev_device_get_action(dev);
468
469 if (action && pa_streq(action, "remove"))
470 remove_card(u, dev);
471 else if ((!action || pa_streq(action, "change")) && udev_device_get_property_value(dev, "SOUND_INITIALIZED"))
472 card_changed(u, dev);
473
474 /* For an explanation why we don't look for 'add' events here
475 * have a look into /lib/udev/rules.d/78-sound-card.rules! */
476 }
477
478 static void process_path(struct userdata *u, const char *path) {
479 struct udev_device *dev;
480
481 if (!path_get_card_id(path))
482 return;
483
484 if (!(dev = udev_device_new_from_syspath(u->udev, path))) {
485 pa_log("Failed to get udev device object from udev.");
486 return;
487 }
488
489 process_device(u, dev);
490 udev_device_unref(dev);
491 }
492
493 static void monitor_cb(
494 pa_mainloop_api*a,
495 pa_io_event* e,
496 int fd,
497 pa_io_event_flags_t events,
498 void *userdata) {
499
500 struct userdata *u = userdata;
501 struct udev_device *dev;
502
503 pa_assert(a);
504
505 if (!(dev = udev_monitor_receive_device(u->monitor))) {
506 pa_log("Failed to get udev device object from monitor.");
507 goto fail;
508 }
509
510 if (!path_get_card_id(udev_device_get_devpath(dev))) {
511 udev_device_unref(dev);
512 return;
513 }
514
515 process_device(u, dev);
516 udev_device_unref(dev);
517 return;
518
519 fail:
520 a->io_free(u->udev_io);
521 u->udev_io = NULL;
522 }
523
524 static pa_bool_t pcm_node_belongs_to_device(
525 struct device *d,
526 const char *node) {
527
528 char *cd;
529 pa_bool_t b;
530
531 cd = pa_sprintf_malloc("pcmC%sD", path_get_card_id(d->path));
532 b = pa_startswith(node, cd);
533 pa_xfree(cd);
534
535 return b;
536 }
537
538 static pa_bool_t control_node_belongs_to_device(
539 struct device *d,
540 const char *node) {
541
542 char *cd;
543 pa_bool_t b;
544
545 cd = pa_sprintf_malloc("controlC%s", path_get_card_id(d->path));
546 b = pa_streq(node, cd);
547 pa_xfree(cd);
548
549 return b;
550 }
551
552 static void inotify_cb(
553 pa_mainloop_api*a,
554 pa_io_event* e,
555 int fd,
556 pa_io_event_flags_t events,
557 void *userdata) {
558
559 struct {
560 struct inotify_event e;
561 char name[NAME_MAX];
562 } buf;
563 struct userdata *u = userdata;
564 static int type = 0;
565 pa_bool_t deleted = FALSE;
566 struct device *d;
567 void *state;
568
569 for (;;) {
570 ssize_t r;
571 struct inotify_event *event;
572
573 pa_zero(buf);
574 if ((r = pa_read(fd, &buf, sizeof(buf), &type)) <= 0) {
575
576 if (r < 0 && errno == EAGAIN)
577 break;
578
579 pa_log("read() from inotify failed: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
580 goto fail;
581 }
582
583 event = &buf.e;
584 while (r > 0) {
585 size_t len;
586
587 if ((size_t) r < sizeof(struct inotify_event)) {
588 pa_log("read() too short.");
589 goto fail;
590 }
591
592 len = sizeof(struct inotify_event) + event->len;
593
594 if ((size_t) r < len) {
595 pa_log("Payload missing.");
596 goto fail;
597 }
598
599 /* From udev we get the guarantee that the control
600 * device's ACL is changed last. To avoid races when ACLs
601 * are changed we hence watch only the control device */
602 if (((event->mask & IN_ATTRIB) && pa_startswith(event->name, "controlC")))
603 PA_HASHMAP_FOREACH(d, u->devices, state)
604 if (control_node_belongs_to_device(d, event->name))
605 d->need_verify = TRUE;
606
607 /* ALSA doesn't really give us any guarantee on the closing
608 * order, so let's simply hope */
609 if (((event->mask & IN_CLOSE_WRITE) && pa_startswith(event->name, "pcmC")))
610 PA_HASHMAP_FOREACH(d, u->devices, state)
611 if (pcm_node_belongs_to_device(d, event->name))
612 d->need_verify = TRUE;
613
614 /* /dev/snd/ might have been removed */
615 if ((event->mask & (IN_DELETE_SELF|IN_MOVE_SELF)))
616 deleted = TRUE;
617
618 event = (struct inotify_event*) ((uint8_t*) event + len);
619 r -= len;
620 }
621 }
622
623 PA_HASHMAP_FOREACH(d, u->devices, state)
624 if (d->need_verify) {
625 d->need_verify = FALSE;
626 verify_access(u, d);
627 }
628
629 if (!deleted)
630 return;
631
632 fail:
633 if (u->inotify_io) {
634 a->io_free(u->inotify_io);
635 u->inotify_io = NULL;
636 }
637
638 if (u->inotify_fd >= 0) {
639 pa_close(u->inotify_fd);
640 u->inotify_fd = -1;
641 }
642 }
643
644 static int setup_inotify(struct userdata *u) {
645 int r;
646
647 if (u->inotify_fd >= 0)
648 return 0;
649
650 if ((u->inotify_fd = inotify_init1(IN_CLOEXEC|IN_NONBLOCK)) < 0) {
651 pa_log("inotify_init1() failed: %s", pa_cstrerror(errno));
652 return -1;
653 }
654
655 r = inotify_add_watch(u->inotify_fd, "/dev/snd", IN_ATTRIB|IN_CLOSE_WRITE|IN_DELETE_SELF|IN_MOVE_SELF);
656
657 if (r < 0) {
658 int saved_errno = errno;
659
660 pa_close(u->inotify_fd);
661 u->inotify_fd = -1;
662
663 if (saved_errno == ENOENT) {
664 pa_log_debug("/dev/snd/ is apparently not existing yet, retrying to create inotify watch later.");
665 return 0;
666 }
667
668 if (saved_errno == ENOSPC) {
669 pa_log("You apparently ran out of inotify watches, probably because Tracker/Beagle took them all away. "
670 "I wished people would do their homework first and fix inotify before using it for watching whole "
671 "directory trees which is something the current inotify is certainly not useful for. "
672 "Please make sure to drop the Tracker/Beagle guys a line complaining about their broken use of inotify.");
673 return 0;
674 }
675
676 pa_log("inotify_add_watch() failed: %s", pa_cstrerror(saved_errno));
677 return -1;
678 }
679
680 pa_assert_se(u->inotify_io = u->core->mainloop->io_new(u->core->mainloop, u->inotify_fd, PA_IO_EVENT_INPUT, inotify_cb, u));
681
682 return 0;
683 }
684
685 int pa__init(pa_module *m) {
686 struct userdata *u = NULL;
687 pa_modargs *ma;
688 struct udev_enumerate *enumerate = NULL;
689 struct udev_list_entry *item = NULL, *first = NULL;
690 int fd;
691 pa_bool_t use_tsched = TRUE, fixed_latency_range = FALSE, ignore_dB = FALSE, deferred_volume = m->core->deferred_volume;
692 bool use_ucm = true;
693
694 pa_assert(m);
695
696 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
697 pa_log("Failed to parse module arguments");
698 goto fail;
699 }
700
701 m->userdata = u = pa_xnew0(struct userdata, 1);
702 u->core = m->core;
703 u->devices = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
704 u->inotify_fd = -1;
705
706 if (pa_modargs_get_value_boolean(ma, "tsched", &use_tsched) < 0) {
707 pa_log("Failed to parse tsched= argument.");
708 goto fail;
709 }
710 u->use_tsched = use_tsched;
711
712 if (pa_modargs_get_value(ma, "tsched_buffer_size", NULL)) {
713 if (pa_modargs_get_value_u32(ma, "tsched_buffer_size", &u->tsched_buffer_size) < 0) {
714 pa_log("Failed to parse tsched_buffer_size= argument.");
715 goto fail;
716 }
717
718 u->tsched_buffer_size_valid = true;
719 }
720
721 if (pa_modargs_get_value_boolean(ma, "fixed_latency_range", &fixed_latency_range) < 0) {
722 pa_log("Failed to parse fixed_latency_range= argument.");
723 goto fail;
724 }
725 u->fixed_latency_range = fixed_latency_range;
726
727 if (pa_modargs_get_value_boolean(ma, "ignore_dB", &ignore_dB) < 0) {
728 pa_log("Failed to parse ignore_dB= argument.");
729 goto fail;
730 }
731 u->ignore_dB = ignore_dB;
732
733 if (pa_modargs_get_value_boolean(ma, "deferred_volume", &deferred_volume) < 0) {
734 pa_log("Failed to parse deferred_volume= argument.");
735 goto fail;
736 }
737 u->deferred_volume = deferred_volume;
738
739 if (pa_modargs_get_value_boolean(ma, "use_ucm", &use_ucm) < 0) {
740 pa_log("Failed to parse use_ucm= argument.");
741 goto fail;
742 }
743 u->use_ucm = use_ucm;
744
745 if (!(u->udev = udev_new())) {
746 pa_log("Failed to initialize udev library.");
747 goto fail;
748 }
749
750 if (setup_inotify(u) < 0)
751 goto fail;
752
753 if (!(u->monitor = udev_monitor_new_from_netlink(u->udev, "udev"))) {
754 pa_log("Failed to initialize monitor.");
755 goto fail;
756 }
757
758 if (udev_monitor_filter_add_match_subsystem_devtype(u->monitor, "sound", NULL) < 0) {
759 pa_log("Failed to subscribe to sound devices.");
760 goto fail;
761 }
762
763 errno = 0;
764 if (udev_monitor_enable_receiving(u->monitor) < 0) {
765 pa_log("Failed to enable monitor: %s", pa_cstrerror(errno));
766 if (errno == EPERM)
767 pa_log_info("Most likely your kernel is simply too old and "
768 "allows only privileged processes to listen to device events. "
769 "Please upgrade your kernel to at least 2.6.30.");
770 goto fail;
771 }
772
773 if ((fd = udev_monitor_get_fd(u->monitor)) < 0) {
774 pa_log("Failed to get udev monitor fd.");
775 goto fail;
776 }
777
778 pa_assert_se(u->udev_io = u->core->mainloop->io_new(u->core->mainloop, fd, PA_IO_EVENT_INPUT, monitor_cb, u));
779
780 if (!(enumerate = udev_enumerate_new(u->udev))) {
781 pa_log("Failed to initialize udev enumerator.");
782 goto fail;
783 }
784
785 if (udev_enumerate_add_match_subsystem(enumerate, "sound") < 0) {
786 pa_log("Failed to match to subsystem.");
787 goto fail;
788 }
789
790 if (udev_enumerate_scan_devices(enumerate) < 0) {
791 pa_log("Failed to scan for devices.");
792 goto fail;
793 }
794
795 first = udev_enumerate_get_list_entry(enumerate);
796 udev_list_entry_foreach(item, first)
797 process_path(u, udev_list_entry_get_name(item));
798
799 udev_enumerate_unref(enumerate);
800
801 pa_log_info("Found %u cards.", pa_hashmap_size(u->devices));
802
803 pa_modargs_free(ma);
804
805 return 0;
806
807 fail:
808
809 if (enumerate)
810 udev_enumerate_unref(enumerate);
811
812 if (ma)
813 pa_modargs_free(ma);
814
815 pa__done(m);
816
817 return -1;
818 }
819
820 void pa__done(pa_module *m) {
821 struct userdata *u;
822
823 pa_assert(m);
824
825 if (!(u = m->userdata))
826 return;
827
828 if (u->udev_io)
829 m->core->mainloop->io_free(u->udev_io);
830
831 if (u->monitor)
832 udev_monitor_unref(u->monitor);
833
834 if (u->udev)
835 udev_unref(u->udev);
836
837 if (u->inotify_io)
838 m->core->mainloop->io_free(u->inotify_io);
839
840 if (u->inotify_fd >= 0)
841 pa_close(u->inotify_fd);
842
843 if (u->devices)
844 pa_hashmap_free(u->devices, (pa_free_cb_t) device_free);
845
846 pa_xfree(u);
847 }