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