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