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