subr_bus.c revision 263754
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 263754 2014-03-25 23:19:45Z mjg $"); 29 30#include "opt_bus.h" 31#include "opt_random.h" 32 33#include <sys/param.h> 34#include <sys/conf.h> 35#include <sys/filio.h> 36#include <sys/lock.h> 37#include <sys/kernel.h> 38#include <sys/kobj.h> 39#include <sys/limits.h> 40#include <sys/malloc.h> 41#include <sys/module.h> 42#include <sys/mutex.h> 43#include <sys/poll.h> 44#include <sys/proc.h> 45#include <sys/condvar.h> 46#include <sys/queue.h> 47#include <machine/bus.h> 48#include <sys/random.h> 49#include <sys/rman.h> 50#include <sys/selinfo.h> 51#include <sys/signalvar.h> 52#include <sys/sysctl.h> 53#include <sys/systm.h> 54#include <sys/uio.h> 55#include <sys/bus.h> 56#include <sys/interrupt.h> 57 58#include <net/vnet.h> 59 60#include <machine/cpu.h> 61#include <machine/stdarg.h> 62 63#include <vm/uma.h> 64 65SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL); 66SYSCTL_NODE(, OID_AUTO, dev, CTLFLAG_RW, NULL, NULL); 67 68/* 69 * Used to attach drivers to devclasses. 70 */ 71typedef struct driverlink *driverlink_t; 72struct driverlink { 73 kobj_class_t driver; 74 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */ 75 int pass; 76 TAILQ_ENTRY(driverlink) passlink; 77}; 78 79/* 80 * Forward declarations 81 */ 82typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t; 83typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t; 84typedef TAILQ_HEAD(device_list, device) device_list_t; 85 86struct devclass { 87 TAILQ_ENTRY(devclass) link; 88 devclass_t parent; /* parent in devclass hierarchy */ 89 driver_list_t drivers; /* bus devclasses store drivers for bus */ 90 char *name; 91 device_t *devices; /* array of devices indexed by unit */ 92 int maxunit; /* size of devices array */ 93 int flags; 94#define DC_HAS_CHILDREN 1 95 96 struct sysctl_ctx_list sysctl_ctx; 97 struct sysctl_oid *sysctl_tree; 98}; 99 100/** 101 * @brief Implementation of device. 102 */ 103struct device { 104 /* 105 * A device is a kernel object. The first field must be the 106 * current ops table for the object. 107 */ 108 KOBJ_FIELDS; 109 110 /* 111 * Device hierarchy. 112 */ 113 TAILQ_ENTRY(device) link; /**< list of devices in parent */ 114 TAILQ_ENTRY(device) devlink; /**< global device list membership */ 115 device_t parent; /**< parent of this device */ 116 device_list_t children; /**< list of child devices */ 117 118 /* 119 * Details of this device. 120 */ 121 driver_t *driver; /**< current driver */ 122 devclass_t devclass; /**< current device class */ 123 int unit; /**< current unit number */ 124 char* nameunit; /**< name+unit e.g. foodev0 */ 125 char* desc; /**< driver specific description */ 126 int busy; /**< count of calls to device_busy() */ 127 device_state_t state; /**< current device state */ 128 uint32_t devflags; /**< api level flags for device_get_flags() */ 129 u_int flags; /**< internal device flags */ 130#define DF_ENABLED 0x01 /* device should be probed/attached */ 131#define DF_FIXEDCLASS 0x02 /* devclass specified at create time */ 132#define DF_WILDCARD 0x04 /* unit was originally wildcard */ 133#define DF_DESCMALLOCED 0x08 /* description was malloced */ 134#define DF_QUIET 0x10 /* don't print verbose attach message */ 135#define DF_DONENOMATCH 0x20 /* don't execute DEVICE_NOMATCH again */ 136#define DF_EXTERNALSOFTC 0x40 /* softc not allocated by us */ 137#define DF_REBID 0x80 /* Can rebid after attach */ 138 u_int order; /**< order from device_add_child_ordered() */ 139 void *ivars; /**< instance variables */ 140 void *softc; /**< current driver's variables */ 141 142 struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables */ 143 struct sysctl_oid *sysctl_tree; /**< state for sysctl variables */ 144}; 145 146static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures"); 147static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc"); 148 149#ifdef BUS_DEBUG 150 151static int bus_debug = 1; 152TUNABLE_INT("bus.debug", &bus_debug); 153SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RW, &bus_debug, 0, 154 "Debug bus code"); 155 156#define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");} 157#define DEVICENAME(d) ((d)? device_get_name(d): "no device") 158#define DRIVERNAME(d) ((d)? d->name : "no driver") 159#define DEVCLANAME(d) ((d)? d->name : "no devclass") 160 161/** 162 * Produce the indenting, indent*2 spaces plus a '.' ahead of that to 163 * prevent syslog from deleting initial spaces 164 */ 165#define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0) 166 167static void print_device_short(device_t dev, int indent); 168static void print_device(device_t dev, int indent); 169void print_device_tree_short(device_t dev, int indent); 170void print_device_tree(device_t dev, int indent); 171static void print_driver_short(driver_t *driver, int indent); 172static void print_driver(driver_t *driver, int indent); 173static void print_driver_list(driver_list_t drivers, int indent); 174static void print_devclass_short(devclass_t dc, int indent); 175static void print_devclass(devclass_t dc, int indent); 176void print_devclass_list_short(void); 177void print_devclass_list(void); 178 179#else 180/* Make the compiler ignore the function calls */ 181#define PDEBUG(a) /* nop */ 182#define DEVICENAME(d) /* nop */ 183#define DRIVERNAME(d) /* nop */ 184#define DEVCLANAME(d) /* nop */ 185 186#define print_device_short(d,i) /* nop */ 187#define print_device(d,i) /* nop */ 188#define print_device_tree_short(d,i) /* nop */ 189#define print_device_tree(d,i) /* nop */ 190#define print_driver_short(d,i) /* nop */ 191#define print_driver(d,i) /* nop */ 192#define print_driver_list(d,i) /* nop */ 193#define print_devclass_short(d,i) /* nop */ 194#define print_devclass(d,i) /* nop */ 195#define print_devclass_list_short() /* nop */ 196#define print_devclass_list() /* nop */ 197#endif 198 199/* 200 * dev sysctl tree 201 */ 202 203enum { 204 DEVCLASS_SYSCTL_PARENT, 205}; 206 207static int 208devclass_sysctl_handler(SYSCTL_HANDLER_ARGS) 209{ 210 devclass_t dc = (devclass_t)arg1; 211 const char *value; 212 213 switch (arg2) { 214 case DEVCLASS_SYSCTL_PARENT: 215 value = dc->parent ? dc->parent->name : ""; 216 break; 217 default: 218 return (EINVAL); 219 } 220 return (SYSCTL_OUT(req, value, strlen(value))); 221} 222 223static void 224devclass_sysctl_init(devclass_t dc) 225{ 226 227 if (dc->sysctl_tree != NULL) 228 return; 229 sysctl_ctx_init(&dc->sysctl_ctx); 230 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx, 231 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name, 232 CTLFLAG_RD, NULL, ""); 233 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree), 234 OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD, 235 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A", 236 "parent class"); 237} 238 239enum { 240 DEVICE_SYSCTL_DESC, 241 DEVICE_SYSCTL_DRIVER, 242 DEVICE_SYSCTL_LOCATION, 243 DEVICE_SYSCTL_PNPINFO, 244 DEVICE_SYSCTL_PARENT, 245}; 246 247static int 248device_sysctl_handler(SYSCTL_HANDLER_ARGS) 249{ 250 device_t dev = (device_t)arg1; 251 const char *value; 252 char *buf; 253 int error; 254 255 buf = NULL; 256 switch (arg2) { 257 case DEVICE_SYSCTL_DESC: 258 value = dev->desc ? dev->desc : ""; 259 break; 260 case DEVICE_SYSCTL_DRIVER: 261 value = dev->driver ? dev->driver->name : ""; 262 break; 263 case DEVICE_SYSCTL_LOCATION: 264 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO); 265 bus_child_location_str(dev, buf, 1024); 266 break; 267 case DEVICE_SYSCTL_PNPINFO: 268 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO); 269 bus_child_pnpinfo_str(dev, buf, 1024); 270 break; 271 case DEVICE_SYSCTL_PARENT: 272 value = dev->parent ? dev->parent->nameunit : ""; 273 break; 274 default: 275 return (EINVAL); 276 } 277 error = SYSCTL_OUT(req, value, strlen(value)); 278 if (buf != NULL) 279 free(buf, M_BUS); 280 return (error); 281} 282 283static void 284device_sysctl_init(device_t dev) 285{ 286 devclass_t dc = dev->devclass; 287 288 if (dev->sysctl_tree != NULL) 289 return; 290 devclass_sysctl_init(dc); 291 sysctl_ctx_init(&dev->sysctl_ctx); 292 dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx, 293 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO, 294 dev->nameunit + strlen(dc->name), 295 CTLFLAG_RD, NULL, ""); 296 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 297 OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD, 298 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A", 299 "device description"); 300 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 301 OID_AUTO, "%driver", CTLTYPE_STRING | CTLFLAG_RD, 302 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A", 303 "device driver name"); 304 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 305 OID_AUTO, "%location", CTLTYPE_STRING | CTLFLAG_RD, 306 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A", 307 "device location relative to parent"); 308 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 309 OID_AUTO, "%pnpinfo", CTLTYPE_STRING | CTLFLAG_RD, 310 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A", 311 "device identification"); 312 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 313 OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD, 314 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A", 315 "parent device"); 316} 317 318static void 319device_sysctl_update(device_t dev) 320{ 321 devclass_t dc = dev->devclass; 322 323 if (dev->sysctl_tree == NULL) 324 return; 325 sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name)); 326} 327 328static void 329device_sysctl_fini(device_t dev) 330{ 331 if (dev->sysctl_tree == NULL) 332 return; 333 sysctl_ctx_free(&dev->sysctl_ctx); 334 dev->sysctl_tree = NULL; 335} 336 337/* 338 * /dev/devctl implementation 339 */ 340 341/* 342 * This design allows only one reader for /dev/devctl. This is not desirable 343 * in the long run, but will get a lot of hair out of this implementation. 344 * Maybe we should make this device a clonable device. 345 * 346 * Also note: we specifically do not attach a device to the device_t tree 347 * to avoid potential chicken and egg problems. One could argue that all 348 * of this belongs to the root node. One could also further argue that the 349 * sysctl interface that we have not might more properly be an ioctl 350 * interface, but at this stage of the game, I'm not inclined to rock that 351 * boat. 352 * 353 * I'm also not sure that the SIGIO support is done correctly or not, as 354 * I copied it from a driver that had SIGIO support that likely hasn't been 355 * tested since 3.4 or 2.2.8! 356 */ 357 358#define DEVCTL_DEFAULT_QUEUE_LEN 1000 359static int sysctl_devctl_queue(SYSCTL_HANDLER_ARGS); 360static int devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN; 361TUNABLE_INT("hw.bus.devctl_queue", &devctl_queue_length); 362SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_queue, CTLTYPE_INT | CTLFLAG_RW | 363 CTLFLAG_MPSAFE, NULL, 0, sysctl_devctl_queue, "I", "devctl queue length"); 364 365static d_open_t devopen; 366static d_close_t devclose; 367static d_read_t devread; 368static d_ioctl_t devioctl; 369static d_poll_t devpoll; 370 371static struct cdevsw dev_cdevsw = { 372 .d_version = D_VERSION, 373 .d_open = devopen, 374 .d_close = devclose, 375 .d_read = devread, 376 .d_ioctl = devioctl, 377 .d_poll = devpoll, 378 .d_name = "devctl", 379}; 380 381struct dev_event_info 382{ 383 char *dei_data; 384 TAILQ_ENTRY(dev_event_info) dei_link; 385}; 386 387TAILQ_HEAD(devq, dev_event_info); 388 389static struct dev_softc 390{ 391 int inuse; 392 int nonblock; 393 int queued; 394 struct mtx mtx; 395 struct cv cv; 396 struct selinfo sel; 397 struct devq devq; 398 struct proc *async_proc; 399} devsoftc; 400 401static struct cdev *devctl_dev; 402 403static void 404devinit(void) 405{ 406 devctl_dev = make_dev_credf(MAKEDEV_ETERNAL, &dev_cdevsw, 0, NULL, 407 UID_ROOT, GID_WHEEL, 0600, "devctl"); 408 mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF); 409 cv_init(&devsoftc.cv, "dev cv"); 410 TAILQ_INIT(&devsoftc.devq); 411} 412 413static int 414devopen(struct cdev *dev, int oflags, int devtype, struct thread *td) 415{ 416 417 mtx_lock(&devsoftc.mtx); 418 if (devsoftc.inuse) { 419 mtx_unlock(&devsoftc.mtx); 420 return (EBUSY); 421 } 422 /* move to init */ 423 devsoftc.inuse = 1; 424 devsoftc.nonblock = 0; 425 devsoftc.async_proc = NULL; 426 mtx_unlock(&devsoftc.mtx); 427 return (0); 428} 429 430static int 431devclose(struct cdev *dev, int fflag, int devtype, struct thread *td) 432{ 433 434 mtx_lock(&devsoftc.mtx); 435 devsoftc.inuse = 0; 436 devsoftc.async_proc = NULL; 437 cv_broadcast(&devsoftc.cv); 438 mtx_unlock(&devsoftc.mtx); 439 return (0); 440} 441 442/* 443 * The read channel for this device is used to report changes to 444 * userland in realtime. We are required to free the data as well as 445 * the n1 object because we allocate them separately. Also note that 446 * we return one record at a time. If you try to read this device a 447 * character at a time, you will lose the rest of the data. Listening 448 * programs are expected to cope. 449 */ 450static int 451devread(struct cdev *dev, struct uio *uio, int ioflag) 452{ 453 struct dev_event_info *n1; 454 int rv; 455 456 mtx_lock(&devsoftc.mtx); 457 while (TAILQ_EMPTY(&devsoftc.devq)) { 458 if (devsoftc.nonblock) { 459 mtx_unlock(&devsoftc.mtx); 460 return (EAGAIN); 461 } 462 rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx); 463 if (rv) { 464 /* 465 * Need to translate ERESTART to EINTR here? -- jake 466 */ 467 mtx_unlock(&devsoftc.mtx); 468 return (rv); 469 } 470 } 471 n1 = TAILQ_FIRST(&devsoftc.devq); 472 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link); 473 devsoftc.queued--; 474 mtx_unlock(&devsoftc.mtx); 475 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio); 476 free(n1->dei_data, M_BUS); 477 free(n1, M_BUS); 478 return (rv); 479} 480 481static int 482devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 483{ 484 switch (cmd) { 485 486 case FIONBIO: 487 if (*(int*)data) 488 devsoftc.nonblock = 1; 489 else 490 devsoftc.nonblock = 0; 491 return (0); 492 case FIOASYNC: 493 if (*(int*)data) 494 devsoftc.async_proc = td->td_proc; 495 else 496 devsoftc.async_proc = NULL; 497 return (0); 498 499 /* (un)Support for other fcntl() calls. */ 500 case FIOCLEX: 501 case FIONCLEX: 502 case FIONREAD: 503 case FIOSETOWN: 504 case FIOGETOWN: 505 default: 506 break; 507 } 508 return (ENOTTY); 509} 510 511static int 512devpoll(struct cdev *dev, int events, struct thread *td) 513{ 514 int revents = 0; 515 516 mtx_lock(&devsoftc.mtx); 517 if (events & (POLLIN | POLLRDNORM)) { 518 if (!TAILQ_EMPTY(&devsoftc.devq)) 519 revents = events & (POLLIN | POLLRDNORM); 520 else 521 selrecord(td, &devsoftc.sel); 522 } 523 mtx_unlock(&devsoftc.mtx); 524 525 return (revents); 526} 527 528/** 529 * @brief Return whether the userland process is running 530 */ 531boolean_t 532devctl_process_running(void) 533{ 534 return (devsoftc.inuse == 1); 535} 536 537/** 538 * @brief Queue data to be read from the devctl device 539 * 540 * Generic interface to queue data to the devctl device. It is 541 * assumed that @p data is properly formatted. It is further assumed 542 * that @p data is allocated using the M_BUS malloc type. 543 */ 544void 545devctl_queue_data_f(char *data, int flags) 546{ 547 struct dev_event_info *n1 = NULL, *n2 = NULL; 548 struct proc *p; 549 550 if (strlen(data) == 0) 551 goto out; 552 if (devctl_queue_length == 0) 553 goto out; 554 n1 = malloc(sizeof(*n1), M_BUS, flags); 555 if (n1 == NULL) 556 goto out; 557 n1->dei_data = data; 558 mtx_lock(&devsoftc.mtx); 559 if (devctl_queue_length == 0) { 560 mtx_unlock(&devsoftc.mtx); 561 free(n1->dei_data, M_BUS); 562 free(n1, M_BUS); 563 return; 564 } 565 /* Leave at least one spot in the queue... */ 566 while (devsoftc.queued > devctl_queue_length - 1) { 567 n2 = TAILQ_FIRST(&devsoftc.devq); 568 TAILQ_REMOVE(&devsoftc.devq, n2, dei_link); 569 free(n2->dei_data, M_BUS); 570 free(n2, M_BUS); 571 devsoftc.queued--; 572 } 573 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link); 574 devsoftc.queued++; 575 cv_broadcast(&devsoftc.cv); 576 mtx_unlock(&devsoftc.mtx); 577 selwakeup(&devsoftc.sel); 578 p = devsoftc.async_proc; 579 if (p != NULL) { 580 PROC_LOCK(p); 581 kern_psignal(p, SIGIO); 582 PROC_UNLOCK(p); 583 } 584 return; 585out: 586 /* 587 * We have to free data on all error paths since the caller 588 * assumes it will be free'd when this item is dequeued. 589 */ 590 free(data, M_BUS); 591 return; 592} 593 594void 595devctl_queue_data(char *data) 596{ 597 598 devctl_queue_data_f(data, M_NOWAIT); 599} 600 601/** 602 * @brief Send a 'notification' to userland, using standard ways 603 */ 604void 605devctl_notify_f(const char *system, const char *subsystem, const char *type, 606 const char *data, int flags) 607{ 608 int len = 0; 609 char *msg; 610 611 if (system == NULL) 612 return; /* BOGUS! Must specify system. */ 613 if (subsystem == NULL) 614 return; /* BOGUS! Must specify subsystem. */ 615 if (type == NULL) 616 return; /* BOGUS! Must specify type. */ 617 len += strlen(" system=") + strlen(system); 618 len += strlen(" subsystem=") + strlen(subsystem); 619 len += strlen(" type=") + strlen(type); 620 /* add in the data message plus newline. */ 621 if (data != NULL) 622 len += strlen(data); 623 len += 3; /* '!', '\n', and NUL */ 624 msg = malloc(len, M_BUS, flags); 625 if (msg == NULL) 626 return; /* Drop it on the floor */ 627 if (data != NULL) 628 snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n", 629 system, subsystem, type, data); 630 else 631 snprintf(msg, len, "!system=%s subsystem=%s type=%s\n", 632 system, subsystem, type); 633 devctl_queue_data_f(msg, flags); 634} 635 636void 637devctl_notify(const char *system, const char *subsystem, const char *type, 638 const char *data) 639{ 640 641 devctl_notify_f(system, subsystem, type, data, M_NOWAIT); 642} 643 644/* 645 * Common routine that tries to make sending messages as easy as possible. 646 * We allocate memory for the data, copy strings into that, but do not 647 * free it unless there's an error. The dequeue part of the driver should 648 * free the data. We don't send data when queue length is 0. We do send 649 * data, even when we have no listeners, because we wish to avoid races 650 * relating to startup and restart of listening applications. 651 * 652 * devaddq is designed to string together the type of event, with the 653 * object of that event, plus the plug and play info and location info 654 * for that event. This is likely most useful for devices, but less 655 * useful for other consumers of this interface. Those should use 656 * the devctl_queue_data() interface instead. 657 */ 658static void 659devaddq(const char *type, const char *what, device_t dev) 660{ 661 char *data = NULL; 662 char *loc = NULL; 663 char *pnp = NULL; 664 const char *parstr; 665 666 if (!devctl_queue_length)/* Rare race, but lost races safely discard */ 667 return; 668 data = malloc(1024, M_BUS, M_NOWAIT); 669 if (data == NULL) 670 goto bad; 671 672 /* get the bus specific location of this device */ 673 loc = malloc(1024, M_BUS, M_NOWAIT); 674 if (loc == NULL) 675 goto bad; 676 *loc = '\0'; 677 bus_child_location_str(dev, loc, 1024); 678 679 /* Get the bus specific pnp info of this device */ 680 pnp = malloc(1024, M_BUS, M_NOWAIT); 681 if (pnp == NULL) 682 goto bad; 683 *pnp = '\0'; 684 bus_child_pnpinfo_str(dev, pnp, 1024); 685 686 /* Get the parent of this device, or / if high enough in the tree. */ 687 if (device_get_parent(dev) == NULL) 688 parstr = "."; /* Or '/' ? */ 689 else 690 parstr = device_get_nameunit(device_get_parent(dev)); 691 /* String it all together. */ 692 snprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp, 693 parstr); 694 free(loc, M_BUS); 695 free(pnp, M_BUS); 696 devctl_queue_data(data); 697 return; 698bad: 699 free(pnp, M_BUS); 700 free(loc, M_BUS); 701 free(data, M_BUS); 702 return; 703} 704 705/* 706 * A device was added to the tree. We are called just after it successfully 707 * attaches (that is, probe and attach success for this device). No call 708 * is made if a device is merely parented into the tree. See devnomatch 709 * if probe fails. If attach fails, no notification is sent (but maybe 710 * we should have a different message for this). 711 */ 712static void 713devadded(device_t dev) 714{ 715 devaddq("+", device_get_nameunit(dev), dev); 716} 717 718/* 719 * A device was removed from the tree. We are called just before this 720 * happens. 721 */ 722static void 723devremoved(device_t dev) 724{ 725 devaddq("-", device_get_nameunit(dev), dev); 726} 727 728/* 729 * Called when there's no match for this device. This is only called 730 * the first time that no match happens, so we don't keep getting this 731 * message. Should that prove to be undesirable, we can change it. 732 * This is called when all drivers that can attach to a given bus 733 * decline to accept this device. Other errors may not be detected. 734 */ 735static void 736devnomatch(device_t dev) 737{ 738 devaddq("?", "", dev); 739} 740 741static int 742sysctl_devctl_queue(SYSCTL_HANDLER_ARGS) 743{ 744 struct dev_event_info *n1; 745 int q, error; 746 747 q = devctl_queue_length; 748 error = sysctl_handle_int(oidp, &q, 0, req); 749 if (error || !req->newptr) 750 return (error); 751 if (q < 0) 752 return (EINVAL); 753 mtx_lock(&devsoftc.mtx); 754 devctl_queue_length = q; 755 while (devsoftc.queued > devctl_queue_length) { 756 n1 = TAILQ_FIRST(&devsoftc.devq); 757 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link); 758 free(n1->dei_data, M_BUS); 759 free(n1, M_BUS); 760 devsoftc.queued--; 761 } 762 mtx_unlock(&devsoftc.mtx); 763 return (0); 764} 765 766/* End of /dev/devctl code */ 767 768static TAILQ_HEAD(,device) bus_data_devices; 769static int bus_data_generation = 1; 770 771static kobj_method_t null_methods[] = { 772 KOBJMETHOD_END 773}; 774 775DEFINE_CLASS(null, null_methods, 0); 776 777/* 778 * Bus pass implementation 779 */ 780 781static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes); 782int bus_current_pass = BUS_PASS_ROOT; 783 784/** 785 * @internal 786 * @brief Register the pass level of a new driver attachment 787 * 788 * Register a new driver attachment's pass level. If no driver 789 * attachment with the same pass level has been added, then @p new 790 * will be added to the global passes list. 791 * 792 * @param new the new driver attachment 793 */ 794static void 795driver_register_pass(struct driverlink *new) 796{ 797 struct driverlink *dl; 798 799 /* We only consider pass numbers during boot. */ 800 if (bus_current_pass == BUS_PASS_DEFAULT) 801 return; 802 803 /* 804 * Walk the passes list. If we already know about this pass 805 * then there is nothing to do. If we don't, then insert this 806 * driver link into the list. 807 */ 808 TAILQ_FOREACH(dl, &passes, passlink) { 809 if (dl->pass < new->pass) 810 continue; 811 if (dl->pass == new->pass) 812 return; 813 TAILQ_INSERT_BEFORE(dl, new, passlink); 814 return; 815 } 816 TAILQ_INSERT_TAIL(&passes, new, passlink); 817} 818 819/** 820 * @brief Raise the current bus pass 821 * 822 * Raise the current bus pass level to @p pass. Call the BUS_NEW_PASS() 823 * method on the root bus to kick off a new device tree scan for each 824 * new pass level that has at least one driver. 825 */ 826void 827bus_set_pass(int pass) 828{ 829 struct driverlink *dl; 830 831 if (bus_current_pass > pass) 832 panic("Attempt to lower bus pass level"); 833 834 TAILQ_FOREACH(dl, &passes, passlink) { 835 /* Skip pass values below the current pass level. */ 836 if (dl->pass <= bus_current_pass) 837 continue; 838 839 /* 840 * Bail once we hit a driver with a pass level that is 841 * too high. 842 */ 843 if (dl->pass > pass) 844 break; 845 846 /* 847 * Raise the pass level to the next level and rescan 848 * the tree. 849 */ 850 bus_current_pass = dl->pass; 851 BUS_NEW_PASS(root_bus); 852 } 853 854 /* 855 * If there isn't a driver registered for the requested pass, 856 * then bus_current_pass might still be less than 'pass'. Set 857 * it to 'pass' in that case. 858 */ 859 if (bus_current_pass < pass) 860 bus_current_pass = pass; 861 KASSERT(bus_current_pass == pass, ("Failed to update bus pass level")); 862} 863 864/* 865 * Devclass implementation 866 */ 867 868static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses); 869 870/** 871 * @internal 872 * @brief Find or create a device class 873 * 874 * If a device class with the name @p classname exists, return it, 875 * otherwise if @p create is non-zero create and return a new device 876 * class. 877 * 878 * If @p parentname is non-NULL, the parent of the devclass is set to 879 * the devclass of that name. 880 * 881 * @param classname the devclass name to find or create 882 * @param parentname the parent devclass name or @c NULL 883 * @param create non-zero to create a devclass 884 */ 885static devclass_t 886devclass_find_internal(const char *classname, const char *parentname, 887 int create) 888{ 889 devclass_t dc; 890 891 PDEBUG(("looking for %s", classname)); 892 if (!classname) 893 return (NULL); 894 895 TAILQ_FOREACH(dc, &devclasses, link) { 896 if (!strcmp(dc->name, classname)) 897 break; 898 } 899 900 if (create && !dc) { 901 PDEBUG(("creating %s", classname)); 902 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1, 903 M_BUS, M_NOWAIT | M_ZERO); 904 if (!dc) 905 return (NULL); 906 dc->parent = NULL; 907 dc->name = (char*) (dc + 1); 908 strcpy(dc->name, classname); 909 TAILQ_INIT(&dc->drivers); 910 TAILQ_INSERT_TAIL(&devclasses, dc, link); 911 912 bus_data_generation_update(); 913 } 914 915 /* 916 * If a parent class is specified, then set that as our parent so 917 * that this devclass will support drivers for the parent class as 918 * well. If the parent class has the same name don't do this though 919 * as it creates a cycle that can trigger an infinite loop in 920 * device_probe_child() if a device exists for which there is no 921 * suitable driver. 922 */ 923 if (parentname && dc && !dc->parent && 924 strcmp(classname, parentname) != 0) { 925 dc->parent = devclass_find_internal(parentname, NULL, TRUE); 926 dc->parent->flags |= DC_HAS_CHILDREN; 927 } 928 929 return (dc); 930} 931 932/** 933 * @brief Create a device class 934 * 935 * If a device class with the name @p classname exists, return it, 936 * otherwise create and return a new device class. 937 * 938 * @param classname the devclass name to find or create 939 */ 940devclass_t 941devclass_create(const char *classname) 942{ 943 return (devclass_find_internal(classname, NULL, TRUE)); 944} 945 946/** 947 * @brief Find a device class 948 * 949 * If a device class with the name @p classname exists, return it, 950 * otherwise return @c NULL. 951 * 952 * @param classname the devclass name to find 953 */ 954devclass_t 955devclass_find(const char *classname) 956{ 957 return (devclass_find_internal(classname, NULL, FALSE)); 958} 959 960/** 961 * @brief Register that a device driver has been added to a devclass 962 * 963 * Register that a device driver has been added to a devclass. This 964 * is called by devclass_add_driver to accomplish the recursive 965 * notification of all the children classes of dc, as well as dc. 966 * Each layer will have BUS_DRIVER_ADDED() called for all instances of 967 * the devclass. 968 * 969 * We do a full search here of the devclass list at each iteration 970 * level to save storing children-lists in the devclass structure. If 971 * we ever move beyond a few dozen devices doing this, we may need to 972 * reevaluate... 973 * 974 * @param dc the devclass to edit 975 * @param driver the driver that was just added 976 */ 977static void 978devclass_driver_added(devclass_t dc, driver_t *driver) 979{ 980 devclass_t parent; 981 int i; 982 983 /* 984 * Call BUS_DRIVER_ADDED for any existing busses in this class. 985 */ 986 for (i = 0; i < dc->maxunit; i++) 987 if (dc->devices[i] && device_is_attached(dc->devices[i])) 988 BUS_DRIVER_ADDED(dc->devices[i], driver); 989 990 /* 991 * Walk through the children classes. Since we only keep a 992 * single parent pointer around, we walk the entire list of 993 * devclasses looking for children. We set the 994 * DC_HAS_CHILDREN flag when a child devclass is created on 995 * the parent, so we only walk the list for those devclasses 996 * that have children. 997 */ 998 if (!(dc->flags & DC_HAS_CHILDREN)) 999 return; 1000 parent = dc; 1001 TAILQ_FOREACH(dc, &devclasses, link) { 1002 if (dc->parent == parent) 1003 devclass_driver_added(dc, driver); 1004 } 1005} 1006 1007/** 1008 * @brief Add a device driver to a device class 1009 * 1010 * Add a device driver to a devclass. This is normally called 1011 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of 1012 * all devices in the devclass will be called to allow them to attempt 1013 * to re-probe any unmatched children. 1014 * 1015 * @param dc the devclass to edit 1016 * @param driver the driver to register 1017 */ 1018int 1019devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp) 1020{ 1021 driverlink_t dl; 1022 const char *parentname; 1023 1024 PDEBUG(("%s", DRIVERNAME(driver))); 1025 1026 /* Don't allow invalid pass values. */ 1027 if (pass <= BUS_PASS_ROOT) 1028 return (EINVAL); 1029 1030 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO); 1031 if (!dl) 1032 return (ENOMEM); 1033 1034 /* 1035 * Compile the driver's methods. Also increase the reference count 1036 * so that the class doesn't get freed when the last instance 1037 * goes. This means we can safely use static methods and avoids a 1038 * double-free in devclass_delete_driver. 1039 */ 1040 kobj_class_compile((kobj_class_t) driver); 1041 1042 /* 1043 * If the driver has any base classes, make the 1044 * devclass inherit from the devclass of the driver's 1045 * first base class. This will allow the system to 1046 * search for drivers in both devclasses for children 1047 * of a device using this driver. 1048 */ 1049 if (driver->baseclasses) 1050 parentname = driver->baseclasses[0]->name; 1051 else 1052 parentname = NULL; 1053 *dcp = devclass_find_internal(driver->name, parentname, TRUE); 1054 1055 dl->driver = driver; 1056 TAILQ_INSERT_TAIL(&dc->drivers, dl, link); 1057 driver->refs++; /* XXX: kobj_mtx */ 1058 dl->pass = pass; 1059 driver_register_pass(dl); 1060 1061 devclass_driver_added(dc, driver); 1062 bus_data_generation_update(); 1063 return (0); 1064} 1065 1066/** 1067 * @brief Register that a device driver has been deleted from a devclass 1068 * 1069 * Register that a device driver has been removed from a devclass. 1070 * This is called by devclass_delete_driver to accomplish the 1071 * recursive notification of all the children classes of busclass, as 1072 * well as busclass. Each layer will attempt to detach the driver 1073 * from any devices that are children of the bus's devclass. The function 1074 * will return an error if a device fails to detach. 1075 * 1076 * We do a full search here of the devclass list at each iteration 1077 * level to save storing children-lists in the devclass structure. If 1078 * we ever move beyond a few dozen devices doing this, we may need to 1079 * reevaluate... 1080 * 1081 * @param busclass the devclass of the parent bus 1082 * @param dc the devclass of the driver being deleted 1083 * @param driver the driver being deleted 1084 */ 1085static int 1086devclass_driver_deleted(devclass_t busclass, devclass_t dc, driver_t *driver) 1087{ 1088 devclass_t parent; 1089 device_t dev; 1090 int error, i; 1091 1092 /* 1093 * Disassociate from any devices. We iterate through all the 1094 * devices in the devclass of the driver and detach any which are 1095 * using the driver and which have a parent in the devclass which 1096 * we are deleting from. 1097 * 1098 * Note that since a driver can be in multiple devclasses, we 1099 * should not detach devices which are not children of devices in 1100 * the affected devclass. 1101 */ 1102 for (i = 0; i < dc->maxunit; i++) { 1103 if (dc->devices[i]) { 1104 dev = dc->devices[i]; 1105 if (dev->driver == driver && dev->parent && 1106 dev->parent->devclass == busclass) { 1107 if ((error = device_detach(dev)) != 0) 1108 return (error); 1109 BUS_PROBE_NOMATCH(dev->parent, dev); 1110 devnomatch(dev); 1111 dev->flags |= DF_DONENOMATCH; 1112 } 1113 } 1114 } 1115 1116 /* 1117 * Walk through the children classes. Since we only keep a 1118 * single parent pointer around, we walk the entire list of 1119 * devclasses looking for children. We set the 1120 * DC_HAS_CHILDREN flag when a child devclass is created on 1121 * the parent, so we only walk the list for those devclasses 1122 * that have children. 1123 */ 1124 if (!(busclass->flags & DC_HAS_CHILDREN)) 1125 return (0); 1126 parent = busclass; 1127 TAILQ_FOREACH(busclass, &devclasses, link) { 1128 if (busclass->parent == parent) { 1129 error = devclass_driver_deleted(busclass, dc, driver); 1130 if (error) 1131 return (error); 1132 } 1133 } 1134 return (0); 1135} 1136 1137/** 1138 * @brief Delete a device driver from a device class 1139 * 1140 * Delete a device driver from a devclass. This is normally called 1141 * automatically by DRIVER_MODULE(). 1142 * 1143 * If the driver is currently attached to any devices, 1144 * devclass_delete_driver() will first attempt to detach from each 1145 * device. If one of the detach calls fails, the driver will not be 1146 * deleted. 1147 * 1148 * @param dc the devclass to edit 1149 * @param driver the driver to unregister 1150 */ 1151int 1152devclass_delete_driver(devclass_t busclass, driver_t *driver) 1153{ 1154 devclass_t dc = devclass_find(driver->name); 1155 driverlink_t dl; 1156 int error; 1157 1158 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass))); 1159 1160 if (!dc) 1161 return (0); 1162 1163 /* 1164 * Find the link structure in the bus' list of drivers. 1165 */ 1166 TAILQ_FOREACH(dl, &busclass->drivers, link) { 1167 if (dl->driver == driver) 1168 break; 1169 } 1170 1171 if (!dl) { 1172 PDEBUG(("%s not found in %s list", driver->name, 1173 busclass->name)); 1174 return (ENOENT); 1175 } 1176 1177 error = devclass_driver_deleted(busclass, dc, driver); 1178 if (error != 0) 1179 return (error); 1180 1181 TAILQ_REMOVE(&busclass->drivers, dl, link); 1182 free(dl, M_BUS); 1183 1184 /* XXX: kobj_mtx */ 1185 driver->refs--; 1186 if (driver->refs == 0) 1187 kobj_class_free((kobj_class_t) driver); 1188 1189 bus_data_generation_update(); 1190 return (0); 1191} 1192 1193/** 1194 * @brief Quiesces a set of device drivers from a device class 1195 * 1196 * Quiesce a device driver from a devclass. This is normally called 1197 * automatically by DRIVER_MODULE(). 1198 * 1199 * If the driver is currently attached to any devices, 1200 * devclass_quiesece_driver() will first attempt to quiesce each 1201 * device. 1202 * 1203 * @param dc the devclass to edit 1204 * @param driver the driver to unregister 1205 */ 1206static int 1207devclass_quiesce_driver(devclass_t busclass, driver_t *driver) 1208{ 1209 devclass_t dc = devclass_find(driver->name); 1210 driverlink_t dl; 1211 device_t dev; 1212 int i; 1213 int error; 1214 1215 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass))); 1216 1217 if (!dc) 1218 return (0); 1219 1220 /* 1221 * Find the link structure in the bus' list of drivers. 1222 */ 1223 TAILQ_FOREACH(dl, &busclass->drivers, link) { 1224 if (dl->driver == driver) 1225 break; 1226 } 1227 1228 if (!dl) { 1229 PDEBUG(("%s not found in %s list", driver->name, 1230 busclass->name)); 1231 return (ENOENT); 1232 } 1233 1234 /* 1235 * Quiesce all devices. We iterate through all the devices in 1236 * the devclass of the driver and quiesce any which are using 1237 * the driver and which have a parent in the devclass which we 1238 * are quiescing. 1239 * 1240 * Note that since a driver can be in multiple devclasses, we 1241 * should not quiesce devices which are not children of 1242 * devices in the affected devclass. 1243 */ 1244 for (i = 0; i < dc->maxunit; i++) { 1245 if (dc->devices[i]) { 1246 dev = dc->devices[i]; 1247 if (dev->driver == driver && dev->parent && 1248 dev->parent->devclass == busclass) { 1249 if ((error = device_quiesce(dev)) != 0) 1250 return (error); 1251 } 1252 } 1253 } 1254 1255 return (0); 1256} 1257 1258/** 1259 * @internal 1260 */ 1261static driverlink_t 1262devclass_find_driver_internal(devclass_t dc, const char *classname) 1263{ 1264 driverlink_t dl; 1265 1266 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc))); 1267 1268 TAILQ_FOREACH(dl, &dc->drivers, link) { 1269 if (!strcmp(dl->driver->name, classname)) 1270 return (dl); 1271 } 1272 1273 PDEBUG(("not found")); 1274 return (NULL); 1275} 1276 1277/** 1278 * @brief Return the name of the devclass 1279 */ 1280const char * 1281devclass_get_name(devclass_t dc) 1282{ 1283 return (dc->name); 1284} 1285 1286/** 1287 * @brief Find a device given a unit number 1288 * 1289 * @param dc the devclass to search 1290 * @param unit the unit number to search for 1291 * 1292 * @returns the device with the given unit number or @c 1293 * NULL if there is no such device 1294 */ 1295device_t 1296devclass_get_device(devclass_t dc, int unit) 1297{ 1298 if (dc == NULL || unit < 0 || unit >= dc->maxunit) 1299 return (NULL); 1300 return (dc->devices[unit]); 1301} 1302 1303/** 1304 * @brief Find the softc field of a device given a unit number 1305 * 1306 * @param dc the devclass to search 1307 * @param unit the unit number to search for 1308 * 1309 * @returns the softc field of the device with the given 1310 * unit number or @c NULL if there is no such 1311 * device 1312 */ 1313void * 1314devclass_get_softc(devclass_t dc, int unit) 1315{ 1316 device_t dev; 1317 1318 dev = devclass_get_device(dc, unit); 1319 if (!dev) 1320 return (NULL); 1321 1322 return (device_get_softc(dev)); 1323} 1324 1325/** 1326 * @brief Get a list of devices in the devclass 1327 * 1328 * An array containing a list of all the devices in the given devclass 1329 * is allocated and returned in @p *devlistp. The number of devices 1330 * in the array is returned in @p *devcountp. The caller should free 1331 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0. 1332 * 1333 * @param dc the devclass to examine 1334 * @param devlistp points at location for array pointer return 1335 * value 1336 * @param devcountp points at location for array size return value 1337 * 1338 * @retval 0 success 1339 * @retval ENOMEM the array allocation failed 1340 */ 1341int 1342devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp) 1343{ 1344 int count, i; 1345 device_t *list; 1346 1347 count = devclass_get_count(dc); 1348 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO); 1349 if (!list) 1350 return (ENOMEM); 1351 1352 count = 0; 1353 for (i = 0; i < dc->maxunit; i++) { 1354 if (dc->devices[i]) { 1355 list[count] = dc->devices[i]; 1356 count++; 1357 } 1358 } 1359 1360 *devlistp = list; 1361 *devcountp = count; 1362 1363 return (0); 1364} 1365 1366/** 1367 * @brief Get a list of drivers in the devclass 1368 * 1369 * An array containing a list of pointers to all the drivers in the 1370 * given devclass is allocated and returned in @p *listp. The number 1371 * of drivers in the array is returned in @p *countp. The caller should 1372 * free the array using @c free(p, M_TEMP). 1373 * 1374 * @param dc the devclass to examine 1375 * @param listp gives location for array pointer return value 1376 * @param countp gives location for number of array elements 1377 * return value 1378 * 1379 * @retval 0 success 1380 * @retval ENOMEM the array allocation failed 1381 */ 1382int 1383devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp) 1384{ 1385 driverlink_t dl; 1386 driver_t **list; 1387 int count; 1388 1389 count = 0; 1390 TAILQ_FOREACH(dl, &dc->drivers, link) 1391 count++; 1392 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT); 1393 if (list == NULL) 1394 return (ENOMEM); 1395 1396 count = 0; 1397 TAILQ_FOREACH(dl, &dc->drivers, link) { 1398 list[count] = dl->driver; 1399 count++; 1400 } 1401 *listp = list; 1402 *countp = count; 1403 1404 return (0); 1405} 1406 1407/** 1408 * @brief Get the number of devices in a devclass 1409 * 1410 * @param dc the devclass to examine 1411 */ 1412int 1413devclass_get_count(devclass_t dc) 1414{ 1415 int count, i; 1416 1417 count = 0; 1418 for (i = 0; i < dc->maxunit; i++) 1419 if (dc->devices[i]) 1420 count++; 1421 return (count); 1422} 1423 1424/** 1425 * @brief Get the maximum unit number used in a devclass 1426 * 1427 * Note that this is one greater than the highest currently-allocated 1428 * unit. If a null devclass_t is passed in, -1 is returned to indicate 1429 * that not even the devclass has been allocated yet. 1430 * 1431 * @param dc the devclass to examine 1432 */ 1433int 1434devclass_get_maxunit(devclass_t dc) 1435{ 1436 if (dc == NULL) 1437 return (-1); 1438 return (dc->maxunit); 1439} 1440 1441/** 1442 * @brief Find a free unit number in a devclass 1443 * 1444 * This function searches for the first unused unit number greater 1445 * that or equal to @p unit. 1446 * 1447 * @param dc the devclass to examine 1448 * @param unit the first unit number to check 1449 */ 1450int 1451devclass_find_free_unit(devclass_t dc, int unit) 1452{ 1453 if (dc == NULL) 1454 return (unit); 1455 while (unit < dc->maxunit && dc->devices[unit] != NULL) 1456 unit++; 1457 return (unit); 1458} 1459 1460/** 1461 * @brief Set the parent of a devclass 1462 * 1463 * The parent class is normally initialised automatically by 1464 * DRIVER_MODULE(). 1465 * 1466 * @param dc the devclass to edit 1467 * @param pdc the new parent devclass 1468 */ 1469void 1470devclass_set_parent(devclass_t dc, devclass_t pdc) 1471{ 1472 dc->parent = pdc; 1473} 1474 1475/** 1476 * @brief Get the parent of a devclass 1477 * 1478 * @param dc the devclass to examine 1479 */ 1480devclass_t 1481devclass_get_parent(devclass_t dc) 1482{ 1483 return (dc->parent); 1484} 1485 1486struct sysctl_ctx_list * 1487devclass_get_sysctl_ctx(devclass_t dc) 1488{ 1489 return (&dc->sysctl_ctx); 1490} 1491 1492struct sysctl_oid * 1493devclass_get_sysctl_tree(devclass_t dc) 1494{ 1495 return (dc->sysctl_tree); 1496} 1497 1498/** 1499 * @internal 1500 * @brief Allocate a unit number 1501 * 1502 * On entry, @p *unitp is the desired unit number (or @c -1 if any 1503 * will do). The allocated unit number is returned in @p *unitp. 1504 1505 * @param dc the devclass to allocate from 1506 * @param unitp points at the location for the allocated unit 1507 * number 1508 * 1509 * @retval 0 success 1510 * @retval EEXIST the requested unit number is already allocated 1511 * @retval ENOMEM memory allocation failure 1512 */ 1513static int 1514devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp) 1515{ 1516 const char *s; 1517 int unit = *unitp; 1518 1519 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc))); 1520 1521 /* Ask the parent bus if it wants to wire this device. */ 1522 if (unit == -1) 1523 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name, 1524 &unit); 1525 1526 /* If we were given a wired unit number, check for existing device */ 1527 /* XXX imp XXX */ 1528 if (unit != -1) { 1529 if (unit >= 0 && unit < dc->maxunit && 1530 dc->devices[unit] != NULL) { 1531 if (bootverbose) 1532 printf("%s: %s%d already exists; skipping it\n", 1533 dc->name, dc->name, *unitp); 1534 return (EEXIST); 1535 } 1536 } else { 1537 /* Unwired device, find the next available slot for it */ 1538 unit = 0; 1539 for (unit = 0;; unit++) { 1540 /* If there is an "at" hint for a unit then skip it. */ 1541 if (resource_string_value(dc->name, unit, "at", &s) == 1542 0) 1543 continue; 1544 1545 /* If this device slot is already in use, skip it. */ 1546 if (unit < dc->maxunit && dc->devices[unit] != NULL) 1547 continue; 1548 1549 break; 1550 } 1551 } 1552 1553 /* 1554 * We've selected a unit beyond the length of the table, so let's 1555 * extend the table to make room for all units up to and including 1556 * this one. 1557 */ 1558 if (unit >= dc->maxunit) { 1559 device_t *newlist, *oldlist; 1560 int newsize; 1561 1562 oldlist = dc->devices; 1563 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t)); 1564 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT); 1565 if (!newlist) 1566 return (ENOMEM); 1567 if (oldlist != NULL) 1568 bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit); 1569 bzero(newlist + dc->maxunit, 1570 sizeof(device_t) * (newsize - dc->maxunit)); 1571 dc->devices = newlist; 1572 dc->maxunit = newsize; 1573 if (oldlist != NULL) 1574 free(oldlist, M_BUS); 1575 } 1576 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc))); 1577 1578 *unitp = unit; 1579 return (0); 1580} 1581 1582/** 1583 * @internal 1584 * @brief Add a device to a devclass 1585 * 1586 * A unit number is allocated for the device (using the device's 1587 * preferred unit number if any) and the device is registered in the 1588 * devclass. This allows the device to be looked up by its unit 1589 * number, e.g. by decoding a dev_t minor number. 1590 * 1591 * @param dc the devclass to add to 1592 * @param dev the device to add 1593 * 1594 * @retval 0 success 1595 * @retval EEXIST the requested unit number is already allocated 1596 * @retval ENOMEM memory allocation failure 1597 */ 1598static int 1599devclass_add_device(devclass_t dc, device_t dev) 1600{ 1601 int buflen, error; 1602 1603 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 1604 1605 buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX); 1606 if (buflen < 0) 1607 return (ENOMEM); 1608 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO); 1609 if (!dev->nameunit) 1610 return (ENOMEM); 1611 1612 if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) { 1613 free(dev->nameunit, M_BUS); 1614 dev->nameunit = NULL; 1615 return (error); 1616 } 1617 dc->devices[dev->unit] = dev; 1618 dev->devclass = dc; 1619 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit); 1620 1621 return (0); 1622} 1623 1624/** 1625 * @internal 1626 * @brief Delete a device from a devclass 1627 * 1628 * The device is removed from the devclass's device list and its unit 1629 * number is freed. 1630 1631 * @param dc the devclass to delete from 1632 * @param dev the device to delete 1633 * 1634 * @retval 0 success 1635 */ 1636static int 1637devclass_delete_device(devclass_t dc, device_t dev) 1638{ 1639 if (!dc || !dev) 1640 return (0); 1641 1642 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 1643 1644 if (dev->devclass != dc || dc->devices[dev->unit] != dev) 1645 panic("devclass_delete_device: inconsistent device class"); 1646 dc->devices[dev->unit] = NULL; 1647 if (dev->flags & DF_WILDCARD) 1648 dev->unit = -1; 1649 dev->devclass = NULL; 1650 free(dev->nameunit, M_BUS); 1651 dev->nameunit = NULL; 1652 1653 return (0); 1654} 1655 1656/** 1657 * @internal 1658 * @brief Make a new device and add it as a child of @p parent 1659 * 1660 * @param parent the parent of the new device 1661 * @param name the devclass name of the new device or @c NULL 1662 * to leave the devclass unspecified 1663 * @parem unit the unit number of the new device of @c -1 to 1664 * leave the unit number unspecified 1665 * 1666 * @returns the new device 1667 */ 1668static device_t 1669make_device(device_t parent, const char *name, int unit) 1670{ 1671 device_t dev; 1672 devclass_t dc; 1673 1674 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit)); 1675 1676 if (name) { 1677 dc = devclass_find_internal(name, NULL, TRUE); 1678 if (!dc) { 1679 printf("make_device: can't find device class %s\n", 1680 name); 1681 return (NULL); 1682 } 1683 } else { 1684 dc = NULL; 1685 } 1686 1687 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO); 1688 if (!dev) 1689 return (NULL); 1690 1691 dev->parent = parent; 1692 TAILQ_INIT(&dev->children); 1693 kobj_init((kobj_t) dev, &null_class); 1694 dev->driver = NULL; 1695 dev->devclass = NULL; 1696 dev->unit = unit; 1697 dev->nameunit = NULL; 1698 dev->desc = NULL; 1699 dev->busy = 0; 1700 dev->devflags = 0; 1701 dev->flags = DF_ENABLED; 1702 dev->order = 0; 1703 if (unit == -1) 1704 dev->flags |= DF_WILDCARD; 1705 if (name) { 1706 dev->flags |= DF_FIXEDCLASS; 1707 if (devclass_add_device(dc, dev)) { 1708 kobj_delete((kobj_t) dev, M_BUS); 1709 return (NULL); 1710 } 1711 } 1712 dev->ivars = NULL; 1713 dev->softc = NULL; 1714 1715 dev->state = DS_NOTPRESENT; 1716 1717 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink); 1718 bus_data_generation_update(); 1719 1720 return (dev); 1721} 1722 1723/** 1724 * @internal 1725 * @brief Print a description of a device. 1726 */ 1727static int 1728device_print_child(device_t dev, device_t child) 1729{ 1730 int retval = 0; 1731 1732 if (device_is_alive(child)) 1733 retval += BUS_PRINT_CHILD(dev, child); 1734 else 1735 retval += device_printf(child, " not found\n"); 1736 1737 return (retval); 1738} 1739 1740/** 1741 * @brief Create a new device 1742 * 1743 * This creates a new device and adds it as a child of an existing 1744 * parent device. The new device will be added after the last existing 1745 * child with order zero. 1746 * 1747 * @param dev the device which will be the parent of the 1748 * new child device 1749 * @param name devclass name for new device or @c NULL if not 1750 * specified 1751 * @param unit unit number for new device or @c -1 if not 1752 * specified 1753 * 1754 * @returns the new device 1755 */ 1756device_t 1757device_add_child(device_t dev, const char *name, int unit) 1758{ 1759 return (device_add_child_ordered(dev, 0, name, unit)); 1760} 1761 1762/** 1763 * @brief Create a new device 1764 * 1765 * This creates a new device and adds it as a child of an existing 1766 * parent device. The new device will be added after the last existing 1767 * child with the same order. 1768 * 1769 * @param dev the device which will be the parent of the 1770 * new child device 1771 * @param order a value which is used to partially sort the 1772 * children of @p dev - devices created using 1773 * lower values of @p order appear first in @p 1774 * dev's list of children 1775 * @param name devclass name for new device or @c NULL if not 1776 * specified 1777 * @param unit unit number for new device or @c -1 if not 1778 * specified 1779 * 1780 * @returns the new device 1781 */ 1782device_t 1783device_add_child_ordered(device_t dev, u_int order, const char *name, int unit) 1784{ 1785 device_t child; 1786 device_t place; 1787 1788 PDEBUG(("%s at %s with order %u as unit %d", 1789 name, DEVICENAME(dev), order, unit)); 1790 KASSERT(name != NULL || unit == -1, 1791 ("child device with wildcard name and specific unit number")); 1792 1793 child = make_device(dev, name, unit); 1794 if (child == NULL) 1795 return (child); 1796 child->order = order; 1797 1798 TAILQ_FOREACH(place, &dev->children, link) { 1799 if (place->order > order) 1800 break; 1801 } 1802 1803 if (place) { 1804 /* 1805 * The device 'place' is the first device whose order is 1806 * greater than the new child. 1807 */ 1808 TAILQ_INSERT_BEFORE(place, child, link); 1809 } else { 1810 /* 1811 * The new child's order is greater or equal to the order of 1812 * any existing device. Add the child to the tail of the list. 1813 */ 1814 TAILQ_INSERT_TAIL(&dev->children, child, link); 1815 } 1816 1817 bus_data_generation_update(); 1818 return (child); 1819} 1820 1821/** 1822 * @brief Delete a device 1823 * 1824 * This function deletes a device along with all of its children. If 1825 * the device currently has a driver attached to it, the device is 1826 * detached first using device_detach(). 1827 * 1828 * @param dev the parent device 1829 * @param child the device to delete 1830 * 1831 * @retval 0 success 1832 * @retval non-zero a unit error code describing the error 1833 */ 1834int 1835device_delete_child(device_t dev, device_t child) 1836{ 1837 int error; 1838 device_t grandchild; 1839 1840 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev))); 1841 1842 /* remove children first */ 1843 while ((grandchild = TAILQ_FIRST(&child->children)) != NULL) { 1844 error = device_delete_child(child, grandchild); 1845 if (error) 1846 return (error); 1847 } 1848 1849 if ((error = device_detach(child)) != 0) 1850 return (error); 1851 if (child->devclass) 1852 devclass_delete_device(child->devclass, child); 1853 if (child->parent) 1854 BUS_CHILD_DELETED(dev, child); 1855 TAILQ_REMOVE(&dev->children, child, link); 1856 TAILQ_REMOVE(&bus_data_devices, child, devlink); 1857 kobj_delete((kobj_t) child, M_BUS); 1858 1859 bus_data_generation_update(); 1860 return (0); 1861} 1862 1863/** 1864 * @brief Delete all children devices of the given device, if any. 1865 * 1866 * This function deletes all children devices of the given device, if 1867 * any, using the device_delete_child() function for each device it 1868 * finds. If a child device cannot be deleted, this function will 1869 * return an error code. 1870 * 1871 * @param dev the parent device 1872 * 1873 * @retval 0 success 1874 * @retval non-zero a device would not detach 1875 */ 1876int 1877device_delete_children(device_t dev) 1878{ 1879 device_t child; 1880 int error; 1881 1882 PDEBUG(("Deleting all children of %s", DEVICENAME(dev))); 1883 1884 error = 0; 1885 1886 while ((child = TAILQ_FIRST(&dev->children)) != NULL) { 1887 error = device_delete_child(dev, child); 1888 if (error) { 1889 PDEBUG(("Failed deleting %s", DEVICENAME(child))); 1890 break; 1891 } 1892 } 1893 return (error); 1894} 1895 1896/** 1897 * @brief Find a device given a unit number 1898 * 1899 * This is similar to devclass_get_devices() but only searches for 1900 * devices which have @p dev as a parent. 1901 * 1902 * @param dev the parent device to search 1903 * @param unit the unit number to search for. If the unit is -1, 1904 * return the first child of @p dev which has name 1905 * @p classname (that is, the one with the lowest unit.) 1906 * 1907 * @returns the device with the given unit number or @c 1908 * NULL if there is no such device 1909 */ 1910device_t 1911device_find_child(device_t dev, const char *classname, int unit) 1912{ 1913 devclass_t dc; 1914 device_t child; 1915 1916 dc = devclass_find(classname); 1917 if (!dc) 1918 return (NULL); 1919 1920 if (unit != -1) { 1921 child = devclass_get_device(dc, unit); 1922 if (child && child->parent == dev) 1923 return (child); 1924 } else { 1925 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) { 1926 child = devclass_get_device(dc, unit); 1927 if (child && child->parent == dev) 1928 return (child); 1929 } 1930 } 1931 return (NULL); 1932} 1933 1934/** 1935 * @internal 1936 */ 1937static driverlink_t 1938first_matching_driver(devclass_t dc, device_t dev) 1939{ 1940 if (dev->devclass) 1941 return (devclass_find_driver_internal(dc, dev->devclass->name)); 1942 return (TAILQ_FIRST(&dc->drivers)); 1943} 1944 1945/** 1946 * @internal 1947 */ 1948static driverlink_t 1949next_matching_driver(devclass_t dc, device_t dev, driverlink_t last) 1950{ 1951 if (dev->devclass) { 1952 driverlink_t dl; 1953 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link)) 1954 if (!strcmp(dev->devclass->name, dl->driver->name)) 1955 return (dl); 1956 return (NULL); 1957 } 1958 return (TAILQ_NEXT(last, link)); 1959} 1960 1961/** 1962 * @internal 1963 */ 1964int 1965device_probe_child(device_t dev, device_t child) 1966{ 1967 devclass_t dc; 1968 driverlink_t best = NULL; 1969 driverlink_t dl; 1970 int result, pri = 0; 1971 int hasclass = (child->devclass != NULL); 1972 1973 GIANT_REQUIRED; 1974 1975 dc = dev->devclass; 1976 if (!dc) 1977 panic("device_probe_child: parent device has no devclass"); 1978 1979 /* 1980 * If the state is already probed, then return. However, don't 1981 * return if we can rebid this object. 1982 */ 1983 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0) 1984 return (0); 1985 1986 for (; dc; dc = dc->parent) { 1987 for (dl = first_matching_driver(dc, child); 1988 dl; 1989 dl = next_matching_driver(dc, child, dl)) { 1990 /* If this driver's pass is too high, then ignore it. */ 1991 if (dl->pass > bus_current_pass) 1992 continue; 1993 1994 PDEBUG(("Trying %s", DRIVERNAME(dl->driver))); 1995 result = device_set_driver(child, dl->driver); 1996 if (result == ENOMEM) 1997 return (result); 1998 else if (result != 0) 1999 continue; 2000 if (!hasclass) { 2001 if (device_set_devclass(child, 2002 dl->driver->name) != 0) { 2003 char const * devname = 2004 device_get_name(child); 2005 if (devname == NULL) 2006 devname = "(unknown)"; 2007 printf("driver bug: Unable to set " 2008 "devclass (class: %s " 2009 "devname: %s)\n", 2010 dl->driver->name, 2011 devname); 2012 (void)device_set_driver(child, NULL); 2013 continue; 2014 } 2015 } 2016 2017 /* Fetch any flags for the device before probing. */ 2018 resource_int_value(dl->driver->name, child->unit, 2019 "flags", &child->devflags); 2020 2021 result = DEVICE_PROBE(child); 2022 2023 /* Reset flags and devclass before the next probe. */ 2024 child->devflags = 0; 2025 if (!hasclass) 2026 (void)device_set_devclass(child, NULL); 2027 2028 /* 2029 * If the driver returns SUCCESS, there can be 2030 * no higher match for this device. 2031 */ 2032 if (result == 0) { 2033 best = dl; 2034 pri = 0; 2035 break; 2036 } 2037 2038 /* 2039 * The driver returned an error so it 2040 * certainly doesn't match. 2041 */ 2042 if (result > 0) { 2043 (void)device_set_driver(child, NULL); 2044 continue; 2045 } 2046 2047 /* 2048 * A priority lower than SUCCESS, remember the 2049 * best matching driver. Initialise the value 2050 * of pri for the first match. 2051 */ 2052 if (best == NULL || result > pri) { 2053 /* 2054 * Probes that return BUS_PROBE_NOWILDCARD 2055 * or lower only match on devices whose 2056 * driver was explicitly specified. 2057 */ 2058 if (result <= BUS_PROBE_NOWILDCARD && 2059 !(child->flags & DF_FIXEDCLASS)) 2060 continue; 2061 best = dl; 2062 pri = result; 2063 continue; 2064 } 2065 } 2066 /* 2067 * If we have an unambiguous match in this devclass, 2068 * don't look in the parent. 2069 */ 2070 if (best && pri == 0) 2071 break; 2072 } 2073 2074 /* 2075 * If we found a driver, change state and initialise the devclass. 2076 */ 2077 /* XXX What happens if we rebid and got no best? */ 2078 if (best) { 2079 /* 2080 * If this device was attached, and we were asked to 2081 * rescan, and it is a different driver, then we have 2082 * to detach the old driver and reattach this new one. 2083 * Note, we don't have to check for DF_REBID here 2084 * because if the state is > DS_ALIVE, we know it must 2085 * be. 2086 * 2087 * This assumes that all DF_REBID drivers can have 2088 * their probe routine called at any time and that 2089 * they are idempotent as well as completely benign in 2090 * normal operations. 2091 * 2092 * We also have to make sure that the detach 2093 * succeeded, otherwise we fail the operation (or 2094 * maybe it should just fail silently? I'm torn). 2095 */ 2096 if (child->state > DS_ALIVE && best->driver != child->driver) 2097 if ((result = device_detach(dev)) != 0) 2098 return (result); 2099 2100 /* Set the winning driver, devclass, and flags. */ 2101 if (!child->devclass) { 2102 result = device_set_devclass(child, best->driver->name); 2103 if (result != 0) 2104 return (result); 2105 } 2106 result = device_set_driver(child, best->driver); 2107 if (result != 0) 2108 return (result); 2109 resource_int_value(best->driver->name, child->unit, 2110 "flags", &child->devflags); 2111 2112 if (pri < 0) { 2113 /* 2114 * A bit bogus. Call the probe method again to make 2115 * sure that we have the right description. 2116 */ 2117 DEVICE_PROBE(child); 2118#if 0 2119 child->flags |= DF_REBID; 2120#endif 2121 } else 2122 child->flags &= ~DF_REBID; 2123 child->state = DS_ALIVE; 2124 2125 bus_data_generation_update(); 2126 return (0); 2127 } 2128 2129 return (ENXIO); 2130} 2131 2132/** 2133 * @brief Return the parent of a device 2134 */ 2135device_t 2136device_get_parent(device_t dev) 2137{ 2138 return (dev->parent); 2139} 2140 2141/** 2142 * @brief Get a list of children of a device 2143 * 2144 * An array containing a list of all the children of the given device 2145 * is allocated and returned in @p *devlistp. The number of devices 2146 * in the array is returned in @p *devcountp. The caller should free 2147 * the array using @c free(p, M_TEMP). 2148 * 2149 * @param dev the device to examine 2150 * @param devlistp points at location for array pointer return 2151 * value 2152 * @param devcountp points at location for array size return value 2153 * 2154 * @retval 0 success 2155 * @retval ENOMEM the array allocation failed 2156 */ 2157int 2158device_get_children(device_t dev, device_t **devlistp, int *devcountp) 2159{ 2160 int count; 2161 device_t child; 2162 device_t *list; 2163 2164 count = 0; 2165 TAILQ_FOREACH(child, &dev->children, link) { 2166 count++; 2167 } 2168 if (count == 0) { 2169 *devlistp = NULL; 2170 *devcountp = 0; 2171 return (0); 2172 } 2173 2174 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO); 2175 if (!list) 2176 return (ENOMEM); 2177 2178 count = 0; 2179 TAILQ_FOREACH(child, &dev->children, link) { 2180 list[count] = child; 2181 count++; 2182 } 2183 2184 *devlistp = list; 2185 *devcountp = count; 2186 2187 return (0); 2188} 2189 2190/** 2191 * @brief Return the current driver for the device or @c NULL if there 2192 * is no driver currently attached 2193 */ 2194driver_t * 2195device_get_driver(device_t dev) 2196{ 2197 return (dev->driver); 2198} 2199 2200/** 2201 * @brief Return the current devclass for the device or @c NULL if 2202 * there is none. 2203 */ 2204devclass_t 2205device_get_devclass(device_t dev) 2206{ 2207 return (dev->devclass); 2208} 2209 2210/** 2211 * @brief Return the name of the device's devclass or @c NULL if there 2212 * is none. 2213 */ 2214const char * 2215device_get_name(device_t dev) 2216{ 2217 if (dev != NULL && dev->devclass) 2218 return (devclass_get_name(dev->devclass)); 2219 return (NULL); 2220} 2221 2222/** 2223 * @brief Return a string containing the device's devclass name 2224 * followed by an ascii representation of the device's unit number 2225 * (e.g. @c "foo2"). 2226 */ 2227const char * 2228device_get_nameunit(device_t dev) 2229{ 2230 return (dev->nameunit); 2231} 2232 2233/** 2234 * @brief Return the device's unit number. 2235 */ 2236int 2237device_get_unit(device_t dev) 2238{ 2239 return (dev->unit); 2240} 2241 2242/** 2243 * @brief Return the device's description string 2244 */ 2245const char * 2246device_get_desc(device_t dev) 2247{ 2248 return (dev->desc); 2249} 2250 2251/** 2252 * @brief Return the device's flags 2253 */ 2254uint32_t 2255device_get_flags(device_t dev) 2256{ 2257 return (dev->devflags); 2258} 2259 2260struct sysctl_ctx_list * 2261device_get_sysctl_ctx(device_t dev) 2262{ 2263 return (&dev->sysctl_ctx); 2264} 2265 2266struct sysctl_oid * 2267device_get_sysctl_tree(device_t dev) 2268{ 2269 return (dev->sysctl_tree); 2270} 2271 2272/** 2273 * @brief Print the name of the device followed by a colon and a space 2274 * 2275 * @returns the number of characters printed 2276 */ 2277int 2278device_print_prettyname(device_t dev) 2279{ 2280 const char *name = device_get_name(dev); 2281 2282 if (name == NULL) 2283 return (printf("unknown: ")); 2284 return (printf("%s%d: ", name, device_get_unit(dev))); 2285} 2286 2287/** 2288 * @brief Print the name of the device followed by a colon, a space 2289 * and the result of calling vprintf() with the value of @p fmt and 2290 * the following arguments. 2291 * 2292 * @returns the number of characters printed 2293 */ 2294int 2295device_printf(device_t dev, const char * fmt, ...) 2296{ 2297 va_list ap; 2298 int retval; 2299 2300 retval = device_print_prettyname(dev); 2301 va_start(ap, fmt); 2302 retval += vprintf(fmt, ap); 2303 va_end(ap); 2304 return (retval); 2305} 2306 2307/** 2308 * @internal 2309 */ 2310static void 2311device_set_desc_internal(device_t dev, const char* desc, int copy) 2312{ 2313 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) { 2314 free(dev->desc, M_BUS); 2315 dev->flags &= ~DF_DESCMALLOCED; 2316 dev->desc = NULL; 2317 } 2318 2319 if (copy && desc) { 2320 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT); 2321 if (dev->desc) { 2322 strcpy(dev->desc, desc); 2323 dev->flags |= DF_DESCMALLOCED; 2324 } 2325 } else { 2326 /* Avoid a -Wcast-qual warning */ 2327 dev->desc = (char *)(uintptr_t) desc; 2328 } 2329 2330 bus_data_generation_update(); 2331} 2332 2333/** 2334 * @brief Set the device's description 2335 * 2336 * The value of @c desc should be a string constant that will not 2337 * change (at least until the description is changed in a subsequent 2338 * call to device_set_desc() or device_set_desc_copy()). 2339 */ 2340void 2341device_set_desc(device_t dev, const char* desc) 2342{ 2343 device_set_desc_internal(dev, desc, FALSE); 2344} 2345 2346/** 2347 * @brief Set the device's description 2348 * 2349 * The string pointed to by @c desc is copied. Use this function if 2350 * the device description is generated, (e.g. with sprintf()). 2351 */ 2352void 2353device_set_desc_copy(device_t dev, const char* desc) 2354{ 2355 device_set_desc_internal(dev, desc, TRUE); 2356} 2357 2358/** 2359 * @brief Set the device's flags 2360 */ 2361void 2362device_set_flags(device_t dev, uint32_t flags) 2363{ 2364 dev->devflags = flags; 2365} 2366 2367/** 2368 * @brief Return the device's softc field 2369 * 2370 * The softc is allocated and zeroed when a driver is attached, based 2371 * on the size field of the driver. 2372 */ 2373void * 2374device_get_softc(device_t dev) 2375{ 2376 return (dev->softc); 2377} 2378 2379/** 2380 * @brief Set the device's softc field 2381 * 2382 * Most drivers do not need to use this since the softc is allocated 2383 * automatically when the driver is attached. 2384 */ 2385void 2386device_set_softc(device_t dev, void *softc) 2387{ 2388 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) 2389 free(dev->softc, M_BUS_SC); 2390 dev->softc = softc; 2391 if (dev->softc) 2392 dev->flags |= DF_EXTERNALSOFTC; 2393 else 2394 dev->flags &= ~DF_EXTERNALSOFTC; 2395} 2396 2397/** 2398 * @brief Free claimed softc 2399 * 2400 * Most drivers do not need to use this since the softc is freed 2401 * automatically when the driver is detached. 2402 */ 2403void 2404device_free_softc(void *softc) 2405{ 2406 free(softc, M_BUS_SC); 2407} 2408 2409/** 2410 * @brief Claim softc 2411 * 2412 * This function can be used to let the driver free the automatically 2413 * allocated softc using "device_free_softc()". This function is 2414 * useful when the driver is refcounting the softc and the softc 2415 * cannot be freed when the "device_detach" method is called. 2416 */ 2417void 2418device_claim_softc(device_t dev) 2419{ 2420 if (dev->softc) 2421 dev->flags |= DF_EXTERNALSOFTC; 2422 else 2423 dev->flags &= ~DF_EXTERNALSOFTC; 2424} 2425 2426/** 2427 * @brief Get the device's ivars field 2428 * 2429 * The ivars field is used by the parent device to store per-device 2430 * state (e.g. the physical location of the device or a list of 2431 * resources). 2432 */ 2433void * 2434device_get_ivars(device_t dev) 2435{ 2436 2437 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)")); 2438 return (dev->ivars); 2439} 2440 2441/** 2442 * @brief Set the device's ivars field 2443 */ 2444void 2445device_set_ivars(device_t dev, void * ivars) 2446{ 2447 2448 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)")); 2449 dev->ivars = ivars; 2450} 2451 2452/** 2453 * @brief Return the device's state 2454 */ 2455device_state_t 2456device_get_state(device_t dev) 2457{ 2458 return (dev->state); 2459} 2460 2461/** 2462 * @brief Set the DF_ENABLED flag for the device 2463 */ 2464void 2465device_enable(device_t dev) 2466{ 2467 dev->flags |= DF_ENABLED; 2468} 2469 2470/** 2471 * @brief Clear the DF_ENABLED flag for the device 2472 */ 2473void 2474device_disable(device_t dev) 2475{ 2476 dev->flags &= ~DF_ENABLED; 2477} 2478 2479/** 2480 * @brief Increment the busy counter for the device 2481 */ 2482void 2483device_busy(device_t dev) 2484{ 2485 if (dev->state < DS_ATTACHING) 2486 panic("device_busy: called for unattached device"); 2487 if (dev->busy == 0 && dev->parent) 2488 device_busy(dev->parent); 2489 dev->busy++; 2490 if (dev->state == DS_ATTACHED) 2491 dev->state = DS_BUSY; 2492} 2493 2494/** 2495 * @brief Decrement the busy counter for the device 2496 */ 2497void 2498device_unbusy(device_t dev) 2499{ 2500 if (dev->busy != 0 && dev->state != DS_BUSY && 2501 dev->state != DS_ATTACHING) 2502 panic("device_unbusy: called for non-busy device %s", 2503 device_get_nameunit(dev)); 2504 dev->busy--; 2505 if (dev->busy == 0) { 2506 if (dev->parent) 2507 device_unbusy(dev->parent); 2508 if (dev->state == DS_BUSY) 2509 dev->state = DS_ATTACHED; 2510 } 2511} 2512 2513/** 2514 * @brief Set the DF_QUIET flag for the device 2515 */ 2516void 2517device_quiet(device_t dev) 2518{ 2519 dev->flags |= DF_QUIET; 2520} 2521 2522/** 2523 * @brief Clear the DF_QUIET flag for the device 2524 */ 2525void 2526device_verbose(device_t dev) 2527{ 2528 dev->flags &= ~DF_QUIET; 2529} 2530 2531/** 2532 * @brief Return non-zero if the DF_QUIET flag is set on the device 2533 */ 2534int 2535device_is_quiet(device_t dev) 2536{ 2537 return ((dev->flags & DF_QUIET) != 0); 2538} 2539 2540/** 2541 * @brief Return non-zero if the DF_ENABLED flag is set on the device 2542 */ 2543int 2544device_is_enabled(device_t dev) 2545{ 2546 return ((dev->flags & DF_ENABLED) != 0); 2547} 2548 2549/** 2550 * @brief Return non-zero if the device was successfully probed 2551 */ 2552int 2553device_is_alive(device_t dev) 2554{ 2555 return (dev->state >= DS_ALIVE); 2556} 2557 2558/** 2559 * @brief Return non-zero if the device currently has a driver 2560 * attached to it 2561 */ 2562int 2563device_is_attached(device_t dev) 2564{ 2565 return (dev->state >= DS_ATTACHED); 2566} 2567 2568/** 2569 * @brief Set the devclass of a device 2570 * @see devclass_add_device(). 2571 */ 2572int 2573device_set_devclass(device_t dev, const char *classname) 2574{ 2575 devclass_t dc; 2576 int error; 2577 2578 if (!classname) { 2579 if (dev->devclass) 2580 devclass_delete_device(dev->devclass, dev); 2581 return (0); 2582 } 2583 2584 if (dev->devclass) { 2585 printf("device_set_devclass: device class already set\n"); 2586 return (EINVAL); 2587 } 2588 2589 dc = devclass_find_internal(classname, NULL, TRUE); 2590 if (!dc) 2591 return (ENOMEM); 2592 2593 error = devclass_add_device(dc, dev); 2594 2595 bus_data_generation_update(); 2596 return (error); 2597} 2598 2599/** 2600 * @brief Set the driver of a device 2601 * 2602 * @retval 0 success 2603 * @retval EBUSY the device already has a driver attached 2604 * @retval ENOMEM a memory allocation failure occurred 2605 */ 2606int 2607device_set_driver(device_t dev, driver_t *driver) 2608{ 2609 if (dev->state >= DS_ATTACHED) 2610 return (EBUSY); 2611 2612 if (dev->driver == driver) 2613 return (0); 2614 2615 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) { 2616 free(dev->softc, M_BUS_SC); 2617 dev->softc = NULL; 2618 } 2619 device_set_desc(dev, NULL); 2620 kobj_delete((kobj_t) dev, NULL); 2621 dev->driver = driver; 2622 if (driver) { 2623 kobj_init((kobj_t) dev, (kobj_class_t) driver); 2624 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) { 2625 dev->softc = malloc(driver->size, M_BUS_SC, 2626 M_NOWAIT | M_ZERO); 2627 if (!dev->softc) { 2628 kobj_delete((kobj_t) dev, NULL); 2629 kobj_init((kobj_t) dev, &null_class); 2630 dev->driver = NULL; 2631 return (ENOMEM); 2632 } 2633 } 2634 } else { 2635 kobj_init((kobj_t) dev, &null_class); 2636 } 2637 2638 bus_data_generation_update(); 2639 return (0); 2640} 2641 2642/** 2643 * @brief Probe a device, and return this status. 2644 * 2645 * This function is the core of the device autoconfiguration 2646 * system. Its purpose is to select a suitable driver for a device and 2647 * then call that driver to initialise the hardware appropriately. The 2648 * driver is selected by calling the DEVICE_PROBE() method of a set of 2649 * candidate drivers and then choosing the driver which returned the 2650 * best value. This driver is then attached to the device using 2651 * device_attach(). 2652 * 2653 * The set of suitable drivers is taken from the list of drivers in 2654 * the parent device's devclass. If the device was originally created 2655 * with a specific class name (see device_add_child()), only drivers 2656 * with that name are probed, otherwise all drivers in the devclass 2657 * are probed. If no drivers return successful probe values in the 2658 * parent devclass, the search continues in the parent of that 2659 * devclass (see devclass_get_parent()) if any. 2660 * 2661 * @param dev the device to initialise 2662 * 2663 * @retval 0 success 2664 * @retval ENXIO no driver was found 2665 * @retval ENOMEM memory allocation failure 2666 * @retval non-zero some other unix error code 2667 * @retval -1 Device already attached 2668 */ 2669int 2670device_probe(device_t dev) 2671{ 2672 int error; 2673 2674 GIANT_REQUIRED; 2675 2676 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0) 2677 return (-1); 2678 2679 if (!(dev->flags & DF_ENABLED)) { 2680 if (bootverbose && device_get_name(dev) != NULL) { 2681 device_print_prettyname(dev); 2682 printf("not probed (disabled)\n"); 2683 } 2684 return (-1); 2685 } 2686 if ((error = device_probe_child(dev->parent, dev)) != 0) { 2687 if (bus_current_pass == BUS_PASS_DEFAULT && 2688 !(dev->flags & DF_DONENOMATCH)) { 2689 BUS_PROBE_NOMATCH(dev->parent, dev); 2690 devnomatch(dev); 2691 dev->flags |= DF_DONENOMATCH; 2692 } 2693 return (error); 2694 } 2695 return (0); 2696} 2697 2698/** 2699 * @brief Probe a device and attach a driver if possible 2700 * 2701 * calls device_probe() and attaches if that was successful. 2702 */ 2703int 2704device_probe_and_attach(device_t dev) 2705{ 2706 int error; 2707 2708 GIANT_REQUIRED; 2709 2710 error = device_probe(dev); 2711 if (error == -1) 2712 return (0); 2713 else if (error != 0) 2714 return (error); 2715 2716 CURVNET_SET_QUIET(vnet0); 2717 error = device_attach(dev); 2718 CURVNET_RESTORE(); 2719 return error; 2720} 2721 2722/** 2723 * @brief Attach a device driver to a device 2724 * 2725 * This function is a wrapper around the DEVICE_ATTACH() driver 2726 * method. In addition to calling DEVICE_ATTACH(), it initialises the 2727 * device's sysctl tree, optionally prints a description of the device 2728 * and queues a notification event for user-based device management 2729 * services. 2730 * 2731 * Normally this function is only called internally from 2732 * device_probe_and_attach(). 2733 * 2734 * @param dev the device to initialise 2735 * 2736 * @retval 0 success 2737 * @retval ENXIO no driver was found 2738 * @retval ENOMEM memory allocation failure 2739 * @retval non-zero some other unix error code 2740 */ 2741int 2742device_attach(device_t dev) 2743{ 2744 uint64_t attachtime; 2745 int error; 2746 2747 if (resource_disabled(dev->driver->name, dev->unit)) { 2748 device_disable(dev); 2749 if (bootverbose) 2750 device_printf(dev, "disabled via hints entry\n"); 2751 return (ENXIO); 2752 } 2753 2754 device_sysctl_init(dev); 2755 if (!device_is_quiet(dev)) 2756 device_print_child(dev->parent, dev); 2757 attachtime = get_cyclecount(); 2758 dev->state = DS_ATTACHING; 2759 if ((error = DEVICE_ATTACH(dev)) != 0) { 2760 printf("device_attach: %s%d attach returned %d\n", 2761 dev->driver->name, dev->unit, error); 2762 if (!(dev->flags & DF_FIXEDCLASS)) 2763 devclass_delete_device(dev->devclass, dev); 2764 (void)device_set_driver(dev, NULL); 2765 device_sysctl_fini(dev); 2766 KASSERT(dev->busy == 0, ("attach failed but busy")); 2767 dev->state = DS_NOTPRESENT; 2768 return (error); 2769 } 2770 attachtime = get_cyclecount() - attachtime; 2771 /* 2772 * 4 bits per device is a reasonable value for desktop and server 2773 * hardware with good get_cyclecount() implementations, but may 2774 * need to be adjusted on other platforms. 2775 */ 2776#ifdef RANDOM_DEBUG 2777 printf("%s(): feeding %d bit(s) of entropy from %s%d\n", 2778 __func__, 4, dev->driver->name, dev->unit); 2779#endif 2780 random_harvest(&attachtime, sizeof(attachtime), 4, RANDOM_ATTACH); 2781 device_sysctl_update(dev); 2782 if (dev->busy) 2783 dev->state = DS_BUSY; 2784 else 2785 dev->state = DS_ATTACHED; 2786 dev->flags &= ~DF_DONENOMATCH; 2787 devadded(dev); 2788 return (0); 2789} 2790 2791/** 2792 * @brief Detach a driver from a device 2793 * 2794 * This function is a wrapper around the DEVICE_DETACH() driver 2795 * method. If the call to DEVICE_DETACH() succeeds, it calls 2796 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a 2797 * notification event for user-based device management services and 2798 * cleans up the device's sysctl tree. 2799 * 2800 * @param dev the device to un-initialise 2801 * 2802 * @retval 0 success 2803 * @retval ENXIO no driver was found 2804 * @retval ENOMEM memory allocation failure 2805 * @retval non-zero some other unix error code 2806 */ 2807int 2808device_detach(device_t dev) 2809{ 2810 int error; 2811 2812 GIANT_REQUIRED; 2813 2814 PDEBUG(("%s", DEVICENAME(dev))); 2815 if (dev->state == DS_BUSY) 2816 return (EBUSY); 2817 if (dev->state != DS_ATTACHED) 2818 return (0); 2819 2820 if ((error = DEVICE_DETACH(dev)) != 0) 2821 return (error); 2822 devremoved(dev); 2823 if (!device_is_quiet(dev)) 2824 device_printf(dev, "detached\n"); 2825 if (dev->parent) 2826 BUS_CHILD_DETACHED(dev->parent, dev); 2827 2828 if (!(dev->flags & DF_FIXEDCLASS)) 2829 devclass_delete_device(dev->devclass, dev); 2830 2831 dev->state = DS_NOTPRESENT; 2832 (void)device_set_driver(dev, NULL); 2833 device_sysctl_fini(dev); 2834 2835 return (0); 2836} 2837 2838/** 2839 * @brief Tells a driver to quiesce itself. 2840 * 2841 * This function is a wrapper around the DEVICE_QUIESCE() driver 2842 * method. If the call to DEVICE_QUIESCE() succeeds. 2843 * 2844 * @param dev the device to quiesce 2845 * 2846 * @retval 0 success 2847 * @retval ENXIO no driver was found 2848 * @retval ENOMEM memory allocation failure 2849 * @retval non-zero some other unix error code 2850 */ 2851int 2852device_quiesce(device_t dev) 2853{ 2854 2855 PDEBUG(("%s", DEVICENAME(dev))); 2856 if (dev->state == DS_BUSY) 2857 return (EBUSY); 2858 if (dev->state != DS_ATTACHED) 2859 return (0); 2860 2861 return (DEVICE_QUIESCE(dev)); 2862} 2863 2864/** 2865 * @brief Notify a device of system shutdown 2866 * 2867 * This function calls the DEVICE_SHUTDOWN() driver method if the 2868 * device currently has an attached driver. 2869 * 2870 * @returns the value returned by DEVICE_SHUTDOWN() 2871 */ 2872int 2873device_shutdown(device_t dev) 2874{ 2875 if (dev->state < DS_ATTACHED) 2876 return (0); 2877 return (DEVICE_SHUTDOWN(dev)); 2878} 2879 2880/** 2881 * @brief Set the unit number of a device 2882 * 2883 * This function can be used to override the unit number used for a 2884 * device (e.g. to wire a device to a pre-configured unit number). 2885 */ 2886int 2887device_set_unit(device_t dev, int unit) 2888{ 2889 devclass_t dc; 2890 int err; 2891 2892 dc = device_get_devclass(dev); 2893 if (unit < dc->maxunit && dc->devices[unit]) 2894 return (EBUSY); 2895 err = devclass_delete_device(dc, dev); 2896 if (err) 2897 return (err); 2898 dev->unit = unit; 2899 err = devclass_add_device(dc, dev); 2900 if (err) 2901 return (err); 2902 2903 bus_data_generation_update(); 2904 return (0); 2905} 2906 2907/*======================================*/ 2908/* 2909 * Some useful method implementations to make life easier for bus drivers. 2910 */ 2911 2912/** 2913 * @brief Initialise a resource list. 2914 * 2915 * @param rl the resource list to initialise 2916 */ 2917void 2918resource_list_init(struct resource_list *rl) 2919{ 2920 STAILQ_INIT(rl); 2921} 2922 2923/** 2924 * @brief Reclaim memory used by a resource list. 2925 * 2926 * This function frees the memory for all resource entries on the list 2927 * (if any). 2928 * 2929 * @param rl the resource list to free 2930 */ 2931void 2932resource_list_free(struct resource_list *rl) 2933{ 2934 struct resource_list_entry *rle; 2935 2936 while ((rle = STAILQ_FIRST(rl)) != NULL) { 2937 if (rle->res) 2938 panic("resource_list_free: resource entry is busy"); 2939 STAILQ_REMOVE_HEAD(rl, link); 2940 free(rle, M_BUS); 2941 } 2942} 2943 2944/** 2945 * @brief Add a resource entry. 2946 * 2947 * This function adds a resource entry using the given @p type, @p 2948 * start, @p end and @p count values. A rid value is chosen by 2949 * searching sequentially for the first unused rid starting at zero. 2950 * 2951 * @param rl the resource list to edit 2952 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2953 * @param start the start address of the resource 2954 * @param end the end address of the resource 2955 * @param count XXX end-start+1 2956 */ 2957int 2958resource_list_add_next(struct resource_list *rl, int type, u_long start, 2959 u_long end, u_long count) 2960{ 2961 int rid; 2962 2963 rid = 0; 2964 while (resource_list_find(rl, type, rid) != NULL) 2965 rid++; 2966 resource_list_add(rl, type, rid, start, end, count); 2967 return (rid); 2968} 2969 2970/** 2971 * @brief Add or modify a resource entry. 2972 * 2973 * If an existing entry exists with the same type and rid, it will be 2974 * modified using the given values of @p start, @p end and @p 2975 * count. If no entry exists, a new one will be created using the 2976 * given values. The resource list entry that matches is then returned. 2977 * 2978 * @param rl the resource list to edit 2979 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2980 * @param rid the resource identifier 2981 * @param start the start address of the resource 2982 * @param end the end address of the resource 2983 * @param count XXX end-start+1 2984 */ 2985struct resource_list_entry * 2986resource_list_add(struct resource_list *rl, int type, int rid, 2987 u_long start, u_long end, u_long count) 2988{ 2989 struct resource_list_entry *rle; 2990 2991 rle = resource_list_find(rl, type, rid); 2992 if (!rle) { 2993 rle = malloc(sizeof(struct resource_list_entry), M_BUS, 2994 M_NOWAIT); 2995 if (!rle) 2996 panic("resource_list_add: can't record entry"); 2997 STAILQ_INSERT_TAIL(rl, rle, link); 2998 rle->type = type; 2999 rle->rid = rid; 3000 rle->res = NULL; 3001 rle->flags = 0; 3002 } 3003 3004 if (rle->res) 3005 panic("resource_list_add: resource entry is busy"); 3006 3007 rle->start = start; 3008 rle->end = end; 3009 rle->count = count; 3010 return (rle); 3011} 3012 3013/** 3014 * @brief Determine if a resource entry is busy. 3015 * 3016 * Returns true if a resource entry is busy meaning that it has an 3017 * associated resource that is not an unallocated "reserved" resource. 3018 * 3019 * @param rl the resource list to search 3020 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 3021 * @param rid the resource identifier 3022 * 3023 * @returns Non-zero if the entry is busy, zero otherwise. 3024 */ 3025int 3026resource_list_busy(struct resource_list *rl, int type, int rid) 3027{ 3028 struct resource_list_entry *rle; 3029 3030 rle = resource_list_find(rl, type, rid); 3031 if (rle == NULL || rle->res == NULL) 3032 return (0); 3033 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == RLE_RESERVED) { 3034 KASSERT(!(rman_get_flags(rle->res) & RF_ACTIVE), 3035 ("reserved resource is active")); 3036 return (0); 3037 } 3038 return (1); 3039} 3040 3041/** 3042 * @brief Determine if a resource entry is reserved. 3043 * 3044 * Returns true if a resource entry is reserved meaning that it has an 3045 * associated "reserved" resource. The resource can either be 3046 * allocated or unallocated. 3047 * 3048 * @param rl the resource list to search 3049 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 3050 * @param rid the resource identifier 3051 * 3052 * @returns Non-zero if the entry is reserved, zero otherwise. 3053 */ 3054int 3055resource_list_reserved(struct resource_list *rl, int type, int rid) 3056{ 3057 struct resource_list_entry *rle; 3058 3059 rle = resource_list_find(rl, type, rid); 3060 if (rle != NULL && rle->flags & RLE_RESERVED) 3061 return (1); 3062 return (0); 3063} 3064 3065/** 3066 * @brief Find a resource entry by type and rid. 3067 * 3068 * @param rl the resource list to search 3069 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 3070 * @param rid the resource identifier 3071 * 3072 * @returns the resource entry pointer or NULL if there is no such 3073 * entry. 3074 */ 3075struct resource_list_entry * 3076resource_list_find(struct resource_list *rl, int type, int rid) 3077{ 3078 struct resource_list_entry *rle; 3079 3080 STAILQ_FOREACH(rle, rl, link) { 3081 if (rle->type == type && rle->rid == rid) 3082 return (rle); 3083 } 3084 return (NULL); 3085} 3086 3087/** 3088 * @brief Delete a resource entry. 3089 * 3090 * @param rl the resource list to edit 3091 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 3092 * @param rid the resource identifier 3093 */ 3094void 3095resource_list_delete(struct resource_list *rl, int type, int rid) 3096{ 3097 struct resource_list_entry *rle = resource_list_find(rl, type, rid); 3098 3099 if (rle) { 3100 if (rle->res != NULL) 3101 panic("resource_list_delete: resource has not been released"); 3102 STAILQ_REMOVE(rl, rle, resource_list_entry, link); 3103 free(rle, M_BUS); 3104 } 3105} 3106 3107/** 3108 * @brief Allocate a reserved resource 3109 * 3110 * This can be used by busses to force the allocation of resources 3111 * that are always active in the system even if they are not allocated 3112 * by a driver (e.g. PCI BARs). This function is usually called when 3113 * adding a new child to the bus. The resource is allocated from the 3114 * parent bus when it is reserved. The resource list entry is marked 3115 * with RLE_RESERVED to note that it is a reserved resource. 3116 * 3117 * Subsequent attempts to allocate the resource with 3118 * resource_list_alloc() will succeed the first time and will set 3119 * RLE_ALLOCATED to note that it has been allocated. When a reserved 3120 * resource that has been allocated is released with 3121 * resource_list_release() the resource RLE_ALLOCATED is cleared, but 3122 * the actual resource remains allocated. The resource can be released to 3123 * the parent bus by calling resource_list_unreserve(). 3124 * 3125 * @param rl the resource list to allocate from 3126 * @param bus the parent device of @p child 3127 * @param child the device for which the resource is being reserved 3128 * @param type the type of resource to allocate 3129 * @param rid a pointer to the resource identifier 3130 * @param start hint at the start of the resource range - pass 3131 * @c 0UL for any start address 3132 * @param end hint at the end of the resource range - pass 3133 * @c ~0UL for any end address 3134 * @param count hint at the size of range required - pass @c 1 3135 * for any size 3136 * @param flags any extra flags to control the resource 3137 * allocation - see @c RF_XXX flags in 3138 * <sys/rman.h> for details 3139 * 3140 * @returns the resource which was allocated or @c NULL if no 3141 * resource could be allocated 3142 */ 3143struct resource * 3144resource_list_reserve(struct resource_list *rl, device_t bus, device_t child, 3145 int type, int *rid, u_long start, u_long end, u_long count, u_int flags) 3146{ 3147 struct resource_list_entry *rle = NULL; 3148 int passthrough = (device_get_parent(child) != bus); 3149 struct resource *r; 3150 3151 if (passthrough) 3152 panic( 3153 "resource_list_reserve() should only be called for direct children"); 3154 if (flags & RF_ACTIVE) 3155 panic( 3156 "resource_list_reserve() should only reserve inactive resources"); 3157 3158 r = resource_list_alloc(rl, bus, child, type, rid, start, end, count, 3159 flags); 3160 if (r != NULL) { 3161 rle = resource_list_find(rl, type, *rid); 3162 rle->flags |= RLE_RESERVED; 3163 } 3164 return (r); 3165} 3166 3167/** 3168 * @brief Helper function for implementing BUS_ALLOC_RESOURCE() 3169 * 3170 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list 3171 * and passing the allocation up to the parent of @p bus. This assumes 3172 * that the first entry of @c device_get_ivars(child) is a struct 3173 * resource_list. This also handles 'passthrough' allocations where a 3174 * child is a remote descendant of bus by passing the allocation up to 3175 * the parent of bus. 3176 * 3177 * Typically, a bus driver would store a list of child resources 3178 * somewhere in the child device's ivars (see device_get_ivars()) and 3179 * its implementation of BUS_ALLOC_RESOURCE() would find that list and 3180 * then call resource_list_alloc() to perform the allocation. 3181 * 3182 * @param rl the resource list to allocate from 3183 * @param bus the parent device of @p child 3184 * @param child the device which is requesting an allocation 3185 * @param type the type of resource to allocate 3186 * @param rid a pointer to the resource identifier 3187 * @param start hint at the start of the resource range - pass 3188 * @c 0UL for any start address 3189 * @param end hint at the end of the resource range - pass 3190 * @c ~0UL for any end address 3191 * @param count hint at the size of range required - pass @c 1 3192 * for any size 3193 * @param flags any extra flags to control the resource 3194 * allocation - see @c RF_XXX flags in 3195 * <sys/rman.h> for details 3196 * 3197 * @returns the resource which was allocated or @c NULL if no 3198 * resource could be allocated 3199 */ 3200struct resource * 3201resource_list_alloc(struct resource_list *rl, device_t bus, device_t child, 3202 int type, int *rid, u_long start, u_long end, u_long count, u_int flags) 3203{ 3204 struct resource_list_entry *rle = NULL; 3205 int passthrough = (device_get_parent(child) != bus); 3206 int isdefault = (start == 0UL && end == ~0UL); 3207 3208 if (passthrough) { 3209 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 3210 type, rid, start, end, count, flags)); 3211 } 3212 3213 rle = resource_list_find(rl, type, *rid); 3214 3215 if (!rle) 3216 return (NULL); /* no resource of that type/rid */ 3217 3218 if (rle->res) { 3219 if (rle->flags & RLE_RESERVED) { 3220 if (rle->flags & RLE_ALLOCATED) 3221 return (NULL); 3222 if ((flags & RF_ACTIVE) && 3223 bus_activate_resource(child, type, *rid, 3224 rle->res) != 0) 3225 return (NULL); 3226 rle->flags |= RLE_ALLOCATED; 3227 return (rle->res); 3228 } 3229 panic("resource_list_alloc: resource entry is busy"); 3230 } 3231 3232 if (isdefault) { 3233 start = rle->start; 3234 count = ulmax(count, rle->count); 3235 end = ulmax(rle->end, start + count - 1); 3236 } 3237 3238 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 3239 type, rid, start, end, count, flags); 3240 3241 /* 3242 * Record the new range. 3243 */ 3244 if (rle->res) { 3245 rle->start = rman_get_start(rle->res); 3246 rle->end = rman_get_end(rle->res); 3247 rle->count = count; 3248 } 3249 3250 return (rle->res); 3251} 3252 3253/** 3254 * @brief Helper function for implementing BUS_RELEASE_RESOURCE() 3255 * 3256 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally 3257 * used with resource_list_alloc(). 3258 * 3259 * @param rl the resource list which was allocated from 3260 * @param bus the parent device of @p child 3261 * @param child the device which is requesting a release 3262 * @param type the type of resource to release 3263 * @param rid the resource identifier 3264 * @param res the resource to release 3265 * 3266 * @retval 0 success 3267 * @retval non-zero a standard unix error code indicating what 3268 * error condition prevented the operation 3269 */ 3270int 3271resource_list_release(struct resource_list *rl, device_t bus, device_t child, 3272 int type, int rid, struct resource *res) 3273{ 3274 struct resource_list_entry *rle = NULL; 3275 int passthrough = (device_get_parent(child) != bus); 3276 int error; 3277 3278 if (passthrough) { 3279 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 3280 type, rid, res)); 3281 } 3282 3283 rle = resource_list_find(rl, type, rid); 3284 3285 if (!rle) 3286 panic("resource_list_release: can't find resource"); 3287 if (!rle->res) 3288 panic("resource_list_release: resource entry is not busy"); 3289 if (rle->flags & RLE_RESERVED) { 3290 if (rle->flags & RLE_ALLOCATED) { 3291 if (rman_get_flags(res) & RF_ACTIVE) { 3292 error = bus_deactivate_resource(child, type, 3293 rid, res); 3294 if (error) 3295 return (error); 3296 } 3297 rle->flags &= ~RLE_ALLOCATED; 3298 return (0); 3299 } 3300 return (EINVAL); 3301 } 3302 3303 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 3304 type, rid, res); 3305 if (error) 3306 return (error); 3307 3308 rle->res = NULL; 3309 return (0); 3310} 3311 3312/** 3313 * @brief Release all active resources of a given type 3314 * 3315 * Release all active resources of a specified type. This is intended 3316 * to be used to cleanup resources leaked by a driver after detach or 3317 * a failed attach. 3318 * 3319 * @param rl the resource list which was allocated from 3320 * @param bus the parent device of @p child 3321 * @param child the device whose active resources are being released 3322 * @param type the type of resources to release 3323 * 3324 * @retval 0 success 3325 * @retval EBUSY at least one resource was active 3326 */ 3327int 3328resource_list_release_active(struct resource_list *rl, device_t bus, 3329 device_t child, int type) 3330{ 3331 struct resource_list_entry *rle; 3332 int error, retval; 3333 3334 retval = 0; 3335 STAILQ_FOREACH(rle, rl, link) { 3336 if (rle->type != type) 3337 continue; 3338 if (rle->res == NULL) 3339 continue; 3340 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == 3341 RLE_RESERVED) 3342 continue; 3343 retval = EBUSY; 3344 error = resource_list_release(rl, bus, child, type, 3345 rman_get_rid(rle->res), rle->res); 3346 if (error != 0) 3347 device_printf(bus, 3348 "Failed to release active resource: %d\n", error); 3349 } 3350 return (retval); 3351} 3352 3353 3354/** 3355 * @brief Fully release a reserved resource 3356 * 3357 * Fully releases a resource reserved via resource_list_reserve(). 3358 * 3359 * @param rl the resource list which was allocated from 3360 * @param bus the parent device of @p child 3361 * @param child the device whose reserved resource is being released 3362 * @param type the type of resource to release 3363 * @param rid the resource identifier 3364 * @param res the resource to release 3365 * 3366 * @retval 0 success 3367 * @retval non-zero a standard unix error code indicating what 3368 * error condition prevented the operation 3369 */ 3370int 3371resource_list_unreserve(struct resource_list *rl, device_t bus, device_t child, 3372 int type, int rid) 3373{ 3374 struct resource_list_entry *rle = NULL; 3375 int passthrough = (device_get_parent(child) != bus); 3376 3377 if (passthrough) 3378 panic( 3379 "resource_list_unreserve() should only be called for direct children"); 3380 3381 rle = resource_list_find(rl, type, rid); 3382 3383 if (!rle) 3384 panic("resource_list_unreserve: can't find resource"); 3385 if (!(rle->flags & RLE_RESERVED)) 3386 return (EINVAL); 3387 if (rle->flags & RLE_ALLOCATED) 3388 return (EBUSY); 3389 rle->flags &= ~RLE_RESERVED; 3390 return (resource_list_release(rl, bus, child, type, rid, rle->res)); 3391} 3392 3393/** 3394 * @brief Print a description of resources in a resource list 3395 * 3396 * Print all resources of a specified type, for use in BUS_PRINT_CHILD(). 3397 * The name is printed if at least one resource of the given type is available. 3398 * The format is used to print resource start and end. 3399 * 3400 * @param rl the resource list to print 3401 * @param name the name of @p type, e.g. @c "memory" 3402 * @param type type type of resource entry to print 3403 * @param format printf(9) format string to print resource 3404 * start and end values 3405 * 3406 * @returns the number of characters printed 3407 */ 3408int 3409resource_list_print_type(struct resource_list *rl, const char *name, int type, 3410 const char *format) 3411{ 3412 struct resource_list_entry *rle; 3413 int printed, retval; 3414 3415 printed = 0; 3416 retval = 0; 3417 /* Yes, this is kinda cheating */ 3418 STAILQ_FOREACH(rle, rl, link) { 3419 if (rle->type == type) { 3420 if (printed == 0) 3421 retval += printf(" %s ", name); 3422 else 3423 retval += printf(","); 3424 printed++; 3425 retval += printf(format, rle->start); 3426 if (rle->count > 1) { 3427 retval += printf("-"); 3428 retval += printf(format, rle->start + 3429 rle->count - 1); 3430 } 3431 } 3432 } 3433 return (retval); 3434} 3435 3436/** 3437 * @brief Releases all the resources in a list. 3438 * 3439 * @param rl The resource list to purge. 3440 * 3441 * @returns nothing 3442 */ 3443void 3444resource_list_purge(struct resource_list *rl) 3445{ 3446 struct resource_list_entry *rle; 3447 3448 while ((rle = STAILQ_FIRST(rl)) != NULL) { 3449 if (rle->res) 3450 bus_release_resource(rman_get_device(rle->res), 3451 rle->type, rle->rid, rle->res); 3452 STAILQ_REMOVE_HEAD(rl, link); 3453 free(rle, M_BUS); 3454 } 3455} 3456 3457device_t 3458bus_generic_add_child(device_t dev, u_int order, const char *name, int unit) 3459{ 3460 3461 return (device_add_child_ordered(dev, order, name, unit)); 3462} 3463 3464/** 3465 * @brief Helper function for implementing DEVICE_PROBE() 3466 * 3467 * This function can be used to help implement the DEVICE_PROBE() for 3468 * a bus (i.e. a device which has other devices attached to it). It 3469 * calls the DEVICE_IDENTIFY() method of each driver in the device's 3470 * devclass. 3471 */ 3472int 3473bus_generic_probe(device_t dev) 3474{ 3475 devclass_t dc = dev->devclass; 3476 driverlink_t dl; 3477 3478 TAILQ_FOREACH(dl, &dc->drivers, link) { 3479 /* 3480 * If this driver's pass is too high, then ignore it. 3481 * For most drivers in the default pass, this will 3482 * never be true. For early-pass drivers they will 3483 * only call the identify routines of eligible drivers 3484 * when this routine is called. Drivers for later 3485 * passes should have their identify routines called 3486 * on early-pass busses during BUS_NEW_PASS(). 3487 */ 3488 if (dl->pass > bus_current_pass) 3489 continue; 3490 DEVICE_IDENTIFY(dl->driver, dev); 3491 } 3492 3493 return (0); 3494} 3495 3496/** 3497 * @brief Helper function for implementing DEVICE_ATTACH() 3498 * 3499 * This function can be used to help implement the DEVICE_ATTACH() for 3500 * a bus. It calls device_probe_and_attach() for each of the device's 3501 * children. 3502 */ 3503int 3504bus_generic_attach(device_t dev) 3505{ 3506 device_t child; 3507 3508 TAILQ_FOREACH(child, &dev->children, link) { 3509 device_probe_and_attach(child); 3510 } 3511 3512 return (0); 3513} 3514 3515/** 3516 * @brief Helper function for implementing DEVICE_DETACH() 3517 * 3518 * This function can be used to help implement the DEVICE_DETACH() for 3519 * a bus. It calls device_detach() for each of the device's 3520 * children. 3521 */ 3522int 3523bus_generic_detach(device_t dev) 3524{ 3525 device_t child; 3526 int error; 3527 3528 if (dev->state != DS_ATTACHED) 3529 return (EBUSY); 3530 3531 TAILQ_FOREACH(child, &dev->children, link) { 3532 if ((error = device_detach(child)) != 0) 3533 return (error); 3534 } 3535 3536 return (0); 3537} 3538 3539/** 3540 * @brief Helper function for implementing DEVICE_SHUTDOWN() 3541 * 3542 * This function can be used to help implement the DEVICE_SHUTDOWN() 3543 * for a bus. It calls device_shutdown() for each of the device's 3544 * children. 3545 */ 3546int 3547bus_generic_shutdown(device_t dev) 3548{ 3549 device_t child; 3550 3551 TAILQ_FOREACH(child, &dev->children, link) { 3552 device_shutdown(child); 3553 } 3554 3555 return (0); 3556} 3557 3558/** 3559 * @brief Helper function for implementing DEVICE_SUSPEND() 3560 * 3561 * This function can be used to help implement the DEVICE_SUSPEND() 3562 * for a bus. It calls DEVICE_SUSPEND() for each of the device's 3563 * children. If any call to DEVICE_SUSPEND() fails, the suspend 3564 * operation is aborted and any devices which were suspended are 3565 * resumed immediately by calling their DEVICE_RESUME() methods. 3566 */ 3567int 3568bus_generic_suspend(device_t dev) 3569{ 3570 int error; 3571 device_t child, child2; 3572 3573 TAILQ_FOREACH(child, &dev->children, link) { 3574 error = DEVICE_SUSPEND(child); 3575 if (error) { 3576 for (child2 = TAILQ_FIRST(&dev->children); 3577 child2 && child2 != child; 3578 child2 = TAILQ_NEXT(child2, link)) 3579 DEVICE_RESUME(child2); 3580 return (error); 3581 } 3582 } 3583 return (0); 3584} 3585 3586/** 3587 * @brief Helper function for implementing DEVICE_RESUME() 3588 * 3589 * This function can be used to help implement the DEVICE_RESUME() for 3590 * a bus. It calls DEVICE_RESUME() on each of the device's children. 3591 */ 3592int 3593bus_generic_resume(device_t dev) 3594{ 3595 device_t child; 3596 3597 TAILQ_FOREACH(child, &dev->children, link) { 3598 DEVICE_RESUME(child); 3599 /* if resume fails, there's nothing we can usefully do... */ 3600 } 3601 return (0); 3602} 3603 3604/** 3605 * @brief Helper function for implementing BUS_PRINT_CHILD(). 3606 * 3607 * This function prints the first part of the ascii representation of 3608 * @p child, including its name, unit and description (if any - see 3609 * device_set_desc()). 3610 * 3611 * @returns the number of characters printed 3612 */ 3613int 3614bus_print_child_header(device_t dev, device_t child) 3615{ 3616 int retval = 0; 3617 3618 if (device_get_desc(child)) { 3619 retval += device_printf(child, "<%s>", device_get_desc(child)); 3620 } else { 3621 retval += printf("%s", device_get_nameunit(child)); 3622 } 3623 3624 return (retval); 3625} 3626 3627/** 3628 * @brief Helper function for implementing BUS_PRINT_CHILD(). 3629 * 3630 * This function prints the last part of the ascii representation of 3631 * @p child, which consists of the string @c " on " followed by the 3632 * name and unit of the @p dev. 3633 * 3634 * @returns the number of characters printed 3635 */ 3636int 3637bus_print_child_footer(device_t dev, device_t child) 3638{ 3639 return (printf(" on %s\n", device_get_nameunit(dev))); 3640} 3641 3642/** 3643 * @brief Helper function for implementing BUS_PRINT_CHILD(). 3644 * 3645 * This function simply calls bus_print_child_header() followed by 3646 * bus_print_child_footer(). 3647 * 3648 * @returns the number of characters printed 3649 */ 3650int 3651bus_generic_print_child(device_t dev, device_t child) 3652{ 3653 int retval = 0; 3654 3655 retval += bus_print_child_header(dev, child); 3656 retval += bus_print_child_footer(dev, child); 3657 3658 return (retval); 3659} 3660 3661/** 3662 * @brief Stub function for implementing BUS_READ_IVAR(). 3663 * 3664 * @returns ENOENT 3665 */ 3666int 3667bus_generic_read_ivar(device_t dev, device_t child, int index, 3668 uintptr_t * result) 3669{ 3670 return (ENOENT); 3671} 3672 3673/** 3674 * @brief Stub function for implementing BUS_WRITE_IVAR(). 3675 * 3676 * @returns ENOENT 3677 */ 3678int 3679bus_generic_write_ivar(device_t dev, device_t child, int index, 3680 uintptr_t value) 3681{ 3682 return (ENOENT); 3683} 3684 3685/** 3686 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST(). 3687 * 3688 * @returns NULL 3689 */ 3690struct resource_list * 3691bus_generic_get_resource_list(device_t dev, device_t child) 3692{ 3693 return (NULL); 3694} 3695 3696/** 3697 * @brief Helper function for implementing BUS_DRIVER_ADDED(). 3698 * 3699 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's 3700 * DEVICE_IDENTIFY() method to allow it to add new children to the bus 3701 * and then calls device_probe_and_attach() for each unattached child. 3702 */ 3703void 3704bus_generic_driver_added(device_t dev, driver_t *driver) 3705{ 3706 device_t child; 3707 3708 DEVICE_IDENTIFY(driver, dev); 3709 TAILQ_FOREACH(child, &dev->children, link) { 3710 if (child->state == DS_NOTPRESENT || 3711 (child->flags & DF_REBID)) 3712 device_probe_and_attach(child); 3713 } 3714} 3715 3716/** 3717 * @brief Helper function for implementing BUS_NEW_PASS(). 3718 * 3719 * This implementing of BUS_NEW_PASS() first calls the identify 3720 * routines for any drivers that probe at the current pass. Then it 3721 * walks the list of devices for this bus. If a device is already 3722 * attached, then it calls BUS_NEW_PASS() on that device. If the 3723 * device is not already attached, it attempts to attach a driver to 3724 * it. 3725 */ 3726void 3727bus_generic_new_pass(device_t dev) 3728{ 3729 driverlink_t dl; 3730 devclass_t dc; 3731 device_t child; 3732 3733 dc = dev->devclass; 3734 TAILQ_FOREACH(dl, &dc->drivers, link) { 3735 if (dl->pass == bus_current_pass) 3736 DEVICE_IDENTIFY(dl->driver, dev); 3737 } 3738 TAILQ_FOREACH(child, &dev->children, link) { 3739 if (child->state >= DS_ATTACHED) 3740 BUS_NEW_PASS(child); 3741 else if (child->state == DS_NOTPRESENT) 3742 device_probe_and_attach(child); 3743 } 3744} 3745 3746/** 3747 * @brief Helper function for implementing BUS_SETUP_INTR(). 3748 * 3749 * This simple implementation of BUS_SETUP_INTR() simply calls the 3750 * BUS_SETUP_INTR() method of the parent of @p dev. 3751 */ 3752int 3753bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq, 3754 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg, 3755 void **cookiep) 3756{ 3757 /* Propagate up the bus hierarchy until someone handles it. */ 3758 if (dev->parent) 3759 return (BUS_SETUP_INTR(dev->parent, child, irq, flags, 3760 filter, intr, arg, cookiep)); 3761 return (EINVAL); 3762} 3763 3764/** 3765 * @brief Helper function for implementing BUS_TEARDOWN_INTR(). 3766 * 3767 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the 3768 * BUS_TEARDOWN_INTR() method of the parent of @p dev. 3769 */ 3770int 3771bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq, 3772 void *cookie) 3773{ 3774 /* Propagate up the bus hierarchy until someone handles it. */ 3775 if (dev->parent) 3776 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie)); 3777 return (EINVAL); 3778} 3779 3780/** 3781 * @brief Helper function for implementing BUS_ADJUST_RESOURCE(). 3782 * 3783 * This simple implementation of BUS_ADJUST_RESOURCE() simply calls the 3784 * BUS_ADJUST_RESOURCE() method of the parent of @p dev. 3785 */ 3786int 3787bus_generic_adjust_resource(device_t dev, device_t child, int type, 3788 struct resource *r, u_long start, u_long end) 3789{ 3790 /* Propagate up the bus hierarchy until someone handles it. */ 3791 if (dev->parent) 3792 return (BUS_ADJUST_RESOURCE(dev->parent, child, type, r, start, 3793 end)); 3794 return (EINVAL); 3795} 3796 3797/** 3798 * @brief Helper function for implementing BUS_ALLOC_RESOURCE(). 3799 * 3800 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the 3801 * BUS_ALLOC_RESOURCE() method of the parent of @p dev. 3802 */ 3803struct resource * 3804bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid, 3805 u_long start, u_long end, u_long count, u_int flags) 3806{ 3807 /* Propagate up the bus hierarchy until someone handles it. */ 3808 if (dev->parent) 3809 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid, 3810 start, end, count, flags)); 3811 return (NULL); 3812} 3813 3814/** 3815 * @brief Helper function for implementing BUS_RELEASE_RESOURCE(). 3816 * 3817 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the 3818 * BUS_RELEASE_RESOURCE() method of the parent of @p dev. 3819 */ 3820int 3821bus_generic_release_resource(device_t dev, device_t child, int type, int rid, 3822 struct resource *r) 3823{ 3824 /* Propagate up the bus hierarchy until someone handles it. */ 3825 if (dev->parent) 3826 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, 3827 r)); 3828 return (EINVAL); 3829} 3830 3831/** 3832 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE(). 3833 * 3834 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the 3835 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev. 3836 */ 3837int 3838bus_generic_activate_resource(device_t dev, device_t child, int type, int rid, 3839 struct resource *r) 3840{ 3841 /* Propagate up the bus hierarchy until someone handles it. */ 3842 if (dev->parent) 3843 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, 3844 r)); 3845 return (EINVAL); 3846} 3847 3848/** 3849 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE(). 3850 * 3851 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the 3852 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev. 3853 */ 3854int 3855bus_generic_deactivate_resource(device_t dev, device_t child, int type, 3856 int rid, struct resource *r) 3857{ 3858 /* Propagate up the bus hierarchy until someone handles it. */ 3859 if (dev->parent) 3860 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid, 3861 r)); 3862 return (EINVAL); 3863} 3864 3865/** 3866 * @brief Helper function for implementing BUS_BIND_INTR(). 3867 * 3868 * This simple implementation of BUS_BIND_INTR() simply calls the 3869 * BUS_BIND_INTR() method of the parent of @p dev. 3870 */ 3871int 3872bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq, 3873 int cpu) 3874{ 3875 3876 /* Propagate up the bus hierarchy until someone handles it. */ 3877 if (dev->parent) 3878 return (BUS_BIND_INTR(dev->parent, child, irq, cpu)); 3879 return (EINVAL); 3880} 3881 3882/** 3883 * @brief Helper function for implementing BUS_CONFIG_INTR(). 3884 * 3885 * This simple implementation of BUS_CONFIG_INTR() simply calls the 3886 * BUS_CONFIG_INTR() method of the parent of @p dev. 3887 */ 3888int 3889bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig, 3890 enum intr_polarity pol) 3891{ 3892 3893 /* Propagate up the bus hierarchy until someone handles it. */ 3894 if (dev->parent) 3895 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol)); 3896 return (EINVAL); 3897} 3898 3899/** 3900 * @brief Helper function for implementing BUS_DESCRIBE_INTR(). 3901 * 3902 * This simple implementation of BUS_DESCRIBE_INTR() simply calls the 3903 * BUS_DESCRIBE_INTR() method of the parent of @p dev. 3904 */ 3905int 3906bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq, 3907 void *cookie, const char *descr) 3908{ 3909 3910 /* Propagate up the bus hierarchy until someone handles it. */ 3911 if (dev->parent) 3912 return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie, 3913 descr)); 3914 return (EINVAL); 3915} 3916 3917/** 3918 * @brief Helper function for implementing BUS_GET_DMA_TAG(). 3919 * 3920 * This simple implementation of BUS_GET_DMA_TAG() simply calls the 3921 * BUS_GET_DMA_TAG() method of the parent of @p dev. 3922 */ 3923bus_dma_tag_t 3924bus_generic_get_dma_tag(device_t dev, device_t child) 3925{ 3926 3927 /* Propagate up the bus hierarchy until someone handles it. */ 3928 if (dev->parent != NULL) 3929 return (BUS_GET_DMA_TAG(dev->parent, child)); 3930 return (NULL); 3931} 3932 3933/** 3934 * @brief Helper function for implementing BUS_GET_RESOURCE(). 3935 * 3936 * This implementation of BUS_GET_RESOURCE() uses the 3937 * resource_list_find() function to do most of the work. It calls 3938 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to 3939 * search. 3940 */ 3941int 3942bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid, 3943 u_long *startp, u_long *countp) 3944{ 3945 struct resource_list * rl = NULL; 3946 struct resource_list_entry * rle = NULL; 3947 3948 rl = BUS_GET_RESOURCE_LIST(dev, child); 3949 if (!rl) 3950 return (EINVAL); 3951 3952 rle = resource_list_find(rl, type, rid); 3953 if (!rle) 3954 return (ENOENT); 3955 3956 if (startp) 3957 *startp = rle->start; 3958 if (countp) 3959 *countp = rle->count; 3960 3961 return (0); 3962} 3963 3964/** 3965 * @brief Helper function for implementing BUS_SET_RESOURCE(). 3966 * 3967 * This implementation of BUS_SET_RESOURCE() uses the 3968 * resource_list_add() function to do most of the work. It calls 3969 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to 3970 * edit. 3971 */ 3972int 3973bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid, 3974 u_long start, u_long count) 3975{ 3976 struct resource_list * rl = NULL; 3977 3978 rl = BUS_GET_RESOURCE_LIST(dev, child); 3979 if (!rl) 3980 return (EINVAL); 3981 3982 resource_list_add(rl, type, rid, start, (start + count - 1), count); 3983 3984 return (0); 3985} 3986 3987/** 3988 * @brief Helper function for implementing BUS_DELETE_RESOURCE(). 3989 * 3990 * This implementation of BUS_DELETE_RESOURCE() uses the 3991 * resource_list_delete() function to do most of the work. It calls 3992 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to 3993 * edit. 3994 */ 3995void 3996bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid) 3997{ 3998 struct resource_list * rl = NULL; 3999 4000 rl = BUS_GET_RESOURCE_LIST(dev, child); 4001 if (!rl) 4002 return; 4003 4004 resource_list_delete(rl, type, rid); 4005 4006 return; 4007} 4008 4009/** 4010 * @brief Helper function for implementing BUS_RELEASE_RESOURCE(). 4011 * 4012 * This implementation of BUS_RELEASE_RESOURCE() uses the 4013 * resource_list_release() function to do most of the work. It calls 4014 * BUS_GET_RESOURCE_LIST() to find a suitable resource list. 4015 */ 4016int 4017bus_generic_rl_release_resource(device_t dev, device_t child, int type, 4018 int rid, struct resource *r) 4019{ 4020 struct resource_list * rl = NULL; 4021 4022 if (device_get_parent(child) != dev) 4023 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 4024 type, rid, r)); 4025 4026 rl = BUS_GET_RESOURCE_LIST(dev, child); 4027 if (!rl) 4028 return (EINVAL); 4029 4030 return (resource_list_release(rl, dev, child, type, rid, r)); 4031} 4032 4033/** 4034 * @brief Helper function for implementing BUS_ALLOC_RESOURCE(). 4035 * 4036 * This implementation of BUS_ALLOC_RESOURCE() uses the 4037 * resource_list_alloc() function to do most of the work. It calls 4038 * BUS_GET_RESOURCE_LIST() to find a suitable resource list. 4039 */ 4040struct resource * 4041bus_generic_rl_alloc_resource(device_t dev, device_t child, int type, 4042 int *rid, u_long start, u_long end, u_long count, u_int flags) 4043{ 4044 struct resource_list * rl = NULL; 4045 4046 if (device_get_parent(child) != dev) 4047 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 4048 type, rid, start, end, count, flags)); 4049 4050 rl = BUS_GET_RESOURCE_LIST(dev, child); 4051 if (!rl) 4052 return (NULL); 4053 4054 return (resource_list_alloc(rl, dev, child, type, rid, 4055 start, end, count, flags)); 4056} 4057 4058/** 4059 * @brief Helper function for implementing BUS_CHILD_PRESENT(). 4060 * 4061 * This simple implementation of BUS_CHILD_PRESENT() simply calls the 4062 * BUS_CHILD_PRESENT() method of the parent of @p dev. 4063 */ 4064int 4065bus_generic_child_present(device_t dev, device_t child) 4066{ 4067 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev)); 4068} 4069 4070/* 4071 * Some convenience functions to make it easier for drivers to use the 4072 * resource-management functions. All these really do is hide the 4073 * indirection through the parent's method table, making for slightly 4074 * less-wordy code. In the future, it might make sense for this code 4075 * to maintain some sort of a list of resources allocated by each device. 4076 */ 4077 4078int 4079bus_alloc_resources(device_t dev, struct resource_spec *rs, 4080 struct resource **res) 4081{ 4082 int i; 4083 4084 for (i = 0; rs[i].type != -1; i++) 4085 res[i] = NULL; 4086 for (i = 0; rs[i].type != -1; i++) { 4087 res[i] = bus_alloc_resource_any(dev, 4088 rs[i].type, &rs[i].rid, rs[i].flags); 4089 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) { 4090 bus_release_resources(dev, rs, res); 4091 return (ENXIO); 4092 } 4093 } 4094 return (0); 4095} 4096 4097void 4098bus_release_resources(device_t dev, const struct resource_spec *rs, 4099 struct resource **res) 4100{ 4101 int i; 4102 4103 for (i = 0; rs[i].type != -1; i++) 4104 if (res[i] != NULL) { 4105 bus_release_resource( 4106 dev, rs[i].type, rs[i].rid, res[i]); 4107 res[i] = NULL; 4108 } 4109} 4110 4111/** 4112 * @brief Wrapper function for BUS_ALLOC_RESOURCE(). 4113 * 4114 * This function simply calls the BUS_ALLOC_RESOURCE() method of the 4115 * parent of @p dev. 4116 */ 4117struct resource * 4118bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end, 4119 u_long count, u_int flags) 4120{ 4121 if (dev->parent == NULL) 4122 return (NULL); 4123 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end, 4124 count, flags)); 4125} 4126 4127/** 4128 * @brief Wrapper function for BUS_ADJUST_RESOURCE(). 4129 * 4130 * This function simply calls the BUS_ADJUST_RESOURCE() method of the 4131 * parent of @p dev. 4132 */ 4133int 4134bus_adjust_resource(device_t dev, int type, struct resource *r, u_long start, 4135 u_long end) 4136{ 4137 if (dev->parent == NULL) 4138 return (EINVAL); 4139 return (BUS_ADJUST_RESOURCE(dev->parent, dev, type, r, start, end)); 4140} 4141 4142/** 4143 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE(). 4144 * 4145 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the 4146 * parent of @p dev. 4147 */ 4148int 4149bus_activate_resource(device_t dev, int type, int rid, struct resource *r) 4150{ 4151 if (dev->parent == NULL) 4152 return (EINVAL); 4153 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 4154} 4155 4156/** 4157 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE(). 4158 * 4159 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the 4160 * parent of @p dev. 4161 */ 4162int 4163bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r) 4164{ 4165 if (dev->parent == NULL) 4166 return (EINVAL); 4167 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 4168} 4169 4170/** 4171 * @brief Wrapper function for BUS_RELEASE_RESOURCE(). 4172 * 4173 * This function simply calls the BUS_RELEASE_RESOURCE() method of the 4174 * parent of @p dev. 4175 */ 4176int 4177bus_release_resource(device_t dev, int type, int rid, struct resource *r) 4178{ 4179 if (dev->parent == NULL) 4180 return (EINVAL); 4181 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r)); 4182} 4183 4184/** 4185 * @brief Wrapper function for BUS_SETUP_INTR(). 4186 * 4187 * This function simply calls the BUS_SETUP_INTR() method of the 4188 * parent of @p dev. 4189 */ 4190int 4191bus_setup_intr(device_t dev, struct resource *r, int flags, 4192 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep) 4193{ 4194 int error; 4195 4196 if (dev->parent == NULL) 4197 return (EINVAL); 4198 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler, 4199 arg, cookiep); 4200 if (error != 0) 4201 return (error); 4202 if (handler != NULL && !(flags & INTR_MPSAFE)) 4203 device_printf(dev, "[GIANT-LOCKED]\n"); 4204 return (0); 4205} 4206 4207/** 4208 * @brief Wrapper function for BUS_TEARDOWN_INTR(). 4209 * 4210 * This function simply calls the BUS_TEARDOWN_INTR() method of the 4211 * parent of @p dev. 4212 */ 4213int 4214bus_teardown_intr(device_t dev, struct resource *r, void *cookie) 4215{ 4216 if (dev->parent == NULL) 4217 return (EINVAL); 4218 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie)); 4219} 4220 4221/** 4222 * @brief Wrapper function for BUS_BIND_INTR(). 4223 * 4224 * This function simply calls the BUS_BIND_INTR() method of the 4225 * parent of @p dev. 4226 */ 4227int 4228bus_bind_intr(device_t dev, struct resource *r, int cpu) 4229{ 4230 if (dev->parent == NULL) 4231 return (EINVAL); 4232 return (BUS_BIND_INTR(dev->parent, dev, r, cpu)); 4233} 4234 4235/** 4236 * @brief Wrapper function for BUS_DESCRIBE_INTR(). 4237 * 4238 * This function first formats the requested description into a 4239 * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of 4240 * the parent of @p dev. 4241 */ 4242int 4243bus_describe_intr(device_t dev, struct resource *irq, void *cookie, 4244 const char *fmt, ...) 4245{ 4246 va_list ap; 4247 char descr[MAXCOMLEN + 1]; 4248 4249 if (dev->parent == NULL) 4250 return (EINVAL); 4251 va_start(ap, fmt); 4252 vsnprintf(descr, sizeof(descr), fmt, ap); 4253 va_end(ap); 4254 return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr)); 4255} 4256 4257/** 4258 * @brief Wrapper function for BUS_SET_RESOURCE(). 4259 * 4260 * This function simply calls the BUS_SET_RESOURCE() method of the 4261 * parent of @p dev. 4262 */ 4263int 4264bus_set_resource(device_t dev, int type, int rid, 4265 u_long start, u_long count) 4266{ 4267 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid, 4268 start, count)); 4269} 4270 4271/** 4272 * @brief Wrapper function for BUS_GET_RESOURCE(). 4273 * 4274 * This function simply calls the BUS_GET_RESOURCE() method of the 4275 * parent of @p dev. 4276 */ 4277int 4278bus_get_resource(device_t dev, int type, int rid, 4279 u_long *startp, u_long *countp) 4280{ 4281 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 4282 startp, countp)); 4283} 4284 4285/** 4286 * @brief Wrapper function for BUS_GET_RESOURCE(). 4287 * 4288 * This function simply calls the BUS_GET_RESOURCE() method of the 4289 * parent of @p dev and returns the start value. 4290 */ 4291u_long 4292bus_get_resource_start(device_t dev, int type, int rid) 4293{ 4294 u_long start, count; 4295 int error; 4296 4297 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 4298 &start, &count); 4299 if (error) 4300 return (0); 4301 return (start); 4302} 4303 4304/** 4305 * @brief Wrapper function for BUS_GET_RESOURCE(). 4306 * 4307 * This function simply calls the BUS_GET_RESOURCE() method of the 4308 * parent of @p dev and returns the count value. 4309 */ 4310u_long 4311bus_get_resource_count(device_t dev, int type, int rid) 4312{ 4313 u_long start, count; 4314 int error; 4315 4316 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 4317 &start, &count); 4318 if (error) 4319 return (0); 4320 return (count); 4321} 4322 4323/** 4324 * @brief Wrapper function for BUS_DELETE_RESOURCE(). 4325 * 4326 * This function simply calls the BUS_DELETE_RESOURCE() method of the 4327 * parent of @p dev. 4328 */ 4329void 4330bus_delete_resource(device_t dev, int type, int rid) 4331{ 4332 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid); 4333} 4334 4335/** 4336 * @brief Wrapper function for BUS_CHILD_PRESENT(). 4337 * 4338 * This function simply calls the BUS_CHILD_PRESENT() method of the 4339 * parent of @p dev. 4340 */ 4341int 4342bus_child_present(device_t child) 4343{ 4344 return (BUS_CHILD_PRESENT(device_get_parent(child), child)); 4345} 4346 4347/** 4348 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR(). 4349 * 4350 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the 4351 * parent of @p dev. 4352 */ 4353int 4354bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen) 4355{ 4356 device_t parent; 4357 4358 parent = device_get_parent(child); 4359 if (parent == NULL) { 4360 *buf = '\0'; 4361 return (0); 4362 } 4363 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen)); 4364} 4365 4366/** 4367 * @brief Wrapper function for BUS_CHILD_LOCATION_STR(). 4368 * 4369 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the 4370 * parent of @p dev. 4371 */ 4372int 4373bus_child_location_str(device_t child, char *buf, size_t buflen) 4374{ 4375 device_t parent; 4376 4377 parent = device_get_parent(child); 4378 if (parent == NULL) { 4379 *buf = '\0'; 4380 return (0); 4381 } 4382 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen)); 4383} 4384 4385/** 4386 * @brief Wrapper function for BUS_GET_DMA_TAG(). 4387 * 4388 * This function simply calls the BUS_GET_DMA_TAG() method of the 4389 * parent of @p dev. 4390 */ 4391bus_dma_tag_t 4392bus_get_dma_tag(device_t dev) 4393{ 4394 device_t parent; 4395 4396 parent = device_get_parent(dev); 4397 if (parent == NULL) 4398 return (NULL); 4399 return (BUS_GET_DMA_TAG(parent, dev)); 4400} 4401 4402/* Resume all devices and then notify userland that we're up again. */ 4403static int 4404root_resume(device_t dev) 4405{ 4406 int error; 4407 4408 error = bus_generic_resume(dev); 4409 if (error == 0) 4410 devctl_notify("kern", "power", "resume", NULL); 4411 return (error); 4412} 4413 4414static int 4415root_print_child(device_t dev, device_t child) 4416{ 4417 int retval = 0; 4418 4419 retval += bus_print_child_header(dev, child); 4420 retval += printf("\n"); 4421 4422 return (retval); 4423} 4424 4425static int 4426root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags, 4427 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep) 4428{ 4429 /* 4430 * If an interrupt mapping gets to here something bad has happened. 4431 */ 4432 panic("root_setup_intr"); 4433} 4434 4435/* 4436 * If we get here, assume that the device is permanant and really is 4437 * present in the system. Removable bus drivers are expected to intercept 4438 * this call long before it gets here. We return -1 so that drivers that 4439 * really care can check vs -1 or some ERRNO returned higher in the food 4440 * chain. 4441 */ 4442static int 4443root_child_present(device_t dev, device_t child) 4444{ 4445 return (-1); 4446} 4447 4448static kobj_method_t root_methods[] = { 4449 /* Device interface */ 4450 KOBJMETHOD(device_shutdown, bus_generic_shutdown), 4451 KOBJMETHOD(device_suspend, bus_generic_suspend), 4452 KOBJMETHOD(device_resume, root_resume), 4453 4454 /* Bus interface */ 4455 KOBJMETHOD(bus_print_child, root_print_child), 4456 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar), 4457 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar), 4458 KOBJMETHOD(bus_setup_intr, root_setup_intr), 4459 KOBJMETHOD(bus_child_present, root_child_present), 4460 4461 KOBJMETHOD_END 4462}; 4463 4464static driver_t root_driver = { 4465 "root", 4466 root_methods, 4467 1, /* no softc */ 4468}; 4469 4470device_t root_bus; 4471devclass_t root_devclass; 4472 4473static int 4474root_bus_module_handler(module_t mod, int what, void* arg) 4475{ 4476 switch (what) { 4477 case MOD_LOAD: 4478 TAILQ_INIT(&bus_data_devices); 4479 kobj_class_compile((kobj_class_t) &root_driver); 4480 root_bus = make_device(NULL, "root", 0); 4481 root_bus->desc = "System root bus"; 4482 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver); 4483 root_bus->driver = &root_driver; 4484 root_bus->state = DS_ATTACHED; 4485 root_devclass = devclass_find_internal("root", NULL, FALSE); 4486 devinit(); 4487 return (0); 4488 4489 case MOD_SHUTDOWN: 4490 device_shutdown(root_bus); 4491 return (0); 4492 default: 4493 return (EOPNOTSUPP); 4494 } 4495 4496 return (0); 4497} 4498 4499static moduledata_t root_bus_mod = { 4500 "rootbus", 4501 root_bus_module_handler, 4502 NULL 4503}; 4504DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 4505 4506/** 4507 * @brief Automatically configure devices 4508 * 4509 * This function begins the autoconfiguration process by calling 4510 * device_probe_and_attach() for each child of the @c root0 device. 4511 */ 4512void 4513root_bus_configure(void) 4514{ 4515 4516 PDEBUG((".")); 4517 4518 /* Eventually this will be split up, but this is sufficient for now. */ 4519 bus_set_pass(BUS_PASS_DEFAULT); 4520} 4521 4522/** 4523 * @brief Module handler for registering device drivers 4524 * 4525 * This module handler is used to automatically register device 4526 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls 4527 * devclass_add_driver() for the driver described by the 4528 * driver_module_data structure pointed to by @p arg 4529 */ 4530int 4531driver_module_handler(module_t mod, int what, void *arg) 4532{ 4533 struct driver_module_data *dmd; 4534 devclass_t bus_devclass; 4535 kobj_class_t driver; 4536 int error, pass; 4537 4538 dmd = (struct driver_module_data *)arg; 4539 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE); 4540 error = 0; 4541 4542 switch (what) { 4543 case MOD_LOAD: 4544 if (dmd->dmd_chainevh) 4545 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 4546 4547 pass = dmd->dmd_pass; 4548 driver = dmd->dmd_driver; 4549 PDEBUG(("Loading module: driver %s on bus %s (pass %d)", 4550 DRIVERNAME(driver), dmd->dmd_busname, pass)); 4551 error = devclass_add_driver(bus_devclass, driver, pass, 4552 dmd->dmd_devclass); 4553 break; 4554 4555 case MOD_UNLOAD: 4556 PDEBUG(("Unloading module: driver %s from bus %s", 4557 DRIVERNAME(dmd->dmd_driver), 4558 dmd->dmd_busname)); 4559 error = devclass_delete_driver(bus_devclass, 4560 dmd->dmd_driver); 4561 4562 if (!error && dmd->dmd_chainevh) 4563 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 4564 break; 4565 case MOD_QUIESCE: 4566 PDEBUG(("Quiesce module: driver %s from bus %s", 4567 DRIVERNAME(dmd->dmd_driver), 4568 dmd->dmd_busname)); 4569 error = devclass_quiesce_driver(bus_devclass, 4570 dmd->dmd_driver); 4571 4572 if (!error && dmd->dmd_chainevh) 4573 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 4574 break; 4575 default: 4576 error = EOPNOTSUPP; 4577 break; 4578 } 4579 4580 return (error); 4581} 4582 4583/** 4584 * @brief Enumerate all hinted devices for this bus. 4585 * 4586 * Walks through the hints for this bus and calls the bus_hinted_child 4587 * routine for each one it fines. It searches first for the specific 4588 * bus that's being probed for hinted children (eg isa0), and then for 4589 * generic children (eg isa). 4590 * 4591 * @param dev bus device to enumerate 4592 */ 4593void 4594bus_enumerate_hinted_children(device_t bus) 4595{ 4596 int i; 4597 const char *dname, *busname; 4598 int dunit; 4599 4600 /* 4601 * enumerate all devices on the specific bus 4602 */ 4603 busname = device_get_nameunit(bus); 4604 i = 0; 4605 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0) 4606 BUS_HINTED_CHILD(bus, dname, dunit); 4607 4608 /* 4609 * and all the generic ones. 4610 */ 4611 busname = device_get_name(bus); 4612 i = 0; 4613 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0) 4614 BUS_HINTED_CHILD(bus, dname, dunit); 4615} 4616 4617#ifdef BUS_DEBUG 4618 4619/* the _short versions avoid iteration by not calling anything that prints 4620 * more than oneliners. I love oneliners. 4621 */ 4622 4623static void 4624print_device_short(device_t dev, int indent) 4625{ 4626 if (!dev) 4627 return; 4628 4629 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n", 4630 dev->unit, dev->desc, 4631 (dev->parent? "":"no "), 4632 (TAILQ_EMPTY(&dev->children)? "no ":""), 4633 (dev->flags&DF_ENABLED? "enabled,":"disabled,"), 4634 (dev->flags&DF_FIXEDCLASS? "fixed,":""), 4635 (dev->flags&DF_WILDCARD? "wildcard,":""), 4636 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""), 4637 (dev->flags&DF_REBID? "rebiddable,":""), 4638 (dev->ivars? "":"no "), 4639 (dev->softc? "":"no "), 4640 dev->busy)); 4641} 4642 4643static void 4644print_device(device_t dev, int indent) 4645{ 4646 if (!dev) 4647 return; 4648 4649 print_device_short(dev, indent); 4650 4651 indentprintf(("Parent:\n")); 4652 print_device_short(dev->parent, indent+1); 4653 indentprintf(("Driver:\n")); 4654 print_driver_short(dev->driver, indent+1); 4655 indentprintf(("Devclass:\n")); 4656 print_devclass_short(dev->devclass, indent+1); 4657} 4658 4659void 4660print_device_tree_short(device_t dev, int indent) 4661/* print the device and all its children (indented) */ 4662{ 4663 device_t child; 4664 4665 if (!dev) 4666 return; 4667 4668 print_device_short(dev, indent); 4669 4670 TAILQ_FOREACH(child, &dev->children, link) { 4671 print_device_tree_short(child, indent+1); 4672 } 4673} 4674 4675void 4676print_device_tree(device_t dev, int indent) 4677/* print the device and all its children (indented) */ 4678{ 4679 device_t child; 4680 4681 if (!dev) 4682 return; 4683 4684 print_device(dev, indent); 4685 4686 TAILQ_FOREACH(child, &dev->children, link) { 4687 print_device_tree(child, indent+1); 4688 } 4689} 4690 4691static void 4692print_driver_short(driver_t *driver, int indent) 4693{ 4694 if (!driver) 4695 return; 4696 4697 indentprintf(("driver %s: softc size = %zd\n", 4698 driver->name, driver->size)); 4699} 4700 4701static void 4702print_driver(driver_t *driver, int indent) 4703{ 4704 if (!driver) 4705 return; 4706 4707 print_driver_short(driver, indent); 4708} 4709 4710static void 4711print_driver_list(driver_list_t drivers, int indent) 4712{ 4713 driverlink_t driver; 4714 4715 TAILQ_FOREACH(driver, &drivers, link) { 4716 print_driver(driver->driver, indent); 4717 } 4718} 4719 4720static void 4721print_devclass_short(devclass_t dc, int indent) 4722{ 4723 if ( !dc ) 4724 return; 4725 4726 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit)); 4727} 4728 4729static void 4730print_devclass(devclass_t dc, int indent) 4731{ 4732 int i; 4733 4734 if ( !dc ) 4735 return; 4736 4737 print_devclass_short(dc, indent); 4738 indentprintf(("Drivers:\n")); 4739 print_driver_list(dc->drivers, indent+1); 4740 4741 indentprintf(("Devices:\n")); 4742 for (i = 0; i < dc->maxunit; i++) 4743 if (dc->devices[i]) 4744 print_device(dc->devices[i], indent+1); 4745} 4746 4747void 4748print_devclass_list_short(void) 4749{ 4750 devclass_t dc; 4751 4752 printf("Short listing of devclasses, drivers & devices:\n"); 4753 TAILQ_FOREACH(dc, &devclasses, link) { 4754 print_devclass_short(dc, 0); 4755 } 4756} 4757 4758void 4759print_devclass_list(void) 4760{ 4761 devclass_t dc; 4762 4763 printf("Full listing of devclasses, drivers & devices:\n"); 4764 TAILQ_FOREACH(dc, &devclasses, link) { 4765 print_devclass(dc, 0); 4766 } 4767} 4768 4769#endif 4770 4771/* 4772 * User-space access to the device tree. 4773 * 4774 * We implement a small set of nodes: 4775 * 4776 * hw.bus Single integer read method to obtain the 4777 * current generation count. 4778 * hw.bus.devices Reads the entire device tree in flat space. 4779 * hw.bus.rman Resource manager interface 4780 * 4781 * We might like to add the ability to scan devclasses and/or drivers to 4782 * determine what else is currently loaded/available. 4783 */ 4784 4785static int 4786sysctl_bus(SYSCTL_HANDLER_ARGS) 4787{ 4788 struct u_businfo ubus; 4789 4790 ubus.ub_version = BUS_USER_VERSION; 4791 ubus.ub_generation = bus_data_generation; 4792 4793 return (SYSCTL_OUT(req, &ubus, sizeof(ubus))); 4794} 4795SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus, 4796 "bus-related data"); 4797 4798static int 4799sysctl_devices(SYSCTL_HANDLER_ARGS) 4800{ 4801 int *name = (int *)arg1; 4802 u_int namelen = arg2; 4803 int index; 4804 struct device *dev; 4805 struct u_device udev; /* XXX this is a bit big */ 4806 int error; 4807 4808 if (namelen != 2) 4809 return (EINVAL); 4810 4811 if (bus_data_generation_check(name[0])) 4812 return (EINVAL); 4813 4814 index = name[1]; 4815 4816 /* 4817 * Scan the list of devices, looking for the requested index. 4818 */ 4819 TAILQ_FOREACH(dev, &bus_data_devices, devlink) { 4820 if (index-- == 0) 4821 break; 4822 } 4823 if (dev == NULL) 4824 return (ENOENT); 4825 4826 /* 4827 * Populate the return array. 4828 */ 4829 bzero(&udev, sizeof(udev)); 4830 udev.dv_handle = (uintptr_t)dev; 4831 udev.dv_parent = (uintptr_t)dev->parent; 4832 if (dev->nameunit != NULL) 4833 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name)); 4834 if (dev->desc != NULL) 4835 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc)); 4836 if (dev->driver != NULL && dev->driver->name != NULL) 4837 strlcpy(udev.dv_drivername, dev->driver->name, 4838 sizeof(udev.dv_drivername)); 4839 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo)); 4840 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location)); 4841 udev.dv_devflags = dev->devflags; 4842 udev.dv_flags = dev->flags; 4843 udev.dv_state = dev->state; 4844 error = SYSCTL_OUT(req, &udev, sizeof(udev)); 4845 return (error); 4846} 4847 4848SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices, 4849 "system device tree"); 4850 4851int 4852bus_data_generation_check(int generation) 4853{ 4854 if (generation != bus_data_generation) 4855 return (1); 4856 4857 /* XXX generate optimised lists here? */ 4858 return (0); 4859} 4860 4861void 4862bus_data_generation_update(void) 4863{ 4864 bus_data_generation++; 4865} 4866 4867int 4868bus_free_resource(device_t dev, int type, struct resource *r) 4869{ 4870 if (r == NULL) 4871 return (0); 4872 return (bus_release_resource(dev, type, rman_get_rid(r), r)); 4873} 4874