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