subr_bus.c revision 144609
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 144609 2005-04-03 22:23:18Z 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 the number of devices in a devclass 1138 * 1139 * @param dc the devclass to examine 1140 */ 1141int 1142devclass_get_count(devclass_t dc) 1143{ 1144 int count, i; 1145 1146 count = 0; 1147 for (i = 0; i < dc->maxunit; i++) 1148 if (dc->devices[i]) 1149 count++; 1150 return (count); 1151} 1152 1153/** 1154 * @brief Get the maximum unit number used in a devclass 1155 * 1156 * @param dc the devclass to examine 1157 */ 1158int 1159devclass_get_maxunit(devclass_t dc) 1160{ 1161 return (dc->maxunit); 1162} 1163 1164/** 1165 * @brief Find a free unit number in a devclass 1166 * 1167 * This function searches for the first unused unit number greater 1168 * that or equal to @p unit. 1169 * 1170 * @param dc the devclass to examine 1171 * @param unit the first unit number to check 1172 */ 1173int 1174devclass_find_free_unit(devclass_t dc, int unit) 1175{ 1176 if (dc == NULL) 1177 return (unit); 1178 while (unit < dc->maxunit && dc->devices[unit] != NULL) 1179 unit++; 1180 return (unit); 1181} 1182 1183/** 1184 * @brief Set the parent of a devclass 1185 * 1186 * The parent class is normally initialised automatically by 1187 * DRIVER_MODULE(). 1188 * 1189 * @param dc the devclass to edit 1190 * @param pdc the new parent devclass 1191 */ 1192void 1193devclass_set_parent(devclass_t dc, devclass_t pdc) 1194{ 1195 dc->parent = pdc; 1196} 1197 1198/** 1199 * @brief Get the parent of a devclass 1200 * 1201 * @param dc the devclass to examine 1202 */ 1203devclass_t 1204devclass_get_parent(devclass_t dc) 1205{ 1206 return (dc->parent); 1207} 1208 1209struct sysctl_ctx_list * 1210devclass_get_sysctl_ctx(devclass_t dc) 1211{ 1212 return (&dc->sysctl_ctx); 1213} 1214 1215struct sysctl_oid * 1216devclass_get_sysctl_tree(devclass_t dc) 1217{ 1218 return (dc->sysctl_tree); 1219} 1220 1221/** 1222 * @internal 1223 * @brief Allocate a unit number 1224 * 1225 * On entry, @p *unitp is the desired unit number (or @c -1 if any 1226 * will do). The allocated unit number is returned in @p *unitp. 1227 1228 * @param dc the devclass to allocate from 1229 * @param unitp points at the location for the allocated unit 1230 * number 1231 * 1232 * @retval 0 success 1233 * @retval EEXIST the requested unit number is already allocated 1234 * @retval ENOMEM memory allocation failure 1235 */ 1236static int 1237devclass_alloc_unit(devclass_t dc, int *unitp) 1238{ 1239 int unit = *unitp; 1240 1241 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc))); 1242 1243 /* If we were given a wired unit number, check for existing device */ 1244 /* XXX imp XXX */ 1245 if (unit != -1) { 1246 if (unit >= 0 && unit < dc->maxunit && 1247 dc->devices[unit] != NULL) { 1248 if (bootverbose) 1249 printf("%s: %s%d already exists; skipping it\n", 1250 dc->name, dc->name, *unitp); 1251 return (EEXIST); 1252 } 1253 } else { 1254 /* Unwired device, find the next available slot for it */ 1255 unit = 0; 1256 while (unit < dc->maxunit && dc->devices[unit] != NULL) 1257 unit++; 1258 } 1259 1260 /* 1261 * We've selected a unit beyond the length of the table, so let's 1262 * extend the table to make room for all units up to and including 1263 * this one. 1264 */ 1265 if (unit >= dc->maxunit) { 1266 device_t *newlist; 1267 int newsize; 1268 1269 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t)); 1270 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT); 1271 if (!newlist) 1272 return (ENOMEM); 1273 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit); 1274 bzero(newlist + dc->maxunit, 1275 sizeof(device_t) * (newsize - dc->maxunit)); 1276 if (dc->devices) 1277 free(dc->devices, M_BUS); 1278 dc->devices = newlist; 1279 dc->maxunit = newsize; 1280 } 1281 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc))); 1282 1283 *unitp = unit; 1284 return (0); 1285} 1286 1287/** 1288 * @internal 1289 * @brief Add a device to a devclass 1290 * 1291 * A unit number is allocated for the device (using the device's 1292 * preferred unit number if any) and the device is registered in the 1293 * devclass. This allows the device to be looked up by its unit 1294 * number, e.g. by decoding a dev_t minor number. 1295 * 1296 * @param dc the devclass to add to 1297 * @param dev the device to add 1298 * 1299 * @retval 0 success 1300 * @retval EEXIST the requested unit number is already allocated 1301 * @retval ENOMEM memory allocation failure 1302 */ 1303static int 1304devclass_add_device(devclass_t dc, device_t dev) 1305{ 1306 int buflen, error; 1307 1308 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 1309 1310 buflen = snprintf(NULL, 0, "%s%d$", dc->name, dev->unit); 1311 if (buflen < 0) 1312 return (ENOMEM); 1313 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO); 1314 if (!dev->nameunit) 1315 return (ENOMEM); 1316 1317 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) { 1318 free(dev->nameunit, M_BUS); 1319 dev->nameunit = NULL; 1320 return (error); 1321 } 1322 dc->devices[dev->unit] = dev; 1323 dev->devclass = dc; 1324 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit); 1325 1326 return (0); 1327} 1328 1329/** 1330 * @internal 1331 * @brief Delete a device from a devclass 1332 * 1333 * The device is removed from the devclass's device list and its unit 1334 * number is freed. 1335 1336 * @param dc the devclass to delete from 1337 * @param dev the device to delete 1338 * 1339 * @retval 0 success 1340 */ 1341static int 1342devclass_delete_device(devclass_t dc, device_t dev) 1343{ 1344 if (!dc || !dev) 1345 return (0); 1346 1347 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 1348 1349 if (dev->devclass != dc || dc->devices[dev->unit] != dev) 1350 panic("devclass_delete_device: inconsistent device class"); 1351 dc->devices[dev->unit] = NULL; 1352 if (dev->flags & DF_WILDCARD) 1353 dev->unit = -1; 1354 dev->devclass = NULL; 1355 free(dev->nameunit, M_BUS); 1356 dev->nameunit = NULL; 1357 1358 return (0); 1359} 1360 1361/** 1362 * @internal 1363 * @brief Make a new device and add it as a child of @p parent 1364 * 1365 * @param parent the parent of the new device 1366 * @param name the devclass name of the new device or @c NULL 1367 * to leave the devclass unspecified 1368 * @parem unit the unit number of the new device of @c -1 to 1369 * leave the unit number unspecified 1370 * 1371 * @returns the new device 1372 */ 1373static device_t 1374make_device(device_t parent, const char *name, int unit) 1375{ 1376 device_t dev; 1377 devclass_t dc; 1378 1379 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit)); 1380 1381 if (name) { 1382 dc = devclass_find_internal(name, 0, TRUE); 1383 if (!dc) { 1384 printf("make_device: can't find device class %s\n", 1385 name); 1386 return (NULL); 1387 } 1388 } else { 1389 dc = NULL; 1390 } 1391 1392 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO); 1393 if (!dev) 1394 return (NULL); 1395 1396 dev->parent = parent; 1397 TAILQ_INIT(&dev->children); 1398 kobj_init((kobj_t) dev, &null_class); 1399 dev->driver = NULL; 1400 dev->devclass = NULL; 1401 dev->unit = unit; 1402 dev->nameunit = NULL; 1403 dev->desc = NULL; 1404 dev->busy = 0; 1405 dev->devflags = 0; 1406 dev->flags = DF_ENABLED; 1407 dev->order = 0; 1408 if (unit == -1) 1409 dev->flags |= DF_WILDCARD; 1410 if (name) { 1411 dev->flags |= DF_FIXEDCLASS; 1412 if (devclass_add_device(dc, dev)) { 1413 kobj_delete((kobj_t) dev, M_BUS); 1414 return (NULL); 1415 } 1416 } 1417 dev->ivars = NULL; 1418 dev->softc = NULL; 1419 1420 dev->state = DS_NOTPRESENT; 1421 1422 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink); 1423 bus_data_generation_update(); 1424 1425 return (dev); 1426} 1427 1428/** 1429 * @internal 1430 * @brief Print a description of a device. 1431 */ 1432static int 1433device_print_child(device_t dev, device_t child) 1434{ 1435 int retval = 0; 1436 1437 if (device_is_alive(child)) 1438 retval += BUS_PRINT_CHILD(dev, child); 1439 else 1440 retval += device_printf(child, " not found\n"); 1441 1442 return (retval); 1443} 1444 1445/** 1446 * @brief Create a new device 1447 * 1448 * This creates a new device and adds it as a child of an existing 1449 * parent device. The new device will be added after the last existing 1450 * child with order zero. 1451 * 1452 * @param dev the device which will be the parent of the 1453 * new child device 1454 * @param name devclass name for new device or @c NULL if not 1455 * specified 1456 * @param unit unit number for new device or @c -1 if not 1457 * specified 1458 * 1459 * @returns the new device 1460 */ 1461device_t 1462device_add_child(device_t dev, const char *name, int unit) 1463{ 1464 return (device_add_child_ordered(dev, 0, name, unit)); 1465} 1466 1467/** 1468 * @brief Create a new device 1469 * 1470 * This creates a new device and adds it as a child of an existing 1471 * parent device. The new device will be added after the last existing 1472 * child with the same order. 1473 * 1474 * @param dev the device which will be the parent of the 1475 * new child device 1476 * @param order a value which is used to partially sort the 1477 * children of @p dev - devices created using 1478 * lower values of @p order appear first in @p 1479 * dev's list of children 1480 * @param name devclass name for new device or @c NULL if not 1481 * specified 1482 * @param unit unit number for new device or @c -1 if not 1483 * specified 1484 * 1485 * @returns the new device 1486 */ 1487device_t 1488device_add_child_ordered(device_t dev, int order, const char *name, int unit) 1489{ 1490 device_t child; 1491 device_t place; 1492 1493 PDEBUG(("%s at %s with order %d as unit %d", 1494 name, DEVICENAME(dev), order, unit)); 1495 1496 child = make_device(dev, name, unit); 1497 if (child == NULL) 1498 return (child); 1499 child->order = order; 1500 1501 TAILQ_FOREACH(place, &dev->children, link) { 1502 if (place->order > order) 1503 break; 1504 } 1505 1506 if (place) { 1507 /* 1508 * The device 'place' is the first device whose order is 1509 * greater than the new child. 1510 */ 1511 TAILQ_INSERT_BEFORE(place, child, link); 1512 } else { 1513 /* 1514 * The new child's order is greater or equal to the order of 1515 * any existing device. Add the child to the tail of the list. 1516 */ 1517 TAILQ_INSERT_TAIL(&dev->children, child, link); 1518 } 1519 1520 bus_data_generation_update(); 1521 return (child); 1522} 1523 1524/** 1525 * @brief Delete a device 1526 * 1527 * This function deletes a device along with all of its children. If 1528 * the device currently has a driver attached to it, the device is 1529 * detached first using device_detach(). 1530 * 1531 * @param dev the parent device 1532 * @param child the device to delete 1533 * 1534 * @retval 0 success 1535 * @retval non-zero a unit error code describing the error 1536 */ 1537int 1538device_delete_child(device_t dev, device_t child) 1539{ 1540 int error; 1541 device_t grandchild; 1542 1543 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev))); 1544 1545 /* remove children first */ 1546 while ( (grandchild = TAILQ_FIRST(&child->children)) ) { 1547 error = device_delete_child(child, grandchild); 1548 if (error) 1549 return (error); 1550 } 1551 1552 if ((error = device_detach(child)) != 0) 1553 return (error); 1554 if (child->devclass) 1555 devclass_delete_device(child->devclass, child); 1556 TAILQ_REMOVE(&dev->children, child, link); 1557 TAILQ_REMOVE(&bus_data_devices, child, devlink); 1558 kobj_delete((kobj_t) child, M_BUS); 1559 1560 bus_data_generation_update(); 1561 return (0); 1562} 1563 1564/** 1565 * @brief Find a device given a unit number 1566 * 1567 * This is similar to devclass_get_devices() but only searches for 1568 * devices which have @p dev as a parent. 1569 * 1570 * @param dev the parent device to search 1571 * @param unit the unit number to search for. If the unit is -1, 1572 * return the first child of @p dev which has name 1573 * @p classname (that is, the one with the lowest unit.) 1574 * 1575 * @returns the device with the given unit number or @c 1576 * NULL if there is no such device 1577 */ 1578device_t 1579device_find_child(device_t dev, const char *classname, int unit) 1580{ 1581 devclass_t dc; 1582 device_t child; 1583 1584 dc = devclass_find(classname); 1585 if (!dc) 1586 return (NULL); 1587 1588 if (unit != -1) { 1589 child = devclass_get_device(dc, unit); 1590 if (child && child->parent == dev) 1591 return (child); 1592 } else { 1593 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) { 1594 child = devclass_get_device(dc, unit); 1595 if (child && child->parent == dev) 1596 return (child); 1597 } 1598 } 1599 return (NULL); 1600} 1601 1602/** 1603 * @internal 1604 */ 1605static driverlink_t 1606first_matching_driver(devclass_t dc, device_t dev) 1607{ 1608 if (dev->devclass) 1609 return (devclass_find_driver_internal(dc, dev->devclass->name)); 1610 return (TAILQ_FIRST(&dc->drivers)); 1611} 1612 1613/** 1614 * @internal 1615 */ 1616static driverlink_t 1617next_matching_driver(devclass_t dc, device_t dev, driverlink_t last) 1618{ 1619 if (dev->devclass) { 1620 driverlink_t dl; 1621 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link)) 1622 if (!strcmp(dev->devclass->name, dl->driver->name)) 1623 return (dl); 1624 return (NULL); 1625 } 1626 return (TAILQ_NEXT(last, link)); 1627} 1628 1629/** 1630 * @internal 1631 */ 1632static int 1633device_probe_child(device_t dev, device_t child) 1634{ 1635 devclass_t dc; 1636 driverlink_t best = 0; 1637 driverlink_t dl; 1638 int result, pri = 0; 1639 int hasclass = (child->devclass != 0); 1640 1641 GIANT_REQUIRED; 1642 1643 dc = dev->devclass; 1644 if (!dc) 1645 panic("device_probe_child: parent device has no devclass"); 1646 1647 /* 1648 * If the state is already probed, then return. However, don't 1649 * return if we can rebid this object. 1650 */ 1651 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0) 1652 return (0); 1653 1654 for (; dc; dc = dc->parent) { 1655 for (dl = first_matching_driver(dc, child); 1656 dl; 1657 dl = next_matching_driver(dc, child, dl)) { 1658 PDEBUG(("Trying %s", DRIVERNAME(dl->driver))); 1659 device_set_driver(child, dl->driver); 1660 if (!hasclass) 1661 device_set_devclass(child, dl->driver->name); 1662 1663 /* Fetch any flags for the device before probing. */ 1664 resource_int_value(dl->driver->name, child->unit, 1665 "flags", &child->devflags); 1666 1667 result = DEVICE_PROBE(child); 1668 1669 /* Reset flags and devclass before the next probe. */ 1670 child->devflags = 0; 1671 if (!hasclass) 1672 device_set_devclass(child, 0); 1673 1674 /* 1675 * If the driver returns SUCCESS, there can be 1676 * no higher match for this device. 1677 */ 1678 if (result == 0) { 1679 best = dl; 1680 pri = 0; 1681 break; 1682 } 1683 1684 /* 1685 * The driver returned an error so it 1686 * certainly doesn't match. 1687 */ 1688 if (result > 0) { 1689 device_set_driver(child, 0); 1690 continue; 1691 } 1692 1693 /* 1694 * A priority lower than SUCCESS, remember the 1695 * best matching driver. Initialise the value 1696 * of pri for the first match. 1697 */ 1698 if (best == 0 || result > pri) { 1699 best = dl; 1700 pri = result; 1701 continue; 1702 } 1703 } 1704 /* 1705 * If we have an unambiguous match in this devclass, 1706 * don't look in the parent. 1707 */ 1708 if (best && pri == 0) 1709 break; 1710 } 1711 1712 /* 1713 * If we found a driver, change state and initialise the devclass. 1714 */ 1715 /* XXX What happens if we rebid and got no best? */ 1716 if (best) { 1717 /* 1718 * If this device was atached, and we were asked to 1719 * rescan, and it is a different driver, then we have 1720 * to detach the old driver and reattach this new one. 1721 * Note, we don't have to check for DF_REBID here 1722 * because if the state is > DS_ALIVE, we know it must 1723 * be. 1724 * 1725 * This assumes that all DF_REBID drivers can have 1726 * their probe routine called at any time and that 1727 * they are idempotent as well as completely benign in 1728 * normal operations. 1729 * 1730 * We also have to make sure that the detach 1731 * succeeded, otherwise we fail the operation (or 1732 * maybe it should just fail silently? I'm torn). 1733 */ 1734 if (child->state > DS_ALIVE && best->driver != child->driver) 1735 if ((result = device_detach(dev)) != 0) 1736 return (result); 1737 1738 /* Set the winning driver, devclass, and flags. */ 1739 if (!child->devclass) 1740 device_set_devclass(child, best->driver->name); 1741 device_set_driver(child, best->driver); 1742 resource_int_value(best->driver->name, child->unit, 1743 "flags", &child->devflags); 1744 1745 if (pri < 0) { 1746 /* 1747 * A bit bogus. Call the probe method again to make 1748 * sure that we have the right description. 1749 */ 1750 DEVICE_PROBE(child); 1751#if 0 1752 child->flags |= DF_REBID; 1753#endif 1754 } else 1755 child->flags &= ~DF_REBID; 1756 child->state = DS_ALIVE; 1757 1758 bus_data_generation_update(); 1759 return (0); 1760 } 1761 1762 return (ENXIO); 1763} 1764 1765/** 1766 * @brief Return the parent of a device 1767 */ 1768device_t 1769device_get_parent(device_t dev) 1770{ 1771 return (dev->parent); 1772} 1773 1774/** 1775 * @brief Get a list of children of a device 1776 * 1777 * An array containing a list of all the children of the given device 1778 * is allocated and returned in @p *devlistp. The number of devices 1779 * in the array is returned in @p *devcountp. The caller should free 1780 * the array using @c free(p, M_TEMP). 1781 * 1782 * @param dev the device to examine 1783 * @param devlistp points at location for array pointer return 1784 * value 1785 * @param devcountp points at location for array size return value 1786 * 1787 * @retval 0 success 1788 * @retval ENOMEM the array allocation failed 1789 */ 1790int 1791device_get_children(device_t dev, device_t **devlistp, int *devcountp) 1792{ 1793 int count; 1794 device_t child; 1795 device_t *list; 1796 1797 count = 0; 1798 TAILQ_FOREACH(child, &dev->children, link) { 1799 count++; 1800 } 1801 1802 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO); 1803 if (!list) 1804 return (ENOMEM); 1805 1806 count = 0; 1807 TAILQ_FOREACH(child, &dev->children, link) { 1808 list[count] = child; 1809 count++; 1810 } 1811 1812 *devlistp = list; 1813 *devcountp = count; 1814 1815 return (0); 1816} 1817 1818/** 1819 * @brief Return the current driver for the device or @c NULL if there 1820 * is no driver currently attached 1821 */ 1822driver_t * 1823device_get_driver(device_t dev) 1824{ 1825 return (dev->driver); 1826} 1827 1828/** 1829 * @brief Return the current devclass for the device or @c NULL if 1830 * there is none. 1831 */ 1832devclass_t 1833device_get_devclass(device_t dev) 1834{ 1835 return (dev->devclass); 1836} 1837 1838/** 1839 * @brief Return the name of the device's devclass or @c NULL if there 1840 * is none. 1841 */ 1842const char * 1843device_get_name(device_t dev) 1844{ 1845 if (dev != NULL && dev->devclass) 1846 return (devclass_get_name(dev->devclass)); 1847 return (NULL); 1848} 1849 1850/** 1851 * @brief Return a string containing the device's devclass name 1852 * followed by an ascii representation of the device's unit number 1853 * (e.g. @c "foo2"). 1854 */ 1855const char * 1856device_get_nameunit(device_t dev) 1857{ 1858 return (dev->nameunit); 1859} 1860 1861/** 1862 * @brief Return the device's unit number. 1863 */ 1864int 1865device_get_unit(device_t dev) 1866{ 1867 return (dev->unit); 1868} 1869 1870/** 1871 * @brief Return the device's description string 1872 */ 1873const char * 1874device_get_desc(device_t dev) 1875{ 1876 return (dev->desc); 1877} 1878 1879/** 1880 * @brief Return the device's flags 1881 */ 1882u_int32_t 1883device_get_flags(device_t dev) 1884{ 1885 return (dev->devflags); 1886} 1887 1888struct sysctl_ctx_list * 1889device_get_sysctl_ctx(device_t dev) 1890{ 1891 return (&dev->sysctl_ctx); 1892} 1893 1894struct sysctl_oid * 1895device_get_sysctl_tree(device_t dev) 1896{ 1897 return (dev->sysctl_tree); 1898} 1899 1900/** 1901 * @brief Print the name of the device followed by a colon and a space 1902 * 1903 * @returns the number of characters printed 1904 */ 1905int 1906device_print_prettyname(device_t dev) 1907{ 1908 const char *name = device_get_name(dev); 1909 1910 if (name == 0) 1911 return (printf("unknown: ")); 1912 return (printf("%s%d: ", name, device_get_unit(dev))); 1913} 1914 1915/** 1916 * @brief Print the name of the device followed by a colon, a space 1917 * and the result of calling vprintf() with the value of @p fmt and 1918 * the following arguments. 1919 * 1920 * @returns the number of characters printed 1921 */ 1922int 1923device_printf(device_t dev, const char * fmt, ...) 1924{ 1925 va_list ap; 1926 int retval; 1927 1928 retval = device_print_prettyname(dev); 1929 va_start(ap, fmt); 1930 retval += vprintf(fmt, ap); 1931 va_end(ap); 1932 return (retval); 1933} 1934 1935/** 1936 * @internal 1937 */ 1938static void 1939device_set_desc_internal(device_t dev, const char* desc, int copy) 1940{ 1941 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) { 1942 free(dev->desc, M_BUS); 1943 dev->flags &= ~DF_DESCMALLOCED; 1944 dev->desc = NULL; 1945 } 1946 1947 if (copy && desc) { 1948 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT); 1949 if (dev->desc) { 1950 strcpy(dev->desc, desc); 1951 dev->flags |= DF_DESCMALLOCED; 1952 } 1953 } else { 1954 /* Avoid a -Wcast-qual warning */ 1955 dev->desc = (char *)(uintptr_t) desc; 1956 } 1957 1958 bus_data_generation_update(); 1959} 1960 1961/** 1962 * @brief Set the device's description 1963 * 1964 * The value of @c desc should be a string constant that will not 1965 * change (at least until the description is changed in a subsequent 1966 * call to device_set_desc() or device_set_desc_copy()). 1967 */ 1968void 1969device_set_desc(device_t dev, const char* desc) 1970{ 1971 device_set_desc_internal(dev, desc, FALSE); 1972} 1973 1974/** 1975 * @brief Set the device's description 1976 * 1977 * The string pointed to by @c desc is copied. Use this function if 1978 * the device description is generated, (e.g. with sprintf()). 1979 */ 1980void 1981device_set_desc_copy(device_t dev, const char* desc) 1982{ 1983 device_set_desc_internal(dev, desc, TRUE); 1984} 1985 1986/** 1987 * @brief Set the device's flags 1988 */ 1989void 1990device_set_flags(device_t dev, u_int32_t flags) 1991{ 1992 dev->devflags = flags; 1993} 1994 1995/** 1996 * @brief Return the device's softc field 1997 * 1998 * The softc is allocated and zeroed when a driver is attached, based 1999 * on the size field of the driver. 2000 */ 2001void * 2002device_get_softc(device_t dev) 2003{ 2004 return (dev->softc); 2005} 2006 2007/** 2008 * @brief Set the device's softc field 2009 * 2010 * Most drivers do not need to use this since the softc is allocated 2011 * automatically when the driver is attached. 2012 */ 2013void 2014device_set_softc(device_t dev, void *softc) 2015{ 2016 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) 2017 free(dev->softc, M_BUS_SC); 2018 dev->softc = softc; 2019 if (dev->softc) 2020 dev->flags |= DF_EXTERNALSOFTC; 2021 else 2022 dev->flags &= ~DF_EXTERNALSOFTC; 2023} 2024 2025/** 2026 * @brief Get the device's ivars field 2027 * 2028 * The ivars field is used by the parent device to store per-device 2029 * state (e.g. the physical location of the device or a list of 2030 * resources). 2031 */ 2032void * 2033device_get_ivars(device_t dev) 2034{ 2035 2036 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)")); 2037 return (dev->ivars); 2038} 2039 2040/** 2041 * @brief Set the device's ivars field 2042 */ 2043void 2044device_set_ivars(device_t dev, void * ivars) 2045{ 2046 2047 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)")); 2048 dev->ivars = ivars; 2049} 2050 2051/** 2052 * @brief Return the device's state 2053 */ 2054device_state_t 2055device_get_state(device_t dev) 2056{ 2057 return (dev->state); 2058} 2059 2060/** 2061 * @brief Set the DF_ENABLED flag for the device 2062 */ 2063void 2064device_enable(device_t dev) 2065{ 2066 dev->flags |= DF_ENABLED; 2067} 2068 2069/** 2070 * @brief Clear the DF_ENABLED flag for the device 2071 */ 2072void 2073device_disable(device_t dev) 2074{ 2075 dev->flags &= ~DF_ENABLED; 2076} 2077 2078/** 2079 * @brief Increment the busy counter for the device 2080 */ 2081void 2082device_busy(device_t dev) 2083{ 2084 if (dev->state < DS_ATTACHED) 2085 panic("device_busy: called for unattached device"); 2086 if (dev->busy == 0 && dev->parent) 2087 device_busy(dev->parent); 2088 dev->busy++; 2089 dev->state = DS_BUSY; 2090} 2091 2092/** 2093 * @brief Decrement the busy counter for the device 2094 */ 2095void 2096device_unbusy(device_t dev) 2097{ 2098 if (dev->state != DS_BUSY) 2099 panic("device_unbusy: called for non-busy device %s", 2100 device_get_nameunit(dev)); 2101 dev->busy--; 2102 if (dev->busy == 0) { 2103 if (dev->parent) 2104 device_unbusy(dev->parent); 2105 dev->state = DS_ATTACHED; 2106 } 2107} 2108 2109/** 2110 * @brief Set the DF_QUIET flag for the device 2111 */ 2112void 2113device_quiet(device_t dev) 2114{ 2115 dev->flags |= DF_QUIET; 2116} 2117 2118/** 2119 * @brief Clear the DF_QUIET flag for the device 2120 */ 2121void 2122device_verbose(device_t dev) 2123{ 2124 dev->flags &= ~DF_QUIET; 2125} 2126 2127/** 2128 * @brief Return non-zero if the DF_QUIET flag is set on the device 2129 */ 2130int 2131device_is_quiet(device_t dev) 2132{ 2133 return ((dev->flags & DF_QUIET) != 0); 2134} 2135 2136/** 2137 * @brief Return non-zero if the DF_ENABLED flag is set on the device 2138 */ 2139int 2140device_is_enabled(device_t dev) 2141{ 2142 return ((dev->flags & DF_ENABLED) != 0); 2143} 2144 2145/** 2146 * @brief Return non-zero if the device was successfully probed 2147 */ 2148int 2149device_is_alive(device_t dev) 2150{ 2151 return (dev->state >= DS_ALIVE); 2152} 2153 2154/** 2155 * @brief Return non-zero if the device currently has a driver 2156 * attached to it 2157 */ 2158int 2159device_is_attached(device_t dev) 2160{ 2161 return (dev->state >= DS_ATTACHED); 2162} 2163 2164/** 2165 * @brief Set the devclass of a device 2166 * @see devclass_add_device(). 2167 */ 2168int 2169device_set_devclass(device_t dev, const char *classname) 2170{ 2171 devclass_t dc; 2172 int error; 2173 2174 if (!classname) { 2175 if (dev->devclass) 2176 devclass_delete_device(dev->devclass, dev); 2177 return (0); 2178 } 2179 2180 if (dev->devclass) { 2181 printf("device_set_devclass: device class already set\n"); 2182 return (EINVAL); 2183 } 2184 2185 dc = devclass_find_internal(classname, 0, TRUE); 2186 if (!dc) 2187 return (ENOMEM); 2188 2189 error = devclass_add_device(dc, dev); 2190 2191 bus_data_generation_update(); 2192 return (error); 2193} 2194 2195/** 2196 * @brief Set the driver of a device 2197 * 2198 * @retval 0 success 2199 * @retval EBUSY the device already has a driver attached 2200 * @retval ENOMEM a memory allocation failure occurred 2201 */ 2202int 2203device_set_driver(device_t dev, driver_t *driver) 2204{ 2205 if (dev->state >= DS_ATTACHED) 2206 return (EBUSY); 2207 2208 if (dev->driver == driver) 2209 return (0); 2210 2211 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) { 2212 free(dev->softc, M_BUS_SC); 2213 dev->softc = NULL; 2214 } 2215 kobj_delete((kobj_t) dev, 0); 2216 dev->driver = driver; 2217 if (driver) { 2218 kobj_init((kobj_t) dev, (kobj_class_t) driver); 2219 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) { 2220 dev->softc = malloc(driver->size, M_BUS_SC, 2221 M_NOWAIT | M_ZERO); 2222 if (!dev->softc) { 2223 kobj_delete((kobj_t) dev, 0); 2224 kobj_init((kobj_t) dev, &null_class); 2225 dev->driver = NULL; 2226 return (ENOMEM); 2227 } 2228 } 2229 } else { 2230 kobj_init((kobj_t) dev, &null_class); 2231 } 2232 2233 bus_data_generation_update(); 2234 return (0); 2235} 2236 2237/** 2238 * @brief Probe a device and attach a driver if possible 2239 * 2240 * This function is the core of the device autoconfiguration 2241 * system. Its purpose is to select a suitable driver for a device and 2242 * then call that driver to initialise the hardware appropriately. The 2243 * driver is selected by calling the DEVICE_PROBE() method of a set of 2244 * candidate drivers and then choosing the driver which returned the 2245 * best value. This driver is then attached to the device using 2246 * device_attach(). 2247 * 2248 * The set of suitable drivers is taken from the list of drivers in 2249 * the parent device's devclass. If the device was originally created 2250 * with a specific class name (see device_add_child()), only drivers 2251 * with that name are probed, otherwise all drivers in the devclass 2252 * are probed. If no drivers return successful probe values in the 2253 * parent devclass, the search continues in the parent of that 2254 * devclass (see devclass_get_parent()) if any. 2255 * 2256 * @param dev the device to initialise 2257 * 2258 * @retval 0 success 2259 * @retval ENXIO no driver was found 2260 * @retval ENOMEM memory allocation failure 2261 * @retval non-zero some other unix error code 2262 */ 2263int 2264device_probe_and_attach(device_t dev) 2265{ 2266 int error; 2267 2268 GIANT_REQUIRED; 2269 2270 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0) 2271 return (0); 2272 2273 if (!(dev->flags & DF_ENABLED)) { 2274 if (bootverbose && device_get_name(dev) != NULL) { 2275 device_print_prettyname(dev); 2276 printf("not probed (disabled)\n"); 2277 } 2278 return (0); 2279 } 2280 if ((error = device_probe_child(dev->parent, dev)) != 0) { 2281 if (!(dev->flags & DF_DONENOMATCH)) { 2282 BUS_PROBE_NOMATCH(dev->parent, dev); 2283 devnomatch(dev); 2284 dev->flags |= DF_DONENOMATCH; 2285 } 2286 return (error); 2287 } 2288 error = device_attach(dev); 2289 2290 return (error); 2291} 2292 2293/** 2294 * @brief Attach a device driver to a device 2295 * 2296 * This function is a wrapper around the DEVICE_ATTACH() driver 2297 * method. In addition to calling DEVICE_ATTACH(), it initialises the 2298 * device's sysctl tree, optionally prints a description of the device 2299 * and queues a notification event for user-based device management 2300 * services. 2301 * 2302 * Normally this function is only called internally from 2303 * device_probe_and_attach(). 2304 * 2305 * @param dev the device to initialise 2306 * 2307 * @retval 0 success 2308 * @retval ENXIO no driver was found 2309 * @retval ENOMEM memory allocation failure 2310 * @retval non-zero some other unix error code 2311 */ 2312int 2313device_attach(device_t dev) 2314{ 2315 int error; 2316 2317 device_sysctl_init(dev); 2318 if (!device_is_quiet(dev)) 2319 device_print_child(dev->parent, dev); 2320 if ((error = DEVICE_ATTACH(dev)) != 0) { 2321 printf("device_attach: %s%d attach returned %d\n", 2322 dev->driver->name, dev->unit, error); 2323 /* Unset the class; set in device_probe_child */ 2324 if (dev->devclass == 0) 2325 device_set_devclass(dev, 0); 2326 device_set_driver(dev, NULL); 2327 device_sysctl_fini(dev); 2328 dev->state = DS_NOTPRESENT; 2329 return (error); 2330 } 2331 dev->state = DS_ATTACHED; 2332 devadded(dev); 2333 return (0); 2334} 2335 2336/** 2337 * @brief Detach a driver from a device 2338 * 2339 * This function is a wrapper around the DEVICE_DETACH() driver 2340 * method. If the call to DEVICE_DETACH() succeeds, it calls 2341 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a 2342 * notification event for user-based device management services and 2343 * cleans up the device's sysctl tree. 2344 * 2345 * @param dev the device to un-initialise 2346 * 2347 * @retval 0 success 2348 * @retval ENXIO no driver was found 2349 * @retval ENOMEM memory allocation failure 2350 * @retval non-zero some other unix error code 2351 */ 2352int 2353device_detach(device_t dev) 2354{ 2355 int error; 2356 2357 GIANT_REQUIRED; 2358 2359 PDEBUG(("%s", DEVICENAME(dev))); 2360 if (dev->state == DS_BUSY) 2361 return (EBUSY); 2362 if (dev->state != DS_ATTACHED) 2363 return (0); 2364 2365 if ((error = DEVICE_DETACH(dev)) != 0) 2366 return (error); 2367 devremoved(dev); 2368 device_printf(dev, "detached\n"); 2369 if (dev->parent) 2370 BUS_CHILD_DETACHED(dev->parent, dev); 2371 2372 if (!(dev->flags & DF_FIXEDCLASS)) 2373 devclass_delete_device(dev->devclass, dev); 2374 2375 dev->state = DS_NOTPRESENT; 2376 device_set_driver(dev, NULL); 2377 device_set_desc(dev, NULL); 2378 device_sysctl_fini(dev); 2379 2380 return (0); 2381} 2382 2383/** 2384 * @brief Tells a driver to quiesce itself. 2385 * 2386 * This function is a wrapper around the DEVICE_QUIESCE() driver 2387 * method. If the call to DEVICE_QUIESCE() succeeds. 2388 * 2389 * @param dev the device to quiesce 2390 * 2391 * @retval 0 success 2392 * @retval ENXIO no driver was found 2393 * @retval ENOMEM memory allocation failure 2394 * @retval non-zero some other unix error code 2395 */ 2396int 2397device_quiesce(device_t dev) 2398{ 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 return (DEVICE_QUIESCE(dev)); 2407} 2408 2409/** 2410 * @brief Notify a device of system shutdown 2411 * 2412 * This function calls the DEVICE_SHUTDOWN() driver method if the 2413 * device currently has an attached driver. 2414 * 2415 * @returns the value returned by DEVICE_SHUTDOWN() 2416 */ 2417int 2418device_shutdown(device_t dev) 2419{ 2420 if (dev->state < DS_ATTACHED) 2421 return (0); 2422 return (DEVICE_SHUTDOWN(dev)); 2423} 2424 2425/** 2426 * @brief Set the unit number of a device 2427 * 2428 * This function can be used to override the unit number used for a 2429 * device (e.g. to wire a device to a pre-configured unit number). 2430 */ 2431int 2432device_set_unit(device_t dev, int unit) 2433{ 2434 devclass_t dc; 2435 int err; 2436 2437 dc = device_get_devclass(dev); 2438 if (unit < dc->maxunit && dc->devices[unit]) 2439 return (EBUSY); 2440 err = devclass_delete_device(dc, dev); 2441 if (err) 2442 return (err); 2443 dev->unit = unit; 2444 err = devclass_add_device(dc, dev); 2445 if (err) 2446 return (err); 2447 2448 bus_data_generation_update(); 2449 return (0); 2450} 2451 2452/*======================================*/ 2453/* 2454 * Some useful method implementations to make life easier for bus drivers. 2455 */ 2456 2457/** 2458 * @brief Initialise a resource list. 2459 * 2460 * @param rl the resource list to initialise 2461 */ 2462void 2463resource_list_init(struct resource_list *rl) 2464{ 2465 STAILQ_INIT(rl); 2466} 2467 2468/** 2469 * @brief Reclaim memory used by a resource list. 2470 * 2471 * This function frees the memory for all resource entries on the list 2472 * (if any). 2473 * 2474 * @param rl the resource list to free 2475 */ 2476void 2477resource_list_free(struct resource_list *rl) 2478{ 2479 struct resource_list_entry *rle; 2480 2481 while ((rle = STAILQ_FIRST(rl)) != NULL) { 2482 if (rle->res) 2483 panic("resource_list_free: resource entry is busy"); 2484 STAILQ_REMOVE_HEAD(rl, link); 2485 free(rle, M_BUS); 2486 } 2487} 2488 2489/** 2490 * @brief Add a resource entry. 2491 * 2492 * This function adds a resource entry using the given @p type, @p 2493 * start, @p end and @p count values. A rid value is chosen by 2494 * searching sequentially for the first unused rid starting at zero. 2495 * 2496 * @param rl the resource list to edit 2497 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2498 * @param start the start address of the resource 2499 * @param end the end address of the resource 2500 * @param count XXX end-start+1 2501 */ 2502int 2503resource_list_add_next(struct resource_list *rl, int type, u_long start, 2504 u_long end, u_long count) 2505{ 2506 int rid; 2507 2508 rid = 0; 2509 while (resource_list_find(rl, type, rid) != NULL) 2510 rid++; 2511 resource_list_add(rl, type, rid, start, end, count); 2512 return (rid); 2513} 2514 2515/** 2516 * @brief Add or modify a resource entry. 2517 * 2518 * If an existing entry exists with the same type and rid, it will be 2519 * modified using the given values of @p start, @p end and @p 2520 * count. If no entry exists, a new one will be created using the 2521 * given values. 2522 * 2523 * @param rl the resource list to edit 2524 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2525 * @param rid the resource identifier 2526 * @param start the start address of the resource 2527 * @param end the end address of the resource 2528 * @param count XXX end-start+1 2529 */ 2530void 2531resource_list_add(struct resource_list *rl, int type, int rid, 2532 u_long start, u_long end, u_long count) 2533{ 2534 struct resource_list_entry *rle; 2535 2536 rle = resource_list_find(rl, type, rid); 2537 if (!rle) { 2538 rle = malloc(sizeof(struct resource_list_entry), M_BUS, 2539 M_NOWAIT); 2540 if (!rle) 2541 panic("resource_list_add: can't record entry"); 2542 STAILQ_INSERT_TAIL(rl, rle, link); 2543 rle->type = type; 2544 rle->rid = rid; 2545 rle->res = NULL; 2546 } 2547 2548 if (rle->res) 2549 panic("resource_list_add: resource entry is busy"); 2550 2551 rle->start = start; 2552 rle->end = end; 2553 rle->count = count; 2554} 2555 2556/** 2557 * @brief Find a resource entry by type and rid. 2558 * 2559 * @param rl the resource list to search 2560 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2561 * @param rid the resource identifier 2562 * 2563 * @returns the resource entry pointer or NULL if there is no such 2564 * entry. 2565 */ 2566struct resource_list_entry * 2567resource_list_find(struct resource_list *rl, int type, int rid) 2568{ 2569 struct resource_list_entry *rle; 2570 2571 STAILQ_FOREACH(rle, rl, link) { 2572 if (rle->type == type && rle->rid == rid) 2573 return (rle); 2574 } 2575 return (NULL); 2576} 2577 2578/** 2579 * @brief Delete a resource entry. 2580 * 2581 * @param rl the resource list to edit 2582 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2583 * @param rid the resource identifier 2584 */ 2585void 2586resource_list_delete(struct resource_list *rl, int type, int rid) 2587{ 2588 struct resource_list_entry *rle = resource_list_find(rl, type, rid); 2589 2590 if (rle) { 2591 if (rle->res != NULL) 2592 panic("resource_list_delete: resource has not been released"); 2593 STAILQ_REMOVE(rl, rle, resource_list_entry, link); 2594 free(rle, M_BUS); 2595 } 2596} 2597 2598/** 2599 * @brief Helper function for implementing BUS_ALLOC_RESOURCE() 2600 * 2601 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list 2602 * and passing the allocation up to the parent of @p bus. This assumes 2603 * that the first entry of @c device_get_ivars(child) is a struct 2604 * resource_list. This also handles 'passthrough' allocations where a 2605 * child is a remote descendant of bus by passing the allocation up to 2606 * the parent of bus. 2607 * 2608 * Typically, a bus driver would store a list of child resources 2609 * somewhere in the child device's ivars (see device_get_ivars()) and 2610 * its implementation of BUS_ALLOC_RESOURCE() would find that list and 2611 * then call resource_list_alloc() to perform the allocation. 2612 * 2613 * @param rl the resource list to allocate from 2614 * @param bus the parent device of @p child 2615 * @param child the device which is requesting an allocation 2616 * @param type the type of resource to allocate 2617 * @param rid a pointer to the resource identifier 2618 * @param start hint at the start of the resource range - pass 2619 * @c 0UL for any start address 2620 * @param end hint at the end of the resource range - pass 2621 * @c ~0UL for any end address 2622 * @param count hint at the size of range required - pass @c 1 2623 * for any size 2624 * @param flags any extra flags to control the resource 2625 * allocation - see @c RF_XXX flags in 2626 * <sys/rman.h> for details 2627 * 2628 * @returns the resource which was allocated or @c NULL if no 2629 * resource could be allocated 2630 */ 2631struct resource * 2632resource_list_alloc(struct resource_list *rl, device_t bus, device_t child, 2633 int type, int *rid, u_long start, u_long end, u_long count, u_int flags) 2634{ 2635 struct resource_list_entry *rle = 0; 2636 int passthrough = (device_get_parent(child) != bus); 2637 int isdefault = (start == 0UL && end == ~0UL); 2638 2639 if (passthrough) { 2640 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 2641 type, rid, start, end, count, flags)); 2642 } 2643 2644 rle = resource_list_find(rl, type, *rid); 2645 2646 if (!rle) 2647 return (NULL); /* no resource of that type/rid */ 2648 2649 if (rle->res) 2650 panic("resource_list_alloc: resource entry is busy"); 2651 2652 if (isdefault) { 2653 start = rle->start; 2654 count = ulmax(count, rle->count); 2655 end = ulmax(rle->end, start + count - 1); 2656 } 2657 2658 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 2659 type, rid, start, end, count, flags); 2660 2661 /* 2662 * Record the new range. 2663 */ 2664 if (rle->res) { 2665 rle->start = rman_get_start(rle->res); 2666 rle->end = rman_get_end(rle->res); 2667 rle->count = count; 2668 } 2669 2670 return (rle->res); 2671} 2672 2673/** 2674 * @brief Helper function for implementing BUS_RELEASE_RESOURCE() 2675 * 2676 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally 2677 * used with resource_list_alloc(). 2678 * 2679 * @param rl the resource list which was allocated from 2680 * @param bus the parent device of @p child 2681 * @param child the device which is requesting a release 2682 * @param type the type of resource to allocate 2683 * @param rid the resource identifier 2684 * @param res the resource to release 2685 * 2686 * @retval 0 success 2687 * @retval non-zero a standard unix error code indicating what 2688 * error condition prevented the operation 2689 */ 2690int 2691resource_list_release(struct resource_list *rl, device_t bus, device_t child, 2692 int type, int rid, struct resource *res) 2693{ 2694 struct resource_list_entry *rle = 0; 2695 int passthrough = (device_get_parent(child) != bus); 2696 int error; 2697 2698 if (passthrough) { 2699 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 2700 type, rid, res)); 2701 } 2702 2703 rle = resource_list_find(rl, type, rid); 2704 2705 if (!rle) 2706 panic("resource_list_release: can't find resource"); 2707 if (!rle->res) 2708 panic("resource_list_release: resource entry is not busy"); 2709 2710 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 2711 type, rid, res); 2712 if (error) 2713 return (error); 2714 2715 rle->res = NULL; 2716 return (0); 2717} 2718 2719/** 2720 * @brief Print a description of resources in a resource list 2721 * 2722 * Print all resources of a specified type, for use in BUS_PRINT_CHILD(). 2723 * The name is printed if at least one resource of the given type is available. 2724 * The format is used to print resource start and end. 2725 * 2726 * @param rl the resource list to print 2727 * @param name the name of @p type, e.g. @c "memory" 2728 * @param type type type of resource entry to print 2729 * @param format printf(9) format string to print resource 2730 * start and end values 2731 * 2732 * @returns the number of characters printed 2733 */ 2734int 2735resource_list_print_type(struct resource_list *rl, const char *name, int type, 2736 const char *format) 2737{ 2738 struct resource_list_entry *rle; 2739 int printed, retval; 2740 2741 printed = 0; 2742 retval = 0; 2743 /* Yes, this is kinda cheating */ 2744 STAILQ_FOREACH(rle, rl, link) { 2745 if (rle->type == type) { 2746 if (printed == 0) 2747 retval += printf(" %s ", name); 2748 else 2749 retval += printf(","); 2750 printed++; 2751 retval += printf(format, rle->start); 2752 if (rle->count > 1) { 2753 retval += printf("-"); 2754 retval += printf(format, rle->start + 2755 rle->count - 1); 2756 } 2757 } 2758 } 2759 return (retval); 2760} 2761 2762/** 2763 * @brief Helper function for implementing DEVICE_PROBE() 2764 * 2765 * This function can be used to help implement the DEVICE_PROBE() for 2766 * a bus (i.e. a device which has other devices attached to it). It 2767 * calls the DEVICE_IDENTIFY() method of each driver in the device's 2768 * devclass. 2769 */ 2770int 2771bus_generic_probe(device_t dev) 2772{ 2773 devclass_t dc = dev->devclass; 2774 driverlink_t dl; 2775 2776 TAILQ_FOREACH(dl, &dc->drivers, link) { 2777 DEVICE_IDENTIFY(dl->driver, dev); 2778 } 2779 2780 return (0); 2781} 2782 2783/** 2784 * @brief Helper function for implementing DEVICE_ATTACH() 2785 * 2786 * This function can be used to help implement the DEVICE_ATTACH() for 2787 * a bus. It calls device_probe_and_attach() for each of the device's 2788 * children. 2789 */ 2790int 2791bus_generic_attach(device_t dev) 2792{ 2793 device_t child; 2794 2795 TAILQ_FOREACH(child, &dev->children, link) { 2796 device_probe_and_attach(child); 2797 } 2798 2799 return (0); 2800} 2801 2802/** 2803 * @brief Helper function for implementing DEVICE_DETACH() 2804 * 2805 * This function can be used to help implement the DEVICE_DETACH() for 2806 * a bus. It calls device_detach() for each of the device's 2807 * children. 2808 */ 2809int 2810bus_generic_detach(device_t dev) 2811{ 2812 device_t child; 2813 int error; 2814 2815 if (dev->state != DS_ATTACHED) 2816 return (EBUSY); 2817 2818 TAILQ_FOREACH(child, &dev->children, link) { 2819 if ((error = device_detach(child)) != 0) 2820 return (error); 2821 } 2822 2823 return (0); 2824} 2825 2826/** 2827 * @brief Helper function for implementing DEVICE_SHUTDOWN() 2828 * 2829 * This function can be used to help implement the DEVICE_SHUTDOWN() 2830 * for a bus. It calls device_shutdown() for each of the device's 2831 * children. 2832 */ 2833int 2834bus_generic_shutdown(device_t dev) 2835{ 2836 device_t child; 2837 2838 TAILQ_FOREACH(child, &dev->children, link) { 2839 device_shutdown(child); 2840 } 2841 2842 return (0); 2843} 2844 2845/** 2846 * @brief Helper function for implementing DEVICE_SUSPEND() 2847 * 2848 * This function can be used to help implement the DEVICE_SUSPEND() 2849 * for a bus. It calls DEVICE_SUSPEND() for each of the device's 2850 * children. If any call to DEVICE_SUSPEND() fails, the suspend 2851 * operation is aborted and any devices which were suspended are 2852 * resumed immediately by calling their DEVICE_RESUME() methods. 2853 */ 2854int 2855bus_generic_suspend(device_t dev) 2856{ 2857 int error; 2858 device_t child, child2; 2859 2860 TAILQ_FOREACH(child, &dev->children, link) { 2861 error = DEVICE_SUSPEND(child); 2862 if (error) { 2863 for (child2 = TAILQ_FIRST(&dev->children); 2864 child2 && child2 != child; 2865 child2 = TAILQ_NEXT(child2, link)) 2866 DEVICE_RESUME(child2); 2867 return (error); 2868 } 2869 } 2870 return (0); 2871} 2872 2873/** 2874 * @brief Helper function for implementing DEVICE_RESUME() 2875 * 2876 * This function can be used to help implement the DEVICE_RESUME() for 2877 * a bus. It calls DEVICE_RESUME() on each of the device's children. 2878 */ 2879int 2880bus_generic_resume(device_t dev) 2881{ 2882 device_t child; 2883 2884 TAILQ_FOREACH(child, &dev->children, link) { 2885 DEVICE_RESUME(child); 2886 /* if resume fails, there's nothing we can usefully do... */ 2887 } 2888 return (0); 2889} 2890 2891/** 2892 * @brief Helper function for implementing BUS_PRINT_CHILD(). 2893 * 2894 * This function prints the first part of the ascii representation of 2895 * @p child, including its name, unit and description (if any - see 2896 * device_set_desc()). 2897 * 2898 * @returns the number of characters printed 2899 */ 2900int 2901bus_print_child_header(device_t dev, device_t child) 2902{ 2903 int retval = 0; 2904 2905 if (device_get_desc(child)) { 2906 retval += device_printf(child, "<%s>", device_get_desc(child)); 2907 } else { 2908 retval += printf("%s", device_get_nameunit(child)); 2909 } 2910 2911 return (retval); 2912} 2913 2914/** 2915 * @brief Helper function for implementing BUS_PRINT_CHILD(). 2916 * 2917 * This function prints the last part of the ascii representation of 2918 * @p child, which consists of the string @c " on " followed by the 2919 * name and unit of the @p dev. 2920 * 2921 * @returns the number of characters printed 2922 */ 2923int 2924bus_print_child_footer(device_t dev, device_t child) 2925{ 2926 return (printf(" on %s\n", device_get_nameunit(dev))); 2927} 2928 2929/** 2930 * @brief Helper function for implementing BUS_PRINT_CHILD(). 2931 * 2932 * This function simply calls bus_print_child_header() followed by 2933 * bus_print_child_footer(). 2934 * 2935 * @returns the number of characters printed 2936 */ 2937int 2938bus_generic_print_child(device_t dev, device_t child) 2939{ 2940 int retval = 0; 2941 2942 retval += bus_print_child_header(dev, child); 2943 retval += bus_print_child_footer(dev, child); 2944 2945 return (retval); 2946} 2947 2948/** 2949 * @brief Stub function for implementing BUS_READ_IVAR(). 2950 * 2951 * @returns ENOENT 2952 */ 2953int 2954bus_generic_read_ivar(device_t dev, device_t child, int index, 2955 uintptr_t * result) 2956{ 2957 return (ENOENT); 2958} 2959 2960/** 2961 * @brief Stub function for implementing BUS_WRITE_IVAR(). 2962 * 2963 * @returns ENOENT 2964 */ 2965int 2966bus_generic_write_ivar(device_t dev, device_t child, int index, 2967 uintptr_t value) 2968{ 2969 return (ENOENT); 2970} 2971 2972/** 2973 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST(). 2974 * 2975 * @returns NULL 2976 */ 2977struct resource_list * 2978bus_generic_get_resource_list(device_t dev, device_t child) 2979{ 2980 return (NULL); 2981} 2982 2983/** 2984 * @brief Helper function for implementing BUS_DRIVER_ADDED(). 2985 * 2986 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's 2987 * DEVICE_IDENTIFY() method to allow it to add new children to the bus 2988 * and then calls device_probe_and_attach() for each unattached child. 2989 */ 2990void 2991bus_generic_driver_added(device_t dev, driver_t *driver) 2992{ 2993 device_t child; 2994 2995 DEVICE_IDENTIFY(driver, dev); 2996 TAILQ_FOREACH(child, &dev->children, link) { 2997 if (child->state == DS_NOTPRESENT || 2998 (child->flags & DF_REBID)) 2999 device_probe_and_attach(child); 3000 } 3001} 3002 3003/** 3004 * @brief Helper function for implementing BUS_SETUP_INTR(). 3005 * 3006 * This simple implementation of BUS_SETUP_INTR() simply calls the 3007 * BUS_SETUP_INTR() method of the parent of @p dev. 3008 */ 3009int 3010bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq, 3011 int flags, driver_intr_t *intr, void *arg, void **cookiep) 3012{ 3013 /* Propagate up the bus hierarchy until someone handles it. */ 3014 if (dev->parent) 3015 return (BUS_SETUP_INTR(dev->parent, child, irq, flags, 3016 intr, arg, cookiep)); 3017 return (EINVAL); 3018} 3019 3020/** 3021 * @brief Helper function for implementing BUS_TEARDOWN_INTR(). 3022 * 3023 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the 3024 * BUS_TEARDOWN_INTR() method of the parent of @p dev. 3025 */ 3026int 3027bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq, 3028 void *cookie) 3029{ 3030 /* Propagate up the bus hierarchy until someone handles it. */ 3031 if (dev->parent) 3032 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie)); 3033 return (EINVAL); 3034} 3035 3036/** 3037 * @brief Helper function for implementing BUS_ALLOC_RESOURCE(). 3038 * 3039 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the 3040 * BUS_ALLOC_RESOURCE() method of the parent of @p dev. 3041 */ 3042struct resource * 3043bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid, 3044 u_long start, u_long end, u_long count, u_int flags) 3045{ 3046 /* Propagate up the bus hierarchy until someone handles it. */ 3047 if (dev->parent) 3048 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid, 3049 start, end, count, flags)); 3050 return (NULL); 3051} 3052 3053/** 3054 * @brief Helper function for implementing BUS_RELEASE_RESOURCE(). 3055 * 3056 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the 3057 * BUS_RELEASE_RESOURCE() method of the parent of @p dev. 3058 */ 3059int 3060bus_generic_release_resource(device_t dev, device_t child, int type, int rid, 3061 struct resource *r) 3062{ 3063 /* Propagate up the bus hierarchy until someone handles it. */ 3064 if (dev->parent) 3065 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, 3066 r)); 3067 return (EINVAL); 3068} 3069 3070/** 3071 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE(). 3072 * 3073 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the 3074 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev. 3075 */ 3076int 3077bus_generic_activate_resource(device_t dev, device_t child, int type, int rid, 3078 struct resource *r) 3079{ 3080 /* Propagate up the bus hierarchy until someone handles it. */ 3081 if (dev->parent) 3082 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, 3083 r)); 3084 return (EINVAL); 3085} 3086 3087/** 3088 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE(). 3089 * 3090 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the 3091 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev. 3092 */ 3093int 3094bus_generic_deactivate_resource(device_t dev, device_t child, int type, 3095 int rid, struct resource *r) 3096{ 3097 /* Propagate up the bus hierarchy until someone handles it. */ 3098 if (dev->parent) 3099 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid, 3100 r)); 3101 return (EINVAL); 3102} 3103 3104/** 3105 * @brief Helper function for implementing BUS_CONFIG_INTR(). 3106 * 3107 * This simple implementation of BUS_CONFIG_INTR() simply calls the 3108 * BUS_CONFIG_INTR() method of the parent of @p dev. 3109 */ 3110int 3111bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig, 3112 enum intr_polarity pol) 3113{ 3114 3115 /* Propagate up the bus hierarchy until someone handles it. */ 3116 if (dev->parent) 3117 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol)); 3118 return (EINVAL); 3119} 3120 3121/** 3122 * @brief Helper function for implementing BUS_GET_RESOURCE(). 3123 * 3124 * This implementation of BUS_GET_RESOURCE() uses the 3125 * resource_list_find() function to do most of the work. It calls 3126 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to 3127 * search. 3128 */ 3129int 3130bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid, 3131 u_long *startp, u_long *countp) 3132{ 3133 struct resource_list * rl = NULL; 3134 struct resource_list_entry * rle = NULL; 3135 3136 rl = BUS_GET_RESOURCE_LIST(dev, child); 3137 if (!rl) 3138 return (EINVAL); 3139 3140 rle = resource_list_find(rl, type, rid); 3141 if (!rle) 3142 return (ENOENT); 3143 3144 if (startp) 3145 *startp = rle->start; 3146 if (countp) 3147 *countp = rle->count; 3148 3149 return (0); 3150} 3151 3152/** 3153 * @brief Helper function for implementing BUS_SET_RESOURCE(). 3154 * 3155 * This implementation of BUS_SET_RESOURCE() uses the 3156 * resource_list_add() function to do most of the work. It calls 3157 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to 3158 * edit. 3159 */ 3160int 3161bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid, 3162 u_long start, u_long count) 3163{ 3164 struct resource_list * rl = NULL; 3165 3166 rl = BUS_GET_RESOURCE_LIST(dev, child); 3167 if (!rl) 3168 return (EINVAL); 3169 3170 resource_list_add(rl, type, rid, start, (start + count - 1), count); 3171 3172 return (0); 3173} 3174 3175/** 3176 * @brief Helper function for implementing BUS_DELETE_RESOURCE(). 3177 * 3178 * This implementation of BUS_DELETE_RESOURCE() uses the 3179 * resource_list_delete() function to do most of the work. It calls 3180 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to 3181 * edit. 3182 */ 3183void 3184bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid) 3185{ 3186 struct resource_list * rl = NULL; 3187 3188 rl = BUS_GET_RESOURCE_LIST(dev, child); 3189 if (!rl) 3190 return; 3191 3192 resource_list_delete(rl, type, rid); 3193 3194 return; 3195} 3196 3197/** 3198 * @brief Helper function for implementing BUS_RELEASE_RESOURCE(). 3199 * 3200 * This implementation of BUS_RELEASE_RESOURCE() uses the 3201 * resource_list_release() function to do most of the work. It calls 3202 * BUS_GET_RESOURCE_LIST() to find a suitable resource list. 3203 */ 3204int 3205bus_generic_rl_release_resource(device_t dev, device_t child, int type, 3206 int rid, struct resource *r) 3207{ 3208 struct resource_list * rl = NULL; 3209 3210 rl = BUS_GET_RESOURCE_LIST(dev, child); 3211 if (!rl) 3212 return (EINVAL); 3213 3214 return (resource_list_release(rl, dev, child, type, rid, r)); 3215} 3216 3217/** 3218 * @brief Helper function for implementing BUS_ALLOC_RESOURCE(). 3219 * 3220 * This implementation of BUS_ALLOC_RESOURCE() uses the 3221 * resource_list_alloc() function to do most of the work. It calls 3222 * BUS_GET_RESOURCE_LIST() to find a suitable resource list. 3223 */ 3224struct resource * 3225bus_generic_rl_alloc_resource(device_t dev, device_t child, int type, 3226 int *rid, u_long start, u_long end, u_long count, u_int flags) 3227{ 3228 struct resource_list * rl = NULL; 3229 3230 rl = BUS_GET_RESOURCE_LIST(dev, child); 3231 if (!rl) 3232 return (NULL); 3233 3234 return (resource_list_alloc(rl, dev, child, type, rid, 3235 start, end, count, flags)); 3236} 3237 3238/** 3239 * @brief Helper function for implementing BUS_CHILD_PRESENT(). 3240 * 3241 * This simple implementation of BUS_CHILD_PRESENT() simply calls the 3242 * BUS_CHILD_PRESENT() method of the parent of @p dev. 3243 */ 3244int 3245bus_generic_child_present(device_t dev, device_t child) 3246{ 3247 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev)); 3248} 3249 3250/* 3251 * Some convenience functions to make it easier for drivers to use the 3252 * resource-management functions. All these really do is hide the 3253 * indirection through the parent's method table, making for slightly 3254 * less-wordy code. In the future, it might make sense for this code 3255 * to maintain some sort of a list of resources allocated by each device. 3256 */ 3257 3258/** 3259 * @brief Wrapper function for BUS_ALLOC_RESOURCE(). 3260 * 3261 * This function simply calls the BUS_ALLOC_RESOURCE() method of the 3262 * parent of @p dev. 3263 */ 3264struct resource * 3265bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end, 3266 u_long count, u_int flags) 3267{ 3268 if (dev->parent == 0) 3269 return (0); 3270 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end, 3271 count, flags)); 3272} 3273 3274/** 3275 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE(). 3276 * 3277 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the 3278 * parent of @p dev. 3279 */ 3280int 3281bus_activate_resource(device_t dev, int type, int rid, struct resource *r) 3282{ 3283 if (dev->parent == 0) 3284 return (EINVAL); 3285 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 3286} 3287 3288/** 3289 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE(). 3290 * 3291 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the 3292 * parent of @p dev. 3293 */ 3294int 3295bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r) 3296{ 3297 if (dev->parent == 0) 3298 return (EINVAL); 3299 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 3300} 3301 3302/** 3303 * @brief Wrapper function for BUS_RELEASE_RESOURCE(). 3304 * 3305 * This function simply calls the BUS_RELEASE_RESOURCE() method of the 3306 * parent of @p dev. 3307 */ 3308int 3309bus_release_resource(device_t dev, int type, int rid, struct resource *r) 3310{ 3311 if (dev->parent == 0) 3312 return (EINVAL); 3313 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r)); 3314} 3315 3316/** 3317 * @brief Wrapper function for BUS_SETUP_INTR(). 3318 * 3319 * This function simply calls the BUS_SETUP_INTR() method of the 3320 * parent of @p dev. 3321 */ 3322int 3323bus_setup_intr(device_t dev, struct resource *r, int flags, 3324 driver_intr_t handler, void *arg, void **cookiep) 3325{ 3326 int error; 3327 3328 if (dev->parent != 0) { 3329 if ((flags &~ INTR_ENTROPY) == (INTR_TYPE_NET | INTR_MPSAFE) && 3330 !debug_mpsafenet) 3331 flags &= ~INTR_MPSAFE; 3332 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, 3333 handler, arg, cookiep); 3334 if (error == 0) { 3335 if (!(flags & (INTR_MPSAFE | INTR_FAST))) 3336 device_printf(dev, "[GIANT-LOCKED]\n"); 3337 if (bootverbose && (flags & INTR_MPSAFE)) 3338 device_printf(dev, "[MPSAFE]\n"); 3339 if (flags & INTR_FAST) 3340 device_printf(dev, "[FAST]\n"); 3341 } 3342 } else 3343 error = EINVAL; 3344 return (error); 3345} 3346 3347/** 3348 * @brief Wrapper function for BUS_TEARDOWN_INTR(). 3349 * 3350 * This function simply calls the BUS_TEARDOWN_INTR() method of the 3351 * parent of @p dev. 3352 */ 3353int 3354bus_teardown_intr(device_t dev, struct resource *r, void *cookie) 3355{ 3356 if (dev->parent == 0) 3357 return (EINVAL); 3358 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie)); 3359} 3360 3361/** 3362 * @brief Wrapper function for BUS_SET_RESOURCE(). 3363 * 3364 * This function simply calls the BUS_SET_RESOURCE() method of the 3365 * parent of @p dev. 3366 */ 3367int 3368bus_set_resource(device_t dev, int type, int rid, 3369 u_long start, u_long count) 3370{ 3371 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid, 3372 start, count)); 3373} 3374 3375/** 3376 * @brief Wrapper function for BUS_GET_RESOURCE(). 3377 * 3378 * This function simply calls the BUS_GET_RESOURCE() method of the 3379 * parent of @p dev. 3380 */ 3381int 3382bus_get_resource(device_t dev, int type, int rid, 3383 u_long *startp, u_long *countp) 3384{ 3385 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 3386 startp, countp)); 3387} 3388 3389/** 3390 * @brief Wrapper function for BUS_GET_RESOURCE(). 3391 * 3392 * This function simply calls the BUS_GET_RESOURCE() method of the 3393 * parent of @p dev and returns the start value. 3394 */ 3395u_long 3396bus_get_resource_start(device_t dev, int type, int rid) 3397{ 3398 u_long start, count; 3399 int error; 3400 3401 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 3402 &start, &count); 3403 if (error) 3404 return (0); 3405 return (start); 3406} 3407 3408/** 3409 * @brief Wrapper function for BUS_GET_RESOURCE(). 3410 * 3411 * This function simply calls the BUS_GET_RESOURCE() method of the 3412 * parent of @p dev and returns the count value. 3413 */ 3414u_long 3415bus_get_resource_count(device_t dev, int type, int rid) 3416{ 3417 u_long start, count; 3418 int error; 3419 3420 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 3421 &start, &count); 3422 if (error) 3423 return (0); 3424 return (count); 3425} 3426 3427/** 3428 * @brief Wrapper function for BUS_DELETE_RESOURCE(). 3429 * 3430 * This function simply calls the BUS_DELETE_RESOURCE() method of the 3431 * parent of @p dev. 3432 */ 3433void 3434bus_delete_resource(device_t dev, int type, int rid) 3435{ 3436 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid); 3437} 3438 3439/** 3440 * @brief Wrapper function for BUS_CHILD_PRESENT(). 3441 * 3442 * This function simply calls the BUS_CHILD_PRESENT() method of the 3443 * parent of @p dev. 3444 */ 3445int 3446bus_child_present(device_t child) 3447{ 3448 return (BUS_CHILD_PRESENT(device_get_parent(child), child)); 3449} 3450 3451/** 3452 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR(). 3453 * 3454 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the 3455 * parent of @p dev. 3456 */ 3457int 3458bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen) 3459{ 3460 device_t parent; 3461 3462 parent = device_get_parent(child); 3463 if (parent == NULL) { 3464 *buf = '\0'; 3465 return (0); 3466 } 3467 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen)); 3468} 3469 3470/** 3471 * @brief Wrapper function for BUS_CHILD_LOCATION_STR(). 3472 * 3473 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the 3474 * parent of @p dev. 3475 */ 3476int 3477bus_child_location_str(device_t child, char *buf, size_t buflen) 3478{ 3479 device_t parent; 3480 3481 parent = device_get_parent(child); 3482 if (parent == NULL) { 3483 *buf = '\0'; 3484 return (0); 3485 } 3486 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen)); 3487} 3488 3489static int 3490root_print_child(device_t dev, device_t child) 3491{ 3492 int retval = 0; 3493 3494 retval += bus_print_child_header(dev, child); 3495 retval += printf("\n"); 3496 3497 return (retval); 3498} 3499 3500static int 3501root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg, 3502 void **cookiep) 3503{ 3504 /* 3505 * If an interrupt mapping gets to here something bad has happened. 3506 */ 3507 panic("root_setup_intr"); 3508} 3509 3510/* 3511 * If we get here, assume that the device is permanant and really is 3512 * present in the system. Removable bus drivers are expected to intercept 3513 * this call long before it gets here. We return -1 so that drivers that 3514 * really care can check vs -1 or some ERRNO returned higher in the food 3515 * chain. 3516 */ 3517static int 3518root_child_present(device_t dev, device_t child) 3519{ 3520 return (-1); 3521} 3522 3523static kobj_method_t root_methods[] = { 3524 /* Device interface */ 3525 KOBJMETHOD(device_shutdown, bus_generic_shutdown), 3526 KOBJMETHOD(device_suspend, bus_generic_suspend), 3527 KOBJMETHOD(device_resume, bus_generic_resume), 3528 3529 /* Bus interface */ 3530 KOBJMETHOD(bus_print_child, root_print_child), 3531 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar), 3532 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar), 3533 KOBJMETHOD(bus_setup_intr, root_setup_intr), 3534 KOBJMETHOD(bus_child_present, root_child_present), 3535 3536 { 0, 0 } 3537}; 3538 3539static driver_t root_driver = { 3540 "root", 3541 root_methods, 3542 1, /* no softc */ 3543}; 3544 3545device_t root_bus; 3546devclass_t root_devclass; 3547 3548static int 3549root_bus_module_handler(module_t mod, int what, void* arg) 3550{ 3551 switch (what) { 3552 case MOD_LOAD: 3553 TAILQ_INIT(&bus_data_devices); 3554 kobj_class_compile((kobj_class_t) &root_driver); 3555 root_bus = make_device(NULL, "root", 0); 3556 root_bus->desc = "System root bus"; 3557 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver); 3558 root_bus->driver = &root_driver; 3559 root_bus->state = DS_ATTACHED; 3560 root_devclass = devclass_find_internal("root", 0, FALSE); 3561 devinit(); 3562 return (0); 3563 3564 case MOD_SHUTDOWN: 3565 device_shutdown(root_bus); 3566 return (0); 3567 default: 3568 return (EOPNOTSUPP); 3569 } 3570 3571 return (0); 3572} 3573 3574static moduledata_t root_bus_mod = { 3575 "rootbus", 3576 root_bus_module_handler, 3577 0 3578}; 3579DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 3580 3581/** 3582 * @brief Automatically configure devices 3583 * 3584 * This function begins the autoconfiguration process by calling 3585 * device_probe_and_attach() for each child of the @c root0 device. 3586 */ 3587void 3588root_bus_configure(void) 3589{ 3590 device_t dev; 3591 3592 PDEBUG((".")); 3593 3594 TAILQ_FOREACH(dev, &root_bus->children, link) { 3595 device_probe_and_attach(dev); 3596 } 3597} 3598 3599/** 3600 * @brief Module handler for registering device drivers 3601 * 3602 * This module handler is used to automatically register device 3603 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls 3604 * devclass_add_driver() for the driver described by the 3605 * driver_module_data structure pointed to by @p arg 3606 */ 3607int 3608driver_module_handler(module_t mod, int what, void *arg) 3609{ 3610 int error; 3611 struct driver_module_data *dmd; 3612 devclass_t bus_devclass; 3613 kobj_class_t driver; 3614 3615 dmd = (struct driver_module_data *)arg; 3616 bus_devclass = devclass_find_internal(dmd->dmd_busname, 0, TRUE); 3617 error = 0; 3618 3619 switch (what) { 3620 case MOD_LOAD: 3621 if (dmd->dmd_chainevh) 3622 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 3623 3624 driver = dmd->dmd_driver; 3625 PDEBUG(("Loading module: driver %s on bus %s", 3626 DRIVERNAME(driver), dmd->dmd_busname)); 3627 error = devclass_add_driver(bus_devclass, driver); 3628 if (error) 3629 break; 3630 3631 /* 3632 * If the driver has any base classes, make the 3633 * devclass inherit from the devclass of the driver's 3634 * first base class. This will allow the system to 3635 * search for drivers in both devclasses for children 3636 * of a device using this driver. 3637 */ 3638 if (driver->baseclasses) { 3639 const char *parentname; 3640 parentname = driver->baseclasses[0]->name; 3641 *dmd->dmd_devclass = 3642 devclass_find_internal(driver->name, 3643 parentname, TRUE); 3644 } else { 3645 *dmd->dmd_devclass = 3646 devclass_find_internal(driver->name, 0, TRUE); 3647 } 3648 break; 3649 3650 case MOD_UNLOAD: 3651 PDEBUG(("Unloading module: driver %s from bus %s", 3652 DRIVERNAME(dmd->dmd_driver), 3653 dmd->dmd_busname)); 3654 error = devclass_delete_driver(bus_devclass, 3655 dmd->dmd_driver); 3656 3657 if (!error && dmd->dmd_chainevh) 3658 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 3659 break; 3660 case MOD_QUIESCE: 3661 PDEBUG(("Quiesce module: driver %s from bus %s", 3662 DRIVERNAME(dmd->dmd_driver), 3663 dmd->dmd_busname)); 3664 error = devclass_quiesce_driver(bus_devclass, 3665 dmd->dmd_driver); 3666 3667 if (!error && dmd->dmd_chainevh) 3668 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 3669 break; 3670 default: 3671 error = EOPNOTSUPP; 3672 break; 3673 } 3674 3675 return (error); 3676} 3677 3678#ifdef BUS_DEBUG 3679 3680/* the _short versions avoid iteration by not calling anything that prints 3681 * more than oneliners. I love oneliners. 3682 */ 3683 3684static void 3685print_device_short(device_t dev, int indent) 3686{ 3687 if (!dev) 3688 return; 3689 3690 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n", 3691 dev->unit, dev->desc, 3692 (dev->parent? "":"no "), 3693 (TAILQ_EMPTY(&dev->children)? "no ":""), 3694 (dev->flags&DF_ENABLED? "enabled,":"disabled,"), 3695 (dev->flags&DF_FIXEDCLASS? "fixed,":""), 3696 (dev->flags&DF_WILDCARD? "wildcard,":""), 3697 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""), 3698 (dev->flags&DF_REBID? "rebiddable,":""), 3699 (dev->ivars? "":"no "), 3700 (dev->softc? "":"no "), 3701 dev->busy)); 3702} 3703 3704static void 3705print_device(device_t dev, int indent) 3706{ 3707 if (!dev) 3708 return; 3709 3710 print_device_short(dev, indent); 3711 3712 indentprintf(("Parent:\n")); 3713 print_device_short(dev->parent, indent+1); 3714 indentprintf(("Driver:\n")); 3715 print_driver_short(dev->driver, indent+1); 3716 indentprintf(("Devclass:\n")); 3717 print_devclass_short(dev->devclass, indent+1); 3718} 3719 3720void 3721print_device_tree_short(device_t dev, int indent) 3722/* print the device and all its children (indented) */ 3723{ 3724 device_t child; 3725 3726 if (!dev) 3727 return; 3728 3729 print_device_short(dev, indent); 3730 3731 TAILQ_FOREACH(child, &dev->children, link) { 3732 print_device_tree_short(child, indent+1); 3733 } 3734} 3735 3736void 3737print_device_tree(device_t dev, int indent) 3738/* print the device and all its children (indented) */ 3739{ 3740 device_t child; 3741 3742 if (!dev) 3743 return; 3744 3745 print_device(dev, indent); 3746 3747 TAILQ_FOREACH(child, &dev->children, link) { 3748 print_device_tree(child, indent+1); 3749 } 3750} 3751 3752static void 3753print_driver_short(driver_t *driver, int indent) 3754{ 3755 if (!driver) 3756 return; 3757 3758 indentprintf(("driver %s: softc size = %zd\n", 3759 driver->name, driver->size)); 3760} 3761 3762static void 3763print_driver(driver_t *driver, int indent) 3764{ 3765 if (!driver) 3766 return; 3767 3768 print_driver_short(driver, indent); 3769} 3770 3771 3772static void 3773print_driver_list(driver_list_t drivers, int indent) 3774{ 3775 driverlink_t driver; 3776 3777 TAILQ_FOREACH(driver, &drivers, link) { 3778 print_driver(driver->driver, indent); 3779 } 3780} 3781 3782static void 3783print_devclass_short(devclass_t dc, int indent) 3784{ 3785 if ( !dc ) 3786 return; 3787 3788 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit)); 3789} 3790 3791static void 3792print_devclass(devclass_t dc, int indent) 3793{ 3794 int i; 3795 3796 if ( !dc ) 3797 return; 3798 3799 print_devclass_short(dc, indent); 3800 indentprintf(("Drivers:\n")); 3801 print_driver_list(dc->drivers, indent+1); 3802 3803 indentprintf(("Devices:\n")); 3804 for (i = 0; i < dc->maxunit; i++) 3805 if (dc->devices[i]) 3806 print_device(dc->devices[i], indent+1); 3807} 3808 3809void 3810print_devclass_list_short(void) 3811{ 3812 devclass_t dc; 3813 3814 printf("Short listing of devclasses, drivers & devices:\n"); 3815 TAILQ_FOREACH(dc, &devclasses, link) { 3816 print_devclass_short(dc, 0); 3817 } 3818} 3819 3820void 3821print_devclass_list(void) 3822{ 3823 devclass_t dc; 3824 3825 printf("Full listing of devclasses, drivers & devices:\n"); 3826 TAILQ_FOREACH(dc, &devclasses, link) { 3827 print_devclass(dc, 0); 3828 } 3829} 3830 3831#endif 3832 3833/* 3834 * User-space access to the device tree. 3835 * 3836 * We implement a small set of nodes: 3837 * 3838 * hw.bus Single integer read method to obtain the 3839 * current generation count. 3840 * hw.bus.devices Reads the entire device tree in flat space. 3841 * hw.bus.rman Resource manager interface 3842 * 3843 * We might like to add the ability to scan devclasses and/or drivers to 3844 * determine what else is currently loaded/available. 3845 */ 3846 3847static int 3848sysctl_bus(SYSCTL_HANDLER_ARGS) 3849{ 3850 struct u_businfo ubus; 3851 3852 ubus.ub_version = BUS_USER_VERSION; 3853 ubus.ub_generation = bus_data_generation; 3854 3855 return (SYSCTL_OUT(req, &ubus, sizeof(ubus))); 3856} 3857SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus, 3858 "bus-related data"); 3859 3860static int 3861sysctl_devices(SYSCTL_HANDLER_ARGS) 3862{ 3863 int *name = (int *)arg1; 3864 u_int namelen = arg2; 3865 int index; 3866 struct device *dev; 3867 struct u_device udev; /* XXX this is a bit big */ 3868 int error; 3869 3870 if (namelen != 2) 3871 return (EINVAL); 3872 3873 if (bus_data_generation_check(name[0])) 3874 return (EINVAL); 3875 3876 index = name[1]; 3877 3878 /* 3879 * Scan the list of devices, looking for the requested index. 3880 */ 3881 TAILQ_FOREACH(dev, &bus_data_devices, devlink) { 3882 if (index-- == 0) 3883 break; 3884 } 3885 if (dev == NULL) 3886 return (ENOENT); 3887 3888 /* 3889 * Populate the return array. 3890 */ 3891 udev.dv_handle = (uintptr_t)dev; 3892 udev.dv_parent = (uintptr_t)dev->parent; 3893 if (dev->nameunit == NULL) 3894 udev.dv_name[0] = '\0'; 3895 else 3896 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name)); 3897 3898 if (dev->desc == NULL) 3899 udev.dv_desc[0] = '\0'; 3900 else 3901 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc)); 3902 if (dev->driver == NULL || dev->driver->name == NULL) 3903 udev.dv_drivername[0] = '\0'; 3904 else 3905 strlcpy(udev.dv_drivername, dev->driver->name, 3906 sizeof(udev.dv_drivername)); 3907 udev.dv_pnpinfo[0] = '\0'; 3908 udev.dv_location[0] = '\0'; 3909 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo)); 3910 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location)); 3911 udev.dv_devflags = dev->devflags; 3912 udev.dv_flags = dev->flags; 3913 udev.dv_state = dev->state; 3914 error = SYSCTL_OUT(req, &udev, sizeof(udev)); 3915 return (error); 3916} 3917 3918SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices, 3919 "system device tree"); 3920 3921int 3922bus_data_generation_check(int generation) 3923{ 3924 if (generation != bus_data_generation) 3925 return (1); 3926 3927 /* XXX generate optimised lists here? */ 3928 return (0); 3929} 3930 3931void 3932bus_data_generation_update(void) 3933{ 3934 bus_data_generation++; 3935} 3936 3937int 3938bus_free_resource(device_t dev, int type, struct resource *r) 3939{ 3940 if (r == NULL) 3941 return (0); 3942 return (bus_release_resource(dev, type, rman_get_rid(r), r)); 3943} 3944