source: mainline/uspace/srv/devman/devman.c@ feeac0d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since feeac0d was feeac0d, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Simplify use of list_foreach.

  • Property mode set to 100644
File size: 36.9 KB
Line 
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/** @file Device Manager
33 *
34 * Locking order:
35 * (1) driver_t.driver_mutex
36 * (2) dev_tree_t.rwlock
37 *
38 * Synchronization:
39 * - device_tree.rwlock protects:
40 * - tree root, complete tree topology
41 * - complete contents of device and function nodes
42 * - dev_node_t.refcnt, fun_node_t.refcnt prevent nodes from
43 * being deallocated
44 * - find_xxx() functions increase reference count of returned object
45 * - find_xxx_no_lock() do not increase reference count
46 *
47 * TODO
48 * - Track all steady and transient device/function states
49 * - Check states, wait for steady state on certain operations
50 */
51
52#include <errno.h>
53#include <fcntl.h>
54#include <sys/stat.h>
55#include <io/log.h>
56#include <ipc/driver.h>
57#include <ipc/devman.h>
58#include <loc.h>
59#include <str_error.h>
60#include <stdio.h>
61
62#include "devman.h"
63
64static fun_node_t *find_node_child(dev_tree_t *, fun_node_t *, const char *);
65
66/* hash table operations */
67
68static inline size_t handle_key_hash(void *key)
69{
70 devman_handle_t handle = *(devman_handle_t*)key;
71 return handle;
72}
73
74static size_t devman_devices_hash(const ht_link_t *item)
75{
76 dev_node_t *dev = hash_table_get_inst(item, dev_node_t, devman_dev);
77 return handle_key_hash(&dev->handle);
78}
79
80static size_t devman_functions_hash(const ht_link_t *item)
81{
82 fun_node_t *fun = hash_table_get_inst(item, fun_node_t, devman_fun);
83 return handle_key_hash(&fun->handle);
84}
85
86static bool devman_devices_key_equal(void *key, const ht_link_t *item)
87{
88 devman_handle_t handle = *(devman_handle_t*)key;
89 dev_node_t *dev = hash_table_get_inst(item, dev_node_t, devman_dev);
90 return dev->handle == handle;
91}
92
93static bool devman_functions_key_equal(void *key, const ht_link_t *item)
94{
95 devman_handle_t handle = *(devman_handle_t*)key;
96 fun_node_t *fun = hash_table_get_inst(item, fun_node_t, devman_fun);
97 return fun->handle == handle;
98}
99
100static inline size_t service_id_key_hash(void *key)
101{
102 service_id_t service_id = *(service_id_t*)key;
103 return service_id;
104}
105
106static size_t loc_functions_hash(const ht_link_t *item)
107{
108 fun_node_t *fun = hash_table_get_inst(item, fun_node_t, loc_fun);
109 return service_id_key_hash(&fun->service_id);
110}
111
112static bool loc_functions_key_equal(void *key, const ht_link_t *item)
113{
114 service_id_t service_id = *(service_id_t*)key;
115 fun_node_t *fun = hash_table_get_inst(item, fun_node_t, loc_fun);
116 return fun->service_id == service_id;
117}
118
119
120static hash_table_ops_t devman_devices_ops = {
121 .hash = devman_devices_hash,
122 .key_hash = handle_key_hash,
123 .key_equal = devman_devices_key_equal,
124 .equal = NULL,
125 .remove_callback = NULL
126};
127
128static hash_table_ops_t devman_functions_ops = {
129 .hash = devman_functions_hash,
130 .key_hash = handle_key_hash,
131 .key_equal = devman_functions_key_equal,
132 .equal = NULL,
133 .remove_callback = NULL
134};
135
136static hash_table_ops_t loc_devices_ops = {
137 .hash = loc_functions_hash,
138 .key_hash = service_id_key_hash,
139 .key_equal = loc_functions_key_equal,
140 .equal = NULL,
141 .remove_callback = NULL
142};
143
144/**
145 * Initialize the list of device driver's.
146 *
147 * @param drv_list the list of device driver's.
148 *
149 */
150void init_driver_list(driver_list_t *drv_list)
151{
152 assert(drv_list != NULL);
153
154 list_initialize(&drv_list->drivers);
155 fibril_mutex_initialize(&drv_list->drivers_mutex);
156}
157
158/** Allocate and initialize a new driver structure.
159 *
160 * @return Driver structure.
161 */
162driver_t *create_driver(void)
163{
164 driver_t *res = malloc(sizeof(driver_t));
165 if (res != NULL)
166 init_driver(res);
167 return res;
168}
169
170/** Add a driver to the list of drivers.
171 *
172 * @param drivers_list List of drivers.
173 * @param drv Driver structure.
174 */
175void add_driver(driver_list_t *drivers_list, driver_t *drv)
176{
177 fibril_mutex_lock(&drivers_list->drivers_mutex);
178 list_prepend(&drv->drivers, &drivers_list->drivers);
179 fibril_mutex_unlock(&drivers_list->drivers_mutex);
180
181 log_msg(LOG_DEFAULT, LVL_NOTE, "Driver `%s' was added to the list of available "
182 "drivers.", drv->name);
183}
184
185/** Read match id at the specified position of a string and set the position in
186 * the string to the first character following the id.
187 *
188 * @param buf The position in the input string.
189 * @return The match id.
190 */
191char *read_match_id(char **buf)
192{
193 char *res = NULL;
194 size_t len = get_nonspace_len(*buf);
195
196 if (len > 0) {
197 res = malloc(len + 1);
198 if (res != NULL) {
199 str_ncpy(res, len + 1, *buf, len);
200 *buf += len;
201 }
202 }
203
204 return res;
205}
206
207/**
208 * Read match ids and associated match scores from a string.
209 *
210 * Each match score in the string is followed by its match id.
211 * The match ids and match scores are separated by whitespaces.
212 * Neither match ids nor match scores can contain whitespaces.
213 *
214 * @param buf The string from which the match ids are read.
215 * @param ids The list of match ids into which the match ids and
216 * scores are added.
217 * @return True if at least one match id and associated match score
218 * was successfully read, false otherwise.
219 */
220bool parse_match_ids(char *buf, match_id_list_t *ids)
221{
222 int score = 0;
223 char *id = NULL;
224 int ids_read = 0;
225
226 while (true) {
227 /* skip spaces */
228 if (!skip_spaces(&buf))
229 break;
230
231 /* read score */
232 score = strtoul(buf, &buf, 10);
233
234 /* skip spaces */
235 if (!skip_spaces(&buf))
236 break;
237
238 /* read id */
239 id = read_match_id(&buf);
240 if (NULL == id)
241 break;
242
243 /* create new match_id structure */
244 match_id_t *mid = create_match_id();
245 mid->id = id;
246 mid->score = score;
247
248 /* add it to the list */
249 add_match_id(ids, mid);
250
251 ids_read++;
252 }
253
254 return ids_read > 0;
255}
256
257/**
258 * Read match ids and associated match scores from a file.
259 *
260 * Each match score in the file is followed by its match id.
261 * The match ids and match scores are separated by whitespaces.
262 * Neither match ids nor match scores can contain whitespaces.
263 *
264 * @param buf The path to the file from which the match ids are read.
265 * @param ids The list of match ids into which the match ids and
266 * scores are added.
267 * @return True if at least one match id and associated match score
268 * was successfully read, false otherwise.
269 */
270bool read_match_ids(const char *conf_path, match_id_list_t *ids)
271{
272 log_msg(LOG_DEFAULT, LVL_DEBUG, "read_match_ids(conf_path=\"%s\")", conf_path);
273
274 bool suc = false;
275 char *buf = NULL;
276 bool opened = false;
277 int fd;
278 size_t len = 0;
279
280 fd = open(conf_path, O_RDONLY);
281 if (fd < 0) {
282 log_msg(LOG_DEFAULT, LVL_ERROR, "Unable to open `%s' for reading: %s.",
283 conf_path, str_error(fd));
284 goto cleanup;
285 }
286 opened = true;
287
288 len = lseek(fd, 0, SEEK_END);
289 lseek(fd, 0, SEEK_SET);
290 if (len == 0) {
291 log_msg(LOG_DEFAULT, LVL_ERROR, "Configuration file '%s' is empty.",
292 conf_path);
293 goto cleanup;
294 }
295
296 buf = malloc(len + 1);
297 if (buf == NULL) {
298 log_msg(LOG_DEFAULT, LVL_ERROR, "Memory allocation failed when parsing file "
299 "'%s'.", conf_path);
300 goto cleanup;
301 }
302
303 ssize_t read_bytes = read_all(fd, buf, len);
304 if (read_bytes <= 0) {
305 log_msg(LOG_DEFAULT, LVL_ERROR, "Unable to read file '%s' (%zd).", conf_path,
306 read_bytes);
307 goto cleanup;
308 }
309 buf[read_bytes] = 0;
310
311 suc = parse_match_ids(buf, ids);
312
313cleanup:
314 free(buf);
315
316 if (opened)
317 close(fd);
318
319 return suc;
320}
321
322/**
323 * Get information about a driver.
324 *
325 * Each driver has its own directory in the base directory.
326 * The name of the driver's directory is the same as the name of the driver.
327 * The driver's directory contains driver's binary (named as the driver without
328 * extension) and the configuration file with match ids for device-to-driver
329 * matching (named as the driver with a special extension).
330 *
331 * This function searches for the driver's directory and containing
332 * configuration files. If all the files needed are found, they are parsed and
333 * the information about the driver is stored in the driver's structure.
334 *
335 * @param base_path The base directory, in which we look for driver's
336 * subdirectory.
337 * @param name The name of the driver.
338 * @param drv The driver structure to fill information in.
339 *
340 * @return True on success, false otherwise.
341 */
342bool get_driver_info(const char *base_path, const char *name, driver_t *drv)
343{
344 log_msg(LOG_DEFAULT, LVL_DEBUG, "get_driver_info(base_path=\"%s\", name=\"%s\")",
345 base_path, name);
346
347 assert(base_path != NULL && name != NULL && drv != NULL);
348
349 bool suc = false;
350 char *match_path = NULL;
351 size_t name_size = 0;
352
353 /* Read the list of match ids from the driver's configuration file. */
354 match_path = get_abs_path(base_path, name, MATCH_EXT);
355 if (match_path == NULL)
356 goto cleanup;
357
358 if (!read_match_ids(match_path, &drv->match_ids))
359 goto cleanup;
360
361 /* Allocate and fill driver's name. */
362 name_size = str_size(name) + 1;
363 drv->name = malloc(name_size);
364 if (drv->name == NULL)
365 goto cleanup;
366 str_cpy(drv->name, name_size, name);
367
368 /* Initialize path with driver's binary. */
369 drv->binary_path = get_abs_path(base_path, name, "");
370 if (drv->binary_path == NULL)
371 goto cleanup;
372
373 /* Check whether the driver's binary exists. */
374 struct stat s;
375 if (stat(drv->binary_path, &s) == ENOENT) { /* FIXME!! */
376 log_msg(LOG_DEFAULT, LVL_ERROR, "Driver not found at path `%s'.",
377 drv->binary_path);
378 goto cleanup;
379 }
380
381 suc = true;
382
383cleanup:
384 if (!suc) {
385 free(drv->binary_path);
386 free(drv->name);
387 /* Set the driver structure to the default state. */
388 init_driver(drv);
389 }
390
391 free(match_path);
392
393 return suc;
394}
395
396/** Lookup drivers in the directory.
397 *
398 * @param drivers_list The list of available drivers.
399 * @param dir_path The path to the directory where we search for drivers.
400 * @return Number of drivers which were found.
401 */
402int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path)
403{
404 log_msg(LOG_DEFAULT, LVL_DEBUG, "lookup_available_drivers(dir=\"%s\")", dir_path);
405
406 int drv_cnt = 0;
407 DIR *dir = NULL;
408 struct dirent *diren;
409
410 dir = opendir(dir_path);
411
412 if (dir != NULL) {
413 driver_t *drv = create_driver();
414 while ((diren = readdir(dir))) {
415 if (get_driver_info(dir_path, diren->d_name, drv)) {
416 add_driver(drivers_list, drv);
417 drv_cnt++;
418 drv = create_driver();
419 }
420 }
421 delete_driver(drv);
422 closedir(dir);
423 }
424
425 return drv_cnt;
426}
427
428/** Create root device and function node in the device tree.
429 *
430 * @param tree The device tree.
431 * @return True on success, false otherwise.
432 */
433bool create_root_nodes(dev_tree_t *tree)
434{
435 fun_node_t *fun;
436 dev_node_t *dev;
437
438 log_msg(LOG_DEFAULT, LVL_DEBUG, "create_root_nodes()");
439
440 fibril_rwlock_write_lock(&tree->rwlock);
441
442 /*
443 * Create root function. This is a pseudo function to which
444 * the root device node is attached. It allows us to match
445 * the root device driver in a standard manner, i.e. against
446 * the parent function.
447 */
448
449 fun = create_fun_node();
450 if (fun == NULL) {
451 fibril_rwlock_write_unlock(&tree->rwlock);
452 return false;
453 }
454
455 fun_add_ref(fun);
456 insert_fun_node(tree, fun, str_dup(""), NULL);
457
458 match_id_t *id = create_match_id();
459 id->id = str_dup("root");
460 id->score = 100;
461 add_match_id(&fun->match_ids, id);
462 tree->root_node = fun;
463
464 /*
465 * Create root device node.
466 */
467 dev = create_dev_node();
468 if (dev == NULL) {
469 fibril_rwlock_write_unlock(&tree->rwlock);
470 return false;
471 }
472
473 dev_add_ref(dev);
474 insert_dev_node(tree, dev, fun);
475
476 fibril_rwlock_write_unlock(&tree->rwlock);
477
478 return dev != NULL;
479}
480
481/** Lookup the best matching driver for the specified device in the list of
482 * drivers.
483 *
484 * A match between a device and a driver is found if one of the driver's match
485 * ids match one of the device's match ids. The score of the match is the
486 * product of the driver's and device's score associated with the matching id.
487 * The best matching driver for a device is the driver with the highest score
488 * of the match between the device and the driver.
489 *
490 * @param drivers_list The list of drivers, where we look for the driver
491 * suitable for handling the device.
492 * @param node The device node structure of the device.
493 * @return The best matching driver or NULL if no matching driver
494 * is found.
495 */
496driver_t *find_best_match_driver(driver_list_t *drivers_list, dev_node_t *node)
497{
498 driver_t *best_drv = NULL;
499 int best_score = 0, score = 0;
500
501 fibril_mutex_lock(&drivers_list->drivers_mutex);
502
503 list_foreach(drivers_list->drivers, drivers, driver_t, drv) {
504 score = get_match_score(drv, node);
505 if (score > best_score) {
506 best_score = score;
507 best_drv = drv;
508 }
509 }
510
511 fibril_mutex_unlock(&drivers_list->drivers_mutex);
512
513 return best_drv;
514}
515
516/** Assign a driver to a device.
517 *
518 * @param tree Device tree
519 * @param node The device's node in the device tree.
520 * @param drv The driver.
521 */
522void attach_driver(dev_tree_t *tree, dev_node_t *dev, driver_t *drv)
523{
524 log_msg(LOG_DEFAULT, LVL_DEBUG, "attach_driver(dev=\"%s\",drv=\"%s\")",
525 dev->pfun->pathname, drv->name);
526
527 fibril_mutex_lock(&drv->driver_mutex);
528 fibril_rwlock_write_lock(&tree->rwlock);
529
530 dev->drv = drv;
531 list_append(&dev->driver_devices, &drv->devices);
532
533 fibril_rwlock_write_unlock(&tree->rwlock);
534 fibril_mutex_unlock(&drv->driver_mutex);
535}
536
537/** Detach driver from device.
538 *
539 * @param tree Device tree
540 * @param node The device's node in the device tree.
541 * @param drv The driver.
542 */
543void detach_driver(dev_tree_t *tree, dev_node_t *dev)
544{
545 driver_t *drv = dev->drv;
546
547 assert(drv != NULL);
548
549 log_msg(LOG_DEFAULT, LVL_DEBUG, "detach_driver(dev=\"%s\",drv=\"%s\")",
550 dev->pfun->pathname, drv->name);
551
552 fibril_mutex_lock(&drv->driver_mutex);
553 fibril_rwlock_write_lock(&tree->rwlock);
554
555 dev->drv = NULL;
556 list_remove(&dev->driver_devices);
557
558 fibril_rwlock_write_unlock(&tree->rwlock);
559 fibril_mutex_unlock(&drv->driver_mutex);
560}
561
562/** Start a driver
563 *
564 * @param drv The driver's structure.
565 * @return True if the driver's task is successfully spawned, false
566 * otherwise.
567 */
568bool start_driver(driver_t *drv)
569{
570 int rc;
571
572 assert(fibril_mutex_is_locked(&drv->driver_mutex));
573
574 log_msg(LOG_DEFAULT, LVL_DEBUG, "start_driver(drv=\"%s\")", drv->name);
575
576 rc = task_spawnl(NULL, drv->binary_path, drv->binary_path, NULL);
577 if (rc != EOK) {
578 log_msg(LOG_DEFAULT, LVL_ERROR, "Spawning driver `%s' (%s) failed: %s.",
579 drv->name, drv->binary_path, str_error(rc));
580 return false;
581 }
582
583 drv->state = DRIVER_STARTING;
584 return true;
585}
586
587/** Find device driver in the list of device drivers.
588 *
589 * @param drv_list The list of device drivers.
590 * @param drv_name The name of the device driver which is searched.
591 * @return The device driver of the specified name, if it is in the
592 * list, NULL otherwise.
593 */
594driver_t *find_driver(driver_list_t *drv_list, const char *drv_name)
595{
596 driver_t *res = NULL;
597
598 fibril_mutex_lock(&drv_list->drivers_mutex);
599
600 list_foreach(drv_list->drivers, drivers, driver_t, drv) {
601 if (str_cmp(drv->name, drv_name) == 0) {
602 res = drv;
603 break;
604 }
605 }
606
607 fibril_mutex_unlock(&drv_list->drivers_mutex);
608
609 return res;
610}
611
612/** Notify driver about the devices to which it was assigned.
613 *
614 * @param driver The driver to which the devices are passed.
615 */
616static void pass_devices_to_driver(driver_t *driver, dev_tree_t *tree)
617{
618 dev_node_t *dev;
619 link_t *link;
620
621 log_msg(LOG_DEFAULT, LVL_DEBUG, "pass_devices_to_driver(driver=\"%s\")",
622 driver->name);
623
624 fibril_mutex_lock(&driver->driver_mutex);
625
626 /*
627 * Go through devices list as long as there is some device
628 * that has not been passed to the driver.
629 */
630 link = driver->devices.head.next;
631 while (link != &driver->devices.head) {
632 dev = list_get_instance(link, dev_node_t, driver_devices);
633 fibril_rwlock_write_lock(&tree->rwlock);
634
635 if (dev->passed_to_driver) {
636 fibril_rwlock_write_unlock(&tree->rwlock);
637 link = link->next;
638 continue;
639 }
640
641 log_msg(LOG_DEFAULT, LVL_DEBUG, "pass_devices_to_driver: dev->refcnt=%d\n",
642 (int)atomic_get(&dev->refcnt));
643 dev_add_ref(dev);
644
645 /*
646 * Unlock to avoid deadlock when adding device
647 * handled by itself.
648 */
649 fibril_mutex_unlock(&driver->driver_mutex);
650 fibril_rwlock_write_unlock(&tree->rwlock);
651
652 add_device(driver, dev, tree);
653
654 dev_del_ref(dev);
655
656 /*
657 * Lock again as we will work with driver's
658 * structure.
659 */
660 fibril_mutex_lock(&driver->driver_mutex);
661
662 /*
663 * Restart the cycle to go through all devices again.
664 */
665 link = driver->devices.head.next;
666 }
667
668 /*
669 * Once we passed all devices to the driver, we need to mark the
670 * driver as running.
671 * It is vital to do it here and inside critical section.
672 *
673 * If we would change the state earlier, other devices added to
674 * the driver would be added to the device list and started
675 * immediately and possibly started here as well.
676 */
677 log_msg(LOG_DEFAULT, LVL_DEBUG, "Driver `%s' enters running state.", driver->name);
678 driver->state = DRIVER_RUNNING;
679
680 fibril_mutex_unlock(&driver->driver_mutex);
681}
682
683/** Finish the initialization of a driver after it has succesfully started
684 * and after it has registered itself by the device manager.
685 *
686 * Pass devices formerly matched to the driver to the driver and remember the
687 * driver is running and fully functional now.
688 *
689 * @param driver The driver which registered itself as running by the
690 * device manager.
691 */
692void initialize_running_driver(driver_t *driver, dev_tree_t *tree)
693{
694 log_msg(LOG_DEFAULT, LVL_DEBUG, "initialize_running_driver(driver=\"%s\")",
695 driver->name);
696
697 /*
698 * Pass devices which have been already assigned to the driver to the
699 * driver.
700 */
701 pass_devices_to_driver(driver, tree);
702}
703
704/** Initialize device driver structure.
705 *
706 * @param drv The device driver structure.
707 */
708void init_driver(driver_t *drv)
709{
710 assert(drv != NULL);
711
712 memset(drv, 0, sizeof(driver_t));
713 list_initialize(&drv->match_ids.ids);
714 list_initialize(&drv->devices);
715 fibril_mutex_initialize(&drv->driver_mutex);
716 drv->sess = NULL;
717}
718
719/** Device driver structure clean-up.
720 *
721 * @param drv The device driver structure.
722 */
723void clean_driver(driver_t *drv)
724{
725 assert(drv != NULL);
726
727 free(drv->name);
728 free(drv->binary_path);
729
730 clean_match_ids(&drv->match_ids);
731
732 init_driver(drv);
733}
734
735/** Delete device driver structure.
736 *
737 * @param drv The device driver structure.
738 */
739void delete_driver(driver_t *drv)
740{
741 assert(drv != NULL);
742
743 clean_driver(drv);
744 free(drv);
745}
746
747/** Create loc path and name for the function. */
748void loc_register_tree_function(fun_node_t *fun, dev_tree_t *tree)
749{
750 char *loc_pathname = NULL;
751 char *loc_name = NULL;
752
753 assert(fibril_rwlock_is_locked(&tree->rwlock));
754
755 asprintf(&loc_name, "%s", fun->pathname);
756 if (loc_name == NULL)
757 return;
758
759 replace_char(loc_name, '/', LOC_SEPARATOR);
760
761 asprintf(&loc_pathname, "%s/%s", LOC_DEVICE_NAMESPACE,
762 loc_name);
763 if (loc_pathname == NULL) {
764 free(loc_name);
765 return;
766 }
767
768 loc_service_register_with_iface(loc_pathname,
769 &fun->service_id, DEVMAN_CONNECT_FROM_LOC);
770
771 tree_add_loc_function(tree, fun);
772
773 free(loc_name);
774 free(loc_pathname);
775}
776
777/** Pass a device to running driver.
778 *
779 * @param drv The driver's structure.
780 * @param node The device's node in the device tree.
781 */
782void add_device(driver_t *drv, dev_node_t *dev, dev_tree_t *tree)
783{
784 /*
785 * We do not expect to have driver's mutex locked as we do not
786 * access any structures that would affect driver_t.
787 */
788 log_msg(LOG_DEFAULT, LVL_DEBUG, "add_device(drv=\"%s\", dev=\"%s\")",
789 drv->name, dev->pfun->name);
790
791 /* Send the device to the driver. */
792 devman_handle_t parent_handle;
793 if (dev->pfun) {
794 parent_handle = dev->pfun->handle;
795 } else {
796 parent_handle = 0;
797 }
798
799 async_exch_t *exch = async_exchange_begin(drv->sess);
800
801 ipc_call_t answer;
802 aid_t req = async_send_2(exch, DRIVER_DEV_ADD, dev->handle,
803 parent_handle, &answer);
804
805 /* Send the device name to the driver. */
806 sysarg_t rc = async_data_write_start(exch, dev->pfun->name,
807 str_size(dev->pfun->name) + 1);
808
809 async_exchange_end(exch);
810
811 if (rc != EOK) {
812 /* TODO handle error */
813 }
814
815 /* Wait for answer from the driver. */
816 async_wait_for(req, &rc);
817
818 switch(rc) {
819 case EOK:
820 dev->state = DEVICE_USABLE;
821 break;
822 case ENOENT:
823 dev->state = DEVICE_NOT_PRESENT;
824 break;
825 default:
826 dev->state = DEVICE_INVALID;
827 break;
828 }
829
830 dev->passed_to_driver = true;
831
832 return;
833}
834
835/** Find suitable driver for a device and assign the driver to it.
836 *
837 * @param node The device node of the device in the device tree.
838 * @param drivers_list The list of available drivers.
839 * @return True if the suitable driver is found and
840 * successfully assigned to the device, false otherwise.
841 */
842bool assign_driver(dev_node_t *dev, driver_list_t *drivers_list,
843 dev_tree_t *tree)
844{
845 assert(dev != NULL);
846 assert(drivers_list != NULL);
847 assert(tree != NULL);
848
849 /*
850 * Find the driver which is the most suitable for handling this device.
851 */
852 driver_t *drv = find_best_match_driver(drivers_list, dev);
853 if (drv == NULL) {
854 log_msg(LOG_DEFAULT, LVL_ERROR, "No driver found for device `%s'.",
855 dev->pfun->pathname);
856 return false;
857 }
858
859 /* Attach the driver to the device. */
860 attach_driver(tree, dev, drv);
861
862 fibril_mutex_lock(&drv->driver_mutex);
863 if (drv->state == DRIVER_NOT_STARTED) {
864 /* Start the driver. */
865 start_driver(drv);
866 }
867 bool is_running = drv->state == DRIVER_RUNNING;
868 fibril_mutex_unlock(&drv->driver_mutex);
869
870 /* Notify the driver about the new device. */
871 if (is_running)
872 add_device(drv, dev, tree);
873
874 fibril_mutex_lock(&drv->driver_mutex);
875 fibril_mutex_unlock(&drv->driver_mutex);
876
877 fibril_rwlock_write_lock(&tree->rwlock);
878 if (dev->pfun != NULL) {
879 dev->pfun->state = FUN_ON_LINE;
880 }
881 fibril_rwlock_write_unlock(&tree->rwlock);
882 return true;
883}
884
885int driver_dev_remove(dev_tree_t *tree, dev_node_t *dev)
886{
887 async_exch_t *exch;
888 sysarg_t retval;
889 driver_t *drv;
890 devman_handle_t handle;
891
892 assert(dev != NULL);
893
894 log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_remove(%p)", dev);
895
896 fibril_rwlock_read_lock(&tree->rwlock);
897 drv = dev->drv;
898 handle = dev->handle;
899 fibril_rwlock_read_unlock(&tree->rwlock);
900
901 exch = async_exchange_begin(drv->sess);
902 retval = async_req_1_0(exch, DRIVER_DEV_REMOVE, handle);
903 async_exchange_end(exch);
904
905 return retval;
906}
907
908int driver_dev_gone(dev_tree_t *tree, dev_node_t *dev)
909{
910 async_exch_t *exch;
911 sysarg_t retval;
912 driver_t *drv;
913 devman_handle_t handle;
914
915 assert(dev != NULL);
916
917 log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_gone(%p)", dev);
918
919 fibril_rwlock_read_lock(&tree->rwlock);
920 drv = dev->drv;
921 handle = dev->handle;
922 fibril_rwlock_read_unlock(&tree->rwlock);
923
924 exch = async_exchange_begin(drv->sess);
925 retval = async_req_1_0(exch, DRIVER_DEV_GONE, handle);
926 async_exchange_end(exch);
927
928 return retval;
929}
930
931int driver_fun_online(dev_tree_t *tree, fun_node_t *fun)
932{
933 async_exch_t *exch;
934 sysarg_t retval;
935 driver_t *drv;
936 devman_handle_t handle;
937
938 log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_fun_online(%p)", fun);
939
940 fibril_rwlock_read_lock(&tree->rwlock);
941
942 if (fun->dev == NULL) {
943 /* XXX root function? */
944 fibril_rwlock_read_unlock(&tree->rwlock);
945 return EINVAL;
946 }
947
948 drv = fun->dev->drv;
949 handle = fun->handle;
950 fibril_rwlock_read_unlock(&tree->rwlock);
951
952 exch = async_exchange_begin(drv->sess);
953 retval = async_req_1_0(exch, DRIVER_FUN_ONLINE, handle);
954 loc_exchange_end(exch);
955
956 return retval;
957}
958
959int driver_fun_offline(dev_tree_t *tree, fun_node_t *fun)
960{
961 async_exch_t *exch;
962 sysarg_t retval;
963 driver_t *drv;
964 devman_handle_t handle;
965
966 log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_fun_offline(%p)", fun);
967
968 fibril_rwlock_read_lock(&tree->rwlock);
969 if (fun->dev == NULL) {
970 /* XXX root function? */
971 fibril_rwlock_read_unlock(&tree->rwlock);
972 return EINVAL;
973 }
974
975 drv = fun->dev->drv;
976 handle = fun->handle;
977 fibril_rwlock_read_unlock(&tree->rwlock);
978
979 exch = async_exchange_begin(drv->sess);
980 retval = async_req_1_0(exch, DRIVER_FUN_OFFLINE, handle);
981 loc_exchange_end(exch);
982
983 return retval;
984
985}
986
987/** Initialize the device tree.
988 *
989 * Create root device node of the tree and assign driver to it.
990 *
991 * @param tree The device tree.
992 * @param drivers_list the list of available drivers.
993 * @return True on success, false otherwise.
994 */
995bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list)
996{
997 log_msg(LOG_DEFAULT, LVL_DEBUG, "init_device_tree()");
998
999 tree->current_handle = 0;
1000
1001 hash_table_create(&tree->devman_devices, 0, 0, &devman_devices_ops);
1002 hash_table_create(&tree->devman_functions, 0, 0, &devman_functions_ops);
1003 hash_table_create(&tree->loc_functions, 0, 0, &loc_devices_ops);
1004
1005 fibril_rwlock_initialize(&tree->rwlock);
1006
1007 /* Create root function and root device and add them to the device tree. */
1008 if (!create_root_nodes(tree))
1009 return false;
1010
1011 /* Find suitable driver and start it. */
1012 dev_node_t *rdev = tree->root_node->child;
1013 dev_add_ref(rdev);
1014 int rc = assign_driver(rdev, drivers_list, tree);
1015 dev_del_ref(rdev);
1016
1017 return rc;
1018}
1019
1020/* Device nodes */
1021
1022/** Create a new device node.
1023 *
1024 * @return A device node structure.
1025 */
1026dev_node_t *create_dev_node(void)
1027{
1028 dev_node_t *dev;
1029
1030 dev = calloc(1, sizeof(dev_node_t));
1031 if (dev == NULL)
1032 return NULL;
1033
1034 atomic_set(&dev->refcnt, 0);
1035 list_initialize(&dev->functions);
1036 link_initialize(&dev->driver_devices);
1037
1038 return dev;
1039}
1040
1041/** Delete a device node.
1042 *
1043 * @param node The device node structure.
1044 */
1045void delete_dev_node(dev_node_t *dev)
1046{
1047 assert(list_empty(&dev->functions));
1048 assert(dev->pfun == NULL);
1049 assert(dev->drv == NULL);
1050
1051 free(dev);
1052}
1053
1054/** Increase device node reference count.
1055 *
1056 * @param dev Device node
1057 */
1058void dev_add_ref(dev_node_t *dev)
1059{
1060 atomic_inc(&dev->refcnt);
1061}
1062
1063/** Decrease device node reference count.
1064 *
1065 * When the count drops to zero the device node is freed.
1066 *
1067 * @param dev Device node
1068 */
1069void dev_del_ref(dev_node_t *dev)
1070{
1071 if (atomic_predec(&dev->refcnt) == 0)
1072 delete_dev_node(dev);
1073}
1074
1075/** Find the device node structure of the device witch has the specified handle.
1076 *
1077 * @param tree The device tree where we look for the device node.
1078 * @param handle The handle of the device.
1079 * @return The device node.
1080 */
1081dev_node_t *find_dev_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
1082{
1083 assert(fibril_rwlock_is_locked(&tree->rwlock));
1084
1085 ht_link_t *link = hash_table_find(&tree->devman_devices, &handle);
1086 if (link == NULL)
1087 return NULL;
1088
1089 return hash_table_get_inst(link, dev_node_t, devman_dev);
1090}
1091
1092/** Find the device node structure of the device witch has the specified handle.
1093 *
1094 * @param tree The device tree where we look for the device node.
1095 * @param handle The handle of the device.
1096 * @return The device node.
1097 */
1098dev_node_t *find_dev_node(dev_tree_t *tree, devman_handle_t handle)
1099{
1100 dev_node_t *dev = NULL;
1101
1102 fibril_rwlock_read_lock(&tree->rwlock);
1103 dev = find_dev_node_no_lock(tree, handle);
1104 if (dev != NULL)
1105 dev_add_ref(dev);
1106
1107 fibril_rwlock_read_unlock(&tree->rwlock);
1108
1109 return dev;
1110}
1111
1112/** Get list of device functions. */
1113int dev_get_functions(dev_tree_t *tree, dev_node_t *dev,
1114 devman_handle_t *hdl_buf, size_t buf_size, size_t *act_size)
1115{
1116 size_t act_cnt;
1117 size_t buf_cnt;
1118
1119 assert(fibril_rwlock_is_locked(&tree->rwlock));
1120
1121 buf_cnt = buf_size / sizeof(devman_handle_t);
1122
1123 act_cnt = list_count(&dev->functions);
1124 *act_size = act_cnt * sizeof(devman_handle_t);
1125
1126 if (buf_size % sizeof(devman_handle_t) != 0)
1127 return EINVAL;
1128
1129 size_t pos = 0;
1130 list_foreach(dev->functions, dev_functions, fun_node_t, fun) {
1131 if (pos < buf_cnt) {
1132 hdl_buf[pos] = fun->handle;
1133 }
1134
1135 pos++;
1136 }
1137
1138 return EOK;
1139}
1140
1141
1142/* Function nodes */
1143
1144/** Create a new function node.
1145 *
1146 * @return A function node structure.
1147 */
1148fun_node_t *create_fun_node(void)
1149{
1150 fun_node_t *fun;
1151
1152 fun = calloc(1, sizeof(fun_node_t));
1153 if (fun == NULL)
1154 return NULL;
1155
1156 fun->state = FUN_INIT;
1157 atomic_set(&fun->refcnt, 0);
1158 fibril_mutex_initialize(&fun->busy_lock);
1159 link_initialize(&fun->dev_functions);
1160 list_initialize(&fun->match_ids.ids);
1161
1162 return fun;
1163}
1164
1165/** Delete a function node.
1166 *
1167 * @param fun The device node structure.
1168 */
1169void delete_fun_node(fun_node_t *fun)
1170{
1171 assert(fun->dev == NULL);
1172 assert(fun->child == NULL);
1173
1174 clean_match_ids(&fun->match_ids);
1175 free(fun->name);
1176 free(fun->pathname);
1177 free(fun);
1178}
1179
1180/** Increase function node reference count.
1181 *
1182 * @param fun Function node
1183 */
1184void fun_add_ref(fun_node_t *fun)
1185{
1186 atomic_inc(&fun->refcnt);
1187}
1188
1189/** Decrease function node reference count.
1190 *
1191 * When the count drops to zero the function node is freed.
1192 *
1193 * @param fun Function node
1194 */
1195void fun_del_ref(fun_node_t *fun)
1196{
1197 if (atomic_predec(&fun->refcnt) == 0)
1198 delete_fun_node(fun);
1199}
1200
1201/** Make function busy for reconfiguration operations. */
1202void fun_busy_lock(fun_node_t *fun)
1203{
1204 fibril_mutex_lock(&fun->busy_lock);
1205}
1206
1207/** Mark end of reconfiguration operation. */
1208void fun_busy_unlock(fun_node_t *fun)
1209{
1210 fibril_mutex_unlock(&fun->busy_lock);
1211}
1212
1213/** Find the function node with the specified handle.
1214 *
1215 * @param tree The device tree where we look for the device node.
1216 * @param handle The handle of the function.
1217 * @return The function node.
1218 */
1219fun_node_t *find_fun_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
1220{
1221 fun_node_t *fun;
1222
1223 assert(fibril_rwlock_is_locked(&tree->rwlock));
1224
1225 ht_link_t *link = hash_table_find(&tree->devman_functions, &handle);
1226 if (link == NULL)
1227 return NULL;
1228
1229 fun = hash_table_get_inst(link, fun_node_t, devman_fun);
1230
1231 return fun;
1232}
1233
1234/** Find the function node with the specified handle.
1235 *
1236 * @param tree The device tree where we look for the device node.
1237 * @param handle The handle of the function.
1238 * @return The function node.
1239 */
1240fun_node_t *find_fun_node(dev_tree_t *tree, devman_handle_t handle)
1241{
1242 fun_node_t *fun = NULL;
1243
1244 fibril_rwlock_read_lock(&tree->rwlock);
1245
1246 fun = find_fun_node_no_lock(tree, handle);
1247 if (fun != NULL)
1248 fun_add_ref(fun);
1249
1250 fibril_rwlock_read_unlock(&tree->rwlock);
1251
1252 return fun;
1253}
1254
1255/** Create and set device's full path in device tree.
1256 *
1257 * @param tree Device tree
1258 * @param node The device's device node.
1259 * @param parent The parent device node.
1260 * @return True on success, false otherwise (insufficient
1261 * resources etc.).
1262 */
1263static bool set_fun_path(dev_tree_t *tree, fun_node_t *fun, fun_node_t *parent)
1264{
1265 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
1266 assert(fun->name != NULL);
1267
1268 size_t pathsize = (str_size(fun->name) + 1);
1269 if (parent != NULL)
1270 pathsize += str_size(parent->pathname) + 1;
1271
1272 fun->pathname = (char *) malloc(pathsize);
1273 if (fun->pathname == NULL) {
1274 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to allocate device path.");
1275 return false;
1276 }
1277
1278 if (parent != NULL) {
1279 str_cpy(fun->pathname, pathsize, parent->pathname);
1280 str_append(fun->pathname, pathsize, "/");
1281 str_append(fun->pathname, pathsize, fun->name);
1282 } else {
1283 str_cpy(fun->pathname, pathsize, fun->name);
1284 }
1285
1286 return true;
1287}
1288
1289/** Insert new device into device tree.
1290 *
1291 * @param tree The device tree.
1292 * @param dev The newly added device node.
1293 * @param pfun The parent function node.
1294 *
1295 * @return True on success, false otherwise (insufficient resources
1296 * etc.).
1297 */
1298bool insert_dev_node(dev_tree_t *tree, dev_node_t *dev, fun_node_t *pfun)
1299{
1300 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
1301
1302 log_msg(LOG_DEFAULT, LVL_DEBUG, "insert_dev_node(dev=%p, pfun=%p [\"%s\"])",
1303 dev, pfun, pfun->pathname);
1304
1305 /* Add the node to the handle-to-node map. */
1306 dev->handle = ++tree->current_handle;
1307 hash_table_insert(&tree->devman_devices, &dev->devman_dev);
1308
1309 /* Add the node to the list of its parent's children. */
1310 dev->pfun = pfun;
1311 pfun->child = dev;
1312
1313 return true;
1314}
1315
1316/** Remove device from device tree.
1317 *
1318 * @param tree Device tree
1319 * @param dev Device node
1320 */
1321void remove_dev_node(dev_tree_t *tree, dev_node_t *dev)
1322{
1323 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
1324
1325 log_msg(LOG_DEFAULT, LVL_DEBUG, "remove_dev_node(dev=%p)", dev);
1326
1327 /* Remove node from the handle-to-node map. */
1328 hash_table_remove(&tree->devman_devices, &dev->handle);
1329
1330 /* Unlink from parent function. */
1331 dev->pfun->child = NULL;
1332 dev->pfun = NULL;
1333
1334 dev->state = DEVICE_REMOVED;
1335}
1336
1337
1338/** Insert new function into device tree.
1339 *
1340 * @param tree The device tree.
1341 * @param fun The newly added function node.
1342 * @param fun_name The name of the newly added function.
1343 * @param dev Owning device node.
1344 *
1345 * @return True on success, false otherwise (insufficient resources
1346 * etc.).
1347 */
1348bool insert_fun_node(dev_tree_t *tree, fun_node_t *fun, char *fun_name,
1349 dev_node_t *dev)
1350{
1351 fun_node_t *pfun;
1352
1353 assert(fun_name != NULL);
1354 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
1355
1356 /*
1357 * The root function is a special case, it does not belong to any
1358 * device so for the root function dev == NULL.
1359 */
1360 pfun = (dev != NULL) ? dev->pfun : NULL;
1361
1362 fun->name = fun_name;
1363 if (!set_fun_path(tree, fun, pfun)) {
1364 return false;
1365 }
1366
1367 /* Add the node to the handle-to-node map. */
1368 fun->handle = ++tree->current_handle;
1369 hash_table_insert(&tree->devman_functions, &fun->devman_fun);
1370
1371 /* Add the node to the list of its parent's children. */
1372 fun->dev = dev;
1373 if (dev != NULL)
1374 list_append(&fun->dev_functions, &dev->functions);
1375
1376 return true;
1377}
1378
1379/** Remove function from device tree.
1380 *
1381 * @param tree Device tree
1382 * @param node Function node to remove
1383 */
1384void remove_fun_node(dev_tree_t *tree, fun_node_t *fun)
1385{
1386 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
1387
1388 /* Remove the node from the handle-to-node map. */
1389 hash_table_remove(&tree->devman_functions, &fun->handle);
1390
1391 /* Remove the node from the list of its parent's children. */
1392 if (fun->dev != NULL)
1393 list_remove(&fun->dev_functions);
1394
1395 fun->dev = NULL;
1396 fun->state = FUN_REMOVED;
1397}
1398
1399/** Find function node with a specified path in the device tree.
1400 *
1401 * @param path The path of the function node in the device tree.
1402 * @param tree The device tree.
1403 * @return The function node if it is present in the tree, NULL
1404 * otherwise.
1405 */
1406fun_node_t *find_fun_node_by_path(dev_tree_t *tree, char *path)
1407{
1408 assert(path != NULL);
1409
1410 bool is_absolute = path[0] == '/';
1411 if (!is_absolute) {
1412 return NULL;
1413 }
1414
1415 fibril_rwlock_read_lock(&tree->rwlock);
1416
1417 fun_node_t *fun = tree->root_node;
1418 fun_add_ref(fun);
1419 /*
1420 * Relative path to the function from its parent (but with '/' at the
1421 * beginning)
1422 */
1423 char *rel_path = path;
1424 char *next_path_elem = NULL;
1425 bool cont = (rel_path[1] != '\0');
1426
1427 while (cont && fun != NULL) {
1428 next_path_elem = get_path_elem_end(rel_path + 1);
1429 if (next_path_elem[0] == '/') {
1430 cont = true;
1431 next_path_elem[0] = 0;
1432 } else {
1433 cont = false;
1434 }
1435
1436 fun_node_t *cfun = find_node_child(tree, fun, rel_path + 1);
1437 fun_del_ref(fun);
1438 fun = cfun;
1439
1440 if (cont) {
1441 /* Restore the original path. */
1442 next_path_elem[0] = '/';
1443 }
1444 rel_path = next_path_elem;
1445 }
1446
1447 fibril_rwlock_read_unlock(&tree->rwlock);
1448
1449 return fun;
1450}
1451
1452/** Find function with a specified name belonging to given device.
1453 *
1454 * Device tree rwlock should be held at least for reading.
1455 *
1456 * @param tree Device tree
1457 * @param dev Device the function belongs to.
1458 * @param name Function name (not path).
1459 * @return Function node.
1460 * @retval NULL No function with given name.
1461 */
1462fun_node_t *find_fun_node_in_device(dev_tree_t *tree, dev_node_t *dev,
1463 const char *name)
1464{
1465 assert(name != NULL);
1466 assert(fibril_rwlock_is_locked(&tree->rwlock));
1467
1468 list_foreach(dev->functions, dev_functions, fun_node_t, fun) {
1469 if (str_cmp(name, fun->name) == 0) {
1470 fun_add_ref(fun);
1471 return fun;
1472 }
1473 }
1474
1475 return NULL;
1476}
1477
1478/** Find child function node with a specified name.
1479 *
1480 * Device tree rwlock should be held at least for reading.
1481 *
1482 * @param tree Device tree
1483 * @param parent The parent function node.
1484 * @param name The name of the child function.
1485 * @return The child function node.
1486 */
1487static fun_node_t *find_node_child(dev_tree_t *tree, fun_node_t *pfun,
1488 const char *name)
1489{
1490 return find_fun_node_in_device(tree, pfun->child, name);
1491}
1492
1493/* loc devices */
1494
1495fun_node_t *find_loc_tree_function(dev_tree_t *tree, service_id_t service_id)
1496{
1497 fun_node_t *fun = NULL;
1498
1499 fibril_rwlock_read_lock(&tree->rwlock);
1500 ht_link_t *link = hash_table_find(&tree->loc_functions, &service_id);
1501 if (link != NULL) {
1502 fun = hash_table_get_inst(link, fun_node_t, loc_fun);
1503 fun_add_ref(fun);
1504 }
1505 fibril_rwlock_read_unlock(&tree->rwlock);
1506
1507 return fun;
1508}
1509
1510void tree_add_loc_function(dev_tree_t *tree, fun_node_t *fun)
1511{
1512 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
1513
1514 hash_table_insert(&tree->loc_functions, &fun->loc_fun);
1515}
1516
1517/** @}
1518 */
Note: See TracBrowser for help on using the repository browser.