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