kernel - Force NFSv3 for diskless nfs mount
[dragonfly.git] / sys / kern / subr_bus.c
blob26cdd6f38ea787a3a0243be92e7df84369d39fcc
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 $
27 * $DragonFly: src/sys/kern/subr_bus.c,v 1.46 2008/10/03 00:26:21 hasso Exp $
30 #include "opt_bus.h"
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/kobj.h>
38 #include <sys/bus_private.h>
39 #include <sys/sysctl.h>
40 #include <sys/systm.h>
41 #include <sys/bus.h>
42 #include <sys/rman.h>
43 #include <sys/device.h>
44 #include <sys/lock.h>
45 #include <sys/conf.h>
46 #include <sys/uio.h>
47 #include <sys/filio.h>
48 #include <sys/event.h>
49 #include <sys/signalvar.h>
51 #include <machine/stdarg.h> /* for device_printf() */
53 #include <sys/thread2.h>
54 #include <sys/mplock2.h>
56 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
58 MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
60 #ifdef BUS_DEBUG
61 #define PDEBUG(a) (kprintf("%s:%d: ", __func__, __LINE__), kprintf a, kprintf("\n"))
62 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
63 #define DRIVERNAME(d) ((d)? d->name : "no driver")
64 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
66 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to
67 * prevent syslog from deleting initial spaces
69 #define indentprintf(p) do { int iJ; kprintf("."); for (iJ=0; iJ<indent; iJ++) kprintf(" "); kprintf p ; } while(0)
71 static void print_device_short(device_t dev, int indent);
72 static void print_device(device_t dev, int indent);
73 void print_device_tree_short(device_t dev, int indent);
74 void print_device_tree(device_t dev, int indent);
75 static void print_driver_short(driver_t *driver, int indent);
76 static void print_driver(driver_t *driver, int indent);
77 static void print_driver_list(driver_list_t drivers, int indent);
78 static void print_devclass_short(devclass_t dc, int indent);
79 static void print_devclass(devclass_t dc, int indent);
80 void print_devclass_list_short(void);
81 void print_devclass_list(void);
83 #else
84 /* Make the compiler ignore the function calls */
85 #define PDEBUG(a) /* nop */
86 #define DEVICENAME(d) /* nop */
87 #define DRIVERNAME(d) /* nop */
88 #define DEVCLANAME(d) /* nop */
90 #define print_device_short(d,i) /* nop */
91 #define print_device(d,i) /* nop */
92 #define print_device_tree_short(d,i) /* nop */
93 #define print_device_tree(d,i) /* nop */
94 #define print_driver_short(d,i) /* nop */
95 #define print_driver(d,i) /* nop */
96 #define print_driver_list(d,i) /* nop */
97 #define print_devclass_short(d,i) /* nop */
98 #define print_devclass(d,i) /* nop */
99 #define print_devclass_list_short() /* nop */
100 #define print_devclass_list() /* nop */
101 #endif
103 static void device_attach_async(device_t dev);
104 static void device_attach_thread(void *arg);
105 static int device_doattach(device_t dev);
107 static int do_async_attach = 0;
108 static int numasyncthreads;
109 TUNABLE_INT("kern.do_async_attach", &do_async_attach);
112 * /dev/devctl implementation
116 * This design allows only one reader for /dev/devctl. This is not desirable
117 * in the long run, but will get a lot of hair out of this implementation.
118 * Maybe we should make this device a clonable device.
120 * Also note: we specifically do not attach a device to the device_t tree
121 * to avoid potential chicken and egg problems. One could argue that all
122 * of this belongs to the root node. One could also further argue that the
123 * sysctl interface that we have not might more properly be an ioctl
124 * interface, but at this stage of the game, I'm not inclined to rock that
125 * boat.
127 * I'm also not sure that the SIGIO support is done correctly or not, as
128 * I copied it from a driver that had SIGIO support that likely hasn't been
129 * tested since 3.4 or 2.2.8!
132 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
133 static int devctl_disable = 0;
134 TUNABLE_INT("hw.bus.devctl_disable", &devctl_disable);
135 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
136 sysctl_devctl_disable, "I", "devctl disable");
138 #define CDEV_MAJOR 188
140 static d_open_t devopen;
141 static d_close_t devclose;
142 static d_read_t devread;
143 static d_ioctl_t devioctl;
144 static d_kqfilter_t devkqfilter;
146 static struct dev_ops devctl_ops = {
147 { "devctl", CDEV_MAJOR, 0 },
148 .d_open = devopen,
149 .d_close = devclose,
150 .d_read = devread,
151 .d_ioctl = devioctl,
152 .d_kqfilter = devkqfilter
155 struct dev_event_info
157 char *dei_data;
158 TAILQ_ENTRY(dev_event_info) dei_link;
161 TAILQ_HEAD(devq, dev_event_info);
163 static struct dev_softc
165 int inuse;
166 int nonblock;
167 struct lock lock;
168 struct kqinfo kq;
169 struct devq devq;
170 struct proc *async_proc;
171 } devsoftc;
173 static void
174 devinit(void)
176 make_dev(&devctl_ops, 0, UID_ROOT, GID_WHEEL, 0600, "devctl");
177 lockinit(&devsoftc.lock, "dev mtx", 0, 0);
178 TAILQ_INIT(&devsoftc.devq);
181 static int
182 devopen(struct dev_open_args *ap)
184 if (devsoftc.inuse)
185 return (EBUSY);
186 /* move to init */
187 devsoftc.inuse = 1;
188 devsoftc.nonblock = 0;
189 devsoftc.async_proc = NULL;
190 return (0);
193 static int
194 devclose(struct dev_close_args *ap)
196 devsoftc.inuse = 0;
197 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
198 wakeup(&devsoftc);
199 lockmgr(&devsoftc.lock, LK_RELEASE);
201 return (0);
205 * The read channel for this device is used to report changes to
206 * userland in realtime. We are required to free the data as well as
207 * the n1 object because we allocate them separately. Also note that
208 * we return one record at a time. If you try to read this device a
209 * character at a time, you will lose the rest of the data. Listening
210 * programs are expected to cope.
212 static int
213 devread(struct dev_read_args *ap)
215 struct uio *uio = ap->a_uio;
216 struct dev_event_info *n1;
217 int rv;
219 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
220 while (TAILQ_EMPTY(&devsoftc.devq)) {
221 if (devsoftc.nonblock) {
222 lockmgr(&devsoftc.lock, LK_RELEASE);
223 return (EAGAIN);
225 tsleep_interlock(&devsoftc, PCATCH);
226 lockmgr(&devsoftc.lock, LK_RELEASE);
227 rv = tsleep(&devsoftc, PCATCH | PINTERLOCKED, "devctl", 0);
228 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
229 if (rv) {
231 * Need to translate ERESTART to EINTR here? -- jake
233 lockmgr(&devsoftc.lock, LK_RELEASE);
234 return (rv);
237 n1 = TAILQ_FIRST(&devsoftc.devq);
238 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
239 lockmgr(&devsoftc.lock, LK_RELEASE);
240 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
241 kfree(n1->dei_data, M_BUS);
242 kfree(n1, M_BUS);
243 return (rv);
246 static int
247 devioctl(struct dev_ioctl_args *ap)
249 switch (ap->a_cmd) {
251 case FIONBIO:
252 if (*(int*)ap->a_data)
253 devsoftc.nonblock = 1;
254 else
255 devsoftc.nonblock = 0;
256 return (0);
257 case FIOASYNC:
258 if (*(int*)ap->a_data)
259 devsoftc.async_proc = curproc;
260 else
261 devsoftc.async_proc = NULL;
262 return (0);
264 /* (un)Support for other fcntl() calls. */
265 case FIOCLEX:
266 case FIONCLEX:
267 case FIONREAD:
268 case FIOSETOWN:
269 case FIOGETOWN:
270 default:
271 break;
273 return (ENOTTY);
276 static void dev_filter_detach(struct knote *);
277 static int dev_filter_read(struct knote *, long);
279 static struct filterops dev_filtops =
280 { FILTEROP_ISFD, NULL, dev_filter_detach, dev_filter_read };
282 static int
283 devkqfilter(struct dev_kqfilter_args *ap)
285 struct knote *kn = ap->a_kn;
286 struct klist *klist;
288 ap->a_result = 0;
289 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
291 switch (kn->kn_filter) {
292 case EVFILT_READ:
293 kn->kn_fop = &dev_filtops;
294 break;
295 default:
296 ap->a_result = EOPNOTSUPP;
297 lockmgr(&devsoftc.lock, LK_RELEASE);
298 return (0);
301 klist = &devsoftc.kq.ki_note;
302 knote_insert(klist, kn);
304 lockmgr(&devsoftc.lock, LK_RELEASE);
306 return (0);
309 static void
310 dev_filter_detach(struct knote *kn)
312 struct klist *klist;
314 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
315 klist = &devsoftc.kq.ki_note;
316 knote_remove(klist, kn);
317 lockmgr(&devsoftc.lock, LK_RELEASE);
320 static int
321 dev_filter_read(struct knote *kn, long hint)
323 int ready = 0;
325 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
326 if (!TAILQ_EMPTY(&devsoftc.devq))
327 ready = 1;
328 lockmgr(&devsoftc.lock, LK_RELEASE);
330 return (ready);
335 * @brief Return whether the userland process is running
337 boolean_t
338 devctl_process_running(void)
340 return (devsoftc.inuse == 1);
344 * @brief Queue data to be read from the devctl device
346 * Generic interface to queue data to the devctl device. It is
347 * assumed that @p data is properly formatted. It is further assumed
348 * that @p data is allocated using the M_BUS malloc type.
350 void
351 devctl_queue_data(char *data)
353 struct dev_event_info *n1 = NULL;
354 struct proc *p;
356 n1 = kmalloc(sizeof(*n1), M_BUS, M_NOWAIT);
357 if (n1 == NULL)
358 return;
359 n1->dei_data = data;
360 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
361 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
362 wakeup(&devsoftc);
363 lockmgr(&devsoftc.lock, LK_RELEASE);
364 get_mplock(); /* XXX */
365 KNOTE(&devsoftc.kq.ki_note, 0);
366 rel_mplock(); /* XXX */
367 p = devsoftc.async_proc;
368 if (p != NULL)
369 ksignal(p, SIGIO);
373 * @brief Send a 'notification' to userland, using standard ways
375 void
376 devctl_notify(const char *system, const char *subsystem, const char *type,
377 const char *data)
379 int len = 0;
380 char *msg;
382 if (system == NULL)
383 return; /* BOGUS! Must specify system. */
384 if (subsystem == NULL)
385 return; /* BOGUS! Must specify subsystem. */
386 if (type == NULL)
387 return; /* BOGUS! Must specify type. */
388 len += strlen(" system=") + strlen(system);
389 len += strlen(" subsystem=") + strlen(subsystem);
390 len += strlen(" type=") + strlen(type);
391 /* add in the data message plus newline. */
392 if (data != NULL)
393 len += strlen(data);
394 len += 3; /* '!', '\n', and NUL */
395 msg = kmalloc(len, M_BUS, M_NOWAIT);
396 if (msg == NULL)
397 return; /* Drop it on the floor */
398 if (data != NULL)
399 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
400 system, subsystem, type, data);
401 else
402 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
403 system, subsystem, type);
404 devctl_queue_data(msg);
408 * Common routine that tries to make sending messages as easy as possible.
409 * We allocate memory for the data, copy strings into that, but do not
410 * free it unless there's an error. The dequeue part of the driver should
411 * free the data. We don't send data when the device is disabled. We do
412 * send data, even when we have no listeners, because we wish to avoid
413 * races relating to startup and restart of listening applications.
415 * devaddq is designed to string together the type of event, with the
416 * object of that event, plus the plug and play info and location info
417 * for that event. This is likely most useful for devices, but less
418 * useful for other consumers of this interface. Those should use
419 * the devctl_queue_data() interface instead.
421 static void
422 devaddq(const char *type, const char *what, device_t dev)
424 char *data = NULL;
425 char *loc = NULL;
426 char *pnp = NULL;
427 const char *parstr;
429 if (devctl_disable)
430 return;
431 data = kmalloc(1024, M_BUS, M_NOWAIT);
432 if (data == NULL)
433 goto bad;
435 /* get the bus specific location of this device */
436 loc = kmalloc(1024, M_BUS, M_NOWAIT);
437 if (loc == NULL)
438 goto bad;
439 *loc = '\0';
440 bus_child_location_str(dev, loc, 1024);
442 /* Get the bus specific pnp info of this device */
443 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
444 if (pnp == NULL)
445 goto bad;
446 *pnp = '\0';
447 bus_child_pnpinfo_str(dev, pnp, 1024);
449 /* Get the parent of this device, or / if high enough in the tree. */
450 if (device_get_parent(dev) == NULL)
451 parstr = "."; /* Or '/' ? */
452 else
453 parstr = device_get_nameunit(device_get_parent(dev));
454 /* String it all together. */
455 ksnprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
456 parstr);
457 kfree(loc, M_BUS);
458 kfree(pnp, M_BUS);
459 devctl_queue_data(data);
460 return;
461 bad:
462 kfree(pnp, M_BUS);
463 kfree(loc, M_BUS);
464 kfree(data, M_BUS);
465 return;
469 * A device was added to the tree. We are called just after it successfully
470 * attaches (that is, probe and attach success for this device). No call
471 * is made if a device is merely parented into the tree. See devnomatch
472 * if probe fails. If attach fails, no notification is sent (but maybe
473 * we should have a different message for this).
475 static void
476 devadded(device_t dev)
478 char *pnp = NULL;
479 char *tmp = NULL;
481 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
482 if (pnp == NULL)
483 goto fail;
484 tmp = kmalloc(1024, M_BUS, M_NOWAIT);
485 if (tmp == NULL)
486 goto fail;
487 *pnp = '\0';
488 bus_child_pnpinfo_str(dev, pnp, 1024);
489 ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
490 devaddq("+", tmp, dev);
491 fail:
492 if (pnp != NULL)
493 kfree(pnp, M_BUS);
494 if (tmp != NULL)
495 kfree(tmp, M_BUS);
496 return;
500 * A device was removed from the tree. We are called just before this
501 * happens.
503 static void
504 devremoved(device_t dev)
506 char *pnp = NULL;
507 char *tmp = NULL;
509 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
510 if (pnp == NULL)
511 goto fail;
512 tmp = kmalloc(1024, M_BUS, M_NOWAIT);
513 if (tmp == NULL)
514 goto fail;
515 *pnp = '\0';
516 bus_child_pnpinfo_str(dev, pnp, 1024);
517 ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
518 devaddq("-", tmp, dev);
519 fail:
520 if (pnp != NULL)
521 kfree(pnp, M_BUS);
522 if (tmp != NULL)
523 kfree(tmp, M_BUS);
524 return;
528 * Called when there's no match for this device. This is only called
529 * the first time that no match happens, so we don't keep getitng this
530 * message. Should that prove to be undesirable, we can change it.
531 * This is called when all drivers that can attach to a given bus
532 * decline to accept this device. Other errrors may not be detected.
534 static void
535 devnomatch(device_t dev)
537 devaddq("?", "", dev);
540 static int
541 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
543 struct dev_event_info *n1;
544 int dis, error;
546 dis = devctl_disable;
547 error = sysctl_handle_int(oidp, &dis, 0, req);
548 if (error || !req->newptr)
549 return (error);
550 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
551 devctl_disable = dis;
552 if (dis) {
553 while (!TAILQ_EMPTY(&devsoftc.devq)) {
554 n1 = TAILQ_FIRST(&devsoftc.devq);
555 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
556 kfree(n1->dei_data, M_BUS);
557 kfree(n1, M_BUS);
560 lockmgr(&devsoftc.lock, LK_RELEASE);
561 return (0);
564 /* End of /dev/devctl code */
566 TAILQ_HEAD(,device) bus_data_devices;
567 static int bus_data_generation = 1;
569 kobj_method_t null_methods[] = {
570 { 0, 0 }
573 DEFINE_CLASS(null, null_methods, 0);
576 * Devclass implementation
579 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
581 static devclass_t
582 devclass_find_internal(const char *classname, const char *parentname,
583 int create)
585 devclass_t dc;
587 PDEBUG(("looking for %s", classname));
588 if (classname == NULL)
589 return(NULL);
591 TAILQ_FOREACH(dc, &devclasses, link)
592 if (!strcmp(dc->name, classname))
593 break;
595 if (create && !dc) {
596 PDEBUG(("creating %s", classname));
597 dc = kmalloc(sizeof(struct devclass) + strlen(classname) + 1,
598 M_BUS, M_INTWAIT | M_ZERO);
599 if (!dc)
600 return(NULL);
601 dc->parent = NULL;
602 dc->name = (char*) (dc + 1);
603 strcpy(dc->name, classname);
604 dc->devices = NULL;
605 dc->maxunit = 0;
606 TAILQ_INIT(&dc->drivers);
607 TAILQ_INSERT_TAIL(&devclasses, dc, link);
609 bus_data_generation_update();
612 if (parentname && dc && !dc->parent)
613 dc->parent = devclass_find_internal(parentname, NULL, FALSE);
615 return(dc);
618 devclass_t
619 devclass_create(const char *classname)
621 return(devclass_find_internal(classname, NULL, TRUE));
624 devclass_t
625 devclass_find(const char *classname)
627 return(devclass_find_internal(classname, NULL, FALSE));
630 device_t
631 devclass_find_unit(const char *classname, int unit)
633 devclass_t dc;
635 if ((dc = devclass_find(classname)) != NULL)
636 return(devclass_get_device(dc, unit));
637 return (NULL);
641 devclass_add_driver(devclass_t dc, driver_t *driver)
643 driverlink_t dl;
644 device_t dev;
645 int i;
647 PDEBUG(("%s", DRIVERNAME(driver)));
649 dl = kmalloc(sizeof *dl, M_BUS, M_INTWAIT | M_ZERO);
650 if (!dl)
651 return(ENOMEM);
654 * Compile the driver's methods. Also increase the reference count
655 * so that the class doesn't get freed when the last instance
656 * goes. This means we can safely use static methods and avoids a
657 * double-free in devclass_delete_driver.
659 kobj_class_instantiate(driver);
662 * Make sure the devclass which the driver is implementing exists.
664 devclass_find_internal(driver->name, NULL, TRUE);
666 dl->driver = driver;
667 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
670 * Call BUS_DRIVER_ADDED for any existing busses in this class,
671 * but only if the bus has already been attached (otherwise we
672 * might probe too early).
674 * This is what will cause a newly loaded module to be associated
675 * with hardware. bus_generic_driver_added() is typically what ends
676 * up being called.
678 for (i = 0; i < dc->maxunit; i++) {
679 if ((dev = dc->devices[i]) != NULL) {
680 if (dev->state >= DS_ATTACHED)
681 BUS_DRIVER_ADDED(dev, driver);
685 bus_data_generation_update();
686 return(0);
690 devclass_delete_driver(devclass_t busclass, driver_t *driver)
692 devclass_t dc = devclass_find(driver->name);
693 driverlink_t dl;
694 device_t dev;
695 int i;
696 int error;
698 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
700 if (!dc)
701 return(0);
704 * Find the link structure in the bus' list of drivers.
706 TAILQ_FOREACH(dl, &busclass->drivers, link)
707 if (dl->driver == driver)
708 break;
710 if (!dl) {
711 PDEBUG(("%s not found in %s list", driver->name, busclass->name));
712 return(ENOENT);
716 * Disassociate from any devices. We iterate through all the
717 * devices in the devclass of the driver and detach any which are
718 * using the driver and which have a parent in the devclass which
719 * we are deleting from.
721 * Note that since a driver can be in multiple devclasses, we
722 * should not detach devices which are not children of devices in
723 * the affected devclass.
725 for (i = 0; i < dc->maxunit; i++)
726 if (dc->devices[i]) {
727 dev = dc->devices[i];
728 if (dev->driver == driver && dev->parent &&
729 dev->parent->devclass == busclass) {
730 if ((error = device_detach(dev)) != 0)
731 return(error);
732 device_set_driver(dev, NULL);
736 TAILQ_REMOVE(&busclass->drivers, dl, link);
737 kfree(dl, M_BUS);
739 kobj_class_uninstantiate(driver);
741 bus_data_generation_update();
742 return(0);
745 static driverlink_t
746 devclass_find_driver_internal(devclass_t dc, const char *classname)
748 driverlink_t dl;
750 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
752 TAILQ_FOREACH(dl, &dc->drivers, link)
753 if (!strcmp(dl->driver->name, classname))
754 return(dl);
756 PDEBUG(("not found"));
757 return(NULL);
760 kobj_class_t
761 devclass_find_driver(devclass_t dc, const char *classname)
763 driverlink_t dl;
765 dl = devclass_find_driver_internal(dc, classname);
766 if (dl)
767 return(dl->driver);
768 else
769 return(NULL);
772 const char *
773 devclass_get_name(devclass_t dc)
775 return(dc->name);
778 device_t
779 devclass_get_device(devclass_t dc, int unit)
781 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
782 return(NULL);
783 return(dc->devices[unit]);
786 void *
787 devclass_get_softc(devclass_t dc, int unit)
789 device_t dev;
791 dev = devclass_get_device(dc, unit);
792 if (!dev)
793 return(NULL);
795 return(device_get_softc(dev));
799 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
801 int i;
802 int count;
803 device_t *list;
805 count = 0;
806 for (i = 0; i < dc->maxunit; i++)
807 if (dc->devices[i])
808 count++;
810 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
811 if (list == NULL)
812 return(ENOMEM);
814 count = 0;
815 for (i = 0; i < dc->maxunit; i++)
816 if (dc->devices[i]) {
817 list[count] = dc->devices[i];
818 count++;
821 *devlistp = list;
822 *devcountp = count;
824 return(0);
828 * @brief Get a list of drivers in the devclass
830 * An array containing a list of pointers to all the drivers in the
831 * given devclass is allocated and returned in @p *listp. The number
832 * of drivers in the array is returned in @p *countp. The caller should
833 * free the array using @c free(p, M_TEMP).
835 * @param dc the devclass to examine
836 * @param listp gives location for array pointer return value
837 * @param countp gives location for number of array elements
838 * return value
840 * @retval 0 success
841 * @retval ENOMEM the array allocation failed
844 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
846 driverlink_t dl;
847 driver_t **list;
848 int count;
850 count = 0;
851 TAILQ_FOREACH(dl, &dc->drivers, link)
852 count++;
853 list = kmalloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
854 if (list == NULL)
855 return (ENOMEM);
857 count = 0;
858 TAILQ_FOREACH(dl, &dc->drivers, link) {
859 list[count] = dl->driver;
860 count++;
862 *listp = list;
863 *countp = count;
865 return (0);
869 * @brief Get the number of devices in a devclass
871 * @param dc the devclass to examine
874 devclass_get_count(devclass_t dc)
876 int count, i;
878 count = 0;
879 for (i = 0; i < dc->maxunit; i++)
880 if (dc->devices[i])
881 count++;
882 return (count);
886 devclass_get_maxunit(devclass_t dc)
888 return(dc->maxunit);
891 void
892 devclass_set_parent(devclass_t dc, devclass_t pdc)
894 dc->parent = pdc;
897 devclass_t
898 devclass_get_parent(devclass_t dc)
900 return(dc->parent);
903 static int
904 devclass_alloc_unit(devclass_t dc, int *unitp)
906 int unit = *unitp;
908 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
910 /* If we have been given a wired unit number, check for existing device */
911 if (unit != -1) {
912 if (unit >= 0 && unit < dc->maxunit &&
913 dc->devices[unit] != NULL) {
914 if (bootverbose)
915 kprintf("%s-: %s%d exists, using next available unit number\n",
916 dc->name, dc->name, unit);
917 /* find the next available slot */
918 while (++unit < dc->maxunit && dc->devices[unit] != NULL)
921 } else {
922 /* Unwired device, find the next available slot for it */
923 unit = 0;
924 while (unit < dc->maxunit && dc->devices[unit] != NULL)
925 unit++;
929 * We've selected a unit beyond the length of the table, so let's
930 * extend the table to make room for all units up to and including
931 * this one.
933 if (unit >= dc->maxunit) {
934 device_t *newlist;
935 int newsize;
937 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
938 newlist = kmalloc(sizeof(device_t) * newsize, M_BUS,
939 M_INTWAIT | M_ZERO);
940 if (newlist == NULL)
941 return(ENOMEM);
942 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
943 if (dc->devices)
944 kfree(dc->devices, M_BUS);
945 dc->devices = newlist;
946 dc->maxunit = newsize;
948 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
950 *unitp = unit;
951 return(0);
954 static int
955 devclass_add_device(devclass_t dc, device_t dev)
957 int buflen, error;
959 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
961 buflen = strlen(dc->name) + 5;
962 dev->nameunit = kmalloc(buflen, M_BUS, M_INTWAIT | M_ZERO);
963 if (!dev->nameunit)
964 return(ENOMEM);
966 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
967 kfree(dev->nameunit, M_BUS);
968 dev->nameunit = NULL;
969 return(error);
971 dc->devices[dev->unit] = dev;
972 dev->devclass = dc;
973 ksnprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
975 return(0);
978 static int
979 devclass_delete_device(devclass_t dc, device_t dev)
981 if (!dc || !dev)
982 return(0);
984 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
986 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
987 panic("devclass_delete_device: inconsistent device class");
988 dc->devices[dev->unit] = NULL;
989 if (dev->flags & DF_WILDCARD)
990 dev->unit = -1;
991 dev->devclass = NULL;
992 kfree(dev->nameunit, M_BUS);
993 dev->nameunit = NULL;
995 return(0);
998 static device_t
999 make_device(device_t parent, const char *name, int unit)
1001 device_t dev;
1002 devclass_t dc;
1004 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1006 if (name != NULL) {
1007 dc = devclass_find_internal(name, NULL, TRUE);
1008 if (!dc) {
1009 kprintf("make_device: can't find device class %s\n", name);
1010 return(NULL);
1012 } else
1013 dc = NULL;
1015 dev = kmalloc(sizeof(struct device), M_BUS, M_INTWAIT | M_ZERO);
1016 if (!dev)
1017 return(0);
1019 dev->parent = parent;
1020 TAILQ_INIT(&dev->children);
1021 kobj_init((kobj_t) dev, &null_class);
1022 dev->driver = NULL;
1023 dev->devclass = NULL;
1024 dev->unit = unit;
1025 dev->nameunit = NULL;
1026 dev->desc = NULL;
1027 dev->busy = 0;
1028 dev->devflags = 0;
1029 dev->flags = DF_ENABLED;
1030 dev->order = 0;
1031 if (unit == -1)
1032 dev->flags |= DF_WILDCARD;
1033 if (name) {
1034 dev->flags |= DF_FIXEDCLASS;
1035 if (devclass_add_device(dc, dev) != 0) {
1036 kobj_delete((kobj_t)dev, M_BUS);
1037 return(NULL);
1040 dev->ivars = NULL;
1041 dev->softc = NULL;
1043 dev->state = DS_NOTPRESENT;
1045 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1046 bus_data_generation_update();
1048 return(dev);
1051 static int
1052 device_print_child(device_t dev, device_t child)
1054 int retval = 0;
1056 if (device_is_alive(child))
1057 retval += BUS_PRINT_CHILD(dev, child);
1058 else
1059 retval += device_printf(child, " not found\n");
1061 return(retval);
1064 device_t
1065 device_add_child(device_t dev, const char *name, int unit)
1067 return device_add_child_ordered(dev, 0, name, unit);
1070 device_t
1071 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
1073 device_t child;
1074 device_t place;
1076 PDEBUG(("%s at %s with order %d as unit %d", name, DEVICENAME(dev),
1077 order, unit));
1079 child = make_device(dev, name, unit);
1080 if (child == NULL)
1081 return child;
1082 child->order = order;
1084 TAILQ_FOREACH(place, &dev->children, link)
1085 if (place->order > order)
1086 break;
1088 if (place) {
1090 * The device 'place' is the first device whose order is
1091 * greater than the new child.
1093 TAILQ_INSERT_BEFORE(place, child, link);
1094 } else {
1096 * The new child's order is greater or equal to the order of
1097 * any existing device. Add the child to the tail of the list.
1099 TAILQ_INSERT_TAIL(&dev->children, child, link);
1102 bus_data_generation_update();
1103 return(child);
1107 device_delete_child(device_t dev, device_t child)
1109 int error;
1110 device_t grandchild;
1112 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1114 /* remove children first */
1115 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1116 error = device_delete_child(child, grandchild);
1117 if (error)
1118 return(error);
1121 if ((error = device_detach(child)) != 0)
1122 return(error);
1123 if (child->devclass)
1124 devclass_delete_device(child->devclass, child);
1125 TAILQ_REMOVE(&dev->children, child, link);
1126 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1127 device_set_desc(child, NULL);
1128 kobj_delete((kobj_t)child, M_BUS);
1130 bus_data_generation_update();
1131 return(0);
1135 * @brief Find a device given a unit number
1137 * This is similar to devclass_get_devices() but only searches for
1138 * devices which have @p dev as a parent.
1140 * @param dev the parent device to search
1141 * @param unit the unit number to search for. If the unit is -1,
1142 * return the first child of @p dev which has name
1143 * @p classname (that is, the one with the lowest unit.)
1145 * @returns the device with the given unit number or @c
1146 * NULL if there is no such device
1148 device_t
1149 device_find_child(device_t dev, const char *classname, int unit)
1151 devclass_t dc;
1152 device_t child;
1154 dc = devclass_find(classname);
1155 if (!dc)
1156 return(NULL);
1158 if (unit != -1) {
1159 child = devclass_get_device(dc, unit);
1160 if (child && child->parent == dev)
1161 return (child);
1162 } else {
1163 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1164 child = devclass_get_device(dc, unit);
1165 if (child && child->parent == dev)
1166 return (child);
1169 return(NULL);
1172 static driverlink_t
1173 first_matching_driver(devclass_t dc, device_t dev)
1175 if (dev->devclass)
1176 return(devclass_find_driver_internal(dc, dev->devclass->name));
1177 else
1178 return(TAILQ_FIRST(&dc->drivers));
1181 static driverlink_t
1182 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1184 if (dev->devclass) {
1185 driverlink_t dl;
1186 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1187 if (!strcmp(dev->devclass->name, dl->driver->name))
1188 return(dl);
1189 return(NULL);
1190 } else
1191 return(TAILQ_NEXT(last, link));
1194 static int
1195 device_probe_child(device_t dev, device_t child)
1197 devclass_t dc;
1198 driverlink_t best = 0;
1199 driverlink_t dl;
1200 int result, pri = 0;
1201 int hasclass = (child->devclass != 0);
1203 dc = dev->devclass;
1204 if (!dc)
1205 panic("device_probe_child: parent device has no devclass");
1207 if (child->state == DS_ALIVE)
1208 return(0);
1210 for (; dc; dc = dc->parent) {
1211 for (dl = first_matching_driver(dc, child); dl;
1212 dl = next_matching_driver(dc, child, dl)) {
1213 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1214 device_set_driver(child, dl->driver);
1215 if (!hasclass)
1216 device_set_devclass(child, dl->driver->name);
1217 result = DEVICE_PROBE(child);
1218 if (!hasclass)
1219 device_set_devclass(child, 0);
1222 * If the driver returns SUCCESS, there can be
1223 * no higher match for this device.
1225 if (result == 0) {
1226 best = dl;
1227 pri = 0;
1228 break;
1232 * The driver returned an error so it
1233 * certainly doesn't match.
1235 if (result > 0) {
1236 device_set_driver(child, 0);
1237 continue;
1241 * A priority lower than SUCCESS, remember the
1242 * best matching driver. Initialise the value
1243 * of pri for the first match.
1245 if (best == 0 || result > pri) {
1246 best = dl;
1247 pri = result;
1248 continue;
1252 * If we have unambiguous match in this devclass,
1253 * don't look in the parent.
1255 if (best && pri == 0)
1256 break;
1260 * If we found a driver, change state and initialise the devclass.
1262 if (best) {
1263 if (!child->devclass)
1264 device_set_devclass(child, best->driver->name);
1265 device_set_driver(child, best->driver);
1266 if (pri < 0) {
1268 * A bit bogus. Call the probe method again to make
1269 * sure that we have the right description.
1271 DEVICE_PROBE(child);
1274 bus_data_generation_update();
1275 child->state = DS_ALIVE;
1276 return(0);
1279 return(ENXIO);
1282 device_t
1283 device_get_parent(device_t dev)
1285 return dev->parent;
1289 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
1291 int count;
1292 device_t child;
1293 device_t *list;
1295 count = 0;
1296 TAILQ_FOREACH(child, &dev->children, link)
1297 count++;
1299 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
1300 if (!list)
1301 return(ENOMEM);
1303 count = 0;
1304 TAILQ_FOREACH(child, &dev->children, link) {
1305 list[count] = child;
1306 count++;
1309 *devlistp = list;
1310 *devcountp = count;
1312 return(0);
1315 driver_t *
1316 device_get_driver(device_t dev)
1318 return(dev->driver);
1321 devclass_t
1322 device_get_devclass(device_t dev)
1324 return(dev->devclass);
1327 const char *
1328 device_get_name(device_t dev)
1330 if (dev->devclass)
1331 return devclass_get_name(dev->devclass);
1332 return(NULL);
1335 const char *
1336 device_get_nameunit(device_t dev)
1338 return(dev->nameunit);
1342 device_get_unit(device_t dev)
1344 return(dev->unit);
1347 const char *
1348 device_get_desc(device_t dev)
1350 return(dev->desc);
1353 uint32_t
1354 device_get_flags(device_t dev)
1356 return(dev->devflags);
1360 device_print_prettyname(device_t dev)
1362 const char *name = device_get_name(dev);
1364 if (name == 0)
1365 return kprintf("unknown: ");
1366 else
1367 return kprintf("%s%d: ", name, device_get_unit(dev));
1371 device_printf(device_t dev, const char * fmt, ...)
1373 __va_list ap;
1374 int retval;
1376 retval = device_print_prettyname(dev);
1377 __va_start(ap, fmt);
1378 retval += kvprintf(fmt, ap);
1379 __va_end(ap);
1380 return retval;
1383 static void
1384 device_set_desc_internal(device_t dev, const char* desc, int copy)
1386 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
1387 kfree(dev->desc, M_BUS);
1388 dev->flags &= ~DF_DESCMALLOCED;
1389 dev->desc = NULL;
1392 if (copy && desc) {
1393 dev->desc = kmalloc(strlen(desc) + 1, M_BUS, M_INTWAIT);
1394 if (dev->desc) {
1395 strcpy(dev->desc, desc);
1396 dev->flags |= DF_DESCMALLOCED;
1398 } else {
1399 /* Avoid a -Wcast-qual warning */
1400 dev->desc = (char *)(uintptr_t) desc;
1403 bus_data_generation_update();
1406 void
1407 device_set_desc(device_t dev, const char* desc)
1409 device_set_desc_internal(dev, desc, FALSE);
1412 void
1413 device_set_desc_copy(device_t dev, const char* desc)
1415 device_set_desc_internal(dev, desc, TRUE);
1418 void
1419 device_set_flags(device_t dev, uint32_t flags)
1421 dev->devflags = flags;
1424 void *
1425 device_get_softc(device_t dev)
1427 return dev->softc;
1430 void
1431 device_set_softc(device_t dev, void *softc)
1433 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
1434 kfree(dev->softc, M_BUS);
1435 dev->softc = softc;
1436 if (dev->softc)
1437 dev->flags |= DF_EXTERNALSOFTC;
1438 else
1439 dev->flags &= ~DF_EXTERNALSOFTC;
1442 void
1443 device_set_async_attach(device_t dev, int enable)
1445 if (enable)
1446 dev->flags |= DF_ASYNCPROBE;
1447 else
1448 dev->flags &= ~DF_ASYNCPROBE;
1451 void *
1452 device_get_ivars(device_t dev)
1454 return dev->ivars;
1457 void
1458 device_set_ivars(device_t dev, void * ivars)
1460 if (!dev)
1461 return;
1463 dev->ivars = ivars;
1466 device_state_t
1467 device_get_state(device_t dev)
1469 return(dev->state);
1472 void
1473 device_enable(device_t dev)
1475 dev->flags |= DF_ENABLED;
1478 void
1479 device_disable(device_t dev)
1481 dev->flags &= ~DF_ENABLED;
1485 * YYY cannot block
1487 void
1488 device_busy(device_t dev)
1490 if (dev->state < DS_ATTACHED)
1491 panic("device_busy: called for unattached device");
1492 if (dev->busy == 0 && dev->parent)
1493 device_busy(dev->parent);
1494 dev->busy++;
1495 dev->state = DS_BUSY;
1499 * YYY cannot block
1501 void
1502 device_unbusy(device_t dev)
1504 if (dev->state != DS_BUSY)
1505 panic("device_unbusy: called for non-busy device");
1506 dev->busy--;
1507 if (dev->busy == 0) {
1508 if (dev->parent)
1509 device_unbusy(dev->parent);
1510 dev->state = DS_ATTACHED;
1514 void
1515 device_quiet(device_t dev)
1517 dev->flags |= DF_QUIET;
1520 void
1521 device_verbose(device_t dev)
1523 dev->flags &= ~DF_QUIET;
1527 device_is_quiet(device_t dev)
1529 return((dev->flags & DF_QUIET) != 0);
1533 device_is_enabled(device_t dev)
1535 return((dev->flags & DF_ENABLED) != 0);
1539 device_is_alive(device_t dev)
1541 return(dev->state >= DS_ALIVE);
1545 device_is_attached(device_t dev)
1547 return(dev->state >= DS_ATTACHED);
1551 device_set_devclass(device_t dev, const char *classname)
1553 devclass_t dc;
1554 int error;
1556 if (!classname) {
1557 if (dev->devclass)
1558 devclass_delete_device(dev->devclass, dev);
1559 return(0);
1562 if (dev->devclass) {
1563 kprintf("device_set_devclass: device class already set\n");
1564 return(EINVAL);
1567 dc = devclass_find_internal(classname, NULL, TRUE);
1568 if (!dc)
1569 return(ENOMEM);
1571 error = devclass_add_device(dc, dev);
1573 bus_data_generation_update();
1574 return(error);
1578 device_set_driver(device_t dev, driver_t *driver)
1580 if (dev->state >= DS_ATTACHED)
1581 return(EBUSY);
1583 if (dev->driver == driver)
1584 return(0);
1586 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
1587 kfree(dev->softc, M_BUS);
1588 dev->softc = NULL;
1590 kobj_delete((kobj_t) dev, 0);
1591 dev->driver = driver;
1592 if (driver) {
1593 kobj_init((kobj_t) dev, (kobj_class_t) driver);
1594 if (!(dev->flags & DF_EXTERNALSOFTC)) {
1595 dev->softc = kmalloc(driver->size, M_BUS,
1596 M_INTWAIT | M_ZERO);
1597 if (!dev->softc) {
1598 kobj_delete((kobj_t)dev, 0);
1599 kobj_init((kobj_t) dev, &null_class);
1600 dev->driver = NULL;
1601 return(ENOMEM);
1604 } else {
1605 kobj_init((kobj_t) dev, &null_class);
1608 bus_data_generation_update();
1609 return(0);
1613 device_probe_and_attach(device_t dev)
1615 device_t bus = dev->parent;
1616 int error = 0;
1618 if (dev->state >= DS_ALIVE)
1619 return(0);
1621 if ((dev->flags & DF_ENABLED) == 0) {
1622 if (bootverbose) {
1623 device_print_prettyname(dev);
1624 kprintf("not probed (disabled)\n");
1626 return(0);
1629 error = device_probe_child(bus, dev);
1630 if (error) {
1631 if (!(dev->flags & DF_DONENOMATCH)) {
1632 BUS_PROBE_NOMATCH(bus, dev);
1633 devnomatch(dev);
1634 dev->flags |= DF_DONENOMATCH;
1636 return(error);
1640 * Output the exact device chain prior to the attach in case the
1641 * system locks up during attach, and generate the full info after
1642 * the attach so correct irq and other information is displayed.
1644 if (bootverbose && !device_is_quiet(dev)) {
1645 device_t tmp;
1647 kprintf("%s", device_get_nameunit(dev));
1648 for (tmp = dev->parent; tmp; tmp = tmp->parent)
1649 kprintf(".%s", device_get_nameunit(tmp));
1650 kprintf("\n");
1652 if (!device_is_quiet(dev))
1653 device_print_child(bus, dev);
1654 if ((dev->flags & DF_ASYNCPROBE) && do_async_attach) {
1655 kprintf("%s: probing asynchronously\n",
1656 device_get_nameunit(dev));
1657 dev->state = DS_INPROGRESS;
1658 device_attach_async(dev);
1659 error = 0;
1660 } else {
1661 error = device_doattach(dev);
1663 return(error);
1667 * Device is known to be alive, do the attach asynchronously.
1668 * However, serialize the attaches with the mp lock.
1670 static void
1671 device_attach_async(device_t dev)
1673 thread_t td;
1675 atomic_add_int(&numasyncthreads, 1);
1676 lwkt_create(device_attach_thread, dev, &td, NULL,
1677 0, 0, (dev->desc ? dev->desc : "devattach"));
1680 static void
1681 device_attach_thread(void *arg)
1683 device_t dev = arg;
1685 get_mplock(); /* XXX replace with devattach_token later */
1686 (void)device_doattach(dev);
1687 atomic_subtract_int(&numasyncthreads, 1);
1688 wakeup(&numasyncthreads);
1689 rel_mplock(); /* XXX replace with devattach_token later */
1693 * Device is known to be alive, do the attach (synchronous or asynchronous)
1695 static int
1696 device_doattach(device_t dev)
1698 device_t bus = dev->parent;
1699 int hasclass = (dev->devclass != 0);
1700 int error;
1702 error = DEVICE_ATTACH(dev);
1703 if (error == 0) {
1704 dev->state = DS_ATTACHED;
1705 if (bootverbose && !device_is_quiet(dev))
1706 device_print_child(bus, dev);
1707 devadded(dev);
1708 } else {
1709 kprintf("device_probe_and_attach: %s%d attach returned %d\n",
1710 dev->driver->name, dev->unit, error);
1711 /* Unset the class that was set in device_probe_child */
1712 if (!hasclass)
1713 device_set_devclass(dev, 0);
1714 device_set_driver(dev, NULL);
1715 dev->state = DS_NOTPRESENT;
1717 return(error);
1721 device_detach(device_t dev)
1723 int error;
1725 PDEBUG(("%s", DEVICENAME(dev)));
1726 if (dev->state == DS_BUSY)
1727 return(EBUSY);
1728 if (dev->state != DS_ATTACHED)
1729 return(0);
1731 if ((error = DEVICE_DETACH(dev)) != 0)
1732 return(error);
1733 devremoved(dev);
1734 device_printf(dev, "detached\n");
1735 if (dev->parent)
1736 BUS_CHILD_DETACHED(dev->parent, dev);
1738 if (!(dev->flags & DF_FIXEDCLASS))
1739 devclass_delete_device(dev->devclass, dev);
1741 dev->state = DS_NOTPRESENT;
1742 device_set_driver(dev, NULL);
1744 return(0);
1748 device_shutdown(device_t dev)
1750 if (dev->state < DS_ATTACHED)
1751 return 0;
1752 PDEBUG(("%s", DEVICENAME(dev)));
1753 return DEVICE_SHUTDOWN(dev);
1757 device_set_unit(device_t dev, int unit)
1759 devclass_t dc;
1760 int err;
1762 dc = device_get_devclass(dev);
1763 if (unit < dc->maxunit && dc->devices[unit])
1764 return(EBUSY);
1765 err = devclass_delete_device(dc, dev);
1766 if (err)
1767 return(err);
1768 dev->unit = unit;
1769 err = devclass_add_device(dc, dev);
1770 if (err)
1771 return(err);
1773 bus_data_generation_update();
1774 return(0);
1777 /*======================================*/
1779 * Access functions for device resources.
1782 /* Supplied by config(8) in ioconf.c */
1783 extern struct config_device config_devtab[];
1784 extern int devtab_count;
1786 /* Runtime version */
1787 struct config_device *devtab = config_devtab;
1789 static int
1790 resource_new_name(const char *name, int unit)
1792 struct config_device *new;
1794 new = kmalloc((devtab_count + 1) * sizeof(*new), M_TEMP,
1795 M_INTWAIT | M_ZERO);
1796 if (new == NULL)
1797 return(-1);
1798 if (devtab && devtab_count > 0)
1799 bcopy(devtab, new, devtab_count * sizeof(*new));
1800 new[devtab_count].name = kmalloc(strlen(name) + 1, M_TEMP, M_INTWAIT);
1801 if (new[devtab_count].name == NULL) {
1802 kfree(new, M_TEMP);
1803 return(-1);
1805 strcpy(new[devtab_count].name, name);
1806 new[devtab_count].unit = unit;
1807 new[devtab_count].resource_count = 0;
1808 new[devtab_count].resources = NULL;
1809 if (devtab && devtab != config_devtab)
1810 kfree(devtab, M_TEMP);
1811 devtab = new;
1812 return devtab_count++;
1815 static int
1816 resource_new_resname(int j, const char *resname, resource_type type)
1818 struct config_resource *new;
1819 int i;
1821 i = devtab[j].resource_count;
1822 new = kmalloc((i + 1) * sizeof(*new), M_TEMP, M_INTWAIT | M_ZERO);
1823 if (new == NULL)
1824 return(-1);
1825 if (devtab[j].resources && i > 0)
1826 bcopy(devtab[j].resources, new, i * sizeof(*new));
1827 new[i].name = kmalloc(strlen(resname) + 1, M_TEMP, M_INTWAIT);
1828 if (new[i].name == NULL) {
1829 kfree(new, M_TEMP);
1830 return(-1);
1832 strcpy(new[i].name, resname);
1833 new[i].type = type;
1834 if (devtab[j].resources)
1835 kfree(devtab[j].resources, M_TEMP);
1836 devtab[j].resources = new;
1837 devtab[j].resource_count = i + 1;
1838 return(i);
1841 static int
1842 resource_match_string(int i, const char *resname, const char *value)
1844 int j;
1845 struct config_resource *res;
1847 for (j = 0, res = devtab[i].resources;
1848 j < devtab[i].resource_count; j++, res++)
1849 if (!strcmp(res->name, resname)
1850 && res->type == RES_STRING
1851 && !strcmp(res->u.stringval, value))
1852 return(j);
1853 return(-1);
1856 static int
1857 resource_find(const char *name, int unit, const char *resname,
1858 struct config_resource **result)
1860 int i, j;
1861 struct config_resource *res;
1864 * First check specific instances, then generic.
1866 for (i = 0; i < devtab_count; i++) {
1867 if (devtab[i].unit < 0)
1868 continue;
1869 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1870 res = devtab[i].resources;
1871 for (j = 0; j < devtab[i].resource_count; j++, res++)
1872 if (!strcmp(res->name, resname)) {
1873 *result = res;
1874 return(0);
1878 for (i = 0; i < devtab_count; i++) {
1879 if (devtab[i].unit >= 0)
1880 continue;
1881 /* XXX should this `&& devtab[i].unit == unit' be here? */
1882 /* XXX if so, then the generic match does nothing */
1883 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1884 res = devtab[i].resources;
1885 for (j = 0; j < devtab[i].resource_count; j++, res++)
1886 if (!strcmp(res->name, resname)) {
1887 *result = res;
1888 return(0);
1892 return(ENOENT);
1896 resource_int_value(const char *name, int unit, const char *resname, int *result)
1898 int error;
1899 struct config_resource *res;
1901 if ((error = resource_find(name, unit, resname, &res)) != 0)
1902 return(error);
1903 if (res->type != RES_INT)
1904 return(EFTYPE);
1905 *result = res->u.intval;
1906 return(0);
1910 resource_long_value(const char *name, int unit, const char *resname,
1911 long *result)
1913 int error;
1914 struct config_resource *res;
1916 if ((error = resource_find(name, unit, resname, &res)) != 0)
1917 return(error);
1918 if (res->type != RES_LONG)
1919 return(EFTYPE);
1920 *result = res->u.longval;
1921 return(0);
1925 resource_string_value(const char *name, int unit, const char *resname,
1926 char **result)
1928 int error;
1929 struct config_resource *res;
1931 if ((error = resource_find(name, unit, resname, &res)) != 0)
1932 return(error);
1933 if (res->type != RES_STRING)
1934 return(EFTYPE);
1935 *result = res->u.stringval;
1936 return(0);
1940 resource_query_string(int i, const char *resname, const char *value)
1942 if (i < 0)
1943 i = 0;
1944 else
1945 i = i + 1;
1946 for (; i < devtab_count; i++)
1947 if (resource_match_string(i, resname, value) >= 0)
1948 return(i);
1949 return(-1);
1953 resource_locate(int i, const char *resname)
1955 if (i < 0)
1956 i = 0;
1957 else
1958 i = i + 1;
1959 for (; i < devtab_count; i++)
1960 if (!strcmp(devtab[i].name, resname))
1961 return(i);
1962 return(-1);
1966 resource_count(void)
1968 return(devtab_count);
1971 char *
1972 resource_query_name(int i)
1974 return(devtab[i].name);
1978 resource_query_unit(int i)
1980 return(devtab[i].unit);
1983 static int
1984 resource_create(const char *name, int unit, const char *resname,
1985 resource_type type, struct config_resource **result)
1987 int i, j;
1988 struct config_resource *res = NULL;
1990 for (i = 0; i < devtab_count; i++)
1991 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1992 res = devtab[i].resources;
1993 break;
1995 if (res == NULL) {
1996 i = resource_new_name(name, unit);
1997 if (i < 0)
1998 return(ENOMEM);
1999 res = devtab[i].resources;
2001 for (j = 0; j < devtab[i].resource_count; j++, res++)
2002 if (!strcmp(res->name, resname)) {
2003 *result = res;
2004 return(0);
2006 j = resource_new_resname(i, resname, type);
2007 if (j < 0)
2008 return(ENOMEM);
2009 res = &devtab[i].resources[j];
2010 *result = res;
2011 return(0);
2015 resource_set_int(const char *name, int unit, const char *resname, int value)
2017 int error;
2018 struct config_resource *res;
2020 error = resource_create(name, unit, resname, RES_INT, &res);
2021 if (error)
2022 return(error);
2023 if (res->type != RES_INT)
2024 return(EFTYPE);
2025 res->u.intval = value;
2026 return(0);
2030 resource_set_long(const char *name, int unit, const char *resname, long value)
2032 int error;
2033 struct config_resource *res;
2035 error = resource_create(name, unit, resname, RES_LONG, &res);
2036 if (error)
2037 return(error);
2038 if (res->type != RES_LONG)
2039 return(EFTYPE);
2040 res->u.longval = value;
2041 return(0);
2045 resource_set_string(const char *name, int unit, const char *resname,
2046 const char *value)
2048 int error;
2049 struct config_resource *res;
2051 error = resource_create(name, unit, resname, RES_STRING, &res);
2052 if (error)
2053 return(error);
2054 if (res->type != RES_STRING)
2055 return(EFTYPE);
2056 if (res->u.stringval)
2057 kfree(res->u.stringval, M_TEMP);
2058 res->u.stringval = kmalloc(strlen(value) + 1, M_TEMP, M_INTWAIT);
2059 if (res->u.stringval == NULL)
2060 return(ENOMEM);
2061 strcpy(res->u.stringval, value);
2062 return(0);
2065 static void
2066 resource_cfgload(void *dummy __unused)
2068 struct config_resource *res, *cfgres;
2069 int i, j;
2070 int error;
2071 char *name, *resname;
2072 int unit;
2073 resource_type type;
2074 char *stringval;
2075 int config_devtab_count;
2077 config_devtab_count = devtab_count;
2078 devtab = NULL;
2079 devtab_count = 0;
2081 for (i = 0; i < config_devtab_count; i++) {
2082 name = config_devtab[i].name;
2083 unit = config_devtab[i].unit;
2085 for (j = 0; j < config_devtab[i].resource_count; j++) {
2086 cfgres = config_devtab[i].resources;
2087 resname = cfgres[j].name;
2088 type = cfgres[j].type;
2089 error = resource_create(name, unit, resname, type,
2090 &res);
2091 if (error) {
2092 kprintf("create resource %s%d: error %d\n",
2093 name, unit, error);
2094 continue;
2096 if (res->type != type) {
2097 kprintf("type mismatch %s%d: %d != %d\n",
2098 name, unit, res->type, type);
2099 continue;
2101 switch (type) {
2102 case RES_INT:
2103 res->u.intval = cfgres[j].u.intval;
2104 break;
2105 case RES_LONG:
2106 res->u.longval = cfgres[j].u.longval;
2107 break;
2108 case RES_STRING:
2109 if (res->u.stringval)
2110 kfree(res->u.stringval, M_TEMP);
2111 stringval = cfgres[j].u.stringval;
2112 res->u.stringval = kmalloc(strlen(stringval) + 1,
2113 M_TEMP, M_INTWAIT);
2114 if (res->u.stringval == NULL)
2115 break;
2116 strcpy(res->u.stringval, stringval);
2117 break;
2118 default:
2119 panic("unknown resource type %d", type);
2124 SYSINIT(cfgload, SI_BOOT1_POST, SI_ORDER_ANY + 50, resource_cfgload, 0)
2127 /*======================================*/
2129 * Some useful method implementations to make life easier for bus drivers.
2132 void
2133 resource_list_init(struct resource_list *rl)
2135 SLIST_INIT(rl);
2138 void
2139 resource_list_free(struct resource_list *rl)
2141 struct resource_list_entry *rle;
2143 while ((rle = SLIST_FIRST(rl)) != NULL) {
2144 if (rle->res)
2145 panic("resource_list_free: resource entry is busy");
2146 SLIST_REMOVE_HEAD(rl, link);
2147 kfree(rle, M_BUS);
2151 void
2152 resource_list_add(struct resource_list *rl,
2153 int type, int rid,
2154 u_long start, u_long end, u_long count)
2156 struct resource_list_entry *rle;
2158 rle = resource_list_find(rl, type, rid);
2159 if (rle == NULL) {
2160 rle = kmalloc(sizeof(struct resource_list_entry), M_BUS,
2161 M_INTWAIT);
2162 if (!rle)
2163 panic("resource_list_add: can't record entry");
2164 SLIST_INSERT_HEAD(rl, rle, link);
2165 rle->type = type;
2166 rle->rid = rid;
2167 rle->res = NULL;
2170 if (rle->res)
2171 panic("resource_list_add: resource entry is busy");
2173 rle->start = start;
2174 rle->end = end;
2175 rle->count = count;
2178 struct resource_list_entry*
2179 resource_list_find(struct resource_list *rl,
2180 int type, int rid)
2182 struct resource_list_entry *rle;
2184 SLIST_FOREACH(rle, rl, link)
2185 if (rle->type == type && rle->rid == rid)
2186 return(rle);
2187 return(NULL);
2190 void
2191 resource_list_delete(struct resource_list *rl,
2192 int type, int rid)
2194 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2196 if (rle) {
2197 if (rle->res != NULL)
2198 panic("resource_list_delete: resource has not been released");
2199 SLIST_REMOVE(rl, rle, resource_list_entry, link);
2200 kfree(rle, M_BUS);
2204 struct resource *
2205 resource_list_alloc(struct resource_list *rl,
2206 device_t bus, device_t child,
2207 int type, int *rid,
2208 u_long start, u_long end,
2209 u_long count, u_int flags)
2211 struct resource_list_entry *rle = 0;
2212 int passthrough = (device_get_parent(child) != bus);
2213 int isdefault = (start == 0UL && end == ~0UL);
2215 if (passthrough) {
2216 return(BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2217 type, rid,
2218 start, end, count, flags));
2221 rle = resource_list_find(rl, type, *rid);
2223 if (!rle)
2224 return(0); /* no resource of that type/rid */
2226 if (rle->res)
2227 panic("resource_list_alloc: resource entry is busy");
2229 if (isdefault) {
2230 start = rle->start;
2231 count = max(count, rle->count);
2232 end = max(rle->end, start + count - 1);
2235 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2236 type, rid, start, end, count, flags);
2239 * Record the new range.
2241 if (rle->res) {
2242 rle->start = rman_get_start(rle->res);
2243 rle->end = rman_get_end(rle->res);
2244 rle->count = count;
2247 return(rle->res);
2251 resource_list_release(struct resource_list *rl,
2252 device_t bus, device_t child,
2253 int type, int rid, struct resource *res)
2255 struct resource_list_entry *rle = 0;
2256 int passthrough = (device_get_parent(child) != bus);
2257 int error;
2259 if (passthrough) {
2260 return(BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2261 type, rid, res));
2264 rle = resource_list_find(rl, type, rid);
2266 if (!rle)
2267 panic("resource_list_release: can't find resource");
2268 if (!rle->res)
2269 panic("resource_list_release: resource entry is not busy");
2271 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2272 type, rid, res);
2273 if (error)
2274 return(error);
2276 rle->res = NULL;
2277 return(0);
2281 resource_list_print_type(struct resource_list *rl, const char *name, int type,
2282 const char *format)
2284 struct resource_list_entry *rle;
2285 int printed, retval;
2287 printed = 0;
2288 retval = 0;
2289 /* Yes, this is kinda cheating */
2290 SLIST_FOREACH(rle, rl, link) {
2291 if (rle->type == type) {
2292 if (printed == 0)
2293 retval += kprintf(" %s ", name);
2294 else
2295 retval += kprintf(",");
2296 printed++;
2297 retval += kprintf(format, rle->start);
2298 if (rle->count > 1) {
2299 retval += kprintf("-");
2300 retval += kprintf(format, rle->start +
2301 rle->count - 1);
2305 return(retval);
2309 * Generic driver/device identify functions. These will install a device
2310 * rendezvous point under the parent using the same name as the driver
2311 * name, which will at a later time be probed and attached.
2313 * These functions are used when the parent does not 'scan' its bus for
2314 * matching devices, or for the particular devices using these functions,
2315 * or when the device is a pseudo or synthesized device (such as can be
2316 * found under firewire and ppbus).
2319 bus_generic_identify(driver_t *driver, device_t parent)
2321 if (parent->state == DS_ATTACHED)
2322 return (0);
2323 BUS_ADD_CHILD(parent, parent, 0, driver->name, -1);
2324 return (0);
2328 bus_generic_identify_sameunit(driver_t *driver, device_t parent)
2330 if (parent->state == DS_ATTACHED)
2331 return (0);
2332 BUS_ADD_CHILD(parent, parent, 0, driver->name, device_get_unit(parent));
2333 return (0);
2337 * Call DEVICE_IDENTIFY for each driver.
2340 bus_generic_probe(device_t dev)
2342 devclass_t dc = dev->devclass;
2343 driverlink_t dl;
2345 TAILQ_FOREACH(dl, &dc->drivers, link) {
2346 DEVICE_IDENTIFY(dl->driver, dev);
2349 return(0);
2353 * This is an aweful hack due to the isa bus and autoconf code not
2354 * probing the ISA devices until after everything else has configured.
2355 * The ISA bus did a dummy attach long ago so we have to set it back
2356 * to an earlier state so the probe thinks its the initial probe and
2357 * not a bus rescan.
2359 * XXX remove by properly defering the ISA bus scan.
2362 bus_generic_probe_hack(device_t dev)
2364 if (dev->state == DS_ATTACHED) {
2365 dev->state = DS_ALIVE;
2366 bus_generic_probe(dev);
2367 dev->state = DS_ATTACHED;
2369 return (0);
2373 bus_generic_attach(device_t dev)
2375 device_t child;
2377 TAILQ_FOREACH(child, &dev->children, link) {
2378 device_probe_and_attach(child);
2381 return(0);
2385 bus_generic_detach(device_t dev)
2387 device_t child;
2388 int error;
2390 if (dev->state != DS_ATTACHED)
2391 return(EBUSY);
2393 TAILQ_FOREACH(child, &dev->children, link)
2394 if ((error = device_detach(child)) != 0)
2395 return(error);
2397 return 0;
2401 bus_generic_shutdown(device_t dev)
2403 device_t child;
2405 TAILQ_FOREACH(child, &dev->children, link)
2406 device_shutdown(child);
2408 return(0);
2412 bus_generic_suspend(device_t dev)
2414 int error;
2415 device_t child, child2;
2417 TAILQ_FOREACH(child, &dev->children, link) {
2418 error = DEVICE_SUSPEND(child);
2419 if (error) {
2420 for (child2 = TAILQ_FIRST(&dev->children);
2421 child2 && child2 != child;
2422 child2 = TAILQ_NEXT(child2, link))
2423 DEVICE_RESUME(child2);
2424 return(error);
2427 return(0);
2431 bus_generic_resume(device_t dev)
2433 device_t child;
2435 TAILQ_FOREACH(child, &dev->children, link)
2436 DEVICE_RESUME(child);
2437 /* if resume fails, there's nothing we can usefully do... */
2439 return(0);
2443 bus_print_child_header(device_t dev, device_t child)
2445 int retval = 0;
2447 if (device_get_desc(child))
2448 retval += device_printf(child, "<%s>", device_get_desc(child));
2449 else
2450 retval += kprintf("%s", device_get_nameunit(child));
2451 if (bootverbose) {
2452 if (child->state != DS_ATTACHED)
2453 kprintf(" [tentative]");
2454 else
2455 kprintf(" [attached!]");
2457 return(retval);
2461 bus_print_child_footer(device_t dev, device_t child)
2463 return(kprintf(" on %s\n", device_get_nameunit(dev)));
2466 device_t
2467 bus_generic_add_child(device_t dev, device_t child, int order,
2468 const char *name, int unit)
2470 if (dev->parent)
2471 dev = BUS_ADD_CHILD(dev->parent, child, order, name, unit);
2472 else
2473 dev = device_add_child_ordered(child, order, name, unit);
2474 return(dev);
2479 bus_generic_print_child(device_t dev, device_t child)
2481 int retval = 0;
2483 retval += bus_print_child_header(dev, child);
2484 retval += bus_print_child_footer(dev, child);
2486 return(retval);
2490 bus_generic_read_ivar(device_t dev, device_t child, int index,
2491 uintptr_t * result)
2493 int error;
2495 if (dev->parent)
2496 error = BUS_READ_IVAR(dev->parent, child, index, result);
2497 else
2498 error = ENOENT;
2499 return (error);
2503 bus_generic_write_ivar(device_t dev, device_t child, int index,
2504 uintptr_t value)
2506 int error;
2508 if (dev->parent)
2509 error = BUS_WRITE_IVAR(dev->parent, child, index, value);
2510 else
2511 error = ENOENT;
2512 return (error);
2516 * Resource list are used for iterations, do not recurse.
2518 struct resource_list *
2519 bus_generic_get_resource_list(device_t dev, device_t child)
2521 return (NULL);
2524 void
2525 bus_generic_driver_added(device_t dev, driver_t *driver)
2527 device_t child;
2529 DEVICE_IDENTIFY(driver, dev);
2530 TAILQ_FOREACH(child, &dev->children, link) {
2531 if (child->state == DS_NOTPRESENT)
2532 device_probe_and_attach(child);
2537 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
2538 int flags, driver_intr_t *intr, void *arg,
2539 void **cookiep, lwkt_serialize_t serializer)
2541 /* Propagate up the bus hierarchy until someone handles it. */
2542 if (dev->parent)
2543 return(BUS_SETUP_INTR(dev->parent, child, irq, flags,
2544 intr, arg, cookiep, serializer));
2545 else
2546 return(EINVAL);
2550 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
2551 void *cookie)
2553 /* Propagate up the bus hierarchy until someone handles it. */
2554 if (dev->parent)
2555 return(BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
2556 else
2557 return(EINVAL);
2561 bus_generic_disable_intr(device_t dev, device_t child, void *cookie)
2563 if (dev->parent)
2564 return(BUS_DISABLE_INTR(dev->parent, child, cookie));
2565 else
2566 return(0);
2569 void
2570 bus_generic_enable_intr(device_t dev, device_t child, void *cookie)
2572 if (dev->parent)
2573 BUS_ENABLE_INTR(dev->parent, child, cookie);
2577 bus_generic_config_intr(device_t dev, device_t child, int irq, enum intr_trigger trig,
2578 enum intr_polarity pol)
2580 /* Propagate up the bus hierarchy until someone handles it. */
2581 if (dev->parent)
2582 return(BUS_CONFIG_INTR(dev->parent, child, irq, trig, pol));
2583 else
2584 return(EINVAL);
2587 struct resource *
2588 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
2589 u_long start, u_long end, u_long count, u_int flags)
2591 /* Propagate up the bus hierarchy until someone handles it. */
2592 if (dev->parent)
2593 return(BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
2594 start, end, count, flags));
2595 else
2596 return(NULL);
2600 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
2601 struct resource *r)
2603 /* Propagate up the bus hierarchy until someone handles it. */
2604 if (dev->parent)
2605 return(BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, r));
2606 else
2607 return(EINVAL);
2611 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
2612 struct resource *r)
2614 /* Propagate up the bus hierarchy until someone handles it. */
2615 if (dev->parent)
2616 return(BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, r));
2617 else
2618 return(EINVAL);
2622 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
2623 int rid, struct resource *r)
2625 /* Propagate up the bus hierarchy until someone handles it. */
2626 if (dev->parent)
2627 return(BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
2628 r));
2629 else
2630 return(EINVAL);
2634 bus_generic_get_resource(device_t dev, device_t child, int type, int rid,
2635 u_long *startp, u_long *countp)
2637 int error;
2639 error = ENOENT;
2640 if (dev->parent) {
2641 error = BUS_GET_RESOURCE(dev->parent, child, type, rid,
2642 startp, countp);
2644 return (error);
2648 bus_generic_set_resource(device_t dev, device_t child, int type, int rid,
2649 u_long start, u_long count)
2651 int error;
2653 error = EINVAL;
2654 if (dev->parent) {
2655 error = BUS_SET_RESOURCE(dev->parent, child, type, rid,
2656 start, count);
2658 return (error);
2661 void
2662 bus_generic_delete_resource(device_t dev, device_t child, int type, int rid)
2664 if (dev->parent)
2665 BUS_DELETE_RESOURCE(dev, child, type, rid);
2669 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
2670 u_long *startp, u_long *countp)
2672 struct resource_list *rl = NULL;
2673 struct resource_list_entry *rle = NULL;
2675 rl = BUS_GET_RESOURCE_LIST(dev, child);
2676 if (!rl)
2677 return(EINVAL);
2679 rle = resource_list_find(rl, type, rid);
2680 if (!rle)
2681 return(ENOENT);
2683 if (startp)
2684 *startp = rle->start;
2685 if (countp)
2686 *countp = rle->count;
2688 return(0);
2692 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
2693 u_long start, u_long count)
2695 struct resource_list *rl = NULL;
2697 rl = BUS_GET_RESOURCE_LIST(dev, child);
2698 if (!rl)
2699 return(EINVAL);
2701 resource_list_add(rl, type, rid, start, (start + count - 1), count);
2703 return(0);
2706 void
2707 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
2709 struct resource_list *rl = NULL;
2711 rl = BUS_GET_RESOURCE_LIST(dev, child);
2712 if (!rl)
2713 return;
2715 resource_list_delete(rl, type, rid);
2719 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
2720 int rid, struct resource *r)
2722 struct resource_list *rl = NULL;
2724 rl = BUS_GET_RESOURCE_LIST(dev, child);
2725 if (!rl)
2726 return(EINVAL);
2728 return(resource_list_release(rl, dev, child, type, rid, r));
2731 struct resource *
2732 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
2733 int *rid, u_long start, u_long end, u_long count, u_int flags)
2735 struct resource_list *rl = NULL;
2737 rl = BUS_GET_RESOURCE_LIST(dev, child);
2738 if (!rl)
2739 return(NULL);
2741 return(resource_list_alloc(rl, dev, child, type, rid,
2742 start, end, count, flags));
2746 bus_generic_child_present(device_t bus, device_t child)
2748 return(BUS_CHILD_PRESENT(device_get_parent(bus), bus));
2753 * Some convenience functions to make it easier for drivers to use the
2754 * resource-management functions. All these really do is hide the
2755 * indirection through the parent's method table, making for slightly
2756 * less-wordy code. In the future, it might make sense for this code
2757 * to maintain some sort of a list of resources allocated by each device.
2760 bus_alloc_resources(device_t dev, struct resource_spec *rs,
2761 struct resource **res)
2763 int i;
2765 for (i = 0; rs[i].type != -1; i++)
2766 res[i] = NULL;
2767 for (i = 0; rs[i].type != -1; i++) {
2768 res[i] = bus_alloc_resource_any(dev,
2769 rs[i].type, &rs[i].rid, rs[i].flags);
2770 if (res[i] == NULL) {
2771 bus_release_resources(dev, rs, res);
2772 return (ENXIO);
2775 return (0);
2778 void
2779 bus_release_resources(device_t dev, const struct resource_spec *rs,
2780 struct resource **res)
2782 int i;
2784 for (i = 0; rs[i].type != -1; i++)
2785 if (res[i] != NULL) {
2786 bus_release_resource(
2787 dev, rs[i].type, rs[i].rid, res[i]);
2788 res[i] = NULL;
2792 struct resource *
2793 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
2794 u_long count, u_int flags)
2796 if (dev->parent == 0)
2797 return(0);
2798 return(BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
2799 count, flags));
2803 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
2805 if (dev->parent == 0)
2806 return(EINVAL);
2807 return(BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2811 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
2813 if (dev->parent == 0)
2814 return(EINVAL);
2815 return(BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2819 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
2821 if (dev->parent == 0)
2822 return(EINVAL);
2823 return(BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
2827 bus_setup_intr(device_t dev, struct resource *r, int flags,
2828 driver_intr_t handler, void *arg,
2829 void **cookiep, lwkt_serialize_t serializer)
2831 if (dev->parent == 0)
2832 return(EINVAL);
2833 return(BUS_SETUP_INTR(dev->parent, dev, r, flags, handler, arg,
2834 cookiep, serializer));
2838 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
2840 if (dev->parent == 0)
2841 return(EINVAL);
2842 return(BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
2845 void
2846 bus_enable_intr(device_t dev, void *cookie)
2848 if (dev->parent)
2849 BUS_ENABLE_INTR(dev->parent, dev, cookie);
2853 bus_disable_intr(device_t dev, void *cookie)
2855 if (dev->parent)
2856 return(BUS_DISABLE_INTR(dev->parent, dev, cookie));
2857 else
2858 return(0);
2862 bus_set_resource(device_t dev, int type, int rid,
2863 u_long start, u_long count)
2865 return(BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
2866 start, count));
2870 bus_get_resource(device_t dev, int type, int rid,
2871 u_long *startp, u_long *countp)
2873 return(BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2874 startp, countp));
2877 u_long
2878 bus_get_resource_start(device_t dev, int type, int rid)
2880 u_long start, count;
2881 int error;
2883 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2884 &start, &count);
2885 if (error)
2886 return(0);
2887 return(start);
2890 u_long
2891 bus_get_resource_count(device_t dev, int type, int rid)
2893 u_long start, count;
2894 int error;
2896 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2897 &start, &count);
2898 if (error)
2899 return(0);
2900 return(count);
2903 void
2904 bus_delete_resource(device_t dev, int type, int rid)
2906 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
2910 bus_child_present(device_t child)
2912 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
2916 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
2918 device_t parent;
2920 parent = device_get_parent(child);
2921 if (parent == NULL) {
2922 *buf = '\0';
2923 return (0);
2925 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
2929 bus_child_location_str(device_t child, char *buf, size_t buflen)
2931 device_t parent;
2933 parent = device_get_parent(child);
2934 if (parent == NULL) {
2935 *buf = '\0';
2936 return (0);
2938 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
2941 static int
2942 root_print_child(device_t dev, device_t child)
2944 return(0);
2947 static int
2948 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
2949 void **cookiep, lwkt_serialize_t serializer)
2952 * If an interrupt mapping gets to here something bad has happened.
2954 panic("root_setup_intr");
2958 * If we get here, assume that the device is permanant and really is
2959 * present in the system. Removable bus drivers are expected to intercept
2960 * this call long before it gets here. We return -1 so that drivers that
2961 * really care can check vs -1 or some ERRNO returned higher in the food
2962 * chain.
2964 static int
2965 root_child_present(device_t dev, device_t child)
2967 return(-1);
2971 * XXX NOTE! other defaults may be set in bus_if.m
2973 static kobj_method_t root_methods[] = {
2974 /* Device interface */
2975 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
2976 KOBJMETHOD(device_suspend, bus_generic_suspend),
2977 KOBJMETHOD(device_resume, bus_generic_resume),
2979 /* Bus interface */
2980 KOBJMETHOD(bus_add_child, bus_generic_add_child),
2981 KOBJMETHOD(bus_print_child, root_print_child),
2982 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
2983 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
2984 KOBJMETHOD(bus_setup_intr, root_setup_intr),
2985 KOBJMETHOD(bus_child_present, root_child_present),
2987 { 0, 0 }
2990 static driver_t root_driver = {
2991 "root",
2992 root_methods,
2993 1, /* no softc */
2996 device_t root_bus;
2997 devclass_t root_devclass;
2999 static int
3000 root_bus_module_handler(module_t mod, int what, void* arg)
3002 switch (what) {
3003 case MOD_LOAD:
3004 TAILQ_INIT(&bus_data_devices);
3005 root_bus = make_device(NULL, "root", 0);
3006 root_bus->desc = "System root bus";
3007 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
3008 root_bus->driver = &root_driver;
3009 root_bus->state = DS_ALIVE;
3010 root_devclass = devclass_find_internal("root", NULL, FALSE);
3011 devinit();
3012 return(0);
3014 case MOD_SHUTDOWN:
3015 device_shutdown(root_bus);
3016 return(0);
3017 default:
3018 return(0);
3022 static moduledata_t root_bus_mod = {
3023 "rootbus",
3024 root_bus_module_handler,
3027 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
3029 void
3030 root_bus_configure(void)
3032 int warncount;
3033 device_t dev;
3035 PDEBUG(("."));
3038 * handle device_identify based device attachments to the root_bus
3039 * (typically nexus).
3041 bus_generic_probe(root_bus);
3044 * Probe and attach the devices under root_bus.
3046 TAILQ_FOREACH(dev, &root_bus->children, link) {
3047 device_probe_and_attach(dev);
3051 * Wait for all asynchronous attaches to complete. If we don't
3052 * our legacy ISA bus scan could steal device unit numbers or
3053 * even I/O ports.
3055 warncount = 10;
3056 if (numasyncthreads)
3057 kprintf("Waiting for async drivers to attach\n");
3058 while (numasyncthreads > 0) {
3059 if (tsleep(&numasyncthreads, 0, "rootbus", hz) == EWOULDBLOCK)
3060 --warncount;
3061 if (warncount == 0) {
3062 kprintf("Warning: Still waiting for %d "
3063 "drivers to attach\n", numasyncthreads);
3064 } else if (warncount == -30) {
3065 kprintf("Giving up on %d drivers\n", numasyncthreads);
3066 break;
3069 root_bus->state = DS_ATTACHED;
3073 driver_module_handler(module_t mod, int what, void *arg)
3075 int error;
3076 struct driver_module_data *dmd;
3077 devclass_t bus_devclass;
3078 kobj_class_t driver;
3079 const char *parentname;
3081 dmd = (struct driver_module_data *)arg;
3082 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
3083 error = 0;
3085 switch (what) {
3086 case MOD_LOAD:
3087 if (dmd->dmd_chainevh)
3088 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3090 driver = dmd->dmd_driver;
3091 PDEBUG(("Loading module: driver %s on bus %s",
3092 DRIVERNAME(driver), dmd->dmd_busname));
3095 * If the driver has any base classes, make the
3096 * devclass inherit from the devclass of the driver's
3097 * first base class. This will allow the system to
3098 * search for drivers in both devclasses for children
3099 * of a device using this driver.
3101 if (driver->baseclasses)
3102 parentname = driver->baseclasses[0]->name;
3103 else
3104 parentname = NULL;
3105 *dmd->dmd_devclass = devclass_find_internal(driver->name,
3106 parentname, TRUE);
3108 error = devclass_add_driver(bus_devclass, driver);
3109 if (error)
3110 break;
3111 break;
3113 case MOD_UNLOAD:
3114 PDEBUG(("Unloading module: driver %s from bus %s",
3115 DRIVERNAME(dmd->dmd_driver), dmd->dmd_busname));
3116 error = devclass_delete_driver(bus_devclass, dmd->dmd_driver);
3118 if (!error && dmd->dmd_chainevh)
3119 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3120 break;
3123 return (error);
3126 #ifdef BUS_DEBUG
3129 * The _short versions avoid iteration by not calling anything that prints
3130 * more than oneliners. I love oneliners.
3133 static void
3134 print_device_short(device_t dev, int indent)
3136 if (!dev)
3137 return;
3139 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
3140 dev->unit, dev->desc,
3141 (dev->parent? "":"no "),
3142 (TAILQ_EMPTY(&dev->children)? "no ":""),
3143 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
3144 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
3145 (dev->flags&DF_WILDCARD? "wildcard,":""),
3146 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
3147 (dev->ivars? "":"no "),
3148 (dev->softc? "":"no "),
3149 dev->busy));
3152 static void
3153 print_device(device_t dev, int indent)
3155 if (!dev)
3156 return;
3158 print_device_short(dev, indent);
3160 indentprintf(("Parent:\n"));
3161 print_device_short(dev->parent, indent+1);
3162 indentprintf(("Driver:\n"));
3163 print_driver_short(dev->driver, indent+1);
3164 indentprintf(("Devclass:\n"));
3165 print_devclass_short(dev->devclass, indent+1);
3169 * Print the device and all its children (indented).
3171 void
3172 print_device_tree_short(device_t dev, int indent)
3174 device_t child;
3176 if (!dev)
3177 return;
3179 print_device_short(dev, indent);
3181 TAILQ_FOREACH(child, &dev->children, link)
3182 print_device_tree_short(child, indent+1);
3186 * Print the device and all its children (indented).
3188 void
3189 print_device_tree(device_t dev, int indent)
3191 device_t child;
3193 if (!dev)
3194 return;
3196 print_device(dev, indent);
3198 TAILQ_FOREACH(child, &dev->children, link)
3199 print_device_tree(child, indent+1);
3202 static void
3203 print_driver_short(driver_t *driver, int indent)
3205 if (!driver)
3206 return;
3208 indentprintf(("driver %s: softc size = %zu\n",
3209 driver->name, driver->size));
3212 static void
3213 print_driver(driver_t *driver, int indent)
3215 if (!driver)
3216 return;
3218 print_driver_short(driver, indent);
3222 static void
3223 print_driver_list(driver_list_t drivers, int indent)
3225 driverlink_t driver;
3227 TAILQ_FOREACH(driver, &drivers, link)
3228 print_driver(driver->driver, indent);
3231 static void
3232 print_devclass_short(devclass_t dc, int indent)
3234 if (!dc)
3235 return;
3237 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
3240 static void
3241 print_devclass(devclass_t dc, int indent)
3243 int i;
3245 if (!dc)
3246 return;
3248 print_devclass_short(dc, indent);
3249 indentprintf(("Drivers:\n"));
3250 print_driver_list(dc->drivers, indent+1);
3252 indentprintf(("Devices:\n"));
3253 for (i = 0; i < dc->maxunit; i++)
3254 if (dc->devices[i])
3255 print_device(dc->devices[i], indent+1);
3258 void
3259 print_devclass_list_short(void)
3261 devclass_t dc;
3263 kprintf("Short listing of devclasses, drivers & devices:\n");
3264 TAILQ_FOREACH(dc, &devclasses, link) {
3265 print_devclass_short(dc, 0);
3269 void
3270 print_devclass_list(void)
3272 devclass_t dc;
3274 kprintf("Full listing of devclasses, drivers & devices:\n");
3275 TAILQ_FOREACH(dc, &devclasses, link) {
3276 print_devclass(dc, 0);
3280 #endif
3283 * Check to see if a device is disabled via a disabled hint.
3286 resource_disabled(const char *name, int unit)
3288 int error, value;
3290 error = resource_int_value(name, unit, "disabled", &value);
3291 if (error)
3292 return(0);
3293 return(value);
3297 * User-space access to the device tree.
3299 * We implement a small set of nodes:
3301 * hw.bus Single integer read method to obtain the
3302 * current generation count.
3303 * hw.bus.devices Reads the entire device tree in flat space.
3304 * hw.bus.rman Resource manager interface
3306 * We might like to add the ability to scan devclasses and/or drivers to
3307 * determine what else is currently loaded/available.
3310 static int
3311 sysctl_bus(SYSCTL_HANDLER_ARGS)
3313 struct u_businfo ubus;
3315 ubus.ub_version = BUS_USER_VERSION;
3316 ubus.ub_generation = bus_data_generation;
3318 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
3320 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
3321 "bus-related data");
3323 static int
3324 sysctl_devices(SYSCTL_HANDLER_ARGS)
3326 int *name = (int *)arg1;
3327 u_int namelen = arg2;
3328 int index;
3329 struct device *dev;
3330 struct u_device udev; /* XXX this is a bit big */
3331 int error;
3333 if (namelen != 2)
3334 return (EINVAL);
3336 if (bus_data_generation_check(name[0]))
3337 return (EINVAL);
3339 index = name[1];
3342 * Scan the list of devices, looking for the requested index.
3344 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
3345 if (index-- == 0)
3346 break;
3348 if (dev == NULL)
3349 return (ENOENT);
3352 * Populate the return array.
3354 bzero(&udev, sizeof(udev));
3355 udev.dv_handle = (uintptr_t)dev;
3356 udev.dv_parent = (uintptr_t)dev->parent;
3357 if (dev->nameunit != NULL)
3358 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
3359 if (dev->desc != NULL)
3360 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
3361 if (dev->driver != NULL && dev->driver->name != NULL)
3362 strlcpy(udev.dv_drivername, dev->driver->name,
3363 sizeof(udev.dv_drivername));
3364 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
3365 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
3366 udev.dv_devflags = dev->devflags;
3367 udev.dv_flags = dev->flags;
3368 udev.dv_state = dev->state;
3369 error = SYSCTL_OUT(req, &udev, sizeof(udev));
3370 return (error);
3373 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
3374 "system device tree");
3377 bus_data_generation_check(int generation)
3379 if (generation != bus_data_generation)
3380 return (1);
3382 /* XXX generate optimised lists here? */
3383 return (0);
3386 void
3387 bus_data_generation_update(void)
3389 bus_data_generation++;