1 | /*
|
---|
2 | * Copyright (c) 2010 Lenka Trochtova
|
---|
3 | * Copyright (c) 2011 Jiri Svoboda
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup devman
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 |
|
---|
34 | #ifndef DEVMAN_H_
|
---|
35 | #define DEVMAN_H_
|
---|
36 |
|
---|
37 | #include <assert.h>
|
---|
38 | #include <bool.h>
|
---|
39 | #include <dirent.h>
|
---|
40 | #include <str.h>
|
---|
41 | #include <adt/list.h>
|
---|
42 | #include <adt/hash_table.h>
|
---|
43 | #include <ipc/devman.h>
|
---|
44 | #include <ipc/loc.h>
|
---|
45 | #include <fibril_synch.h>
|
---|
46 | #include <atomic.h>
|
---|
47 | #include <async.h>
|
---|
48 |
|
---|
49 | #include "util.h"
|
---|
50 |
|
---|
51 | #define NAME "devman"
|
---|
52 |
|
---|
53 | #define MATCH_EXT ".ma"
|
---|
54 |
|
---|
55 | #define LOC_DEVICE_NAMESPACE "devices"
|
---|
56 | #define LOC_SEPARATOR '\\'
|
---|
57 |
|
---|
58 | struct dev_node;
|
---|
59 | typedef struct dev_node dev_node_t;
|
---|
60 |
|
---|
61 | struct fun_node;
|
---|
62 | typedef struct fun_node fun_node_t;
|
---|
63 |
|
---|
64 | typedef struct {
|
---|
65 | fibril_mutex_t mutex;
|
---|
66 | struct driver *driver;
|
---|
67 | } client_t;
|
---|
68 |
|
---|
69 | typedef enum {
|
---|
70 | /** Driver has not been started. */
|
---|
71 | DRIVER_NOT_STARTED = 0,
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Driver has been started, but has not registered as running and ready
|
---|
75 | * to receive requests.
|
---|
76 | */
|
---|
77 | DRIVER_STARTING,
|
---|
78 |
|
---|
79 | /** Driver is running and prepared to serve incomming requests. */
|
---|
80 | DRIVER_RUNNING
|
---|
81 | } driver_state_t;
|
---|
82 |
|
---|
83 | /** Representation of device driver. */
|
---|
84 | typedef struct driver {
|
---|
85 | /** Pointers to previous and next drivers in a linked list. */
|
---|
86 | link_t drivers;
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Specifies whether the driver has been started and wheter is running
|
---|
90 | * and prepared to receive requests.
|
---|
91 | */
|
---|
92 | int state;
|
---|
93 |
|
---|
94 | /** Session asociated with this driver. */
|
---|
95 | async_sess_t *sess;
|
---|
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 | /** List of devices controlled by this driver. */
|
---|
103 | list_t devices;
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Fibril mutex for this driver - driver state, list of devices, session.
|
---|
107 | */
|
---|
108 | fibril_mutex_t driver_mutex;
|
---|
109 | } driver_t;
|
---|
110 |
|
---|
111 | /** The list of drivers. */
|
---|
112 | typedef struct driver_list {
|
---|
113 | /** List of drivers */
|
---|
114 | list_t drivers;
|
---|
115 | /** Fibril mutex for list of drivers. */
|
---|
116 | fibril_mutex_t drivers_mutex;
|
---|
117 | } driver_list_t;
|
---|
118 |
|
---|
119 | /** Device state */
|
---|
120 | typedef enum {
|
---|
121 | DEVICE_NOT_INITIALIZED = 0,
|
---|
122 | DEVICE_USABLE,
|
---|
123 | DEVICE_NOT_PRESENT,
|
---|
124 | DEVICE_INVALID,
|
---|
125 | /** Device node has been removed from the tree */
|
---|
126 | DEVICE_REMOVED
|
---|
127 | } device_state_t;
|
---|
128 |
|
---|
129 | /** Device node in the device tree. */
|
---|
130 | struct dev_node {
|
---|
131 | /** Reference count */
|
---|
132 | atomic_t refcnt;
|
---|
133 |
|
---|
134 | /** The global unique identifier of the device. */
|
---|
135 | devman_handle_t handle;
|
---|
136 |
|
---|
137 | /** (Parent) function the device is attached to. */
|
---|
138 | fun_node_t *pfun;
|
---|
139 |
|
---|
140 | /** List of device functions. */
|
---|
141 | list_t functions;
|
---|
142 | /** Driver of this device. */
|
---|
143 | driver_t *drv;
|
---|
144 | /** The state of the device. */
|
---|
145 | device_state_t state;
|
---|
146 | /** Link to list of devices owned by driver (driver_t.devices) */
|
---|
147 | link_t driver_devices;
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Used by the hash table of devices indexed by devman device handles.
|
---|
151 | */
|
---|
152 | ht_link_t devman_dev;
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Whether this device was already passed to the driver.
|
---|
156 | */
|
---|
157 | bool passed_to_driver;
|
---|
158 | };
|
---|
159 |
|
---|
160 | /** Function state */
|
---|
161 | typedef enum {
|
---|
162 | FUN_INIT = 0,
|
---|
163 | FUN_OFF_LINE,
|
---|
164 | FUN_ON_LINE,
|
---|
165 | /** Function node has been removed from the tree */
|
---|
166 | FUN_REMOVED
|
---|
167 | } fun_state_t;
|
---|
168 |
|
---|
169 | /** Function node in the device tree. */
|
---|
170 | struct fun_node {
|
---|
171 | /** Reference count */
|
---|
172 | atomic_t refcnt;
|
---|
173 | /** State */
|
---|
174 | fun_state_t state;
|
---|
175 | /** Locked while performing reconfiguration operations */
|
---|
176 | fibril_mutex_t busy_lock;
|
---|
177 |
|
---|
178 | /** The global unique identifier of the function */
|
---|
179 | devman_handle_t handle;
|
---|
180 | /** Name of the function, assigned by the device driver */
|
---|
181 | char *name;
|
---|
182 | /** Function type */
|
---|
183 | fun_type_t ftype;
|
---|
184 |
|
---|
185 | /** Full path and name of the device in device hierarchy */
|
---|
186 | char *pathname;
|
---|
187 |
|
---|
188 | /** Device which this function belongs to */
|
---|
189 | dev_node_t *dev;
|
---|
190 |
|
---|
191 | /** Link to list of functions in the device (ddf_dev_t.functions) */
|
---|
192 | link_t dev_functions;
|
---|
193 |
|
---|
194 | /** Child device node (if any attached). */
|
---|
195 | dev_node_t *child;
|
---|
196 | /** List of device ids for device-to-driver matching. */
|
---|
197 | match_id_list_t match_ids;
|
---|
198 |
|
---|
199 | /** Service ID if the device function is registered with loc. */
|
---|
200 | service_id_t service_id;
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Used by the hash table of functions indexed by devman device handles.
|
---|
204 | */
|
---|
205 | ht_link_t devman_fun;
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Used by the hash table of functions indexed by service IDs.
|
---|
209 | */
|
---|
210 | ht_link_t loc_fun;
|
---|
211 | };
|
---|
212 |
|
---|
213 | /** Represents device tree. */
|
---|
214 | typedef struct dev_tree {
|
---|
215 | /** Root device node. */
|
---|
216 | fun_node_t *root_node;
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * The next available handle - handles are assigned in a sequential
|
---|
220 | * manner.
|
---|
221 | */
|
---|
222 | devman_handle_t current_handle;
|
---|
223 |
|
---|
224 | /** Synchronize access to the device tree. */
|
---|
225 | fibril_rwlock_t rwlock;
|
---|
226 |
|
---|
227 | /** Hash table of all devices indexed by devman handles. */
|
---|
228 | hash_table_t devman_devices;
|
---|
229 |
|
---|
230 | /** Hash table of all devices indexed by devman handles. */
|
---|
231 | hash_table_t devman_functions;
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Hash table of services registered with location service, indexed by
|
---|
235 | * service IDs.
|
---|
236 | */
|
---|
237 | hash_table_t loc_functions;
|
---|
238 | } dev_tree_t;
|
---|
239 |
|
---|
240 | /* Match ids and scores */
|
---|
241 |
|
---|
242 | extern int get_match_score(driver_t *, dev_node_t *);
|
---|
243 |
|
---|
244 | extern bool parse_match_ids(char *, match_id_list_t *);
|
---|
245 | extern bool read_match_ids(const char *, match_id_list_t *);
|
---|
246 | extern char *read_match_id(char **);
|
---|
247 | extern char *read_id(const char **);
|
---|
248 |
|
---|
249 | /* Drivers */
|
---|
250 |
|
---|
251 | extern void init_driver_list(driver_list_t *);
|
---|
252 | extern driver_t *create_driver(void);
|
---|
253 | extern bool get_driver_info(const char *, const char *, driver_t *);
|
---|
254 | extern int lookup_available_drivers(driver_list_t *, const char *);
|
---|
255 |
|
---|
256 | extern driver_t *find_best_match_driver(driver_list_t *, dev_node_t *);
|
---|
257 | extern bool assign_driver(dev_node_t *, driver_list_t *, dev_tree_t *);
|
---|
258 |
|
---|
259 | extern void add_driver(driver_list_t *, driver_t *);
|
---|
260 | extern void attach_driver(dev_tree_t *, dev_node_t *, driver_t *);
|
---|
261 | extern void detach_driver(dev_tree_t *, dev_node_t *);
|
---|
262 | extern void add_device(driver_t *, dev_node_t *, dev_tree_t *);
|
---|
263 | extern bool start_driver(driver_t *);
|
---|
264 | extern int driver_dev_remove(dev_tree_t *, dev_node_t *);
|
---|
265 | extern int driver_dev_gone(dev_tree_t *, dev_node_t *);
|
---|
266 | extern int driver_fun_online(dev_tree_t *, fun_node_t *);
|
---|
267 | extern int driver_fun_offline(dev_tree_t *, fun_node_t *);
|
---|
268 |
|
---|
269 | extern driver_t *find_driver(driver_list_t *, const char *);
|
---|
270 | extern void initialize_running_driver(driver_t *, dev_tree_t *);
|
---|
271 |
|
---|
272 | extern void init_driver(driver_t *);
|
---|
273 | extern void clean_driver(driver_t *);
|
---|
274 | extern void delete_driver(driver_t *);
|
---|
275 |
|
---|
276 | /* Device nodes */
|
---|
277 |
|
---|
278 | extern dev_node_t *create_dev_node(void);
|
---|
279 | extern void delete_dev_node(dev_node_t *node);
|
---|
280 | extern void dev_add_ref(dev_node_t *);
|
---|
281 | extern void dev_del_ref(dev_node_t *);
|
---|
282 |
|
---|
283 | extern dev_node_t *find_dev_node_no_lock(dev_tree_t *tree,
|
---|
284 | devman_handle_t handle);
|
---|
285 | extern dev_node_t *find_dev_node(dev_tree_t *tree, devman_handle_t handle);
|
---|
286 | extern dev_node_t *find_dev_function(dev_node_t *, const char *);
|
---|
287 | extern int dev_get_functions(dev_tree_t *tree, dev_node_t *, devman_handle_t *,
|
---|
288 | size_t, size_t *);
|
---|
289 |
|
---|
290 | extern fun_node_t *create_fun_node(void);
|
---|
291 | extern void delete_fun_node(fun_node_t *);
|
---|
292 | extern void fun_add_ref(fun_node_t *);
|
---|
293 | extern void fun_del_ref(fun_node_t *);
|
---|
294 | extern void fun_busy_lock(fun_node_t *);
|
---|
295 | extern void fun_busy_unlock(fun_node_t *);
|
---|
296 | extern fun_node_t *find_fun_node_no_lock(dev_tree_t *tree,
|
---|
297 | devman_handle_t handle);
|
---|
298 | extern fun_node_t *find_fun_node(dev_tree_t *tree, devman_handle_t handle);
|
---|
299 | extern fun_node_t *find_fun_node_by_path(dev_tree_t *, char *);
|
---|
300 | extern fun_node_t *find_fun_node_in_device(dev_tree_t *tree, dev_node_t *,
|
---|
301 | const char *);
|
---|
302 |
|
---|
303 | /* Device tree */
|
---|
304 |
|
---|
305 | extern bool init_device_tree(dev_tree_t *, driver_list_t *);
|
---|
306 | extern bool create_root_nodes(dev_tree_t *);
|
---|
307 | extern bool insert_dev_node(dev_tree_t *, dev_node_t *, fun_node_t *);
|
---|
308 | extern void remove_dev_node(dev_tree_t *, dev_node_t *);
|
---|
309 | extern bool insert_fun_node(dev_tree_t *, fun_node_t *, char *, dev_node_t *);
|
---|
310 | extern void remove_fun_node(dev_tree_t *, fun_node_t *);
|
---|
311 |
|
---|
312 | /* Loc services */
|
---|
313 |
|
---|
314 | extern void loc_register_tree_function(fun_node_t *, dev_tree_t *);
|
---|
315 |
|
---|
316 | extern fun_node_t *find_loc_tree_function(dev_tree_t *, service_id_t);
|
---|
317 |
|
---|
318 | extern void tree_add_loc_function(dev_tree_t *, fun_node_t *);
|
---|
319 |
|
---|
320 | #endif
|
---|
321 |
|
---|
322 | /** @}
|
---|
323 | */
|
---|