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 | #include <errno.h>
|
---|
34 | #include <fcntl.h>
|
---|
35 | #include <sys/stat.h>
|
---|
36 | #include <ipc/driver.h>
|
---|
37 | #include <ipc/devman.h>
|
---|
38 | #include <devmap.h>
|
---|
39 |
|
---|
40 | #include "devman.h"
|
---|
41 |
|
---|
42 | /* hash table operations */
|
---|
43 |
|
---|
44 | static hash_index_t devices_hash(unsigned long key[])
|
---|
45 | {
|
---|
46 | return key[0] % DEVICE_BUCKETS;
|
---|
47 | }
|
---|
48 |
|
---|
49 | static int
|
---|
50 | devman_devices_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
---|
51 | {
|
---|
52 | node_t *dev = hash_table_get_instance(item, node_t, devman_link);
|
---|
53 | return (dev->handle == (device_handle_t) key[0]);
|
---|
54 | }
|
---|
55 |
|
---|
56 | static int
|
---|
57 | devmap_devices_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
---|
58 | {
|
---|
59 | node_t *dev = hash_table_get_instance(item, node_t, devmap_link);
|
---|
60 | return (dev->devmap_handle == (dev_handle_t) key[0]);
|
---|
61 | }
|
---|
62 |
|
---|
63 | static void devices_remove_callback(link_t *item)
|
---|
64 | {
|
---|
65 | }
|
---|
66 |
|
---|
67 | static hash_table_operations_t devman_devices_ops = {
|
---|
68 | .hash = devices_hash,
|
---|
69 | .compare = devman_devices_compare,
|
---|
70 | .remove_callback = devices_remove_callback
|
---|
71 | };
|
---|
72 |
|
---|
73 | static hash_table_operations_t devmap_devices_ops = {
|
---|
74 | .hash = devices_hash,
|
---|
75 | .compare = devmap_devices_compare,
|
---|
76 | .remove_callback = devices_remove_callback
|
---|
77 | };
|
---|
78 |
|
---|
79 | /** Allocate and initialize a new driver structure.
|
---|
80 | *
|
---|
81 | * @return Driver structure.
|
---|
82 | */
|
---|
83 | driver_t *create_driver(void)
|
---|
84 | {
|
---|
85 | driver_t *res = malloc(sizeof(driver_t));
|
---|
86 | if (res != NULL)
|
---|
87 | init_driver(res);
|
---|
88 | return res;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /** Add a driver to the list of drivers.
|
---|
92 | *
|
---|
93 | * @param drivers_list the list of drivers.
|
---|
94 | * @param drv the driver's structure.
|
---|
95 | */
|
---|
96 | void add_driver(driver_list_t *drivers_list, driver_t *drv)
|
---|
97 | {
|
---|
98 | fibril_mutex_lock(&drivers_list->drivers_mutex);
|
---|
99 | list_prepend(&drv->drivers, &drivers_list->drivers);
|
---|
100 | fibril_mutex_unlock(&drivers_list->drivers_mutex);
|
---|
101 |
|
---|
102 | printf(NAME": the '%s' driver was added to the list of available "
|
---|
103 | "drivers.\n", drv->name);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /** Read match id at the specified position of a string and set the position in
|
---|
107 | * the string to the first character following the id.
|
---|
108 | *
|
---|
109 | * @param buf The position in the input string.
|
---|
110 | * @return The match id.
|
---|
111 | */
|
---|
112 | char *read_match_id(char **buf)
|
---|
113 | {
|
---|
114 | char *res = NULL;
|
---|
115 | size_t len = get_nonspace_len(*buf);
|
---|
116 |
|
---|
117 | if (len > 0) {
|
---|
118 | res = malloc(len + 1);
|
---|
119 | if (res != NULL) {
|
---|
120 | str_ncpy(res, len + 1, *buf, len);
|
---|
121 | *buf += len;
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | return res;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Read match ids and associated match scores from a string.
|
---|
130 | *
|
---|
131 | * Each match score in the string is followed by its match id.
|
---|
132 | * The match ids and match scores are separated by whitespaces.
|
---|
133 | * Neither match ids nor match scores can contain whitespaces.
|
---|
134 | *
|
---|
135 | * @param buf The string from which the match ids are read.
|
---|
136 | * @param ids The list of match ids into which the match ids and
|
---|
137 | * scores are added.
|
---|
138 | * @return True if at least one match id and associated match score
|
---|
139 | * was successfully read, false otherwise.
|
---|
140 | */
|
---|
141 | bool parse_match_ids(char *buf, match_id_list_t *ids)
|
---|
142 | {
|
---|
143 | int score = 0;
|
---|
144 | char *id = NULL;
|
---|
145 | int ids_read = 0;
|
---|
146 |
|
---|
147 | while (true) {
|
---|
148 | /* skip spaces */
|
---|
149 | if (!skip_spaces(&buf))
|
---|
150 | break;
|
---|
151 |
|
---|
152 | /* read score */
|
---|
153 | score = strtoul(buf, &buf, 10);
|
---|
154 |
|
---|
155 | /* skip spaces */
|
---|
156 | if (!skip_spaces(&buf))
|
---|
157 | break;
|
---|
158 |
|
---|
159 | /* read id */
|
---|
160 | id = read_match_id(&buf);
|
---|
161 | if (NULL == id)
|
---|
162 | break;
|
---|
163 |
|
---|
164 | /* create new match_id structure */
|
---|
165 | match_id_t *mid = create_match_id();
|
---|
166 | mid->id = id;
|
---|
167 | mid->score = score;
|
---|
168 |
|
---|
169 | /* add it to the list */
|
---|
170 | add_match_id(ids, mid);
|
---|
171 |
|
---|
172 | ids_read++;
|
---|
173 | }
|
---|
174 |
|
---|
175 | return ids_read > 0;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Read match ids and associated match scores from a file.
|
---|
180 | *
|
---|
181 | * Each match score in the file is followed by its match id.
|
---|
182 | * The match ids and match scores are separated by whitespaces.
|
---|
183 | * Neither match ids nor match scores can contain whitespaces.
|
---|
184 | *
|
---|
185 | * @param buf The path to the file from which the match ids are read.
|
---|
186 | * @param ids The list of match ids into which the match ids and
|
---|
187 | * scores are added.
|
---|
188 | * @return True if at least one match id and associated match score
|
---|
189 | * was successfully read, false otherwise.
|
---|
190 | */
|
---|
191 | bool read_match_ids(const char *conf_path, match_id_list_t *ids)
|
---|
192 | {
|
---|
193 | printf(NAME ": read_match_ids conf_path = %s.\n", conf_path);
|
---|
194 |
|
---|
195 | bool suc = false;
|
---|
196 | char *buf = NULL;
|
---|
197 | bool opened = false;
|
---|
198 | int fd;
|
---|
199 | size_t len = 0;
|
---|
200 |
|
---|
201 | fd = open(conf_path, O_RDONLY);
|
---|
202 | if (fd < 0) {
|
---|
203 | printf(NAME ": unable to open %s\n", conf_path);
|
---|
204 | goto cleanup;
|
---|
205 | }
|
---|
206 | opened = true;
|
---|
207 |
|
---|
208 | len = lseek(fd, 0, SEEK_END);
|
---|
209 | lseek(fd, 0, SEEK_SET);
|
---|
210 | if (len == 0) {
|
---|
211 | printf(NAME ": configuration file '%s' is empty.\n", conf_path);
|
---|
212 | goto cleanup;
|
---|
213 | }
|
---|
214 |
|
---|
215 | buf = malloc(len + 1);
|
---|
216 | if (buf == NULL) {
|
---|
217 | printf(NAME ": memory allocation failed when parsing file "
|
---|
218 | "'%s'.\n", conf_path);
|
---|
219 | goto cleanup;
|
---|
220 | }
|
---|
221 |
|
---|
222 | if (0 >= read(fd, buf, len)) {
|
---|
223 | printf(NAME ": unable to read file '%s'.\n", conf_path);
|
---|
224 | goto cleanup;
|
---|
225 | }
|
---|
226 | buf[len] = 0;
|
---|
227 |
|
---|
228 | suc = parse_match_ids(buf, ids);
|
---|
229 |
|
---|
230 | cleanup:
|
---|
231 | free(buf);
|
---|
232 |
|
---|
233 | if(opened)
|
---|
234 | close(fd);
|
---|
235 |
|
---|
236 | return suc;
|
---|
237 | }
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * Get information about a driver.
|
---|
241 | *
|
---|
242 | * Each driver has its own directory in the base directory.
|
---|
243 | * The name of the driver's directory is the same as the name of the driver.
|
---|
244 | * The driver's directory contains driver's binary (named as the driver without
|
---|
245 | * extension) and the configuration file with match ids for device-to-driver
|
---|
246 | * matching (named as the driver with a special extension).
|
---|
247 | *
|
---|
248 | * This function searches for the driver's directory and containing
|
---|
249 | * configuration files. If all the files needed are found, they are parsed and
|
---|
250 | * the information about the driver is stored in the driver's structure.
|
---|
251 | *
|
---|
252 | * @param base_path The base directory, in which we look for driver's
|
---|
253 | * subdirectory.
|
---|
254 | * @param name The name of the driver.
|
---|
255 | * @param drv The driver structure to fill information in.
|
---|
256 | *
|
---|
257 | * @return True on success, false otherwise.
|
---|
258 | */
|
---|
259 | bool get_driver_info(const char *base_path, const char *name, driver_t *drv)
|
---|
260 | {
|
---|
261 | printf(NAME ": get_driver_info base_path = %s, name = %s.\n",
|
---|
262 | base_path, name);
|
---|
263 |
|
---|
264 | assert(base_path != NULL && name != NULL && drv != NULL);
|
---|
265 |
|
---|
266 | bool suc = false;
|
---|
267 | char *match_path = NULL;
|
---|
268 | size_t name_size = 0;
|
---|
269 |
|
---|
270 | /* Read the list of match ids from the driver's configuration file. */
|
---|
271 | match_path = get_abs_path(base_path, name, MATCH_EXT);
|
---|
272 | if (NULL == match_path)
|
---|
273 | goto cleanup;
|
---|
274 |
|
---|
275 | if (!read_match_ids(match_path, &drv->match_ids))
|
---|
276 | goto cleanup;
|
---|
277 |
|
---|
278 | /* Allocate and fill driver's name. */
|
---|
279 | name_size = str_size(name) + 1;
|
---|
280 | drv->name = malloc(name_size);
|
---|
281 | if (!drv->name)
|
---|
282 | goto cleanup;
|
---|
283 | str_cpy(drv->name, name_size, name);
|
---|
284 |
|
---|
285 | /* Initialize path with driver's binary. */
|
---|
286 | drv->binary_path = get_abs_path(base_path, name, "");
|
---|
287 | if (NULL == drv->binary_path)
|
---|
288 | goto cleanup;
|
---|
289 |
|
---|
290 | /* Check whether the driver's binary exists. */
|
---|
291 | struct stat s;
|
---|
292 | if (stat(drv->binary_path, &s) == ENOENT) {
|
---|
293 | printf(NAME ": driver not found at path %s.", drv->binary_path);
|
---|
294 | goto cleanup;
|
---|
295 | }
|
---|
296 |
|
---|
297 | suc = true;
|
---|
298 |
|
---|
299 | cleanup:
|
---|
300 | if (!suc) {
|
---|
301 | free(drv->binary_path);
|
---|
302 | free(drv->name);
|
---|
303 | /* Set the driver structure to the default state. */
|
---|
304 | init_driver(drv);
|
---|
305 | }
|
---|
306 |
|
---|
307 | free(match_path);
|
---|
308 |
|
---|
309 | return suc;
|
---|
310 | }
|
---|
311 |
|
---|
312 | /** Lookup drivers in the directory.
|
---|
313 | *
|
---|
314 | * @param drivers_list The list of available drivers.
|
---|
315 | * @param dir_path The path to the directory where we search for drivers.
|
---|
316 | * @return Number of drivers which were found.
|
---|
317 | */
|
---|
318 | int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path)
|
---|
319 | {
|
---|
320 | printf(NAME ": lookup_available_drivers, dir = %s \n", dir_path);
|
---|
321 |
|
---|
322 | int drv_cnt = 0;
|
---|
323 | DIR *dir = NULL;
|
---|
324 | struct dirent *diren;
|
---|
325 |
|
---|
326 | dir = opendir(dir_path);
|
---|
327 |
|
---|
328 | if (dir != NULL) {
|
---|
329 | driver_t *drv = create_driver();
|
---|
330 | while ((diren = readdir(dir))) {
|
---|
331 | if (get_driver_info(dir_path, diren->d_name, drv)) {
|
---|
332 | add_driver(drivers_list, drv);
|
---|
333 | drv_cnt++;
|
---|
334 | drv = create_driver();
|
---|
335 | }
|
---|
336 | }
|
---|
337 | delete_driver(drv);
|
---|
338 | closedir(dir);
|
---|
339 | }
|
---|
340 |
|
---|
341 | return drv_cnt;
|
---|
342 | }
|
---|
343 |
|
---|
344 | /** Create root device node in the device tree.
|
---|
345 | *
|
---|
346 | * @param tree The device tree.
|
---|
347 | * @return True on success, false otherwise.
|
---|
348 | */
|
---|
349 | bool create_root_node(dev_tree_t *tree)
|
---|
350 | {
|
---|
351 | printf(NAME ": create_root_node\n");
|
---|
352 | node_t *node = create_dev_node();
|
---|
353 | if (node) {
|
---|
354 | insert_dev_node(tree, node, clone_string(""), NULL);
|
---|
355 | match_id_t *id = create_match_id();
|
---|
356 | id->id = clone_string("root");
|
---|
357 | id->score = 100;
|
---|
358 | add_match_id(&node->match_ids, id);
|
---|
359 | tree->root_node = node;
|
---|
360 | }
|
---|
361 | return node != NULL;
|
---|
362 | }
|
---|
363 |
|
---|
364 | /** Lookup the best matching driver for the specified device in the list of
|
---|
365 | * drivers.
|
---|
366 | *
|
---|
367 | * A match between a device and a driver is found if one of the driver's match
|
---|
368 | * ids match one of the device's match ids. The score of the match is the
|
---|
369 | * product of the driver's and device's score associated with the matching id.
|
---|
370 | * The best matching driver for a device is the driver with the highest score
|
---|
371 | * of the match between the device and the driver.
|
---|
372 | *
|
---|
373 | * @param drivers_list The list of drivers, where we look for the driver
|
---|
374 | * suitable for handling the device.
|
---|
375 | * @param node The device node structure of the device.
|
---|
376 | * @return The best matching driver or NULL if no matching driver
|
---|
377 | * is found.
|
---|
378 | */
|
---|
379 | driver_t *find_best_match_driver(driver_list_t *drivers_list, node_t *node)
|
---|
380 | {
|
---|
381 | driver_t *best_drv = NULL, *drv = NULL;
|
---|
382 | int best_score = 0, score = 0;
|
---|
383 |
|
---|
384 | fibril_mutex_lock(&drivers_list->drivers_mutex);
|
---|
385 |
|
---|
386 | link_t *link = drivers_list->drivers.next;
|
---|
387 | while (link != &drivers_list->drivers) {
|
---|
388 | drv = list_get_instance(link, driver_t, drivers);
|
---|
389 | score = get_match_score(drv, node);
|
---|
390 | if (score > best_score) {
|
---|
391 | best_score = score;
|
---|
392 | best_drv = drv;
|
---|
393 | }
|
---|
394 | link = link->next;
|
---|
395 | }
|
---|
396 |
|
---|
397 | fibril_mutex_unlock(&drivers_list->drivers_mutex);
|
---|
398 |
|
---|
399 | return best_drv;
|
---|
400 | }
|
---|
401 |
|
---|
402 | /** Assign a driver to a device.
|
---|
403 | *
|
---|
404 | * @param node The device's node in the device tree.
|
---|
405 | * @param drv The driver.
|
---|
406 | */
|
---|
407 | void attach_driver(node_t *node, driver_t *drv)
|
---|
408 | {
|
---|
409 | printf(NAME ": attach_driver %s to device %s\n",
|
---|
410 | drv->name, node->pathname);
|
---|
411 |
|
---|
412 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
413 |
|
---|
414 | node->drv = drv;
|
---|
415 | list_append(&node->driver_devices, &drv->devices);
|
---|
416 |
|
---|
417 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
418 | }
|
---|
419 |
|
---|
420 | /** Start a driver
|
---|
421 | *
|
---|
422 | * The driver's mutex is assumed to be locked.
|
---|
423 | *
|
---|
424 | * @param drv The driver's structure.
|
---|
425 | * @return True if the driver's task is successfully spawned, false
|
---|
426 | * otherwise.
|
---|
427 | */
|
---|
428 | bool start_driver(driver_t *drv)
|
---|
429 | {
|
---|
430 | printf(NAME ": start_driver '%s'\n", drv->name);
|
---|
431 |
|
---|
432 | const char *argv[2];
|
---|
433 |
|
---|
434 | argv[0] = drv->name;
|
---|
435 | argv[1] = NULL;
|
---|
436 |
|
---|
437 | int err;
|
---|
438 | if (!task_spawn(drv->binary_path, argv, &err)) {
|
---|
439 | printf(NAME ": error spawning %s, errno = %d\n",
|
---|
440 | drv->name, err);
|
---|
441 | return false;
|
---|
442 | }
|
---|
443 |
|
---|
444 | drv->state = DRIVER_STARTING;
|
---|
445 | return true;
|
---|
446 | }
|
---|
447 |
|
---|
448 | /** Find device driver in the list of device drivers.
|
---|
449 | *
|
---|
450 | * @param drv_list The list of device drivers.
|
---|
451 | * @param drv_name The name of the device driver which is searched.
|
---|
452 | * @return The device driver of the specified name, if it is in the
|
---|
453 | * list, NULL otherwise.
|
---|
454 | */
|
---|
455 | driver_t *find_driver(driver_list_t *drv_list, const char *drv_name)
|
---|
456 | {
|
---|
457 | driver_t *res = NULL;
|
---|
458 |
|
---|
459 | fibril_mutex_lock(&drv_list->drivers_mutex);
|
---|
460 |
|
---|
461 | driver_t *drv = NULL;
|
---|
462 | link_t *link = drv_list->drivers.next;
|
---|
463 | while (link != &drv_list->drivers) {
|
---|
464 | drv = list_get_instance(link, driver_t, drivers);
|
---|
465 | if (0 == str_cmp(drv->name, drv_name)) {
|
---|
466 | res = drv;
|
---|
467 | break;
|
---|
468 | }
|
---|
469 | link = link->next;
|
---|
470 | }
|
---|
471 |
|
---|
472 | fibril_mutex_unlock(&drv_list->drivers_mutex);
|
---|
473 |
|
---|
474 | return res;
|
---|
475 | }
|
---|
476 |
|
---|
477 | /** Remember the driver's phone.
|
---|
478 | *
|
---|
479 | * @param driver The driver.
|
---|
480 | * @param phone The phone to the driver.
|
---|
481 | */
|
---|
482 | void set_driver_phone(driver_t *driver, ipcarg_t phone)
|
---|
483 | {
|
---|
484 | fibril_mutex_lock(&driver->driver_mutex);
|
---|
485 | assert(DRIVER_STARTING == driver->state);
|
---|
486 | driver->phone = phone;
|
---|
487 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
488 | }
|
---|
489 |
|
---|
490 | /** Notify driver about the devices to which it was assigned.
|
---|
491 | *
|
---|
492 | * The driver's mutex must be locked.
|
---|
493 | *
|
---|
494 | * @param driver The driver to which the devices are passed.
|
---|
495 | */
|
---|
496 | static void pass_devices_to_driver(driver_t *driver, dev_tree_t *tree)
|
---|
497 | {
|
---|
498 | printf(NAME ": pass_devices_to_driver\n");
|
---|
499 | node_t *dev;
|
---|
500 | link_t *link;
|
---|
501 |
|
---|
502 | int phone = ipc_connect_me_to(driver->phone, DRIVER_DEVMAN, 0, 0);
|
---|
503 |
|
---|
504 | if (0 < phone) {
|
---|
505 |
|
---|
506 | link = driver->devices.next;
|
---|
507 | while (link != &driver->devices) {
|
---|
508 | dev = list_get_instance(link, node_t, driver_devices);
|
---|
509 | add_device(phone, driver, dev, tree);
|
---|
510 | link = link->next;
|
---|
511 | }
|
---|
512 |
|
---|
513 | ipc_hangup(phone);
|
---|
514 | }
|
---|
515 | }
|
---|
516 |
|
---|
517 | /** Finish the initialization of a driver after it has succesfully started
|
---|
518 | * and after it has registered itself by the device manager.
|
---|
519 | *
|
---|
520 | * Pass devices formerly matched to the driver to the driver and remember the
|
---|
521 | * driver is running and fully functional now.
|
---|
522 | *
|
---|
523 | * @param driver The driver which registered itself as running by the
|
---|
524 | * device manager.
|
---|
525 | */
|
---|
526 | void initialize_running_driver(driver_t *driver, dev_tree_t *tree)
|
---|
527 | {
|
---|
528 | printf(NAME ": initialize_running_driver\n");
|
---|
529 | fibril_mutex_lock(&driver->driver_mutex);
|
---|
530 |
|
---|
531 | /*
|
---|
532 | * Pass devices which have been already assigned to the driver to the
|
---|
533 | * driver.
|
---|
534 | */
|
---|
535 | pass_devices_to_driver(driver, tree);
|
---|
536 |
|
---|
537 | /* Change driver's state to running. */
|
---|
538 | driver->state = DRIVER_RUNNING;
|
---|
539 |
|
---|
540 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
541 | }
|
---|
542 |
|
---|
543 |
|
---|
544 | /** Create devmap path and name for the device. */
|
---|
545 | static void devmap_register_tree_device(node_t *node, dev_tree_t *tree)
|
---|
546 | {
|
---|
547 | char *devmap_pathname = NULL;
|
---|
548 | char *devmap_name = NULL;
|
---|
549 |
|
---|
550 | asprintf(&devmap_name, "%s", node->pathname);
|
---|
551 | if (NULL == devmap_name)
|
---|
552 | return;
|
---|
553 |
|
---|
554 | replace_char(devmap_name, '/', DEVMAP_SEPARATOR);
|
---|
555 |
|
---|
556 | asprintf(&devmap_pathname, "%s/%s", DEVMAP_DEVICE_NAMESPACE,
|
---|
557 | devmap_name);
|
---|
558 | if (NULL == devmap_pathname) {
|
---|
559 | free(devmap_name);
|
---|
560 | return;
|
---|
561 | }
|
---|
562 |
|
---|
563 | devmap_device_register(devmap_pathname, &node->devmap_handle);
|
---|
564 |
|
---|
565 | tree_add_devmap_device(tree, node);
|
---|
566 |
|
---|
567 | free(devmap_name);
|
---|
568 | free(devmap_pathname);
|
---|
569 | }
|
---|
570 |
|
---|
571 |
|
---|
572 | /** Pass a device to running driver.
|
---|
573 | *
|
---|
574 | * @param drv The driver's structure.
|
---|
575 | * @param node The device's node in the device tree.
|
---|
576 | */
|
---|
577 | void add_device(int phone, driver_t *drv, node_t *node, dev_tree_t *tree)
|
---|
578 | {
|
---|
579 | printf(NAME ": add_device\n");
|
---|
580 |
|
---|
581 | ipcarg_t rc;
|
---|
582 | ipc_call_t answer;
|
---|
583 |
|
---|
584 | /* Send the device to the driver. */
|
---|
585 | aid_t req = async_send_1(phone, DRIVER_ADD_DEVICE, node->handle,
|
---|
586 | &answer);
|
---|
587 |
|
---|
588 | /* Send the device's name to the driver. */
|
---|
589 | rc = async_data_write_start(phone, node->name,
|
---|
590 | str_size(node->name) + 1);
|
---|
591 | if (rc != EOK) {
|
---|
592 | /* TODO handle error */
|
---|
593 | }
|
---|
594 |
|
---|
595 | /* Wait for answer from the driver. */
|
---|
596 | async_wait_for(req, &rc);
|
---|
597 | switch(rc) {
|
---|
598 | case EOK:
|
---|
599 | node->state = DEVICE_USABLE;
|
---|
600 | devmap_register_tree_device(node, tree);
|
---|
601 | break;
|
---|
602 | case ENOENT:
|
---|
603 | node->state = DEVICE_NOT_PRESENT;
|
---|
604 | break;
|
---|
605 | default:
|
---|
606 | node->state = DEVICE_INVALID;
|
---|
607 | }
|
---|
608 |
|
---|
609 | return;
|
---|
610 | }
|
---|
611 |
|
---|
612 | /** Find suitable driver for a device and assign the driver to it.
|
---|
613 | *
|
---|
614 | * @param node The device node of the device in the device tree.
|
---|
615 | * @param drivers_list The list of available drivers.
|
---|
616 | * @return True if the suitable driver is found and
|
---|
617 | * successfully assigned to the device, false otherwise.
|
---|
618 | */
|
---|
619 | bool assign_driver(node_t *node, driver_list_t *drivers_list, dev_tree_t *tree)
|
---|
620 | {
|
---|
621 | /*
|
---|
622 | * Find the driver which is the most suitable for handling this device.
|
---|
623 | */
|
---|
624 | driver_t *drv = find_best_match_driver(drivers_list, node);
|
---|
625 | if (NULL == drv) {
|
---|
626 | printf(NAME ": no driver found for device '%s'.\n",
|
---|
627 | node->pathname);
|
---|
628 | return false;
|
---|
629 | }
|
---|
630 |
|
---|
631 | /* Attach the driver to the device. */
|
---|
632 | attach_driver(node, drv);
|
---|
633 |
|
---|
634 | if (DRIVER_NOT_STARTED == drv->state) {
|
---|
635 | /* Start the driver. */
|
---|
636 | start_driver(drv);
|
---|
637 | }
|
---|
638 |
|
---|
639 | if (DRIVER_RUNNING == drv->state) {
|
---|
640 | /* Notify the driver about the new device. */
|
---|
641 | int phone = ipc_connect_me_to(drv->phone, DRIVER_DEVMAN, 0, 0);
|
---|
642 | if (phone > 0) {
|
---|
643 | add_device(phone, drv, node, tree);
|
---|
644 | ipc_hangup(phone);
|
---|
645 | }
|
---|
646 | }
|
---|
647 |
|
---|
648 | return true;
|
---|
649 | }
|
---|
650 |
|
---|
651 | /** Initialize the device tree.
|
---|
652 | *
|
---|
653 | * Create root device node of the tree and assign driver to it.
|
---|
654 | *
|
---|
655 | * @param tree The device tree.
|
---|
656 | * @param drivers_list the list of available drivers.
|
---|
657 | * @return True on success, false otherwise.
|
---|
658 | */
|
---|
659 | bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list)
|
---|
660 | {
|
---|
661 | printf(NAME ": init_device_tree.\n");
|
---|
662 |
|
---|
663 | tree->current_handle = 0;
|
---|
664 |
|
---|
665 | hash_table_create(&tree->devman_devices, DEVICE_BUCKETS, 1,
|
---|
666 | &devman_devices_ops);
|
---|
667 | hash_table_create(&tree->devmap_devices, DEVICE_BUCKETS, 1,
|
---|
668 | &devmap_devices_ops);
|
---|
669 |
|
---|
670 | fibril_rwlock_initialize(&tree->rwlock);
|
---|
671 |
|
---|
672 | /* Create root node and add it to the device tree. */
|
---|
673 | if (!create_root_node(tree))
|
---|
674 | return false;
|
---|
675 |
|
---|
676 | /* Find suitable driver and start it. */
|
---|
677 | return assign_driver(tree->root_node, drivers_list, tree);
|
---|
678 | }
|
---|
679 |
|
---|
680 | /** Create and set device's full path in device tree.
|
---|
681 | *
|
---|
682 | * @param node The device's device node.
|
---|
683 | * @param parent The parent device node.
|
---|
684 | * @return True on success, false otherwise (insufficient
|
---|
685 | * resources etc.).
|
---|
686 | */
|
---|
687 | static bool set_dev_path(node_t *node, node_t *parent)
|
---|
688 | {
|
---|
689 | assert(NULL != node->name);
|
---|
690 |
|
---|
691 | size_t pathsize = (str_size(node->name) + 1);
|
---|
692 | if (NULL != parent)
|
---|
693 | pathsize += str_size(parent->pathname) + 1;
|
---|
694 |
|
---|
695 | node->pathname = (char *) malloc(pathsize);
|
---|
696 | if (NULL == node->pathname) {
|
---|
697 | printf(NAME ": failed to allocate device path.\n");
|
---|
698 | return false;
|
---|
699 | }
|
---|
700 |
|
---|
701 | if (NULL != parent) {
|
---|
702 | str_cpy(node->pathname, pathsize, parent->pathname);
|
---|
703 | str_append(node->pathname, pathsize, "/");
|
---|
704 | str_append(node->pathname, pathsize, node->name);
|
---|
705 | } else {
|
---|
706 | str_cpy(node->pathname, pathsize, node->name);
|
---|
707 | }
|
---|
708 |
|
---|
709 | return true;
|
---|
710 | }
|
---|
711 |
|
---|
712 | /** Insert new device into device tree.
|
---|
713 | *
|
---|
714 | * The device tree's rwlock should be already held exclusively when calling this
|
---|
715 | * function.
|
---|
716 | *
|
---|
717 | * @param tree The device tree.
|
---|
718 | * @param node The newly added device node.
|
---|
719 | * @param dev_name The name of the newly added device.
|
---|
720 | * @param parent The parent device node.
|
---|
721 | * @return True on success, false otherwise (insufficient resources
|
---|
722 | * etc.).
|
---|
723 | */
|
---|
724 | bool
|
---|
725 | insert_dev_node(dev_tree_t *tree, node_t *node, char *dev_name, node_t *parent)
|
---|
726 | {
|
---|
727 | assert(NULL != node && NULL != tree && NULL != dev_name);
|
---|
728 |
|
---|
729 | node->name = dev_name;
|
---|
730 | if (!set_dev_path(node, parent)) {
|
---|
731 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
732 | return false;
|
---|
733 | }
|
---|
734 |
|
---|
735 | /* Add the node to the handle-to-node map. */
|
---|
736 | node->handle = ++tree->current_handle;
|
---|
737 | unsigned long key = node->handle;
|
---|
738 | hash_table_insert(&tree->devman_devices, &key, &node->devman_link);
|
---|
739 |
|
---|
740 | /* Add the node to the list of its parent's children. */
|
---|
741 | node->parent = parent;
|
---|
742 | if (NULL != parent)
|
---|
743 | list_append(&node->sibling, &parent->children);
|
---|
744 |
|
---|
745 | return true;
|
---|
746 | }
|
---|
747 |
|
---|
748 | /** Find device node with a specified path in the device tree.
|
---|
749 | *
|
---|
750 | * @param path The path of the device node in the device tree.
|
---|
751 | * @param tree The device tree.
|
---|
752 | * @return The device node if it is present in the tree, NULL
|
---|
753 | * otherwise.
|
---|
754 | */
|
---|
755 | node_t *find_dev_node_by_path(dev_tree_t *tree, char *path)
|
---|
756 | {
|
---|
757 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
758 |
|
---|
759 | node_t *dev = tree->root_node;
|
---|
760 | /*
|
---|
761 | * Relative path to the device from its parent (but with '/' at the
|
---|
762 | * beginning)
|
---|
763 | */
|
---|
764 | char *rel_path = path;
|
---|
765 | char *next_path_elem = NULL;
|
---|
766 | bool cont = '/' == rel_path[0];
|
---|
767 |
|
---|
768 | while (cont && NULL != dev) {
|
---|
769 | next_path_elem = get_path_elem_end(rel_path + 1);
|
---|
770 | if ('/' == next_path_elem[0]) {
|
---|
771 | cont = true;
|
---|
772 | next_path_elem[0] = 0;
|
---|
773 | } else {
|
---|
774 | cont = false;
|
---|
775 | }
|
---|
776 |
|
---|
777 | dev = find_node_child(dev, rel_path + 1);
|
---|
778 |
|
---|
779 | if (cont) {
|
---|
780 | /* Restore the original path. */
|
---|
781 | next_path_elem[0] = '/';
|
---|
782 | }
|
---|
783 | rel_path = next_path_elem;
|
---|
784 | }
|
---|
785 |
|
---|
786 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
787 |
|
---|
788 | return dev;
|
---|
789 | }
|
---|
790 |
|
---|
791 | /** Find child device node with a specified name.
|
---|
792 | *
|
---|
793 | * Device tree rwlock should be held at least for reading.
|
---|
794 | *
|
---|
795 | * @param parent The parent device node.
|
---|
796 | * @param name The name of the child device node.
|
---|
797 | * @return The child device node.
|
---|
798 | */
|
---|
799 | node_t *find_node_child(node_t *parent, const char *name)
|
---|
800 | {
|
---|
801 | node_t *dev;
|
---|
802 | link_t *link;
|
---|
803 |
|
---|
804 | link = parent->children.next;
|
---|
805 |
|
---|
806 | while (link != &parent->children) {
|
---|
807 | dev = list_get_instance(link, node_t, sibling);
|
---|
808 |
|
---|
809 | if (0 == str_cmp(name, dev->name))
|
---|
810 | return dev;
|
---|
811 |
|
---|
812 | link = link->next;
|
---|
813 | }
|
---|
814 |
|
---|
815 | return NULL;
|
---|
816 | }
|
---|
817 |
|
---|
818 | /** Create unique device name within the class.
|
---|
819 | *
|
---|
820 | * @param cl The class.
|
---|
821 | * @param base_dev_name Contains the base name for the device if it was
|
---|
822 | * specified by the driver when it registered the device by
|
---|
823 | * the class; NULL if driver specified no base name.
|
---|
824 | * @return The unique name for the device within the class.
|
---|
825 | */
|
---|
826 | char *create_dev_name_for_class(dev_class_t *cl, const char *base_dev_name)
|
---|
827 | {
|
---|
828 | char *dev_name;
|
---|
829 | const char *base_name;
|
---|
830 |
|
---|
831 | if (NULL != base_dev_name)
|
---|
832 | base_name = base_dev_name;
|
---|
833 | else
|
---|
834 | base_name = cl->base_dev_name;
|
---|
835 |
|
---|
836 | size_t idx = get_new_class_dev_idx(cl);
|
---|
837 | asprintf(&dev_name, "%s%d", base_name, idx);
|
---|
838 |
|
---|
839 | return dev_name;
|
---|
840 | }
|
---|
841 |
|
---|
842 | /** Add the device to the class.
|
---|
843 | *
|
---|
844 | * The device may be added to multiple classes and a class may contain multiple
|
---|
845 | * devices. The class and the device are associated with each other by the
|
---|
846 | * dev_class_info_t structure.
|
---|
847 | *
|
---|
848 | * @param dev The device.
|
---|
849 | * @param class The class.
|
---|
850 | * @param base_dev_name The base name of the device within the class if
|
---|
851 | * specified by the driver, NULL otherwise.
|
---|
852 | * @return dev_class_info_t structure which associates the device
|
---|
853 | * with the class.
|
---|
854 | */
|
---|
855 | dev_class_info_t *
|
---|
856 | add_device_to_class(node_t *dev, dev_class_t *cl, const char *base_dev_name)
|
---|
857 | {
|
---|
858 | dev_class_info_t *info = create_dev_class_info();
|
---|
859 |
|
---|
860 | if (NULL != info) {
|
---|
861 | info->dev_class = cl;
|
---|
862 | info->dev = dev;
|
---|
863 |
|
---|
864 | /* Add the device to the class. */
|
---|
865 | fibril_mutex_lock(&cl->mutex);
|
---|
866 | list_append(&info->link, &cl->devices);
|
---|
867 | fibril_mutex_unlock(&cl->mutex);
|
---|
868 |
|
---|
869 | /* Add the class to the device. */
|
---|
870 | list_append(&info->dev_classes, &dev->classes);
|
---|
871 |
|
---|
872 | /* Create unique name for the device within the class. */
|
---|
873 | info->dev_name = create_dev_name_for_class(cl, base_dev_name);
|
---|
874 | }
|
---|
875 |
|
---|
876 | return info;
|
---|
877 | }
|
---|
878 |
|
---|
879 | dev_class_t *get_dev_class(class_list_t *class_list, char *class_name)
|
---|
880 | {
|
---|
881 | dev_class_t *cl;
|
---|
882 |
|
---|
883 | fibril_rwlock_write_lock(&class_list->rwlock);
|
---|
884 | cl = find_dev_class_no_lock(class_list, class_name);
|
---|
885 | if (NULL == cl) {
|
---|
886 | cl = create_dev_class();
|
---|
887 | if (NULL != cl) {
|
---|
888 | cl->name = class_name;
|
---|
889 | cl->base_dev_name = "";
|
---|
890 | add_dev_class_no_lock(class_list, cl);
|
---|
891 | }
|
---|
892 | }
|
---|
893 | fibril_rwlock_write_unlock(&class_list->rwlock);
|
---|
894 | return cl;
|
---|
895 | }
|
---|
896 |
|
---|
897 | dev_class_t *
|
---|
898 | find_dev_class_no_lock(class_list_t *class_list, const char *class_name)
|
---|
899 | {
|
---|
900 | dev_class_t *cl;
|
---|
901 | link_t *link = class_list->classes.next;
|
---|
902 |
|
---|
903 | while (link != &class_list->classes) {
|
---|
904 | cl = list_get_instance(link, dev_class_t, link);
|
---|
905 | if (0 == str_cmp(cl->name, class_name))
|
---|
906 | return cl;
|
---|
907 | }
|
---|
908 |
|
---|
909 | return NULL;
|
---|
910 | }
|
---|
911 |
|
---|
912 | void init_class_list(class_list_t *class_list)
|
---|
913 | {
|
---|
914 | list_initialize(&class_list->classes);
|
---|
915 | fibril_rwlock_initialize(&class_list->rwlock);
|
---|
916 | hash_table_create(&class_list->devmap_devices, DEVICE_BUCKETS, 1,
|
---|
917 | &devmap_devices_ops);
|
---|
918 | }
|
---|
919 |
|
---|
920 |
|
---|
921 | /* devmap devices */
|
---|
922 |
|
---|
923 | node_t *find_devmap_tree_device(dev_tree_t *tree, dev_handle_t devmap_handle)
|
---|
924 | {
|
---|
925 | node_t *dev = NULL;
|
---|
926 | link_t *link;
|
---|
927 | unsigned long key = (unsigned long) devmap_handle;
|
---|
928 |
|
---|
929 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
930 | link = hash_table_find(&tree->devmap_devices, &key);
|
---|
931 | if (NULL != link)
|
---|
932 | dev = hash_table_get_instance(link, node_t, devmap_link);
|
---|
933 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
934 |
|
---|
935 | return dev;
|
---|
936 | }
|
---|
937 |
|
---|
938 | node_t *
|
---|
939 | find_devmap_class_device(class_list_t *classes, dev_handle_t devmap_handle)
|
---|
940 | {
|
---|
941 | node_t *dev = NULL;
|
---|
942 | dev_class_info_t *cli;
|
---|
943 | link_t *link;
|
---|
944 | unsigned long key = (unsigned long)devmap_handle;
|
---|
945 |
|
---|
946 | fibril_rwlock_read_lock(&classes->rwlock);
|
---|
947 | link = hash_table_find(&classes->devmap_devices, &key);
|
---|
948 | if (NULL != link) {
|
---|
949 | cli = hash_table_get_instance(link, dev_class_info_t,
|
---|
950 | devmap_link);
|
---|
951 | dev = cli->dev;
|
---|
952 | }
|
---|
953 | fibril_rwlock_read_unlock(&classes->rwlock);
|
---|
954 |
|
---|
955 | return dev;
|
---|
956 | }
|
---|
957 |
|
---|
958 | /** @}
|
---|
959 | */ |
---|