subr_bus.c revision 144626
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 144626 2005-04-04 15:26:51Z njl $"); 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 * @brief 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; /**< parent of this device */ 105 device_list_t children; /**< list of child devices */ 106 107 /* 108 * Details of this device. 109 */ 110 driver_t *driver; /**< current driver */ 111 devclass_t devclass; /**< current device class */ 112 int unit; /**< current unit number */ 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; /**< current device state */ 117 u_int32_t devflags; /**< api level flags for device_get_flags() */ 118 u_short flags; /**< internal device 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#define DF_REBID 128 /* Can rebid after attach */ 127 u_char order; /**< order from device_add_child_ordered() */ 128 u_char pad; 129 void *ivars; /**< instance variables */ 130 void *softc; /**< current driver's variables */ 131 132 struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables */ 133 struct sysctl_oid *sysctl_tree; /**< state for sysctl variables */ 134}; 135 136static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures"); 137static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc"); 138 139#ifdef BUS_DEBUG 140 141static int bus_debug = 1; 142TUNABLE_INT("bus.debug", &bus_debug); 143SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RW, &bus_debug, 0, 144 "Debug bus code"); 145 146#define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");} 147#define DEVICENAME(d) ((d)? device_get_name(d): "no device") 148#define DRIVERNAME(d) ((d)? d->name : "no driver") 149#define DEVCLANAME(d) ((d)? d->name : "no devclass") 150 151/** 152 * Produce the indenting, indent*2 spaces plus a '.' ahead of that to 153 * prevent syslog from deleting initial spaces 154 */ 155#define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0) 156 157static void print_device_short(device_t dev, int indent); 158static void print_device(device_t dev, int indent); 159void print_device_tree_short(device_t dev, int indent); 160void print_device_tree(device_t dev, int indent); 161static void print_driver_short(driver_t *driver, int indent); 162static void print_driver(driver_t *driver, int indent); 163static void print_driver_list(driver_list_t drivers, int indent); 164static void print_devclass_short(devclass_t dc, int indent); 165static void print_devclass(devclass_t dc, int indent); 166void print_devclass_list_short(void); 167void print_devclass_list(void); 168 169#else 170/* Make the compiler ignore the function calls */ 171#define PDEBUG(a) /* nop */ 172#define DEVICENAME(d) /* nop */ 173#define DRIVERNAME(d) /* nop */ 174#define DEVCLANAME(d) /* nop */ 175 176#define print_device_short(d,i) /* nop */ 177#define print_device(d,i) /* nop */ 178#define print_device_tree_short(d,i) /* nop */ 179#define print_device_tree(d,i) /* nop */ 180#define print_driver_short(d,i) /* nop */ 181#define print_driver(d,i) /* nop */ 182#define print_driver_list(d,i) /* nop */ 183#define print_devclass_short(d,i) /* nop */ 184#define print_devclass(d,i) /* nop */ 185#define print_devclass_list_short() /* nop */ 186#define print_devclass_list() /* nop */ 187#endif 188 189/* 190 * dev sysctl tree 191 */ 192 193enum { 194 DEVCLASS_SYSCTL_PARENT, 195}; 196 197static int 198devclass_sysctl_handler(SYSCTL_HANDLER_ARGS) 199{ 200 devclass_t dc = (devclass_t)arg1; 201 const char *value; 202 203 switch (arg2) { 204 case DEVCLASS_SYSCTL_PARENT: 205 value = dc->parent ? dc->parent->name : ""; 206 break; 207 default: 208 return (EINVAL); 209 } 210 return (SYSCTL_OUT(req, value, strlen(value))); 211} 212 213static void 214devclass_sysctl_init(devclass_t dc) 215{ 216 217 if (dc->sysctl_tree != NULL) 218 return; 219 sysctl_ctx_init(&dc->sysctl_ctx); 220 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx, 221 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name, 222 CTLFLAG_RD, 0, ""); 223 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree), 224 OID_AUTO, "%parent", CTLFLAG_RD, 225 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A", 226 "parent class"); 227} 228 229enum { 230 DEVICE_SYSCTL_DESC, 231 DEVICE_SYSCTL_DRIVER, 232 DEVICE_SYSCTL_LOCATION, 233 DEVICE_SYSCTL_PNPINFO, 234 DEVICE_SYSCTL_PARENT, 235}; 236 237static int 238device_sysctl_handler(SYSCTL_HANDLER_ARGS) 239{ 240 device_t dev = (device_t)arg1; 241 const char *value; 242 char *buf; 243 int error; 244 245 buf = NULL; 246 switch (arg2) { 247 case DEVICE_SYSCTL_DESC: 248 value = dev->desc ? dev->desc : ""; 249 break; 250 case DEVICE_SYSCTL_DRIVER: 251 value = dev->driver ? dev->driver->name : ""; 252 break; 253 case DEVICE_SYSCTL_LOCATION: 254 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO); 255 bus_child_location_str(dev, buf, 1024); 256 break; 257 case DEVICE_SYSCTL_PNPINFO: 258 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO); 259 bus_child_pnpinfo_str(dev, buf, 1024); 260 break; 261 case DEVICE_SYSCTL_PARENT: 262 value = dev->parent ? dev->parent->nameunit : ""; 263 break; 264 default: 265 return (EINVAL); 266 } 267 error = SYSCTL_OUT(req, value, strlen(value)); 268 if (buf != NULL) 269 free(buf, M_BUS); 270 return (error); 271} 272 273static void 274device_sysctl_init(device_t dev) 275{ 276 devclass_t dc = dev->devclass; 277 278 if (dev->sysctl_tree != NULL) 279 return; 280 devclass_sysctl_init(dc); 281 sysctl_ctx_init(&dev->sysctl_ctx); 282 dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx, 283 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO, 284 dev->nameunit + strlen(dc->name), 285 CTLFLAG_RD, 0, ""); 286 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 287 OID_AUTO, "%desc", CTLFLAG_RD, 288 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A", 289 "device description"); 290 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 291 OID_AUTO, "%driver", CTLFLAG_RD, 292 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A", 293 "device driver name"); 294 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 295 OID_AUTO, "%location", CTLFLAG_RD, 296 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A", 297 "device location relative to parent"); 298 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 299 OID_AUTO, "%pnpinfo", CTLFLAG_RD, 300 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A", 301 "device identification"); 302 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 303 OID_AUTO, "%parent", CTLFLAG_RD, 304 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A", 305 "parent device"); 306} 307 308static void 309device_sysctl_fini(device_t dev) 310{ 311 if (dev->sysctl_tree == NULL) 312 return; 313 sysctl_ctx_free(&dev->sysctl_ctx); 314 dev->sysctl_tree = NULL; 315} 316 317/* 318 * /dev/devctl implementation 319 */ 320 321/* 322 * This design allows only one reader for /dev/devctl. This is not desirable 323 * in the long run, but will get a lot of hair out of this implementation. 324 * Maybe we should make this device a clonable device. 325 * 326 * Also note: we specifically do not attach a device to the device_t tree 327 * to avoid potential chicken and egg problems. One could argue that all 328 * of this belongs to the root node. One could also further argue that the 329 * sysctl interface that we have not might more properly be an ioctl 330 * interface, but at this stage of the game, I'm not inclined to rock that 331 * boat. 332 * 333 * I'm also not sure that the SIGIO support is done correctly or not, as 334 * I copied it from a driver that had SIGIO support that likely hasn't been 335 * tested since 3.4 or 2.2.8! 336 */ 337 338static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS); 339static int devctl_disable = 0; 340TUNABLE_INT("hw.bus.devctl_disable", &devctl_disable); 341SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, 0, 0, 342 sysctl_devctl_disable, "I", "devctl disable"); 343 344static d_open_t devopen; 345static d_close_t devclose; 346static d_read_t devread; 347static d_ioctl_t devioctl; 348static d_poll_t devpoll; 349 350static struct cdevsw dev_cdevsw = { 351 .d_version = D_VERSION, 352 .d_flags = D_NEEDGIANT, 353 .d_open = devopen, 354 .d_close = devclose, 355 .d_read = devread, 356 .d_ioctl = devioctl, 357 .d_poll = devpoll, 358 .d_name = "devctl", 359}; 360 361struct dev_event_info 362{ 363 char *dei_data; 364 TAILQ_ENTRY(dev_event_info) dei_link; 365}; 366 367TAILQ_HEAD(devq, dev_event_info); 368 369static struct dev_softc 370{ 371 int inuse; 372 int nonblock; 373 struct mtx mtx; 374 struct cv cv; 375 struct selinfo sel; 376 struct devq devq; 377 struct proc *async_proc; 378} devsoftc; 379 380static struct cdev *devctl_dev; 381 382static void 383devinit(void) 384{ 385 devctl_dev = make_dev(&dev_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, 386 "devctl"); 387 mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF); 388 cv_init(&devsoftc.cv, "dev cv"); 389 TAILQ_INIT(&devsoftc.devq); 390} 391 392static int 393devopen(struct cdev *dev, int oflags, int devtype, d_thread_t *td) 394{ 395 if (devsoftc.inuse) 396 return (EBUSY); 397 /* move to init */ 398 devsoftc.inuse = 1; 399 devsoftc.nonblock = 0; 400 devsoftc.async_proc = NULL; 401 return (0); 402} 403 404static int 405devclose(struct cdev *dev, int fflag, int devtype, d_thread_t *td) 406{ 407 devsoftc.inuse = 0; 408 mtx_lock(&devsoftc.mtx); 409 cv_broadcast(&devsoftc.cv); 410 mtx_unlock(&devsoftc.mtx); 411 412 return (0); 413} 414 415/* 416 * The read channel for this device is used to report changes to 417 * userland in realtime. We are required to free the data as well as 418 * the n1 object because we allocate them separately. Also note that 419 * we return one record at a time. If you try to read this device a 420 * character at a time, you will loose the rest of the data. Listening 421 * programs are expected to cope. 422 */ 423static int 424devread(struct cdev *dev, struct uio *uio, int ioflag) 425{ 426 struct dev_event_info *n1; 427 int rv; 428 429 mtx_lock(&devsoftc.mtx); 430 while (TAILQ_EMPTY(&devsoftc.devq)) { 431 if (devsoftc.nonblock) { 432 mtx_unlock(&devsoftc.mtx); 433 return (EAGAIN); 434 } 435 rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx); 436 if (rv) { 437 /* 438 * Need to translate ERESTART to EINTR here? -- jake 439 */ 440 mtx_unlock(&devsoftc.mtx); 441 return (rv); 442 } 443 } 444 n1 = TAILQ_FIRST(&devsoftc.devq); 445 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link); 446 mtx_unlock(&devsoftc.mtx); 447 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio); 448 free(n1->dei_data, M_BUS); 449 free(n1, M_BUS); 450 return (rv); 451} 452 453static int 454devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, d_thread_t *td) 455{ 456 switch (cmd) { 457 458 case FIONBIO: 459 if (*(int*)data) 460 devsoftc.nonblock = 1; 461 else 462 devsoftc.nonblock = 0; 463 return (0); 464 case FIOASYNC: 465 if (*(int*)data) 466 devsoftc.async_proc = td->td_proc; 467 else 468 devsoftc.async_proc = NULL; 469 return (0); 470 471 /* (un)Support for other fcntl() calls. */ 472 case FIOCLEX: 473 case FIONCLEX: 474 case FIONREAD: 475 case FIOSETOWN: 476 case FIOGETOWN: 477 default: 478 break; 479 } 480 return (ENOTTY); 481} 482 483static int 484devpoll(struct cdev *dev, int events, d_thread_t *td) 485{ 486 int revents = 0; 487 488 mtx_lock(&devsoftc.mtx); 489 if (events & (POLLIN | POLLRDNORM)) { 490 if (!TAILQ_EMPTY(&devsoftc.devq)) 491 revents = events & (POLLIN | POLLRDNORM); 492 else 493 selrecord(td, &devsoftc.sel); 494 } 495 mtx_unlock(&devsoftc.mtx); 496 497 return (revents); 498} 499 500/** 501 * @brief Queue data to be read from the devctl device 502 * 503 * Generic interface to queue data to the devctl device. It is 504 * assumed that @p data is properly formatted. It is further assumed 505 * that @p data is allocated using the M_BUS malloc type. 506 */ 507void 508devctl_queue_data(char *data) 509{ 510 struct dev_event_info *n1 = NULL; 511 struct proc *p; 512 513 n1 = malloc(sizeof(*n1), M_BUS, M_NOWAIT); 514 if (n1 == NULL) 515 return; 516 n1->dei_data = data; 517 mtx_lock(&devsoftc.mtx); 518 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link); 519 cv_broadcast(&devsoftc.cv); 520 mtx_unlock(&devsoftc.mtx); 521 selwakeup(&devsoftc.sel); 522 p = devsoftc.async_proc; 523 if (p != NULL) { 524 PROC_LOCK(p); 525 psignal(p, SIGIO); 526 PROC_UNLOCK(p); 527 } 528} 529 530/** 531 * @brief Send a 'notification' to userland, using standard ways 532 */ 533void 534devctl_notify(const char *system, const char *subsystem, const char *type, 535 const char *data) 536{ 537 int len = 0; 538 char *msg; 539 540 if (system == NULL) 541 return; /* BOGUS! Must specify system. */ 542 if (subsystem == NULL) 543 return; /* BOGUS! Must specify subsystem. */ 544 if (type == NULL) 545 return; /* BOGUS! Must specify type. */ 546 len += strlen(" system=") + strlen(system); 547 len += strlen(" subsystem=") + strlen(subsystem); 548 len += strlen(" type=") + strlen(type); 549 /* add in the data message plus newline. */ 550 if (data != NULL) 551 len += strlen(data); 552 len += 3; /* '!', '\n', and NUL */ 553 msg = malloc(len, M_BUS, M_NOWAIT); 554 if (msg == NULL) 555 return; /* Drop it on the floor */ 556 snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n", system, 557 subsystem, type, data); 558 devctl_queue_data(msg); 559} 560 561/* 562 * Common routine that tries to make sending messages as easy as possible. 563 * We allocate memory for the data, copy strings into that, but do not 564 * free it unless there's an error. The dequeue part of the driver should 565 * free the data. We don't send data when the device is disabled. We do 566 * send data, even when we have no listeners, because we wish to avoid 567 * races relating to startup and restart of listening applications. 568 * 569 * devaddq is designed to string together the type of event, with the 570 * object of that event, plus the plug and play info and location info 571 * for that event. This is likely most useful for devices, but less 572 * useful for other consumers of this interface. Those should use 573 * the devctl_queue_data() interface instead. 574 */ 575static void 576devaddq(const char *type, const char *what, device_t dev) 577{ 578 char *data = NULL; 579 char *loc = NULL; 580 char *pnp = NULL; 581 const char *parstr; 582 583 if (devctl_disable) 584 return; 585 data = malloc(1024, M_BUS, M_NOWAIT); 586 if (data == NULL) 587 goto bad; 588 589 /* get the bus specific location of this device */ 590 loc = malloc(1024, M_BUS, M_NOWAIT); 591 if (loc == NULL) 592 goto bad; 593 *loc = '\0'; 594 bus_child_location_str(dev, loc, 1024); 595 596 /* Get the bus specific pnp info of this device */ 597 pnp = malloc(1024, M_BUS, M_NOWAIT); 598 if (pnp == NULL) 599 goto bad; 600 *pnp = '\0'; 601 bus_child_pnpinfo_str(dev, pnp, 1024); 602 603 /* Get the parent of this device, or / if high enough in the tree. */ 604 if (device_get_parent(dev) == NULL) 605 parstr = "."; /* Or '/' ? */ 606 else 607 parstr = device_get_nameunit(device_get_parent(dev)); 608 /* String it all together. */ 609 snprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp, 610 parstr); 611 free(loc, M_BUS); 612 free(pnp, M_BUS); 613 devctl_queue_data(data); 614 return; 615bad: 616 free(pnp, M_BUS); 617 free(loc, M_BUS); 618 free(data, M_BUS); 619 return; 620} 621 622/* 623 * A device was added to the tree. We are called just after it successfully 624 * attaches (that is, probe and attach success for this device). No call 625 * is made if a device is merely parented into the tree. See devnomatch 626 * if probe fails. If attach fails, no notification is sent (but maybe 627 * we should have a different message for this). 628 */ 629static void 630devadded(device_t dev) 631{ 632 char *pnp = NULL; 633 char *tmp = NULL; 634 635 pnp = malloc(1024, M_BUS, M_NOWAIT); 636 if (pnp == NULL) 637 goto fail; 638 tmp = malloc(1024, M_BUS, M_NOWAIT); 639 if (tmp == NULL) 640 goto fail; 641 *pnp = '\0'; 642 bus_child_pnpinfo_str(dev, pnp, 1024); 643 snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp); 644 devaddq("+", tmp, dev); 645fail: 646 if (pnp != NULL) 647 free(pnp, M_BUS); 648 if (tmp != NULL) 649 free(tmp, M_BUS); 650 return; 651} 652 653/* 654 * A device was removed from the tree. We are called just before this 655 * happens. 656 */ 657static void 658devremoved(device_t dev) 659{ 660 char *pnp = NULL; 661 char *tmp = NULL; 662 663 pnp = malloc(1024, M_BUS, M_NOWAIT); 664 if (pnp == NULL) 665 goto fail; 666 tmp = malloc(1024, M_BUS, M_NOWAIT); 667 if (tmp == NULL) 668 goto fail; 669 *pnp = '\0'; 670 bus_child_pnpinfo_str(dev, pnp, 1024); 671 snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp); 672 devaddq("-", tmp, dev); 673fail: 674 if (pnp != NULL) 675 free(pnp, M_BUS); 676 if (tmp != NULL) 677 free(tmp, M_BUS); 678 return; 679} 680 681/* 682 * Called when there's no match for this device. This is only called 683 * the first time that no match happens, so we don't keep getitng this 684 * message. Should that prove to be undesirable, we can change it. 685 * This is called when all drivers that can attach to a given bus 686 * decline to accept this device. Other errrors may not be detected. 687 */ 688static void 689devnomatch(device_t dev) 690{ 691 devaddq("?", "", dev); 692} 693 694static int 695sysctl_devctl_disable(SYSCTL_HANDLER_ARGS) 696{ 697 struct dev_event_info *n1; 698 int dis, error; 699 700 dis = devctl_disable; 701 error = sysctl_handle_int(oidp, &dis, 0, req); 702 if (error || !req->newptr) 703 return (error); 704 mtx_lock(&devsoftc.mtx); 705 devctl_disable = dis; 706 if (dis) { 707 while (!TAILQ_EMPTY(&devsoftc.devq)) { 708 n1 = TAILQ_FIRST(&devsoftc.devq); 709 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link); 710 free(n1->dei_data, M_BUS); 711 free(n1, M_BUS); 712 } 713 } 714 mtx_unlock(&devsoftc.mtx); 715 return (0); 716} 717 718/* End of /dev/devctl code */ 719 720TAILQ_HEAD(,device) bus_data_devices; 721static int bus_data_generation = 1; 722 723kobj_method_t null_methods[] = { 724 { 0, 0 } 725}; 726 727DEFINE_CLASS(null, null_methods, 0); 728 729/* 730 * Devclass implementation 731 */ 732 733static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses); 734 735 736/** 737 * @internal 738 * @brief Find or create a device class 739 * 740 * If a device class with the name @p classname exists, return it, 741 * otherwise if @p create is non-zero create and return a new device 742 * class. 743 * 744 * If @p parentname is non-NULL, the parent of the devclass is set to 745 * the devclass of that name. 746 * 747 * @param classname the devclass name to find or create 748 * @param parentname the parent devclass name or @c NULL 749 * @param create non-zero to create a devclass 750 */ 751static devclass_t 752devclass_find_internal(const char *classname, const char *parentname, 753 int create) 754{ 755 devclass_t dc; 756 757 PDEBUG(("looking for %s", classname)); 758 if (!classname) 759 return (NULL); 760 761 TAILQ_FOREACH(dc, &devclasses, link) { 762 if (!strcmp(dc->name, classname)) 763 break; 764 } 765 766 if (create && !dc) { 767 PDEBUG(("creating %s", classname)); 768 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1, 769 M_BUS, M_NOWAIT|M_ZERO); 770 if (!dc) 771 return (NULL); 772 dc->parent = NULL; 773 dc->name = (char*) (dc + 1); 774 strcpy(dc->name, classname); 775 TAILQ_INIT(&dc->drivers); 776 TAILQ_INSERT_TAIL(&devclasses, dc, link); 777 778 bus_data_generation_update(); 779 } 780 if (parentname && dc && !dc->parent) { 781 dc->parent = devclass_find_internal(parentname, 0, FALSE); 782 } 783 784 return (dc); 785} 786 787/** 788 * @brief Create a device class 789 * 790 * If a device class with the name @p classname exists, return it, 791 * otherwise create and return a new device class. 792 * 793 * @param classname the devclass name to find or create 794 */ 795devclass_t 796devclass_create(const char *classname) 797{ 798 return (devclass_find_internal(classname, 0, TRUE)); 799} 800 801/** 802 * @brief Find a device class 803 * 804 * If a device class with the name @p classname exists, return it, 805 * otherwise return @c NULL. 806 * 807 * @param classname the devclass name to find 808 */ 809devclass_t 810devclass_find(const char *classname) 811{ 812 return (devclass_find_internal(classname, 0, FALSE)); 813} 814 815/** 816 * @brief Add a device driver to a device class 817 * 818 * Add a device driver to a devclass. This is normally called 819 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of 820 * all devices in the devclass will be called to allow them to attempt 821 * to re-probe any unmatched children. 822 * 823 * @param dc the devclass to edit 824 * @param driver the driver to register 825 */ 826int 827devclass_add_driver(devclass_t dc, driver_t *driver) 828{ 829 driverlink_t dl; 830 int i; 831 832 PDEBUG(("%s", DRIVERNAME(driver))); 833 834 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO); 835 if (!dl) 836 return (ENOMEM); 837 838 /* 839 * Compile the driver's methods. Also increase the reference count 840 * so that the class doesn't get freed when the last instance 841 * goes. This means we can safely use static methods and avoids a 842 * double-free in devclass_delete_driver. 843 */ 844 kobj_class_compile((kobj_class_t) driver); 845 846 /* 847 * Make sure the devclass which the driver is implementing exists. 848 */ 849 devclass_find_internal(driver->name, 0, TRUE); 850 851 dl->driver = driver; 852 TAILQ_INSERT_TAIL(&dc->drivers, dl, link); 853 driver->refs++; /* XXX: kobj_mtx */ 854 855 /* 856 * Call BUS_DRIVER_ADDED for any existing busses in this class. 857 */ 858 for (i = 0; i < dc->maxunit; i++) 859 if (dc->devices[i]) 860 BUS_DRIVER_ADDED(dc->devices[i], driver); 861 862 bus_data_generation_update(); 863 return (0); 864} 865 866/** 867 * @brief Delete a device driver from a device class 868 * 869 * Delete a device driver from a devclass. This is normally called 870 * automatically by DRIVER_MODULE(). 871 * 872 * If the driver is currently attached to any devices, 873 * devclass_delete_driver() will first attempt to detach from each 874 * device. If one of the detach calls fails, the driver will not be 875 * deleted. 876 * 877 * @param dc the devclass to edit 878 * @param driver the driver to unregister 879 */ 880int 881devclass_delete_driver(devclass_t busclass, driver_t *driver) 882{ 883 devclass_t dc = devclass_find(driver->name); 884 driverlink_t dl; 885 device_t dev; 886 int i; 887 int error; 888 889 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass))); 890 891 if (!dc) 892 return (0); 893 894 /* 895 * Find the link structure in the bus' list of drivers. 896 */ 897 TAILQ_FOREACH(dl, &busclass->drivers, link) { 898 if (dl->driver == driver) 899 break; 900 } 901 902 if (!dl) { 903 PDEBUG(("%s not found in %s list", driver->name, 904 busclass->name)); 905 return (ENOENT); 906 } 907 908 /* 909 * Disassociate from any devices. We iterate through all the 910 * devices in the devclass of the driver and detach any which are 911 * using the driver and which have a parent in the devclass which 912 * we are deleting from. 913 * 914 * Note that since a driver can be in multiple devclasses, we 915 * should not detach devices which are not children of devices in 916 * the affected devclass. 917 */ 918 for (i = 0; i < dc->maxunit; i++) { 919 if (dc->devices[i]) { 920 dev = dc->devices[i]; 921 if (dev->driver == driver && dev->parent && 922 dev->parent->devclass == busclass) { 923 if ((error = device_detach(dev)) != 0) 924 return (error); 925 device_set_driver(dev, NULL); 926 } 927 } 928 } 929 930 TAILQ_REMOVE(&busclass->drivers, dl, link); 931 free(dl, M_BUS); 932 933 /* XXX: kobj_mtx */ 934 driver->refs--; 935 if (driver->refs == 0) 936 kobj_class_free((kobj_class_t) driver); 937 938 bus_data_generation_update(); 939 return (0); 940} 941 942/** 943 * @brief Quiesces a set of device drivers from a device class 944 * 945 * Quiesce a device driver from a devclass. This is normally called 946 * automatically by DRIVER_MODULE(). 947 * 948 * If the driver is currently attached to any devices, 949 * devclass_quiesece_driver() will first attempt to quiesce each 950 * device. 951 * 952 * @param dc the devclass to edit 953 * @param driver the driver to unregister 954 */ 955int 956devclass_quiesce_driver(devclass_t busclass, driver_t *driver) 957{ 958 devclass_t dc = devclass_find(driver->name); 959 driverlink_t dl; 960 device_t dev; 961 int i; 962 int error; 963 964 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass))); 965 966 if (!dc) 967 return (0); 968 969 /* 970 * Find the link structure in the bus' list of drivers. 971 */ 972 TAILQ_FOREACH(dl, &busclass->drivers, link) { 973 if (dl->driver == driver) 974 break; 975 } 976 977 if (!dl) { 978 PDEBUG(("%s not found in %s list", driver->name, 979 busclass->name)); 980 return (ENOENT); 981 } 982 983 /* 984 * Quiesce all devices. We iterate through all the devices in 985 * the devclass of the driver and quiesce any which are using 986 * the driver and which have a parent in the devclass which we 987 * are quiescing. 988 * 989 * Note that since a driver can be in multiple devclasses, we 990 * should not quiesce devices which are not children of 991 * devices in the affected devclass. 992 */ 993 for (i = 0; i < dc->maxunit; i++) { 994 if (dc->devices[i]) { 995 dev = dc->devices[i]; 996 if (dev->driver == driver && dev->parent && 997 dev->parent->devclass == busclass) { 998 if ((error = device_quiesce(dev)) != 0) 999 return (error); 1000 } 1001 } 1002 } 1003 1004 return (0); 1005} 1006 1007/** 1008 * @internal 1009 */ 1010static driverlink_t 1011devclass_find_driver_internal(devclass_t dc, const char *classname) 1012{ 1013 driverlink_t dl; 1014 1015 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc))); 1016 1017 TAILQ_FOREACH(dl, &dc->drivers, link) { 1018 if (!strcmp(dl->driver->name, classname)) 1019 return (dl); 1020 } 1021 1022 PDEBUG(("not found")); 1023 return (NULL); 1024} 1025 1026/** 1027 * @brief Search a devclass for a driver 1028 * 1029 * This function searches the devclass's list of drivers and returns 1030 * the first driver whose name is @p classname or @c NULL if there is 1031 * no driver of that name. 1032 * 1033 * @param dc the devclass to search 1034 * @param classname the driver name to search for 1035 */ 1036kobj_class_t 1037devclass_find_driver(devclass_t dc, const char *classname) 1038{ 1039 driverlink_t dl; 1040 1041 dl = devclass_find_driver_internal(dc, classname); 1042 if (dl) 1043 return (dl->driver); 1044 return (NULL); 1045} 1046 1047/** 1048 * @brief Return the name of the devclass 1049 */ 1050const char * 1051devclass_get_name(devclass_t dc) 1052{ 1053 return (dc->name); 1054} 1055 1056/** 1057 * @brief Find a device given a unit number 1058 * 1059 * @param dc the devclass to search 1060 * @param unit the unit number to search for 1061 * 1062 * @returns the device with the given unit number or @c 1063 * NULL if there is no such device 1064 */ 1065device_t 1066devclass_get_device(devclass_t dc, int unit) 1067{ 1068 if (dc == NULL || unit < 0 || unit >= dc->maxunit) 1069 return (NULL); 1070 return (dc->devices[unit]); 1071} 1072 1073/** 1074 * @brief Find the softc field of a device given a unit number 1075 * 1076 * @param dc the devclass to search 1077 * @param unit the unit number to search for 1078 * 1079 * @returns the softc field of the device with the given 1080 * unit number or @c NULL if there is no such 1081 * device 1082 */ 1083void * 1084devclass_get_softc(devclass_t dc, int unit) 1085{ 1086 device_t dev; 1087 1088 dev = devclass_get_device(dc, unit); 1089 if (!dev) 1090 return (NULL); 1091 1092 return (device_get_softc(dev)); 1093} 1094 1095/** 1096 * @brief Get a list of devices in the devclass 1097 * 1098 * An array containing a list of all the devices in the given devclass 1099 * is allocated and returned in @p *devlistp. The number of devices 1100 * in the array is returned in @p *devcountp. The caller should free 1101 * the array using @c free(p, M_TEMP). 1102 * 1103 * @param dc the devclass to examine 1104 * @param devlistp points at location for array pointer return 1105 * value 1106 * @param devcountp points at location for array size return value 1107 * 1108 * @retval 0 success 1109 * @retval ENOMEM the array allocation failed 1110 */ 1111int 1112devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp) 1113{ 1114 int count, i; 1115 device_t *list; 1116 1117 count = devclass_get_count(dc); 1118 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO); 1119 if (!list) 1120 return (ENOMEM); 1121 1122 count = 0; 1123 for (i = 0; i < dc->maxunit; i++) { 1124 if (dc->devices[i]) { 1125 list[count] = dc->devices[i]; 1126 count++; 1127 } 1128 } 1129 1130 *devlistp = list; 1131 *devcountp = count; 1132 1133 return (0); 1134} 1135 1136/** 1137 * @brief Get a list of drivers in the devclass 1138 * 1139 * An array containing a list of pointers to all the drivers in the 1140 * given devclass is allocated and returned in @p *listp. The number 1141 * of drivers in the array is returned in @p *countp. The caller should 1142 * free the array using @c free(p, M_TEMP). 1143 * 1144 * @param dc the devclass to examine 1145 * @param listp gives location for array pointer return value 1146 * @param countp gives location for number of array elements 1147 * return value 1148 * 1149 * @retval 0 success 1150 * @retval ENOMEM the array allocation failed 1151 */ 1152int 1153devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp) 1154{ 1155 driverlink_t dl; 1156 driver_t **list; 1157 int count; 1158 1159 count = 0; 1160 TAILQ_FOREACH(dl, &dc->drivers, link) 1161 count++; 1162 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT); 1163 if (list == NULL) 1164 return (ENOMEM); 1165 1166 count = 0; 1167 TAILQ_FOREACH(dl, &dc->drivers, link) { 1168 list[count] = dl->driver; 1169 count++; 1170 } 1171 *listp = list; 1172 *countp = count; 1173 1174 return (0); 1175} 1176 1177/** 1178 * @brief Get the number of devices in a devclass 1179 * 1180 * @param dc the devclass to examine 1181 */ 1182int 1183devclass_get_count(devclass_t dc) 1184{ 1185 int count, i; 1186 1187 count = 0; 1188 for (i = 0; i < dc->maxunit; i++) 1189 if (dc->devices[i]) 1190 count++; 1191 return (count); 1192} 1193 1194/** 1195 * @brief Get the maximum unit number used in a devclass 1196 * 1197 * @param dc the devclass to examine 1198 */ 1199int 1200devclass_get_maxunit(devclass_t dc) 1201{ 1202 return (dc->maxunit); 1203} 1204 1205/** 1206 * @brief Find a free unit number in a devclass 1207 * 1208 * This function searches for the first unused unit number greater 1209 * that or equal to @p unit. 1210 * 1211 * @param dc the devclass to examine 1212 * @param unit the first unit number to check 1213 */ 1214int 1215devclass_find_free_unit(devclass_t dc, int unit) 1216{ 1217 if (dc == NULL) 1218 return (unit); 1219 while (unit < dc->maxunit && dc->devices[unit] != NULL) 1220 unit++; 1221 return (unit); 1222} 1223 1224/** 1225 * @brief Set the parent of a devclass 1226 * 1227 * The parent class is normally initialised automatically by 1228 * DRIVER_MODULE(). 1229 * 1230 * @param dc the devclass to edit 1231 * @param pdc the new parent devclass 1232 */ 1233void 1234devclass_set_parent(devclass_t dc, devclass_t pdc) 1235{ 1236 dc->parent = pdc; 1237} 1238 1239/** 1240 * @brief Get the parent of a devclass 1241 * 1242 * @param dc the devclass to examine 1243 */ 1244devclass_t 1245devclass_get_parent(devclass_t dc) 1246{ 1247 return (dc->parent); 1248} 1249 1250struct sysctl_ctx_list * 1251devclass_get_sysctl_ctx(devclass_t dc) 1252{ 1253 return (&dc->sysctl_ctx); 1254} 1255 1256struct sysctl_oid * 1257devclass_get_sysctl_tree(devclass_t dc) 1258{ 1259 return (dc->sysctl_tree); 1260} 1261 1262/** 1263 * @internal 1264 * @brief Allocate a unit number 1265 * 1266 * On entry, @p *unitp is the desired unit number (or @c -1 if any 1267 * will do). The allocated unit number is returned in @p *unitp. 1268 1269 * @param dc the devclass to allocate from 1270 * @param unitp points at the location for the allocated unit 1271 * number 1272 * 1273 * @retval 0 success 1274 * @retval EEXIST the requested unit number is already allocated 1275 * @retval ENOMEM memory allocation failure 1276 */ 1277static int 1278devclass_alloc_unit(devclass_t dc, int *unitp) 1279{ 1280 int unit = *unitp; 1281 1282 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc))); 1283 1284 /* If we were given a wired unit number, check for existing device */ 1285 /* XXX imp XXX */ 1286 if (unit != -1) { 1287 if (unit >= 0 && unit < dc->maxunit && 1288 dc->devices[unit] != NULL) { 1289 if (bootverbose) 1290 printf("%s: %s%d already exists; skipping it\n", 1291 dc->name, dc->name, *unitp); 1292 return (EEXIST); 1293 } 1294 } else { 1295 /* Unwired device, find the next available slot for it */ 1296 unit = 0; 1297 while (unit < dc->maxunit && dc->devices[unit] != NULL) 1298 unit++; 1299 } 1300 1301 /* 1302 * We've selected a unit beyond the length of the table, so let's 1303 * extend the table to make room for all units up to and including 1304 * this one. 1305 */ 1306 if (unit >= dc->maxunit) { 1307 device_t *newlist; 1308 int newsize; 1309 1310 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t)); 1311 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT); 1312 if (!newlist) 1313 return (ENOMEM); 1314 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit); 1315 bzero(newlist + dc->maxunit, 1316 sizeof(device_t) * (newsize - dc->maxunit)); 1317 if (dc->devices) 1318 free(dc->devices, M_BUS); 1319 dc->devices = newlist; 1320 dc->maxunit = newsize; 1321 } 1322 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc))); 1323 1324 *unitp = unit; 1325 return (0); 1326} 1327 1328/** 1329 * @internal 1330 * @brief Add a device to a devclass 1331 * 1332 * A unit number is allocated for the device (using the device's 1333 * preferred unit number if any) and the device is registered in the 1334 * devclass. This allows the device to be looked up by its unit 1335 * number, e.g. by decoding a dev_t minor number. 1336 * 1337 * @param dc the devclass to add to 1338 * @param dev the device to add 1339 * 1340 * @retval 0 success 1341 * @retval EEXIST the requested unit number is already allocated 1342 * @retval ENOMEM memory allocation failure 1343 */ 1344static int 1345devclass_add_device(devclass_t dc, device_t dev) 1346{ 1347 int buflen, error; 1348 1349 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 1350 1351 buflen = snprintf(NULL, 0, "%s%d$", dc->name, dev->unit); 1352 if (buflen < 0) 1353 return (ENOMEM); 1354 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO); 1355 if (!dev->nameunit) 1356 return (ENOMEM); 1357 1358 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) { 1359 free(dev->nameunit, M_BUS); 1360 dev->nameunit = NULL; 1361 return (error); 1362 } 1363 dc->devices[dev->unit] = dev; 1364 dev->devclass = dc; 1365 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit); 1366 1367 return (0); 1368} 1369 1370/** 1371 * @internal 1372 * @brief Delete a device from a devclass 1373 * 1374 * The device is removed from the devclass's device list and its unit 1375 * number is freed. 1376 1377 * @param dc the devclass to delete from 1378 * @param dev the device to delete 1379 * 1380 * @retval 0 success 1381 */ 1382static int 1383devclass_delete_device(devclass_t dc, device_t dev) 1384{ 1385 if (!dc || !dev) 1386 return (0); 1387 1388 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 1389 1390 if (dev->devclass != dc || dc->devices[dev->unit] != dev) 1391 panic("devclass_delete_device: inconsistent device class"); 1392 dc->devices[dev->unit] = NULL; 1393 if (dev->flags & DF_WILDCARD) 1394 dev->unit = -1; 1395 dev->devclass = NULL; 1396 free(dev->nameunit, M_BUS); 1397 dev->nameunit = NULL; 1398 1399 return (0); 1400} 1401 1402/** 1403 * @internal 1404 * @brief Make a new device and add it as a child of @p parent 1405 * 1406 * @param parent the parent of the new device 1407 * @param name the devclass name of the new device or @c NULL 1408 * to leave the devclass unspecified 1409 * @parem unit the unit number of the new device of @c -1 to 1410 * leave the unit number unspecified 1411 * 1412 * @returns the new device 1413 */ 1414static device_t 1415make_device(device_t parent, const char *name, int unit) 1416{ 1417 device_t dev; 1418 devclass_t dc; 1419 1420 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit)); 1421 1422 if (name) { 1423 dc = devclass_find_internal(name, 0, TRUE); 1424 if (!dc) { 1425 printf("make_device: can't find device class %s\n", 1426 name); 1427 return (NULL); 1428 } 1429 } else { 1430 dc = NULL; 1431 } 1432 1433 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO); 1434 if (!dev) 1435 return (NULL); 1436 1437 dev->parent = parent; 1438 TAILQ_INIT(&dev->children); 1439 kobj_init((kobj_t) dev, &null_class); 1440 dev->driver = NULL; 1441 dev->devclass = NULL; 1442 dev->unit = unit; 1443 dev->nameunit = NULL; 1444 dev->desc = NULL; 1445 dev->busy = 0; 1446 dev->devflags = 0; 1447 dev->flags = DF_ENABLED; 1448 dev->order = 0; 1449 if (unit == -1) 1450 dev->flags |= DF_WILDCARD; 1451 if (name) { 1452 dev->flags |= DF_FIXEDCLASS; 1453 if (devclass_add_device(dc, dev)) { 1454 kobj_delete((kobj_t) dev, M_BUS); 1455 return (NULL); 1456 } 1457 } 1458 dev->ivars = NULL; 1459 dev->softc = NULL; 1460 1461 dev->state = DS_NOTPRESENT; 1462 1463 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink); 1464 bus_data_generation_update(); 1465 1466 return (dev); 1467} 1468 1469/** 1470 * @internal 1471 * @brief Print a description of a device. 1472 */ 1473static int 1474device_print_child(device_t dev, device_t child) 1475{ 1476 int retval = 0; 1477 1478 if (device_is_alive(child)) 1479 retval += BUS_PRINT_CHILD(dev, child); 1480 else 1481 retval += device_printf(child, " not found\n"); 1482 1483 return (retval); 1484} 1485 1486/** 1487 * @brief Create a new device 1488 * 1489 * This creates a new device and adds it as a child of an existing 1490 * parent device. The new device will be added after the last existing 1491 * child with order zero. 1492 * 1493 * @param dev the device which will be the parent of the 1494 * new child device 1495 * @param name devclass name for new device or @c NULL if not 1496 * specified 1497 * @param unit unit number for new device or @c -1 if not 1498 * specified 1499 * 1500 * @returns the new device 1501 */ 1502device_t 1503device_add_child(device_t dev, const char *name, int unit) 1504{ 1505 return (device_add_child_ordered(dev, 0, name, unit)); 1506} 1507 1508/** 1509 * @brief Create a new device 1510 * 1511 * This creates a new device and adds it as a child of an existing 1512 * parent device. The new device will be added after the last existing 1513 * child with the same order. 1514 * 1515 * @param dev the device which will be the parent of the 1516 * new child device 1517 * @param order a value which is used to partially sort the 1518 * children of @p dev - devices created using 1519 * lower values of @p order appear first in @p 1520 * dev's list of children 1521 * @param name devclass name for new device or @c NULL if not 1522 * specified 1523 * @param unit unit number for new device or @c -1 if not 1524 * specified 1525 * 1526 * @returns the new device 1527 */ 1528device_t 1529device_add_child_ordered(device_t dev, int order, const char *name, int unit) 1530{ 1531 device_t child; 1532 device_t place; 1533 1534 PDEBUG(("%s at %s with order %d as unit %d", 1535 name, DEVICENAME(dev), order, unit)); 1536 1537 child = make_device(dev, name, unit); 1538 if (child == NULL) 1539 return (child); 1540 child->order = order; 1541 1542 TAILQ_FOREACH(place, &dev->children, link) { 1543 if (place->order > order) 1544 break; 1545 } 1546 1547 if (place) { 1548 /* 1549 * The device 'place' is the first device whose order is 1550 * greater than the new child. 1551 */ 1552 TAILQ_INSERT_BEFORE(place, child, link); 1553 } else { 1554 /* 1555 * The new child's order is greater or equal to the order of 1556 * any existing device. Add the child to the tail of the list. 1557 */ 1558 TAILQ_INSERT_TAIL(&dev->children, child, link); 1559 } 1560 1561 bus_data_generation_update(); 1562 return (child); 1563} 1564 1565/** 1566 * @brief Delete a device 1567 * 1568 * This function deletes a device along with all of its children. If 1569 * the device currently has a driver attached to it, the device is 1570 * detached first using device_detach(). 1571 * 1572 * @param dev the parent device 1573 * @param child the device to delete 1574 * 1575 * @retval 0 success 1576 * @retval non-zero a unit error code describing the error 1577 */ 1578int 1579device_delete_child(device_t dev, device_t child) 1580{ 1581 int error; 1582 device_t grandchild; 1583 1584 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev))); 1585 1586 /* remove children first */ 1587 while ( (grandchild = TAILQ_FIRST(&child->children)) ) { 1588 error = device_delete_child(child, grandchild); 1589 if (error) 1590 return (error); 1591 } 1592 1593 if ((error = device_detach(child)) != 0) 1594 return (error); 1595 if (child->devclass) 1596 devclass_delete_device(child->devclass, child); 1597 TAILQ_REMOVE(&dev->children, child, link); 1598 TAILQ_REMOVE(&bus_data_devices, child, devlink); 1599 kobj_delete((kobj_t) child, M_BUS); 1600 1601 bus_data_generation_update(); 1602 return (0); 1603} 1604 1605/** 1606 * @brief Find a device given a unit number 1607 * 1608 * This is similar to devclass_get_devices() but only searches for 1609 * devices which have @p dev as a parent. 1610 * 1611 * @param dev the parent device to search 1612 * @param unit the unit number to search for. If the unit is -1, 1613 * return the first child of @p dev which has name 1614 * @p classname (that is, the one with the lowest unit.) 1615 * 1616 * @returns the device with the given unit number or @c 1617 * NULL if there is no such device 1618 */ 1619device_t 1620device_find_child(device_t dev, const char *classname, int unit) 1621{ 1622 devclass_t dc; 1623 device_t child; 1624 1625 dc = devclass_find(classname); 1626 if (!dc) 1627 return (NULL); 1628 1629 if (unit != -1) { 1630 child = devclass_get_device(dc, unit); 1631 if (child && child->parent == dev) 1632 return (child); 1633 } else { 1634 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) { 1635 child = devclass_get_device(dc, unit); 1636 if (child && child->parent == dev) 1637 return (child); 1638 } 1639 } 1640 return (NULL); 1641} 1642 1643/** 1644 * @internal 1645 */ 1646static driverlink_t 1647first_matching_driver(devclass_t dc, device_t dev) 1648{ 1649 if (dev->devclass) 1650 return (devclass_find_driver_internal(dc, dev->devclass->name)); 1651 return (TAILQ_FIRST(&dc->drivers)); 1652} 1653 1654/** 1655 * @internal 1656 */ 1657static driverlink_t 1658next_matching_driver(devclass_t dc, device_t dev, driverlink_t last) 1659{ 1660 if (dev->devclass) { 1661 driverlink_t dl; 1662 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link)) 1663 if (!strcmp(dev->devclass->name, dl->driver->name)) 1664 return (dl); 1665 return (NULL); 1666 } 1667 return (TAILQ_NEXT(last, link)); 1668} 1669 1670/** 1671 * @internal 1672 */ 1673static int 1674device_probe_child(device_t dev, device_t child) 1675{ 1676 devclass_t dc; 1677 driverlink_t best = 0; 1678 driverlink_t dl; 1679 int result, pri = 0; 1680 int hasclass = (child->devclass != 0); 1681 1682 GIANT_REQUIRED; 1683 1684 dc = dev->devclass; 1685 if (!dc) 1686 panic("device_probe_child: parent device has no devclass"); 1687 1688 /* 1689 * If the state is already probed, then return. However, don't 1690 * return if we can rebid this object. 1691 */ 1692 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0) 1693 return (0); 1694 1695 for (; dc; dc = dc->parent) { 1696 for (dl = first_matching_driver(dc, child); 1697 dl; 1698 dl = next_matching_driver(dc, child, dl)) { 1699 PDEBUG(("Trying %s", DRIVERNAME(dl->driver))); 1700 device_set_driver(child, dl->driver); 1701 if (!hasclass) 1702 device_set_devclass(child, dl->driver->name); 1703 1704 /* Fetch any flags for the device before probing. */ 1705 resource_int_value(dl->driver->name, child->unit, 1706 "flags", &child->devflags); 1707 1708 result = DEVICE_PROBE(child); 1709 1710 /* Reset flags and devclass before the next probe. */ 1711 child->devflags = 0; 1712 if (!hasclass) 1713 device_set_devclass(child, 0); 1714 1715 /* 1716 * If the driver returns SUCCESS, there can be 1717 * no higher match for this device. 1718 */ 1719 if (result == 0) { 1720 best = dl; 1721 pri = 0; 1722 break; 1723 } 1724 1725 /* 1726 * The driver returned an error so it 1727 * certainly doesn't match. 1728 */ 1729 if (result > 0) { 1730 device_set_driver(child, 0); 1731 continue; 1732 } 1733 1734 /* 1735 * A priority lower than SUCCESS, remember the 1736 * best matching driver. Initialise the value 1737 * of pri for the first match. 1738 */ 1739 if (best == 0 || result > pri) { 1740 best = dl; 1741 pri = result; 1742 continue; 1743 } 1744 } 1745 /* 1746 * If we have an unambiguous match in this devclass, 1747 * don't look in the parent. 1748 */ 1749 if (best && pri == 0) 1750 break; 1751 } 1752 1753 /* 1754 * If we found a driver, change state and initialise the devclass. 1755 */ 1756 /* XXX What happens if we rebid and got no best? */ 1757 if (best) { 1758 /* 1759 * If this device was atached, and we were asked to 1760 * rescan, and it is a different driver, then we have 1761 * to detach the old driver and reattach this new one. 1762 * Note, we don't have to check for DF_REBID here 1763 * because if the state is > DS_ALIVE, we know it must 1764 * be. 1765 * 1766 * This assumes that all DF_REBID drivers can have 1767 * their probe routine called at any time and that 1768 * they are idempotent as well as completely benign in 1769 * normal operations. 1770 * 1771 * We also have to make sure that the detach 1772 * succeeded, otherwise we fail the operation (or 1773 * maybe it should just fail silently? I'm torn). 1774 */ 1775 if (child->state > DS_ALIVE && best->driver != child->driver) 1776 if ((result = device_detach(dev)) != 0) 1777 return (result); 1778 1779 /* Set the winning driver, devclass, and flags. */ 1780 if (!child->devclass) 1781 device_set_devclass(child, best->driver->name); 1782 device_set_driver(child, best->driver); 1783 resource_int_value(best->driver->name, child->unit, 1784 "flags", &child->devflags); 1785 1786 if (pri < 0) { 1787 /* 1788 * A bit bogus. Call the probe method again to make 1789 * sure that we have the right description. 1790 */ 1791 DEVICE_PROBE(child); 1792#if 0 1793 child->flags |= DF_REBID; 1794#endif 1795 } else 1796 child->flags &= ~DF_REBID; 1797 child->state = DS_ALIVE; 1798 1799 bus_data_generation_update(); 1800 return (0); 1801 } 1802 1803 return (ENXIO); 1804} 1805 1806/** 1807 * @brief Return the parent of a device 1808 */ 1809device_t 1810device_get_parent(device_t dev) 1811{ 1812 return (dev->parent); 1813} 1814 1815/** 1816 * @brief Get a list of children of a device 1817 * 1818 * An array containing a list of all the children of the given device 1819 * is allocated and returned in @p *devlistp. The number of devices 1820 * in the array is returned in @p *devcountp. The caller should free 1821 * the array using @c free(p, M_TEMP). 1822 * 1823 * @param dev the device to examine 1824 * @param devlistp points at location for array pointer return 1825 * value 1826 * @param devcountp points at location for array size return value 1827 * 1828 * @retval 0 success 1829 * @retval ENOMEM the array allocation failed 1830 */ 1831int 1832device_get_children(device_t dev, device_t **devlistp, int *devcountp) 1833{ 1834 int count; 1835 device_t child; 1836 device_t *list; 1837 1838 count = 0; 1839 TAILQ_FOREACH(child, &dev->children, link) { 1840 count++; 1841 } 1842 1843 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO); 1844 if (!list) 1845 return (ENOMEM); 1846 1847 count = 0; 1848 TAILQ_FOREACH(child, &dev->children, link) { 1849 list[count] = child; 1850 count++; 1851 } 1852 1853 *devlistp = list; 1854 *devcountp = count; 1855 1856 return (0); 1857} 1858 1859/** 1860 * @brief Return the current driver for the device or @c NULL if there 1861 * is no driver currently attached 1862 */ 1863driver_t * 1864device_get_driver(device_t dev) 1865{ 1866 return (dev->driver); 1867} 1868 1869/** 1870 * @brief Return the current devclass for the device or @c NULL if 1871 * there is none. 1872 */ 1873devclass_t 1874device_get_devclass(device_t dev) 1875{ 1876 return (dev->devclass); 1877} 1878 1879/** 1880 * @brief Return the name of the device's devclass or @c NULL if there 1881 * is none. 1882 */ 1883const char * 1884device_get_name(device_t dev) 1885{ 1886 if (dev != NULL && dev->devclass) 1887 return (devclass_get_name(dev->devclass)); 1888 return (NULL); 1889} 1890 1891/** 1892 * @brief Return a string containing the device's devclass name 1893 * followed by an ascii representation of the device's unit number 1894 * (e.g. @c "foo2"). 1895 */ 1896const char * 1897device_get_nameunit(device_t dev) 1898{ 1899 return (dev->nameunit); 1900} 1901 1902/** 1903 * @brief Return the device's unit number. 1904 */ 1905int 1906device_get_unit(device_t dev) 1907{ 1908 return (dev->unit); 1909} 1910 1911/** 1912 * @brief Return the device's description string 1913 */ 1914const char * 1915device_get_desc(device_t dev) 1916{ 1917 return (dev->desc); 1918} 1919 1920/** 1921 * @brief Return the device's flags 1922 */ 1923u_int32_t 1924device_get_flags(device_t dev) 1925{ 1926 return (dev->devflags); 1927} 1928 1929struct sysctl_ctx_list * 1930device_get_sysctl_ctx(device_t dev) 1931{ 1932 return (&dev->sysctl_ctx); 1933} 1934 1935struct sysctl_oid * 1936device_get_sysctl_tree(device_t dev) 1937{ 1938 return (dev->sysctl_tree); 1939} 1940 1941/** 1942 * @brief Print the name of the device followed by a colon and a space 1943 * 1944 * @returns the number of characters printed 1945 */ 1946int 1947device_print_prettyname(device_t dev) 1948{ 1949 const char *name = device_get_name(dev); 1950 1951 if (name == 0) 1952 return (printf("unknown: ")); 1953 return (printf("%s%d: ", name, device_get_unit(dev))); 1954} 1955 1956/** 1957 * @brief Print the name of the device followed by a colon, a space 1958 * and the result of calling vprintf() with the value of @p fmt and 1959 * the following arguments. 1960 * 1961 * @returns the number of characters printed 1962 */ 1963int 1964device_printf(device_t dev, const char * fmt, ...) 1965{ 1966 va_list ap; 1967 int retval; 1968 1969 retval = device_print_prettyname(dev); 1970 va_start(ap, fmt); 1971 retval += vprintf(fmt, ap); 1972 va_end(ap); 1973 return (retval); 1974} 1975 1976/** 1977 * @internal 1978 */ 1979static void 1980device_set_desc_internal(device_t dev, const char* desc, int copy) 1981{ 1982 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) { 1983 free(dev->desc, M_BUS); 1984 dev->flags &= ~DF_DESCMALLOCED; 1985 dev->desc = NULL; 1986 } 1987 1988 if (copy && desc) { 1989 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT); 1990 if (dev->desc) { 1991 strcpy(dev->desc, desc); 1992 dev->flags |= DF_DESCMALLOCED; 1993 } 1994 } else { 1995 /* Avoid a -Wcast-qual warning */ 1996 dev->desc = (char *)(uintptr_t) desc; 1997 } 1998 1999 bus_data_generation_update(); 2000} 2001 2002/** 2003 * @brief Set the device's description 2004 * 2005 * The value of @c desc should be a string constant that will not 2006 * change (at least until the description is changed in a subsequent 2007 * call to device_set_desc() or device_set_desc_copy()). 2008 */ 2009void 2010device_set_desc(device_t dev, const char* desc) 2011{ 2012 device_set_desc_internal(dev, desc, FALSE); 2013} 2014 2015/** 2016 * @brief Set the device's description 2017 * 2018 * The string pointed to by @c desc is copied. Use this function if 2019 * the device description is generated, (e.g. with sprintf()). 2020 */ 2021void 2022device_set_desc_copy(device_t dev, const char* desc) 2023{ 2024 device_set_desc_internal(dev, desc, TRUE); 2025} 2026 2027/** 2028 * @brief Set the device's flags 2029 */ 2030void 2031device_set_flags(device_t dev, u_int32_t flags) 2032{ 2033 dev->devflags = flags; 2034} 2035 2036/** 2037 * @brief Return the device's softc field 2038 * 2039 * The softc is allocated and zeroed when a driver is attached, based 2040 * on the size field of the driver. 2041 */ 2042void * 2043device_get_softc(device_t dev) 2044{ 2045 return (dev->softc); 2046} 2047 2048/** 2049 * @brief Set the device's softc field 2050 * 2051 * Most drivers do not need to use this since the softc is allocated 2052 * automatically when the driver is attached. 2053 */ 2054void 2055device_set_softc(device_t dev, void *softc) 2056{ 2057 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) 2058 free(dev->softc, M_BUS_SC); 2059 dev->softc = softc; 2060 if (dev->softc) 2061 dev->flags |= DF_EXTERNALSOFTC; 2062 else 2063 dev->flags &= ~DF_EXTERNALSOFTC; 2064} 2065 2066/** 2067 * @brief Get the device's ivars field 2068 * 2069 * The ivars field is used by the parent device to store per-device 2070 * state (e.g. the physical location of the device or a list of 2071 * resources). 2072 */ 2073void * 2074device_get_ivars(device_t dev) 2075{ 2076 2077 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)")); 2078 return (dev->ivars); 2079} 2080 2081/** 2082 * @brief Set the device's ivars field 2083 */ 2084void 2085device_set_ivars(device_t dev, void * ivars) 2086{ 2087 2088 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)")); 2089 dev->ivars = ivars; 2090} 2091 2092/** 2093 * @brief Return the device's state 2094 */ 2095device_state_t 2096device_get_state(device_t dev) 2097{ 2098 return (dev->state); 2099} 2100 2101/** 2102 * @brief Set the DF_ENABLED flag for the device 2103 */ 2104void 2105device_enable(device_t dev) 2106{ 2107 dev->flags |= DF_ENABLED; 2108} 2109 2110/** 2111 * @brief Clear the DF_ENABLED flag for the device 2112 */ 2113void 2114device_disable(device_t dev) 2115{ 2116 dev->flags &= ~DF_ENABLED; 2117} 2118 2119/** 2120 * @brief Increment the busy counter for the device 2121 */ 2122void 2123device_busy(device_t dev) 2124{ 2125 if (dev->state < DS_ATTACHED) 2126 panic("device_busy: called for unattached device"); 2127 if (dev->busy == 0 && dev->parent) 2128 device_busy(dev->parent); 2129 dev->busy++; 2130 dev->state = DS_BUSY; 2131} 2132 2133/** 2134 * @brief Decrement the busy counter for the device 2135 */ 2136void 2137device_unbusy(device_t dev) 2138{ 2139 if (dev->state != DS_BUSY) 2140 panic("device_unbusy: called for non-busy device %s", 2141 device_get_nameunit(dev)); 2142 dev->busy--; 2143 if (dev->busy == 0) { 2144 if (dev->parent) 2145 device_unbusy(dev->parent); 2146 dev->state = DS_ATTACHED; 2147 } 2148} 2149 2150/** 2151 * @brief Set the DF_QUIET flag for the device 2152 */ 2153void 2154device_quiet(device_t dev) 2155{ 2156 dev->flags |= DF_QUIET; 2157} 2158 2159/** 2160 * @brief Clear the DF_QUIET flag for the device 2161 */ 2162void 2163device_verbose(device_t dev) 2164{ 2165 dev->flags &= ~DF_QUIET; 2166} 2167 2168/** 2169 * @brief Return non-zero if the DF_QUIET flag is set on the device 2170 */ 2171int 2172device_is_quiet(device_t dev) 2173{ 2174 return ((dev->flags & DF_QUIET) != 0); 2175} 2176 2177/** 2178 * @brief Return non-zero if the DF_ENABLED flag is set on the device 2179 */ 2180int 2181device_is_enabled(device_t dev) 2182{ 2183 return ((dev->flags & DF_ENABLED) != 0); 2184} 2185 2186/** 2187 * @brief Return non-zero if the device was successfully probed 2188 */ 2189int 2190device_is_alive(device_t dev) 2191{ 2192 return (dev->state >= DS_ALIVE); 2193} 2194 2195/** 2196 * @brief Return non-zero if the device currently has a driver 2197 * attached to it 2198 */ 2199int 2200device_is_attached(device_t dev) 2201{ 2202 return (dev->state >= DS_ATTACHED); 2203} 2204 2205/** 2206 * @brief Set the devclass of a device 2207 * @see devclass_add_device(). 2208 */ 2209int 2210device_set_devclass(device_t dev, const char *classname) 2211{ 2212 devclass_t dc; 2213 int error; 2214 2215 if (!classname) { 2216 if (dev->devclass) 2217 devclass_delete_device(dev->devclass, dev); 2218 return (0); 2219 } 2220 2221 if (dev->devclass) { 2222 printf("device_set_devclass: device class already set\n"); 2223 return (EINVAL); 2224 } 2225 2226 dc = devclass_find_internal(classname, 0, TRUE); 2227 if (!dc) 2228 return (ENOMEM); 2229 2230 error = devclass_add_device(dc, dev); 2231 2232 bus_data_generation_update(); 2233 return (error); 2234} 2235 2236/** 2237 * @brief Set the driver of a device 2238 * 2239 * @retval 0 success 2240 * @retval EBUSY the device already has a driver attached 2241 * @retval ENOMEM a memory allocation failure occurred 2242 */ 2243int 2244device_set_driver(device_t dev, driver_t *driver) 2245{ 2246 if (dev->state >= DS_ATTACHED) 2247 return (EBUSY); 2248 2249 if (dev->driver == driver) 2250 return (0); 2251 2252 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) { 2253 free(dev->softc, M_BUS_SC); 2254 dev->softc = NULL; 2255 } 2256 kobj_delete((kobj_t) dev, 0); 2257 dev->driver = driver; 2258 if (driver) { 2259 kobj_init((kobj_t) dev, (kobj_class_t) driver); 2260 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) { 2261 dev->softc = malloc(driver->size, M_BUS_SC, 2262 M_NOWAIT | M_ZERO); 2263 if (!dev->softc) { 2264 kobj_delete((kobj_t) dev, 0); 2265 kobj_init((kobj_t) dev, &null_class); 2266 dev->driver = NULL; 2267 return (ENOMEM); 2268 } 2269 } 2270 } else { 2271 kobj_init((kobj_t) dev, &null_class); 2272 } 2273 2274 bus_data_generation_update(); 2275 return (0); 2276} 2277 2278/** 2279 * @brief Probe a device and attach a driver if possible 2280 * 2281 * This function is the core of the device autoconfiguration 2282 * system. Its purpose is to select a suitable driver for a device and 2283 * then call that driver to initialise the hardware appropriately. The 2284 * driver is selected by calling the DEVICE_PROBE() method of a set of 2285 * candidate drivers and then choosing the driver which returned the 2286 * best value. This driver is then attached to the device using 2287 * device_attach(). 2288 * 2289 * The set of suitable drivers is taken from the list of drivers in 2290 * the parent device's devclass. If the device was originally created 2291 * with a specific class name (see device_add_child()), only drivers 2292 * with that name are probed, otherwise all drivers in the devclass 2293 * are probed. If no drivers return successful probe values in the 2294 * parent devclass, the search continues in the parent of that 2295 * devclass (see devclass_get_parent()) if any. 2296 * 2297 * @param dev the device to initialise 2298 * 2299 * @retval 0 success 2300 * @retval ENXIO no driver was found 2301 * @retval ENOMEM memory allocation failure 2302 * @retval non-zero some other unix error code 2303 */ 2304int 2305device_probe_and_attach(device_t dev) 2306{ 2307 int error; 2308 2309 GIANT_REQUIRED; 2310 2311 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0) 2312 return (0); 2313 2314 if (!(dev->flags & DF_ENABLED)) { 2315 if (bootverbose && device_get_name(dev) != NULL) { 2316 device_print_prettyname(dev); 2317 printf("not probed (disabled)\n"); 2318 } 2319 return (0); 2320 } 2321 if ((error = device_probe_child(dev->parent, dev)) != 0) { 2322 if (!(dev->flags & DF_DONENOMATCH)) { 2323 BUS_PROBE_NOMATCH(dev->parent, dev); 2324 devnomatch(dev); 2325 dev->flags |= DF_DONENOMATCH; 2326 } 2327 return (error); 2328 } 2329 error = device_attach(dev); 2330 2331 return (error); 2332} 2333 2334/** 2335 * @brief Attach a device driver to a device 2336 * 2337 * This function is a wrapper around the DEVICE_ATTACH() driver 2338 * method. In addition to calling DEVICE_ATTACH(), it initialises the 2339 * device's sysctl tree, optionally prints a description of the device 2340 * and queues a notification event for user-based device management 2341 * services. 2342 * 2343 * Normally this function is only called internally from 2344 * device_probe_and_attach(). 2345 * 2346 * @param dev the device to initialise 2347 * 2348 * @retval 0 success 2349 * @retval ENXIO no driver was found 2350 * @retval ENOMEM memory allocation failure 2351 * @retval non-zero some other unix error code 2352 */ 2353int 2354device_attach(device_t dev) 2355{ 2356 int error; 2357 2358 device_sysctl_init(dev); 2359 if (!device_is_quiet(dev)) 2360 device_print_child(dev->parent, dev); 2361 if ((error = DEVICE_ATTACH(dev)) != 0) { 2362 printf("device_attach: %s%d attach returned %d\n", 2363 dev->driver->name, dev->unit, error); 2364 /* Unset the class; set in device_probe_child */ 2365 if (dev->devclass == 0) 2366 device_set_devclass(dev, 0); 2367 device_set_driver(dev, NULL); 2368 device_sysctl_fini(dev); 2369 dev->state = DS_NOTPRESENT; 2370 return (error); 2371 } 2372 dev->state = DS_ATTACHED; 2373 devadded(dev); 2374 return (0); 2375} 2376 2377/** 2378 * @brief Detach a driver from a device 2379 * 2380 * This function is a wrapper around the DEVICE_DETACH() driver 2381 * method. If the call to DEVICE_DETACH() succeeds, it calls 2382 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a 2383 * notification event for user-based device management services and 2384 * cleans up the device's sysctl tree. 2385 * 2386 * @param dev the device to un-initialise 2387 * 2388 * @retval 0 success 2389 * @retval ENXIO no driver was found 2390 * @retval ENOMEM memory allocation failure 2391 * @retval non-zero some other unix error code 2392 */ 2393int 2394device_detach(device_t dev) 2395{ 2396 int error; 2397 2398 GIANT_REQUIRED; 2399 2400 PDEBUG(("%s", DEVICENAME(dev))); 2401 if (dev->state == DS_BUSY) 2402 return (EBUSY); 2403 if (dev->state != DS_ATTACHED) 2404 return (0); 2405 2406 if ((error = DEVICE_DETACH(dev)) != 0) 2407 return (error); 2408 devremoved(dev); 2409 device_printf(dev, "detached\n"); 2410 if (dev->parent) 2411 BUS_CHILD_DETACHED(dev->parent, dev); 2412 2413 if (!(dev->flags & DF_FIXEDCLASS)) 2414 devclass_delete_device(dev->devclass, dev); 2415 2416 dev->state = DS_NOTPRESENT; 2417 device_set_driver(dev, NULL); 2418 device_set_desc(dev, NULL); 2419 device_sysctl_fini(dev); 2420 2421 return (0); 2422} 2423 2424/** 2425 * @brief Tells a driver to quiesce itself. 2426 * 2427 * This function is a wrapper around the DEVICE_QUIESCE() driver 2428 * method. If the call to DEVICE_QUIESCE() succeeds. 2429 * 2430 * @param dev the device to quiesce 2431 * 2432 * @retval 0 success 2433 * @retval ENXIO no driver was found 2434 * @retval ENOMEM memory allocation failure 2435 * @retval non-zero some other unix error code 2436 */ 2437int 2438device_quiesce(device_t dev) 2439{ 2440 2441 PDEBUG(("%s", DEVICENAME(dev))); 2442 if (dev->state == DS_BUSY) 2443 return (EBUSY); 2444 if (dev->state != DS_ATTACHED) 2445 return (0); 2446 2447 return (DEVICE_QUIESCE(dev)); 2448} 2449 2450/** 2451 * @brief Notify a device of system shutdown 2452 * 2453 * This function calls the DEVICE_SHUTDOWN() driver method if the 2454 * device currently has an attached driver. 2455 * 2456 * @returns the value returned by DEVICE_SHUTDOWN() 2457 */ 2458int 2459device_shutdown(device_t dev) 2460{ 2461 if (dev->state < DS_ATTACHED) 2462 return (0); 2463 return (DEVICE_SHUTDOWN(dev)); 2464} 2465 2466/** 2467 * @brief Set the unit number of a device 2468 * 2469 * This function can be used to override the unit number used for a 2470 * device (e.g. to wire a device to a pre-configured unit number). 2471 */ 2472int 2473device_set_unit(device_t dev, int unit) 2474{ 2475 devclass_t dc; 2476 int err; 2477 2478 dc = device_get_devclass(dev); 2479 if (unit < dc->maxunit && dc->devices[unit]) 2480 return (EBUSY); 2481 err = devclass_delete_device(dc, dev); 2482 if (err) 2483 return (err); 2484 dev->unit = unit; 2485 err = devclass_add_device(dc, dev); 2486 if (err) 2487 return (err); 2488 2489 bus_data_generation_update(); 2490 return (0); 2491} 2492 2493/*======================================*/ 2494/* 2495 * Some useful method implementations to make life easier for bus drivers. 2496 */ 2497 2498/** 2499 * @brief Initialise a resource list. 2500 * 2501 * @param rl the resource list to initialise 2502 */ 2503void 2504resource_list_init(struct resource_list *rl) 2505{ 2506 STAILQ_INIT(rl); 2507} 2508 2509/** 2510 * @brief Reclaim memory used by a resource list. 2511 * 2512 * This function frees the memory for all resource entries on the list 2513 * (if any). 2514 * 2515 * @param rl the resource list to free 2516 */ 2517void 2518resource_list_free(struct resource_list *rl) 2519{ 2520 struct resource_list_entry *rle; 2521 2522 while ((rle = STAILQ_FIRST(rl)) != NULL) { 2523 if (rle->res) 2524 panic("resource_list_free: resource entry is busy"); 2525 STAILQ_REMOVE_HEAD(rl, link); 2526 free(rle, M_BUS); 2527 } 2528} 2529 2530/** 2531 * @brief Add a resource entry. 2532 * 2533 * This function adds a resource entry using the given @p type, @p 2534 * start, @p end and @p count values. A rid value is chosen by 2535 * searching sequentially for the first unused rid starting at zero. 2536 * 2537 * @param rl the resource list to edit 2538 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2539 * @param start the start address of the resource 2540 * @param end the end address of the resource 2541 * @param count XXX end-start+1 2542 */ 2543int 2544resource_list_add_next(struct resource_list *rl, int type, u_long start, 2545 u_long end, u_long count) 2546{ 2547 int rid; 2548 2549 rid = 0; 2550 while (resource_list_find(rl, type, rid) != NULL) 2551 rid++; 2552 resource_list_add(rl, type, rid, start, end, count); 2553 return (rid); 2554} 2555 2556/** 2557 * @brief Add or modify a resource entry. 2558 * 2559 * If an existing entry exists with the same type and rid, it will be 2560 * modified using the given values of @p start, @p end and @p 2561 * count. If no entry exists, a new one will be created using the 2562 * given values. 2563 * 2564 * @param rl the resource list to edit 2565 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2566 * @param rid the resource identifier 2567 * @param start the start address of the resource 2568 * @param end the end address of the resource 2569 * @param count XXX end-start+1 2570 */ 2571void 2572resource_list_add(struct resource_list *rl, int type, int rid, 2573 u_long start, u_long end, u_long count) 2574{ 2575 struct resource_list_entry *rle; 2576 2577 rle = resource_list_find(rl, type, rid); 2578 if (!rle) { 2579 rle = malloc(sizeof(struct resource_list_entry), M_BUS, 2580 M_NOWAIT); 2581 if (!rle) 2582 panic("resource_list_add: can't record entry"); 2583 STAILQ_INSERT_TAIL(rl, rle, link); 2584 rle->type = type; 2585 rle->rid = rid; 2586 rle->res = NULL; 2587 } 2588 2589 if (rle->res) 2590 panic("resource_list_add: resource entry is busy"); 2591 2592 rle->start = start; 2593 rle->end = end; 2594 rle->count = count; 2595} 2596 2597/** 2598 * @brief Find a resource entry by type and rid. 2599 * 2600 * @param rl the resource list to search 2601 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2602 * @param rid the resource identifier 2603 * 2604 * @returns the resource entry pointer or NULL if there is no such 2605 * entry. 2606 */ 2607struct resource_list_entry * 2608resource_list_find(struct resource_list *rl, int type, int rid) 2609{ 2610 struct resource_list_entry *rle; 2611 2612 STAILQ_FOREACH(rle, rl, link) { 2613 if (rle->type == type && rle->rid == rid) 2614 return (rle); 2615 } 2616 return (NULL); 2617} 2618 2619/** 2620 * @brief Delete a resource entry. 2621 * 2622 * @param rl the resource list to edit 2623 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2624 * @param rid the resource identifier 2625 */ 2626void 2627resource_list_delete(struct resource_list *rl, int type, int rid) 2628{ 2629 struct resource_list_entry *rle = resource_list_find(rl, type, rid); 2630 2631 if (rle) { 2632 if (rle->res != NULL) 2633 panic("resource_list_delete: resource has not been released"); 2634 STAILQ_REMOVE(rl, rle, resource_list_entry, link); 2635 free(rle, M_BUS); 2636 } 2637} 2638 2639/** 2640 * @brief Helper function for implementing BUS_ALLOC_RESOURCE() 2641 * 2642 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list 2643 * and passing the allocation up to the parent of @p bus. This assumes 2644 * that the first entry of @c device_get_ivars(child) is a struct 2645 * resource_list. This also handles 'passthrough' allocations where a 2646 * child is a remote descendant of bus by passing the allocation up to 2647 * the parent of bus. 2648 * 2649 * Typically, a bus driver would store a list of child resources 2650 * somewhere in the child device's ivars (see device_get_ivars()) and 2651 * its implementation of BUS_ALLOC_RESOURCE() would find that list and 2652 * then call resource_list_alloc() to perform the allocation. 2653 * 2654 * @param rl the resource list to allocate from 2655 * @param bus the parent device of @p child 2656 * @param child the device which is requesting an allocation 2657 * @param type the type of resource to allocate 2658 * @param rid a pointer to the resource identifier 2659 * @param start hint at the start of the resource range - pass 2660 * @c 0UL for any start address 2661 * @param end hint at the end of the resource range - pass 2662 * @c ~0UL for any end address 2663 * @param count hint at the size of range required - pass @c 1 2664 * for any size 2665 * @param flags any extra flags to control the resource 2666 * allocation - see @c RF_XXX flags in 2667 * <sys/rman.h> for details 2668 * 2669 * @returns the resource which was allocated or @c NULL if no 2670 * resource could be allocated 2671 */ 2672struct resource * 2673resource_list_alloc(struct resource_list *rl, device_t bus, device_t child, 2674 int type, int *rid, u_long start, u_long end, u_long count, u_int flags) 2675{ 2676 struct resource_list_entry *rle = 0; 2677 int passthrough = (device_get_parent(child) != bus); 2678 int isdefault = (start == 0UL && end == ~0UL); 2679 2680 if (passthrough) { 2681 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 2682 type, rid, start, end, count, flags)); 2683 } 2684 2685 rle = resource_list_find(rl, type, *rid); 2686 2687 if (!rle) 2688 return (NULL); /* no resource of that type/rid */ 2689 2690 if (rle->res) 2691 panic("resource_list_alloc: resource entry is busy"); 2692 2693 if (isdefault) { 2694 start = rle->start; 2695 count = ulmax(count, rle->count); 2696 end = ulmax(rle->end, start + count - 1); 2697 } 2698 2699 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 2700 type, rid, start, end, count, flags); 2701 2702 /* 2703 * Record the new range. 2704 */ 2705 if (rle->res) { 2706 rle->start = rman_get_start(rle->res); 2707 rle->end = rman_get_end(rle->res); 2708 rle->count = count; 2709 } 2710 2711 return (rle->res); 2712} 2713 2714/** 2715 * @brief Helper function for implementing BUS_RELEASE_RESOURCE() 2716 * 2717 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally 2718 * used with resource_list_alloc(). 2719 * 2720 * @param rl the resource list which was allocated from 2721 * @param bus the parent device of @p child 2722 * @param child the device which is requesting a release 2723 * @param type the type of resource to allocate 2724 * @param rid the resource identifier 2725 * @param res the resource to release 2726 * 2727 * @retval 0 success 2728 * @retval non-zero a standard unix error code indicating what 2729 * error condition prevented the operation 2730 */ 2731int 2732resource_list_release(struct resource_list *rl, device_t bus, device_t child, 2733 int type, int rid, struct resource *res) 2734{ 2735 struct resource_list_entry *rle = 0; 2736 int passthrough = (device_get_parent(child) != bus); 2737 int error; 2738 2739 if (passthrough) { 2740 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 2741 type, rid, res)); 2742 } 2743 2744 rle = resource_list_find(rl, type, rid); 2745 2746 if (!rle) 2747 panic("resource_list_release: can't find resource"); 2748 if (!rle->res) 2749 panic("resource_list_release: resource entry is not busy"); 2750 2751 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 2752 type, rid, res); 2753 if (error) 2754 return (error); 2755 2756 rle->res = NULL; 2757 return (0); 2758} 2759 2760/** 2761 * @brief Print a description of resources in a resource list 2762 * 2763 * Print all resources of a specified type, for use in BUS_PRINT_CHILD(). 2764 * The name is printed if at least one resource of the given type is available. 2765 * The format is used to print resource start and end. 2766 * 2767 * @param rl the resource list to print 2768 * @param name the name of @p type, e.g. @c "memory" 2769 * @param type type type of resource entry to print 2770 * @param format printf(9) format string to print resource 2771 * start and end values 2772 * 2773 * @returns the number of characters printed 2774 */ 2775int 2776resource_list_print_type(struct resource_list *rl, const char *name, int type, 2777 const char *format) 2778{ 2779 struct resource_list_entry *rle; 2780 int printed, retval; 2781 2782 printed = 0; 2783 retval = 0; 2784 /* Yes, this is kinda cheating */ 2785 STAILQ_FOREACH(rle, rl, link) { 2786 if (rle->type == type) { 2787 if (printed == 0) 2788 retval += printf(" %s ", name); 2789 else 2790 retval += printf(","); 2791 printed++; 2792 retval += printf(format, rle->start); 2793 if (rle->count > 1) { 2794 retval += printf("-"); 2795 retval += printf(format, rle->start + 2796 rle->count - 1); 2797 } 2798 } 2799 } 2800 return (retval); 2801} 2802 2803/** 2804 * @brief Helper function for implementing DEVICE_PROBE() 2805 * 2806 * This function can be used to help implement the DEVICE_PROBE() for 2807 * a bus (i.e. a device which has other devices attached to it). It 2808 * calls the DEVICE_IDENTIFY() method of each driver in the device's 2809 * devclass. 2810 */ 2811int 2812bus_generic_probe(device_t dev) 2813{ 2814 devclass_t dc = dev->devclass; 2815 driverlink_t dl; 2816 2817 TAILQ_FOREACH(dl, &dc->drivers, link) { 2818 DEVICE_IDENTIFY(dl->driver, dev); 2819 } 2820 2821 return (0); 2822} 2823 2824/** 2825 * @brief Helper function for implementing DEVICE_ATTACH() 2826 * 2827 * This function can be used to help implement the DEVICE_ATTACH() for 2828 * a bus. It calls device_probe_and_attach() for each of the device's 2829 * children. 2830 */ 2831int 2832bus_generic_attach(device_t dev) 2833{ 2834 device_t child; 2835 2836 TAILQ_FOREACH(child, &dev->children, link) { 2837 device_probe_and_attach(child); 2838 } 2839 2840 return (0); 2841} 2842 2843/** 2844 * @brief Helper function for implementing DEVICE_DETACH() 2845 * 2846 * This function can be used to help implement the DEVICE_DETACH() for 2847 * a bus. It calls device_detach() for each of the device's 2848 * children. 2849 */ 2850int 2851bus_generic_detach(device_t dev) 2852{ 2853 device_t child; 2854 int error; 2855 2856 if (dev->state != DS_ATTACHED) 2857 return (EBUSY); 2858 2859 TAILQ_FOREACH(child, &dev->children, link) { 2860 if ((error = device_detach(child)) != 0) 2861 return (error); 2862 } 2863 2864 return (0); 2865} 2866 2867/** 2868 * @brief Helper function for implementing DEVICE_SHUTDOWN() 2869 * 2870 * This function can be used to help implement the DEVICE_SHUTDOWN() 2871 * for a bus. It calls device_shutdown() for each of the device's 2872 * children. 2873 */ 2874int 2875bus_generic_shutdown(device_t dev) 2876{ 2877 device_t child; 2878 2879 TAILQ_FOREACH(child, &dev->children, link) { 2880 device_shutdown(child); 2881 } 2882 2883 return (0); 2884} 2885 2886/** 2887 * @brief Helper function for implementing DEVICE_SUSPEND() 2888 * 2889 * This function can be used to help implement the DEVICE_SUSPEND() 2890 * for a bus. It calls DEVICE_SUSPEND() for each of the device's 2891 * children. If any call to DEVICE_SUSPEND() fails, the suspend 2892 * operation is aborted and any devices which were suspended are 2893 * resumed immediately by calling their DEVICE_RESUME() methods. 2894 */ 2895int 2896bus_generic_suspend(device_t dev) 2897{ 2898 int error; 2899 device_t child, child2; 2900 2901 TAILQ_FOREACH(child, &dev->children, link) { 2902 error = DEVICE_SUSPEND(child); 2903 if (error) { 2904 for (child2 = TAILQ_FIRST(&dev->children); 2905 child2 && child2 != child; 2906 child2 = TAILQ_NEXT(child2, link)) 2907 DEVICE_RESUME(child2); 2908 return (error); 2909 } 2910 } 2911 return (0); 2912} 2913 2914/** 2915 * @brief Helper function for implementing DEVICE_RESUME() 2916 * 2917 * This function can be used to help implement the DEVICE_RESUME() for 2918 * a bus. It calls DEVICE_RESUME() on each of the device's children. 2919 */ 2920int 2921bus_generic_resume(device_t dev) 2922{ 2923 device_t child; 2924 2925 TAILQ_FOREACH(child, &dev->children, link) { 2926 DEVICE_RESUME(child); 2927 /* if resume fails, there's nothing we can usefully do... */ 2928 } 2929 return (0); 2930} 2931 2932/** 2933 * @brief Helper function for implementing BUS_PRINT_CHILD(). 2934 * 2935 * This function prints the first part of the ascii representation of 2936 * @p child, including its name, unit and description (if any - see 2937 * device_set_desc()). 2938 * 2939 * @returns the number of characters printed 2940 */ 2941int 2942bus_print_child_header(device_t dev, device_t child) 2943{ 2944 int retval = 0; 2945 2946 if (device_get_desc(child)) { 2947 retval += device_printf(child, "<%s>", device_get_desc(child)); 2948 } else { 2949 retval += printf("%s", device_get_nameunit(child)); 2950 } 2951 2952 return (retval); 2953} 2954 2955/** 2956 * @brief Helper function for implementing BUS_PRINT_CHILD(). 2957 * 2958 * This function prints the last part of the ascii representation of 2959 * @p child, which consists of the string @c " on " followed by the 2960 * name and unit of the @p dev. 2961 * 2962 * @returns the number of characters printed 2963 */ 2964int 2965bus_print_child_footer(device_t dev, device_t child) 2966{ 2967 return (printf(" on %s\n", device_get_nameunit(dev))); 2968} 2969 2970/** 2971 * @brief Helper function for implementing BUS_PRINT_CHILD(). 2972 * 2973 * This function simply calls bus_print_child_header() followed by 2974 * bus_print_child_footer(). 2975 * 2976 * @returns the number of characters printed 2977 */ 2978int 2979bus_generic_print_child(device_t dev, device_t child) 2980{ 2981 int retval = 0; 2982 2983 retval += bus_print_child_header(dev, child); 2984 retval += bus_print_child_footer(dev, child); 2985 2986 return (retval); 2987} 2988 2989/** 2990 * @brief Stub function for implementing BUS_READ_IVAR(). 2991 * 2992 * @returns ENOENT 2993 */ 2994int 2995bus_generic_read_ivar(device_t dev, device_t child, int index, 2996 uintptr_t * result) 2997{ 2998 return (ENOENT); 2999} 3000 3001/** 3002 * @brief Stub function for implementing BUS_WRITE_IVAR(). 3003 * 3004 * @returns ENOENT 3005 */ 3006int 3007bus_generic_write_ivar(device_t dev, device_t child, int index, 3008 uintptr_t value) 3009{ 3010 return (ENOENT); 3011} 3012 3013/** 3014 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST(). 3015 * 3016 * @returns NULL 3017 */ 3018struct resource_list * 3019bus_generic_get_resource_list(device_t dev, device_t child) 3020{ 3021 return (NULL); 3022} 3023 3024/** 3025 * @brief Helper function for implementing BUS_DRIVER_ADDED(). 3026 * 3027 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's 3028 * DEVICE_IDENTIFY() method to allow it to add new children to the bus 3029 * and then calls device_probe_and_attach() for each unattached child. 3030 */ 3031void 3032bus_generic_driver_added(device_t dev, driver_t *driver) 3033{ 3034 device_t child; 3035 3036 DEVICE_IDENTIFY(driver, dev); 3037 TAILQ_FOREACH(child, &dev->children, link) { 3038 if (child->state == DS_NOTPRESENT || 3039 (child->flags & DF_REBID)) 3040 device_probe_and_attach(child); 3041 } 3042} 3043 3044/** 3045 * @brief Helper function for implementing BUS_SETUP_INTR(). 3046 * 3047 * This simple implementation of BUS_SETUP_INTR() simply calls the 3048 * BUS_SETUP_INTR() method of the parent of @p dev. 3049 */ 3050int 3051bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq, 3052 int flags, driver_intr_t *intr, void *arg, void **cookiep) 3053{ 3054 /* Propagate up the bus hierarchy until someone handles it. */ 3055 if (dev->parent) 3056 return (BUS_SETUP_INTR(dev->parent, child, irq, flags, 3057 intr, arg, cookiep)); 3058 return (EINVAL); 3059} 3060 3061/** 3062 * @brief Helper function for implementing BUS_TEARDOWN_INTR(). 3063 * 3064 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the 3065 * BUS_TEARDOWN_INTR() method of the parent of @p dev. 3066 */ 3067int 3068bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq, 3069 void *cookie) 3070{ 3071 /* Propagate up the bus hierarchy until someone handles it. */ 3072 if (dev->parent) 3073 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie)); 3074 return (EINVAL); 3075} 3076 3077/** 3078 * @brief Helper function for implementing BUS_ALLOC_RESOURCE(). 3079 * 3080 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the 3081 * BUS_ALLOC_RESOURCE() method of the parent of @p dev. 3082 */ 3083struct resource * 3084bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid, 3085 u_long start, u_long end, u_long count, u_int flags) 3086{ 3087 /* Propagate up the bus hierarchy until someone handles it. */ 3088 if (dev->parent) 3089 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid, 3090 start, end, count, flags)); 3091 return (NULL); 3092} 3093 3094/** 3095 * @brief Helper function for implementing BUS_RELEASE_RESOURCE(). 3096 * 3097 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the 3098 * BUS_RELEASE_RESOURCE() method of the parent of @p dev. 3099 */ 3100int 3101bus_generic_release_resource(device_t dev, device_t child, int type, int rid, 3102 struct resource *r) 3103{ 3104 /* Propagate up the bus hierarchy until someone handles it. */ 3105 if (dev->parent) 3106 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, 3107 r)); 3108 return (EINVAL); 3109} 3110 3111/** 3112 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE(). 3113 * 3114 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the 3115 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev. 3116 */ 3117int 3118bus_generic_activate_resource(device_t dev, device_t child, int type, int rid, 3119 struct resource *r) 3120{ 3121 /* Propagate up the bus hierarchy until someone handles it. */ 3122 if (dev->parent) 3123 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, 3124 r)); 3125 return (EINVAL); 3126} 3127 3128/** 3129 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE(). 3130 * 3131 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the 3132 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev. 3133 */ 3134int 3135bus_generic_deactivate_resource(device_t dev, device_t child, int type, 3136 int rid, struct resource *r) 3137{ 3138 /* Propagate up the bus hierarchy until someone handles it. */ 3139 if (dev->parent) 3140 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid, 3141 r)); 3142 return (EINVAL); 3143} 3144 3145/** 3146 * @brief Helper function for implementing BUS_CONFIG_INTR(). 3147 * 3148 * This simple implementation of BUS_CONFIG_INTR() simply calls the 3149 * BUS_CONFIG_INTR() method of the parent of @p dev. 3150 */ 3151int 3152bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig, 3153 enum intr_polarity pol) 3154{ 3155 3156 /* Propagate up the bus hierarchy until someone handles it. */ 3157 if (dev->parent) 3158 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol)); 3159 return (EINVAL); 3160} 3161 3162/** 3163 * @brief Helper function for implementing BUS_GET_RESOURCE(). 3164 * 3165 * This implementation of BUS_GET_RESOURCE() uses the 3166 * resource_list_find() function to do most of the work. It calls 3167 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to 3168 * search. 3169 */ 3170int 3171bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid, 3172 u_long *startp, u_long *countp) 3173{ 3174 struct resource_list * rl = NULL; 3175 struct resource_list_entry * rle = NULL; 3176 3177 rl = BUS_GET_RESOURCE_LIST(dev, child); 3178 if (!rl) 3179 return (EINVAL); 3180 3181 rle = resource_list_find(rl, type, rid); 3182 if (!rle) 3183 return (ENOENT); 3184 3185 if (startp) 3186 *startp = rle->start; 3187 if (countp) 3188 *countp = rle->count; 3189 3190 return (0); 3191} 3192 3193/** 3194 * @brief Helper function for implementing BUS_SET_RESOURCE(). 3195 * 3196 * This implementation of BUS_SET_RESOURCE() uses the 3197 * resource_list_add() function to do most of the work. It calls 3198 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to 3199 * edit. 3200 */ 3201int 3202bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid, 3203 u_long start, u_long count) 3204{ 3205 struct resource_list * rl = NULL; 3206 3207 rl = BUS_GET_RESOURCE_LIST(dev, child); 3208 if (!rl) 3209 return (EINVAL); 3210 3211 resource_list_add(rl, type, rid, start, (start + count - 1), count); 3212 3213 return (0); 3214} 3215 3216/** 3217 * @brief Helper function for implementing BUS_DELETE_RESOURCE(). 3218 * 3219 * This implementation of BUS_DELETE_RESOURCE() uses the 3220 * resource_list_delete() function to do most of the work. It calls 3221 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to 3222 * edit. 3223 */ 3224void 3225bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid) 3226{ 3227 struct resource_list * rl = NULL; 3228 3229 rl = BUS_GET_RESOURCE_LIST(dev, child); 3230 if (!rl) 3231 return; 3232 3233 resource_list_delete(rl, type, rid); 3234 3235 return; 3236} 3237 3238/** 3239 * @brief Helper function for implementing BUS_RELEASE_RESOURCE(). 3240 * 3241 * This implementation of BUS_RELEASE_RESOURCE() uses the 3242 * resource_list_release() function to do most of the work. It calls 3243 * BUS_GET_RESOURCE_LIST() to find a suitable resource list. 3244 */ 3245int 3246bus_generic_rl_release_resource(device_t dev, device_t child, int type, 3247 int rid, struct resource *r) 3248{ 3249 struct resource_list * rl = NULL; 3250 3251 rl = BUS_GET_RESOURCE_LIST(dev, child); 3252 if (!rl) 3253 return (EINVAL); 3254 3255 return (resource_list_release(rl, dev, child, type, rid, r)); 3256} 3257 3258/** 3259 * @brief Helper function for implementing BUS_ALLOC_RESOURCE(). 3260 * 3261 * This implementation of BUS_ALLOC_RESOURCE() uses the 3262 * resource_list_alloc() function to do most of the work. It calls 3263 * BUS_GET_RESOURCE_LIST() to find a suitable resource list. 3264 */ 3265struct resource * 3266bus_generic_rl_alloc_resource(device_t dev, device_t child, int type, 3267 int *rid, u_long start, u_long end, u_long count, u_int flags) 3268{ 3269 struct resource_list * rl = NULL; 3270 3271 rl = BUS_GET_RESOURCE_LIST(dev, child); 3272 if (!rl) 3273 return (NULL); 3274 3275 return (resource_list_alloc(rl, dev, child, type, rid, 3276 start, end, count, flags)); 3277} 3278 3279/** 3280 * @brief Helper function for implementing BUS_CHILD_PRESENT(). 3281 * 3282 * This simple implementation of BUS_CHILD_PRESENT() simply calls the 3283 * BUS_CHILD_PRESENT() method of the parent of @p dev. 3284 */ 3285int 3286bus_generic_child_present(device_t dev, device_t child) 3287{ 3288 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev)); 3289} 3290 3291/* 3292 * Some convenience functions to make it easier for drivers to use the 3293 * resource-management functions. All these really do is hide the 3294 * indirection through the parent's method table, making for slightly 3295 * less-wordy code. In the future, it might make sense for this code 3296 * to maintain some sort of a list of resources allocated by each device. 3297 */ 3298 3299/** 3300 * @brief Wrapper function for BUS_ALLOC_RESOURCE(). 3301 * 3302 * This function simply calls the BUS_ALLOC_RESOURCE() method of the 3303 * parent of @p dev. 3304 */ 3305struct resource * 3306bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end, 3307 u_long count, u_int flags) 3308{ 3309 if (dev->parent == 0) 3310 return (0); 3311 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end, 3312 count, flags)); 3313} 3314 3315/** 3316 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE(). 3317 * 3318 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the 3319 * parent of @p dev. 3320 */ 3321int 3322bus_activate_resource(device_t dev, int type, int rid, struct resource *r) 3323{ 3324 if (dev->parent == 0) 3325 return (EINVAL); 3326 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 3327} 3328 3329/** 3330 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE(). 3331 * 3332 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the 3333 * parent of @p dev. 3334 */ 3335int 3336bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r) 3337{ 3338 if (dev->parent == 0) 3339 return (EINVAL); 3340 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 3341} 3342 3343/** 3344 * @brief Wrapper function for BUS_RELEASE_RESOURCE(). 3345 * 3346 * This function simply calls the BUS_RELEASE_RESOURCE() method of the 3347 * parent of @p dev. 3348 */ 3349int 3350bus_release_resource(device_t dev, int type, int rid, struct resource *r) 3351{ 3352 if (dev->parent == 0) 3353 return (EINVAL); 3354 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r)); 3355} 3356 3357/** 3358 * @brief Wrapper function for BUS_SETUP_INTR(). 3359 * 3360 * This function simply calls the BUS_SETUP_INTR() method of the 3361 * parent of @p dev. 3362 */ 3363int 3364bus_setup_intr(device_t dev, struct resource *r, int flags, 3365 driver_intr_t handler, void *arg, void **cookiep) 3366{ 3367 int error; 3368 3369 if (dev->parent != 0) { 3370 if ((flags &~ INTR_ENTROPY) == (INTR_TYPE_NET | INTR_MPSAFE) && 3371 !debug_mpsafenet) 3372 flags &= ~INTR_MPSAFE; 3373 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, 3374 handler, arg, cookiep); 3375 if (error == 0) { 3376 if (!(flags & (INTR_MPSAFE | INTR_FAST))) 3377 device_printf(dev, "[GIANT-LOCKED]\n"); 3378 if (bootverbose && (flags & INTR_MPSAFE)) 3379 device_printf(dev, "[MPSAFE]\n"); 3380 if (flags & INTR_FAST) 3381 device_printf(dev, "[FAST]\n"); 3382 } 3383 } else 3384 error = EINVAL; 3385 return (error); 3386} 3387 3388/** 3389 * @brief Wrapper function for BUS_TEARDOWN_INTR(). 3390 * 3391 * This function simply calls the BUS_TEARDOWN_INTR() method of the 3392 * parent of @p dev. 3393 */ 3394int 3395bus_teardown_intr(device_t dev, struct resource *r, void *cookie) 3396{ 3397 if (dev->parent == 0) 3398 return (EINVAL); 3399 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie)); 3400} 3401 3402/** 3403 * @brief Wrapper function for BUS_SET_RESOURCE(). 3404 * 3405 * This function simply calls the BUS_SET_RESOURCE() method of the 3406 * parent of @p dev. 3407 */ 3408int 3409bus_set_resource(device_t dev, int type, int rid, 3410 u_long start, u_long count) 3411{ 3412 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid, 3413 start, count)); 3414} 3415 3416/** 3417 * @brief Wrapper function for BUS_GET_RESOURCE(). 3418 * 3419 * This function simply calls the BUS_GET_RESOURCE() method of the 3420 * parent of @p dev. 3421 */ 3422int 3423bus_get_resource(device_t dev, int type, int rid, 3424 u_long *startp, u_long *countp) 3425{ 3426 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 3427 startp, countp)); 3428} 3429 3430/** 3431 * @brief Wrapper function for BUS_GET_RESOURCE(). 3432 * 3433 * This function simply calls the BUS_GET_RESOURCE() method of the 3434 * parent of @p dev and returns the start value. 3435 */ 3436u_long 3437bus_get_resource_start(device_t dev, int type, int rid) 3438{ 3439 u_long start, count; 3440 int error; 3441 3442 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 3443 &start, &count); 3444 if (error) 3445 return (0); 3446 return (start); 3447} 3448 3449/** 3450 * @brief Wrapper function for BUS_GET_RESOURCE(). 3451 * 3452 * This function simply calls the BUS_GET_RESOURCE() method of the 3453 * parent of @p dev and returns the count value. 3454 */ 3455u_long 3456bus_get_resource_count(device_t dev, int type, int rid) 3457{ 3458 u_long start, count; 3459 int error; 3460 3461 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 3462 &start, &count); 3463 if (error) 3464 return (0); 3465 return (count); 3466} 3467 3468/** 3469 * @brief Wrapper function for BUS_DELETE_RESOURCE(). 3470 * 3471 * This function simply calls the BUS_DELETE_RESOURCE() method of the 3472 * parent of @p dev. 3473 */ 3474void 3475bus_delete_resource(device_t dev, int type, int rid) 3476{ 3477 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid); 3478} 3479 3480/** 3481 * @brief Wrapper function for BUS_CHILD_PRESENT(). 3482 * 3483 * This function simply calls the BUS_CHILD_PRESENT() method of the 3484 * parent of @p dev. 3485 */ 3486int 3487bus_child_present(device_t child) 3488{ 3489 return (BUS_CHILD_PRESENT(device_get_parent(child), child)); 3490} 3491 3492/** 3493 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR(). 3494 * 3495 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the 3496 * parent of @p dev. 3497 */ 3498int 3499bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen) 3500{ 3501 device_t parent; 3502 3503 parent = device_get_parent(child); 3504 if (parent == NULL) { 3505 *buf = '\0'; 3506 return (0); 3507 } 3508 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen)); 3509} 3510 3511/** 3512 * @brief Wrapper function for BUS_CHILD_LOCATION_STR(). 3513 * 3514 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the 3515 * parent of @p dev. 3516 */ 3517int 3518bus_child_location_str(device_t child, char *buf, size_t buflen) 3519{ 3520 device_t parent; 3521 3522 parent = device_get_parent(child); 3523 if (parent == NULL) { 3524 *buf = '\0'; 3525 return (0); 3526 } 3527 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen)); 3528} 3529 3530static int 3531root_print_child(device_t dev, device_t child) 3532{ 3533 int retval = 0; 3534 3535 retval += bus_print_child_header(dev, child); 3536 retval += printf("\n"); 3537 3538 return (retval); 3539} 3540 3541static int 3542root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg, 3543 void **cookiep) 3544{ 3545 /* 3546 * If an interrupt mapping gets to here something bad has happened. 3547 */ 3548 panic("root_setup_intr"); 3549} 3550 3551/* 3552 * If we get here, assume that the device is permanant and really is 3553 * present in the system. Removable bus drivers are expected to intercept 3554 * this call long before it gets here. We return -1 so that drivers that 3555 * really care can check vs -1 or some ERRNO returned higher in the food 3556 * chain. 3557 */ 3558static int 3559root_child_present(device_t dev, device_t child) 3560{ 3561 return (-1); 3562} 3563 3564static kobj_method_t root_methods[] = { 3565 /* Device interface */ 3566 KOBJMETHOD(device_shutdown, bus_generic_shutdown), 3567 KOBJMETHOD(device_suspend, bus_generic_suspend), 3568 KOBJMETHOD(device_resume, bus_generic_resume), 3569 3570 /* Bus interface */ 3571 KOBJMETHOD(bus_print_child, root_print_child), 3572 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar), 3573 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar), 3574 KOBJMETHOD(bus_setup_intr, root_setup_intr), 3575 KOBJMETHOD(bus_child_present, root_child_present), 3576 3577 { 0, 0 } 3578}; 3579 3580static driver_t root_driver = { 3581 "root", 3582 root_methods, 3583 1, /* no softc */ 3584}; 3585 3586device_t root_bus; 3587devclass_t root_devclass; 3588 3589static int 3590root_bus_module_handler(module_t mod, int what, void* arg) 3591{ 3592 switch (what) { 3593 case MOD_LOAD: 3594 TAILQ_INIT(&bus_data_devices); 3595 kobj_class_compile((kobj_class_t) &root_driver); 3596 root_bus = make_device(NULL, "root", 0); 3597 root_bus->desc = "System root bus"; 3598 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver); 3599 root_bus->driver = &root_driver; 3600 root_bus->state = DS_ATTACHED; 3601 root_devclass = devclass_find_internal("root", 0, FALSE); 3602 devinit(); 3603 return (0); 3604 3605 case MOD_SHUTDOWN: 3606 device_shutdown(root_bus); 3607 return (0); 3608 default: 3609 return (EOPNOTSUPP); 3610 } 3611 3612 return (0); 3613} 3614 3615static moduledata_t root_bus_mod = { 3616 "rootbus", 3617 root_bus_module_handler, 3618 0 3619}; 3620DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 3621 3622/** 3623 * @brief Automatically configure devices 3624 * 3625 * This function begins the autoconfiguration process by calling 3626 * device_probe_and_attach() for each child of the @c root0 device. 3627 */ 3628void 3629root_bus_configure(void) 3630{ 3631 device_t dev; 3632 3633 PDEBUG((".")); 3634 3635 TAILQ_FOREACH(dev, &root_bus->children, link) { 3636 device_probe_and_attach(dev); 3637 } 3638} 3639 3640/** 3641 * @brief Module handler for registering device drivers 3642 * 3643 * This module handler is used to automatically register device 3644 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls 3645 * devclass_add_driver() for the driver described by the 3646 * driver_module_data structure pointed to by @p arg 3647 */ 3648int 3649driver_module_handler(module_t mod, int what, void *arg) 3650{ 3651 int error; 3652 struct driver_module_data *dmd; 3653 devclass_t bus_devclass; 3654 kobj_class_t driver; 3655 3656 dmd = (struct driver_module_data *)arg; 3657 bus_devclass = devclass_find_internal(dmd->dmd_busname, 0, TRUE); 3658 error = 0; 3659 3660 switch (what) { 3661 case MOD_LOAD: 3662 if (dmd->dmd_chainevh) 3663 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 3664 3665 driver = dmd->dmd_driver; 3666 PDEBUG(("Loading module: driver %s on bus %s", 3667 DRIVERNAME(driver), dmd->dmd_busname)); 3668 error = devclass_add_driver(bus_devclass, driver); 3669 if (error) 3670 break; 3671 3672 /* 3673 * If the driver has any base classes, make the 3674 * devclass inherit from the devclass of the driver's 3675 * first base class. This will allow the system to 3676 * search for drivers in both devclasses for children 3677 * of a device using this driver. 3678 */ 3679 if (driver->baseclasses) { 3680 const char *parentname; 3681 parentname = driver->baseclasses[0]->name; 3682 *dmd->dmd_devclass = 3683 devclass_find_internal(driver->name, 3684 parentname, TRUE); 3685 } else { 3686 *dmd->dmd_devclass = 3687 devclass_find_internal(driver->name, 0, TRUE); 3688 } 3689 break; 3690 3691 case MOD_UNLOAD: 3692 PDEBUG(("Unloading module: driver %s from bus %s", 3693 DRIVERNAME(dmd->dmd_driver), 3694 dmd->dmd_busname)); 3695 error = devclass_delete_driver(bus_devclass, 3696 dmd->dmd_driver); 3697 3698 if (!error && dmd->dmd_chainevh) 3699 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 3700 break; 3701 case MOD_QUIESCE: 3702 PDEBUG(("Quiesce module: driver %s from bus %s", 3703 DRIVERNAME(dmd->dmd_driver), 3704 dmd->dmd_busname)); 3705 error = devclass_quiesce_driver(bus_devclass, 3706 dmd->dmd_driver); 3707 3708 if (!error && dmd->dmd_chainevh) 3709 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 3710 break; 3711 default: 3712 error = EOPNOTSUPP; 3713 break; 3714 } 3715 3716 return (error); 3717} 3718 3719#ifdef BUS_DEBUG 3720 3721/* the _short versions avoid iteration by not calling anything that prints 3722 * more than oneliners. I love oneliners. 3723 */ 3724 3725static void 3726print_device_short(device_t dev, int indent) 3727{ 3728 if (!dev) 3729 return; 3730 3731 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n", 3732 dev->unit, dev->desc, 3733 (dev->parent? "":"no "), 3734 (TAILQ_EMPTY(&dev->children)? "no ":""), 3735 (dev->flags&DF_ENABLED? "enabled,":"disabled,"), 3736 (dev->flags&DF_FIXEDCLASS? "fixed,":""), 3737 (dev->flags&DF_WILDCARD? "wildcard,":""), 3738 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""), 3739 (dev->flags&DF_REBID? "rebiddable,":""), 3740 (dev->ivars? "":"no "), 3741 (dev->softc? "":"no "), 3742 dev->busy)); 3743} 3744 3745static void 3746print_device(device_t dev, int indent) 3747{ 3748 if (!dev) 3749 return; 3750 3751 print_device_short(dev, indent); 3752 3753 indentprintf(("Parent:\n")); 3754 print_device_short(dev->parent, indent+1); 3755 indentprintf(("Driver:\n")); 3756 print_driver_short(dev->driver, indent+1); 3757 indentprintf(("Devclass:\n")); 3758 print_devclass_short(dev->devclass, indent+1); 3759} 3760 3761void 3762print_device_tree_short(device_t dev, int indent) 3763/* print the device and all its children (indented) */ 3764{ 3765 device_t child; 3766 3767 if (!dev) 3768 return; 3769 3770 print_device_short(dev, indent); 3771 3772 TAILQ_FOREACH(child, &dev->children, link) { 3773 print_device_tree_short(child, indent+1); 3774 } 3775} 3776 3777void 3778print_device_tree(device_t dev, int indent) 3779/* print the device and all its children (indented) */ 3780{ 3781 device_t child; 3782 3783 if (!dev) 3784 return; 3785 3786 print_device(dev, indent); 3787 3788 TAILQ_FOREACH(child, &dev->children, link) { 3789 print_device_tree(child, indent+1); 3790 } 3791} 3792 3793static void 3794print_driver_short(driver_t *driver, int indent) 3795{ 3796 if (!driver) 3797 return; 3798 3799 indentprintf(("driver %s: softc size = %zd\n", 3800 driver->name, driver->size)); 3801} 3802 3803static void 3804print_driver(driver_t *driver, int indent) 3805{ 3806 if (!driver) 3807 return; 3808 3809 print_driver_short(driver, indent); 3810} 3811 3812 3813static void 3814print_driver_list(driver_list_t drivers, int indent) 3815{ 3816 driverlink_t driver; 3817 3818 TAILQ_FOREACH(driver, &drivers, link) { 3819 print_driver(driver->driver, indent); 3820 } 3821} 3822 3823static void 3824print_devclass_short(devclass_t dc, int indent) 3825{ 3826 if ( !dc ) 3827 return; 3828 3829 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit)); 3830} 3831 3832static void 3833print_devclass(devclass_t dc, int indent) 3834{ 3835 int i; 3836 3837 if ( !dc ) 3838 return; 3839 3840 print_devclass_short(dc, indent); 3841 indentprintf(("Drivers:\n")); 3842 print_driver_list(dc->drivers, indent+1); 3843 3844 indentprintf(("Devices:\n")); 3845 for (i = 0; i < dc->maxunit; i++) 3846 if (dc->devices[i]) 3847 print_device(dc->devices[i], indent+1); 3848} 3849 3850void 3851print_devclass_list_short(void) 3852{ 3853 devclass_t dc; 3854 3855 printf("Short listing of devclasses, drivers & devices:\n"); 3856 TAILQ_FOREACH(dc, &devclasses, link) { 3857 print_devclass_short(dc, 0); 3858 } 3859} 3860 3861void 3862print_devclass_list(void) 3863{ 3864 devclass_t dc; 3865 3866 printf("Full listing of devclasses, drivers & devices:\n"); 3867 TAILQ_FOREACH(dc, &devclasses, link) { 3868 print_devclass(dc, 0); 3869 } 3870} 3871 3872#endif 3873 3874/* 3875 * User-space access to the device tree. 3876 * 3877 * We implement a small set of nodes: 3878 * 3879 * hw.bus Single integer read method to obtain the 3880 * current generation count. 3881 * hw.bus.devices Reads the entire device tree in flat space. 3882 * hw.bus.rman Resource manager interface 3883 * 3884 * We might like to add the ability to scan devclasses and/or drivers to 3885 * determine what else is currently loaded/available. 3886 */ 3887 3888static int 3889sysctl_bus(SYSCTL_HANDLER_ARGS) 3890{ 3891 struct u_businfo ubus; 3892 3893 ubus.ub_version = BUS_USER_VERSION; 3894 ubus.ub_generation = bus_data_generation; 3895 3896 return (SYSCTL_OUT(req, &ubus, sizeof(ubus))); 3897} 3898SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus, 3899 "bus-related data"); 3900 3901static int 3902sysctl_devices(SYSCTL_HANDLER_ARGS) 3903{ 3904 int *name = (int *)arg1; 3905 u_int namelen = arg2; 3906 int index; 3907 struct device *dev; 3908 struct u_device udev; /* XXX this is a bit big */ 3909 int error; 3910 3911 if (namelen != 2) 3912 return (EINVAL); 3913 3914 if (bus_data_generation_check(name[0])) 3915 return (EINVAL); 3916 3917 index = name[1]; 3918 3919 /* 3920 * Scan the list of devices, looking for the requested index. 3921 */ 3922 TAILQ_FOREACH(dev, &bus_data_devices, devlink) { 3923 if (index-- == 0) 3924 break; 3925 } 3926 if (dev == NULL) 3927 return (ENOENT); 3928 3929 /* 3930 * Populate the return array. 3931 */ 3932 udev.dv_handle = (uintptr_t)dev; 3933 udev.dv_parent = (uintptr_t)dev->parent; 3934 if (dev->nameunit == NULL) 3935 udev.dv_name[0] = '\0'; 3936 else 3937 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name)); 3938 3939 if (dev->desc == NULL) 3940 udev.dv_desc[0] = '\0'; 3941 else 3942 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc)); 3943 if (dev->driver == NULL || dev->driver->name == NULL) 3944 udev.dv_drivername[0] = '\0'; 3945 else 3946 strlcpy(udev.dv_drivername, dev->driver->name, 3947 sizeof(udev.dv_drivername)); 3948 udev.dv_pnpinfo[0] = '\0'; 3949 udev.dv_location[0] = '\0'; 3950 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo)); 3951 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location)); 3952 udev.dv_devflags = dev->devflags; 3953 udev.dv_flags = dev->flags; 3954 udev.dv_state = dev->state; 3955 error = SYSCTL_OUT(req, &udev, sizeof(udev)); 3956 return (error); 3957} 3958 3959SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices, 3960 "system device tree"); 3961 3962int 3963bus_data_generation_check(int generation) 3964{ 3965 if (generation != bus_data_generation) 3966 return (1); 3967 3968 /* XXX generate optimised lists here? */ 3969 return (0); 3970} 3971 3972void 3973bus_data_generation_update(void) 3974{ 3975 bus_data_generation++; 3976} 3977 3978int 3979bus_free_resource(device_t dev, int type, struct resource *r) 3980{ 3981 if (r == NULL) 3982 return (0); 3983 return (bus_release_resource(dev, type, rman_get_rid(r), r)); 3984} 3985