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