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