1 /*- 2 * Copyright (c) 1997,1998,2003 Doug Rabson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include "opt_bus.h" 31 32 #include <sys/param.h> 33 #include <sys/conf.h> 34 #include <sys/filio.h> 35 #include <sys/lock.h> 36 #include <sys/kernel.h> 37 #include <sys/kobj.h> 38 #include <sys/malloc.h> 39 #include <sys/module.h> 40 #include <sys/mutex.h> 41 #include <sys/poll.h> 42 #include <sys/proc.h> 43 #include <sys/condvar.h> 44 #include <sys/queue.h> 45 #include <machine/bus.h> 46 #include <sys/rman.h> 47 #include <sys/selinfo.h> 48 #include <sys/signalvar.h> 49 #include <sys/sysctl.h> 50 #include <sys/systm.h> 51 #include <sys/uio.h> 52 #include <sys/bus.h> 53 54 #include <machine/stdarg.h> 55 56 #include <vm/uma.h> 57 58 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL); 59 SYSCTL_NODE(, OID_AUTO, dev, CTLFLAG_RW, NULL, NULL); 60 61 /* 62 * Used to attach drivers to devclasses. 63 */ 64 typedef struct driverlink *driverlink_t; 65 struct driverlink { 66 kobj_class_t driver; 67 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */ 68 }; 69 70 /* 71 * Forward declarations 72 */ 73 typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t; 74 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t; 75 typedef TAILQ_HEAD(device_list, device) device_list_t; 76 77 struct devclass { 78 TAILQ_ENTRY(devclass) link; 79 devclass_t parent; /* parent in devclass hierarchy */ 80 driver_list_t drivers; /* bus devclasses store drivers for bus */ 81 char *name; 82 device_t *devices; /* array of devices indexed by unit */ 83 int maxunit; /* size of devices array */ 84 }; 85 86 /* 87 * Implementation of device. 88 */ 89 struct device { 90 /* 91 * A device is a kernel object. The first field must be the 92 * current ops table for the object. 93 */ 94 KOBJ_FIELDS; 95 96 /* 97 * Device hierarchy. 98 */ 99 TAILQ_ENTRY(device) link; /* list of devices in parent */ 100 TAILQ_ENTRY(device) devlink; /* global device list membership */ 101 device_t parent; 102 device_list_t children; /* list of subordinate devices */ 103 104 /* 105 * Details of this device. 106 */ 107 driver_t *driver; 108 devclass_t devclass; /* device class which we are in */ 109 int unit; 110 char* nameunit; /* name+unit e.g. foodev0 */ 111 char* desc; /* driver specific description */ 112 int busy; /* count of calls to device_busy() */ 113 device_state_t state; 114 u_int32_t devflags; /* api level flags for device_get_flags() */ 115 u_short flags; 116 #define DF_ENABLED 1 /* device should be probed/attached */ 117 #define DF_FIXEDCLASS 2 /* devclass specified at create time */ 118 #define DF_WILDCARD 4 /* unit was originally wildcard */ 119 #define DF_DESCMALLOCED 8 /* description was malloced */ 120 #define DF_QUIET 16 /* don't print verbose attach message */ 121 #define DF_DONENOMATCH 32 /* don't execute DEVICE_NOMATCH again */ 122 #define DF_EXTERNALSOFTC 64 /* softc not allocated by us */ 123 u_char order; /* order from device_add_child_ordered() */ 124 u_char pad; 125 void *ivars; 126 void *softc; 127 128 struct sysctl_ctx_list sysctl_ctx; 129 struct sysctl_oid *sysctl_tree; 130 }; 131 132 struct device_op_desc { 133 unsigned int offset; /* offset in driver ops */ 134 struct method* method; /* internal method implementation */ 135 devop_t deflt; /* default implementation */ 136 const char* name; /* unique name (for registration) */ 137 }; 138 139 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures"); 140 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc"); 141 142 #ifdef BUS_DEBUG 143 144 static int bus_debug = 1; 145 TUNABLE_INT("bus.debug", &bus_debug); 146 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RW, &bus_debug, 0, 147 "Debug bus code"); 148 149 #define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");} 150 #define DEVICENAME(d) ((d)? device_get_name(d): "no device") 151 #define DRIVERNAME(d) ((d)? d->name : "no driver") 152 #define DEVCLANAME(d) ((d)? d->name : "no devclass") 153 154 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to 155 * prevent syslog from deleting initial spaces 156 */ 157 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0) 158 159 static void print_device_short(device_t dev, int indent); 160 static void print_device(device_t dev, int indent); 161 void print_device_tree_short(device_t dev, int indent); 162 void print_device_tree(device_t dev, int indent); 163 static void print_driver_short(driver_t *driver, int indent); 164 static void print_driver(driver_t *driver, int indent); 165 static void print_driver_list(driver_list_t drivers, int indent); 166 static void print_devclass_short(devclass_t dc, int indent); 167 static void print_devclass(devclass_t dc, int indent); 168 void print_devclass_list_short(void); 169 void print_devclass_list(void); 170 171 #else 172 /* Make the compiler ignore the function calls */ 173 #define PDEBUG(a) /* nop */ 174 #define DEVICENAME(d) /* nop */ 175 #define DRIVERNAME(d) /* nop */ 176 #define DEVCLANAME(d) /* nop */ 177 178 #define print_device_short(d,i) /* nop */ 179 #define print_device(d,i) /* nop */ 180 #define print_device_tree_short(d,i) /* nop */ 181 #define print_device_tree(d,i) /* nop */ 182 #define print_driver_short(d,i) /* nop */ 183 #define print_driver(d,i) /* nop */ 184 #define print_driver_list(d,i) /* nop */ 185 #define print_devclass_short(d,i) /* nop */ 186 #define print_devclass(d,i) /* nop */ 187 #define print_devclass_list_short() /* nop */ 188 #define print_devclass_list() /* nop */ 189 #endif 190 191 /* 192 * dev sysctl tree 193 */ 194 195 enum { 196 DEVICE_SYSCTL_CLASS, 197 DEVICE_SYSCTL_DESC, 198 DEVICE_SYSCTL_DRIVER, 199 DEVICE_SYSCTL_LOCATION, 200 DEVICE_SYSCTL_PNPINFO, 201 }; 202 203 static int 204 device_sysctl_handler(SYSCTL_HANDLER_ARGS) 205 { 206 device_t dev = (device_t)arg1; 207 const char *value; 208 char *buf; 209 int error; 210 211 buf = NULL; 212 switch (arg2) { 213 case DEVICE_SYSCTL_CLASS: 214 value = dev->devclass ? dev->devclass->name : NULL; 215 break; 216 case DEVICE_SYSCTL_DESC: 217 value = dev->desc; 218 break; 219 case DEVICE_SYSCTL_DRIVER: 220 value = dev->driver ? dev->driver->name : NULL; 221 break; 222 case DEVICE_SYSCTL_LOCATION: 223 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO); 224 bus_child_location_str(dev, buf, 1024); 225 break; 226 case DEVICE_SYSCTL_PNPINFO: 227 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO); 228 bus_child_pnpinfo_str(dev, buf, 1024); 229 break; 230 default: 231 return (EINVAL); 232 } 233 if (value == NULL) 234 value = "?"; 235 error = SYSCTL_OUT(req, value, strlen(value)); 236 if (buf != NULL) 237 free(buf, M_BUS); 238 return (error); 239 } 240 241 static void 242 device_sysctl_init(device_t dev) 243 { 244 struct sysctl_oid_list *parent_node; 245 246 if (dev->parent) { 247 if (dev->parent->sysctl_tree == NULL) 248 device_sysctl_init(dev->parent); 249 parent_node = SYSCTL_CHILDREN(dev->parent->sysctl_tree); 250 } else { 251 parent_node = SYSCTL_STATIC_CHILDREN(_dev); 252 } 253 if (dev->sysctl_tree != NULL) { 254 sysctl_move_oid(dev->sysctl_tree, parent_node); 255 return; 256 } 257 sysctl_ctx_init(&dev->sysctl_ctx); 258 dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx, parent_node, 259 OID_AUTO, dev->nameunit, CTLFLAG_RD, 0, ""); 260 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 261 OID_AUTO, "%class", CTLFLAG_RD, 262 dev, DEVICE_SYSCTL_CLASS, device_sysctl_handler, "A", 263 "device class name"); 264 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 265 OID_AUTO, "%desc", CTLFLAG_RD, 266 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A", 267 "device description"); 268 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 269 OID_AUTO, "%driver", CTLFLAG_RD, 270 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A", 271 "device driver name"); 272 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 273 OID_AUTO, "%location", CTLFLAG_RD, 274 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A", 275 "device location relative to parent"); 276 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 277 OID_AUTO, "%pnpinfo", CTLFLAG_RD, 278 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A", 279 "device identification"); 280 } 281 282 static void 283 device_sysctl_fini(device_t dev) 284 { 285 if (dev->sysctl_tree == NULL) 286 return; 287 sysctl_ctx_free(&dev->sysctl_ctx); 288 dev->sysctl_tree = NULL; 289 } 290 291 /* 292 * /dev/devctl implementation 293 */ 294 295 /* 296 * This design allows only one reader for /dev/devctl. This is not desirable 297 * in the long run, but will get a lot of hair out of this implementation. 298 * Maybe we should make this device a clonable device. 299 * 300 * Also note: we specifically do not attach a device to the device_t tree 301 * to avoid potential chicken and egg problems. One could argue that all 302 * of this belongs to the root node. One could also further argue that the 303 * sysctl interface that we have not might more properly be an ioctl 304 * interface, but at this stage of the game, I'm not inclined to rock that 305 * boat. 306 * 307 * I'm also not sure that the SIGIO support is done correctly or not, as 308 * I copied it from a driver that had SIGIO support that likely hasn't been 309 * tested since 3.4 or 2.2.8! 310 */ 311 312 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS); 313 static int devctl_disable = 0; 314 TUNABLE_INT("hw.bus.devctl_disable", &devctl_disable); 315 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, 316 CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0, sysctl_devctl_disable, 317 "I", "devctl disable"); 318 319 static d_open_t devopen; 320 static d_close_t devclose; 321 static d_read_t devread; 322 static d_ioctl_t devioctl; 323 static d_poll_t devpoll; 324 325 #define CDEV_MAJOR 173 326 static struct cdevsw dev_cdevsw = { 327 .d_version = D_VERSION, 328 .d_flags = D_NEEDGIANT, 329 .d_open = devopen, 330 .d_close = devclose, 331 .d_read = devread, 332 .d_ioctl = devioctl, 333 .d_poll = devpoll, 334 .d_name = "devctl", 335 .d_maj = CDEV_MAJOR, 336 }; 337 338 struct dev_event_info 339 { 340 char *dei_data; 341 TAILQ_ENTRY(dev_event_info) dei_link; 342 }; 343 344 TAILQ_HEAD(devq, dev_event_info); 345 346 static struct dev_softc 347 { 348 int inuse; 349 int nonblock; 350 struct mtx mtx; 351 struct cv cv; 352 struct selinfo sel; 353 struct devq devq; 354 struct proc *async_proc; 355 } devsoftc; 356 357 static dev_t devctl_dev; 358 359 static void 360 devinit(void) 361 { 362 devctl_dev = make_dev(&dev_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, 363 "devctl"); 364 mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF); 365 cv_init(&devsoftc.cv, "dev cv"); 366 TAILQ_INIT(&devsoftc.devq); 367 } 368 369 static int 370 devopen(dev_t dev, int oflags, int devtype, d_thread_t *td) 371 { 372 if (devsoftc.inuse) 373 return (EBUSY); 374 /* move to init */ 375 devsoftc.inuse = 1; 376 devsoftc.nonblock = 0; 377 devsoftc.async_proc = NULL; 378 return (0); 379 } 380 381 static int 382 devclose(dev_t dev, int fflag, int devtype, d_thread_t *td) 383 { 384 devsoftc.inuse = 0; 385 mtx_lock(&devsoftc.mtx); 386 cv_broadcast(&devsoftc.cv); 387 mtx_unlock(&devsoftc.mtx); 388 389 return (0); 390 } 391 392 /* 393 * The read channel for this device is used to report changes to 394 * userland in realtime. We are required to free the data as well as 395 * the n1 object because we allocate them separately. Also note that 396 * we return one record at a time. If you try to read this device a 397 * character at a time, you will loose the rest of the data. Listening 398 * programs are expected to cope. 399 */ 400 static int 401 devread(dev_t dev, struct uio *uio, int ioflag) 402 { 403 struct dev_event_info *n1; 404 int rv; 405 406 mtx_lock(&devsoftc.mtx); 407 while (TAILQ_EMPTY(&devsoftc.devq)) { 408 if (devsoftc.nonblock) { 409 mtx_unlock(&devsoftc.mtx); 410 return (EAGAIN); 411 } 412 rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx); 413 if (rv) { 414 /* 415 * Need to translate ERESTART to EINTR here? -- jake 416 */ 417 mtx_unlock(&devsoftc.mtx); 418 return (rv); 419 } 420 } 421 n1 = TAILQ_FIRST(&devsoftc.devq); 422 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link); 423 mtx_unlock(&devsoftc.mtx); 424 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio); 425 free(n1->dei_data, M_BUS); 426 free(n1, M_BUS); 427 return (rv); 428 } 429 430 static int 431 devioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, d_thread_t *td) 432 { 433 switch (cmd) { 434 435 case FIONBIO: 436 if (*(int*)data) 437 devsoftc.nonblock = 1; 438 else 439 devsoftc.nonblock = 0; 440 return (0); 441 case FIOASYNC: 442 if (*(int*)data) 443 devsoftc.async_proc = td->td_proc; 444 else 445 devsoftc.async_proc = NULL; 446 return (0); 447 448 /* (un)Support for other fcntl() calls. */ 449 case FIOCLEX: 450 case FIONCLEX: 451 case FIONREAD: 452 case FIOSETOWN: 453 case FIOGETOWN: 454 default: 455 break; 456 } 457 return (ENOTTY); 458 } 459 460 static int 461 devpoll(dev_t dev, int events, d_thread_t *td) 462 { 463 int revents = 0; 464 465 mtx_lock(&devsoftc.mtx); 466 if (events & (POLLIN | POLLRDNORM)) { 467 if (!TAILQ_EMPTY(&devsoftc.devq)) 468 revents = events & (POLLIN | POLLRDNORM); 469 else 470 selrecord(td, &devsoftc.sel); 471 } 472 mtx_unlock(&devsoftc.mtx); 473 474 return (revents); 475 } 476 477 /* 478 * Generic interface to queue data to the devctl device. It is 479 * assumed that data is properly formatted. It is further assumed 480 * that data is allocated. 481 */ 482 void 483 devctl_queue_data(char *data) 484 { 485 struct dev_event_info *n1 = NULL; 486 struct proc *p; 487 488 n1 = malloc(sizeof(*n1), M_BUS, M_NOWAIT); 489 if (n1 == NULL) 490 return; 491 n1->dei_data = data; 492 mtx_lock(&devsoftc.mtx); 493 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link); 494 cv_broadcast(&devsoftc.cv); 495 mtx_unlock(&devsoftc.mtx); 496 selwakeup(&devsoftc.sel); 497 p = devsoftc.async_proc; 498 if (p != NULL) { 499 PROC_LOCK(p); 500 psignal(p, SIGIO); 501 PROC_UNLOCK(p); 502 } 503 } 504 505 /* 506 * Send a 'notification' to userland, using standard ways 507 */ 508 void 509 devctl_notify(const char *system, const char *subsystem, const char *type, 510 const char *data) 511 { 512 int len = 0; 513 char *msg; 514 515 if (system == NULL) 516 return; /* BOGUS! Must specify system. */ 517 if (subsystem == NULL) 518 return; /* BOGUS! Must specify subsystem. */ 519 if (type == NULL) 520 return; /* BOGUS! Must specify type. */ 521 len += strlen(" system=") + strlen(system); 522 len += strlen(" subsystem=") + strlen(subsystem); 523 len += strlen(" type=") + strlen(type); 524 /* add in the data message plus newline. */ 525 if (data != NULL) 526 len += strlen(data); 527 len += 3; /* '!', '\n', and NUL */ 528 msg = malloc(len, M_BUS, M_NOWAIT); 529 if (msg == NULL) 530 return; /* Drop it on the floor */ 531 snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n", system, 532 subsystem, type, data); 533 devctl_queue_data(msg); 534 } 535 536 /* 537 * Common routine that tries to make sending messages as easy as possible. 538 * We allocate memory for the data, copy strings into that, but do not 539 * free it unless there's an error. The dequeue part of the driver should 540 * free the data. We don't send data when the device is disabled. We do 541 * send data, even when we have no listeners, because we wish to avoid 542 * races relating to startup and restart of listening applications. 543 */ 544 static void 545 devaddq(const char *type, const char *what, device_t dev) 546 { 547 char *data = NULL; 548 char *loc; 549 const char *parstr; 550 551 if (devctl_disable) 552 return; 553 data = malloc(1024, M_BUS, M_NOWAIT); 554 if (data == NULL) 555 goto bad; 556 loc = malloc(1024, M_BUS, M_NOWAIT); 557 if (loc == NULL) 558 goto bad; 559 *loc = '\0'; 560 bus_child_location_str(dev, loc, 1024); 561 if (device_get_parent(dev) == NULL) 562 parstr = "."; /* Or '/' ? */ 563 else 564 parstr = device_get_nameunit(device_get_parent(dev)); 565 snprintf(data, 1024, "%s%s at %s on %s\n", type, what, loc, parstr); 566 free(loc, M_BUS); 567 devctl_queue_data(data); 568 return; 569 bad: 570 free(data, M_BUS); 571 return; 572 } 573 574 /* 575 * A device was added to the tree. We are called just after it successfully 576 * attaches (that is, probe and attach success for this device). No call 577 * is made if a device is merely parented into the tree. See devnomatch 578 * if probe fails. If attach fails, no notification is sent (but maybe 579 * we should have a different message for this). 580 */ 581 static void 582 devadded(device_t dev) 583 { 584 devaddq("+", device_get_nameunit(dev), dev); 585 } 586 587 /* 588 * A device was removed from the tree. We are called just before this 589 * happens. 590 */ 591 static void 592 devremoved(device_t dev) 593 { 594 devaddq("-", device_get_nameunit(dev), dev); 595 } 596 597 /* 598 * Called when there's no match for this device. This is only called 599 * the first time that no match happens, so we don't keep getitng this 600 * message. Should that prove to be undesirable, we can change it. 601 * This is called when all drivers that can attach to a given bus 602 * decline to accept this device. Other errrors may not be detected. 603 */ 604 static void 605 devnomatch(device_t dev) 606 { 607 char *pnp = NULL; 608 609 pnp = malloc(1024, M_BUS, M_NOWAIT); 610 if (pnp == NULL) 611 return; 612 *pnp = '\0'; 613 bus_child_pnpinfo_str(dev, pnp, 1024); 614 devaddq("?", pnp, dev); 615 free(pnp, M_BUS); 616 return; 617 } 618 619 static int 620 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS) 621 { 622 struct dev_event_info *n1; 623 int dis, error; 624 625 dis = devctl_disable; 626 error = sysctl_handle_int(oidp, &dis, 0, req); 627 if (error || !req->newptr) 628 return (error); 629 mtx_lock(&devsoftc.mtx); 630 devctl_disable = dis; 631 if (dis) { 632 while (!TAILQ_EMPTY(&devsoftc.devq)) { 633 n1 = TAILQ_FIRST(&devsoftc.devq); 634 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link); 635 free(n1->dei_data, M_BUS); 636 free(n1, M_BUS); 637 } 638 } 639 mtx_unlock(&devsoftc.mtx); 640 return (0); 641 } 642 643 /* End of /dev/devctl code */ 644 645 TAILQ_HEAD(,device) bus_data_devices; 646 static int bus_data_generation = 1; 647 648 kobj_method_t null_methods[] = { 649 { 0, 0 } 650 }; 651 652 DEFINE_CLASS(null, null_methods, 0); 653 654 /* 655 * Devclass implementation 656 */ 657 658 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses); 659 660 static devclass_t 661 devclass_find_internal(const char *classname, const char *parentname, 662 int create) 663 { 664 devclass_t dc; 665 666 PDEBUG(("looking for %s", classname)); 667 if (!classname) 668 return (NULL); 669 670 TAILQ_FOREACH(dc, &devclasses, link) { 671 if (!strcmp(dc->name, classname)) 672 break; 673 } 674 675 if (create && !dc) { 676 PDEBUG(("creating %s", classname)); 677 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1, 678 M_BUS, M_NOWAIT|M_ZERO); 679 if (!dc) 680 return (NULL); 681 dc->parent = NULL; 682 dc->name = (char*) (dc + 1); 683 strcpy(dc->name, classname); 684 TAILQ_INIT(&dc->drivers); 685 TAILQ_INSERT_TAIL(&devclasses, dc, link); 686 687 bus_data_generation_update(); 688 } 689 if (parentname && dc && !dc->parent) { 690 dc->parent = devclass_find_internal(parentname, 0, FALSE); 691 } 692 693 return (dc); 694 } 695 696 devclass_t 697 devclass_create(const char *classname) 698 { 699 return (devclass_find_internal(classname, 0, TRUE)); 700 } 701 702 devclass_t 703 devclass_find(const char *classname) 704 { 705 return (devclass_find_internal(classname, 0, FALSE)); 706 } 707 708 int 709 devclass_add_driver(devclass_t dc, driver_t *driver) 710 { 711 driverlink_t dl; 712 int i; 713 714 PDEBUG(("%s", DRIVERNAME(driver))); 715 716 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO); 717 if (!dl) 718 return (ENOMEM); 719 720 /* 721 * Compile the driver's methods. Also increase the reference count 722 * so that the class doesn't get freed when the last instance 723 * goes. This means we can safely use static methods and avoids a 724 * double-free in devclass_delete_driver. 725 */ 726 kobj_class_compile((kobj_class_t) driver); 727 728 /* 729 * Make sure the devclass which the driver is implementing exists. 730 */ 731 devclass_find_internal(driver->name, 0, TRUE); 732 733 dl->driver = driver; 734 TAILQ_INSERT_TAIL(&dc->drivers, dl, link); 735 driver->refs++; 736 737 /* 738 * Call BUS_DRIVER_ADDED for any existing busses in this class. 739 */ 740 for (i = 0; i < dc->maxunit; i++) 741 if (dc->devices[i]) 742 BUS_DRIVER_ADDED(dc->devices[i], driver); 743 744 bus_data_generation_update(); 745 return (0); 746 } 747 748 int 749 devclass_delete_driver(devclass_t busclass, driver_t *driver) 750 { 751 devclass_t dc = devclass_find(driver->name); 752 driverlink_t dl; 753 device_t dev; 754 int i; 755 int error; 756 757 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass))); 758 759 if (!dc) 760 return (0); 761 762 /* 763 * Find the link structure in the bus' list of drivers. 764 */ 765 TAILQ_FOREACH(dl, &busclass->drivers, link) { 766 if (dl->driver == driver) 767 break; 768 } 769 770 if (!dl) { 771 PDEBUG(("%s not found in %s list", driver->name, 772 busclass->name)); 773 return (ENOENT); 774 } 775 776 /* 777 * Disassociate from any devices. We iterate through all the 778 * devices in the devclass of the driver and detach any which are 779 * using the driver and which have a parent in the devclass which 780 * we are deleting from. 781 * 782 * Note that since a driver can be in multiple devclasses, we 783 * should not detach devices which are not children of devices in 784 * the affected devclass. 785 */ 786 for (i = 0; i < dc->maxunit; i++) { 787 if (dc->devices[i]) { 788 dev = dc->devices[i]; 789 if (dev->driver == driver && dev->parent && 790 dev->parent->devclass == busclass) { 791 if ((error = device_detach(dev)) != 0) 792 return (error); 793 device_set_driver(dev, NULL); 794 } 795 } 796 } 797 798 TAILQ_REMOVE(&busclass->drivers, dl, link); 799 free(dl, M_BUS); 800 801 driver->refs--; 802 if (driver->refs == 0) 803 kobj_class_free((kobj_class_t) driver); 804 805 bus_data_generation_update(); 806 return (0); 807 } 808 809 static driverlink_t 810 devclass_find_driver_internal(devclass_t dc, const char *classname) 811 { 812 driverlink_t dl; 813 814 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc))); 815 816 TAILQ_FOREACH(dl, &dc->drivers, link) { 817 if (!strcmp(dl->driver->name, classname)) 818 return (dl); 819 } 820 821 PDEBUG(("not found")); 822 return (NULL); 823 } 824 825 kobj_class_t 826 devclass_find_driver(devclass_t dc, const char *classname) 827 { 828 driverlink_t dl; 829 830 dl = devclass_find_driver_internal(dc, classname); 831 if (dl) 832 return (dl->driver); 833 return (NULL); 834 } 835 836 const char * 837 devclass_get_name(devclass_t dc) 838 { 839 return (dc->name); 840 } 841 842 device_t 843 devclass_get_device(devclass_t dc, int unit) 844 { 845 if (dc == NULL || unit < 0 || unit >= dc->maxunit) 846 return (NULL); 847 return (dc->devices[unit]); 848 } 849 850 void * 851 devclass_get_softc(devclass_t dc, int unit) 852 { 853 device_t dev; 854 855 dev = devclass_get_device(dc, unit); 856 if (!dev) 857 return (NULL); 858 859 return (device_get_softc(dev)); 860 } 861 862 int 863 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp) 864 { 865 int i; 866 int count; 867 device_t *list; 868 869 count = 0; 870 for (i = 0; i < dc->maxunit; i++) 871 if (dc->devices[i]) 872 count++; 873 874 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO); 875 if (!list) 876 return (ENOMEM); 877 878 count = 0; 879 for (i = 0; i < dc->maxunit; i++) { 880 if (dc->devices[i]) { 881 list[count] = dc->devices[i]; 882 count++; 883 } 884 } 885 886 *devlistp = list; 887 *devcountp = count; 888 889 return (0); 890 } 891 892 int 893 devclass_get_maxunit(devclass_t dc) 894 { 895 return (dc->maxunit); 896 } 897 898 int 899 devclass_find_free_unit(devclass_t dc, int unit) 900 { 901 if (dc == NULL) 902 return (unit); 903 while (unit < dc->maxunit && dc->devices[unit] != NULL) 904 unit++; 905 return (unit); 906 } 907 908 void 909 devclass_set_parent(devclass_t dc, devclass_t pdc) 910 { 911 dc->parent = pdc; 912 } 913 914 devclass_t 915 devclass_get_parent(devclass_t dc) 916 { 917 return (dc->parent); 918 } 919 920 static int 921 devclass_alloc_unit(devclass_t dc, int *unitp) 922 { 923 int unit = *unitp; 924 925 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc))); 926 927 /* If we were given a wired unit number, check for existing device */ 928 /* XXX imp XXX */ 929 if (unit != -1) { 930 if (unit >= 0 && unit < dc->maxunit && 931 dc->devices[unit] != NULL) { 932 if (bootverbose) 933 printf("%s: %s%d already exists; skipping it\n", 934 dc->name, dc->name, *unitp); 935 return (EEXIST); 936 } 937 } else { 938 /* Unwired device, find the next available slot for it */ 939 unit = 0; 940 while (unit < dc->maxunit && dc->devices[unit] != NULL) 941 unit++; 942 } 943 944 /* 945 * We've selected a unit beyond the length of the table, so let's 946 * extend the table to make room for all units up to and including 947 * this one. 948 */ 949 if (unit >= dc->maxunit) { 950 device_t *newlist; 951 int newsize; 952 953 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t)); 954 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT); 955 if (!newlist) 956 return (ENOMEM); 957 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit); 958 bzero(newlist + dc->maxunit, 959 sizeof(device_t) * (newsize - dc->maxunit)); 960 if (dc->devices) 961 free(dc->devices, M_BUS); 962 dc->devices = newlist; 963 dc->maxunit = newsize; 964 } 965 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc))); 966 967 *unitp = unit; 968 return (0); 969 } 970 971 static int 972 devclass_add_device(devclass_t dc, device_t dev) 973 { 974 int buflen, error; 975 976 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 977 978 buflen = snprintf(NULL, 0, "%s%d$", dc->name, dev->unit); 979 if (buflen < 0) 980 return (ENOMEM); 981 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO); 982 if (!dev->nameunit) 983 return (ENOMEM); 984 985 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) { 986 free(dev->nameunit, M_BUS); 987 dev->nameunit = NULL; 988 return (error); 989 } 990 dc->devices[dev->unit] = dev; 991 dev->devclass = dc; 992 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit); 993 994 return (0); 995 } 996 997 static int 998 devclass_delete_device(devclass_t dc, device_t dev) 999 { 1000 if (!dc || !dev) 1001 return (0); 1002 1003 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 1004 1005 if (dev->devclass != dc || dc->devices[dev->unit] != dev) 1006 panic("devclass_delete_device: inconsistent device class"); 1007 dc->devices[dev->unit] = NULL; 1008 if (dev->flags & DF_WILDCARD) 1009 dev->unit = -1; 1010 dev->devclass = NULL; 1011 free(dev->nameunit, M_BUS); 1012 dev->nameunit = NULL; 1013 1014 return (0); 1015 } 1016 1017 static device_t 1018 make_device(device_t parent, const char *name, int unit) 1019 { 1020 device_t dev; 1021 devclass_t dc; 1022 1023 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit)); 1024 1025 if (name) { 1026 dc = devclass_find_internal(name, 0, TRUE); 1027 if (!dc) { 1028 printf("make_device: can't find device class %s\n", 1029 name); 1030 return (NULL); 1031 } 1032 } else { 1033 dc = NULL; 1034 } 1035 1036 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO); 1037 if (!dev) 1038 return (NULL); 1039 1040 dev->parent = parent; 1041 TAILQ_INIT(&dev->children); 1042 kobj_init((kobj_t) dev, &null_class); 1043 dev->driver = NULL; 1044 dev->devclass = NULL; 1045 dev->unit = unit; 1046 dev->nameunit = NULL; 1047 dev->desc = NULL; 1048 dev->busy = 0; 1049 dev->devflags = 0; 1050 dev->flags = DF_ENABLED; 1051 dev->order = 0; 1052 if (unit == -1) 1053 dev->flags |= DF_WILDCARD; 1054 if (name) { 1055 dev->flags |= DF_FIXEDCLASS; 1056 if (devclass_add_device(dc, dev)) { 1057 kobj_delete((kobj_t) dev, M_BUS); 1058 return (NULL); 1059 } 1060 } 1061 dev->ivars = NULL; 1062 dev->softc = NULL; 1063 1064 dev->state = DS_NOTPRESENT; 1065 1066 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink); 1067 bus_data_generation_update(); 1068 1069 return (dev); 1070 } 1071 1072 static int 1073 device_print_child(device_t dev, device_t child) 1074 { 1075 int retval = 0; 1076 1077 if (device_is_alive(child)) 1078 retval += BUS_PRINT_CHILD(dev, child); 1079 else 1080 retval += device_printf(child, " not found\n"); 1081 1082 return (retval); 1083 } 1084 1085 device_t 1086 device_add_child(device_t dev, const char *name, int unit) 1087 { 1088 return (device_add_child_ordered(dev, 0, name, unit)); 1089 } 1090 1091 device_t 1092 device_add_child_ordered(device_t dev, int order, const char *name, int unit) 1093 { 1094 device_t child; 1095 device_t place; 1096 1097 PDEBUG(("%s at %s with order %d as unit %d", 1098 name, DEVICENAME(dev), order, unit)); 1099 1100 child = make_device(dev, name, unit); 1101 if (child == NULL) 1102 return (child); 1103 child->order = order; 1104 1105 TAILQ_FOREACH(place, &dev->children, link) { 1106 if (place->order > order) 1107 break; 1108 } 1109 1110 if (place) { 1111 /* 1112 * The device 'place' is the first device whose order is 1113 * greater than the new child. 1114 */ 1115 TAILQ_INSERT_BEFORE(place, child, link); 1116 } else { 1117 /* 1118 * The new child's order is greater or equal to the order of 1119 * any existing device. Add the child to the tail of the list. 1120 */ 1121 TAILQ_INSERT_TAIL(&dev->children, child, link); 1122 } 1123 1124 bus_data_generation_update(); 1125 return (child); 1126 } 1127 1128 int 1129 device_delete_child(device_t dev, device_t child) 1130 { 1131 int error; 1132 device_t grandchild; 1133 1134 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev))); 1135 1136 /* remove children first */ 1137 while ( (grandchild = TAILQ_FIRST(&child->children)) ) { 1138 error = device_delete_child(child, grandchild); 1139 if (error) 1140 return (error); 1141 } 1142 1143 if ((error = device_detach(child)) != 0) 1144 return (error); 1145 if (child->devclass) 1146 devclass_delete_device(child->devclass, child); 1147 TAILQ_REMOVE(&dev->children, child, link); 1148 TAILQ_REMOVE(&bus_data_devices, child, devlink); 1149 device_set_desc(child, NULL); 1150 kobj_delete((kobj_t) child, M_BUS); 1151 1152 bus_data_generation_update(); 1153 return (0); 1154 } 1155 1156 /* 1157 * Find only devices attached to this bus. 1158 */ 1159 device_t 1160 device_find_child(device_t dev, const char *classname, int unit) 1161 { 1162 devclass_t dc; 1163 device_t child; 1164 1165 dc = devclass_find(classname); 1166 if (!dc) 1167 return (NULL); 1168 1169 child = devclass_get_device(dc, unit); 1170 if (child && child->parent == dev) 1171 return (child); 1172 return (NULL); 1173 } 1174 1175 static driverlink_t 1176 first_matching_driver(devclass_t dc, device_t dev) 1177 { 1178 if (dev->devclass) 1179 return (devclass_find_driver_internal(dc, dev->devclass->name)); 1180 return (TAILQ_FIRST(&dc->drivers)); 1181 } 1182 1183 static driverlink_t 1184 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last) 1185 { 1186 if (dev->devclass) { 1187 driverlink_t dl; 1188 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link)) 1189 if (!strcmp(dev->devclass->name, dl->driver->name)) 1190 return (dl); 1191 return (NULL); 1192 } 1193 return (TAILQ_NEXT(last, link)); 1194 } 1195 1196 static int 1197 device_probe_child(device_t dev, device_t child) 1198 { 1199 devclass_t dc; 1200 driverlink_t best = 0; 1201 driverlink_t dl; 1202 int result, pri = 0; 1203 int hasclass = (child->devclass != 0); 1204 1205 dc = dev->devclass; 1206 if (!dc) 1207 panic("device_probe_child: parent device has no devclass"); 1208 1209 if (child->state == DS_ALIVE) 1210 return (0); 1211 1212 for (; dc; dc = dc->parent) { 1213 for (dl = first_matching_driver(dc, child); 1214 dl; 1215 dl = next_matching_driver(dc, child, dl)) { 1216 PDEBUG(("Trying %s", DRIVERNAME(dl->driver))); 1217 device_set_driver(child, dl->driver); 1218 if (!hasclass) 1219 device_set_devclass(child, dl->driver->name); 1220 result = DEVICE_PROBE(child); 1221 if (!hasclass) 1222 device_set_devclass(child, 0); 1223 1224 /* 1225 * If the driver returns SUCCESS, there can be 1226 * no higher match for this device. 1227 */ 1228 if (result == 0) { 1229 best = dl; 1230 pri = 0; 1231 break; 1232 } 1233 1234 /* 1235 * The driver returned an error so it 1236 * certainly doesn't match. 1237 */ 1238 if (result > 0) { 1239 device_set_driver(child, 0); 1240 continue; 1241 } 1242 1243 /* 1244 * A priority lower than SUCCESS, remember the 1245 * best matching driver. Initialise the value 1246 * of pri for the first match. 1247 */ 1248 if (best == 0 || result > pri) { 1249 best = dl; 1250 pri = result; 1251 continue; 1252 } 1253 } 1254 /* 1255 * If we have an unambiguous match in this devclass, 1256 * don't look in the parent. 1257 */ 1258 if (best && pri == 0) 1259 break; 1260 } 1261 1262 /* 1263 * If we found a driver, change state and initialise the devclass. 1264 */ 1265 if (best) { 1266 if (!child->devclass) 1267 device_set_devclass(child, best->driver->name); 1268 device_set_driver(child, best->driver); 1269 if (pri < 0) { 1270 /* 1271 * A bit bogus. Call the probe method again to make 1272 * sure that we have the right description. 1273 */ 1274 DEVICE_PROBE(child); 1275 } 1276 child->state = DS_ALIVE; 1277 1278 bus_data_generation_update(); 1279 return (0); 1280 } 1281 1282 return (ENXIO); 1283 } 1284 1285 device_t 1286 device_get_parent(device_t dev) 1287 { 1288 return (dev->parent); 1289 } 1290 1291 int 1292 device_get_children(device_t dev, device_t **devlistp, int *devcountp) 1293 { 1294 int count; 1295 device_t child; 1296 device_t *list; 1297 1298 count = 0; 1299 TAILQ_FOREACH(child, &dev->children, link) { 1300 count++; 1301 } 1302 1303 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO); 1304 if (!list) 1305 return (ENOMEM); 1306 1307 count = 0; 1308 TAILQ_FOREACH(child, &dev->children, link) { 1309 list[count] = child; 1310 count++; 1311 } 1312 1313 *devlistp = list; 1314 *devcountp = count; 1315 1316 return (0); 1317 } 1318 1319 driver_t * 1320 device_get_driver(device_t dev) 1321 { 1322 return (dev->driver); 1323 } 1324 1325 devclass_t 1326 device_get_devclass(device_t dev) 1327 { 1328 return (dev->devclass); 1329 } 1330 1331 const char * 1332 device_get_name(device_t dev) 1333 { 1334 if (dev != NULL && dev->devclass) 1335 return (devclass_get_name(dev->devclass)); 1336 return (NULL); 1337 } 1338 1339 const char * 1340 device_get_nameunit(device_t dev) 1341 { 1342 return (dev->nameunit); 1343 } 1344 1345 int 1346 device_get_unit(device_t dev) 1347 { 1348 return (dev->unit); 1349 } 1350 1351 const char * 1352 device_get_desc(device_t dev) 1353 { 1354 return (dev->desc); 1355 } 1356 1357 u_int32_t 1358 device_get_flags(device_t dev) 1359 { 1360 return (dev->devflags); 1361 } 1362 1363 struct sysctl_ctx_list * 1364 device_get_sysctl_ctx(device_t dev) 1365 { 1366 return (&dev->sysctl_ctx); 1367 } 1368 1369 struct sysctl_oid * 1370 device_get_sysctl_tree(device_t dev) 1371 { 1372 return (dev->sysctl_tree); 1373 } 1374 1375 int 1376 device_print_prettyname(device_t dev) 1377 { 1378 const char *name = device_get_name(dev); 1379 1380 if (name == 0) 1381 return (printf("unknown: ")); 1382 return (printf("%s%d: ", name, device_get_unit(dev))); 1383 } 1384 1385 int 1386 device_printf(device_t dev, const char * fmt, ...) 1387 { 1388 va_list ap; 1389 int retval; 1390 1391 retval = device_print_prettyname(dev); 1392 va_start(ap, fmt); 1393 retval += vprintf(fmt, ap); 1394 va_end(ap); 1395 return (retval); 1396 } 1397 1398 static void 1399 device_set_desc_internal(device_t dev, const char* desc, int copy) 1400 { 1401 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) { 1402 free(dev->desc, M_BUS); 1403 dev->flags &= ~DF_DESCMALLOCED; 1404 dev->desc = NULL; 1405 } 1406 1407 if (copy && desc) { 1408 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT); 1409 if (dev->desc) { 1410 strcpy(dev->desc, desc); 1411 dev->flags |= DF_DESCMALLOCED; 1412 } 1413 } else { 1414 /* Avoid a -Wcast-qual warning */ 1415 dev->desc = (char *)(uintptr_t) desc; 1416 } 1417 1418 bus_data_generation_update(); 1419 } 1420 1421 void 1422 device_set_desc(device_t dev, const char* desc) 1423 { 1424 device_set_desc_internal(dev, desc, FALSE); 1425 } 1426 1427 void 1428 device_set_desc_copy(device_t dev, const char* desc) 1429 { 1430 device_set_desc_internal(dev, desc, TRUE); 1431 } 1432 1433 void 1434 device_set_flags(device_t dev, u_int32_t flags) 1435 { 1436 dev->devflags = flags; 1437 } 1438 1439 void * 1440 device_get_softc(device_t dev) 1441 { 1442 return (dev->softc); 1443 } 1444 1445 void 1446 device_set_softc(device_t dev, void *softc) 1447 { 1448 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) 1449 free(dev->softc, M_BUS_SC); 1450 dev->softc = softc; 1451 if (dev->softc) 1452 dev->flags |= DF_EXTERNALSOFTC; 1453 else 1454 dev->flags &= ~DF_EXTERNALSOFTC; 1455 } 1456 1457 void * 1458 device_get_ivars(device_t dev) 1459 { 1460 1461 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)")); 1462 return (dev->ivars); 1463 } 1464 1465 void 1466 device_set_ivars(device_t dev, void * ivars) 1467 { 1468 1469 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)")); 1470 dev->ivars = ivars; 1471 } 1472 1473 device_state_t 1474 device_get_state(device_t dev) 1475 { 1476 return (dev->state); 1477 } 1478 1479 void 1480 device_enable(device_t dev) 1481 { 1482 dev->flags |= DF_ENABLED; 1483 } 1484 1485 void 1486 device_disable(device_t dev) 1487 { 1488 dev->flags &= ~DF_ENABLED; 1489 } 1490 1491 void 1492 device_busy(device_t dev) 1493 { 1494 if (dev->state < DS_ATTACHED) 1495 panic("device_busy: called for unattached device"); 1496 if (dev->busy == 0 && dev->parent) 1497 device_busy(dev->parent); 1498 dev->busy++; 1499 dev->state = DS_BUSY; 1500 } 1501 1502 void 1503 device_unbusy(device_t dev) 1504 { 1505 if (dev->state != DS_BUSY) 1506 panic("device_unbusy: called for non-busy device"); 1507 dev->busy--; 1508 if (dev->busy == 0) { 1509 if (dev->parent) 1510 device_unbusy(dev->parent); 1511 dev->state = DS_ATTACHED; 1512 } 1513 } 1514 1515 void 1516 device_quiet(device_t dev) 1517 { 1518 dev->flags |= DF_QUIET; 1519 } 1520 1521 void 1522 device_verbose(device_t dev) 1523 { 1524 dev->flags &= ~DF_QUIET; 1525 } 1526 1527 int 1528 device_is_quiet(device_t dev) 1529 { 1530 return ((dev->flags & DF_QUIET) != 0); 1531 } 1532 1533 int 1534 device_is_enabled(device_t dev) 1535 { 1536 return ((dev->flags & DF_ENABLED) != 0); 1537 } 1538 1539 int 1540 device_is_alive(device_t dev) 1541 { 1542 return (dev->state >= DS_ALIVE); 1543 } 1544 1545 int 1546 device_is_attached(device_t dev) 1547 { 1548 return (dev->state >= DS_ATTACHED); 1549 } 1550 1551 int 1552 device_set_devclass(device_t dev, const char *classname) 1553 { 1554 devclass_t dc; 1555 int error; 1556 1557 if (!classname) { 1558 if (dev->devclass) 1559 devclass_delete_device(dev->devclass, dev); 1560 return (0); 1561 } 1562 1563 if (dev->devclass) { 1564 printf("device_set_devclass: device class already set\n"); 1565 return (EINVAL); 1566 } 1567 1568 dc = devclass_find_internal(classname, 0, TRUE); 1569 if (!dc) 1570 return (ENOMEM); 1571 1572 error = devclass_add_device(dc, dev); 1573 1574 bus_data_generation_update(); 1575 return (error); 1576 } 1577 1578 int 1579 device_set_driver(device_t dev, driver_t *driver) 1580 { 1581 if (dev->state >= DS_ATTACHED) 1582 return (EBUSY); 1583 1584 if (dev->driver == driver) 1585 return (0); 1586 1587 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) { 1588 free(dev->softc, M_BUS_SC); 1589 dev->softc = NULL; 1590 } 1591 kobj_delete((kobj_t) dev, 0); 1592 dev->driver = driver; 1593 if (driver) { 1594 kobj_init((kobj_t) dev, (kobj_class_t) driver); 1595 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) { 1596 dev->softc = malloc(driver->size, M_BUS_SC, 1597 M_NOWAIT | M_ZERO); 1598 if (!dev->softc) { 1599 kobj_delete((kobj_t) dev, 0); 1600 kobj_init((kobj_t) dev, &null_class); 1601 dev->driver = NULL; 1602 return (ENOMEM); 1603 } 1604 } 1605 } else { 1606 kobj_init((kobj_t) dev, &null_class); 1607 } 1608 1609 bus_data_generation_update(); 1610 return (0); 1611 } 1612 1613 int 1614 device_probe_and_attach(device_t dev) 1615 { 1616 int error; 1617 1618 if (dev->state >= DS_ALIVE) 1619 return (0); 1620 1621 if (!(dev->flags & DF_ENABLED)) { 1622 if (bootverbose) { 1623 device_print_prettyname(dev); 1624 printf("not probed (disabled)\n"); 1625 } 1626 return (0); 1627 } 1628 if ((error = device_probe_child(dev->parent, dev)) != 0) { 1629 if (!(dev->flags & DF_DONENOMATCH)) { 1630 BUS_PROBE_NOMATCH(dev->parent, dev); 1631 devnomatch(dev); 1632 dev->flags |= DF_DONENOMATCH; 1633 } 1634 return (error); 1635 } 1636 error = device_attach(dev); 1637 1638 return (error); 1639 } 1640 1641 int 1642 device_attach(device_t dev) 1643 { 1644 int error; 1645 1646 device_sysctl_init(dev); 1647 if (!device_is_quiet(dev)) 1648 device_print_child(dev->parent, dev); 1649 if ((error = DEVICE_ATTACH(dev)) != 0) { 1650 printf("device_attach: %s%d attach returned %d\n", 1651 dev->driver->name, dev->unit, error); 1652 /* Unset the class; set in device_probe_child */ 1653 if (dev->devclass == 0) 1654 device_set_devclass(dev, 0); 1655 device_set_driver(dev, NULL); 1656 device_sysctl_fini(dev); 1657 dev->state = DS_NOTPRESENT; 1658 return (error); 1659 } 1660 dev->state = DS_ATTACHED; 1661 devadded(dev); 1662 return (0); 1663 } 1664 1665 int 1666 device_detach(device_t dev) 1667 { 1668 int error; 1669 1670 PDEBUG(("%s", DEVICENAME(dev))); 1671 if (dev->state == DS_BUSY) 1672 return (EBUSY); 1673 if (dev->state != DS_ATTACHED) 1674 return (0); 1675 1676 if ((error = DEVICE_DETACH(dev)) != 0) 1677 return (error); 1678 devremoved(dev); 1679 device_printf(dev, "detached\n"); 1680 if (dev->parent) 1681 BUS_CHILD_DETACHED(dev->parent, dev); 1682 1683 if (!(dev->flags & DF_FIXEDCLASS)) 1684 devclass_delete_device(dev->devclass, dev); 1685 1686 dev->state = DS_NOTPRESENT; 1687 device_set_driver(dev, NULL); 1688 device_sysctl_fini(dev); 1689 1690 return (0); 1691 } 1692 1693 int 1694 device_shutdown(device_t dev) 1695 { 1696 if (dev->state < DS_ATTACHED) 1697 return (0); 1698 return (DEVICE_SHUTDOWN(dev)); 1699 } 1700 1701 int 1702 device_set_unit(device_t dev, int unit) 1703 { 1704 devclass_t dc; 1705 int err; 1706 1707 dc = device_get_devclass(dev); 1708 if (unit < dc->maxunit && dc->devices[unit]) 1709 return (EBUSY); 1710 err = devclass_delete_device(dc, dev); 1711 if (err) 1712 return (err); 1713 dev->unit = unit; 1714 err = devclass_add_device(dc, dev); 1715 if (err) 1716 return (err); 1717 1718 bus_data_generation_update(); 1719 return (0); 1720 } 1721 1722 /*======================================*/ 1723 /* 1724 * Some useful method implementations to make life easier for bus drivers. 1725 */ 1726 1727 void 1728 resource_list_init(struct resource_list *rl) 1729 { 1730 SLIST_INIT(rl); 1731 } 1732 1733 void 1734 resource_list_free(struct resource_list *rl) 1735 { 1736 struct resource_list_entry *rle; 1737 1738 while ((rle = SLIST_FIRST(rl)) != NULL) { 1739 if (rle->res) 1740 panic("resource_list_free: resource entry is busy"); 1741 SLIST_REMOVE_HEAD(rl, link); 1742 free(rle, M_BUS); 1743 } 1744 } 1745 1746 int 1747 resource_list_add_next(struct resource_list *rl, int type, u_long start, 1748 u_long end, u_long count) 1749 { 1750 int rid; 1751 1752 rid = 0; 1753 while (resource_list_find(rl, type, rid) != NULL) 1754 rid++; 1755 resource_list_add(rl, type, rid, start, end, count); 1756 return (rid); 1757 } 1758 1759 void 1760 resource_list_add(struct resource_list *rl, int type, int rid, 1761 u_long start, u_long end, u_long count) 1762 { 1763 struct resource_list_entry *rle; 1764 1765 rle = resource_list_find(rl, type, rid); 1766 if (!rle) { 1767 rle = malloc(sizeof(struct resource_list_entry), M_BUS, 1768 M_NOWAIT); 1769 if (!rle) 1770 panic("resource_list_add: can't record entry"); 1771 SLIST_INSERT_HEAD(rl, rle, link); 1772 rle->type = type; 1773 rle->rid = rid; 1774 rle->res = NULL; 1775 } 1776 1777 if (rle->res) 1778 panic("resource_list_add: resource entry is busy"); 1779 1780 rle->start = start; 1781 rle->end = end; 1782 rle->count = count; 1783 } 1784 1785 struct resource_list_entry * 1786 resource_list_find(struct resource_list *rl, int type, int rid) 1787 { 1788 struct resource_list_entry *rle; 1789 1790 SLIST_FOREACH(rle, rl, link) { 1791 if (rle->type == type && rle->rid == rid) 1792 return (rle); 1793 } 1794 return (NULL); 1795 } 1796 1797 void 1798 resource_list_delete(struct resource_list *rl, int type, int rid) 1799 { 1800 struct resource_list_entry *rle = resource_list_find(rl, type, rid); 1801 1802 if (rle) { 1803 if (rle->res != NULL) 1804 panic("resource_list_delete: resource has not been released"); 1805 SLIST_REMOVE(rl, rle, resource_list_entry, link); 1806 free(rle, M_BUS); 1807 } 1808 } 1809 1810 struct resource * 1811 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child, 1812 int type, int *rid, u_long start, u_long end, u_long count, u_int flags) 1813 { 1814 struct resource_list_entry *rle = 0; 1815 int passthrough = (device_get_parent(child) != bus); 1816 int isdefault = (start == 0UL && end == ~0UL); 1817 1818 if (passthrough) { 1819 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 1820 type, rid, start, end, count, flags)); 1821 } 1822 1823 rle = resource_list_find(rl, type, *rid); 1824 1825 if (!rle) 1826 return (NULL); /* no resource of that type/rid */ 1827 1828 if (rle->res) 1829 panic("resource_list_alloc: resource entry is busy"); 1830 1831 if (isdefault) { 1832 start = rle->start; 1833 count = ulmax(count, rle->count); 1834 end = ulmax(rle->end, start + count - 1); 1835 } 1836 1837 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 1838 type, rid, start, end, count, flags); 1839 1840 /* 1841 * Record the new range. 1842 */ 1843 if (rle->res) { 1844 rle->start = rman_get_start(rle->res); 1845 rle->end = rman_get_end(rle->res); 1846 rle->count = count; 1847 } 1848 1849 return (rle->res); 1850 } 1851 1852 int 1853 resource_list_release(struct resource_list *rl, device_t bus, device_t child, 1854 int type, int rid, struct resource *res) 1855 { 1856 struct resource_list_entry *rle = 0; 1857 int passthrough = (device_get_parent(child) != bus); 1858 int error; 1859 1860 if (passthrough) { 1861 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 1862 type, rid, res)); 1863 } 1864 1865 rle = resource_list_find(rl, type, rid); 1866 1867 if (!rle) 1868 panic("resource_list_release: can't find resource"); 1869 if (!rle->res) 1870 panic("resource_list_release: resource entry is not busy"); 1871 1872 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 1873 type, rid, res); 1874 if (error) 1875 return (error); 1876 1877 rle->res = NULL; 1878 return (0); 1879 } 1880 1881 int 1882 resource_list_print_type(struct resource_list *rl, const char *name, int type, 1883 const char *format) 1884 { 1885 struct resource_list_entry *rle; 1886 int printed, retval; 1887 1888 printed = 0; 1889 retval = 0; 1890 /* Yes, this is kinda cheating */ 1891 SLIST_FOREACH(rle, rl, link) { 1892 if (rle->type == type) { 1893 if (printed == 0) 1894 retval += printf(" %s ", name); 1895 else 1896 retval += printf(","); 1897 printed++; 1898 retval += printf(format, rle->start); 1899 if (rle->count > 1) { 1900 retval += printf("-"); 1901 retval += printf(format, rle->start + 1902 rle->count - 1); 1903 } 1904 } 1905 } 1906 return (retval); 1907 } 1908 1909 /* 1910 * Call DEVICE_IDENTIFY for each driver. 1911 */ 1912 int 1913 bus_generic_probe(device_t dev) 1914 { 1915 devclass_t dc = dev->devclass; 1916 driverlink_t dl; 1917 1918 TAILQ_FOREACH(dl, &dc->drivers, link) { 1919 DEVICE_IDENTIFY(dl->driver, dev); 1920 } 1921 1922 return (0); 1923 } 1924 1925 int 1926 bus_generic_attach(device_t dev) 1927 { 1928 device_t child; 1929 1930 TAILQ_FOREACH(child, &dev->children, link) { 1931 device_probe_and_attach(child); 1932 } 1933 1934 return (0); 1935 } 1936 1937 int 1938 bus_generic_detach(device_t dev) 1939 { 1940 device_t child; 1941 int error; 1942 1943 if (dev->state != DS_ATTACHED) 1944 return (EBUSY); 1945 1946 TAILQ_FOREACH(child, &dev->children, link) { 1947 if ((error = device_detach(child)) != 0) 1948 return (error); 1949 } 1950 1951 return (0); 1952 } 1953 1954 int 1955 bus_generic_shutdown(device_t dev) 1956 { 1957 device_t child; 1958 1959 TAILQ_FOREACH(child, &dev->children, link) { 1960 device_shutdown(child); 1961 } 1962 1963 return (0); 1964 } 1965 1966 int 1967 bus_generic_suspend(device_t dev) 1968 { 1969 int error; 1970 device_t child, child2; 1971 1972 TAILQ_FOREACH(child, &dev->children, link) { 1973 error = DEVICE_SUSPEND(child); 1974 if (error) { 1975 for (child2 = TAILQ_FIRST(&dev->children); 1976 child2 && child2 != child; 1977 child2 = TAILQ_NEXT(child2, link)) 1978 DEVICE_RESUME(child2); 1979 return (error); 1980 } 1981 } 1982 return (0); 1983 } 1984 1985 int 1986 bus_generic_resume(device_t dev) 1987 { 1988 device_t child; 1989 1990 TAILQ_FOREACH(child, &dev->children, link) { 1991 DEVICE_RESUME(child); 1992 /* if resume fails, there's nothing we can usefully do... */ 1993 } 1994 return (0); 1995 } 1996 1997 int 1998 bus_print_child_header(device_t dev, device_t child) 1999 { 2000 int retval = 0; 2001 2002 if (device_get_desc(child)) { 2003 retval += device_printf(child, "<%s>", device_get_desc(child)); 2004 } else { 2005 retval += printf("%s", device_get_nameunit(child)); 2006 } 2007 2008 return (retval); 2009 } 2010 2011 int 2012 bus_print_child_footer(device_t dev, device_t child) 2013 { 2014 return (printf(" on %s\n", device_get_nameunit(dev))); 2015 } 2016 2017 int 2018 bus_generic_print_child(device_t dev, device_t child) 2019 { 2020 int retval = 0; 2021 2022 retval += bus_print_child_header(dev, child); 2023 retval += bus_print_child_footer(dev, child); 2024 2025 return (retval); 2026 } 2027 2028 int 2029 bus_generic_read_ivar(device_t dev, device_t child, int index, 2030 uintptr_t * result) 2031 { 2032 return (ENOENT); 2033 } 2034 2035 int 2036 bus_generic_write_ivar(device_t dev, device_t child, int index, 2037 uintptr_t value) 2038 { 2039 return (ENOENT); 2040 } 2041 2042 struct resource_list * 2043 bus_generic_get_resource_list(device_t dev, device_t child) 2044 { 2045 return (NULL); 2046 } 2047 2048 void 2049 bus_generic_driver_added(device_t dev, driver_t *driver) 2050 { 2051 device_t child; 2052 2053 DEVICE_IDENTIFY(driver, dev); 2054 TAILQ_FOREACH(child, &dev->children, link) { 2055 if (child->state == DS_NOTPRESENT) 2056 device_probe_and_attach(child); 2057 } 2058 } 2059 2060 int 2061 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq, 2062 int flags, driver_intr_t *intr, void *arg, void **cookiep) 2063 { 2064 /* Propagate up the bus hierarchy until someone handles it. */ 2065 if (dev->parent) 2066 return (BUS_SETUP_INTR(dev->parent, child, irq, flags, 2067 intr, arg, cookiep)); 2068 return (EINVAL); 2069 } 2070 2071 int 2072 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq, 2073 void *cookie) 2074 { 2075 /* Propagate up the bus hierarchy until someone handles it. */ 2076 if (dev->parent) 2077 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie)); 2078 return (EINVAL); 2079 } 2080 2081 struct resource * 2082 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid, 2083 u_long start, u_long end, u_long count, u_int flags) 2084 { 2085 /* Propagate up the bus hierarchy until someone handles it. */ 2086 if (dev->parent) 2087 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid, 2088 start, end, count, flags)); 2089 return (NULL); 2090 } 2091 2092 int 2093 bus_generic_release_resource(device_t dev, device_t child, int type, int rid, 2094 struct resource *r) 2095 { 2096 /* Propagate up the bus hierarchy until someone handles it. */ 2097 if (dev->parent) 2098 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, 2099 r)); 2100 return (EINVAL); 2101 } 2102 2103 int 2104 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid, 2105 struct resource *r) 2106 { 2107 /* Propagate up the bus hierarchy until someone handles it. */ 2108 if (dev->parent) 2109 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, 2110 r)); 2111 return (EINVAL); 2112 } 2113 2114 int 2115 bus_generic_deactivate_resource(device_t dev, device_t child, int type, 2116 int rid, struct resource *r) 2117 { 2118 /* Propagate up the bus hierarchy until someone handles it. */ 2119 if (dev->parent) 2120 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid, 2121 r)); 2122 return (EINVAL); 2123 } 2124 2125 int 2126 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig, 2127 enum intr_polarity pol) 2128 { 2129 2130 /* Propagate up the bus hierarchy until someone handles it. */ 2131 if (dev->parent) 2132 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol)); 2133 return (EINVAL); 2134 } 2135 2136 int 2137 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid, 2138 u_long *startp, u_long *countp) 2139 { 2140 struct resource_list * rl = NULL; 2141 struct resource_list_entry * rle = NULL; 2142 2143 rl = BUS_GET_RESOURCE_LIST(dev, child); 2144 if (!rl) 2145 return (EINVAL); 2146 2147 rle = resource_list_find(rl, type, rid); 2148 if (!rle) 2149 return (ENOENT); 2150 2151 if (startp) 2152 *startp = rle->start; 2153 if (countp) 2154 *countp = rle->count; 2155 2156 return (0); 2157 } 2158 2159 int 2160 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid, 2161 u_long start, u_long count) 2162 { 2163 struct resource_list * rl = NULL; 2164 2165 rl = BUS_GET_RESOURCE_LIST(dev, child); 2166 if (!rl) 2167 return (EINVAL); 2168 2169 resource_list_add(rl, type, rid, start, (start + count - 1), count); 2170 2171 return (0); 2172 } 2173 2174 void 2175 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid) 2176 { 2177 struct resource_list * rl = NULL; 2178 2179 rl = BUS_GET_RESOURCE_LIST(dev, child); 2180 if (!rl) 2181 return; 2182 2183 resource_list_delete(rl, type, rid); 2184 2185 return; 2186 } 2187 2188 int 2189 bus_generic_rl_release_resource(device_t dev, device_t child, int type, 2190 int rid, struct resource *r) 2191 { 2192 struct resource_list * rl = NULL; 2193 2194 rl = BUS_GET_RESOURCE_LIST(dev, child); 2195 if (!rl) 2196 return (EINVAL); 2197 2198 return (resource_list_release(rl, dev, child, type, rid, r)); 2199 } 2200 2201 struct resource * 2202 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type, 2203 int *rid, u_long start, u_long end, u_long count, u_int flags) 2204 { 2205 struct resource_list * rl = NULL; 2206 2207 rl = BUS_GET_RESOURCE_LIST(dev, child); 2208 if (!rl) 2209 return (NULL); 2210 2211 return (resource_list_alloc(rl, dev, child, type, rid, 2212 start, end, count, flags)); 2213 } 2214 2215 int 2216 bus_generic_child_present(device_t bus, device_t child) 2217 { 2218 return (BUS_CHILD_PRESENT(device_get_parent(bus), bus)); 2219 } 2220 2221 /* 2222 * Some convenience functions to make it easier for drivers to use the 2223 * resource-management functions. All these really do is hide the 2224 * indirection through the parent's method table, making for slightly 2225 * less-wordy code. In the future, it might make sense for this code 2226 * to maintain some sort of a list of resources allocated by each device. 2227 */ 2228 struct resource * 2229 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end, 2230 u_long count, u_int flags) 2231 { 2232 if (dev->parent == 0) 2233 return (0); 2234 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end, 2235 count, flags)); 2236 } 2237 2238 int 2239 bus_activate_resource(device_t dev, int type, int rid, struct resource *r) 2240 { 2241 if (dev->parent == 0) 2242 return (EINVAL); 2243 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 2244 } 2245 2246 int 2247 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r) 2248 { 2249 if (dev->parent == 0) 2250 return (EINVAL); 2251 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 2252 } 2253 2254 int 2255 bus_release_resource(device_t dev, int type, int rid, struct resource *r) 2256 { 2257 if (dev->parent == 0) 2258 return (EINVAL); 2259 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r)); 2260 } 2261 2262 int 2263 bus_setup_intr(device_t dev, struct resource *r, int flags, 2264 driver_intr_t handler, void *arg, void **cookiep) 2265 { 2266 int error; 2267 2268 if (dev->parent != 0) { 2269 if ((flags &~ INTR_ENTROPY) == (INTR_TYPE_NET | INTR_MPSAFE) && 2270 !debug_mpsafenet) 2271 flags &= ~INTR_MPSAFE; 2272 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, 2273 handler, arg, cookiep); 2274 if (error == 0) { 2275 if (!(flags & (INTR_MPSAFE | INTR_FAST))) 2276 device_printf(dev, "[GIANT-LOCKED]\n"); 2277 if (bootverbose && (flags & INTR_MPSAFE)) 2278 device_printf(dev, "[MPSAFE]\n"); 2279 if (flags & INTR_FAST) 2280 device_printf(dev, "[FAST]\n"); 2281 } 2282 } else 2283 error = EINVAL; 2284 return (error); 2285 } 2286 2287 int 2288 bus_teardown_intr(device_t dev, struct resource *r, void *cookie) 2289 { 2290 if (dev->parent == 0) 2291 return (EINVAL); 2292 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie)); 2293 } 2294 2295 int 2296 bus_set_resource(device_t dev, int type, int rid, 2297 u_long start, u_long count) 2298 { 2299 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid, 2300 start, count)); 2301 } 2302 2303 int 2304 bus_get_resource(device_t dev, int type, int rid, 2305 u_long *startp, u_long *countp) 2306 { 2307 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 2308 startp, countp)); 2309 } 2310 2311 u_long 2312 bus_get_resource_start(device_t dev, int type, int rid) 2313 { 2314 u_long start, count; 2315 int error; 2316 2317 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 2318 &start, &count); 2319 if (error) 2320 return (0); 2321 return (start); 2322 } 2323 2324 u_long 2325 bus_get_resource_count(device_t dev, int type, int rid) 2326 { 2327 u_long start, count; 2328 int error; 2329 2330 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 2331 &start, &count); 2332 if (error) 2333 return (0); 2334 return (count); 2335 } 2336 2337 void 2338 bus_delete_resource(device_t dev, int type, int rid) 2339 { 2340 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid); 2341 } 2342 2343 int 2344 bus_child_present(device_t child) 2345 { 2346 return (BUS_CHILD_PRESENT(device_get_parent(child), child)); 2347 } 2348 2349 int 2350 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen) 2351 { 2352 device_t parent; 2353 2354 parent = device_get_parent(child); 2355 if (parent == NULL) { 2356 *buf = '\0'; 2357 return (0); 2358 } 2359 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen)); 2360 } 2361 2362 int 2363 bus_child_location_str(device_t child, char *buf, size_t buflen) 2364 { 2365 device_t parent; 2366 2367 parent = device_get_parent(child); 2368 if (parent == NULL) { 2369 *buf = '\0'; 2370 return (0); 2371 } 2372 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen)); 2373 } 2374 2375 static int 2376 root_print_child(device_t dev, device_t child) 2377 { 2378 int retval = 0; 2379 2380 retval += bus_print_child_header(dev, child); 2381 retval += printf("\n"); 2382 2383 return (retval); 2384 } 2385 2386 static int 2387 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg, 2388 void **cookiep) 2389 { 2390 /* 2391 * If an interrupt mapping gets to here something bad has happened. 2392 */ 2393 panic("root_setup_intr"); 2394 } 2395 2396 /* 2397 * If we get here, assume that the device is permanant and really is 2398 * present in the system. Removable bus drivers are expected to intercept 2399 * this call long before it gets here. We return -1 so that drivers that 2400 * really care can check vs -1 or some ERRNO returned higher in the food 2401 * chain. 2402 */ 2403 static int 2404 root_child_present(device_t dev, device_t child) 2405 { 2406 return (-1); 2407 } 2408 2409 static kobj_method_t root_methods[] = { 2410 /* Device interface */ 2411 KOBJMETHOD(device_shutdown, bus_generic_shutdown), 2412 KOBJMETHOD(device_suspend, bus_generic_suspend), 2413 KOBJMETHOD(device_resume, bus_generic_resume), 2414 2415 /* Bus interface */ 2416 KOBJMETHOD(bus_print_child, root_print_child), 2417 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar), 2418 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar), 2419 KOBJMETHOD(bus_setup_intr, root_setup_intr), 2420 KOBJMETHOD(bus_child_present, root_child_present), 2421 2422 { 0, 0 } 2423 }; 2424 2425 static driver_t root_driver = { 2426 "root", 2427 root_methods, 2428 1, /* no softc */ 2429 }; 2430 2431 device_t root_bus; 2432 devclass_t root_devclass; 2433 2434 static int 2435 root_bus_module_handler(module_t mod, int what, void* arg) 2436 { 2437 switch (what) { 2438 case MOD_LOAD: 2439 TAILQ_INIT(&bus_data_devices); 2440 kobj_class_compile((kobj_class_t) &root_driver); 2441 root_bus = make_device(NULL, "root", 0); 2442 root_bus->desc = "System root bus"; 2443 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver); 2444 root_bus->driver = &root_driver; 2445 root_bus->state = DS_ATTACHED; 2446 root_devclass = devclass_find_internal("root", 0, FALSE); 2447 devinit(); 2448 return (0); 2449 2450 case MOD_SHUTDOWN: 2451 device_shutdown(root_bus); 2452 return (0); 2453 } 2454 2455 return (0); 2456 } 2457 2458 static moduledata_t root_bus_mod = { 2459 "rootbus", 2460 root_bus_module_handler, 2461 0 2462 }; 2463 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 2464 2465 void 2466 root_bus_configure(void) 2467 { 2468 device_t dev; 2469 2470 PDEBUG((".")); 2471 2472 TAILQ_FOREACH(dev, &root_bus->children, link) { 2473 device_probe_and_attach(dev); 2474 } 2475 } 2476 2477 int 2478 driver_module_handler(module_t mod, int what, void *arg) 2479 { 2480 int error; 2481 struct driver_module_data *dmd; 2482 devclass_t bus_devclass; 2483 kobj_class_t driver; 2484 2485 dmd = (struct driver_module_data *)arg; 2486 bus_devclass = devclass_find_internal(dmd->dmd_busname, 0, TRUE); 2487 error = 0; 2488 2489 switch (what) { 2490 case MOD_LOAD: 2491 if (dmd->dmd_chainevh) 2492 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 2493 2494 driver = dmd->dmd_driver; 2495 PDEBUG(("Loading module: driver %s on bus %s", 2496 DRIVERNAME(driver), dmd->dmd_busname)); 2497 error = devclass_add_driver(bus_devclass, driver); 2498 if (error) 2499 break; 2500 2501 /* 2502 * If the driver has any base classes, make the 2503 * devclass inherit from the devclass of the driver's 2504 * first base class. This will allow the system to 2505 * search for drivers in both devclasses for children 2506 * of a device using this driver. 2507 */ 2508 if (driver->baseclasses) { 2509 const char *parentname; 2510 parentname = driver->baseclasses[0]->name; 2511 *dmd->dmd_devclass = 2512 devclass_find_internal(driver->name, 2513 parentname, TRUE); 2514 } else { 2515 *dmd->dmd_devclass = 2516 devclass_find_internal(driver->name, 0, TRUE); 2517 } 2518 break; 2519 2520 case MOD_UNLOAD: 2521 PDEBUG(("Unloading module: driver %s from bus %s", 2522 DRIVERNAME(dmd->dmd_driver), 2523 dmd->dmd_busname)); 2524 error = devclass_delete_driver(bus_devclass, 2525 dmd->dmd_driver); 2526 2527 if (!error && dmd->dmd_chainevh) 2528 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 2529 break; 2530 } 2531 2532 return (error); 2533 } 2534 2535 #ifdef BUS_DEBUG 2536 2537 /* the _short versions avoid iteration by not calling anything that prints 2538 * more than oneliners. I love oneliners. 2539 */ 2540 2541 static void 2542 print_device_short(device_t dev, int indent) 2543 { 2544 if (!dev) 2545 return; 2546 2547 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n", 2548 dev->unit, dev->desc, 2549 (dev->parent? "":"no "), 2550 (TAILQ_EMPTY(&dev->children)? "no ":""), 2551 (dev->flags&DF_ENABLED? "enabled,":"disabled,"), 2552 (dev->flags&DF_FIXEDCLASS? "fixed,":""), 2553 (dev->flags&DF_WILDCARD? "wildcard,":""), 2554 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""), 2555 (dev->ivars? "":"no "), 2556 (dev->softc? "":"no "), 2557 dev->busy)); 2558 } 2559 2560 static void 2561 print_device(device_t dev, int indent) 2562 { 2563 if (!dev) 2564 return; 2565 2566 print_device_short(dev, indent); 2567 2568 indentprintf(("Parent:\n")); 2569 print_device_short(dev->parent, indent+1); 2570 indentprintf(("Driver:\n")); 2571 print_driver_short(dev->driver, indent+1); 2572 indentprintf(("Devclass:\n")); 2573 print_devclass_short(dev->devclass, indent+1); 2574 } 2575 2576 void 2577 print_device_tree_short(device_t dev, int indent) 2578 /* print the device and all its children (indented) */ 2579 { 2580 device_t child; 2581 2582 if (!dev) 2583 return; 2584 2585 print_device_short(dev, indent); 2586 2587 TAILQ_FOREACH(child, &dev->children, link) { 2588 print_device_tree_short(child, indent+1); 2589 } 2590 } 2591 2592 void 2593 print_device_tree(device_t dev, int indent) 2594 /* print the device and all its children (indented) */ 2595 { 2596 device_t child; 2597 2598 if (!dev) 2599 return; 2600 2601 print_device(dev, indent); 2602 2603 TAILQ_FOREACH(child, &dev->children, link) { 2604 print_device_tree(child, indent+1); 2605 } 2606 } 2607 2608 static void 2609 print_driver_short(driver_t *driver, int indent) 2610 { 2611 if (!driver) 2612 return; 2613 2614 indentprintf(("driver %s: softc size = %zd\n", 2615 driver->name, driver->size)); 2616 } 2617 2618 static void 2619 print_driver(driver_t *driver, int indent) 2620 { 2621 if (!driver) 2622 return; 2623 2624 print_driver_short(driver, indent); 2625 } 2626 2627 2628 static void 2629 print_driver_list(driver_list_t drivers, int indent) 2630 { 2631 driverlink_t driver; 2632 2633 TAILQ_FOREACH(driver, &drivers, link) { 2634 print_driver(driver->driver, indent); 2635 } 2636 } 2637 2638 static void 2639 print_devclass_short(devclass_t dc, int indent) 2640 { 2641 if ( !dc ) 2642 return; 2643 2644 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit)); 2645 } 2646 2647 static void 2648 print_devclass(devclass_t dc, int indent) 2649 { 2650 int i; 2651 2652 if ( !dc ) 2653 return; 2654 2655 print_devclass_short(dc, indent); 2656 indentprintf(("Drivers:\n")); 2657 print_driver_list(dc->drivers, indent+1); 2658 2659 indentprintf(("Devices:\n")); 2660 for (i = 0; i < dc->maxunit; i++) 2661 if (dc->devices[i]) 2662 print_device(dc->devices[i], indent+1); 2663 } 2664 2665 void 2666 print_devclass_list_short(void) 2667 { 2668 devclass_t dc; 2669 2670 printf("Short listing of devclasses, drivers & devices:\n"); 2671 TAILQ_FOREACH(dc, &devclasses, link) { 2672 print_devclass_short(dc, 0); 2673 } 2674 } 2675 2676 void 2677 print_devclass_list(void) 2678 { 2679 devclass_t dc; 2680 2681 printf("Full listing of devclasses, drivers & devices:\n"); 2682 TAILQ_FOREACH(dc, &devclasses, link) { 2683 print_devclass(dc, 0); 2684 } 2685 } 2686 2687 #endif 2688 2689 /* 2690 * User-space access to the device tree. 2691 * 2692 * We implement a small set of nodes: 2693 * 2694 * hw.bus Single integer read method to obtain the 2695 * current generation count. 2696 * hw.bus.devices Reads the entire device tree in flat space. 2697 * hw.bus.rman Resource manager interface 2698 * 2699 * We might like to add the ability to scan devclasses and/or drivers to 2700 * determine what else is currently loaded/available. 2701 */ 2702 2703 static int 2704 sysctl_bus(SYSCTL_HANDLER_ARGS) 2705 { 2706 struct u_businfo ubus; 2707 2708 ubus.ub_version = BUS_USER_VERSION; 2709 ubus.ub_generation = bus_data_generation; 2710 2711 return (SYSCTL_OUT(req, &ubus, sizeof(ubus))); 2712 } 2713 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus, 2714 "bus-related data"); 2715 2716 static int 2717 sysctl_devices(SYSCTL_HANDLER_ARGS) 2718 { 2719 int *name = (int *)arg1; 2720 u_int namelen = arg2; 2721 int index; 2722 struct device *dev; 2723 struct u_device udev; /* XXX this is a bit big */ 2724 int error; 2725 2726 if (namelen != 2) 2727 return (EINVAL); 2728 2729 if (bus_data_generation_check(name[0])) 2730 return (EINVAL); 2731 2732 index = name[1]; 2733 2734 /* 2735 * Scan the list of devices, looking for the requested index. 2736 */ 2737 TAILQ_FOREACH(dev, &bus_data_devices, devlink) { 2738 if (index-- == 0) 2739 break; 2740 } 2741 if (dev == NULL) 2742 return (ENOENT); 2743 2744 /* 2745 * Populate the return array. 2746 */ 2747 udev.dv_handle = (uintptr_t)dev; 2748 udev.dv_parent = (uintptr_t)dev->parent; 2749 if (dev->nameunit == NULL) 2750 udev.dv_name[0] = '\0'; 2751 else 2752 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name)); 2753 2754 if (dev->desc == NULL) 2755 udev.dv_desc[0] = '\0'; 2756 else 2757 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc)); 2758 if (dev->driver == NULL || dev->driver->name == NULL) 2759 udev.dv_drivername[0] = '\0'; 2760 else 2761 strlcpy(udev.dv_drivername, dev->driver->name, 2762 sizeof(udev.dv_drivername)); 2763 udev.dv_pnpinfo[0] = '\0'; 2764 udev.dv_location[0] = '\0'; 2765 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo)); 2766 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location)); 2767 udev.dv_devflags = dev->devflags; 2768 udev.dv_flags = dev->flags; 2769 udev.dv_state = dev->state; 2770 error = SYSCTL_OUT(req, &udev, sizeof(udev)); 2771 return (error); 2772 } 2773 2774 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices, 2775 "system device tree"); 2776 2777 /* 2778 * Sysctl interface for scanning the resource lists. 2779 * 2780 * We take two input parameters; the index into the list of resource 2781 * managers, and the resource offset into the list. 2782 */ 2783 static int 2784 sysctl_rman(SYSCTL_HANDLER_ARGS) 2785 { 2786 int *name = (int *)arg1; 2787 u_int namelen = arg2; 2788 int rman_idx, res_idx; 2789 struct rman *rm; 2790 struct resource *res; 2791 struct u_rman urm; 2792 struct u_resource ures; 2793 int error; 2794 2795 if (namelen != 3) 2796 return (EINVAL); 2797 2798 if (bus_data_generation_check(name[0])) 2799 return (EINVAL); 2800 rman_idx = name[1]; 2801 res_idx = name[2]; 2802 2803 /* 2804 * Find the indexed resource manager 2805 */ 2806 TAILQ_FOREACH(rm, &rman_head, rm_link) { 2807 if (rman_idx-- == 0) 2808 break; 2809 } 2810 if (rm == NULL) 2811 return (ENOENT); 2812 2813 /* 2814 * If the resource index is -1, we want details on the 2815 * resource manager. 2816 */ 2817 if (res_idx == -1) { 2818 urm.rm_handle = (uintptr_t)rm; 2819 strlcpy(urm.rm_descr, rm->rm_descr, RM_TEXTLEN); 2820 urm.rm_start = rm->rm_start; 2821 urm.rm_size = rm->rm_end - rm->rm_start + 1; 2822 urm.rm_type = rm->rm_type; 2823 2824 error = SYSCTL_OUT(req, &urm, sizeof(urm)); 2825 return (error); 2826 } 2827 2828 /* 2829 * Find the indexed resource and return it. 2830 */ 2831 TAILQ_FOREACH(res, &rm->rm_list, r_link) { 2832 if (res_idx-- == 0) { 2833 ures.r_handle = (uintptr_t)res; 2834 ures.r_parent = (uintptr_t)res->r_rm; 2835 ures.r_device = (uintptr_t)res->r_dev; 2836 if (res->r_dev != NULL) { 2837 if (device_get_name(res->r_dev) != NULL) { 2838 snprintf(ures.r_devname, RM_TEXTLEN, 2839 "%s%d", 2840 device_get_name(res->r_dev), 2841 device_get_unit(res->r_dev)); 2842 } else { 2843 strlcpy(ures.r_devname, "nomatch", 2844 RM_TEXTLEN); 2845 } 2846 } else { 2847 ures.r_devname[0] = '\0'; 2848 } 2849 ures.r_start = res->r_start; 2850 ures.r_size = res->r_end - res->r_start + 1; 2851 ures.r_flags = res->r_flags; 2852 2853 error = SYSCTL_OUT(req, &ures, sizeof(ures)); 2854 return (error); 2855 } 2856 } 2857 return (ENOENT); 2858 } 2859 2860 SYSCTL_NODE(_hw_bus, OID_AUTO, rman, CTLFLAG_RD, sysctl_rman, 2861 "kernel resource manager"); 2862 2863 int 2864 bus_data_generation_check(int generation) 2865 { 2866 if (generation != bus_data_generation) 2867 return (1); 2868 2869 /* XXX generate optimised lists here? */ 2870 return (0); 2871 } 2872 2873 void 2874 bus_data_generation_update(void) 2875 { 2876 bus_data_generation++; 2877 } 2878