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