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