]> code.delx.au - pulseaudio/blob - src/modules/module-udev-detect.c
Merge commit 'origin/master-tx'
[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;
49 char *card_name;
50 uint32_t module;
51 };
52
53 struct userdata {
54 pa_core *core;
55 pa_hashmap *devices;
56
57 pa_bool_t use_tsched:1;
58 pa_bool_t ignore_dB:1;
59
60 struct udev* udev;
61 struct udev_monitor *monitor;
62 pa_io_event *udev_io;
63
64 int inotify_fd;
65 pa_io_event *inotify_io;
66 };
67
68 static const char* const valid_modargs[] = {
69 "tsched",
70 "ignore_dB",
71 NULL
72 };
73
74 static int setup_inotify(struct userdata *u);
75
76 static void device_free(struct device *d) {
77 pa_assert(d);
78
79 pa_xfree(d->path);
80 pa_xfree(d->card_name);
81 pa_xfree(d);
82 }
83
84 static const char *path_get_card_id(const char *path) {
85 const char *e;
86
87 if (!path)
88 return NULL;
89
90 if (!(e = strrchr(path, '/')))
91 return NULL;
92
93 if (!pa_startswith(e, "/card"))
94 return NULL;
95
96 return e + 5;
97 }
98
99 static void verify_access(struct userdata *u, struct device *d) {
100 char *cd;
101 pa_card *card;
102
103 pa_assert(u);
104 pa_assert(d);
105
106 if (!(card = pa_namereg_get(u->core, d->card_name, PA_NAMEREG_CARD)))
107 return;
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, W_OK) >= 0;
111 pa_log_info("%s is accessible: %s", cd, pa_yes_no(d->accessible));
112 pa_xfree(cd);
113
114 pa_card_suspend(card, !d->accessible, PA_SUSPEND_SESSION);
115 }
116
117 static void card_changed(struct userdata *u, struct udev_device *dev) {
118 struct device *d;
119 const char *path;
120 const char *t;
121 char *card_name, *args;
122 pa_module *m;
123 char *n;
124
125 pa_assert(u);
126 pa_assert(dev);
127
128 /* Maybe /dev/snd is now available? */
129 setup_inotify(u);
130
131 path = udev_device_get_devpath(dev);
132
133 if ((d = pa_hashmap_get(u->devices, path))) {
134 verify_access(u, d);
135 return;
136 }
137
138 if (!(t = udev_device_get_property_value(dev, "PULSE_NAME")))
139 if (!(t = udev_device_get_property_value(dev, "ID_ID")))
140 if (!(t = udev_device_get_property_value(dev, "ID_PATH")))
141 t = path_get_card_id(path);
142
143 n = pa_namereg_make_valid_name(t);
144
145 card_name = pa_sprintf_malloc("alsa_card.%s", n);
146 args = pa_sprintf_malloc("device_id=\"%s\" "
147 "name=\"%s\" "
148 "card_name=\"%s\" "
149 "tsched=%s "
150 "ignore_dB=%s "
151 "card_properties=\"module-udev-detect.discovered=1\"",
152 path_get_card_id(path),
153 n,
154 card_name,
155 pa_yes_no(u->use_tsched),
156 pa_yes_no(u->ignore_dB));
157
158 pa_log_debug("Loading module-alsa-card with arguments '%s'", args);
159 m = pa_module_load(u->core, "module-alsa-card", args);
160 pa_xfree(args);
161
162 if (m) {
163 pa_log_info("Card %s (%s) added.", path, n);
164
165 d = pa_xnew(struct device, 1);
166 d->path = pa_xstrdup(path);
167 d->card_name = card_name;
168 d->module = m->index;
169 d->accessible = TRUE;
170
171 pa_hashmap_put(u->devices, d->path, d);
172 } else
173 pa_xfree(card_name);
174
175 pa_xfree(n);
176 }
177
178 static void remove_card(struct userdata *u, struct udev_device *dev) {
179 struct device *d;
180
181 pa_assert(u);
182 pa_assert(dev);
183
184 if (!(d = pa_hashmap_remove(u->devices, udev_device_get_devpath(dev))))
185 return;
186
187 pa_log_info("Card %s removed.", d->path);
188 pa_module_unload_request_by_index(u->core, d->module, TRUE);
189 device_free(d);
190 }
191
192 static void process_device(struct userdata *u, struct udev_device *dev) {
193 const char *action, *ff;
194
195 pa_assert(u);
196 pa_assert(dev);
197
198 if (udev_device_get_property_value(dev, "PULSE_IGNORE")) {
199 pa_log_debug("Ignoring %s, because marked so.", udev_device_get_devpath(dev));
200 return;
201 }
202
203 if ((ff = udev_device_get_property_value(dev, "SOUND_FORM_FACTOR")) &&
204 pa_streq(ff, "modem")) {
205 pa_log_debug("Ignoring %s, because it is a modem.", udev_device_get_devpath(dev));
206 return;
207 }
208
209 action = udev_device_get_action(dev);
210
211 if (action && pa_streq(action, "remove"))
212 remove_card(u, dev);
213 else if ((!action || pa_streq(action, "change")) &&
214 udev_device_get_property_value(dev, "SOUND_INITIALIZED"))
215 card_changed(u, dev);
216
217 /* For an explanation why we don't look for 'add' events here
218 * have a look into /lib/udev/rules.d/78-sound-card.rules! */
219 }
220
221 static void process_path(struct userdata *u, const char *path) {
222 struct udev_device *dev;
223
224 if (!path_get_card_id(path))
225 return;
226
227 if (!(dev = udev_device_new_from_syspath(u->udev, path))) {
228 pa_log("Failed to get udev device object from udev.");
229 return;
230 }
231
232 process_device(u, dev);
233 udev_device_unref(dev);
234 }
235
236 static void monitor_cb(
237 pa_mainloop_api*a,
238 pa_io_event* e,
239 int fd,
240 pa_io_event_flags_t events,
241 void *userdata) {
242
243 struct userdata *u = userdata;
244 struct udev_device *dev;
245
246 pa_assert(a);
247
248 if (!(dev = udev_monitor_receive_device(u->monitor))) {
249 pa_log("Failed to get udev device object from monitor.");
250 goto fail;
251 }
252
253 if (!path_get_card_id(udev_device_get_devpath(dev)))
254 return;
255
256 process_device(u, dev);
257 udev_device_unref(dev);
258 return;
259
260 fail:
261 a->io_free(u->udev_io);
262 u->udev_io = NULL;
263 }
264
265 static void inotify_cb(
266 pa_mainloop_api*a,
267 pa_io_event* e,
268 int fd,
269 pa_io_event_flags_t events,
270 void *userdata) {
271
272 struct {
273 struct inotify_event e;
274 char name[NAME_MAX];
275 } buf;
276 struct userdata *u = userdata;
277 static int type = 0;
278 pa_bool_t verify = FALSE, deleted = FALSE;
279
280 for (;;) {
281 ssize_t r;
282
283 pa_zero(buf);
284 if ((r = pa_read(fd, &buf, sizeof(buf), &type)) <= 0) {
285
286 if (r < 0 && errno == EAGAIN)
287 break;
288
289 pa_log("read() from inotify failed: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
290 goto fail;
291 }
292
293 if ((buf.e.mask & IN_CLOSE_WRITE) && pa_startswith(buf.e.name, "pcmC"))
294 verify = TRUE;
295
296 if ((buf.e.mask & (IN_DELETE_SELF|IN_MOVE_SELF)))
297 deleted = TRUE;
298 }
299
300 if (verify) {
301 struct device *d;
302 void *state;
303
304 pa_log_debug("Verifying access.");
305
306 PA_HASHMAP_FOREACH(d, u->devices, state)
307 verify_access(u, d);
308 }
309
310 if (!deleted)
311 return;
312
313 fail:
314 if (u->inotify_io) {
315 a->io_free(u->inotify_io);
316 u->inotify_io = NULL;
317 }
318
319 if (u->inotify_fd >= 0) {
320 pa_close(u->inotify_fd);
321 u->inotify_fd = -1;
322 }
323 }
324
325 static int setup_inotify(struct userdata *u) {
326 char *dev_snd;
327 int r;
328
329 if (u->inotify_fd >= 0)
330 return 0;
331
332 if ((u->inotify_fd = inotify_init1(IN_CLOEXEC|IN_NONBLOCK)) < 0) {
333 pa_log("inotify_init1() failed: %s", pa_cstrerror(errno));
334 return -1;
335 }
336
337 dev_snd = pa_sprintf_malloc("%s/snd", udev_get_dev_path(u->udev));
338 r = inotify_add_watch(u->inotify_fd, dev_snd, IN_CLOSE_WRITE|IN_DELETE_SELF|IN_MOVE_SELF);
339 pa_xfree(dev_snd);
340
341 if (r < 0) {
342 int saved_errno = errno;
343
344 pa_close(u->inotify_fd);
345 u->inotify_fd = -1;
346
347 if (saved_errno == ENOENT) {
348 pa_log_debug("/dev/snd/ is apparently not existing yet, retrying to create inotify watch later.");
349 return 0;
350 }
351
352 if (saved_errno == ENOSPC) {
353 pa_log("You apparently ran out of inotify watches, probably because Tracker/Beagle took them all away. "
354 "I wished people would do their homework first and fix inotify before using it for watching whole "
355 "directory trees which is something the current inotify is certainly not useful for. "
356 "Please make sure to drop the Tracker/Beagle guys a line complaining about their broken use of inotify.");
357 return 0;
358 }
359
360 pa_log("inotify_add_watch() failed: %s", pa_cstrerror(saved_errno));
361 return -1;
362 }
363
364 pa_assert_se(u->inotify_io = u->core->mainloop->io_new(u->core->mainloop, u->inotify_fd, PA_IO_EVENT_INPUT, inotify_cb, u));
365
366 return 0;
367 }
368
369 int pa__init(pa_module *m) {
370 struct userdata *u = NULL;
371 pa_modargs *ma;
372 struct udev_enumerate *enumerate = NULL;
373 struct udev_list_entry *item = NULL, *first = NULL;
374 int fd;
375 pa_bool_t use_tsched = TRUE, ignore_dB = FALSE;
376
377 pa_assert(m);
378
379 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
380 pa_log("Failed to parse module arguments");
381 goto fail;
382 }
383
384 m->userdata = u = pa_xnew0(struct userdata, 1);
385 u->core = m->core;
386 u->devices = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
387 u->inotify_fd = -1;
388
389 if (pa_modargs_get_value_boolean(ma, "tsched", &use_tsched) < 0) {
390 pa_log("Failed to parse tsched= argument.");
391 goto fail;
392 }
393 u->use_tsched = use_tsched;
394
395 if (pa_modargs_get_value_boolean(ma, "ignore_dB", &ignore_dB) < 0) {
396 pa_log("Failed to parse ignore_dB= argument.");
397 goto fail;
398 }
399 u->ignore_dB = ignore_dB;
400
401 if (!(u->udev = udev_new())) {
402 pa_log("Failed to initialize udev library.");
403 goto fail;
404 }
405
406 if (setup_inotify(u) < 0)
407 goto fail;
408
409 if (!(u->monitor = udev_monitor_new_from_netlink(u->udev, "udev"))) {
410 pa_log("Failed to initialize monitor.");
411 goto fail;
412 }
413
414 errno = 0;
415 if (udev_monitor_enable_receiving(u->monitor) < 0) {
416 pa_log("Failed to enable monitor: %s", pa_cstrerror(errno));
417 if (errno == EPERM)
418 pa_log_info("Most likely your kernel is simply too old and "
419 "allows only priviliged processes to listen to device events. "
420 "Please upgrade your kernel to at least 2.6.30.");
421 goto fail;
422 }
423
424 if ((fd = udev_monitor_get_fd(u->monitor)) < 0) {
425 pa_log("Failed to get udev monitor fd.");
426 goto fail;
427 }
428
429 pa_assert_se(u->udev_io = u->core->mainloop->io_new(u->core->mainloop, fd, PA_IO_EVENT_INPUT, monitor_cb, u));
430
431 if (!(enumerate = udev_enumerate_new(u->udev))) {
432 pa_log("Failed to initialize udev enumerator.");
433 goto fail;
434 }
435
436 if (udev_enumerate_add_match_subsystem(enumerate, "sound") < 0) {
437 pa_log("Failed to match to subsystem.");
438 goto fail;
439 }
440
441 if (udev_enumerate_scan_devices(enumerate) < 0) {
442 pa_log("Failed to scan for devices.");
443 goto fail;
444 }
445
446 first = udev_enumerate_get_list_entry(enumerate);
447 udev_list_entry_foreach(item, first)
448 process_path(u, udev_list_entry_get_name(item));
449
450 udev_enumerate_unref(enumerate);
451
452 pa_log_info("Loaded %u modules.", pa_hashmap_size(u->devices));
453
454 pa_modargs_free(ma);
455
456 return 0;
457
458 fail:
459
460 if (enumerate)
461 udev_enumerate_unref(enumerate);
462
463 if (ma)
464 pa_modargs_free(ma);
465
466 pa__done(m);
467
468 return -1;
469 }
470
471 void pa__done(pa_module *m) {
472 struct userdata *u;
473
474 pa_assert(m);
475
476 if (!(u = m->userdata))
477 return;
478
479 if (u->udev_io)
480 m->core->mainloop->io_free(u->udev_io);
481
482 if (u->monitor)
483 udev_monitor_unref(u->monitor);
484
485 if (u->udev)
486 udev_unref(u->udev);
487
488 if (u->inotify_io)
489 m->core->mainloop->io_free(u->inotify_io);
490
491 if (u->inotify_fd >= 0)
492 pa_close(u->inotify_fd);
493
494 if (u->devices) {
495 struct device *d;
496
497 while ((d = pa_hashmap_steal_first(u->devices)))
498 device_free(d);
499
500 pa_hashmap_free(u->devices, NULL, NULL);
501 }
502
503 pa_xfree(u);
504 }