subr_autoconf.c revision 1.62.6.3 1 1.62.6.3 eeh /* $NetBSD: subr_autoconf.c,v 1.62.6.3 2002/04/06 16:04:31 eeh Exp $ */
2 1.53 cgd
3 1.53 cgd /*
4 1.62.6.2 eeh * Copyright (c) 2001-2002 Eduardo E. Horvath
5 1.53 cgd * Copyright (c) 1996, 2000 Christopher G. Demetriou
6 1.53 cgd * All rights reserved.
7 1.54 cgd *
8 1.53 cgd * Redistribution and use in source and binary forms, with or without
9 1.53 cgd * modification, are permitted provided that the following conditions
10 1.53 cgd * are met:
11 1.53 cgd * 1. Redistributions of source code must retain the above copyright
12 1.53 cgd * notice, this list of conditions and the following disclaimer.
13 1.53 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.53 cgd * notice, this list of conditions and the following disclaimer in the
15 1.53 cgd * documentation and/or other materials provided with the distribution.
16 1.53 cgd * 3. All advertising materials mentioning features or use of this software
17 1.53 cgd * must display the following acknowledgement:
18 1.54 cgd * This product includes software developed for the
19 1.54 cgd * NetBSD Project. See http://www.netbsd.org/ for
20 1.54 cgd * information about NetBSD.
21 1.53 cgd * 4. The name of the author may not be used to endorse or promote products
22 1.54 cgd * derived from this software without specific prior written permission.
23 1.54 cgd *
24 1.53 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 1.53 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 1.53 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 1.53 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 1.53 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 1.53 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 1.53 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 1.53 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 1.53 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 1.53 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.54 cgd *
35 1.54 cgd * --(license Id: LICENSE.proto,v 1.1 2000/06/13 21:40:26 cgd Exp )--
36 1.53 cgd */
37 1.9 cgd
38 1.1 glass /*
39 1.7 glass * Copyright (c) 1992, 1993
40 1.7 glass * The Regents of the University of California. All rights reserved.
41 1.1 glass *
42 1.1 glass * This software was developed by the Computer Systems Engineering group
43 1.1 glass * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
44 1.1 glass * contributed to Berkeley.
45 1.1 glass *
46 1.1 glass * All advertising materials mentioning features or use of this software
47 1.1 glass * must display the following acknowledgement:
48 1.1 glass * This product includes software developed by the University of
49 1.1 glass * California, Lawrence Berkeley Laboratories.
50 1.1 glass *
51 1.7 glass * Redistribution and use in source and binary forms, with or without
52 1.7 glass * modification, are permitted provided that the following conditions
53 1.7 glass * are met:
54 1.7 glass * 1. Redistributions of source code must retain the above copyright
55 1.7 glass * notice, this list of conditions and the following disclaimer.
56 1.7 glass * 2. Redistributions in binary form must reproduce the above copyright
57 1.7 glass * notice, this list of conditions and the following disclaimer in the
58 1.7 glass * documentation and/or other materials provided with the distribution.
59 1.7 glass * 3. All advertising materials mentioning features or use of this software
60 1.7 glass * must display the following acknowledgement:
61 1.7 glass * This product includes software developed by the University of
62 1.7 glass * California, Berkeley and its contributors.
63 1.7 glass * 4. Neither the name of the University nor the names of its contributors
64 1.7 glass * may be used to endorse or promote products derived from this software
65 1.7 glass * without specific prior written permission.
66 1.1 glass *
67 1.7 glass * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
68 1.7 glass * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
69 1.7 glass * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
70 1.7 glass * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
71 1.7 glass * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
72 1.7 glass * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
73 1.7 glass * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
74 1.7 glass * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
75 1.7 glass * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
76 1.7 glass * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77 1.7 glass * SUCH DAMAGE.
78 1.1 glass *
79 1.8 cgd * from: Header: subr_autoconf.c,v 1.12 93/02/01 19:31:48 torek Exp (LBL)
80 1.9 cgd *
81 1.28 fvdl * @(#)subr_autoconf.c 8.3 (Berkeley) 5/17/94
82 1.1 glass */
83 1.1 glass
84 1.51 cgd #include <sys/cdefs.h>
85 1.62.6.3 eeh __KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.62.6.3 2002/04/06 16:04:31 eeh Exp $");
86 1.62 simonb
87 1.62 simonb #include "opt_ddb.h"
88 1.51 cgd
89 1.4 mycroft #include <sys/param.h>
90 1.4 mycroft #include <sys/device.h>
91 1.4 mycroft #include <sys/malloc.h>
92 1.17 christos #include <sys/systm.h>
93 1.43 thorpej #include <sys/kernel.h>
94 1.33 thorpej #include <sys/errno.h>
95 1.47 thorpej #include <sys/proc.h>
96 1.62.6.1 eeh #include <sys/properties.h>
97 1.16 mycroft #include <machine/limits.h>
98 1.1 glass
99 1.57 gmcgarry #include "opt_userconf.h"
100 1.57 gmcgarry #ifdef USERCONF
101 1.57 gmcgarry #include <sys/userconf.h>
102 1.57 gmcgarry #include <sys/reboot.h>
103 1.57 gmcgarry #endif
104 1.57 gmcgarry
105 1.1 glass /*
106 1.1 glass * Autoconfiguration subroutines.
107 1.1 glass */
108 1.1 glass
109 1.62.6.1 eeh static struct device *dev_create(struct device *, ssize_t, int);
110 1.62.6.1 eeh
111 1.1 glass /*
112 1.1 glass * ioconf.c exports exactly two names: cfdata and cfroots. All system
113 1.1 glass * devices and drivers are found via these tables.
114 1.1 glass */
115 1.1 glass extern struct cfdata cfdata[];
116 1.1 glass extern short cfroots[];
117 1.1 glass
118 1.1 glass #define ROOT ((struct device *)NULL)
119 1.1 glass
120 1.16 mycroft struct matchinfo {
121 1.16 mycroft cfmatch_t fn;
122 1.16 mycroft struct device *parent;
123 1.25 cgd void *aux;
124 1.25 cgd struct cfdata *match;
125 1.25 cgd int pri;
126 1.16 mycroft };
127 1.17 christos
128 1.51 cgd static char *number(char *, int);
129 1.62.6.1 eeh static void mapply(struct matchinfo *, struct cfdata *, struct device *);
130 1.62.6.1 eeh static void locset(struct device *, struct cfdata *);
131 1.62.6.1 eeh static void locrm(struct device *, struct cfdata *);
132 1.16 mycroft
133 1.29 thorpej struct deferred_config {
134 1.29 thorpej TAILQ_ENTRY(deferred_config) dc_queue;
135 1.29 thorpej struct device *dc_dev;
136 1.51 cgd void (*dc_func)(struct device *);
137 1.29 thorpej };
138 1.29 thorpej
139 1.42 thorpej TAILQ_HEAD(deferred_config_head, deferred_config);
140 1.29 thorpej
141 1.42 thorpej struct deferred_config_head deferred_config_queue;
142 1.42 thorpej struct deferred_config_head interrupt_config_queue;
143 1.42 thorpej
144 1.51 cgd static void config_process_deferred(struct deferred_config_head *,
145 1.51 cgd struct device *);
146 1.29 thorpej
147 1.56 thorpej /* list of all devices */
148 1.56 thorpej struct devicelist alldevs;
149 1.56 thorpej
150 1.56 thorpej /* list of all events */
151 1.56 thorpej struct evcntlist allevents = TAILQ_HEAD_INITIALIZER(allevents);
152 1.20 cgd
153 1.47 thorpej __volatile int config_pending; /* semaphore for mountroot */
154 1.47 thorpej
155 1.62.6.1 eeh /* device properties database */
156 1.62.6.1 eeh static propdb_t devpropdb;
157 1.62.6.1 eeh
158 1.20 cgd /*
159 1.41 thorpej * Configure the system's hardware.
160 1.20 cgd */
161 1.20 cgd void
162 1.51 cgd configure(void)
163 1.20 cgd {
164 1.20 cgd
165 1.29 thorpej TAILQ_INIT(&deferred_config_queue);
166 1.42 thorpej TAILQ_INIT(&interrupt_config_queue);
167 1.41 thorpej TAILQ_INIT(&alldevs);
168 1.57 gmcgarry
169 1.62.6.1 eeh devpropdb = propdb_create("devprop");
170 1.62.6.1 eeh
171 1.57 gmcgarry #ifdef USERCONF
172 1.57 gmcgarry if (boothowto & RB_USERCONF)
173 1.57 gmcgarry user_config();
174 1.57 gmcgarry #endif
175 1.41 thorpej
176 1.41 thorpej /*
177 1.41 thorpej * Do the machine-dependent portion of autoconfiguration. This
178 1.41 thorpej * sets the configuration machinery here in motion by "finding"
179 1.41 thorpej * the root bus. When this function returns, we expect interrupts
180 1.41 thorpej * to be enabled.
181 1.41 thorpej */
182 1.41 thorpej cpu_configure();
183 1.43 thorpej
184 1.43 thorpej /*
185 1.43 thorpej * Now that we've found all the hardware, start the real time
186 1.43 thorpej * and statistics clocks.
187 1.43 thorpej */
188 1.43 thorpej initclocks();
189 1.43 thorpej
190 1.43 thorpej cold = 0; /* clocks are running, we're warm now! */
191 1.42 thorpej
192 1.42 thorpej /*
193 1.42 thorpej * Now callback to finish configuration for devices which want
194 1.42 thorpej * to do this once interrupts are enabled.
195 1.42 thorpej */
196 1.42 thorpej config_process_deferred(&interrupt_config_queue, NULL);
197 1.20 cgd }
198 1.20 cgd
199 1.1 glass /*
200 1.62.6.1 eeh * Set locators as properties on device node.
201 1.62.6.1 eeh */
202 1.62.6.1 eeh static void
203 1.62.6.1 eeh locset(struct device *d, struct cfdata *cf)
204 1.62.6.1 eeh {
205 1.62.6.1 eeh int i;
206 1.62.6.1 eeh int type = PROP_CONST|PROP_INT;
207 1.62.6.1 eeh char buf[32];
208 1.62.6.1 eeh
209 1.62.6.2 eeh dev_setprop(d, "cd-name", cf->cf_driver->cd_name,
210 1.62.6.3 eeh strlen(cf->cf_driver->cd_name) + 1,
211 1.62.6.3 eeh PROP_STRING|PROP_CONST, (!cold));
212 1.62.6.2 eeh
213 1.62.6.1 eeh for (i=0; cf->cf_locnames[i]; i++) {
214 1.62.6.1 eeh sprintf(buf, "loc-%s", cf->cf_locnames[i]);
215 1.62.6.1 eeh dev_setprop(d, buf, &cf->cf_loc[i],
216 1.62.6.1 eeh sizeof(int), type, (!cold));
217 1.62.6.1 eeh }
218 1.62.6.1 eeh }
219 1.62.6.1 eeh
220 1.62.6.1 eeh /*
221 1.62.6.1 eeh * Remove locator properties from device node.
222 1.62.6.1 eeh */
223 1.62.6.1 eeh static void
224 1.62.6.1 eeh locrm(struct device *d, struct cfdata *cf)
225 1.62.6.1 eeh {
226 1.62.6.1 eeh int i;
227 1.62.6.1 eeh char buf[32];
228 1.62.6.1 eeh
229 1.62.6.2 eeh dev_delprop(d, "cd-name");
230 1.62.6.1 eeh for (i=0; cf->cf_locnames[i]; i++) {
231 1.62.6.1 eeh sprintf(buf, "loc-%s", cf->cf_locnames[i]);
232 1.62.6.1 eeh dev_delprop(d, buf);
233 1.62.6.1 eeh }
234 1.62.6.1 eeh }
235 1.62.6.1 eeh
236 1.62.6.1 eeh /*
237 1.1 glass * Apply the matching function and choose the best. This is used
238 1.1 glass * a few times and we want to keep the code small.
239 1.1 glass */
240 1.16 mycroft static void
241 1.62.6.1 eeh mapply(struct matchinfo *m, struct cfdata *cf, struct device *child)
242 1.1 glass {
243 1.50 augustss int pri;
244 1.62.6.1 eeh void *aux = m->aux;
245 1.1 glass
246 1.1 glass if (m->fn != NULL)
247 1.62.6.1 eeh /* Someday the submatch function should use properties too. */
248 1.25 cgd pri = (*m->fn)(m->parent, cf, m->aux);
249 1.3 glass else {
250 1.62.6.2 eeh if ((ssize_t)cf->cf_attach->ca_devsize < 0) {
251 1.62.6.2 eeh /* New-style device driver */
252 1.62.6.2 eeh if (!child)
253 1.62.6.2 eeh panic("mapply: no dev for new-style driver\n");
254 1.62.6.2 eeh locset(child, cf);
255 1.62.6.2 eeh child->dv_private = aux;
256 1.62.6.2 eeh aux = child;
257 1.62.6.2 eeh }
258 1.62.6.2 eeh
259 1.62.6.2 eeh if (cf->cf_attach->ca_match == NULL) {
260 1.16 mycroft panic("mapply: no match function for '%s' device\n",
261 1.62.6.2 eeh cf->cf_driver->cd_name);
262 1.3 glass }
263 1.62.6.1 eeh pri = (*cf->cf_attach->ca_match)(m->parent, cf, aux);
264 1.3 glass }
265 1.1 glass if (pri > m->pri) {
266 1.25 cgd m->match = cf;
267 1.1 glass m->pri = pri;
268 1.1 glass }
269 1.62.6.1 eeh if (child) {
270 1.62.6.1 eeh locrm(child, cf);
271 1.62.6.1 eeh child->dv_private = NULL;
272 1.62.6.1 eeh }
273 1.1 glass }
274 1.1 glass
275 1.1 glass /*
276 1.1 glass * Iterate over all potential children of some device, calling the given
277 1.1 glass * function (default being the child's match function) for each one.
278 1.1 glass * Nonzero returns are matches; the highest value returned is considered
279 1.1 glass * the best match. Return the `found child' if we got a match, or NULL
280 1.1 glass * otherwise. The `aux' pointer is simply passed on through.
281 1.1 glass *
282 1.1 glass * Note that this function is designed so that it can be used to apply
283 1.1 glass * an arbitrary function to all potential children (its return value
284 1.1 glass * can be ignored).
285 1.1 glass */
286 1.25 cgd struct cfdata *
287 1.62.6.1 eeh config_search_ad(cfmatch_t fn, struct device *parent, void *aux,
288 1.62.6.1 eeh struct device *child)
289 1.1 glass {
290 1.50 augustss struct cfdata *cf;
291 1.50 augustss short *p;
292 1.1 glass struct matchinfo m;
293 1.1 glass
294 1.1 glass m.fn = fn;
295 1.1 glass m.parent = parent;
296 1.25 cgd m.aux = aux;
297 1.14 mycroft m.match = NULL;
298 1.1 glass m.pri = 0;
299 1.62.6.1 eeh
300 1.1 glass for (cf = cfdata; cf->cf_driver; cf++) {
301 1.1 glass /*
302 1.1 glass * Skip cf if no longer eligible, otherwise scan through
303 1.1 glass * parents for one matching `parent', and try match function.
304 1.1 glass */
305 1.1 glass if (cf->cf_fstate == FSTATE_FOUND)
306 1.1 glass continue;
307 1.1 glass for (p = cf->cf_parents; *p >= 0; p++)
308 1.1 glass if (parent->dv_cfdata == &cfdata[*p])
309 1.62.6.1 eeh mapply(&m, cf, child);
310 1.1 glass }
311 1.62.6.3 eeh if (m.match == NULL && child != NULL) {
312 1.62.6.3 eeh /* Consume the device if we failed to match */
313 1.62.6.3 eeh config_detach(child, 0);
314 1.62.6.3 eeh }
315 1.1 glass return (m.match);
316 1.1 glass }
317 1.1 glass
318 1.16 mycroft /*
319 1.1 glass * Find the given root device.
320 1.1 glass * This is much like config_search, but there is no parent.
321 1.1 glass */
322 1.25 cgd struct cfdata *
323 1.62.6.1 eeh config_rootsearch_ad(cfmatch_t fn, const char *rootname, void *aux,
324 1.62.6.1 eeh struct device *root)
325 1.1 glass {
326 1.50 augustss struct cfdata *cf;
327 1.50 augustss short *p;
328 1.1 glass struct matchinfo m;
329 1.1 glass
330 1.1 glass m.fn = fn;
331 1.1 glass m.parent = ROOT;
332 1.25 cgd m.aux = aux;
333 1.14 mycroft m.match = NULL;
334 1.1 glass m.pri = 0;
335 1.1 glass /*
336 1.1 glass * Look at root entries for matching name. We do not bother
337 1.1 glass * with found-state here since only one root should ever be
338 1.1 glass * searched (and it must be done first).
339 1.1 glass */
340 1.1 glass for (p = cfroots; *p >= 0; p++) {
341 1.1 glass cf = &cfdata[*p];
342 1.1 glass if (strcmp(cf->cf_driver->cd_name, rootname) == 0)
343 1.62.6.1 eeh mapply(&m, cf, root);
344 1.1 glass }
345 1.1 glass return (m.match);
346 1.1 glass }
347 1.1 glass
348 1.52 cgd static const char *msgs[3] = { "", " not configured\n", " unsupported\n" };
349 1.1 glass
350 1.1 glass /*
351 1.1 glass * The given `aux' argument describes a device that has been found
352 1.1 glass * on the given parent, but not necessarily configured. Locate the
353 1.18 cgd * configuration data for that device (using the submatch function
354 1.18 cgd * provided, or using candidates' cd_match configuration driver
355 1.18 cgd * functions) and attach it, and return true. If the device was
356 1.1 glass * not configured, call the given `print' function and return 0.
357 1.1 glass */
358 1.21 cgd struct device *
359 1.62.6.1 eeh config_found_sad(struct device *parent, void *aux, cfprint_t print,
360 1.62.6.1 eeh cfmatch_t submatch, struct device *d)
361 1.1 glass {
362 1.25 cgd struct cfdata *cf;
363 1.25 cgd
364 1.62.6.1 eeh /*
365 1.62.6.1 eeh * Creating the dummy device here is really optional. But
366 1.62.6.1 eeh * it allows old-style bus drivers to attach new-style children.
367 1.62.6.1 eeh */
368 1.62.6.1 eeh if (!d) d = dev_create(ROOT, 0, cold ? M_NOWAIT : M_WAITOK);
369 1.62.6.1 eeh if ((cf = config_search_ad(submatch, parent, aux, d)) != NULL)
370 1.62.6.1 eeh return (config_attach_ad(parent, cf, aux, print, d));
371 1.3 glass if (print)
372 1.55 sommerfe printf("%s", msgs[(*print)(aux, parent->dv_xname)]);
373 1.21 cgd return (NULL);
374 1.1 glass }
375 1.1 glass
376 1.1 glass /*
377 1.1 glass * As above, but for root devices.
378 1.1 glass */
379 1.21 cgd struct device *
380 1.52 cgd config_rootfound(const char *rootname, void *aux)
381 1.1 glass {
382 1.25 cgd struct cfdata *cf;
383 1.62.6.1 eeh struct device *root;
384 1.25 cgd
385 1.62.6.1 eeh root = dev_create(ROOT, 0, cold ? M_NOWAIT : M_WAITOK);
386 1.62.6.1 eeh if ((cf = config_rootsearch_ad((cfmatch_t)NULL, rootname, aux, root)) != NULL)
387 1.62.6.1 eeh return (config_attach_ad(ROOT, cf, aux, (cfprint_t)NULL, root));
388 1.24 christos printf("root device %s not configured\n", rootname);
389 1.21 cgd return (NULL);
390 1.1 glass }
391 1.1 glass
392 1.1 glass /* just like sprintf(buf, "%d") except that it works from the end */
393 1.1 glass static char *
394 1.51 cgd number(char *ep, int n)
395 1.1 glass {
396 1.1 glass
397 1.1 glass *--ep = 0;
398 1.1 glass while (n >= 10) {
399 1.1 glass *--ep = (n % 10) + '0';
400 1.1 glass n /= 10;
401 1.1 glass }
402 1.1 glass *--ep = n + '0';
403 1.1 glass return (ep);
404 1.1 glass }
405 1.1 glass
406 1.1 glass /*
407 1.62.6.1 eeh * Allocate an empty device node. If `parent' is provided then the
408 1.62.6.1 eeh * new node is attached to the tree under `parent'. If `size'
409 1.62.6.1 eeh * is provided, then the new device node is allocated to be that
410 1.62.6.1 eeh * size.
411 1.62.6.1 eeh */
412 1.62.6.1 eeh static struct device *
413 1.62.6.1 eeh dev_create(struct device *parent, ssize_t size, int wait) {
414 1.62.6.1 eeh struct device * dev;
415 1.62.6.1 eeh
416 1.62.6.1 eeh /* get memory for all device vars */
417 1.62.6.1 eeh if (size == 0)
418 1.62.6.1 eeh size = sizeof(struct device);
419 1.62.6.1 eeh dev = (struct device *)malloc(size, M_DEVBUF, wait);
420 1.62.6.1 eeh if (!dev)
421 1.62.6.1 eeh panic("dev_create: memory allocation for device failed");
422 1.62.6.1 eeh memset(dev, 0, size);
423 1.62.6.1 eeh TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */
424 1.62.6.1 eeh dev->dv_class = DV_EMPTY;
425 1.62.6.1 eeh if (parent)
426 1.62.6.1 eeh dev->dv_parent = parent;
427 1.62.6.1 eeh return (dev);
428 1.62.6.1 eeh }
429 1.62.6.1 eeh
430 1.62.6.1 eeh /*
431 1.59 augustss * Expand the size of the cd_devs array if necessary.
432 1.59 augustss */
433 1.59 augustss void
434 1.59 augustss config_makeroom(int n, struct cfdriver *cd)
435 1.59 augustss {
436 1.59 augustss int old, new;
437 1.59 augustss void **nsp;
438 1.59 augustss
439 1.59 augustss if (n < cd->cd_ndevs)
440 1.59 augustss return;
441 1.59 augustss
442 1.59 augustss /*
443 1.59 augustss * Need to expand the array.
444 1.59 augustss */
445 1.59 augustss old = cd->cd_ndevs;
446 1.61 thorpej if (old == 0)
447 1.59 augustss new = MINALLOCSIZE / sizeof(void *);
448 1.59 augustss else
449 1.59 augustss new = old * 2;
450 1.59 augustss while (new <= n)
451 1.59 augustss new *= 2;
452 1.59 augustss cd->cd_ndevs = new;
453 1.59 augustss nsp = malloc(new * sizeof(void *), M_DEVBUF,
454 1.59 augustss cold ? M_NOWAIT : M_WAITOK);
455 1.60 augustss if (nsp == NULL)
456 1.59 augustss panic("config_attach: %sing dev array",
457 1.59 augustss old != 0 ? "expand" : "creat");
458 1.59 augustss memset(nsp + old, 0, (new - old) * sizeof(void *));
459 1.61 thorpej if (old != 0) {
460 1.59 augustss memcpy(nsp, cd->cd_devs, old * sizeof(void *));
461 1.59 augustss free(cd->cd_devs, M_DEVBUF);
462 1.59 augustss }
463 1.59 augustss cd->cd_devs = nsp;
464 1.59 augustss }
465 1.59 augustss
466 1.59 augustss /*
467 1.1 glass * Attach a found device. Allocates memory for device variables.
468 1.1 glass */
469 1.25 cgd struct device *
470 1.62.6.1 eeh config_attach_ad(struct device *parent, struct cfdata *cf, void *aux,
471 1.62.6.1 eeh cfprint_t print, struct device *propdev)
472 1.25 cgd {
473 1.50 augustss struct device *dev;
474 1.50 augustss struct cfdriver *cd;
475 1.50 augustss struct cfattach *ca;
476 1.50 augustss size_t lname, lunit;
477 1.62.6.1 eeh ssize_t softsize;
478 1.52 cgd const char *xunit;
479 1.25 cgd int myunit;
480 1.25 cgd char num[10];
481 1.25 cgd
482 1.25 cgd cd = cf->cf_driver;
483 1.25 cgd ca = cf->cf_attach;
484 1.62.6.1 eeh softsize = ca->ca_devsize;
485 1.62.6.1 eeh
486 1.46 cgd #ifndef __BROKEN_CONFIG_UNIT_USAGE
487 1.45 cgd if (cf->cf_fstate == FSTATE_STAR) {
488 1.45 cgd for (myunit = cf->cf_unit; myunit < cd->cd_ndevs; myunit++)
489 1.45 cgd if (cd->cd_devs[myunit] == NULL)
490 1.45 cgd break;
491 1.45 cgd /*
492 1.45 cgd * myunit is now the unit of the first NULL device pointer,
493 1.45 cgd * or max(cd->cd_ndevs,cf->cf_unit).
494 1.45 cgd */
495 1.45 cgd } else {
496 1.45 cgd myunit = cf->cf_unit;
497 1.46 cgd #else /* __BROKEN_CONFIG_UNIT_USAGE */
498 1.46 cgd myunit = cf->cf_unit;
499 1.46 cgd if (cf->cf_fstate == FSTATE_STAR)
500 1.46 cgd cf->cf_unit++;
501 1.46 cgd else {
502 1.46 cgd #endif /* __BROKEN_CONFIG_UNIT_USAGE */
503 1.25 cgd KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
504 1.25 cgd cf->cf_fstate = FSTATE_FOUND;
505 1.25 cgd }
506 1.25 cgd
507 1.25 cgd /* compute length of name and decimal expansion of unit number */
508 1.25 cgd lname = strlen(cd->cd_name);
509 1.30 perry xunit = number(&num[sizeof(num)], myunit);
510 1.30 perry lunit = &num[sizeof(num)] - xunit;
511 1.25 cgd if (lname + lunit >= sizeof(dev->dv_xname))
512 1.25 cgd panic("config_attach: device name too long");
513 1.25 cgd
514 1.62.6.2 eeh /*
515 1.62.6.2 eeh * XXXXX We will continue to use the old-style device+softc in one
516 1.62.6.2 eeh * until all drivers are fixed to use DEV_PRIVATE() and not to refer
517 1.62.6.2 eeh * to sc->sc_dev.
518 1.62.6.2 eeh */
519 1.62.6.2 eeh if (softsize < 0) softsize = -softsize;
520 1.62.6.2 eeh
521 1.25 cgd /* get memory for all device vars */
522 1.62.6.1 eeh if (softsize < 0) {
523 1.62.6.1 eeh /* New-style device */
524 1.62.6.1 eeh if (propdev) {
525 1.62.6.1 eeh /* Great, we can use the propdev. */
526 1.62.6.1 eeh dev = propdev;
527 1.62.6.1 eeh } else {
528 1.62.6.1 eeh dev = dev_create(parent, 0, cold ? M_NOWAIT : M_WAITOK);
529 1.62.6.1 eeh if (!dev)
530 1.62.6.1 eeh panic("config_attach: memory allocation for device failed");
531 1.62.6.1 eeh }
532 1.62.6.1 eeh if (softsize < 0) softsize = -softsize;
533 1.62.6.1 eeh if (softsize < sizeof(struct device)) panic("config_attach");
534 1.62.6.1 eeh dev->dv_private = malloc(softsize, M_DEVBUF,
535 1.62.6.1 eeh cold ? M_NOWAIT : M_WAITOK);
536 1.62.6.1 eeh if (!dev->dv_private)
537 1.62.6.1 eeh panic("config_attach: memory allocation for device softc failed");
538 1.62.6.1 eeh memset(dev->dv_private, 0, softsize);
539 1.62.6.1 eeh dev->dv_flags |= DVF_SOFTC;
540 1.62.6.1 eeh } else {
541 1.62.6.1 eeh dev = (struct device *)malloc(softsize, M_DEVBUF,
542 1.62.6.1 eeh cold ? M_NOWAIT : M_WAITOK);
543 1.62.6.1 eeh if (!dev)
544 1.62.6.1 eeh panic("config_attach: memory allocation for device softc failed");
545 1.62.6.1 eeh memset(dev, 0, softsize);
546 1.62.6.1 eeh TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */
547 1.62.6.1 eeh dev->dv_private = dev; /* Point private to ourself... */
548 1.62.6.1 eeh }
549 1.25 cgd dev->dv_class = cd->cd_class;
550 1.25 cgd dev->dv_cfdata = cf;
551 1.25 cgd dev->dv_unit = myunit;
552 1.31 perry memcpy(dev->dv_xname, cd->cd_name, lname);
553 1.31 perry memcpy(dev->dv_xname + lname, xunit, lunit);
554 1.25 cgd dev->dv_parent = parent;
555 1.62.6.1 eeh dev->dv_flags |= DVF_ACTIVE; /* always initially active */
556 1.62.6.2 eeh /* Set the locators once again. */
557 1.62.6.2 eeh locset(dev, cf);
558 1.62.6.1 eeh if (propdev && propdev != dev) {
559 1.62.6.1 eeh /* Inherit (steal) properties from dummy device */
560 1.62.6.1 eeh dev_copyprops(propdev, dev, (!cold));
561 1.62.6.1 eeh dev_delprop(propdev, NULL);
562 1.62.6.1 eeh /* destroy propdev */
563 1.62.6.1 eeh config_detach(propdev, 0);
564 1.62.6.1 eeh }
565 1.29 thorpej
566 1.25 cgd if (parent == ROOT)
567 1.25 cgd printf("%s (root)", dev->dv_xname);
568 1.25 cgd else {
569 1.25 cgd printf("%s at %s", dev->dv_xname, parent->dv_xname);
570 1.25 cgd if (print)
571 1.52 cgd (void) (*print)(aux, NULL);
572 1.25 cgd }
573 1.25 cgd
574 1.25 cgd /* put this device in the devices array */
575 1.59 augustss config_makeroom(dev->dv_unit, cd);
576 1.25 cgd if (cd->cd_devs[dev->dv_unit])
577 1.25 cgd panic("config_attach: duplicate %s", dev->dv_xname);
578 1.25 cgd cd->cd_devs[dev->dv_unit] = dev;
579 1.25 cgd
580 1.25 cgd /*
581 1.25 cgd * Before attaching, clobber any unfound devices that are
582 1.45 cgd * otherwise identical.
583 1.25 cgd */
584 1.46 cgd #ifdef __BROKEN_CONFIG_UNIT_USAGE
585 1.46 cgd /* bump the unit number on all starred cfdata for this device. */
586 1.46 cgd #endif /* __BROKEN_CONFIG_UNIT_USAGE */
587 1.25 cgd for (cf = cfdata; cf->cf_driver; cf++)
588 1.25 cgd if (cf->cf_driver == cd && cf->cf_unit == dev->dv_unit) {
589 1.25 cgd if (cf->cf_fstate == FSTATE_NOTFOUND)
590 1.25 cgd cf->cf_fstate = FSTATE_FOUND;
591 1.46 cgd #ifdef __BROKEN_CONFIG_UNIT_USAGE
592 1.46 cgd if (cf->cf_fstate == FSTATE_STAR)
593 1.46 cgd cf->cf_unit++;
594 1.46 cgd #endif /* __BROKEN_CONFIG_UNIT_USAGE */
595 1.25 cgd }
596 1.49 danw #ifdef __HAVE_DEVICE_REGISTER
597 1.25 cgd device_register(dev, aux);
598 1.25 cgd #endif
599 1.25 cgd (*ca->ca_attach)(parent, dev, aux);
600 1.42 thorpej config_process_deferred(&deferred_config_queue, dev);
601 1.25 cgd return (dev);
602 1.25 cgd }
603 1.29 thorpej
604 1.29 thorpej /*
605 1.33 thorpej * Detach a device. Optionally forced (e.g. because of hardware
606 1.33 thorpej * removal) and quiet. Returns zero if successful, non-zero
607 1.33 thorpej * (an error code) otherwise.
608 1.33 thorpej *
609 1.33 thorpej * Note that this code wants to be run from a process context, so
610 1.33 thorpej * that the detach can sleep to allow processes which have a device
611 1.33 thorpej * open to run and unwind their stacks.
612 1.33 thorpej */
613 1.33 thorpej int
614 1.51 cgd config_detach(struct device *dev, int flags)
615 1.33 thorpej {
616 1.33 thorpej struct cfdata *cf;
617 1.33 thorpej struct cfattach *ca;
618 1.33 thorpej struct cfdriver *cd;
619 1.33 thorpej #ifdef DIAGNOSTIC
620 1.33 thorpej struct device *d;
621 1.33 thorpej #endif
622 1.36 thorpej int rv = 0, i;
623 1.33 thorpej
624 1.62.6.1 eeh if (dev->dv_class == DV_EMPTY) {
625 1.62.6.1 eeh /*
626 1.62.6.1 eeh * The device is a dummy that never fully attached.
627 1.62.6.1 eeh * Simply remove it from the global list and free it.
628 1.62.6.1 eeh */
629 1.62.6.1 eeh TAILQ_REMOVE(&alldevs, dev, dv_list);
630 1.62.6.1 eeh dev_delprop(dev, NULL);
631 1.62.6.1 eeh if (dev->dv_flags & DVF_SOFTC)
632 1.62.6.1 eeh /* Separately allocated softc */
633 1.62.6.1 eeh free(dev->dv_private, M_DEVBUF);
634 1.62.6.1 eeh free(dev, M_DEVBUF);
635 1.62.6.1 eeh return (0);
636 1.62.6.1 eeh }
637 1.62.6.1 eeh
638 1.33 thorpej cf = dev->dv_cfdata;
639 1.33 thorpej #ifdef DIAGNOSTIC
640 1.33 thorpej if (cf->cf_fstate != FSTATE_FOUND && cf->cf_fstate != FSTATE_STAR)
641 1.33 thorpej panic("config_detach: bad device fstate");
642 1.33 thorpej #endif
643 1.33 thorpej ca = cf->cf_attach;
644 1.33 thorpej cd = cf->cf_driver;
645 1.62.6.1 eeh
646 1.33 thorpej /*
647 1.33 thorpej * Ensure the device is deactivated. If the device doesn't
648 1.33 thorpej * have an activation entry point, we allow DVF_ACTIVE to
649 1.33 thorpej * remain set. Otherwise, if DVF_ACTIVE is still set, the
650 1.33 thorpej * device is busy, and the detach fails.
651 1.33 thorpej */
652 1.35 thorpej if (ca->ca_activate != NULL)
653 1.35 thorpej rv = config_deactivate(dev);
654 1.62.6.1 eeh
655 1.33 thorpej /*
656 1.33 thorpej * Try to detach the device. If that's not possible, then
657 1.33 thorpej * we either panic() (for the forced but failed case), or
658 1.33 thorpej * return an error.
659 1.33 thorpej */
660 1.33 thorpej if (rv == 0) {
661 1.33 thorpej if (ca->ca_detach != NULL)
662 1.33 thorpej rv = (*ca->ca_detach)(dev, flags);
663 1.33 thorpej else
664 1.33 thorpej rv = EOPNOTSUPP;
665 1.33 thorpej }
666 1.33 thorpej if (rv != 0) {
667 1.33 thorpej if ((flags & DETACH_FORCE) == 0)
668 1.33 thorpej return (rv);
669 1.33 thorpej else
670 1.33 thorpej panic("config_detach: forced detach of %s failed (%d)",
671 1.62.6.1 eeh dev->dv_xname, rv);
672 1.33 thorpej }
673 1.62.6.1 eeh
674 1.33 thorpej /*
675 1.33 thorpej * The device has now been successfully detached.
676 1.33 thorpej */
677 1.62.6.1 eeh
678 1.33 thorpej #ifdef DIAGNOSTIC
679 1.33 thorpej /*
680 1.33 thorpej * Sanity: If you're successfully detached, you should have no
681 1.33 thorpej * children. (Note that because children must be attached
682 1.33 thorpej * after parents, we only need to search the latter part of
683 1.33 thorpej * the list.)
684 1.33 thorpej */
685 1.33 thorpej for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
686 1.62.6.1 eeh d = TAILQ_NEXT(d, dv_list)) {
687 1.48 enami if (d->dv_parent == dev) {
688 1.48 enami printf("config_detach: detached device %s"
689 1.62.6.1 eeh " has children %s\n", dev->dv_xname, d->dv_xname);
690 1.48 enami panic("config_detach");
691 1.48 enami }
692 1.33 thorpej }
693 1.33 thorpej #endif
694 1.62.6.1 eeh
695 1.33 thorpej /*
696 1.33 thorpej * Mark cfdata to show that the unit can be reused, if possible.
697 1.33 thorpej */
698 1.46 cgd #ifdef __BROKEN_CONFIG_UNIT_USAGE
699 1.46 cgd /*
700 1.46 cgd * Note that we can only re-use a starred unit number if the unit
701 1.46 cgd * being detached had the last assigned unit number.
702 1.46 cgd */
703 1.46 cgd #endif /* __BROKEN_CONFIG_UNIT_USAGE */
704 1.33 thorpej for (cf = cfdata; cf->cf_driver; cf++) {
705 1.33 thorpej if (cf->cf_driver == cd) {
706 1.33 thorpej if (cf->cf_fstate == FSTATE_FOUND &&
707 1.33 thorpej cf->cf_unit == dev->dv_unit)
708 1.33 thorpej cf->cf_fstate = FSTATE_NOTFOUND;
709 1.46 cgd #ifdef __BROKEN_CONFIG_UNIT_USAGE
710 1.46 cgd if (cf->cf_fstate == FSTATE_STAR &&
711 1.46 cgd cf->cf_unit == dev->dv_unit + 1)
712 1.46 cgd cf->cf_unit--;
713 1.46 cgd #endif /* __BROKEN_CONFIG_UNIT_USAGE */
714 1.33 thorpej }
715 1.33 thorpej }
716 1.33 thorpej
717 1.33 thorpej /*
718 1.33 thorpej * Unlink from device list.
719 1.33 thorpej */
720 1.33 thorpej TAILQ_REMOVE(&alldevs, dev, dv_list);
721 1.33 thorpej
722 1.33 thorpej /*
723 1.33 thorpej * Remove from cfdriver's array, tell the world, and free softc.
724 1.33 thorpej */
725 1.62.6.1 eeh if (cd) {
726 1.62.6.1 eeh cd->cd_devs[dev->dv_unit] = NULL;
727 1.62.6.1 eeh if ((flags & DETACH_QUIET) == 0)
728 1.62.6.1 eeh printf("%s detached\n", dev->dv_xname);
729 1.62.6.1 eeh }
730 1.62.6.1 eeh dev_delprop(dev, NULL);
731 1.62.6.1 eeh if (dev->dv_flags & DVF_SOFTC)
732 1.62.6.1 eeh /* Separately allocated softc */
733 1.62.6.1 eeh free(dev->dv_private, M_DEVBUF);
734 1.33 thorpej free(dev, M_DEVBUF);
735 1.33 thorpej
736 1.33 thorpej /*
737 1.33 thorpej * If the device now has no units in use, deallocate its softc array.
738 1.33 thorpej */
739 1.33 thorpej for (i = 0; i < cd->cd_ndevs; i++)
740 1.33 thorpej if (cd->cd_devs[i] != NULL)
741 1.33 thorpej break;
742 1.33 thorpej if (i == cd->cd_ndevs) { /* nothing found; deallocate */
743 1.33 thorpej free(cd->cd_devs, M_DEVBUF);
744 1.33 thorpej cd->cd_devs = NULL;
745 1.33 thorpej cd->cd_ndevs = 0;
746 1.33 thorpej }
747 1.33 thorpej
748 1.33 thorpej /*
749 1.33 thorpej * Return success.
750 1.33 thorpej */
751 1.33 thorpej return (0);
752 1.33 thorpej }
753 1.33 thorpej
754 1.33 thorpej int
755 1.51 cgd config_activate(struct device *dev)
756 1.33 thorpej {
757 1.33 thorpej struct cfattach *ca = dev->dv_cfdata->cf_attach;
758 1.34 thorpej int rv = 0, oflags = dev->dv_flags;
759 1.33 thorpej
760 1.33 thorpej if (ca->ca_activate == NULL)
761 1.33 thorpej return (EOPNOTSUPP);
762 1.33 thorpej
763 1.33 thorpej if ((dev->dv_flags & DVF_ACTIVE) == 0) {
764 1.33 thorpej dev->dv_flags |= DVF_ACTIVE;
765 1.33 thorpej rv = (*ca->ca_activate)(dev, DVACT_ACTIVATE);
766 1.34 thorpej if (rv)
767 1.34 thorpej dev->dv_flags = oflags;
768 1.33 thorpej }
769 1.33 thorpej return (rv);
770 1.33 thorpej }
771 1.33 thorpej
772 1.33 thorpej int
773 1.51 cgd config_deactivate(struct device *dev)
774 1.33 thorpej {
775 1.33 thorpej struct cfattach *ca = dev->dv_cfdata->cf_attach;
776 1.34 thorpej int rv = 0, oflags = dev->dv_flags;
777 1.33 thorpej
778 1.33 thorpej if (ca->ca_activate == NULL)
779 1.33 thorpej return (EOPNOTSUPP);
780 1.33 thorpej
781 1.33 thorpej if (dev->dv_flags & DVF_ACTIVE) {
782 1.33 thorpej dev->dv_flags &= ~DVF_ACTIVE;
783 1.33 thorpej rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE);
784 1.34 thorpej if (rv)
785 1.34 thorpej dev->dv_flags = oflags;
786 1.33 thorpej }
787 1.33 thorpej return (rv);
788 1.33 thorpej }
789 1.33 thorpej
790 1.33 thorpej /*
791 1.29 thorpej * Defer the configuration of the specified device until all
792 1.29 thorpej * of its parent's devices have been attached.
793 1.29 thorpej */
794 1.29 thorpej void
795 1.51 cgd config_defer(struct device *dev, void (*func)(struct device *))
796 1.29 thorpej {
797 1.29 thorpej struct deferred_config *dc;
798 1.29 thorpej
799 1.29 thorpej if (dev->dv_parent == NULL)
800 1.29 thorpej panic("config_defer: can't defer config of a root device");
801 1.29 thorpej
802 1.29 thorpej #ifdef DIAGNOSTIC
803 1.29 thorpej for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL;
804 1.29 thorpej dc = TAILQ_NEXT(dc, dc_queue)) {
805 1.29 thorpej if (dc->dc_dev == dev)
806 1.29 thorpej panic("config_defer: deferred twice");
807 1.29 thorpej }
808 1.29 thorpej #endif
809 1.29 thorpej
810 1.43 thorpej dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
811 1.43 thorpej if (dc == NULL)
812 1.43 thorpej panic("config_defer: unable to allocate callback");
813 1.29 thorpej
814 1.29 thorpej dc->dc_dev = dev;
815 1.29 thorpej dc->dc_func = func;
816 1.29 thorpej TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
817 1.47 thorpej config_pending_incr();
818 1.29 thorpej }
819 1.29 thorpej
820 1.29 thorpej /*
821 1.42 thorpej * Defer some autoconfiguration for a device until after interrupts
822 1.42 thorpej * are enabled.
823 1.42 thorpej */
824 1.42 thorpej void
825 1.51 cgd config_interrupts(struct device *dev, void (*func)(struct device *))
826 1.42 thorpej {
827 1.42 thorpej struct deferred_config *dc;
828 1.42 thorpej
829 1.42 thorpej /*
830 1.42 thorpej * If interrupts are enabled, callback now.
831 1.42 thorpej */
832 1.43 thorpej if (cold == 0) {
833 1.42 thorpej (*func)(dev);
834 1.42 thorpej return;
835 1.42 thorpej }
836 1.42 thorpej
837 1.42 thorpej #ifdef DIAGNOSTIC
838 1.42 thorpej for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL;
839 1.42 thorpej dc = TAILQ_NEXT(dc, dc_queue)) {
840 1.42 thorpej if (dc->dc_dev == dev)
841 1.42 thorpej panic("config_interrupts: deferred twice");
842 1.42 thorpej }
843 1.42 thorpej #endif
844 1.42 thorpej
845 1.43 thorpej dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
846 1.43 thorpej if (dc == NULL)
847 1.43 thorpej panic("config_interrupts: unable to allocate callback");
848 1.42 thorpej
849 1.42 thorpej dc->dc_dev = dev;
850 1.42 thorpej dc->dc_func = func;
851 1.42 thorpej TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue);
852 1.47 thorpej config_pending_incr();
853 1.42 thorpej }
854 1.42 thorpej
855 1.42 thorpej /*
856 1.42 thorpej * Process a deferred configuration queue.
857 1.29 thorpej */
858 1.29 thorpej static void
859 1.51 cgd config_process_deferred(struct deferred_config_head *queue,
860 1.51 cgd struct device *parent)
861 1.29 thorpej {
862 1.29 thorpej struct deferred_config *dc, *ndc;
863 1.29 thorpej
864 1.42 thorpej for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) {
865 1.29 thorpej ndc = TAILQ_NEXT(dc, dc_queue);
866 1.42 thorpej if (parent == NULL || dc->dc_dev->dv_parent == parent) {
867 1.42 thorpej TAILQ_REMOVE(queue, dc, dc_queue);
868 1.29 thorpej (*dc->dc_func)(dc->dc_dev);
869 1.29 thorpej free(dc, M_DEVBUF);
870 1.47 thorpej config_pending_decr();
871 1.29 thorpej }
872 1.29 thorpej }
873 1.47 thorpej }
874 1.47 thorpej
875 1.47 thorpej /*
876 1.47 thorpej * Manipulate the config_pending semaphore.
877 1.47 thorpej */
878 1.47 thorpej void
879 1.51 cgd config_pending_incr(void)
880 1.47 thorpej {
881 1.47 thorpej
882 1.47 thorpej config_pending++;
883 1.47 thorpej }
884 1.47 thorpej
885 1.47 thorpej void
886 1.51 cgd config_pending_decr(void)
887 1.47 thorpej {
888 1.47 thorpej
889 1.47 thorpej #ifdef DIAGNOSTIC
890 1.47 thorpej if (config_pending == 0)
891 1.47 thorpej panic("config_pending_decr: config_pending == 0");
892 1.47 thorpej #endif
893 1.47 thorpej config_pending--;
894 1.47 thorpej if (config_pending == 0)
895 1.47 thorpej wakeup((void *)&config_pending);
896 1.29 thorpej }
897 1.1 glass
898 1.1 glass /*
899 1.53 cgd * Attach a statically-initialized event. The type and string pointers
900 1.53 cgd * are already set up.
901 1.1 glass */
902 1.1 glass void
903 1.53 cgd evcnt_attach_static(struct evcnt *ev)
904 1.1 glass {
905 1.53 cgd int len;
906 1.1 glass
907 1.53 cgd len = strlen(ev->ev_group);
908 1.1 glass #ifdef DIAGNOSTIC
909 1.53 cgd if (len >= EVCNT_STRING_MAX) /* ..._MAX includes NUL */
910 1.53 cgd panic("evcnt_attach_static: group length (%s)", ev->ev_group);
911 1.1 glass #endif
912 1.53 cgd ev->ev_grouplen = len;
913 1.53 cgd
914 1.53 cgd len = strlen(ev->ev_name);
915 1.53 cgd #ifdef DIAGNOSTIC
916 1.53 cgd if (len >= EVCNT_STRING_MAX) /* ..._MAX includes NUL */
917 1.53 cgd panic("evcnt_attach_static: name length (%s)", ev->ev_name);
918 1.53 cgd #endif
919 1.53 cgd ev->ev_namelen = len;
920 1.53 cgd
921 1.20 cgd TAILQ_INSERT_TAIL(&allevents, ev, ev_list);
922 1.53 cgd }
923 1.53 cgd
924 1.53 cgd /*
925 1.53 cgd * Attach a dynamically-initialized event. Zero it, set up the type
926 1.53 cgd * and string pointers and then act like it was statically initialized.
927 1.53 cgd */
928 1.53 cgd void
929 1.53 cgd evcnt_attach_dynamic(struct evcnt *ev, int type, const struct evcnt *parent,
930 1.53 cgd const char *group, const char *name)
931 1.53 cgd {
932 1.53 cgd
933 1.53 cgd memset(ev, 0, sizeof *ev);
934 1.53 cgd ev->ev_type = type;
935 1.53 cgd ev->ev_parent = parent;
936 1.53 cgd ev->ev_group = group;
937 1.53 cgd ev->ev_name = name;
938 1.53 cgd evcnt_attach_static(ev);
939 1.33 thorpej }
940 1.33 thorpej
941 1.33 thorpej /*
942 1.33 thorpej * Detach an event.
943 1.33 thorpej */
944 1.33 thorpej void
945 1.51 cgd evcnt_detach(struct evcnt *ev)
946 1.33 thorpej {
947 1.33 thorpej
948 1.33 thorpej TAILQ_REMOVE(&allevents, ev, ev_list);
949 1.62.6.1 eeh }
950 1.62.6.1 eeh
951 1.62.6.1 eeh /*
952 1.62.6.1 eeh * Device property management routines.
953 1.62.6.1 eeh */
954 1.62.6.1 eeh #ifdef DEBUG
955 1.62.6.1 eeh int devprop_debug = 0;
956 1.62.6.1 eeh #endif
957 1.62.6.1 eeh
958 1.62.6.1 eeh /*
959 1.62.6.1 eeh * Create a dummy device you can attach properties to.
960 1.62.6.1 eeh */
961 1.62.6.1 eeh struct device *
962 1.62.6.1 eeh dev_config_create(struct device *parent, int wait)
963 1.62.6.1 eeh {
964 1.62.6.1 eeh struct device *dev;
965 1.62.6.1 eeh
966 1.62.6.1 eeh dev = dev_create(parent, 0, wait ? M_WAITOK : M_NOWAIT);
967 1.62.6.1 eeh return (dev);
968 1.62.6.1 eeh }
969 1.62.6.1 eeh
970 1.62.6.1 eeh
971 1.62.6.1 eeh int
972 1.62.6.1 eeh dev_setprop(struct device *dev, const char *name, void *val,
973 1.62.6.1 eeh size_t len, int type, int wait)
974 1.62.6.1 eeh {
975 1.62.6.1 eeh #ifdef DEBUG
976 1.62.6.1 eeh if (devprop_debug)
977 1.62.6.1 eeh printf("dev_setprop(%p, %s, %p %s, %ld, %x, %d)\n",
978 1.62.6.1 eeh dev, name, val, (PROP_TYPE(type) == PROP_STRING) ?
979 1.62.6.3 eeh (char *)val : "", (u_long)len, type, wait);
980 1.62.6.1 eeh #endif
981 1.62.6.1 eeh
982 1.62.6.1 eeh return (prop_set(devpropdb, dev, name, val, len, type, wait));
983 1.62.6.1 eeh }
984 1.62.6.1 eeh
985 1.62.6.1 eeh size_t
986 1.62.6.1 eeh dev_getprop(struct device *dev, const char *name, void *val,
987 1.62.6.1 eeh size_t len, int *type, int search)
988 1.62.6.1 eeh {
989 1.62.6.1 eeh ssize_t rv;
990 1.62.6.1 eeh
991 1.62.6.1 eeh #ifdef DEBUG
992 1.62.6.1 eeh if (devprop_debug) {
993 1.62.6.1 eeh printf("dev_getprop(%p, %s, %p, %ld, %p, %d)\n",
994 1.62.6.3 eeh dev, name, val, (u_long)len, type, search);
995 1.62.6.1 eeh }
996 1.62.6.1 eeh #endif
997 1.62.6.1 eeh rv = prop_get(devpropdb, dev, name, val, len, type);
998 1.62.6.3 eeh #if defined(dev_mdgetprop)
999 1.62.6.1 eeh if (rv == -1) {
1000 1.62.6.1 eeh /* Not found -- try md_getprop */
1001 1.62.6.2 eeh rv = dev_mdgetprop(dev, name, val,
1002 1.62.6.1 eeh len, type);
1003 1.62.6.1 eeh }
1004 1.62.6.3 eeh #endif
1005 1.62.6.1 eeh if ((rv == -1) && search) {
1006 1.62.6.1 eeh if (dev->dv_parent)
1007 1.62.6.1 eeh /* Tail recursion -- there should be no stack growth. */
1008 1.62.6.1 eeh return (dev_getprop(dev->dv_parent, name,
1009 1.62.6.1 eeh val, len, type, search));
1010 1.62.6.1 eeh }
1011 1.62.6.1 eeh #ifdef DEBUG
1012 1.62.6.1 eeh if (devprop_debug && (rv == -1)) {
1013 1.62.6.1 eeh printf("%s no found\n", name);
1014 1.62.6.1 eeh }
1015 1.62.6.1 eeh #endif
1016 1.62.6.1 eeh return (rv);
1017 1.62.6.1 eeh }
1018 1.62.6.1 eeh
1019 1.62.6.1 eeh int
1020 1.62.6.1 eeh dev_delprop(struct device *dev, const char *name)
1021 1.62.6.1 eeh {
1022 1.62.6.1 eeh
1023 1.62.6.1 eeh #ifdef DEBUG_N
1024 1.62.6.1 eeh if (devprop_debug)
1025 1.62.6.1 eeh printf("dev_delprop(%p, %s, %x)\n", dev, name);
1026 1.62.6.1 eeh #endif
1027 1.62.6.1 eeh return (prop_delete(devpropdb, dev, name));
1028 1.62.6.1 eeh }
1029 1.62.6.1 eeh
1030 1.62.6.1 eeh
1031 1.62.6.1 eeh int
1032 1.62.6.1 eeh dev_copyprops(struct device *src, struct device *dest, int wait)
1033 1.62.6.1 eeh {
1034 1.62.6.1 eeh return (prop_copy(devpropdb, src, dest, wait));
1035 1.1 glass }
1036 1.62.6.3 eeh
1037 1.62.6.3 eeh /*
1038 1.62.6.3 eeh * For debug: print some of the properties associated with a device.
1039 1.62.6.3 eeh *
1040 1.62.6.3 eeh * XXX not reentrant.
1041 1.62.6.3 eeh */
1042 1.62.6.3 eeh #ifdef DEBUG
1043 1.62.6.3 eeh void
1044 1.62.6.3 eeh dev_dumprops(struct device *dev)
1045 1.62.6.3 eeh {
1046 1.62.6.3 eeh size_t len;
1047 1.62.6.3 eeh int i;
1048 1.62.6.3 eeh static char buf[2048];
1049 1.62.6.3 eeh
1050 1.62.6.3 eeh len = prop_list(devpropdb, dev, buf, sizeof(buf));
1051 1.62.6.3 eeh
1052 1.62.6.3 eeh printf("Device %p has:\n", dev);
1053 1.62.6.3 eeh while (i < len) {
1054 1.62.6.3 eeh printf("%s\n", &buf[i]);
1055 1.62.6.3 eeh i += strlen(&buf[i]);
1056 1.62.6.3 eeh }
1057 1.62.6.3 eeh }
1058 1.62.6.3 eeh #endif
1059 1.62 simonb
1060 1.62 simonb #ifdef DDB
1061 1.62 simonb void
1062 1.62 simonb event_print(int full, void (*pr)(const char *, ...))
1063 1.62 simonb {
1064 1.62 simonb struct evcnt *evp;
1065 1.62 simonb
1066 1.62 simonb TAILQ_FOREACH(evp, &allevents, ev_list) {
1067 1.62 simonb if (evp->ev_count == 0 && !full)
1068 1.62 simonb continue;
1069 1.62 simonb
1070 1.62 simonb (*pr)("evcnt type %d: %s %s = %lld\n", evp->ev_type,
1071 1.62 simonb evp->ev_group, evp->ev_name, evp->ev_count);
1072 1.62 simonb }
1073 1.62 simonb }
1074 1.62 simonb #endif /* DDB */
1075