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