source: mainline/uspace/srv/devman/devman.c@ 3e6a98c5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3e6a98c5 was 4e00f87, checked in by Jakub Jermar <jakub@…>, 13 years ago

Use NULL instead of 0 as a hash_table_ops_t member initializer.

  • Property mode set to 100644
File size: 37.1 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, *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, link) {
504 drv = list_get_instance(link, driver_t, drivers);
505 score = get_match_score(drv, node);
506 if (score > best_score) {
507 best_score = score;
508 best_drv = drv;
509 }
510 }
511
512 fibril_mutex_unlock(&drivers_list->drivers_mutex);
513
514 return best_drv;
515}
516
517/** Assign a driver to a device.
518 *
519 * @param tree Device tree
520 * @param node The device's node in the device tree.
521 * @param drv The driver.
522 */
523void attach_driver(dev_tree_t *tree, dev_node_t *dev, driver_t *drv)
524{
525 log_msg(LOG_DEFAULT, LVL_DEBUG, "attach_driver(dev=\"%s\",drv=\"%s\")",
526 dev->pfun->pathname, drv->name);
527
528 fibril_mutex_lock(&drv->driver_mutex);
529 fibril_rwlock_write_lock(&tree->rwlock);
530
531 dev->drv = drv;
532 list_append(&dev->driver_devices, &drv->devices);
533
534 fibril_rwlock_write_unlock(&tree->rwlock);
535 fibril_mutex_unlock(&drv->driver_mutex);
536}
537
538/** Detach driver from device.
539 *
540 * @param tree Device tree
541 * @param node The device's node in the device tree.
542 * @param drv The driver.
543 */
544void detach_driver(dev_tree_t *tree, dev_node_t *dev)
545{
546 driver_t *drv = dev->drv;
547
548 assert(drv != NULL);
549
550 log_msg(LOG_DEFAULT, LVL_DEBUG, "detach_driver(dev=\"%s\",drv=\"%s\")",
551 dev->pfun->pathname, drv->name);
552
553 fibril_mutex_lock(&drv->driver_mutex);
554 fibril_rwlock_write_lock(&tree->rwlock);
555
556 dev->drv = NULL;
557 list_remove(&dev->driver_devices);
558
559 fibril_rwlock_write_unlock(&tree->rwlock);
560 fibril_mutex_unlock(&drv->driver_mutex);
561}
562
563/** Start a driver
564 *
565 * @param drv The driver's structure.
566 * @return True if the driver's task is successfully spawned, false
567 * otherwise.
568 */
569bool start_driver(driver_t *drv)
570{
571 int rc;
572
573 assert(fibril_mutex_is_locked(&drv->driver_mutex));
574
575 log_msg(LOG_DEFAULT, LVL_DEBUG, "start_driver(drv=\"%s\")", drv->name);
576
577 rc = task_spawnl(NULL, drv->binary_path, drv->binary_path, NULL);
578 if (rc != EOK) {
579 log_msg(LOG_DEFAULT, LVL_ERROR, "Spawning driver `%s' (%s) failed: %s.",
580 drv->name, drv->binary_path, str_error(rc));
581 return false;
582 }
583
584 drv->state = DRIVER_STARTING;
585 return true;
586}
587
588/** Find device driver in the list of device drivers.
589 *
590 * @param drv_list The list of device drivers.
591 * @param drv_name The name of the device driver which is searched.
592 * @return The device driver of the specified name, if it is in the
593 * list, NULL otherwise.
594 */
595driver_t *find_driver(driver_list_t *drv_list, const char *drv_name)
596{
597 driver_t *res = NULL;
598 driver_t *drv = NULL;
599
600 fibril_mutex_lock(&drv_list->drivers_mutex);
601
602 list_foreach(drv_list->drivers, link) {
603 drv = list_get_instance(link, driver_t, drivers);
604 if (str_cmp(drv->name, drv_name) == 0) {
605 res = drv;
606 break;
607 }
608 }
609
610 fibril_mutex_unlock(&drv_list->drivers_mutex);
611
612 return res;
613}
614
615/** Notify driver about the devices to which it was assigned.
616 *
617 * @param driver The driver to which the devices are passed.
618 */
619static void pass_devices_to_driver(driver_t *driver, dev_tree_t *tree)
620{
621 dev_node_t *dev;
622 link_t *link;
623
624 log_msg(LOG_DEFAULT, LVL_DEBUG, "pass_devices_to_driver(driver=\"%s\")",
625 driver->name);
626
627 fibril_mutex_lock(&driver->driver_mutex);
628
629 /*
630 * Go through devices list as long as there is some device
631 * that has not been passed to the driver.
632 */
633 link = driver->devices.head.next;
634 while (link != &driver->devices.head) {
635 dev = list_get_instance(link, dev_node_t, driver_devices);
636 fibril_rwlock_write_lock(&tree->rwlock);
637
638 if (dev->passed_to_driver) {
639 fibril_rwlock_write_unlock(&tree->rwlock);
640 link = link->next;
641 continue;
642 }
643
644 log_msg(LOG_DEFAULT, LVL_DEBUG, "pass_devices_to_driver: dev->refcnt=%d\n",
645 (int)atomic_get(&dev->refcnt));
646 dev_add_ref(dev);
647
648 /*
649 * Unlock to avoid deadlock when adding device
650 * handled by itself.
651 */
652 fibril_mutex_unlock(&driver->driver_mutex);
653 fibril_rwlock_write_unlock(&tree->rwlock);
654
655 add_device(driver, dev, tree);
656
657 dev_del_ref(dev);
658
659 /*
660 * Lock again as we will work with driver's
661 * structure.
662 */
663 fibril_mutex_lock(&driver->driver_mutex);
664
665 /*
666 * Restart the cycle to go through all devices again.
667 */
668 link = driver->devices.head.next;
669 }
670
671 /*
672 * Once we passed all devices to the driver, we need to mark the
673 * driver as running.
674 * It is vital to do it here and inside critical section.
675 *
676 * If we would change the state earlier, other devices added to
677 * the driver would be added to the device list and started
678 * immediately and possibly started here as well.
679 */
680 log_msg(LOG_DEFAULT, LVL_DEBUG, "Driver `%s' enters running state.", driver->name);
681 driver->state = DRIVER_RUNNING;
682
683 fibril_mutex_unlock(&driver->driver_mutex);
684}
685
686/** Finish the initialization of a driver after it has succesfully started
687 * and after it has registered itself by the device manager.
688 *
689 * Pass devices formerly matched to the driver to the driver and remember the
690 * driver is running and fully functional now.
691 *
692 * @param driver The driver which registered itself as running by the
693 * device manager.
694 */
695void initialize_running_driver(driver_t *driver, dev_tree_t *tree)
696{
697 log_msg(LOG_DEFAULT, LVL_DEBUG, "initialize_running_driver(driver=\"%s\")",
698 driver->name);
699
700 /*
701 * Pass devices which have been already assigned to the driver to the
702 * driver.
703 */
704 pass_devices_to_driver(driver, tree);
705}
706
707/** Initialize device driver structure.
708 *
709 * @param drv The device driver structure.
710 */
711void init_driver(driver_t *drv)
712{
713 assert(drv != NULL);
714
715 memset(drv, 0, sizeof(driver_t));
716 list_initialize(&drv->match_ids.ids);
717 list_initialize(&drv->devices);
718 fibril_mutex_initialize(&drv->driver_mutex);
719 drv->sess = NULL;
720}
721
722/** Device driver structure clean-up.
723 *
724 * @param drv The device driver structure.
725 */
726void clean_driver(driver_t *drv)
727{
728 assert(drv != NULL);
729
730 free_not_null(drv->name);
731 free_not_null(drv->binary_path);
732
733 clean_match_ids(&drv->match_ids);
734
735 init_driver(drv);
736}
737
738/** Delete device driver structure.
739 *
740 * @param drv The device driver structure.
741 */
742void delete_driver(driver_t *drv)
743{
744 assert(drv != NULL);
745
746 clean_driver(drv);
747 free(drv);
748}
749
750/** Create loc path and name for the function. */
751void loc_register_tree_function(fun_node_t *fun, dev_tree_t *tree)
752{
753 char *loc_pathname = NULL;
754 char *loc_name = NULL;
755
756 assert(fibril_rwlock_is_locked(&tree->rwlock));
757
758 asprintf(&loc_name, "%s", fun->pathname);
759 if (loc_name == NULL)
760 return;
761
762 replace_char(loc_name, '/', LOC_SEPARATOR);
763
764 asprintf(&loc_pathname, "%s/%s", LOC_DEVICE_NAMESPACE,
765 loc_name);
766 if (loc_pathname == NULL) {
767 free(loc_name);
768 return;
769 }
770
771 loc_service_register_with_iface(loc_pathname,
772 &fun->service_id, DEVMAN_CONNECT_FROM_LOC);
773
774 tree_add_loc_function(tree, fun);
775
776 free(loc_name);
777 free(loc_pathname);
778}
779
780/** Pass a device to running driver.
781 *
782 * @param drv The driver's structure.
783 * @param node The device's node in the device tree.
784 */
785void add_device(driver_t *drv, dev_node_t *dev, dev_tree_t *tree)
786{
787 /*
788 * We do not expect to have driver's mutex locked as we do not
789 * access any structures that would affect driver_t.
790 */
791 log_msg(LOG_DEFAULT, LVL_DEBUG, "add_device(drv=\"%s\", dev=\"%s\")",
792 drv->name, dev->pfun->name);
793
794 /* Send the device to the driver. */
795 devman_handle_t parent_handle;
796 if (dev->pfun) {
797 parent_handle = dev->pfun->handle;
798 } else {
799 parent_handle = 0;
800 }
801
802 async_exch_t *exch = async_exchange_begin(drv->sess);
803
804 ipc_call_t answer;
805 aid_t req = async_send_2(exch, DRIVER_DEV_ADD, dev->handle,
806 parent_handle, &answer);
807
808 /* Send the device name to the driver. */
809 sysarg_t rc = async_data_write_start(exch, dev->pfun->name,
810 str_size(dev->pfun->name) + 1);
811
812 async_exchange_end(exch);
813
814 if (rc != EOK) {
815 /* TODO handle error */
816 }
817
818 /* Wait for answer from the driver. */
819 async_wait_for(req, &rc);
820
821 switch(rc) {
822 case EOK:
823 dev->state = DEVICE_USABLE;
824 break;
825 case ENOENT:
826 dev->state = DEVICE_NOT_PRESENT;
827 break;
828 default:
829 dev->state = DEVICE_INVALID;
830 break;
831 }
832
833 dev->passed_to_driver = true;
834
835 return;
836}
837
838/** Find suitable driver for a device and assign the driver to it.
839 *
840 * @param node The device node of the device in the device tree.
841 * @param drivers_list The list of available drivers.
842 * @return True if the suitable driver is found and
843 * successfully assigned to the device, false otherwise.
844 */
845bool assign_driver(dev_node_t *dev, driver_list_t *drivers_list,
846 dev_tree_t *tree)
847{
848 assert(dev != NULL);
849 assert(drivers_list != NULL);
850 assert(tree != NULL);
851
852 /*
853 * Find the driver which is the most suitable for handling this device.
854 */
855 driver_t *drv = find_best_match_driver(drivers_list, dev);
856 if (drv == NULL) {
857 log_msg(LOG_DEFAULT, LVL_ERROR, "No driver found for device `%s'.",
858 dev->pfun->pathname);
859 return false;
860 }
861
862 /* Attach the driver to the device. */
863 attach_driver(tree, dev, drv);
864
865 fibril_mutex_lock(&drv->driver_mutex);
866 if (drv->state == DRIVER_NOT_STARTED) {
867 /* Start the driver. */
868 start_driver(drv);
869 }
870 bool is_running = drv->state == DRIVER_RUNNING;
871 fibril_mutex_unlock(&drv->driver_mutex);
872
873 /* Notify the driver about the new device. */
874 if (is_running)
875 add_device(drv, dev, tree);
876
877 fibril_mutex_lock(&drv->driver_mutex);
878 fibril_mutex_unlock(&drv->driver_mutex);
879
880 fibril_rwlock_write_lock(&tree->rwlock);
881 if (dev->pfun != NULL) {
882 dev->pfun->state = FUN_ON_LINE;
883 }
884 fibril_rwlock_write_unlock(&tree->rwlock);
885 return true;
886}
887
888int driver_dev_remove(dev_tree_t *tree, dev_node_t *dev)
889{
890 async_exch_t *exch;
891 sysarg_t retval;
892 driver_t *drv;
893 devman_handle_t handle;
894
895 assert(dev != NULL);
896
897 log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_remove(%p)", dev);
898
899 fibril_rwlock_read_lock(&tree->rwlock);
900 drv = dev->drv;
901 handle = dev->handle;
902 fibril_rwlock_read_unlock(&tree->rwlock);
903
904 exch = async_exchange_begin(drv->sess);
905 retval = async_req_1_0(exch, DRIVER_DEV_REMOVE, handle);
906 async_exchange_end(exch);
907
908 return retval;
909}
910
911int driver_dev_gone(dev_tree_t *tree, dev_node_t *dev)
912{
913 async_exch_t *exch;
914 sysarg_t retval;
915 driver_t *drv;
916 devman_handle_t handle;
917
918 assert(dev != NULL);
919
920 log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_gone(%p)", dev);
921
922 fibril_rwlock_read_lock(&tree->rwlock);
923 drv = dev->drv;
924 handle = dev->handle;
925 fibril_rwlock_read_unlock(&tree->rwlock);
926
927 exch = async_exchange_begin(drv->sess);
928 retval = async_req_1_0(exch, DRIVER_DEV_GONE, handle);
929 async_exchange_end(exch);
930
931 return retval;
932}
933
934int driver_fun_online(dev_tree_t *tree, fun_node_t *fun)
935{
936 async_exch_t *exch;
937 sysarg_t retval;
938 driver_t *drv;
939 devman_handle_t handle;
940
941 log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_fun_online(%p)", fun);
942
943 fibril_rwlock_read_lock(&tree->rwlock);
944
945 if (fun->dev == NULL) {
946 /* XXX root function? */
947 fibril_rwlock_read_unlock(&tree->rwlock);
948 return EINVAL;
949 }
950
951 drv = fun->dev->drv;
952 handle = fun->handle;
953 fibril_rwlock_read_unlock(&tree->rwlock);
954
955 exch = async_exchange_begin(drv->sess);
956 retval = async_req_1_0(exch, DRIVER_FUN_ONLINE, handle);
957 loc_exchange_end(exch);
958
959 return retval;
960}
961
962int driver_fun_offline(dev_tree_t *tree, fun_node_t *fun)
963{
964 async_exch_t *exch;
965 sysarg_t retval;
966 driver_t *drv;
967 devman_handle_t handle;
968
969 log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_fun_offline(%p)", fun);
970
971 fibril_rwlock_read_lock(&tree->rwlock);
972 if (fun->dev == NULL) {
973 /* XXX root function? */
974 fibril_rwlock_read_unlock(&tree->rwlock);
975 return EINVAL;
976 }
977
978 drv = fun->dev->drv;
979 handle = fun->handle;
980 fibril_rwlock_read_unlock(&tree->rwlock);
981
982 exch = async_exchange_begin(drv->sess);
983 retval = async_req_1_0(exch, DRIVER_FUN_OFFLINE, handle);
984 loc_exchange_end(exch);
985
986 return retval;
987
988}
989
990/** Initialize the device tree.
991 *
992 * Create root device node of the tree and assign driver to it.
993 *
994 * @param tree The device tree.
995 * @param drivers_list the list of available drivers.
996 * @return True on success, false otherwise.
997 */
998bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list)
999{
1000 log_msg(LOG_DEFAULT, LVL_DEBUG, "init_device_tree()");
1001
1002 tree->current_handle = 0;
1003
1004 hash_table_create(&tree->devman_devices, 0, 0, &devman_devices_ops);
1005 hash_table_create(&tree->devman_functions, 0, 0, &devman_functions_ops);
1006 hash_table_create(&tree->loc_functions, 0, 0, &loc_devices_ops);
1007
1008 fibril_rwlock_initialize(&tree->rwlock);
1009
1010 /* Create root function and root device and add them to the device tree. */
1011 if (!create_root_nodes(tree))
1012 return false;
1013
1014 /* Find suitable driver and start it. */
1015 dev_node_t *rdev = tree->root_node->child;
1016 dev_add_ref(rdev);
1017 int rc = assign_driver(rdev, drivers_list, tree);
1018 dev_del_ref(rdev);
1019
1020 return rc;
1021}
1022
1023/* Device nodes */
1024
1025/** Create a new device node.
1026 *
1027 * @return A device node structure.
1028 */
1029dev_node_t *create_dev_node(void)
1030{
1031 dev_node_t *dev;
1032
1033 dev = calloc(1, sizeof(dev_node_t));
1034 if (dev == NULL)
1035 return NULL;
1036
1037 atomic_set(&dev->refcnt, 0);
1038 list_initialize(&dev->functions);
1039 link_initialize(&dev->driver_devices);
1040
1041 return dev;
1042}
1043
1044/** Delete a device node.
1045 *
1046 * @param node The device node structure.
1047 */
1048void delete_dev_node(dev_node_t *dev)
1049{
1050 assert(list_empty(&dev->functions));
1051 assert(dev->pfun == NULL);
1052 assert(dev->drv == NULL);
1053
1054 free(dev);
1055}
1056
1057/** Increase device node reference count.
1058 *
1059 * @param dev Device node
1060 */
1061void dev_add_ref(dev_node_t *dev)
1062{
1063 atomic_inc(&dev->refcnt);
1064}
1065
1066/** Decrease device node reference count.
1067 *
1068 * When the count drops to zero the device node is freed.
1069 *
1070 * @param dev Device node
1071 */
1072void dev_del_ref(dev_node_t *dev)
1073{
1074 if (atomic_predec(&dev->refcnt) == 0)
1075 delete_dev_node(dev);
1076}
1077
1078/** Find the device node structure of the device witch has the specified handle.
1079 *
1080 * @param tree The device tree where we look for the device node.
1081 * @param handle The handle of the device.
1082 * @return The device node.
1083 */
1084dev_node_t *find_dev_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
1085{
1086 assert(fibril_rwlock_is_locked(&tree->rwlock));
1087
1088 ht_link_t *link = hash_table_find(&tree->devman_devices, &handle);
1089 if (link == NULL)
1090 return NULL;
1091
1092 return hash_table_get_inst(link, dev_node_t, devman_dev);
1093}
1094
1095/** Find the device node structure of the device witch has the specified handle.
1096 *
1097 * @param tree The device tree where we look for the device node.
1098 * @param handle The handle of the device.
1099 * @return The device node.
1100 */
1101dev_node_t *find_dev_node(dev_tree_t *tree, devman_handle_t handle)
1102{
1103 dev_node_t *dev = NULL;
1104
1105 fibril_rwlock_read_lock(&tree->rwlock);
1106 dev = find_dev_node_no_lock(tree, handle);
1107 if (dev != NULL)
1108 dev_add_ref(dev);
1109
1110 fibril_rwlock_read_unlock(&tree->rwlock);
1111
1112 return dev;
1113}
1114
1115/** Get list of device functions. */
1116int dev_get_functions(dev_tree_t *tree, dev_node_t *dev,
1117 devman_handle_t *hdl_buf, size_t buf_size, size_t *act_size)
1118{
1119 size_t act_cnt;
1120 size_t buf_cnt;
1121
1122 assert(fibril_rwlock_is_locked(&tree->rwlock));
1123
1124 buf_cnt = buf_size / sizeof(devman_handle_t);
1125
1126 act_cnt = list_count(&dev->functions);
1127 *act_size = act_cnt * sizeof(devman_handle_t);
1128
1129 if (buf_size % sizeof(devman_handle_t) != 0)
1130 return EINVAL;
1131
1132 size_t pos = 0;
1133 list_foreach(dev->functions, item) {
1134 fun_node_t *fun =
1135 list_get_instance(item, fun_node_t, dev_functions);
1136
1137 if (pos < buf_cnt) {
1138 hdl_buf[pos] = fun->handle;
1139 }
1140
1141 pos++;
1142 }
1143
1144 return EOK;
1145}
1146
1147
1148/* Function nodes */
1149
1150/** Create a new function node.
1151 *
1152 * @return A function node structure.
1153 */
1154fun_node_t *create_fun_node(void)
1155{
1156 fun_node_t *fun;
1157
1158 fun = calloc(1, sizeof(fun_node_t));
1159 if (fun == NULL)
1160 return NULL;
1161
1162 fun->state = FUN_INIT;
1163 atomic_set(&fun->refcnt, 0);
1164 fibril_mutex_initialize(&fun->busy_lock);
1165 link_initialize(&fun->dev_functions);
1166 list_initialize(&fun->match_ids.ids);
1167
1168 return fun;
1169}
1170
1171/** Delete a function node.
1172 *
1173 * @param fun The device node structure.
1174 */
1175void delete_fun_node(fun_node_t *fun)
1176{
1177 assert(fun->dev == NULL);
1178 assert(fun->child == NULL);
1179
1180 clean_match_ids(&fun->match_ids);
1181 free_not_null(fun->name);
1182 free_not_null(fun->pathname);
1183 free(fun);
1184}
1185
1186/** Increase function node reference count.
1187 *
1188 * @param fun Function node
1189 */
1190void fun_add_ref(fun_node_t *fun)
1191{
1192 atomic_inc(&fun->refcnt);
1193}
1194
1195/** Decrease function node reference count.
1196 *
1197 * When the count drops to zero the function node is freed.
1198 *
1199 * @param fun Function node
1200 */
1201void fun_del_ref(fun_node_t *fun)
1202{
1203 if (atomic_predec(&fun->refcnt) == 0)
1204 delete_fun_node(fun);
1205}
1206
1207/** Make function busy for reconfiguration operations. */
1208void fun_busy_lock(fun_node_t *fun)
1209{
1210 fibril_mutex_lock(&fun->busy_lock);
1211}
1212
1213/** Mark end of reconfiguration operation. */
1214void fun_busy_unlock(fun_node_t *fun)
1215{
1216 fibril_mutex_unlock(&fun->busy_lock);
1217}
1218
1219/** Find the function node with the specified handle.
1220 *
1221 * @param tree The device tree where we look for the device node.
1222 * @param handle The handle of the function.
1223 * @return The function node.
1224 */
1225fun_node_t *find_fun_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
1226{
1227 fun_node_t *fun;
1228
1229 assert(fibril_rwlock_is_locked(&tree->rwlock));
1230
1231 ht_link_t *link = hash_table_find(&tree->devman_functions, &handle);
1232 if (link == NULL)
1233 return NULL;
1234
1235 fun = hash_table_get_inst(link, fun_node_t, devman_fun);
1236
1237 return fun;
1238}
1239
1240/** Find the function node with the specified handle.
1241 *
1242 * @param tree The device tree where we look for the device node.
1243 * @param handle The handle of the function.
1244 * @return The function node.
1245 */
1246fun_node_t *find_fun_node(dev_tree_t *tree, devman_handle_t handle)
1247{
1248 fun_node_t *fun = NULL;
1249
1250 fibril_rwlock_read_lock(&tree->rwlock);
1251
1252 fun = find_fun_node_no_lock(tree, handle);
1253 if (fun != NULL)
1254 fun_add_ref(fun);
1255
1256 fibril_rwlock_read_unlock(&tree->rwlock);
1257
1258 return fun;
1259}
1260
1261/** Create and set device's full path in device tree.
1262 *
1263 * @param tree Device tree
1264 * @param node The device's device node.
1265 * @param parent The parent device node.
1266 * @return True on success, false otherwise (insufficient
1267 * resources etc.).
1268 */
1269static bool set_fun_path(dev_tree_t *tree, fun_node_t *fun, fun_node_t *parent)
1270{
1271 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
1272 assert(fun->name != NULL);
1273
1274 size_t pathsize = (str_size(fun->name) + 1);
1275 if (parent != NULL)
1276 pathsize += str_size(parent->pathname) + 1;
1277
1278 fun->pathname = (char *) malloc(pathsize);
1279 if (fun->pathname == NULL) {
1280 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to allocate device path.");
1281 return false;
1282 }
1283
1284 if (parent != NULL) {
1285 str_cpy(fun->pathname, pathsize, parent->pathname);
1286 str_append(fun->pathname, pathsize, "/");
1287 str_append(fun->pathname, pathsize, fun->name);
1288 } else {
1289 str_cpy(fun->pathname, pathsize, fun->name);
1290 }
1291
1292 return true;
1293}
1294
1295/** Insert new device into device tree.
1296 *
1297 * @param tree The device tree.
1298 * @param dev The newly added device node.
1299 * @param pfun The parent function node.
1300 *
1301 * @return True on success, false otherwise (insufficient resources
1302 * etc.).
1303 */
1304bool insert_dev_node(dev_tree_t *tree, dev_node_t *dev, fun_node_t *pfun)
1305{
1306 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
1307
1308 log_msg(LOG_DEFAULT, LVL_DEBUG, "insert_dev_node(dev=%p, pfun=%p [\"%s\"])",
1309 dev, pfun, pfun->pathname);
1310
1311 /* Add the node to the handle-to-node map. */
1312 dev->handle = ++tree->current_handle;
1313 hash_table_insert(&tree->devman_devices, &dev->devman_dev);
1314
1315 /* Add the node to the list of its parent's children. */
1316 dev->pfun = pfun;
1317 pfun->child = dev;
1318
1319 return true;
1320}
1321
1322/** Remove device from device tree.
1323 *
1324 * @param tree Device tree
1325 * @param dev Device node
1326 */
1327void remove_dev_node(dev_tree_t *tree, dev_node_t *dev)
1328{
1329 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
1330
1331 log_msg(LOG_DEFAULT, LVL_DEBUG, "remove_dev_node(dev=%p)", dev);
1332
1333 /* Remove node from the handle-to-node map. */
1334 hash_table_remove(&tree->devman_devices, &dev->handle);
1335
1336 /* Unlink from parent function. */
1337 dev->pfun->child = NULL;
1338 dev->pfun = NULL;
1339
1340 dev->state = DEVICE_REMOVED;
1341}
1342
1343
1344/** Insert new function into device tree.
1345 *
1346 * @param tree The device tree.
1347 * @param fun The newly added function node.
1348 * @param fun_name The name of the newly added function.
1349 * @param dev Owning device node.
1350 *
1351 * @return True on success, false otherwise (insufficient resources
1352 * etc.).
1353 */
1354bool insert_fun_node(dev_tree_t *tree, fun_node_t *fun, char *fun_name,
1355 dev_node_t *dev)
1356{
1357 fun_node_t *pfun;
1358
1359 assert(fun_name != NULL);
1360 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
1361
1362 /*
1363 * The root function is a special case, it does not belong to any
1364 * device so for the root function dev == NULL.
1365 */
1366 pfun = (dev != NULL) ? dev->pfun : NULL;
1367
1368 fun->name = fun_name;
1369 if (!set_fun_path(tree, fun, pfun)) {
1370 return false;
1371 }
1372
1373 /* Add the node to the handle-to-node map. */
1374 fun->handle = ++tree->current_handle;
1375 hash_table_insert(&tree->devman_functions, &fun->devman_fun);
1376
1377 /* Add the node to the list of its parent's children. */
1378 fun->dev = dev;
1379 if (dev != NULL)
1380 list_append(&fun->dev_functions, &dev->functions);
1381
1382 return true;
1383}
1384
1385/** Remove function from device tree.
1386 *
1387 * @param tree Device tree
1388 * @param node Function node to remove
1389 */
1390void remove_fun_node(dev_tree_t *tree, fun_node_t *fun)
1391{
1392 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
1393
1394 /* Remove the node from the handle-to-node map. */
1395 hash_table_remove(&tree->devman_functions, &fun->handle);
1396
1397 /* Remove the node from the list of its parent's children. */
1398 if (fun->dev != NULL)
1399 list_remove(&fun->dev_functions);
1400
1401 fun->dev = NULL;
1402 fun->state = FUN_REMOVED;
1403}
1404
1405/** Find function node with a specified path in the device tree.
1406 *
1407 * @param path The path of the function node in the device tree.
1408 * @param tree The device tree.
1409 * @return The function node if it is present in the tree, NULL
1410 * otherwise.
1411 */
1412fun_node_t *find_fun_node_by_path(dev_tree_t *tree, char *path)
1413{
1414 assert(path != NULL);
1415
1416 bool is_absolute = path[0] == '/';
1417 if (!is_absolute) {
1418 return NULL;
1419 }
1420
1421 fibril_rwlock_read_lock(&tree->rwlock);
1422
1423 fun_node_t *fun = tree->root_node;
1424 fun_add_ref(fun);
1425 /*
1426 * Relative path to the function from its parent (but with '/' at the
1427 * beginning)
1428 */
1429 char *rel_path = path;
1430 char *next_path_elem = NULL;
1431 bool cont = (rel_path[1] != '\0');
1432
1433 while (cont && fun != NULL) {
1434 next_path_elem = get_path_elem_end(rel_path + 1);
1435 if (next_path_elem[0] == '/') {
1436 cont = true;
1437 next_path_elem[0] = 0;
1438 } else {
1439 cont = false;
1440 }
1441
1442 fun_node_t *cfun = find_node_child(tree, fun, rel_path + 1);
1443 fun_del_ref(fun);
1444 fun = cfun;
1445
1446 if (cont) {
1447 /* Restore the original path. */
1448 next_path_elem[0] = '/';
1449 }
1450 rel_path = next_path_elem;
1451 }
1452
1453 fibril_rwlock_read_unlock(&tree->rwlock);
1454
1455 return fun;
1456}
1457
1458/** Find function with a specified name belonging to given device.
1459 *
1460 * Device tree rwlock should be held at least for reading.
1461 *
1462 * @param tree Device tree
1463 * @param dev Device the function belongs to.
1464 * @param name Function name (not path).
1465 * @return Function node.
1466 * @retval NULL No function with given name.
1467 */
1468fun_node_t *find_fun_node_in_device(dev_tree_t *tree, dev_node_t *dev,
1469 const char *name)
1470{
1471 assert(name != NULL);
1472 assert(fibril_rwlock_is_locked(&tree->rwlock));
1473
1474 fun_node_t *fun;
1475
1476 list_foreach(dev->functions, link) {
1477 fun = list_get_instance(link, fun_node_t, dev_functions);
1478
1479 if (str_cmp(name, fun->name) == 0) {
1480 fun_add_ref(fun);
1481 return fun;
1482 }
1483 }
1484
1485 return NULL;
1486}
1487
1488/** Find child function node with a specified name.
1489 *
1490 * Device tree rwlock should be held at least for reading.
1491 *
1492 * @param tree Device tree
1493 * @param parent The parent function node.
1494 * @param name The name of the child function.
1495 * @return The child function node.
1496 */
1497static fun_node_t *find_node_child(dev_tree_t *tree, fun_node_t *pfun,
1498 const char *name)
1499{
1500 return find_fun_node_in_device(tree, pfun->child, name);
1501}
1502
1503/* loc devices */
1504
1505fun_node_t *find_loc_tree_function(dev_tree_t *tree, service_id_t service_id)
1506{
1507 fun_node_t *fun = NULL;
1508
1509 fibril_rwlock_read_lock(&tree->rwlock);
1510 ht_link_t *link = hash_table_find(&tree->loc_functions, &service_id);
1511 if (link != NULL) {
1512 fun = hash_table_get_inst(link, fun_node_t, loc_fun);
1513 fun_add_ref(fun);
1514 }
1515 fibril_rwlock_read_unlock(&tree->rwlock);
1516
1517 return fun;
1518}
1519
1520void tree_add_loc_function(dev_tree_t *tree, fun_node_t *fun)
1521{
1522 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
1523
1524 hash_table_insert(&tree->loc_functions, &fun->loc_fun);
1525}
1526
1527/** @}
1528 */
Note: See TracBrowser for help on using the repository browser.