subr_autoconf.c revision 1.57 1 1.57 gmcgarry /* $NetBSD: subr_autoconf.c,v 1.57 2001/07/01 02:56:20 gmcgarry Exp $ */
2 1.53 cgd
3 1.53 cgd /*
4 1.53 cgd * Copyright (c) 1996, 2000 Christopher G. Demetriou
5 1.53 cgd * All rights reserved.
6 1.54 cgd *
7 1.53 cgd * Redistribution and use in source and binary forms, with or without
8 1.53 cgd * modification, are permitted provided that the following conditions
9 1.53 cgd * are met:
10 1.53 cgd * 1. Redistributions of source code must retain the above copyright
11 1.53 cgd * notice, this list of conditions and the following disclaimer.
12 1.53 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.53 cgd * notice, this list of conditions and the following disclaimer in the
14 1.53 cgd * documentation and/or other materials provided with the distribution.
15 1.53 cgd * 3. All advertising materials mentioning features or use of this software
16 1.53 cgd * must display the following acknowledgement:
17 1.54 cgd * This product includes software developed for the
18 1.54 cgd * NetBSD Project. See http://www.netbsd.org/ for
19 1.54 cgd * information about NetBSD.
20 1.53 cgd * 4. The name of the author may not be used to endorse or promote products
21 1.54 cgd * derived from this software without specific prior written permission.
22 1.54 cgd *
23 1.53 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 1.53 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.53 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.53 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 1.53 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 1.53 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 1.53 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 1.53 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 1.53 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 1.53 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.54 cgd *
34 1.54 cgd * --(license Id: LICENSE.proto,v 1.1 2000/06/13 21:40:26 cgd Exp )--
35 1.53 cgd */
36 1.9 cgd
37 1.1 glass /*
38 1.7 glass * Copyright (c) 1992, 1993
39 1.7 glass * The Regents of the University of California. All rights reserved.
40 1.1 glass *
41 1.1 glass * This software was developed by the Computer Systems Engineering group
42 1.1 glass * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
43 1.1 glass * contributed to Berkeley.
44 1.1 glass *
45 1.1 glass * All advertising materials mentioning features or use of this software
46 1.1 glass * must display the following acknowledgement:
47 1.1 glass * This product includes software developed by the University of
48 1.1 glass * California, Lawrence Berkeley Laboratories.
49 1.1 glass *
50 1.7 glass * Redistribution and use in source and binary forms, with or without
51 1.7 glass * modification, are permitted provided that the following conditions
52 1.7 glass * are met:
53 1.7 glass * 1. Redistributions of source code must retain the above copyright
54 1.7 glass * notice, this list of conditions and the following disclaimer.
55 1.7 glass * 2. Redistributions in binary form must reproduce the above copyright
56 1.7 glass * notice, this list of conditions and the following disclaimer in the
57 1.7 glass * documentation and/or other materials provided with the distribution.
58 1.7 glass * 3. All advertising materials mentioning features or use of this software
59 1.7 glass * must display the following acknowledgement:
60 1.7 glass * This product includes software developed by the University of
61 1.7 glass * California, Berkeley and its contributors.
62 1.7 glass * 4. Neither the name of the University nor the names of its contributors
63 1.7 glass * may be used to endorse or promote products derived from this software
64 1.7 glass * without specific prior written permission.
65 1.1 glass *
66 1.7 glass * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
67 1.7 glass * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68 1.7 glass * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
69 1.7 glass * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
70 1.7 glass * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
71 1.7 glass * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
72 1.7 glass * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
73 1.7 glass * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
74 1.7 glass * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
75 1.7 glass * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76 1.7 glass * SUCH DAMAGE.
77 1.1 glass *
78 1.8 cgd * from: Header: subr_autoconf.c,v 1.12 93/02/01 19:31:48 torek Exp (LBL)
79 1.9 cgd *
80 1.28 fvdl * @(#)subr_autoconf.c 8.3 (Berkeley) 5/17/94
81 1.1 glass */
82 1.1 glass
83 1.51 cgd #include <sys/cdefs.h>
84 1.51 cgd
85 1.57 gmcgarry __KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.57 2001/07/01 02:56:20 gmcgarry Exp $");
86 1.51 cgd
87 1.4 mycroft #include <sys/param.h>
88 1.4 mycroft #include <sys/device.h>
89 1.4 mycroft #include <sys/malloc.h>
90 1.17 christos #include <sys/systm.h>
91 1.43 thorpej #include <sys/kernel.h>
92 1.33 thorpej #include <sys/errno.h>
93 1.47 thorpej #include <sys/proc.h>
94 1.16 mycroft #include <machine/limits.h>
95 1.1 glass
96 1.57 gmcgarry #include "opt_userconf.h"
97 1.57 gmcgarry #ifdef USERCONF
98 1.57 gmcgarry #include <sys/userconf.h>
99 1.57 gmcgarry #include <sys/reboot.h>
100 1.57 gmcgarry #endif
101 1.57 gmcgarry
102 1.1 glass /*
103 1.1 glass * Autoconfiguration subroutines.
104 1.1 glass */
105 1.1 glass
106 1.1 glass /*
107 1.1 glass * ioconf.c exports exactly two names: cfdata and cfroots. All system
108 1.1 glass * devices and drivers are found via these tables.
109 1.1 glass */
110 1.1 glass extern struct cfdata cfdata[];
111 1.1 glass extern short cfroots[];
112 1.1 glass
113 1.1 glass #define ROOT ((struct device *)NULL)
114 1.1 glass
115 1.16 mycroft struct matchinfo {
116 1.16 mycroft cfmatch_t fn;
117 1.16 mycroft struct device *parent;
118 1.25 cgd void *aux;
119 1.25 cgd struct cfdata *match;
120 1.25 cgd int pri;
121 1.16 mycroft };
122 1.17 christos
123 1.51 cgd static char *number(char *, int);
124 1.51 cgd static void mapply(struct matchinfo *, struct cfdata *);
125 1.16 mycroft
126 1.29 thorpej struct deferred_config {
127 1.29 thorpej TAILQ_ENTRY(deferred_config) dc_queue;
128 1.29 thorpej struct device *dc_dev;
129 1.51 cgd void (*dc_func)(struct device *);
130 1.29 thorpej };
131 1.29 thorpej
132 1.42 thorpej TAILQ_HEAD(deferred_config_head, deferred_config);
133 1.29 thorpej
134 1.42 thorpej struct deferred_config_head deferred_config_queue;
135 1.42 thorpej struct deferred_config_head interrupt_config_queue;
136 1.42 thorpej
137 1.51 cgd static void config_process_deferred(struct deferred_config_head *,
138 1.51 cgd struct device *);
139 1.29 thorpej
140 1.56 thorpej /* list of all devices */
141 1.56 thorpej struct devicelist alldevs;
142 1.56 thorpej
143 1.56 thorpej /* list of all events */
144 1.56 thorpej struct evcntlist allevents = TAILQ_HEAD_INITIALIZER(allevents);
145 1.20 cgd
146 1.47 thorpej __volatile int config_pending; /* semaphore for mountroot */
147 1.47 thorpej
148 1.20 cgd /*
149 1.41 thorpej * Configure the system's hardware.
150 1.20 cgd */
151 1.20 cgd void
152 1.51 cgd configure(void)
153 1.20 cgd {
154 1.20 cgd
155 1.29 thorpej TAILQ_INIT(&deferred_config_queue);
156 1.42 thorpej TAILQ_INIT(&interrupt_config_queue);
157 1.41 thorpej TAILQ_INIT(&alldevs);
158 1.57 gmcgarry
159 1.57 gmcgarry #ifdef USERCONF
160 1.57 gmcgarry if (boothowto & RB_USERCONF)
161 1.57 gmcgarry user_config();
162 1.57 gmcgarry #endif
163 1.41 thorpej
164 1.41 thorpej /*
165 1.41 thorpej * Do the machine-dependent portion of autoconfiguration. This
166 1.41 thorpej * sets the configuration machinery here in motion by "finding"
167 1.41 thorpej * the root bus. When this function returns, we expect interrupts
168 1.41 thorpej * to be enabled.
169 1.41 thorpej */
170 1.41 thorpej cpu_configure();
171 1.43 thorpej
172 1.43 thorpej /*
173 1.43 thorpej * Now that we've found all the hardware, start the real time
174 1.43 thorpej * and statistics clocks.
175 1.43 thorpej */
176 1.43 thorpej initclocks();
177 1.43 thorpej
178 1.43 thorpej cold = 0; /* clocks are running, we're warm now! */
179 1.42 thorpej
180 1.42 thorpej /*
181 1.42 thorpej * Now callback to finish configuration for devices which want
182 1.42 thorpej * to do this once interrupts are enabled.
183 1.42 thorpej */
184 1.42 thorpej config_process_deferred(&interrupt_config_queue, NULL);
185 1.20 cgd }
186 1.20 cgd
187 1.1 glass /*
188 1.1 glass * Apply the matching function and choose the best. This is used
189 1.1 glass * a few times and we want to keep the code small.
190 1.1 glass */
191 1.16 mycroft static void
192 1.51 cgd mapply(struct matchinfo *m, struct cfdata *cf)
193 1.1 glass {
194 1.50 augustss int pri;
195 1.1 glass
196 1.1 glass if (m->fn != NULL)
197 1.25 cgd pri = (*m->fn)(m->parent, cf, m->aux);
198 1.3 glass else {
199 1.19 thorpej if (cf->cf_attach->ca_match == NULL) {
200 1.16 mycroft panic("mapply: no match function for '%s' device\n",
201 1.12 mycroft cf->cf_driver->cd_name);
202 1.3 glass }
203 1.25 cgd pri = (*cf->cf_attach->ca_match)(m->parent, cf, m->aux);
204 1.3 glass }
205 1.1 glass if (pri > m->pri) {
206 1.25 cgd m->match = cf;
207 1.1 glass m->pri = pri;
208 1.1 glass }
209 1.1 glass }
210 1.1 glass
211 1.1 glass /*
212 1.1 glass * Iterate over all potential children of some device, calling the given
213 1.1 glass * function (default being the child's match function) for each one.
214 1.1 glass * Nonzero returns are matches; the highest value returned is considered
215 1.1 glass * the best match. Return the `found child' if we got a match, or NULL
216 1.1 glass * otherwise. The `aux' pointer is simply passed on through.
217 1.1 glass *
218 1.1 glass * Note that this function is designed so that it can be used to apply
219 1.1 glass * an arbitrary function to all potential children (its return value
220 1.1 glass * can be ignored).
221 1.1 glass */
222 1.25 cgd struct cfdata *
223 1.51 cgd config_search(cfmatch_t fn, struct device *parent, void *aux)
224 1.1 glass {
225 1.50 augustss struct cfdata *cf;
226 1.50 augustss short *p;
227 1.1 glass struct matchinfo m;
228 1.1 glass
229 1.1 glass m.fn = fn;
230 1.1 glass m.parent = parent;
231 1.25 cgd m.aux = aux;
232 1.14 mycroft m.match = NULL;
233 1.1 glass m.pri = 0;
234 1.1 glass for (cf = cfdata; cf->cf_driver; cf++) {
235 1.1 glass /*
236 1.1 glass * Skip cf if no longer eligible, otherwise scan through
237 1.1 glass * parents for one matching `parent', and try match function.
238 1.1 glass */
239 1.1 glass if (cf->cf_fstate == FSTATE_FOUND)
240 1.1 glass continue;
241 1.1 glass for (p = cf->cf_parents; *p >= 0; p++)
242 1.1 glass if (parent->dv_cfdata == &cfdata[*p])
243 1.16 mycroft mapply(&m, cf);
244 1.1 glass }
245 1.1 glass return (m.match);
246 1.1 glass }
247 1.1 glass
248 1.16 mycroft /*
249 1.1 glass * Find the given root device.
250 1.1 glass * This is much like config_search, but there is no parent.
251 1.1 glass */
252 1.25 cgd struct cfdata *
253 1.52 cgd config_rootsearch(cfmatch_t fn, const char *rootname, void *aux)
254 1.1 glass {
255 1.50 augustss struct cfdata *cf;
256 1.50 augustss short *p;
257 1.1 glass struct matchinfo m;
258 1.1 glass
259 1.1 glass m.fn = fn;
260 1.1 glass m.parent = ROOT;
261 1.25 cgd m.aux = aux;
262 1.14 mycroft m.match = NULL;
263 1.1 glass m.pri = 0;
264 1.1 glass /*
265 1.1 glass * Look at root entries for matching name. We do not bother
266 1.1 glass * with found-state here since only one root should ever be
267 1.1 glass * searched (and it must be done first).
268 1.1 glass */
269 1.1 glass for (p = cfroots; *p >= 0; p++) {
270 1.1 glass cf = &cfdata[*p];
271 1.1 glass if (strcmp(cf->cf_driver->cd_name, rootname) == 0)
272 1.16 mycroft mapply(&m, cf);
273 1.1 glass }
274 1.1 glass return (m.match);
275 1.1 glass }
276 1.1 glass
277 1.52 cgd static const char *msgs[3] = { "", " not configured\n", " unsupported\n" };
278 1.1 glass
279 1.1 glass /*
280 1.1 glass * The given `aux' argument describes a device that has been found
281 1.1 glass * on the given parent, but not necessarily configured. Locate the
282 1.18 cgd * configuration data for that device (using the submatch function
283 1.18 cgd * provided, or using candidates' cd_match configuration driver
284 1.18 cgd * functions) and attach it, and return true. If the device was
285 1.1 glass * not configured, call the given `print' function and return 0.
286 1.1 glass */
287 1.21 cgd struct device *
288 1.51 cgd config_found_sm(struct device *parent, void *aux, cfprint_t print,
289 1.51 cgd cfmatch_t submatch)
290 1.1 glass {
291 1.25 cgd struct cfdata *cf;
292 1.25 cgd
293 1.25 cgd if ((cf = config_search(submatch, parent, aux)) != NULL)
294 1.25 cgd return (config_attach(parent, cf, aux, print));
295 1.3 glass if (print)
296 1.55 sommerfe printf("%s", msgs[(*print)(aux, parent->dv_xname)]);
297 1.21 cgd return (NULL);
298 1.1 glass }
299 1.1 glass
300 1.1 glass /*
301 1.1 glass * As above, but for root devices.
302 1.1 glass */
303 1.21 cgd struct device *
304 1.52 cgd config_rootfound(const char *rootname, void *aux)
305 1.1 glass {
306 1.25 cgd struct cfdata *cf;
307 1.25 cgd
308 1.25 cgd if ((cf = config_rootsearch((cfmatch_t)NULL, rootname, aux)) != NULL)
309 1.25 cgd return (config_attach(ROOT, cf, aux, (cfprint_t)NULL));
310 1.24 christos printf("root device %s not configured\n", rootname);
311 1.21 cgd return (NULL);
312 1.1 glass }
313 1.1 glass
314 1.1 glass /* just like sprintf(buf, "%d") except that it works from the end */
315 1.1 glass static char *
316 1.51 cgd number(char *ep, int n)
317 1.1 glass {
318 1.1 glass
319 1.1 glass *--ep = 0;
320 1.1 glass while (n >= 10) {
321 1.1 glass *--ep = (n % 10) + '0';
322 1.1 glass n /= 10;
323 1.1 glass }
324 1.1 glass *--ep = n + '0';
325 1.1 glass return (ep);
326 1.1 glass }
327 1.1 glass
328 1.1 glass /*
329 1.1 glass * Attach a found device. Allocates memory for device variables.
330 1.1 glass */
331 1.25 cgd struct device *
332 1.51 cgd config_attach(struct device *parent, struct cfdata *cf, void *aux,
333 1.51 cgd cfprint_t print)
334 1.25 cgd {
335 1.50 augustss struct device *dev;
336 1.50 augustss struct cfdriver *cd;
337 1.50 augustss struct cfattach *ca;
338 1.50 augustss size_t lname, lunit;
339 1.52 cgd const char *xunit;
340 1.25 cgd int myunit;
341 1.25 cgd char num[10];
342 1.25 cgd
343 1.25 cgd cd = cf->cf_driver;
344 1.25 cgd ca = cf->cf_attach;
345 1.25 cgd if (ca->ca_devsize < sizeof(struct device))
346 1.25 cgd panic("config_attach");
347 1.46 cgd #ifndef __BROKEN_CONFIG_UNIT_USAGE
348 1.45 cgd if (cf->cf_fstate == FSTATE_STAR) {
349 1.45 cgd for (myunit = cf->cf_unit; myunit < cd->cd_ndevs; myunit++)
350 1.45 cgd if (cd->cd_devs[myunit] == NULL)
351 1.45 cgd break;
352 1.45 cgd /*
353 1.45 cgd * myunit is now the unit of the first NULL device pointer,
354 1.45 cgd * or max(cd->cd_ndevs,cf->cf_unit).
355 1.45 cgd */
356 1.45 cgd } else {
357 1.45 cgd myunit = cf->cf_unit;
358 1.46 cgd #else /* __BROKEN_CONFIG_UNIT_USAGE */
359 1.46 cgd myunit = cf->cf_unit;
360 1.46 cgd if (cf->cf_fstate == FSTATE_STAR)
361 1.46 cgd cf->cf_unit++;
362 1.46 cgd else {
363 1.46 cgd #endif /* __BROKEN_CONFIG_UNIT_USAGE */
364 1.25 cgd KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
365 1.25 cgd cf->cf_fstate = FSTATE_FOUND;
366 1.25 cgd }
367 1.25 cgd
368 1.25 cgd /* compute length of name and decimal expansion of unit number */
369 1.25 cgd lname = strlen(cd->cd_name);
370 1.30 perry xunit = number(&num[sizeof(num)], myunit);
371 1.30 perry lunit = &num[sizeof(num)] - xunit;
372 1.25 cgd if (lname + lunit >= sizeof(dev->dv_xname))
373 1.25 cgd panic("config_attach: device name too long");
374 1.25 cgd
375 1.25 cgd /* get memory for all device vars */
376 1.43 thorpej dev = (struct device *)malloc(ca->ca_devsize, M_DEVBUF,
377 1.43 thorpej cold ? M_NOWAIT : M_WAITOK);
378 1.25 cgd if (!dev)
379 1.25 cgd panic("config_attach: memory allocation for device softc failed");
380 1.31 perry memset(dev, 0, ca->ca_devsize);
381 1.25 cgd TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */
382 1.25 cgd dev->dv_class = cd->cd_class;
383 1.25 cgd dev->dv_cfdata = cf;
384 1.25 cgd dev->dv_unit = myunit;
385 1.31 perry memcpy(dev->dv_xname, cd->cd_name, lname);
386 1.31 perry memcpy(dev->dv_xname + lname, xunit, lunit);
387 1.25 cgd dev->dv_parent = parent;
388 1.33 thorpej dev->dv_flags = DVF_ACTIVE; /* always initially active */
389 1.29 thorpej
390 1.25 cgd if (parent == ROOT)
391 1.25 cgd printf("%s (root)", dev->dv_xname);
392 1.25 cgd else {
393 1.25 cgd printf("%s at %s", dev->dv_xname, parent->dv_xname);
394 1.25 cgd if (print)
395 1.52 cgd (void) (*print)(aux, NULL);
396 1.25 cgd }
397 1.25 cgd
398 1.25 cgd /* put this device in the devices array */
399 1.25 cgd if (dev->dv_unit >= cd->cd_ndevs) {
400 1.25 cgd /*
401 1.25 cgd * Need to expand the array.
402 1.25 cgd */
403 1.25 cgd int old = cd->cd_ndevs, new;
404 1.25 cgd void **nsp;
405 1.25 cgd
406 1.25 cgd if (old == 0)
407 1.25 cgd new = MINALLOCSIZE / sizeof(void *);
408 1.25 cgd else
409 1.25 cgd new = old * 2;
410 1.25 cgd while (new <= dev->dv_unit)
411 1.25 cgd new *= 2;
412 1.25 cgd cd->cd_ndevs = new;
413 1.43 thorpej nsp = malloc(new * sizeof(void *), M_DEVBUF,
414 1.43 thorpej cold ? M_NOWAIT : M_WAITOK);
415 1.25 cgd if (nsp == 0)
416 1.25 cgd panic("config_attach: %sing dev array",
417 1.25 cgd old != 0 ? "expand" : "creat");
418 1.31 perry memset(nsp + old, 0, (new - old) * sizeof(void *));
419 1.25 cgd if (old != 0) {
420 1.31 perry memcpy(nsp, cd->cd_devs, old * sizeof(void *));
421 1.25 cgd free(cd->cd_devs, M_DEVBUF);
422 1.25 cgd }
423 1.25 cgd cd->cd_devs = nsp;
424 1.25 cgd }
425 1.25 cgd if (cd->cd_devs[dev->dv_unit])
426 1.25 cgd panic("config_attach: duplicate %s", dev->dv_xname);
427 1.25 cgd cd->cd_devs[dev->dv_unit] = dev;
428 1.25 cgd
429 1.25 cgd /*
430 1.25 cgd * Before attaching, clobber any unfound devices that are
431 1.45 cgd * otherwise identical.
432 1.25 cgd */
433 1.46 cgd #ifdef __BROKEN_CONFIG_UNIT_USAGE
434 1.46 cgd /* bump the unit number on all starred cfdata for this device. */
435 1.46 cgd #endif /* __BROKEN_CONFIG_UNIT_USAGE */
436 1.25 cgd for (cf = cfdata; cf->cf_driver; cf++)
437 1.25 cgd if (cf->cf_driver == cd && cf->cf_unit == dev->dv_unit) {
438 1.25 cgd if (cf->cf_fstate == FSTATE_NOTFOUND)
439 1.25 cgd cf->cf_fstate = FSTATE_FOUND;
440 1.46 cgd #ifdef __BROKEN_CONFIG_UNIT_USAGE
441 1.46 cgd if (cf->cf_fstate == FSTATE_STAR)
442 1.46 cgd cf->cf_unit++;
443 1.46 cgd #endif /* __BROKEN_CONFIG_UNIT_USAGE */
444 1.25 cgd }
445 1.49 danw #ifdef __HAVE_DEVICE_REGISTER
446 1.25 cgd device_register(dev, aux);
447 1.25 cgd #endif
448 1.25 cgd (*ca->ca_attach)(parent, dev, aux);
449 1.42 thorpej config_process_deferred(&deferred_config_queue, dev);
450 1.25 cgd return (dev);
451 1.25 cgd }
452 1.29 thorpej
453 1.29 thorpej /*
454 1.33 thorpej * Detach a device. Optionally forced (e.g. because of hardware
455 1.33 thorpej * removal) and quiet. Returns zero if successful, non-zero
456 1.33 thorpej * (an error code) otherwise.
457 1.33 thorpej *
458 1.33 thorpej * Note that this code wants to be run from a process context, so
459 1.33 thorpej * that the detach can sleep to allow processes which have a device
460 1.33 thorpej * open to run and unwind their stacks.
461 1.33 thorpej */
462 1.33 thorpej int
463 1.51 cgd config_detach(struct device *dev, int flags)
464 1.33 thorpej {
465 1.33 thorpej struct cfdata *cf;
466 1.33 thorpej struct cfattach *ca;
467 1.33 thorpej struct cfdriver *cd;
468 1.33 thorpej #ifdef DIAGNOSTIC
469 1.33 thorpej struct device *d;
470 1.33 thorpej #endif
471 1.36 thorpej int rv = 0, i;
472 1.33 thorpej
473 1.33 thorpej cf = dev->dv_cfdata;
474 1.33 thorpej #ifdef DIAGNOSTIC
475 1.33 thorpej if (cf->cf_fstate != FSTATE_FOUND && cf->cf_fstate != FSTATE_STAR)
476 1.33 thorpej panic("config_detach: bad device fstate");
477 1.33 thorpej #endif
478 1.33 thorpej ca = cf->cf_attach;
479 1.33 thorpej cd = cf->cf_driver;
480 1.33 thorpej
481 1.33 thorpej /*
482 1.33 thorpej * Ensure the device is deactivated. If the device doesn't
483 1.33 thorpej * have an activation entry point, we allow DVF_ACTIVE to
484 1.33 thorpej * remain set. Otherwise, if DVF_ACTIVE is still set, the
485 1.33 thorpej * device is busy, and the detach fails.
486 1.33 thorpej */
487 1.35 thorpej if (ca->ca_activate != NULL)
488 1.35 thorpej rv = config_deactivate(dev);
489 1.33 thorpej
490 1.33 thorpej /*
491 1.33 thorpej * Try to detach the device. If that's not possible, then
492 1.33 thorpej * we either panic() (for the forced but failed case), or
493 1.33 thorpej * return an error.
494 1.33 thorpej */
495 1.33 thorpej if (rv == 0) {
496 1.33 thorpej if (ca->ca_detach != NULL)
497 1.33 thorpej rv = (*ca->ca_detach)(dev, flags);
498 1.33 thorpej else
499 1.33 thorpej rv = EOPNOTSUPP;
500 1.33 thorpej }
501 1.33 thorpej if (rv != 0) {
502 1.33 thorpej if ((flags & DETACH_FORCE) == 0)
503 1.33 thorpej return (rv);
504 1.33 thorpej else
505 1.33 thorpej panic("config_detach: forced detach of %s failed (%d)",
506 1.33 thorpej dev->dv_xname, rv);
507 1.33 thorpej }
508 1.33 thorpej
509 1.33 thorpej /*
510 1.33 thorpej * The device has now been successfully detached.
511 1.33 thorpej */
512 1.33 thorpej
513 1.33 thorpej #ifdef DIAGNOSTIC
514 1.33 thorpej /*
515 1.33 thorpej * Sanity: If you're successfully detached, you should have no
516 1.33 thorpej * children. (Note that because children must be attached
517 1.33 thorpej * after parents, we only need to search the latter part of
518 1.33 thorpej * the list.)
519 1.33 thorpej */
520 1.33 thorpej for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
521 1.48 enami d = TAILQ_NEXT(d, dv_list)) {
522 1.48 enami if (d->dv_parent == dev) {
523 1.48 enami printf("config_detach: detached device %s"
524 1.48 enami " has children %s\n", dev->dv_xname, d->dv_xname);
525 1.48 enami panic("config_detach");
526 1.48 enami }
527 1.33 thorpej }
528 1.33 thorpej #endif
529 1.33 thorpej
530 1.33 thorpej /*
531 1.33 thorpej * Mark cfdata to show that the unit can be reused, if possible.
532 1.33 thorpej */
533 1.46 cgd #ifdef __BROKEN_CONFIG_UNIT_USAGE
534 1.46 cgd /*
535 1.46 cgd * Note that we can only re-use a starred unit number if the unit
536 1.46 cgd * being detached had the last assigned unit number.
537 1.46 cgd */
538 1.46 cgd #endif /* __BROKEN_CONFIG_UNIT_USAGE */
539 1.33 thorpej for (cf = cfdata; cf->cf_driver; cf++) {
540 1.33 thorpej if (cf->cf_driver == cd) {
541 1.33 thorpej if (cf->cf_fstate == FSTATE_FOUND &&
542 1.33 thorpej cf->cf_unit == dev->dv_unit)
543 1.33 thorpej cf->cf_fstate = FSTATE_NOTFOUND;
544 1.46 cgd #ifdef __BROKEN_CONFIG_UNIT_USAGE
545 1.46 cgd if (cf->cf_fstate == FSTATE_STAR &&
546 1.46 cgd cf->cf_unit == dev->dv_unit + 1)
547 1.46 cgd cf->cf_unit--;
548 1.46 cgd #endif /* __BROKEN_CONFIG_UNIT_USAGE */
549 1.33 thorpej }
550 1.33 thorpej }
551 1.33 thorpej
552 1.33 thorpej /*
553 1.33 thorpej * Unlink from device list.
554 1.33 thorpej */
555 1.33 thorpej TAILQ_REMOVE(&alldevs, dev, dv_list);
556 1.33 thorpej
557 1.33 thorpej /*
558 1.33 thorpej * Remove from cfdriver's array, tell the world, and free softc.
559 1.33 thorpej */
560 1.33 thorpej cd->cd_devs[dev->dv_unit] = NULL;
561 1.33 thorpej if ((flags & DETACH_QUIET) == 0)
562 1.33 thorpej printf("%s detached\n", dev->dv_xname);
563 1.33 thorpej free(dev, M_DEVBUF);
564 1.33 thorpej
565 1.33 thorpej /*
566 1.33 thorpej * If the device now has no units in use, deallocate its softc array.
567 1.33 thorpej */
568 1.33 thorpej for (i = 0; i < cd->cd_ndevs; i++)
569 1.33 thorpej if (cd->cd_devs[i] != NULL)
570 1.33 thorpej break;
571 1.33 thorpej if (i == cd->cd_ndevs) { /* nothing found; deallocate */
572 1.33 thorpej free(cd->cd_devs, M_DEVBUF);
573 1.33 thorpej cd->cd_devs = NULL;
574 1.33 thorpej cd->cd_ndevs = 0;
575 1.33 thorpej }
576 1.33 thorpej
577 1.33 thorpej /*
578 1.33 thorpej * Return success.
579 1.33 thorpej */
580 1.33 thorpej return (0);
581 1.33 thorpej }
582 1.33 thorpej
583 1.33 thorpej int
584 1.51 cgd config_activate(struct device *dev)
585 1.33 thorpej {
586 1.33 thorpej struct cfattach *ca = dev->dv_cfdata->cf_attach;
587 1.34 thorpej int rv = 0, oflags = dev->dv_flags;
588 1.33 thorpej
589 1.33 thorpej if (ca->ca_activate == NULL)
590 1.33 thorpej return (EOPNOTSUPP);
591 1.33 thorpej
592 1.33 thorpej if ((dev->dv_flags & DVF_ACTIVE) == 0) {
593 1.33 thorpej dev->dv_flags |= DVF_ACTIVE;
594 1.33 thorpej rv = (*ca->ca_activate)(dev, DVACT_ACTIVATE);
595 1.34 thorpej if (rv)
596 1.34 thorpej dev->dv_flags = oflags;
597 1.33 thorpej }
598 1.33 thorpej return (rv);
599 1.33 thorpej }
600 1.33 thorpej
601 1.33 thorpej int
602 1.51 cgd config_deactivate(struct device *dev)
603 1.33 thorpej {
604 1.33 thorpej struct cfattach *ca = dev->dv_cfdata->cf_attach;
605 1.34 thorpej int rv = 0, oflags = dev->dv_flags;
606 1.33 thorpej
607 1.33 thorpej if (ca->ca_activate == NULL)
608 1.33 thorpej return (EOPNOTSUPP);
609 1.33 thorpej
610 1.33 thorpej if (dev->dv_flags & DVF_ACTIVE) {
611 1.33 thorpej dev->dv_flags &= ~DVF_ACTIVE;
612 1.33 thorpej rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE);
613 1.34 thorpej if (rv)
614 1.34 thorpej dev->dv_flags = oflags;
615 1.33 thorpej }
616 1.33 thorpej return (rv);
617 1.33 thorpej }
618 1.33 thorpej
619 1.33 thorpej /*
620 1.29 thorpej * Defer the configuration of the specified device until all
621 1.29 thorpej * of its parent's devices have been attached.
622 1.29 thorpej */
623 1.29 thorpej void
624 1.51 cgd config_defer(struct device *dev, void (*func)(struct device *))
625 1.29 thorpej {
626 1.29 thorpej struct deferred_config *dc;
627 1.29 thorpej
628 1.29 thorpej if (dev->dv_parent == NULL)
629 1.29 thorpej panic("config_defer: can't defer config of a root device");
630 1.29 thorpej
631 1.29 thorpej #ifdef DIAGNOSTIC
632 1.29 thorpej for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL;
633 1.29 thorpej dc = TAILQ_NEXT(dc, dc_queue)) {
634 1.29 thorpej if (dc->dc_dev == dev)
635 1.29 thorpej panic("config_defer: deferred twice");
636 1.29 thorpej }
637 1.29 thorpej #endif
638 1.29 thorpej
639 1.43 thorpej dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
640 1.43 thorpej if (dc == NULL)
641 1.43 thorpej panic("config_defer: unable to allocate callback");
642 1.29 thorpej
643 1.29 thorpej dc->dc_dev = dev;
644 1.29 thorpej dc->dc_func = func;
645 1.29 thorpej TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
646 1.47 thorpej config_pending_incr();
647 1.29 thorpej }
648 1.29 thorpej
649 1.29 thorpej /*
650 1.42 thorpej * Defer some autoconfiguration for a device until after interrupts
651 1.42 thorpej * are enabled.
652 1.42 thorpej */
653 1.42 thorpej void
654 1.51 cgd config_interrupts(struct device *dev, void (*func)(struct device *))
655 1.42 thorpej {
656 1.42 thorpej struct deferred_config *dc;
657 1.42 thorpej
658 1.42 thorpej /*
659 1.42 thorpej * If interrupts are enabled, callback now.
660 1.42 thorpej */
661 1.43 thorpej if (cold == 0) {
662 1.42 thorpej (*func)(dev);
663 1.42 thorpej return;
664 1.42 thorpej }
665 1.42 thorpej
666 1.42 thorpej #ifdef DIAGNOSTIC
667 1.42 thorpej for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL;
668 1.42 thorpej dc = TAILQ_NEXT(dc, dc_queue)) {
669 1.42 thorpej if (dc->dc_dev == dev)
670 1.42 thorpej panic("config_interrupts: deferred twice");
671 1.42 thorpej }
672 1.42 thorpej #endif
673 1.42 thorpej
674 1.43 thorpej dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
675 1.43 thorpej if (dc == NULL)
676 1.43 thorpej panic("config_interrupts: unable to allocate callback");
677 1.42 thorpej
678 1.42 thorpej dc->dc_dev = dev;
679 1.42 thorpej dc->dc_func = func;
680 1.42 thorpej TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue);
681 1.47 thorpej config_pending_incr();
682 1.42 thorpej }
683 1.42 thorpej
684 1.42 thorpej /*
685 1.42 thorpej * Process a deferred configuration queue.
686 1.29 thorpej */
687 1.29 thorpej static void
688 1.51 cgd config_process_deferred(struct deferred_config_head *queue,
689 1.51 cgd struct device *parent)
690 1.29 thorpej {
691 1.29 thorpej struct deferred_config *dc, *ndc;
692 1.29 thorpej
693 1.42 thorpej for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) {
694 1.29 thorpej ndc = TAILQ_NEXT(dc, dc_queue);
695 1.42 thorpej if (parent == NULL || dc->dc_dev->dv_parent == parent) {
696 1.42 thorpej TAILQ_REMOVE(queue, dc, dc_queue);
697 1.29 thorpej (*dc->dc_func)(dc->dc_dev);
698 1.29 thorpej free(dc, M_DEVBUF);
699 1.47 thorpej config_pending_decr();
700 1.29 thorpej }
701 1.29 thorpej }
702 1.47 thorpej }
703 1.47 thorpej
704 1.47 thorpej /*
705 1.47 thorpej * Manipulate the config_pending semaphore.
706 1.47 thorpej */
707 1.47 thorpej void
708 1.51 cgd config_pending_incr(void)
709 1.47 thorpej {
710 1.47 thorpej
711 1.47 thorpej config_pending++;
712 1.47 thorpej }
713 1.47 thorpej
714 1.47 thorpej void
715 1.51 cgd config_pending_decr(void)
716 1.47 thorpej {
717 1.47 thorpej
718 1.47 thorpej #ifdef DIAGNOSTIC
719 1.47 thorpej if (config_pending == 0)
720 1.47 thorpej panic("config_pending_decr: config_pending == 0");
721 1.47 thorpej #endif
722 1.47 thorpej config_pending--;
723 1.47 thorpej if (config_pending == 0)
724 1.47 thorpej wakeup((void *)&config_pending);
725 1.29 thorpej }
726 1.1 glass
727 1.1 glass /*
728 1.53 cgd * Attach a statically-initialized event. The type and string pointers
729 1.53 cgd * are already set up.
730 1.1 glass */
731 1.1 glass void
732 1.53 cgd evcnt_attach_static(struct evcnt *ev)
733 1.1 glass {
734 1.53 cgd int len;
735 1.1 glass
736 1.53 cgd len = strlen(ev->ev_group);
737 1.1 glass #ifdef DIAGNOSTIC
738 1.53 cgd if (len >= EVCNT_STRING_MAX) /* ..._MAX includes NUL */
739 1.53 cgd panic("evcnt_attach_static: group length (%s)", ev->ev_group);
740 1.1 glass #endif
741 1.53 cgd ev->ev_grouplen = len;
742 1.53 cgd
743 1.53 cgd len = strlen(ev->ev_name);
744 1.53 cgd #ifdef DIAGNOSTIC
745 1.53 cgd if (len >= EVCNT_STRING_MAX) /* ..._MAX includes NUL */
746 1.53 cgd panic("evcnt_attach_static: name length (%s)", ev->ev_name);
747 1.53 cgd #endif
748 1.53 cgd ev->ev_namelen = len;
749 1.53 cgd
750 1.20 cgd TAILQ_INSERT_TAIL(&allevents, ev, ev_list);
751 1.53 cgd }
752 1.53 cgd
753 1.53 cgd /*
754 1.53 cgd * Attach a dynamically-initialized event. Zero it, set up the type
755 1.53 cgd * and string pointers and then act like it was statically initialized.
756 1.53 cgd */
757 1.53 cgd void
758 1.53 cgd evcnt_attach_dynamic(struct evcnt *ev, int type, const struct evcnt *parent,
759 1.53 cgd const char *group, const char *name)
760 1.53 cgd {
761 1.53 cgd
762 1.53 cgd memset(ev, 0, sizeof *ev);
763 1.53 cgd ev->ev_type = type;
764 1.53 cgd ev->ev_parent = parent;
765 1.53 cgd ev->ev_group = group;
766 1.53 cgd ev->ev_name = name;
767 1.53 cgd evcnt_attach_static(ev);
768 1.33 thorpej }
769 1.33 thorpej
770 1.33 thorpej /*
771 1.33 thorpej * Detach an event.
772 1.33 thorpej */
773 1.33 thorpej void
774 1.51 cgd evcnt_detach(struct evcnt *ev)
775 1.33 thorpej {
776 1.33 thorpej
777 1.33 thorpej TAILQ_REMOVE(&allevents, ev, ev_list);
778 1.1 glass }
779