subr_autoconf.c revision 1.1.1.1 1 1.1 glass /*
2 1.1.1.1 fvdl * Copyright (c) 1992, 1993
3 1.1.1.1 fvdl * The Regents of the University of California. All rights reserved.
4 1.1 glass *
5 1.1 glass * This software was developed by the Computer Systems Engineering group
6 1.1 glass * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7 1.1 glass * contributed to Berkeley.
8 1.1 glass *
9 1.1 glass * All advertising materials mentioning features or use of this software
10 1.1 glass * must display the following acknowledgement:
11 1.1 glass * This product includes software developed by the University of
12 1.1 glass * California, Lawrence Berkeley Laboratories.
13 1.1 glass *
14 1.1.1.1 fvdl * Redistribution and use in source and binary forms, with or without
15 1.1.1.1 fvdl * modification, are permitted provided that the following conditions
16 1.1.1.1 fvdl * are met:
17 1.1.1.1 fvdl * 1. Redistributions of source code must retain the above copyright
18 1.1.1.1 fvdl * notice, this list of conditions and the following disclaimer.
19 1.1.1.1 fvdl * 2. Redistributions in binary form must reproduce the above copyright
20 1.1.1.1 fvdl * notice, this list of conditions and the following disclaimer in the
21 1.1.1.1 fvdl * documentation and/or other materials provided with the distribution.
22 1.1.1.1 fvdl * 3. All advertising materials mentioning features or use of this software
23 1.1.1.1 fvdl * must display the following acknowledgement:
24 1.1.1.1 fvdl * This product includes software developed by the University of
25 1.1.1.1 fvdl * California, Berkeley and its contributors.
26 1.1.1.1 fvdl * 4. Neither the name of the University nor the names of its contributors
27 1.1.1.1 fvdl * may be used to endorse or promote products derived from this software
28 1.1.1.1 fvdl * without specific prior written permission.
29 1.1.1.1 fvdl *
30 1.1.1.1 fvdl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 1.1.1.1 fvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 1.1.1.1 fvdl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 1.1.1.1 fvdl * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 1.1.1.1 fvdl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 1.1.1.1 fvdl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 1.1.1.1 fvdl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 1.1.1.1 fvdl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 1.1.1.1 fvdl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 1.1.1.1 fvdl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 1.1.1.1 fvdl * SUCH DAMAGE.
41 1.1 glass *
42 1.1.1.1 fvdl * @(#)subr_autoconf.c 8.1 (Berkeley) 6/10/93
43 1.1 glass *
44 1.1 glass * from: $Header: /tank/opengrok/rsync2/NetBSD/src/sys/kern/subr_autoconf.c,v 1.1.1.1 1998/03/01 02:09:44 fvdl Exp $ (LBL)
45 1.1 glass */
46 1.1 glass
47 1.1 glass #include <sys/param.h>
48 1.1 glass #include <sys/device.h>
49 1.1 glass #include <sys/malloc.h>
50 1.1 glass
51 1.1 glass /*
52 1.1 glass * Autoconfiguration subroutines.
53 1.1 glass */
54 1.1 glass
55 1.1 glass /*
56 1.1 glass * ioconf.c exports exactly two names: cfdata and cfroots. All system
57 1.1 glass * devices and drivers are found via these tables.
58 1.1 glass */
59 1.1 glass extern struct cfdata cfdata[];
60 1.1 glass extern short cfroots[];
61 1.1 glass
62 1.1 glass #define ROOT ((struct device *)NULL)
63 1.1 glass
64 1.1 glass struct matchinfo {
65 1.1 glass cfmatch_t fn;
66 1.1 glass struct device *parent;
67 1.1 glass void *aux;
68 1.1 glass struct cfdata *match;
69 1.1 glass int pri;
70 1.1 glass };
71 1.1 glass
72 1.1 glass /*
73 1.1 glass * Apply the matching function and choose the best. This is used
74 1.1 glass * a few times and we want to keep the code small.
75 1.1 glass */
76 1.1 glass static void
77 1.1 glass mapply(m, cf)
78 1.1 glass register struct matchinfo *m;
79 1.1 glass register struct cfdata *cf;
80 1.1 glass {
81 1.1 glass register int pri;
82 1.1 glass
83 1.1 glass if (m->fn != NULL)
84 1.1 glass pri = (*m->fn)(m->parent, cf, m->aux);
85 1.1 glass else
86 1.1 glass pri = (*cf->cf_driver->cd_match)(m->parent, cf, m->aux);
87 1.1 glass if (pri > m->pri) {
88 1.1 glass m->match = cf;
89 1.1 glass m->pri = pri;
90 1.1 glass }
91 1.1 glass }
92 1.1 glass
93 1.1 glass /*
94 1.1 glass * Iterate over all potential children of some device, calling the given
95 1.1 glass * function (default being the child's match function) for each one.
96 1.1 glass * Nonzero returns are matches; the highest value returned is considered
97 1.1 glass * the best match. Return the `found child' if we got a match, or NULL
98 1.1 glass * otherwise. The `aux' pointer is simply passed on through.
99 1.1 glass *
100 1.1 glass * Note that this function is designed so that it can be used to apply
101 1.1 glass * an arbitrary function to all potential children (its return value
102 1.1 glass * can be ignored).
103 1.1 glass */
104 1.1 glass struct cfdata *
105 1.1 glass config_search(fn, parent, aux)
106 1.1 glass cfmatch_t fn;
107 1.1 glass register struct device *parent;
108 1.1 glass void *aux;
109 1.1 glass {
110 1.1 glass register struct cfdata *cf;
111 1.1 glass register short *p;
112 1.1 glass struct matchinfo m;
113 1.1 glass
114 1.1 glass m.fn = fn;
115 1.1 glass m.parent = parent;
116 1.1 glass m.aux = aux;
117 1.1 glass m.match = NULL;
118 1.1 glass m.pri = 0;
119 1.1 glass for (cf = cfdata; cf->cf_driver; cf++) {
120 1.1 glass /*
121 1.1 glass * Skip cf if no longer eligible, otherwise scan through
122 1.1 glass * parents for one matching `parent', and try match function.
123 1.1 glass */
124 1.1 glass if (cf->cf_fstate == FSTATE_FOUND)
125 1.1 glass continue;
126 1.1 glass for (p = cf->cf_parents; *p >= 0; p++)
127 1.1 glass if (parent->dv_cfdata == &cfdata[*p])
128 1.1 glass mapply(&m, cf);
129 1.1 glass }
130 1.1 glass return (m.match);
131 1.1 glass }
132 1.1 glass
133 1.1 glass /*
134 1.1 glass * Find the given root device.
135 1.1 glass * This is much like config_search, but there is no parent.
136 1.1 glass */
137 1.1 glass struct cfdata *
138 1.1 glass config_rootsearch(fn, rootname, aux)
139 1.1 glass register cfmatch_t fn;
140 1.1 glass register char *rootname;
141 1.1 glass register void *aux;
142 1.1 glass {
143 1.1 glass register struct cfdata *cf;
144 1.1 glass register short *p;
145 1.1 glass struct matchinfo m;
146 1.1 glass
147 1.1 glass m.fn = fn;
148 1.1 glass m.parent = ROOT;
149 1.1 glass m.aux = aux;
150 1.1 glass m.match = NULL;
151 1.1 glass m.pri = 0;
152 1.1 glass /*
153 1.1 glass * Look at root entries for matching name. We do not bother
154 1.1 glass * with found-state here since only one root should ever be
155 1.1 glass * searched (and it must be done first).
156 1.1 glass */
157 1.1 glass for (p = cfroots; *p >= 0; p++) {
158 1.1 glass cf = &cfdata[*p];
159 1.1 glass if (strcmp(cf->cf_driver->cd_name, rootname) == 0)
160 1.1 glass mapply(&m, cf);
161 1.1 glass }
162 1.1 glass return (m.match);
163 1.1 glass }
164 1.1 glass
165 1.1 glass static char *msgs[3] = { "", " not configured\n", " unsupported\n" };
166 1.1 glass
167 1.1 glass /*
168 1.1 glass * The given `aux' argument describes a device that has been found
169 1.1 glass * on the given parent, but not necessarily configured. Locate the
170 1.1 glass * configuration data for that device (using the cd_match configuration
171 1.1 glass * driver function) and attach it, and return true. If the device was
172 1.1 glass * not configured, call the given `print' function and return 0.
173 1.1 glass */
174 1.1 glass int
175 1.1 glass config_found(parent, aux, print)
176 1.1 glass struct device *parent;
177 1.1 glass void *aux;
178 1.1 glass cfprint_t print;
179 1.1 glass {
180 1.1 glass struct cfdata *cf;
181 1.1 glass
182 1.1 glass if ((cf = config_search((cfmatch_t)NULL, parent, aux)) != NULL) {
183 1.1 glass config_attach(parent, cf, aux, print);
184 1.1 glass return (1);
185 1.1 glass }
186 1.1 glass printf(msgs[(*print)(aux, parent->dv_xname)]);
187 1.1 glass return (0);
188 1.1 glass }
189 1.1 glass
190 1.1 glass /*
191 1.1 glass * As above, but for root devices.
192 1.1 glass */
193 1.1 glass int
194 1.1 glass config_rootfound(rootname, aux)
195 1.1 glass char *rootname;
196 1.1 glass void *aux;
197 1.1 glass {
198 1.1 glass struct cfdata *cf;
199 1.1 glass
200 1.1 glass if ((cf = config_rootsearch((cfmatch_t)NULL, rootname, aux)) != NULL) {
201 1.1 glass config_attach(ROOT, cf, aux, (cfprint_t)NULL);
202 1.1 glass return (1);
203 1.1 glass }
204 1.1 glass printf("root device %s not configured\n", rootname);
205 1.1 glass return (0);
206 1.1 glass }
207 1.1 glass
208 1.1 glass /* just like sprintf(buf, "%d") except that it works from the end */
209 1.1 glass static char *
210 1.1 glass number(ep, n)
211 1.1 glass register char *ep;
212 1.1 glass register int n;
213 1.1 glass {
214 1.1 glass
215 1.1 glass *--ep = 0;
216 1.1 glass while (n >= 10) {
217 1.1 glass *--ep = (n % 10) + '0';
218 1.1 glass n /= 10;
219 1.1 glass }
220 1.1 glass *--ep = n + '0';
221 1.1 glass return (ep);
222 1.1 glass }
223 1.1 glass
224 1.1 glass /*
225 1.1 glass * Attach a found device. Allocates memory for device variables.
226 1.1 glass */
227 1.1 glass void
228 1.1 glass config_attach(parent, cf, aux, print)
229 1.1 glass register struct device *parent;
230 1.1 glass register struct cfdata *cf;
231 1.1 glass register void *aux;
232 1.1 glass cfprint_t print;
233 1.1 glass {
234 1.1 glass register struct device *dev;
235 1.1 glass register struct cfdriver *cd;
236 1.1 glass register size_t lname, lunit;
237 1.1 glass register char *xunit;
238 1.1 glass int myunit;
239 1.1 glass char num[10];
240 1.1 glass static struct device **nextp = &alldevs;
241 1.1 glass
242 1.1 glass cd = cf->cf_driver;
243 1.1 glass if (cd->cd_devsize < sizeof(struct device))
244 1.1 glass panic("config_attach");
245 1.1 glass myunit = cf->cf_unit;
246 1.1 glass if (cf->cf_fstate == FSTATE_NOTFOUND)
247 1.1 glass cf->cf_fstate = FSTATE_FOUND;
248 1.1 glass else
249 1.1 glass cf->cf_unit++;
250 1.1 glass
251 1.1 glass /* compute length of name and decimal expansion of unit number */
252 1.1 glass lname = strlen(cd->cd_name);
253 1.1 glass xunit = number(&num[sizeof num], myunit);
254 1.1 glass lunit = &num[sizeof num] - xunit;
255 1.1 glass if (lname + lunit >= sizeof(dev->dv_xname))
256 1.1 glass panic("config_attach: device name too long");
257 1.1 glass
258 1.1 glass /* get memory for all device vars */
259 1.1 glass dev = (struct device *)malloc(cd->cd_devsize, M_DEVBUF, M_WAITOK);
260 1.1 glass /* XXX cannot wait! */
261 1.1 glass bzero(dev, cd->cd_devsize);
262 1.1 glass *nextp = dev; /* link up */
263 1.1 glass nextp = &dev->dv_next;
264 1.1 glass dev->dv_class = cd->cd_class;
265 1.1 glass dev->dv_cfdata = cf;
266 1.1 glass dev->dv_unit = myunit;
267 1.1 glass bcopy(cd->cd_name, dev->dv_xname, lname);
268 1.1 glass bcopy(xunit, dev->dv_xname + lname, lunit);
269 1.1 glass dev->dv_parent = parent;
270 1.1 glass if (parent == ROOT)
271 1.1 glass printf("%s (root)", dev->dv_xname);
272 1.1 glass else {
273 1.1 glass printf("%s at %s", dev->dv_xname, parent->dv_xname);
274 1.1 glass (void) (*print)(aux, (char *)0);
275 1.1 glass }
276 1.1 glass
277 1.1 glass /* put this device in the devices array */
278 1.1 glass if (dev->dv_unit >= cd->cd_ndevs) {
279 1.1 glass /*
280 1.1 glass * Need to expand the array.
281 1.1 glass */
282 1.1 glass int old = cd->cd_ndevs, oldbytes, new, newbytes;
283 1.1 glass void **nsp;
284 1.1 glass
285 1.1 glass if (old == 0) {
286 1.1 glass nsp = malloc(MINALLOCSIZE, M_DEVBUF, M_WAITOK); /*XXX*/
287 1.1 glass bzero(nsp, MINALLOCSIZE);
288 1.1 glass cd->cd_ndevs = MINALLOCSIZE / sizeof(void *);
289 1.1 glass } else {
290 1.1 glass new = cd->cd_ndevs;
291 1.1 glass do {
292 1.1 glass new *= 2;
293 1.1 glass } while (new <= dev->dv_unit);
294 1.1 glass cd->cd_ndevs = new;
295 1.1 glass oldbytes = old * sizeof(void *);
296 1.1 glass newbytes = new * sizeof(void *);
297 1.1 glass nsp = malloc(newbytes, M_DEVBUF, M_WAITOK); /*XXX*/
298 1.1 glass bcopy(cd->cd_devs, nsp, oldbytes);
299 1.1 glass bzero(&nsp[old], newbytes - oldbytes);
300 1.1 glass free(cd->cd_devs, M_DEVBUF);
301 1.1 glass }
302 1.1 glass cd->cd_devs = nsp;
303 1.1 glass }
304 1.1 glass if (cd->cd_devs[dev->dv_unit])
305 1.1 glass panic("config_attach: duplicate %s", dev->dv_xname);
306 1.1 glass cd->cd_devs[dev->dv_unit] = dev;
307 1.1 glass
308 1.1 glass /*
309 1.1 glass * Before attaching, clobber any unfound devices that are
310 1.1 glass * otherwise identical.
311 1.1 glass */
312 1.1 glass for (cf = cfdata; cf->cf_driver; cf++)
313 1.1 glass if (cf->cf_driver == cd && cf->cf_unit == dev->dv_unit &&
314 1.1 glass cf->cf_fstate == FSTATE_NOTFOUND)
315 1.1 glass cf->cf_fstate = FSTATE_FOUND;
316 1.1 glass (*cd->cd_attach)(parent, dev, aux);
317 1.1 glass }
318 1.1 glass
319 1.1 glass /*
320 1.1 glass * Attach an event. These must come from initially-zero space (see
321 1.1 glass * commented-out assignments below), but that occurs naturally for
322 1.1 glass * device instance variables.
323 1.1 glass */
324 1.1 glass void
325 1.1 glass evcnt_attach(dev, name, ev)
326 1.1 glass struct device *dev;
327 1.1 glass const char *name;
328 1.1 glass struct evcnt *ev;
329 1.1 glass {
330 1.1 glass static struct evcnt **nextp = &allevents;
331 1.1 glass
332 1.1 glass #ifdef DIAGNOSTIC
333 1.1 glass if (strlen(name) >= sizeof(ev->ev_name))
334 1.1 glass panic("evcnt_attach");
335 1.1 glass #endif
336 1.1 glass /* ev->ev_next = NULL; */
337 1.1 glass ev->ev_dev = dev;
338 1.1 glass /* ev->ev_count = 0; */
339 1.1 glass strcpy(ev->ev_name, name);
340 1.1 glass *nextp = ev;
341 1.1 glass nextp = &ev->ev_next;
342 1.1 glass }
343