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 * %sccs.include.redist.c%
15 *
16 * @(#)subr_autoconf.c 8.3 (Berkeley) 05/17/94
17 *
18 * from: $Header: subr_autoconf.c,v 1.12 93/02/01 19:31:48 torek Exp $ (LBL)
19 */
20
21 #include <sys/param.h>
22 #include <sys/device.h>
23 #include <sys/malloc.h>
24 #include <libkern/libkern.h>
25
26 /*
27 * Autoconfiguration subroutines.
28 */
29
30 /*
31 * ioconf.c exports exactly two names: cfdata and cfroots. All system
32 * devices and drivers are found via these tables.
33 */
34 extern struct cfdata cfdata[];
35 extern short cfroots[];
36
37 #define ROOT ((struct device *)NULL)
38
39 struct matchinfo {
40 cfmatch_t fn;
41 struct device *parent;
42 void *aux;
43 struct cfdata *match;
44 int pri;
45 };
46
47 /*
48 * Apply the matching function and choose the best. This is used
49 * a few times and we want to keep the code small.
50 */
51 static void
mapply(m,cf)52 mapply(m, cf)
53 register struct matchinfo *m;
54 register struct cfdata *cf;
55 {
56 register int pri;
57
58 if (m->fn != NULL)
59 pri = (*m->fn)(m->parent, cf, m->aux);
60 else
61 pri = (*cf->cf_driver->cd_match)(m->parent, cf, m->aux);
62 if (pri > m->pri) {
63 m->match = cf;
64 m->pri = pri;
65 }
66 }
67
68 /*
69 * Iterate over all potential children of some device, calling the given
70 * function (default being the child's match function) for each one.
71 * Nonzero returns are matches; the highest value returned is considered
72 * the best match. Return the `found child' if we got a match, or NULL
73 * otherwise. The `aux' pointer is simply passed on through.
74 *
75 * Note that this function is designed so that it can be used to apply
76 * an arbitrary function to all potential children (its return value
77 * can be ignored).
78 */
79 struct cfdata *
config_search(fn,parent,aux)80 config_search(fn, parent, aux)
81 cfmatch_t fn;
82 register struct device *parent;
83 void *aux;
84 {
85 register struct cfdata *cf;
86 register short *p;
87 struct matchinfo m;
88
89 m.fn = fn;
90 m.parent = parent;
91 m.aux = aux;
92 m.match = NULL;
93 m.pri = 0;
94 for (cf = cfdata; cf->cf_driver; cf++) {
95 /*
96 * Skip cf if no longer eligible, otherwise scan through
97 * parents for one matching `parent', and try match function.
98 */
99 if (cf->cf_fstate == FSTATE_FOUND)
100 continue;
101 for (p = cf->cf_parents; *p >= 0; p++)
102 if (parent->dv_cfdata == &cfdata[*p])
103 mapply(&m, cf);
104 }
105 return (m.match);
106 }
107
108 /*
109 * Find the given root device.
110 * This is much like config_search, but there is no parent.
111 */
112 struct cfdata *
config_rootsearch(fn,rootname,aux)113 config_rootsearch(fn, rootname, aux)
114 register cfmatch_t fn;
115 register char *rootname;
116 register void *aux;
117 {
118 register struct cfdata *cf;
119 register short *p;
120 struct matchinfo m;
121
122 m.fn = fn;
123 m.parent = ROOT;
124 m.aux = aux;
125 m.match = NULL;
126 m.pri = 0;
127 /*
128 * Look at root entries for matching name. We do not bother
129 * with found-state here since only one root should ever be
130 * searched (and it must be done first).
131 */
132 for (p = cfroots; *p >= 0; p++) {
133 cf = &cfdata[*p];
134 if (strcmp(cf->cf_driver->cd_name, rootname) == 0)
135 mapply(&m, cf);
136 }
137 return (m.match);
138 }
139
140 static char *msgs[3] = { "", " not configured\n", " unsupported\n" };
141
142 /*
143 * The given `aux' argument describes a device that has been found
144 * on the given parent, but not necessarily configured. Locate the
145 * configuration data for that device (using the cd_match configuration
146 * driver function) and attach it, and return true. If the device was
147 * not configured, call the given `print' function and return 0.
148 */
149 int
config_found(parent,aux,print)150 config_found(parent, aux, print)
151 struct device *parent;
152 void *aux;
153 cfprint_t print;
154 {
155 struct cfdata *cf;
156
157 if ((cf = config_search((cfmatch_t)NULL, parent, aux)) != NULL) {
158 config_attach(parent, cf, aux, print);
159 return (1);
160 }
161 printf(msgs[(*print)(aux, parent->dv_xname)]);
162 return (0);
163 }
164
165 /*
166 * As above, but for root devices.
167 */
168 int
config_rootfound(rootname,aux)169 config_rootfound(rootname, aux)
170 char *rootname;
171 void *aux;
172 {
173 struct cfdata *cf;
174
175 if ((cf = config_rootsearch((cfmatch_t)NULL, rootname, aux)) != NULL) {
176 config_attach(ROOT, cf, aux, (cfprint_t)NULL);
177 return (1);
178 }
179 printf("root device %s not configured\n", rootname);
180 return (0);
181 }
182
183 /* just like sprintf(buf, "%d") except that it works from the end */
184 static char *
number(ep,n)185 number(ep, n)
186 register char *ep;
187 register int n;
188 {
189
190 *--ep = 0;
191 while (n >= 10) {
192 *--ep = (n % 10) + '0';
193 n /= 10;
194 }
195 *--ep = n + '0';
196 return (ep);
197 }
198
199 /*
200 * Attach a found device. Allocates memory for device variables.
201 */
202 void
config_attach(parent,cf,aux,print)203 config_attach(parent, cf, aux, print)
204 register struct device *parent;
205 register struct cfdata *cf;
206 register void *aux;
207 cfprint_t print;
208 {
209 register struct device *dev;
210 register struct cfdriver *cd;
211 register size_t lname, lunit;
212 register char *xunit;
213 int myunit;
214 char num[10];
215 static struct device **nextp = &alldevs;
216
217 cd = cf->cf_driver;
218 if (cd->cd_devsize < sizeof(struct device))
219 panic("config_attach");
220 myunit = cf->cf_unit;
221 if (cf->cf_fstate == FSTATE_NOTFOUND)
222 cf->cf_fstate = FSTATE_FOUND;
223 else
224 cf->cf_unit++;
225
226 /* compute length of name and decimal expansion of unit number */
227 lname = strlen(cd->cd_name);
228 xunit = number(&num[sizeof num], myunit);
229 lunit = &num[sizeof num] - xunit;
230 if (lname + lunit >= sizeof(dev->dv_xname))
231 panic("config_attach: device name too long");
232
233 /* get memory for all device vars */
234 dev = (struct device *)malloc(cd->cd_devsize, M_DEVBUF, M_WAITOK);
235 /* XXX cannot wait! */
236 bzero(dev, cd->cd_devsize);
237 *nextp = dev; /* link up */
238 nextp = &dev->dv_next;
239 dev->dv_class = cd->cd_class;
240 dev->dv_cfdata = cf;
241 dev->dv_unit = myunit;
242 bcopy(cd->cd_name, dev->dv_xname, lname);
243 bcopy(xunit, dev->dv_xname + lname, lunit);
244 dev->dv_parent = parent;
245 if (parent == ROOT)
246 printf("%s (root)", dev->dv_xname);
247 else {
248 printf("%s at %s", dev->dv_xname, parent->dv_xname);
249 (void) (*print)(aux, (char *)0);
250 }
251
252 /* put this device in the devices array */
253 if (dev->dv_unit >= cd->cd_ndevs) {
254 /*
255 * Need to expand the array.
256 */
257 int old = cd->cd_ndevs, oldbytes, new, newbytes;
258 void **nsp;
259
260 if (old == 0) {
261 new = max(MINALLOCSIZE / sizeof(void *),
262 dev->dv_unit + 1);
263 newbytes = new * sizeof(void *);
264 nsp = malloc(newbytes, M_DEVBUF, M_WAITOK); /*XXX*/
265 bzero(nsp, newbytes);
266 } else {
267 new = cd->cd_ndevs;
268 do {
269 new *= 2;
270 } while (new <= dev->dv_unit);
271 oldbytes = old * sizeof(void *);
272 newbytes = new * sizeof(void *);
273 nsp = malloc(newbytes, M_DEVBUF, M_WAITOK); /*XXX*/
274 bcopy(cd->cd_devs, nsp, oldbytes);
275 bzero(&nsp[old], newbytes - oldbytes);
276 free(cd->cd_devs, M_DEVBUF);
277 }
278 cd->cd_ndevs = new;
279 cd->cd_devs = nsp;
280 }
281 if (cd->cd_devs[dev->dv_unit])
282 panic("config_attach: duplicate %s", dev->dv_xname);
283 cd->cd_devs[dev->dv_unit] = dev;
284
285 /*
286 * Before attaching, clobber any unfound devices that are
287 * otherwise identical.
288 */
289 for (cf = cfdata; cf->cf_driver; cf++)
290 if (cf->cf_driver == cd && cf->cf_unit == dev->dv_unit &&
291 cf->cf_fstate == FSTATE_NOTFOUND)
292 cf->cf_fstate = FSTATE_FOUND;
293 (*cd->cd_attach)(parent, dev, aux);
294 }
295
296 /*
297 * Attach an event. These must come from initially-zero space (see
298 * commented-out assignments below), but that occurs naturally for
299 * device instance variables.
300 */
301 void
evcnt_attach(dev,name,ev)302 evcnt_attach(dev, name, ev)
303 struct device *dev;
304 const char *name;
305 struct evcnt *ev;
306 {
307 static struct evcnt **nextp = &allevents;
308
309 #ifdef DIAGNOSTIC
310 if (strlen(name) >= sizeof(ev->ev_name))
311 panic("evcnt_attach");
312 #endif
313 /* ev->ev_next = NULL; */
314 ev->ev_dev = dev;
315 /* ev->ev_count = 0; */
316 strcpy(ev->ev_name, name);
317 *nextp = ev;
318 nextp = &ev->ev_next;
319 }
320