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