FreeBSD/Linux Kernel Cross Reference
sys/sys/device.h
1 /* $NetBSD: device.h,v 1.67 2003/11/17 10:07:58 keihan Exp $ */
2
3 /*
4 * Copyright (c) 1996, 2000 Christopher G. Demetriou
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the
18 * NetBSD Project. See http://www.NetBSD.org/ for
19 * information about NetBSD.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 * --(license Id: LICENSE.proto,v 1.1 2000/06/13 21:40:26 cgd Exp )--
35 */
36
37 /*
38 * Copyright (c) 1992, 1993
39 * The Regents of the University of California. All rights reserved.
40 *
41 * This software was developed by the Computer Systems Engineering group
42 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
43 * contributed to Berkeley.
44 *
45 * All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Lawrence Berkeley Laboratories.
49 *
50 * Redistribution and use in source and binary forms, with or without
51 * modification, are permitted provided that the following conditions
52 * are met:
53 * 1. Redistributions of source code must retain the above copyright
54 * notice, this list of conditions and the following disclaimer.
55 * 2. Redistributions in binary form must reproduce the above copyright
56 * notice, this list of conditions and the following disclaimer in the
57 * documentation and/or other materials provided with the distribution.
58 * 3. Neither the name of the University nor the names of its contributors
59 * may be used to endorse or promote products derived from this software
60 * without specific prior written permission.
61 *
62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * SUCH DAMAGE.
73 *
74 * @(#)device.h 8.2 (Berkeley) 2/17/94
75 */
76
77 #ifndef _SYS_DEVICE_H_
78 #define _SYS_DEVICE_H_
79
80 #include <sys/properties.h>
81 #include <sys/queue.h>
82
83 /*
84 * Minimal device structures.
85 * Note that all ``system'' device types are listed here.
86 */
87 enum devclass {
88 DV_DULL, /* generic, no special info */
89 DV_CPU, /* CPU (carries resource utilization) */
90 DV_DISK, /* disk drive (label, etc) */
91 DV_IFNET, /* network interface */
92 DV_TAPE, /* tape device */
93 DV_TTY /* serial line interface (?) */
94 };
95
96 /*
97 * Actions for ca_activate.
98 */
99 enum devact {
100 DVACT_ACTIVATE, /* activate the device */
101 DVACT_DEACTIVATE /* deactivate the device */
102 };
103
104 struct device {
105 enum devclass dv_class; /* this device's classification */
106 TAILQ_ENTRY(device) dv_list; /* entry on list of all devices */
107 struct cfdata *dv_cfdata; /* config data that found us
108 (NULL if pseudo-device) */
109 struct cfdriver *dv_cfdriver; /* our cfdriver */
110 struct cfattach *dv_cfattach; /* our cfattach */
111 int dv_unit; /* device unit number */
112 char dv_xname[16]; /* external name (name + unit) */
113 struct device *dv_parent; /* pointer to parent device
114 (NULL if pesudo- or root node) */
115 int dv_flags; /* misc. flags; see below */
116 };
117
118 /* dv_flags */
119 #define DVF_ACTIVE 0x0001 /* device is activated */
120
121 TAILQ_HEAD(devicelist, device);
122
123 /*
124 * `event' counters (use zero or more per device instance, as needed)
125 */
126
127 struct evcnt {
128 u_int64_t ev_count; /* how many have occurred */
129 TAILQ_ENTRY(evcnt) ev_list; /* entry on list of all counters */
130 unsigned char ev_type; /* counter type; see below */
131 unsigned char ev_grouplen; /* 'group' len, excluding NUL */
132 unsigned char ev_namelen; /* 'name' len, excluding NUL */
133 char ev_pad1; /* reserved (for now); 0 */
134 const struct evcnt *ev_parent; /* parent, for hierarchical ctrs */
135 const char *ev_group; /* name of group */
136 const char *ev_name; /* name of specific event */
137 };
138 TAILQ_HEAD(evcntlist, evcnt);
139
140 /* maximum group/name lengths, including trailing NUL */
141 #define EVCNT_STRING_MAX 256
142
143 /* ev_type values */
144 #define EVCNT_TYPE_MISC 0 /* miscellaneous; catch all */
145 #define EVCNT_TYPE_INTR 1 /* interrupt; count with vmstat -i */
146 #define EVCNT_TYPE_TRAP 2 /* processor trap/execption */
147
148 /*
149 * initializer for an event count structure. the lengths are initted and
150 * it is added to the evcnt list at attach time.
151 */
152 #define EVCNT_INITIALIZER(type, parent, group, name) \
153 { \
154 0, /* ev_count */ \
155 { 0 }, /* ev_list */ \
156 type, /* ev_type */ \
157 0, /* ev_grouplen */ \
158 0, /* ev_namelen */ \
159 0, /* ev_pad1 */ \
160 parent, /* ev_parent */ \
161 group, /* ev_group */ \
162 name, /* ev_name */ \
163 }
164
165 /*
166 * Attach a static event counter. This uses a link set to do the work.
167 * NOTE: "ev" should not be a pointer to the object, but rather a direct
168 * reference to the object itself.
169 */
170 #define EVCNT_ATTACH_STATIC(ev) __link_set_add_data(evcnts, ev)
171
172 /*
173 * Description of a configuration parent. Each device attachment attaches
174 * to an "interface attribute", which is given in this structure. The parent
175 * *must* carry this attribute. Optionally, an individual device instance
176 * may also specify a specific parent device instance.
177 */
178 struct cfparent {
179 const char *cfp_iattr; /* interface attribute */
180 const char *cfp_parent; /* optional specific parent */
181 int cfp_unit; /* optional specific unit
182 (-1 to wildcard) */
183 };
184
185 /*
186 * Configuration data (i.e., data placed in ioconf.c).
187 */
188 struct cfdata {
189 const char *cf_name; /* driver name */
190 const char *cf_atname; /* attachment name */
191 short cf_unit; /* unit number */
192 short cf_fstate; /* finding state (below) */
193 int *cf_loc; /* locators (machine dependent) */
194 int cf_flags; /* flags from config */
195 const struct cfparent *cf_pspec;/* parent specification */
196 const char * const *cf_locnames;/* locator names (machine dependent) */
197 };
198 #define FSTATE_NOTFOUND 0 /* has not been found */
199 #define FSTATE_FOUND 1 /* has been found */
200 #define FSTATE_STAR 2 /* duplicable */
201 #define FSTATE_DSTAR 3 /* has not been found, and disabled */
202 #define FSTATE_DNOTFOUND 4 /* duplicate, and disabled */
203
204 /*
205 * Multiple configuration data tables may be maintained. This structure
206 * provides the linkage.
207 */
208 struct cftable {
209 struct cfdata *ct_cfdata; /* pointer to cfdata table */
210 TAILQ_ENTRY(cftable) ct_list; /* list linkage */
211 };
212 TAILQ_HEAD(cftablelist, cftable);
213
214 typedef int (*cfmatch_t)(struct device *, struct cfdata *, void *);
215
216 /*
217 * `configuration' attachment and driver (what the machine-independent
218 * autoconf uses). As devices are found, they are applied against all
219 * the potential matches. The one with the best match is taken, and a
220 * device structure (plus any other data desired) is allocated. Pointers
221 * to these are placed into an array of pointers. The array itself must
222 * be dynamic since devices can be found long after the machine is up
223 * and running.
224 *
225 * Devices can have multiple configuration attachments if they attach
226 * to different attributes (busses, or whatever), to allow specification
227 * of multiple match and attach functions. There is only one configuration
228 * driver per driver, so that things like unit numbers and the device
229 * structure array will be shared.
230 */
231 struct cfattach {
232 const char *ca_name; /* name of attachment */
233 LIST_ENTRY(cfattach) ca_list; /* link on cfdriver's list */
234 size_t ca_devsize; /* size of dev data (for malloc) */
235 cfmatch_t ca_match; /* returns a match level */
236 void (*ca_attach)(struct device *, struct device *, void *);
237 int (*ca_detach)(struct device *, int);
238 int (*ca_activate)(struct device *, enum devact);
239 };
240 LIST_HEAD(cfattachlist, cfattach);
241
242 #define CFATTACH_DECL(name, ddsize, matfn, attfn, detfn, actfn) \
243 struct cfattach __CONCAT(name,_ca) = { \
244 ___STRING(name), { 0 }, ddsize, matfn, attfn, detfn, actfn \
245 }
246
247 /* Flags given to config_detach(), and the ca_detach function. */
248 #define DETACH_FORCE 0x01 /* force detachment; hardware gone */
249 #define DETACH_QUIET 0x02 /* don't print a notice */
250
251 struct cfdriver {
252 LIST_ENTRY(cfdriver) cd_list; /* link on allcfdrivers */
253 struct cfattachlist cd_attach; /* list of all attachments */
254 void **cd_devs; /* devices found */
255 const char *cd_name; /* device name */
256 enum devclass cd_class; /* device classification */
257 int cd_ndevs; /* size of cd_devs array */
258 const char * const *cd_attrs; /* attributes for this device */
259 };
260 LIST_HEAD(cfdriverlist, cfdriver);
261
262 #define CFDRIVER_DECL(name, class, attrs) \
263 struct cfdriver __CONCAT(name,_cd) = { \
264 { 0 }, { 0 }, NULL, ___STRING(name), class, 0, attrs \
265 }
266
267 /*
268 * The cfattachinit is a data structure used to associate a list of
269 * cfattach's with cfdrivers as found in the static kernel configuration.
270 */
271 struct cfattachinit {
272 const char *cfai_name; /* driver name */
273 struct cfattach * const *cfai_list;/* list of attachments */
274 };
275
276 /*
277 * Configuration printing functions, and their return codes. The second
278 * argument is NULL if the device was configured; otherwise it is the name
279 * of the parent device. The return value is ignored if the device was
280 * configured, so most functions can return UNCONF unconditionally.
281 */
282 typedef int (*cfprint_t)(void *, const char *); /* XXX const char * */
283 #define QUIET 0 /* print nothing */
284 #define UNCONF 1 /* print " not configured\n" */
285 #define UNSUPP 2 /* print " not supported\n" */
286
287 /*
288 * Pseudo-device attach information (function + number of pseudo-devs).
289 */
290 struct pdevinit {
291 void (*pdev_attach)(int);
292 int pdev_count;
293 };
294
295 /* This allows us to wildcard a device unit. */
296 #define DVUNIT_ANY -1
297
298 #ifdef _KERNEL
299
300 extern struct cfdriverlist allcfdrivers;/* list of all cfdrivers */
301 extern struct devicelist alldevs; /* list of all devices */
302 extern struct evcntlist allevents; /* list of all event counters */
303 extern struct cftablelist allcftables; /* list of all cfdata tables */
304 extern struct device *booted_device; /* the device we booted from */
305
306 extern __volatile int config_pending; /* semaphore for mountroot */
307
308 extern propdb_t dev_propdb; /* device properties database */
309
310 void config_init(void);
311 void configure(void);
312
313 int config_cfdriver_attach(struct cfdriver *);
314 int config_cfdriver_detach(struct cfdriver *);
315
316 int config_cfattach_attach(const char *, struct cfattach *);
317 int config_cfattach_detach(const char *, struct cfattach *);
318
319 struct cfdriver *config_cfdriver_lookup(const char *);
320 struct cfattach *config_cfattach_lookup(const char *, const char *);
321
322 struct cfdata *config_search(cfmatch_t, struct device *, void *);
323 struct cfdata *config_rootsearch(cfmatch_t, const char *, void *);
324 struct device *config_found_sm(struct device *, void *, cfprint_t, cfmatch_t);
325 struct device *config_rootfound(const char *, void *);
326 struct device *config_attach(struct device *, struct cfdata *, void *,
327 cfprint_t);
328 int config_match(struct device *, struct cfdata *, void *);
329
330 struct device *config_attach_pseudo(const char *, int);
331
332 void config_makeroom(int n, struct cfdriver *cd);
333 int config_detach(struct device *, int);
334 int config_activate(struct device *);
335 int config_deactivate(struct device *);
336 void config_defer(struct device *, void (*)(struct device *));
337 void config_interrupts(struct device *, void (*)(struct device *));
338 void config_pending_incr(void);
339 void config_pending_decr(void);
340
341 int config_finalize_register(struct device *, int (*)(struct device *));
342 void config_finalize(void);
343
344 #ifdef __HAVE_DEVICE_REGISTER
345 void device_register(struct device *, void *);
346 #endif
347
348 void evcnt_init(void);
349 void evcnt_attach_static(struct evcnt *);
350 void evcnt_attach_dynamic(struct evcnt *, int, const struct evcnt *,
351 const char *, const char *);
352 void evcnt_detach(struct evcnt *);
353
354 /* compatibility definitions */
355 #define config_found(d, a, p) config_found_sm((d), (a), (p), NULL)
356
357 /* convenience definitions */
358 #define device_lookup(cfd, unit) \
359 (((unit) < (cfd)->cd_ndevs) ? (cfd)->cd_devs[(unit)] : NULL)
360
361 #ifdef DDB
362 void event_print(int, void (*)(const char *, ...));
363 #endif
364 #endif /* _KERNEL */
365
366 #endif /* !_SYS_DEVICE_H_ */
Cache object: 38ed8a4fb6a657662f323102a6da85e0
|