subr_bus.c revision 122020
1/*- 2 * Copyright (c) 1997,1998,2003 Doug Rabson 3 * All rights reserved. 4 * 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. 13 * 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. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/sys/kern/subr_bus.c 122020 2003-11-04 02:01:42Z sam $"); 29 30#include "opt_bus.h" 31 32#include <sys/param.h> 33#include <sys/conf.h> 34#include <sys/filio.h> 35#include <sys/lock.h> 36#include <sys/kernel.h> 37#include <sys/kobj.h> 38#include <sys/malloc.h> 39#include <sys/module.h> 40#include <sys/mutex.h> 41#include <sys/poll.h> 42#include <sys/proc.h> 43#include <sys/condvar.h> 44#include <sys/queue.h> 45#include <machine/bus.h> 46#include <sys/rman.h> 47#include <sys/selinfo.h> 48#include <sys/signalvar.h> 49#include <sys/sysctl.h> 50#include <sys/systm.h> 51#include <sys/uio.h> 52#include <sys/bus.h> 53 54#include <machine/stdarg.h> 55 56#include <vm/uma.h> 57 58SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL); 59 60/* 61 * Used to attach drivers to devclasses. 62 */ 63typedef struct driverlink *driverlink_t; 64struct driverlink { 65 kobj_class_t driver; 66 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */ 67}; 68 69/* 70 * Forward declarations 71 */ 72typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t; 73typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t; 74typedef TAILQ_HEAD(device_list, device) device_list_t; 75 76struct devclass { 77 TAILQ_ENTRY(devclass) link; 78 devclass_t parent; /* parent in devclass hierarchy */ 79 driver_list_t drivers; /* bus devclasses store drivers for bus */ 80 char *name; 81 device_t *devices; /* array of devices indexed by unit */ 82 int maxunit; /* size of devices array */ 83}; 84 85/* 86 * Implementation of device. 87 */ 88struct device { 89 /* 90 * A device is a kernel object. The first field must be the 91 * current ops table for the object. 92 */ 93 KOBJ_FIELDS; 94 95 /* 96 * Device hierarchy. 97 */ 98 TAILQ_ENTRY(device) link; /* list of devices in parent */ 99 TAILQ_ENTRY(device) devlink; /* global device list membership */ 100 device_t parent; 101 device_list_t children; /* list of subordinate devices */ 102 103 /* 104 * Details of this device. 105 */ 106 driver_t *driver; 107 devclass_t devclass; /* device class which we are in */ 108 int unit; 109 char* nameunit; /* name+unit e.g. foodev0 */ 110 char* desc; /* driver specific description */ 111 int busy; /* count of calls to device_busy() */ 112 device_state_t state; 113 u_int32_t devflags; /* api level flags for device_get_flags() */ 114 u_short flags; 115#define DF_ENABLED 1 /* device should be probed/attached */ 116#define DF_FIXEDCLASS 2 /* devclass specified at create time */ 117#define DF_WILDCARD 4 /* unit was originally wildcard */ 118#define DF_DESCMALLOCED 8 /* description was malloced */ 119#define DF_QUIET 16 /* don't print verbose attach message */ 120#define DF_DONENOMATCH 32 /* don't execute DEVICE_NOMATCH again */ 121#define DF_EXTERNALSOFTC 64 /* softc not allocated by us */ 122 u_char order; /* order from device_add_child_ordered() */ 123 u_char pad; 124 void *ivars; 125 void *softc; 126}; 127 128struct device_op_desc { 129 unsigned int offset; /* offset in driver ops */ 130 struct method* method; /* internal method implementation */ 131 devop_t deflt; /* default implementation */ 132 const char* name; /* unique name (for registration) */ 133}; 134 135static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures"); 136static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc"); 137 138#ifdef BUS_DEBUG 139 140static int bus_debug = 1; 141TUNABLE_INT("bus.debug", &bus_debug); 142SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RW, &bus_debug, 0, 143 "Debug bus code"); 144 145#define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");} 146#define DEVICENAME(d) ((d)? device_get_name(d): "no device") 147#define DRIVERNAME(d) ((d)? d->name : "no driver") 148#define DEVCLANAME(d) ((d)? d->name : "no devclass") 149 150/* Produce the indenting, indent*2 spaces plus a '.' ahead of that to 151 * prevent syslog from deleting initial spaces 152 */ 153#define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0) 154 155static void print_device_short(device_t dev, int indent); 156static void print_device(device_t dev, int indent); 157void print_device_tree_short(device_t dev, int indent); 158void print_device_tree(device_t dev, int indent); 159static void print_driver_short(driver_t *driver, int indent); 160static void print_driver(driver_t *driver, int indent); 161static void print_driver_list(driver_list_t drivers, int indent); 162static void print_devclass_short(devclass_t dc, int indent); 163static void print_devclass(devclass_t dc, int indent); 164void print_devclass_list_short(void); 165void print_devclass_list(void); 166 167#else 168/* Make the compiler ignore the function calls */ 169#define PDEBUG(a) /* nop */ 170#define DEVICENAME(d) /* nop */ 171#define DRIVERNAME(d) /* nop */ 172#define DEVCLANAME(d) /* nop */ 173 174#define print_device_short(d,i) /* nop */ 175#define print_device(d,i) /* nop */ 176#define print_device_tree_short(d,i) /* nop */ 177#define print_device_tree(d,i) /* nop */ 178#define print_driver_short(d,i) /* nop */ 179#define print_driver(d,i) /* nop */ 180#define print_driver_list(d,i) /* nop */ 181#define print_devclass_short(d,i) /* nop */ 182#define print_devclass(d,i) /* nop */ 183#define print_devclass_list_short() /* nop */ 184#define print_devclass_list() /* nop */ 185#endif 186 187/* 188 * /dev/devctl implementation 189 */ 190 191/* 192 * This design allows only one reader for /dev/devctl. This is not desirable 193 * in the long run, but will get a lot of hair out of this implementation. 194 * Maybe we should make this device a clonable device. 195 * 196 * Also note: we specifically do not attach a device to the device_t tree 197 * to avoid potential chicken and egg problems. One could argue that all 198 * of this belongs to the root node. One could also further argue that the 199 * sysctl interface that we have not might more properly be an ioctl 200 * interface, but at this stage of the game, I'm not inclined to rock that 201 * boat. 202 * 203 * I'm also not sure that the SIGIO support is done correctly or not, as 204 * I copied it from a driver that had SIGIO support that likely hasn't been 205 * tested since 3.4 or 2.2.8! 206 */ 207 208static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS); 209static int devctl_disable = 0; 210TUNABLE_INT("hw.bus.devctl_disable", &devctl_disable); 211SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, 212 CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0, sysctl_devctl_disable, 213 "I", "devctl disable"); 214 215static d_open_t devopen; 216static d_close_t devclose; 217static d_read_t devread; 218static d_ioctl_t devioctl; 219static d_poll_t devpoll; 220 221#define CDEV_MAJOR 173 222static struct cdevsw dev_cdevsw = { 223 .d_open = devopen, 224 .d_close = devclose, 225 .d_read = devread, 226 .d_ioctl = devioctl, 227 .d_poll = devpoll, 228 .d_name = "devctl", 229 .d_maj = CDEV_MAJOR, 230}; 231 232struct dev_event_info 233{ 234 char *dei_data; 235 TAILQ_ENTRY(dev_event_info) dei_link; 236}; 237 238TAILQ_HEAD(devq, dev_event_info); 239 240static struct dev_softc 241{ 242 int inuse; 243 int nonblock; 244 struct mtx mtx; 245 struct cv cv; 246 struct selinfo sel; 247 struct devq devq; 248 struct proc *async_proc; 249} devsoftc; 250 251static dev_t devctl_dev; 252 253static void 254devinit(void) 255{ 256 devctl_dev = make_dev(&dev_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, 257 "devctl"); 258 mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF); 259 cv_init(&devsoftc.cv, "dev cv"); 260 TAILQ_INIT(&devsoftc.devq); 261} 262 263static int 264devopen(dev_t dev, int oflags, int devtype, d_thread_t *td) 265{ 266 if (devsoftc.inuse) 267 return (EBUSY); 268 /* move to init */ 269 devsoftc.inuse = 1; 270 devsoftc.nonblock = 0; 271 devsoftc.async_proc = NULL; 272 return (0); 273} 274 275static int 276devclose(dev_t dev, int fflag, int devtype, d_thread_t *td) 277{ 278 devsoftc.inuse = 0; 279 mtx_lock(&devsoftc.mtx); 280 cv_broadcast(&devsoftc.cv); 281 mtx_unlock(&devsoftc.mtx); 282 283 return (0); 284} 285 286/* 287 * The read channel for this device is used to report changes to 288 * userland in realtime. We are required to free the data as well as 289 * the n1 object because we allocate them separately. Also note that 290 * we return one record at a time. If you try to read this device a 291 * character at a time, you will loose the rest of the data. Listening 292 * programs are expected to cope. 293 */ 294static int 295devread(dev_t dev, struct uio *uio, int ioflag) 296{ 297 struct dev_event_info *n1; 298 int rv; 299 300 mtx_lock(&devsoftc.mtx); 301 while (TAILQ_EMPTY(&devsoftc.devq)) { 302 if (devsoftc.nonblock) { 303 mtx_unlock(&devsoftc.mtx); 304 return (EAGAIN); 305 } 306 rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx); 307 if (rv) { 308 /* 309 * Need to translate ERESTART to EINTR here? -- jake 310 */ 311 mtx_unlock(&devsoftc.mtx); 312 return (rv); 313 } 314 } 315 n1 = TAILQ_FIRST(&devsoftc.devq); 316 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link); 317 mtx_unlock(&devsoftc.mtx); 318 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio); 319 free(n1->dei_data, M_BUS); 320 free(n1, M_BUS); 321 return (rv); 322} 323 324static int 325devioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, d_thread_t *td) 326{ 327 switch (cmd) { 328 329 case FIONBIO: 330 if (*(int*)data) 331 devsoftc.nonblock = 1; 332 else 333 devsoftc.nonblock = 0; 334 return (0); 335 case FIOASYNC: 336 if (*(int*)data) 337 devsoftc.async_proc = td->td_proc; 338 else 339 devsoftc.async_proc = NULL; 340 return (0); 341 342 /* (un)Support for other fcntl() calls. */ 343 case FIOCLEX: 344 case FIONCLEX: 345 case FIONREAD: 346 case FIOSETOWN: 347 case FIOGETOWN: 348 default: 349 break; 350 } 351 return (ENOTTY); 352} 353 354static int 355devpoll(dev_t dev, int events, d_thread_t *td) 356{ 357 int revents = 0; 358 359 mtx_lock(&devsoftc.mtx); 360 if (events & (POLLIN | POLLRDNORM)) { 361 if (!TAILQ_EMPTY(&devsoftc.devq)) 362 revents = events & (POLLIN | POLLRDNORM); 363 else 364 selrecord(td, &devsoftc.sel); 365 } 366 mtx_unlock(&devsoftc.mtx); 367 368 return (revents); 369} 370 371/* 372 * Generic interface to queue data to the devctl device. It is 373 * assumed that data is properly formatted. It is further assumed 374 * that data is allocated. 375 */ 376void 377devctl_queue_data(char *data) 378{ 379 struct dev_event_info *n1 = NULL; 380 struct proc *p; 381 382 n1 = malloc(sizeof(*n1), M_BUS, M_NOWAIT); 383 if (n1 == NULL) 384 return; 385 n1->dei_data = data; 386 mtx_lock(&devsoftc.mtx); 387 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link); 388 cv_broadcast(&devsoftc.cv); 389 mtx_unlock(&devsoftc.mtx); 390 selwakeup(&devsoftc.sel); 391 p = devsoftc.async_proc; 392 if (p != NULL) { 393 PROC_LOCK(p); 394 psignal(p, SIGIO); 395 PROC_UNLOCK(p); 396 } 397} 398 399/* 400 * Send a 'notification' to userland, using standard ways 401 */ 402void 403devctl_notify(const char *system, const char *subsystem, const char *type, 404 const char *data) 405{ 406 int len = 0; 407 char *msg; 408 409 if (system == NULL) 410 return; /* BOGUS! Must specify system. */ 411 if (subsystem == NULL) 412 return; /* BOGUS! Must specify subsystem. */ 413 if (type == NULL) 414 return; /* BOGUS! Must specify type. */ 415 len += strlen(" system=") + strlen(system); 416 len += strlen(" subsystem=") + strlen(subsystem); 417 len += strlen(" type=") + strlen(type); 418 /* add in the data message plus newline. */ 419 if (data != NULL) 420 len += strlen(data); 421 len += 3; /* '!', '\n', and NUL */ 422 msg = malloc(len, M_BUS, M_NOWAIT); 423 if (msg == NULL) 424 return; /* Drop it on the floor */ 425 snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n", system, 426 subsystem, type, data); 427 devctl_queue_data(msg); 428} 429 430/* 431 * Common routine that tries to make sending messages as easy as possible. 432 * We allocate memory for the data, copy strings into that, but do not 433 * free it unless there's an error. The dequeue part of the driver should 434 * free the data. We don't send data when the device is disabled. We do 435 * send data, even when we have no listeners, because we wish to avoid 436 * races relating to startup and restart of listening applications. 437 */ 438static void 439devaddq(const char *type, const char *what, device_t dev) 440{ 441 char *data = NULL; 442 char *loc; 443 const char *parstr; 444 445 if (devctl_disable) 446 return; 447 data = malloc(1024, M_BUS, M_NOWAIT); 448 if (data == NULL) 449 goto bad; 450 loc = malloc(1024, M_BUS, M_NOWAIT); 451 if (loc == NULL) 452 goto bad; 453 *loc = '\0'; 454 bus_child_location_str(dev, loc, 1024); 455 if (device_get_parent(dev) == NULL) 456 parstr = "."; /* Or '/' ? */ 457 else 458 parstr = device_get_nameunit(device_get_parent(dev)); 459 snprintf(data, 1024, "%s%s at %s on %s\n", type, what, loc, parstr); 460 free(loc, M_BUS); 461 devctl_queue_data(data); 462 return; 463bad: 464 free(data, M_BUS); 465 return; 466} 467 468/* 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). 474 */ 475static void 476devadded(device_t dev) 477{ 478 devaddq("+", device_get_nameunit(dev), dev); 479} 480 481/* 482 * A device was removed from the tree. We are called just before this 483 * happens. 484 */ 485static void 486devremoved(device_t dev) 487{ 488 devaddq("-", device_get_nameunit(dev), dev); 489} 490 491/* 492 * Called when there's no match for this device. This is only called 493 * the first time that no match happens, so we don't keep getitng this 494 * message. Should that prove to be undesirable, we can change it. 495 * This is called when all drivers that can attach to a given bus 496 * decline to accept this device. Other errrors may not be detected. 497 */ 498static void 499devnomatch(device_t dev) 500{ 501 char *pnp = NULL; 502 503 pnp = malloc(1024, M_BUS, M_NOWAIT); 504 if (pnp == NULL) 505 return; 506 *pnp = '\0'; 507 bus_child_pnpinfo_str(dev, pnp, 1024); 508 devaddq("?", pnp, dev); 509 free(pnp, M_BUS); 510 return; 511} 512 513static int 514sysctl_devctl_disable(SYSCTL_HANDLER_ARGS) 515{ 516 struct dev_event_info *n1; 517 int dis, error; 518 519 dis = devctl_disable; 520 error = sysctl_handle_int(oidp, &dis, 0, req); 521 if (error || !req->newptr) 522 return (error); 523 mtx_lock(&devsoftc.mtx); 524 devctl_disable = dis; 525 if (dis) { 526 while (!TAILQ_EMPTY(&devsoftc.devq)) { 527 n1 = TAILQ_FIRST(&devsoftc.devq); 528 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link); 529 free(n1->dei_data, M_BUS); 530 free(n1, M_BUS); 531 } 532 } 533 mtx_unlock(&devsoftc.mtx); 534 return (0); 535} 536 537/* End of /dev/devctl code */ 538 539TAILQ_HEAD(,device) bus_data_devices; 540static int bus_data_generation = 1; 541 542kobj_method_t null_methods[] = { 543 { 0, 0 } 544}; 545 546DEFINE_CLASS(null, null_methods, 0); 547 548/* 549 * Devclass implementation 550 */ 551 552static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses); 553 554static devclass_t 555devclass_find_internal(const char *classname, const char *parentname, 556 int create) 557{ 558 devclass_t dc; 559 560 PDEBUG(("looking for %s", classname)); 561 if (!classname) 562 return (NULL); 563 564 TAILQ_FOREACH(dc, &devclasses, link) { 565 if (!strcmp(dc->name, classname)) 566 break; 567 } 568 569 if (create && !dc) { 570 PDEBUG(("creating %s", classname)); 571 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1, 572 M_BUS, M_NOWAIT|M_ZERO); 573 if (!dc) 574 return (NULL); 575 dc->parent = NULL; 576 dc->name = (char*) (dc + 1); 577 strcpy(dc->name, classname); 578 TAILQ_INIT(&dc->drivers); 579 TAILQ_INSERT_TAIL(&devclasses, dc, link); 580 581 bus_data_generation_update(); 582 } 583 if (parentname && dc && !dc->parent) { 584 dc->parent = devclass_find_internal(parentname, 0, FALSE); 585 } 586 587 return (dc); 588} 589 590devclass_t 591devclass_create(const char *classname) 592{ 593 return (devclass_find_internal(classname, 0, TRUE)); 594} 595 596devclass_t 597devclass_find(const char *classname) 598{ 599 return (devclass_find_internal(classname, 0, FALSE)); 600} 601 602int 603devclass_add_driver(devclass_t dc, driver_t *driver) 604{ 605 driverlink_t dl; 606 int i; 607 608 PDEBUG(("%s", DRIVERNAME(driver))); 609 610 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO); 611 if (!dl) 612 return (ENOMEM); 613 614 /* 615 * Compile the driver's methods. Also increase the reference count 616 * so that the class doesn't get freed when the last instance 617 * goes. This means we can safely use static methods and avoids a 618 * double-free in devclass_delete_driver. 619 */ 620 kobj_class_compile((kobj_class_t) driver); 621 622 /* 623 * Make sure the devclass which the driver is implementing exists. 624 */ 625 devclass_find_internal(driver->name, 0, TRUE); 626 627 dl->driver = driver; 628 TAILQ_INSERT_TAIL(&dc->drivers, dl, link); 629 driver->refs++; 630 631 /* 632 * Call BUS_DRIVER_ADDED for any existing busses in this class. 633 */ 634 for (i = 0; i < dc->maxunit; i++) 635 if (dc->devices[i]) 636 BUS_DRIVER_ADDED(dc->devices[i], driver); 637 638 bus_data_generation_update(); 639 return (0); 640} 641 642int 643devclass_delete_driver(devclass_t busclass, driver_t *driver) 644{ 645 devclass_t dc = devclass_find(driver->name); 646 driverlink_t dl; 647 device_t dev; 648 int i; 649 int error; 650 651 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass))); 652 653 if (!dc) 654 return (0); 655 656 /* 657 * Find the link structure in the bus' list of drivers. 658 */ 659 TAILQ_FOREACH(dl, &busclass->drivers, link) { 660 if (dl->driver == driver) 661 break; 662 } 663 664 if (!dl) { 665 PDEBUG(("%s not found in %s list", driver->name, 666 busclass->name)); 667 return (ENOENT); 668 } 669 670 /* 671 * Disassociate from any devices. We iterate through all the 672 * devices in the devclass of the driver and detach any which are 673 * using the driver and which have a parent in the devclass which 674 * we are deleting from. 675 * 676 * Note that since a driver can be in multiple devclasses, we 677 * should not detach devices which are not children of devices in 678 * the affected devclass. 679 */ 680 for (i = 0; i < dc->maxunit; i++) { 681 if (dc->devices[i]) { 682 dev = dc->devices[i]; 683 if (dev->driver == driver && dev->parent && 684 dev->parent->devclass == busclass) { 685 if ((error = device_detach(dev)) != 0) 686 return (error); 687 device_set_driver(dev, NULL); 688 } 689 } 690 } 691 692 TAILQ_REMOVE(&busclass->drivers, dl, link); 693 free(dl, M_BUS); 694 695 driver->refs--; 696 if (driver->refs == 0) 697 kobj_class_free((kobj_class_t) driver); 698 699 bus_data_generation_update(); 700 return (0); 701} 702 703static driverlink_t 704devclass_find_driver_internal(devclass_t dc, const char *classname) 705{ 706 driverlink_t dl; 707 708 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc))); 709 710 TAILQ_FOREACH(dl, &dc->drivers, link) { 711 if (!strcmp(dl->driver->name, classname)) 712 return (dl); 713 } 714 715 PDEBUG(("not found")); 716 return (NULL); 717} 718 719kobj_class_t 720devclass_find_driver(devclass_t dc, const char *classname) 721{ 722 driverlink_t dl; 723 724 dl = devclass_find_driver_internal(dc, classname); 725 if (dl) 726 return (dl->driver); 727 return (NULL); 728} 729 730const char * 731devclass_get_name(devclass_t dc) 732{ 733 return (dc->name); 734} 735 736device_t 737devclass_get_device(devclass_t dc, int unit) 738{ 739 if (dc == NULL || unit < 0 || unit >= dc->maxunit) 740 return (NULL); 741 return (dc->devices[unit]); 742} 743 744void * 745devclass_get_softc(devclass_t dc, int unit) 746{ 747 device_t dev; 748 749 dev = devclass_get_device(dc, unit); 750 if (!dev) 751 return (NULL); 752 753 return (device_get_softc(dev)); 754} 755 756int 757devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp) 758{ 759 int i; 760 int count; 761 device_t *list; 762 763 count = 0; 764 for (i = 0; i < dc->maxunit; i++) 765 if (dc->devices[i]) 766 count++; 767 768 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO); 769 if (!list) 770 return (ENOMEM); 771 772 count = 0; 773 for (i = 0; i < dc->maxunit; i++) { 774 if (dc->devices[i]) { 775 list[count] = dc->devices[i]; 776 count++; 777 } 778 } 779 780 *devlistp = list; 781 *devcountp = count; 782 783 return (0); 784} 785 786int 787devclass_get_maxunit(devclass_t dc) 788{ 789 return (dc->maxunit); 790} 791 792int 793devclass_find_free_unit(devclass_t dc, int unit) 794{ 795 if (dc == NULL) 796 return (unit); 797 while (unit < dc->maxunit && dc->devices[unit] != NULL) 798 unit++; 799 return (unit); 800} 801 802void 803devclass_set_parent(devclass_t dc, devclass_t pdc) 804{ 805 dc->parent = pdc; 806} 807 808devclass_t 809devclass_get_parent(devclass_t dc) 810{ 811 return (dc->parent); 812} 813 814static int 815devclass_alloc_unit(devclass_t dc, int *unitp) 816{ 817 int unit = *unitp; 818 819 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc))); 820 821 /* If we were given a wired unit number, check for existing device */ 822 /* XXX imp XXX */ 823 if (unit != -1) { 824 if (unit >= 0 && unit < dc->maxunit && 825 dc->devices[unit] != NULL) { 826 if (bootverbose) 827 printf("%s: %s%d already exists; skipping it\n", 828 dc->name, dc->name, *unitp); 829 return (EEXIST); 830 } 831 } else { 832 /* Unwired device, find the next available slot for it */ 833 unit = 0; 834 while (unit < dc->maxunit && dc->devices[unit] != NULL) 835 unit++; 836 } 837 838 /* 839 * We've selected a unit beyond the length of the table, so let's 840 * extend the table to make room for all units up to and including 841 * this one. 842 */ 843 if (unit >= dc->maxunit) { 844 device_t *newlist; 845 int newsize; 846 847 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t)); 848 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT); 849 if (!newlist) 850 return (ENOMEM); 851 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit); 852 bzero(newlist + dc->maxunit, 853 sizeof(device_t) * (newsize - dc->maxunit)); 854 if (dc->devices) 855 free(dc->devices, M_BUS); 856 dc->devices = newlist; 857 dc->maxunit = newsize; 858 } 859 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc))); 860 861 *unitp = unit; 862 return (0); 863} 864 865static int 866devclass_add_device(devclass_t dc, device_t dev) 867{ 868 int buflen, error; 869 870 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 871 872 buflen = snprintf(NULL, 0, "%s%d$", dc->name, dev->unit); 873 if (buflen < 0) 874 return (ENOMEM); 875 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO); 876 if (!dev->nameunit) 877 return (ENOMEM); 878 879 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) { 880 free(dev->nameunit, M_BUS); 881 dev->nameunit = NULL; 882 return (error); 883 } 884 dc->devices[dev->unit] = dev; 885 dev->devclass = dc; 886 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit); 887 888 return (0); 889} 890 891static int 892devclass_delete_device(devclass_t dc, device_t dev) 893{ 894 if (!dc || !dev) 895 return (0); 896 897 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 898 899 if (dev->devclass != dc || dc->devices[dev->unit] != dev) 900 panic("devclass_delete_device: inconsistent device class"); 901 dc->devices[dev->unit] = NULL; 902 if (dev->flags & DF_WILDCARD) 903 dev->unit = -1; 904 dev->devclass = NULL; 905 free(dev->nameunit, M_BUS); 906 dev->nameunit = NULL; 907 908 return (0); 909} 910 911static device_t 912make_device(device_t parent, const char *name, int unit) 913{ 914 device_t dev; 915 devclass_t dc; 916 917 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit)); 918 919 if (name) { 920 dc = devclass_find_internal(name, 0, TRUE); 921 if (!dc) { 922 printf("make_device: can't find device class %s\n", 923 name); 924 return (NULL); 925 } 926 } else { 927 dc = NULL; 928 } 929 930 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO); 931 if (!dev) 932 return (NULL); 933 934 dev->parent = parent; 935 TAILQ_INIT(&dev->children); 936 kobj_init((kobj_t) dev, &null_class); 937 dev->driver = NULL; 938 dev->devclass = NULL; 939 dev->unit = unit; 940 dev->nameunit = NULL; 941 dev->desc = NULL; 942 dev->busy = 0; 943 dev->devflags = 0; 944 dev->flags = DF_ENABLED; 945 dev->order = 0; 946 if (unit == -1) 947 dev->flags |= DF_WILDCARD; 948 if (name) { 949 dev->flags |= DF_FIXEDCLASS; 950 if (devclass_add_device(dc, dev)) { 951 kobj_delete((kobj_t) dev, M_BUS); 952 return (NULL); 953 } 954 } 955 dev->ivars = NULL; 956 dev->softc = NULL; 957 958 dev->state = DS_NOTPRESENT; 959 960 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink); 961 bus_data_generation_update(); 962 963 return (dev); 964} 965 966static int 967device_print_child(device_t dev, device_t child) 968{ 969 int retval = 0; 970 971 if (device_is_alive(child)) 972 retval += BUS_PRINT_CHILD(dev, child); 973 else 974 retval += device_printf(child, " not found\n"); 975 976 return (retval); 977} 978 979device_t 980device_add_child(device_t dev, const char *name, int unit) 981{ 982 return (device_add_child_ordered(dev, 0, name, unit)); 983} 984 985device_t 986device_add_child_ordered(device_t dev, int order, const char *name, int unit) 987{ 988 device_t child; 989 device_t place; 990 991 PDEBUG(("%s at %s with order %d as unit %d", 992 name, DEVICENAME(dev), order, unit)); 993 994 child = make_device(dev, name, unit); 995 if (child == NULL) 996 return (child); 997 child->order = order; 998 999 TAILQ_FOREACH(place, &dev->children, link) { 1000 if (place->order > order) 1001 break; 1002 } 1003 1004 if (place) { 1005 /* 1006 * The device 'place' is the first device whose order is 1007 * greater than the new child. 1008 */ 1009 TAILQ_INSERT_BEFORE(place, child, link); 1010 } else { 1011 /* 1012 * The new child's order is greater or equal to the order of 1013 * any existing device. Add the child to the tail of the list. 1014 */ 1015 TAILQ_INSERT_TAIL(&dev->children, child, link); 1016 } 1017 1018 bus_data_generation_update(); 1019 return (child); 1020} 1021 1022int 1023device_delete_child(device_t dev, device_t child) 1024{ 1025 int error; 1026 device_t grandchild; 1027 1028 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev))); 1029 1030 /* remove children first */ 1031 while ( (grandchild = TAILQ_FIRST(&child->children)) ) { 1032 error = device_delete_child(child, grandchild); 1033 if (error) 1034 return (error); 1035 } 1036 1037 if ((error = device_detach(child)) != 0) 1038 return (error); 1039 if (child->devclass) 1040 devclass_delete_device(child->devclass, child); 1041 TAILQ_REMOVE(&dev->children, child, link); 1042 TAILQ_REMOVE(&bus_data_devices, child, devlink); 1043 device_set_desc(child, NULL); 1044 kobj_delete((kobj_t) child, M_BUS); 1045 1046 bus_data_generation_update(); 1047 return (0); 1048} 1049 1050/* 1051 * Find only devices attached to this bus. 1052 */ 1053device_t 1054device_find_child(device_t dev, const char *classname, int unit) 1055{ 1056 devclass_t dc; 1057 device_t child; 1058 1059 dc = devclass_find(classname); 1060 if (!dc) 1061 return (NULL); 1062 1063 child = devclass_get_device(dc, unit); 1064 if (child && child->parent == dev) 1065 return (child); 1066 return (NULL); 1067} 1068 1069static driverlink_t 1070first_matching_driver(devclass_t dc, device_t dev) 1071{ 1072 if (dev->devclass) 1073 return (devclass_find_driver_internal(dc, dev->devclass->name)); 1074 return (TAILQ_FIRST(&dc->drivers)); 1075} 1076 1077static driverlink_t 1078next_matching_driver(devclass_t dc, device_t dev, driverlink_t last) 1079{ 1080 if (dev->devclass) { 1081 driverlink_t dl; 1082 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link)) 1083 if (!strcmp(dev->devclass->name, dl->driver->name)) 1084 return (dl); 1085 return (NULL); 1086 } 1087 return (TAILQ_NEXT(last, link)); 1088} 1089 1090static int 1091device_probe_child(device_t dev, device_t child) 1092{ 1093 devclass_t dc; 1094 driverlink_t best = 0; 1095 driverlink_t dl; 1096 int result, pri = 0; 1097 int hasclass = (child->devclass != 0); 1098 1099 dc = dev->devclass; 1100 if (!dc) 1101 panic("device_probe_child: parent device has no devclass"); 1102 1103 if (child->state == DS_ALIVE) 1104 return (0); 1105 1106 for (; dc; dc = dc->parent) { 1107 for (dl = first_matching_driver(dc, child); 1108 dl; 1109 dl = next_matching_driver(dc, child, dl)) { 1110 PDEBUG(("Trying %s", DRIVERNAME(dl->driver))); 1111 device_set_driver(child, dl->driver); 1112 if (!hasclass) 1113 device_set_devclass(child, dl->driver->name); 1114 result = DEVICE_PROBE(child); 1115 if (!hasclass) 1116 device_set_devclass(child, 0); 1117 1118 /* 1119 * If the driver returns SUCCESS, there can be 1120 * no higher match for this device. 1121 */ 1122 if (result == 0) { 1123 best = dl; 1124 pri = 0; 1125 break; 1126 } 1127 1128 /* 1129 * The driver returned an error so it 1130 * certainly doesn't match. 1131 */ 1132 if (result > 0) { 1133 device_set_driver(child, 0); 1134 continue; 1135 } 1136 1137 /* 1138 * A priority lower than SUCCESS, remember the 1139 * best matching driver. Initialise the value 1140 * of pri for the first match. 1141 */ 1142 if (best == 0 || result > pri) { 1143 best = dl; 1144 pri = result; 1145 continue; 1146 } 1147 } 1148 /* 1149 * If we have an unambiguous match in this devclass, 1150 * don't look in the parent. 1151 */ 1152 if (best && pri == 0) 1153 break; 1154 } 1155 1156 /* 1157 * If we found a driver, change state and initialise the devclass. 1158 */ 1159 if (best) { 1160 if (!child->devclass) 1161 device_set_devclass(child, best->driver->name); 1162 device_set_driver(child, best->driver); 1163 if (pri < 0) { 1164 /* 1165 * A bit bogus. Call the probe method again to make 1166 * sure that we have the right description. 1167 */ 1168 DEVICE_PROBE(child); 1169 } 1170 child->state = DS_ALIVE; 1171 1172 bus_data_generation_update(); 1173 return (0); 1174 } 1175 1176 return (ENXIO); 1177} 1178 1179device_t 1180device_get_parent(device_t dev) 1181{ 1182 return (dev->parent); 1183} 1184 1185int 1186device_get_children(device_t dev, device_t **devlistp, int *devcountp) 1187{ 1188 int count; 1189 device_t child; 1190 device_t *list; 1191 1192 count = 0; 1193 TAILQ_FOREACH(child, &dev->children, link) { 1194 count++; 1195 } 1196 1197 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO); 1198 if (!list) 1199 return (ENOMEM); 1200 1201 count = 0; 1202 TAILQ_FOREACH(child, &dev->children, link) { 1203 list[count] = child; 1204 count++; 1205 } 1206 1207 *devlistp = list; 1208 *devcountp = count; 1209 1210 return (0); 1211} 1212 1213driver_t * 1214device_get_driver(device_t dev) 1215{ 1216 return (dev->driver); 1217} 1218 1219devclass_t 1220device_get_devclass(device_t dev) 1221{ 1222 return (dev->devclass); 1223} 1224 1225const char * 1226device_get_name(device_t dev) 1227{ 1228 if (dev != NULL && dev->devclass) 1229 return (devclass_get_name(dev->devclass)); 1230 return (NULL); 1231} 1232 1233const char * 1234device_get_nameunit(device_t dev) 1235{ 1236 return (dev->nameunit); 1237} 1238 1239int 1240device_get_unit(device_t dev) 1241{ 1242 return (dev->unit); 1243} 1244 1245const char * 1246device_get_desc(device_t dev) 1247{ 1248 return (dev->desc); 1249} 1250 1251u_int32_t 1252device_get_flags(device_t dev) 1253{ 1254 return (dev->devflags); 1255} 1256 1257int 1258device_print_prettyname(device_t dev) 1259{ 1260 const char *name = device_get_name(dev); 1261 1262 if (name == 0) 1263 return (printf("unknown: ")); 1264 return (printf("%s%d: ", name, device_get_unit(dev))); 1265} 1266 1267int 1268device_printf(device_t dev, const char * fmt, ...) 1269{ 1270 va_list ap; 1271 int retval; 1272 1273 retval = device_print_prettyname(dev); 1274 va_start(ap, fmt); 1275 retval += vprintf(fmt, ap); 1276 va_end(ap); 1277 return (retval); 1278} 1279 1280static void 1281device_set_desc_internal(device_t dev, const char* desc, int copy) 1282{ 1283 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) { 1284 free(dev->desc, M_BUS); 1285 dev->flags &= ~DF_DESCMALLOCED; 1286 dev->desc = NULL; 1287 } 1288 1289 if (copy && desc) { 1290 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT); 1291 if (dev->desc) { 1292 strcpy(dev->desc, desc); 1293 dev->flags |= DF_DESCMALLOCED; 1294 } 1295 } else { 1296 /* Avoid a -Wcast-qual warning */ 1297 dev->desc = (char *)(uintptr_t) desc; 1298 } 1299 1300 bus_data_generation_update(); 1301} 1302 1303void 1304device_set_desc(device_t dev, const char* desc) 1305{ 1306 device_set_desc_internal(dev, desc, FALSE); 1307} 1308 1309void 1310device_set_desc_copy(device_t dev, const char* desc) 1311{ 1312 device_set_desc_internal(dev, desc, TRUE); 1313} 1314 1315void 1316device_set_flags(device_t dev, u_int32_t flags) 1317{ 1318 dev->devflags = flags; 1319} 1320 1321void * 1322device_get_softc(device_t dev) 1323{ 1324 return (dev->softc); 1325} 1326 1327void 1328device_set_softc(device_t dev, void *softc) 1329{ 1330 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) 1331 free(dev->softc, M_BUS_SC); 1332 dev->softc = softc; 1333 if (dev->softc) 1334 dev->flags |= DF_EXTERNALSOFTC; 1335 else 1336 dev->flags &= ~DF_EXTERNALSOFTC; 1337} 1338 1339void * 1340device_get_ivars(device_t dev) 1341{ 1342 1343 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)")); 1344 return (dev->ivars); 1345} 1346 1347void 1348device_set_ivars(device_t dev, void * ivars) 1349{ 1350 1351 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)")); 1352 dev->ivars = ivars; 1353} 1354 1355device_state_t 1356device_get_state(device_t dev) 1357{ 1358 return (dev->state); 1359} 1360 1361void 1362device_enable(device_t dev) 1363{ 1364 dev->flags |= DF_ENABLED; 1365} 1366 1367void 1368device_disable(device_t dev) 1369{ 1370 dev->flags &= ~DF_ENABLED; 1371} 1372 1373void 1374device_busy(device_t dev) 1375{ 1376 if (dev->state < DS_ATTACHED) 1377 panic("device_busy: called for unattached device"); 1378 if (dev->busy == 0 && dev->parent) 1379 device_busy(dev->parent); 1380 dev->busy++; 1381 dev->state = DS_BUSY; 1382} 1383 1384void 1385device_unbusy(device_t dev) 1386{ 1387 if (dev->state != DS_BUSY) 1388 panic("device_unbusy: called for non-busy device"); 1389 dev->busy--; 1390 if (dev->busy == 0) { 1391 if (dev->parent) 1392 device_unbusy(dev->parent); 1393 dev->state = DS_ATTACHED; 1394 } 1395} 1396 1397void 1398device_quiet(device_t dev) 1399{ 1400 dev->flags |= DF_QUIET; 1401} 1402 1403void 1404device_verbose(device_t dev) 1405{ 1406 dev->flags &= ~DF_QUIET; 1407} 1408 1409int 1410device_is_quiet(device_t dev) 1411{ 1412 return ((dev->flags & DF_QUIET) != 0); 1413} 1414 1415int 1416device_is_enabled(device_t dev) 1417{ 1418 return ((dev->flags & DF_ENABLED) != 0); 1419} 1420 1421int 1422device_is_alive(device_t dev) 1423{ 1424 return (dev->state >= DS_ALIVE); 1425} 1426 1427int 1428device_is_attached(device_t dev) 1429{ 1430 return (dev->state >= DS_ATTACHED); 1431} 1432 1433int 1434device_set_devclass(device_t dev, const char *classname) 1435{ 1436 devclass_t dc; 1437 int error; 1438 1439 if (!classname) { 1440 if (dev->devclass) 1441 devclass_delete_device(dev->devclass, dev); 1442 return (0); 1443 } 1444 1445 if (dev->devclass) { 1446 printf("device_set_devclass: device class already set\n"); 1447 return (EINVAL); 1448 } 1449 1450 dc = devclass_find_internal(classname, 0, TRUE); 1451 if (!dc) 1452 return (ENOMEM); 1453 1454 error = devclass_add_device(dc, dev); 1455 1456 bus_data_generation_update(); 1457 return (error); 1458} 1459 1460int 1461device_set_driver(device_t dev, driver_t *driver) 1462{ 1463 if (dev->state >= DS_ATTACHED) 1464 return (EBUSY); 1465 1466 if (dev->driver == driver) 1467 return (0); 1468 1469 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) { 1470 free(dev->softc, M_BUS_SC); 1471 dev->softc = NULL; 1472 } 1473 kobj_delete((kobj_t) dev, 0); 1474 dev->driver = driver; 1475 if (driver) { 1476 kobj_init((kobj_t) dev, (kobj_class_t) driver); 1477 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) { 1478 dev->softc = malloc(driver->size, M_BUS_SC, 1479 M_NOWAIT | M_ZERO); 1480 if (!dev->softc) { 1481 kobj_delete((kobj_t) dev, 0); 1482 kobj_init((kobj_t) dev, &null_class); 1483 dev->driver = NULL; 1484 return (ENOMEM); 1485 } 1486 } 1487 } else { 1488 kobj_init((kobj_t) dev, &null_class); 1489 } 1490 1491 bus_data_generation_update(); 1492 return (0); 1493} 1494 1495int 1496device_probe_and_attach(device_t dev) 1497{ 1498 device_t bus = dev->parent; 1499 int error = 0; 1500 int hasclass = (dev->devclass != 0); 1501 1502 if (dev->state >= DS_ALIVE) 1503 return (0); 1504 1505 if (dev->flags & DF_ENABLED) { 1506 error = device_probe_child(bus, dev); 1507 if (!error) { 1508 if (!device_is_quiet(dev)) 1509 device_print_child(bus, dev); 1510 error = DEVICE_ATTACH(dev); 1511 if (!error) { 1512 dev->state = DS_ATTACHED; 1513 devadded(dev); 1514 } else { 1515 printf("device_probe_and_attach: %s%d attach returned %d\n", 1516 dev->driver->name, dev->unit, error); 1517 /* Unset the class; set in device_probe_child */ 1518 if (!hasclass) 1519 device_set_devclass(dev, 0); 1520 device_set_driver(dev, NULL); 1521 dev->state = DS_NOTPRESENT; 1522 } 1523 } else { 1524 if (!(dev->flags & DF_DONENOMATCH)) { 1525 BUS_PROBE_NOMATCH(bus, dev); 1526 devnomatch(dev); 1527 dev->flags |= DF_DONENOMATCH; 1528 } 1529 } 1530 } else { 1531 if (bootverbose) { 1532 device_print_prettyname(dev); 1533 printf("not probed (disabled)\n"); 1534 } 1535 } 1536 1537 return (error); 1538} 1539 1540int 1541device_detach(device_t dev) 1542{ 1543 int error; 1544 1545 PDEBUG(("%s", DEVICENAME(dev))); 1546 if (dev->state == DS_BUSY) 1547 return (EBUSY); 1548 if (dev->state != DS_ATTACHED) 1549 return (0); 1550 1551 if ((error = DEVICE_DETACH(dev)) != 0) 1552 return (error); 1553 devremoved(dev); 1554 device_printf(dev, "detached\n"); 1555 if (dev->parent) 1556 BUS_CHILD_DETACHED(dev->parent, dev); 1557 1558 if (!(dev->flags & DF_FIXEDCLASS)) 1559 devclass_delete_device(dev->devclass, dev); 1560 1561 dev->state = DS_NOTPRESENT; 1562 device_set_driver(dev, NULL); 1563 1564 return (0); 1565} 1566 1567int 1568device_shutdown(device_t dev) 1569{ 1570 if (dev->state < DS_ATTACHED) 1571 return (0); 1572 return (DEVICE_SHUTDOWN(dev)); 1573} 1574 1575int 1576device_set_unit(device_t dev, int unit) 1577{ 1578 devclass_t dc; 1579 int err; 1580 1581 dc = device_get_devclass(dev); 1582 if (unit < dc->maxunit && dc->devices[unit]) 1583 return (EBUSY); 1584 err = devclass_delete_device(dc, dev); 1585 if (err) 1586 return (err); 1587 dev->unit = unit; 1588 err = devclass_add_device(dc, dev); 1589 if (err) 1590 return (err); 1591 1592 bus_data_generation_update(); 1593 return (0); 1594} 1595 1596/*======================================*/ 1597/* 1598 * Some useful method implementations to make life easier for bus drivers. 1599 */ 1600 1601void 1602resource_list_init(struct resource_list *rl) 1603{ 1604 SLIST_INIT(rl); 1605} 1606 1607void 1608resource_list_free(struct resource_list *rl) 1609{ 1610 struct resource_list_entry *rle; 1611 1612 while ((rle = SLIST_FIRST(rl)) != NULL) { 1613 if (rle->res) 1614 panic("resource_list_free: resource entry is busy"); 1615 SLIST_REMOVE_HEAD(rl, link); 1616 free(rle, M_BUS); 1617 } 1618} 1619 1620int 1621resource_list_add_next(struct resource_list *rl, int type, u_long start, 1622 u_long end, u_long count) 1623{ 1624 int rid; 1625 1626 rid = 0; 1627 while (resource_list_find(rl, type, rid) != NULL) 1628 rid++; 1629 resource_list_add(rl, type, rid, start, end, count); 1630 return (rid); 1631} 1632 1633void 1634resource_list_add(struct resource_list *rl, int type, int rid, 1635 u_long start, u_long end, u_long count) 1636{ 1637 struct resource_list_entry *rle; 1638 1639 rle = resource_list_find(rl, type, rid); 1640 if (!rle) { 1641 rle = malloc(sizeof(struct resource_list_entry), M_BUS, 1642 M_NOWAIT); 1643 if (!rle) 1644 panic("resource_list_add: can't record entry"); 1645 SLIST_INSERT_HEAD(rl, rle, link); 1646 rle->type = type; 1647 rle->rid = rid; 1648 rle->res = NULL; 1649 } 1650 1651 if (rle->res) 1652 panic("resource_list_add: resource entry is busy"); 1653 1654 rle->start = start; 1655 rle->end = end; 1656 rle->count = count; 1657} 1658 1659struct resource_list_entry * 1660resource_list_find(struct resource_list *rl, int type, int rid) 1661{ 1662 struct resource_list_entry *rle; 1663 1664 SLIST_FOREACH(rle, rl, link) { 1665 if (rle->type == type && rle->rid == rid) 1666 return (rle); 1667 } 1668 return (NULL); 1669} 1670 1671void 1672resource_list_delete(struct resource_list *rl, int type, int rid) 1673{ 1674 struct resource_list_entry *rle = resource_list_find(rl, type, rid); 1675 1676 if (rle) { 1677 if (rle->res != NULL) 1678 panic("resource_list_delete: resource has not been released"); 1679 SLIST_REMOVE(rl, rle, resource_list_entry, link); 1680 free(rle, M_BUS); 1681 } 1682} 1683 1684struct resource * 1685resource_list_alloc(struct resource_list *rl, device_t bus, device_t child, 1686 int type, int *rid, u_long start, u_long end, u_long count, u_int flags) 1687{ 1688 struct resource_list_entry *rle = 0; 1689 int passthrough = (device_get_parent(child) != bus); 1690 int isdefault = (start == 0UL && end == ~0UL); 1691 1692 if (passthrough) { 1693 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 1694 type, rid, start, end, count, flags)); 1695 } 1696 1697 rle = resource_list_find(rl, type, *rid); 1698 1699 if (!rle) 1700 return (NULL); /* no resource of that type/rid */ 1701 1702 if (rle->res) 1703 panic("resource_list_alloc: resource entry is busy"); 1704 1705 if (isdefault) { 1706 start = rle->start; 1707 count = ulmax(count, rle->count); 1708 end = ulmax(rle->end, start + count - 1); 1709 } 1710 1711 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 1712 type, rid, start, end, count, flags); 1713 1714 /* 1715 * Record the new range. 1716 */ 1717 if (rle->res) { 1718 rle->start = rman_get_start(rle->res); 1719 rle->end = rman_get_end(rle->res); 1720 rle->count = count; 1721 } 1722 1723 return (rle->res); 1724} 1725 1726int 1727resource_list_release(struct resource_list *rl, device_t bus, device_t child, 1728 int type, int rid, struct resource *res) 1729{ 1730 struct resource_list_entry *rle = 0; 1731 int passthrough = (device_get_parent(child) != bus); 1732 int error; 1733 1734 if (passthrough) { 1735 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 1736 type, rid, res)); 1737 } 1738 1739 rle = resource_list_find(rl, type, rid); 1740 1741 if (!rle) 1742 panic("resource_list_release: can't find resource"); 1743 if (!rle->res) 1744 panic("resource_list_release: resource entry is not busy"); 1745 1746 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 1747 type, rid, res); 1748 if (error) 1749 return (error); 1750 1751 rle->res = NULL; 1752 return (0); 1753} 1754 1755int 1756resource_list_print_type(struct resource_list *rl, const char *name, int type, 1757 const char *format) 1758{ 1759 struct resource_list_entry *rle; 1760 int printed, retval; 1761 1762 printed = 0; 1763 retval = 0; 1764 /* Yes, this is kinda cheating */ 1765 SLIST_FOREACH(rle, rl, link) { 1766 if (rle->type == type) { 1767 if (printed == 0) 1768 retval += printf(" %s ", name); 1769 else 1770 retval += printf(","); 1771 printed++; 1772 retval += printf(format, rle->start); 1773 if (rle->count > 1) { 1774 retval += printf("-"); 1775 retval += printf(format, rle->start + 1776 rle->count - 1); 1777 } 1778 } 1779 } 1780 return (retval); 1781} 1782 1783/* 1784 * Call DEVICE_IDENTIFY for each driver. 1785 */ 1786int 1787bus_generic_probe(device_t dev) 1788{ 1789 devclass_t dc = dev->devclass; 1790 driverlink_t dl; 1791 1792 TAILQ_FOREACH(dl, &dc->drivers, link) { 1793 DEVICE_IDENTIFY(dl->driver, dev); 1794 } 1795 1796 return (0); 1797} 1798 1799int 1800bus_generic_attach(device_t dev) 1801{ 1802 device_t child; 1803 1804 TAILQ_FOREACH(child, &dev->children, link) { 1805 device_probe_and_attach(child); 1806 } 1807 1808 return (0); 1809} 1810 1811int 1812bus_generic_detach(device_t dev) 1813{ 1814 device_t child; 1815 int error; 1816 1817 if (dev->state != DS_ATTACHED) 1818 return (EBUSY); 1819 1820 TAILQ_FOREACH(child, &dev->children, link) { 1821 if ((error = device_detach(child)) != 0) 1822 return (error); 1823 } 1824 1825 return (0); 1826} 1827 1828int 1829bus_generic_shutdown(device_t dev) 1830{ 1831 device_t child; 1832 1833 TAILQ_FOREACH(child, &dev->children, link) { 1834 device_shutdown(child); 1835 } 1836 1837 return (0); 1838} 1839 1840int 1841bus_generic_suspend(device_t dev) 1842{ 1843 int error; 1844 device_t child, child2; 1845 1846 TAILQ_FOREACH(child, &dev->children, link) { 1847 error = DEVICE_SUSPEND(child); 1848 if (error) { 1849 for (child2 = TAILQ_FIRST(&dev->children); 1850 child2 && child2 != child; 1851 child2 = TAILQ_NEXT(child2, link)) 1852 DEVICE_RESUME(child2); 1853 return (error); 1854 } 1855 } 1856 return (0); 1857} 1858 1859int 1860bus_generic_resume(device_t dev) 1861{ 1862 device_t child; 1863 1864 TAILQ_FOREACH(child, &dev->children, link) { 1865 DEVICE_RESUME(child); 1866 /* if resume fails, there's nothing we can usefully do... */ 1867 } 1868 return (0); 1869} 1870 1871int 1872bus_print_child_header (device_t dev, device_t child) 1873{ 1874 int retval = 0; 1875 1876 if (device_get_desc(child)) { 1877 retval += device_printf(child, "<%s>", device_get_desc(child)); 1878 } else { 1879 retval += printf("%s", device_get_nameunit(child)); 1880 } 1881 1882 return (retval); 1883} 1884 1885int 1886bus_print_child_footer (device_t dev, device_t child) 1887{ 1888 return (printf(" on %s\n", device_get_nameunit(dev))); 1889} 1890 1891int 1892bus_generic_print_child(device_t dev, device_t child) 1893{ 1894 int retval = 0; 1895 1896 retval += bus_print_child_header(dev, child); 1897 retval += bus_print_child_footer(dev, child); 1898 1899 return (retval); 1900} 1901 1902int 1903bus_generic_read_ivar(device_t dev, device_t child, int index, 1904 uintptr_t * result) 1905{ 1906 return (ENOENT); 1907} 1908 1909int 1910bus_generic_write_ivar(device_t dev, device_t child, int index, 1911 uintptr_t value) 1912{ 1913 return (ENOENT); 1914} 1915 1916struct resource_list * 1917bus_generic_get_resource_list (device_t dev, device_t child) 1918{ 1919 return (NULL); 1920} 1921 1922void 1923bus_generic_driver_added(device_t dev, driver_t *driver) 1924{ 1925 device_t child; 1926 1927 DEVICE_IDENTIFY(driver, dev); 1928 TAILQ_FOREACH(child, &dev->children, link) { 1929 if (child->state == DS_NOTPRESENT) 1930 device_probe_and_attach(child); 1931 } 1932} 1933 1934int 1935bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq, 1936 int flags, driver_intr_t *intr, void *arg, void **cookiep) 1937{ 1938 /* Propagate up the bus hierarchy until someone handles it. */ 1939 if (dev->parent) 1940 return (BUS_SETUP_INTR(dev->parent, child, irq, flags, 1941 intr, arg, cookiep)); 1942 return (EINVAL); 1943} 1944 1945int 1946bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq, 1947 void *cookie) 1948{ 1949 /* Propagate up the bus hierarchy until someone handles it. */ 1950 if (dev->parent) 1951 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie)); 1952 return (EINVAL); 1953} 1954 1955struct resource * 1956bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid, 1957 u_long start, u_long end, u_long count, u_int flags) 1958{ 1959 /* Propagate up the bus hierarchy until someone handles it. */ 1960 if (dev->parent) 1961 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid, 1962 start, end, count, flags)); 1963 return (NULL); 1964} 1965 1966int 1967bus_generic_release_resource(device_t dev, device_t child, int type, int rid, 1968 struct resource *r) 1969{ 1970 /* Propagate up the bus hierarchy until someone handles it. */ 1971 if (dev->parent) 1972 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, 1973 r)); 1974 return (EINVAL); 1975} 1976 1977int 1978bus_generic_activate_resource(device_t dev, device_t child, int type, int rid, 1979 struct resource *r) 1980{ 1981 /* Propagate up the bus hierarchy until someone handles it. */ 1982 if (dev->parent) 1983 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, 1984 r)); 1985 return (EINVAL); 1986} 1987 1988int 1989bus_generic_deactivate_resource(device_t dev, device_t child, int type, 1990 int rid, struct resource *r) 1991{ 1992 /* Propagate up the bus hierarchy until someone handles it. */ 1993 if (dev->parent) 1994 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid, 1995 r)); 1996 return (EINVAL); 1997} 1998 1999int 2000bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig, 2001 enum intr_polarity pol) 2002{ 2003 2004 /* Propagate up the bus hierarchy until someone handles it. */ 2005 if (dev->parent) 2006 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol)); 2007 return (EINVAL); 2008} 2009 2010int 2011bus_generic_rl_get_resource (device_t dev, device_t child, int type, int rid, 2012 u_long *startp, u_long *countp) 2013{ 2014 struct resource_list * rl = NULL; 2015 struct resource_list_entry * rle = NULL; 2016 2017 rl = BUS_GET_RESOURCE_LIST(dev, child); 2018 if (!rl) 2019 return (EINVAL); 2020 2021 rle = resource_list_find(rl, type, rid); 2022 if (!rle) 2023 return (ENOENT); 2024 2025 if (startp) 2026 *startp = rle->start; 2027 if (countp) 2028 *countp = rle->count; 2029 2030 return (0); 2031} 2032 2033int 2034bus_generic_rl_set_resource (device_t dev, device_t child, int type, int rid, 2035 u_long start, u_long count) 2036{ 2037 struct resource_list * rl = NULL; 2038 2039 rl = BUS_GET_RESOURCE_LIST(dev, child); 2040 if (!rl) 2041 return (EINVAL); 2042 2043 resource_list_add(rl, type, rid, start, (start + count - 1), count); 2044 2045 return (0); 2046} 2047 2048void 2049bus_generic_rl_delete_resource (device_t dev, device_t child, int type, int rid) 2050{ 2051 struct resource_list * rl = NULL; 2052 2053 rl = BUS_GET_RESOURCE_LIST(dev, child); 2054 if (!rl) 2055 return; 2056 2057 resource_list_delete(rl, type, rid); 2058 2059 return; 2060} 2061 2062int 2063bus_generic_rl_release_resource (device_t dev, device_t child, int type, 2064 int rid, struct resource *r) 2065{ 2066 struct resource_list * rl = NULL; 2067 2068 rl = BUS_GET_RESOURCE_LIST(dev, child); 2069 if (!rl) 2070 return (EINVAL); 2071 2072 return (resource_list_release(rl, dev, child, type, rid, r)); 2073} 2074 2075struct resource * 2076bus_generic_rl_alloc_resource (device_t dev, device_t child, int type, 2077 int *rid, u_long start, u_long end, u_long count, u_int flags) 2078{ 2079 struct resource_list * rl = NULL; 2080 2081 rl = BUS_GET_RESOURCE_LIST(dev, child); 2082 if (!rl) 2083 return (NULL); 2084 2085 return (resource_list_alloc(rl, dev, child, type, rid, 2086 start, end, count, flags)); 2087} 2088 2089int 2090bus_generic_child_present(device_t bus, device_t child) 2091{ 2092 return (BUS_CHILD_PRESENT(device_get_parent(bus), bus)); 2093} 2094 2095/* 2096 * Some convenience functions to make it easier for drivers to use the 2097 * resource-management functions. All these really do is hide the 2098 * indirection through the parent's method table, making for slightly 2099 * less-wordy code. In the future, it might make sense for this code 2100 * to maintain some sort of a list of resources allocated by each device. 2101 */ 2102struct resource * 2103bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end, 2104 u_long count, u_int flags) 2105{ 2106 if (dev->parent == 0) 2107 return (0); 2108 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end, 2109 count, flags)); 2110} 2111 2112int 2113bus_activate_resource(device_t dev, int type, int rid, struct resource *r) 2114{ 2115 if (dev->parent == 0) 2116 return (EINVAL); 2117 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 2118} 2119 2120int 2121bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r) 2122{ 2123 if (dev->parent == 0) 2124 return (EINVAL); 2125 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 2126} 2127 2128int 2129bus_release_resource(device_t dev, int type, int rid, struct resource *r) 2130{ 2131 if (dev->parent == 0) 2132 return (EINVAL); 2133 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r)); 2134} 2135 2136/* 2137 * XXX this is a temporary measure to allow folks to 2138 * XXX disable INTR_MPSAFE in network drivers without 2139 * XXX recompiling--in case of problems. 2140 */ 2141int debug_mpsafenet = 0; 2142TUNABLE_INT("debug.mpsafenet", &debug_mpsafenet); 2143SYSCTL_INT(_debug, OID_AUTO, mpsafenet, CTLFLAG_RW, &debug_mpsafenet, 0, 2144 "Enable/disable MPSAFE network support"); 2145 2146int 2147bus_setup_intr(device_t dev, struct resource *r, int flags, 2148 driver_intr_t handler, void *arg, void **cookiep) 2149{ 2150 int error; 2151 2152 if (dev->parent != 0) { 2153 if ((flags &~ INTR_ENTROPY) == (INTR_TYPE_NET | INTR_MPSAFE) && 2154 !debug_mpsafenet) 2155 flags &= ~INTR_MPSAFE; 2156 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, 2157 handler, arg, cookiep); 2158 if (error == 0) { 2159 if (flags & INTR_MPSAFE) 2160 device_printf(dev, "[MPSAFE]\n"); 2161 if (flags & INTR_FAST) 2162 device_printf(dev, "[FAST]\n"); 2163 } 2164 } else 2165 error = EINVAL; 2166 return (error); 2167} 2168 2169int 2170bus_teardown_intr(device_t dev, struct resource *r, void *cookie) 2171{ 2172 if (dev->parent == 0) 2173 return (EINVAL); 2174 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie)); 2175} 2176 2177int 2178bus_set_resource(device_t dev, int type, int rid, 2179 u_long start, u_long count) 2180{ 2181 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid, 2182 start, count)); 2183} 2184 2185int 2186bus_get_resource(device_t dev, int type, int rid, 2187 u_long *startp, u_long *countp) 2188{ 2189 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 2190 startp, countp)); 2191} 2192 2193u_long 2194bus_get_resource_start(device_t dev, int type, int rid) 2195{ 2196 u_long start, count; 2197 int error; 2198 2199 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 2200 &start, &count); 2201 if (error) 2202 return (0); 2203 return (start); 2204} 2205 2206u_long 2207bus_get_resource_count(device_t dev, int type, int rid) 2208{ 2209 u_long start, count; 2210 int error; 2211 2212 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 2213 &start, &count); 2214 if (error) 2215 return (0); 2216 return (count); 2217} 2218 2219void 2220bus_delete_resource(device_t dev, int type, int rid) 2221{ 2222 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid); 2223} 2224 2225int 2226bus_child_present(device_t child) 2227{ 2228 return (BUS_CHILD_PRESENT(device_get_parent(child), child)); 2229} 2230 2231int 2232bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen) 2233{ 2234 device_t parent; 2235 2236 parent = device_get_parent(child); 2237 if (parent == NULL) { 2238 *buf = '\0'; 2239 return (0); 2240 } 2241 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen)); 2242} 2243 2244int 2245bus_child_location_str(device_t child, char *buf, size_t buflen) 2246{ 2247 device_t parent; 2248 2249 parent = device_get_parent(child); 2250 if (parent == NULL) { 2251 *buf = '\0'; 2252 return (0); 2253 } 2254 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen)); 2255} 2256 2257static int 2258root_print_child(device_t dev, device_t child) 2259{ 2260 int retval = 0; 2261 2262 retval += bus_print_child_header(dev, child); 2263 retval += printf("\n"); 2264 2265 return (retval); 2266} 2267 2268static int 2269root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg, 2270 void **cookiep) 2271{ 2272 /* 2273 * If an interrupt mapping gets to here something bad has happened. 2274 */ 2275 panic("root_setup_intr"); 2276} 2277 2278/* 2279 * If we get here, assume that the device is permanant and really is 2280 * present in the system. Removable bus drivers are expected to intercept 2281 * this call long before it gets here. We return -1 so that drivers that 2282 * really care can check vs -1 or some ERRNO returned higher in the food 2283 * chain. 2284 */ 2285static int 2286root_child_present(device_t dev, device_t child) 2287{ 2288 return (-1); 2289} 2290 2291static kobj_method_t root_methods[] = { 2292 /* Device interface */ 2293 KOBJMETHOD(device_shutdown, bus_generic_shutdown), 2294 KOBJMETHOD(device_suspend, bus_generic_suspend), 2295 KOBJMETHOD(device_resume, bus_generic_resume), 2296 2297 /* Bus interface */ 2298 KOBJMETHOD(bus_print_child, root_print_child), 2299 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar), 2300 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar), 2301 KOBJMETHOD(bus_setup_intr, root_setup_intr), 2302 KOBJMETHOD(bus_child_present, root_child_present), 2303 2304 { 0, 0 } 2305}; 2306 2307static driver_t root_driver = { 2308 "root", 2309 root_methods, 2310 1, /* no softc */ 2311}; 2312 2313device_t root_bus; 2314devclass_t root_devclass; 2315 2316static int 2317root_bus_module_handler(module_t mod, int what, void* arg) 2318{ 2319 switch (what) { 2320 case MOD_LOAD: 2321 TAILQ_INIT(&bus_data_devices); 2322 kobj_class_compile((kobj_class_t) &root_driver); 2323 root_bus = make_device(NULL, "root", 0); 2324 root_bus->desc = "System root bus"; 2325 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver); 2326 root_bus->driver = &root_driver; 2327 root_bus->state = DS_ATTACHED; 2328 root_devclass = devclass_find_internal("root", 0, FALSE); 2329 devinit(); 2330 return (0); 2331 2332 case MOD_SHUTDOWN: 2333 device_shutdown(root_bus); 2334 return (0); 2335 } 2336 2337 return (0); 2338} 2339 2340static moduledata_t root_bus_mod = { 2341 "rootbus", 2342 root_bus_module_handler, 2343 0 2344}; 2345DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 2346 2347void 2348root_bus_configure(void) 2349{ 2350 device_t dev; 2351 2352 PDEBUG((".")); 2353 2354 TAILQ_FOREACH(dev, &root_bus->children, link) { 2355 device_probe_and_attach(dev); 2356 } 2357} 2358 2359int 2360driver_module_handler(module_t mod, int what, void *arg) 2361{ 2362 int error; 2363 struct driver_module_data *dmd; 2364 devclass_t bus_devclass; 2365 kobj_class_t driver; 2366 2367 dmd = (struct driver_module_data *)arg; 2368 bus_devclass = devclass_find_internal(dmd->dmd_busname, 0, TRUE); 2369 error = 0; 2370 2371 switch (what) { 2372 case MOD_LOAD: 2373 if (dmd->dmd_chainevh) 2374 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 2375 2376 driver = dmd->dmd_driver; 2377 PDEBUG(("Loading module: driver %s on bus %s", 2378 DRIVERNAME(driver), dmd->dmd_busname)); 2379 error = devclass_add_driver(bus_devclass, driver); 2380 if (error) 2381 break; 2382 2383 /* 2384 * If the driver has any base classes, make the 2385 * devclass inherit from the devclass of the driver's 2386 * first base class. This will allow the system to 2387 * search for drivers in both devclasses for children 2388 * of a device using this driver. 2389 */ 2390 if (driver->baseclasses) { 2391 const char *parentname; 2392 parentname = driver->baseclasses[0]->name; 2393 *dmd->dmd_devclass = 2394 devclass_find_internal(driver->name, 2395 parentname, TRUE); 2396 } else { 2397 *dmd->dmd_devclass = 2398 devclass_find_internal(driver->name, 0, TRUE); 2399 } 2400 break; 2401 2402 case MOD_UNLOAD: 2403 PDEBUG(("Unloading module: driver %s from bus %s", 2404 DRIVERNAME(dmd->dmd_driver), 2405 dmd->dmd_busname)); 2406 error = devclass_delete_driver(bus_devclass, 2407 dmd->dmd_driver); 2408 2409 if (!error && dmd->dmd_chainevh) 2410 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 2411 break; 2412 } 2413 2414 return (error); 2415} 2416 2417#ifdef BUS_DEBUG 2418 2419/* the _short versions avoid iteration by not calling anything that prints 2420 * more than oneliners. I love oneliners. 2421 */ 2422 2423static void 2424print_device_short(device_t dev, int indent) 2425{ 2426 if (!dev) 2427 return; 2428 2429 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n", 2430 dev->unit, dev->desc, 2431 (dev->parent? "":"no "), 2432 (TAILQ_EMPTY(&dev->children)? "no ":""), 2433 (dev->flags&DF_ENABLED? "enabled,":"disabled,"), 2434 (dev->flags&DF_FIXEDCLASS? "fixed,":""), 2435 (dev->flags&DF_WILDCARD? "wildcard,":""), 2436 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""), 2437 (dev->ivars? "":"no "), 2438 (dev->softc? "":"no "), 2439 dev->busy)); 2440} 2441 2442static void 2443print_device(device_t dev, int indent) 2444{ 2445 if (!dev) 2446 return; 2447 2448 print_device_short(dev, indent); 2449 2450 indentprintf(("Parent:\n")); 2451 print_device_short(dev->parent, indent+1); 2452 indentprintf(("Driver:\n")); 2453 print_driver_short(dev->driver, indent+1); 2454 indentprintf(("Devclass:\n")); 2455 print_devclass_short(dev->devclass, indent+1); 2456} 2457 2458void 2459print_device_tree_short(device_t dev, int indent) 2460/* print the device and all its children (indented) */ 2461{ 2462 device_t child; 2463 2464 if (!dev) 2465 return; 2466 2467 print_device_short(dev, indent); 2468 2469 TAILQ_FOREACH(child, &dev->children, link) { 2470 print_device_tree_short(child, indent+1); 2471 } 2472} 2473 2474void 2475print_device_tree(device_t dev, int indent) 2476/* print the device and all its children (indented) */ 2477{ 2478 device_t child; 2479 2480 if (!dev) 2481 return; 2482 2483 print_device(dev, indent); 2484 2485 TAILQ_FOREACH(child, &dev->children, link) { 2486 print_device_tree(child, indent+1); 2487 } 2488} 2489 2490static void 2491print_driver_short(driver_t *driver, int indent) 2492{ 2493 if (!driver) 2494 return; 2495 2496 indentprintf(("driver %s: softc size = %zd\n", 2497 driver->name, driver->size)); 2498} 2499 2500static void 2501print_driver(driver_t *driver, int indent) 2502{ 2503 if (!driver) 2504 return; 2505 2506 print_driver_short(driver, indent); 2507} 2508 2509 2510static void 2511print_driver_list(driver_list_t drivers, int indent) 2512{ 2513 driverlink_t driver; 2514 2515 TAILQ_FOREACH(driver, &drivers, link) { 2516 print_driver(driver->driver, indent); 2517 } 2518} 2519 2520static void 2521print_devclass_short(devclass_t dc, int indent) 2522{ 2523 if ( !dc ) 2524 return; 2525 2526 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit)); 2527} 2528 2529static void 2530print_devclass(devclass_t dc, int indent) 2531{ 2532 int i; 2533 2534 if ( !dc ) 2535 return; 2536 2537 print_devclass_short(dc, indent); 2538 indentprintf(("Drivers:\n")); 2539 print_driver_list(dc->drivers, indent+1); 2540 2541 indentprintf(("Devices:\n")); 2542 for (i = 0; i < dc->maxunit; i++) 2543 if (dc->devices[i]) 2544 print_device(dc->devices[i], indent+1); 2545} 2546 2547void 2548print_devclass_list_short(void) 2549{ 2550 devclass_t dc; 2551 2552 printf("Short listing of devclasses, drivers & devices:\n"); 2553 TAILQ_FOREACH(dc, &devclasses, link) { 2554 print_devclass_short(dc, 0); 2555 } 2556} 2557 2558void 2559print_devclass_list(void) 2560{ 2561 devclass_t dc; 2562 2563 printf("Full listing of devclasses, drivers & devices:\n"); 2564 TAILQ_FOREACH(dc, &devclasses, link) { 2565 print_devclass(dc, 0); 2566 } 2567} 2568 2569#endif 2570 2571/* 2572 * User-space access to the device tree. 2573 * 2574 * We implement a small set of nodes: 2575 * 2576 * hw.bus Single integer read method to obtain the 2577 * current generation count. 2578 * hw.bus.devices Reads the entire device tree in flat space. 2579 * hw.bus.rman Resource manager interface 2580 * 2581 * We might like to add the ability to scan devclasses and/or drivers to 2582 * determine what else is currently loaded/available. 2583 */ 2584 2585static int 2586sysctl_bus(SYSCTL_HANDLER_ARGS) 2587{ 2588 struct u_businfo ubus; 2589 2590 ubus.ub_version = BUS_USER_VERSION; 2591 ubus.ub_generation = bus_data_generation; 2592 2593 return (SYSCTL_OUT(req, &ubus, sizeof(ubus))); 2594} 2595SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus, 2596 "bus-related data"); 2597 2598static int 2599sysctl_devices(SYSCTL_HANDLER_ARGS) 2600{ 2601 int *name = (int *)arg1; 2602 u_int namelen = arg2; 2603 int index; 2604 struct device *dev; 2605 struct u_device udev; /* XXX this is a bit big */ 2606 int error; 2607 2608 if (namelen != 2) 2609 return (EINVAL); 2610 2611 if (bus_data_generation_check(name[0])) 2612 return (EINVAL); 2613 2614 index = name[1]; 2615 2616 /* 2617 * Scan the list of devices, looking for the requested index. 2618 */ 2619 TAILQ_FOREACH(dev, &bus_data_devices, devlink) { 2620 if (index-- == 0) 2621 break; 2622 } 2623 if (dev == NULL) 2624 return (ENOENT); 2625 2626 /* 2627 * Populate the return array. 2628 */ 2629 udev.dv_handle = (uintptr_t)dev; 2630 udev.dv_parent = (uintptr_t)dev->parent; 2631 if (dev->nameunit == NULL) 2632 udev.dv_name[0] = '\0'; 2633 else 2634 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name)); 2635 2636 if (dev->desc == NULL) 2637 udev.dv_desc[0] = '\0'; 2638 else 2639 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc)); 2640 if (dev->driver == NULL || dev->driver->name == NULL) 2641 udev.dv_drivername[0] = '\0'; 2642 else 2643 strlcpy(udev.dv_drivername, dev->driver->name, 2644 sizeof(udev.dv_drivername)); 2645 udev.dv_pnpinfo[0] = '\0'; 2646 udev.dv_location[0] = '\0'; 2647 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo)); 2648 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location)); 2649 udev.dv_devflags = dev->devflags; 2650 udev.dv_flags = dev->flags; 2651 udev.dv_state = dev->state; 2652 error = SYSCTL_OUT(req, &udev, sizeof(udev)); 2653 return (error); 2654} 2655 2656SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices, 2657 "system device tree"); 2658 2659/* 2660 * Sysctl interface for scanning the resource lists. 2661 * 2662 * We take two input parameters; the index into the list of resource 2663 * managers, and the resource offset into the list. 2664 */ 2665static int 2666sysctl_rman(SYSCTL_HANDLER_ARGS) 2667{ 2668 int *name = (int *)arg1; 2669 u_int namelen = arg2; 2670 int rman_idx, res_idx; 2671 struct rman *rm; 2672 struct resource *res; 2673 struct u_rman urm; 2674 struct u_resource ures; 2675 int error; 2676 2677 if (namelen != 3) 2678 return (EINVAL); 2679 2680 if (bus_data_generation_check(name[0])) 2681 return (EINVAL); 2682 rman_idx = name[1]; 2683 res_idx = name[2]; 2684 2685 /* 2686 * Find the indexed resource manager 2687 */ 2688 TAILQ_FOREACH(rm, &rman_head, rm_link) { 2689 if (rman_idx-- == 0) 2690 break; 2691 } 2692 if (rm == NULL) 2693 return (ENOENT); 2694 2695 /* 2696 * If the resource index is -1, we want details on the 2697 * resource manager. 2698 */ 2699 if (res_idx == -1) { 2700 urm.rm_handle = (uintptr_t)rm; 2701 strlcpy(urm.rm_descr, rm->rm_descr, RM_TEXTLEN); 2702 urm.rm_start = rm->rm_start; 2703 urm.rm_size = rm->rm_end - rm->rm_start + 1; 2704 urm.rm_type = rm->rm_type; 2705 2706 error = SYSCTL_OUT(req, &urm, sizeof(urm)); 2707 return (error); 2708 } 2709 2710 /* 2711 * Find the indexed resource and return it. 2712 */ 2713 TAILQ_FOREACH(res, &rm->rm_list, r_link) { 2714 if (res_idx-- == 0) { 2715 ures.r_handle = (uintptr_t)res; 2716 ures.r_parent = (uintptr_t)res->r_rm; 2717 ures.r_device = (uintptr_t)res->r_dev; 2718 if (res->r_dev != NULL) { 2719 if (device_get_name(res->r_dev) != NULL) { 2720 snprintf(ures.r_devname, RM_TEXTLEN, 2721 "%s%d", 2722 device_get_name(res->r_dev), 2723 device_get_unit(res->r_dev)); 2724 } else { 2725 strlcpy(ures.r_devname, "nomatch", 2726 RM_TEXTLEN); 2727 } 2728 } else { 2729 ures.r_devname[0] = '\0'; 2730 } 2731 ures.r_start = res->r_start; 2732 ures.r_size = res->r_end - res->r_start + 1; 2733 ures.r_flags = res->r_flags; 2734 2735 error = SYSCTL_OUT(req, &ures, sizeof(ures)); 2736 return (error); 2737 } 2738 } 2739 return (ENOENT); 2740} 2741 2742SYSCTL_NODE(_hw_bus, OID_AUTO, rman, CTLFLAG_RD, sysctl_rman, 2743 "kernel resource manager"); 2744 2745int 2746bus_data_generation_check(int generation) 2747{ 2748 if (generation != bus_data_generation) 2749 return (1); 2750 2751 /* XXX generate optimised lists here? */ 2752 return (0); 2753} 2754 2755void 2756bus_data_generation_update(void) 2757{ 2758 bus_data_generation++; 2759} 2760