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