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