[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>
|
---|
[0358da0] | 38 |
|
---|
[e2b9a993] | 39 | #include "devman.h"
|
---|
| 40 | #include "util.h"
|
---|
[0358da0] | 41 |
|
---|
[0c3666d] | 42 | /** Allocate and initialize a new driver structure.
|
---|
| 43 | *
|
---|
| 44 | * @return driver structure.
|
---|
| 45 | */
|
---|
[e2b9a993] | 46 | driver_t * create_driver()
|
---|
[92413de] | 47 | {
|
---|
[e4c4247] | 48 | driver_t *res = malloc(sizeof(driver_t));
|
---|
| 49 | if(res != NULL) {
|
---|
[08d9c4e6] | 50 | init_driver(res);
|
---|
[e4c4247] | 51 | }
|
---|
| 52 | return res;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[0c3666d] | 55 | /** Add a driver to the list of drivers.
|
---|
| 56 | *
|
---|
| 57 | * @param drivers_list the list of drivers.
|
---|
| 58 | * @param drv the driver's structure.
|
---|
| 59 | */
|
---|
| 60 | void add_driver(driver_list_t *drivers_list, driver_t *drv)
|
---|
| 61 | {
|
---|
| 62 | fibril_mutex_lock(&drivers_list->drivers_mutex);
|
---|
| 63 | list_prepend(&drv->drivers, &drivers_list->drivers);
|
---|
| 64 | fibril_mutex_unlock(&drivers_list->drivers_mutex);
|
---|
| 65 |
|
---|
| 66 | printf(NAME": the '%s' driver was added to the list of available drivers.\n", drv->name);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /** Read match id at the specified position of a string and set
|
---|
| 70 | * the position in the string to the first character following the id.
|
---|
| 71 | *
|
---|
| 72 | * @param buf the position in the input string.
|
---|
| 73 | *
|
---|
| 74 | * @return the match id.
|
---|
| 75 | */
|
---|
| 76 | char * read_match_id(const char **buf)
|
---|
[e4c4247] | 77 | {
|
---|
| 78 | char *res = NULL;
|
---|
[e2b9a993] | 79 | size_t len = get_nonspace_len(*buf);
|
---|
[e4c4247] | 80 | if (len > 0) {
|
---|
| 81 | res = malloc(len + 1);
|
---|
| 82 | if (res != NULL) {
|
---|
| 83 | str_ncpy(res, len + 1, *buf, len);
|
---|
| 84 | *buf += len;
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 | return res;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[0c3666d] | 90 | /**
|
---|
| 91 | * Read match ids and associated match scores from a string.
|
---|
| 92 | *
|
---|
| 93 | * Each match score in the string is followed by its match id.
|
---|
| 94 | * The match ids and match scores are separated by whitespaces.
|
---|
| 95 | * Neither match ids nor match scores can contain whitespaces.
|
---|
| 96 | *
|
---|
| 97 | * @param buf the string from which the match ids are read.
|
---|
| 98 | * @param ids the list of match ids into which the match ids and scores are added.
|
---|
| 99 | *
|
---|
| 100 | * @return true if at least one match id and associated match score was successfully read, false otherwise.
|
---|
| 101 | */
|
---|
[e2b9a993] | 102 | bool parse_match_ids(const char *buf, match_id_list_t *ids)
|
---|
[e4c4247] | 103 | {
|
---|
| 104 | int score = 0;
|
---|
| 105 | char *id = NULL;
|
---|
| 106 | int ids_read = 0;
|
---|
| 107 |
|
---|
| 108 | while (true) {
|
---|
| 109 | // skip spaces
|
---|
| 110 | if (!skip_spaces(&buf)) {
|
---|
| 111 | break;
|
---|
| 112 | }
|
---|
| 113 | // read score
|
---|
| 114 | score = strtoul(buf, &buf, 10);
|
---|
| 115 |
|
---|
| 116 | // skip spaces
|
---|
| 117 | if (!skip_spaces(&buf)) {
|
---|
| 118 | break;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | // read id
|
---|
[0c3666d] | 122 | if (NULL == (id = read_match_id(&buf))) {
|
---|
[e4c4247] | 123 | break;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | // create new match_id structure
|
---|
| 127 | match_id_t *mid = create_match_id();
|
---|
| 128 | mid->id = id;
|
---|
| 129 | mid->score = score;
|
---|
| 130 |
|
---|
| 131 | /// add it to the list
|
---|
| 132 | add_match_id(ids, mid);
|
---|
| 133 |
|
---|
| 134 | ids_read++;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | return ids_read > 0;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[0c3666d] | 140 | /**
|
---|
| 141 | * Read match ids and associated match scores from a file.
|
---|
| 142 | *
|
---|
| 143 | * Each match score in the file is followed by its match id.
|
---|
| 144 | * The match ids and match scores are separated by whitespaces.
|
---|
| 145 | * Neither match ids nor match scores can contain whitespaces.
|
---|
| 146 | *
|
---|
| 147 | * @param buf the path to the file from which the match ids are read.
|
---|
| 148 | * @param ids the list of match ids into which the match ids and scores are added.
|
---|
| 149 | *
|
---|
| 150 | * @return true if at least one match id and associated match score was successfully read, false otherwise.
|
---|
| 151 | */
|
---|
[e2b9a993] | 152 | bool read_match_ids(const char *conf_path, match_id_list_t *ids)
|
---|
[e4c4247] | 153 | {
|
---|
[08d9c4e6] | 154 | printf(NAME ": read_match_ids conf_path = %s.\n", conf_path);
|
---|
| 155 |
|
---|
[e4c4247] | 156 | bool suc = false;
|
---|
| 157 | char *buf = NULL;
|
---|
| 158 | bool opened = false;
|
---|
| 159 | int fd;
|
---|
| 160 | off_t len = 0;
|
---|
| 161 |
|
---|
| 162 | fd = open(conf_path, O_RDONLY);
|
---|
| 163 | if (fd < 0) {
|
---|
| 164 | printf(NAME ": unable to open %s\n", conf_path);
|
---|
| 165 | goto cleanup;
|
---|
| 166 | }
|
---|
| 167 | opened = true;
|
---|
| 168 |
|
---|
| 169 | len = lseek(fd, 0, SEEK_END);
|
---|
| 170 | lseek(fd, 0, SEEK_SET);
|
---|
| 171 | if (len == 0) {
|
---|
| 172 | printf(NAME ": configuration file '%s' is empty.\n", conf_path);
|
---|
| 173 | goto cleanup;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | buf = malloc(len + 1);
|
---|
| 177 | if (buf == NULL) {
|
---|
| 178 | printf(NAME ": memory allocation failed when parsing file '%s'.\n", conf_path);
|
---|
| 179 | goto cleanup;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | if (0 >= read(fd, buf, len)) {
|
---|
| 183 | printf(NAME ": unable to read file '%s'.\n", conf_path);
|
---|
| 184 | goto cleanup;
|
---|
| 185 | }
|
---|
| 186 | buf[len] = 0;
|
---|
| 187 |
|
---|
| 188 | suc = parse_match_ids(buf, ids);
|
---|
| 189 |
|
---|
| 190 | cleanup:
|
---|
| 191 |
|
---|
| 192 | free(buf);
|
---|
| 193 |
|
---|
| 194 | if(opened) {
|
---|
| 195 | close(fd);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | return suc;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[0c3666d] | 201 | /**
|
---|
| 202 | * Get information about a driver.
|
---|
| 203 | *
|
---|
| 204 | * Each driver has its own directory in the base directory.
|
---|
| 205 | * The name of the driver's directory is the same as the name of the driver.
|
---|
| 206 | * The driver's directory contains driver's binary (named as the driver without extension)
|
---|
| 207 | * and the configuration file with match ids for device-to-driver matching
|
---|
| 208 | * (named as the driver with a special extension).
|
---|
| 209 | *
|
---|
| 210 | * This function searches for the driver's directory and containing configuration files.
|
---|
| 211 | * If all the files needed are found, they are parsed and
|
---|
| 212 | * the information about the driver is stored to the driver's structure.
|
---|
| 213 | *
|
---|
| 214 | * @param base_path the base directory, in which we look for driver's subdirectory.
|
---|
| 215 | * @param name the name of the driver.
|
---|
| 216 | * @param drv the driver structure to fill information in.
|
---|
| 217 | *
|
---|
| 218 | * @return true on success, false otherwise.
|
---|
| 219 | */
|
---|
[e2b9a993] | 220 | bool get_driver_info(const char *base_path, const char *name, driver_t *drv)
|
---|
[e4c4247] | 221 | {
|
---|
[08d9c4e6] | 222 | printf(NAME ": get_driver_info base_path = %s, name = %s.\n", base_path, name);
|
---|
| 223 |
|
---|
[e4c4247] | 224 | assert(base_path != NULL && name != NULL && drv != NULL);
|
---|
| 225 |
|
---|
| 226 | bool suc = false;
|
---|
| 227 | char *match_path = NULL;
|
---|
| 228 | size_t name_size = 0;
|
---|
| 229 |
|
---|
| 230 | // read the list of match ids from the driver's configuration file
|
---|
| 231 | if (NULL == (match_path = get_abs_path(base_path, name, MATCH_EXT))) {
|
---|
| 232 | goto cleanup;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
[08d9c4e6] | 235 | printf(NAME ": get_driver_info - path to match id list = %s.\n", match_path);
|
---|
| 236 |
|
---|
[e4c4247] | 237 | if (!read_match_ids(match_path, &drv->match_ids)) {
|
---|
| 238 | goto cleanup;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | // allocate and fill driver's name
|
---|
| 242 | name_size = str_size(name)+1;
|
---|
| 243 | drv->name = malloc(name_size);
|
---|
| 244 | if (!drv->name) {
|
---|
| 245 | goto cleanup;
|
---|
| 246 | }
|
---|
| 247 | str_cpy(drv->name, name_size, name);
|
---|
| 248 |
|
---|
[85e48a9] | 249 | // initialize path with driver's binary
|
---|
| 250 | if (NULL == (drv->binary_path = get_abs_path(base_path, name, ""))) {
|
---|
| 251 | goto cleanup;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | // check whether the driver's binary exists
|
---|
| 255 | struct stat s;
|
---|
| 256 | if (stat(drv->binary_path, &s) == ENOENT) {
|
---|
| 257 | printf(NAME ": driver not found at path %s.", drv->binary_path);
|
---|
| 258 | goto cleanup;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
[e4c4247] | 261 | suc = true;
|
---|
| 262 |
|
---|
| 263 | cleanup:
|
---|
| 264 |
|
---|
| 265 | if (!suc) {
|
---|
| 266 | free(drv->binary_path);
|
---|
| 267 | free(drv->name);
|
---|
| 268 | // set the driver structure to the default state
|
---|
| 269 | init_driver(drv);
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | free(match_path);
|
---|
| 273 |
|
---|
| 274 | return suc;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | /** Lookup drivers in the directory.
|
---|
| 278 | *
|
---|
[e2b9a993] | 279 | * @param drivers_list the list of available drivers.
|
---|
| 280 | * @param dir_path the path to the directory where we search for drivers.
|
---|
[0c3666d] | 281 | *
|
---|
| 282 | * @return number of drivers which were found.
|
---|
[e4c4247] | 283 | */
|
---|
[0c3666d] | 284 | int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path)
|
---|
[e4c4247] | 285 | {
|
---|
[08d9c4e6] | 286 | printf(NAME ": lookup_available_drivers \n");
|
---|
| 287 |
|
---|
[e4c4247] | 288 | int drv_cnt = 0;
|
---|
| 289 | DIR *dir = NULL;
|
---|
| 290 | struct dirent *diren;
|
---|
| 291 |
|
---|
| 292 | dir = opendir(dir_path);
|
---|
[08d9c4e6] | 293 | printf(NAME ": lookup_available_drivers has opened directory %s for driver search.\n", dir_path);
|
---|
| 294 |
|
---|
[e4c4247] | 295 | if (dir != NULL) {
|
---|
| 296 | driver_t *drv = create_driver();
|
---|
[08d9c4e6] | 297 | printf(NAME ": lookup_available_drivers has created driver structure.\n");
|
---|
[e4c4247] | 298 | while ((diren = readdir(dir))) {
|
---|
| 299 | if (get_driver_info(dir_path, diren->d_name, drv)) {
|
---|
[e2b9a993] | 300 | add_driver(drivers_list, drv);
|
---|
[08d9c4e6] | 301 | drv_cnt++;
|
---|
[e4c4247] | 302 | drv = create_driver();
|
---|
| 303 | }
|
---|
| 304 | }
|
---|
| 305 | delete_driver(drv);
|
---|
| 306 | closedir(dir);
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | return drv_cnt;
|
---|
| 310 | }
|
---|
| 311 |
|
---|
[084ff99] | 312 | /** Create root device node in the device tree.
|
---|
[0c3666d] | 313 | *
|
---|
[084ff99] | 314 | * @param tree the device tree.
|
---|
| 315 | * @return true on success, false otherwise.
|
---|
[0c3666d] | 316 | */
|
---|
[084ff99] | 317 | bool create_root_node(dev_tree_t *tree)
|
---|
[e4c4247] | 318 | {
|
---|
[e85920d] | 319 | printf(NAME ": create_root_node\n");
|
---|
[e4c4247] | 320 | node_t *node = create_dev_node();
|
---|
[85e48a9] | 321 | if (node) {
|
---|
[bda60d9] | 322 | insert_dev_node(tree, node, "", NULL);
|
---|
[85e48a9] | 323 | match_id_t *id = create_match_id();
|
---|
| 324 | id->id = "root";
|
---|
| 325 | id->score = 100;
|
---|
| 326 | add_match_id(&node->match_ids, id);
|
---|
[084ff99] | 327 | tree->root_node = node;
|
---|
[85e48a9] | 328 | }
|
---|
[084ff99] | 329 | return node != NULL;
|
---|
[85e48a9] | 330 | }
|
---|
| 331 |
|
---|
[0c3666d] | 332 | /** Lookup the best matching driver for the specified device in the list of drivers.
|
---|
| 333 | *
|
---|
| 334 | * A match between a device and a driver is found
|
---|
| 335 | * if one of the driver's match ids match one of the device's match ids.
|
---|
| 336 | * The score of the match is the product of the driver's and device's score associated with the matching id.
|
---|
| 337 | * The best matching driver for a device is the driver
|
---|
| 338 | * with the highest score of the match between the device and the driver.
|
---|
| 339 | *
|
---|
| 340 | * @param drivers_list the list of drivers, where we look for the driver suitable for handling the device.
|
---|
| 341 | * @param node the device node structure of the device.
|
---|
| 342 | *
|
---|
| 343 | * @return the best matching driver or NULL if no matching driver is found.
|
---|
| 344 | */
|
---|
| 345 | driver_t * find_best_match_driver(driver_list_t *drivers_list, node_t *node)
|
---|
[e4c4247] | 346 | {
|
---|
[e85920d] | 347 | printf(NAME ": find_best_match_driver\n");
|
---|
[85e48a9] | 348 | driver_t *best_drv = NULL, *drv = NULL;
|
---|
| 349 | int best_score = 0, score = 0;
|
---|
| 350 |
|
---|
[0c3666d] | 351 | fibril_mutex_lock(&drivers_list->drivers_mutex);
|
---|
[729fa2d6] | 352 |
|
---|
[0c3666d] | 353 | link_t *link = drivers_list->drivers.next;
|
---|
| 354 | while (link != &drivers_list->drivers) {
|
---|
[85e48a9] | 355 | drv = list_get_instance(link, driver_t, drivers);
|
---|
| 356 | score = get_match_score(drv, node);
|
---|
| 357 | if (score > best_score) {
|
---|
| 358 | best_score = score;
|
---|
| 359 | best_drv = drv;
|
---|
[e85920d] | 360 | }
|
---|
| 361 | link = link->next;
|
---|
[0c3666d] | 362 | }
|
---|
[729fa2d6] | 363 |
|
---|
[0c3666d] | 364 | fibril_mutex_unlock(&drivers_list->drivers_mutex);
|
---|
[e4c4247] | 365 |
|
---|
[85e48a9] | 366 | return best_drv;
|
---|
| 367 | }
|
---|
| 368 |
|
---|
[0c3666d] | 369 | /**
|
---|
| 370 | * Assign a driver to a device.
|
---|
| 371 | *
|
---|
| 372 | * @param node the device's node in the device tree.
|
---|
| 373 | * @param drv the driver.
|
---|
| 374 | */
|
---|
[e2b9a993] | 375 | void attach_driver(node_t *node, driver_t *drv)
|
---|
[85e48a9] | 376 | {
|
---|
[0c3666d] | 377 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
| 378 |
|
---|
[85e48a9] | 379 | node->drv = drv;
|
---|
| 380 | list_append(&node->driver_devices, &drv->devices);
|
---|
[0c3666d] | 381 |
|
---|
| 382 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
[85e48a9] | 383 | }
|
---|
| 384 |
|
---|
[0c3666d] | 385 | /** Start a driver.
|
---|
| 386 | *
|
---|
| 387 | * The driver's mutex is assumed to be locked.
|
---|
| 388 | *
|
---|
| 389 | * @param drv the driver's structure.
|
---|
| 390 | * @return true if the driver's task is successfully spawned, false otherwise.
|
---|
| 391 | */
|
---|
[e2b9a993] | 392 | bool start_driver(driver_t *drv)
|
---|
[85e48a9] | 393 | {
|
---|
[e85920d] | 394 | printf(NAME ": start_driver\n");
|
---|
| 395 |
|
---|
[85e48a9] | 396 | char *argv[2];
|
---|
| 397 |
|
---|
| 398 | printf(NAME ": spawning driver %s\n", drv->name);
|
---|
| 399 |
|
---|
| 400 | argv[0] = drv->name;
|
---|
| 401 | argv[1] = NULL;
|
---|
| 402 |
|
---|
| 403 | if (!task_spawn(drv->binary_path, argv)) {
|
---|
| 404 | printf(NAME ": error spawning %s\n", drv->name);
|
---|
| 405 | return false;
|
---|
| 406 | }
|
---|
| 407 |
|
---|
[e85920d] | 408 | drv->state = DRIVER_STARTING;
|
---|
[85e48a9] | 409 | return true;
|
---|
| 410 | }
|
---|
| 411 |
|
---|
[bda60d9] | 412 | /** Find device driver in the list of device drivers.
|
---|
| 413 | *
|
---|
| 414 | * @param drv_list the list of device drivers.
|
---|
| 415 | * @param drv_name the name of the device driver which is searched.
|
---|
| 416 | * @return the device driver of the specified name, if it is in the list, NULL otherwise.
|
---|
| 417 | */
|
---|
[729fa2d6] | 418 | driver_t * find_driver(driver_list_t *drv_list, const char *drv_name)
|
---|
| 419 | {
|
---|
| 420 | driver_t *res = NULL;
|
---|
| 421 |
|
---|
| 422 | fibril_mutex_lock(&drv_list->drivers_mutex);
|
---|
| 423 |
|
---|
| 424 | driver_t *drv = NULL;
|
---|
| 425 | link_t *link = drv_list->drivers.next;
|
---|
| 426 | while (link != &drv_list->drivers) {
|
---|
| 427 | drv = list_get_instance(link, driver_t, drivers);
|
---|
| 428 | if (0 == str_cmp(drv->name, drv_name)) {
|
---|
| 429 | res = drv;
|
---|
| 430 | break;
|
---|
| 431 | }
|
---|
| 432 | link = link->next;
|
---|
| 433 | }
|
---|
| 434 |
|
---|
| 435 | fibril_mutex_unlock(&drv_list->drivers_mutex);
|
---|
| 436 |
|
---|
| 437 | return res;
|
---|
| 438 | }
|
---|
| 439 |
|
---|
[bda60d9] | 440 | /** Remember the driver's phone.
|
---|
| 441 | * @param driver the driver.
|
---|
| 442 | * @param phone the phone to the driver.
|
---|
| 443 | */
|
---|
[c16cf62] | 444 | void set_driver_phone(driver_t *driver, ipcarg_t phone)
|
---|
| 445 | {
|
---|
| 446 | fibril_mutex_lock(&driver->driver_mutex);
|
---|
| 447 | assert(DRIVER_STARTING == driver->state);
|
---|
| 448 | driver->phone = phone;
|
---|
| 449 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
| 450 | }
|
---|
| 451 |
|
---|
| 452 | /**
|
---|
| 453 | * Notify driver about the devices to which it was assigned.
|
---|
| 454 | *
|
---|
| 455 | * The driver's mutex must be locked.
|
---|
| 456 | *
|
---|
| 457 | * @param driver the driver to which the devices are passed.
|
---|
| 458 | */
|
---|
| 459 | static void pass_devices_to_driver(driver_t *driver)
|
---|
| 460 | {
|
---|
| 461 | node_t *dev;
|
---|
| 462 | link_t *link;
|
---|
| 463 |
|
---|
[084ff99] | 464 | int phone = ipc_connect_me_to(driver->phone, DRIVER_DEVMAN, 0, 0);
|
---|
| 465 |
|
---|
| 466 | if (0 < phone) {
|
---|
| 467 |
|
---|
| 468 | link = driver->devices.next;
|
---|
| 469 | while (link != &driver->devices) {
|
---|
| 470 | dev = list_get_instance(link, node_t, driver_devices);
|
---|
| 471 | add_device(phone, driver, dev);
|
---|
| 472 | link = link->next;
|
---|
| 473 | }
|
---|
| 474 |
|
---|
| 475 | ipc_hangup(phone);
|
---|
| 476 | }
|
---|
[c16cf62] | 477 | }
|
---|
| 478 |
|
---|
[bda60d9] | 479 | /** Finish the initialization of a driver after it has succesfully started
|
---|
| 480 | * and after it has registered itself by the device manager.
|
---|
[c16cf62] | 481 | *
|
---|
| 482 | * Pass devices formerly matched to the driver to the driver and remember the driver is running and fully functional now.
|
---|
| 483 | *
|
---|
| 484 | * @param driver the driver which registered itself as running by the device manager.
|
---|
| 485 | */
|
---|
| 486 | void initialize_running_driver(driver_t *driver)
|
---|
[084ff99] | 487 | {
|
---|
[c16cf62] | 488 | fibril_mutex_lock(&driver->driver_mutex);
|
---|
| 489 |
|
---|
| 490 | // pass devices which have been already assigned to the driver to the driver
|
---|
| 491 | pass_devices_to_driver(driver);
|
---|
| 492 |
|
---|
| 493 | // change driver's state to running
|
---|
| 494 | driver->state = DRIVER_RUNNING;
|
---|
| 495 |
|
---|
| 496 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
| 497 | }
|
---|
| 498 |
|
---|
[0c3666d] | 499 | /** Pass a device to running driver.
|
---|
| 500 | *
|
---|
| 501 | * @param drv the driver's structure.
|
---|
| 502 | * @param node the device's node in the device tree.
|
---|
| 503 | */
|
---|
[084ff99] | 504 | void add_device(int phone, driver_t *drv, node_t *node)
|
---|
[85e48a9] | 505 | {
|
---|
[e85920d] | 506 | printf(NAME ": add_device\n");
|
---|
[084ff99] | 507 |
|
---|
| 508 | ipcarg_t ret;
|
---|
| 509 | ipcarg_t rc = async_req_1_1(phone, DRIVER_ADD_DEVICE, node->handle, &ret);
|
---|
| 510 | if (rc != EOK) {
|
---|
| 511 | // TODO handle error
|
---|
| 512 | return false;
|
---|
| 513 | }
|
---|
[e85920d] | 514 |
|
---|
[084ff99] | 515 | // TODO inspect return value (ret) to find out whether the device was successfully probed and added
|
---|
[85e48a9] | 516 |
|
---|
| 517 | return true;
|
---|
| 518 | }
|
---|
| 519 |
|
---|
[084ff99] | 520 | /**
|
---|
[0c3666d] | 521 | * Find suitable driver for a device and assign the driver to it.
|
---|
| 522 | *
|
---|
| 523 | * @param node the device node of the device in the device tree.
|
---|
| 524 | * @param drivers_list the list of available drivers.
|
---|
| 525 | *
|
---|
| 526 | * @return true if the suitable driver is found and successfully assigned to the device, false otherwise.
|
---|
| 527 | */
|
---|
| 528 | bool assign_driver(node_t *node, driver_list_t *drivers_list)
|
---|
[85e48a9] | 529 | {
|
---|
[e85920d] | 530 | printf(NAME ": assign_driver\n");
|
---|
| 531 |
|
---|
[85e48a9] | 532 | // find the driver which is the most suitable for handling this device
|
---|
[e2b9a993] | 533 | driver_t *drv = find_best_match_driver(drivers_list, node);
|
---|
[85e48a9] | 534 | if (NULL == drv) {
|
---|
[e85920d] | 535 | printf(NAME ": no driver found for device.\n");
|
---|
[85e48a9] | 536 | return false;
|
---|
| 537 | }
|
---|
| 538 |
|
---|
| 539 | // attach the driver to the device
|
---|
| 540 | attach_driver(node, drv);
|
---|
| 541 |
|
---|
[e85920d] | 542 | if (DRIVER_NOT_STARTED == drv->state) {
|
---|
[85e48a9] | 543 | // start driver
|
---|
| 544 | start_driver(drv);
|
---|
[e85920d] | 545 | }
|
---|
| 546 |
|
---|
| 547 | if (DRIVER_RUNNING == drv->state) {
|
---|
[85e48a9] | 548 | // notify driver about new device
|
---|
[084ff99] | 549 | int phone = ipc_connect_me_to(drv->phone, DRIVER_DEVMAN, 0, 0);
|
---|
| 550 | if (phone > 0) {
|
---|
| 551 | add_device(phone, drv, node);
|
---|
| 552 | ipc_hangup(phone);
|
---|
| 553 | }
|
---|
[85e48a9] | 554 | }
|
---|
| 555 |
|
---|
| 556 | return true;
|
---|
| 557 | }
|
---|
| 558 |
|
---|
[0c3666d] | 559 | /**
|
---|
| 560 | * Initialize the device tree.
|
---|
| 561 | *
|
---|
| 562 | * Create root device node of the tree and assign driver to it.
|
---|
| 563 | *
|
---|
| 564 | * @param tree the device tree.
|
---|
| 565 | * @param the list of available drivers.
|
---|
| 566 | * @return true on success, false otherwise.
|
---|
| 567 | */
|
---|
| 568 | bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list)
|
---|
[85e48a9] | 569 | {
|
---|
[e85920d] | 570 | printf(NAME ": init_device_tree.\n");
|
---|
[0c3666d] | 571 |
|
---|
[bda60d9] | 572 | memset(tree->node_map, 0, MAX_DEV * sizeof(node_t *));
|
---|
| 573 |
|
---|
[084ff99] | 574 | atomic_set(&tree->current_handle, 0);
|
---|
| 575 |
|
---|
[85e48a9] | 576 | // create root node and add it to the device tree
|
---|
[084ff99] | 577 | if (!create_root_node(tree)) {
|
---|
[85e48a9] | 578 | return false;
|
---|
| 579 | }
|
---|
[e4c4247] | 580 |
|
---|
| 581 | // find suitable driver and start it
|
---|
[e2b9a993] | 582 | return assign_driver(tree->root_node, drivers_list);
|
---|
[e4c4247] | 583 | }
|
---|
| 584 |
|
---|
[bda60d9] | 585 | /** Create and set device's full path in device tree.
|
---|
| 586 | *
|
---|
| 587 | * @param node the device's device node.
|
---|
| 588 | * @param parent the parent device node.
|
---|
| 589 | * @return true on success, false otherwise (insufficient resources etc.).
|
---|
| 590 | */
|
---|
| 591 | static bool set_dev_path(node_t *node, node_t *parent)
|
---|
| 592 | {
|
---|
| 593 | assert(NULL != node->name);
|
---|
| 594 |
|
---|
| 595 | size_t pathsize = (str_size(node->name) + 1);
|
---|
| 596 | if (NULL != parent) {
|
---|
| 597 | pathsize += str_size(parent->name) + 1;
|
---|
| 598 | }
|
---|
| 599 |
|
---|
| 600 | if (NULL == (node->pathname = (char *)malloc(pathsize))) {
|
---|
| 601 | printf(NAME ": failed to allocate device path.\n");
|
---|
| 602 | return false;
|
---|
| 603 | }
|
---|
| 604 |
|
---|
| 605 | if (NULL != parent) {
|
---|
| 606 | str_cpy(node->pathname, pathsize, parent->pathname);
|
---|
| 607 | str_append(node->pathname, pathsize, "/");
|
---|
| 608 | str_append(node->pathname, pathsize, node->name);
|
---|
| 609 | } else {
|
---|
| 610 | str_cpy(node->pathname, pathsize, node->name);
|
---|
| 611 | }
|
---|
| 612 |
|
---|
| 613 | return true;
|
---|
| 614 | }
|
---|
| 615 |
|
---|
| 616 | /** Insert new device into device tree.
|
---|
| 617 | *
|
---|
| 618 | * @param tree the device tree.
|
---|
| 619 | * @param node the newly added device node.
|
---|
| 620 | * @param dev_name the name of the newly added device.
|
---|
| 621 | * @param parent the parent device node.
|
---|
| 622 | * @return true on success, false otherwise (insufficient resources etc.).
|
---|
| 623 | */
|
---|
| 624 | bool insert_dev_node(dev_tree_t *tree, node_t *node, const char *dev_name, node_t *parent)
|
---|
| 625 | {
|
---|
| 626 | printf(NAME ": insert_dev_node\n");
|
---|
| 627 |
|
---|
| 628 | assert(NULL != node && NULL != tree && dev_name != NULL);
|
---|
| 629 |
|
---|
| 630 | node->name = dev_name;
|
---|
| 631 | if (!set_dev_path(node, parent)) {
|
---|
| 632 | return false;
|
---|
| 633 | }
|
---|
| 634 |
|
---|
| 635 | // add the node to the handle-to-node map
|
---|
| 636 | node->handle = atomic_postinc(&tree->current_handle);
|
---|
| 637 | if (node->handle >= MAX_DEV) {
|
---|
| 638 | printf(NAME ": failed to add device to device tree, because maximum number of devices was reached.\n");
|
---|
| 639 | free(node->pathname);
|
---|
| 640 | node->pathname = NULL;
|
---|
| 641 | atomic_postdec(&tree->current_handle);
|
---|
| 642 | return false;
|
---|
| 643 | }
|
---|
| 644 | tree->node_map[node->handle] = node;
|
---|
| 645 |
|
---|
| 646 | // add the node to the list of its parent's children
|
---|
| 647 | node->parent = parent;
|
---|
| 648 | if (NULL != parent) {
|
---|
| 649 | fibril_mutex_lock(&parent->children_mutex);
|
---|
| 650 | list_append(&node->sibling, &parent->children);
|
---|
| 651 | fibril_mutex_unlock(&parent->children_mutex);
|
---|
| 652 | }
|
---|
| 653 | return true;
|
---|
| 654 | }
|
---|
| 655 |
|
---|
[c16cf62] | 656 | /** @}
|
---|
| 657 | */ |
---|