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 | /** Representation of a node in the device tree.*/
|
---|
96 | struct node {
|
---|
97 | /** The global unique identifier of the device.*/
|
---|
98 | device_handle_t handle;
|
---|
99 | /** The name of the device specified by its parent. */
|
---|
100 | char *name;
|
---|
101 | /** Full path and name of the device in device hierarchi (i. e. in full path in device tree).*/
|
---|
102 | char *pathname;
|
---|
103 | /** The node of the parent device. */
|
---|
104 | node_t *parent;
|
---|
105 | /** Pointers to previous and next child devices in the linked list of parent device's node.*/
|
---|
106 | link_t sibling;
|
---|
107 | /** List of child device nodes. */
|
---|
108 | link_t children;
|
---|
109 | /** Fibril mutex for the list of child device nodes of this node. */
|
---|
110 | fibril_mutex_t children_mutex;
|
---|
111 | /** List of device ids for device-to-driver matching.*/
|
---|
112 | match_id_list_t match_ids;
|
---|
113 | /** Driver of this device.*/
|
---|
114 | driver_t *drv;
|
---|
115 | /** Pointer to the previous and next device in the list of devices
|
---|
116 | owned by one driver */
|
---|
117 | link_t driver_devices;
|
---|
118 | };
|
---|
119 |
|
---|
120 | /** Represents device tree.
|
---|
121 | */
|
---|
122 | typedef struct dev_tree {
|
---|
123 | /** Root device node. */
|
---|
124 | node_t *root_node;
|
---|
125 | /** The next available handle - handles are assigned in a sequential manner.*/
|
---|
126 | atomic_t current_handle;
|
---|
127 | /** Handle-to-node mapping. */
|
---|
128 | node_t * node_map[MAX_DEV];
|
---|
129 | } dev_tree_t;
|
---|
130 |
|
---|
131 |
|
---|
132 |
|
---|
133 | // Match ids and scores
|
---|
134 |
|
---|
135 | int get_match_score(driver_t *drv, node_t *dev);
|
---|
136 |
|
---|
137 | bool parse_match_ids(const char *buf, match_id_list_t *ids);
|
---|
138 | bool read_match_ids(const char *conf_path, match_id_list_t *ids);
|
---|
139 | char * read_id(const char **buf) ;
|
---|
140 |
|
---|
141 | // Drivers
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Initialize the list of device driver's.
|
---|
145 | *
|
---|
146 | * @param drv_list the list of device driver's.
|
---|
147 | *
|
---|
148 | */
|
---|
149 | static inline void init_driver_list(driver_list_t *drv_list)
|
---|
150 | {
|
---|
151 | assert(NULL != drv_list);
|
---|
152 |
|
---|
153 | list_initialize(&drv_list->drivers);
|
---|
154 | fibril_mutex_initialize(&drv_list->drivers_mutex);
|
---|
155 | }
|
---|
156 |
|
---|
157 | driver_t * create_driver();
|
---|
158 | bool get_driver_info(const char *base_path, const char *name, driver_t *drv);
|
---|
159 | int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path);
|
---|
160 |
|
---|
161 | driver_t * find_best_match_driver(driver_list_t *drivers_list, node_t *node);
|
---|
162 | bool assign_driver(node_t *node, driver_list_t *drivers_list);
|
---|
163 |
|
---|
164 | void add_driver(driver_list_t *drivers_list, driver_t *drv);
|
---|
165 | void attach_driver(node_t *node, driver_t *drv);
|
---|
166 | void add_device(int phone, driver_t *drv, node_t *node);
|
---|
167 | bool start_driver(driver_t *drv);
|
---|
168 |
|
---|
169 | driver_t * find_driver(driver_list_t *drv_list, const char *drv_name);
|
---|
170 | void set_driver_phone(driver_t *driver, ipcarg_t phone);
|
---|
171 | void initialize_running_driver(driver_t *driver);
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Initialize device driver structure.
|
---|
175 | *
|
---|
176 | * @param drv the device driver structure.
|
---|
177 | *
|
---|
178 | */
|
---|
179 | static inline void init_driver(driver_t *drv)
|
---|
180 | {
|
---|
181 | assert(drv != NULL);
|
---|
182 |
|
---|
183 | memset(drv, 0, sizeof(driver_t));
|
---|
184 | list_initialize(&drv->match_ids.ids);
|
---|
185 | list_initialize(&drv->devices);
|
---|
186 | fibril_mutex_initialize(&drv->driver_mutex);
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Device driver structure clean-up.
|
---|
191 | *
|
---|
192 | * @param drv the device driver structure.
|
---|
193 | */
|
---|
194 | static inline void clean_driver(driver_t *drv)
|
---|
195 | {
|
---|
196 | assert(drv != NULL);
|
---|
197 |
|
---|
198 | free_not_null(drv->name);
|
---|
199 | free_not_null(drv->binary_path);
|
---|
200 |
|
---|
201 | clean_match_ids(&drv->match_ids);
|
---|
202 |
|
---|
203 | init_driver(drv);
|
---|
204 | }
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Delete device driver structure.
|
---|
208 | *
|
---|
209 | * @param drv the device driver structure.*
|
---|
210 | */
|
---|
211 | static inline void delete_driver(driver_t *drv)
|
---|
212 | {
|
---|
213 | assert(NULL != drv);
|
---|
214 |
|
---|
215 | clean_driver(drv);
|
---|
216 | free(drv);
|
---|
217 | }
|
---|
218 |
|
---|
219 | // Device nodes
|
---|
220 | /**
|
---|
221 | * Create a new device node.
|
---|
222 | *
|
---|
223 | * @return a device node structure.
|
---|
224 | *
|
---|
225 | */
|
---|
226 | static inline node_t * create_dev_node()
|
---|
227 | {
|
---|
228 | node_t *res = malloc(sizeof(node_t));
|
---|
229 | if (res != NULL) {
|
---|
230 | memset(res, 0, sizeof(node_t));
|
---|
231 | }
|
---|
232 |
|
---|
233 | list_initialize(&res->children);
|
---|
234 | list_initialize(&res->match_ids.ids);
|
---|
235 | fibril_mutex_initialize(&res->children_mutex);
|
---|
236 |
|
---|
237 | return res;
|
---|
238 | }
|
---|
239 |
|
---|
240 | static inline void delete_dev_node(node_t *node)
|
---|
241 | {
|
---|
242 | assert(list_empty(&node->children) && NULL == node->parent && NULL == node->drv);
|
---|
243 |
|
---|
244 | clean_match_ids(&node->match_ids);
|
---|
245 | free_not_null(node->name);
|
---|
246 | free_not_null(node->pathname);
|
---|
247 | free(node);
|
---|
248 | }
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Find the device node structure of the device witch has the specified handle.
|
---|
252 | *
|
---|
253 | * @param tree the device tree where we look for the device node.
|
---|
254 | * @param handle the handle of the device.
|
---|
255 | * @return the device node.
|
---|
256 | */
|
---|
257 | static inline node_t * find_dev_node(dev_tree_t *tree, long handle)
|
---|
258 | {
|
---|
259 | if (handle < MAX_DEV) {
|
---|
260 | return tree->node_map[handle];
|
---|
261 | }
|
---|
262 | return NULL;
|
---|
263 | }
|
---|
264 |
|
---|
265 | // Device tree
|
---|
266 |
|
---|
267 | bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list);
|
---|
268 | bool create_root_node(dev_tree_t *tree);
|
---|
269 | bool insert_dev_node(dev_tree_t *tree, node_t *node, const char *dev_name, node_t *parent);
|
---|
270 |
|
---|
271 | #endif
|
---|
272 |
|
---|
273 | /** @}
|
---|
274 | */ |
---|