source: mainline/uspace/srv/devman/devman.c@ 1a5b252

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

DDF support for function offlining and onlining. This allows
(anticipated) hot removal — support needs to be added in individual
drivers, currently there is support in test1 and partially in rootvirt.
Surprise removal is not supported. TODO synchronization.

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