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 <fibril_synch.h>
|
---|
43 | #include <atomic.h>
|
---|
44 |
|
---|
45 | #include "util.h"
|
---|
46 |
|
---|
47 | #define NAME "devman"
|
---|
48 |
|
---|
49 | #define MATCH_EXT ".ma"
|
---|
50 |
|
---|
51 | struct node;
|
---|
52 |
|
---|
53 | typedef struct node node_t;
|
---|
54 |
|
---|
55 | /** Ids of device models used for device-to-driver matching.
|
---|
56 | */
|
---|
57 | typedef struct match_id {
|
---|
58 | /** Pointers to next and previous ids.
|
---|
59 | */
|
---|
60 | link_t link;
|
---|
61 | /** Id of device model.
|
---|
62 | */
|
---|
63 | const char *id;
|
---|
64 | /** Relevancy of device-to-driver match.
|
---|
65 | * The higher is the product of scores specified for the device by the bus driver and by the leaf driver,
|
---|
66 | * the more suitable is the leaf driver for handling the device.
|
---|
67 | */
|
---|
68 | unsigned int score;
|
---|
69 | } match_id_t;
|
---|
70 |
|
---|
71 | /** List of ids for matching devices to drivers sorted
|
---|
72 | * according to match scores in descending order.
|
---|
73 | */
|
---|
74 | typedef struct match_id_list {
|
---|
75 | link_t ids;
|
---|
76 | } match_id_list_t;
|
---|
77 |
|
---|
78 | typedef enum {
|
---|
79 | /** driver has not been started */
|
---|
80 | DRIVER_NOT_STARTED = 0,
|
---|
81 | /** driver has been started, but has not registered as running and ready to receive requests */
|
---|
82 | DRIVER_STARTING,
|
---|
83 | /** driver is running and prepared to serve incomming requests */
|
---|
84 | DRIVER_RUNNING
|
---|
85 | } driver_state_t;
|
---|
86 |
|
---|
87 | /** Representation of device driver.
|
---|
88 | */
|
---|
89 | typedef struct driver {
|
---|
90 | /** Pointers to previous and next drivers in a linked list */
|
---|
91 | link_t drivers;
|
---|
92 | /** Specifies whether the driver has been started and wheter is running and prepared to receive requests.*/
|
---|
93 | int state;
|
---|
94 | /** Phone asociated with this driver */
|
---|
95 | ipcarg_t phone;
|
---|
96 | /** Name of the device driver */
|
---|
97 | char *name;
|
---|
98 | /** Path to the driver's binary */
|
---|
99 | const char *binary_path;
|
---|
100 | /** List of device ids for device-to-driver matching.*/
|
---|
101 | match_id_list_t match_ids;
|
---|
102 | /** Pointer to the linked list of devices controlled by this driver */
|
---|
103 | link_t devices;
|
---|
104 | /** Fibril mutex for this driver - driver state, list of devices, phone.*/
|
---|
105 | fibril_mutex_t driver_mutex;
|
---|
106 | } driver_t;
|
---|
107 |
|
---|
108 | /** The list of drivers. */
|
---|
109 | typedef struct driver_list {
|
---|
110 | /** List of drivers */
|
---|
111 | link_t drivers;
|
---|
112 | /** Fibril mutex for list of drivers. */
|
---|
113 | fibril_mutex_t drivers_mutex;
|
---|
114 | } driver_list_t;
|
---|
115 |
|
---|
116 | /** Representation of a node in the device tree.*/
|
---|
117 | struct node {
|
---|
118 | /** The global unique identifier of the device.*/
|
---|
119 | long handle;
|
---|
120 | /** The node of the parent device. */
|
---|
121 | node_t *parent;
|
---|
122 | /** Pointers to previous and next child devices in the linked list of parent device's node.*/
|
---|
123 | link_t sibling;
|
---|
124 | /** List of child device nodes. */
|
---|
125 | link_t children;
|
---|
126 | /** Fibril mutex for the list of child device nodes of this node. */
|
---|
127 | fibril_mutex_t children_mutex;
|
---|
128 | /** List of device ids for device-to-driver matching.*/
|
---|
129 | match_id_list_t match_ids;
|
---|
130 | /** Driver of this device.*/
|
---|
131 | driver_t *drv;
|
---|
132 | /** Pointer to the previous and next device in the list of devices
|
---|
133 | owned by one driver */
|
---|
134 | link_t driver_devices;
|
---|
135 | };
|
---|
136 |
|
---|
137 | /** Represents device tree.
|
---|
138 | */
|
---|
139 | typedef struct dev_tree {
|
---|
140 | /** Root device node. */
|
---|
141 | node_t *root_node;
|
---|
142 | atomic_t current_handle;
|
---|
143 | } dev_tree_t;
|
---|
144 |
|
---|
145 |
|
---|
146 |
|
---|
147 | // Match ids and scores
|
---|
148 |
|
---|
149 | int get_match_score(driver_t *drv, node_t *dev);
|
---|
150 |
|
---|
151 | bool parse_match_ids(const char *buf, match_id_list_t *ids);
|
---|
152 | bool read_match_ids(const char *conf_path, match_id_list_t *ids);
|
---|
153 | char * read_id(const char **buf) ;
|
---|
154 | void add_match_id(match_id_list_t *ids, match_id_t *id);
|
---|
155 |
|
---|
156 | void clean_match_ids(match_id_list_t *ids);
|
---|
157 |
|
---|
158 |
|
---|
159 | static inline match_id_t * create_match_id()
|
---|
160 | {
|
---|
161 | match_id_t *id = malloc(sizeof(match_id_t));
|
---|
162 | memset(id, 0, sizeof(match_id_t));
|
---|
163 | return id;
|
---|
164 | }
|
---|
165 |
|
---|
166 | static inline void delete_match_id(match_id_t *id)
|
---|
167 | {
|
---|
168 | if (id) {
|
---|
169 | free_not_null(id->id);
|
---|
170 | free(id);
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 |
|
---|
176 |
|
---|
177 |
|
---|
178 | // Drivers
|
---|
179 |
|
---|
180 | static inline void init_driver_list(driver_list_t *drv_list)
|
---|
181 | {
|
---|
182 | assert(NULL != drv_list);
|
---|
183 |
|
---|
184 | list_initialize(&drv_list->drivers);
|
---|
185 | fibril_mutex_initialize(&drv_list->drivers_mutex);
|
---|
186 | }
|
---|
187 |
|
---|
188 | driver_t * create_driver();
|
---|
189 | bool get_driver_info(const char *base_path, const char *name, driver_t *drv);
|
---|
190 | int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path);
|
---|
191 |
|
---|
192 | driver_t * find_best_match_driver(driver_list_t *drivers_list, node_t *node);
|
---|
193 | bool assign_driver(node_t *node, driver_list_t *drivers_list);
|
---|
194 |
|
---|
195 | void add_driver(driver_list_t *drivers_list, driver_t *drv);
|
---|
196 | void attach_driver(node_t *node, driver_t *drv);
|
---|
197 | void add_device(int phone, driver_t *drv, node_t *node);
|
---|
198 | bool start_driver(driver_t *drv);
|
---|
199 |
|
---|
200 | driver_t * find_driver(driver_list_t *drv_list, const char *drv_name);
|
---|
201 | void set_driver_phone(driver_t *driver, ipcarg_t phone);
|
---|
202 | void initialize_running_driver(driver_t *driver);
|
---|
203 |
|
---|
204 |
|
---|
205 | static inline void init_driver(driver_t *drv)
|
---|
206 | {
|
---|
207 | assert(drv != NULL);
|
---|
208 |
|
---|
209 | memset(drv, 0, sizeof(driver_t));
|
---|
210 | list_initialize(&drv->match_ids.ids);
|
---|
211 | list_initialize(&drv->devices);
|
---|
212 | fibril_mutex_initialize(&drv->driver_mutex);
|
---|
213 | }
|
---|
214 |
|
---|
215 | static inline void clean_driver(driver_t *drv)
|
---|
216 | {
|
---|
217 | assert(drv != NULL);
|
---|
218 |
|
---|
219 | free_not_null(drv->name);
|
---|
220 | free_not_null(drv->binary_path);
|
---|
221 |
|
---|
222 | clean_match_ids(&drv->match_ids);
|
---|
223 |
|
---|
224 | init_driver(drv);
|
---|
225 | }
|
---|
226 |
|
---|
227 | static inline void delete_driver(driver_t *drv)
|
---|
228 | {
|
---|
229 | assert(NULL != drv);
|
---|
230 |
|
---|
231 | clean_driver(drv);
|
---|
232 | free(drv);
|
---|
233 | }
|
---|
234 |
|
---|
235 | // Device nodes
|
---|
236 |
|
---|
237 | static inline node_t * create_dev_node()
|
---|
238 | {
|
---|
239 | node_t *res = malloc(sizeof(node_t));
|
---|
240 | if (res != NULL) {
|
---|
241 | memset(res, 0, sizeof(node_t));
|
---|
242 | }
|
---|
243 |
|
---|
244 | list_initialize(&res->children);
|
---|
245 | list_initialize(&res->match_ids.ids);
|
---|
246 |
|
---|
247 | return res;
|
---|
248 | }
|
---|
249 |
|
---|
250 | static inline void insert_dev_node(dev_tree_t *tree, node_t *node, node_t *parent)
|
---|
251 | {
|
---|
252 | assert(NULL != node && NULL != tree);
|
---|
253 |
|
---|
254 | node->handle = atomic_postinc(&tree->current_handle);
|
---|
255 |
|
---|
256 | node->parent = parent;
|
---|
257 | if (NULL != parent) {
|
---|
258 | fibril_mutex_lock(&parent->children_mutex);
|
---|
259 | list_append(&node->sibling, &parent->children);
|
---|
260 | fibril_mutex_unlock(&parent->children_mutex);
|
---|
261 | }
|
---|
262 | }
|
---|
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 |
|
---|
270 |
|
---|
271 | #endif
|
---|
272 |
|
---|
273 | /** @}
|
---|
274 | */ |
---|