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