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 | /* device interface */
|
---|
53 |
|
---|
54 | /*
|
---|
55 | * First two parameters: device and interface structure registered by the
|
---|
56 | * devices driver.
|
---|
57 | */
|
---|
58 | typedef void remote_iface_func_t(device_t *, void *, ipc_callid_t,
|
---|
59 | ipc_call_t *);
|
---|
60 | typedef remote_iface_func_t *remote_iface_func_ptr_t;
|
---|
61 | typedef void remote_handler_t(device_t *, ipc_callid_t, ipc_call_t *);
|
---|
62 |
|
---|
63 | typedef struct {
|
---|
64 | size_t method_count;
|
---|
65 | remote_iface_func_ptr_t *methods;
|
---|
66 | } remote_iface_t;
|
---|
67 |
|
---|
68 | typedef struct {
|
---|
69 | remote_iface_t *ifaces[DEV_IFACE_COUNT];
|
---|
70 | } iface_dipatch_table_t;
|
---|
71 |
|
---|
72 |
|
---|
73 | static inline bool is_valid_iface_idx(int idx)
|
---|
74 | {
|
---|
75 | return 0 <= idx && idx < DEV_IFACE_MAX;
|
---|
76 | }
|
---|
77 |
|
---|
78 | remote_iface_t *get_remote_iface(int);
|
---|
79 | remote_iface_func_ptr_t get_remote_method(remote_iface_t *, sysarg_t);
|
---|
80 |
|
---|
81 |
|
---|
82 | /* device class */
|
---|
83 |
|
---|
84 | /** Devices operations. */
|
---|
85 | typedef struct device_ops {
|
---|
86 | /**
|
---|
87 | * Optional callback function called when a client is connecting to the
|
---|
88 | * device.
|
---|
89 | */
|
---|
90 | int (*open)(device_t *);
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Optional callback function called when a client is disconnecting from
|
---|
94 | * the device.
|
---|
95 | */
|
---|
96 | void (*close)(device_t *);
|
---|
97 |
|
---|
98 | /** The table of standard interfaces implemented by the device. */
|
---|
99 | void *interfaces[DEV_IFACE_COUNT];
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * The default handler of remote client requests. If the client's remote
|
---|
103 | * request cannot be handled by any of the standard interfaces, the
|
---|
104 | * default handler is used.
|
---|
105 | */
|
---|
106 | remote_handler_t *default_handler;
|
---|
107 | } device_ops_t;
|
---|
108 |
|
---|
109 |
|
---|
110 | /* device */
|
---|
111 |
|
---|
112 | /** The device. */
|
---|
113 | struct device {
|
---|
114 | /**
|
---|
115 | * Globally unique device identifier (assigned to the device by the
|
---|
116 | * device manager).
|
---|
117 | */
|
---|
118 | devman_handle_t handle;
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * The phone to the parent device driver (if it is different from this
|
---|
122 | * driver).
|
---|
123 | */
|
---|
124 | int parent_phone;
|
---|
125 |
|
---|
126 | /** Parent device if handled by this driver, NULL otherwise. */
|
---|
127 | device_t *parent;
|
---|
128 | /** The device's name. */
|
---|
129 | const char *name;
|
---|
130 | /** The list of device ids for device-to-driver matching. */
|
---|
131 | match_id_list_t match_ids;
|
---|
132 | /** The device driver's data associated with this device. */
|
---|
133 | void *driver_data;
|
---|
134 | /** The implementation of operations provided by this device. */
|
---|
135 | device_ops_t *ops;
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Pointer to the previous and next device in the list of devices
|
---|
139 | * handled by the driver.
|
---|
140 | */
|
---|
141 | link_t link;
|
---|
142 | };
|
---|
143 |
|
---|
144 |
|
---|
145 | /* driver */
|
---|
146 |
|
---|
147 | /** Generic device driver operations. */
|
---|
148 | typedef struct driver_ops {
|
---|
149 | /** Callback method for passing a new device to the device driver.*/
|
---|
150 | int (*add_device)(device_t *dev);
|
---|
151 | /* TODO add other generic driver operations */
|
---|
152 | } driver_ops_t;
|
---|
153 |
|
---|
154 | /** The driver structure.*/
|
---|
155 | typedef struct driver {
|
---|
156 | /** The name of the device driver. */
|
---|
157 | const char *name;
|
---|
158 | /** Generic device driver operations. */
|
---|
159 | driver_ops_t *driver_ops;
|
---|
160 | } driver_t;
|
---|
161 |
|
---|
162 | int driver_main(driver_t *);
|
---|
163 |
|
---|
164 | /** Create new device structure.
|
---|
165 | *
|
---|
166 | * @return The device structure.
|
---|
167 | */
|
---|
168 | static inline device_t *create_device(void)
|
---|
169 | {
|
---|
170 | device_t *dev = malloc(sizeof(device_t));
|
---|
171 | if (NULL != dev) {
|
---|
172 | memset(dev, 0, sizeof(device_t));
|
---|
173 | init_match_ids(&dev->match_ids);
|
---|
174 | }
|
---|
175 | return dev;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /** Delete device structure.
|
---|
179 | *
|
---|
180 | * @param dev The device structure.
|
---|
181 | */
|
---|
182 | static inline void delete_device(device_t *dev)
|
---|
183 | {
|
---|
184 | clean_match_ids(&dev->match_ids);
|
---|
185 | if (NULL != dev->name)
|
---|
186 | free(dev->name);
|
---|
187 | free(dev);
|
---|
188 | }
|
---|
189 |
|
---|
190 | static inline void *device_get_iface(device_t *dev, dev_inferface_idx_t idx)
|
---|
191 | {
|
---|
192 | assert(is_valid_iface_idx(idx));
|
---|
193 | if (NULL == dev->ops)
|
---|
194 | return NULL;
|
---|
195 | return dev->ops->interfaces[idx];
|
---|
196 | }
|
---|
197 |
|
---|
198 | int child_device_register(device_t *, device_t *);
|
---|
199 | int child_device_register_wrapper(device_t *, const char *, const char *, int);
|
---|
200 |
|
---|
201 |
|
---|
202 | /* interrupts */
|
---|
203 |
|
---|
204 | typedef void interrupt_handler_t(device_t *, ipc_callid_t, ipc_call_t *);
|
---|
205 |
|
---|
206 | typedef struct interrupt_context {
|
---|
207 | int id;
|
---|
208 | device_t *dev;
|
---|
209 | int irq;
|
---|
210 | interrupt_handler_t *handler;
|
---|
211 | link_t link;
|
---|
212 | } interrupt_context_t;
|
---|
213 |
|
---|
214 | typedef struct interrupt_context_list {
|
---|
215 | int curr_id;
|
---|
216 | link_t contexts;
|
---|
217 | fibril_mutex_t mutex;
|
---|
218 | } interrupt_context_list_t;
|
---|
219 |
|
---|
220 | static inline interrupt_context_t *create_interrupt_context(void)
|
---|
221 | {
|
---|
222 | interrupt_context_t *ctx;
|
---|
223 |
|
---|
224 | ctx = (interrupt_context_t *) malloc(sizeof(interrupt_context_t));
|
---|
225 | if (NULL != ctx)
|
---|
226 | memset(ctx, 0, sizeof(interrupt_context_t));
|
---|
227 |
|
---|
228 | return ctx;
|
---|
229 | }
|
---|
230 |
|
---|
231 | static inline void delete_interrupt_context(interrupt_context_t *ctx)
|
---|
232 | {
|
---|
233 | if (NULL != ctx)
|
---|
234 | free(ctx);
|
---|
235 | }
|
---|
236 |
|
---|
237 | static inline void init_interrupt_context_list(interrupt_context_list_t *list)
|
---|
238 | {
|
---|
239 | memset(list, 0, sizeof(interrupt_context_list_t));
|
---|
240 | fibril_mutex_initialize(&list->mutex);
|
---|
241 | list_initialize(&list->contexts);
|
---|
242 | }
|
---|
243 |
|
---|
244 | static inline void
|
---|
245 | add_interrupt_context(interrupt_context_list_t *list, interrupt_context_t *ctx)
|
---|
246 | {
|
---|
247 | fibril_mutex_lock(&list->mutex);
|
---|
248 | ctx->id = list->curr_id++;
|
---|
249 | list_append(&ctx->link, &list->contexts);
|
---|
250 | fibril_mutex_unlock(&list->mutex);
|
---|
251 | }
|
---|
252 |
|
---|
253 | static inline void
|
---|
254 | remove_interrupt_context(interrupt_context_list_t *list,
|
---|
255 | interrupt_context_t *ctx)
|
---|
256 | {
|
---|
257 | fibril_mutex_lock(&list->mutex);
|
---|
258 | list_remove(&ctx->link);
|
---|
259 | fibril_mutex_unlock(&list->mutex);
|
---|
260 | }
|
---|
261 |
|
---|
262 | static inline interrupt_context_t *
|
---|
263 | find_interrupt_context_by_id(interrupt_context_list_t *list, int id)
|
---|
264 | {
|
---|
265 | fibril_mutex_lock(&list->mutex);
|
---|
266 |
|
---|
267 | link_t *link = list->contexts.next;
|
---|
268 | interrupt_context_t *ctx;
|
---|
269 |
|
---|
270 | while (link != &list->contexts) {
|
---|
271 | ctx = list_get_instance(link, interrupt_context_t, link);
|
---|
272 | if (id == ctx->id) {
|
---|
273 | fibril_mutex_unlock(&list->mutex);
|
---|
274 | return ctx;
|
---|
275 | }
|
---|
276 | link = link->next;
|
---|
277 | }
|
---|
278 |
|
---|
279 | fibril_mutex_unlock(&list->mutex);
|
---|
280 | return NULL;
|
---|
281 | }
|
---|
282 |
|
---|
283 | static inline interrupt_context_t *
|
---|
284 | find_interrupt_context(interrupt_context_list_t *list, device_t *dev, int irq)
|
---|
285 | {
|
---|
286 | fibril_mutex_lock(&list->mutex);
|
---|
287 |
|
---|
288 | link_t *link = list->contexts.next;
|
---|
289 | interrupt_context_t *ctx;
|
---|
290 |
|
---|
291 | while (link != &list->contexts) {
|
---|
292 | ctx = list_get_instance(link, interrupt_context_t, link);
|
---|
293 | if (irq == ctx->irq && dev == ctx->dev) {
|
---|
294 | fibril_mutex_unlock(&list->mutex);
|
---|
295 | return ctx;
|
---|
296 | }
|
---|
297 | link = link->next;
|
---|
298 | }
|
---|
299 |
|
---|
300 | fibril_mutex_unlock(&list->mutex);
|
---|
301 | return NULL;
|
---|
302 | }
|
---|
303 |
|
---|
304 | int register_interrupt_handler(device_t *, int, interrupt_handler_t *,
|
---|
305 | irq_code_t *);
|
---|
306 | int unregister_interrupt_handler(device_t *, int);
|
---|
307 |
|
---|
308 |
|
---|
309 | /* default handler for client requests */
|
---|
310 |
|
---|
311 | static inline remote_handler_t *device_get_default_handler(device_t *dev)
|
---|
312 | {
|
---|
313 | if (NULL == dev->ops)
|
---|
314 | return NULL;
|
---|
315 | return dev->ops->default_handler;
|
---|
316 | }
|
---|
317 |
|
---|
318 | static inline int add_device_to_class(device_t *dev, const char *class_name)
|
---|
319 | {
|
---|
320 | return devman_add_device_to_class(dev->handle, class_name);
|
---|
321 | }
|
---|
322 |
|
---|
323 | #endif
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * @}
|
---|
327 | */
|
---|