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 <str.h>
|
---|
40 | #include <adt/list.h>
|
---|
41 | #include <adt/hash_table.h>
|
---|
42 | #include <ipc/devman.h>
|
---|
43 | #include <ipc/loc.h>
|
---|
44 | #include <fibril_synch.h>
|
---|
45 | #include <atomic.h>
|
---|
46 | #include <async.h>
|
---|
47 |
|
---|
48 | #include "util.h"
|
---|
49 |
|
---|
50 | #define NAME "devman"
|
---|
51 |
|
---|
52 | #define MATCH_EXT ".ma"
|
---|
53 | #define DEVICE_BUCKETS 256
|
---|
54 |
|
---|
55 | #define LOC_CLASS_NAMESPACE "class"
|
---|
56 | #define LOC_DEVICE_NAMESPACE "devices"
|
---|
57 | #define LOC_SEPARATOR '\\'
|
---|
58 |
|
---|
59 | struct dev_node;
|
---|
60 | typedef struct dev_node dev_node_t;
|
---|
61 |
|
---|
62 | struct fun_node;
|
---|
63 | typedef struct fun_node fun_node_t;
|
---|
64 |
|
---|
65 | typedef enum {
|
---|
66 | /** Driver has not been started. */
|
---|
67 | DRIVER_NOT_STARTED = 0,
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Driver has been started, but has not registered as running and ready
|
---|
71 | * to receive requests.
|
---|
72 | */
|
---|
73 | DRIVER_STARTING,
|
---|
74 |
|
---|
75 | /** Driver is running and prepared to serve incomming requests. */
|
---|
76 | DRIVER_RUNNING
|
---|
77 | } driver_state_t;
|
---|
78 |
|
---|
79 | /** Representation of device driver. */
|
---|
80 | typedef struct driver {
|
---|
81 | /** Pointers to previous and next drivers in a linked list. */
|
---|
82 | link_t drivers;
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Specifies whether the driver has been started and wheter is running
|
---|
86 | * and prepared to receive requests.
|
---|
87 | */
|
---|
88 | int state;
|
---|
89 |
|
---|
90 | /** Session asociated with this driver. */
|
---|
91 | async_sess_t *sess;
|
---|
92 | /** Name of the device driver. */
|
---|
93 | char *name;
|
---|
94 | /** Path to the driver's binary. */
|
---|
95 | const char *binary_path;
|
---|
96 | /** List of device ids for device-to-driver matching. */
|
---|
97 | match_id_list_t match_ids;
|
---|
98 | /** List of devices controlled by this driver. */
|
---|
99 | list_t devices;
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Fibril mutex for this driver - driver state, list of devices, session.
|
---|
103 | */
|
---|
104 | fibril_mutex_t driver_mutex;
|
---|
105 | } driver_t;
|
---|
106 |
|
---|
107 | /** The list of drivers. */
|
---|
108 | typedef struct driver_list {
|
---|
109 | /** List of drivers */
|
---|
110 | list_t drivers;
|
---|
111 | /** Fibril mutex for list of drivers. */
|
---|
112 | fibril_mutex_t drivers_mutex;
|
---|
113 | } driver_list_t;
|
---|
114 |
|
---|
115 | /** The state of the device. */
|
---|
116 | typedef enum {
|
---|
117 | DEVICE_NOT_INITIALIZED = 0,
|
---|
118 | DEVICE_USABLE,
|
---|
119 | DEVICE_NOT_PRESENT,
|
---|
120 | DEVICE_INVALID
|
---|
121 | } device_state_t;
|
---|
122 |
|
---|
123 | /** Device node in the device tree. */
|
---|
124 | struct dev_node {
|
---|
125 | /** The global unique identifier of the device. */
|
---|
126 | devman_handle_t handle;
|
---|
127 |
|
---|
128 | /** (Parent) function the device is attached to. */
|
---|
129 | fun_node_t *pfun;
|
---|
130 |
|
---|
131 | /** List of device functions. */
|
---|
132 | list_t functions;
|
---|
133 | /** Driver of this device. */
|
---|
134 | driver_t *drv;
|
---|
135 | /** The state of the device. */
|
---|
136 | device_state_t state;
|
---|
137 | /** Link to list of devices owned by driver (driver_t.devices) */
|
---|
138 | link_t driver_devices;
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Used by the hash table of devices indexed by devman device handles.
|
---|
142 | */
|
---|
143 | link_t devman_dev;
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Whether this device was already passed to the driver.
|
---|
147 | */
|
---|
148 | bool passed_to_driver;
|
---|
149 | };
|
---|
150 |
|
---|
151 | /** Function node in the device tree. */
|
---|
152 | struct fun_node {
|
---|
153 | /** The global unique identifier of the function */
|
---|
154 | devman_handle_t handle;
|
---|
155 | /** Name of the function, assigned by the device driver */
|
---|
156 | char *name;
|
---|
157 |
|
---|
158 | /** Full path and name of the device in device hierarchy */
|
---|
159 | char *pathname;
|
---|
160 |
|
---|
161 | /** Device which this function belongs to */
|
---|
162 | dev_node_t *dev;
|
---|
163 |
|
---|
164 | /** Link to list of functions in the device (ddf_dev_t.functions) */
|
---|
165 | link_t dev_functions;
|
---|
166 |
|
---|
167 | /** Child device node (if any attached). */
|
---|
168 | dev_node_t *child;
|
---|
169 | /** List of device ids for device-to-driver matching. */
|
---|
170 | match_id_list_t match_ids;
|
---|
171 |
|
---|
172 | /** List of device classes to which this device function belongs. */
|
---|
173 | list_t classes;
|
---|
174 | /** Service ID if the device function is registered with loc. */
|
---|
175 | service_id_t service_id;
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Used by the hash table of functions indexed by devman device handles.
|
---|
179 | */
|
---|
180 | link_t devman_fun;
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Used by the hash table of functions indexed by service IDs.
|
---|
184 | */
|
---|
185 | link_t loc_fun;
|
---|
186 | };
|
---|
187 |
|
---|
188 |
|
---|
189 | /** Represents device tree. */
|
---|
190 | typedef struct dev_tree {
|
---|
191 | /** Root device node. */
|
---|
192 | fun_node_t *root_node;
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * The next available handle - handles are assigned in a sequential
|
---|
196 | * manner.
|
---|
197 | */
|
---|
198 | devman_handle_t current_handle;
|
---|
199 |
|
---|
200 | /** Synchronize access to the device tree. */
|
---|
201 | fibril_rwlock_t rwlock;
|
---|
202 |
|
---|
203 | /** Hash table of all devices indexed by devman handles. */
|
---|
204 | hash_table_t devman_devices;
|
---|
205 |
|
---|
206 | /** Hash table of all devices indexed by devman handles. */
|
---|
207 | hash_table_t devman_functions;
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Hash table of services registered with location service, indexed by
|
---|
211 | * service IDs.
|
---|
212 | */
|
---|
213 | hash_table_t loc_functions;
|
---|
214 | } dev_tree_t;
|
---|
215 |
|
---|
216 | typedef struct dev_class {
|
---|
217 | /** The name of the class. */
|
---|
218 | const char *name;
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Pointer to the previous and next class in the list of registered
|
---|
222 | * classes.
|
---|
223 | */
|
---|
224 | link_t link;
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * List of dev_class_info structures - one for each device registered by
|
---|
228 | * this class.
|
---|
229 | */
|
---|
230 | list_t devices;
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Default base name for the device within the class, might be overrided
|
---|
234 | * by the driver.
|
---|
235 | */
|
---|
236 | const char *base_dev_name;
|
---|
237 |
|
---|
238 | /** Unique numerical identifier of the newly added device. */
|
---|
239 | size_t curr_dev_idx;
|
---|
240 | /** Synchronize access to the list of devices in this class. */
|
---|
241 | fibril_mutex_t mutex;
|
---|
242 | } dev_class_t;
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Provides n-to-m mapping between function nodes and classes - each function
|
---|
246 | * can register in an arbitrary number of classes and each class can contain
|
---|
247 | * an arbitrary number of device functions.
|
---|
248 | */
|
---|
249 | typedef struct dev_class_info {
|
---|
250 | /** The class. */
|
---|
251 | dev_class_t *dev_class;
|
---|
252 | /** The device. */
|
---|
253 | fun_node_t *fun;
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Pointer to the previous and next class info in the list of devices
|
---|
257 | * registered by the class.
|
---|
258 | */
|
---|
259 | link_t link;
|
---|
260 |
|
---|
261 | /**
|
---|
262 | * Pointer to the previous and next class info in the list of classes
|
---|
263 | * by which the device is registered.
|
---|
264 | */
|
---|
265 | link_t dev_classes;
|
---|
266 |
|
---|
267 | /** The name of the device function within the class. */
|
---|
268 | char *dev_name;
|
---|
269 | /** Service ID in the class namespace. */
|
---|
270 | service_id_t service_id;
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * Link to hash table of services registered with location service using
|
---|
274 | * their class names.
|
---|
275 | */
|
---|
276 | link_t loc_link;
|
---|
277 | } dev_class_info_t;
|
---|
278 |
|
---|
279 | /** The list of device classes. */
|
---|
280 | typedef struct class_list {
|
---|
281 | /** List of classes. */
|
---|
282 | list_t classes;
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * Hash table of services registered with location service using their
|
---|
286 | * class name, indexed by service IDs.
|
---|
287 | */
|
---|
288 | hash_table_t loc_functions;
|
---|
289 |
|
---|
290 | /** Fibril mutex for list of classes. */
|
---|
291 | fibril_rwlock_t rwlock;
|
---|
292 | } class_list_t;
|
---|
293 |
|
---|
294 | /* Match ids and scores */
|
---|
295 |
|
---|
296 | extern int get_match_score(driver_t *, dev_node_t *);
|
---|
297 |
|
---|
298 | extern bool parse_match_ids(char *, match_id_list_t *);
|
---|
299 | extern bool read_match_ids(const char *, match_id_list_t *);
|
---|
300 | extern char *read_match_id(char **);
|
---|
301 | extern char *read_id(const char **);
|
---|
302 |
|
---|
303 | /* Drivers */
|
---|
304 |
|
---|
305 | extern void init_driver_list(driver_list_t *);
|
---|
306 | extern driver_t *create_driver(void);
|
---|
307 | extern bool get_driver_info(const char *, const char *, driver_t *);
|
---|
308 | extern int lookup_available_drivers(driver_list_t *, const char *);
|
---|
309 |
|
---|
310 | extern driver_t *find_best_match_driver(driver_list_t *, dev_node_t *);
|
---|
311 | extern bool assign_driver(dev_node_t *, driver_list_t *, dev_tree_t *);
|
---|
312 |
|
---|
313 | extern void add_driver(driver_list_t *, driver_t *);
|
---|
314 | extern void attach_driver(dev_node_t *, driver_t *);
|
---|
315 | extern void add_device(async_sess_t *, driver_t *, dev_node_t *, dev_tree_t *);
|
---|
316 | extern bool start_driver(driver_t *);
|
---|
317 |
|
---|
318 | extern driver_t *find_driver(driver_list_t *, const char *);
|
---|
319 | extern void initialize_running_driver(driver_t *, dev_tree_t *);
|
---|
320 |
|
---|
321 | extern void init_driver(driver_t *);
|
---|
322 | extern void clean_driver(driver_t *);
|
---|
323 | extern void delete_driver(driver_t *);
|
---|
324 |
|
---|
325 | /* Device nodes */
|
---|
326 |
|
---|
327 | extern dev_node_t *create_dev_node(void);
|
---|
328 | extern void delete_dev_node(dev_node_t *node);
|
---|
329 | extern dev_node_t *find_dev_node_no_lock(dev_tree_t *tree,
|
---|
330 | devman_handle_t handle);
|
---|
331 | extern dev_node_t *find_dev_node(dev_tree_t *tree, devman_handle_t handle);
|
---|
332 | extern dev_node_t *find_dev_function(dev_node_t *, const char *);
|
---|
333 |
|
---|
334 | extern fun_node_t *create_fun_node(void);
|
---|
335 | extern void delete_fun_node(fun_node_t *);
|
---|
336 | extern fun_node_t *find_fun_node_no_lock(dev_tree_t *tree,
|
---|
337 | devman_handle_t handle);
|
---|
338 | extern fun_node_t *find_fun_node(dev_tree_t *tree, devman_handle_t handle);
|
---|
339 | extern fun_node_t *find_fun_node_by_path(dev_tree_t *, char *);
|
---|
340 | extern fun_node_t *find_fun_node_in_device(dev_node_t *, const char *);
|
---|
341 | extern fun_node_t *find_fun_node_by_class(class_list_t *, const char *, const char *);
|
---|
342 |
|
---|
343 | /* Device tree */
|
---|
344 |
|
---|
345 | extern bool init_device_tree(dev_tree_t *, driver_list_t *);
|
---|
346 | extern bool create_root_nodes(dev_tree_t *);
|
---|
347 | extern bool insert_dev_node(dev_tree_t *, dev_node_t *, fun_node_t *);
|
---|
348 | extern bool insert_fun_node(dev_tree_t *, fun_node_t *, char *, dev_node_t *);
|
---|
349 |
|
---|
350 | /* Device classes */
|
---|
351 |
|
---|
352 | extern dev_class_t *create_dev_class(void);
|
---|
353 | extern dev_class_info_t *create_dev_class_info(void);
|
---|
354 | extern size_t get_new_class_dev_idx(dev_class_t *);
|
---|
355 | extern char *create_dev_name_for_class(dev_class_t *, const char *);
|
---|
356 | extern dev_class_info_t *add_function_to_class(fun_node_t *, dev_class_t *,
|
---|
357 | const char *);
|
---|
358 |
|
---|
359 | extern void init_class_list(class_list_t *);
|
---|
360 |
|
---|
361 | extern dev_class_t *get_dev_class(class_list_t *, char *);
|
---|
362 | extern dev_class_t *find_dev_class_no_lock(class_list_t *, const char *);
|
---|
363 | extern dev_class_info_t *find_dev_in_class(dev_class_t *, const char *);
|
---|
364 | extern void add_dev_class_no_lock(class_list_t *, dev_class_t *);
|
---|
365 |
|
---|
366 | /* Loc services */
|
---|
367 |
|
---|
368 | extern void loc_register_tree_function(fun_node_t *, dev_tree_t *);
|
---|
369 |
|
---|
370 | extern fun_node_t *find_loc_tree_function(dev_tree_t *, service_id_t);
|
---|
371 | extern fun_node_t *find_loc_class_function(class_list_t *, service_id_t);
|
---|
372 |
|
---|
373 | extern void class_add_loc_function(class_list_t *, dev_class_info_t *);
|
---|
374 | extern void tree_add_loc_function(dev_tree_t *, fun_node_t *);
|
---|
375 |
|
---|
376 | #endif
|
---|
377 |
|
---|
378 | /** @}
|
---|
379 | */
|
---|