[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] | 64 | static fun_node_t *find_node_child(dev_tree_t *, fun_node_t *, const char *);
|
---|
[ba38f72c] | 65 |
|
---|
[38b3baf] | 66 | /* hash table operations */
|
---|
[957cfa58] | 67 |
|
---|
[062d900] | 68 | static 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] | 74 | static 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] | 80 | static 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] | 86 | static 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] | 93 | static 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] | 100 | static 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 |
|
---|
| 106 | static 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 |
|
---|
| 112 | static 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 |
|
---|
| 120 | static 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] | 128 | static 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] | 136 | static 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 | */
|
---|
| 150 | void 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] | 162 | driver_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 | */
|
---|
| 175 | void 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] | 191 | char *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] | 220 | bool 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] | 270 | bool 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 |
|
---|
| 313 | cleanup:
|
---|
| 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] | 342 | bool 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 |
|
---|
| 383 | cleanup:
|
---|
| 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] | 402 | int 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] | 433 | bool 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] | 496 | driver_t *find_best_match_driver(driver_list_t *drivers_list, dev_node_t *node)
|
---|
[e4c4247] | 497 | {
|
---|
[85e48a9] | 498 | driver_t *best_drv = NULL, *drv = NULL;
|
---|
| 499 | int best_score = 0, score = 0;
|
---|
| 500 |
|
---|
[0c3666d] | 501 | fibril_mutex_lock(&drivers_list->drivers_mutex);
|
---|
[729fa2d6] | 502 |
|
---|
[b72efe8] | 503 | list_foreach(drivers_list->drivers, link) {
|
---|
[85e48a9] | 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;
|
---|
[58b833c] | 509 | }
|
---|
[0c3666d] | 510 | }
|
---|
[729fa2d6] | 511 |
|
---|
[0c3666d] | 512 | fibril_mutex_unlock(&drivers_list->drivers_mutex);
|
---|
[e4c4247] | 513 |
|
---|
[38b3baf] | 514 | return best_drv;
|
---|
[85e48a9] | 515 | }
|
---|
| 516 |
|
---|
[38b3baf] | 517 | /** Assign a driver to a device.
|
---|
| 518 | *
|
---|
[58cbb0c8] | 519 | * @param tree Device tree
|
---|
[38b3baf] | 520 | * @param node The device's node in the device tree.
|
---|
| 521 | * @param drv The driver.
|
---|
[0c3666d] | 522 | */
|
---|
[58cbb0c8] | 523 | void attach_driver(dev_tree_t *tree, dev_node_t *dev, driver_t *drv)
|
---|
[85e48a9] | 524 | {
|
---|
[a1a101d] | 525 | log_msg(LOG_DEFAULT, LVL_DEBUG, "attach_driver(dev=\"%s\",drv=\"%s\")",
|
---|
[9b415c9] | 526 | dev->pfun->pathname, drv->name);
|
---|
[2480e19] | 527 |
|
---|
[0c3666d] | 528 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
[58cbb0c8] | 529 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
[0c3666d] | 530 |
|
---|
[ba38f72c] | 531 | dev->drv = drv;
|
---|
| 532 | list_append(&dev->driver_devices, &drv->devices);
|
---|
[0c3666d] | 533 |
|
---|
[58cbb0c8] | 534 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
[0c3666d] | 535 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
[85e48a9] | 536 | }
|
---|
| 537 |
|
---|
[1a5b252] | 538 | /** Detach driver from device.
|
---|
| 539 | *
|
---|
[58cbb0c8] | 540 | * @param tree Device tree
|
---|
[1a5b252] | 541 | * @param node The device's node in the device tree.
|
---|
| 542 | * @param drv The driver.
|
---|
| 543 | */
|
---|
[58cbb0c8] | 544 | void detach_driver(dev_tree_t *tree, dev_node_t *dev)
|
---|
[1a5b252] | 545 | {
|
---|
| 546 | driver_t *drv = dev->drv;
|
---|
| 547 |
|
---|
| 548 | assert(drv != NULL);
|
---|
[58cbb0c8] | 549 |
|
---|
[a1a101d] | 550 | log_msg(LOG_DEFAULT, LVL_DEBUG, "detach_driver(dev=\"%s\",drv=\"%s\")",
|
---|
[1a5b252] | 551 | dev->pfun->pathname, drv->name);
|
---|
| 552 |
|
---|
| 553 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
[58cbb0c8] | 554 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
[1a5b252] | 555 |
|
---|
| 556 | dev->drv = NULL;
|
---|
| 557 | list_remove(&dev->driver_devices);
|
---|
| 558 |
|
---|
[58cbb0c8] | 559 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
[1a5b252] | 560 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
| 561 | }
|
---|
| 562 |
|
---|
[38b3baf] | 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.
|
---|
[0c3666d] | 568 | */
|
---|
[e2b9a993] | 569 | bool start_driver(driver_t *drv)
|
---|
[85e48a9] | 570 | {
|
---|
[0485135] | 571 | int rc;
|
---|
| 572 |
|
---|
[01b87dc5] | 573 | assert(fibril_mutex_is_locked(&drv->driver_mutex));
|
---|
| 574 |
|
---|
[a1a101d] | 575 | log_msg(LOG_DEFAULT, LVL_DEBUG, "start_driver(drv=\"%s\")", drv->name);
|
---|
[e85920d] | 576 |
|
---|
[0485135] | 577 | rc = task_spawnl(NULL, drv->binary_path, drv->binary_path, NULL);
|
---|
| 578 | if (rc != EOK) {
|
---|
[a1a101d] | 579 | log_msg(LOG_DEFAULT, LVL_ERROR, "Spawning driver `%s' (%s) failed: %s.",
|
---|
[9b415c9] | 580 | drv->name, drv->binary_path, str_error(rc));
|
---|
[85e48a9] | 581 | return false;
|
---|
| 582 | }
|
---|
| 583 |
|
---|
[e85920d] | 584 | drv->state = DRIVER_STARTING;
|
---|
[85e48a9] | 585 | return true;
|
---|
| 586 | }
|
---|
| 587 |
|
---|
[bda60d9] | 588 | /** Find device driver in the list of device drivers.
|
---|
[38b3baf] | 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.
|
---|
[bda60d9] | 594 | */
|
---|
[38b3baf] | 595 | driver_t *find_driver(driver_list_t *drv_list, const char *drv_name)
|
---|
| 596 | {
|
---|
[729fa2d6] | 597 | driver_t *res = NULL;
|
---|
[58b833c] | 598 | driver_t *drv = NULL;
|
---|
[729fa2d6] | 599 |
|
---|
[38b3baf] | 600 | fibril_mutex_lock(&drv_list->drivers_mutex);
|
---|
[729fa2d6] | 601 |
|
---|
[b72efe8] | 602 | list_foreach(drv_list->drivers, link) {
|
---|
[729fa2d6] | 603 | drv = list_get_instance(link, driver_t, drivers);
|
---|
[58b833c] | 604 | if (str_cmp(drv->name, drv_name) == 0) {
|
---|
[729fa2d6] | 605 | res = drv;
|
---|
| 606 | break;
|
---|
[58b833c] | 607 | }
|
---|
| 608 | }
|
---|
[729fa2d6] | 609 |
|
---|
| 610 | fibril_mutex_unlock(&drv_list->drivers_mutex);
|
---|
| 611 |
|
---|
| 612 | return res;
|
---|
| 613 | }
|
---|
| 614 |
|
---|
[38b3baf] | 615 | /** Notify driver about the devices to which it was assigned.
|
---|
| 616 | *
|
---|
| 617 | * @param driver The driver to which the devices are passed.
|
---|
[c16cf62] | 618 | */
|
---|
[a32defa] | 619 | static void pass_devices_to_driver(driver_t *driver, dev_tree_t *tree)
|
---|
[38b3baf] | 620 | {
|
---|
[ba38f72c] | 621 | dev_node_t *dev;
|
---|
[c16cf62] | 622 | link_t *link;
|
---|
[58b833c] | 623 |
|
---|
[a1a101d] | 624 | log_msg(LOG_DEFAULT, LVL_DEBUG, "pass_devices_to_driver(driver=\"%s\")",
|
---|
[9b415c9] | 625 | driver->name);
|
---|
[58b833c] | 626 |
|
---|
[5bee897] | 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 | */
|
---|
[b72efe8] | 633 | link = driver->devices.head.next;
|
---|
| 634 | while (link != &driver->devices.head) {
|
---|
[ba38f72c] | 635 | dev = list_get_instance(link, dev_node_t, driver_devices);
|
---|
[58cbb0c8] | 636 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
| 637 |
|
---|
[5bee897] | 638 | if (dev->passed_to_driver) {
|
---|
[58cbb0c8] | 639 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
[084ff99] | 640 | link = link->next;
|
---|
[5bee897] | 641 | continue;
|
---|
[084ff99] | 642 | }
|
---|
[5bee897] | 643 |
|
---|
[a1a101d] | 644 | log_msg(LOG_DEFAULT, LVL_DEBUG, "pass_devices_to_driver: dev->refcnt=%d\n",
|
---|
[58cbb0c8] | 645 | (int)atomic_get(&dev->refcnt));
|
---|
| 646 | dev_add_ref(dev);
|
---|
[2edcb63] | 647 |
|
---|
[5bee897] | 648 | /*
|
---|
| 649 | * Unlock to avoid deadlock when adding device
|
---|
| 650 | * handled by itself.
|
---|
| 651 | */
|
---|
| 652 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
[58cbb0c8] | 653 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
[5bee897] | 654 |
|
---|
[45059d6b] | 655 | add_device(driver, dev, tree);
|
---|
[5bee897] | 656 |
|
---|
[58cbb0c8] | 657 | dev_del_ref(dev);
|
---|
| 658 |
|
---|
[5bee897] | 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 | */
|
---|
[b72efe8] | 668 | link = driver->devices.head.next;
|
---|
[084ff99] | 669 | }
|
---|
[5bee897] | 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 | */
|
---|
[a1a101d] | 680 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Driver `%s' enters running state.", driver->name);
|
---|
[5bee897] | 681 | driver->state = DRIVER_RUNNING;
|
---|
| 682 |
|
---|
| 683 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
[c16cf62] | 684 | }
|
---|
| 685 |
|
---|
[38b3baf] | 686 | /** Finish the initialization of a driver after it has succesfully started
|
---|
[bda60d9] | 687 | * and after it has registered itself by the device manager.
|
---|
[38b3baf] | 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.
|
---|
[c16cf62] | 694 | */
|
---|
[38b3baf] | 695 | void initialize_running_driver(driver_t *driver, dev_tree_t *tree)
|
---|
| 696 | {
|
---|
[a1a101d] | 697 | log_msg(LOG_DEFAULT, LVL_DEBUG, "initialize_running_driver(driver=\"%s\")",
|
---|
[9b415c9] | 698 | driver->name);
|
---|
[c16cf62] | 699 |
|
---|
[38b3baf] | 700 | /*
|
---|
| 701 | * Pass devices which have been already assigned to the driver to the
|
---|
| 702 | * driver.
|
---|
| 703 | */
|
---|
| 704 | pass_devices_to_driver(driver, tree);
|
---|
[c16cf62] | 705 | }
|
---|
| 706 |
|
---|
[791f58c] | 707 | /** Initialize device driver structure.
|
---|
| 708 | *
|
---|
| 709 | * @param drv The device driver structure.
|
---|
| 710 | */
|
---|
| 711 | void 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);
|
---|
[79ae36dd] | 719 | drv->sess = NULL;
|
---|
[791f58c] | 720 | }
|
---|
| 721 |
|
---|
| 722 | /** Device driver structure clean-up.
|
---|
| 723 | *
|
---|
| 724 | * @param drv The device driver structure.
|
---|
| 725 | */
|
---|
| 726 | void 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 | */
|
---|
| 742 | void delete_driver(driver_t *drv)
|
---|
| 743 | {
|
---|
| 744 | assert(drv != NULL);
|
---|
| 745 |
|
---|
| 746 | clean_driver(drv);
|
---|
| 747 | free(drv);
|
---|
| 748 | }
|
---|
[a32defa] | 749 |
|
---|
[15f3c3f] | 750 | /** Create loc path and name for the function. */
|
---|
| 751 | void loc_register_tree_function(fun_node_t *fun, dev_tree_t *tree)
|
---|
[a32defa] | 752 | {
|
---|
[15f3c3f] | 753 | char *loc_pathname = NULL;
|
---|
| 754 | char *loc_name = NULL;
|
---|
[a32defa] | 755 |
|
---|
[58cbb0c8] | 756 | assert(fibril_rwlock_is_locked(&tree->rwlock));
|
---|
| 757 |
|
---|
[15f3c3f] | 758 | asprintf(&loc_name, "%s", fun->pathname);
|
---|
| 759 | if (loc_name == NULL)
|
---|
[a32defa] | 760 | return;
|
---|
| 761 |
|
---|
[15f3c3f] | 762 | replace_char(loc_name, '/', LOC_SEPARATOR);
|
---|
[a32defa] | 763 |
|
---|
[15f3c3f] | 764 | asprintf(&loc_pathname, "%s/%s", LOC_DEVICE_NAMESPACE,
|
---|
| 765 | loc_name);
|
---|
| 766 | if (loc_pathname == NULL) {
|
---|
| 767 | free(loc_name);
|
---|
[a32defa] | 768 | return;
|
---|
[38b3baf] | 769 | }
|
---|
[a32defa] | 770 |
|
---|
[15f3c3f] | 771 | loc_service_register_with_iface(loc_pathname,
|
---|
| 772 | &fun->service_id, DEVMAN_CONNECT_FROM_LOC);
|
---|
[a32defa] | 773 |
|
---|
[15f3c3f] | 774 | tree_add_loc_function(tree, fun);
|
---|
[a32defa] | 775 |
|
---|
[15f3c3f] | 776 | free(loc_name);
|
---|
| 777 | free(loc_pathname);
|
---|
[a32defa] | 778 | }
|
---|
| 779 |
|
---|
[0c3666d] | 780 | /** Pass a device to running driver.
|
---|
[38b3baf] | 781 | *
|
---|
| 782 | * @param drv The driver's structure.
|
---|
| 783 | * @param node The device's node in the device tree.
|
---|
[0c3666d] | 784 | */
|
---|
[45059d6b] | 785 | void add_device(driver_t *drv, dev_node_t *dev, dev_tree_t *tree)
|
---|
[85e48a9] | 786 | {
|
---|
[5bee897] | 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 | */
|
---|
[a1a101d] | 791 | log_msg(LOG_DEFAULT, LVL_DEBUG, "add_device(drv=\"%s\", dev=\"%s\")",
|
---|
[9b415c9] | 792 | drv->name, dev->pfun->name);
|
---|
[a78fa2a] | 793 |
|
---|
[38b3baf] | 794 | /* Send the device to the driver. */
|
---|
[0d6915f] | 795 | devman_handle_t parent_handle;
|
---|
[ba38f72c] | 796 | if (dev->pfun) {
|
---|
| 797 | parent_handle = dev->pfun->handle;
|
---|
[0d6915f] | 798 | } else {
|
---|
| 799 | parent_handle = 0;
|
---|
| 800 | }
|
---|
[79ae36dd] | 801 |
|
---|
[45059d6b] | 802 | async_exch_t *exch = async_exchange_begin(drv->sess);
|
---|
[79ae36dd] | 803 |
|
---|
| 804 | ipc_call_t answer;
|
---|
[1a5b252] | 805 | aid_t req = async_send_2(exch, DRIVER_DEV_ADD, dev->handle,
|
---|
[0d6915f] | 806 | parent_handle, &answer);
|
---|
[a78fa2a] | 807 |
|
---|
[79ae36dd] | 808 | /* Send the device name to the driver. */
|
---|
| 809 | sysarg_t rc = async_data_write_start(exch, dev->pfun->name,
|
---|
[ba38f72c] | 810 | str_size(dev->pfun->name) + 1);
|
---|
[79ae36dd] | 811 |
|
---|
| 812 | async_exchange_end(exch);
|
---|
| 813 |
|
---|
[38b3baf] | 814 | if (rc != EOK) {
|
---|
| 815 | /* TODO handle error */
|
---|
| 816 | }
|
---|
[398c4d7] | 817 |
|
---|
[38b3baf] | 818 | /* Wait for answer from the driver. */
|
---|
[a78fa2a] | 819 | async_wait_for(req, &rc);
|
---|
[5bee897] | 820 |
|
---|
[a78fa2a] | 821 | switch(rc) {
|
---|
| 822 | case EOK:
|
---|
[ba38f72c] | 823 | dev->state = DEVICE_USABLE;
|
---|
[df747b9c] | 824 | break;
|
---|
[a78fa2a] | 825 | case ENOENT:
|
---|
[ba38f72c] | 826 | dev->state = DEVICE_NOT_PRESENT;
|
---|
[a78fa2a] | 827 | break;
|
---|
[df747b9c] | 828 | default:
|
---|
[ba38f72c] | 829 | dev->state = DEVICE_INVALID;
|
---|
[77a69ea] | 830 | break;
|
---|
[084ff99] | 831 | }
|
---|
[e85920d] | 832 |
|
---|
[ba38f72c] | 833 | dev->passed_to_driver = true;
|
---|
[5bee897] | 834 |
|
---|
[5cd136ab] | 835 | return;
|
---|
[85e48a9] | 836 | }
|
---|
| 837 |
|
---|
[38b3baf] | 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.
|
---|
[0c3666d] | 844 | */
|
---|
[ba38f72c] | 845 | bool assign_driver(dev_node_t *dev, driver_list_t *drivers_list,
|
---|
| 846 | dev_tree_t *tree)
|
---|
[85e48a9] | 847 | {
|
---|
[ba38f72c] | 848 | assert(dev != NULL);
|
---|
| 849 | assert(drivers_list != NULL);
|
---|
| 850 | assert(tree != NULL);
|
---|
| 851 |
|
---|
[38b3baf] | 852 | /*
|
---|
| 853 | * Find the driver which is the most suitable for handling this device.
|
---|
| 854 | */
|
---|
[ba38f72c] | 855 | driver_t *drv = find_best_match_driver(drivers_list, dev);
|
---|
[58b833c] | 856 | if (drv == NULL) {
|
---|
[a1a101d] | 857 | log_msg(LOG_DEFAULT, LVL_ERROR, "No driver found for device `%s'.",
|
---|
[ba38f72c] | 858 | dev->pfun->pathname);
|
---|
[38b3baf] | 859 | return false;
|
---|
[85e48a9] | 860 | }
|
---|
| 861 |
|
---|
[38b3baf] | 862 | /* Attach the driver to the device. */
|
---|
[58cbb0c8] | 863 | attach_driver(tree, dev, drv);
|
---|
[85e48a9] | 864 |
|
---|
[398c4d7] | 865 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
[58b833c] | 866 | if (drv->state == DRIVER_NOT_STARTED) {
|
---|
[38b3baf] | 867 | /* Start the driver. */
|
---|
[85e48a9] | 868 | start_driver(drv);
|
---|
[38b3baf] | 869 | }
|
---|
[398c4d7] | 870 | bool is_running = drv->state == DRIVER_RUNNING;
|
---|
| 871 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
| 872 |
|
---|
[45059d6b] | 873 | /* Notify the driver about the new device. */
|
---|
| 874 | if (is_running)
|
---|
| 875 | add_device(drv, dev, tree);
|
---|
[85e48a9] | 876 |
|
---|
[c1a0488] | 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);
|
---|
[85e48a9] | 885 | return true;
|
---|
| 886 | }
|
---|
| 887 |
|
---|
[58cbb0c8] | 888 | int driver_dev_remove(dev_tree_t *tree, dev_node_t *dev)
|
---|
[1a5b252] | 889 | {
|
---|
| 890 | async_exch_t *exch;
|
---|
| 891 | sysarg_t retval;
|
---|
| 892 | driver_t *drv;
|
---|
[58cbb0c8] | 893 | devman_handle_t handle;
|
---|
[1a5b252] | 894 |
|
---|
| 895 | assert(dev != NULL);
|
---|
[58cbb0c8] | 896 |
|
---|
[a1a101d] | 897 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_remove(%p)", dev);
|
---|
[58cbb0c8] | 898 |
|
---|
| 899 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
[1a5b252] | 900 | drv = dev->drv;
|
---|
[58cbb0c8] | 901 | handle = dev->handle;
|
---|
| 902 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
[1a5b252] | 903 |
|
---|
| 904 | exch = async_exchange_begin(drv->sess);
|
---|
[58cbb0c8] | 905 | retval = async_req_1_0(exch, DRIVER_DEV_REMOVE, handle);
|
---|
[1a5b252] | 906 | async_exchange_end(exch);
|
---|
| 907 |
|
---|
| 908 | return retval;
|
---|
[80a96d2] | 909 | }
|
---|
[1a5b252] | 910 |
|
---|
[80a96d2] | 911 | int 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 |
|
---|
[a1a101d] | 920 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_gone(%p)", dev);
|
---|
[80a96d2] | 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;
|
---|
[1a5b252] | 932 | }
|
---|
| 933 |
|
---|
[58cbb0c8] | 934 | int driver_fun_online(dev_tree_t *tree, fun_node_t *fun)
|
---|
[1a5b252] | 935 | {
|
---|
| 936 | async_exch_t *exch;
|
---|
| 937 | sysarg_t retval;
|
---|
| 938 | driver_t *drv;
|
---|
[58cbb0c8] | 939 | devman_handle_t handle;
|
---|
[1a5b252] | 940 |
|
---|
[a1a101d] | 941 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_fun_online(%p)", fun);
|
---|
[58cbb0c8] | 942 |
|
---|
| 943 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
| 944 |
|
---|
[1a5b252] | 945 | if (fun->dev == NULL) {
|
---|
| 946 | /* XXX root function? */
|
---|
[58cbb0c8] | 947 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
[1a5b252] | 948 | return EINVAL;
|
---|
| 949 | }
|
---|
| 950 |
|
---|
| 951 | drv = fun->dev->drv;
|
---|
[58cbb0c8] | 952 | handle = fun->handle;
|
---|
| 953 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
[1a5b252] | 954 |
|
---|
| 955 | exch = async_exchange_begin(drv->sess);
|
---|
[58cbb0c8] | 956 | retval = async_req_1_0(exch, DRIVER_FUN_ONLINE, handle);
|
---|
[1a5b252] | 957 | loc_exchange_end(exch);
|
---|
| 958 |
|
---|
| 959 | return retval;
|
---|
| 960 | }
|
---|
| 961 |
|
---|
[58cbb0c8] | 962 | int driver_fun_offline(dev_tree_t *tree, fun_node_t *fun)
|
---|
[1a5b252] | 963 | {
|
---|
| 964 | async_exch_t *exch;
|
---|
| 965 | sysarg_t retval;
|
---|
| 966 | driver_t *drv;
|
---|
[58cbb0c8] | 967 | devman_handle_t handle;
|
---|
[1a5b252] | 968 |
|
---|
[a1a101d] | 969 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_fun_offline(%p)", fun);
|
---|
[58cbb0c8] | 970 |
|
---|
| 971 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
[1a5b252] | 972 | if (fun->dev == NULL) {
|
---|
| 973 | /* XXX root function? */
|
---|
[58cbb0c8] | 974 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
[1a5b252] | 975 | return EINVAL;
|
---|
| 976 | }
|
---|
| 977 |
|
---|
| 978 | drv = fun->dev->drv;
|
---|
[58cbb0c8] | 979 | handle = fun->handle;
|
---|
| 980 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
[1a5b252] | 981 |
|
---|
| 982 | exch = async_exchange_begin(drv->sess);
|
---|
[58cbb0c8] | 983 | retval = async_req_1_0(exch, DRIVER_FUN_OFFLINE, handle);
|
---|
[1a5b252] | 984 | loc_exchange_end(exch);
|
---|
| 985 |
|
---|
| 986 | return retval;
|
---|
| 987 |
|
---|
| 988 | }
|
---|
| 989 |
|
---|
[38b3baf] | 990 | /** Initialize the device tree.
|
---|
| 991 | *
|
---|
[0c3666d] | 992 | * Create root device node of the tree and assign driver to it.
|
---|
[38b3baf] | 993 | *
|
---|
| 994 | * @param tree The device tree.
|
---|
| 995 | * @param drivers_list the list of available drivers.
|
---|
| 996 | * @return True on success, false otherwise.
|
---|
[0c3666d] | 997 | */
|
---|
| 998 | bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list)
|
---|
[85e48a9] | 999 | {
|
---|
[a1a101d] | 1000 | log_msg(LOG_DEFAULT, LVL_DEBUG, "init_device_tree()");
|
---|
[0c3666d] | 1001 |
|
---|
[957cfa58] | 1002 | tree->current_handle = 0;
|
---|
| 1003 |
|
---|
[062d900] | 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);
|
---|
[bda60d9] | 1007 |
|
---|
[957cfa58] | 1008 | fibril_rwlock_initialize(&tree->rwlock);
|
---|
[084ff99] | 1009 |
|
---|
[ba38f72c] | 1010 | /* Create root function and root device and add them to the device tree. */
|
---|
| 1011 | if (!create_root_nodes(tree))
|
---|
[85e48a9] | 1012 | return false;
|
---|
[58cbb0c8] | 1013 |
|
---|
[38b3baf] | 1014 | /* Find suitable driver and start it. */
|
---|
[58cbb0c8] | 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;
|
---|
[e4c4247] | 1021 | }
|
---|
| 1022 |
|
---|
[791f58c] | 1023 | /* Device nodes */
|
---|
| 1024 |
|
---|
| 1025 | /** Create a new device node.
|
---|
| 1026 | *
|
---|
| 1027 | * @return A device node structure.
|
---|
| 1028 | */
|
---|
[ba38f72c] | 1029 | dev_node_t *create_dev_node(void)
|
---|
[791f58c] | 1030 | {
|
---|
[58cbb0c8] | 1031 | dev_node_t *dev;
|
---|
[791f58c] | 1032 |
|
---|
[58cbb0c8] | 1033 | dev = calloc(1, sizeof(dev_node_t));
|
---|
| 1034 | if (dev == NULL)
|
---|
| 1035 | return NULL;
|
---|
[791f58c] | 1036 |
|
---|
[58cbb0c8] | 1037 | atomic_set(&dev->refcnt, 0);
|
---|
| 1038 | list_initialize(&dev->functions);
|
---|
| 1039 | link_initialize(&dev->driver_devices);
|
---|
| 1040 |
|
---|
| 1041 | return dev;
|
---|
[791f58c] | 1042 | }
|
---|
| 1043 |
|
---|
| 1044 | /** Delete a device node.
|
---|
| 1045 | *
|
---|
| 1046 | * @param node The device node structure.
|
---|
| 1047 | */
|
---|
[ba38f72c] | 1048 | void delete_dev_node(dev_node_t *dev)
|
---|
[791f58c] | 1049 | {
|
---|
[ba38f72c] | 1050 | assert(list_empty(&dev->functions));
|
---|
| 1051 | assert(dev->pfun == NULL);
|
---|
| 1052 | assert(dev->drv == NULL);
|
---|
| 1053 |
|
---|
| 1054 | free(dev);
|
---|
[791f58c] | 1055 | }
|
---|
| 1056 |
|
---|
[58cbb0c8] | 1057 | /** Increase device node reference count.
|
---|
| 1058 | *
|
---|
| 1059 | * @param dev Device node
|
---|
| 1060 | */
|
---|
| 1061 | void 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 | */
|
---|
| 1072 | void dev_del_ref(dev_node_t *dev)
|
---|
| 1073 | {
|
---|
| 1074 | if (atomic_predec(&dev->refcnt) == 0)
|
---|
| 1075 | delete_dev_node(dev);
|
---|
| 1076 | }
|
---|
| 1077 |
|
---|
[791f58c] | 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 | */
|
---|
[ba38f72c] | 1084 | dev_node_t *find_dev_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
|
---|
[791f58c] | 1085 | {
|
---|
[01b87dc5] | 1086 | assert(fibril_rwlock_is_locked(&tree->rwlock));
|
---|
| 1087 |
|
---|
[062d900] | 1088 | ht_link_t *link = hash_table_find(&tree->devman_devices, &handle);
|
---|
[f98bec0f] | 1089 | if (link == NULL)
|
---|
| 1090 | return NULL;
|
---|
| 1091 |
|
---|
[062d900] | 1092 | return hash_table_get_inst(link, dev_node_t, devman_dev);
|
---|
[791f58c] | 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 | */
|
---|
[ba38f72c] | 1101 | dev_node_t *find_dev_node(dev_tree_t *tree, devman_handle_t handle)
|
---|
[791f58c] | 1102 | {
|
---|
[ba38f72c] | 1103 | dev_node_t *dev = NULL;
|
---|
[791f58c] | 1104 |
|
---|
| 1105 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
[ba38f72c] | 1106 | dev = find_dev_node_no_lock(tree, handle);
|
---|
[58cbb0c8] | 1107 | if (dev != NULL)
|
---|
| 1108 | dev_add_ref(dev);
|
---|
| 1109 |
|
---|
[791f58c] | 1110 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
| 1111 |
|
---|
[ba38f72c] | 1112 | return dev;
|
---|
| 1113 | }
|
---|
| 1114 |
|
---|
[7beb220] | 1115 | /** Get list of device functions. */
|
---|
| 1116 | int 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 |
|
---|
[58cbb0c8] | 1137 | if (pos < buf_cnt) {
|
---|
[7beb220] | 1138 | hdl_buf[pos] = fun->handle;
|
---|
[58cbb0c8] | 1139 | }
|
---|
| 1140 |
|
---|
[7beb220] | 1141 | pos++;
|
---|
| 1142 | }
|
---|
| 1143 |
|
---|
| 1144 | return EOK;
|
---|
| 1145 | }
|
---|
| 1146 |
|
---|
| 1147 |
|
---|
[ba38f72c] | 1148 | /* Function nodes */
|
---|
| 1149 |
|
---|
| 1150 | /** Create a new function node.
|
---|
| 1151 | *
|
---|
| 1152 | * @return A function node structure.
|
---|
| 1153 | */
|
---|
| 1154 | fun_node_t *create_fun_node(void)
|
---|
| 1155 | {
|
---|
[58cbb0c8] | 1156 | fun_node_t *fun;
|
---|
| 1157 |
|
---|
| 1158 | fun = calloc(1, sizeof(fun_node_t));
|
---|
| 1159 | if (fun == NULL)
|
---|
| 1160 | return NULL;
|
---|
[ba38f72c] | 1161 |
|
---|
[c1a0488] | 1162 | fun->state = FUN_INIT;
|
---|
[58cbb0c8] | 1163 | atomic_set(&fun->refcnt, 0);
|
---|
[4820360] | 1164 | fibril_mutex_initialize(&fun->busy_lock);
|
---|
[58cbb0c8] | 1165 | link_initialize(&fun->dev_functions);
|
---|
| 1166 | list_initialize(&fun->match_ids.ids);
|
---|
| 1167 |
|
---|
| 1168 | return fun;
|
---|
[ba38f72c] | 1169 | }
|
---|
| 1170 |
|
---|
| 1171 | /** Delete a function node.
|
---|
| 1172 | *
|
---|
| 1173 | * @param fun The device node structure.
|
---|
| 1174 | */
|
---|
| 1175 | void 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 |
|
---|
[58cbb0c8] | 1186 | /** Increase function node reference count.
|
---|
| 1187 | *
|
---|
| 1188 | * @param fun Function node
|
---|
| 1189 | */
|
---|
| 1190 | void 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 | */
|
---|
| 1201 | void fun_del_ref(fun_node_t *fun)
|
---|
| 1202 | {
|
---|
| 1203 | if (atomic_predec(&fun->refcnt) == 0)
|
---|
| 1204 | delete_fun_node(fun);
|
---|
| 1205 | }
|
---|
| 1206 |
|
---|
[4820360] | 1207 | /** Make function busy for reconfiguration operations. */
|
---|
| 1208 | void fun_busy_lock(fun_node_t *fun)
|
---|
| 1209 | {
|
---|
| 1210 | fibril_mutex_lock(&fun->busy_lock);
|
---|
| 1211 | }
|
---|
| 1212 |
|
---|
| 1213 | /** Mark end of reconfiguration operation. */
|
---|
| 1214 | void fun_busy_unlock(fun_node_t *fun)
|
---|
| 1215 | {
|
---|
| 1216 | fibril_mutex_unlock(&fun->busy_lock);
|
---|
| 1217 | }
|
---|
| 1218 |
|
---|
[ba38f72c] | 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 | */
|
---|
| 1225 | fun_node_t *find_fun_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
|
---|
| 1226 | {
|
---|
[58cbb0c8] | 1227 | fun_node_t *fun;
|
---|
[ba38f72c] | 1228 |
|
---|
| 1229 | assert(fibril_rwlock_is_locked(&tree->rwlock));
|
---|
| 1230 |
|
---|
[062d900] | 1231 | ht_link_t *link = hash_table_find(&tree->devman_functions, &handle);
|
---|
[ba38f72c] | 1232 | if (link == NULL)
|
---|
| 1233 | return NULL;
|
---|
| 1234 |
|
---|
[062d900] | 1235 | fun = hash_table_get_inst(link, fun_node_t, devman_fun);
|
---|
[58cbb0c8] | 1236 |
|
---|
| 1237 | return fun;
|
---|
[791f58c] | 1238 | }
|
---|
| 1239 |
|
---|
[ba38f72c] | 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 | */
|
---|
| 1246 | fun_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);
|
---|
[58cbb0c8] | 1251 |
|
---|
[ba38f72c] | 1252 | fun = find_fun_node_no_lock(tree, handle);
|
---|
[58cbb0c8] | 1253 | if (fun != NULL)
|
---|
| 1254 | fun_add_ref(fun);
|
---|
| 1255 |
|
---|
[ba38f72c] | 1256 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
| 1257 |
|
---|
| 1258 | return fun;
|
---|
| 1259 | }
|
---|
[791f58c] | 1260 |
|
---|
[bda60d9] | 1261 | /** Create and set device's full path in device tree.
|
---|
[38b3baf] | 1262 | *
|
---|
[58cbb0c8] | 1263 | * @param tree Device tree
|
---|
[38b3baf] | 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.).
|
---|
[bda60d9] | 1268 | */
|
---|
[58cbb0c8] | 1269 | static bool set_fun_path(dev_tree_t *tree, fun_node_t *fun, fun_node_t *parent)
|
---|
[38b3baf] | 1270 | {
|
---|
[58cbb0c8] | 1271 | assert(fibril_rwlock_is_write_locked(&tree->rwlock));
|
---|
[ba38f72c] | 1272 | assert(fun->name != NULL);
|
---|
[bda60d9] | 1273 |
|
---|
[ba38f72c] | 1274 | size_t pathsize = (str_size(fun->name) + 1);
|
---|
[58b833c] | 1275 | if (parent != NULL)
|
---|
[38b3baf] | 1276 | pathsize += str_size(parent->pathname) + 1;
|
---|
[bda60d9] | 1277 |
|
---|
[ba38f72c] | 1278 | fun->pathname = (char *) malloc(pathsize);
|
---|
| 1279 | if (fun->pathname == NULL) {
|
---|
[a1a101d] | 1280 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to allocate device path.");
|
---|
[bda60d9] | 1281 | return false;
|
---|
| 1282 | }
|
---|
| 1283 |
|
---|
[58b833c] | 1284 | if (parent != NULL) {
|
---|
[ba38f72c] | 1285 | str_cpy(fun->pathname, pathsize, parent->pathname);
|
---|
| 1286 | str_append(fun->pathname, pathsize, "/");
|
---|
| 1287 | str_append(fun->pathname, pathsize, fun->name);
|
---|
[bda60d9] | 1288 | } else {
|
---|
[ba38f72c] | 1289 | str_cpy(fun->pathname, pathsize, fun->name);
|
---|
[bda60d9] | 1290 | }
|
---|
| 1291 |
|
---|
| 1292 | return true;
|
---|
| 1293 | }
|
---|
| 1294 |
|
---|
| 1295 | /** Insert new device into device tree.
|
---|
[38b3baf] | 1296 | *
|
---|
| 1297 | * @param tree The device tree.
|
---|
[58cbb0c8] | 1298 | * @param dev The newly added device node.
|
---|
| 1299 | * @param pfun The parent function node.
|
---|
[58b833c] | 1300 | *
|
---|
[38b3baf] | 1301 | * @return True on success, false otherwise (insufficient resources
|
---|
| 1302 | * etc.).
|
---|
[bda60d9] | 1303 | */
|
---|
[ba38f72c] | 1304 | bool 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 |
|
---|
[a1a101d] | 1308 | log_msg(LOG_DEFAULT, LVL_DEBUG, "insert_dev_node(dev=%p, pfun=%p [\"%s\"])",
|
---|
[9b415c9] | 1309 | dev, pfun, pfun->pathname);
|
---|
| 1310 |
|
---|
[ba38f72c] | 1311 | /* Add the node to the handle-to-node map. */
|
---|
| 1312 | dev->handle = ++tree->current_handle;
|
---|
[062d900] | 1313 | hash_table_insert(&tree->devman_devices, &dev->devman_dev);
|
---|
[ba38f72c] | 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 |
|
---|
[1a5b252] | 1322 | /** Remove device from device tree.
|
---|
| 1323 | *
|
---|
| 1324 | * @param tree Device tree
|
---|
| 1325 | * @param dev Device node
|
---|
| 1326 | */
|
---|
| 1327 | void remove_dev_node(dev_tree_t *tree, dev_node_t *dev)
|
---|
| 1328 | {
|
---|
| 1329 | assert(fibril_rwlock_is_write_locked(&tree->rwlock));
|
---|
| 1330 |
|
---|
[a1a101d] | 1331 | log_msg(LOG_DEFAULT, LVL_DEBUG, "remove_dev_node(dev=%p)", dev);
|
---|
[1a5b252] | 1332 |
|
---|
| 1333 | /* Remove node from the handle-to-node map. */
|
---|
[062d900] | 1334 | hash_table_remove(&tree->devman_devices, &dev->handle);
|
---|
[1a5b252] | 1335 |
|
---|
| 1336 | /* Unlink from parent function. */
|
---|
| 1337 | dev->pfun->child = NULL;
|
---|
| 1338 | dev->pfun = NULL;
|
---|
[c1a0488] | 1339 |
|
---|
| 1340 | dev->state = DEVICE_REMOVED;
|
---|
[1a5b252] | 1341 | }
|
---|
| 1342 |
|
---|
| 1343 |
|
---|
[ba38f72c] | 1344 | /** Insert new function into device tree.
|
---|
| 1345 | *
|
---|
| 1346 | * @param tree The device tree.
|
---|
[58cbb0c8] | 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.
|
---|
[ba38f72c] | 1350 | *
|
---|
| 1351 | * @return True on success, false otherwise (insufficient resources
|
---|
| 1352 | * etc.).
|
---|
| 1353 | */
|
---|
| 1354 | bool insert_fun_node(dev_tree_t *tree, fun_node_t *fun, char *fun_name,
|
---|
| 1355 | dev_node_t *dev)
|
---|
[bda60d9] | 1356 | {
|
---|
[ba38f72c] | 1357 | fun_node_t *pfun;
|
---|
| 1358 |
|
---|
| 1359 | assert(fun_name != NULL);
|
---|
[01b87dc5] | 1360 | assert(fibril_rwlock_is_write_locked(&tree->rwlock));
|
---|
[bda60d9] | 1361 |
|
---|
[ba38f72c] | 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;
|
---|
[58cbb0c8] | 1369 | if (!set_fun_path(tree, fun, pfun)) {
|
---|
[38b3baf] | 1370 | return false;
|
---|
[bda60d9] | 1371 | }
|
---|
| 1372 |
|
---|
[38b3baf] | 1373 | /* Add the node to the handle-to-node map. */
|
---|
[ba38f72c] | 1374 | fun->handle = ++tree->current_handle;
|
---|
[062d900] | 1375 | hash_table_insert(&tree->devman_functions, &fun->devman_fun);
|
---|
[bda60d9] | 1376 |
|
---|
[38b3baf] | 1377 | /* Add the node to the list of its parent's children. */
|
---|
[ba38f72c] | 1378 | fun->dev = dev;
|
---|
| 1379 | if (dev != NULL)
|
---|
| 1380 | list_append(&fun->dev_functions, &dev->functions);
|
---|
[38b3baf] | 1381 |
|
---|
[bda60d9] | 1382 | return true;
|
---|
| 1383 | }
|
---|
| 1384 |
|
---|
[d0dd7b5] | 1385 | /** Remove function from device tree.
|
---|
| 1386 | *
|
---|
| 1387 | * @param tree Device tree
|
---|
| 1388 | * @param node Function node to remove
|
---|
| 1389 | */
|
---|
| 1390 | void 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. */
|
---|
[062d900] | 1395 | hash_table_remove(&tree->devman_functions, &fun->handle);
|
---|
[d0dd7b5] | 1396 |
|
---|
| 1397 | /* Remove the node from the list of its parent's children. */
|
---|
| 1398 | if (fun->dev != NULL)
|
---|
| 1399 | list_remove(&fun->dev_functions);
|
---|
[1a5b252] | 1400 |
|
---|
| 1401 | fun->dev = NULL;
|
---|
[c1a0488] | 1402 | fun->state = FUN_REMOVED;
|
---|
[d0dd7b5] | 1403 | }
|
---|
| 1404 |
|
---|
[ba38f72c] | 1405 | /** Find function node with a specified path in the device tree.
|
---|
[5cd136ab] | 1406 | *
|
---|
[ba38f72c] | 1407 | * @param path The path of the function node in the device tree.
|
---|
[38b3baf] | 1408 | * @param tree The device tree.
|
---|
[ba38f72c] | 1409 | * @return The function node if it is present in the tree, NULL
|
---|
[38b3baf] | 1410 | * otherwise.
|
---|
[5cd136ab] | 1411 | */
|
---|
[ba38f72c] | 1412 | fun_node_t *find_fun_node_by_path(dev_tree_t *tree, char *path)
|
---|
[5cd136ab] | 1413 | {
|
---|
[df147c7] | 1414 | assert(path != NULL);
|
---|
| 1415 |
|
---|
| 1416 | bool is_absolute = path[0] == '/';
|
---|
| 1417 | if (!is_absolute) {
|
---|
| 1418 | return NULL;
|
---|
| 1419 | }
|
---|
| 1420 |
|
---|
[957cfa58] | 1421 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
| 1422 |
|
---|
[ba38f72c] | 1423 | fun_node_t *fun = tree->root_node;
|
---|
[58cbb0c8] | 1424 | fun_add_ref(fun);
|
---|
[38b3baf] | 1425 | /*
|
---|
[ba38f72c] | 1426 | * Relative path to the function from its parent (but with '/' at the
|
---|
[38b3baf] | 1427 | * beginning)
|
---|
| 1428 | */
|
---|
[5cd136ab] | 1429 | char *rel_path = path;
|
---|
| 1430 | char *next_path_elem = NULL;
|
---|
[7beb220] | 1431 | bool cont = (rel_path[1] != '\0');
|
---|
[5cd136ab] | 1432 |
|
---|
[ba38f72c] | 1433 | while (cont && fun != NULL) {
|
---|
[38b3baf] | 1434 | next_path_elem = get_path_elem_end(rel_path + 1);
|
---|
[58b833c] | 1435 | if (next_path_elem[0] == '/') {
|
---|
[5cd136ab] | 1436 | cont = true;
|
---|
| 1437 | next_path_elem[0] = 0;
|
---|
| 1438 | } else {
|
---|
| 1439 | cont = false;
|
---|
| 1440 | }
|
---|
| 1441 |
|
---|
[58cbb0c8] | 1442 | fun_node_t *cfun = find_node_child(tree, fun, rel_path + 1);
|
---|
| 1443 | fun_del_ref(fun);
|
---|
| 1444 | fun = cfun;
|
---|
[5cd136ab] | 1445 |
|
---|
| 1446 | if (cont) {
|
---|
[38b3baf] | 1447 | /* Restore the original path. */
|
---|
[5cd136ab] | 1448 | next_path_elem[0] = '/';
|
---|
| 1449 | }
|
---|
[38b3baf] | 1450 | rel_path = next_path_elem;
|
---|
[5cd136ab] | 1451 | }
|
---|
| 1452 |
|
---|
[957cfa58] | 1453 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
| 1454 |
|
---|
[ba38f72c] | 1455 | return fun;
|
---|
[5cd136ab] | 1456 | }
|
---|
| 1457 |
|
---|
[0876062] | 1458 | /** Find function with a specified name belonging to given device.
|
---|
[38b3baf] | 1459 | *
|
---|
| 1460 | * Device tree rwlock should be held at least for reading.
|
---|
| 1461 | *
|
---|
[58cbb0c8] | 1462 | * @param tree Device tree
|
---|
[0876062] | 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.
|
---|
[5cd136ab] | 1467 | */
|
---|
[58cbb0c8] | 1468 | fun_node_t *find_fun_node_in_device(dev_tree_t *tree, dev_node_t *dev,
|
---|
| 1469 | const char *name)
|
---|
[5cd136ab] | 1470 | {
|
---|
[0876062] | 1471 | assert(name != NULL);
|
---|
[58cbb0c8] | 1472 | assert(fibril_rwlock_is_locked(&tree->rwlock));
|
---|
[0876062] | 1473 |
|
---|
[ba38f72c] | 1474 | fun_node_t *fun;
|
---|
[0876062] | 1475 |
|
---|
[b72efe8] | 1476 | list_foreach(dev->functions, link) {
|
---|
[ba38f72c] | 1477 | fun = list_get_instance(link, fun_node_t, dev_functions);
|
---|
[0876062] | 1478 |
|
---|
[58cbb0c8] | 1479 | if (str_cmp(name, fun->name) == 0) {
|
---|
| 1480 | fun_add_ref(fun);
|
---|
[ba38f72c] | 1481 | return fun;
|
---|
[58cbb0c8] | 1482 | }
|
---|
[38b3baf] | 1483 | }
|
---|
[0876062] | 1484 |
|
---|
[5cd136ab] | 1485 | return NULL;
|
---|
| 1486 | }
|
---|
| 1487 |
|
---|
[0876062] | 1488 | /** Find child function node with a specified name.
|
---|
| 1489 | *
|
---|
| 1490 | * Device tree rwlock should be held at least for reading.
|
---|
| 1491 | *
|
---|
[58cbb0c8] | 1492 | * @param tree Device tree
|
---|
[0876062] | 1493 | * @param parent The parent function node.
|
---|
| 1494 | * @param name The name of the child function.
|
---|
| 1495 | * @return The child function node.
|
---|
| 1496 | */
|
---|
[58cbb0c8] | 1497 | static fun_node_t *find_node_child(dev_tree_t *tree, fun_node_t *pfun,
|
---|
| 1498 | const char *name)
|
---|
[0876062] | 1499 | {
|
---|
[58cbb0c8] | 1500 | return find_fun_node_in_device(tree, pfun->child, name);
|
---|
[0876062] | 1501 | }
|
---|
| 1502 |
|
---|
[15f3c3f] | 1503 | /* loc devices */
|
---|
[ce89036b] | 1504 |
|
---|
[15f3c3f] | 1505 | fun_node_t *find_loc_tree_function(dev_tree_t *tree, service_id_t service_id)
|
---|
[ce89036b] | 1506 | {
|
---|
[ba38f72c] | 1507 | fun_node_t *fun = NULL;
|
---|
[ce89036b] | 1508 |
|
---|
| 1509 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
[062d900] | 1510 | ht_link_t *link = hash_table_find(&tree->loc_functions, &service_id);
|
---|
[58cbb0c8] | 1511 | if (link != NULL) {
|
---|
[062d900] | 1512 | fun = hash_table_get_inst(link, fun_node_t, loc_fun);
|
---|
[58cbb0c8] | 1513 | fun_add_ref(fun);
|
---|
| 1514 | }
|
---|
[ce89036b] | 1515 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
| 1516 |
|
---|
[ba38f72c] | 1517 | return fun;
|
---|
[ce89036b] | 1518 | }
|
---|
| 1519 |
|
---|
[15f3c3f] | 1520 | void tree_add_loc_function(dev_tree_t *tree, fun_node_t *fun)
|
---|
[791f58c] | 1521 | {
|
---|
[58cbb0c8] | 1522 | assert(fibril_rwlock_is_write_locked(&tree->rwlock));
|
---|
| 1523 |
|
---|
[062d900] | 1524 | hash_table_insert(&tree->loc_functions, &fun->loc_fun);
|
---|
[791f58c] | 1525 | }
|
---|
| 1526 |
|
---|
[c16cf62] | 1527 | /** @}
|
---|
[58b833c] | 1528 | */
|
---|