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 devman
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | #ifndef DEVMAN_H_
|
---|
34 | #define DEVMAN_H_
|
---|
35 |
|
---|
36 | #include <assert.h>
|
---|
37 | #include <bool.h>
|
---|
38 | #include <dirent.h>
|
---|
39 | #include <string.h>
|
---|
40 | #include <adt/list.h>
|
---|
41 | #include <ipc/ipc.h>
|
---|
42 | #include <ipc/devman.h>
|
---|
43 | #include <fibril_synch.h>
|
---|
44 | #include <atomic.h>
|
---|
45 |
|
---|
46 | #include "util.h"
|
---|
47 |
|
---|
48 | #define NAME "devman"
|
---|
49 |
|
---|
50 | #define MATCH_EXT ".ma"
|
---|
51 | #define MAX_DEV 256
|
---|
52 |
|
---|
53 | struct node;
|
---|
54 |
|
---|
55 | typedef struct node node_t;
|
---|
56 |
|
---|
57 | typedef enum {
|
---|
58 | /** driver has not been started */
|
---|
59 | DRIVER_NOT_STARTED = 0,
|
---|
60 | /** driver has been started, but has not registered as running and ready to receive requests */
|
---|
61 | DRIVER_STARTING,
|
---|
62 | /** driver is running and prepared to serve incomming requests */
|
---|
63 | DRIVER_RUNNING
|
---|
64 | } driver_state_t;
|
---|
65 |
|
---|
66 | /** Representation of device driver.
|
---|
67 | */
|
---|
68 | typedef struct driver {
|
---|
69 | /** Pointers to previous and next drivers in a linked list */
|
---|
70 | link_t drivers;
|
---|
71 | /** Specifies whether the driver has been started and wheter is running and prepared to receive requests.*/
|
---|
72 | int state;
|
---|
73 | /** Phone asociated with this driver */
|
---|
74 | ipcarg_t phone;
|
---|
75 | /** Name of the device driver */
|
---|
76 | char *name;
|
---|
77 | /** Path to the driver's binary */
|
---|
78 | const char *binary_path;
|
---|
79 | /** List of device ids for device-to-driver matching.*/
|
---|
80 | match_id_list_t match_ids;
|
---|
81 | /** Pointer to the linked list of devices controlled by this driver */
|
---|
82 | link_t devices;
|
---|
83 | /** Fibril mutex for this driver - driver state, list of devices, phone.*/
|
---|
84 | fibril_mutex_t driver_mutex;
|
---|
85 | } driver_t;
|
---|
86 |
|
---|
87 | /** The list of drivers. */
|
---|
88 | typedef struct driver_list {
|
---|
89 | /** List of drivers */
|
---|
90 | link_t drivers;
|
---|
91 | /** Fibril mutex for list of drivers. */
|
---|
92 | fibril_mutex_t drivers_mutex;
|
---|
93 | } driver_list_t;
|
---|
94 |
|
---|
95 | /** The state of the device. */
|
---|
96 | typedef enum {
|
---|
97 | DEVICE_NOT_INITIALIZED = 0,
|
---|
98 | DEVICE_USABLE,
|
---|
99 | DEVICE_NOT_PRESENT,
|
---|
100 | DEVICE_INVALID
|
---|
101 | } device_state_t;
|
---|
102 |
|
---|
103 | /** Representation of a node in the device tree.*/
|
---|
104 | struct node {
|
---|
105 | /** The global unique identifier of the device.*/
|
---|
106 | device_handle_t handle;
|
---|
107 | /** The name of the device specified by its parent. */
|
---|
108 | char *name;
|
---|
109 | /** Full path and name of the device in device hierarchi (i. e. in full path in device tree).*/
|
---|
110 | char *pathname;
|
---|
111 | /** The node of the parent device. */
|
---|
112 | node_t *parent;
|
---|
113 | /** Pointers to previous and next child devices in the linked list of parent device's node.*/
|
---|
114 | link_t sibling;
|
---|
115 | /** List of child device nodes. */
|
---|
116 | link_t children;
|
---|
117 | /** Fibril mutex for the list of child device nodes of this node. */
|
---|
118 | fibril_mutex_t children_mutex;
|
---|
119 | /** List of device ids for device-to-driver matching.*/
|
---|
120 | match_id_list_t match_ids;
|
---|
121 | /** Driver of this device.*/
|
---|
122 | driver_t *drv;
|
---|
123 | /** The state of the device. */
|
---|
124 | device_state_t state;
|
---|
125 | /** Pointer to the previous and next device in the list of devices
|
---|
126 | owned by one driver */
|
---|
127 | link_t driver_devices;
|
---|
128 | };
|
---|
129 |
|
---|
130 | /** Represents device tree.
|
---|
131 | */
|
---|
132 | typedef struct dev_tree {
|
---|
133 | /** Root device node. */
|
---|
134 | node_t *root_node;
|
---|
135 | /** The next available handle - handles are assigned in a sequential manner.*/
|
---|
136 | atomic_t current_handle;
|
---|
137 | /** Handle-to-node mapping. */
|
---|
138 | node_t * node_map[MAX_DEV];
|
---|
139 | } dev_tree_t;
|
---|
140 |
|
---|
141 |
|
---|
142 |
|
---|
143 | // Match ids and scores
|
---|
144 |
|
---|
145 | int get_match_score(driver_t *drv, node_t *dev);
|
---|
146 |
|
---|
147 | bool parse_match_ids(const char *buf, match_id_list_t *ids);
|
---|
148 | bool read_match_ids(const char *conf_path, match_id_list_t *ids);
|
---|
149 | char * read_id(const char **buf) ;
|
---|
150 |
|
---|
151 | // Drivers
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Initialize the list of device driver's.
|
---|
155 | *
|
---|
156 | * @param drv_list the list of device driver's.
|
---|
157 | *
|
---|
158 | */
|
---|
159 | static inline void init_driver_list(driver_list_t *drv_list)
|
---|
160 | {
|
---|
161 | assert(NULL != drv_list);
|
---|
162 |
|
---|
163 | list_initialize(&drv_list->drivers);
|
---|
164 | fibril_mutex_initialize(&drv_list->drivers_mutex);
|
---|
165 | }
|
---|
166 |
|
---|
167 | driver_t * create_driver();
|
---|
168 | bool get_driver_info(const char *base_path, const char *name, driver_t *drv);
|
---|
169 | int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path);
|
---|
170 |
|
---|
171 | driver_t * find_best_match_driver(driver_list_t *drivers_list, node_t *node);
|
---|
172 | bool assign_driver(node_t *node, driver_list_t *drivers_list);
|
---|
173 |
|
---|
174 | void add_driver(driver_list_t *drivers_list, driver_t *drv);
|
---|
175 | void attach_driver(node_t *node, driver_t *drv);
|
---|
176 | void add_device(int phone, driver_t *drv, node_t *node);
|
---|
177 | bool start_driver(driver_t *drv);
|
---|
178 |
|
---|
179 | driver_t * find_driver(driver_list_t *drv_list, const char *drv_name);
|
---|
180 | void set_driver_phone(driver_t *driver, ipcarg_t phone);
|
---|
181 | void initialize_running_driver(driver_t *driver);
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Initialize device driver structure.
|
---|
185 | *
|
---|
186 | * @param drv the device driver structure.
|
---|
187 | *
|
---|
188 | */
|
---|
189 | static inline void init_driver(driver_t *drv)
|
---|
190 | {
|
---|
191 | assert(drv != NULL);
|
---|
192 |
|
---|
193 | memset(drv, 0, sizeof(driver_t));
|
---|
194 | list_initialize(&drv->match_ids.ids);
|
---|
195 | list_initialize(&drv->devices);
|
---|
196 | fibril_mutex_initialize(&drv->driver_mutex);
|
---|
197 | }
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Device driver structure clean-up.
|
---|
201 | *
|
---|
202 | * @param drv the device driver structure.
|
---|
203 | */
|
---|
204 | static inline void clean_driver(driver_t *drv)
|
---|
205 | {
|
---|
206 | assert(drv != NULL);
|
---|
207 |
|
---|
208 | free_not_null(drv->name);
|
---|
209 | free_not_null(drv->binary_path);
|
---|
210 |
|
---|
211 | clean_match_ids(&drv->match_ids);
|
---|
212 |
|
---|
213 | init_driver(drv);
|
---|
214 | }
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Delete device driver structure.
|
---|
218 | *
|
---|
219 | * @param drv the device driver structure.*
|
---|
220 | */
|
---|
221 | static inline void delete_driver(driver_t *drv)
|
---|
222 | {
|
---|
223 | assert(NULL != drv);
|
---|
224 |
|
---|
225 | clean_driver(drv);
|
---|
226 | free(drv);
|
---|
227 | }
|
---|
228 |
|
---|
229 | // Device nodes
|
---|
230 | /**
|
---|
231 | * Create a new device node.
|
---|
232 | *
|
---|
233 | * @return a device node structure.
|
---|
234 | *
|
---|
235 | */
|
---|
236 | static inline node_t * create_dev_node()
|
---|
237 | {
|
---|
238 | node_t *res = malloc(sizeof(node_t));
|
---|
239 | if (res != NULL) {
|
---|
240 | memset(res, 0, sizeof(node_t));
|
---|
241 | }
|
---|
242 |
|
---|
243 | list_initialize(&res->children);
|
---|
244 | list_initialize(&res->match_ids.ids);
|
---|
245 | fibril_mutex_initialize(&res->children_mutex);
|
---|
246 |
|
---|
247 | return res;
|
---|
248 | }
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Delete a device node.
|
---|
252 | *
|
---|
253 | * @param node a device node structure.
|
---|
254 | *
|
---|
255 | */
|
---|
256 | static inline void delete_dev_node(node_t *node)
|
---|
257 | {
|
---|
258 | assert(list_empty(&node->children) && NULL == node->parent && NULL == node->drv);
|
---|
259 |
|
---|
260 | clean_match_ids(&node->match_ids);
|
---|
261 | free_not_null(node->name);
|
---|
262 | free_not_null(node->pathname);
|
---|
263 | free(node);
|
---|
264 | }
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Find the device node structure of the device witch has the specified handle.
|
---|
268 | *
|
---|
269 | * @param tree the device tree where we look for the device node.
|
---|
270 | * @param handle the handle of the device.
|
---|
271 | * @return the device node.
|
---|
272 | */
|
---|
273 | static inline node_t * find_dev_node(dev_tree_t *tree, long handle)
|
---|
274 | {
|
---|
275 | if (handle < MAX_DEV) {
|
---|
276 | return tree->node_map[handle];
|
---|
277 | }
|
---|
278 | return NULL;
|
---|
279 | }
|
---|
280 |
|
---|
281 | node_t * find_dev_node_by_path(dev_tree_t *tree, char *path);
|
---|
282 | node_t *find_node_child(node_t *parent, const char *name);
|
---|
283 |
|
---|
284 | // Device tree
|
---|
285 |
|
---|
286 | bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list);
|
---|
287 | bool create_root_node(dev_tree_t *tree);
|
---|
288 | bool insert_dev_node(dev_tree_t *tree, node_t *node, char *dev_name, node_t *parent);
|
---|
289 |
|
---|
290 | #endif
|
---|
291 |
|
---|
292 | /** @}
|
---|
293 | */ |
---|