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