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