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