1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 1997,1998,2003 Doug Rabson 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_bus.h" 33 #include "opt_ddb.h" 34 35 #include <sys/param.h> 36 #include <sys/conf.h> 37 #include <sys/domainset.h> 38 #include <sys/eventhandler.h> 39 #include <sys/filio.h> 40 #include <sys/lock.h> 41 #include <sys/kernel.h> 42 #include <sys/kobj.h> 43 #include <sys/limits.h> 44 #include <sys/malloc.h> 45 #include <sys/module.h> 46 #include <sys/mutex.h> 47 #include <sys/poll.h> 48 #include <sys/priv.h> 49 #include <sys/proc.h> 50 #include <sys/condvar.h> 51 #include <sys/queue.h> 52 #include <machine/bus.h> 53 #include <sys/random.h> 54 #include <sys/rman.h> 55 #include <sys/sbuf.h> 56 #include <sys/selinfo.h> 57 #include <sys/signalvar.h> 58 #include <sys/smp.h> 59 #include <sys/sysctl.h> 60 #include <sys/systm.h> 61 #include <sys/uio.h> 62 #include <sys/bus.h> 63 #include <sys/cpuset.h> 64 65 #include <net/vnet.h> 66 67 #include <machine/cpu.h> 68 #include <machine/stdarg.h> 69 70 #include <vm/uma.h> 71 #include <vm/vm.h> 72 73 #include <ddb/ddb.h> 74 75 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 76 NULL); 77 SYSCTL_ROOT_NODE(OID_AUTO, dev, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 78 NULL); 79 80 /* 81 * Used to attach drivers to devclasses. 82 */ 83 typedef struct driverlink *driverlink_t; 84 struct driverlink { 85 kobj_class_t driver; 86 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */ 87 int pass; 88 int flags; 89 #define DL_DEFERRED_PROBE 1 /* Probe deferred on this */ 90 TAILQ_ENTRY(driverlink) passlink; 91 }; 92 93 /* 94 * Forward declarations 95 */ 96 typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t; 97 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t; 98 typedef TAILQ_HEAD(device_list, device) device_list_t; 99 100 struct devclass { 101 TAILQ_ENTRY(devclass) link; 102 devclass_t parent; /* parent in devclass hierarchy */ 103 driver_list_t drivers; /* bus devclasses store drivers for bus */ 104 char *name; 105 device_t *devices; /* array of devices indexed by unit */ 106 int maxunit; /* size of devices array */ 107 int flags; 108 #define DC_HAS_CHILDREN 1 109 110 struct sysctl_ctx_list sysctl_ctx; 111 struct sysctl_oid *sysctl_tree; 112 }; 113 114 /** 115 * @brief Implementation of device. 116 */ 117 struct device { 118 /* 119 * A device is a kernel object. The first field must be the 120 * current ops table for the object. 121 */ 122 KOBJ_FIELDS; 123 124 /* 125 * Device hierarchy. 126 */ 127 TAILQ_ENTRY(device) link; /**< list of devices in parent */ 128 TAILQ_ENTRY(device) devlink; /**< global device list membership */ 129 device_t parent; /**< parent of this device */ 130 device_list_t children; /**< list of child devices */ 131 132 /* 133 * Details of this device. 134 */ 135 driver_t *driver; /**< current driver */ 136 devclass_t devclass; /**< current device class */ 137 int unit; /**< current unit number */ 138 char* nameunit; /**< name+unit e.g. foodev0 */ 139 char* desc; /**< driver specific description */ 140 int busy; /**< count of calls to device_busy() */ 141 device_state_t state; /**< current device state */ 142 uint32_t devflags; /**< api level flags for device_get_flags() */ 143 u_int flags; /**< internal device flags */ 144 u_int order; /**< order from device_add_child_ordered() */ 145 void *ivars; /**< instance variables */ 146 void *softc; /**< current driver's variables */ 147 148 struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables */ 149 struct sysctl_oid *sysctl_tree; /**< state for sysctl variables */ 150 }; 151 152 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures"); 153 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc"); 154 155 EVENTHANDLER_LIST_DEFINE(device_attach); 156 EVENTHANDLER_LIST_DEFINE(device_detach); 157 EVENTHANDLER_LIST_DEFINE(dev_lookup); 158 159 static int bus_child_location_sb(device_t child, struct sbuf *sb); 160 static int bus_child_pnpinfo_sb(device_t child, struct sbuf *sb); 161 static void devctl2_init(void); 162 static bool device_frozen; 163 164 #define DRIVERNAME(d) ((d)? d->name : "no driver") 165 #define DEVCLANAME(d) ((d)? d->name : "no devclass") 166 167 #ifdef BUS_DEBUG 168 169 static int bus_debug = 1; 170 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RWTUN, &bus_debug, 0, 171 "Bus debug level"); 172 173 #define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");} 174 #define DEVICENAME(d) ((d)? device_get_name(d): "no device") 175 176 /** 177 * Produce the indenting, indent*2 spaces plus a '.' ahead of that to 178 * prevent syslog from deleting initial spaces 179 */ 180 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0) 181 182 static void print_device_short(device_t dev, int indent); 183 static void print_device(device_t dev, int indent); 184 void print_device_tree_short(device_t dev, int indent); 185 void print_device_tree(device_t dev, int indent); 186 static void print_driver_short(driver_t *driver, int indent); 187 static void print_driver(driver_t *driver, int indent); 188 static void print_driver_list(driver_list_t drivers, int indent); 189 static void print_devclass_short(devclass_t dc, int indent); 190 static void print_devclass(devclass_t dc, int indent); 191 void print_devclass_list_short(void); 192 void print_devclass_list(void); 193 194 #else 195 /* Make the compiler ignore the function calls */ 196 #define PDEBUG(a) /* nop */ 197 #define DEVICENAME(d) /* nop */ 198 199 #define print_device_short(d,i) /* nop */ 200 #define print_device(d,i) /* nop */ 201 #define print_device_tree_short(d,i) /* nop */ 202 #define print_device_tree(d,i) /* nop */ 203 #define print_driver_short(d,i) /* nop */ 204 #define print_driver(d,i) /* nop */ 205 #define print_driver_list(d,i) /* nop */ 206 #define print_devclass_short(d,i) /* nop */ 207 #define print_devclass(d,i) /* nop */ 208 #define print_devclass_list_short() /* nop */ 209 #define print_devclass_list() /* nop */ 210 #endif 211 212 /* 213 * dev sysctl tree 214 */ 215 216 enum { 217 DEVCLASS_SYSCTL_PARENT, 218 }; 219 220 static int 221 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS) 222 { 223 devclass_t dc = (devclass_t)arg1; 224 const char *value; 225 226 switch (arg2) { 227 case DEVCLASS_SYSCTL_PARENT: 228 value = dc->parent ? dc->parent->name : ""; 229 break; 230 default: 231 return (EINVAL); 232 } 233 return (SYSCTL_OUT_STR(req, value)); 234 } 235 236 static void 237 devclass_sysctl_init(devclass_t dc) 238 { 239 if (dc->sysctl_tree != NULL) 240 return; 241 sysctl_ctx_init(&dc->sysctl_ctx); 242 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx, 243 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name, 244 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 245 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree), 246 OID_AUTO, "%parent", 247 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 248 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A", 249 "parent class"); 250 } 251 252 enum { 253 DEVICE_SYSCTL_DESC, 254 DEVICE_SYSCTL_DRIVER, 255 DEVICE_SYSCTL_LOCATION, 256 DEVICE_SYSCTL_PNPINFO, 257 DEVICE_SYSCTL_PARENT, 258 }; 259 260 static int 261 device_sysctl_handler(SYSCTL_HANDLER_ARGS) 262 { 263 struct sbuf sb; 264 device_t dev = (device_t)arg1; 265 int error; 266 267 sbuf_new_for_sysctl(&sb, NULL, 1024, req); 268 sbuf_clear_flags(&sb, SBUF_INCLUDENUL); 269 switch (arg2) { 270 case DEVICE_SYSCTL_DESC: 271 sbuf_cat(&sb, dev->desc ? dev->desc : ""); 272 break; 273 case DEVICE_SYSCTL_DRIVER: 274 sbuf_cat(&sb, dev->driver ? dev->driver->name : ""); 275 break; 276 case DEVICE_SYSCTL_LOCATION: 277 bus_child_location_sb(dev, &sb); 278 break; 279 case DEVICE_SYSCTL_PNPINFO: 280 bus_child_pnpinfo_sb(dev, &sb); 281 break; 282 case DEVICE_SYSCTL_PARENT: 283 sbuf_cat(&sb, dev->parent ? dev->parent->nameunit : ""); 284 break; 285 default: 286 sbuf_delete(&sb); 287 return (EINVAL); 288 } 289 error = sbuf_finish(&sb); 290 sbuf_delete(&sb); 291 return (error); 292 } 293 294 static void 295 device_sysctl_init(device_t dev) 296 { 297 devclass_t dc = dev->devclass; 298 int domain; 299 300 if (dev->sysctl_tree != NULL) 301 return; 302 devclass_sysctl_init(dc); 303 sysctl_ctx_init(&dev->sysctl_ctx); 304 dev->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&dev->sysctl_ctx, 305 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO, 306 dev->nameunit + strlen(dc->name), 307 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "", "device_index"); 308 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 309 OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 310 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A", 311 "device description"); 312 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 313 OID_AUTO, "%driver", 314 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 315 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A", 316 "device driver name"); 317 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 318 OID_AUTO, "%location", 319 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 320 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A", 321 "device location relative to parent"); 322 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 323 OID_AUTO, "%pnpinfo", 324 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 325 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A", 326 "device identification"); 327 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 328 OID_AUTO, "%parent", 329 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 330 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A", 331 "parent device"); 332 if (bus_get_domain(dev, &domain) == 0) 333 SYSCTL_ADD_INT(&dev->sysctl_ctx, 334 SYSCTL_CHILDREN(dev->sysctl_tree), OID_AUTO, "%domain", 335 CTLFLAG_RD, NULL, domain, "NUMA domain"); 336 } 337 338 static void 339 device_sysctl_update(device_t dev) 340 { 341 devclass_t dc = dev->devclass; 342 343 if (dev->sysctl_tree == NULL) 344 return; 345 sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name)); 346 } 347 348 static void 349 device_sysctl_fini(device_t dev) 350 { 351 if (dev->sysctl_tree == NULL) 352 return; 353 sysctl_ctx_free(&dev->sysctl_ctx); 354 dev->sysctl_tree = NULL; 355 } 356 357 /* 358 * /dev/devctl implementation 359 */ 360 361 /* 362 * This design allows only one reader for /dev/devctl. This is not desirable 363 * in the long run, but will get a lot of hair out of this implementation. 364 * Maybe we should make this device a clonable device. 365 * 366 * Also note: we specifically do not attach a device to the device_t tree 367 * to avoid potential chicken and egg problems. One could argue that all 368 * of this belongs to the root node. 369 */ 370 371 #define DEVCTL_DEFAULT_QUEUE_LEN 1000 372 static int sysctl_devctl_queue(SYSCTL_HANDLER_ARGS); 373 static int devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN; 374 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_queue, CTLTYPE_INT | CTLFLAG_RWTUN | 375 CTLFLAG_MPSAFE, NULL, 0, sysctl_devctl_queue, "I", "devctl queue length"); 376 377 static d_open_t devopen; 378 static d_close_t devclose; 379 static d_read_t devread; 380 static d_ioctl_t devioctl; 381 static d_poll_t devpoll; 382 static d_kqfilter_t devkqfilter; 383 384 static struct cdevsw dev_cdevsw = { 385 .d_version = D_VERSION, 386 .d_open = devopen, 387 .d_close = devclose, 388 .d_read = devread, 389 .d_ioctl = devioctl, 390 .d_poll = devpoll, 391 .d_kqfilter = devkqfilter, 392 .d_name = "devctl", 393 }; 394 395 #define DEVCTL_BUFFER (1024 - sizeof(void *)) 396 struct dev_event_info { 397 STAILQ_ENTRY(dev_event_info) dei_link; 398 char dei_data[DEVCTL_BUFFER]; 399 }; 400 401 STAILQ_HEAD(devq, dev_event_info); 402 403 static struct dev_softc { 404 int inuse; 405 int nonblock; 406 int queued; 407 int async; 408 struct mtx mtx; 409 struct cv cv; 410 struct selinfo sel; 411 struct devq devq; 412 struct sigio *sigio; 413 uma_zone_t zone; 414 } devsoftc; 415 416 static void filt_devctl_detach(struct knote *kn); 417 static int filt_devctl_read(struct knote *kn, long hint); 418 419 struct filterops devctl_rfiltops = { 420 .f_isfd = 1, 421 .f_detach = filt_devctl_detach, 422 .f_event = filt_devctl_read, 423 }; 424 425 static struct cdev *devctl_dev; 426 427 static void 428 devinit(void) 429 { 430 devctl_dev = make_dev_credf(MAKEDEV_ETERNAL, &dev_cdevsw, 0, NULL, 431 UID_ROOT, GID_WHEEL, 0600, "devctl"); 432 mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF); 433 cv_init(&devsoftc.cv, "dev cv"); 434 STAILQ_INIT(&devsoftc.devq); 435 knlist_init_mtx(&devsoftc.sel.si_note, &devsoftc.mtx); 436 if (devctl_queue_length > 0) { 437 devsoftc.zone = uma_zcreate("DEVCTL", sizeof(struct dev_event_info), 438 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 439 uma_prealloc(devsoftc.zone, devctl_queue_length); 440 } 441 devctl2_init(); 442 } 443 444 static int 445 devopen(struct cdev *dev, int oflags, int devtype, struct thread *td) 446 { 447 mtx_lock(&devsoftc.mtx); 448 if (devsoftc.inuse) { 449 mtx_unlock(&devsoftc.mtx); 450 return (EBUSY); 451 } 452 /* move to init */ 453 devsoftc.inuse = 1; 454 mtx_unlock(&devsoftc.mtx); 455 return (0); 456 } 457 458 static int 459 devclose(struct cdev *dev, int fflag, int devtype, struct thread *td) 460 { 461 mtx_lock(&devsoftc.mtx); 462 devsoftc.inuse = 0; 463 devsoftc.nonblock = 0; 464 devsoftc.async = 0; 465 cv_broadcast(&devsoftc.cv); 466 funsetown(&devsoftc.sigio); 467 mtx_unlock(&devsoftc.mtx); 468 return (0); 469 } 470 471 /* 472 * The read channel for this device is used to report changes to 473 * userland in realtime. We are required to free the data as well as 474 * the n1 object because we allocate them separately. Also note that 475 * we return one record at a time. If you try to read this device a 476 * character at a time, you will lose the rest of the data. Listening 477 * programs are expected to cope. 478 */ 479 static int 480 devread(struct cdev *dev, struct uio *uio, int ioflag) 481 { 482 struct dev_event_info *n1; 483 int rv; 484 485 mtx_lock(&devsoftc.mtx); 486 while (STAILQ_EMPTY(&devsoftc.devq)) { 487 if (devsoftc.nonblock) { 488 mtx_unlock(&devsoftc.mtx); 489 return (EAGAIN); 490 } 491 rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx); 492 if (rv) { 493 /* 494 * Need to translate ERESTART to EINTR here? -- jake 495 */ 496 mtx_unlock(&devsoftc.mtx); 497 return (rv); 498 } 499 } 500 n1 = STAILQ_FIRST(&devsoftc.devq); 501 STAILQ_REMOVE_HEAD(&devsoftc.devq, dei_link); 502 devsoftc.queued--; 503 mtx_unlock(&devsoftc.mtx); 504 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio); 505 uma_zfree(devsoftc.zone, n1); 506 return (rv); 507 } 508 509 static int 510 devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 511 { 512 switch (cmd) { 513 case FIONBIO: 514 if (*(int*)data) 515 devsoftc.nonblock = 1; 516 else 517 devsoftc.nonblock = 0; 518 return (0); 519 case FIOASYNC: 520 if (*(int*)data) 521 devsoftc.async = 1; 522 else 523 devsoftc.async = 0; 524 return (0); 525 case FIOSETOWN: 526 return fsetown(*(int *)data, &devsoftc.sigio); 527 case FIOGETOWN: 528 *(int *)data = fgetown(&devsoftc.sigio); 529 return (0); 530 531 /* (un)Support for other fcntl() calls. */ 532 case FIOCLEX: 533 case FIONCLEX: 534 case FIONREAD: 535 default: 536 break; 537 } 538 return (ENOTTY); 539 } 540 541 static int 542 devpoll(struct cdev *dev, int events, struct thread *td) 543 { 544 int revents = 0; 545 546 mtx_lock(&devsoftc.mtx); 547 if (events & (POLLIN | POLLRDNORM)) { 548 if (!STAILQ_EMPTY(&devsoftc.devq)) 549 revents = events & (POLLIN | POLLRDNORM); 550 else 551 selrecord(td, &devsoftc.sel); 552 } 553 mtx_unlock(&devsoftc.mtx); 554 555 return (revents); 556 } 557 558 static int 559 devkqfilter(struct cdev *dev, struct knote *kn) 560 { 561 int error; 562 563 if (kn->kn_filter == EVFILT_READ) { 564 kn->kn_fop = &devctl_rfiltops; 565 knlist_add(&devsoftc.sel.si_note, kn, 0); 566 error = 0; 567 } else 568 error = EINVAL; 569 return (error); 570 } 571 572 static void 573 filt_devctl_detach(struct knote *kn) 574 { 575 knlist_remove(&devsoftc.sel.si_note, kn, 0); 576 } 577 578 static int 579 filt_devctl_read(struct knote *kn, long hint) 580 { 581 kn->kn_data = devsoftc.queued; 582 return (kn->kn_data != 0); 583 } 584 585 /** 586 * @brief Return whether the userland process is running 587 */ 588 boolean_t 589 devctl_process_running(void) 590 { 591 return (devsoftc.inuse == 1); 592 } 593 594 static struct dev_event_info * 595 devctl_alloc_dei(void) 596 { 597 struct dev_event_info *dei = NULL; 598 599 mtx_lock(&devsoftc.mtx); 600 if (devctl_queue_length == 0) 601 goto out; 602 if (devctl_queue_length == devsoftc.queued) { 603 dei = STAILQ_FIRST(&devsoftc.devq); 604 STAILQ_REMOVE_HEAD(&devsoftc.devq, dei_link); 605 devsoftc.queued--; 606 } else { 607 /* dei can't be NULL -- we know we have at least one in the zone */ 608 dei = uma_zalloc(devsoftc.zone, M_NOWAIT); 609 MPASS(dei != NULL); 610 } 611 *dei->dei_data = '\0'; 612 out: 613 mtx_unlock(&devsoftc.mtx); 614 return (dei); 615 } 616 617 static struct dev_event_info * 618 devctl_alloc_dei_sb(struct sbuf *sb) 619 { 620 struct dev_event_info *dei; 621 622 dei = devctl_alloc_dei(); 623 if (dei != NULL) 624 sbuf_new(sb, dei->dei_data, sizeof(dei->dei_data), SBUF_FIXEDLEN); 625 return (dei); 626 } 627 628 static void 629 devctl_free_dei(struct dev_event_info *dei) 630 { 631 uma_zfree(devsoftc.zone, dei); 632 } 633 634 static void 635 devctl_queue(struct dev_event_info *dei) 636 { 637 mtx_lock(&devsoftc.mtx); 638 STAILQ_INSERT_TAIL(&devsoftc.devq, dei, dei_link); 639 devsoftc.queued++; 640 cv_broadcast(&devsoftc.cv); 641 KNOTE_LOCKED(&devsoftc.sel.si_note, 0); 642 mtx_unlock(&devsoftc.mtx); 643 selwakeup(&devsoftc.sel); 644 if (devsoftc.async && devsoftc.sigio != NULL) 645 pgsigio(&devsoftc.sigio, SIGIO, 0); 646 } 647 648 /** 649 * @brief Send a 'notification' to userland, using standard ways 650 */ 651 void 652 devctl_notify(const char *system, const char *subsystem, const char *type, 653 const char *data) 654 { 655 struct dev_event_info *dei; 656 struct sbuf sb; 657 658 if (system == NULL || subsystem == NULL || type == NULL) 659 return; 660 dei = devctl_alloc_dei_sb(&sb); 661 if (dei == NULL) 662 return; 663 sbuf_cpy(&sb, "!system="); 664 sbuf_cat(&sb, system); 665 sbuf_cat(&sb, " subsystem="); 666 sbuf_cat(&sb, subsystem); 667 sbuf_cat(&sb, " type="); 668 sbuf_cat(&sb, type); 669 if (data != NULL) { 670 sbuf_putc(&sb, ' '); 671 sbuf_cat(&sb, data); 672 } 673 sbuf_putc(&sb, '\n'); 674 if (sbuf_finish(&sb) != 0) 675 devctl_free_dei(dei); /* overflow -> drop it */ 676 else 677 devctl_queue(dei); 678 } 679 680 /* 681 * Common routine that tries to make sending messages as easy as possible. 682 * We allocate memory for the data, copy strings into that, but do not 683 * free it unless there's an error. The dequeue part of the driver should 684 * free the data. We don't send data when the device is disabled. We do 685 * send data, even when we have no listeners, because we wish to avoid 686 * races relating to startup and restart of listening applications. 687 * 688 * devaddq is designed to string together the type of event, with the 689 * object of that event, plus the plug and play info and location info 690 * for that event. This is likely most useful for devices, but less 691 * useful for other consumers of this interface. Those should use 692 * the devctl_notify() interface instead. 693 * 694 * Output: 695 * ${type}${what} at $(location dev) $(pnp-info dev) on $(parent dev) 696 */ 697 static void 698 devaddq(const char *type, const char *what, device_t dev) 699 { 700 struct dev_event_info *dei; 701 const char *parstr; 702 struct sbuf sb; 703 704 dei = devctl_alloc_dei_sb(&sb); 705 if (dei == NULL) 706 return; 707 sbuf_cpy(&sb, type); 708 sbuf_cat(&sb, what); 709 sbuf_cat(&sb, " at "); 710 711 /* Add in the location */ 712 bus_child_location_sb(dev, &sb); 713 sbuf_putc(&sb, ' '); 714 715 /* Add in pnpinfo */ 716 bus_child_pnpinfo_sb(dev, &sb); 717 718 /* Get the parent of this device, or / if high enough in the tree. */ 719 if (device_get_parent(dev) == NULL) 720 parstr = "."; /* Or '/' ? */ 721 else 722 parstr = device_get_nameunit(device_get_parent(dev)); 723 sbuf_cat(&sb, " on "); 724 sbuf_cat(&sb, parstr); 725 sbuf_putc(&sb, '\n'); 726 if (sbuf_finish(&sb) != 0) 727 goto bad; 728 devctl_queue(dei); 729 return; 730 bad: 731 devctl_free_dei(dei); 732 } 733 734 /* 735 * A device was added to the tree. We are called just after it successfully 736 * attaches (that is, probe and attach success for this device). No call 737 * is made if a device is merely parented into the tree. See devnomatch 738 * if probe fails. If attach fails, no notification is sent (but maybe 739 * we should have a different message for this). 740 */ 741 static void 742 devadded(device_t dev) 743 { 744 devaddq("+", device_get_nameunit(dev), dev); 745 } 746 747 /* 748 * A device was removed from the tree. We are called just before this 749 * happens. 750 */ 751 static void 752 devremoved(device_t dev) 753 { 754 devaddq("-", device_get_nameunit(dev), dev); 755 } 756 757 /* 758 * Called when there's no match for this device. This is only called 759 * the first time that no match happens, so we don't keep getting this 760 * message. Should that prove to be undesirable, we can change it. 761 * This is called when all drivers that can attach to a given bus 762 * decline to accept this device. Other errors may not be detected. 763 */ 764 static void 765 devnomatch(device_t dev) 766 { 767 devaddq("?", "", dev); 768 } 769 770 static int 771 sysctl_devctl_queue(SYSCTL_HANDLER_ARGS) 772 { 773 int q, error; 774 775 q = devctl_queue_length; 776 error = sysctl_handle_int(oidp, &q, 0, req); 777 if (error || !req->newptr) 778 return (error); 779 if (q < 0) 780 return (EINVAL); 781 782 /* 783 * When set as a tunable, we've not yet initialized the mutex. 784 * It is safe to just assign to devctl_queue_length and return 785 * as we're racing no one. We'll use whatever value set in 786 * devinit. 787 */ 788 if (!mtx_initialized(&devsoftc.mtx)) { 789 devctl_queue_length = q; 790 return (0); 791 } 792 793 /* 794 * XXX It's hard to grow or shrink the UMA zone. Only allow 795 * disabling the queue size for the moment until underlying 796 * UMA issues can be sorted out. 797 */ 798 if (q != 0) 799 return (EINVAL); 800 if (q == devctl_queue_length) 801 return (0); 802 mtx_lock(&devsoftc.mtx); 803 devctl_queue_length = 0; 804 uma_zdestroy(devsoftc.zone); 805 devsoftc.zone = 0; 806 mtx_unlock(&devsoftc.mtx); 807 return (0); 808 } 809 810 /** 811 * @brief safely quotes strings that might have double quotes in them. 812 * 813 * The devctl protocol relies on quoted strings having matching quotes. 814 * This routine quotes any internal quotes so the resulting string 815 * is safe to pass to snprintf to construct, for example pnp info strings. 816 * 817 * @param sb sbuf to place the characters into 818 * @param src Original buffer. 819 */ 820 void 821 devctl_safe_quote_sb(struct sbuf *sb, const char *src) 822 { 823 while (*src != '\0') { 824 if (*src == '"' || *src == '\\') 825 sbuf_putc(sb, '\\'); 826 sbuf_putc(sb, *src++); 827 } 828 } 829 830 /* End of /dev/devctl code */ 831 832 static TAILQ_HEAD(,device) bus_data_devices; 833 static int bus_data_generation = 1; 834 835 static kobj_method_t null_methods[] = { 836 KOBJMETHOD_END 837 }; 838 839 DEFINE_CLASS(null, null_methods, 0); 840 841 /* 842 * Bus pass implementation 843 */ 844 845 static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes); 846 int bus_current_pass = BUS_PASS_ROOT; 847 848 /** 849 * @internal 850 * @brief Register the pass level of a new driver attachment 851 * 852 * Register a new driver attachment's pass level. If no driver 853 * attachment with the same pass level has been added, then @p new 854 * will be added to the global passes list. 855 * 856 * @param new the new driver attachment 857 */ 858 static void 859 driver_register_pass(struct driverlink *new) 860 { 861 struct driverlink *dl; 862 863 /* We only consider pass numbers during boot. */ 864 if (bus_current_pass == BUS_PASS_DEFAULT) 865 return; 866 867 /* 868 * Walk the passes list. If we already know about this pass 869 * then there is nothing to do. If we don't, then insert this 870 * driver link into the list. 871 */ 872 TAILQ_FOREACH(dl, &passes, passlink) { 873 if (dl->pass < new->pass) 874 continue; 875 if (dl->pass == new->pass) 876 return; 877 TAILQ_INSERT_BEFORE(dl, new, passlink); 878 return; 879 } 880 TAILQ_INSERT_TAIL(&passes, new, passlink); 881 } 882 883 /** 884 * @brief Raise the current bus pass 885 * 886 * Raise the current bus pass level to @p pass. Call the BUS_NEW_PASS() 887 * method on the root bus to kick off a new device tree scan for each 888 * new pass level that has at least one driver. 889 */ 890 void 891 bus_set_pass(int pass) 892 { 893 struct driverlink *dl; 894 895 if (bus_current_pass > pass) 896 panic("Attempt to lower bus pass level"); 897 898 TAILQ_FOREACH(dl, &passes, passlink) { 899 /* Skip pass values below the current pass level. */ 900 if (dl->pass <= bus_current_pass) 901 continue; 902 903 /* 904 * Bail once we hit a driver with a pass level that is 905 * too high. 906 */ 907 if (dl->pass > pass) 908 break; 909 910 /* 911 * Raise the pass level to the next level and rescan 912 * the tree. 913 */ 914 bus_current_pass = dl->pass; 915 BUS_NEW_PASS(root_bus); 916 } 917 918 /* 919 * If there isn't a driver registered for the requested pass, 920 * then bus_current_pass might still be less than 'pass'. Set 921 * it to 'pass' in that case. 922 */ 923 if (bus_current_pass < pass) 924 bus_current_pass = pass; 925 KASSERT(bus_current_pass == pass, ("Failed to update bus pass level")); 926 } 927 928 /* 929 * Devclass implementation 930 */ 931 932 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses); 933 934 /** 935 * @internal 936 * @brief Find or create a device class 937 * 938 * If a device class with the name @p classname exists, return it, 939 * otherwise if @p create is non-zero create and return a new device 940 * class. 941 * 942 * If @p parentname is non-NULL, the parent of the devclass is set to 943 * the devclass of that name. 944 * 945 * @param classname the devclass name to find or create 946 * @param parentname the parent devclass name or @c NULL 947 * @param create non-zero to create a devclass 948 */ 949 static devclass_t 950 devclass_find_internal(const char *classname, const char *parentname, 951 int create) 952 { 953 devclass_t dc; 954 955 PDEBUG(("looking for %s", classname)); 956 if (!classname) 957 return (NULL); 958 959 TAILQ_FOREACH(dc, &devclasses, link) { 960 if (!strcmp(dc->name, classname)) 961 break; 962 } 963 964 if (create && !dc) { 965 PDEBUG(("creating %s", classname)); 966 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1, 967 M_BUS, M_NOWAIT | M_ZERO); 968 if (!dc) 969 return (NULL); 970 dc->parent = NULL; 971 dc->name = (char*) (dc + 1); 972 strcpy(dc->name, classname); 973 TAILQ_INIT(&dc->drivers); 974 TAILQ_INSERT_TAIL(&devclasses, dc, link); 975 976 bus_data_generation_update(); 977 } 978 979 /* 980 * If a parent class is specified, then set that as our parent so 981 * that this devclass will support drivers for the parent class as 982 * well. If the parent class has the same name don't do this though 983 * as it creates a cycle that can trigger an infinite loop in 984 * device_probe_child() if a device exists for which there is no 985 * suitable driver. 986 */ 987 if (parentname && dc && !dc->parent && 988 strcmp(classname, parentname) != 0) { 989 dc->parent = devclass_find_internal(parentname, NULL, TRUE); 990 dc->parent->flags |= DC_HAS_CHILDREN; 991 } 992 993 return (dc); 994 } 995 996 /** 997 * @brief Create a device class 998 * 999 * If a device class with the name @p classname exists, return it, 1000 * otherwise create and return a new device class. 1001 * 1002 * @param classname the devclass name to find or create 1003 */ 1004 devclass_t 1005 devclass_create(const char *classname) 1006 { 1007 return (devclass_find_internal(classname, NULL, TRUE)); 1008 } 1009 1010 /** 1011 * @brief Find a device class 1012 * 1013 * If a device class with the name @p classname exists, return it, 1014 * otherwise return @c NULL. 1015 * 1016 * @param classname the devclass name to find 1017 */ 1018 devclass_t 1019 devclass_find(const char *classname) 1020 { 1021 return (devclass_find_internal(classname, NULL, FALSE)); 1022 } 1023 1024 /** 1025 * @brief Register that a device driver has been added to a devclass 1026 * 1027 * Register that a device driver has been added to a devclass. This 1028 * is called by devclass_add_driver to accomplish the recursive 1029 * notification of all the children classes of dc, as well as dc. 1030 * Each layer will have BUS_DRIVER_ADDED() called for all instances of 1031 * the devclass. 1032 * 1033 * We do a full search here of the devclass list at each iteration 1034 * level to save storing children-lists in the devclass structure. If 1035 * we ever move beyond a few dozen devices doing this, we may need to 1036 * reevaluate... 1037 * 1038 * @param dc the devclass to edit 1039 * @param driver the driver that was just added 1040 */ 1041 static void 1042 devclass_driver_added(devclass_t dc, driver_t *driver) 1043 { 1044 devclass_t parent; 1045 int i; 1046 1047 /* 1048 * Call BUS_DRIVER_ADDED for any existing buses in this class. 1049 */ 1050 for (i = 0; i < dc->maxunit; i++) 1051 if (dc->devices[i] && device_is_attached(dc->devices[i])) 1052 BUS_DRIVER_ADDED(dc->devices[i], driver); 1053 1054 /* 1055 * Walk through the children classes. Since we only keep a 1056 * single parent pointer around, we walk the entire list of 1057 * devclasses looking for children. We set the 1058 * DC_HAS_CHILDREN flag when a child devclass is created on 1059 * the parent, so we only walk the list for those devclasses 1060 * that have children. 1061 */ 1062 if (!(dc->flags & DC_HAS_CHILDREN)) 1063 return; 1064 parent = dc; 1065 TAILQ_FOREACH(dc, &devclasses, link) { 1066 if (dc->parent == parent) 1067 devclass_driver_added(dc, driver); 1068 } 1069 } 1070 1071 /** 1072 * @brief Add a device driver to a device class 1073 * 1074 * Add a device driver to a devclass. This is normally called 1075 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of 1076 * all devices in the devclass will be called to allow them to attempt 1077 * to re-probe any unmatched children. 1078 * 1079 * @param dc the devclass to edit 1080 * @param driver the driver to register 1081 */ 1082 int 1083 devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp) 1084 { 1085 driverlink_t dl; 1086 const char *parentname; 1087 1088 PDEBUG(("%s", DRIVERNAME(driver))); 1089 1090 /* Don't allow invalid pass values. */ 1091 if (pass <= BUS_PASS_ROOT) 1092 return (EINVAL); 1093 1094 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO); 1095 if (!dl) 1096 return (ENOMEM); 1097 1098 /* 1099 * Compile the driver's methods. Also increase the reference count 1100 * so that the class doesn't get freed when the last instance 1101 * goes. This means we can safely use static methods and avoids a 1102 * double-free in devclass_delete_driver. 1103 */ 1104 kobj_class_compile((kobj_class_t) driver); 1105 1106 /* 1107 * If the driver has any base classes, make the 1108 * devclass inherit from the devclass of the driver's 1109 * first base class. This will allow the system to 1110 * search for drivers in both devclasses for children 1111 * of a device using this driver. 1112 */ 1113 if (driver->baseclasses) 1114 parentname = driver->baseclasses[0]->name; 1115 else 1116 parentname = NULL; 1117 *dcp = devclass_find_internal(driver->name, parentname, TRUE); 1118 1119 dl->driver = driver; 1120 TAILQ_INSERT_TAIL(&dc->drivers, dl, link); 1121 driver->refs++; /* XXX: kobj_mtx */ 1122 dl->pass = pass; 1123 driver_register_pass(dl); 1124 1125 if (device_frozen) { 1126 dl->flags |= DL_DEFERRED_PROBE; 1127 } else { 1128 devclass_driver_added(dc, driver); 1129 } 1130 bus_data_generation_update(); 1131 return (0); 1132 } 1133 1134 /** 1135 * @brief Register that a device driver has been deleted from a devclass 1136 * 1137 * Register that a device driver has been removed from a devclass. 1138 * This is called by devclass_delete_driver to accomplish the 1139 * recursive notification of all the children classes of busclass, as 1140 * well as busclass. Each layer will attempt to detach the driver 1141 * from any devices that are children of the bus's devclass. The function 1142 * will return an error if a device fails to detach. 1143 * 1144 * We do a full search here of the devclass list at each iteration 1145 * level to save storing children-lists in the devclass structure. If 1146 * we ever move beyond a few dozen devices doing this, we may need to 1147 * reevaluate... 1148 * 1149 * @param busclass the devclass of the parent bus 1150 * @param dc the devclass of the driver being deleted 1151 * @param driver the driver being deleted 1152 */ 1153 static int 1154 devclass_driver_deleted(devclass_t busclass, devclass_t dc, driver_t *driver) 1155 { 1156 devclass_t parent; 1157 device_t dev; 1158 int error, i; 1159 1160 /* 1161 * Disassociate from any devices. We iterate through all the 1162 * devices in the devclass of the driver and detach any which are 1163 * using the driver and which have a parent in the devclass which 1164 * we are deleting from. 1165 * 1166 * Note that since a driver can be in multiple devclasses, we 1167 * should not detach devices which are not children of devices in 1168 * the affected devclass. 1169 * 1170 * If we're frozen, we don't generate NOMATCH events. Mark to 1171 * generate later. 1172 */ 1173 for (i = 0; i < dc->maxunit; i++) { 1174 if (dc->devices[i]) { 1175 dev = dc->devices[i]; 1176 if (dev->driver == driver && dev->parent && 1177 dev->parent->devclass == busclass) { 1178 if ((error = device_detach(dev)) != 0) 1179 return (error); 1180 if (device_frozen) { 1181 dev->flags &= ~DF_DONENOMATCH; 1182 dev->flags |= DF_NEEDNOMATCH; 1183 } else { 1184 BUS_PROBE_NOMATCH(dev->parent, dev); 1185 devnomatch(dev); 1186 dev->flags |= DF_DONENOMATCH; 1187 } 1188 } 1189 } 1190 } 1191 1192 /* 1193 * Walk through the children classes. Since we only keep a 1194 * single parent pointer around, we walk the entire list of 1195 * devclasses looking for children. We set the 1196 * DC_HAS_CHILDREN flag when a child devclass is created on 1197 * the parent, so we only walk the list for those devclasses 1198 * that have children. 1199 */ 1200 if (!(busclass->flags & DC_HAS_CHILDREN)) 1201 return (0); 1202 parent = busclass; 1203 TAILQ_FOREACH(busclass, &devclasses, link) { 1204 if (busclass->parent == parent) { 1205 error = devclass_driver_deleted(busclass, dc, driver); 1206 if (error) 1207 return (error); 1208 } 1209 } 1210 return (0); 1211 } 1212 1213 /** 1214 * @brief Delete a device driver from a device class 1215 * 1216 * Delete a device driver from a devclass. This is normally called 1217 * automatically by DRIVER_MODULE(). 1218 * 1219 * If the driver is currently attached to any devices, 1220 * devclass_delete_driver() will first attempt to detach from each 1221 * device. If one of the detach calls fails, the driver will not be 1222 * deleted. 1223 * 1224 * @param dc the devclass to edit 1225 * @param driver the driver to unregister 1226 */ 1227 int 1228 devclass_delete_driver(devclass_t busclass, driver_t *driver) 1229 { 1230 devclass_t dc = devclass_find(driver->name); 1231 driverlink_t dl; 1232 int error; 1233 1234 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass))); 1235 1236 if (!dc) 1237 return (0); 1238 1239 /* 1240 * Find the link structure in the bus' list of drivers. 1241 */ 1242 TAILQ_FOREACH(dl, &busclass->drivers, link) { 1243 if (dl->driver == driver) 1244 break; 1245 } 1246 1247 if (!dl) { 1248 PDEBUG(("%s not found in %s list", driver->name, 1249 busclass->name)); 1250 return (ENOENT); 1251 } 1252 1253 error = devclass_driver_deleted(busclass, dc, driver); 1254 if (error != 0) 1255 return (error); 1256 1257 TAILQ_REMOVE(&busclass->drivers, dl, link); 1258 free(dl, M_BUS); 1259 1260 /* XXX: kobj_mtx */ 1261 driver->refs--; 1262 if (driver->refs == 0) 1263 kobj_class_free((kobj_class_t) driver); 1264 1265 bus_data_generation_update(); 1266 return (0); 1267 } 1268 1269 /** 1270 * @brief Quiesces a set of device drivers from a device class 1271 * 1272 * Quiesce a device driver from a devclass. This is normally called 1273 * automatically by DRIVER_MODULE(). 1274 * 1275 * If the driver is currently attached to any devices, 1276 * devclass_quiesece_driver() will first attempt to quiesce each 1277 * device. 1278 * 1279 * @param dc the devclass to edit 1280 * @param driver the driver to unregister 1281 */ 1282 static int 1283 devclass_quiesce_driver(devclass_t busclass, driver_t *driver) 1284 { 1285 devclass_t dc = devclass_find(driver->name); 1286 driverlink_t dl; 1287 device_t dev; 1288 int i; 1289 int error; 1290 1291 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass))); 1292 1293 if (!dc) 1294 return (0); 1295 1296 /* 1297 * Find the link structure in the bus' list of drivers. 1298 */ 1299 TAILQ_FOREACH(dl, &busclass->drivers, link) { 1300 if (dl->driver == driver) 1301 break; 1302 } 1303 1304 if (!dl) { 1305 PDEBUG(("%s not found in %s list", driver->name, 1306 busclass->name)); 1307 return (ENOENT); 1308 } 1309 1310 /* 1311 * Quiesce all devices. We iterate through all the devices in 1312 * the devclass of the driver and quiesce any which are using 1313 * the driver and which have a parent in the devclass which we 1314 * are quiescing. 1315 * 1316 * Note that since a driver can be in multiple devclasses, we 1317 * should not quiesce devices which are not children of 1318 * devices in the affected devclass. 1319 */ 1320 for (i = 0; i < dc->maxunit; i++) { 1321 if (dc->devices[i]) { 1322 dev = dc->devices[i]; 1323 if (dev->driver == driver && dev->parent && 1324 dev->parent->devclass == busclass) { 1325 if ((error = device_quiesce(dev)) != 0) 1326 return (error); 1327 } 1328 } 1329 } 1330 1331 return (0); 1332 } 1333 1334 /** 1335 * @internal 1336 */ 1337 static driverlink_t 1338 devclass_find_driver_internal(devclass_t dc, const char *classname) 1339 { 1340 driverlink_t dl; 1341 1342 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc))); 1343 1344 TAILQ_FOREACH(dl, &dc->drivers, link) { 1345 if (!strcmp(dl->driver->name, classname)) 1346 return (dl); 1347 } 1348 1349 PDEBUG(("not found")); 1350 return (NULL); 1351 } 1352 1353 /** 1354 * @brief Return the name of the devclass 1355 */ 1356 const char * 1357 devclass_get_name(devclass_t dc) 1358 { 1359 return (dc->name); 1360 } 1361 1362 /** 1363 * @brief Find a device given a unit number 1364 * 1365 * @param dc the devclass to search 1366 * @param unit the unit number to search for 1367 * 1368 * @returns the device with the given unit number or @c 1369 * NULL if there is no such device 1370 */ 1371 device_t 1372 devclass_get_device(devclass_t dc, int unit) 1373 { 1374 if (dc == NULL || unit < 0 || unit >= dc->maxunit) 1375 return (NULL); 1376 return (dc->devices[unit]); 1377 } 1378 1379 /** 1380 * @brief Find the softc field of a device given a unit number 1381 * 1382 * @param dc the devclass to search 1383 * @param unit the unit number to search for 1384 * 1385 * @returns the softc field of the device with the given 1386 * unit number or @c NULL if there is no such 1387 * device 1388 */ 1389 void * 1390 devclass_get_softc(devclass_t dc, int unit) 1391 { 1392 device_t dev; 1393 1394 dev = devclass_get_device(dc, unit); 1395 if (!dev) 1396 return (NULL); 1397 1398 return (device_get_softc(dev)); 1399 } 1400 1401 /** 1402 * @brief Get a list of devices in the devclass 1403 * 1404 * An array containing a list of all the devices in the given devclass 1405 * is allocated and returned in @p *devlistp. The number of devices 1406 * in the array is returned in @p *devcountp. The caller should free 1407 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0. 1408 * 1409 * @param dc the devclass to examine 1410 * @param devlistp points at location for array pointer return 1411 * value 1412 * @param devcountp points at location for array size return value 1413 * 1414 * @retval 0 success 1415 * @retval ENOMEM the array allocation failed 1416 */ 1417 int 1418 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp) 1419 { 1420 int count, i; 1421 device_t *list; 1422 1423 count = devclass_get_count(dc); 1424 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO); 1425 if (!list) 1426 return (ENOMEM); 1427 1428 count = 0; 1429 for (i = 0; i < dc->maxunit; i++) { 1430 if (dc->devices[i]) { 1431 list[count] = dc->devices[i]; 1432 count++; 1433 } 1434 } 1435 1436 *devlistp = list; 1437 *devcountp = count; 1438 1439 return (0); 1440 } 1441 1442 /** 1443 * @brief Get a list of drivers in the devclass 1444 * 1445 * An array containing a list of pointers to all the drivers in the 1446 * given devclass is allocated and returned in @p *listp. The number 1447 * of drivers in the array is returned in @p *countp. The caller should 1448 * free the array using @c free(p, M_TEMP). 1449 * 1450 * @param dc the devclass to examine 1451 * @param listp gives location for array pointer return value 1452 * @param countp gives location for number of array elements 1453 * return value 1454 * 1455 * @retval 0 success 1456 * @retval ENOMEM the array allocation failed 1457 */ 1458 int 1459 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp) 1460 { 1461 driverlink_t dl; 1462 driver_t **list; 1463 int count; 1464 1465 count = 0; 1466 TAILQ_FOREACH(dl, &dc->drivers, link) 1467 count++; 1468 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT); 1469 if (list == NULL) 1470 return (ENOMEM); 1471 1472 count = 0; 1473 TAILQ_FOREACH(dl, &dc->drivers, link) { 1474 list[count] = dl->driver; 1475 count++; 1476 } 1477 *listp = list; 1478 *countp = count; 1479 1480 return (0); 1481 } 1482 1483 /** 1484 * @brief Get the number of devices in a devclass 1485 * 1486 * @param dc the devclass to examine 1487 */ 1488 int 1489 devclass_get_count(devclass_t dc) 1490 { 1491 int count, i; 1492 1493 count = 0; 1494 for (i = 0; i < dc->maxunit; i++) 1495 if (dc->devices[i]) 1496 count++; 1497 return (count); 1498 } 1499 1500 /** 1501 * @brief Get the maximum unit number used in a devclass 1502 * 1503 * Note that this is one greater than the highest currently-allocated 1504 * unit. If a null devclass_t is passed in, -1 is returned to indicate 1505 * that not even the devclass has been allocated yet. 1506 * 1507 * @param dc the devclass to examine 1508 */ 1509 int 1510 devclass_get_maxunit(devclass_t dc) 1511 { 1512 if (dc == NULL) 1513 return (-1); 1514 return (dc->maxunit); 1515 } 1516 1517 /** 1518 * @brief Find a free unit number in a devclass 1519 * 1520 * This function searches for the first unused unit number greater 1521 * that or equal to @p unit. 1522 * 1523 * @param dc the devclass to examine 1524 * @param unit the first unit number to check 1525 */ 1526 int 1527 devclass_find_free_unit(devclass_t dc, int unit) 1528 { 1529 if (dc == NULL) 1530 return (unit); 1531 while (unit < dc->maxunit && dc->devices[unit] != NULL) 1532 unit++; 1533 return (unit); 1534 } 1535 1536 /** 1537 * @brief Set the parent of a devclass 1538 * 1539 * The parent class is normally initialised automatically by 1540 * DRIVER_MODULE(). 1541 * 1542 * @param dc the devclass to edit 1543 * @param pdc the new parent devclass 1544 */ 1545 void 1546 devclass_set_parent(devclass_t dc, devclass_t pdc) 1547 { 1548 dc->parent = pdc; 1549 } 1550 1551 /** 1552 * @brief Get the parent of a devclass 1553 * 1554 * @param dc the devclass to examine 1555 */ 1556 devclass_t 1557 devclass_get_parent(devclass_t dc) 1558 { 1559 return (dc->parent); 1560 } 1561 1562 struct sysctl_ctx_list * 1563 devclass_get_sysctl_ctx(devclass_t dc) 1564 { 1565 return (&dc->sysctl_ctx); 1566 } 1567 1568 struct sysctl_oid * 1569 devclass_get_sysctl_tree(devclass_t dc) 1570 { 1571 return (dc->sysctl_tree); 1572 } 1573 1574 /** 1575 * @internal 1576 * @brief Allocate a unit number 1577 * 1578 * On entry, @p *unitp is the desired unit number (or @c -1 if any 1579 * will do). The allocated unit number is returned in @p *unitp. 1580 1581 * @param dc the devclass to allocate from 1582 * @param unitp points at the location for the allocated unit 1583 * number 1584 * 1585 * @retval 0 success 1586 * @retval EEXIST the requested unit number is already allocated 1587 * @retval ENOMEM memory allocation failure 1588 */ 1589 static int 1590 devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp) 1591 { 1592 const char *s; 1593 int unit = *unitp; 1594 1595 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc))); 1596 1597 /* Ask the parent bus if it wants to wire this device. */ 1598 if (unit == -1) 1599 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name, 1600 &unit); 1601 1602 /* If we were given a wired unit number, check for existing device */ 1603 /* XXX imp XXX */ 1604 if (unit != -1) { 1605 if (unit >= 0 && unit < dc->maxunit && 1606 dc->devices[unit] != NULL) { 1607 if (bootverbose) 1608 printf("%s: %s%d already exists; skipping it\n", 1609 dc->name, dc->name, *unitp); 1610 return (EEXIST); 1611 } 1612 } else { 1613 /* Unwired device, find the next available slot for it */ 1614 unit = 0; 1615 for (unit = 0;; unit++) { 1616 /* If there is an "at" hint for a unit then skip it. */ 1617 if (resource_string_value(dc->name, unit, "at", &s) == 1618 0) 1619 continue; 1620 1621 /* If this device slot is already in use, skip it. */ 1622 if (unit < dc->maxunit && dc->devices[unit] != NULL) 1623 continue; 1624 1625 break; 1626 } 1627 } 1628 1629 /* 1630 * We've selected a unit beyond the length of the table, so let's 1631 * extend the table to make room for all units up to and including 1632 * this one. 1633 */ 1634 if (unit >= dc->maxunit) { 1635 device_t *newlist, *oldlist; 1636 int newsize; 1637 1638 oldlist = dc->devices; 1639 newsize = roundup((unit + 1), 1640 MAX(1, MINALLOCSIZE / sizeof(device_t))); 1641 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT); 1642 if (!newlist) 1643 return (ENOMEM); 1644 if (oldlist != NULL) 1645 bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit); 1646 bzero(newlist + dc->maxunit, 1647 sizeof(device_t) * (newsize - dc->maxunit)); 1648 dc->devices = newlist; 1649 dc->maxunit = newsize; 1650 if (oldlist != NULL) 1651 free(oldlist, M_BUS); 1652 } 1653 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc))); 1654 1655 *unitp = unit; 1656 return (0); 1657 } 1658 1659 /** 1660 * @internal 1661 * @brief Add a device to a devclass 1662 * 1663 * A unit number is allocated for the device (using the device's 1664 * preferred unit number if any) and the device is registered in the 1665 * devclass. This allows the device to be looked up by its unit 1666 * number, e.g. by decoding a dev_t minor number. 1667 * 1668 * @param dc the devclass to add to 1669 * @param dev the device to add 1670 * 1671 * @retval 0 success 1672 * @retval EEXIST the requested unit number is already allocated 1673 * @retval ENOMEM memory allocation failure 1674 */ 1675 static int 1676 devclass_add_device(devclass_t dc, device_t dev) 1677 { 1678 int buflen, error; 1679 1680 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 1681 1682 buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX); 1683 if (buflen < 0) 1684 return (ENOMEM); 1685 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO); 1686 if (!dev->nameunit) 1687 return (ENOMEM); 1688 1689 if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) { 1690 free(dev->nameunit, M_BUS); 1691 dev->nameunit = NULL; 1692 return (error); 1693 } 1694 dc->devices[dev->unit] = dev; 1695 dev->devclass = dc; 1696 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit); 1697 1698 return (0); 1699 } 1700 1701 /** 1702 * @internal 1703 * @brief Delete a device from a devclass 1704 * 1705 * The device is removed from the devclass's device list and its unit 1706 * number is freed. 1707 1708 * @param dc the devclass to delete from 1709 * @param dev the device to delete 1710 * 1711 * @retval 0 success 1712 */ 1713 static int 1714 devclass_delete_device(devclass_t dc, device_t dev) 1715 { 1716 if (!dc || !dev) 1717 return (0); 1718 1719 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 1720 1721 if (dev->devclass != dc || dc->devices[dev->unit] != dev) 1722 panic("devclass_delete_device: inconsistent device class"); 1723 dc->devices[dev->unit] = NULL; 1724 if (dev->flags & DF_WILDCARD) 1725 dev->unit = -1; 1726 dev->devclass = NULL; 1727 free(dev->nameunit, M_BUS); 1728 dev->nameunit = NULL; 1729 1730 return (0); 1731 } 1732 1733 /** 1734 * @internal 1735 * @brief Make a new device and add it as a child of @p parent 1736 * 1737 * @param parent the parent of the new device 1738 * @param name the devclass name of the new device or @c NULL 1739 * to leave the devclass unspecified 1740 * @parem unit the unit number of the new device of @c -1 to 1741 * leave the unit number unspecified 1742 * 1743 * @returns the new device 1744 */ 1745 static device_t 1746 make_device(device_t parent, const char *name, int unit) 1747 { 1748 device_t dev; 1749 devclass_t dc; 1750 1751 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit)); 1752 1753 if (name) { 1754 dc = devclass_find_internal(name, NULL, TRUE); 1755 if (!dc) { 1756 printf("make_device: can't find device class %s\n", 1757 name); 1758 return (NULL); 1759 } 1760 } else { 1761 dc = NULL; 1762 } 1763 1764 dev = malloc(sizeof(*dev), M_BUS, M_NOWAIT|M_ZERO); 1765 if (!dev) 1766 return (NULL); 1767 1768 dev->parent = parent; 1769 TAILQ_INIT(&dev->children); 1770 kobj_init((kobj_t) dev, &null_class); 1771 dev->driver = NULL; 1772 dev->devclass = NULL; 1773 dev->unit = unit; 1774 dev->nameunit = NULL; 1775 dev->desc = NULL; 1776 dev->busy = 0; 1777 dev->devflags = 0; 1778 dev->flags = DF_ENABLED; 1779 dev->order = 0; 1780 if (unit == -1) 1781 dev->flags |= DF_WILDCARD; 1782 if (name) { 1783 dev->flags |= DF_FIXEDCLASS; 1784 if (devclass_add_device(dc, dev)) { 1785 kobj_delete((kobj_t) dev, M_BUS); 1786 return (NULL); 1787 } 1788 } 1789 if (parent != NULL && device_has_quiet_children(parent)) 1790 dev->flags |= DF_QUIET | DF_QUIET_CHILDREN; 1791 dev->ivars = NULL; 1792 dev->softc = NULL; 1793 1794 dev->state = DS_NOTPRESENT; 1795 1796 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink); 1797 bus_data_generation_update(); 1798 1799 return (dev); 1800 } 1801 1802 /** 1803 * @internal 1804 * @brief Print a description of a device. 1805 */ 1806 static int 1807 device_print_child(device_t dev, device_t child) 1808 { 1809 int retval = 0; 1810 1811 if (device_is_alive(child)) 1812 retval += BUS_PRINT_CHILD(dev, child); 1813 else 1814 retval += device_printf(child, " not found\n"); 1815 1816 return (retval); 1817 } 1818 1819 /** 1820 * @brief Create a new device 1821 * 1822 * This creates a new device and adds it as a child of an existing 1823 * parent device. The new device will be added after the last existing 1824 * child with order zero. 1825 * 1826 * @param dev the device which will be the parent of the 1827 * new child device 1828 * @param name devclass name for new device or @c NULL if not 1829 * specified 1830 * @param unit unit number for new device or @c -1 if not 1831 * specified 1832 * 1833 * @returns the new device 1834 */ 1835 device_t 1836 device_add_child(device_t dev, const char *name, int unit) 1837 { 1838 return (device_add_child_ordered(dev, 0, name, unit)); 1839 } 1840 1841 /** 1842 * @brief Create a new device 1843 * 1844 * This creates a new device and adds it as a child of an existing 1845 * parent device. The new device will be added after the last existing 1846 * child with the same order. 1847 * 1848 * @param dev the device which will be the parent of the 1849 * new child device 1850 * @param order a value which is used to partially sort the 1851 * children of @p dev - devices created using 1852 * lower values of @p order appear first in @p 1853 * dev's list of children 1854 * @param name devclass name for new device or @c NULL if not 1855 * specified 1856 * @param unit unit number for new device or @c -1 if not 1857 * specified 1858 * 1859 * @returns the new device 1860 */ 1861 device_t 1862 device_add_child_ordered(device_t dev, u_int order, const char *name, int unit) 1863 { 1864 device_t child; 1865 device_t place; 1866 1867 PDEBUG(("%s at %s with order %u as unit %d", 1868 name, DEVICENAME(dev), order, unit)); 1869 KASSERT(name != NULL || unit == -1, 1870 ("child device with wildcard name and specific unit number")); 1871 1872 child = make_device(dev, name, unit); 1873 if (child == NULL) 1874 return (child); 1875 child->order = order; 1876 1877 TAILQ_FOREACH(place, &dev->children, link) { 1878 if (place->order > order) 1879 break; 1880 } 1881 1882 if (place) { 1883 /* 1884 * The device 'place' is the first device whose order is 1885 * greater than the new child. 1886 */ 1887 TAILQ_INSERT_BEFORE(place, child, link); 1888 } else { 1889 /* 1890 * The new child's order is greater or equal to the order of 1891 * any existing device. Add the child to the tail of the list. 1892 */ 1893 TAILQ_INSERT_TAIL(&dev->children, child, link); 1894 } 1895 1896 bus_data_generation_update(); 1897 return (child); 1898 } 1899 1900 /** 1901 * @brief Delete a device 1902 * 1903 * This function deletes a device along with all of its children. If 1904 * the device currently has a driver attached to it, the device is 1905 * detached first using device_detach(). 1906 * 1907 * @param dev the parent device 1908 * @param child the device to delete 1909 * 1910 * @retval 0 success 1911 * @retval non-zero a unit error code describing the error 1912 */ 1913 int 1914 device_delete_child(device_t dev, device_t child) 1915 { 1916 int error; 1917 device_t grandchild; 1918 1919 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev))); 1920 1921 /* detach parent before deleting children, if any */ 1922 if ((error = device_detach(child)) != 0) 1923 return (error); 1924 1925 /* remove children second */ 1926 while ((grandchild = TAILQ_FIRST(&child->children)) != NULL) { 1927 error = device_delete_child(child, grandchild); 1928 if (error) 1929 return (error); 1930 } 1931 1932 if (child->devclass) 1933 devclass_delete_device(child->devclass, child); 1934 if (child->parent) 1935 BUS_CHILD_DELETED(dev, child); 1936 TAILQ_REMOVE(&dev->children, child, link); 1937 TAILQ_REMOVE(&bus_data_devices, child, devlink); 1938 kobj_delete((kobj_t) child, M_BUS); 1939 1940 bus_data_generation_update(); 1941 return (0); 1942 } 1943 1944 /** 1945 * @brief Delete all children devices of the given device, if any. 1946 * 1947 * This function deletes all children devices of the given device, if 1948 * any, using the device_delete_child() function for each device it 1949 * finds. If a child device cannot be deleted, this function will 1950 * return an error code. 1951 * 1952 * @param dev the parent device 1953 * 1954 * @retval 0 success 1955 * @retval non-zero a device would not detach 1956 */ 1957 int 1958 device_delete_children(device_t dev) 1959 { 1960 device_t child; 1961 int error; 1962 1963 PDEBUG(("Deleting all children of %s", DEVICENAME(dev))); 1964 1965 error = 0; 1966 1967 while ((child = TAILQ_FIRST(&dev->children)) != NULL) { 1968 error = device_delete_child(dev, child); 1969 if (error) { 1970 PDEBUG(("Failed deleting %s", DEVICENAME(child))); 1971 break; 1972 } 1973 } 1974 return (error); 1975 } 1976 1977 /** 1978 * @brief Find a device given a unit number 1979 * 1980 * This is similar to devclass_get_devices() but only searches for 1981 * devices which have @p dev as a parent. 1982 * 1983 * @param dev the parent device to search 1984 * @param unit the unit number to search for. If the unit is -1, 1985 * return the first child of @p dev which has name 1986 * @p classname (that is, the one with the lowest unit.) 1987 * 1988 * @returns the device with the given unit number or @c 1989 * NULL if there is no such device 1990 */ 1991 device_t 1992 device_find_child(device_t dev, const char *classname, int unit) 1993 { 1994 devclass_t dc; 1995 device_t child; 1996 1997 dc = devclass_find(classname); 1998 if (!dc) 1999 return (NULL); 2000 2001 if (unit != -1) { 2002 child = devclass_get_device(dc, unit); 2003 if (child && child->parent == dev) 2004 return (child); 2005 } else { 2006 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) { 2007 child = devclass_get_device(dc, unit); 2008 if (child && child->parent == dev) 2009 return (child); 2010 } 2011 } 2012 return (NULL); 2013 } 2014 2015 /** 2016 * @internal 2017 */ 2018 static driverlink_t 2019 first_matching_driver(devclass_t dc, device_t dev) 2020 { 2021 if (dev->devclass) 2022 return (devclass_find_driver_internal(dc, dev->devclass->name)); 2023 return (TAILQ_FIRST(&dc->drivers)); 2024 } 2025 2026 /** 2027 * @internal 2028 */ 2029 static driverlink_t 2030 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last) 2031 { 2032 if (dev->devclass) { 2033 driverlink_t dl; 2034 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link)) 2035 if (!strcmp(dev->devclass->name, dl->driver->name)) 2036 return (dl); 2037 return (NULL); 2038 } 2039 return (TAILQ_NEXT(last, link)); 2040 } 2041 2042 /** 2043 * @internal 2044 */ 2045 int 2046 device_probe_child(device_t dev, device_t child) 2047 { 2048 devclass_t dc; 2049 driverlink_t best = NULL; 2050 driverlink_t dl; 2051 int result, pri = 0; 2052 int hasclass = (child->devclass != NULL); 2053 2054 GIANT_REQUIRED; 2055 2056 dc = dev->devclass; 2057 if (!dc) 2058 panic("device_probe_child: parent device has no devclass"); 2059 2060 /* 2061 * If the state is already probed, then return. However, don't 2062 * return if we can rebid this object. 2063 */ 2064 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0) 2065 return (0); 2066 2067 for (; dc; dc = dc->parent) { 2068 for (dl = first_matching_driver(dc, child); 2069 dl; 2070 dl = next_matching_driver(dc, child, dl)) { 2071 /* If this driver's pass is too high, then ignore it. */ 2072 if (dl->pass > bus_current_pass) 2073 continue; 2074 2075 PDEBUG(("Trying %s", DRIVERNAME(dl->driver))); 2076 result = device_set_driver(child, dl->driver); 2077 if (result == ENOMEM) 2078 return (result); 2079 else if (result != 0) 2080 continue; 2081 if (!hasclass) { 2082 if (device_set_devclass(child, 2083 dl->driver->name) != 0) { 2084 char const * devname = 2085 device_get_name(child); 2086 if (devname == NULL) 2087 devname = "(unknown)"; 2088 printf("driver bug: Unable to set " 2089 "devclass (class: %s " 2090 "devname: %s)\n", 2091 dl->driver->name, 2092 devname); 2093 (void)device_set_driver(child, NULL); 2094 continue; 2095 } 2096 } 2097 2098 /* Fetch any flags for the device before probing. */ 2099 resource_int_value(dl->driver->name, child->unit, 2100 "flags", &child->devflags); 2101 2102 result = DEVICE_PROBE(child); 2103 2104 /* Reset flags and devclass before the next probe. */ 2105 child->devflags = 0; 2106 if (!hasclass) 2107 (void)device_set_devclass(child, NULL); 2108 2109 /* 2110 * If the driver returns SUCCESS, there can be 2111 * no higher match for this device. 2112 */ 2113 if (result == 0) { 2114 best = dl; 2115 pri = 0; 2116 break; 2117 } 2118 2119 /* 2120 * Reset DF_QUIET in case this driver doesn't 2121 * end up as the best driver. 2122 */ 2123 device_verbose(child); 2124 2125 /* 2126 * Probes that return BUS_PROBE_NOWILDCARD or lower 2127 * only match on devices whose driver was explicitly 2128 * specified. 2129 */ 2130 if (result <= BUS_PROBE_NOWILDCARD && 2131 !(child->flags & DF_FIXEDCLASS)) { 2132 result = ENXIO; 2133 } 2134 2135 /* 2136 * The driver returned an error so it 2137 * certainly doesn't match. 2138 */ 2139 if (result > 0) { 2140 (void)device_set_driver(child, NULL); 2141 continue; 2142 } 2143 2144 /* 2145 * A priority lower than SUCCESS, remember the 2146 * best matching driver. Initialise the value 2147 * of pri for the first match. 2148 */ 2149 if (best == NULL || result > pri) { 2150 best = dl; 2151 pri = result; 2152 continue; 2153 } 2154 } 2155 /* 2156 * If we have an unambiguous match in this devclass, 2157 * don't look in the parent. 2158 */ 2159 if (best && pri == 0) 2160 break; 2161 } 2162 2163 /* 2164 * If we found a driver, change state and initialise the devclass. 2165 */ 2166 /* XXX What happens if we rebid and got no best? */ 2167 if (best) { 2168 /* 2169 * If this device was attached, and we were asked to 2170 * rescan, and it is a different driver, then we have 2171 * to detach the old driver and reattach this new one. 2172 * Note, we don't have to check for DF_REBID here 2173 * because if the state is > DS_ALIVE, we know it must 2174 * be. 2175 * 2176 * This assumes that all DF_REBID drivers can have 2177 * their probe routine called at any time and that 2178 * they are idempotent as well as completely benign in 2179 * normal operations. 2180 * 2181 * We also have to make sure that the detach 2182 * succeeded, otherwise we fail the operation (or 2183 * maybe it should just fail silently? I'm torn). 2184 */ 2185 if (child->state > DS_ALIVE && best->driver != child->driver) 2186 if ((result = device_detach(dev)) != 0) 2187 return (result); 2188 2189 /* Set the winning driver, devclass, and flags. */ 2190 if (!child->devclass) { 2191 result = device_set_devclass(child, best->driver->name); 2192 if (result != 0) 2193 return (result); 2194 } 2195 result = device_set_driver(child, best->driver); 2196 if (result != 0) 2197 return (result); 2198 resource_int_value(best->driver->name, child->unit, 2199 "flags", &child->devflags); 2200 2201 if (pri < 0) { 2202 /* 2203 * A bit bogus. Call the probe method again to make 2204 * sure that we have the right description. 2205 */ 2206 DEVICE_PROBE(child); 2207 #if 0 2208 child->flags |= DF_REBID; 2209 #endif 2210 } else 2211 child->flags &= ~DF_REBID; 2212 child->state = DS_ALIVE; 2213 2214 bus_data_generation_update(); 2215 return (0); 2216 } 2217 2218 return (ENXIO); 2219 } 2220 2221 /** 2222 * @brief Return the parent of a device 2223 */ 2224 device_t 2225 device_get_parent(device_t dev) 2226 { 2227 return (dev->parent); 2228 } 2229 2230 /** 2231 * @brief Get a list of children of a device 2232 * 2233 * An array containing a list of all the children of the given device 2234 * is allocated and returned in @p *devlistp. The number of devices 2235 * in the array is returned in @p *devcountp. The caller should free 2236 * the array using @c free(p, M_TEMP). 2237 * 2238 * @param dev the device to examine 2239 * @param devlistp points at location for array pointer return 2240 * value 2241 * @param devcountp points at location for array size return value 2242 * 2243 * @retval 0 success 2244 * @retval ENOMEM the array allocation failed 2245 */ 2246 int 2247 device_get_children(device_t dev, device_t **devlistp, int *devcountp) 2248 { 2249 int count; 2250 device_t child; 2251 device_t *list; 2252 2253 count = 0; 2254 TAILQ_FOREACH(child, &dev->children, link) { 2255 count++; 2256 } 2257 if (count == 0) { 2258 *devlistp = NULL; 2259 *devcountp = 0; 2260 return (0); 2261 } 2262 2263 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO); 2264 if (!list) 2265 return (ENOMEM); 2266 2267 count = 0; 2268 TAILQ_FOREACH(child, &dev->children, link) { 2269 list[count] = child; 2270 count++; 2271 } 2272 2273 *devlistp = list; 2274 *devcountp = count; 2275 2276 return (0); 2277 } 2278 2279 /** 2280 * @brief Return the current driver for the device or @c NULL if there 2281 * is no driver currently attached 2282 */ 2283 driver_t * 2284 device_get_driver(device_t dev) 2285 { 2286 return (dev->driver); 2287 } 2288 2289 /** 2290 * @brief Return the current devclass for the device or @c NULL if 2291 * there is none. 2292 */ 2293 devclass_t 2294 device_get_devclass(device_t dev) 2295 { 2296 return (dev->devclass); 2297 } 2298 2299 /** 2300 * @brief Return the name of the device's devclass or @c NULL if there 2301 * is none. 2302 */ 2303 const char * 2304 device_get_name(device_t dev) 2305 { 2306 if (dev != NULL && dev->devclass) 2307 return (devclass_get_name(dev->devclass)); 2308 return (NULL); 2309 } 2310 2311 /** 2312 * @brief Return a string containing the device's devclass name 2313 * followed by an ascii representation of the device's unit number 2314 * (e.g. @c "foo2"). 2315 */ 2316 const char * 2317 device_get_nameunit(device_t dev) 2318 { 2319 return (dev->nameunit); 2320 } 2321 2322 /** 2323 * @brief Return the device's unit number. 2324 */ 2325 int 2326 device_get_unit(device_t dev) 2327 { 2328 return (dev->unit); 2329 } 2330 2331 /** 2332 * @brief Return the device's description string 2333 */ 2334 const char * 2335 device_get_desc(device_t dev) 2336 { 2337 return (dev->desc); 2338 } 2339 2340 /** 2341 * @brief Return the device's flags 2342 */ 2343 uint32_t 2344 device_get_flags(device_t dev) 2345 { 2346 return (dev->devflags); 2347 } 2348 2349 struct sysctl_ctx_list * 2350 device_get_sysctl_ctx(device_t dev) 2351 { 2352 return (&dev->sysctl_ctx); 2353 } 2354 2355 struct sysctl_oid * 2356 device_get_sysctl_tree(device_t dev) 2357 { 2358 return (dev->sysctl_tree); 2359 } 2360 2361 /** 2362 * @brief Print the name of the device followed by a colon and a space 2363 * 2364 * @returns the number of characters printed 2365 */ 2366 int 2367 device_print_prettyname(device_t dev) 2368 { 2369 const char *name = device_get_name(dev); 2370 2371 if (name == NULL) 2372 return (printf("unknown: ")); 2373 return (printf("%s%d: ", name, device_get_unit(dev))); 2374 } 2375 2376 /** 2377 * @brief Print the name of the device followed by a colon, a space 2378 * and the result of calling vprintf() with the value of @p fmt and 2379 * the following arguments. 2380 * 2381 * @returns the number of characters printed 2382 */ 2383 int 2384 device_printf(device_t dev, const char * fmt, ...) 2385 { 2386 char buf[128]; 2387 struct sbuf sb; 2388 const char *name; 2389 va_list ap; 2390 size_t retval; 2391 2392 retval = 0; 2393 2394 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN); 2395 sbuf_set_drain(&sb, sbuf_printf_drain, &retval); 2396 2397 name = device_get_name(dev); 2398 2399 if (name == NULL) 2400 sbuf_cat(&sb, "unknown: "); 2401 else 2402 sbuf_printf(&sb, "%s%d: ", name, device_get_unit(dev)); 2403 2404 va_start(ap, fmt); 2405 sbuf_vprintf(&sb, fmt, ap); 2406 va_end(ap); 2407 2408 sbuf_finish(&sb); 2409 sbuf_delete(&sb); 2410 2411 return (retval); 2412 } 2413 2414 /** 2415 * @internal 2416 */ 2417 static void 2418 device_set_desc_internal(device_t dev, const char* desc, int copy) 2419 { 2420 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) { 2421 free(dev->desc, M_BUS); 2422 dev->flags &= ~DF_DESCMALLOCED; 2423 dev->desc = NULL; 2424 } 2425 2426 if (copy && desc) { 2427 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT); 2428 if (dev->desc) { 2429 strcpy(dev->desc, desc); 2430 dev->flags |= DF_DESCMALLOCED; 2431 } 2432 } else { 2433 /* Avoid a -Wcast-qual warning */ 2434 dev->desc = (char *)(uintptr_t) desc; 2435 } 2436 2437 bus_data_generation_update(); 2438 } 2439 2440 /** 2441 * @brief Set the device's description 2442 * 2443 * The value of @c desc should be a string constant that will not 2444 * change (at least until the description is changed in a subsequent 2445 * call to device_set_desc() or device_set_desc_copy()). 2446 */ 2447 void 2448 device_set_desc(device_t dev, const char* desc) 2449 { 2450 device_set_desc_internal(dev, desc, FALSE); 2451 } 2452 2453 /** 2454 * @brief Set the device's description 2455 * 2456 * The string pointed to by @c desc is copied. Use this function if 2457 * the device description is generated, (e.g. with sprintf()). 2458 */ 2459 void 2460 device_set_desc_copy(device_t dev, const char* desc) 2461 { 2462 device_set_desc_internal(dev, desc, TRUE); 2463 } 2464 2465 /** 2466 * @brief Set the device's flags 2467 */ 2468 void 2469 device_set_flags(device_t dev, uint32_t flags) 2470 { 2471 dev->devflags = flags; 2472 } 2473 2474 /** 2475 * @brief Return the device's softc field 2476 * 2477 * The softc is allocated and zeroed when a driver is attached, based 2478 * on the size field of the driver. 2479 */ 2480 void * 2481 device_get_softc(device_t dev) 2482 { 2483 return (dev->softc); 2484 } 2485 2486 /** 2487 * @brief Set the device's softc field 2488 * 2489 * Most drivers do not need to use this since the softc is allocated 2490 * automatically when the driver is attached. 2491 */ 2492 void 2493 device_set_softc(device_t dev, void *softc) 2494 { 2495 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) 2496 free(dev->softc, M_BUS_SC); 2497 dev->softc = softc; 2498 if (dev->softc) 2499 dev->flags |= DF_EXTERNALSOFTC; 2500 else 2501 dev->flags &= ~DF_EXTERNALSOFTC; 2502 } 2503 2504 /** 2505 * @brief Free claimed softc 2506 * 2507 * Most drivers do not need to use this since the softc is freed 2508 * automatically when the driver is detached. 2509 */ 2510 void 2511 device_free_softc(void *softc) 2512 { 2513 free(softc, M_BUS_SC); 2514 } 2515 2516 /** 2517 * @brief Claim softc 2518 * 2519 * This function can be used to let the driver free the automatically 2520 * allocated softc using "device_free_softc()". This function is 2521 * useful when the driver is refcounting the softc and the softc 2522 * cannot be freed when the "device_detach" method is called. 2523 */ 2524 void 2525 device_claim_softc(device_t dev) 2526 { 2527 if (dev->softc) 2528 dev->flags |= DF_EXTERNALSOFTC; 2529 else 2530 dev->flags &= ~DF_EXTERNALSOFTC; 2531 } 2532 2533 /** 2534 * @brief Get the device's ivars field 2535 * 2536 * The ivars field is used by the parent device to store per-device 2537 * state (e.g. the physical location of the device or a list of 2538 * resources). 2539 */ 2540 void * 2541 device_get_ivars(device_t dev) 2542 { 2543 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)")); 2544 return (dev->ivars); 2545 } 2546 2547 /** 2548 * @brief Set the device's ivars field 2549 */ 2550 void 2551 device_set_ivars(device_t dev, void * ivars) 2552 { 2553 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)")); 2554 dev->ivars = ivars; 2555 } 2556 2557 /** 2558 * @brief Return the device's state 2559 */ 2560 device_state_t 2561 device_get_state(device_t dev) 2562 { 2563 return (dev->state); 2564 } 2565 2566 /** 2567 * @brief Set the DF_ENABLED flag for the device 2568 */ 2569 void 2570 device_enable(device_t dev) 2571 { 2572 dev->flags |= DF_ENABLED; 2573 } 2574 2575 /** 2576 * @brief Clear the DF_ENABLED flag for the device 2577 */ 2578 void 2579 device_disable(device_t dev) 2580 { 2581 dev->flags &= ~DF_ENABLED; 2582 } 2583 2584 /** 2585 * @brief Increment the busy counter for the device 2586 */ 2587 void 2588 device_busy(device_t dev) 2589 { 2590 if (dev->state < DS_ATTACHING) 2591 panic("device_busy: called for unattached device"); 2592 if (dev->busy == 0 && dev->parent) 2593 device_busy(dev->parent); 2594 dev->busy++; 2595 if (dev->state == DS_ATTACHED) 2596 dev->state = DS_BUSY; 2597 } 2598 2599 /** 2600 * @brief Decrement the busy counter for the device 2601 */ 2602 void 2603 device_unbusy(device_t dev) 2604 { 2605 if (dev->busy != 0 && dev->state != DS_BUSY && 2606 dev->state != DS_ATTACHING) 2607 panic("device_unbusy: called for non-busy device %s", 2608 device_get_nameunit(dev)); 2609 dev->busy--; 2610 if (dev->busy == 0) { 2611 if (dev->parent) 2612 device_unbusy(dev->parent); 2613 if (dev->state == DS_BUSY) 2614 dev->state = DS_ATTACHED; 2615 } 2616 } 2617 2618 /** 2619 * @brief Set the DF_QUIET flag for the device 2620 */ 2621 void 2622 device_quiet(device_t dev) 2623 { 2624 dev->flags |= DF_QUIET; 2625 } 2626 2627 /** 2628 * @brief Set the DF_QUIET_CHILDREN flag for the device 2629 */ 2630 void 2631 device_quiet_children(device_t dev) 2632 { 2633 dev->flags |= DF_QUIET_CHILDREN; 2634 } 2635 2636 /** 2637 * @brief Clear the DF_QUIET flag for the device 2638 */ 2639 void 2640 device_verbose(device_t dev) 2641 { 2642 dev->flags &= ~DF_QUIET; 2643 } 2644 2645 /** 2646 * @brief Return non-zero if the DF_QUIET_CHIDLREN flag is set on the device 2647 */ 2648 int 2649 device_has_quiet_children(device_t dev) 2650 { 2651 return ((dev->flags & DF_QUIET_CHILDREN) != 0); 2652 } 2653 2654 /** 2655 * @brief Return non-zero if the DF_QUIET flag is set on the device 2656 */ 2657 int 2658 device_is_quiet(device_t dev) 2659 { 2660 return ((dev->flags & DF_QUIET) != 0); 2661 } 2662 2663 /** 2664 * @brief Return non-zero if the DF_ENABLED flag is set on the device 2665 */ 2666 int 2667 device_is_enabled(device_t dev) 2668 { 2669 return ((dev->flags & DF_ENABLED) != 0); 2670 } 2671 2672 /** 2673 * @brief Return non-zero if the device was successfully probed 2674 */ 2675 int 2676 device_is_alive(device_t dev) 2677 { 2678 return (dev->state >= DS_ALIVE); 2679 } 2680 2681 /** 2682 * @brief Return non-zero if the device currently has a driver 2683 * attached to it 2684 */ 2685 int 2686 device_is_attached(device_t dev) 2687 { 2688 return (dev->state >= DS_ATTACHED); 2689 } 2690 2691 /** 2692 * @brief Return non-zero if the device is currently suspended. 2693 */ 2694 int 2695 device_is_suspended(device_t dev) 2696 { 2697 return ((dev->flags & DF_SUSPENDED) != 0); 2698 } 2699 2700 /** 2701 * @brief Set the devclass of a device 2702 * @see devclass_add_device(). 2703 */ 2704 int 2705 device_set_devclass(device_t dev, const char *classname) 2706 { 2707 devclass_t dc; 2708 int error; 2709 2710 if (!classname) { 2711 if (dev->devclass) 2712 devclass_delete_device(dev->devclass, dev); 2713 return (0); 2714 } 2715 2716 if (dev->devclass) { 2717 printf("device_set_devclass: device class already set\n"); 2718 return (EINVAL); 2719 } 2720 2721 dc = devclass_find_internal(classname, NULL, TRUE); 2722 if (!dc) 2723 return (ENOMEM); 2724 2725 error = devclass_add_device(dc, dev); 2726 2727 bus_data_generation_update(); 2728 return (error); 2729 } 2730 2731 /** 2732 * @brief Set the devclass of a device and mark the devclass fixed. 2733 * @see device_set_devclass() 2734 */ 2735 int 2736 device_set_devclass_fixed(device_t dev, const char *classname) 2737 { 2738 int error; 2739 2740 if (classname == NULL) 2741 return (EINVAL); 2742 2743 error = device_set_devclass(dev, classname); 2744 if (error) 2745 return (error); 2746 dev->flags |= DF_FIXEDCLASS; 2747 return (0); 2748 } 2749 2750 /** 2751 * @brief Query the device to determine if it's of a fixed devclass 2752 * @see device_set_devclass_fixed() 2753 */ 2754 bool 2755 device_is_devclass_fixed(device_t dev) 2756 { 2757 return ((dev->flags & DF_FIXEDCLASS) != 0); 2758 } 2759 2760 /** 2761 * @brief Set the driver of a device 2762 * 2763 * @retval 0 success 2764 * @retval EBUSY the device already has a driver attached 2765 * @retval ENOMEM a memory allocation failure occurred 2766 */ 2767 int 2768 device_set_driver(device_t dev, driver_t *driver) 2769 { 2770 int domain; 2771 struct domainset *policy; 2772 2773 if (dev->state >= DS_ATTACHED) 2774 return (EBUSY); 2775 2776 if (dev->driver == driver) 2777 return (0); 2778 2779 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) { 2780 free(dev->softc, M_BUS_SC); 2781 dev->softc = NULL; 2782 } 2783 device_set_desc(dev, NULL); 2784 kobj_delete((kobj_t) dev, NULL); 2785 dev->driver = driver; 2786 if (driver) { 2787 kobj_init((kobj_t) dev, (kobj_class_t) driver); 2788 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) { 2789 if (bus_get_domain(dev, &domain) == 0) 2790 policy = DOMAINSET_PREF(domain); 2791 else 2792 policy = DOMAINSET_RR(); 2793 dev->softc = malloc_domainset(driver->size, M_BUS_SC, 2794 policy, M_NOWAIT | M_ZERO); 2795 if (!dev->softc) { 2796 kobj_delete((kobj_t) dev, NULL); 2797 kobj_init((kobj_t) dev, &null_class); 2798 dev->driver = NULL; 2799 return (ENOMEM); 2800 } 2801 } 2802 } else { 2803 kobj_init((kobj_t) dev, &null_class); 2804 } 2805 2806 bus_data_generation_update(); 2807 return (0); 2808 } 2809 2810 /** 2811 * @brief Probe a device, and return this status. 2812 * 2813 * This function is the core of the device autoconfiguration 2814 * system. Its purpose is to select a suitable driver for a device and 2815 * then call that driver to initialise the hardware appropriately. The 2816 * driver is selected by calling the DEVICE_PROBE() method of a set of 2817 * candidate drivers and then choosing the driver which returned the 2818 * best value. This driver is then attached to the device using 2819 * device_attach(). 2820 * 2821 * The set of suitable drivers is taken from the list of drivers in 2822 * the parent device's devclass. If the device was originally created 2823 * with a specific class name (see device_add_child()), only drivers 2824 * with that name are probed, otherwise all drivers in the devclass 2825 * are probed. If no drivers return successful probe values in the 2826 * parent devclass, the search continues in the parent of that 2827 * devclass (see devclass_get_parent()) if any. 2828 * 2829 * @param dev the device to initialise 2830 * 2831 * @retval 0 success 2832 * @retval ENXIO no driver was found 2833 * @retval ENOMEM memory allocation failure 2834 * @retval non-zero some other unix error code 2835 * @retval -1 Device already attached 2836 */ 2837 int 2838 device_probe(device_t dev) 2839 { 2840 int error; 2841 2842 GIANT_REQUIRED; 2843 2844 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0) 2845 return (-1); 2846 2847 if (!(dev->flags & DF_ENABLED)) { 2848 if (bootverbose && device_get_name(dev) != NULL) { 2849 device_print_prettyname(dev); 2850 printf("not probed (disabled)\n"); 2851 } 2852 return (-1); 2853 } 2854 if ((error = device_probe_child(dev->parent, dev)) != 0) { 2855 if (bus_current_pass == BUS_PASS_DEFAULT && 2856 !(dev->flags & DF_DONENOMATCH)) { 2857 BUS_PROBE_NOMATCH(dev->parent, dev); 2858 devnomatch(dev); 2859 dev->flags |= DF_DONENOMATCH; 2860 } 2861 return (error); 2862 } 2863 return (0); 2864 } 2865 2866 /** 2867 * @brief Probe a device and attach a driver if possible 2868 * 2869 * calls device_probe() and attaches if that was successful. 2870 */ 2871 int 2872 device_probe_and_attach(device_t dev) 2873 { 2874 int error; 2875 2876 GIANT_REQUIRED; 2877 2878 error = device_probe(dev); 2879 if (error == -1) 2880 return (0); 2881 else if (error != 0) 2882 return (error); 2883 2884 CURVNET_SET_QUIET(vnet0); 2885 error = device_attach(dev); 2886 CURVNET_RESTORE(); 2887 return error; 2888 } 2889 2890 /** 2891 * @brief Attach a device driver to a device 2892 * 2893 * This function is a wrapper around the DEVICE_ATTACH() driver 2894 * method. In addition to calling DEVICE_ATTACH(), it initialises the 2895 * device's sysctl tree, optionally prints a description of the device 2896 * and queues a notification event for user-based device management 2897 * services. 2898 * 2899 * Normally this function is only called internally from 2900 * device_probe_and_attach(). 2901 * 2902 * @param dev the device to initialise 2903 * 2904 * @retval 0 success 2905 * @retval ENXIO no driver was found 2906 * @retval ENOMEM memory allocation failure 2907 * @retval non-zero some other unix error code 2908 */ 2909 int 2910 device_attach(device_t dev) 2911 { 2912 uint64_t attachtime; 2913 uint16_t attachentropy; 2914 int error; 2915 2916 if (resource_disabled(dev->driver->name, dev->unit)) { 2917 device_disable(dev); 2918 if (bootverbose) 2919 device_printf(dev, "disabled via hints entry\n"); 2920 return (ENXIO); 2921 } 2922 2923 device_sysctl_init(dev); 2924 if (!device_is_quiet(dev)) 2925 device_print_child(dev->parent, dev); 2926 attachtime = get_cyclecount(); 2927 dev->state = DS_ATTACHING; 2928 if ((error = DEVICE_ATTACH(dev)) != 0) { 2929 printf("device_attach: %s%d attach returned %d\n", 2930 dev->driver->name, dev->unit, error); 2931 if (!(dev->flags & DF_FIXEDCLASS)) 2932 devclass_delete_device(dev->devclass, dev); 2933 (void)device_set_driver(dev, NULL); 2934 device_sysctl_fini(dev); 2935 KASSERT(dev->busy == 0, ("attach failed but busy")); 2936 dev->state = DS_NOTPRESENT; 2937 return (error); 2938 } 2939 dev->flags |= DF_ATTACHED_ONCE; 2940 /* We only need the low bits of this time, but ranges from tens to thousands 2941 * have been seen, so keep 2 bytes' worth. 2942 */ 2943 attachentropy = (uint16_t)(get_cyclecount() - attachtime); 2944 random_harvest_direct(&attachentropy, sizeof(attachentropy), RANDOM_ATTACH); 2945 device_sysctl_update(dev); 2946 if (dev->busy) 2947 dev->state = DS_BUSY; 2948 else 2949 dev->state = DS_ATTACHED; 2950 dev->flags &= ~DF_DONENOMATCH; 2951 EVENTHANDLER_DIRECT_INVOKE(device_attach, dev); 2952 devadded(dev); 2953 return (0); 2954 } 2955 2956 /** 2957 * @brief Detach a driver from a device 2958 * 2959 * This function is a wrapper around the DEVICE_DETACH() driver 2960 * method. If the call to DEVICE_DETACH() succeeds, it calls 2961 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a 2962 * notification event for user-based device management services and 2963 * cleans up the device's sysctl tree. 2964 * 2965 * @param dev the device to un-initialise 2966 * 2967 * @retval 0 success 2968 * @retval ENXIO no driver was found 2969 * @retval ENOMEM memory allocation failure 2970 * @retval non-zero some other unix error code 2971 */ 2972 int 2973 device_detach(device_t dev) 2974 { 2975 int error; 2976 2977 GIANT_REQUIRED; 2978 2979 PDEBUG(("%s", DEVICENAME(dev))); 2980 if (dev->state == DS_BUSY) 2981 return (EBUSY); 2982 if (dev->state == DS_ATTACHING) { 2983 device_printf(dev, "device in attaching state! Deferring detach.\n"); 2984 return (EBUSY); 2985 } 2986 if (dev->state != DS_ATTACHED) 2987 return (0); 2988 2989 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev, EVHDEV_DETACH_BEGIN); 2990 if ((error = DEVICE_DETACH(dev)) != 0) { 2991 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev, 2992 EVHDEV_DETACH_FAILED); 2993 return (error); 2994 } else { 2995 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev, 2996 EVHDEV_DETACH_COMPLETE); 2997 } 2998 devremoved(dev); 2999 if (!device_is_quiet(dev)) 3000 device_printf(dev, "detached\n"); 3001 if (dev->parent) 3002 BUS_CHILD_DETACHED(dev->parent, dev); 3003 3004 if (!(dev->flags & DF_FIXEDCLASS)) 3005 devclass_delete_device(dev->devclass, dev); 3006 3007 device_verbose(dev); 3008 dev->state = DS_NOTPRESENT; 3009 (void)device_set_driver(dev, NULL); 3010 device_sysctl_fini(dev); 3011 3012 return (0); 3013 } 3014 3015 /** 3016 * @brief Tells a driver to quiesce itself. 3017 * 3018 * This function is a wrapper around the DEVICE_QUIESCE() driver 3019 * method. If the call to DEVICE_QUIESCE() succeeds. 3020 * 3021 * @param dev the device to quiesce 3022 * 3023 * @retval 0 success 3024 * @retval ENXIO no driver was found 3025 * @retval ENOMEM memory allocation failure 3026 * @retval non-zero some other unix error code 3027 */ 3028 int 3029 device_quiesce(device_t dev) 3030 { 3031 PDEBUG(("%s", DEVICENAME(dev))); 3032 if (dev->state == DS_BUSY) 3033 return (EBUSY); 3034 if (dev->state != DS_ATTACHED) 3035 return (0); 3036 3037 return (DEVICE_QUIESCE(dev)); 3038 } 3039 3040 /** 3041 * @brief Notify a device of system shutdown 3042 * 3043 * This function calls the DEVICE_SHUTDOWN() driver method if the 3044 * device currently has an attached driver. 3045 * 3046 * @returns the value returned by DEVICE_SHUTDOWN() 3047 */ 3048 int 3049 device_shutdown(device_t dev) 3050 { 3051 if (dev->state < DS_ATTACHED) 3052 return (0); 3053 return (DEVICE_SHUTDOWN(dev)); 3054 } 3055 3056 /** 3057 * @brief Set the unit number of a device 3058 * 3059 * This function can be used to override the unit number used for a 3060 * device (e.g. to wire a device to a pre-configured unit number). 3061 */ 3062 int 3063 device_set_unit(device_t dev, int unit) 3064 { 3065 devclass_t dc; 3066 int err; 3067 3068 dc = device_get_devclass(dev); 3069 if (unit < dc->maxunit && dc->devices[unit]) 3070 return (EBUSY); 3071 err = devclass_delete_device(dc, dev); 3072 if (err) 3073 return (err); 3074 dev->unit = unit; 3075 err = devclass_add_device(dc, dev); 3076 if (err) 3077 return (err); 3078 3079 bus_data_generation_update(); 3080 return (0); 3081 } 3082 3083 /*======================================*/ 3084 /* 3085 * Some useful method implementations to make life easier for bus drivers. 3086 */ 3087 3088 void 3089 resource_init_map_request_impl(struct resource_map_request *args, size_t sz) 3090 { 3091 bzero(args, sz); 3092 args->size = sz; 3093 args->memattr = VM_MEMATTR_UNCACHEABLE; 3094 } 3095 3096 /** 3097 * @brief Initialise a resource list. 3098 * 3099 * @param rl the resource list to initialise 3100 */ 3101 void 3102 resource_list_init(struct resource_list *rl) 3103 { 3104 STAILQ_INIT(rl); 3105 } 3106 3107 /** 3108 * @brief Reclaim memory used by a resource list. 3109 * 3110 * This function frees the memory for all resource entries on the list 3111 * (if any). 3112 * 3113 * @param rl the resource list to free 3114 */ 3115 void 3116 resource_list_free(struct resource_list *rl) 3117 { 3118 struct resource_list_entry *rle; 3119 3120 while ((rle = STAILQ_FIRST(rl)) != NULL) { 3121 if (rle->res) 3122 panic("resource_list_free: resource entry is busy"); 3123 STAILQ_REMOVE_HEAD(rl, link); 3124 free(rle, M_BUS); 3125 } 3126 } 3127 3128 /** 3129 * @brief Add a resource entry. 3130 * 3131 * This function adds a resource entry using the given @p type, @p 3132 * start, @p end and @p count values. A rid value is chosen by 3133 * searching sequentially for the first unused rid starting at zero. 3134 * 3135 * @param rl the resource list to edit 3136 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 3137 * @param start the start address of the resource 3138 * @param end the end address of the resource 3139 * @param count XXX end-start+1 3140 */ 3141 int 3142 resource_list_add_next(struct resource_list *rl, int type, rman_res_t start, 3143 rman_res_t end, rman_res_t count) 3144 { 3145 int rid; 3146 3147 rid = 0; 3148 while (resource_list_find(rl, type, rid) != NULL) 3149 rid++; 3150 resource_list_add(rl, type, rid, start, end, count); 3151 return (rid); 3152 } 3153 3154 /** 3155 * @brief Add or modify a resource entry. 3156 * 3157 * If an existing entry exists with the same type and rid, it will be 3158 * modified using the given values of @p start, @p end and @p 3159 * count. If no entry exists, a new one will be created using the 3160 * given values. The resource list entry that matches is then returned. 3161 * 3162 * @param rl the resource list to edit 3163 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 3164 * @param rid the resource identifier 3165 * @param start the start address of the resource 3166 * @param end the end address of the resource 3167 * @param count XXX end-start+1 3168 */ 3169 struct resource_list_entry * 3170 resource_list_add(struct resource_list *rl, int type, int rid, 3171 rman_res_t start, rman_res_t end, rman_res_t count) 3172 { 3173 struct resource_list_entry *rle; 3174 3175 rle = resource_list_find(rl, type, rid); 3176 if (!rle) { 3177 rle = malloc(sizeof(struct resource_list_entry), M_BUS, 3178 M_NOWAIT); 3179 if (!rle) 3180 panic("resource_list_add: can't record entry"); 3181 STAILQ_INSERT_TAIL(rl, rle, link); 3182 rle->type = type; 3183 rle->rid = rid; 3184 rle->res = NULL; 3185 rle->flags = 0; 3186 } 3187 3188 if (rle->res) 3189 panic("resource_list_add: resource entry is busy"); 3190 3191 rle->start = start; 3192 rle->end = end; 3193 rle->count = count; 3194 return (rle); 3195 } 3196 3197 /** 3198 * @brief Determine if a resource entry is busy. 3199 * 3200 * Returns true if a resource entry is busy meaning that it has an 3201 * associated resource that is not an unallocated "reserved" resource. 3202 * 3203 * @param rl the resource list to search 3204 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 3205 * @param rid the resource identifier 3206 * 3207 * @returns Non-zero if the entry is busy, zero otherwise. 3208 */ 3209 int 3210 resource_list_busy(struct resource_list *rl, int type, int rid) 3211 { 3212 struct resource_list_entry *rle; 3213 3214 rle = resource_list_find(rl, type, rid); 3215 if (rle == NULL || rle->res == NULL) 3216 return (0); 3217 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == RLE_RESERVED) { 3218 KASSERT(!(rman_get_flags(rle->res) & RF_ACTIVE), 3219 ("reserved resource is active")); 3220 return (0); 3221 } 3222 return (1); 3223 } 3224 3225 /** 3226 * @brief Determine if a resource entry is reserved. 3227 * 3228 * Returns true if a resource entry is reserved meaning that it has an 3229 * associated "reserved" resource. The resource can either be 3230 * allocated or unallocated. 3231 * 3232 * @param rl the resource list to search 3233 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 3234 * @param rid the resource identifier 3235 * 3236 * @returns Non-zero if the entry is reserved, zero otherwise. 3237 */ 3238 int 3239 resource_list_reserved(struct resource_list *rl, int type, int rid) 3240 { 3241 struct resource_list_entry *rle; 3242 3243 rle = resource_list_find(rl, type, rid); 3244 if (rle != NULL && rle->flags & RLE_RESERVED) 3245 return (1); 3246 return (0); 3247 } 3248 3249 /** 3250 * @brief Find a resource entry by type and rid. 3251 * 3252 * @param rl the resource list to search 3253 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 3254 * @param rid the resource identifier 3255 * 3256 * @returns the resource entry pointer or NULL if there is no such 3257 * entry. 3258 */ 3259 struct resource_list_entry * 3260 resource_list_find(struct resource_list *rl, int type, int rid) 3261 { 3262 struct resource_list_entry *rle; 3263 3264 STAILQ_FOREACH(rle, rl, link) { 3265 if (rle->type == type && rle->rid == rid) 3266 return (rle); 3267 } 3268 return (NULL); 3269 } 3270 3271 /** 3272 * @brief Delete a resource entry. 3273 * 3274 * @param rl the resource list to edit 3275 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 3276 * @param rid the resource identifier 3277 */ 3278 void 3279 resource_list_delete(struct resource_list *rl, int type, int rid) 3280 { 3281 struct resource_list_entry *rle = resource_list_find(rl, type, rid); 3282 3283 if (rle) { 3284 if (rle->res != NULL) 3285 panic("resource_list_delete: resource has not been released"); 3286 STAILQ_REMOVE(rl, rle, resource_list_entry, link); 3287 free(rle, M_BUS); 3288 } 3289 } 3290 3291 /** 3292 * @brief Allocate a reserved resource 3293 * 3294 * This can be used by buses to force the allocation of resources 3295 * that are always active in the system even if they are not allocated 3296 * by a driver (e.g. PCI BARs). This function is usually called when 3297 * adding a new child to the bus. The resource is allocated from the 3298 * parent bus when it is reserved. The resource list entry is marked 3299 * with RLE_RESERVED to note that it is a reserved resource. 3300 * 3301 * Subsequent attempts to allocate the resource with 3302 * resource_list_alloc() will succeed the first time and will set 3303 * RLE_ALLOCATED to note that it has been allocated. When a reserved 3304 * resource that has been allocated is released with 3305 * resource_list_release() the resource RLE_ALLOCATED is cleared, but 3306 * the actual resource remains allocated. The resource can be released to 3307 * the parent bus by calling resource_list_unreserve(). 3308 * 3309 * @param rl the resource list to allocate from 3310 * @param bus the parent device of @p child 3311 * @param child the device for which the resource is being reserved 3312 * @param type the type of resource to allocate 3313 * @param rid a pointer to the resource identifier 3314 * @param start hint at the start of the resource range - pass 3315 * @c 0 for any start address 3316 * @param end hint at the end of the resource range - pass 3317 * @c ~0 for any end address 3318 * @param count hint at the size of range required - pass @c 1 3319 * for any size 3320 * @param flags any extra flags to control the resource 3321 * allocation - see @c RF_XXX flags in 3322 * <sys/rman.h> for details 3323 * 3324 * @returns the resource which was allocated or @c NULL if no 3325 * resource could be allocated 3326 */ 3327 struct resource * 3328 resource_list_reserve(struct resource_list *rl, device_t bus, device_t child, 3329 int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 3330 { 3331 struct resource_list_entry *rle = NULL; 3332 int passthrough = (device_get_parent(child) != bus); 3333 struct resource *r; 3334 3335 if (passthrough) 3336 panic( 3337 "resource_list_reserve() should only be called for direct children"); 3338 if (flags & RF_ACTIVE) 3339 panic( 3340 "resource_list_reserve() should only reserve inactive resources"); 3341 3342 r = resource_list_alloc(rl, bus, child, type, rid, start, end, count, 3343 flags); 3344 if (r != NULL) { 3345 rle = resource_list_find(rl, type, *rid); 3346 rle->flags |= RLE_RESERVED; 3347 } 3348 return (r); 3349 } 3350 3351 /** 3352 * @brief Helper function for implementing BUS_ALLOC_RESOURCE() 3353 * 3354 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list 3355 * and passing the allocation up to the parent of @p bus. This assumes 3356 * that the first entry of @c device_get_ivars(child) is a struct 3357 * resource_list. This also handles 'passthrough' allocations where a 3358 * child is a remote descendant of bus by passing the allocation up to 3359 * the parent of bus. 3360 * 3361 * Typically, a bus driver would store a list of child resources 3362 * somewhere in the child device's ivars (see device_get_ivars()) and 3363 * its implementation of BUS_ALLOC_RESOURCE() would find that list and 3364 * then call resource_list_alloc() to perform the allocation. 3365 * 3366 * @param rl the resource list to allocate from 3367 * @param bus the parent device of @p child 3368 * @param child the device which is requesting an allocation 3369 * @param type the type of resource to allocate 3370 * @param rid a pointer to the resource identifier 3371 * @param start hint at the start of the resource range - pass 3372 * @c 0 for any start address 3373 * @param end hint at the end of the resource range - pass 3374 * @c ~0 for any end address 3375 * @param count hint at the size of range required - pass @c 1 3376 * for any size 3377 * @param flags any extra flags to control the resource 3378 * allocation - see @c RF_XXX flags in 3379 * <sys/rman.h> for details 3380 * 3381 * @returns the resource which was allocated or @c NULL if no 3382 * resource could be allocated 3383 */ 3384 struct resource * 3385 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child, 3386 int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 3387 { 3388 struct resource_list_entry *rle = NULL; 3389 int passthrough = (device_get_parent(child) != bus); 3390 int isdefault = RMAN_IS_DEFAULT_RANGE(start, end); 3391 3392 if (passthrough) { 3393 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 3394 type, rid, start, end, count, flags)); 3395 } 3396 3397 rle = resource_list_find(rl, type, *rid); 3398 3399 if (!rle) 3400 return (NULL); /* no resource of that type/rid */ 3401 3402 if (rle->res) { 3403 if (rle->flags & RLE_RESERVED) { 3404 if (rle->flags & RLE_ALLOCATED) 3405 return (NULL); 3406 if ((flags & RF_ACTIVE) && 3407 bus_activate_resource(child, type, *rid, 3408 rle->res) != 0) 3409 return (NULL); 3410 rle->flags |= RLE_ALLOCATED; 3411 return (rle->res); 3412 } 3413 device_printf(bus, 3414 "resource entry %#x type %d for child %s is busy\n", *rid, 3415 type, device_get_nameunit(child)); 3416 return (NULL); 3417 } 3418 3419 if (isdefault) { 3420 start = rle->start; 3421 count = ulmax(count, rle->count); 3422 end = ulmax(rle->end, start + count - 1); 3423 } 3424 3425 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 3426 type, rid, start, end, count, flags); 3427 3428 /* 3429 * Record the new range. 3430 */ 3431 if (rle->res) { 3432 rle->start = rman_get_start(rle->res); 3433 rle->end = rman_get_end(rle->res); 3434 rle->count = count; 3435 } 3436 3437 return (rle->res); 3438 } 3439 3440 /** 3441 * @brief Helper function for implementing BUS_RELEASE_RESOURCE() 3442 * 3443 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally 3444 * used with resource_list_alloc(). 3445 * 3446 * @param rl the resource list which was allocated from 3447 * @param bus the parent device of @p child 3448 * @param child the device which is requesting a release 3449 * @param type the type of resource to release 3450 * @param rid the resource identifier 3451 * @param res the resource to release 3452 * 3453 * @retval 0 success 3454 * @retval non-zero a standard unix error code indicating what 3455 * error condition prevented the operation 3456 */ 3457 int 3458 resource_list_release(struct resource_list *rl, device_t bus, device_t child, 3459 int type, int rid, struct resource *res) 3460 { 3461 struct resource_list_entry *rle = NULL; 3462 int passthrough = (device_get_parent(child) != bus); 3463 int error; 3464 3465 if (passthrough) { 3466 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 3467 type, rid, res)); 3468 } 3469 3470 rle = resource_list_find(rl, type, rid); 3471 3472 if (!rle) 3473 panic("resource_list_release: can't find resource"); 3474 if (!rle->res) 3475 panic("resource_list_release: resource entry is not busy"); 3476 if (rle->flags & RLE_RESERVED) { 3477 if (rle->flags & RLE_ALLOCATED) { 3478 if (rman_get_flags(res) & RF_ACTIVE) { 3479 error = bus_deactivate_resource(child, type, 3480 rid, res); 3481 if (error) 3482 return (error); 3483 } 3484 rle->flags &= ~RLE_ALLOCATED; 3485 return (0); 3486 } 3487 return (EINVAL); 3488 } 3489 3490 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 3491 type, rid, res); 3492 if (error) 3493 return (error); 3494 3495 rle->res = NULL; 3496 return (0); 3497 } 3498 3499 /** 3500 * @brief Release all active resources of a given type 3501 * 3502 * Release all active resources of a specified type. This is intended 3503 * to be used to cleanup resources leaked by a driver after detach or 3504 * a failed attach. 3505 * 3506 * @param rl the resource list which was allocated from 3507 * @param bus the parent device of @p child 3508 * @param child the device whose active resources are being released 3509 * @param type the type of resources to release 3510 * 3511 * @retval 0 success 3512 * @retval EBUSY at least one resource was active 3513 */ 3514 int 3515 resource_list_release_active(struct resource_list *rl, device_t bus, 3516 device_t child, int type) 3517 { 3518 struct resource_list_entry *rle; 3519 int error, retval; 3520 3521 retval = 0; 3522 STAILQ_FOREACH(rle, rl, link) { 3523 if (rle->type != type) 3524 continue; 3525 if (rle->res == NULL) 3526 continue; 3527 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == 3528 RLE_RESERVED) 3529 continue; 3530 retval = EBUSY; 3531 error = resource_list_release(rl, bus, child, type, 3532 rman_get_rid(rle->res), rle->res); 3533 if (error != 0) 3534 device_printf(bus, 3535 "Failed to release active resource: %d\n", error); 3536 } 3537 return (retval); 3538 } 3539 3540 /** 3541 * @brief Fully release a reserved resource 3542 * 3543 * Fully releases a resource reserved via resource_list_reserve(). 3544 * 3545 * @param rl the resource list which was allocated from 3546 * @param bus the parent device of @p child 3547 * @param child the device whose reserved resource is being released 3548 * @param type the type of resource to release 3549 * @param rid the resource identifier 3550 * @param res the resource to release 3551 * 3552 * @retval 0 success 3553 * @retval non-zero a standard unix error code indicating what 3554 * error condition prevented the operation 3555 */ 3556 int 3557 resource_list_unreserve(struct resource_list *rl, device_t bus, device_t child, 3558 int type, int rid) 3559 { 3560 struct resource_list_entry *rle = NULL; 3561 int passthrough = (device_get_parent(child) != bus); 3562 3563 if (passthrough) 3564 panic( 3565 "resource_list_unreserve() should only be called for direct children"); 3566 3567 rle = resource_list_find(rl, type, rid); 3568 3569 if (!rle) 3570 panic("resource_list_unreserve: can't find resource"); 3571 if (!(rle->flags & RLE_RESERVED)) 3572 return (EINVAL); 3573 if (rle->flags & RLE_ALLOCATED) 3574 return (EBUSY); 3575 rle->flags &= ~RLE_RESERVED; 3576 return (resource_list_release(rl, bus, child, type, rid, rle->res)); 3577 } 3578 3579 /** 3580 * @brief Print a description of resources in a resource list 3581 * 3582 * Print all resources of a specified type, for use in BUS_PRINT_CHILD(). 3583 * The name is printed if at least one resource of the given type is available. 3584 * The format is used to print resource start and end. 3585 * 3586 * @param rl the resource list to print 3587 * @param name the name of @p type, e.g. @c "memory" 3588 * @param type type type of resource entry to print 3589 * @param format printf(9) format string to print resource 3590 * start and end values 3591 * 3592 * @returns the number of characters printed 3593 */ 3594 int 3595 resource_list_print_type(struct resource_list *rl, const char *name, int type, 3596 const char *format) 3597 { 3598 struct resource_list_entry *rle; 3599 int printed, retval; 3600 3601 printed = 0; 3602 retval = 0; 3603 /* Yes, this is kinda cheating */ 3604 STAILQ_FOREACH(rle, rl, link) { 3605 if (rle->type == type) { 3606 if (printed == 0) 3607 retval += printf(" %s ", name); 3608 else 3609 retval += printf(","); 3610 printed++; 3611 retval += printf(format, rle->start); 3612 if (rle->count > 1) { 3613 retval += printf("-"); 3614 retval += printf(format, rle->start + 3615 rle->count - 1); 3616 } 3617 } 3618 } 3619 return (retval); 3620 } 3621 3622 /** 3623 * @brief Releases all the resources in a list. 3624 * 3625 * @param rl The resource list to purge. 3626 * 3627 * @returns nothing 3628 */ 3629 void 3630 resource_list_purge(struct resource_list *rl) 3631 { 3632 struct resource_list_entry *rle; 3633 3634 while ((rle = STAILQ_FIRST(rl)) != NULL) { 3635 if (rle->res) 3636 bus_release_resource(rman_get_device(rle->res), 3637 rle->type, rle->rid, rle->res); 3638 STAILQ_REMOVE_HEAD(rl, link); 3639 free(rle, M_BUS); 3640 } 3641 } 3642 3643 device_t 3644 bus_generic_add_child(device_t dev, u_int order, const char *name, int unit) 3645 { 3646 return (device_add_child_ordered(dev, order, name, unit)); 3647 } 3648 3649 /** 3650 * @brief Helper function for implementing DEVICE_PROBE() 3651 * 3652 * This function can be used to help implement the DEVICE_PROBE() for 3653 * a bus (i.e. a device which has other devices attached to it). It 3654 * calls the DEVICE_IDENTIFY() method of each driver in the device's 3655 * devclass. 3656 */ 3657 int 3658 bus_generic_probe(device_t dev) 3659 { 3660 devclass_t dc = dev->devclass; 3661 driverlink_t dl; 3662 3663 TAILQ_FOREACH(dl, &dc->drivers, link) { 3664 /* 3665 * If this driver's pass is too high, then ignore it. 3666 * For most drivers in the default pass, this will 3667 * never be true. For early-pass drivers they will 3668 * only call the identify routines of eligible drivers 3669 * when this routine is called. Drivers for later 3670 * passes should have their identify routines called 3671 * on early-pass buses during BUS_NEW_PASS(). 3672 */ 3673 if (dl->pass > bus_current_pass) 3674 continue; 3675 DEVICE_IDENTIFY(dl->driver, dev); 3676 } 3677 3678 return (0); 3679 } 3680 3681 /** 3682 * @brief Helper function for implementing DEVICE_ATTACH() 3683 * 3684 * This function can be used to help implement the DEVICE_ATTACH() for 3685 * a bus. It calls device_probe_and_attach() for each of the device's 3686 * children. 3687 */ 3688 int 3689 bus_generic_attach(device_t dev) 3690 { 3691 device_t child; 3692 3693 TAILQ_FOREACH(child, &dev->children, link) { 3694 device_probe_and_attach(child); 3695 } 3696 3697 return (0); 3698 } 3699 3700 /** 3701 * @brief Helper function for delaying attaching children 3702 * 3703 * Many buses can't run transactions on the bus which children need to probe and 3704 * attach until after interrupts and/or timers are running. This function 3705 * delays their attach until interrupts and timers are enabled. 3706 */ 3707 int 3708 bus_delayed_attach_children(device_t dev) 3709 { 3710 /* Probe and attach the bus children when interrupts are available */ 3711 config_intrhook_oneshot((ich_func_t)bus_generic_attach, dev); 3712 3713 return (0); 3714 } 3715 3716 /** 3717 * @brief Helper function for implementing DEVICE_DETACH() 3718 * 3719 * This function can be used to help implement the DEVICE_DETACH() for 3720 * a bus. It calls device_detach() for each of the device's 3721 * children. 3722 */ 3723 int 3724 bus_generic_detach(device_t dev) 3725 { 3726 device_t child; 3727 int error; 3728 3729 if (dev->state != DS_ATTACHED) 3730 return (EBUSY); 3731 3732 /* 3733 * Detach children in the reverse order. 3734 * See bus_generic_suspend for details. 3735 */ 3736 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) { 3737 if ((error = device_detach(child)) != 0) 3738 return (error); 3739 } 3740 3741 return (0); 3742 } 3743 3744 /** 3745 * @brief Helper function for implementing DEVICE_SHUTDOWN() 3746 * 3747 * This function can be used to help implement the DEVICE_SHUTDOWN() 3748 * for a bus. It calls device_shutdown() for each of the device's 3749 * children. 3750 */ 3751 int 3752 bus_generic_shutdown(device_t dev) 3753 { 3754 device_t child; 3755 3756 /* 3757 * Shut down children in the reverse order. 3758 * See bus_generic_suspend for details. 3759 */ 3760 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) { 3761 device_shutdown(child); 3762 } 3763 3764 return (0); 3765 } 3766 3767 /** 3768 * @brief Default function for suspending a child device. 3769 * 3770 * This function is to be used by a bus's DEVICE_SUSPEND_CHILD(). 3771 */ 3772 int 3773 bus_generic_suspend_child(device_t dev, device_t child) 3774 { 3775 int error; 3776 3777 error = DEVICE_SUSPEND(child); 3778 3779 if (error == 0) 3780 child->flags |= DF_SUSPENDED; 3781 3782 return (error); 3783 } 3784 3785 /** 3786 * @brief Default function for resuming a child device. 3787 * 3788 * This function is to be used by a bus's DEVICE_RESUME_CHILD(). 3789 */ 3790 int 3791 bus_generic_resume_child(device_t dev, device_t child) 3792 { 3793 DEVICE_RESUME(child); 3794 child->flags &= ~DF_SUSPENDED; 3795 3796 return (0); 3797 } 3798 3799 /** 3800 * @brief Helper function for implementing DEVICE_SUSPEND() 3801 * 3802 * This function can be used to help implement the DEVICE_SUSPEND() 3803 * for a bus. It calls DEVICE_SUSPEND() for each of the device's 3804 * children. If any call to DEVICE_SUSPEND() fails, the suspend 3805 * operation is aborted and any devices which were suspended are 3806 * resumed immediately by calling their DEVICE_RESUME() methods. 3807 */ 3808 int 3809 bus_generic_suspend(device_t dev) 3810 { 3811 int error; 3812 device_t child; 3813 3814 /* 3815 * Suspend children in the reverse order. 3816 * For most buses all children are equal, so the order does not matter. 3817 * Other buses, such as acpi, carefully order their child devices to 3818 * express implicit dependencies between them. For such buses it is 3819 * safer to bring down devices in the reverse order. 3820 */ 3821 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) { 3822 error = BUS_SUSPEND_CHILD(dev, child); 3823 if (error != 0) { 3824 child = TAILQ_NEXT(child, link); 3825 if (child != NULL) { 3826 TAILQ_FOREACH_FROM(child, &dev->children, link) 3827 BUS_RESUME_CHILD(dev, child); 3828 } 3829 return (error); 3830 } 3831 } 3832 return (0); 3833 } 3834 3835 /** 3836 * @brief Helper function for implementing DEVICE_RESUME() 3837 * 3838 * This function can be used to help implement the DEVICE_RESUME() for 3839 * a bus. It calls DEVICE_RESUME() on each of the device's children. 3840 */ 3841 int 3842 bus_generic_resume(device_t dev) 3843 { 3844 device_t child; 3845 3846 TAILQ_FOREACH(child, &dev->children, link) { 3847 BUS_RESUME_CHILD(dev, child); 3848 /* if resume fails, there's nothing we can usefully do... */ 3849 } 3850 return (0); 3851 } 3852 3853 /** 3854 * @brief Helper function for implementing BUS_RESET_POST 3855 * 3856 * Bus can use this function to implement common operations of 3857 * re-attaching or resuming the children after the bus itself was 3858 * reset, and after restoring bus-unique state of children. 3859 * 3860 * @param dev The bus 3861 * #param flags DEVF_RESET_* 3862 */ 3863 int 3864 bus_helper_reset_post(device_t dev, int flags) 3865 { 3866 device_t child; 3867 int error, error1; 3868 3869 error = 0; 3870 TAILQ_FOREACH(child, &dev->children,link) { 3871 BUS_RESET_POST(dev, child); 3872 error1 = (flags & DEVF_RESET_DETACH) != 0 ? 3873 device_probe_and_attach(child) : 3874 BUS_RESUME_CHILD(dev, child); 3875 if (error == 0 && error1 != 0) 3876 error = error1; 3877 } 3878 return (error); 3879 } 3880 3881 static void 3882 bus_helper_reset_prepare_rollback(device_t dev, device_t child, int flags) 3883 { 3884 child = TAILQ_NEXT(child, link); 3885 if (child == NULL) 3886 return; 3887 TAILQ_FOREACH_FROM(child, &dev->children,link) { 3888 BUS_RESET_POST(dev, child); 3889 if ((flags & DEVF_RESET_DETACH) != 0) 3890 device_probe_and_attach(child); 3891 else 3892 BUS_RESUME_CHILD(dev, child); 3893 } 3894 } 3895 3896 /** 3897 * @brief Helper function for implementing BUS_RESET_PREPARE 3898 * 3899 * Bus can use this function to implement common operations of 3900 * detaching or suspending the children before the bus itself is 3901 * reset, and then save bus-unique state of children that must 3902 * persists around reset. 3903 * 3904 * @param dev The bus 3905 * #param flags DEVF_RESET_* 3906 */ 3907 int 3908 bus_helper_reset_prepare(device_t dev, int flags) 3909 { 3910 device_t child; 3911 int error; 3912 3913 if (dev->state != DS_ATTACHED) 3914 return (EBUSY); 3915 3916 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) { 3917 if ((flags & DEVF_RESET_DETACH) != 0) { 3918 error = device_get_state(child) == DS_ATTACHED ? 3919 device_detach(child) : 0; 3920 } else { 3921 error = BUS_SUSPEND_CHILD(dev, child); 3922 } 3923 if (error == 0) { 3924 error = BUS_RESET_PREPARE(dev, child); 3925 if (error != 0) { 3926 if ((flags & DEVF_RESET_DETACH) != 0) 3927 device_probe_and_attach(child); 3928 else 3929 BUS_RESUME_CHILD(dev, child); 3930 } 3931 } 3932 if (error != 0) { 3933 bus_helper_reset_prepare_rollback(dev, child, flags); 3934 return (error); 3935 } 3936 } 3937 return (0); 3938 } 3939 3940 /** 3941 * @brief Helper function for implementing BUS_PRINT_CHILD(). 3942 * 3943 * This function prints the first part of the ascii representation of 3944 * @p child, including its name, unit and description (if any - see 3945 * device_set_desc()). 3946 * 3947 * @returns the number of characters printed 3948 */ 3949 int 3950 bus_print_child_header(device_t dev, device_t child) 3951 { 3952 int retval = 0; 3953 3954 if (device_get_desc(child)) { 3955 retval += device_printf(child, "<%s>", device_get_desc(child)); 3956 } else { 3957 retval += printf("%s", device_get_nameunit(child)); 3958 } 3959 3960 return (retval); 3961 } 3962 3963 /** 3964 * @brief Helper function for implementing BUS_PRINT_CHILD(). 3965 * 3966 * This function prints the last part of the ascii representation of 3967 * @p child, which consists of the string @c " on " followed by the 3968 * name and unit of the @p dev. 3969 * 3970 * @returns the number of characters printed 3971 */ 3972 int 3973 bus_print_child_footer(device_t dev, device_t child) 3974 { 3975 return (printf(" on %s\n", device_get_nameunit(dev))); 3976 } 3977 3978 /** 3979 * @brief Helper function for implementing BUS_PRINT_CHILD(). 3980 * 3981 * This function prints out the VM domain for the given device. 3982 * 3983 * @returns the number of characters printed 3984 */ 3985 int 3986 bus_print_child_domain(device_t dev, device_t child) 3987 { 3988 int domain; 3989 3990 /* No domain? Don't print anything */ 3991 if (BUS_GET_DOMAIN(dev, child, &domain) != 0) 3992 return (0); 3993 3994 return (printf(" numa-domain %d", domain)); 3995 } 3996 3997 /** 3998 * @brief Helper function for implementing BUS_PRINT_CHILD(). 3999 * 4000 * This function simply calls bus_print_child_header() followed by 4001 * bus_print_child_footer(). 4002 * 4003 * @returns the number of characters printed 4004 */ 4005 int 4006 bus_generic_print_child(device_t dev, device_t child) 4007 { 4008 int retval = 0; 4009 4010 retval += bus_print_child_header(dev, child); 4011 retval += bus_print_child_domain(dev, child); 4012 retval += bus_print_child_footer(dev, child); 4013 4014 return (retval); 4015 } 4016 4017 /** 4018 * @brief Stub function for implementing BUS_READ_IVAR(). 4019 * 4020 * @returns ENOENT 4021 */ 4022 int 4023 bus_generic_read_ivar(device_t dev, device_t child, int index, 4024 uintptr_t * result) 4025 { 4026 return (ENOENT); 4027 } 4028 4029 /** 4030 * @brief Stub function for implementing BUS_WRITE_IVAR(). 4031 * 4032 * @returns ENOENT 4033 */ 4034 int 4035 bus_generic_write_ivar(device_t dev, device_t child, int index, 4036 uintptr_t value) 4037 { 4038 return (ENOENT); 4039 } 4040 4041 /** 4042 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST(). 4043 * 4044 * @returns NULL 4045 */ 4046 struct resource_list * 4047 bus_generic_get_resource_list(device_t dev, device_t child) 4048 { 4049 return (NULL); 4050 } 4051 4052 /** 4053 * @brief Helper function for implementing BUS_DRIVER_ADDED(). 4054 * 4055 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's 4056 * DEVICE_IDENTIFY() method to allow it to add new children to the bus 4057 * and then calls device_probe_and_attach() for each unattached child. 4058 */ 4059 void 4060 bus_generic_driver_added(device_t dev, driver_t *driver) 4061 { 4062 device_t child; 4063 4064 DEVICE_IDENTIFY(driver, dev); 4065 TAILQ_FOREACH(child, &dev->children, link) { 4066 if (child->state == DS_NOTPRESENT || 4067 (child->flags & DF_REBID)) 4068 device_probe_and_attach(child); 4069 } 4070 } 4071 4072 /** 4073 * @brief Helper function for implementing BUS_NEW_PASS(). 4074 * 4075 * This implementing of BUS_NEW_PASS() first calls the identify 4076 * routines for any drivers that probe at the current pass. Then it 4077 * walks the list of devices for this bus. If a device is already 4078 * attached, then it calls BUS_NEW_PASS() on that device. If the 4079 * device is not already attached, it attempts to attach a driver to 4080 * it. 4081 */ 4082 void 4083 bus_generic_new_pass(device_t dev) 4084 { 4085 driverlink_t dl; 4086 devclass_t dc; 4087 device_t child; 4088 4089 dc = dev->devclass; 4090 TAILQ_FOREACH(dl, &dc->drivers, link) { 4091 if (dl->pass == bus_current_pass) 4092 DEVICE_IDENTIFY(dl->driver, dev); 4093 } 4094 TAILQ_FOREACH(child, &dev->children, link) { 4095 if (child->state >= DS_ATTACHED) 4096 BUS_NEW_PASS(child); 4097 else if (child->state == DS_NOTPRESENT) 4098 device_probe_and_attach(child); 4099 } 4100 } 4101 4102 /** 4103 * @brief Helper function for implementing BUS_SETUP_INTR(). 4104 * 4105 * This simple implementation of BUS_SETUP_INTR() simply calls the 4106 * BUS_SETUP_INTR() method of the parent of @p dev. 4107 */ 4108 int 4109 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq, 4110 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg, 4111 void **cookiep) 4112 { 4113 /* Propagate up the bus hierarchy until someone handles it. */ 4114 if (dev->parent) 4115 return (BUS_SETUP_INTR(dev->parent, child, irq, flags, 4116 filter, intr, arg, cookiep)); 4117 return (EINVAL); 4118 } 4119 4120 /** 4121 * @brief Helper function for implementing BUS_TEARDOWN_INTR(). 4122 * 4123 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the 4124 * BUS_TEARDOWN_INTR() method of the parent of @p dev. 4125 */ 4126 int 4127 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq, 4128 void *cookie) 4129 { 4130 /* Propagate up the bus hierarchy until someone handles it. */ 4131 if (dev->parent) 4132 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie)); 4133 return (EINVAL); 4134 } 4135 4136 /** 4137 * @brief Helper function for implementing BUS_SUSPEND_INTR(). 4138 * 4139 * This simple implementation of BUS_SUSPEND_INTR() simply calls the 4140 * BUS_SUSPEND_INTR() method of the parent of @p dev. 4141 */ 4142 int 4143 bus_generic_suspend_intr(device_t dev, device_t child, struct resource *irq) 4144 { 4145 /* Propagate up the bus hierarchy until someone handles it. */ 4146 if (dev->parent) 4147 return (BUS_SUSPEND_INTR(dev->parent, child, irq)); 4148 return (EINVAL); 4149 } 4150 4151 /** 4152 * @brief Helper function for implementing BUS_RESUME_INTR(). 4153 * 4154 * This simple implementation of BUS_RESUME_INTR() simply calls the 4155 * BUS_RESUME_INTR() method of the parent of @p dev. 4156 */ 4157 int 4158 bus_generic_resume_intr(device_t dev, device_t child, struct resource *irq) 4159 { 4160 /* Propagate up the bus hierarchy until someone handles it. */ 4161 if (dev->parent) 4162 return (BUS_RESUME_INTR(dev->parent, child, irq)); 4163 return (EINVAL); 4164 } 4165 4166 /** 4167 * @brief Helper function for implementing BUS_ADJUST_RESOURCE(). 4168 * 4169 * This simple implementation of BUS_ADJUST_RESOURCE() simply calls the 4170 * BUS_ADJUST_RESOURCE() method of the parent of @p dev. 4171 */ 4172 int 4173 bus_generic_adjust_resource(device_t dev, device_t child, int type, 4174 struct resource *r, rman_res_t start, rman_res_t end) 4175 { 4176 /* Propagate up the bus hierarchy until someone handles it. */ 4177 if (dev->parent) 4178 return (BUS_ADJUST_RESOURCE(dev->parent, child, type, r, start, 4179 end)); 4180 return (EINVAL); 4181 } 4182 4183 /** 4184 * @brief Helper function for implementing BUS_ALLOC_RESOURCE(). 4185 * 4186 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the 4187 * BUS_ALLOC_RESOURCE() method of the parent of @p dev. 4188 */ 4189 struct resource * 4190 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid, 4191 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 4192 { 4193 /* Propagate up the bus hierarchy until someone handles it. */ 4194 if (dev->parent) 4195 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid, 4196 start, end, count, flags)); 4197 return (NULL); 4198 } 4199 4200 /** 4201 * @brief Helper function for implementing BUS_RELEASE_RESOURCE(). 4202 * 4203 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the 4204 * BUS_RELEASE_RESOURCE() method of the parent of @p dev. 4205 */ 4206 int 4207 bus_generic_release_resource(device_t dev, device_t child, int type, int rid, 4208 struct resource *r) 4209 { 4210 /* Propagate up the bus hierarchy until someone handles it. */ 4211 if (dev->parent) 4212 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, 4213 r)); 4214 return (EINVAL); 4215 } 4216 4217 /** 4218 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE(). 4219 * 4220 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the 4221 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev. 4222 */ 4223 int 4224 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid, 4225 struct resource *r) 4226 { 4227 /* Propagate up the bus hierarchy until someone handles it. */ 4228 if (dev->parent) 4229 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, 4230 r)); 4231 return (EINVAL); 4232 } 4233 4234 /** 4235 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE(). 4236 * 4237 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the 4238 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev. 4239 */ 4240 int 4241 bus_generic_deactivate_resource(device_t dev, device_t child, int type, 4242 int rid, struct resource *r) 4243 { 4244 /* Propagate up the bus hierarchy until someone handles it. */ 4245 if (dev->parent) 4246 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid, 4247 r)); 4248 return (EINVAL); 4249 } 4250 4251 /** 4252 * @brief Helper function for implementing BUS_MAP_RESOURCE(). 4253 * 4254 * This simple implementation of BUS_MAP_RESOURCE() simply calls the 4255 * BUS_MAP_RESOURCE() method of the parent of @p dev. 4256 */ 4257 int 4258 bus_generic_map_resource(device_t dev, device_t child, int type, 4259 struct resource *r, struct resource_map_request *args, 4260 struct resource_map *map) 4261 { 4262 /* Propagate up the bus hierarchy until someone handles it. */ 4263 if (dev->parent) 4264 return (BUS_MAP_RESOURCE(dev->parent, child, type, r, args, 4265 map)); 4266 return (EINVAL); 4267 } 4268 4269 /** 4270 * @brief Helper function for implementing BUS_UNMAP_RESOURCE(). 4271 * 4272 * This simple implementation of BUS_UNMAP_RESOURCE() simply calls the 4273 * BUS_UNMAP_RESOURCE() method of the parent of @p dev. 4274 */ 4275 int 4276 bus_generic_unmap_resource(device_t dev, device_t child, int type, 4277 struct resource *r, struct resource_map *map) 4278 { 4279 /* Propagate up the bus hierarchy until someone handles it. */ 4280 if (dev->parent) 4281 return (BUS_UNMAP_RESOURCE(dev->parent, child, type, r, map)); 4282 return (EINVAL); 4283 } 4284 4285 /** 4286 * @brief Helper function for implementing BUS_BIND_INTR(). 4287 * 4288 * This simple implementation of BUS_BIND_INTR() simply calls the 4289 * BUS_BIND_INTR() method of the parent of @p dev. 4290 */ 4291 int 4292 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq, 4293 int cpu) 4294 { 4295 /* Propagate up the bus hierarchy until someone handles it. */ 4296 if (dev->parent) 4297 return (BUS_BIND_INTR(dev->parent, child, irq, cpu)); 4298 return (EINVAL); 4299 } 4300 4301 /** 4302 * @brief Helper function for implementing BUS_CONFIG_INTR(). 4303 * 4304 * This simple implementation of BUS_CONFIG_INTR() simply calls the 4305 * BUS_CONFIG_INTR() method of the parent of @p dev. 4306 */ 4307 int 4308 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig, 4309 enum intr_polarity pol) 4310 { 4311 /* Propagate up the bus hierarchy until someone handles it. */ 4312 if (dev->parent) 4313 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol)); 4314 return (EINVAL); 4315 } 4316 4317 /** 4318 * @brief Helper function for implementing BUS_DESCRIBE_INTR(). 4319 * 4320 * This simple implementation of BUS_DESCRIBE_INTR() simply calls the 4321 * BUS_DESCRIBE_INTR() method of the parent of @p dev. 4322 */ 4323 int 4324 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq, 4325 void *cookie, const char *descr) 4326 { 4327 /* Propagate up the bus hierarchy until someone handles it. */ 4328 if (dev->parent) 4329 return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie, 4330 descr)); 4331 return (EINVAL); 4332 } 4333 4334 /** 4335 * @brief Helper function for implementing BUS_GET_CPUS(). 4336 * 4337 * This simple implementation of BUS_GET_CPUS() simply calls the 4338 * BUS_GET_CPUS() method of the parent of @p dev. 4339 */ 4340 int 4341 bus_generic_get_cpus(device_t dev, device_t child, enum cpu_sets op, 4342 size_t setsize, cpuset_t *cpuset) 4343 { 4344 /* Propagate up the bus hierarchy until someone handles it. */ 4345 if (dev->parent != NULL) 4346 return (BUS_GET_CPUS(dev->parent, child, op, setsize, cpuset)); 4347 return (EINVAL); 4348 } 4349 4350 /** 4351 * @brief Helper function for implementing BUS_GET_DMA_TAG(). 4352 * 4353 * This simple implementation of BUS_GET_DMA_TAG() simply calls the 4354 * BUS_GET_DMA_TAG() method of the parent of @p dev. 4355 */ 4356 bus_dma_tag_t 4357 bus_generic_get_dma_tag(device_t dev, device_t child) 4358 { 4359 /* Propagate up the bus hierarchy until someone handles it. */ 4360 if (dev->parent != NULL) 4361 return (BUS_GET_DMA_TAG(dev->parent, child)); 4362 return (NULL); 4363 } 4364 4365 /** 4366 * @brief Helper function for implementing BUS_GET_BUS_TAG(). 4367 * 4368 * This simple implementation of BUS_GET_BUS_TAG() simply calls the 4369 * BUS_GET_BUS_TAG() method of the parent of @p dev. 4370 */ 4371 bus_space_tag_t 4372 bus_generic_get_bus_tag(device_t dev, device_t child) 4373 { 4374 /* Propagate up the bus hierarchy until someone handles it. */ 4375 if (dev->parent != NULL) 4376 return (BUS_GET_BUS_TAG(dev->parent, child)); 4377 return ((bus_space_tag_t)0); 4378 } 4379 4380 /** 4381 * @brief Helper function for implementing BUS_GET_RESOURCE(). 4382 * 4383 * This implementation of BUS_GET_RESOURCE() uses the 4384 * resource_list_find() function to do most of the work. It calls 4385 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to 4386 * search. 4387 */ 4388 int 4389 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid, 4390 rman_res_t *startp, rman_res_t *countp) 4391 { 4392 struct resource_list * rl = NULL; 4393 struct resource_list_entry * rle = NULL; 4394 4395 rl = BUS_GET_RESOURCE_LIST(dev, child); 4396 if (!rl) 4397 return (EINVAL); 4398 4399 rle = resource_list_find(rl, type, rid); 4400 if (!rle) 4401 return (ENOENT); 4402 4403 if (startp) 4404 *startp = rle->start; 4405 if (countp) 4406 *countp = rle->count; 4407 4408 return (0); 4409 } 4410 4411 /** 4412 * @brief Helper function for implementing BUS_SET_RESOURCE(). 4413 * 4414 * This implementation of BUS_SET_RESOURCE() uses the 4415 * resource_list_add() function to do most of the work. It calls 4416 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to 4417 * edit. 4418 */ 4419 int 4420 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid, 4421 rman_res_t start, rman_res_t count) 4422 { 4423 struct resource_list * rl = NULL; 4424 4425 rl = BUS_GET_RESOURCE_LIST(dev, child); 4426 if (!rl) 4427 return (EINVAL); 4428 4429 resource_list_add(rl, type, rid, start, (start + count - 1), count); 4430 4431 return (0); 4432 } 4433 4434 /** 4435 * @brief Helper function for implementing BUS_DELETE_RESOURCE(). 4436 * 4437 * This implementation of BUS_DELETE_RESOURCE() uses the 4438 * resource_list_delete() function to do most of the work. It calls 4439 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to 4440 * edit. 4441 */ 4442 void 4443 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid) 4444 { 4445 struct resource_list * rl = NULL; 4446 4447 rl = BUS_GET_RESOURCE_LIST(dev, child); 4448 if (!rl) 4449 return; 4450 4451 resource_list_delete(rl, type, rid); 4452 4453 return; 4454 } 4455 4456 /** 4457 * @brief Helper function for implementing BUS_RELEASE_RESOURCE(). 4458 * 4459 * This implementation of BUS_RELEASE_RESOURCE() uses the 4460 * resource_list_release() function to do most of the work. It calls 4461 * BUS_GET_RESOURCE_LIST() to find a suitable resource list. 4462 */ 4463 int 4464 bus_generic_rl_release_resource(device_t dev, device_t child, int type, 4465 int rid, struct resource *r) 4466 { 4467 struct resource_list * rl = NULL; 4468 4469 if (device_get_parent(child) != dev) 4470 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 4471 type, rid, r)); 4472 4473 rl = BUS_GET_RESOURCE_LIST(dev, child); 4474 if (!rl) 4475 return (EINVAL); 4476 4477 return (resource_list_release(rl, dev, child, type, rid, r)); 4478 } 4479 4480 /** 4481 * @brief Helper function for implementing BUS_ALLOC_RESOURCE(). 4482 * 4483 * This implementation of BUS_ALLOC_RESOURCE() uses the 4484 * resource_list_alloc() function to do most of the work. It calls 4485 * BUS_GET_RESOURCE_LIST() to find a suitable resource list. 4486 */ 4487 struct resource * 4488 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type, 4489 int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 4490 { 4491 struct resource_list * rl = NULL; 4492 4493 if (device_get_parent(child) != dev) 4494 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 4495 type, rid, start, end, count, flags)); 4496 4497 rl = BUS_GET_RESOURCE_LIST(dev, child); 4498 if (!rl) 4499 return (NULL); 4500 4501 return (resource_list_alloc(rl, dev, child, type, rid, 4502 start, end, count, flags)); 4503 } 4504 4505 /** 4506 * @brief Helper function for implementing BUS_CHILD_PRESENT(). 4507 * 4508 * This simple implementation of BUS_CHILD_PRESENT() simply calls the 4509 * BUS_CHILD_PRESENT() method of the parent of @p dev. 4510 */ 4511 int 4512 bus_generic_child_present(device_t dev, device_t child) 4513 { 4514 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev)); 4515 } 4516 4517 int 4518 bus_generic_get_domain(device_t dev, device_t child, int *domain) 4519 { 4520 if (dev->parent) 4521 return (BUS_GET_DOMAIN(dev->parent, dev, domain)); 4522 4523 return (ENOENT); 4524 } 4525 4526 /** 4527 * @brief Helper function for implementing BUS_RESCAN(). 4528 * 4529 * This null implementation of BUS_RESCAN() always fails to indicate 4530 * the bus does not support rescanning. 4531 */ 4532 int 4533 bus_null_rescan(device_t dev) 4534 { 4535 return (ENXIO); 4536 } 4537 4538 /* 4539 * Some convenience functions to make it easier for drivers to use the 4540 * resource-management functions. All these really do is hide the 4541 * indirection through the parent's method table, making for slightly 4542 * less-wordy code. In the future, it might make sense for this code 4543 * to maintain some sort of a list of resources allocated by each device. 4544 */ 4545 4546 int 4547 bus_alloc_resources(device_t dev, struct resource_spec *rs, 4548 struct resource **res) 4549 { 4550 int i; 4551 4552 for (i = 0; rs[i].type != -1; i++) 4553 res[i] = NULL; 4554 for (i = 0; rs[i].type != -1; i++) { 4555 res[i] = bus_alloc_resource_any(dev, 4556 rs[i].type, &rs[i].rid, rs[i].flags); 4557 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) { 4558 bus_release_resources(dev, rs, res); 4559 return (ENXIO); 4560 } 4561 } 4562 return (0); 4563 } 4564 4565 void 4566 bus_release_resources(device_t dev, const struct resource_spec *rs, 4567 struct resource **res) 4568 { 4569 int i; 4570 4571 for (i = 0; rs[i].type != -1; i++) 4572 if (res[i] != NULL) { 4573 bus_release_resource( 4574 dev, rs[i].type, rs[i].rid, res[i]); 4575 res[i] = NULL; 4576 } 4577 } 4578 4579 /** 4580 * @brief Wrapper function for BUS_ALLOC_RESOURCE(). 4581 * 4582 * This function simply calls the BUS_ALLOC_RESOURCE() method of the 4583 * parent of @p dev. 4584 */ 4585 struct resource * 4586 bus_alloc_resource(device_t dev, int type, int *rid, rman_res_t start, 4587 rman_res_t end, rman_res_t count, u_int flags) 4588 { 4589 struct resource *res; 4590 4591 if (dev->parent == NULL) 4592 return (NULL); 4593 res = BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end, 4594 count, flags); 4595 return (res); 4596 } 4597 4598 /** 4599 * @brief Wrapper function for BUS_ADJUST_RESOURCE(). 4600 * 4601 * This function simply calls the BUS_ADJUST_RESOURCE() method of the 4602 * parent of @p dev. 4603 */ 4604 int 4605 bus_adjust_resource(device_t dev, int type, struct resource *r, rman_res_t start, 4606 rman_res_t end) 4607 { 4608 if (dev->parent == NULL) 4609 return (EINVAL); 4610 return (BUS_ADJUST_RESOURCE(dev->parent, dev, type, r, start, end)); 4611 } 4612 4613 /** 4614 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE(). 4615 * 4616 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the 4617 * parent of @p dev. 4618 */ 4619 int 4620 bus_activate_resource(device_t dev, int type, int rid, struct resource *r) 4621 { 4622 if (dev->parent == NULL) 4623 return (EINVAL); 4624 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 4625 } 4626 4627 /** 4628 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE(). 4629 * 4630 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the 4631 * parent of @p dev. 4632 */ 4633 int 4634 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r) 4635 { 4636 if (dev->parent == NULL) 4637 return (EINVAL); 4638 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 4639 } 4640 4641 /** 4642 * @brief Wrapper function for BUS_MAP_RESOURCE(). 4643 * 4644 * This function simply calls the BUS_MAP_RESOURCE() method of the 4645 * parent of @p dev. 4646 */ 4647 int 4648 bus_map_resource(device_t dev, int type, struct resource *r, 4649 struct resource_map_request *args, struct resource_map *map) 4650 { 4651 if (dev->parent == NULL) 4652 return (EINVAL); 4653 return (BUS_MAP_RESOURCE(dev->parent, dev, type, r, args, map)); 4654 } 4655 4656 /** 4657 * @brief Wrapper function for BUS_UNMAP_RESOURCE(). 4658 * 4659 * This function simply calls the BUS_UNMAP_RESOURCE() method of the 4660 * parent of @p dev. 4661 */ 4662 int 4663 bus_unmap_resource(device_t dev, int type, struct resource *r, 4664 struct resource_map *map) 4665 { 4666 if (dev->parent == NULL) 4667 return (EINVAL); 4668 return (BUS_UNMAP_RESOURCE(dev->parent, dev, type, r, map)); 4669 } 4670 4671 /** 4672 * @brief Wrapper function for BUS_RELEASE_RESOURCE(). 4673 * 4674 * This function simply calls the BUS_RELEASE_RESOURCE() method of the 4675 * parent of @p dev. 4676 */ 4677 int 4678 bus_release_resource(device_t dev, int type, int rid, struct resource *r) 4679 { 4680 int rv; 4681 4682 if (dev->parent == NULL) 4683 return (EINVAL); 4684 rv = BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r); 4685 return (rv); 4686 } 4687 4688 /** 4689 * @brief Wrapper function for BUS_SETUP_INTR(). 4690 * 4691 * This function simply calls the BUS_SETUP_INTR() method of the 4692 * parent of @p dev. 4693 */ 4694 int 4695 bus_setup_intr(device_t dev, struct resource *r, int flags, 4696 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep) 4697 { 4698 int error; 4699 4700 if (dev->parent == NULL) 4701 return (EINVAL); 4702 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler, 4703 arg, cookiep); 4704 if (error != 0) 4705 return (error); 4706 if (handler != NULL && !(flags & INTR_MPSAFE)) 4707 device_printf(dev, "[GIANT-LOCKED]\n"); 4708 return (0); 4709 } 4710 4711 /** 4712 * @brief Wrapper function for BUS_TEARDOWN_INTR(). 4713 * 4714 * This function simply calls the BUS_TEARDOWN_INTR() method of the 4715 * parent of @p dev. 4716 */ 4717 int 4718 bus_teardown_intr(device_t dev, struct resource *r, void *cookie) 4719 { 4720 if (dev->parent == NULL) 4721 return (EINVAL); 4722 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie)); 4723 } 4724 4725 /** 4726 * @brief Wrapper function for BUS_SUSPEND_INTR(). 4727 * 4728 * This function simply calls the BUS_SUSPEND_INTR() method of the 4729 * parent of @p dev. 4730 */ 4731 int 4732 bus_suspend_intr(device_t dev, struct resource *r) 4733 { 4734 if (dev->parent == NULL) 4735 return (EINVAL); 4736 return (BUS_SUSPEND_INTR(dev->parent, dev, r)); 4737 } 4738 4739 /** 4740 * @brief Wrapper function for BUS_RESUME_INTR(). 4741 * 4742 * This function simply calls the BUS_RESUME_INTR() method of the 4743 * parent of @p dev. 4744 */ 4745 int 4746 bus_resume_intr(device_t dev, struct resource *r) 4747 { 4748 if (dev->parent == NULL) 4749 return (EINVAL); 4750 return (BUS_RESUME_INTR(dev->parent, dev, r)); 4751 } 4752 4753 /** 4754 * @brief Wrapper function for BUS_BIND_INTR(). 4755 * 4756 * This function simply calls the BUS_BIND_INTR() method of the 4757 * parent of @p dev. 4758 */ 4759 int 4760 bus_bind_intr(device_t dev, struct resource *r, int cpu) 4761 { 4762 if (dev->parent == NULL) 4763 return (EINVAL); 4764 return (BUS_BIND_INTR(dev->parent, dev, r, cpu)); 4765 } 4766 4767 /** 4768 * @brief Wrapper function for BUS_DESCRIBE_INTR(). 4769 * 4770 * This function first formats the requested description into a 4771 * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of 4772 * the parent of @p dev. 4773 */ 4774 int 4775 bus_describe_intr(device_t dev, struct resource *irq, void *cookie, 4776 const char *fmt, ...) 4777 { 4778 va_list ap; 4779 char descr[MAXCOMLEN + 1]; 4780 4781 if (dev->parent == NULL) 4782 return (EINVAL); 4783 va_start(ap, fmt); 4784 vsnprintf(descr, sizeof(descr), fmt, ap); 4785 va_end(ap); 4786 return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr)); 4787 } 4788 4789 /** 4790 * @brief Wrapper function for BUS_SET_RESOURCE(). 4791 * 4792 * This function simply calls the BUS_SET_RESOURCE() method of the 4793 * parent of @p dev. 4794 */ 4795 int 4796 bus_set_resource(device_t dev, int type, int rid, 4797 rman_res_t start, rman_res_t count) 4798 { 4799 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid, 4800 start, count)); 4801 } 4802 4803 /** 4804 * @brief Wrapper function for BUS_GET_RESOURCE(). 4805 * 4806 * This function simply calls the BUS_GET_RESOURCE() method of the 4807 * parent of @p dev. 4808 */ 4809 int 4810 bus_get_resource(device_t dev, int type, int rid, 4811 rman_res_t *startp, rman_res_t *countp) 4812 { 4813 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 4814 startp, countp)); 4815 } 4816 4817 /** 4818 * @brief Wrapper function for BUS_GET_RESOURCE(). 4819 * 4820 * This function simply calls the BUS_GET_RESOURCE() method of the 4821 * parent of @p dev and returns the start value. 4822 */ 4823 rman_res_t 4824 bus_get_resource_start(device_t dev, int type, int rid) 4825 { 4826 rman_res_t start; 4827 rman_res_t count; 4828 int error; 4829 4830 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 4831 &start, &count); 4832 if (error) 4833 return (0); 4834 return (start); 4835 } 4836 4837 /** 4838 * @brief Wrapper function for BUS_GET_RESOURCE(). 4839 * 4840 * This function simply calls the BUS_GET_RESOURCE() method of the 4841 * parent of @p dev and returns the count value. 4842 */ 4843 rman_res_t 4844 bus_get_resource_count(device_t dev, int type, int rid) 4845 { 4846 rman_res_t start; 4847 rman_res_t count; 4848 int error; 4849 4850 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 4851 &start, &count); 4852 if (error) 4853 return (0); 4854 return (count); 4855 } 4856 4857 /** 4858 * @brief Wrapper function for BUS_DELETE_RESOURCE(). 4859 * 4860 * This function simply calls the BUS_DELETE_RESOURCE() method of the 4861 * parent of @p dev. 4862 */ 4863 void 4864 bus_delete_resource(device_t dev, int type, int rid) 4865 { 4866 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid); 4867 } 4868 4869 /** 4870 * @brief Wrapper function for BUS_CHILD_PRESENT(). 4871 * 4872 * This function simply calls the BUS_CHILD_PRESENT() method of the 4873 * parent of @p dev. 4874 */ 4875 int 4876 bus_child_present(device_t child) 4877 { 4878 return (BUS_CHILD_PRESENT(device_get_parent(child), child)); 4879 } 4880 4881 /** 4882 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR(). 4883 * 4884 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the 4885 * parent of @p dev. 4886 */ 4887 int 4888 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen) 4889 { 4890 device_t parent; 4891 4892 parent = device_get_parent(child); 4893 if (parent == NULL) { 4894 *buf = '\0'; 4895 return (0); 4896 } 4897 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen)); 4898 } 4899 4900 /** 4901 * @brief Wrapper function for BUS_CHILD_LOCATION_STR(). 4902 * 4903 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the 4904 * parent of @p dev. 4905 */ 4906 int 4907 bus_child_location_str(device_t child, char *buf, size_t buflen) 4908 { 4909 device_t parent; 4910 4911 parent = device_get_parent(child); 4912 if (parent == NULL) { 4913 *buf = '\0'; 4914 return (0); 4915 } 4916 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen)); 4917 } 4918 4919 /** 4920 * @brief Wrapper function for bus_child_pnpinfo_str using sbuf 4921 * 4922 * A convenient wrapper frunction for bus_child_pnpinfo_str that allows 4923 * us to splat that into an sbuf. It uses unholy knowledge of sbuf to 4924 * accomplish this, however. It is an interim function until we can convert 4925 * this interface more fully. 4926 */ 4927 /* Note: we reach inside of sbuf because it's API isn't rich enough to do this */ 4928 #define SPACE(s) ((s)->s_size - (s)->s_len) 4929 #define EOB(s) ((s)->s_buf + (s)->s_len) 4930 4931 static int 4932 bus_child_pnpinfo_sb(device_t dev, struct sbuf *sb) 4933 { 4934 char *p; 4935 size_t space; 4936 4937 MPASS((sb->s_flags & SBUF_INCLUDENUL) == 0); 4938 if (sb->s_error != 0) 4939 return (-1); 4940 p = EOB(sb); 4941 *p = '\0'; /* sbuf buffer isn't NUL terminated until sbuf_finish() */ 4942 space = SPACE(sb); 4943 if (space <= 1) { 4944 sb->s_error = ENOMEM; 4945 return (-1); 4946 } 4947 bus_child_pnpinfo_str(dev, p, space); 4948 sb->s_len += strlen(p); 4949 return (0); 4950 } 4951 4952 /** 4953 * @brief Wrapper function for bus_child_pnpinfo_str using sbuf 4954 * 4955 * A convenient wrapper frunction for bus_child_pnpinfo_str that allows 4956 * us to splat that into an sbuf. It uses unholy knowledge of sbuf to 4957 * accomplish this, however. It is an interim function until we can convert 4958 * this interface more fully. 4959 */ 4960 static int 4961 bus_child_location_sb(device_t dev, struct sbuf *sb) 4962 { 4963 char *p; 4964 size_t space; 4965 4966 MPASS((sb->s_flags & SBUF_INCLUDENUL) == 0); 4967 if (sb->s_error != 0) 4968 return (-1); 4969 p = EOB(sb); 4970 *p = '\0'; /* sbuf buffer isn't NUL terminated until sbuf_finish() */ 4971 space = SPACE(sb); 4972 if (space <= 1) { 4973 sb->s_error = ENOMEM; 4974 return (-1); 4975 } 4976 bus_child_location_str(dev, p, space); 4977 sb->s_len += strlen(p); 4978 return (0); 4979 } 4980 #undef SPACE 4981 #undef EOB 4982 4983 /** 4984 * @brief Wrapper function for BUS_GET_CPUS(). 4985 * 4986 * This function simply calls the BUS_GET_CPUS() method of the 4987 * parent of @p dev. 4988 */ 4989 int 4990 bus_get_cpus(device_t dev, enum cpu_sets op, size_t setsize, cpuset_t *cpuset) 4991 { 4992 device_t parent; 4993 4994 parent = device_get_parent(dev); 4995 if (parent == NULL) 4996 return (EINVAL); 4997 return (BUS_GET_CPUS(parent, dev, op, setsize, cpuset)); 4998 } 4999 5000 /** 5001 * @brief Wrapper function for BUS_GET_DMA_TAG(). 5002 * 5003 * This function simply calls the BUS_GET_DMA_TAG() method of the 5004 * parent of @p dev. 5005 */ 5006 bus_dma_tag_t 5007 bus_get_dma_tag(device_t dev) 5008 { 5009 device_t parent; 5010 5011 parent = device_get_parent(dev); 5012 if (parent == NULL) 5013 return (NULL); 5014 return (BUS_GET_DMA_TAG(parent, dev)); 5015 } 5016 5017 /** 5018 * @brief Wrapper function for BUS_GET_BUS_TAG(). 5019 * 5020 * This function simply calls the BUS_GET_BUS_TAG() method of the 5021 * parent of @p dev. 5022 */ 5023 bus_space_tag_t 5024 bus_get_bus_tag(device_t dev) 5025 { 5026 device_t parent; 5027 5028 parent = device_get_parent(dev); 5029 if (parent == NULL) 5030 return ((bus_space_tag_t)0); 5031 return (BUS_GET_BUS_TAG(parent, dev)); 5032 } 5033 5034 /** 5035 * @brief Wrapper function for BUS_GET_DOMAIN(). 5036 * 5037 * This function simply calls the BUS_GET_DOMAIN() method of the 5038 * parent of @p dev. 5039 */ 5040 int 5041 bus_get_domain(device_t dev, int *domain) 5042 { 5043 return (BUS_GET_DOMAIN(device_get_parent(dev), dev, domain)); 5044 } 5045 5046 /* Resume all devices and then notify userland that we're up again. */ 5047 static int 5048 root_resume(device_t dev) 5049 { 5050 int error; 5051 5052 error = bus_generic_resume(dev); 5053 if (error == 0) { 5054 devctl_notify("kern", "power", "resume", NULL); /* Deprecated gone in 14 */ 5055 devctl_notify("kernel", "power", "resume", NULL); 5056 } 5057 return (error); 5058 } 5059 5060 static int 5061 root_print_child(device_t dev, device_t child) 5062 { 5063 int retval = 0; 5064 5065 retval += bus_print_child_header(dev, child); 5066 retval += printf("\n"); 5067 5068 return (retval); 5069 } 5070 5071 static int 5072 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags, 5073 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep) 5074 { 5075 /* 5076 * If an interrupt mapping gets to here something bad has happened. 5077 */ 5078 panic("root_setup_intr"); 5079 } 5080 5081 /* 5082 * If we get here, assume that the device is permanent and really is 5083 * present in the system. Removable bus drivers are expected to intercept 5084 * this call long before it gets here. We return -1 so that drivers that 5085 * really care can check vs -1 or some ERRNO returned higher in the food 5086 * chain. 5087 */ 5088 static int 5089 root_child_present(device_t dev, device_t child) 5090 { 5091 return (-1); 5092 } 5093 5094 static int 5095 root_get_cpus(device_t dev, device_t child, enum cpu_sets op, size_t setsize, 5096 cpuset_t *cpuset) 5097 { 5098 switch (op) { 5099 case INTR_CPUS: 5100 /* Default to returning the set of all CPUs. */ 5101 if (setsize != sizeof(cpuset_t)) 5102 return (EINVAL); 5103 *cpuset = all_cpus; 5104 return (0); 5105 default: 5106 return (EINVAL); 5107 } 5108 } 5109 5110 static kobj_method_t root_methods[] = { 5111 /* Device interface */ 5112 KOBJMETHOD(device_shutdown, bus_generic_shutdown), 5113 KOBJMETHOD(device_suspend, bus_generic_suspend), 5114 KOBJMETHOD(device_resume, root_resume), 5115 5116 /* Bus interface */ 5117 KOBJMETHOD(bus_print_child, root_print_child), 5118 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar), 5119 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar), 5120 KOBJMETHOD(bus_setup_intr, root_setup_intr), 5121 KOBJMETHOD(bus_child_present, root_child_present), 5122 KOBJMETHOD(bus_get_cpus, root_get_cpus), 5123 5124 KOBJMETHOD_END 5125 }; 5126 5127 static driver_t root_driver = { 5128 "root", 5129 root_methods, 5130 1, /* no softc */ 5131 }; 5132 5133 device_t root_bus; 5134 devclass_t root_devclass; 5135 5136 static int 5137 root_bus_module_handler(module_t mod, int what, void* arg) 5138 { 5139 switch (what) { 5140 case MOD_LOAD: 5141 TAILQ_INIT(&bus_data_devices); 5142 kobj_class_compile((kobj_class_t) &root_driver); 5143 root_bus = make_device(NULL, "root", 0); 5144 root_bus->desc = "System root bus"; 5145 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver); 5146 root_bus->driver = &root_driver; 5147 root_bus->state = DS_ATTACHED; 5148 root_devclass = devclass_find_internal("root", NULL, FALSE); 5149 devinit(); 5150 return (0); 5151 5152 case MOD_SHUTDOWN: 5153 device_shutdown(root_bus); 5154 return (0); 5155 default: 5156 return (EOPNOTSUPP); 5157 } 5158 5159 return (0); 5160 } 5161 5162 static moduledata_t root_bus_mod = { 5163 "rootbus", 5164 root_bus_module_handler, 5165 NULL 5166 }; 5167 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 5168 5169 /** 5170 * @brief Automatically configure devices 5171 * 5172 * This function begins the autoconfiguration process by calling 5173 * device_probe_and_attach() for each child of the @c root0 device. 5174 */ 5175 void 5176 root_bus_configure(void) 5177 { 5178 PDEBUG((".")); 5179 5180 /* Eventually this will be split up, but this is sufficient for now. */ 5181 bus_set_pass(BUS_PASS_DEFAULT); 5182 } 5183 5184 /** 5185 * @brief Module handler for registering device drivers 5186 * 5187 * This module handler is used to automatically register device 5188 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls 5189 * devclass_add_driver() for the driver described by the 5190 * driver_module_data structure pointed to by @p arg 5191 */ 5192 int 5193 driver_module_handler(module_t mod, int what, void *arg) 5194 { 5195 struct driver_module_data *dmd; 5196 devclass_t bus_devclass; 5197 kobj_class_t driver; 5198 int error, pass; 5199 5200 dmd = (struct driver_module_data *)arg; 5201 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE); 5202 error = 0; 5203 5204 switch (what) { 5205 case MOD_LOAD: 5206 if (dmd->dmd_chainevh) 5207 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 5208 5209 pass = dmd->dmd_pass; 5210 driver = dmd->dmd_driver; 5211 PDEBUG(("Loading module: driver %s on bus %s (pass %d)", 5212 DRIVERNAME(driver), dmd->dmd_busname, pass)); 5213 error = devclass_add_driver(bus_devclass, driver, pass, 5214 dmd->dmd_devclass); 5215 break; 5216 5217 case MOD_UNLOAD: 5218 PDEBUG(("Unloading module: driver %s from bus %s", 5219 DRIVERNAME(dmd->dmd_driver), 5220 dmd->dmd_busname)); 5221 error = devclass_delete_driver(bus_devclass, 5222 dmd->dmd_driver); 5223 5224 if (!error && dmd->dmd_chainevh) 5225 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 5226 break; 5227 case MOD_QUIESCE: 5228 PDEBUG(("Quiesce module: driver %s from bus %s", 5229 DRIVERNAME(dmd->dmd_driver), 5230 dmd->dmd_busname)); 5231 error = devclass_quiesce_driver(bus_devclass, 5232 dmd->dmd_driver); 5233 5234 if (!error && dmd->dmd_chainevh) 5235 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 5236 break; 5237 default: 5238 error = EOPNOTSUPP; 5239 break; 5240 } 5241 5242 return (error); 5243 } 5244 5245 /** 5246 * @brief Enumerate all hinted devices for this bus. 5247 * 5248 * Walks through the hints for this bus and calls the bus_hinted_child 5249 * routine for each one it fines. It searches first for the specific 5250 * bus that's being probed for hinted children (eg isa0), and then for 5251 * generic children (eg isa). 5252 * 5253 * @param dev bus device to enumerate 5254 */ 5255 void 5256 bus_enumerate_hinted_children(device_t bus) 5257 { 5258 int i; 5259 const char *dname, *busname; 5260 int dunit; 5261 5262 /* 5263 * enumerate all devices on the specific bus 5264 */ 5265 busname = device_get_nameunit(bus); 5266 i = 0; 5267 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0) 5268 BUS_HINTED_CHILD(bus, dname, dunit); 5269 5270 /* 5271 * and all the generic ones. 5272 */ 5273 busname = device_get_name(bus); 5274 i = 0; 5275 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0) 5276 BUS_HINTED_CHILD(bus, dname, dunit); 5277 } 5278 5279 #ifdef BUS_DEBUG 5280 5281 /* the _short versions avoid iteration by not calling anything that prints 5282 * more than oneliners. I love oneliners. 5283 */ 5284 5285 static void 5286 print_device_short(device_t dev, int indent) 5287 { 5288 if (!dev) 5289 return; 5290 5291 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n", 5292 dev->unit, dev->desc, 5293 (dev->parent? "":"no "), 5294 (TAILQ_EMPTY(&dev->children)? "no ":""), 5295 (dev->flags&DF_ENABLED? "enabled,":"disabled,"), 5296 (dev->flags&DF_FIXEDCLASS? "fixed,":""), 5297 (dev->flags&DF_WILDCARD? "wildcard,":""), 5298 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""), 5299 (dev->flags&DF_REBID? "rebiddable,":""), 5300 (dev->flags&DF_SUSPENDED? "suspended,":""), 5301 (dev->ivars? "":"no "), 5302 (dev->softc? "":"no "), 5303 dev->busy)); 5304 } 5305 5306 static void 5307 print_device(device_t dev, int indent) 5308 { 5309 if (!dev) 5310 return; 5311 5312 print_device_short(dev, indent); 5313 5314 indentprintf(("Parent:\n")); 5315 print_device_short(dev->parent, indent+1); 5316 indentprintf(("Driver:\n")); 5317 print_driver_short(dev->driver, indent+1); 5318 indentprintf(("Devclass:\n")); 5319 print_devclass_short(dev->devclass, indent+1); 5320 } 5321 5322 void 5323 print_device_tree_short(device_t dev, int indent) 5324 /* print the device and all its children (indented) */ 5325 { 5326 device_t child; 5327 5328 if (!dev) 5329 return; 5330 5331 print_device_short(dev, indent); 5332 5333 TAILQ_FOREACH(child, &dev->children, link) { 5334 print_device_tree_short(child, indent+1); 5335 } 5336 } 5337 5338 void 5339 print_device_tree(device_t dev, int indent) 5340 /* print the device and all its children (indented) */ 5341 { 5342 device_t child; 5343 5344 if (!dev) 5345 return; 5346 5347 print_device(dev, indent); 5348 5349 TAILQ_FOREACH(child, &dev->children, link) { 5350 print_device_tree(child, indent+1); 5351 } 5352 } 5353 5354 static void 5355 print_driver_short(driver_t *driver, int indent) 5356 { 5357 if (!driver) 5358 return; 5359 5360 indentprintf(("driver %s: softc size = %zd\n", 5361 driver->name, driver->size)); 5362 } 5363 5364 static void 5365 print_driver(driver_t *driver, int indent) 5366 { 5367 if (!driver) 5368 return; 5369 5370 print_driver_short(driver, indent); 5371 } 5372 5373 static void 5374 print_driver_list(driver_list_t drivers, int indent) 5375 { 5376 driverlink_t driver; 5377 5378 TAILQ_FOREACH(driver, &drivers, link) { 5379 print_driver(driver->driver, indent); 5380 } 5381 } 5382 5383 static void 5384 print_devclass_short(devclass_t dc, int indent) 5385 { 5386 if ( !dc ) 5387 return; 5388 5389 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit)); 5390 } 5391 5392 static void 5393 print_devclass(devclass_t dc, int indent) 5394 { 5395 int i; 5396 5397 if ( !dc ) 5398 return; 5399 5400 print_devclass_short(dc, indent); 5401 indentprintf(("Drivers:\n")); 5402 print_driver_list(dc->drivers, indent+1); 5403 5404 indentprintf(("Devices:\n")); 5405 for (i = 0; i < dc->maxunit; i++) 5406 if (dc->devices[i]) 5407 print_device(dc->devices[i], indent+1); 5408 } 5409 5410 void 5411 print_devclass_list_short(void) 5412 { 5413 devclass_t dc; 5414 5415 printf("Short listing of devclasses, drivers & devices:\n"); 5416 TAILQ_FOREACH(dc, &devclasses, link) { 5417 print_devclass_short(dc, 0); 5418 } 5419 } 5420 5421 void 5422 print_devclass_list(void) 5423 { 5424 devclass_t dc; 5425 5426 printf("Full listing of devclasses, drivers & devices:\n"); 5427 TAILQ_FOREACH(dc, &devclasses, link) { 5428 print_devclass(dc, 0); 5429 } 5430 } 5431 5432 #endif 5433 5434 /* 5435 * User-space access to the device tree. 5436 * 5437 * We implement a small set of nodes: 5438 * 5439 * hw.bus Single integer read method to obtain the 5440 * current generation count. 5441 * hw.bus.devices Reads the entire device tree in flat space. 5442 * hw.bus.rman Resource manager interface 5443 * 5444 * We might like to add the ability to scan devclasses and/or drivers to 5445 * determine what else is currently loaded/available. 5446 */ 5447 5448 static int 5449 sysctl_bus_info(SYSCTL_HANDLER_ARGS) 5450 { 5451 struct u_businfo ubus; 5452 5453 ubus.ub_version = BUS_USER_VERSION; 5454 ubus.ub_generation = bus_data_generation; 5455 5456 return (SYSCTL_OUT(req, &ubus, sizeof(ubus))); 5457 } 5458 SYSCTL_PROC(_hw_bus, OID_AUTO, info, CTLTYPE_STRUCT | CTLFLAG_RD | 5459 CTLFLAG_MPSAFE, NULL, 0, sysctl_bus_info, "S,u_businfo", 5460 "bus-related data"); 5461 5462 static int 5463 sysctl_devices(SYSCTL_HANDLER_ARGS) 5464 { 5465 struct sbuf sb; 5466 int *name = (int *)arg1; 5467 u_int namelen = arg2; 5468 int index; 5469 device_t dev; 5470 struct u_device *udev; 5471 int error; 5472 5473 if (namelen != 2) 5474 return (EINVAL); 5475 5476 if (bus_data_generation_check(name[0])) 5477 return (EINVAL); 5478 5479 index = name[1]; 5480 5481 /* 5482 * Scan the list of devices, looking for the requested index. 5483 */ 5484 TAILQ_FOREACH(dev, &bus_data_devices, devlink) { 5485 if (index-- == 0) 5486 break; 5487 } 5488 if (dev == NULL) 5489 return (ENOENT); 5490 5491 /* 5492 * Populate the return item, careful not to overflow the buffer. 5493 */ 5494 udev = malloc(sizeof(*udev), M_BUS, M_WAITOK | M_ZERO); 5495 if (udev == NULL) 5496 return (ENOMEM); 5497 udev->dv_handle = (uintptr_t)dev; 5498 udev->dv_parent = (uintptr_t)dev->parent; 5499 udev->dv_devflags = dev->devflags; 5500 udev->dv_flags = dev->flags; 5501 udev->dv_state = dev->state; 5502 sbuf_new(&sb, udev->dv_fields, sizeof(udev->dv_fields), SBUF_FIXEDLEN); 5503 if (dev->nameunit != NULL) 5504 sbuf_cat(&sb, dev->nameunit); 5505 else 5506 sbuf_putc(&sb, '\0'); 5507 sbuf_putc(&sb, '\0'); 5508 if (dev->desc != NULL) 5509 sbuf_cat(&sb, dev->desc); 5510 else 5511 sbuf_putc(&sb, '\0'); 5512 sbuf_putc(&sb, '\0'); 5513 if (dev->driver != NULL) 5514 sbuf_cat(&sb, dev->driver->name); 5515 else 5516 sbuf_putc(&sb, '\0'); 5517 sbuf_putc(&sb, '\0'); 5518 bus_child_pnpinfo_sb(dev, &sb); 5519 sbuf_putc(&sb, '\0'); 5520 bus_child_location_sb(dev, &sb); 5521 sbuf_putc(&sb, '\0'); 5522 error = sbuf_finish(&sb); 5523 if (error == 0) 5524 error = SYSCTL_OUT(req, udev, sizeof(*udev)); 5525 sbuf_delete(&sb); 5526 free(udev, M_BUS); 5527 return (error); 5528 } 5529 5530 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, 5531 CTLFLAG_RD | CTLFLAG_NEEDGIANT, sysctl_devices, 5532 "system device tree"); 5533 5534 int 5535 bus_data_generation_check(int generation) 5536 { 5537 if (generation != bus_data_generation) 5538 return (1); 5539 5540 /* XXX generate optimised lists here? */ 5541 return (0); 5542 } 5543 5544 void 5545 bus_data_generation_update(void) 5546 { 5547 atomic_add_int(&bus_data_generation, 1); 5548 } 5549 5550 int 5551 bus_free_resource(device_t dev, int type, struct resource *r) 5552 { 5553 if (r == NULL) 5554 return (0); 5555 return (bus_release_resource(dev, type, rman_get_rid(r), r)); 5556 } 5557 5558 device_t 5559 device_lookup_by_name(const char *name) 5560 { 5561 device_t dev; 5562 5563 TAILQ_FOREACH(dev, &bus_data_devices, devlink) { 5564 if (dev->nameunit != NULL && strcmp(dev->nameunit, name) == 0) 5565 return (dev); 5566 } 5567 return (NULL); 5568 } 5569 5570 /* 5571 * /dev/devctl2 implementation. The existing /dev/devctl device has 5572 * implicit semantics on open, so it could not be reused for this. 5573 * Another option would be to call this /dev/bus? 5574 */ 5575 static int 5576 find_device(struct devreq *req, device_t *devp) 5577 { 5578 device_t dev; 5579 5580 /* 5581 * First, ensure that the name is nul terminated. 5582 */ 5583 if (memchr(req->dr_name, '\0', sizeof(req->dr_name)) == NULL) 5584 return (EINVAL); 5585 5586 /* 5587 * Second, try to find an attached device whose name matches 5588 * 'name'. 5589 */ 5590 dev = device_lookup_by_name(req->dr_name); 5591 if (dev != NULL) { 5592 *devp = dev; 5593 return (0); 5594 } 5595 5596 /* Finally, give device enumerators a chance. */ 5597 dev = NULL; 5598 EVENTHANDLER_DIRECT_INVOKE(dev_lookup, req->dr_name, &dev); 5599 if (dev == NULL) 5600 return (ENOENT); 5601 *devp = dev; 5602 return (0); 5603 } 5604 5605 static bool 5606 driver_exists(device_t bus, const char *driver) 5607 { 5608 devclass_t dc; 5609 5610 for (dc = bus->devclass; dc != NULL; dc = dc->parent) { 5611 if (devclass_find_driver_internal(dc, driver) != NULL) 5612 return (true); 5613 } 5614 return (false); 5615 } 5616 5617 static void 5618 device_gen_nomatch(device_t dev) 5619 { 5620 device_t child; 5621 5622 if (dev->flags & DF_NEEDNOMATCH && 5623 dev->state == DS_NOTPRESENT) { 5624 BUS_PROBE_NOMATCH(dev->parent, dev); 5625 devnomatch(dev); 5626 dev->flags |= DF_DONENOMATCH; 5627 } 5628 dev->flags &= ~DF_NEEDNOMATCH; 5629 TAILQ_FOREACH(child, &dev->children, link) { 5630 device_gen_nomatch(child); 5631 } 5632 } 5633 5634 static void 5635 device_do_deferred_actions(void) 5636 { 5637 devclass_t dc; 5638 driverlink_t dl; 5639 5640 /* 5641 * Walk through the devclasses to find all the drivers we've tagged as 5642 * deferred during the freeze and call the driver added routines. They 5643 * have already been added to the lists in the background, so the driver 5644 * added routines that trigger a probe will have all the right bidders 5645 * for the probe auction. 5646 */ 5647 TAILQ_FOREACH(dc, &devclasses, link) { 5648 TAILQ_FOREACH(dl, &dc->drivers, link) { 5649 if (dl->flags & DL_DEFERRED_PROBE) { 5650 devclass_driver_added(dc, dl->driver); 5651 dl->flags &= ~DL_DEFERRED_PROBE; 5652 } 5653 } 5654 } 5655 5656 /* 5657 * We also defer no-match events during a freeze. Walk the tree and 5658 * generate all the pent-up events that are still relevant. 5659 */ 5660 device_gen_nomatch(root_bus); 5661 bus_data_generation_update(); 5662 } 5663 5664 static int 5665 devctl2_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag, 5666 struct thread *td) 5667 { 5668 struct devreq *req; 5669 device_t dev; 5670 int error, old; 5671 5672 /* Locate the device to control. */ 5673 mtx_lock(&Giant); 5674 req = (struct devreq *)data; 5675 switch (cmd) { 5676 case DEV_ATTACH: 5677 case DEV_DETACH: 5678 case DEV_ENABLE: 5679 case DEV_DISABLE: 5680 case DEV_SUSPEND: 5681 case DEV_RESUME: 5682 case DEV_SET_DRIVER: 5683 case DEV_CLEAR_DRIVER: 5684 case DEV_RESCAN: 5685 case DEV_DELETE: 5686 case DEV_RESET: 5687 error = priv_check(td, PRIV_DRIVER); 5688 if (error == 0) 5689 error = find_device(req, &dev); 5690 break; 5691 case DEV_FREEZE: 5692 case DEV_THAW: 5693 error = priv_check(td, PRIV_DRIVER); 5694 break; 5695 default: 5696 error = ENOTTY; 5697 break; 5698 } 5699 if (error) { 5700 mtx_unlock(&Giant); 5701 return (error); 5702 } 5703 5704 /* Perform the requested operation. */ 5705 switch (cmd) { 5706 case DEV_ATTACH: 5707 if (device_is_attached(dev) && (dev->flags & DF_REBID) == 0) 5708 error = EBUSY; 5709 else if (!device_is_enabled(dev)) 5710 error = ENXIO; 5711 else 5712 error = device_probe_and_attach(dev); 5713 break; 5714 case DEV_DETACH: 5715 if (!device_is_attached(dev)) { 5716 error = ENXIO; 5717 break; 5718 } 5719 if (!(req->dr_flags & DEVF_FORCE_DETACH)) { 5720 error = device_quiesce(dev); 5721 if (error) 5722 break; 5723 } 5724 error = device_detach(dev); 5725 break; 5726 case DEV_ENABLE: 5727 if (device_is_enabled(dev)) { 5728 error = EBUSY; 5729 break; 5730 } 5731 5732 /* 5733 * If the device has been probed but not attached (e.g. 5734 * when it has been disabled by a loader hint), just 5735 * attach the device rather than doing a full probe. 5736 */ 5737 device_enable(dev); 5738 if (device_is_alive(dev)) { 5739 /* 5740 * If the device was disabled via a hint, clear 5741 * the hint. 5742 */ 5743 if (resource_disabled(dev->driver->name, dev->unit)) 5744 resource_unset_value(dev->driver->name, 5745 dev->unit, "disabled"); 5746 error = device_attach(dev); 5747 } else 5748 error = device_probe_and_attach(dev); 5749 break; 5750 case DEV_DISABLE: 5751 if (!device_is_enabled(dev)) { 5752 error = ENXIO; 5753 break; 5754 } 5755 5756 if (!(req->dr_flags & DEVF_FORCE_DETACH)) { 5757 error = device_quiesce(dev); 5758 if (error) 5759 break; 5760 } 5761 5762 /* 5763 * Force DF_FIXEDCLASS on around detach to preserve 5764 * the existing name. 5765 */ 5766 old = dev->flags; 5767 dev->flags |= DF_FIXEDCLASS; 5768 error = device_detach(dev); 5769 if (!(old & DF_FIXEDCLASS)) 5770 dev->flags &= ~DF_FIXEDCLASS; 5771 if (error == 0) 5772 device_disable(dev); 5773 break; 5774 case DEV_SUSPEND: 5775 if (device_is_suspended(dev)) { 5776 error = EBUSY; 5777 break; 5778 } 5779 if (device_get_parent(dev) == NULL) { 5780 error = EINVAL; 5781 break; 5782 } 5783 error = BUS_SUSPEND_CHILD(device_get_parent(dev), dev); 5784 break; 5785 case DEV_RESUME: 5786 if (!device_is_suspended(dev)) { 5787 error = EINVAL; 5788 break; 5789 } 5790 if (device_get_parent(dev) == NULL) { 5791 error = EINVAL; 5792 break; 5793 } 5794 error = BUS_RESUME_CHILD(device_get_parent(dev), dev); 5795 break; 5796 case DEV_SET_DRIVER: { 5797 devclass_t dc; 5798 char driver[128]; 5799 5800 error = copyinstr(req->dr_data, driver, sizeof(driver), NULL); 5801 if (error) 5802 break; 5803 if (driver[0] == '\0') { 5804 error = EINVAL; 5805 break; 5806 } 5807 if (dev->devclass != NULL && 5808 strcmp(driver, dev->devclass->name) == 0) 5809 /* XXX: Could possibly force DF_FIXEDCLASS on? */ 5810 break; 5811 5812 /* 5813 * Scan drivers for this device's bus looking for at 5814 * least one matching driver. 5815 */ 5816 if (dev->parent == NULL) { 5817 error = EINVAL; 5818 break; 5819 } 5820 if (!driver_exists(dev->parent, driver)) { 5821 error = ENOENT; 5822 break; 5823 } 5824 dc = devclass_create(driver); 5825 if (dc == NULL) { 5826 error = ENOMEM; 5827 break; 5828 } 5829 5830 /* Detach device if necessary. */ 5831 if (device_is_attached(dev)) { 5832 if (req->dr_flags & DEVF_SET_DRIVER_DETACH) 5833 error = device_detach(dev); 5834 else 5835 error = EBUSY; 5836 if (error) 5837 break; 5838 } 5839 5840 /* Clear any previously-fixed device class and unit. */ 5841 if (dev->flags & DF_FIXEDCLASS) 5842 devclass_delete_device(dev->devclass, dev); 5843 dev->flags |= DF_WILDCARD; 5844 dev->unit = -1; 5845 5846 /* Force the new device class. */ 5847 error = devclass_add_device(dc, dev); 5848 if (error) 5849 break; 5850 dev->flags |= DF_FIXEDCLASS; 5851 error = device_probe_and_attach(dev); 5852 break; 5853 } 5854 case DEV_CLEAR_DRIVER: 5855 if (!(dev->flags & DF_FIXEDCLASS)) { 5856 error = 0; 5857 break; 5858 } 5859 if (device_is_attached(dev)) { 5860 if (req->dr_flags & DEVF_CLEAR_DRIVER_DETACH) 5861 error = device_detach(dev); 5862 else 5863 error = EBUSY; 5864 if (error) 5865 break; 5866 } 5867 5868 dev->flags &= ~DF_FIXEDCLASS; 5869 dev->flags |= DF_WILDCARD; 5870 devclass_delete_device(dev->devclass, dev); 5871 error = device_probe_and_attach(dev); 5872 break; 5873 case DEV_RESCAN: 5874 if (!device_is_attached(dev)) { 5875 error = ENXIO; 5876 break; 5877 } 5878 error = BUS_RESCAN(dev); 5879 break; 5880 case DEV_DELETE: { 5881 device_t parent; 5882 5883 parent = device_get_parent(dev); 5884 if (parent == NULL) { 5885 error = EINVAL; 5886 break; 5887 } 5888 if (!(req->dr_flags & DEVF_FORCE_DELETE)) { 5889 if (bus_child_present(dev) != 0) { 5890 error = EBUSY; 5891 break; 5892 } 5893 } 5894 5895 error = device_delete_child(parent, dev); 5896 break; 5897 } 5898 case DEV_FREEZE: 5899 if (device_frozen) 5900 error = EBUSY; 5901 else 5902 device_frozen = true; 5903 break; 5904 case DEV_THAW: 5905 if (!device_frozen) 5906 error = EBUSY; 5907 else { 5908 device_do_deferred_actions(); 5909 device_frozen = false; 5910 } 5911 break; 5912 case DEV_RESET: 5913 if ((req->dr_flags & ~(DEVF_RESET_DETACH)) != 0) { 5914 error = EINVAL; 5915 break; 5916 } 5917 error = BUS_RESET_CHILD(device_get_parent(dev), dev, 5918 req->dr_flags); 5919 break; 5920 } 5921 mtx_unlock(&Giant); 5922 return (error); 5923 } 5924 5925 static struct cdevsw devctl2_cdevsw = { 5926 .d_version = D_VERSION, 5927 .d_ioctl = devctl2_ioctl, 5928 .d_name = "devctl2", 5929 }; 5930 5931 static void 5932 devctl2_init(void) 5933 { 5934 make_dev_credf(MAKEDEV_ETERNAL, &devctl2_cdevsw, 0, NULL, 5935 UID_ROOT, GID_WHEEL, 0600, "devctl2"); 5936 } 5937 5938 /* 5939 * APIs to manage deprecation and obsolescence. 5940 */ 5941 static int obsolete_panic = 0; 5942 SYSCTL_INT(_debug, OID_AUTO, obsolete_panic, CTLFLAG_RWTUN, &obsolete_panic, 0, 5943 "Panic when obsolete features are used (0 = never, 1 = if osbolete, " 5944 "2 = if deprecated)"); 5945 5946 static void 5947 gone_panic(int major, int running, const char *msg) 5948 { 5949 switch (obsolete_panic) 5950 { 5951 case 0: 5952 return; 5953 case 1: 5954 if (running < major) 5955 return; 5956 /* FALLTHROUGH */ 5957 default: 5958 panic("%s", msg); 5959 } 5960 } 5961 5962 void 5963 _gone_in(int major, const char *msg) 5964 { 5965 gone_panic(major, P_OSREL_MAJOR(__FreeBSD_version), msg); 5966 if (P_OSREL_MAJOR(__FreeBSD_version) >= major) 5967 printf("Obsolete code will be removed soon: %s\n", msg); 5968 else 5969 printf("Deprecated code (to be removed in FreeBSD %d): %s\n", 5970 major, msg); 5971 } 5972 5973 void 5974 _gone_in_dev(device_t dev, int major, const char *msg) 5975 { 5976 gone_panic(major, P_OSREL_MAJOR(__FreeBSD_version), msg); 5977 if (P_OSREL_MAJOR(__FreeBSD_version) >= major) 5978 device_printf(dev, 5979 "Obsolete code will be removed soon: %s\n", msg); 5980 else 5981 device_printf(dev, 5982 "Deprecated code (to be removed in FreeBSD %d): %s\n", 5983 major, msg); 5984 } 5985 5986 #ifdef DDB 5987 DB_SHOW_COMMAND(device, db_show_device) 5988 { 5989 device_t dev; 5990 5991 if (!have_addr) 5992 return; 5993 5994 dev = (device_t)addr; 5995 5996 db_printf("name: %s\n", device_get_nameunit(dev)); 5997 db_printf(" driver: %s\n", DRIVERNAME(dev->driver)); 5998 db_printf(" class: %s\n", DEVCLANAME(dev->devclass)); 5999 db_printf(" addr: %p\n", dev); 6000 db_printf(" parent: %p\n", dev->parent); 6001 db_printf(" softc: %p\n", dev->softc); 6002 db_printf(" ivars: %p\n", dev->ivars); 6003 } 6004 6005 DB_SHOW_ALL_COMMAND(devices, db_show_all_devices) 6006 { 6007 device_t dev; 6008 6009 TAILQ_FOREACH(dev, &bus_data_devices, devlink) { 6010 db_show_device((db_expr_t)dev, true, count, modif); 6011 } 6012 } 6013 #endif 6014