2 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Alex Hornung <ahornung@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
41 #include <sys/ioccom.h>
42 #include <sys/malloc.h>
43 #include <sys/ctype.h>
44 #include <sys/syslog.h>
46 #include <sys/devfs.h>
47 #include <libprop/proplib.h>
49 MALLOC_DEFINE(M_UDEV, "udev", "udev allocs");
51 /* XXX: use UUIDs for identification; would need help from devfs */
53 static cdev_t udev_dev;
54 static d_open_t udev_dev_open;
55 static d_close_t udev_dev_close;
56 static d_read_t udev_dev_read;
57 static d_poll_t udev_dev_poll;
58 static d_ioctl_t udev_dev_ioctl;
60 static int _udev_dict_set_cstr(prop_dictionary_t, const char *, char *);
61 static int _udev_dict_set_int(prop_dictionary_t, const char *, int64_t);
62 static int _udev_dict_set_uint(prop_dictionary_t, const char *, uint64_t);
63 static int _udev_dict_delete_key(prop_dictionary_t, const char *);
64 static prop_dictionary_t udev_init_dict_event(cdev_t, const char *);
65 static int udev_init_dict(cdev_t);
66 static int udev_destroy_dict(cdev_t);
67 static void udev_event_insert(int, prop_dictionary_t);
68 static struct udev_event_kernel *udev_event_remove(void);
69 static void udev_event_free(struct udev_event_kernel *);
70 static char *udev_event_externalize(struct udev_event_kernel *);
71 static void udev_getdevs_scan_callback(cdev_t, void *);
72 static int udev_getdevs_ioctl(struct plistref *, u_long, prop_dictionary_t);
76 int (*fn)(struct plistref *, u_long, prop_dictionary_t);
79 struct udev_prop_ctx {
84 struct udev_event_kernel {
86 TAILQ_ENTRY(udev_event_kernel) link;
97 TAILQ_HEAD(, udev_event_kernel) ev_queue; /* list of thread_io */
100 static struct dev_ops udev_dev_ops = {
102 .d_open = udev_dev_open,
103 .d_close = udev_dev_close,
104 .d_read = udev_dev_read,
105 .d_poll = udev_dev_poll,
106 .d_ioctl = udev_dev_ioctl
109 static struct cmd_function cmd_fn[] = {
110 { .cmd = "getdevs", .fn = udev_getdevs_ioctl},
115 _udev_dict_set_cstr(prop_dictionary_t dict, const char *key, char *str)
119 KKASSERT(dict != NULL);
121 ps = prop_string_create_cstring(str);
126 if (prop_dictionary_set(dict, key, ps) == false) {
127 prop_object_release(ps);
131 prop_object_release(ps);
136 _udev_dict_set_int(prop_dictionary_t dict, const char *key, int64_t val)
140 KKASSERT(dict != NULL);
142 pn = prop_number_create_integer(val);
146 if (prop_dictionary_set(dict, key, pn) == false) {
147 prop_object_release(pn);
151 prop_object_release(pn);
156 _udev_dict_set_uint(prop_dictionary_t dict, const char *key, uint64_t val)
160 KKASSERT(dict != NULL);
162 pn = prop_number_create_unsigned_integer(val);
166 if (prop_dictionary_set(dict, key, pn) == false) {
167 prop_object_release(pn);
171 prop_object_release(pn);
176 _udev_dict_delete_key(prop_dictionary_t dict, const char *key)
178 KKASSERT(dict != NULL);
180 prop_dictionary_remove(dict, key);
186 * Initialize an event dictionary, which contains three parameters to
187 * identify the device referred to (name, devnum, kptr) and the affected key.
189 static prop_dictionary_t
190 udev_init_dict_event(cdev_t dev, const char *key)
192 prop_dictionary_t dict;
196 kptr = (uint64_t)(uintptr_t)dev;
197 KKASSERT(dev != NULL);
199 dict = prop_dictionary_create();
201 log(LOG_DEBUG, "udev_init_dict_event: prop_dictionary_create() failed\n");
205 if ((error = _udev_dict_set_cstr(dict, "name", dev->si_name)))
207 if ((error = _udev_dict_set_uint(dict, "devnum", dev->si_inode)))
209 if ((error = _udev_dict_set_uint(dict, "kptr", kptr)))
211 if ((error = _udev_dict_set_cstr(dict, "key", __DECONST(char *, key))))
217 prop_object_release(dict);
222 udev_dict_set_cstr(cdev_t dev, const char *key, char *str)
224 prop_dictionary_t dict;
227 KKASSERT(dev != NULL);
229 if (dev->si_dict == NULL) {
230 error = udev_init_dict(dev);
235 /* Queue a key update event */
236 dict = udev_init_dict_event(dev, key);
240 if ((error = _udev_dict_set_cstr(dict, "value", str))) {
241 prop_object_release(dict);
244 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
245 prop_object_release(dict);
247 error = _udev_dict_set_cstr(dev->si_dict, key, str);
252 udev_dict_set_int(cdev_t dev, const char *key, int64_t val)
254 prop_dictionary_t dict;
257 KKASSERT(dev != NULL);
259 if (dev->si_dict == NULL) {
260 error = udev_init_dict(dev);
265 /* Queue a key update event */
266 dict = udev_init_dict_event(dev, key);
269 if ((error = _udev_dict_set_int(dict, "value", val))) {
270 prop_object_release(dict);
273 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
274 prop_object_release(dict);
276 return _udev_dict_set_int(dev->si_dict, key, val);
280 udev_dict_set_uint(cdev_t dev, const char *key, uint64_t val)
282 prop_dictionary_t dict;
285 KKASSERT(dev != NULL);
287 if (dev->si_dict == NULL) {
288 error = udev_init_dict(dev);
293 /* Queue a key update event */
294 dict = udev_init_dict_event(dev, key);
297 if ((error = _udev_dict_set_uint(dict, "value", val))) {
298 prop_object_release(dict);
301 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
302 prop_object_release(dict);
304 return _udev_dict_set_uint(dev->si_dict, key, val);
308 udev_dict_delete_key(cdev_t dev, const char *key)
310 prop_dictionary_t dict;
312 KKASSERT(dev != NULL);
314 /* Queue a key removal event */
315 dict = udev_init_dict_event(dev, key);
318 udev_event_insert(UDEV_EV_KEY_REMOVE, dict);
319 prop_object_release(dict);
321 return _udev_dict_delete_key(dev->si_dict, key);
325 udev_init_dict(cdev_t dev)
327 prop_dictionary_t dict;
331 kptr = (uint64_t)(uintptr_t)dev;
333 KKASSERT(dev != NULL);
335 if (dev->si_dict != NULL) {
338 "udev_init_dict: new dict for %s, but has dict already (%p)!\n",
339 dev->si_name, dev->si_dict);
344 dict = prop_dictionary_create();
346 log(LOG_DEBUG, "udev_init_dict: prop_dictionary_create() failed\n");
350 if ((error = _udev_dict_set_cstr(dict, "name", dev->si_name)))
352 if ((error = _udev_dict_set_uint(dict, "devnum", dev->si_inode)))
354 if ((error = _udev_dict_set_uint(dict, "kptr", kptr)))
357 /* XXX: The next 3 are marginallly useful, if at all */
358 if ((error = _udev_dict_set_uint(dict, "uid", dev->si_uid)))
360 if ((error = _udev_dict_set_uint(dict, "gid", dev->si_gid)))
362 if ((error = _udev_dict_set_int(dict, "mode", dev->si_perms)))
365 if ((error = _udev_dict_set_int(dict, "major", umajor(dev->si_inode))))
367 if ((error = _udev_dict_set_int(dict, "minor", dev->si_uminor)))
369 if (dev->si_ops->head.name != NULL) {
370 if ((error = _udev_dict_set_cstr(dict, "driver",
371 __DECONST(char *, dev->si_ops->head.name))))
380 prop_object_release(dict);
385 udev_destroy_dict(cdev_t dev)
387 KKASSERT(dev != NULL);
389 if (dev->si_dict != NULL) {
390 prop_object_release(dev->si_dict);
398 udev_event_insert(int ev_type, prop_dictionary_t dict)
400 struct udev_event_kernel *ev;
402 /* Only start queing events after client has initiated properly */
403 if (!udevctx.initiated)
406 /* XXX: use objcache eventually */
407 ev = kmalloc(sizeof(*ev), M_UDEV, M_WAITOK);
408 ev->ev.ev_dict = prop_dictionary_copy(dict);
409 if (ev->ev.ev_dict == NULL) {
413 ev->ev.ev_type = ev_type;
415 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
416 TAILQ_INSERT_TAIL(&udevctx.ev_queue, ev, link);
418 lockmgr(&udevctx.lock, LK_RELEASE);
421 selwakeup(&udevctx.sel);
424 static struct udev_event_kernel *
425 udev_event_remove(void)
427 struct udev_event_kernel *ev;
429 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
430 if (TAILQ_EMPTY(&udevctx.ev_queue)) {
431 lockmgr(&udevctx.lock, LK_RELEASE);
435 ev = TAILQ_FIRST(&udevctx.ev_queue);
436 TAILQ_REMOVE(&udevctx.ev_queue, ev, link);
438 lockmgr(&udevctx.lock, LK_RELEASE);
444 udev_event_free(struct udev_event_kernel *ev)
446 /* XXX: use objcache eventually */
451 udev_event_externalize(struct udev_event_kernel *ev)
453 prop_dictionary_t dict;
458 dict = prop_dictionary_create();
460 log(LOG_DEBUG, "udev_event_externalize: prop_dictionary_create() failed\n");
464 if ((error = _udev_dict_set_int(dict, "evtype", ev->ev.ev_type))) {
465 prop_object_release(dict);
469 if (prop_dictionary_set(dict, "evdict", ev->ev.ev_dict) == false) {
470 prop_object_release(dict);
474 prop_object_release(ev->ev.ev_dict);
476 xml = prop_dictionary_externalize(dict);
478 prop_object_release(dict);
484 udev_event_attach(cdev_t dev, char *name, int alias)
486 prop_dictionary_t dict;
489 KKASSERT(dev != NULL);
494 dict = prop_dictionary_copy(dev->si_dict);
498 if ((error = _udev_dict_set_cstr(dict, "name", name))) {
499 prop_object_release(dict);
503 _udev_dict_set_int(dict, "alias", 1);
505 udev_event_insert(UDEV_EVENT_ATTACH, dict);
506 prop_object_release(dict);
508 error = udev_init_dict(dev);
512 _udev_dict_set_int(dev->si_dict, "alias", 0);
513 udev_event_insert(UDEV_EVENT_ATTACH, dev->si_dict);
521 udev_event_detach(cdev_t dev, char *name, int alias)
523 prop_dictionary_t dict;
525 KKASSERT(dev != NULL);
528 dict = prop_dictionary_copy(dev->si_dict);
532 if (_udev_dict_set_cstr(dict, "name", name)) {
533 prop_object_release(dict);
537 _udev_dict_set_int(dict, "alias", 1);
539 udev_event_insert(UDEV_EVENT_DETACH, dict);
540 prop_object_release(dict);
542 udev_event_insert(UDEV_EVENT_DETACH, dev->si_dict);
546 udev_destroy_dict(dev);
555 udev_dev_open(struct dev_open_args *ap)
566 udev_dev_close(struct dev_close_args *ap)
569 udevctx.initiated = 0;
576 udev_dev_poll(struct dev_poll_args *ap)
580 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
581 if (ap->a_events & (POLLIN | POLLRDNORM)) {
582 if (!TAILQ_EMPTY(&udevctx.ev_queue))
583 revents = ap->a_events & (POLLIN | POLLRDNORM);
585 selrecord(curthread, &udevctx.sel);
587 lockmgr(&udevctx.lock, LK_RELEASE);
589 ap->a_events = revents;
594 udev_dev_read(struct dev_read_args *ap)
596 struct udev_event_kernel *ev;
597 struct uio *uio = ap->a_uio;
603 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
606 if ((ev = udev_event_remove()) != NULL) {
607 if ((xml = udev_event_externalize(ev)) == NULL) {
608 lockmgr(&udevctx.lock, LK_RELEASE);
612 len = strlen(xml) + 1; /* account for NULL-termination */
613 if (uio->uio_resid < len) {
616 error = uiomove((caddr_t)xml, len, uio);
621 lockmgr(&udevctx.lock, LK_RELEASE);
625 if ((error = lksleep(&udevctx, &udevctx.lock, 0, "udevq", 0))) {
626 lockmgr(&udevctx.lock, LK_RELEASE);
631 lockmgr(&udevctx.lock, LK_RELEASE);
636 udev_dev_ioctl(struct dev_ioctl_args *ap)
638 prop_dictionary_t dict;
641 struct plistref *pref;
648 /* Use proplib(3) for userspace/kernel communication */
649 pref = (struct plistref *)ap->a_data;
650 error = prop_dictionary_copyin_ioctl(pref, ap->a_cmd, &dict);
654 po = prop_dictionary_get(dict, "command");
655 if (po == NULL || prop_object_type(po) != PROP_TYPE_STRING) {
656 log(LOG_DEBUG, "udev: prop_dictionary_get() failed\n");
657 prop_object_release(dict);
663 for(i = 0; cmd_fn[i].cmd != NULL; i++) {
664 if (prop_string_equals_cstring(ps, cmd_fn[i].cmd))
668 if (cmd_fn[i].cmd != NULL) {
669 error = cmd_fn[i].fn(pref, ap->a_cmd, dict);
674 //prop_object_release(po);
675 prop_object_release(dict);
678 error = ENOTTY; /* Inappropriate ioctl for device */
686 udev_getdevs_scan_callback(cdev_t cdev, void *arg)
688 struct udev_prop_ctx *ctx = arg;
690 KKASSERT(arg != NULL);
692 if (cdev->si_dict == NULL)
695 if (prop_array_add(ctx->cdevs, cdev->si_dict) == false) {
702 udev_getdevs_ioctl(struct plistref *pref, u_long cmd, prop_dictionary_t dict)
704 prop_dictionary_t odict;
705 struct udev_prop_ctx ctx;
709 ctx.cdevs = prop_array_create();
710 if (ctx.cdevs == NULL) {
711 log(LOG_DEBUG, "udev_getdevs_ioctl: prop_array_create() failed\n");
715 /* XXX: need devfs_scan_alias_callback() */
716 devfs_scan_callback(udev_getdevs_scan_callback, &ctx);
718 if (ctx.error != 0) {
719 prop_object_release(ctx.cdevs);
722 udevctx.initiated = 1;
724 odict = prop_dictionary_create();
729 if ((prop_dictionary_set(odict, "array", ctx.cdevs)) == 0) {
730 log(LOG_DEBUG, "udev_getdevs_ioctl: prop_dictionary_set failed\n");
731 prop_object_release(odict);
735 error = prop_dictionary_copyout_ioctl(pref, cmd, odict);
737 prop_object_release(odict);
748 lockinit(&udevctx.lock, "udevevq", 0, LK_CANRECURSE);
749 TAILQ_INIT(&udevctx.ev_queue);
760 udev_dev = make_dev(&udev_dev_ops,
769 udev_dev_uninit(void)
771 destroy_dev(udev_dev);
774 SYSINIT(subr_udev_register, SI_SUB_CREATE_INIT, SI_ORDER_ANY, udev_init, NULL);
775 SYSUNINIT(subr_udev_register, SI_SUB_CREATE_INIT, SI_ORDER_ANY, udev_uninit, NULL);
776 SYSINIT(subr_udev_dev_register, SI_SUB_DRIVERS, SI_ORDER_ANY, udev_dev_init, NULL);
777 SYSUNINIT(subr_udev_dev_register, SI_SUB_DRIVERS, SI_ORDER_ANY, udev_dev_uninit, NULL);