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