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
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 */
[58cbb0c8]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 */
[0358da0]51
52#include <errno.h>
[e4c4247]53#include <fcntl.h>
[85e48a9]54#include <sys/stat.h>
[9b415c9]55#include <io/log.h>
[084ff99]56#include <ipc/driver.h>
57#include <ipc/devman.h>
[15f3c3f]58#include <loc.h>
[0485135]59#include <str_error.h>
[c7bbf029]60#include <stdio.h>
[0358da0]61
[e2b9a993]62#include "devman.h"
[0358da0]63
[58cbb0c8]64static fun_node_t *find_node_child(dev_tree_t *, fun_node_t *, const char *);
[ba38f72c]65
[38b3baf]66/* hash table operations */
[957cfa58]67
[062d900]68static inline size_t handle_key_hash(void *key)
[957cfa58]69{
[062d900]70 devman_handle_t handle = *(devman_handle_t*)key;
71 return handle;
[957cfa58]72}
73
[062d900]74static size_t devman_devices_hash(const ht_link_t *item)
[957cfa58]75{
[062d900]76 dev_node_t *dev = hash_table_get_inst(item, dev_node_t, devman_dev);
77 return handle_key_hash(&dev->handle);
[957cfa58]78}
79
[062d900]80static size_t devman_functions_hash(const ht_link_t *item)
[957cfa58]81{
[062d900]82 fun_node_t *fun = hash_table_get_inst(item, fun_node_t, devman_fun);
83 return handle_key_hash(&fun->handle);
[ba38f72c]84}
85
[062d900]86static bool devman_devices_key_equal(void *key, const ht_link_t *item)
[ba38f72c]87{
[062d900]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;
[957cfa58]91}
92
[062d900]93static bool devman_functions_key_equal(void *key, const ht_link_t *item)
[957cfa58]94{
[062d900]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;
[957cfa58]98}
99
[062d900]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,
[4e00f87]124 .equal = NULL,
125 .remove_callback = NULL
[957cfa58]126};
127
[062d900]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,
[4e00f87]132 .equal = NULL,
133 .remove_callback = NULL
[ba38f72c]134};
135
[062d900]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,
[4e00f87]140 .equal = NULL,
141 .remove_callback = NULL
[957cfa58]142};
143
[791f58c]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
[0c3666d]158/** Allocate and initialize a new driver structure.
[38b3baf]159 *
[58b833c]160 * @return Driver structure.
[0c3666d]161 */
[38b3baf]162driver_t *create_driver(void)
[58b833c]163{
[e4c4247]164 driver_t *res = malloc(sizeof(driver_t));
[38b3baf]165 if (res != NULL)
[08d9c4e6]166 init_driver(res);
[e4c4247]167 return res;
168}
169
[0c3666d]170/** Add a driver to the list of drivers.
[38b3baf]171 *
[58b833c]172 * @param drivers_list List of drivers.
173 * @param drv Driver structure.
[0c3666d]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);
[58b833c]180
[a1a101d]181 log_msg(LOG_DEFAULT, LVL_NOTE, "Driver `%s' was added to the list of available "
[ebcb05a]182 "drivers.", drv->name);
[0c3666d]183}
184
[38b3baf]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.
[0c3666d]190 */
[38b3baf]191char *read_match_id(char **buf)
[e4c4247]192{
193 char *res = NULL;
[e2b9a993]194 size_t len = get_nonspace_len(*buf);
[38b3baf]195
[e4c4247]196 if (len > 0) {
197 res = malloc(len + 1);
198 if (res != NULL) {
[38b3baf]199 str_ncpy(res, len + 1, *buf, len);
[e4c4247]200 *buf += len;
201 }
202 }
[38b3baf]203
[e4c4247]204 return res;
205}
206
[0c3666d]207/**
208 * Read match ids and associated match scores from a string.
[38b3baf]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.
[0c3666d]219 */
[c47e1a8]220bool parse_match_ids(char *buf, match_id_list_t *ids)
[e4c4247]221{
222 int score = 0;
223 char *id = NULL;
224 int ids_read = 0;
225
226 while (true) {
[38b3baf]227 /* skip spaces */
228 if (!skip_spaces(&buf))
[e4c4247]229 break;
[38b3baf]230
231 /* read score */
[e4c4247]232 score = strtoul(buf, &buf, 10);
233
[38b3baf]234 /* skip spaces */
235 if (!skip_spaces(&buf))
[e4c4247]236 break;
237
[38b3baf]238 /* read id */
239 id = read_match_id(&buf);
240 if (NULL == id)
241 break;
[e4c4247]242
[38b3baf]243 /* create new match_id structure */
[e4c4247]244 match_id_t *mid = create_match_id();
245 mid->id = id;
246 mid->score = score;
247
[38b3baf]248 /* add it to the list */
[e4c4247]249 add_match_id(ids, mid);
250
[38b3baf]251 ids_read++;
252 }
[e4c4247]253
254 return ids_read > 0;
255}
256
[0c3666d]257/**
258 * Read match ids and associated match scores from a file.
[38b3baf]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.
[0c3666d]269 */
[38b3baf]270bool read_match_ids(const char *conf_path, match_id_list_t *ids)
271{
[a1a101d]272 log_msg(LOG_DEFAULT, LVL_DEBUG, "read_match_ids(conf_path=\"%s\")", conf_path);
[08d9c4e6]273
[38b3baf]274 bool suc = false;
[e4c4247]275 char *buf = NULL;
276 bool opened = false;
[38b3baf]277 int fd;
[c47e1a8]278 size_t len = 0;
[e4c4247]279
280 fd = open(conf_path, O_RDONLY);
281 if (fd < 0) {
[a1a101d]282 log_msg(LOG_DEFAULT, LVL_ERROR, "Unable to open `%s' for reading: %s.",
[9b415c9]283 conf_path, str_error(fd));
[e4c4247]284 goto cleanup;
[38b3baf]285 }
286 opened = true;
[e4c4247]287
288 len = lseek(fd, 0, SEEK_END);
[38b3baf]289 lseek(fd, 0, SEEK_SET);
[e4c4247]290 if (len == 0) {
[a1a101d]291 log_msg(LOG_DEFAULT, LVL_ERROR, "Configuration file '%s' is empty.",
[9b415c9]292 conf_path);
[38b3baf]293 goto cleanup;
[e4c4247]294 }
295
296 buf = malloc(len + 1);
297 if (buf == NULL) {
[a1a101d]298 log_msg(LOG_DEFAULT, LVL_ERROR, "Memory allocation failed when parsing file "
[ebcb05a]299 "'%s'.", conf_path);
[e4c4247]300 goto cleanup;
[58b833c]301 }
[e4c4247]302
[8fd04ba9]303 ssize_t read_bytes = read_all(fd, buf, len);
[dc87f3fd]304 if (read_bytes <= 0) {
[a1a101d]305 log_msg(LOG_DEFAULT, LVL_ERROR, "Unable to read file '%s' (%zd).", conf_path,
[8fd04ba9]306 read_bytes);
[e4c4247]307 goto cleanup;
308 }
[dc87f3fd]309 buf[read_bytes] = 0;
[e4c4247]310
311 suc = parse_match_ids(buf, ids);
312
313cleanup:
314 free(buf);
315
[58b833c]316 if (opened)
[38b3baf]317 close(fd);
[e4c4247]318
319 return suc;
320}
321
[0c3666d]322/**
323 * Get information about a driver.
[38b3baf]324 *
325 * Each driver has its own directory in the base directory.
[0c3666d]326 * The name of the driver's directory is the same as the name of the driver.
[38b3baf]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.
[0c3666d]341 */
[e2b9a993]342bool get_driver_info(const char *base_path, const char *name, driver_t *drv)
[e4c4247]343{
[a1a101d]344 log_msg(LOG_DEFAULT, LVL_DEBUG, "get_driver_info(base_path=\"%s\", name=\"%s\")",
[38b3baf]345 base_path, name);
[08d9c4e6]346
[e4c4247]347 assert(base_path != NULL && name != NULL && drv != NULL);
348
349 bool suc = false;
[38b3baf]350 char *match_path = NULL;
[e4c4247]351 size_t name_size = 0;
352
[38b3baf]353 /* Read the list of match ids from the driver's configuration file. */
354 match_path = get_abs_path(base_path, name, MATCH_EXT);
[58b833c]355 if (match_path == NULL)
[e4c4247]356 goto cleanup;
357
[38b3baf]358 if (!read_match_ids(match_path, &drv->match_ids))
[e4c4247]359 goto cleanup;
360
[38b3baf]361 /* Allocate and fill driver's name. */
362 name_size = str_size(name) + 1;
[e4c4247]363 drv->name = malloc(name_size);
[58b833c]364 if (drv->name == NULL)
[e4c4247]365 goto cleanup;
366 str_cpy(drv->name, name_size, name);
367
[38b3baf]368 /* Initialize path with driver's binary. */
369 drv->binary_path = get_abs_path(base_path, name, "");
[58b833c]370 if (drv->binary_path == NULL)
[85e48a9]371 goto cleanup;
372
[38b3baf]373 /* Check whether the driver's binary exists. */
[85e48a9]374 struct stat s;
[58b833c]375 if (stat(drv->binary_path, &s) == ENOENT) { /* FIXME!! */
[a1a101d]376 log_msg(LOG_DEFAULT, LVL_ERROR, "Driver not found at path `%s'.",
[9b415c9]377 drv->binary_path);
[85e48a9]378 goto cleanup;
379 }
380
[e4c4247]381 suc = true;
382
383cleanup:
384 if (!suc) {
385 free(drv->binary_path);
386 free(drv->name);
[38b3baf]387 /* Set the driver structure to the default state. */
388 init_driver(drv);
[e4c4247]389 }
390
391 free(match_path);
392
393 return suc;
394}
395
396/** Lookup drivers in the directory.
[38b3baf]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 */
[0c3666d]402int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path)
[e4c4247]403{
[a1a101d]404 log_msg(LOG_DEFAULT, LVL_DEBUG, "lookup_available_drivers(dir=\"%s\")", dir_path);
[08d9c4e6]405
[e4c4247]406 int drv_cnt = 0;
407 DIR *dir = NULL;
408 struct dirent *diren;
409
410 dir = opendir(dir_path);
[08d9c4e6]411
[e4c4247]412 if (dir != NULL) {
413 driver_t *drv = create_driver();
[38b3baf]414 while ((diren = readdir(dir))) {
[e4c4247]415 if (get_driver_info(dir_path, diren->d_name, drv)) {
[e2b9a993]416 add_driver(drivers_list, drv);
[08d9c4e6]417 drv_cnt++;
[e4c4247]418 drv = create_driver();
[38b3baf]419 }
[e4c4247]420 }
421 delete_driver(drv);
422 closedir(dir);
423 }
424
425 return drv_cnt;
426}
427
[ba38f72c]428/** Create root device and function node in the device tree.
[38b3baf]429 *
[58b833c]430 * @param tree The device tree.
431 * @return True on success, false otherwise.
[0c3666d]432 */
[ba38f72c]433bool create_root_nodes(dev_tree_t *tree)
[e4c4247]434{
[ba38f72c]435 fun_node_t *fun;
436 dev_node_t *dev;
437
[a1a101d]438 log_msg(LOG_DEFAULT, LVL_DEBUG, "create_root_nodes()");
[ba38f72c]439
[01b87dc5]440 fibril_rwlock_write_lock(&tree->rwlock);
[ba38f72c]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;
[85e48a9]453 }
[ba38f72c]454
[58cbb0c8]455 fun_add_ref(fun);
[4022513]456 insert_fun_node(tree, fun, str_dup(""), NULL);
[58cbb0c8]457
[ba38f72c]458 match_id_t *id = create_match_id();
[4022513]459 id->id = str_dup("root");
[ba38f72c]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
[58cbb0c8]473 dev_add_ref(dev);
[ba38f72c]474 insert_dev_node(tree, dev, fun);
475
[01b87dc5]476 fibril_rwlock_write_unlock(&tree->rwlock);
[ba38f72c]477
478 return dev != NULL;
[85e48a9]479}
480
[38b3baf]481/** Lookup the best matching driver for the specified device in the list of
482 * drivers.
[0c3666d]483 *
[38b3baf]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.
[0c3666d]495 */
[ba38f72c]496driver_t *find_best_match_driver(driver_list_t *drivers_list, dev_node_t *node)
[e4c4247]497{
[feeac0d]498 driver_t *best_drv = NULL;
[85e48a9]499 int best_score = 0, score = 0;
500
[0c3666d]501 fibril_mutex_lock(&drivers_list->drivers_mutex);
[729fa2d6]502
[feeac0d]503 list_foreach(drivers_list->drivers, drivers, driver_t, drv) {
[85e48a9]504 score = get_match_score(drv, node);
505 if (score > best_score) {
506 best_score = score;
507 best_drv = drv;
[58b833c]508 }
[0c3666d]509 }
[729fa2d6]510
[0c3666d]511 fibril_mutex_unlock(&drivers_list->drivers_mutex);
[e4c4247]512
[38b3baf]513 return best_drv;
[85e48a9]514}
515
[38b3baf]516/** Assign a driver to a device.
517 *
[58cbb0c8]518 * @param tree Device tree
[38b3baf]519 * @param node The device's node in the device tree.
520 * @param drv The driver.
[0c3666d]521 */
[58cbb0c8]522void attach_driver(dev_tree_t *tree, dev_node_t *dev, driver_t *drv)
[85e48a9]523{
[a1a101d]524 log_msg(LOG_DEFAULT, LVL_DEBUG, "attach_driver(dev=\"%s\",drv=\"%s\")",
[9b415c9]525 dev->pfun->pathname, drv->name);
[2480e19]526
[0c3666d]527 fibril_mutex_lock(&drv->driver_mutex);
[58cbb0c8]528 fibril_rwlock_write_lock(&tree->rwlock);
[0c3666d]529
[ba38f72c]530 dev->drv = drv;
531 list_append(&dev->driver_devices, &drv->devices);
[0c3666d]532
[58cbb0c8]533 fibril_rwlock_write_unlock(&tree->rwlock);
[0c3666d]534 fibril_mutex_unlock(&drv->driver_mutex);
[85e48a9]535}
536
[1a5b252]537/** Detach driver from device.
538 *
[58cbb0c8]539 * @param tree Device tree
[1a5b252]540 * @param node The device's node in the device tree.
541 * @param drv The driver.
542 */
[58cbb0c8]543void detach_driver(dev_tree_t *tree, dev_node_t *dev)
[1a5b252]544{
545 driver_t *drv = dev->drv;
546
547 assert(drv != NULL);
[58cbb0c8]548
[a1a101d]549 log_msg(LOG_DEFAULT, LVL_DEBUG, "detach_driver(dev=\"%s\",drv=\"%s\")",
[1a5b252]550 dev->pfun->pathname, drv->name);
551
552 fibril_mutex_lock(&drv->driver_mutex);
[58cbb0c8]553 fibril_rwlock_write_lock(&tree->rwlock);
[1a5b252]554
555 dev->drv = NULL;
556 list_remove(&dev->driver_devices);
557
[58cbb0c8]558 fibril_rwlock_write_unlock(&tree->rwlock);
[1a5b252]559 fibril_mutex_unlock(&drv->driver_mutex);
560}
561
[38b3baf]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.
[0c3666d]567 */
[e2b9a993]568bool start_driver(driver_t *drv)
[85e48a9]569{
[0485135]570 int rc;
571
[01b87dc5]572 assert(fibril_mutex_is_locked(&drv->driver_mutex));
573
[a1a101d]574 log_msg(LOG_DEFAULT, LVL_DEBUG, "start_driver(drv=\"%s\")", drv->name);
[e85920d]575
[0485135]576 rc = task_spawnl(NULL, drv->binary_path, drv->binary_path, NULL);
577 if (rc != EOK) {
[a1a101d]578 log_msg(LOG_DEFAULT, LVL_ERROR, "Spawning driver `%s' (%s) failed: %s.",
[9b415c9]579 drv->name, drv->binary_path, str_error(rc));
[85e48a9]580 return false;
581 }
582
[e85920d]583 drv->state = DRIVER_STARTING;
[85e48a9]584 return true;
585}
586
[bda60d9]587/** Find device driver in the list of device drivers.
[38b3baf]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.
[bda60d9]593 */
[38b3baf]594driver_t *find_driver(driver_list_t *drv_list, const char *drv_name)
595{
[729fa2d6]596 driver_t *res = NULL;
597
[38b3baf]598 fibril_mutex_lock(&drv_list->drivers_mutex);
[729fa2d6]599
[feeac0d]600 list_foreach(drv_list->drivers, drivers, driver_t, drv) {
[58b833c]601 if (str_cmp(drv->name, drv_name) == 0) {
[729fa2d6]602 res = drv;
603 break;
[58b833c]604 }
605 }
[729fa2d6]606
607 fibril_mutex_unlock(&drv_list->drivers_mutex);
608
609 return res;
610}
611
[38b3baf]612/** Notify driver about the devices to which it was assigned.
613 *
614 * @param driver The driver to which the devices are passed.
[c16cf62]615 */
[a32defa]616static void pass_devices_to_driver(driver_t *driver, dev_tree_t *tree)
[38b3baf]617{
[ba38f72c]618 dev_node_t *dev;
[c16cf62]619 link_t *link;
[58b833c]620
[a1a101d]621 log_msg(LOG_DEFAULT, LVL_DEBUG, "pass_devices_to_driver(driver=\"%s\")",
[9b415c9]622 driver->name);
[58b833c]623
[5bee897]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 */
[b72efe8]630 link = driver->devices.head.next;
631 while (link != &driver->devices.head) {
[ba38f72c]632 dev = list_get_instance(link, dev_node_t, driver_devices);
[58cbb0c8]633 fibril_rwlock_write_lock(&tree->rwlock);
634
[5bee897]635 if (dev->passed_to_driver) {
[58cbb0c8]636 fibril_rwlock_write_unlock(&tree->rwlock);
[084ff99]637 link = link->next;
[5bee897]638 continue;
[084ff99]639 }
[5bee897]640
[a1a101d]641 log_msg(LOG_DEFAULT, LVL_DEBUG, "pass_devices_to_driver: dev->refcnt=%d\n",
[58cbb0c8]642 (int)atomic_get(&dev->refcnt));
643 dev_add_ref(dev);
[2edcb63]644
[5bee897]645 /*
646 * Unlock to avoid deadlock when adding device
647 * handled by itself.
648 */
649 fibril_mutex_unlock(&driver->driver_mutex);
[58cbb0c8]650 fibril_rwlock_write_unlock(&tree->rwlock);
[5bee897]651
[45059d6b]652 add_device(driver, dev, tree);
[5bee897]653
[58cbb0c8]654 dev_del_ref(dev);
655
[5bee897]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 */
[b72efe8]665 link = driver->devices.head.next;
[084ff99]666 }
[5bee897]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 */
[a1a101d]677 log_msg(LOG_DEFAULT, LVL_DEBUG, "Driver `%s' enters running state.", driver->name);
[5bee897]678 driver->state = DRIVER_RUNNING;
679
680 fibril_mutex_unlock(&driver->driver_mutex);
[c16cf62]681}
682
[38b3baf]683/** Finish the initialization of a driver after it has succesfully started
[bda60d9]684 * and after it has registered itself by the device manager.
[38b3baf]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.
[c16cf62]691 */
[38b3baf]692void initialize_running_driver(driver_t *driver, dev_tree_t *tree)
693{
[a1a101d]694 log_msg(LOG_DEFAULT, LVL_DEBUG, "initialize_running_driver(driver=\"%s\")",
[9b415c9]695 driver->name);
[c16cf62]696
[38b3baf]697 /*
698 * Pass devices which have been already assigned to the driver to the
699 * driver.
700 */
701 pass_devices_to_driver(driver, tree);
[c16cf62]702}
703
[791f58c]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);
[79ae36dd]716 drv->sess = NULL;
[791f58c]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
[5b08d750]727 free(drv->name);
728 free(drv->binary_path);
[791f58c]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}
[a32defa]746
[15f3c3f]747/** Create loc path and name for the function. */
748void loc_register_tree_function(fun_node_t *fun, dev_tree_t *tree)
[a32defa]749{
[15f3c3f]750 char *loc_pathname = NULL;
751 char *loc_name = NULL;
[a32defa]752
[58cbb0c8]753 assert(fibril_rwlock_is_locked(&tree->rwlock));
754
[15f3c3f]755 asprintf(&loc_name, "%s", fun->pathname);
756 if (loc_name == NULL)
[a32defa]757 return;
758
[15f3c3f]759 replace_char(loc_name, '/', LOC_SEPARATOR);
[a32defa]760
[15f3c3f]761 asprintf(&loc_pathname, "%s/%s", LOC_DEVICE_NAMESPACE,
762 loc_name);
763 if (loc_pathname == NULL) {
764 free(loc_name);
[a32defa]765 return;
[38b3baf]766 }
[a32defa]767
[15f3c3f]768 loc_service_register_with_iface(loc_pathname,
769 &fun->service_id, DEVMAN_CONNECT_FROM_LOC);
[a32defa]770
[15f3c3f]771 tree_add_loc_function(tree, fun);
[a32defa]772
[15f3c3f]773 free(loc_name);
774 free(loc_pathname);
[a32defa]775}
776
[0c3666d]777/** Pass a device to running driver.
[38b3baf]778 *
779 * @param drv The driver's structure.
780 * @param node The device's node in the device tree.
[0c3666d]781 */
[45059d6b]782void add_device(driver_t *drv, dev_node_t *dev, dev_tree_t *tree)
[85e48a9]783{
[5bee897]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 */
[a1a101d]788 log_msg(LOG_DEFAULT, LVL_DEBUG, "add_device(drv=\"%s\", dev=\"%s\")",
[9b415c9]789 drv->name, dev->pfun->name);
[a78fa2a]790
[38b3baf]791 /* Send the device to the driver. */
[0d6915f]792 devman_handle_t parent_handle;
[ba38f72c]793 if (dev->pfun) {
794 parent_handle = dev->pfun->handle;
[0d6915f]795 } else {
796 parent_handle = 0;
797 }
[79ae36dd]798
[45059d6b]799 async_exch_t *exch = async_exchange_begin(drv->sess);
[79ae36dd]800
801 ipc_call_t answer;
[1a5b252]802 aid_t req = async_send_2(exch, DRIVER_DEV_ADD, dev->handle,
[0d6915f]803 parent_handle, &answer);
[a78fa2a]804
[79ae36dd]805 /* Send the device name to the driver. */
806 sysarg_t rc = async_data_write_start(exch, dev->pfun->name,
[ba38f72c]807 str_size(dev->pfun->name) + 1);
[79ae36dd]808
809 async_exchange_end(exch);
810
[38b3baf]811 if (rc != EOK) {
812 /* TODO handle error */
813 }
[398c4d7]814
[38b3baf]815 /* Wait for answer from the driver. */
[a78fa2a]816 async_wait_for(req, &rc);
[5bee897]817
[a78fa2a]818 switch(rc) {
819 case EOK:
[ba38f72c]820 dev->state = DEVICE_USABLE;
[df747b9c]821 break;
[a78fa2a]822 case ENOENT:
[ba38f72c]823 dev->state = DEVICE_NOT_PRESENT;
[a78fa2a]824 break;
[df747b9c]825 default:
[ba38f72c]826 dev->state = DEVICE_INVALID;
[77a69ea]827 break;
[084ff99]828 }
[e85920d]829
[ba38f72c]830 dev->passed_to_driver = true;
[5bee897]831
[5cd136ab]832 return;
[85e48a9]833}
834
[38b3baf]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.
[0c3666d]841 */
[ba38f72c]842bool assign_driver(dev_node_t *dev, driver_list_t *drivers_list,
843 dev_tree_t *tree)
[85e48a9]844{
[ba38f72c]845 assert(dev != NULL);
846 assert(drivers_list != NULL);
847 assert(tree != NULL);
848
[38b3baf]849 /*
850 * Find the driver which is the most suitable for handling this device.
851 */
[ba38f72c]852 driver_t *drv = find_best_match_driver(drivers_list, dev);
[58b833c]853 if (drv == NULL) {
[a1a101d]854 log_msg(LOG_DEFAULT, LVL_ERROR, "No driver found for device `%s'.",
[ba38f72c]855 dev->pfun->pathname);
[38b3baf]856 return false;
[85e48a9]857 }
858
[38b3baf]859 /* Attach the driver to the device. */
[58cbb0c8]860 attach_driver(tree, dev, drv);
[85e48a9]861
[398c4d7]862 fibril_mutex_lock(&drv->driver_mutex);
[58b833c]863 if (drv->state == DRIVER_NOT_STARTED) {
[38b3baf]864 /* Start the driver. */
[85e48a9]865 start_driver(drv);
[38b3baf]866 }
[398c4d7]867 bool is_running = drv->state == DRIVER_RUNNING;
868 fibril_mutex_unlock(&drv->driver_mutex);
869
[45059d6b]870 /* Notify the driver about the new device. */
871 if (is_running)
872 add_device(drv, dev, tree);
[85e48a9]873
[c1a0488]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);
[85e48a9]882 return true;
883}
884
[58cbb0c8]885int driver_dev_remove(dev_tree_t *tree, dev_node_t *dev)
[1a5b252]886{
887 async_exch_t *exch;
888 sysarg_t retval;
889 driver_t *drv;
[58cbb0c8]890 devman_handle_t handle;
[1a5b252]891
892 assert(dev != NULL);
[58cbb0c8]893
[a1a101d]894 log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_remove(%p)", dev);
[58cbb0c8]895
896 fibril_rwlock_read_lock(&tree->rwlock);
[1a5b252]897 drv = dev->drv;
[58cbb0c8]898 handle = dev->handle;
899 fibril_rwlock_read_unlock(&tree->rwlock);
[1a5b252]900
901 exch = async_exchange_begin(drv->sess);
[58cbb0c8]902 retval = async_req_1_0(exch, DRIVER_DEV_REMOVE, handle);
[1a5b252]903 async_exchange_end(exch);
904
905 return retval;
[80a96d2]906}
[1a5b252]907
[80a96d2]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
[a1a101d]917 log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_gone(%p)", dev);
[80a96d2]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;
[1a5b252]929}
930
[58cbb0c8]931int driver_fun_online(dev_tree_t *tree, fun_node_t *fun)
[1a5b252]932{
933 async_exch_t *exch;
934 sysarg_t retval;
935 driver_t *drv;
[58cbb0c8]936 devman_handle_t handle;
[1a5b252]937
[a1a101d]938 log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_fun_online(%p)", fun);
[58cbb0c8]939
940 fibril_rwlock_read_lock(&tree->rwlock);
941
[1a5b252]942 if (fun->dev == NULL) {
943 /* XXX root function? */
[58cbb0c8]944 fibril_rwlock_read_unlock(&tree->rwlock);
[1a5b252]945 return EINVAL;
946 }
947
948 drv = fun->dev->drv;
[58cbb0c8]949 handle = fun->handle;
950 fibril_rwlock_read_unlock(&tree->rwlock);
[1a5b252]951
952 exch = async_exchange_begin(drv->sess);
[58cbb0c8]953 retval = async_req_1_0(exch, DRIVER_FUN_ONLINE, handle);
[1a5b252]954 loc_exchange_end(exch);
955
956 return retval;
957}
958
[58cbb0c8]959int driver_fun_offline(dev_tree_t *tree, fun_node_t *fun)
[1a5b252]960{
961 async_exch_t *exch;
962 sysarg_t retval;
963 driver_t *drv;
[58cbb0c8]964 devman_handle_t handle;
[1a5b252]965
[a1a101d]966 log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_fun_offline(%p)", fun);
[58cbb0c8]967
968 fibril_rwlock_read_lock(&tree->rwlock);
[1a5b252]969 if (fun->dev == NULL) {
970 /* XXX root function? */
[58cbb0c8]971 fibril_rwlock_read_unlock(&tree->rwlock);
[1a5b252]972 return EINVAL;
973 }
974
975 drv = fun->dev->drv;
[58cbb0c8]976 handle = fun->handle;
977 fibril_rwlock_read_unlock(&tree->rwlock);
[1a5b252]978
979 exch = async_exchange_begin(drv->sess);
[58cbb0c8]980 retval = async_req_1_0(exch, DRIVER_FUN_OFFLINE, handle);
[1a5b252]981 loc_exchange_end(exch);
982
983 return retval;
984
985}
986
[38b3baf]987/** Initialize the device tree.
988 *
[0c3666d]989 * Create root device node of the tree and assign driver to it.
[38b3baf]990 *
991 * @param tree The device tree.
992 * @param drivers_list the list of available drivers.
993 * @return True on success, false otherwise.
[0c3666d]994 */
995bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list)
[85e48a9]996{
[a1a101d]997 log_msg(LOG_DEFAULT, LVL_DEBUG, "init_device_tree()");
[0c3666d]998
[957cfa58]999 tree->current_handle = 0;
1000
[062d900]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);
[bda60d9]1004
[957cfa58]1005 fibril_rwlock_initialize(&tree->rwlock);
[084ff99]1006
[ba38f72c]1007 /* Create root function and root device and add them to the device tree. */
1008 if (!create_root_nodes(tree))
[85e48a9]1009 return false;
[58cbb0c8]1010
[38b3baf]1011 /* Find suitable driver and start it. */
[58cbb0c8]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;
[e4c4247]1018}
1019
[791f58c]1020/* Device nodes */
1021
1022/** Create a new device node.
1023 *
1024 * @return A device node structure.
1025 */
[ba38f72c]1026dev_node_t *create_dev_node(void)
[791f58c]1027{
[58cbb0c8]1028 dev_node_t *dev;
[791f58c]1029
[58cbb0c8]1030 dev = calloc(1, sizeof(dev_node_t));
1031 if (dev == NULL)
1032 return NULL;
[791f58c]1033
[58cbb0c8]1034 atomic_set(&dev->refcnt, 0);
1035 list_initialize(&dev->functions);
1036 link_initialize(&dev->driver_devices);
1037
1038 return dev;
[791f58c]1039}
1040
1041/** Delete a device node.
1042 *
1043 * @param node The device node structure.
1044 */
[ba38f72c]1045void delete_dev_node(dev_node_t *dev)
[791f58c]1046{
[ba38f72c]1047 assert(list_empty(&dev->functions));
1048 assert(dev->pfun == NULL);
1049 assert(dev->drv == NULL);
1050
1051 free(dev);
[791f58c]1052}
1053
[58cbb0c8]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
[791f58c]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 */
[ba38f72c]1081dev_node_t *find_dev_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
[791f58c]1082{
[01b87dc5]1083 assert(fibril_rwlock_is_locked(&tree->rwlock));
1084
[062d900]1085 ht_link_t *link = hash_table_find(&tree->devman_devices, &handle);
[f98bec0f]1086 if (link == NULL)
1087 return NULL;
1088
[062d900]1089 return hash_table_get_inst(link, dev_node_t, devman_dev);
[791f58c]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 */
[ba38f72c]1098dev_node_t *find_dev_node(dev_tree_t *tree, devman_handle_t handle)
[791f58c]1099{
[ba38f72c]1100 dev_node_t *dev = NULL;
[791f58c]1101
1102 fibril_rwlock_read_lock(&tree->rwlock);
[ba38f72c]1103 dev = find_dev_node_no_lock(tree, handle);
[58cbb0c8]1104 if (dev != NULL)
1105 dev_add_ref(dev);
1106
[791f58c]1107 fibril_rwlock_read_unlock(&tree->rwlock);
1108
[ba38f72c]1109 return dev;
1110}
1111
[7beb220]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;
[feeac0d]1130 list_foreach(dev->functions, dev_functions, fun_node_t, fun) {
[58cbb0c8]1131 if (pos < buf_cnt) {
[7beb220]1132 hdl_buf[pos] = fun->handle;
[58cbb0c8]1133 }
1134
[7beb220]1135 pos++;
1136 }
1137
1138 return EOK;
1139}
1140
1141
[ba38f72c]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{
[58cbb0c8]1150 fun_node_t *fun;
1151
1152 fun = calloc(1, sizeof(fun_node_t));
1153 if (fun == NULL)
1154 return NULL;
[ba38f72c]1155
[c1a0488]1156 fun->state = FUN_INIT;
[58cbb0c8]1157 atomic_set(&fun->refcnt, 0);
[4820360]1158 fibril_mutex_initialize(&fun->busy_lock);
[58cbb0c8]1159 link_initialize(&fun->dev_functions);
1160 list_initialize(&fun->match_ids.ids);
1161
1162 return fun;
[ba38f72c]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);
[5b08d750]1175 free(fun->name);
1176 free(fun->pathname);
[ba38f72c]1177 free(fun);
1178}
1179
[58cbb0c8]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
[4820360]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
[ba38f72c]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{
[58cbb0c8]1221 fun_node_t *fun;
[ba38f72c]1222
1223 assert(fibril_rwlock_is_locked(&tree->rwlock));
1224
[062d900]1225 ht_link_t *link = hash_table_find(&tree->devman_functions, &handle);
[ba38f72c]1226 if (link == NULL)
1227 return NULL;
1228
[062d900]1229 fun = hash_table_get_inst(link, fun_node_t, devman_fun);
[58cbb0c8]1230
1231 return fun;
[791f58c]1232}
1233
[ba38f72c]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);
[58cbb0c8]1245
[ba38f72c]1246 fun = find_fun_node_no_lock(tree, handle);
[58cbb0c8]1247 if (fun != NULL)
1248 fun_add_ref(fun);
1249
[ba38f72c]1250 fibril_rwlock_read_unlock(&tree->rwlock);
1251
1252 return fun;
1253}
[791f58c]1254
[bda60d9]1255/** Create and set device's full path in device tree.
[38b3baf]1256 *
[58cbb0c8]1257 * @param tree Device tree
[38b3baf]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.).
[bda60d9]1262 */
[58cbb0c8]1263static bool set_fun_path(dev_tree_t *tree, fun_node_t *fun, fun_node_t *parent)
[38b3baf]1264{
[58cbb0c8]1265 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
[ba38f72c]1266 assert(fun->name != NULL);
[bda60d9]1267
[ba38f72c]1268 size_t pathsize = (str_size(fun->name) + 1);
[58b833c]1269 if (parent != NULL)
[38b3baf]1270 pathsize += str_size(parent->pathname) + 1;
[bda60d9]1271
[ba38f72c]1272 fun->pathname = (char *) malloc(pathsize);
1273 if (fun->pathname == NULL) {
[a1a101d]1274 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to allocate device path.");
[bda60d9]1275 return false;
1276 }
1277
[58b833c]1278 if (parent != NULL) {
[ba38f72c]1279 str_cpy(fun->pathname, pathsize, parent->pathname);
1280 str_append(fun->pathname, pathsize, "/");
1281 str_append(fun->pathname, pathsize, fun->name);
[bda60d9]1282 } else {
[ba38f72c]1283 str_cpy(fun->pathname, pathsize, fun->name);
[bda60d9]1284 }
1285
1286 return true;
1287}
1288
1289/** Insert new device into device tree.
[38b3baf]1290 *
1291 * @param tree The device tree.
[58cbb0c8]1292 * @param dev The newly added device node.
1293 * @param pfun The parent function node.
[58b833c]1294 *
[38b3baf]1295 * @return True on success, false otherwise (insufficient resources
1296 * etc.).
[bda60d9]1297 */
[ba38f72c]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
[a1a101d]1302 log_msg(LOG_DEFAULT, LVL_DEBUG, "insert_dev_node(dev=%p, pfun=%p [\"%s\"])",
[9b415c9]1303 dev, pfun, pfun->pathname);
1304
[ba38f72c]1305 /* Add the node to the handle-to-node map. */
1306 dev->handle = ++tree->current_handle;
[062d900]1307 hash_table_insert(&tree->devman_devices, &dev->devman_dev);
[ba38f72c]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
[1a5b252]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
[a1a101d]1325 log_msg(LOG_DEFAULT, LVL_DEBUG, "remove_dev_node(dev=%p)", dev);
[1a5b252]1326
1327 /* Remove node from the handle-to-node map. */
[062d900]1328 hash_table_remove(&tree->devman_devices, &dev->handle);
[1a5b252]1329
1330 /* Unlink from parent function. */
1331 dev->pfun->child = NULL;
1332 dev->pfun = NULL;
[c1a0488]1333
1334 dev->state = DEVICE_REMOVED;
[1a5b252]1335}
1336
1337
[ba38f72c]1338/** Insert new function into device tree.
1339 *
1340 * @param tree The device tree.
[58cbb0c8]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.
[ba38f72c]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)
[bda60d9]1350{
[ba38f72c]1351 fun_node_t *pfun;
1352
1353 assert(fun_name != NULL);
[01b87dc5]1354 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
[bda60d9]1355
[ba38f72c]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;
[58cbb0c8]1363 if (!set_fun_path(tree, fun, pfun)) {
[38b3baf]1364 return false;
[bda60d9]1365 }
1366
[38b3baf]1367 /* Add the node to the handle-to-node map. */
[ba38f72c]1368 fun->handle = ++tree->current_handle;
[062d900]1369 hash_table_insert(&tree->devman_functions, &fun->devman_fun);
[bda60d9]1370
[38b3baf]1371 /* Add the node to the list of its parent's children. */
[ba38f72c]1372 fun->dev = dev;
1373 if (dev != NULL)
1374 list_append(&fun->dev_functions, &dev->functions);
[38b3baf]1375
[bda60d9]1376 return true;
1377}
1378
[d0dd7b5]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. */
[062d900]1389 hash_table_remove(&tree->devman_functions, &fun->handle);
[d0dd7b5]1390
1391 /* Remove the node from the list of its parent's children. */
1392 if (fun->dev != NULL)
1393 list_remove(&fun->dev_functions);
[1a5b252]1394
1395 fun->dev = NULL;
[c1a0488]1396 fun->state = FUN_REMOVED;
[d0dd7b5]1397}
1398
[ba38f72c]1399/** Find function node with a specified path in the device tree.
[5cd136ab]1400 *
[ba38f72c]1401 * @param path The path of the function node in the device tree.
[38b3baf]1402 * @param tree The device tree.
[ba38f72c]1403 * @return The function node if it is present in the tree, NULL
[38b3baf]1404 * otherwise.
[5cd136ab]1405 */
[ba38f72c]1406fun_node_t *find_fun_node_by_path(dev_tree_t *tree, char *path)
[5cd136ab]1407{
[df147c7]1408 assert(path != NULL);
1409
1410 bool is_absolute = path[0] == '/';
1411 if (!is_absolute) {
1412 return NULL;
1413 }
1414
[957cfa58]1415 fibril_rwlock_read_lock(&tree->rwlock);
1416
[ba38f72c]1417 fun_node_t *fun = tree->root_node;
[58cbb0c8]1418 fun_add_ref(fun);
[38b3baf]1419 /*
[ba38f72c]1420 * Relative path to the function from its parent (but with '/' at the
[38b3baf]1421 * beginning)
1422 */
[5cd136ab]1423 char *rel_path = path;
1424 char *next_path_elem = NULL;
[7beb220]1425 bool cont = (rel_path[1] != '\0');
[5cd136ab]1426
[ba38f72c]1427 while (cont && fun != NULL) {
[38b3baf]1428 next_path_elem = get_path_elem_end(rel_path + 1);
[58b833c]1429 if (next_path_elem[0] == '/') {
[5cd136ab]1430 cont = true;
1431 next_path_elem[0] = 0;
1432 } else {
1433 cont = false;
1434 }
1435
[58cbb0c8]1436 fun_node_t *cfun = find_node_child(tree, fun, rel_path + 1);
1437 fun_del_ref(fun);
1438 fun = cfun;
[5cd136ab]1439
1440 if (cont) {
[38b3baf]1441 /* Restore the original path. */
[5cd136ab]1442 next_path_elem[0] = '/';
1443 }
[38b3baf]1444 rel_path = next_path_elem;
[5cd136ab]1445 }
1446
[957cfa58]1447 fibril_rwlock_read_unlock(&tree->rwlock);
1448
[ba38f72c]1449 return fun;
[5cd136ab]1450}
1451
[0876062]1452/** Find function with a specified name belonging to given device.
[38b3baf]1453 *
1454 * Device tree rwlock should be held at least for reading.
1455 *
[58cbb0c8]1456 * @param tree Device tree
[0876062]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.
[5cd136ab]1461 */
[58cbb0c8]1462fun_node_t *find_fun_node_in_device(dev_tree_t *tree, dev_node_t *dev,
1463 const char *name)
[5cd136ab]1464{
[0876062]1465 assert(name != NULL);
[58cbb0c8]1466 assert(fibril_rwlock_is_locked(&tree->rwlock));
[0876062]1467
[feeac0d]1468 list_foreach(dev->functions, dev_functions, fun_node_t, fun) {
[58cbb0c8]1469 if (str_cmp(name, fun->name) == 0) {
1470 fun_add_ref(fun);
[ba38f72c]1471 return fun;
[58cbb0c8]1472 }
[38b3baf]1473 }
[0876062]1474
[5cd136ab]1475 return NULL;
1476}
1477
[0876062]1478/** Find child function node with a specified name.
1479 *
1480 * Device tree rwlock should be held at least for reading.
1481 *
[58cbb0c8]1482 * @param tree Device tree
[0876062]1483 * @param parent The parent function node.
1484 * @param name The name of the child function.
1485 * @return The child function node.
1486 */
[58cbb0c8]1487static fun_node_t *find_node_child(dev_tree_t *tree, fun_node_t *pfun,
1488 const char *name)
[0876062]1489{
[58cbb0c8]1490 return find_fun_node_in_device(tree, pfun->child, name);
[0876062]1491}
1492
[15f3c3f]1493/* loc devices */
[ce89036b]1494
[15f3c3f]1495fun_node_t *find_loc_tree_function(dev_tree_t *tree, service_id_t service_id)
[ce89036b]1496{
[ba38f72c]1497 fun_node_t *fun = NULL;
[ce89036b]1498
1499 fibril_rwlock_read_lock(&tree->rwlock);
[062d900]1500 ht_link_t *link = hash_table_find(&tree->loc_functions, &service_id);
[58cbb0c8]1501 if (link != NULL) {
[062d900]1502 fun = hash_table_get_inst(link, fun_node_t, loc_fun);
[58cbb0c8]1503 fun_add_ref(fun);
1504 }
[ce89036b]1505 fibril_rwlock_read_unlock(&tree->rwlock);
1506
[ba38f72c]1507 return fun;
[ce89036b]1508}
1509
[15f3c3f]1510void tree_add_loc_function(dev_tree_t *tree, fun_node_t *fun)
[791f58c]1511{
[58cbb0c8]1512 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
1513
[062d900]1514 hash_table_insert(&tree->loc_functions, &fun->loc_fun);
[791f58c]1515}
1516
[c16cf62]1517/** @}
[58b833c]1518 */
Note: See TracBrowser for help on using the repository browser.