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