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