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