[041b026] | 1 | /*
|
---|
[c3d9aaf5] | 2 | * Copyright (c) 2025 Jiri Svoboda
|
---|
[041b026] | 3 | * Copyright (c) 2010 Lenka Trochtova
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @addtogroup devman
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 |
|
---|
[d1bafbf] | 34 | #include <dirent.h>
|
---|
[041b026] | 35 | #include <errno.h>
|
---|
| 36 | #include <io/log.h>
|
---|
[23a0368] | 37 | #include <vfs/vfs.h>
|
---|
[041b026] | 38 | #include <loc.h>
|
---|
| 39 | #include <str_error.h>
|
---|
| 40 | #include <stdio.h>
|
---|
[1c635d6] | 41 | #include <task.h>
|
---|
[041b026] | 42 |
|
---|
[d1bafbf] | 43 | #include "dev.h"
|
---|
[041b026] | 44 | #include "devman.h"
|
---|
| 45 | #include "driver.h"
|
---|
[b25970f] | 46 | #include "fun.h"
|
---|
[a60e90b] | 47 | #include "match.h"
|
---|
[b25970f] | 48 | #include "main.h"
|
---|
| 49 |
|
---|
| 50 | static errno_t driver_reassign_fibril(void *);
|
---|
[041b026] | 51 |
|
---|
| 52 | /**
|
---|
| 53 | * Initialize the list of device driver's.
|
---|
| 54 | *
|
---|
| 55 | * @param drv_list the list of device driver's.
|
---|
| 56 | *
|
---|
| 57 | */
|
---|
| 58 | void init_driver_list(driver_list_t *drv_list)
|
---|
| 59 | {
|
---|
| 60 | assert(drv_list != NULL);
|
---|
[a35b458] | 61 |
|
---|
[041b026] | 62 | list_initialize(&drv_list->drivers);
|
---|
| 63 | fibril_mutex_initialize(&drv_list->drivers_mutex);
|
---|
[0511549] | 64 | drv_list->next_handle = 1;
|
---|
[041b026] | 65 | }
|
---|
| 66 |
|
---|
| 67 | /** Allocate and initialize a new driver structure.
|
---|
| 68 | *
|
---|
| 69 | * @return Driver structure.
|
---|
| 70 | */
|
---|
| 71 | driver_t *create_driver(void)
|
---|
| 72 | {
|
---|
| 73 | driver_t *res = malloc(sizeof(driver_t));
|
---|
| 74 | if (res != NULL)
|
---|
| 75 | init_driver(res);
|
---|
| 76 | return res;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | /** Add a driver to the list of drivers.
|
---|
| 80 | *
|
---|
| 81 | * @param drivers_list List of drivers.
|
---|
| 82 | * @param drv Driver structure.
|
---|
| 83 | */
|
---|
| 84 | void add_driver(driver_list_t *drivers_list, driver_t *drv)
|
---|
| 85 | {
|
---|
| 86 | fibril_mutex_lock(&drivers_list->drivers_mutex);
|
---|
[e5556e4a] | 87 | list_append(&drv->drivers, &drivers_list->drivers);
|
---|
[0511549] | 88 | drv->handle = drivers_list->next_handle++;
|
---|
[041b026] | 89 | fibril_mutex_unlock(&drivers_list->drivers_mutex);
|
---|
| 90 |
|
---|
[de3d15b4] | 91 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Driver `%s' was added to the list of available "
|
---|
[041b026] | 92 | "drivers.", drv->name);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /**
|
---|
| 96 | * Get information about a driver.
|
---|
| 97 | *
|
---|
| 98 | * Each driver has its own directory in the base directory.
|
---|
| 99 | * The name of the driver's directory is the same as the name of the driver.
|
---|
| 100 | * The driver's directory contains driver's binary (named as the driver without
|
---|
| 101 | * extension) and the configuration file with match ids for device-to-driver
|
---|
| 102 | * matching (named as the driver with a special extension).
|
---|
| 103 | *
|
---|
| 104 | * This function searches for the driver's directory and containing
|
---|
| 105 | * configuration files. If all the files needed are found, they are parsed and
|
---|
| 106 | * the information about the driver is stored in the driver's structure.
|
---|
| 107 | *
|
---|
| 108 | * @param base_path The base directory, in which we look for driver's
|
---|
| 109 | * subdirectory.
|
---|
| 110 | * @param name The name of the driver.
|
---|
| 111 | * @param drv The driver structure to fill information in.
|
---|
| 112 | *
|
---|
| 113 | * @return True on success, false otherwise.
|
---|
| 114 | */
|
---|
| 115 | bool get_driver_info(const char *base_path, const char *name, driver_t *drv)
|
---|
| 116 | {
|
---|
| 117 | log_msg(LOG_DEFAULT, LVL_DEBUG, "get_driver_info(base_path=\"%s\", name=\"%s\")",
|
---|
| 118 | base_path, name);
|
---|
[a35b458] | 119 |
|
---|
[041b026] | 120 | assert(base_path != NULL && name != NULL && drv != NULL);
|
---|
[a35b458] | 121 |
|
---|
[041b026] | 122 | bool suc = false;
|
---|
| 123 | char *match_path = NULL;
|
---|
| 124 | size_t name_size = 0;
|
---|
[a35b458] | 125 |
|
---|
[041b026] | 126 | /* Read the list of match ids from the driver's configuration file. */
|
---|
| 127 | match_path = get_abs_path(base_path, name, MATCH_EXT);
|
---|
| 128 | if (match_path == NULL)
|
---|
| 129 | goto cleanup;
|
---|
[a35b458] | 130 |
|
---|
[041b026] | 131 | if (!read_match_ids(match_path, &drv->match_ids))
|
---|
| 132 | goto cleanup;
|
---|
[a35b458] | 133 |
|
---|
[041b026] | 134 | /* Allocate and fill driver's name. */
|
---|
| 135 | name_size = str_size(name) + 1;
|
---|
| 136 | drv->name = malloc(name_size);
|
---|
| 137 | if (drv->name == NULL)
|
---|
| 138 | goto cleanup;
|
---|
| 139 | str_cpy(drv->name, name_size, name);
|
---|
[a35b458] | 140 |
|
---|
[041b026] | 141 | /* Initialize path with driver's binary. */
|
---|
| 142 | drv->binary_path = get_abs_path(base_path, name, "");
|
---|
| 143 | if (drv->binary_path == NULL)
|
---|
| 144 | goto cleanup;
|
---|
[a35b458] | 145 |
|
---|
[041b026] | 146 | /* Check whether the driver's binary exists. */
|
---|
[39330200] | 147 | vfs_stat_t s;
|
---|
[23a0368] | 148 | if (vfs_stat_path(drv->binary_path, &s) != EOK) {
|
---|
[041b026] | 149 | log_msg(LOG_DEFAULT, LVL_ERROR, "Driver not found at path `%s'.",
|
---|
| 150 | drv->binary_path);
|
---|
| 151 | goto cleanup;
|
---|
| 152 | }
|
---|
[a35b458] | 153 |
|
---|
[041b026] | 154 | suc = true;
|
---|
[a35b458] | 155 |
|
---|
[041b026] | 156 | cleanup:
|
---|
| 157 | if (!suc) {
|
---|
| 158 | free(drv->binary_path);
|
---|
| 159 | free(drv->name);
|
---|
| 160 | /* Set the driver structure to the default state. */
|
---|
| 161 | init_driver(drv);
|
---|
| 162 | }
|
---|
[a35b458] | 163 |
|
---|
[041b026] | 164 | free(match_path);
|
---|
[a35b458] | 165 |
|
---|
[041b026] | 166 | return suc;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | /** Lookup drivers in the directory.
|
---|
| 170 | *
|
---|
| 171 | * @param drivers_list The list of available drivers.
|
---|
| 172 | * @param dir_path The path to the directory where we search for drivers.
|
---|
| 173 | * @return Number of drivers which were found.
|
---|
| 174 | */
|
---|
| 175 | int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path)
|
---|
| 176 | {
|
---|
| 177 | log_msg(LOG_DEFAULT, LVL_DEBUG, "lookup_available_drivers(dir=\"%s\")", dir_path);
|
---|
[a35b458] | 178 |
|
---|
[041b026] | 179 | int drv_cnt = 0;
|
---|
| 180 | DIR *dir = NULL;
|
---|
| 181 | struct dirent *diren;
|
---|
| 182 |
|
---|
| 183 | dir = opendir(dir_path);
|
---|
[a35b458] | 184 |
|
---|
[041b026] | 185 | if (dir != NULL) {
|
---|
| 186 | driver_t *drv = create_driver();
|
---|
| 187 | while ((diren = readdir(dir))) {
|
---|
| 188 | if (get_driver_info(dir_path, diren->d_name, drv)) {
|
---|
| 189 | add_driver(drivers_list, drv);
|
---|
| 190 | drv_cnt++;
|
---|
| 191 | drv = create_driver();
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 | delete_driver(drv);
|
---|
| 195 | closedir(dir);
|
---|
| 196 | }
|
---|
[a35b458] | 197 |
|
---|
[041b026] | 198 | return drv_cnt;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[b25970f] | 201 | /** Lookup the next best matching driver for a device.
|
---|
[041b026] | 202 | *
|
---|
| 203 | * A match between a device and a driver is found if one of the driver's match
|
---|
| 204 | * ids match one of the device's match ids. The score of the match is the
|
---|
| 205 | * product of the driver's and device's score associated with the matching id.
|
---|
| 206 | * The best matching driver for a device is the driver with the highest score
|
---|
| 207 | * of the match between the device and the driver.
|
---|
| 208 | *
|
---|
[b25970f] | 209 | * If a driver is already assigned to the device (node->drv != NULL),
|
---|
| 210 | * we look for the next best driver. That is either the next driver with the
|
---|
| 211 | * same score in the list of drivers, or a driver with the next best score
|
---|
| 212 | * (greater than zero).
|
---|
| 213 | *
|
---|
[041b026] | 214 | * @param drivers_list The list of drivers, where we look for the driver
|
---|
| 215 | * suitable for handling the device.
|
---|
| 216 | * @param node The device node structure of the device.
|
---|
| 217 | * @return The best matching driver or NULL if no matching driver
|
---|
| 218 | * is found.
|
---|
| 219 | */
|
---|
| 220 | driver_t *find_best_match_driver(driver_list_t *drivers_list, dev_node_t *node)
|
---|
| 221 | {
|
---|
[08bc23d] | 222 | driver_t *best_drv = NULL;
|
---|
[041b026] | 223 | int best_score = 0, score = 0;
|
---|
[b25970f] | 224 | int cur_score;
|
---|
| 225 | link_t *link;
|
---|
[a35b458] | 226 |
|
---|
[041b026] | 227 | fibril_mutex_lock(&drivers_list->drivers_mutex);
|
---|
[a35b458] | 228 |
|
---|
[b25970f] | 229 | if (node->drv != NULL) {
|
---|
| 230 | cur_score = get_match_score(node->drv, node);
|
---|
| 231 |
|
---|
[64f85f5] | 232 | link = list_next(&node->drv->drivers, &drivers_list->drivers);
|
---|
[b25970f] | 233 |
|
---|
| 234 | /*
|
---|
| 235 | * Find next driver with score equal to the current.
|
---|
| 236 | */
|
---|
| 237 | while (link != NULL) {
|
---|
[64f85f5] | 238 | driver_t *drv = list_get_instance(link, driver_t,
|
---|
| 239 | drivers);
|
---|
[b25970f] | 240 | score = get_match_score(drv, node);
|
---|
| 241 | if (score == cur_score) {
|
---|
| 242 | /* Found it */
|
---|
| 243 | fibril_mutex_unlock(&drivers_list->drivers_mutex);
|
---|
| 244 | return drv;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | link = list_next(link, &drivers_list->drivers);
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | /* There is no driver with the same score */
|
---|
| 251 | } else {
|
---|
| 252 | cur_score = INT_MAX;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | /*
|
---|
| 256 | * Find driver with the next best score
|
---|
| 257 | */
|
---|
[08bc23d] | 258 | list_foreach(drivers_list->drivers, drivers, driver_t, drv) {
|
---|
[041b026] | 259 | score = get_match_score(drv, node);
|
---|
[b25970f] | 260 | if (score > best_score && score < cur_score) {
|
---|
[041b026] | 261 | best_score = score;
|
---|
| 262 | best_drv = drv;
|
---|
| 263 | }
|
---|
| 264 | }
|
---|
[a35b458] | 265 |
|
---|
[041b026] | 266 | fibril_mutex_unlock(&drivers_list->drivers_mutex);
|
---|
| 267 | return best_drv;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | /** Assign a driver to a device.
|
---|
| 271 | *
|
---|
| 272 | * @param tree Device tree
|
---|
| 273 | * @param node The device's node in the device tree.
|
---|
| 274 | * @param drv The driver.
|
---|
| 275 | */
|
---|
| 276 | void attach_driver(dev_tree_t *tree, dev_node_t *dev, driver_t *drv)
|
---|
| 277 | {
|
---|
| 278 | log_msg(LOG_DEFAULT, LVL_DEBUG, "attach_driver(dev=\"%s\",drv=\"%s\")",
|
---|
| 279 | dev->pfun->pathname, drv->name);
|
---|
[a35b458] | 280 |
|
---|
[041b026] | 281 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
| 282 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
[a35b458] | 283 |
|
---|
[041b026] | 284 | dev->drv = drv;
|
---|
[b25970f] | 285 | dev->passed_to_driver = false;
|
---|
[c3d9aaf5] | 286 | fibril_mutex_lock(&dev->state_lock);
|
---|
| 287 | dev->state = DEVICE_ATTACHING;
|
---|
| 288 | fibril_mutex_unlock(&dev->state_lock);
|
---|
| 289 | fibril_condvar_broadcast(&dev->state_cv);
|
---|
[041b026] | 290 | list_append(&dev->driver_devices, &drv->devices);
|
---|
[a35b458] | 291 |
|
---|
[041b026] | 292 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
| 293 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | /** Detach driver from device.
|
---|
| 297 | *
|
---|
| 298 | * @param tree Device tree
|
---|
| 299 | * @param node The device's node in the device tree.
|
---|
| 300 | * @param drv The driver.
|
---|
| 301 | */
|
---|
| 302 | void detach_driver(dev_tree_t *tree, dev_node_t *dev)
|
---|
| 303 | {
|
---|
| 304 | driver_t *drv = dev->drv;
|
---|
[a35b458] | 305 |
|
---|
[041b026] | 306 | assert(drv != NULL);
|
---|
[a35b458] | 307 |
|
---|
[041b026] | 308 | log_msg(LOG_DEFAULT, LVL_DEBUG, "detach_driver(dev=\"%s\",drv=\"%s\")",
|
---|
| 309 | dev->pfun->pathname, drv->name);
|
---|
[a35b458] | 310 |
|
---|
[041b026] | 311 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
| 312 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
[a35b458] | 313 |
|
---|
[041b026] | 314 | dev->drv = NULL;
|
---|
| 315 | list_remove(&dev->driver_devices);
|
---|
[a35b458] | 316 |
|
---|
[041b026] | 317 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
| 318 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | /** Start a driver
|
---|
| 322 | *
|
---|
| 323 | * @param drv The driver's structure.
|
---|
| 324 | * @return True if the driver's task is successfully spawned, false
|
---|
| 325 | * otherwise.
|
---|
| 326 | */
|
---|
| 327 | bool start_driver(driver_t *drv)
|
---|
| 328 | {
|
---|
[b7fd2a0] | 329 | errno_t rc;
|
---|
[041b026] | 330 |
|
---|
| 331 | assert(fibril_mutex_is_locked(&drv->driver_mutex));
|
---|
[a35b458] | 332 |
|
---|
[041b026] | 333 | log_msg(LOG_DEFAULT, LVL_DEBUG, "start_driver(drv=\"%s\")", drv->name);
|
---|
[a35b458] | 334 |
|
---|
[1c635d6] | 335 | rc = task_spawnl(NULL, NULL, drv->binary_path, drv->binary_path, NULL);
|
---|
[041b026] | 336 | if (rc != EOK) {
|
---|
| 337 | log_msg(LOG_DEFAULT, LVL_ERROR, "Spawning driver `%s' (%s) failed: %s.",
|
---|
| 338 | drv->name, drv->binary_path, str_error(rc));
|
---|
| 339 | return false;
|
---|
| 340 | }
|
---|
[a35b458] | 341 |
|
---|
[041b026] | 342 | drv->state = DRIVER_STARTING;
|
---|
| 343 | return true;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
[81685dd9] | 346 | /** Stop a driver
|
---|
| 347 | *
|
---|
| 348 | * @param drv The driver's structure.
|
---|
| 349 | * @return True if the driver's task is successfully spawned, false
|
---|
| 350 | * otherwise.
|
---|
| 351 | */
|
---|
[b7fd2a0] | 352 | errno_t stop_driver(driver_t *drv)
|
---|
[81685dd9] | 353 | {
|
---|
| 354 | async_exch_t *exch;
|
---|
[b7fd2a0] | 355 | errno_t retval;
|
---|
[a35b458] | 356 |
|
---|
[81685dd9] | 357 | log_msg(LOG_DEFAULT, LVL_DEBUG, "stop_driver(drv=\"%s\")", drv->name);
|
---|
| 358 |
|
---|
| 359 | exch = async_exchange_begin(drv->sess);
|
---|
| 360 | retval = async_req_0_0(exch, DRIVER_STOP);
|
---|
| 361 | loc_exchange_end(exch);
|
---|
[a35b458] | 362 |
|
---|
[81685dd9] | 363 | if (retval != EOK)
|
---|
| 364 | return retval;
|
---|
[a35b458] | 365 |
|
---|
[81685dd9] | 366 | drv->state = DRIVER_NOT_STARTED;
|
---|
| 367 | async_hangup(drv->sess);
|
---|
| 368 | drv->sess = NULL;
|
---|
| 369 | return EOK;
|
---|
| 370 | }
|
---|
| 371 |
|
---|
[0511549] | 372 | /** Find device driver by handle.
|
---|
| 373 | *
|
---|
| 374 | * @param drv_list The list of device drivers
|
---|
| 375 | * @param handle Driver handle
|
---|
| 376 | * @return The device driver, if it is in the list,
|
---|
| 377 | * NULL otherwise.
|
---|
| 378 | */
|
---|
| 379 | driver_t *driver_find(driver_list_t *drv_list, devman_handle_t handle)
|
---|
| 380 | {
|
---|
| 381 | driver_t *res = NULL;
|
---|
[a35b458] | 382 |
|
---|
[0511549] | 383 | fibril_mutex_lock(&drv_list->drivers_mutex);
|
---|
[a35b458] | 384 |
|
---|
[0511549] | 385 | list_foreach(drv_list->drivers, drivers, driver_t, drv) {
|
---|
| 386 | if (drv->handle == handle) {
|
---|
| 387 | res = drv;
|
---|
| 388 | break;
|
---|
| 389 | }
|
---|
| 390 | }
|
---|
[a35b458] | 391 |
|
---|
[0511549] | 392 | fibril_mutex_unlock(&drv_list->drivers_mutex);
|
---|
[a35b458] | 393 |
|
---|
[0511549] | 394 | return res;
|
---|
| 395 | }
|
---|
| 396 |
|
---|
| 397 | /** Find device driver by name.
|
---|
[041b026] | 398 | *
|
---|
| 399 | * @param drv_list The list of device drivers.
|
---|
| 400 | * @param drv_name The name of the device driver which is searched.
|
---|
| 401 | * @return The device driver of the specified name, if it is in the
|
---|
| 402 | * list, NULL otherwise.
|
---|
| 403 | */
|
---|
[0511549] | 404 | driver_t *driver_find_by_name(driver_list_t *drv_list, const char *drv_name)
|
---|
[041b026] | 405 | {
|
---|
| 406 | driver_t *res = NULL;
|
---|
[a35b458] | 407 |
|
---|
[041b026] | 408 | fibril_mutex_lock(&drv_list->drivers_mutex);
|
---|
[a35b458] | 409 |
|
---|
[08bc23d] | 410 | list_foreach(drv_list->drivers, drivers, driver_t, drv) {
|
---|
[041b026] | 411 | if (str_cmp(drv->name, drv_name) == 0) {
|
---|
| 412 | res = drv;
|
---|
| 413 | break;
|
---|
| 414 | }
|
---|
| 415 | }
|
---|
[a35b458] | 416 |
|
---|
[041b026] | 417 | fibril_mutex_unlock(&drv_list->drivers_mutex);
|
---|
[a35b458] | 418 |
|
---|
[041b026] | 419 | return res;
|
---|
| 420 | }
|
---|
| 421 |
|
---|
| 422 | /** Notify driver about the devices to which it was assigned.
|
---|
| 423 | *
|
---|
| 424 | * @param driver The driver to which the devices are passed.
|
---|
| 425 | */
|
---|
| 426 | static void pass_devices_to_driver(driver_t *driver, dev_tree_t *tree)
|
---|
| 427 | {
|
---|
| 428 | dev_node_t *dev;
|
---|
| 429 | link_t *link;
|
---|
| 430 |
|
---|
| 431 | log_msg(LOG_DEFAULT, LVL_DEBUG, "pass_devices_to_driver(driver=\"%s\")",
|
---|
| 432 | driver->name);
|
---|
| 433 |
|
---|
| 434 | fibril_mutex_lock(&driver->driver_mutex);
|
---|
| 435 |
|
---|
| 436 | /*
|
---|
| 437 | * Go through devices list as long as there is some device
|
---|
| 438 | * that has not been passed to the driver.
|
---|
| 439 | */
|
---|
| 440 | link = driver->devices.head.next;
|
---|
| 441 | while (link != &driver->devices.head) {
|
---|
| 442 | dev = list_get_instance(link, dev_node_t, driver_devices);
|
---|
| 443 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
[a35b458] | 444 |
|
---|
[041b026] | 445 | if (dev->passed_to_driver) {
|
---|
| 446 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
| 447 | link = link->next;
|
---|
| 448 | continue;
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | dev_add_ref(dev);
|
---|
| 452 |
|
---|
| 453 | /*
|
---|
| 454 | * Unlock to avoid deadlock when adding device
|
---|
| 455 | * handled by itself.
|
---|
| 456 | */
|
---|
| 457 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
| 458 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
| 459 |
|
---|
| 460 | add_device(driver, dev, tree);
|
---|
| 461 |
|
---|
[b25970f] | 462 | /* Device probe failed, need to try next best driver */
|
---|
| 463 | if (dev->state == DEVICE_NOT_PRESENT) {
|
---|
| 464 | fibril_mutex_lock(&driver->driver_mutex);
|
---|
| 465 | list_remove(&dev->driver_devices);
|
---|
| 466 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
[5aa06ad] | 467 | /* Give an extra reference to driver_reassign_fibril */
|
---|
| 468 | dev_add_ref(dev);
|
---|
[b25970f] | 469 | fid_t fid = fibril_create(driver_reassign_fibril, dev);
|
---|
| 470 | if (fid == 0) {
|
---|
| 471 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 472 | "Error creating fibril to assign driver.");
|
---|
[5aa06ad] | 473 | dev_del_ref(dev);
|
---|
[b25970f] | 474 | }
|
---|
| 475 | fibril_add_ready(fid);
|
---|
| 476 | }
|
---|
| 477 |
|
---|
[041b026] | 478 | dev_del_ref(dev);
|
---|
| 479 |
|
---|
| 480 | /*
|
---|
| 481 | * Lock again as we will work with driver's
|
---|
| 482 | * structure.
|
---|
| 483 | */
|
---|
| 484 | fibril_mutex_lock(&driver->driver_mutex);
|
---|
| 485 |
|
---|
| 486 | /*
|
---|
| 487 | * Restart the cycle to go through all devices again.
|
---|
| 488 | */
|
---|
| 489 | link = driver->devices.head.next;
|
---|
| 490 | }
|
---|
| 491 |
|
---|
| 492 | /*
|
---|
| 493 | * Once we passed all devices to the driver, we need to mark the
|
---|
| 494 | * driver as running.
|
---|
| 495 | * It is vital to do it here and inside critical section.
|
---|
| 496 | *
|
---|
| 497 | * If we would change the state earlier, other devices added to
|
---|
| 498 | * the driver would be added to the device list and started
|
---|
| 499 | * immediately and possibly started here as well.
|
---|
| 500 | */
|
---|
| 501 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Driver `%s' enters running state.", driver->name);
|
---|
| 502 | driver->state = DRIVER_RUNNING;
|
---|
| 503 |
|
---|
| 504 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
| 505 | }
|
---|
| 506 |
|
---|
| 507 | /** Finish the initialization of a driver after it has succesfully started
|
---|
| 508 | * and after it has registered itself by the device manager.
|
---|
| 509 | *
|
---|
| 510 | * Pass devices formerly matched to the driver to the driver and remember the
|
---|
| 511 | * driver is running and fully functional now.
|
---|
| 512 | *
|
---|
| 513 | * @param driver The driver which registered itself as running by the
|
---|
| 514 | * device manager.
|
---|
| 515 | */
|
---|
| 516 | void initialize_running_driver(driver_t *driver, dev_tree_t *tree)
|
---|
| 517 | {
|
---|
| 518 | log_msg(LOG_DEFAULT, LVL_DEBUG, "initialize_running_driver(driver=\"%s\")",
|
---|
| 519 | driver->name);
|
---|
[a35b458] | 520 |
|
---|
[041b026] | 521 | /*
|
---|
| 522 | * Pass devices which have been already assigned to the driver to the
|
---|
| 523 | * driver.
|
---|
| 524 | */
|
---|
| 525 | pass_devices_to_driver(driver, tree);
|
---|
| 526 | }
|
---|
| 527 |
|
---|
| 528 | /** Initialize device driver structure.
|
---|
| 529 | *
|
---|
| 530 | * @param drv The device driver structure.
|
---|
| 531 | */
|
---|
| 532 | void init_driver(driver_t *drv)
|
---|
| 533 | {
|
---|
| 534 | assert(drv != NULL);
|
---|
| 535 |
|
---|
| 536 | memset(drv, 0, sizeof(driver_t));
|
---|
| 537 | list_initialize(&drv->match_ids.ids);
|
---|
| 538 | list_initialize(&drv->devices);
|
---|
| 539 | fibril_mutex_initialize(&drv->driver_mutex);
|
---|
| 540 | drv->sess = NULL;
|
---|
| 541 | }
|
---|
| 542 |
|
---|
| 543 | /** Device driver structure clean-up.
|
---|
| 544 | *
|
---|
| 545 | * @param drv The device driver structure.
|
---|
| 546 | */
|
---|
| 547 | void clean_driver(driver_t *drv)
|
---|
| 548 | {
|
---|
| 549 | assert(drv != NULL);
|
---|
| 550 |
|
---|
| 551 | free(drv->name);
|
---|
| 552 | free(drv->binary_path);
|
---|
| 553 |
|
---|
| 554 | clean_match_ids(&drv->match_ids);
|
---|
| 555 |
|
---|
| 556 | init_driver(drv);
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 | /** Delete device driver structure.
|
---|
| 560 | *
|
---|
| 561 | * @param drv The device driver structure.
|
---|
| 562 | */
|
---|
| 563 | void delete_driver(driver_t *drv)
|
---|
| 564 | {
|
---|
| 565 | assert(drv != NULL);
|
---|
[a35b458] | 566 |
|
---|
[041b026] | 567 | clean_driver(drv);
|
---|
| 568 | free(drv);
|
---|
| 569 | }
|
---|
| 570 |
|
---|
| 571 | /** Find suitable driver for a device and assign the driver to it.
|
---|
| 572 | *
|
---|
| 573 | * @param node The device node of the device in the device tree.
|
---|
| 574 | * @param drivers_list The list of available drivers.
|
---|
| 575 | * @return True if the suitable driver is found and
|
---|
| 576 | * successfully assigned to the device, false otherwise.
|
---|
| 577 | */
|
---|
| 578 | bool assign_driver(dev_node_t *dev, driver_list_t *drivers_list,
|
---|
| 579 | dev_tree_t *tree)
|
---|
| 580 | {
|
---|
[b25970f] | 581 | driver_t *drv;
|
---|
| 582 |
|
---|
[041b026] | 583 | assert(dev != NULL);
|
---|
| 584 | assert(drivers_list != NULL);
|
---|
| 585 | assert(tree != NULL);
|
---|
[a35b458] | 586 |
|
---|
[041b026] | 587 | /*
|
---|
[b25970f] | 588 | * Find the next best driver for this device.
|
---|
[041b026] | 589 | */
|
---|
[b25970f] | 590 | again:
|
---|
| 591 | drv = find_best_match_driver(drivers_list, dev);
|
---|
[041b026] | 592 | if (drv == NULL) {
|
---|
| 593 | log_msg(LOG_DEFAULT, LVL_ERROR, "No driver found for device `%s'.",
|
---|
| 594 | dev->pfun->pathname);
|
---|
| 595 | return false;
|
---|
| 596 | }
|
---|
[a35b458] | 597 |
|
---|
[041b026] | 598 | /* Attach the driver to the device. */
|
---|
| 599 | attach_driver(tree, dev, drv);
|
---|
[a35b458] | 600 |
|
---|
[041b026] | 601 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
| 602 | if (drv->state == DRIVER_NOT_STARTED) {
|
---|
| 603 | /* Start the driver. */
|
---|
| 604 | start_driver(drv);
|
---|
| 605 | }
|
---|
| 606 | bool is_running = drv->state == DRIVER_RUNNING;
|
---|
| 607 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
| 608 |
|
---|
| 609 | /* Notify the driver about the new device. */
|
---|
[b25970f] | 610 | if (is_running) {
|
---|
[041b026] | 611 | add_device(drv, dev, tree);
|
---|
[a35b458] | 612 |
|
---|
[b25970f] | 613 | /* If the device probe failed, need to try next available driver */
|
---|
| 614 | if (dev->state == DEVICE_NOT_PRESENT)
|
---|
| 615 | goto again;
|
---|
| 616 | }
|
---|
| 617 |
|
---|
[041b026] | 618 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
| 619 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
| 620 |
|
---|
| 621 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
| 622 | if (dev->pfun != NULL) {
|
---|
| 623 | dev->pfun->state = FUN_ON_LINE;
|
---|
| 624 | }
|
---|
| 625 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
| 626 | return true;
|
---|
| 627 | }
|
---|
| 628 |
|
---|
[a60e90b] | 629 | /** Pass a device to running driver.
|
---|
| 630 | *
|
---|
| 631 | * @param drv The driver's structure.
|
---|
| 632 | * @param node The device's node in the device tree.
|
---|
| 633 | */
|
---|
| 634 | void add_device(driver_t *drv, dev_node_t *dev, dev_tree_t *tree)
|
---|
| 635 | {
|
---|
[c3d9aaf5] | 636 | link_t *link;
|
---|
| 637 | fun_node_t *fun;
|
---|
| 638 |
|
---|
[a60e90b] | 639 | /*
|
---|
| 640 | * We do not expect to have driver's mutex locked as we do not
|
---|
| 641 | * access any structures that would affect driver_t.
|
---|
| 642 | */
|
---|
| 643 | log_msg(LOG_DEFAULT, LVL_DEBUG, "add_device(drv=\"%s\", dev=\"%s\")",
|
---|
| 644 | drv->name, dev->pfun->name);
|
---|
[a35b458] | 645 |
|
---|
[a60e90b] | 646 | /* Send the device to the driver. */
|
---|
| 647 | devman_handle_t parent_handle;
|
---|
| 648 | if (dev->pfun) {
|
---|
| 649 | parent_handle = dev->pfun->handle;
|
---|
| 650 | } else {
|
---|
| 651 | parent_handle = 0;
|
---|
| 652 | }
|
---|
[a35b458] | 653 |
|
---|
[a60e90b] | 654 | async_exch_t *exch = async_exchange_begin(drv->sess);
|
---|
[a35b458] | 655 |
|
---|
[a60e90b] | 656 | ipc_call_t answer;
|
---|
| 657 | aid_t req = async_send_2(exch, DRIVER_DEV_ADD, dev->handle,
|
---|
| 658 | parent_handle, &answer);
|
---|
[a35b458] | 659 |
|
---|
[a60e90b] | 660 | /* Send the device name to the driver. */
|
---|
[b7fd2a0] | 661 | errno_t rc = async_data_write_start(exch, dev->pfun->name,
|
---|
[a60e90b] | 662 | str_size(dev->pfun->name) + 1);
|
---|
[a35b458] | 663 |
|
---|
[a60e90b] | 664 | async_exchange_end(exch);
|
---|
[a35b458] | 665 |
|
---|
[a60e90b] | 666 | if (rc != EOK) {
|
---|
[f9b2cb4c] | 667 | async_forget(req);
|
---|
| 668 | } else {
|
---|
| 669 | /* Wait for answer from the driver. */
|
---|
| 670 | async_wait_for(req, &rc);
|
---|
[a60e90b] | 671 | }
|
---|
| 672 |
|
---|
[c3d9aaf5] | 673 | if (rc == EOK) {
|
---|
| 674 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Device was added. Wait for "
|
---|
| 675 | "child functions' devices to stabilize.");
|
---|
| 676 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
| 677 | link = list_first(&dev->functions);
|
---|
| 678 | while (link != NULL) {
|
---|
| 679 | fun = list_get_instance(link, fun_node_t,
|
---|
| 680 | dev_functions);
|
---|
| 681 |
|
---|
| 682 | if (fun->child != NULL) {
|
---|
| 683 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "Wait for "
|
---|
| 684 | "child device %p.", (void *)fun->child);
|
---|
| 685 | fun_add_ref(fun);
|
---|
| 686 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
| 687 | dev_wait_stable(fun->child);
|
---|
| 688 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
| 689 | fun_del_ref(fun);
|
---|
| 690 | }
|
---|
| 691 |
|
---|
| 692 | link = list_next(link, &dev->functions);
|
---|
| 693 | }
|
---|
| 694 |
|
---|
| 695 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
| 696 | log_msg(LOG_DEFAULT, LVL_DEBUG,
|
---|
| 697 | "Finished waiting for children.");
|
---|
| 698 | }
|
---|
| 699 |
|
---|
| 700 | fibril_mutex_lock(&dev->state_lock);
|
---|
| 701 |
|
---|
[f9b2cb4c] | 702 | switch (rc) {
|
---|
[a60e90b] | 703 | case EOK:
|
---|
| 704 | dev->state = DEVICE_USABLE;
|
---|
| 705 | break;
|
---|
| 706 | case ENOENT:
|
---|
| 707 | dev->state = DEVICE_NOT_PRESENT;
|
---|
| 708 | break;
|
---|
| 709 | default:
|
---|
| 710 | dev->state = DEVICE_INVALID;
|
---|
| 711 | break;
|
---|
| 712 | }
|
---|
[a35b458] | 713 |
|
---|
[c3d9aaf5] | 714 | fibril_mutex_unlock(&dev->state_lock);
|
---|
| 715 | fibril_condvar_broadcast(&dev->state_cv);
|
---|
| 716 |
|
---|
[a60e90b] | 717 | dev->passed_to_driver = true;
|
---|
| 718 | }
|
---|
| 719 |
|
---|
[b7fd2a0] | 720 | errno_t driver_dev_remove(dev_tree_t *tree, dev_node_t *dev)
|
---|
[041b026] | 721 | {
|
---|
| 722 | async_exch_t *exch;
|
---|
[b7fd2a0] | 723 | errno_t retval;
|
---|
[041b026] | 724 | driver_t *drv;
|
---|
| 725 | devman_handle_t handle;
|
---|
[a35b458] | 726 |
|
---|
[041b026] | 727 | assert(dev != NULL);
|
---|
[a35b458] | 728 |
|
---|
[041b026] | 729 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_remove(%p)", dev);
|
---|
[a35b458] | 730 |
|
---|
[041b026] | 731 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
| 732 | drv = dev->drv;
|
---|
| 733 | handle = dev->handle;
|
---|
| 734 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
[a35b458] | 735 |
|
---|
[041b026] | 736 | exch = async_exchange_begin(drv->sess);
|
---|
| 737 | retval = async_req_1_0(exch, DRIVER_DEV_REMOVE, handle);
|
---|
| 738 | async_exchange_end(exch);
|
---|
[a35b458] | 739 |
|
---|
[041b026] | 740 | return retval;
|
---|
| 741 | }
|
---|
| 742 |
|
---|
[8300c72] | 743 | errno_t driver_dev_quiesce(dev_tree_t *tree, dev_node_t *dev)
|
---|
| 744 | {
|
---|
| 745 | async_exch_t *exch;
|
---|
| 746 | errno_t retval;
|
---|
| 747 | driver_t *drv;
|
---|
| 748 | devman_handle_t handle;
|
---|
| 749 |
|
---|
| 750 | assert(dev != NULL);
|
---|
| 751 |
|
---|
| 752 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_quiesce(%p)", dev);
|
---|
| 753 |
|
---|
| 754 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
| 755 | drv = dev->drv;
|
---|
| 756 | handle = dev->handle;
|
---|
| 757 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
| 758 |
|
---|
| 759 | exch = async_exchange_begin(drv->sess);
|
---|
| 760 | retval = async_req_1_0(exch, DRIVER_DEV_QUIESCE, handle);
|
---|
| 761 | async_exchange_end(exch);
|
---|
| 762 |
|
---|
| 763 | return retval;
|
---|
| 764 | }
|
---|
| 765 |
|
---|
[b7fd2a0] | 766 | errno_t driver_dev_gone(dev_tree_t *tree, dev_node_t *dev)
|
---|
[041b026] | 767 | {
|
---|
| 768 | async_exch_t *exch;
|
---|
[b7fd2a0] | 769 | errno_t retval;
|
---|
[041b026] | 770 | driver_t *drv;
|
---|
| 771 | devman_handle_t handle;
|
---|
[a35b458] | 772 |
|
---|
[041b026] | 773 | assert(dev != NULL);
|
---|
[a35b458] | 774 |
|
---|
[041b026] | 775 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_gone(%p)", dev);
|
---|
[a35b458] | 776 |
|
---|
[041b026] | 777 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
| 778 | drv = dev->drv;
|
---|
| 779 | handle = dev->handle;
|
---|
| 780 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
[a35b458] | 781 |
|
---|
[041b026] | 782 | exch = async_exchange_begin(drv->sess);
|
---|
| 783 | retval = async_req_1_0(exch, DRIVER_DEV_GONE, handle);
|
---|
| 784 | async_exchange_end(exch);
|
---|
[a35b458] | 785 |
|
---|
[041b026] | 786 | return retval;
|
---|
| 787 | }
|
---|
| 788 |
|
---|
[b7fd2a0] | 789 | errno_t driver_fun_online(dev_tree_t *tree, fun_node_t *fun)
|
---|
[041b026] | 790 | {
|
---|
| 791 | async_exch_t *exch;
|
---|
[b7fd2a0] | 792 | errno_t retval;
|
---|
[041b026] | 793 | driver_t *drv;
|
---|
| 794 | devman_handle_t handle;
|
---|
[a35b458] | 795 |
|
---|
[041b026] | 796 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_fun_online(%p)", fun);
|
---|
| 797 |
|
---|
| 798 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
[a35b458] | 799 |
|
---|
[041b026] | 800 | if (fun->dev == NULL) {
|
---|
| 801 | /* XXX root function? */
|
---|
| 802 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
| 803 | return EINVAL;
|
---|
| 804 | }
|
---|
[a35b458] | 805 |
|
---|
[041b026] | 806 | drv = fun->dev->drv;
|
---|
| 807 | handle = fun->handle;
|
---|
| 808 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
[a35b458] | 809 |
|
---|
[041b026] | 810 | exch = async_exchange_begin(drv->sess);
|
---|
| 811 | retval = async_req_1_0(exch, DRIVER_FUN_ONLINE, handle);
|
---|
| 812 | loc_exchange_end(exch);
|
---|
[a35b458] | 813 |
|
---|
[041b026] | 814 | return retval;
|
---|
| 815 | }
|
---|
| 816 |
|
---|
[b7fd2a0] | 817 | errno_t driver_fun_offline(dev_tree_t *tree, fun_node_t *fun)
|
---|
[041b026] | 818 | {
|
---|
| 819 | async_exch_t *exch;
|
---|
[b7fd2a0] | 820 | errno_t retval;
|
---|
[041b026] | 821 | driver_t *drv;
|
---|
| 822 | devman_handle_t handle;
|
---|
[a35b458] | 823 |
|
---|
[041b026] | 824 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_fun_offline(%p)", fun);
|
---|
| 825 |
|
---|
| 826 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
| 827 | if (fun->dev == NULL) {
|
---|
| 828 | /* XXX root function? */
|
---|
| 829 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
| 830 | return EINVAL;
|
---|
| 831 | }
|
---|
[a35b458] | 832 |
|
---|
[041b026] | 833 | drv = fun->dev->drv;
|
---|
| 834 | handle = fun->handle;
|
---|
| 835 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
[a35b458] | 836 |
|
---|
[041b026] | 837 | exch = async_exchange_begin(drv->sess);
|
---|
| 838 | retval = async_req_1_0(exch, DRIVER_FUN_OFFLINE, handle);
|
---|
| 839 | loc_exchange_end(exch);
|
---|
[a35b458] | 840 |
|
---|
[041b026] | 841 | return retval;
|
---|
| 842 |
|
---|
| 843 | }
|
---|
| 844 |
|
---|
[0511549] | 845 | /** Get list of registered drivers. */
|
---|
[b7fd2a0] | 846 | errno_t driver_get_list(driver_list_t *driver_list, devman_handle_t *hdl_buf,
|
---|
[0511549] | 847 | size_t buf_size, size_t *act_size)
|
---|
| 848 | {
|
---|
| 849 | size_t act_cnt;
|
---|
| 850 | size_t buf_cnt;
|
---|
| 851 |
|
---|
| 852 | fibril_mutex_lock(&driver_list->drivers_mutex);
|
---|
| 853 |
|
---|
| 854 | buf_cnt = buf_size / sizeof(devman_handle_t);
|
---|
| 855 |
|
---|
| 856 | act_cnt = list_count(&driver_list->drivers);
|
---|
| 857 | *act_size = act_cnt * sizeof(devman_handle_t);
|
---|
| 858 |
|
---|
| 859 | if (buf_size % sizeof(devman_handle_t) != 0) {
|
---|
| 860 | fibril_mutex_unlock(&driver_list->drivers_mutex);
|
---|
| 861 | return EINVAL;
|
---|
| 862 | }
|
---|
| 863 |
|
---|
| 864 | size_t pos = 0;
|
---|
| 865 | list_foreach(driver_list->drivers, drivers, driver_t, drv) {
|
---|
| 866 | if (pos < buf_cnt) {
|
---|
| 867 | hdl_buf[pos] = drv->handle;
|
---|
| 868 | }
|
---|
| 869 |
|
---|
| 870 | pos++;
|
---|
| 871 | }
|
---|
| 872 |
|
---|
| 873 | fibril_mutex_unlock(&driver_list->drivers_mutex);
|
---|
| 874 | return EOK;
|
---|
| 875 | }
|
---|
| 876 |
|
---|
[1db5669] | 877 | /** Get list of device functions. */
|
---|
[b7fd2a0] | 878 | errno_t driver_get_devices(driver_t *driver, devman_handle_t *hdl_buf,
|
---|
[1db5669] | 879 | size_t buf_size, size_t *act_size)
|
---|
| 880 | {
|
---|
| 881 | size_t act_cnt;
|
---|
| 882 | size_t buf_cnt;
|
---|
| 883 |
|
---|
| 884 | fibril_mutex_lock(&driver->driver_mutex);
|
---|
| 885 |
|
---|
| 886 | buf_cnt = buf_size / sizeof(devman_handle_t);
|
---|
| 887 |
|
---|
| 888 | act_cnt = list_count(&driver->devices);
|
---|
| 889 | *act_size = act_cnt * sizeof(devman_handle_t);
|
---|
| 890 |
|
---|
| 891 | if (buf_size % sizeof(devman_handle_t) != 0) {
|
---|
| 892 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
| 893 | return EINVAL;
|
---|
| 894 | }
|
---|
| 895 |
|
---|
| 896 | size_t pos = 0;
|
---|
| 897 | list_foreach(driver->devices, driver_devices, dev_node_t, dev) {
|
---|
| 898 | if (pos < buf_cnt) {
|
---|
| 899 | hdl_buf[pos] = dev->handle;
|
---|
| 900 | }
|
---|
| 901 |
|
---|
| 902 | pos++;
|
---|
| 903 | }
|
---|
| 904 |
|
---|
| 905 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
| 906 | return EOK;
|
---|
| 907 | }
|
---|
| 908 |
|
---|
[b25970f] | 909 | /** Try to find next available driver in a separate fibril.
|
---|
| 910 | *
|
---|
| 911 | * @param arg Device node (dev_node_t)
|
---|
| 912 | */
|
---|
| 913 | static errno_t driver_reassign_fibril(void *arg)
|
---|
| 914 | {
|
---|
| 915 | dev_node_t *dev_node = (dev_node_t *) arg;
|
---|
| 916 | assign_driver(dev_node, &drivers_list, &device_tree);
|
---|
| 917 |
|
---|
| 918 | /* Delete one reference we got from the caller. */
|
---|
| 919 | dev_del_ref(dev_node);
|
---|
| 920 | return EOK;
|
---|
| 921 | }
|
---|
| 922 |
|
---|
[041b026] | 923 | /** @}
|
---|
| 924 | */
|
---|