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