Add device_is_attached to allow a driver to check wether a given device
[dragonfly.git] / sys / sys / bus.h
1 /*-
2  * Copyright (c) 1997,1998 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: src/sys/sys/bus.h,v 1.30.2.4 2002/10/10 15:13:33 jhb Exp $
27  * $DragonFly: src/sys/sys/bus.h,v 1.6 2004/02/16 18:48:03 joerg Exp $
28  */
29
30 #ifndef _SYS_BUS_H_
31 #define _SYS_BUS_H_
32
33 #ifdef _KERNEL
34
35 #include <sys/queue.h>
36 #include <sys/kobj.h>
37
38 /*
39  * Forward declarations
40  */
41 typedef struct device           *device_t;
42 typedef struct driver           driver_t;
43 typedef struct devclass         *devclass_t;
44 #define device_method_t         kobj_method_t
45
46 typedef void driver_intr_t(void*);
47
48 /*
49  * We define this in terms of bits because some devices may belong
50  * to multiple classes (and therefore need to be included in
51  * multiple interrupt masks, which is what this really serves to
52  * indicate.  Buses which do interrupt remapping will want to
53  * change their type to reflect what sort of devices are underneath.
54  */
55 enum intr_type {
56     INTR_TYPE_TTY = 1,
57     INTR_TYPE_BIO = 2,
58     INTR_TYPE_NET = 4,
59     INTR_TYPE_CAM = 8,
60     INTR_TYPE_MISC = 16,
61     INTR_TYPE_FAST = 128
62 };
63 #define INTR_TYPE_AV INTR_TYPE_TTY      /* for source compatibility with 5.x */
64
65 typedef int (*devop_t)(void);
66
67 struct driver {
68     KOBJ_CLASS_FIELDS;
69     void                *priv;          /* driver private data */
70 };
71
72 typedef enum device_state {
73     DS_NOTPRESENT,                      /* not probed or probe failed */
74     DS_ALIVE,                           /* probe succeeded */
75     DS_ATTACHED,                        /* attach method called */
76     DS_BUSY                             /* device is open */
77 } device_state_t;
78
79 /*
80  * Definitions for drivers which need to keep simple lists of resources
81  * for their child devices.
82  */
83 struct  resource;
84
85 struct resource_list_entry {
86     SLIST_ENTRY(resource_list_entry) link;
87     int                 type;           /* type argument to alloc_resource */
88     int                 rid;            /* resource identifier */
89     struct resource     *res;           /* the real resource when allocated */
90     u_long              start;          /* start of resource range */
91     u_long              end;            /* end of resource range */
92     u_long              count;          /* count within range */
93 };
94 SLIST_HEAD(resource_list, resource_list_entry);
95
96 /*
97  * Initialise a resource list.
98  */
99 void    resource_list_init(struct resource_list *rl);
100
101 /*
102  * Reclaim memory used by a resource list.
103  */
104 void    resource_list_free(struct resource_list *rl);
105
106 /*
107  * Add a resource entry or modify an existing entry if one exists with 
108  * the same type and rid.
109  */
110 void    resource_list_add(struct resource_list *rl,
111                           int type, int rid,
112                           u_long start, u_long end, u_long count);
113
114 /*
115  * Find a resource entry by type and rid.
116  */
117 struct resource_list_entry*
118         resource_list_find(struct resource_list *rl,
119                            int type, int rid);
120
121 /*
122  * Delete a resource entry.
123  */
124 void    resource_list_delete(struct resource_list *rl,
125                              int type, int rid);
126
127 /*
128  * Implement BUS_ALLOC_RESOURCE by looking up a resource from the list 
129  * and passing the allocation up to the parent of bus. This assumes
130  * that the first entry of device_get_ivars(child) is a struct
131  * resource_list. This also handles 'passthrough' allocations where a
132  * child is a remote descendant of bus by passing the allocation up to 
133  * the parent of bus.
134  */
135 struct resource *
136         resource_list_alloc(struct resource_list *rl,
137                             device_t bus, device_t child,
138                             int type, int *rid,
139                             u_long start, u_long end,
140                             u_long count, u_int flags);
141
142 /*
143  * Implement BUS_RELEASE_RESOURCE.
144  */
145 int     resource_list_release(struct resource_list *rl,
146                               device_t bus, device_t child,
147                               int type, int rid, struct resource *res);
148
149 /*
150  * Print all resources of a specified type, for use in bus_print_child.
151  * The name is printed if at least one resource of the given type is available.
152  * The format ist used to print resource start and end.
153  */
154 int     resource_list_print_type(struct resource_list *rl,
155                                  const char *name, int type,
156                                  const char *format);
157
158 /*
159  * The root bus, to which all top-level busses are attached.
160  */
161 extern device_t root_bus;
162 extern devclass_t root_devclass;
163 void    root_bus_configure(void);
164
165 /*
166  * Useful functions for implementing busses.
167  */
168
169 int     bus_generic_activate_resource(device_t dev, device_t child, int type,
170                                       int rid, struct resource *r);
171 struct resource *bus_generic_alloc_resource(device_t bus, device_t child,
172                                             int type, int *rid,
173                                             u_long start, u_long end,
174                                             u_long count, u_int flags);
175 int     bus_generic_attach(device_t dev);
176 int     bus_generic_child_present(device_t dev, device_t child);
177 int     bus_generic_deactivate_resource(device_t dev, device_t child, int type,
178                                         int rid, struct resource *r);
179 int     bus_generic_detach(device_t dev);
180 void    bus_generic_driver_added(device_t dev, driver_t *driver);
181 int     bus_print_child_header(device_t dev, device_t child);
182 int     bus_print_child_footer(device_t dev, device_t child);
183 int     bus_generic_print_child(device_t dev, device_t child);
184 int     bus_generic_probe(device_t dev);
185 int     bus_generic_read_ivar(device_t dev, device_t child, int which,
186                               uintptr_t *result);
187 int     bus_generic_release_resource(device_t bus, device_t child,
188                                      int type, int rid, struct resource *r);
189 int     bus_generic_resume(device_t dev);
190 int     bus_generic_setup_intr(device_t dev, device_t child,
191                                struct resource *irq, int flags,
192                                driver_intr_t *intr, void *arg, void **cookiep);
193 int     bus_generic_shutdown(device_t dev);
194 int     bus_generic_suspend(device_t dev);
195 int     bus_generic_teardown_intr(device_t dev, device_t child,
196                                   struct resource *irq, void *cookie);
197 int     bus_generic_write_ivar(device_t dev, device_t child, int which,
198                                uintptr_t value);
199
200 /*
201  * Wrapper functions for the BUS_*_RESOURCE methods to make client code
202  * a little simpler.
203  */
204 struct  resource *bus_alloc_resource(device_t dev, int type, int *rid,
205                                      u_long start, u_long end, u_long count,
206                                      u_int flags);
207 int     bus_activate_resource(device_t dev, int type, int rid, 
208                               struct resource *r);
209 int     bus_deactivate_resource(device_t dev, int type, int rid,
210                                 struct resource *r);
211 int     bus_release_resource(device_t dev, int type, int rid, 
212                              struct resource *r);
213 int     bus_setup_intr(device_t dev, struct resource *r, int flags,
214                        driver_intr_t handler, void *arg, void **cookiep);
215 int     bus_teardown_intr(device_t dev, struct resource *r, void *cookie);
216 int     bus_set_resource(device_t dev, int type, int rid,
217                          u_long start, u_long count);
218 int     bus_get_resource(device_t dev, int type, int rid,
219                          u_long *startp, u_long *countp);
220 u_long  bus_get_resource_start(device_t dev, int type, int rid);
221 u_long  bus_get_resource_count(device_t dev, int type, int rid);
222 void    bus_delete_resource(device_t dev, int type, int rid);
223
224 /*
225  * Access functions for device.
226  */
227 device_t        device_add_child(device_t dev, const char *name, int unit);
228 device_t        device_add_child_ordered(device_t dev, int order,
229                                          const char *name, int unit);
230 void    device_busy(device_t dev);
231 int     device_delete_child(device_t dev, device_t child);
232 int     device_detach(device_t dev);
233 void    device_disable(device_t dev);
234 void    device_enable(device_t dev);
235 device_t        device_find_child(device_t dev, const char *classname,
236                                   int unit);
237 const char      *device_get_desc(device_t dev);
238 devclass_t      device_get_devclass(device_t dev);
239 driver_t        *device_get_driver(device_t dev);
240 u_int32_t       device_get_flags(device_t dev);
241 device_t        device_get_parent(device_t dev);
242 int     device_get_children(device_t dev, device_t **listp, int *countp);
243 void    *device_get_ivars(device_t dev);
244 void    device_set_ivars(device_t dev, void *ivars);
245 const   char *device_get_name(device_t dev);
246 const   char *device_get_nameunit(device_t dev);
247 void    *device_get_softc(device_t dev);
248 device_state_t  device_get_state(device_t dev);
249 int     device_get_unit(device_t dev);
250 int     device_is_alive(device_t dev); /* did probe succeed? */
251 int     device_is_attached(device_t dev);       /* did attach succeed? */
252 int     device_is_enabled(device_t dev);
253 int     device_is_quiet(device_t dev);
254 int     device_print_prettyname(device_t dev);
255 int     device_printf(device_t dev, const char *, ...) __printflike(2, 3);
256 int     device_probe_and_attach(device_t dev);
257 void    device_quiet(device_t dev);
258 void    device_set_desc(device_t dev, const char* desc);
259 void    device_set_desc_copy(device_t dev, const char* desc);
260 int     device_set_devclass(device_t dev, const char *classname);
261 int     device_set_driver(device_t dev, driver_t *driver);
262 void    device_set_flags(device_t dev, u_int32_t flags);
263 void    device_set_softc(device_t dev, void *softc);
264 int     device_set_unit(device_t dev, int unit);        /* XXX DONT USE XXX */
265 int     device_shutdown(device_t dev);
266 void    device_unbusy(device_t dev);
267 void    device_verbose(device_t dev);
268
269 /*
270  * Access functions for devclass.
271  */
272 int     devclass_add_driver(devclass_t dc, driver_t *driver);
273 int     devclass_delete_driver(devclass_t dc, driver_t *driver);
274 devclass_t      devclass_create(const char *classname);
275 devclass_t      devclass_find(const char *classname);
276 driver_t        *devclass_find_driver(devclass_t dc, const char *classname);
277 const char      *devclass_get_name(devclass_t dc);
278 device_t        devclass_get_device(devclass_t dc, int unit);
279 void    *devclass_get_softc(devclass_t dc, int unit);
280 int     devclass_get_devices(devclass_t dc, device_t **listp, int *countp);
281 int     devclass_get_maxunit(devclass_t dc);
282
283 /*
284  * Access functions for device resources.
285  */
286
287 int     resource_int_value(const char *name, int unit, const char *resname,
288                            int *result);
289 int     resource_long_value(const char *name, int unit, const char *resname,
290                             long *result);
291 int     resource_string_value(const char *name, int unit, const char *resname,
292                               char **result);
293 int     resource_query_string(int i, const char *resname, const char *value);
294 char    *resource_query_name(int i);
295 int     resource_query_unit(int i);
296 int     resource_locate(int i, const char *resname);
297 int     resource_set_int(const char *name, int unit, const char *resname,
298                          int value);
299 int     resource_set_long(const char *name, int unit, const char *resname,
300                           long value);
301 int     resource_set_string(const char *name, int unit, const char *resname,
302                             const char *value);
303 int     resource_count(void);
304
305 /*
306  * Shorthand for constructing method tables.
307  */
308 #define DEVMETHOD       KOBJMETHOD
309
310 /*
311  * Some common device interfaces.
312  */
313 #include "device_if.h"
314 #include "bus_if.h"
315
316 struct  module;
317
318 int     driver_module_handler(struct module *, int, void *);
319
320 /*
321  * Module support for automatically adding drivers to busses.
322  */
323 struct driver_module_data {
324         int             (*dmd_chainevh)(struct module *, int, void *);
325         void            *dmd_chainarg;
326         const char      *dmd_busname;
327         driver_t        **dmd_drivers;
328         int             dmd_ndrivers;
329         devclass_t      *dmd_devclass;
330 };
331
332 #define DRIVER_MODULE(name, busname, driver, devclass, evh, arg)        \
333                                                                         \
334 static driver_t *name##_##busname##_driver_list[] = { &driver };        \
335 static struct driver_module_data name##_##busname##_driver_mod = {      \
336         evh, arg,                                                       \
337         #busname,                                                       \
338         name##_##busname##_driver_list,                                 \
339         (sizeof name##_##busname##_driver_list) /                       \
340                 (sizeof name##_##busname##_driver_list[0]),             \
341         &devclass                                                       \
342 };                                                                      \
343                                                                         \
344 static moduledata_t name##_##busname##_mod = {                          \
345         #busname "/" #name,                                             \
346         driver_module_handler,                                          \
347         &name##_##busname##_driver_mod                                  \
348 };                                                                      \
349 DECLARE_MODULE(name##_##busname, name##_##busname##_mod,                \
350                SI_SUB_DRIVERS, SI_ORDER_MIDDLE)
351
352 #define MULTI_DRIVER_MODULE(name, busname, drivers, devclass, evh, arg) \
353                                                                         \
354 static driver_t name##_##busname##_driver_list[] = drivers;             \
355 static struct driver_module_data name##_##busname##_driver_mod = {      \
356         evh, arg,                                                       \
357         #busname,                                                       \
358         name##_##busname##_driver_list,                                 \
359         (sizeof name##_##busname##_driver_list) /                       \
360                 (sizeof name##_##busname##_driver_list[0]),             \
361         &devclass                                                       \
362 };                                                                      \
363                                                                         \
364 static moduledata_t name##_##busname##_mod = {                          \
365         #busname "/" #name,                                             \
366         driver_module_handler,                                          \
367         &name##_##busname##_driver_mod                                  \
368 };                                                                      \
369 DECLARE_MODULE(name##_##busname, name##_##busname##_mod,                \
370                SI_SUB_DRIVERS, SI_ORDER_MIDDLE)
371
372 #endif /* _KERNEL */
373
374 #endif /* !_SYS_BUS_H_ */