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