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