source: mainline/uspace/lib/drv/include/driver.h@ 692c40cb

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 692c40cb was 692c40cb, checked in by Lenka Trochtova <trochtova.lenka@…>, 15 years ago

Introduce device classes.Device class specifies functional type of the device. Device classes are identified by their string names (actually device class is just a string value). Device classes can be dynamically added. A device can be added to any number of classes.

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