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