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