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