FreeBSD/Linux Kernel Cross Reference
sys/sys/bus.h
1 /*-
2 * Copyright (c) 1997,1998,2003 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: releng/6.0/sys/sys/bus.h 144952 2005-04-12 15:20:36Z imp $
27 */
28
29 #ifndef _SYS_BUS_H_
30 #define _SYS_BUS_H_
31
32 /**
33 * @defgroup NEWBUS newbus - a generic framework for managing devices
34 * @{
35 */
36
37 /**
38 * @brief Interface information structure.
39 */
40 struct u_businfo {
41 int ub_version; /**< @brief interface version */
42 #define BUS_USER_VERSION 1
43 int ub_generation; /**< @brief generation count */
44 };
45
46 /**
47 * @brief State of the device.
48 */
49 typedef enum device_state {
50 DS_NOTPRESENT, /**< @brief not probed or probe failed */
51 DS_ALIVE, /**< @brief probe succeeded */
52 DS_ATTACHED, /**< @brief attach method called */
53 DS_BUSY /**< @brief device is open */
54 } device_state_t;
55
56 /**
57 * @brief Device information exported to userspace.
58 */
59 struct u_device {
60 uintptr_t dv_handle;
61 uintptr_t dv_parent;
62
63 char dv_name[32]; /**< @brief Name of device in tree. */
64 char dv_desc[32]; /**< @brief Driver description */
65 char dv_drivername[32]; /**< @brief Driver name */
66 char dv_pnpinfo[128]; /**< @brief Plug and play info */
67 char dv_location[128]; /**< @brief Where is the device? */
68 uint32_t dv_devflags; /**< @brief API Flags for device */
69 uint16_t dv_flags; /**< @brief flags for dev date */
70 device_state_t dv_state; /**< @brief State of attachment */
71 /* XXX more driver info? */
72 };
73
74 #ifdef _KERNEL
75
76 #include <sys/queue.h>
77 #include <sys/kobj.h>
78
79 /**
80 * devctl hooks. Typically one should use the devctl_notify
81 * hook to send the message. However, devctl_queue_data is also
82 * included in case devctl_notify isn't sufficiently general.
83 */
84 void devctl_notify(const char *__system, const char *__subsystem,
85 const char *__type, const char *__data);
86 void devctl_queue_data(char *__data);
87
88 /*
89 * Forward declarations
90 */
91 typedef struct device *device_t;
92
93 /**
94 * @brief A device driver (included mainly for compatibility with
95 * FreeBSD 4.x).
96 */
97 typedef struct kobj_class driver_t;
98
99 /**
100 * @brief A device class
101 *
102 * The devclass object has two main functions in the system. The first
103 * is to manage the allocation of unit numbers for device instances
104 * and the second is to hold the list of device drivers for a
105 * particular bus type. Each devclass has a name and there cannot be
106 * two devclasses with the same name. This ensures that unique unit
107 * numbers are allocated to device instances.
108 *
109 * Drivers that support several different bus attachments (e.g. isa,
110 * pci, pccard) should all use the same devclass to ensure that unit
111 * numbers do not conflict.
112 *
113 * Each devclass may also have a parent devclass. This is used when
114 * searching for device drivers to allow a form of inheritance. When
115 * matching drivers with devices, first the driver list of the parent
116 * device's devclass is searched. If no driver is found in that list,
117 * the search continues in the parent devclass (if any).
118 */
119 typedef struct devclass *devclass_t;
120
121 /**
122 * @brief A device method (included mainly for compatibility with
123 * FreeBSD 4.x).
124 */
125 #define device_method_t kobj_method_t
126
127 /**
128 * @brief A driver interrupt service routine
129 */
130 typedef void driver_intr_t(void*);
131
132 /**
133 * @brief Interrupt type bits.
134 *
135 * These flags are used both by newbus interrupt
136 * registration (nexus.c) and also in struct intrec, which defines
137 * interrupt properties.
138 *
139 * XXX We should probably revisit this and remove the vestiges of the
140 * spls implicit in names like INTR_TYPE_TTY. In the meantime, don't
141 * confuse things by renaming them (Grog, 18 July 2000).
142 *
143 * We define this in terms of bits because some devices may belong
144 * to multiple classes (and therefore need to be included in
145 * multiple interrupt masks, which is what this really serves to
146 * indicate. Buses which do interrupt remapping will want to
147 * change their type to reflect what sort of devices are underneath.
148 */
149 enum intr_type {
150 INTR_TYPE_TTY = 1,
151 INTR_TYPE_BIO = 2,
152 INTR_TYPE_NET = 4,
153 INTR_TYPE_CAM = 8,
154 INTR_TYPE_MISC = 16,
155 INTR_TYPE_CLK = 32,
156 INTR_TYPE_AV = 64,
157 INTR_FAST = 128,
158 INTR_EXCL = 256, /* exclusive interrupt */
159 INTR_MPSAFE = 512, /* this interrupt is SMP safe */
160 INTR_ENTROPY = 1024 /* this interrupt provides entropy */
161 };
162
163 enum intr_trigger {
164 INTR_TRIGGER_CONFORM = 0,
165 INTR_TRIGGER_EDGE = 1,
166 INTR_TRIGGER_LEVEL = 2
167 };
168
169 enum intr_polarity {
170 INTR_POLARITY_CONFORM = 0,
171 INTR_POLARITY_HIGH = 1,
172 INTR_POLARITY_LOW = 2
173 };
174
175 typedef int (*devop_t)(void);
176
177 /**
178 * @brief This structure is deprecated.
179 *
180 * Use the kobj(9) macro DEFINE_CLASS to
181 * declare classes which implement device drivers.
182 */
183 struct driver {
184 KOBJ_CLASS_FIELDS;
185 };
186
187 /*
188 * Definitions for drivers which need to keep simple lists of resources
189 * for their child devices.
190 */
191 struct resource;
192
193 /**
194 * @brief An entry for a single resource in a resource list.
195 */
196 struct resource_list_entry {
197 STAILQ_ENTRY(resource_list_entry) link;
198 int type; /**< @brief type argument to alloc_resource */
199 int rid; /**< @brief resource identifier */
200 struct resource *res; /**< @brief the real resource when allocated */
201 u_long start; /**< @brief start of resource range */
202 u_long end; /**< @brief end of resource range */
203 u_long count; /**< @brief count within range */
204 };
205 STAILQ_HEAD(resource_list, resource_list_entry);
206
207 void resource_list_init(struct resource_list *rl);
208 void resource_list_free(struct resource_list *rl);
209 struct resource_list_entry *
210 resource_list_add(struct resource_list *rl,
211 int type, int rid,
212 u_long start, u_long end, u_long count);
213 int resource_list_add_next(struct resource_list *rl,
214 int type,
215 u_long start, u_long end, u_long count);
216 struct resource_list_entry*
217 resource_list_find(struct resource_list *rl,
218 int type, int rid);
219 void resource_list_delete(struct resource_list *rl,
220 int type, int rid);
221 struct resource *
222 resource_list_alloc(struct resource_list *rl,
223 device_t bus, device_t child,
224 int type, int *rid,
225 u_long start, u_long end,
226 u_long count, u_int flags);
227 int resource_list_release(struct resource_list *rl,
228 device_t bus, device_t child,
229 int type, int rid, struct resource *res);
230 void resource_list_purge(struct resource_list *rl);
231 int resource_list_print_type(struct resource_list *rl,
232 const char *name, int type,
233 const char *format);
234
235 /*
236 * The root bus, to which all top-level busses are attached.
237 */
238 extern device_t root_bus;
239 extern devclass_t root_devclass;
240 void root_bus_configure(void);
241
242 /*
243 * Useful functions for implementing busses.
244 */
245
246 int bus_generic_activate_resource(device_t dev, device_t child, int type,
247 int rid, struct resource *r);
248 struct resource *
249 bus_generic_alloc_resource(device_t bus, device_t child, int type,
250 int *rid, u_long start, u_long end,
251 u_long count, u_int flags);
252 int bus_generic_attach(device_t dev);
253 int bus_generic_child_present(device_t dev, device_t child);
254 int bus_generic_config_intr(device_t, int, enum intr_trigger,
255 enum intr_polarity);
256 int bus_generic_deactivate_resource(device_t dev, device_t child, int type,
257 int rid, struct resource *r);
258 int bus_generic_detach(device_t dev);
259 void bus_generic_driver_added(device_t dev, driver_t *driver);
260 struct resource_list *
261 bus_generic_get_resource_list (device_t, device_t);
262 int bus_print_child_header(device_t dev, device_t child);
263 int bus_print_child_footer(device_t dev, device_t child);
264 int bus_generic_print_child(device_t dev, device_t child);
265 int bus_generic_probe(device_t dev);
266 int bus_generic_read_ivar(device_t dev, device_t child, int which,
267 uintptr_t *result);
268 int bus_generic_release_resource(device_t bus, device_t child,
269 int type, int rid, struct resource *r);
270 int bus_generic_resume(device_t dev);
271 int bus_generic_setup_intr(device_t dev, device_t child,
272 struct resource *irq, int flags,
273 driver_intr_t *intr, void *arg, void **cookiep);
274
275 struct resource *
276 bus_generic_rl_alloc_resource (device_t, device_t, int, int *,
277 u_long, u_long, u_long, u_int);
278 void bus_generic_rl_delete_resource (device_t, device_t, int, int);
279 int bus_generic_rl_get_resource (device_t, device_t, int, int, u_long *,
280 u_long *);
281 int bus_generic_rl_set_resource (device_t, device_t, int, int, u_long,
282 u_long);
283 int bus_generic_rl_release_resource (device_t, device_t, int, int,
284 struct resource *);
285
286 int bus_generic_shutdown(device_t dev);
287 int bus_generic_suspend(device_t dev);
288 int bus_generic_teardown_intr(device_t dev, device_t child,
289 struct resource *irq, void *cookie);
290 int bus_generic_write_ivar(device_t dev, device_t child, int which,
291 uintptr_t value);
292
293 /*
294 * Wrapper functions for the BUS_*_RESOURCE methods to make client code
295 * a little simpler.
296 */
297 struct resource *bus_alloc_resource(device_t dev, int type, int *rid,
298 u_long start, u_long end, u_long count,
299 u_int flags);
300 int bus_activate_resource(device_t dev, int type, int rid,
301 struct resource *r);
302 int bus_deactivate_resource(device_t dev, int type, int rid,
303 struct resource *r);
304 int bus_release_resource(device_t dev, int type, int rid,
305 struct resource *r);
306 int bus_free_resource(device_t dev, int type, struct resource *r);
307 int bus_setup_intr(device_t dev, struct resource *r, int flags,
308 driver_intr_t handler, void *arg, void **cookiep);
309 int bus_teardown_intr(device_t dev, struct resource *r, void *cookie);
310 int bus_set_resource(device_t dev, int type, int rid,
311 u_long start, u_long count);
312 int bus_get_resource(device_t dev, int type, int rid,
313 u_long *startp, u_long *countp);
314 u_long bus_get_resource_start(device_t dev, int type, int rid);
315 u_long bus_get_resource_count(device_t dev, int type, int rid);
316 void bus_delete_resource(device_t dev, int type, int rid);
317 int bus_child_present(device_t child);
318 int bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen);
319 int bus_child_location_str(device_t child, char *buf, size_t buflen);
320
321 static __inline struct resource *
322 bus_alloc_resource_any(device_t dev, int type, int *rid, u_int flags)
323 {
324 return (bus_alloc_resource(dev, type, rid, 0ul, ~0ul, 1, flags));
325 }
326
327 /*
328 * Access functions for device.
329 */
330 device_t device_add_child(device_t dev, const char *name, int unit);
331 device_t device_add_child_ordered(device_t dev, int order,
332 const char *name, int unit);
333 void device_busy(device_t dev);
334 int device_delete_child(device_t dev, device_t child);
335 int device_attach(device_t dev);
336 int device_detach(device_t dev);
337 void device_disable(device_t dev);
338 void device_enable(device_t dev);
339 device_t device_find_child(device_t dev, const char *classname,
340 int unit);
341 const char *device_get_desc(device_t dev);
342 devclass_t device_get_devclass(device_t dev);
343 driver_t *device_get_driver(device_t dev);
344 u_int32_t device_get_flags(device_t dev);
345 device_t device_get_parent(device_t dev);
346 int device_get_children(device_t dev, device_t **listp, int *countp);
347 void *device_get_ivars(device_t dev);
348 void device_set_ivars(device_t dev, void *ivars);
349 const char *device_get_name(device_t dev);
350 const char *device_get_nameunit(device_t dev);
351 void *device_get_softc(device_t dev);
352 device_state_t device_get_state(device_t dev);
353 int device_get_unit(device_t dev);
354 struct sysctl_ctx_list *device_get_sysctl_ctx(device_t dev);
355 struct sysctl_oid *device_get_sysctl_tree(device_t dev);
356 int device_is_alive(device_t dev); /* did probe succeed? */
357 int device_is_attached(device_t dev); /* did attach succeed? */
358 int device_is_enabled(device_t dev);
359 int device_is_quiet(device_t dev);
360 int device_print_prettyname(device_t dev);
361 int device_printf(device_t dev, const char *, ...) __printflike(2, 3);
362 int device_probe_and_attach(device_t dev);
363 int device_quiesce(device_t dev);
364 void device_quiet(device_t dev);
365 void device_set_desc(device_t dev, const char* desc);
366 void device_set_desc_copy(device_t dev, const char* desc);
367 int device_set_devclass(device_t dev, const char *classname);
368 int device_set_driver(device_t dev, driver_t *driver);
369 void device_set_flags(device_t dev, u_int32_t flags);
370 void device_set_softc(device_t dev, void *softc);
371 int device_set_unit(device_t dev, int unit); /* XXX DONT USE XXX */
372 int device_shutdown(device_t dev);
373 void device_unbusy(device_t dev);
374 void device_verbose(device_t dev);
375
376 /*
377 * Access functions for devclass.
378 */
379 int devclass_add_driver(devclass_t dc, kobj_class_t driver);
380 int devclass_delete_driver(devclass_t dc, kobj_class_t driver);
381 devclass_t devclass_create(const char *classname);
382 devclass_t devclass_find(const char *classname);
383 kobj_class_t devclass_find_driver(devclass_t dc, const char *classname);
384 const char *devclass_get_name(devclass_t dc);
385 device_t devclass_get_device(devclass_t dc, int unit);
386 void *devclass_get_softc(devclass_t dc, int unit);
387 int devclass_get_devices(devclass_t dc, device_t **listp, int *countp);
388 int devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp);
389 int devclass_get_count(devclass_t dc);
390 int devclass_get_maxunit(devclass_t dc);
391 int devclass_find_free_unit(devclass_t dc, int unit);
392 void devclass_set_parent(devclass_t dc, devclass_t pdc);
393 devclass_t devclass_get_parent(devclass_t dc);
394 struct sysctl_ctx_list *devclass_get_sysctl_ctx(devclass_t dc);
395 struct sysctl_oid *devclass_get_sysctl_tree(devclass_t dc);
396 int devclass_quiesce_driver(devclass_t dc, kobj_class_t driver);
397
398 /*
399 * Access functions for device resources.
400 */
401
402 int resource_int_value(const char *name, int unit, const char *resname,
403 int *result);
404 int resource_long_value(const char *name, int unit, const char *resname,
405 long *result);
406 int resource_string_value(const char *name, int unit, const char *resname,
407 const char **result);
408 int resource_disabled(const char *name, int unit);
409 int resource_find_match(int *anchor, const char **name, int *unit,
410 const char *resname, const char *value);
411 int resource_find_dev(int *anchor, const char *name, int *unit,
412 const char *resname, const char *value);
413 int resource_set_int(const char *name, int unit, const char *resname,
414 int value);
415 int resource_set_long(const char *name, int unit, const char *resname,
416 long value);
417 int resource_set_string(const char *name, int unit, const char *resname,
418 const char *value);
419 /*
420 * Functions for maintaining and checking consistency of
421 * bus information exported to userspace.
422 */
423 int bus_data_generation_check(int generation);
424 void bus_data_generation_update(void);
425
426 /**
427 * Some convenience defines for probe routines to return. These are just
428 * suggested values, and there's nothing magical about them.
429 * BUS_PROBE_SPECIFIC is for devices that cannot be reprobed, and that no
430 * possible other driver may exist (typically legacy drivers who don't fallow
431 * all the rules, or special needs drivers). BUS_PROBE_VENDOR is the
432 * suggested value that vendor supplied drivers use. This is for source or
433 * binary drivers that are not yet integrated into the FreeBSD tree. Its use
434 * in the base OS is prohibited. BUS_PROBE_DEFAULT is the normal return value
435 * for drivers to use. It is intended that nearly all of the drivers in the
436 * tree should return this value. BUS_PROBE_LOW_PRIORITY are for drivers that
437 * have special requirements like when there are two drivers that support
438 * overlapping series of hardware devices. In this case the one that supports
439 * the older part of the line would return this value, while the one that
440 * supports the newer ones would return BUS_PROBE_DEFAULT. BUS_PROBE_GENERIC
441 * is for drivers that wish to have a generic form and a specialized form,
442 * like is done with the pci bus and the acpi pci bus. BUS_PROBE_HOOVER is
443 * for those busses that implement a generic device place-holder for devices on
444 * the bus that have no more specific driver for them (aka ugen).
445 */
446 #define BUS_PROBE_SPECIFIC 0 /* Only I can use this device */
447 #define BUS_PROBE_VENDOR (-10) /* Vendor supplied driver */
448 #define BUS_PROBE_DEFAULT (-20) /* Base OS default driver */
449 #define BUS_PROBE_LOW_PRIORITY (-40) /* Older, less desirable drivers */
450 #define BUS_PROBE_GENERIC (-100) /* generic driver for dev */
451 #define BUS_PROBE_HOOVER (-500) /* Generic dev for all devs on bus */
452
453 /**
454 * Shorthand for constructing method tables.
455 */
456 #define DEVMETHOD KOBJMETHOD
457
458 /*
459 * Some common device interfaces.
460 */
461 #include "device_if.h"
462 #include "bus_if.h"
463
464 struct module;
465
466 int driver_module_handler(struct module *, int, void *);
467
468 /**
469 * Module support for automatically adding drivers to busses.
470 */
471 struct driver_module_data {
472 int (*dmd_chainevh)(struct module *, int, void *);
473 void *dmd_chainarg;
474 const char *dmd_busname;
475 kobj_class_t dmd_driver;
476 devclass_t *dmd_devclass;
477 };
478
479 #define DRIVER_MODULE(name, busname, driver, devclass, evh, arg) \
480 \
481 static struct driver_module_data name##_##busname##_driver_mod = { \
482 evh, arg, \
483 #busname, \
484 (kobj_class_t) &driver, \
485 &devclass \
486 }; \
487 \
488 static moduledata_t name##_##busname##_mod = { \
489 #busname "/" #name, \
490 driver_module_handler, \
491 &name##_##busname##_driver_mod \
492 }; \
493 DECLARE_MODULE(name##_##busname, name##_##busname##_mod, \
494 SI_SUB_DRIVERS, SI_ORDER_MIDDLE)
495
496 /**
497 * Generic ivar accessor generation macros for bus drivers
498 */
499 #define __BUS_ACCESSOR(varp, var, ivarp, ivar, type) \
500 \
501 static __inline type varp ## _get_ ## var(device_t dev) \
502 { \
503 uintptr_t v; \
504 BUS_READ_IVAR(device_get_parent(dev), dev, \
505 ivarp ## _IVAR_ ## ivar, &v); \
506 return ((type) v); \
507 } \
508 \
509 static __inline void varp ## _set_ ## var(device_t dev, type t) \
510 { \
511 uintptr_t v = (uintptr_t) t; \
512 BUS_WRITE_IVAR(device_get_parent(dev), dev, \
513 ivarp ## _IVAR_ ## ivar, v); \
514 }
515
516 #endif /* _KERNEL */
517
518 #endif /* !_SYS_BUS_H_ */
Cache object: 61b71cbe3c2323ae29878e2fe3852b18
|