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