source: mainline/uspace/lib/drv/include/driver.h@ 96b02eb9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 96b02eb9 was 96b02eb9, checked in by Martin Decky <martin@…>, 15 years ago

more unification of basic types

  • use sysarg_t and native_t (unsigned and signed variant) in both kernel and uspace
  • remove ipcarg_t in favour of sysarg_t

(no change in functionality)

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