]> code.delx.au - pulseaudio/blob - src/modules/module-udev-detect.c
udev: process all inotify events queued up, not just the first one in the queue
[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 <sys/inotify.h>
29 #include <libudev.h>
30
31 #include <pulsecore/modargs.h>
32 #include <pulsecore/core-error.h>
33 #include <pulsecore/core-util.h>
34 #include <pulsecore/namereg.h>
35
36 #include "module-udev-detect-symdef.h"
37
38 PA_MODULE_AUTHOR("Lennart Poettering");
39 PA_MODULE_DESCRIPTION("Detect available audio hardware and load matching drivers");
40 PA_MODULE_VERSION(PACKAGE_VERSION);
41 PA_MODULE_LOAD_ONCE(TRUE);
42 PA_MODULE_USAGE(
43 "tsched=<enable system timer based scheduling mode?> "
44 "ignore_dB=<ignore dB information from the device?>");
45
46 struct device {
47 char *path;
48 pa_bool_t accessible:1;
49 pa_bool_t need_verify:1;
50 char *card_name;
51 char *args;
52 uint32_t module;
53 };
54
55 struct userdata {
56 pa_core *core;
57 pa_hashmap *devices;
58
59 pa_bool_t use_tsched:1;
60 pa_bool_t ignore_dB:1;
61
62 struct udev* udev;
63 struct udev_monitor *monitor;
64 pa_io_event *udev_io;
65
66 int inotify_fd;
67 pa_io_event *inotify_io;
68 };
69
70 static const char* const valid_modargs[] = {
71 "tsched",
72 "ignore_dB",
73 NULL
74 };
75
76 static int setup_inotify(struct userdata *u);
77
78 static void device_free(struct device *d) {
79 pa_assert(d);
80
81 pa_xfree(d->path);
82 pa_xfree(d->card_name);
83 pa_xfree(d->args);
84 pa_xfree(d);
85 }
86
87 static const char *path_get_card_id(const char *path) {
88 const char *e;
89
90 if (!path)
91 return NULL;
92
93 if (!(e = strrchr(path, '/')))
94 return NULL;
95
96 if (!pa_startswith(e, "/card"))
97 return NULL;
98
99 return e + 5;
100 }
101
102 static void verify_access(struct userdata *u, struct device *d) {
103 char *cd;
104 pa_card *card;
105
106 pa_assert(u);
107 pa_assert(d);
108
109 cd = pa_sprintf_malloc("%s/snd/controlC%s", udev_get_dev_path(u->udev), path_get_card_id(d->path));
110 d->accessible = access(cd, R_OK|W_OK) >= 0;
111
112 pa_log_info("%s is accessible: %s", cd, pa_yes_no(d->accessible));
113 pa_xfree(cd);
114
115 if (d->module == PA_INVALID_INDEX) {
116
117 /* If we not loaded, try to load */
118
119 if (d->accessible) {
120 pa_module *m;
121
122 pa_log_debug("Loading module-alsa-card with arguments '%s'", d->args);
123 m = pa_module_load(u->core, "module-alsa-card", d->args);
124
125 if (m) {
126 d->module = m->index;
127 pa_log_info("Card %s (%s) module loaded.", d->path, d->card_name);
128 } else
129 pa_log_info("Card %s (%s) failed to load module.", d->path, d->card_name);
130 }
131
132 } else {
133
134 /* If we are already loaded update suspend status with
135 * accessible boolean */
136
137 if ((card = pa_namereg_get(u->core, d->card_name, PA_NAMEREG_CARD)))
138 pa_card_suspend(card, !d->accessible, PA_SUSPEND_SESSION);
139 }
140 }
141
142 static void card_changed(struct userdata *u, struct udev_device *dev) {
143 struct device *d;
144 const char *path;
145 const char *t;
146 char *n;
147
148 pa_assert(u);
149 pa_assert(dev);
150
151 /* Maybe /dev/snd is now available? */
152 setup_inotify(u);
153
154 path = udev_device_get_devpath(dev);
155
156 if ((d = pa_hashmap_get(u->devices, path))) {
157 verify_access(u, d);
158 return;
159 }
160
161 d = pa_xnew0(struct device, 1);
162 d->path = pa_xstrdup(path);
163 d->accessible = TRUE;
164 d->module = PA_INVALID_INDEX;
165
166 if (!(t = udev_device_get_property_value(dev, "PULSE_NAME")))
167 if (!(t = udev_device_get_property_value(dev, "ID_ID")))
168 if (!(t = udev_device_get_property_value(dev, "ID_PATH")))
169 t = path_get_card_id(path);
170
171 n = pa_namereg_make_valid_name(t);
172 d->card_name = pa_sprintf_malloc("alsa_card.%s", n);
173 d->args = pa_sprintf_malloc("device_id=\"%s\" "
174 "name=\"%s\" "
175 "card_name=\"%s\" "
176 "tsched=%s "
177 "ignore_dB=%s "
178 "card_properties=\"module-udev-detect.discovered=1\"",
179 path_get_card_id(path),
180 n,
181 d->card_name,
182 pa_yes_no(u->use_tsched),
183 pa_yes_no(u->ignore_dB));
184 pa_xfree(n);
185
186 pa_hashmap_put(u->devices, d->path, d);
187
188 verify_access(u, d);
189 }
190
191 static void remove_card(struct userdata *u, struct udev_device *dev) {
192 struct device *d;
193
194 pa_assert(u);
195 pa_assert(dev);
196
197 if (!(d = pa_hashmap_remove(u->devices, udev_device_get_devpath(dev))))
198 return;
199
200 pa_log_info("Card %s removed.", d->path);
201
202 if (d->module != PA_INVALID_INDEX)
203 pa_module_unload_request_by_index(u->core, d->module, TRUE);
204
205 device_free(d);
206 }
207
208 static void process_device(struct userdata *u, struct udev_device *dev) {
209 const char *action, *ff;
210
211 pa_assert(u);
212 pa_assert(dev);
213
214 if (udev_device_get_property_value(dev, "PULSE_IGNORE")) {
215 pa_log_debug("Ignoring %s, because marked so.", udev_device_get_devpath(dev));
216 return;
217 }
218
219 if ((ff = udev_device_get_property_value(dev, "SOUND_FORM_FACTOR")) &&
220 pa_streq(ff, "modem")) {
221 pa_log_debug("Ignoring %s, because it is a modem.", udev_device_get_devpath(dev));
222 return;
223 }
224
225 action = udev_device_get_action(dev);
226
227 if (action && pa_streq(action, "remove"))
228 remove_card(u, dev);
229 else if ((!action || pa_streq(action, "change")) &&
230 udev_device_get_property_value(dev, "SOUND_INITIALIZED"))
231 card_changed(u, dev);
232
233 /* For an explanation why we don't look for 'add' events here
234 * have a look into /lib/udev/rules.d/78-sound-card.rules! */
235 }
236
237 static void process_path(struct userdata *u, const char *path) {
238 struct udev_device *dev;
239
240 if (!path_get_card_id(path))
241 return;
242
243 if (!(dev = udev_device_new_from_syspath(u->udev, path))) {
244 pa_log("Failed to get udev device object from udev.");
245 return;
246 }
247
248 process_device(u, dev);
249 udev_device_unref(dev);
250 }
251
252 static void monitor_cb(
253 pa_mainloop_api*a,
254 pa_io_event* e,
255 int fd,
256 pa_io_event_flags_t events,
257 void *userdata) {
258
259 struct userdata *u = userdata;
260 struct udev_device *dev;
261
262 pa_assert(a);
263
264 if (!(dev = udev_monitor_receive_device(u->monitor))) {
265 pa_log("Failed to get udev device object from monitor.");
266 goto fail;
267 }
268
269 if (!path_get_card_id(udev_device_get_devpath(dev)))
270 return;
271
272 process_device(u, dev);
273 udev_device_unref(dev);
274 return;
275
276 fail:
277 a->io_free(u->udev_io);
278 u->udev_io = NULL;
279 }
280
281 static pa_bool_t pcm_node_belongs_to_device(
282 struct device *d,
283 const char *node) {
284
285 char *cd;
286 pa_bool_t b;
287
288 cd = pa_sprintf_malloc("pcmC%sD", path_get_card_id(d->path));
289 b = pa_startswith(node, cd);
290 pa_xfree(cd);
291
292 return b;
293 }
294
295 static pa_bool_t control_node_belongs_to_device(
296 struct device *d,
297 const char *node) {
298
299 char *cd;
300 pa_bool_t b;
301
302 cd = pa_sprintf_malloc("controlC%s", path_get_card_id(d->path));
303 b = pa_streq(node, cd);
304 pa_xfree(cd);
305
306 return b;
307 }
308
309 static void inotify_cb(
310 pa_mainloop_api*a,
311 pa_io_event* e,
312 int fd,
313 pa_io_event_flags_t events,
314 void *userdata) {
315
316 struct {
317 struct inotify_event e;
318 char name[NAME_MAX];
319 } buf;
320 struct userdata *u = userdata;
321 static int type = 0;
322 pa_bool_t deleted = FALSE;
323 struct device *d;
324 void *state;
325
326 for (;;) {
327 ssize_t r;
328 struct inotify_event *event;
329
330 pa_zero(buf);
331 if ((r = pa_read(fd, &buf, sizeof(buf), &type)) <= 0) {
332
333 if (r < 0 && errno == EAGAIN)
334 break;
335
336 pa_log("read() from inotify failed: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
337 goto fail;
338 }
339
340 event = &buf.e;
341 while (r > 0) {
342 size_t len;
343
344 if ((size_t) r < sizeof(struct inotify_event)) {
345 pa_log("read() too short.");
346 goto fail;
347 }
348
349 len = sizeof(struct inotify_event) + event->len;
350
351 if ((size_t) r < len) {
352 pa_log("Payload missing.");
353 goto fail;
354 }
355
356 /* From udev we get the guarantee that the control
357 * device's ACL is changed last. To avoid races when ACLs
358 * are changed we hence watch only the control device */
359 if (((event->mask & IN_ATTRIB) && pa_startswith(event->name, "controlC")))
360 PA_HASHMAP_FOREACH(d, u->devices, state)
361 if (control_node_belongs_to_device(d, event->name))
362 d->need_verify = TRUE;
363
364 /* ALSA doesn't really give us any guarantee on the closing
365 * order, so let's simply hope */
366 if (((event->mask & IN_CLOSE_WRITE) && pa_startswith(event->name, "pcmC")))
367 PA_HASHMAP_FOREACH(d, u->devices, state)
368 if (pcm_node_belongs_to_device(d, event->name))
369 d->need_verify = TRUE;
370
371 /* /dev/snd/ might have been removed */
372 if ((event->mask & (IN_DELETE_SELF|IN_MOVE_SELF)))
373 deleted = TRUE;
374
375 event = (struct inotify_event*) ((uint8_t*) event + len);
376 r -= len;
377 }
378 }
379
380 PA_HASHMAP_FOREACH(d, u->devices, state)
381 if (d->need_verify) {
382 d->need_verify = FALSE;
383 verify_access(u, d);
384 }
385
386 if (!deleted)
387 return;
388
389 fail:
390 if (u->inotify_io) {
391 a->io_free(u->inotify_io);
392 u->inotify_io = NULL;
393 }
394
395 if (u->inotify_fd >= 0) {
396 pa_close(u->inotify_fd);
397 u->inotify_fd = -1;
398 }
399 }
400
401 static int setup_inotify(struct userdata *u) {
402 char *dev_snd;
403 int r;
404
405 if (u->inotify_fd >= 0)
406 return 0;
407
408 if ((u->inotify_fd = inotify_init1(IN_CLOEXEC|IN_NONBLOCK)) < 0) {
409 pa_log("inotify_init1() failed: %s", pa_cstrerror(errno));
410 return -1;
411 }
412
413 dev_snd = pa_sprintf_malloc("%s/snd", udev_get_dev_path(u->udev));
414 r = inotify_add_watch(u->inotify_fd, dev_snd, IN_ATTRIB|IN_CLOSE_WRITE|IN_DELETE_SELF|IN_MOVE_SELF);
415 pa_xfree(dev_snd);
416
417 if (r < 0) {
418 int saved_errno = errno;
419
420 pa_close(u->inotify_fd);
421 u->inotify_fd = -1;
422
423 if (saved_errno == ENOENT) {
424 pa_log_debug("/dev/snd/ is apparently not existing yet, retrying to create inotify watch later.");
425 return 0;
426 }
427
428 if (saved_errno == ENOSPC) {
429 pa_log("You apparently ran out of inotify watches, probably because Tracker/Beagle took them all away. "
430 "I wished people would do their homework first and fix inotify before using it for watching whole "
431 "directory trees which is something the current inotify is certainly not useful for. "
432 "Please make sure to drop the Tracker/Beagle guys a line complaining about their broken use of inotify.");
433 return 0;
434 }
435
436 pa_log("inotify_add_watch() failed: %s", pa_cstrerror(saved_errno));
437 return -1;
438 }
439
440 pa_assert_se(u->inotify_io = u->core->mainloop->io_new(u->core->mainloop, u->inotify_fd, PA_IO_EVENT_INPUT, inotify_cb, u));
441
442 return 0;
443 }
444
445 int pa__init(pa_module *m) {
446 struct userdata *u = NULL;
447 pa_modargs *ma;
448 struct udev_enumerate *enumerate = NULL;
449 struct udev_list_entry *item = NULL, *first = NULL;
450 int fd;
451 pa_bool_t use_tsched = TRUE, ignore_dB = FALSE;
452
453 pa_assert(m);
454
455 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
456 pa_log("Failed to parse module arguments");
457 goto fail;
458 }
459
460 m->userdata = u = pa_xnew0(struct userdata, 1);
461 u->core = m->core;
462 u->devices = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
463 u->inotify_fd = -1;
464
465 if (pa_modargs_get_value_boolean(ma, "tsched", &use_tsched) < 0) {
466 pa_log("Failed to parse tsched= argument.");
467 goto fail;
468 }
469 u->use_tsched = use_tsched;
470
471 if (pa_modargs_get_value_boolean(ma, "ignore_dB", &ignore_dB) < 0) {
472 pa_log("Failed to parse ignore_dB= argument.");
473 goto fail;
474 }
475 u->ignore_dB = ignore_dB;
476
477 if (!(u->udev = udev_new())) {
478 pa_log("Failed to initialize udev library.");
479 goto fail;
480 }
481
482 if (setup_inotify(u) < 0)
483 goto fail;
484
485 if (!(u->monitor = udev_monitor_new_from_netlink(u->udev, "udev"))) {
486 pa_log("Failed to initialize monitor.");
487 goto fail;
488 }
489
490 errno = 0;
491 if (udev_monitor_enable_receiving(u->monitor) < 0) {
492 pa_log("Failed to enable monitor: %s", pa_cstrerror(errno));
493 if (errno == EPERM)
494 pa_log_info("Most likely your kernel is simply too old and "
495 "allows only priviliged processes to listen to device events. "
496 "Please upgrade your kernel to at least 2.6.30.");
497 goto fail;
498 }
499
500 if ((fd = udev_monitor_get_fd(u->monitor)) < 0) {
501 pa_log("Failed to get udev monitor fd.");
502 goto fail;
503 }
504
505 pa_assert_se(u->udev_io = u->core->mainloop->io_new(u->core->mainloop, fd, PA_IO_EVENT_INPUT, monitor_cb, u));
506
507 if (!(enumerate = udev_enumerate_new(u->udev))) {
508 pa_log("Failed to initialize udev enumerator.");
509 goto fail;
510 }
511
512 if (udev_enumerate_add_match_subsystem(enumerate, "sound") < 0) {
513 pa_log("Failed to match to subsystem.");
514 goto fail;
515 }
516
517 if (udev_enumerate_scan_devices(enumerate) < 0) {
518 pa_log("Failed to scan for devices.");
519 goto fail;
520 }
521
522 first = udev_enumerate_get_list_entry(enumerate);
523 udev_list_entry_foreach(item, first)
524 process_path(u, udev_list_entry_get_name(item));
525
526 udev_enumerate_unref(enumerate);
527
528 pa_log_info("Found %u cards.", pa_hashmap_size(u->devices));
529
530 pa_modargs_free(ma);
531
532 return 0;
533
534 fail:
535
536 if (enumerate)
537 udev_enumerate_unref(enumerate);
538
539 if (ma)
540 pa_modargs_free(ma);
541
542 pa__done(m);
543
544 return -1;
545 }
546
547 void pa__done(pa_module *m) {
548 struct userdata *u;
549
550 pa_assert(m);
551
552 if (!(u = m->userdata))
553 return;
554
555 if (u->udev_io)
556 m->core->mainloop->io_free(u->udev_io);
557
558 if (u->monitor)
559 udev_monitor_unref(u->monitor);
560
561 if (u->udev)
562 udev_unref(u->udev);
563
564 if (u->inotify_io)
565 m->core->mainloop->io_free(u->inotify_io);
566
567 if (u->inotify_fd >= 0)
568 pa_close(u->inotify_fd);
569
570 if (u->devices) {
571 struct device *d;
572
573 while ((d = pa_hashmap_steal_first(u->devices)))
574 device_free(d);
575
576 pa_hashmap_free(u->devices, NULL, NULL);
577 }
578
579 pa_xfree(u);
580 }