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