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