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