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