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 <kernel/ddi/irq.h>
|
---|
39 | #include <adt/list.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 | #include "dev_iface.h"
|
---|
50 |
|
---|
51 | typedef struct ddf_dev ddf_dev_t;
|
---|
52 | typedef struct ddf_fun ddf_fun_t;
|
---|
53 |
|
---|
54 | /*
|
---|
55 | * Device class
|
---|
56 | */
|
---|
57 |
|
---|
58 | /** Devices operations */
|
---|
59 | typedef struct ddf_dev_ops {
|
---|
60 | /**
|
---|
61 | * Optional callback function called when a client is connecting to the
|
---|
62 | * device.
|
---|
63 | */
|
---|
64 | int (*open)(ddf_fun_t *);
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Optional callback function called when a client is disconnecting from
|
---|
68 | * the device.
|
---|
69 | */
|
---|
70 | void (*close)(ddf_fun_t *);
|
---|
71 |
|
---|
72 | /** The table of standard interfaces implemented by the device. */
|
---|
73 | void *interfaces[DEV_IFACE_COUNT];
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * The default handler of remote client requests. If the client's remote
|
---|
77 | * request cannot be handled by any of the standard interfaces, the
|
---|
78 | * default handler is used.
|
---|
79 | */
|
---|
80 | remote_handler_t *default_handler;
|
---|
81 | } ddf_dev_ops_t;
|
---|
82 |
|
---|
83 | /*
|
---|
84 | * Device
|
---|
85 | */
|
---|
86 |
|
---|
87 | /** Device structure */
|
---|
88 | struct ddf_dev {
|
---|
89 | /**
|
---|
90 | * Globally unique device identifier (assigned to the device by the
|
---|
91 | * device manager).
|
---|
92 | */
|
---|
93 | devman_handle_t handle;
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Phone to the parent device driver (if it is different from this
|
---|
97 | * driver)
|
---|
98 | */
|
---|
99 | int parent_phone;
|
---|
100 |
|
---|
101 | /** Device name */
|
---|
102 | const char *name;
|
---|
103 |
|
---|
104 | /** Driver-specific data associated with this device */
|
---|
105 | void *driver_data;
|
---|
106 |
|
---|
107 | /** Link in the list of devices handled by the driver */
|
---|
108 | link_t link;
|
---|
109 | };
|
---|
110 |
|
---|
111 | /** Function structure */
|
---|
112 | struct ddf_fun {
|
---|
113 | /** True if bound to the device manager */
|
---|
114 | bool bound;
|
---|
115 | /** Function indentifier (asigned by device manager) */
|
---|
116 | devman_handle_t handle;
|
---|
117 |
|
---|
118 | /** Device which this function belogs to */
|
---|
119 | ddf_dev_t *dev;
|
---|
120 |
|
---|
121 | /** Function type */
|
---|
122 | fun_type_t ftype;
|
---|
123 | /** Function name */
|
---|
124 | const char *name;
|
---|
125 | /** List of device ids for driver matching */
|
---|
126 | match_id_list_t match_ids;
|
---|
127 | /** Driver-specific data associated with this function */
|
---|
128 | void *driver_data;
|
---|
129 | /** Implementation of operations provided by this function */
|
---|
130 | ddf_dev_ops_t *ops;
|
---|
131 |
|
---|
132 | /** Link in the list of functions handled by the driver */
|
---|
133 | link_t link;
|
---|
134 | };
|
---|
135 |
|
---|
136 | /*
|
---|
137 | * Driver
|
---|
138 | */
|
---|
139 |
|
---|
140 | /** Generic device driver operations */
|
---|
141 | typedef struct driver_ops {
|
---|
142 | /** Callback method for passing a new device to the device driver */
|
---|
143 | int (*add_device)(ddf_dev_t *dev);
|
---|
144 | /* TODO: add other generic driver operations */
|
---|
145 | } driver_ops_t;
|
---|
146 |
|
---|
147 | /** Driver structure */
|
---|
148 | typedef struct driver {
|
---|
149 | /** Name of the device driver */
|
---|
150 | const char *name;
|
---|
151 | /** Generic device driver operations */
|
---|
152 | driver_ops_t *driver_ops;
|
---|
153 | } driver_t;
|
---|
154 |
|
---|
155 | int ddf_driver_main(driver_t *);
|
---|
156 |
|
---|
157 | extern ddf_fun_t *ddf_fun_create(ddf_dev_t *, fun_type_t, const char *);
|
---|
158 | extern void ddf_fun_destroy(ddf_fun_t *);
|
---|
159 | extern int ddf_fun_bind(ddf_fun_t *);
|
---|
160 | extern int ddf_fun_add_match_id(ddf_fun_t *, const char *, int);
|
---|
161 |
|
---|
162 | extern void *function_get_ops(ddf_fun_t *, dev_inferface_idx_t);
|
---|
163 |
|
---|
164 | /*
|
---|
165 | * Interrupts
|
---|
166 | */
|
---|
167 |
|
---|
168 | typedef void interrupt_handler_t(ddf_dev_t *, ipc_callid_t, ipc_call_t *);
|
---|
169 |
|
---|
170 | typedef struct interrupt_context {
|
---|
171 | int id;
|
---|
172 | ddf_dev_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 | extern interrupt_context_t *create_interrupt_context(void);
|
---|
185 | extern void delete_interrupt_context(interrupt_context_t *);
|
---|
186 | extern void init_interrupt_context_list(interrupt_context_list_t *);
|
---|
187 | extern void add_interrupt_context(interrupt_context_list_t *,
|
---|
188 | interrupt_context_t *);
|
---|
189 | extern void remove_interrupt_context(interrupt_context_list_t *,
|
---|
190 | interrupt_context_t *);
|
---|
191 | extern interrupt_context_t *find_interrupt_context_by_id(
|
---|
192 | interrupt_context_list_t *, int);
|
---|
193 | extern interrupt_context_t *find_interrupt_context(
|
---|
194 | interrupt_context_list_t *, ddf_dev_t *, int);
|
---|
195 |
|
---|
196 | extern int register_interrupt_handler(ddf_dev_t *, int, interrupt_handler_t *,
|
---|
197 | irq_code_t *);
|
---|
198 | extern int unregister_interrupt_handler(ddf_dev_t *, int);
|
---|
199 |
|
---|
200 | extern remote_handler_t *function_get_default_handler(ddf_fun_t *);
|
---|
201 | extern int ddf_fun_add_to_class(ddf_fun_t *fun, const char *class_name);
|
---|
202 |
|
---|
203 | #endif
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * @}
|
---|
207 | */
|
---|