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