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