1 | /*
|
---|
2 | * Copyright (c) 2010 Lenka Trochtova
|
---|
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 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libdrv
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #ifndef LIBDRV_DRIVER_H_
|
---|
36 | #define LIBDRV_DRIVER_H_
|
---|
37 |
|
---|
38 | #include <adt/list.h>
|
---|
39 | #include <ipc/ipc.h>
|
---|
40 | #include <devman.h>
|
---|
41 | #include <ipc/devman.h>
|
---|
42 | #include <ipc/dev_iface.h>
|
---|
43 | #include <assert.h>
|
---|
44 | #include <ddi.h>
|
---|
45 | #include <libarch/ddi.h>
|
---|
46 | #include <fibril_synch.h>
|
---|
47 | #include <malloc.h>
|
---|
48 |
|
---|
49 | struct device;
|
---|
50 | typedef struct device device_t;
|
---|
51 |
|
---|
52 | /*
|
---|
53 | * Device interface
|
---|
54 | */
|
---|
55 |
|
---|
56 | /*
|
---|
57 | * First two parameters: device and interface structure registered by the
|
---|
58 | * devices driver.
|
---|
59 | */
|
---|
60 | typedef void remote_iface_func_t(device_t *, void *, ipc_callid_t,
|
---|
61 | ipc_call_t *);
|
---|
62 | typedef remote_iface_func_t *remote_iface_func_ptr_t;
|
---|
63 | typedef void remote_handler_t(device_t *, ipc_callid_t, ipc_call_t *);
|
---|
64 |
|
---|
65 | typedef struct {
|
---|
66 | size_t method_count;
|
---|
67 | remote_iface_func_ptr_t *methods;
|
---|
68 | } remote_iface_t;
|
---|
69 |
|
---|
70 | typedef struct {
|
---|
71 | remote_iface_t *ifaces[DEV_IFACE_COUNT];
|
---|
72 | } iface_dipatch_table_t;
|
---|
73 |
|
---|
74 |
|
---|
75 | static inline bool is_valid_iface_idx(int idx)
|
---|
76 | {
|
---|
77 | return (0 <= idx) && (idx < DEV_IFACE_MAX);
|
---|
78 | }
|
---|
79 |
|
---|
80 | remote_iface_t *get_remote_iface(int);
|
---|
81 | remote_iface_func_ptr_t get_remote_method(remote_iface_t *, sysarg_t);
|
---|
82 |
|
---|
83 |
|
---|
84 | /*
|
---|
85 | * Device class
|
---|
86 | */
|
---|
87 |
|
---|
88 | /** Devices operations */
|
---|
89 | typedef struct device_ops {
|
---|
90 | /**
|
---|
91 | * Optional callback function called when a client is connecting to the
|
---|
92 | * device.
|
---|
93 | */
|
---|
94 | int (*open)(device_t *);
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Optional callback function called when a client is disconnecting from
|
---|
98 | * the device.
|
---|
99 | */
|
---|
100 | void (*close)(device_t *);
|
---|
101 |
|
---|
102 | /** The table of standard interfaces implemented by the device. */
|
---|
103 | void *interfaces[DEV_IFACE_COUNT];
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * The default handler of remote client requests. If the client's remote
|
---|
107 | * request cannot be handled by any of the standard interfaces, the
|
---|
108 | * default handler is used.
|
---|
109 | */
|
---|
110 | remote_handler_t *default_handler;
|
---|
111 | } device_ops_t;
|
---|
112 |
|
---|
113 |
|
---|
114 | /*
|
---|
115 | * Device
|
---|
116 | */
|
---|
117 |
|
---|
118 | /** Device structure */
|
---|
119 | struct device {
|
---|
120 | /**
|
---|
121 | * Globally unique device identifier (assigned to the device by the
|
---|
122 | * device manager).
|
---|
123 | */
|
---|
124 | devman_handle_t handle;
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Phone to the parent device driver (if it is different from this
|
---|
128 | * driver)
|
---|
129 | */
|
---|
130 | int parent_phone;
|
---|
131 |
|
---|
132 | /** Parent device if handled by this driver, NULL otherwise */
|
---|
133 | device_t *parent;
|
---|
134 | /** Device name */
|
---|
135 | const char *name;
|
---|
136 | /** List of device ids for device-to-driver matching */
|
---|
137 | match_id_list_t match_ids;
|
---|
138 | /** Driver-specific data associated with this device */
|
---|
139 | void *driver_data;
|
---|
140 | /** The implementation of operations provided by this device */
|
---|
141 | device_ops_t *ops;
|
---|
142 |
|
---|
143 | /** Link in the list of devices handled by the driver */
|
---|
144 | link_t link;
|
---|
145 | };
|
---|
146 |
|
---|
147 |
|
---|
148 | /*
|
---|
149 | * Driver
|
---|
150 | */
|
---|
151 |
|
---|
152 | /** Generic device driver operations */
|
---|
153 | typedef struct driver_ops {
|
---|
154 | /** Callback method for passing a new device to the device driver */
|
---|
155 | int (*add_device)(device_t *dev);
|
---|
156 | /* TODO: add other generic driver operations */
|
---|
157 | } driver_ops_t;
|
---|
158 |
|
---|
159 | /** Driver structure */
|
---|
160 | typedef struct driver {
|
---|
161 | /** Name of the device driver */
|
---|
162 | const char *name;
|
---|
163 | /** Generic device driver operations */
|
---|
164 | driver_ops_t *driver_ops;
|
---|
165 | } driver_t;
|
---|
166 |
|
---|
167 | int driver_main(driver_t *);
|
---|
168 |
|
---|
169 | /** Create new device structure.
|
---|
170 | *
|
---|
171 | * @return The device structure.
|
---|
172 | */
|
---|
173 | static inline device_t *create_device(void)
|
---|
174 | {
|
---|
175 | device_t *dev = malloc(sizeof(device_t));
|
---|
176 |
|
---|
177 | if (dev != NULL) {
|
---|
178 | memset(dev, 0, sizeof(device_t));
|
---|
179 | init_match_ids(&dev->match_ids);
|
---|
180 | }
|
---|
181 |
|
---|
182 | return dev;
|
---|
183 | }
|
---|
184 |
|
---|
185 | /** Delete device structure.
|
---|
186 | *
|
---|
187 | * @param dev The device structure.
|
---|
188 | */
|
---|
189 | static inline void delete_device(device_t *dev)
|
---|
190 | {
|
---|
191 | clean_match_ids(&dev->match_ids);
|
---|
192 | if (dev->name != NULL)
|
---|
193 | free(dev->name);
|
---|
194 | free(dev);
|
---|
195 | }
|
---|
196 |
|
---|
197 | static inline void *device_get_ops(device_t *dev, dev_inferface_idx_t idx)
|
---|
198 | {
|
---|
199 | assert(is_valid_iface_idx(idx));
|
---|
200 | if (dev->ops == NULL)
|
---|
201 | return NULL;
|
---|
202 | return dev->ops->interfaces[idx];
|
---|
203 | }
|
---|
204 |
|
---|
205 | int child_device_register(device_t *, device_t *);
|
---|
206 | int child_device_register_wrapper(device_t *, const char *, const char *, int);
|
---|
207 |
|
---|
208 | /*
|
---|
209 | * Interrupts
|
---|
210 | */
|
---|
211 |
|
---|
212 | typedef void interrupt_handler_t(device_t *, ipc_callid_t, ipc_call_t *);
|
---|
213 |
|
---|
214 | typedef struct interrupt_context {
|
---|
215 | int id;
|
---|
216 | device_t *dev;
|
---|
217 | int irq;
|
---|
218 | interrupt_handler_t *handler;
|
---|
219 | link_t link;
|
---|
220 | } interrupt_context_t;
|
---|
221 |
|
---|
222 | typedef struct interrupt_context_list {
|
---|
223 | int curr_id;
|
---|
224 | link_t contexts;
|
---|
225 | fibril_mutex_t mutex;
|
---|
226 | } interrupt_context_list_t;
|
---|
227 |
|
---|
228 | static inline interrupt_context_t *create_interrupt_context(void)
|
---|
229 | {
|
---|
230 | interrupt_context_t *ctx;
|
---|
231 |
|
---|
232 | ctx = (interrupt_context_t *) malloc(sizeof(interrupt_context_t));
|
---|
233 | if (ctx != NULL)
|
---|
234 | memset(ctx, 0, sizeof(interrupt_context_t));
|
---|
235 |
|
---|
236 | return ctx;
|
---|
237 | }
|
---|
238 |
|
---|
239 | static inline void delete_interrupt_context(interrupt_context_t *ctx)
|
---|
240 | {
|
---|
241 | if (ctx != NULL)
|
---|
242 | free(ctx);
|
---|
243 | }
|
---|
244 |
|
---|
245 | static inline void init_interrupt_context_list(interrupt_context_list_t *list)
|
---|
246 | {
|
---|
247 | memset(list, 0, sizeof(interrupt_context_list_t));
|
---|
248 | fibril_mutex_initialize(&list->mutex);
|
---|
249 | list_initialize(&list->contexts);
|
---|
250 | }
|
---|
251 |
|
---|
252 | static inline void
|
---|
253 | add_interrupt_context(interrupt_context_list_t *list, interrupt_context_t *ctx)
|
---|
254 | {
|
---|
255 | fibril_mutex_lock(&list->mutex);
|
---|
256 | ctx->id = list->curr_id++;
|
---|
257 | list_append(&ctx->link, &list->contexts);
|
---|
258 | fibril_mutex_unlock(&list->mutex);
|
---|
259 | }
|
---|
260 |
|
---|
261 | static inline void
|
---|
262 | remove_interrupt_context(interrupt_context_list_t *list,
|
---|
263 | interrupt_context_t *ctx)
|
---|
264 | {
|
---|
265 | fibril_mutex_lock(&list->mutex);
|
---|
266 | list_remove(&ctx->link);
|
---|
267 | fibril_mutex_unlock(&list->mutex);
|
---|
268 | }
|
---|
269 |
|
---|
270 | static inline interrupt_context_t *
|
---|
271 | find_interrupt_context_by_id(interrupt_context_list_t *list, int id)
|
---|
272 | {
|
---|
273 | fibril_mutex_lock(&list->mutex);
|
---|
274 |
|
---|
275 | link_t *link = list->contexts.next;
|
---|
276 | interrupt_context_t *ctx;
|
---|
277 |
|
---|
278 | while (link != &list->contexts) {
|
---|
279 | ctx = list_get_instance(link, interrupt_context_t, link);
|
---|
280 | if (ctx->id == id) {
|
---|
281 | fibril_mutex_unlock(&list->mutex);
|
---|
282 | return ctx;
|
---|
283 | }
|
---|
284 | link = link->next;
|
---|
285 | }
|
---|
286 |
|
---|
287 | fibril_mutex_unlock(&list->mutex);
|
---|
288 | return NULL;
|
---|
289 | }
|
---|
290 |
|
---|
291 | static inline interrupt_context_t *
|
---|
292 | find_interrupt_context(interrupt_context_list_t *list, device_t *dev, int irq)
|
---|
293 | {
|
---|
294 | fibril_mutex_lock(&list->mutex);
|
---|
295 |
|
---|
296 | link_t *link = list->contexts.next;
|
---|
297 | interrupt_context_t *ctx;
|
---|
298 |
|
---|
299 | while (link != &list->contexts) {
|
---|
300 | ctx = list_get_instance(link, interrupt_context_t, link);
|
---|
301 | if (ctx->irq == irq && ctx->dev == dev) {
|
---|
302 | fibril_mutex_unlock(&list->mutex);
|
---|
303 | return ctx;
|
---|
304 | }
|
---|
305 | link = link->next;
|
---|
306 | }
|
---|
307 |
|
---|
308 | fibril_mutex_unlock(&list->mutex);
|
---|
309 | return NULL;
|
---|
310 | }
|
---|
311 |
|
---|
312 | int register_interrupt_handler(device_t *, int, interrupt_handler_t *,
|
---|
313 | irq_code_t *);
|
---|
314 | int unregister_interrupt_handler(device_t *, int);
|
---|
315 |
|
---|
316 |
|
---|
317 | /** Get default handler for client requests */
|
---|
318 | static inline remote_handler_t *device_get_default_handler(device_t *dev)
|
---|
319 | {
|
---|
320 | if (dev->ops == NULL)
|
---|
321 | return NULL;
|
---|
322 | return dev->ops->default_handler;
|
---|
323 | }
|
---|
324 |
|
---|
325 | static inline int add_device_to_class(device_t *dev, const char *class_name)
|
---|
326 | {
|
---|
327 | return devman_add_device_to_class(dev->handle, class_name);
|
---|
328 | }
|
---|
329 |
|
---|
330 | #endif
|
---|
331 |
|
---|
332 | /**
|
---|
333 | * @}
|
---|
334 | */
|
---|