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