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