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