bridge.4: Add missing .Bl/.El
[dragonfly.git] / sys / kern / subr_bus.c
blob45b9453f7231639ffaebe8b915a83a09ef096ef3
1 /*
2 * Copyright (c) 1997,1998 Doug Rabson
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
26 * $FreeBSD: src/sys/kern/subr_bus.c,v 1.54.2.9 2002/10/10 15:13:32 jhb Exp $
29 #include "opt_bus.h"
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/malloc.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/kobj.h>
37 #include <sys/bus_private.h>
38 #include <sys/sysctl.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/rman.h>
42 #include <sys/device.h>
43 #include <sys/lock.h>
44 #include <sys/conf.h>
45 #include <sys/uio.h>
46 #include <sys/filio.h>
47 #include <sys/event.h>
48 #include <sys/signalvar.h>
50 #include <machine/stdarg.h> /* for device_printf() */
52 #include <sys/thread2.h>
53 #include <sys/mplock2.h>
55 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
57 MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
59 #ifdef BUS_DEBUG
60 #define PDEBUG(a) (kprintf("%s:%d: ", __func__, __LINE__), kprintf a, kprintf("\n"))
61 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
62 #define DRIVERNAME(d) ((d)? d->name : "no driver")
63 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
65 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to
66 * prevent syslog from deleting initial spaces
68 #define indentprintf(p) do { int iJ; kprintf("."); for (iJ=0; iJ<indent; iJ++) kprintf(" "); kprintf p ; } while(0)
70 static void print_device_short(device_t dev, int indent);
71 static void print_device(device_t dev, int indent);
72 void print_device_tree_short(device_t dev, int indent);
73 void print_device_tree(device_t dev, int indent);
74 static void print_driver_short(driver_t *driver, int indent);
75 static void print_driver(driver_t *driver, int indent);
76 static void print_driver_list(driver_list_t drivers, int indent);
77 static void print_devclass_short(devclass_t dc, int indent);
78 static void print_devclass(devclass_t dc, int indent);
79 void print_devclass_list_short(void);
80 void print_devclass_list(void);
82 #else
83 /* Make the compiler ignore the function calls */
84 #define PDEBUG(a) /* nop */
85 #define DEVICENAME(d) /* nop */
86 #define DRIVERNAME(d) /* nop */
87 #define DEVCLANAME(d) /* nop */
89 #define print_device_short(d,i) /* nop */
90 #define print_device(d,i) /* nop */
91 #define print_device_tree_short(d,i) /* nop */
92 #define print_device_tree(d,i) /* nop */
93 #define print_driver_short(d,i) /* nop */
94 #define print_driver(d,i) /* nop */
95 #define print_driver_list(d,i) /* nop */
96 #define print_devclass_short(d,i) /* nop */
97 #define print_devclass(d,i) /* nop */
98 #define print_devclass_list_short() /* nop */
99 #define print_devclass_list() /* nop */
100 #endif
102 static void device_attach_async(device_t dev);
103 static void device_attach_thread(void *arg);
104 static int device_doattach(device_t dev);
106 static int do_async_attach = 0;
107 static int numasyncthreads;
108 TUNABLE_INT("kern.do_async_attach", &do_async_attach);
111 * /dev/devctl implementation
115 * This design allows only one reader for /dev/devctl. This is not desirable
116 * in the long run, but will get a lot of hair out of this implementation.
117 * Maybe we should make this device a clonable device.
119 * Also note: we specifically do not attach a device to the device_t tree
120 * to avoid potential chicken and egg problems. One could argue that all
121 * of this belongs to the root node. One could also further argue that the
122 * sysctl interface that we have not might more properly be an ioctl
123 * interface, but at this stage of the game, I'm not inclined to rock that
124 * boat.
126 * I'm also not sure that the SIGIO support is done correctly or not, as
127 * I copied it from a driver that had SIGIO support that likely hasn't been
128 * tested since 3.4 or 2.2.8!
131 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
132 static int devctl_disable = 0;
133 TUNABLE_INT("hw.bus.devctl_disable", &devctl_disable);
134 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
135 sysctl_devctl_disable, "I", "devctl disable");
137 static d_open_t devopen;
138 static d_close_t devclose;
139 static d_read_t devread;
140 static d_ioctl_t devioctl;
141 static d_kqfilter_t devkqfilter;
143 static struct dev_ops devctl_ops = {
144 { "devctl", 0, 0 },
145 .d_open = devopen,
146 .d_close = devclose,
147 .d_read = devread,
148 .d_ioctl = devioctl,
149 .d_kqfilter = devkqfilter
152 struct dev_event_info
154 char *dei_data;
155 TAILQ_ENTRY(dev_event_info) dei_link;
158 TAILQ_HEAD(devq, dev_event_info);
160 static struct dev_softc
162 int inuse;
163 int nonblock;
164 struct lock lock;
165 struct kqinfo kq;
166 struct devq devq;
167 struct proc *async_proc;
168 } devsoftc;
170 static void
171 devinit(void)
173 make_dev(&devctl_ops, 0, UID_ROOT, GID_WHEEL, 0600, "devctl");
174 lockinit(&devsoftc.lock, "dev mtx", 0, 0);
175 TAILQ_INIT(&devsoftc.devq);
178 static int
179 devopen(struct dev_open_args *ap)
181 if (devsoftc.inuse)
182 return (EBUSY);
183 /* move to init */
184 devsoftc.inuse = 1;
185 devsoftc.nonblock = 0;
186 devsoftc.async_proc = NULL;
187 return (0);
190 static int
191 devclose(struct dev_close_args *ap)
193 devsoftc.inuse = 0;
194 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
195 wakeup(&devsoftc);
196 lockmgr(&devsoftc.lock, LK_RELEASE);
198 return (0);
202 * The read channel for this device is used to report changes to
203 * userland in realtime. We are required to free the data as well as
204 * the n1 object because we allocate them separately. Also note that
205 * we return one record at a time. If you try to read this device a
206 * character at a time, you will lose the rest of the data. Listening
207 * programs are expected to cope.
209 static int
210 devread(struct dev_read_args *ap)
212 struct uio *uio = ap->a_uio;
213 struct dev_event_info *n1;
214 int rv;
216 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
217 while (TAILQ_EMPTY(&devsoftc.devq)) {
218 if (devsoftc.nonblock) {
219 lockmgr(&devsoftc.lock, LK_RELEASE);
220 return (EAGAIN);
222 tsleep_interlock(&devsoftc, PCATCH);
223 lockmgr(&devsoftc.lock, LK_RELEASE);
224 rv = tsleep(&devsoftc, PCATCH | PINTERLOCKED, "devctl", 0);
225 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
226 if (rv) {
228 * Need to translate ERESTART to EINTR here? -- jake
230 lockmgr(&devsoftc.lock, LK_RELEASE);
231 return (rv);
234 n1 = TAILQ_FIRST(&devsoftc.devq);
235 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
236 lockmgr(&devsoftc.lock, LK_RELEASE);
237 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
238 kfree(n1->dei_data, M_BUS);
239 kfree(n1, M_BUS);
240 return (rv);
243 static int
244 devioctl(struct dev_ioctl_args *ap)
246 switch (ap->a_cmd) {
248 case FIONBIO:
249 if (*(int*)ap->a_data)
250 devsoftc.nonblock = 1;
251 else
252 devsoftc.nonblock = 0;
253 return (0);
254 case FIOASYNC:
255 if (*(int*)ap->a_data)
256 devsoftc.async_proc = curproc;
257 else
258 devsoftc.async_proc = NULL;
259 return (0);
261 /* (un)Support for other fcntl() calls. */
262 case FIOCLEX:
263 case FIONCLEX:
264 case FIONREAD:
265 case FIOSETOWN:
266 case FIOGETOWN:
267 default:
268 break;
270 return (ENOTTY);
273 static void dev_filter_detach(struct knote *);
274 static int dev_filter_read(struct knote *, long);
276 static struct filterops dev_filtops =
277 { FILTEROP_ISFD, NULL, dev_filter_detach, dev_filter_read };
279 static int
280 devkqfilter(struct dev_kqfilter_args *ap)
282 struct knote *kn = ap->a_kn;
283 struct klist *klist;
285 ap->a_result = 0;
286 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
288 switch (kn->kn_filter) {
289 case EVFILT_READ:
290 kn->kn_fop = &dev_filtops;
291 break;
292 default:
293 ap->a_result = EOPNOTSUPP;
294 lockmgr(&devsoftc.lock, LK_RELEASE);
295 return (0);
298 klist = &devsoftc.kq.ki_note;
299 knote_insert(klist, kn);
301 lockmgr(&devsoftc.lock, LK_RELEASE);
303 return (0);
306 static void
307 dev_filter_detach(struct knote *kn)
309 struct klist *klist;
311 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
312 klist = &devsoftc.kq.ki_note;
313 knote_remove(klist, kn);
314 lockmgr(&devsoftc.lock, LK_RELEASE);
317 static int
318 dev_filter_read(struct knote *kn, long hint)
320 int ready = 0;
322 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
323 if (!TAILQ_EMPTY(&devsoftc.devq))
324 ready = 1;
325 lockmgr(&devsoftc.lock, LK_RELEASE);
327 return (ready);
332 * @brief Return whether the userland process is running
334 boolean_t
335 devctl_process_running(void)
337 return (devsoftc.inuse == 1);
341 * @brief Queue data to be read from the devctl device
343 * Generic interface to queue data to the devctl device. It is
344 * assumed that @p data is properly formatted. It is further assumed
345 * that @p data is allocated using the M_BUS malloc type.
347 void
348 devctl_queue_data(char *data)
350 struct dev_event_info *n1 = NULL;
351 struct proc *p;
353 n1 = kmalloc(sizeof(*n1), M_BUS, M_NOWAIT);
354 if (n1 == NULL)
355 return;
356 n1->dei_data = data;
357 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
358 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
359 wakeup(&devsoftc);
360 lockmgr(&devsoftc.lock, LK_RELEASE);
361 get_mplock(); /* XXX */
362 KNOTE(&devsoftc.kq.ki_note, 0);
363 rel_mplock(); /* XXX */
364 p = devsoftc.async_proc;
365 if (p != NULL)
366 ksignal(p, SIGIO);
370 * @brief Send a 'notification' to userland, using standard ways
372 void
373 devctl_notify(const char *system, const char *subsystem, const char *type,
374 const char *data)
376 int len = 0;
377 char *msg;
379 if (system == NULL)
380 return; /* BOGUS! Must specify system. */
381 if (subsystem == NULL)
382 return; /* BOGUS! Must specify subsystem. */
383 if (type == NULL)
384 return; /* BOGUS! Must specify type. */
385 len += strlen(" system=") + strlen(system);
386 len += strlen(" subsystem=") + strlen(subsystem);
387 len += strlen(" type=") + strlen(type);
388 /* add in the data message plus newline. */
389 if (data != NULL)
390 len += strlen(data);
391 len += 3; /* '!', '\n', and NUL */
392 msg = kmalloc(len, M_BUS, M_NOWAIT);
393 if (msg == NULL)
394 return; /* Drop it on the floor */
395 if (data != NULL)
396 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
397 system, subsystem, type, data);
398 else
399 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
400 system, subsystem, type);
401 devctl_queue_data(msg);
405 * Common routine that tries to make sending messages as easy as possible.
406 * We allocate memory for the data, copy strings into that, but do not
407 * free it unless there's an error. The dequeue part of the driver should
408 * free the data. We don't send data when the device is disabled. We do
409 * send data, even when we have no listeners, because we wish to avoid
410 * races relating to startup and restart of listening applications.
412 * devaddq is designed to string together the type of event, with the
413 * object of that event, plus the plug and play info and location info
414 * for that event. This is likely most useful for devices, but less
415 * useful for other consumers of this interface. Those should use
416 * the devctl_queue_data() interface instead.
418 static void
419 devaddq(const char *type, const char *what, device_t dev)
421 char *data = NULL;
422 char *loc = NULL;
423 char *pnp = NULL;
424 const char *parstr;
426 if (devctl_disable)
427 return;
428 data = kmalloc(1024, M_BUS, M_NOWAIT);
429 if (data == NULL)
430 goto bad;
432 /* get the bus specific location of this device */
433 loc = kmalloc(1024, M_BUS, M_NOWAIT);
434 if (loc == NULL)
435 goto bad;
436 *loc = '\0';
437 bus_child_location_str(dev, loc, 1024);
439 /* Get the bus specific pnp info of this device */
440 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
441 if (pnp == NULL)
442 goto bad;
443 *pnp = '\0';
444 bus_child_pnpinfo_str(dev, pnp, 1024);
446 /* Get the parent of this device, or / if high enough in the tree. */
447 if (device_get_parent(dev) == NULL)
448 parstr = "."; /* Or '/' ? */
449 else
450 parstr = device_get_nameunit(device_get_parent(dev));
451 /* String it all together. */
452 ksnprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
453 parstr);
454 kfree(loc, M_BUS);
455 kfree(pnp, M_BUS);
456 devctl_queue_data(data);
457 return;
458 bad:
459 kfree(pnp, M_BUS);
460 kfree(loc, M_BUS);
461 kfree(data, M_BUS);
462 return;
466 * A device was added to the tree. We are called just after it successfully
467 * attaches (that is, probe and attach success for this device). No call
468 * is made if a device is merely parented into the tree. See devnomatch
469 * if probe fails. If attach fails, no notification is sent (but maybe
470 * we should have a different message for this).
472 static void
473 devadded(device_t dev)
475 char *pnp = NULL;
476 char *tmp = NULL;
478 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
479 if (pnp == NULL)
480 goto fail;
481 tmp = kmalloc(1024, M_BUS, M_NOWAIT);
482 if (tmp == NULL)
483 goto fail;
484 *pnp = '\0';
485 bus_child_pnpinfo_str(dev, pnp, 1024);
486 ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
487 devaddq("+", tmp, dev);
488 fail:
489 if (pnp != NULL)
490 kfree(pnp, M_BUS);
491 if (tmp != NULL)
492 kfree(tmp, M_BUS);
493 return;
497 * A device was removed from the tree. We are called just before this
498 * happens.
500 static void
501 devremoved(device_t dev)
503 char *pnp = NULL;
504 char *tmp = NULL;
506 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
507 if (pnp == NULL)
508 goto fail;
509 tmp = kmalloc(1024, M_BUS, M_NOWAIT);
510 if (tmp == NULL)
511 goto fail;
512 *pnp = '\0';
513 bus_child_pnpinfo_str(dev, pnp, 1024);
514 ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
515 devaddq("-", tmp, dev);
516 fail:
517 if (pnp != NULL)
518 kfree(pnp, M_BUS);
519 if (tmp != NULL)
520 kfree(tmp, M_BUS);
521 return;
525 * Called when there's no match for this device. This is only called
526 * the first time that no match happens, so we don't keep getitng this
527 * message. Should that prove to be undesirable, we can change it.
528 * This is called when all drivers that can attach to a given bus
529 * decline to accept this device. Other errrors may not be detected.
531 static void
532 devnomatch(device_t dev)
534 devaddq("?", "", dev);
537 static int
538 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
540 struct dev_event_info *n1;
541 int dis, error;
543 dis = devctl_disable;
544 error = sysctl_handle_int(oidp, &dis, 0, req);
545 if (error || !req->newptr)
546 return (error);
547 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
548 devctl_disable = dis;
549 if (dis) {
550 while (!TAILQ_EMPTY(&devsoftc.devq)) {
551 n1 = TAILQ_FIRST(&devsoftc.devq);
552 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
553 kfree(n1->dei_data, M_BUS);
554 kfree(n1, M_BUS);
557 lockmgr(&devsoftc.lock, LK_RELEASE);
558 return (0);
561 /* End of /dev/devctl code */
563 TAILQ_HEAD(,device) bus_data_devices;
564 static int bus_data_generation = 1;
566 kobj_method_t null_methods[] = {
567 { 0, 0 }
570 DEFINE_CLASS(null, null_methods, 0);
573 * Devclass implementation
576 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
578 static devclass_t
579 devclass_find_internal(const char *classname, const char *parentname,
580 int create)
582 devclass_t dc;
584 PDEBUG(("looking for %s", classname));
585 if (classname == NULL)
586 return(NULL);
588 TAILQ_FOREACH(dc, &devclasses, link)
589 if (!strcmp(dc->name, classname))
590 break;
592 if (create && !dc) {
593 PDEBUG(("creating %s", classname));
594 dc = kmalloc(sizeof(struct devclass) + strlen(classname) + 1,
595 M_BUS, M_INTWAIT | M_ZERO);
596 if (!dc)
597 return(NULL);
598 dc->parent = NULL;
599 dc->name = (char*) (dc + 1);
600 strcpy(dc->name, classname);
601 dc->devices = NULL;
602 dc->maxunit = 0;
603 TAILQ_INIT(&dc->drivers);
604 TAILQ_INSERT_TAIL(&devclasses, dc, link);
606 bus_data_generation_update();
611 * If a parent class is specified, then set that as our parent so
612 * that this devclass will support drivers for the parent class as
613 * well. If the parent class has the same name don't do this though
614 * as it creates a cycle that can trigger an infinite loop in
615 * device_probe_child() if a device exists for which there is no
616 * suitable driver.
618 if (parentname && dc && !dc->parent &&
619 strcmp(classname, parentname) != 0)
620 dc->parent = devclass_find_internal(parentname, NULL, FALSE);
622 return(dc);
625 devclass_t
626 devclass_create(const char *classname)
628 return(devclass_find_internal(classname, NULL, TRUE));
631 devclass_t
632 devclass_find(const char *classname)
634 return(devclass_find_internal(classname, NULL, FALSE));
637 device_t
638 devclass_find_unit(const char *classname, int unit)
640 devclass_t dc;
642 if ((dc = devclass_find(classname)) != NULL)
643 return(devclass_get_device(dc, unit));
644 return (NULL);
648 devclass_add_driver(devclass_t dc, driver_t *driver)
650 driverlink_t dl;
651 device_t dev;
652 int i;
654 PDEBUG(("%s", DRIVERNAME(driver)));
656 dl = kmalloc(sizeof *dl, M_BUS, M_INTWAIT | M_ZERO);
657 if (!dl)
658 return(ENOMEM);
661 * Compile the driver's methods. Also increase the reference count
662 * so that the class doesn't get freed when the last instance
663 * goes. This means we can safely use static methods and avoids a
664 * double-free in devclass_delete_driver.
666 kobj_class_instantiate(driver);
669 * Make sure the devclass which the driver is implementing exists.
671 devclass_find_internal(driver->name, NULL, TRUE);
673 dl->driver = driver;
674 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
677 * Call BUS_DRIVER_ADDED for any existing busses in this class,
678 * but only if the bus has already been attached (otherwise we
679 * might probe too early).
681 * This is what will cause a newly loaded module to be associated
682 * with hardware. bus_generic_driver_added() is typically what ends
683 * up being called.
685 for (i = 0; i < dc->maxunit; i++) {
686 if ((dev = dc->devices[i]) != NULL) {
687 if (dev->state >= DS_ATTACHED)
688 BUS_DRIVER_ADDED(dev, driver);
692 bus_data_generation_update();
693 return(0);
697 devclass_delete_driver(devclass_t busclass, driver_t *driver)
699 devclass_t dc = devclass_find(driver->name);
700 driverlink_t dl;
701 device_t dev;
702 int i;
703 int error;
705 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
707 if (!dc)
708 return(0);
711 * Find the link structure in the bus' list of drivers.
713 TAILQ_FOREACH(dl, &busclass->drivers, link)
714 if (dl->driver == driver)
715 break;
717 if (!dl) {
718 PDEBUG(("%s not found in %s list", driver->name, busclass->name));
719 return(ENOENT);
723 * Disassociate from any devices. We iterate through all the
724 * devices in the devclass of the driver and detach any which are
725 * using the driver and which have a parent in the devclass which
726 * we are deleting from.
728 * Note that since a driver can be in multiple devclasses, we
729 * should not detach devices which are not children of devices in
730 * the affected devclass.
732 for (i = 0; i < dc->maxunit; i++)
733 if (dc->devices[i]) {
734 dev = dc->devices[i];
735 if (dev->driver == driver && dev->parent &&
736 dev->parent->devclass == busclass) {
737 if ((error = device_detach(dev)) != 0)
738 return(error);
739 device_set_driver(dev, NULL);
743 TAILQ_REMOVE(&busclass->drivers, dl, link);
744 kfree(dl, M_BUS);
746 kobj_class_uninstantiate(driver);
748 bus_data_generation_update();
749 return(0);
752 static driverlink_t
753 devclass_find_driver_internal(devclass_t dc, const char *classname)
755 driverlink_t dl;
757 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
759 TAILQ_FOREACH(dl, &dc->drivers, link)
760 if (!strcmp(dl->driver->name, classname))
761 return(dl);
763 PDEBUG(("not found"));
764 return(NULL);
767 kobj_class_t
768 devclass_find_driver(devclass_t dc, const char *classname)
770 driverlink_t dl;
772 dl = devclass_find_driver_internal(dc, classname);
773 if (dl)
774 return(dl->driver);
775 else
776 return(NULL);
779 const char *
780 devclass_get_name(devclass_t dc)
782 return(dc->name);
785 device_t
786 devclass_get_device(devclass_t dc, int unit)
788 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
789 return(NULL);
790 return(dc->devices[unit]);
793 void *
794 devclass_get_softc(devclass_t dc, int unit)
796 device_t dev;
798 dev = devclass_get_device(dc, unit);
799 if (!dev)
800 return(NULL);
802 return(device_get_softc(dev));
806 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
808 int i;
809 int count;
810 device_t *list;
812 count = 0;
813 for (i = 0; i < dc->maxunit; i++)
814 if (dc->devices[i])
815 count++;
817 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
818 if (list == NULL)
819 return(ENOMEM);
821 count = 0;
822 for (i = 0; i < dc->maxunit; i++)
823 if (dc->devices[i]) {
824 list[count] = dc->devices[i];
825 count++;
828 *devlistp = list;
829 *devcountp = count;
831 return(0);
835 * @brief Get a list of drivers in the devclass
837 * An array containing a list of pointers to all the drivers in the
838 * given devclass is allocated and returned in @p *listp. The number
839 * of drivers in the array is returned in @p *countp. The caller should
840 * free the array using @c free(p, M_TEMP).
842 * @param dc the devclass to examine
843 * @param listp gives location for array pointer return value
844 * @param countp gives location for number of array elements
845 * return value
847 * @retval 0 success
848 * @retval ENOMEM the array allocation failed
851 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
853 driverlink_t dl;
854 driver_t **list;
855 int count;
857 count = 0;
858 TAILQ_FOREACH(dl, &dc->drivers, link)
859 count++;
860 list = kmalloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
861 if (list == NULL)
862 return (ENOMEM);
864 count = 0;
865 TAILQ_FOREACH(dl, &dc->drivers, link) {
866 list[count] = dl->driver;
867 count++;
869 *listp = list;
870 *countp = count;
872 return (0);
876 * @brief Get the number of devices in a devclass
878 * @param dc the devclass to examine
881 devclass_get_count(devclass_t dc)
883 int count, i;
885 count = 0;
886 for (i = 0; i < dc->maxunit; i++)
887 if (dc->devices[i])
888 count++;
889 return (count);
893 devclass_get_maxunit(devclass_t dc)
895 return(dc->maxunit);
898 void
899 devclass_set_parent(devclass_t dc, devclass_t pdc)
901 dc->parent = pdc;
904 devclass_t
905 devclass_get_parent(devclass_t dc)
907 return(dc->parent);
910 static int
911 devclass_alloc_unit(devclass_t dc, int *unitp)
913 int unit = *unitp;
915 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
917 /* If we have been given a wired unit number, check for existing device */
918 if (unit != -1) {
919 if (unit >= 0 && unit < dc->maxunit &&
920 dc->devices[unit] != NULL) {
921 if (bootverbose)
922 kprintf("%s-: %s%d exists, using next available unit number\n",
923 dc->name, dc->name, unit);
924 /* find the next available slot */
925 while (++unit < dc->maxunit && dc->devices[unit] != NULL)
928 } else {
929 /* Unwired device, find the next available slot for it */
930 unit = 0;
931 while (unit < dc->maxunit && dc->devices[unit] != NULL)
932 unit++;
936 * We've selected a unit beyond the length of the table, so let's
937 * extend the table to make room for all units up to and including
938 * this one.
940 if (unit >= dc->maxunit) {
941 device_t *newlist;
942 int newsize;
944 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
945 newlist = kmalloc(sizeof(device_t) * newsize, M_BUS,
946 M_INTWAIT | M_ZERO);
947 if (newlist == NULL)
948 return(ENOMEM);
949 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
950 if (dc->devices)
951 kfree(dc->devices, M_BUS);
952 dc->devices = newlist;
953 dc->maxunit = newsize;
955 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
957 *unitp = unit;
958 return(0);
961 static int
962 devclass_add_device(devclass_t dc, device_t dev)
964 int buflen, error;
966 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
968 buflen = strlen(dc->name) + 5;
969 dev->nameunit = kmalloc(buflen, M_BUS, M_INTWAIT | M_ZERO);
970 if (!dev->nameunit)
971 return(ENOMEM);
973 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
974 kfree(dev->nameunit, M_BUS);
975 dev->nameunit = NULL;
976 return(error);
978 dc->devices[dev->unit] = dev;
979 dev->devclass = dc;
980 ksnprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
982 return(0);
985 static int
986 devclass_delete_device(devclass_t dc, device_t dev)
988 if (!dc || !dev)
989 return(0);
991 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
993 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
994 panic("devclass_delete_device: inconsistent device class");
995 dc->devices[dev->unit] = NULL;
996 if (dev->flags & DF_WILDCARD)
997 dev->unit = -1;
998 dev->devclass = NULL;
999 kfree(dev->nameunit, M_BUS);
1000 dev->nameunit = NULL;
1002 return(0);
1005 static device_t
1006 make_device(device_t parent, const char *name, int unit)
1008 device_t dev;
1009 devclass_t dc;
1011 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1013 if (name != NULL) {
1014 dc = devclass_find_internal(name, NULL, TRUE);
1015 if (!dc) {
1016 kprintf("make_device: can't find device class %s\n", name);
1017 return(NULL);
1019 } else
1020 dc = NULL;
1022 dev = kmalloc(sizeof(struct device), M_BUS, M_INTWAIT | M_ZERO);
1023 if (!dev)
1024 return(0);
1026 dev->parent = parent;
1027 TAILQ_INIT(&dev->children);
1028 kobj_init((kobj_t) dev, &null_class);
1029 dev->driver = NULL;
1030 dev->devclass = NULL;
1031 dev->unit = unit;
1032 dev->nameunit = NULL;
1033 dev->desc = NULL;
1034 dev->busy = 0;
1035 dev->devflags = 0;
1036 dev->flags = DF_ENABLED;
1037 dev->order = 0;
1038 if (unit == -1)
1039 dev->flags |= DF_WILDCARD;
1040 if (name) {
1041 dev->flags |= DF_FIXEDCLASS;
1042 if (devclass_add_device(dc, dev) != 0) {
1043 kobj_delete((kobj_t)dev, M_BUS);
1044 return(NULL);
1047 dev->ivars = NULL;
1048 dev->softc = NULL;
1050 dev->state = DS_NOTPRESENT;
1052 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1053 bus_data_generation_update();
1055 return(dev);
1058 static int
1059 device_print_child(device_t dev, device_t child)
1061 int retval = 0;
1063 if (device_is_alive(child))
1064 retval += BUS_PRINT_CHILD(dev, child);
1065 else
1066 retval += device_printf(child, " not found\n");
1068 return(retval);
1071 device_t
1072 device_add_child(device_t dev, const char *name, int unit)
1074 return device_add_child_ordered(dev, 0, name, unit);
1077 device_t
1078 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
1080 device_t child;
1081 device_t place;
1083 PDEBUG(("%s at %s with order %d as unit %d", name, DEVICENAME(dev),
1084 order, unit));
1086 child = make_device(dev, name, unit);
1087 if (child == NULL)
1088 return child;
1089 child->order = order;
1091 TAILQ_FOREACH(place, &dev->children, link)
1092 if (place->order > order)
1093 break;
1095 if (place) {
1097 * The device 'place' is the first device whose order is
1098 * greater than the new child.
1100 TAILQ_INSERT_BEFORE(place, child, link);
1101 } else {
1103 * The new child's order is greater or equal to the order of
1104 * any existing device. Add the child to the tail of the list.
1106 TAILQ_INSERT_TAIL(&dev->children, child, link);
1109 bus_data_generation_update();
1110 return(child);
1114 device_delete_child(device_t dev, device_t child)
1116 int error;
1117 device_t grandchild;
1119 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1121 /* remove children first */
1122 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1123 error = device_delete_child(child, grandchild);
1124 if (error)
1125 return(error);
1128 if ((error = device_detach(child)) != 0)
1129 return(error);
1130 if (child->devclass)
1131 devclass_delete_device(child->devclass, child);
1132 TAILQ_REMOVE(&dev->children, child, link);
1133 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1134 device_set_desc(child, NULL);
1135 kobj_delete((kobj_t)child, M_BUS);
1137 bus_data_generation_update();
1138 return(0);
1142 * @brief Find a device given a unit number
1144 * This is similar to devclass_get_devices() but only searches for
1145 * devices which have @p dev as a parent.
1147 * @param dev the parent device to search
1148 * @param unit the unit number to search for. If the unit is -1,
1149 * return the first child of @p dev which has name
1150 * @p classname (that is, the one with the lowest unit.)
1152 * @returns the device with the given unit number or @c
1153 * NULL if there is no such device
1155 device_t
1156 device_find_child(device_t dev, const char *classname, int unit)
1158 devclass_t dc;
1159 device_t child;
1161 dc = devclass_find(classname);
1162 if (!dc)
1163 return(NULL);
1165 if (unit != -1) {
1166 child = devclass_get_device(dc, unit);
1167 if (child && child->parent == dev)
1168 return (child);
1169 } else {
1170 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1171 child = devclass_get_device(dc, unit);
1172 if (child && child->parent == dev)
1173 return (child);
1176 return(NULL);
1179 static driverlink_t
1180 first_matching_driver(devclass_t dc, device_t dev)
1182 if (dev->devclass)
1183 return(devclass_find_driver_internal(dc, dev->devclass->name));
1184 else
1185 return(TAILQ_FIRST(&dc->drivers));
1188 static driverlink_t
1189 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1191 if (dev->devclass) {
1192 driverlink_t dl;
1193 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1194 if (!strcmp(dev->devclass->name, dl->driver->name))
1195 return(dl);
1196 return(NULL);
1197 } else
1198 return(TAILQ_NEXT(last, link));
1201 static int
1202 device_probe_child(device_t dev, device_t child)
1204 devclass_t dc;
1205 driverlink_t best = 0;
1206 driverlink_t dl;
1207 int result, pri = 0;
1208 int hasclass = (child->devclass != 0);
1210 dc = dev->devclass;
1211 if (!dc)
1212 panic("device_probe_child: parent device has no devclass");
1214 if (child->state == DS_ALIVE)
1215 return(0);
1217 for (; dc; dc = dc->parent) {
1218 for (dl = first_matching_driver(dc, child); dl;
1219 dl = next_matching_driver(dc, child, dl)) {
1220 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1221 device_set_driver(child, dl->driver);
1222 if (!hasclass)
1223 device_set_devclass(child, dl->driver->name);
1224 result = DEVICE_PROBE(child);
1225 if (!hasclass)
1226 device_set_devclass(child, 0);
1229 * If the driver returns SUCCESS, there can be
1230 * no higher match for this device.
1232 if (result == 0) {
1233 best = dl;
1234 pri = 0;
1235 break;
1239 * The driver returned an error so it
1240 * certainly doesn't match.
1242 if (result > 0) {
1243 device_set_driver(child, 0);
1244 continue;
1248 * A priority lower than SUCCESS, remember the
1249 * best matching driver. Initialise the value
1250 * of pri for the first match.
1252 if (best == 0 || result > pri) {
1253 best = dl;
1254 pri = result;
1255 continue;
1259 * If we have unambiguous match in this devclass,
1260 * don't look in the parent.
1262 if (best && pri == 0)
1263 break;
1267 * If we found a driver, change state and initialise the devclass.
1269 if (best) {
1270 if (!child->devclass)
1271 device_set_devclass(child, best->driver->name);
1272 device_set_driver(child, best->driver);
1273 if (pri < 0) {
1275 * A bit bogus. Call the probe method again to make
1276 * sure that we have the right description.
1278 DEVICE_PROBE(child);
1281 bus_data_generation_update();
1282 child->state = DS_ALIVE;
1283 return(0);
1286 return(ENXIO);
1289 device_t
1290 device_get_parent(device_t dev)
1292 return dev->parent;
1296 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
1298 int count;
1299 device_t child;
1300 device_t *list;
1302 count = 0;
1303 TAILQ_FOREACH(child, &dev->children, link)
1304 count++;
1306 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
1307 if (!list)
1308 return(ENOMEM);
1310 count = 0;
1311 TAILQ_FOREACH(child, &dev->children, link) {
1312 list[count] = child;
1313 count++;
1316 *devlistp = list;
1317 *devcountp = count;
1319 return(0);
1322 driver_t *
1323 device_get_driver(device_t dev)
1325 return(dev->driver);
1328 devclass_t
1329 device_get_devclass(device_t dev)
1331 return(dev->devclass);
1334 const char *
1335 device_get_name(device_t dev)
1337 if (dev->devclass)
1338 return devclass_get_name(dev->devclass);
1339 return(NULL);
1342 const char *
1343 device_get_nameunit(device_t dev)
1345 return(dev->nameunit);
1349 device_get_unit(device_t dev)
1351 return(dev->unit);
1354 const char *
1355 device_get_desc(device_t dev)
1357 return(dev->desc);
1360 uint32_t
1361 device_get_flags(device_t dev)
1363 return(dev->devflags);
1367 device_print_prettyname(device_t dev)
1369 const char *name = device_get_name(dev);
1371 if (name == 0)
1372 return kprintf("unknown: ");
1373 else
1374 return kprintf("%s%d: ", name, device_get_unit(dev));
1378 device_printf(device_t dev, const char * fmt, ...)
1380 __va_list ap;
1381 int retval;
1383 retval = device_print_prettyname(dev);
1384 __va_start(ap, fmt);
1385 retval += kvprintf(fmt, ap);
1386 __va_end(ap);
1387 return retval;
1390 static void
1391 device_set_desc_internal(device_t dev, const char* desc, int copy)
1393 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
1394 kfree(dev->desc, M_BUS);
1395 dev->flags &= ~DF_DESCMALLOCED;
1396 dev->desc = NULL;
1399 if (copy && desc) {
1400 dev->desc = kmalloc(strlen(desc) + 1, M_BUS, M_INTWAIT);
1401 if (dev->desc) {
1402 strcpy(dev->desc, desc);
1403 dev->flags |= DF_DESCMALLOCED;
1405 } else {
1406 /* Avoid a -Wcast-qual warning */
1407 dev->desc = (char *)(uintptr_t) desc;
1410 bus_data_generation_update();
1413 void
1414 device_set_desc(device_t dev, const char* desc)
1416 device_set_desc_internal(dev, desc, FALSE);
1419 void
1420 device_set_desc_copy(device_t dev, const char* desc)
1422 device_set_desc_internal(dev, desc, TRUE);
1425 void
1426 device_set_flags(device_t dev, uint32_t flags)
1428 dev->devflags = flags;
1431 void *
1432 device_get_softc(device_t dev)
1434 return dev->softc;
1437 void
1438 device_set_softc(device_t dev, void *softc)
1440 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
1441 kfree(dev->softc, M_BUS);
1442 dev->softc = softc;
1443 if (dev->softc)
1444 dev->flags |= DF_EXTERNALSOFTC;
1445 else
1446 dev->flags &= ~DF_EXTERNALSOFTC;
1449 void
1450 device_set_async_attach(device_t dev, int enable)
1452 if (enable)
1453 dev->flags |= DF_ASYNCPROBE;
1454 else
1455 dev->flags &= ~DF_ASYNCPROBE;
1458 void *
1459 device_get_ivars(device_t dev)
1461 return dev->ivars;
1464 void
1465 device_set_ivars(device_t dev, void * ivars)
1467 if (!dev)
1468 return;
1470 dev->ivars = ivars;
1473 device_state_t
1474 device_get_state(device_t dev)
1476 return(dev->state);
1479 void
1480 device_enable(device_t dev)
1482 dev->flags |= DF_ENABLED;
1485 void
1486 device_disable(device_t dev)
1488 dev->flags &= ~DF_ENABLED;
1492 * YYY cannot block
1494 void
1495 device_busy(device_t dev)
1497 if (dev->state < DS_ATTACHED)
1498 panic("device_busy: called for unattached device");
1499 if (dev->busy == 0 && dev->parent)
1500 device_busy(dev->parent);
1501 dev->busy++;
1502 dev->state = DS_BUSY;
1506 * YYY cannot block
1508 void
1509 device_unbusy(device_t dev)
1511 if (dev->state != DS_BUSY)
1512 panic("device_unbusy: called for non-busy device");
1513 dev->busy--;
1514 if (dev->busy == 0) {
1515 if (dev->parent)
1516 device_unbusy(dev->parent);
1517 dev->state = DS_ATTACHED;
1521 void
1522 device_quiet(device_t dev)
1524 dev->flags |= DF_QUIET;
1527 void
1528 device_verbose(device_t dev)
1530 dev->flags &= ~DF_QUIET;
1534 device_is_quiet(device_t dev)
1536 return((dev->flags & DF_QUIET) != 0);
1540 device_is_enabled(device_t dev)
1542 return((dev->flags & DF_ENABLED) != 0);
1546 device_is_alive(device_t dev)
1548 return(dev->state >= DS_ALIVE);
1552 device_is_attached(device_t dev)
1554 return(dev->state >= DS_ATTACHED);
1558 device_set_devclass(device_t dev, const char *classname)
1560 devclass_t dc;
1561 int error;
1563 if (!classname) {
1564 if (dev->devclass)
1565 devclass_delete_device(dev->devclass, dev);
1566 return(0);
1569 if (dev->devclass) {
1570 kprintf("device_set_devclass: device class already set\n");
1571 return(EINVAL);
1574 dc = devclass_find_internal(classname, NULL, TRUE);
1575 if (!dc)
1576 return(ENOMEM);
1578 error = devclass_add_device(dc, dev);
1580 bus_data_generation_update();
1581 return(error);
1585 device_set_driver(device_t dev, driver_t *driver)
1587 if (dev->state >= DS_ATTACHED)
1588 return(EBUSY);
1590 if (dev->driver == driver)
1591 return(0);
1593 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
1594 kfree(dev->softc, M_BUS);
1595 dev->softc = NULL;
1597 kobj_delete((kobj_t) dev, 0);
1598 dev->driver = driver;
1599 if (driver) {
1600 kobj_init((kobj_t) dev, (kobj_class_t) driver);
1601 if (!(dev->flags & DF_EXTERNALSOFTC)) {
1602 dev->softc = kmalloc(driver->size, M_BUS,
1603 M_INTWAIT | M_ZERO);
1604 if (!dev->softc) {
1605 kobj_delete((kobj_t)dev, 0);
1606 kobj_init((kobj_t) dev, &null_class);
1607 dev->driver = NULL;
1608 return(ENOMEM);
1611 } else {
1612 kobj_init((kobj_t) dev, &null_class);
1615 bus_data_generation_update();
1616 return(0);
1620 device_probe_and_attach(device_t dev)
1622 device_t bus = dev->parent;
1623 int error = 0;
1625 if (dev->state >= DS_ALIVE)
1626 return(0);
1628 if ((dev->flags & DF_ENABLED) == 0) {
1629 if (bootverbose) {
1630 device_print_prettyname(dev);
1631 kprintf("not probed (disabled)\n");
1633 return(0);
1636 error = device_probe_child(bus, dev);
1637 if (error) {
1638 if (!(dev->flags & DF_DONENOMATCH)) {
1639 BUS_PROBE_NOMATCH(bus, dev);
1640 devnomatch(dev);
1641 dev->flags |= DF_DONENOMATCH;
1643 return(error);
1647 * Output the exact device chain prior to the attach in case the
1648 * system locks up during attach, and generate the full info after
1649 * the attach so correct irq and other information is displayed.
1651 if (bootverbose && !device_is_quiet(dev)) {
1652 device_t tmp;
1654 kprintf("%s", device_get_nameunit(dev));
1655 for (tmp = dev->parent; tmp; tmp = tmp->parent)
1656 kprintf(".%s", device_get_nameunit(tmp));
1657 kprintf("\n");
1659 if (!device_is_quiet(dev))
1660 device_print_child(bus, dev);
1661 if ((dev->flags & DF_ASYNCPROBE) && do_async_attach) {
1662 kprintf("%s: probing asynchronously\n",
1663 device_get_nameunit(dev));
1664 dev->state = DS_INPROGRESS;
1665 device_attach_async(dev);
1666 error = 0;
1667 } else {
1668 error = device_doattach(dev);
1670 return(error);
1674 * Device is known to be alive, do the attach asynchronously.
1675 * However, serialize the attaches with the mp lock.
1677 static void
1678 device_attach_async(device_t dev)
1680 thread_t td;
1682 atomic_add_int(&numasyncthreads, 1);
1683 lwkt_create(device_attach_thread, dev, &td, NULL,
1684 0, 0, (dev->desc ? dev->desc : "devattach"));
1687 static void
1688 device_attach_thread(void *arg)
1690 device_t dev = arg;
1692 get_mplock(); /* XXX replace with devattach_token later */
1693 (void)device_doattach(dev);
1694 atomic_subtract_int(&numasyncthreads, 1);
1695 wakeup(&numasyncthreads);
1696 rel_mplock(); /* XXX replace with devattach_token later */
1700 * Device is known to be alive, do the attach (synchronous or asynchronous)
1702 static int
1703 device_doattach(device_t dev)
1705 device_t bus = dev->parent;
1706 int hasclass = (dev->devclass != 0);
1707 int error;
1709 error = DEVICE_ATTACH(dev);
1710 if (error == 0) {
1711 dev->state = DS_ATTACHED;
1712 if (bootverbose && !device_is_quiet(dev))
1713 device_print_child(bus, dev);
1714 devadded(dev);
1715 } else {
1716 kprintf("device_probe_and_attach: %s%d attach returned %d\n",
1717 dev->driver->name, dev->unit, error);
1718 /* Unset the class that was set in device_probe_child */
1719 if (!hasclass)
1720 device_set_devclass(dev, 0);
1721 device_set_driver(dev, NULL);
1722 dev->state = DS_NOTPRESENT;
1724 return(error);
1728 device_detach(device_t dev)
1730 int error;
1732 PDEBUG(("%s", DEVICENAME(dev)));
1733 if (dev->state == DS_BUSY)
1734 return(EBUSY);
1735 if (dev->state != DS_ATTACHED)
1736 return(0);
1738 if ((error = DEVICE_DETACH(dev)) != 0)
1739 return(error);
1740 devremoved(dev);
1741 device_printf(dev, "detached\n");
1742 if (dev->parent)
1743 BUS_CHILD_DETACHED(dev->parent, dev);
1745 if (!(dev->flags & DF_FIXEDCLASS))
1746 devclass_delete_device(dev->devclass, dev);
1748 dev->state = DS_NOTPRESENT;
1749 device_set_driver(dev, NULL);
1751 return(0);
1755 device_shutdown(device_t dev)
1757 if (dev->state < DS_ATTACHED)
1758 return 0;
1759 PDEBUG(("%s", DEVICENAME(dev)));
1760 return DEVICE_SHUTDOWN(dev);
1764 device_set_unit(device_t dev, int unit)
1766 devclass_t dc;
1767 int err;
1769 dc = device_get_devclass(dev);
1770 if (unit < dc->maxunit && dc->devices[unit])
1771 return(EBUSY);
1772 err = devclass_delete_device(dc, dev);
1773 if (err)
1774 return(err);
1775 dev->unit = unit;
1776 err = devclass_add_device(dc, dev);
1777 if (err)
1778 return(err);
1780 bus_data_generation_update();
1781 return(0);
1784 /*======================================*/
1786 * Access functions for device resources.
1789 /* Supplied by config(8) in ioconf.c */
1790 extern struct config_device config_devtab[];
1791 extern int devtab_count;
1793 /* Runtime version */
1794 struct config_device *devtab = config_devtab;
1796 static int
1797 resource_new_name(const char *name, int unit)
1799 struct config_device *new;
1801 new = kmalloc((devtab_count + 1) * sizeof(*new), M_TEMP,
1802 M_INTWAIT | M_ZERO);
1803 if (new == NULL)
1804 return(-1);
1805 if (devtab && devtab_count > 0)
1806 bcopy(devtab, new, devtab_count * sizeof(*new));
1807 new[devtab_count].name = kmalloc(strlen(name) + 1, M_TEMP, M_INTWAIT);
1808 if (new[devtab_count].name == NULL) {
1809 kfree(new, M_TEMP);
1810 return(-1);
1812 strcpy(new[devtab_count].name, name);
1813 new[devtab_count].unit = unit;
1814 new[devtab_count].resource_count = 0;
1815 new[devtab_count].resources = NULL;
1816 if (devtab && devtab != config_devtab)
1817 kfree(devtab, M_TEMP);
1818 devtab = new;
1819 return devtab_count++;
1822 static int
1823 resource_new_resname(int j, const char *resname, resource_type type)
1825 struct config_resource *new;
1826 int i;
1828 i = devtab[j].resource_count;
1829 new = kmalloc((i + 1) * sizeof(*new), M_TEMP, M_INTWAIT | M_ZERO);
1830 if (new == NULL)
1831 return(-1);
1832 if (devtab[j].resources && i > 0)
1833 bcopy(devtab[j].resources, new, i * sizeof(*new));
1834 new[i].name = kmalloc(strlen(resname) + 1, M_TEMP, M_INTWAIT);
1835 if (new[i].name == NULL) {
1836 kfree(new, M_TEMP);
1837 return(-1);
1839 strcpy(new[i].name, resname);
1840 new[i].type = type;
1841 if (devtab[j].resources)
1842 kfree(devtab[j].resources, M_TEMP);
1843 devtab[j].resources = new;
1844 devtab[j].resource_count = i + 1;
1845 return(i);
1848 static int
1849 resource_match_string(int i, const char *resname, const char *value)
1851 int j;
1852 struct config_resource *res;
1854 for (j = 0, res = devtab[i].resources;
1855 j < devtab[i].resource_count; j++, res++)
1856 if (!strcmp(res->name, resname)
1857 && res->type == RES_STRING
1858 && !strcmp(res->u.stringval, value))
1859 return(j);
1860 return(-1);
1863 static int
1864 resource_find(const char *name, int unit, const char *resname,
1865 struct config_resource **result)
1867 int i, j;
1868 struct config_resource *res;
1871 * First check specific instances, then generic.
1873 for (i = 0; i < devtab_count; i++) {
1874 if (devtab[i].unit < 0)
1875 continue;
1876 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1877 res = devtab[i].resources;
1878 for (j = 0; j < devtab[i].resource_count; j++, res++)
1879 if (!strcmp(res->name, resname)) {
1880 *result = res;
1881 return(0);
1885 for (i = 0; i < devtab_count; i++) {
1886 if (devtab[i].unit >= 0)
1887 continue;
1888 /* XXX should this `&& devtab[i].unit == unit' be here? */
1889 /* XXX if so, then the generic match does nothing */
1890 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1891 res = devtab[i].resources;
1892 for (j = 0; j < devtab[i].resource_count; j++, res++)
1893 if (!strcmp(res->name, resname)) {
1894 *result = res;
1895 return(0);
1899 return(ENOENT);
1903 resource_int_value(const char *name, int unit, const char *resname, int *result)
1905 int error;
1906 struct config_resource *res;
1908 if ((error = resource_find(name, unit, resname, &res)) != 0)
1909 return(error);
1910 if (res->type != RES_INT)
1911 return(EFTYPE);
1912 *result = res->u.intval;
1913 return(0);
1917 resource_long_value(const char *name, int unit, const char *resname,
1918 long *result)
1920 int error;
1921 struct config_resource *res;
1923 if ((error = resource_find(name, unit, resname, &res)) != 0)
1924 return(error);
1925 if (res->type != RES_LONG)
1926 return(EFTYPE);
1927 *result = res->u.longval;
1928 return(0);
1932 resource_string_value(const char *name, int unit, const char *resname,
1933 char **result)
1935 int error;
1936 struct config_resource *res;
1938 if ((error = resource_find(name, unit, resname, &res)) != 0)
1939 return(error);
1940 if (res->type != RES_STRING)
1941 return(EFTYPE);
1942 *result = res->u.stringval;
1943 return(0);
1947 resource_query_string(int i, const char *resname, const char *value)
1949 if (i < 0)
1950 i = 0;
1951 else
1952 i = i + 1;
1953 for (; i < devtab_count; i++)
1954 if (resource_match_string(i, resname, value) >= 0)
1955 return(i);
1956 return(-1);
1960 resource_locate(int i, const char *resname)
1962 if (i < 0)
1963 i = 0;
1964 else
1965 i = i + 1;
1966 for (; i < devtab_count; i++)
1967 if (!strcmp(devtab[i].name, resname))
1968 return(i);
1969 return(-1);
1973 resource_count(void)
1975 return(devtab_count);
1978 char *
1979 resource_query_name(int i)
1981 return(devtab[i].name);
1985 resource_query_unit(int i)
1987 return(devtab[i].unit);
1990 static int
1991 resource_create(const char *name, int unit, const char *resname,
1992 resource_type type, struct config_resource **result)
1994 int i, j;
1995 struct config_resource *res = NULL;
1997 for (i = 0; i < devtab_count; i++)
1998 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1999 res = devtab[i].resources;
2000 break;
2002 if (res == NULL) {
2003 i = resource_new_name(name, unit);
2004 if (i < 0)
2005 return(ENOMEM);
2006 res = devtab[i].resources;
2008 for (j = 0; j < devtab[i].resource_count; j++, res++)
2009 if (!strcmp(res->name, resname)) {
2010 *result = res;
2011 return(0);
2013 j = resource_new_resname(i, resname, type);
2014 if (j < 0)
2015 return(ENOMEM);
2016 res = &devtab[i].resources[j];
2017 *result = res;
2018 return(0);
2022 resource_set_int(const char *name, int unit, const char *resname, int value)
2024 int error;
2025 struct config_resource *res;
2027 error = resource_create(name, unit, resname, RES_INT, &res);
2028 if (error)
2029 return(error);
2030 if (res->type != RES_INT)
2031 return(EFTYPE);
2032 res->u.intval = value;
2033 return(0);
2037 resource_set_long(const char *name, int unit, const char *resname, long value)
2039 int error;
2040 struct config_resource *res;
2042 error = resource_create(name, unit, resname, RES_LONG, &res);
2043 if (error)
2044 return(error);
2045 if (res->type != RES_LONG)
2046 return(EFTYPE);
2047 res->u.longval = value;
2048 return(0);
2052 resource_set_string(const char *name, int unit, const char *resname,
2053 const char *value)
2055 int error;
2056 struct config_resource *res;
2058 error = resource_create(name, unit, resname, RES_STRING, &res);
2059 if (error)
2060 return(error);
2061 if (res->type != RES_STRING)
2062 return(EFTYPE);
2063 if (res->u.stringval)
2064 kfree(res->u.stringval, M_TEMP);
2065 res->u.stringval = kmalloc(strlen(value) + 1, M_TEMP, M_INTWAIT);
2066 if (res->u.stringval == NULL)
2067 return(ENOMEM);
2068 strcpy(res->u.stringval, value);
2069 return(0);
2072 static void
2073 resource_cfgload(void *dummy __unused)
2075 struct config_resource *res, *cfgres;
2076 int i, j;
2077 int error;
2078 char *name, *resname;
2079 int unit;
2080 resource_type type;
2081 char *stringval;
2082 int config_devtab_count;
2084 config_devtab_count = devtab_count;
2085 devtab = NULL;
2086 devtab_count = 0;
2088 for (i = 0; i < config_devtab_count; i++) {
2089 name = config_devtab[i].name;
2090 unit = config_devtab[i].unit;
2092 for (j = 0; j < config_devtab[i].resource_count; j++) {
2093 cfgres = config_devtab[i].resources;
2094 resname = cfgres[j].name;
2095 type = cfgres[j].type;
2096 error = resource_create(name, unit, resname, type,
2097 &res);
2098 if (error) {
2099 kprintf("create resource %s%d: error %d\n",
2100 name, unit, error);
2101 continue;
2103 if (res->type != type) {
2104 kprintf("type mismatch %s%d: %d != %d\n",
2105 name, unit, res->type, type);
2106 continue;
2108 switch (type) {
2109 case RES_INT:
2110 res->u.intval = cfgres[j].u.intval;
2111 break;
2112 case RES_LONG:
2113 res->u.longval = cfgres[j].u.longval;
2114 break;
2115 case RES_STRING:
2116 if (res->u.stringval)
2117 kfree(res->u.stringval, M_TEMP);
2118 stringval = cfgres[j].u.stringval;
2119 res->u.stringval = kmalloc(strlen(stringval) + 1,
2120 M_TEMP, M_INTWAIT);
2121 if (res->u.stringval == NULL)
2122 break;
2123 strcpy(res->u.stringval, stringval);
2124 break;
2125 default:
2126 panic("unknown resource type %d", type);
2131 SYSINIT(cfgload, SI_BOOT1_POST, SI_ORDER_ANY + 50, resource_cfgload, 0)
2134 /*======================================*/
2136 * Some useful method implementations to make life easier for bus drivers.
2139 void
2140 resource_list_init(struct resource_list *rl)
2142 SLIST_INIT(rl);
2145 void
2146 resource_list_free(struct resource_list *rl)
2148 struct resource_list_entry *rle;
2150 while ((rle = SLIST_FIRST(rl)) != NULL) {
2151 if (rle->res)
2152 panic("resource_list_free: resource entry is busy");
2153 SLIST_REMOVE_HEAD(rl, link);
2154 kfree(rle, M_BUS);
2158 void
2159 resource_list_add(struct resource_list *rl,
2160 int type, int rid,
2161 u_long start, u_long end, u_long count)
2163 struct resource_list_entry *rle;
2165 rle = resource_list_find(rl, type, rid);
2166 if (rle == NULL) {
2167 rle = kmalloc(sizeof(struct resource_list_entry), M_BUS,
2168 M_INTWAIT);
2169 if (!rle)
2170 panic("resource_list_add: can't record entry");
2171 SLIST_INSERT_HEAD(rl, rle, link);
2172 rle->type = type;
2173 rle->rid = rid;
2174 rle->res = NULL;
2177 if (rle->res)
2178 panic("resource_list_add: resource entry is busy");
2180 rle->start = start;
2181 rle->end = end;
2182 rle->count = count;
2185 struct resource_list_entry*
2186 resource_list_find(struct resource_list *rl,
2187 int type, int rid)
2189 struct resource_list_entry *rle;
2191 SLIST_FOREACH(rle, rl, link)
2192 if (rle->type == type && rle->rid == rid)
2193 return(rle);
2194 return(NULL);
2197 void
2198 resource_list_delete(struct resource_list *rl,
2199 int type, int rid)
2201 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2203 if (rle) {
2204 if (rle->res != NULL)
2205 panic("resource_list_delete: resource has not been released");
2206 SLIST_REMOVE(rl, rle, resource_list_entry, link);
2207 kfree(rle, M_BUS);
2211 struct resource *
2212 resource_list_alloc(struct resource_list *rl,
2213 device_t bus, device_t child,
2214 int type, int *rid,
2215 u_long start, u_long end,
2216 u_long count, u_int flags)
2218 struct resource_list_entry *rle = 0;
2219 int passthrough = (device_get_parent(child) != bus);
2220 int isdefault = (start == 0UL && end == ~0UL);
2222 if (passthrough) {
2223 return(BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2224 type, rid,
2225 start, end, count, flags));
2228 rle = resource_list_find(rl, type, *rid);
2230 if (!rle)
2231 return(0); /* no resource of that type/rid */
2233 if (rle->res)
2234 panic("resource_list_alloc: resource entry is busy");
2236 if (isdefault) {
2237 start = rle->start;
2238 count = max(count, rle->count);
2239 end = max(rle->end, start + count - 1);
2242 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2243 type, rid, start, end, count, flags);
2246 * Record the new range.
2248 if (rle->res) {
2249 rle->start = rman_get_start(rle->res);
2250 rle->end = rman_get_end(rle->res);
2251 rle->count = count;
2254 return(rle->res);
2258 resource_list_release(struct resource_list *rl,
2259 device_t bus, device_t child,
2260 int type, int rid, struct resource *res)
2262 struct resource_list_entry *rle = 0;
2263 int passthrough = (device_get_parent(child) != bus);
2264 int error;
2266 if (passthrough) {
2267 return(BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2268 type, rid, res));
2271 rle = resource_list_find(rl, type, rid);
2273 if (!rle)
2274 panic("resource_list_release: can't find resource");
2275 if (!rle->res)
2276 panic("resource_list_release: resource entry is not busy");
2278 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2279 type, rid, res);
2280 if (error)
2281 return(error);
2283 rle->res = NULL;
2284 return(0);
2288 resource_list_print_type(struct resource_list *rl, const char *name, int type,
2289 const char *format)
2291 struct resource_list_entry *rle;
2292 int printed, retval;
2294 printed = 0;
2295 retval = 0;
2296 /* Yes, this is kinda cheating */
2297 SLIST_FOREACH(rle, rl, link) {
2298 if (rle->type == type) {
2299 if (printed == 0)
2300 retval += kprintf(" %s ", name);
2301 else
2302 retval += kprintf(",");
2303 printed++;
2304 retval += kprintf(format, rle->start);
2305 if (rle->count > 1) {
2306 retval += kprintf("-");
2307 retval += kprintf(format, rle->start +
2308 rle->count - 1);
2312 return(retval);
2316 * Generic driver/device identify functions. These will install a device
2317 * rendezvous point under the parent using the same name as the driver
2318 * name, which will at a later time be probed and attached.
2320 * These functions are used when the parent does not 'scan' its bus for
2321 * matching devices, or for the particular devices using these functions,
2322 * or when the device is a pseudo or synthesized device (such as can be
2323 * found under firewire and ppbus).
2326 bus_generic_identify(driver_t *driver, device_t parent)
2328 if (parent->state == DS_ATTACHED)
2329 return (0);
2330 BUS_ADD_CHILD(parent, parent, 0, driver->name, -1);
2331 return (0);
2335 bus_generic_identify_sameunit(driver_t *driver, device_t parent)
2337 if (parent->state == DS_ATTACHED)
2338 return (0);
2339 BUS_ADD_CHILD(parent, parent, 0, driver->name, device_get_unit(parent));
2340 return (0);
2344 * Call DEVICE_IDENTIFY for each driver.
2347 bus_generic_probe(device_t dev)
2349 devclass_t dc = dev->devclass;
2350 driverlink_t dl;
2352 TAILQ_FOREACH(dl, &dc->drivers, link) {
2353 DEVICE_IDENTIFY(dl->driver, dev);
2356 return(0);
2360 * This is an aweful hack due to the isa bus and autoconf code not
2361 * probing the ISA devices until after everything else has configured.
2362 * The ISA bus did a dummy attach long ago so we have to set it back
2363 * to an earlier state so the probe thinks its the initial probe and
2364 * not a bus rescan.
2366 * XXX remove by properly defering the ISA bus scan.
2369 bus_generic_probe_hack(device_t dev)
2371 if (dev->state == DS_ATTACHED) {
2372 dev->state = DS_ALIVE;
2373 bus_generic_probe(dev);
2374 dev->state = DS_ATTACHED;
2376 return (0);
2380 bus_generic_attach(device_t dev)
2382 device_t child;
2384 TAILQ_FOREACH(child, &dev->children, link) {
2385 device_probe_and_attach(child);
2388 return(0);
2392 bus_generic_detach(device_t dev)
2394 device_t child;
2395 int error;
2397 if (dev->state != DS_ATTACHED)
2398 return(EBUSY);
2400 TAILQ_FOREACH(child, &dev->children, link)
2401 if ((error = device_detach(child)) != 0)
2402 return(error);
2404 return 0;
2408 bus_generic_shutdown(device_t dev)
2410 device_t child;
2412 TAILQ_FOREACH(child, &dev->children, link)
2413 device_shutdown(child);
2415 return(0);
2419 bus_generic_suspend(device_t dev)
2421 int error;
2422 device_t child, child2;
2424 TAILQ_FOREACH(child, &dev->children, link) {
2425 error = DEVICE_SUSPEND(child);
2426 if (error) {
2427 for (child2 = TAILQ_FIRST(&dev->children);
2428 child2 && child2 != child;
2429 child2 = TAILQ_NEXT(child2, link))
2430 DEVICE_RESUME(child2);
2431 return(error);
2434 return(0);
2438 bus_generic_resume(device_t dev)
2440 device_t child;
2442 TAILQ_FOREACH(child, &dev->children, link)
2443 DEVICE_RESUME(child);
2444 /* if resume fails, there's nothing we can usefully do... */
2446 return(0);
2450 bus_print_child_header(device_t dev, device_t child)
2452 int retval = 0;
2454 if (device_get_desc(child))
2455 retval += device_printf(child, "<%s>", device_get_desc(child));
2456 else
2457 retval += kprintf("%s", device_get_nameunit(child));
2458 if (bootverbose) {
2459 if (child->state != DS_ATTACHED)
2460 kprintf(" [tentative]");
2461 else
2462 kprintf(" [attached!]");
2464 return(retval);
2468 bus_print_child_footer(device_t dev, device_t child)
2470 return(kprintf(" on %s\n", device_get_nameunit(dev)));
2473 device_t
2474 bus_generic_add_child(device_t dev, device_t child, int order,
2475 const char *name, int unit)
2477 if (dev->parent)
2478 dev = BUS_ADD_CHILD(dev->parent, child, order, name, unit);
2479 else
2480 dev = device_add_child_ordered(child, order, name, unit);
2481 return(dev);
2486 bus_generic_print_child(device_t dev, device_t child)
2488 int retval = 0;
2490 retval += bus_print_child_header(dev, child);
2491 retval += bus_print_child_footer(dev, child);
2493 return(retval);
2497 bus_generic_read_ivar(device_t dev, device_t child, int index,
2498 uintptr_t * result)
2500 int error;
2502 if (dev->parent)
2503 error = BUS_READ_IVAR(dev->parent, child, index, result);
2504 else
2505 error = ENOENT;
2506 return (error);
2510 bus_generic_write_ivar(device_t dev, device_t child, int index,
2511 uintptr_t value)
2513 int error;
2515 if (dev->parent)
2516 error = BUS_WRITE_IVAR(dev->parent, child, index, value);
2517 else
2518 error = ENOENT;
2519 return (error);
2523 * Resource list are used for iterations, do not recurse.
2525 struct resource_list *
2526 bus_generic_get_resource_list(device_t dev, device_t child)
2528 return (NULL);
2531 void
2532 bus_generic_driver_added(device_t dev, driver_t *driver)
2534 device_t child;
2536 DEVICE_IDENTIFY(driver, dev);
2537 TAILQ_FOREACH(child, &dev->children, link) {
2538 if (child->state == DS_NOTPRESENT)
2539 device_probe_and_attach(child);
2544 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
2545 int flags, driver_intr_t *intr, void *arg,
2546 void **cookiep, lwkt_serialize_t serializer)
2548 /* Propagate up the bus hierarchy until someone handles it. */
2549 if (dev->parent)
2550 return(BUS_SETUP_INTR(dev->parent, child, irq, flags,
2551 intr, arg, cookiep, serializer));
2552 else
2553 return(EINVAL);
2557 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
2558 void *cookie)
2560 /* Propagate up the bus hierarchy until someone handles it. */
2561 if (dev->parent)
2562 return(BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
2563 else
2564 return(EINVAL);
2568 bus_generic_disable_intr(device_t dev, device_t child, void *cookie)
2570 if (dev->parent)
2571 return(BUS_DISABLE_INTR(dev->parent, child, cookie));
2572 else
2573 return(0);
2576 void
2577 bus_generic_enable_intr(device_t dev, device_t child, void *cookie)
2579 if (dev->parent)
2580 BUS_ENABLE_INTR(dev->parent, child, cookie);
2584 bus_generic_config_intr(device_t dev, device_t child, int irq, enum intr_trigger trig,
2585 enum intr_polarity pol)
2587 /* Propagate up the bus hierarchy until someone handles it. */
2588 if (dev->parent)
2589 return(BUS_CONFIG_INTR(dev->parent, child, irq, trig, pol));
2590 else
2591 return(EINVAL);
2594 struct resource *
2595 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
2596 u_long start, u_long end, u_long count, u_int flags)
2598 /* Propagate up the bus hierarchy until someone handles it. */
2599 if (dev->parent)
2600 return(BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
2601 start, end, count, flags));
2602 else
2603 return(NULL);
2607 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
2608 struct resource *r)
2610 /* Propagate up the bus hierarchy until someone handles it. */
2611 if (dev->parent)
2612 return(BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, r));
2613 else
2614 return(EINVAL);
2618 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
2619 struct resource *r)
2621 /* Propagate up the bus hierarchy until someone handles it. */
2622 if (dev->parent)
2623 return(BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, r));
2624 else
2625 return(EINVAL);
2629 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
2630 int rid, struct resource *r)
2632 /* Propagate up the bus hierarchy until someone handles it. */
2633 if (dev->parent)
2634 return(BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
2635 r));
2636 else
2637 return(EINVAL);
2641 bus_generic_get_resource(device_t dev, device_t child, int type, int rid,
2642 u_long *startp, u_long *countp)
2644 int error;
2646 error = ENOENT;
2647 if (dev->parent) {
2648 error = BUS_GET_RESOURCE(dev->parent, child, type, rid,
2649 startp, countp);
2651 return (error);
2655 bus_generic_set_resource(device_t dev, device_t child, int type, int rid,
2656 u_long start, u_long count)
2658 int error;
2660 error = EINVAL;
2661 if (dev->parent) {
2662 error = BUS_SET_RESOURCE(dev->parent, child, type, rid,
2663 start, count);
2665 return (error);
2668 void
2669 bus_generic_delete_resource(device_t dev, device_t child, int type, int rid)
2671 if (dev->parent)
2672 BUS_DELETE_RESOURCE(dev, child, type, rid);
2676 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
2677 u_long *startp, u_long *countp)
2679 struct resource_list *rl = NULL;
2680 struct resource_list_entry *rle = NULL;
2682 rl = BUS_GET_RESOURCE_LIST(dev, child);
2683 if (!rl)
2684 return(EINVAL);
2686 rle = resource_list_find(rl, type, rid);
2687 if (!rle)
2688 return(ENOENT);
2690 if (startp)
2691 *startp = rle->start;
2692 if (countp)
2693 *countp = rle->count;
2695 return(0);
2699 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
2700 u_long start, u_long count)
2702 struct resource_list *rl = NULL;
2704 rl = BUS_GET_RESOURCE_LIST(dev, child);
2705 if (!rl)
2706 return(EINVAL);
2708 resource_list_add(rl, type, rid, start, (start + count - 1), count);
2710 return(0);
2713 void
2714 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
2716 struct resource_list *rl = NULL;
2718 rl = BUS_GET_RESOURCE_LIST(dev, child);
2719 if (!rl)
2720 return;
2722 resource_list_delete(rl, type, rid);
2726 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
2727 int rid, struct resource *r)
2729 struct resource_list *rl = NULL;
2731 rl = BUS_GET_RESOURCE_LIST(dev, child);
2732 if (!rl)
2733 return(EINVAL);
2735 return(resource_list_release(rl, dev, child, type, rid, r));
2738 struct resource *
2739 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
2740 int *rid, u_long start, u_long end, u_long count, u_int flags)
2742 struct resource_list *rl = NULL;
2744 rl = BUS_GET_RESOURCE_LIST(dev, child);
2745 if (!rl)
2746 return(NULL);
2748 return(resource_list_alloc(rl, dev, child, type, rid,
2749 start, end, count, flags));
2753 bus_generic_child_present(device_t bus, device_t child)
2755 return(BUS_CHILD_PRESENT(device_get_parent(bus), bus));
2760 * Some convenience functions to make it easier for drivers to use the
2761 * resource-management functions. All these really do is hide the
2762 * indirection through the parent's method table, making for slightly
2763 * less-wordy code. In the future, it might make sense for this code
2764 * to maintain some sort of a list of resources allocated by each device.
2767 bus_alloc_resources(device_t dev, struct resource_spec *rs,
2768 struct resource **res)
2770 int i;
2772 for (i = 0; rs[i].type != -1; i++)
2773 res[i] = NULL;
2774 for (i = 0; rs[i].type != -1; i++) {
2775 res[i] = bus_alloc_resource_any(dev,
2776 rs[i].type, &rs[i].rid, rs[i].flags);
2777 if (res[i] == NULL) {
2778 bus_release_resources(dev, rs, res);
2779 return (ENXIO);
2782 return (0);
2785 void
2786 bus_release_resources(device_t dev, const struct resource_spec *rs,
2787 struct resource **res)
2789 int i;
2791 for (i = 0; rs[i].type != -1; i++)
2792 if (res[i] != NULL) {
2793 bus_release_resource(
2794 dev, rs[i].type, rs[i].rid, res[i]);
2795 res[i] = NULL;
2799 struct resource *
2800 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
2801 u_long count, u_int flags)
2803 if (dev->parent == 0)
2804 return(0);
2805 return(BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
2806 count, flags));
2810 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
2812 if (dev->parent == 0)
2813 return(EINVAL);
2814 return(BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2818 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
2820 if (dev->parent == 0)
2821 return(EINVAL);
2822 return(BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2826 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
2828 if (dev->parent == 0)
2829 return(EINVAL);
2830 return(BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
2834 bus_setup_intr(device_t dev, struct resource *r, int flags,
2835 driver_intr_t handler, void *arg,
2836 void **cookiep, lwkt_serialize_t serializer)
2838 if (dev->parent == 0)
2839 return(EINVAL);
2840 return(BUS_SETUP_INTR(dev->parent, dev, r, flags, handler, arg,
2841 cookiep, serializer));
2845 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
2847 if (dev->parent == 0)
2848 return(EINVAL);
2849 return(BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
2852 void
2853 bus_enable_intr(device_t dev, void *cookie)
2855 if (dev->parent)
2856 BUS_ENABLE_INTR(dev->parent, dev, cookie);
2860 bus_disable_intr(device_t dev, void *cookie)
2862 if (dev->parent)
2863 return(BUS_DISABLE_INTR(dev->parent, dev, cookie));
2864 else
2865 return(0);
2869 bus_set_resource(device_t dev, int type, int rid,
2870 u_long start, u_long count)
2872 return(BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
2873 start, count));
2877 bus_get_resource(device_t dev, int type, int rid,
2878 u_long *startp, u_long *countp)
2880 return(BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2881 startp, countp));
2884 u_long
2885 bus_get_resource_start(device_t dev, int type, int rid)
2887 u_long start, count;
2888 int error;
2890 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2891 &start, &count);
2892 if (error)
2893 return(0);
2894 return(start);
2897 u_long
2898 bus_get_resource_count(device_t dev, int type, int rid)
2900 u_long start, count;
2901 int error;
2903 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2904 &start, &count);
2905 if (error)
2906 return(0);
2907 return(count);
2910 void
2911 bus_delete_resource(device_t dev, int type, int rid)
2913 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
2917 bus_child_present(device_t child)
2919 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
2923 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
2925 device_t parent;
2927 parent = device_get_parent(child);
2928 if (parent == NULL) {
2929 *buf = '\0';
2930 return (0);
2932 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
2936 bus_child_location_str(device_t child, char *buf, size_t buflen)
2938 device_t parent;
2940 parent = device_get_parent(child);
2941 if (parent == NULL) {
2942 *buf = '\0';
2943 return (0);
2945 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
2948 static int
2949 root_print_child(device_t dev, device_t child)
2951 return(0);
2954 static int
2955 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
2956 void **cookiep, lwkt_serialize_t serializer)
2959 * If an interrupt mapping gets to here something bad has happened.
2961 panic("root_setup_intr");
2965 * If we get here, assume that the device is permanant and really is
2966 * present in the system. Removable bus drivers are expected to intercept
2967 * this call long before it gets here. We return -1 so that drivers that
2968 * really care can check vs -1 or some ERRNO returned higher in the food
2969 * chain.
2971 static int
2972 root_child_present(device_t dev, device_t child)
2974 return(-1);
2978 * XXX NOTE! other defaults may be set in bus_if.m
2980 static kobj_method_t root_methods[] = {
2981 /* Device interface */
2982 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
2983 KOBJMETHOD(device_suspend, bus_generic_suspend),
2984 KOBJMETHOD(device_resume, bus_generic_resume),
2986 /* Bus interface */
2987 KOBJMETHOD(bus_add_child, bus_generic_add_child),
2988 KOBJMETHOD(bus_print_child, root_print_child),
2989 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
2990 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
2991 KOBJMETHOD(bus_setup_intr, root_setup_intr),
2992 KOBJMETHOD(bus_child_present, root_child_present),
2994 { 0, 0 }
2997 static driver_t root_driver = {
2998 "root",
2999 root_methods,
3000 1, /* no softc */
3003 device_t root_bus;
3004 devclass_t root_devclass;
3006 static int
3007 root_bus_module_handler(module_t mod, int what, void* arg)
3009 switch (what) {
3010 case MOD_LOAD:
3011 TAILQ_INIT(&bus_data_devices);
3012 root_bus = make_device(NULL, "root", 0);
3013 root_bus->desc = "System root bus";
3014 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
3015 root_bus->driver = &root_driver;
3016 root_bus->state = DS_ALIVE;
3017 root_devclass = devclass_find_internal("root", NULL, FALSE);
3018 devinit();
3019 return(0);
3021 case MOD_SHUTDOWN:
3022 device_shutdown(root_bus);
3023 return(0);
3024 default:
3025 return(0);
3029 static moduledata_t root_bus_mod = {
3030 "rootbus",
3031 root_bus_module_handler,
3034 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
3036 void
3037 root_bus_configure(void)
3039 int warncount;
3040 device_t dev;
3042 PDEBUG(("."));
3045 * handle device_identify based device attachments to the root_bus
3046 * (typically nexus).
3048 bus_generic_probe(root_bus);
3051 * Probe and attach the devices under root_bus.
3053 TAILQ_FOREACH(dev, &root_bus->children, link) {
3054 device_probe_and_attach(dev);
3058 * Wait for all asynchronous attaches to complete. If we don't
3059 * our legacy ISA bus scan could steal device unit numbers or
3060 * even I/O ports.
3062 warncount = 10;
3063 if (numasyncthreads)
3064 kprintf("Waiting for async drivers to attach\n");
3065 while (numasyncthreads > 0) {
3066 if (tsleep(&numasyncthreads, 0, "rootbus", hz) == EWOULDBLOCK)
3067 --warncount;
3068 if (warncount == 0) {
3069 kprintf("Warning: Still waiting for %d "
3070 "drivers to attach\n", numasyncthreads);
3071 } else if (warncount == -30) {
3072 kprintf("Giving up on %d drivers\n", numasyncthreads);
3073 break;
3076 root_bus->state = DS_ATTACHED;
3080 driver_module_handler(module_t mod, int what, void *arg)
3082 int error;
3083 struct driver_module_data *dmd;
3084 devclass_t bus_devclass;
3085 kobj_class_t driver;
3086 const char *parentname;
3088 dmd = (struct driver_module_data *)arg;
3089 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
3090 error = 0;
3092 switch (what) {
3093 case MOD_LOAD:
3094 if (dmd->dmd_chainevh)
3095 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3097 driver = dmd->dmd_driver;
3098 PDEBUG(("Loading module: driver %s on bus %s",
3099 DRIVERNAME(driver), dmd->dmd_busname));
3102 * If the driver has any base classes, make the
3103 * devclass inherit from the devclass of the driver's
3104 * first base class. This will allow the system to
3105 * search for drivers in both devclasses for children
3106 * of a device using this driver.
3108 if (driver->baseclasses)
3109 parentname = driver->baseclasses[0]->name;
3110 else
3111 parentname = NULL;
3112 *dmd->dmd_devclass = devclass_find_internal(driver->name,
3113 parentname, TRUE);
3115 error = devclass_add_driver(bus_devclass, driver);
3116 if (error)
3117 break;
3118 break;
3120 case MOD_UNLOAD:
3121 PDEBUG(("Unloading module: driver %s from bus %s",
3122 DRIVERNAME(dmd->dmd_driver), dmd->dmd_busname));
3123 error = devclass_delete_driver(bus_devclass, dmd->dmd_driver);
3125 if (!error && dmd->dmd_chainevh)
3126 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3127 break;
3130 return (error);
3133 #ifdef BUS_DEBUG
3136 * The _short versions avoid iteration by not calling anything that prints
3137 * more than oneliners. I love oneliners.
3140 static void
3141 print_device_short(device_t dev, int indent)
3143 if (!dev)
3144 return;
3146 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
3147 dev->unit, dev->desc,
3148 (dev->parent? "":"no "),
3149 (TAILQ_EMPTY(&dev->children)? "no ":""),
3150 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
3151 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
3152 (dev->flags&DF_WILDCARD? "wildcard,":""),
3153 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
3154 (dev->ivars? "":"no "),
3155 (dev->softc? "":"no "),
3156 dev->busy));
3159 static void
3160 print_device(device_t dev, int indent)
3162 if (!dev)
3163 return;
3165 print_device_short(dev, indent);
3167 indentprintf(("Parent:\n"));
3168 print_device_short(dev->parent, indent+1);
3169 indentprintf(("Driver:\n"));
3170 print_driver_short(dev->driver, indent+1);
3171 indentprintf(("Devclass:\n"));
3172 print_devclass_short(dev->devclass, indent+1);
3176 * Print the device and all its children (indented).
3178 void
3179 print_device_tree_short(device_t dev, int indent)
3181 device_t child;
3183 if (!dev)
3184 return;
3186 print_device_short(dev, indent);
3188 TAILQ_FOREACH(child, &dev->children, link)
3189 print_device_tree_short(child, indent+1);
3193 * Print the device and all its children (indented).
3195 void
3196 print_device_tree(device_t dev, int indent)
3198 device_t child;
3200 if (!dev)
3201 return;
3203 print_device(dev, indent);
3205 TAILQ_FOREACH(child, &dev->children, link)
3206 print_device_tree(child, indent+1);
3209 static void
3210 print_driver_short(driver_t *driver, int indent)
3212 if (!driver)
3213 return;
3215 indentprintf(("driver %s: softc size = %zu\n",
3216 driver->name, driver->size));
3219 static void
3220 print_driver(driver_t *driver, int indent)
3222 if (!driver)
3223 return;
3225 print_driver_short(driver, indent);
3229 static void
3230 print_driver_list(driver_list_t drivers, int indent)
3232 driverlink_t driver;
3234 TAILQ_FOREACH(driver, &drivers, link)
3235 print_driver(driver->driver, indent);
3238 static void
3239 print_devclass_short(devclass_t dc, int indent)
3241 if (!dc)
3242 return;
3244 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
3247 static void
3248 print_devclass(devclass_t dc, int indent)
3250 int i;
3252 if (!dc)
3253 return;
3255 print_devclass_short(dc, indent);
3256 indentprintf(("Drivers:\n"));
3257 print_driver_list(dc->drivers, indent+1);
3259 indentprintf(("Devices:\n"));
3260 for (i = 0; i < dc->maxunit; i++)
3261 if (dc->devices[i])
3262 print_device(dc->devices[i], indent+1);
3265 void
3266 print_devclass_list_short(void)
3268 devclass_t dc;
3270 kprintf("Short listing of devclasses, drivers & devices:\n");
3271 TAILQ_FOREACH(dc, &devclasses, link) {
3272 print_devclass_short(dc, 0);
3276 void
3277 print_devclass_list(void)
3279 devclass_t dc;
3281 kprintf("Full listing of devclasses, drivers & devices:\n");
3282 TAILQ_FOREACH(dc, &devclasses, link) {
3283 print_devclass(dc, 0);
3287 #endif
3290 * Check to see if a device is disabled via a disabled hint.
3293 resource_disabled(const char *name, int unit)
3295 int error, value;
3297 error = resource_int_value(name, unit, "disabled", &value);
3298 if (error)
3299 return(0);
3300 return(value);
3304 * User-space access to the device tree.
3306 * We implement a small set of nodes:
3308 * hw.bus Single integer read method to obtain the
3309 * current generation count.
3310 * hw.bus.devices Reads the entire device tree in flat space.
3311 * hw.bus.rman Resource manager interface
3313 * We might like to add the ability to scan devclasses and/or drivers to
3314 * determine what else is currently loaded/available.
3317 static int
3318 sysctl_bus(SYSCTL_HANDLER_ARGS)
3320 struct u_businfo ubus;
3322 ubus.ub_version = BUS_USER_VERSION;
3323 ubus.ub_generation = bus_data_generation;
3325 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
3327 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
3328 "bus-related data");
3330 static int
3331 sysctl_devices(SYSCTL_HANDLER_ARGS)
3333 int *name = (int *)arg1;
3334 u_int namelen = arg2;
3335 int index;
3336 struct device *dev;
3337 struct u_device udev; /* XXX this is a bit big */
3338 int error;
3340 if (namelen != 2)
3341 return (EINVAL);
3343 if (bus_data_generation_check(name[0]))
3344 return (EINVAL);
3346 index = name[1];
3349 * Scan the list of devices, looking for the requested index.
3351 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
3352 if (index-- == 0)
3353 break;
3355 if (dev == NULL)
3356 return (ENOENT);
3359 * Populate the return array.
3361 bzero(&udev, sizeof(udev));
3362 udev.dv_handle = (uintptr_t)dev;
3363 udev.dv_parent = (uintptr_t)dev->parent;
3364 if (dev->nameunit != NULL)
3365 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
3366 if (dev->desc != NULL)
3367 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
3368 if (dev->driver != NULL && dev->driver->name != NULL)
3369 strlcpy(udev.dv_drivername, dev->driver->name,
3370 sizeof(udev.dv_drivername));
3371 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
3372 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
3373 udev.dv_devflags = dev->devflags;
3374 udev.dv_flags = dev->flags;
3375 udev.dv_state = dev->state;
3376 error = SYSCTL_OUT(req, &udev, sizeof(udev));
3377 return (error);
3380 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
3381 "system device tree");
3384 bus_data_generation_check(int generation)
3386 if (generation != bus_data_generation)
3387 return (1);
3389 /* XXX generate optimised lists here? */
3390 return (0);
3393 void
3394 bus_data_generation_update(void)
3396 bus_data_generation++;
3399 const char *
3400 intr_str_polarity(enum intr_polarity pola)
3402 switch (pola) {
3403 case INTR_POLARITY_LOW:
3404 return "low";
3406 case INTR_POLARITY_HIGH:
3407 return "high";
3409 case INTR_POLARITY_CONFORM:
3410 return "conform";
3412 return "unknown";
3415 const char *
3416 intr_str_trigger(enum intr_trigger trig)
3418 switch (trig) {
3419 case INTR_TRIGGER_EDGE:
3420 return "edge";
3422 case INTR_TRIGGER_LEVEL:
3423 return "level";
3425 case INTR_TRIGGER_CONFORM:
3426 return "conform";
3428 return "unknown";