subr_autoconf.c revision 1.67 1 /* $NetBSD: subr_autoconf.c,v 1.67 2002/09/27 02:24:33 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 __KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.67 2002/09/27 02:24:33 thorpej Exp $");
85
86 #include "opt_ddb.h"
87
88 #include <sys/param.h>
89 #include <sys/device.h>
90 #include <sys/malloc.h>
91 #include <sys/systm.h>
92 #include <sys/kernel.h>
93 #include <sys/errno.h>
94 #include <sys/proc.h>
95 #include <machine/limits.h>
96
97 #include "opt_userconf.h"
98 #ifdef USERCONF
99 #include <sys/userconf.h>
100 #include <sys/reboot.h>
101 #endif
102
103 /*
104 * Autoconfiguration subroutines.
105 */
106
107 /*
108 * ioconf.c exports exactly two names: cfdata and cfroots. All system
109 * devices and drivers are found via these tables.
110 */
111 extern struct cfdata cfdata[];
112 extern short cfroots[];
113
114 /*
115 * List of all cfdriver structures. We use this to detect duplicates
116 * when other cfdrivers are loaded.
117 */
118 struct cfdriverlist allcfdrivers;
119
120 /*
121 * List of cfdata tables. We always have one such list -- the one
122 * built statically when the kernel was configured.
123 */
124 struct cftablelist allcftables;
125 static struct cftable initcftable;
126
127 #define ROOT ((struct device *)NULL)
128
129 struct matchinfo {
130 cfmatch_t fn;
131 struct device *parent;
132 void *aux;
133 struct cfdata *match;
134 int pri;
135 };
136
137 static char *number(char *, int);
138 static void mapply(struct matchinfo *, struct cfdata *);
139
140 struct deferred_config {
141 TAILQ_ENTRY(deferred_config) dc_queue;
142 struct device *dc_dev;
143 void (*dc_func)(struct device *);
144 };
145
146 TAILQ_HEAD(deferred_config_head, deferred_config);
147
148 struct deferred_config_head deferred_config_queue;
149 struct deferred_config_head interrupt_config_queue;
150
151 static void config_process_deferred(struct deferred_config_head *,
152 struct device *);
153
154 /* list of all devices */
155 struct devicelist alldevs;
156
157 /* list of all events */
158 struct evcntlist allevents = TAILQ_HEAD_INITIALIZER(allevents);
159
160 __volatile int config_pending; /* semaphore for mountroot */
161
162 #define STREQ(s1, s2) \
163 ((s1)[0] == (s2)[0] && strcmp((s1), (s2)) == 0)
164
165 /*
166 * Configure the system's hardware.
167 */
168 void
169 configure(void)
170 {
171 extern struct cfdriver * const cfdriver_list_initial[];
172 int i;
173
174 LIST_INIT(&allcfdrivers);
175 for (i = 0; cfdriver_list_initial[i] != NULL; i++)
176 if (config_cfdriver_attach(cfdriver_list_initial[i]) != 0)
177 panic("configure: duplicate `%s' drivers",
178 cfdriver_list_initial[i]->cd_name);
179
180 TAILQ_INIT(&allcftables);
181 initcftable.ct_cfdata = cfdata;
182 TAILQ_INSERT_TAIL(&allcftables, &initcftable, ct_list);
183
184 TAILQ_INIT(&deferred_config_queue);
185 TAILQ_INIT(&interrupt_config_queue);
186 TAILQ_INIT(&alldevs);
187
188 #ifdef USERCONF
189 if (boothowto & RB_USERCONF)
190 user_config();
191 #endif
192
193 /*
194 * Do the machine-dependent portion of autoconfiguration. This
195 * sets the configuration machinery here in motion by "finding"
196 * the root bus. When this function returns, we expect interrupts
197 * to be enabled.
198 */
199 cpu_configure();
200
201 /*
202 * Now that we've found all the hardware, start the real time
203 * and statistics clocks.
204 */
205 initclocks();
206
207 cold = 0; /* clocks are running, we're warm now! */
208
209 /*
210 * Now callback to finish configuration for devices which want
211 * to do this once interrupts are enabled.
212 */
213 config_process_deferred(&interrupt_config_queue, NULL);
214 }
215
216 /*
217 * Add a cfdriver to the system.
218 */
219 int
220 config_cfdriver_attach(struct cfdriver *cd)
221 {
222 struct cfdriver *lcd;
223
224 /* Make sure this driver isn't already in the system. */
225 LIST_FOREACH(lcd, &allcfdrivers, cd_list) {
226 if (STREQ(lcd->cd_name, cd->cd_name))
227 return (EEXIST);
228 }
229
230 LIST_INSERT_HEAD(&allcfdrivers, cd, cd_list);
231
232 return (0);
233 }
234
235 /*
236 * Remove a cfdriver from the system.
237 */
238 int
239 config_cfdriver_detach(struct cfdriver *cd)
240 {
241 int i;
242
243 /* Make sure there are no active instances. */
244 for (i = 0; i < cd->cd_ndevs; i++) {
245 if (cd->cd_devs[i] != NULL)
246 return (EBUSY);
247 }
248
249 LIST_REMOVE(cd, cd_list);
250
251 KASSERT(cd->cd_devs == NULL);
252
253 return (0);
254 }
255
256 /*
257 * Look up a cfdriver by name.
258 */
259 static struct cfdriver *
260 config_cfdriver_lookup(const char *name)
261 {
262 struct cfdriver *cd;
263
264 LIST_FOREACH(cd, &allcfdrivers, cd_list) {
265 if (STREQ(cd->cd_name, name))
266 return (cd);
267 }
268
269 return (NULL);
270 }
271
272 /*
273 * Apply the matching function and choose the best. This is used
274 * a few times and we want to keep the code small.
275 */
276 static void
277 mapply(struct matchinfo *m, struct cfdata *cf)
278 {
279 int pri;
280
281 if (m->fn != NULL)
282 pri = (*m->fn)(m->parent, cf, m->aux);
283 else {
284 if (cf->cf_attach->ca_match == NULL) {
285 panic("mapply: no match function for '%s' device\n",
286 cf->cf_name);
287 }
288 pri = (*cf->cf_attach->ca_match)(m->parent, cf, m->aux);
289 }
290 if (pri > m->pri) {
291 m->match = cf;
292 m->pri = pri;
293 }
294 }
295
296 /*
297 * Determine if `parent' is a potential parent for a device spec based
298 * on `cfp'.
299 */
300 static int
301 cfparent_match(struct device *parent, const struct cfparent *cfp)
302 {
303 struct cfdriver *pcd;
304 const char * const *cpp;
305 const char *cp;
306
307 pcd = config_cfdriver_lookup(parent->dv_cfdata->cf_name);
308 KASSERT(pcd != NULL);
309
310 /*
311 * First, ensure this parent has the correct interface
312 * attribute.
313 */
314 if (pcd->cd_attrs == NULL)
315 return (0); /* no interface attributes -> no children */
316 for (cpp = pcd->cd_attrs; (cp = *cpp) != NULL; cpp++) {
317 if (STREQ(cp, cfp->cfp_iattr)) {
318 /* Match. */
319 break;
320 }
321 }
322 if (cp == NULL)
323 return (0); /* doesn't carry the req'd attribute */
324
325 /*
326 * If no specific parent device instance was specified (i.e.
327 * we're attaching to the attribute only), we're done!
328 */
329 if (cfp->cfp_parent == NULL)
330 return (1);
331
332 /*
333 * Check the parent device's name.
334 */
335 if (pcd->cd_name[0] != cfp->cfp_parent[0] ||
336 strcmp(pcd->cd_name, cfp->cfp_parent) != 0)
337 return (0); /* not the same parent */
338
339 /*
340 * Make sure the unit number matches.
341 */
342 if (cfp->cfp_unit == -1 || /* wildcard */
343 cfp->cfp_unit == parent->dv_unit)
344 return (1);
345
346 /* Unit numbers don't match. */
347 return (0);
348 }
349
350 /*
351 * Iterate over all potential children of some device, calling the given
352 * function (default being the child's match function) for each one.
353 * Nonzero returns are matches; the highest value returned is considered
354 * the best match. Return the `found child' if we got a match, or NULL
355 * otherwise. The `aux' pointer is simply passed on through.
356 *
357 * Note that this function is designed so that it can be used to apply
358 * an arbitrary function to all potential children (its return value
359 * can be ignored).
360 */
361 struct cfdata *
362 config_search(cfmatch_t fn, struct device *parent, void *aux)
363 {
364 struct cftable *ct;
365 struct cfdata *cf;
366 struct matchinfo m;
367
368 m.fn = fn;
369 m.parent = parent;
370 m.aux = aux;
371 m.match = NULL;
372 m.pri = 0;
373
374 TAILQ_FOREACH(ct, &allcftables, ct_list) {
375 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
376 /*
377 * Skip cf if no longer eligible, otherwise scan
378 * through parents for one matching `parent', and
379 * try match function.
380 */
381 if (cf->cf_fstate == FSTATE_FOUND)
382 continue;
383 if (cf->cf_fstate == FSTATE_DNOTFOUND ||
384 cf->cf_fstate == FSTATE_DSTAR)
385 continue;
386 if (cfparent_match(parent, cf->cf_pspec))
387 mapply(&m, cf);
388 }
389 }
390 return (m.match);
391 }
392
393 /*
394 * Find the given root device.
395 * This is much like config_search, but there is no parent.
396 * Don't bother with multiple cfdata tables; the root node
397 * must always be in the initial table.
398 */
399 struct cfdata *
400 config_rootsearch(cfmatch_t fn, const char *rootname, void *aux)
401 {
402 struct cfdata *cf;
403 short *p;
404 struct matchinfo m;
405
406 m.fn = fn;
407 m.parent = ROOT;
408 m.aux = aux;
409 m.match = NULL;
410 m.pri = 0;
411 /*
412 * Look at root entries for matching name. We do not bother
413 * with found-state here since only one root should ever be
414 * searched (and it must be done first).
415 */
416 for (p = cfroots; *p >= 0; p++) {
417 cf = &cfdata[*p];
418 if (strcmp(cf->cf_name, rootname) == 0)
419 mapply(&m, cf);
420 }
421 return (m.match);
422 }
423
424 static const char *msgs[3] = { "", " not configured\n", " unsupported\n" };
425
426 /*
427 * The given `aux' argument describes a device that has been found
428 * on the given parent, but not necessarily configured. Locate the
429 * configuration data for that device (using the submatch function
430 * provided, or using candidates' cd_match configuration driver
431 * functions) and attach it, and return true. If the device was
432 * not configured, call the given `print' function and return 0.
433 */
434 struct device *
435 config_found_sm(struct device *parent, void *aux, cfprint_t print,
436 cfmatch_t submatch)
437 {
438 struct cfdata *cf;
439
440 if ((cf = config_search(submatch, parent, aux)) != NULL)
441 return (config_attach(parent, cf, aux, print));
442 if (print)
443 printf("%s", msgs[(*print)(aux, parent->dv_xname)]);
444 return (NULL);
445 }
446
447 /*
448 * As above, but for root devices.
449 */
450 struct device *
451 config_rootfound(const char *rootname, void *aux)
452 {
453 struct cfdata *cf;
454
455 if ((cf = config_rootsearch((cfmatch_t)NULL, rootname, aux)) != NULL)
456 return (config_attach(ROOT, cf, aux, (cfprint_t)NULL));
457 printf("root device %s not configured\n", rootname);
458 return (NULL);
459 }
460
461 /* just like sprintf(buf, "%d") except that it works from the end */
462 static char *
463 number(char *ep, int n)
464 {
465
466 *--ep = 0;
467 while (n >= 10) {
468 *--ep = (n % 10) + '0';
469 n /= 10;
470 }
471 *--ep = n + '0';
472 return (ep);
473 }
474
475 /*
476 * Expand the size of the cd_devs array if necessary.
477 */
478 void
479 config_makeroom(int n, struct cfdriver *cd)
480 {
481 int old, new;
482 void **nsp;
483
484 if (n < cd->cd_ndevs)
485 return;
486
487 /*
488 * Need to expand the array.
489 */
490 old = cd->cd_ndevs;
491 if (old == 0)
492 new = MINALLOCSIZE / sizeof(void *);
493 else
494 new = old * 2;
495 while (new <= n)
496 new *= 2;
497 cd->cd_ndevs = new;
498 nsp = malloc(new * sizeof(void *), M_DEVBUF,
499 cold ? M_NOWAIT : M_WAITOK);
500 if (nsp == NULL)
501 panic("config_attach: %sing dev array",
502 old != 0 ? "expand" : "creat");
503 memset(nsp + old, 0, (new - old) * sizeof(void *));
504 if (old != 0) {
505 memcpy(nsp, cd->cd_devs, old * sizeof(void *));
506 free(cd->cd_devs, M_DEVBUF);
507 }
508 cd->cd_devs = nsp;
509 }
510
511 /*
512 * Attach a found device. Allocates memory for device variables.
513 */
514 struct device *
515 config_attach(struct device *parent, struct cfdata *cf, void *aux,
516 cfprint_t print)
517 {
518 struct device *dev;
519 struct cftable *ct;
520 struct cfdriver *cd;
521 struct cfattach *ca;
522 size_t lname, lunit;
523 const char *xunit;
524 int myunit;
525 char num[10];
526
527 cd = config_cfdriver_lookup(cf->cf_name);
528 KASSERT(cd != NULL);
529 ca = cf->cf_attach;
530 if (ca->ca_devsize < sizeof(struct device))
531 panic("config_attach");
532
533 #ifndef __BROKEN_CONFIG_UNIT_USAGE
534 if (cf->cf_fstate == FSTATE_STAR) {
535 for (myunit = cf->cf_unit; myunit < cd->cd_ndevs; myunit++)
536 if (cd->cd_devs[myunit] == NULL)
537 break;
538 /*
539 * myunit is now the unit of the first NULL device pointer,
540 * or max(cd->cd_ndevs,cf->cf_unit).
541 */
542 } else {
543 myunit = cf->cf_unit;
544 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
545 cf->cf_fstate = FSTATE_FOUND;
546 }
547 #else
548 myunit = cf->cf_unit;
549 if (cf->cf_fstate == FSTATE_STAR)
550 cf->cf_unit++;
551 else {
552 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
553 cf->cf_fstate = FSTATE_FOUND;
554 }
555 #endif /* ! __BROKEN_CONFIG_UNIT_USAGE */
556
557 /* compute length of name and decimal expansion of unit number */
558 lname = strlen(cd->cd_name);
559 xunit = number(&num[sizeof(num)], myunit);
560 lunit = &num[sizeof(num)] - xunit;
561 if (lname + lunit > sizeof(dev->dv_xname))
562 panic("config_attach: device name too long");
563
564 /* get memory for all device vars */
565 dev = (struct device *)malloc(ca->ca_devsize, M_DEVBUF,
566 cold ? M_NOWAIT : M_WAITOK);
567 if (!dev)
568 panic("config_attach: memory allocation for device softc failed");
569 memset(dev, 0, ca->ca_devsize);
570 TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */
571 dev->dv_class = cd->cd_class;
572 dev->dv_cfdata = cf;
573 dev->dv_unit = myunit;
574 memcpy(dev->dv_xname, cd->cd_name, lname);
575 memcpy(dev->dv_xname + lname, xunit, lunit);
576 dev->dv_parent = parent;
577 dev->dv_flags = DVF_ACTIVE; /* always initially active */
578
579 if (parent == ROOT)
580 printf("%s (root)", dev->dv_xname);
581 else {
582 printf("%s at %s", dev->dv_xname, parent->dv_xname);
583 if (print)
584 (void) (*print)(aux, NULL);
585 }
586
587 /* put this device in the devices array */
588 config_makeroom(dev->dv_unit, cd);
589 if (cd->cd_devs[dev->dv_unit])
590 panic("config_attach: duplicate %s", dev->dv_xname);
591 cd->cd_devs[dev->dv_unit] = dev;
592
593 /*
594 * Before attaching, clobber any unfound devices that are
595 * otherwise identical.
596 */
597 TAILQ_FOREACH(ct, &allcftables, ct_list) {
598 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
599 if (STREQ(cf->cf_name, cd->cd_name) &&
600 cf->cf_unit == dev->dv_unit) {
601 if (cf->cf_fstate == FSTATE_NOTFOUND)
602 cf->cf_fstate = FSTATE_FOUND;
603 #ifdef __BROKEN_CONFIG_UNIT_USAGE
604 /*
605 * Bump the unit number on all starred cfdata
606 * entries for this device.
607 */
608 if (cf->cf_fstate == FSTATE_STAR)
609 cf->cf_unit++;
610 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
611 }
612 }
613 }
614 #ifdef __HAVE_DEVICE_REGISTER
615 device_register(dev, aux);
616 #endif
617 (*ca->ca_attach)(parent, dev, aux);
618 config_process_deferred(&deferred_config_queue, dev);
619 return (dev);
620 }
621
622 /*
623 * Detach a device. Optionally forced (e.g. because of hardware
624 * removal) and quiet. Returns zero if successful, non-zero
625 * (an error code) otherwise.
626 *
627 * Note that this code wants to be run from a process context, so
628 * that the detach can sleep to allow processes which have a device
629 * open to run and unwind their stacks.
630 */
631 int
632 config_detach(struct device *dev, int flags)
633 {
634 struct cftable *ct;
635 struct cfdata *cf;
636 struct cfattach *ca;
637 struct cfdriver *cd;
638 #ifdef DIAGNOSTIC
639 struct device *d;
640 #endif
641 int rv = 0, i;
642
643 cf = dev->dv_cfdata;
644 #ifdef DIAGNOSTIC
645 if (cf->cf_fstate != FSTATE_FOUND && cf->cf_fstate != FSTATE_STAR)
646 panic("config_detach: bad device fstate");
647 #endif
648 cd = config_cfdriver_lookup(cf->cf_name);
649 KASSERT(cd != NULL);
650 ca = cf->cf_attach;
651
652 /*
653 * Ensure the device is deactivated. If the device doesn't
654 * have an activation entry point, we allow DVF_ACTIVE to
655 * remain set. Otherwise, if DVF_ACTIVE is still set, the
656 * device is busy, and the detach fails.
657 */
658 if (ca->ca_activate != NULL)
659 rv = config_deactivate(dev);
660
661 /*
662 * Try to detach the device. If that's not possible, then
663 * we either panic() (for the forced but failed case), or
664 * return an error.
665 */
666 if (rv == 0) {
667 if (ca->ca_detach != NULL)
668 rv = (*ca->ca_detach)(dev, flags);
669 else
670 rv = EOPNOTSUPP;
671 }
672 if (rv != 0) {
673 if ((flags & DETACH_FORCE) == 0)
674 return (rv);
675 else
676 panic("config_detach: forced detach of %s failed (%d)",
677 dev->dv_xname, rv);
678 }
679
680 /*
681 * The device has now been successfully detached.
682 */
683
684 #ifdef DIAGNOSTIC
685 /*
686 * Sanity: If you're successfully detached, you should have no
687 * children. (Note that because children must be attached
688 * after parents, we only need to search the latter part of
689 * the list.)
690 */
691 for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
692 d = TAILQ_NEXT(d, dv_list)) {
693 if (d->dv_parent == dev) {
694 printf("config_detach: detached device %s"
695 " has children %s\n", dev->dv_xname, d->dv_xname);
696 panic("config_detach");
697 }
698 }
699 #endif
700
701 /*
702 * Mark cfdata to show that the unit can be reused, if possible.
703 */
704 TAILQ_FOREACH(ct, &allcftables, ct_list) {
705 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
706 if (STREQ(cf->cf_name, cd->cd_name)) {
707 if (cf->cf_fstate == FSTATE_FOUND &&
708 cf->cf_unit == dev->dv_unit)
709 cf->cf_fstate = FSTATE_NOTFOUND;
710 #ifdef __BROKEN_CONFIG_UNIT_USAGE
711 /*
712 * Note that we can only re-use a starred
713 * unit number if the unit being detached
714 * had the last assigned unit number.
715 */
716 if (cf->cf_fstate == FSTATE_STAR &&
717 cf->cf_unit == dev->dv_unit + 1)
718 cf->cf_unit--;
719 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
720 }
721 }
722 }
723
724 /*
725 * Unlink from device list.
726 */
727 TAILQ_REMOVE(&alldevs, dev, dv_list);
728
729 /*
730 * Remove from cfdriver's array, tell the world, and free softc.
731 */
732 cd->cd_devs[dev->dv_unit] = NULL;
733 if ((flags & DETACH_QUIET) == 0)
734 printf("%s detached\n", dev->dv_xname);
735 free(dev, M_DEVBUF);
736
737 /*
738 * If the device now has no units in use, deallocate its softc array.
739 */
740 for (i = 0; i < cd->cd_ndevs; i++)
741 if (cd->cd_devs[i] != NULL)
742 break;
743 if (i == cd->cd_ndevs) { /* nothing found; deallocate */
744 free(cd->cd_devs, M_DEVBUF);
745 cd->cd_devs = NULL;
746 cd->cd_ndevs = 0;
747 }
748
749 /*
750 * Return success.
751 */
752 return (0);
753 }
754
755 int
756 config_activate(struct device *dev)
757 {
758 struct cfattach *ca = dev->dv_cfdata->cf_attach;
759 int rv = 0, oflags = dev->dv_flags;
760
761 if (ca->ca_activate == NULL)
762 return (EOPNOTSUPP);
763
764 if ((dev->dv_flags & DVF_ACTIVE) == 0) {
765 dev->dv_flags |= DVF_ACTIVE;
766 rv = (*ca->ca_activate)(dev, DVACT_ACTIVATE);
767 if (rv)
768 dev->dv_flags = oflags;
769 }
770 return (rv);
771 }
772
773 int
774 config_deactivate(struct device *dev)
775 {
776 struct cfattach *ca = dev->dv_cfdata->cf_attach;
777 int rv = 0, oflags = dev->dv_flags;
778
779 if (ca->ca_activate == NULL)
780 return (EOPNOTSUPP);
781
782 if (dev->dv_flags & DVF_ACTIVE) {
783 dev->dv_flags &= ~DVF_ACTIVE;
784 rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE);
785 if (rv)
786 dev->dv_flags = oflags;
787 }
788 return (rv);
789 }
790
791 /*
792 * Defer the configuration of the specified device until all
793 * of its parent's devices have been attached.
794 */
795 void
796 config_defer(struct device *dev, void (*func)(struct device *))
797 {
798 struct deferred_config *dc;
799
800 if (dev->dv_parent == NULL)
801 panic("config_defer: can't defer config of a root device");
802
803 #ifdef DIAGNOSTIC
804 for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL;
805 dc = TAILQ_NEXT(dc, dc_queue)) {
806 if (dc->dc_dev == dev)
807 panic("config_defer: deferred twice");
808 }
809 #endif
810
811 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
812 if (dc == NULL)
813 panic("config_defer: unable to allocate callback");
814
815 dc->dc_dev = dev;
816 dc->dc_func = func;
817 TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
818 config_pending_incr();
819 }
820
821 /*
822 * Defer some autoconfiguration for a device until after interrupts
823 * are enabled.
824 */
825 void
826 config_interrupts(struct device *dev, void (*func)(struct device *))
827 {
828 struct deferred_config *dc;
829
830 /*
831 * If interrupts are enabled, callback now.
832 */
833 if (cold == 0) {
834 (*func)(dev);
835 return;
836 }
837
838 #ifdef DIAGNOSTIC
839 for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL;
840 dc = TAILQ_NEXT(dc, dc_queue)) {
841 if (dc->dc_dev == dev)
842 panic("config_interrupts: deferred twice");
843 }
844 #endif
845
846 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
847 if (dc == NULL)
848 panic("config_interrupts: unable to allocate callback");
849
850 dc->dc_dev = dev;
851 dc->dc_func = func;
852 TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue);
853 config_pending_incr();
854 }
855
856 /*
857 * Process a deferred configuration queue.
858 */
859 static void
860 config_process_deferred(struct deferred_config_head *queue,
861 struct device *parent)
862 {
863 struct deferred_config *dc, *ndc;
864
865 for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) {
866 ndc = TAILQ_NEXT(dc, dc_queue);
867 if (parent == NULL || dc->dc_dev->dv_parent == parent) {
868 TAILQ_REMOVE(queue, dc, dc_queue);
869 (*dc->dc_func)(dc->dc_dev);
870 free(dc, M_DEVBUF);
871 config_pending_decr();
872 }
873 }
874 }
875
876 /*
877 * Manipulate the config_pending semaphore.
878 */
879 void
880 config_pending_incr(void)
881 {
882
883 config_pending++;
884 }
885
886 void
887 config_pending_decr(void)
888 {
889
890 #ifdef DIAGNOSTIC
891 if (config_pending == 0)
892 panic("config_pending_decr: config_pending == 0");
893 #endif
894 config_pending--;
895 if (config_pending == 0)
896 wakeup((void *)&config_pending);
897 }
898
899 /*
900 * Attach a statically-initialized event. The type and string pointers
901 * are already set up.
902 */
903 void
904 evcnt_attach_static(struct evcnt *ev)
905 {
906 int len;
907
908 len = strlen(ev->ev_group);
909 #ifdef DIAGNOSTIC
910 if (len >= EVCNT_STRING_MAX) /* ..._MAX includes NUL */
911 panic("evcnt_attach_static: group length (%s)", ev->ev_group);
912 #endif
913 ev->ev_grouplen = len;
914
915 len = strlen(ev->ev_name);
916 #ifdef DIAGNOSTIC
917 if (len >= EVCNT_STRING_MAX) /* ..._MAX includes NUL */
918 panic("evcnt_attach_static: name length (%s)", ev->ev_name);
919 #endif
920 ev->ev_namelen = len;
921
922 TAILQ_INSERT_TAIL(&allevents, ev, ev_list);
923 }
924
925 /*
926 * Attach a dynamically-initialized event. Zero it, set up the type
927 * and string pointers and then act like it was statically initialized.
928 */
929 void
930 evcnt_attach_dynamic(struct evcnt *ev, int type, const struct evcnt *parent,
931 const char *group, const char *name)
932 {
933
934 memset(ev, 0, sizeof *ev);
935 ev->ev_type = type;
936 ev->ev_parent = parent;
937 ev->ev_group = group;
938 ev->ev_name = name;
939 evcnt_attach_static(ev);
940 }
941
942 /*
943 * Detach an event.
944 */
945 void
946 evcnt_detach(struct evcnt *ev)
947 {
948
949 TAILQ_REMOVE(&allevents, ev, ev_list);
950 }
951
952 #ifdef DDB
953 void
954 event_print(int full, void (*pr)(const char *, ...))
955 {
956 struct evcnt *evp;
957
958 TAILQ_FOREACH(evp, &allevents, ev_list) {
959 if (evp->ev_count == 0 && !full)
960 continue;
961
962 (*pr)("evcnt type %d: %s %s = %lld\n", evp->ev_type,
963 evp->ev_group, evp->ev_name, evp->ev_count);
964 }
965 }
966 #endif /* DDB */
967