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