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