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