1 /* $NetBSD: subr_autoconf.c,v 1.56 2001/05/28 16:40:31 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1996, 2000 Christopher G. Demetriou 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed for the 18 * NetBSD Project. See http://www.netbsd.org/ for 19 * information about NetBSD. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 * 34 * --(license Id: LICENSE.proto,v 1.1 2000/06/13 21:40:26 cgd Exp )-- 35 */ 36 37 /* 38 * Copyright (c) 1992, 1993 39 * The Regents of the University of California. All rights reserved. 40 * 41 * This software was developed by the Computer Systems Engineering group 42 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 43 * contributed to Berkeley. 44 * 45 * All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the University of 48 * California, Lawrence Berkeley Laboratories. 49 * 50 * Redistribution and use in source and binary forms, with or without 51 * modification, are permitted provided that the following conditions 52 * are met: 53 * 1. Redistributions of source code must retain the above copyright 54 * notice, this list of conditions and the following disclaimer. 55 * 2. Redistributions in binary form must reproduce the above copyright 56 * notice, this list of conditions and the following disclaimer in the 57 * documentation and/or other materials provided with the distribution. 58 * 3. All advertising materials mentioning features or use of this software 59 * must display the following acknowledgement: 60 * This product includes software developed by the University of 61 * California, Berkeley and its contributors. 62 * 4. Neither the name of the University nor the names of its contributors 63 * may be used to endorse or promote products derived from this software 64 * without specific prior written permission. 65 * 66 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 67 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 68 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 69 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 70 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 71 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 72 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 73 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 74 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 75 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 76 * SUCH DAMAGE. 77 * 78 * from: Header: subr_autoconf.c,v 1.12 93/02/01 19:31:48 torek Exp (LBL) 79 * 80 * @(#)subr_autoconf.c 8.3 (Berkeley) 5/17/94 81 */ 82 83 #include <sys/cdefs.h> 84 85 __KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.56 2001/05/28 16:40:31 thorpej Exp $"); 86 87 #include <sys/param.h> 88 #include <sys/device.h> 89 #include <sys/malloc.h> 90 #include <sys/systm.h> 91 #include <sys/kernel.h> 92 #include <sys/errno.h> 93 #include <sys/proc.h> 94 #include <machine/limits.h> 95 96 /* 97 * Autoconfiguration subroutines. 98 */ 99 100 /* 101 * ioconf.c exports exactly two names: cfdata and cfroots. All system 102 * devices and drivers are found via these tables. 103 */ 104 extern struct cfdata cfdata[]; 105 extern short cfroots[]; 106 107 #define ROOT ((struct device *)NULL) 108 109 struct matchinfo { 110 cfmatch_t fn; 111 struct device *parent; 112 void *aux; 113 struct cfdata *match; 114 int pri; 115 }; 116 117 static char *number(char *, int); 118 static void mapply(struct matchinfo *, struct cfdata *); 119 120 struct deferred_config { 121 TAILQ_ENTRY(deferred_config) dc_queue; 122 struct device *dc_dev; 123 void (*dc_func)(struct device *); 124 }; 125 126 TAILQ_HEAD(deferred_config_head, deferred_config); 127 128 struct deferred_config_head deferred_config_queue; 129 struct deferred_config_head interrupt_config_queue; 130 131 static void config_process_deferred(struct deferred_config_head *, 132 struct device *); 133 134 /* list of all devices */ 135 struct devicelist alldevs; 136 137 /* list of all events */ 138 struct evcntlist allevents = TAILQ_HEAD_INITIALIZER(allevents); 139 140 __volatile int config_pending; /* semaphore for mountroot */ 141 142 /* 143 * Configure the system's hardware. 144 */ 145 void 146 configure(void) 147 { 148 149 TAILQ_INIT(&deferred_config_queue); 150 TAILQ_INIT(&interrupt_config_queue); 151 TAILQ_INIT(&alldevs); 152 153 /* 154 * Do the machine-dependent portion of autoconfiguration. This 155 * sets the configuration machinery here in motion by "finding" 156 * the root bus. When this function returns, we expect interrupts 157 * to be enabled. 158 */ 159 cpu_configure(); 160 161 /* 162 * Now that we've found all the hardware, start the real time 163 * and statistics clocks. 164 */ 165 initclocks(); 166 167 cold = 0; /* clocks are running, we're warm now! */ 168 169 /* 170 * Now callback to finish configuration for devices which want 171 * to do this once interrupts are enabled. 172 */ 173 config_process_deferred(&interrupt_config_queue, NULL); 174 } 175 176 /* 177 * Apply the matching function and choose the best. This is used 178 * a few times and we want to keep the code small. 179 */ 180 static void 181 mapply(struct matchinfo *m, struct cfdata *cf) 182 { 183 int pri; 184 185 if (m->fn != NULL) 186 pri = (*m->fn)(m->parent, cf, m->aux); 187 else { 188 if (cf->cf_attach->ca_match == NULL) { 189 panic("mapply: no match function for '%s' device\n", 190 cf->cf_driver->cd_name); 191 } 192 pri = (*cf->cf_attach->ca_match)(m->parent, cf, m->aux); 193 } 194 if (pri > m->pri) { 195 m->match = cf; 196 m->pri = pri; 197 } 198 } 199 200 /* 201 * Iterate over all potential children of some device, calling the given 202 * function (default being the child's match function) for each one. 203 * Nonzero returns are matches; the highest value returned is considered 204 * the best match. Return the `found child' if we got a match, or NULL 205 * otherwise. The `aux' pointer is simply passed on through. 206 * 207 * Note that this function is designed so that it can be used to apply 208 * an arbitrary function to all potential children (its return value 209 * can be ignored). 210 */ 211 struct cfdata * 212 config_search(cfmatch_t fn, struct device *parent, void *aux) 213 { 214 struct cfdata *cf; 215 short *p; 216 struct matchinfo m; 217 218 m.fn = fn; 219 m.parent = parent; 220 m.aux = aux; 221 m.match = NULL; 222 m.pri = 0; 223 for (cf = cfdata; cf->cf_driver; cf++) { 224 /* 225 * Skip cf if no longer eligible, otherwise scan through 226 * parents for one matching `parent', and try match function. 227 */ 228 if (cf->cf_fstate == FSTATE_FOUND) 229 continue; 230 for (p = cf->cf_parents; *p >= 0; p++) 231 if (parent->dv_cfdata == &cfdata[*p]) 232 mapply(&m, cf); 233 } 234 return (m.match); 235 } 236 237 /* 238 * Find the given root device. 239 * This is much like config_search, but there is no parent. 240 */ 241 struct cfdata * 242 config_rootsearch(cfmatch_t fn, const char *rootname, void *aux) 243 { 244 struct cfdata *cf; 245 short *p; 246 struct matchinfo m; 247 248 m.fn = fn; 249 m.parent = ROOT; 250 m.aux = aux; 251 m.match = NULL; 252 m.pri = 0; 253 /* 254 * Look at root entries for matching name. We do not bother 255 * with found-state here since only one root should ever be 256 * searched (and it must be done first). 257 */ 258 for (p = cfroots; *p >= 0; p++) { 259 cf = &cfdata[*p]; 260 if (strcmp(cf->cf_driver->cd_name, rootname) == 0) 261 mapply(&m, cf); 262 } 263 return (m.match); 264 } 265 266 static const char *msgs[3] = { "", " not configured\n", " unsupported\n" }; 267 268 /* 269 * The given `aux' argument describes a device that has been found 270 * on the given parent, but not necessarily configured. Locate the 271 * configuration data for that device (using the submatch function 272 * provided, or using candidates' cd_match configuration driver 273 * functions) and attach it, and return true. If the device was 274 * not configured, call the given `print' function and return 0. 275 */ 276 struct device * 277 config_found_sm(struct device *parent, void *aux, cfprint_t print, 278 cfmatch_t submatch) 279 { 280 struct cfdata *cf; 281 282 if ((cf = config_search(submatch, parent, aux)) != NULL) 283 return (config_attach(parent, cf, aux, print)); 284 if (print) 285 printf("%s", msgs[(*print)(aux, parent->dv_xname)]); 286 return (NULL); 287 } 288 289 /* 290 * As above, but for root devices. 291 */ 292 struct device * 293 config_rootfound(const char *rootname, void *aux) 294 { 295 struct cfdata *cf; 296 297 if ((cf = config_rootsearch((cfmatch_t)NULL, rootname, aux)) != NULL) 298 return (config_attach(ROOT, cf, aux, (cfprint_t)NULL)); 299 printf("root device %s not configured\n", rootname); 300 return (NULL); 301 } 302 303 /* just like sprintf(buf, "%d") except that it works from the end */ 304 static char * 305 number(char *ep, int n) 306 { 307 308 *--ep = 0; 309 while (n >= 10) { 310 *--ep = (n % 10) + '0'; 311 n /= 10; 312 } 313 *--ep = n + '0'; 314 return (ep); 315 } 316 317 /* 318 * Attach a found device. Allocates memory for device variables. 319 */ 320 struct device * 321 config_attach(struct device *parent, struct cfdata *cf, void *aux, 322 cfprint_t print) 323 { 324 struct device *dev; 325 struct cfdriver *cd; 326 struct cfattach *ca; 327 size_t lname, lunit; 328 const char *xunit; 329 int myunit; 330 char num[10]; 331 332 cd = cf->cf_driver; 333 ca = cf->cf_attach; 334 if (ca->ca_devsize < sizeof(struct device)) 335 panic("config_attach"); 336 #ifndef __BROKEN_CONFIG_UNIT_USAGE 337 if (cf->cf_fstate == FSTATE_STAR) { 338 for (myunit = cf->cf_unit; myunit < cd->cd_ndevs; myunit++) 339 if (cd->cd_devs[myunit] == NULL) 340 break; 341 /* 342 * myunit is now the unit of the first NULL device pointer, 343 * or max(cd->cd_ndevs,cf->cf_unit). 344 */ 345 } else { 346 myunit = cf->cf_unit; 347 #else /* __BROKEN_CONFIG_UNIT_USAGE */ 348 myunit = cf->cf_unit; 349 if (cf->cf_fstate == FSTATE_STAR) 350 cf->cf_unit++; 351 else { 352 #endif /* __BROKEN_CONFIG_UNIT_USAGE */ 353 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND); 354 cf->cf_fstate = FSTATE_FOUND; 355 } 356 357 /* compute length of name and decimal expansion of unit number */ 358 lname = strlen(cd->cd_name); 359 xunit = number(&num[sizeof(num)], myunit); 360 lunit = &num[sizeof(num)] - xunit; 361 if (lname + lunit >= sizeof(dev->dv_xname)) 362 panic("config_attach: device name too long"); 363 364 /* get memory for all device vars */ 365 dev = (struct device *)malloc(ca->ca_devsize, M_DEVBUF, 366 cold ? M_NOWAIT : M_WAITOK); 367 if (!dev) 368 panic("config_attach: memory allocation for device softc failed"); 369 memset(dev, 0, ca->ca_devsize); 370 TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */ 371 dev->dv_class = cd->cd_class; 372 dev->dv_cfdata = cf; 373 dev->dv_unit = myunit; 374 memcpy(dev->dv_xname, cd->cd_name, lname); 375 memcpy(dev->dv_xname + lname, xunit, lunit); 376 dev->dv_parent = parent; 377 dev->dv_flags = DVF_ACTIVE; /* always initially active */ 378 379 if (parent == ROOT) 380 printf("%s (root)", dev->dv_xname); 381 else { 382 printf("%s at %s", dev->dv_xname, parent->dv_xname); 383 if (print) 384 (void) (*print)(aux, NULL); 385 } 386 387 /* put this device in the devices array */ 388 if (dev->dv_unit >= cd->cd_ndevs) { 389 /* 390 * Need to expand the array. 391 */ 392 int old = cd->cd_ndevs, new; 393 void **nsp; 394 395 if (old == 0) 396 new = MINALLOCSIZE / sizeof(void *); 397 else 398 new = old * 2; 399 while (new <= dev->dv_unit) 400 new *= 2; 401 cd->cd_ndevs = new; 402 nsp = malloc(new * sizeof(void *), M_DEVBUF, 403 cold ? M_NOWAIT : M_WAITOK); 404 if (nsp == 0) 405 panic("config_attach: %sing dev array", 406 old != 0 ? "expand" : "creat"); 407 memset(nsp + old, 0, (new - old) * sizeof(void *)); 408 if (old != 0) { 409 memcpy(nsp, cd->cd_devs, old * sizeof(void *)); 410 free(cd->cd_devs, M_DEVBUF); 411 } 412 cd->cd_devs = nsp; 413 } 414 if (cd->cd_devs[dev->dv_unit]) 415 panic("config_attach: duplicate %s", dev->dv_xname); 416 cd->cd_devs[dev->dv_unit] = dev; 417 418 /* 419 * Before attaching, clobber any unfound devices that are 420 * otherwise identical. 421 */ 422 #ifdef __BROKEN_CONFIG_UNIT_USAGE 423 /* bump the unit number on all starred cfdata for this device. */ 424 #endif /* __BROKEN_CONFIG_UNIT_USAGE */ 425 for (cf = cfdata; cf->cf_driver; cf++) 426 if (cf->cf_driver == cd && cf->cf_unit == dev->dv_unit) { 427 if (cf->cf_fstate == FSTATE_NOTFOUND) 428 cf->cf_fstate = FSTATE_FOUND; 429 #ifdef __BROKEN_CONFIG_UNIT_USAGE 430 if (cf->cf_fstate == FSTATE_STAR) 431 cf->cf_unit++; 432 #endif /* __BROKEN_CONFIG_UNIT_USAGE */ 433 } 434 #ifdef __HAVE_DEVICE_REGISTER 435 device_register(dev, aux); 436 #endif 437 (*ca->ca_attach)(parent, dev, aux); 438 config_process_deferred(&deferred_config_queue, dev); 439 return (dev); 440 } 441 442 /* 443 * Detach a device. Optionally forced (e.g. because of hardware 444 * removal) and quiet. Returns zero if successful, non-zero 445 * (an error code) otherwise. 446 * 447 * Note that this code wants to be run from a process context, so 448 * that the detach can sleep to allow processes which have a device 449 * open to run and unwind their stacks. 450 */ 451 int 452 config_detach(struct device *dev, int flags) 453 { 454 struct cfdata *cf; 455 struct cfattach *ca; 456 struct cfdriver *cd; 457 #ifdef DIAGNOSTIC 458 struct device *d; 459 #endif 460 int rv = 0, i; 461 462 cf = dev->dv_cfdata; 463 #ifdef DIAGNOSTIC 464 if (cf->cf_fstate != FSTATE_FOUND && cf->cf_fstate != FSTATE_STAR) 465 panic("config_detach: bad device fstate"); 466 #endif 467 ca = cf->cf_attach; 468 cd = cf->cf_driver; 469 470 /* 471 * Ensure the device is deactivated. If the device doesn't 472 * have an activation entry point, we allow DVF_ACTIVE to 473 * remain set. Otherwise, if DVF_ACTIVE is still set, the 474 * device is busy, and the detach fails. 475 */ 476 if (ca->ca_activate != NULL) 477 rv = config_deactivate(dev); 478 479 /* 480 * Try to detach the device. If that's not possible, then 481 * we either panic() (for the forced but failed case), or 482 * return an error. 483 */ 484 if (rv == 0) { 485 if (ca->ca_detach != NULL) 486 rv = (*ca->ca_detach)(dev, flags); 487 else 488 rv = EOPNOTSUPP; 489 } 490 if (rv != 0) { 491 if ((flags & DETACH_FORCE) == 0) 492 return (rv); 493 else 494 panic("config_detach: forced detach of %s failed (%d)", 495 dev->dv_xname, rv); 496 } 497 498 /* 499 * The device has now been successfully detached. 500 */ 501 502 #ifdef DIAGNOSTIC 503 /* 504 * Sanity: If you're successfully detached, you should have no 505 * children. (Note that because children must be attached 506 * after parents, we only need to search the latter part of 507 * the list.) 508 */ 509 for (d = TAILQ_NEXT(dev, dv_list); d != NULL; 510 d = TAILQ_NEXT(d, dv_list)) { 511 if (d->dv_parent == dev) { 512 printf("config_detach: detached device %s" 513 " has children %s\n", dev->dv_xname, d->dv_xname); 514 panic("config_detach"); 515 } 516 } 517 #endif 518 519 /* 520 * Mark cfdata to show that the unit can be reused, if possible. 521 */ 522 #ifdef __BROKEN_CONFIG_UNIT_USAGE 523 /* 524 * Note that we can only re-use a starred unit number if the unit 525 * being detached had the last assigned unit number. 526 */ 527 #endif /* __BROKEN_CONFIG_UNIT_USAGE */ 528 for (cf = cfdata; cf->cf_driver; cf++) { 529 if (cf->cf_driver == cd) { 530 if (cf->cf_fstate == FSTATE_FOUND && 531 cf->cf_unit == dev->dv_unit) 532 cf->cf_fstate = FSTATE_NOTFOUND; 533 #ifdef __BROKEN_CONFIG_UNIT_USAGE 534 if (cf->cf_fstate == FSTATE_STAR && 535 cf->cf_unit == dev->dv_unit + 1) 536 cf->cf_unit--; 537 #endif /* __BROKEN_CONFIG_UNIT_USAGE */ 538 } 539 } 540 541 /* 542 * Unlink from device list. 543 */ 544 TAILQ_REMOVE(&alldevs, dev, dv_list); 545 546 /* 547 * Remove from cfdriver's array, tell the world, and free softc. 548 */ 549 cd->cd_devs[dev->dv_unit] = NULL; 550 if ((flags & DETACH_QUIET) == 0) 551 printf("%s detached\n", dev->dv_xname); 552 free(dev, M_DEVBUF); 553 554 /* 555 * If the device now has no units in use, deallocate its softc array. 556 */ 557 for (i = 0; i < cd->cd_ndevs; i++) 558 if (cd->cd_devs[i] != NULL) 559 break; 560 if (i == cd->cd_ndevs) { /* nothing found; deallocate */ 561 free(cd->cd_devs, M_DEVBUF); 562 cd->cd_devs = NULL; 563 cd->cd_ndevs = 0; 564 } 565 566 /* 567 * Return success. 568 */ 569 return (0); 570 } 571 572 int 573 config_activate(struct device *dev) 574 { 575 struct cfattach *ca = dev->dv_cfdata->cf_attach; 576 int rv = 0, oflags = dev->dv_flags; 577 578 if (ca->ca_activate == NULL) 579 return (EOPNOTSUPP); 580 581 if ((dev->dv_flags & DVF_ACTIVE) == 0) { 582 dev->dv_flags |= DVF_ACTIVE; 583 rv = (*ca->ca_activate)(dev, DVACT_ACTIVATE); 584 if (rv) 585 dev->dv_flags = oflags; 586 } 587 return (rv); 588 } 589 590 int 591 config_deactivate(struct device *dev) 592 { 593 struct cfattach *ca = dev->dv_cfdata->cf_attach; 594 int rv = 0, oflags = dev->dv_flags; 595 596 if (ca->ca_activate == NULL) 597 return (EOPNOTSUPP); 598 599 if (dev->dv_flags & DVF_ACTIVE) { 600 dev->dv_flags &= ~DVF_ACTIVE; 601 rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE); 602 if (rv) 603 dev->dv_flags = oflags; 604 } 605 return (rv); 606 } 607 608 /* 609 * Defer the configuration of the specified device until all 610 * of its parent's devices have been attached. 611 */ 612 void 613 config_defer(struct device *dev, void (*func)(struct device *)) 614 { 615 struct deferred_config *dc; 616 617 if (dev->dv_parent == NULL) 618 panic("config_defer: can't defer config of a root device"); 619 620 #ifdef DIAGNOSTIC 621 for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL; 622 dc = TAILQ_NEXT(dc, dc_queue)) { 623 if (dc->dc_dev == dev) 624 panic("config_defer: deferred twice"); 625 } 626 #endif 627 628 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK); 629 if (dc == NULL) 630 panic("config_defer: unable to allocate callback"); 631 632 dc->dc_dev = dev; 633 dc->dc_func = func; 634 TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue); 635 config_pending_incr(); 636 } 637 638 /* 639 * Defer some autoconfiguration for a device until after interrupts 640 * are enabled. 641 */ 642 void 643 config_interrupts(struct device *dev, void (*func)(struct device *)) 644 { 645 struct deferred_config *dc; 646 647 /* 648 * If interrupts are enabled, callback now. 649 */ 650 if (cold == 0) { 651 (*func)(dev); 652 return; 653 } 654 655 #ifdef DIAGNOSTIC 656 for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL; 657 dc = TAILQ_NEXT(dc, dc_queue)) { 658 if (dc->dc_dev == dev) 659 panic("config_interrupts: deferred twice"); 660 } 661 #endif 662 663 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK); 664 if (dc == NULL) 665 panic("config_interrupts: unable to allocate callback"); 666 667 dc->dc_dev = dev; 668 dc->dc_func = func; 669 TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue); 670 config_pending_incr(); 671 } 672 673 /* 674 * Process a deferred configuration queue. 675 */ 676 static void 677 config_process_deferred(struct deferred_config_head *queue, 678 struct device *parent) 679 { 680 struct deferred_config *dc, *ndc; 681 682 for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) { 683 ndc = TAILQ_NEXT(dc, dc_queue); 684 if (parent == NULL || dc->dc_dev->dv_parent == parent) { 685 TAILQ_REMOVE(queue, dc, dc_queue); 686 (*dc->dc_func)(dc->dc_dev); 687 free(dc, M_DEVBUF); 688 config_pending_decr(); 689 } 690 } 691 } 692 693 /* 694 * Manipulate the config_pending semaphore. 695 */ 696 void 697 config_pending_incr(void) 698 { 699 700 config_pending++; 701 } 702 703 void 704 config_pending_decr(void) 705 { 706 707 #ifdef DIAGNOSTIC 708 if (config_pending == 0) 709 panic("config_pending_decr: config_pending == 0"); 710 #endif 711 config_pending--; 712 if (config_pending == 0) 713 wakeup((void *)&config_pending); 714 } 715 716 /* 717 * Attach a statically-initialized event. The type and string pointers 718 * are already set up. 719 */ 720 void 721 evcnt_attach_static(struct evcnt *ev) 722 { 723 int len; 724 725 len = strlen(ev->ev_group); 726 #ifdef DIAGNOSTIC 727 if (len >= EVCNT_STRING_MAX) /* ..._MAX includes NUL */ 728 panic("evcnt_attach_static: group length (%s)", ev->ev_group); 729 #endif 730 ev->ev_grouplen = len; 731 732 len = strlen(ev->ev_name); 733 #ifdef DIAGNOSTIC 734 if (len >= EVCNT_STRING_MAX) /* ..._MAX includes NUL */ 735 panic("evcnt_attach_static: name length (%s)", ev->ev_name); 736 #endif 737 ev->ev_namelen = len; 738 739 TAILQ_INSERT_TAIL(&allevents, ev, ev_list); 740 } 741 742 /* 743 * Attach a dynamically-initialized event. Zero it, set up the type 744 * and string pointers and then act like it was statically initialized. 745 */ 746 void 747 evcnt_attach_dynamic(struct evcnt *ev, int type, const struct evcnt *parent, 748 const char *group, const char *name) 749 { 750 751 memset(ev, 0, sizeof *ev); 752 ev->ev_type = type; 753 ev->ev_parent = parent; 754 ev->ev_group = group; 755 ev->ev_name = name; 756 evcnt_attach_static(ev); 757 } 758 759 /* 760 * Detach an event. 761 */ 762 void 763 evcnt_detach(struct evcnt *ev) 764 { 765 766 TAILQ_REMOVE(&allevents, ev, ev_list); 767 } 768