subr_autoconf.c revision 1.68 1 /* $NetBSD: subr_autoconf.c,v 1.68 2002/09/27 03:18:23 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.68 2002/09/27 03:18:23 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 * Invoke the "match" routine for a cfdata entry on behalf of
352 * an external caller, usually a "submatch" routine.
353 */
354 int
355 config_match(struct device *parent, struct cfdata *cf, void *aux)
356 {
357
358 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
359 }
360
361 /*
362 * Iterate over all potential children of some device, calling the given
363 * function (default being the child's match function) for each one.
364 * Nonzero returns are matches; the highest value returned is considered
365 * the best match. Return the `found child' if we got a match, or NULL
366 * otherwise. The `aux' pointer is simply passed on through.
367 *
368 * Note that this function is designed so that it can be used to apply
369 * an arbitrary function to all potential children (its return value
370 * can be ignored).
371 */
372 struct cfdata *
373 config_search(cfmatch_t fn, struct device *parent, void *aux)
374 {
375 struct cftable *ct;
376 struct cfdata *cf;
377 struct matchinfo m;
378
379 m.fn = fn;
380 m.parent = parent;
381 m.aux = aux;
382 m.match = NULL;
383 m.pri = 0;
384
385 TAILQ_FOREACH(ct, &allcftables, ct_list) {
386 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
387 /*
388 * Skip cf if no longer eligible, otherwise scan
389 * through parents for one matching `parent', and
390 * try match function.
391 */
392 if (cf->cf_fstate == FSTATE_FOUND)
393 continue;
394 if (cf->cf_fstate == FSTATE_DNOTFOUND ||
395 cf->cf_fstate == FSTATE_DSTAR)
396 continue;
397 if (cfparent_match(parent, cf->cf_pspec))
398 mapply(&m, cf);
399 }
400 }
401 return (m.match);
402 }
403
404 /*
405 * Find the given root device.
406 * This is much like config_search, but there is no parent.
407 * Don't bother with multiple cfdata tables; the root node
408 * must always be in the initial table.
409 */
410 struct cfdata *
411 config_rootsearch(cfmatch_t fn, const char *rootname, void *aux)
412 {
413 struct cfdata *cf;
414 short *p;
415 struct matchinfo m;
416
417 m.fn = fn;
418 m.parent = ROOT;
419 m.aux = aux;
420 m.match = NULL;
421 m.pri = 0;
422 /*
423 * Look at root entries for matching name. We do not bother
424 * with found-state here since only one root should ever be
425 * searched (and it must be done first).
426 */
427 for (p = cfroots; *p >= 0; p++) {
428 cf = &cfdata[*p];
429 if (strcmp(cf->cf_name, rootname) == 0)
430 mapply(&m, cf);
431 }
432 return (m.match);
433 }
434
435 static const char *msgs[3] = { "", " not configured\n", " unsupported\n" };
436
437 /*
438 * The given `aux' argument describes a device that has been found
439 * on the given parent, but not necessarily configured. Locate the
440 * configuration data for that device (using the submatch function
441 * provided, or using candidates' cd_match configuration driver
442 * functions) and attach it, and return true. If the device was
443 * not configured, call the given `print' function and return 0.
444 */
445 struct device *
446 config_found_sm(struct device *parent, void *aux, cfprint_t print,
447 cfmatch_t submatch)
448 {
449 struct cfdata *cf;
450
451 if ((cf = config_search(submatch, parent, aux)) != NULL)
452 return (config_attach(parent, cf, aux, print));
453 if (print)
454 printf("%s", msgs[(*print)(aux, parent->dv_xname)]);
455 return (NULL);
456 }
457
458 /*
459 * As above, but for root devices.
460 */
461 struct device *
462 config_rootfound(const char *rootname, void *aux)
463 {
464 struct cfdata *cf;
465
466 if ((cf = config_rootsearch((cfmatch_t)NULL, rootname, aux)) != NULL)
467 return (config_attach(ROOT, cf, aux, (cfprint_t)NULL));
468 printf("root device %s not configured\n", rootname);
469 return (NULL);
470 }
471
472 /* just like sprintf(buf, "%d") except that it works from the end */
473 static char *
474 number(char *ep, int n)
475 {
476
477 *--ep = 0;
478 while (n >= 10) {
479 *--ep = (n % 10) + '0';
480 n /= 10;
481 }
482 *--ep = n + '0';
483 return (ep);
484 }
485
486 /*
487 * Expand the size of the cd_devs array if necessary.
488 */
489 void
490 config_makeroom(int n, struct cfdriver *cd)
491 {
492 int old, new;
493 void **nsp;
494
495 if (n < cd->cd_ndevs)
496 return;
497
498 /*
499 * Need to expand the array.
500 */
501 old = cd->cd_ndevs;
502 if (old == 0)
503 new = MINALLOCSIZE / sizeof(void *);
504 else
505 new = old * 2;
506 while (new <= n)
507 new *= 2;
508 cd->cd_ndevs = new;
509 nsp = malloc(new * sizeof(void *), M_DEVBUF,
510 cold ? M_NOWAIT : M_WAITOK);
511 if (nsp == NULL)
512 panic("config_attach: %sing dev array",
513 old != 0 ? "expand" : "creat");
514 memset(nsp + old, 0, (new - old) * sizeof(void *));
515 if (old != 0) {
516 memcpy(nsp, cd->cd_devs, old * sizeof(void *));
517 free(cd->cd_devs, M_DEVBUF);
518 }
519 cd->cd_devs = nsp;
520 }
521
522 /*
523 * Attach a found device. Allocates memory for device variables.
524 */
525 struct device *
526 config_attach(struct device *parent, struct cfdata *cf, void *aux,
527 cfprint_t print)
528 {
529 struct device *dev;
530 struct cftable *ct;
531 struct cfdriver *cd;
532 struct cfattach *ca;
533 size_t lname, lunit;
534 const char *xunit;
535 int myunit;
536 char num[10];
537
538 cd = config_cfdriver_lookup(cf->cf_name);
539 KASSERT(cd != NULL);
540 ca = cf->cf_attach;
541 if (ca->ca_devsize < sizeof(struct device))
542 panic("config_attach");
543
544 #ifndef __BROKEN_CONFIG_UNIT_USAGE
545 if (cf->cf_fstate == FSTATE_STAR) {
546 for (myunit = cf->cf_unit; myunit < cd->cd_ndevs; myunit++)
547 if (cd->cd_devs[myunit] == NULL)
548 break;
549 /*
550 * myunit is now the unit of the first NULL device pointer,
551 * or max(cd->cd_ndevs,cf->cf_unit).
552 */
553 } else {
554 myunit = cf->cf_unit;
555 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
556 cf->cf_fstate = FSTATE_FOUND;
557 }
558 #else
559 myunit = cf->cf_unit;
560 if (cf->cf_fstate == FSTATE_STAR)
561 cf->cf_unit++;
562 else {
563 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
564 cf->cf_fstate = FSTATE_FOUND;
565 }
566 #endif /* ! __BROKEN_CONFIG_UNIT_USAGE */
567
568 /* compute length of name and decimal expansion of unit number */
569 lname = strlen(cd->cd_name);
570 xunit = number(&num[sizeof(num)], myunit);
571 lunit = &num[sizeof(num)] - xunit;
572 if (lname + lunit > sizeof(dev->dv_xname))
573 panic("config_attach: device name too long");
574
575 /* get memory for all device vars */
576 dev = (struct device *)malloc(ca->ca_devsize, M_DEVBUF,
577 cold ? M_NOWAIT : M_WAITOK);
578 if (!dev)
579 panic("config_attach: memory allocation for device softc failed");
580 memset(dev, 0, ca->ca_devsize);
581 TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */
582 dev->dv_class = cd->cd_class;
583 dev->dv_cfdata = cf;
584 dev->dv_unit = myunit;
585 memcpy(dev->dv_xname, cd->cd_name, lname);
586 memcpy(dev->dv_xname + lname, xunit, lunit);
587 dev->dv_parent = parent;
588 dev->dv_flags = DVF_ACTIVE; /* always initially active */
589
590 if (parent == ROOT)
591 printf("%s (root)", dev->dv_xname);
592 else {
593 printf("%s at %s", dev->dv_xname, parent->dv_xname);
594 if (print)
595 (void) (*print)(aux, NULL);
596 }
597
598 /* put this device in the devices array */
599 config_makeroom(dev->dv_unit, cd);
600 if (cd->cd_devs[dev->dv_unit])
601 panic("config_attach: duplicate %s", dev->dv_xname);
602 cd->cd_devs[dev->dv_unit] = dev;
603
604 /*
605 * Before attaching, clobber any unfound devices that are
606 * otherwise identical.
607 */
608 TAILQ_FOREACH(ct, &allcftables, ct_list) {
609 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
610 if (STREQ(cf->cf_name, cd->cd_name) &&
611 cf->cf_unit == dev->dv_unit) {
612 if (cf->cf_fstate == FSTATE_NOTFOUND)
613 cf->cf_fstate = FSTATE_FOUND;
614 #ifdef __BROKEN_CONFIG_UNIT_USAGE
615 /*
616 * Bump the unit number on all starred cfdata
617 * entries for this device.
618 */
619 if (cf->cf_fstate == FSTATE_STAR)
620 cf->cf_unit++;
621 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
622 }
623 }
624 }
625 #ifdef __HAVE_DEVICE_REGISTER
626 device_register(dev, aux);
627 #endif
628 (*ca->ca_attach)(parent, dev, aux);
629 config_process_deferred(&deferred_config_queue, dev);
630 return (dev);
631 }
632
633 /*
634 * Detach a device. Optionally forced (e.g. because of hardware
635 * removal) and quiet. Returns zero if successful, non-zero
636 * (an error code) otherwise.
637 *
638 * Note that this code wants to be run from a process context, so
639 * that the detach can sleep to allow processes which have a device
640 * open to run and unwind their stacks.
641 */
642 int
643 config_detach(struct device *dev, int flags)
644 {
645 struct cftable *ct;
646 struct cfdata *cf;
647 struct cfattach *ca;
648 struct cfdriver *cd;
649 #ifdef DIAGNOSTIC
650 struct device *d;
651 #endif
652 int rv = 0, i;
653
654 cf = dev->dv_cfdata;
655 #ifdef DIAGNOSTIC
656 if (cf->cf_fstate != FSTATE_FOUND && cf->cf_fstate != FSTATE_STAR)
657 panic("config_detach: bad device fstate");
658 #endif
659 cd = config_cfdriver_lookup(cf->cf_name);
660 KASSERT(cd != NULL);
661 ca = cf->cf_attach;
662
663 /*
664 * Ensure the device is deactivated. If the device doesn't
665 * have an activation entry point, we allow DVF_ACTIVE to
666 * remain set. Otherwise, if DVF_ACTIVE is still set, the
667 * device is busy, and the detach fails.
668 */
669 if (ca->ca_activate != NULL)
670 rv = config_deactivate(dev);
671
672 /*
673 * Try to detach the device. If that's not possible, then
674 * we either panic() (for the forced but failed case), or
675 * return an error.
676 */
677 if (rv == 0) {
678 if (ca->ca_detach != NULL)
679 rv = (*ca->ca_detach)(dev, flags);
680 else
681 rv = EOPNOTSUPP;
682 }
683 if (rv != 0) {
684 if ((flags & DETACH_FORCE) == 0)
685 return (rv);
686 else
687 panic("config_detach: forced detach of %s failed (%d)",
688 dev->dv_xname, rv);
689 }
690
691 /*
692 * The device has now been successfully detached.
693 */
694
695 #ifdef DIAGNOSTIC
696 /*
697 * Sanity: If you're successfully detached, you should have no
698 * children. (Note that because children must be attached
699 * after parents, we only need to search the latter part of
700 * the list.)
701 */
702 for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
703 d = TAILQ_NEXT(d, dv_list)) {
704 if (d->dv_parent == dev) {
705 printf("config_detach: detached device %s"
706 " has children %s\n", dev->dv_xname, d->dv_xname);
707 panic("config_detach");
708 }
709 }
710 #endif
711
712 /*
713 * Mark cfdata to show that the unit can be reused, if possible.
714 */
715 TAILQ_FOREACH(ct, &allcftables, ct_list) {
716 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
717 if (STREQ(cf->cf_name, cd->cd_name)) {
718 if (cf->cf_fstate == FSTATE_FOUND &&
719 cf->cf_unit == dev->dv_unit)
720 cf->cf_fstate = FSTATE_NOTFOUND;
721 #ifdef __BROKEN_CONFIG_UNIT_USAGE
722 /*
723 * Note that we can only re-use a starred
724 * unit number if the unit being detached
725 * had the last assigned unit number.
726 */
727 if (cf->cf_fstate == FSTATE_STAR &&
728 cf->cf_unit == dev->dv_unit + 1)
729 cf->cf_unit--;
730 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
731 }
732 }
733 }
734
735 /*
736 * Unlink from device list.
737 */
738 TAILQ_REMOVE(&alldevs, dev, dv_list);
739
740 /*
741 * Remove from cfdriver's array, tell the world, and free softc.
742 */
743 cd->cd_devs[dev->dv_unit] = NULL;
744 if ((flags & DETACH_QUIET) == 0)
745 printf("%s detached\n", dev->dv_xname);
746 free(dev, M_DEVBUF);
747
748 /*
749 * If the device now has no units in use, deallocate its softc array.
750 */
751 for (i = 0; i < cd->cd_ndevs; i++)
752 if (cd->cd_devs[i] != NULL)
753 break;
754 if (i == cd->cd_ndevs) { /* nothing found; deallocate */
755 free(cd->cd_devs, M_DEVBUF);
756 cd->cd_devs = NULL;
757 cd->cd_ndevs = 0;
758 }
759
760 /*
761 * Return success.
762 */
763 return (0);
764 }
765
766 int
767 config_activate(struct device *dev)
768 {
769 struct cfattach *ca = dev->dv_cfdata->cf_attach;
770 int rv = 0, oflags = dev->dv_flags;
771
772 if (ca->ca_activate == NULL)
773 return (EOPNOTSUPP);
774
775 if ((dev->dv_flags & DVF_ACTIVE) == 0) {
776 dev->dv_flags |= DVF_ACTIVE;
777 rv = (*ca->ca_activate)(dev, DVACT_ACTIVATE);
778 if (rv)
779 dev->dv_flags = oflags;
780 }
781 return (rv);
782 }
783
784 int
785 config_deactivate(struct device *dev)
786 {
787 struct cfattach *ca = dev->dv_cfdata->cf_attach;
788 int rv = 0, oflags = dev->dv_flags;
789
790 if (ca->ca_activate == NULL)
791 return (EOPNOTSUPP);
792
793 if (dev->dv_flags & DVF_ACTIVE) {
794 dev->dv_flags &= ~DVF_ACTIVE;
795 rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE);
796 if (rv)
797 dev->dv_flags = oflags;
798 }
799 return (rv);
800 }
801
802 /*
803 * Defer the configuration of the specified device until all
804 * of its parent's devices have been attached.
805 */
806 void
807 config_defer(struct device *dev, void (*func)(struct device *))
808 {
809 struct deferred_config *dc;
810
811 if (dev->dv_parent == NULL)
812 panic("config_defer: can't defer config of a root device");
813
814 #ifdef DIAGNOSTIC
815 for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL;
816 dc = TAILQ_NEXT(dc, dc_queue)) {
817 if (dc->dc_dev == dev)
818 panic("config_defer: deferred twice");
819 }
820 #endif
821
822 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
823 if (dc == NULL)
824 panic("config_defer: unable to allocate callback");
825
826 dc->dc_dev = dev;
827 dc->dc_func = func;
828 TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
829 config_pending_incr();
830 }
831
832 /*
833 * Defer some autoconfiguration for a device until after interrupts
834 * are enabled.
835 */
836 void
837 config_interrupts(struct device *dev, void (*func)(struct device *))
838 {
839 struct deferred_config *dc;
840
841 /*
842 * If interrupts are enabled, callback now.
843 */
844 if (cold == 0) {
845 (*func)(dev);
846 return;
847 }
848
849 #ifdef DIAGNOSTIC
850 for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL;
851 dc = TAILQ_NEXT(dc, dc_queue)) {
852 if (dc->dc_dev == dev)
853 panic("config_interrupts: deferred twice");
854 }
855 #endif
856
857 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
858 if (dc == NULL)
859 panic("config_interrupts: unable to allocate callback");
860
861 dc->dc_dev = dev;
862 dc->dc_func = func;
863 TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue);
864 config_pending_incr();
865 }
866
867 /*
868 * Process a deferred configuration queue.
869 */
870 static void
871 config_process_deferred(struct deferred_config_head *queue,
872 struct device *parent)
873 {
874 struct deferred_config *dc, *ndc;
875
876 for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) {
877 ndc = TAILQ_NEXT(dc, dc_queue);
878 if (parent == NULL || dc->dc_dev->dv_parent == parent) {
879 TAILQ_REMOVE(queue, dc, dc_queue);
880 (*dc->dc_func)(dc->dc_dev);
881 free(dc, M_DEVBUF);
882 config_pending_decr();
883 }
884 }
885 }
886
887 /*
888 * Manipulate the config_pending semaphore.
889 */
890 void
891 config_pending_incr(void)
892 {
893
894 config_pending++;
895 }
896
897 void
898 config_pending_decr(void)
899 {
900
901 #ifdef DIAGNOSTIC
902 if (config_pending == 0)
903 panic("config_pending_decr: config_pending == 0");
904 #endif
905 config_pending--;
906 if (config_pending == 0)
907 wakeup((void *)&config_pending);
908 }
909
910 /*
911 * Attach a statically-initialized event. The type and string pointers
912 * are already set up.
913 */
914 void
915 evcnt_attach_static(struct evcnt *ev)
916 {
917 int len;
918
919 len = strlen(ev->ev_group);
920 #ifdef DIAGNOSTIC
921 if (len >= EVCNT_STRING_MAX) /* ..._MAX includes NUL */
922 panic("evcnt_attach_static: group length (%s)", ev->ev_group);
923 #endif
924 ev->ev_grouplen = len;
925
926 len = strlen(ev->ev_name);
927 #ifdef DIAGNOSTIC
928 if (len >= EVCNT_STRING_MAX) /* ..._MAX includes NUL */
929 panic("evcnt_attach_static: name length (%s)", ev->ev_name);
930 #endif
931 ev->ev_namelen = len;
932
933 TAILQ_INSERT_TAIL(&allevents, ev, ev_list);
934 }
935
936 /*
937 * Attach a dynamically-initialized event. Zero it, set up the type
938 * and string pointers and then act like it was statically initialized.
939 */
940 void
941 evcnt_attach_dynamic(struct evcnt *ev, int type, const struct evcnt *parent,
942 const char *group, const char *name)
943 {
944
945 memset(ev, 0, sizeof *ev);
946 ev->ev_type = type;
947 ev->ev_parent = parent;
948 ev->ev_group = group;
949 ev->ev_name = name;
950 evcnt_attach_static(ev);
951 }
952
953 /*
954 * Detach an event.
955 */
956 void
957 evcnt_detach(struct evcnt *ev)
958 {
959
960 TAILQ_REMOVE(&allevents, ev, ev_list);
961 }
962
963 #ifdef DDB
964 void
965 event_print(int full, void (*pr)(const char *, ...))
966 {
967 struct evcnt *evp;
968
969 TAILQ_FOREACH(evp, &allevents, ev_list) {
970 if (evp->ev_count == 0 && !full)
971 continue;
972
973 (*pr)("evcnt type %d: %s %s = %lld\n", evp->ev_type,
974 evp->ev_group, evp->ev_name, evp->ev_count);
975 }
976 }
977 #endif /* DDB */
978