[041b026] | 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 |
|
---|
| 29 | /** @addtogroup devman
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
[d1bafbf] | 33 | #include <dirent.h>
|
---|
[041b026] | 34 | #include <errno.h>
|
---|
| 35 | #include <fcntl.h>
|
---|
| 36 | #include <sys/stat.h>
|
---|
| 37 | #include <io/log.h>
|
---|
| 38 | #include <ipc/driver.h>
|
---|
| 39 | #include <loc.h>
|
---|
| 40 | #include <str_error.h>
|
---|
| 41 | #include <stdio.h>
|
---|
| 42 |
|
---|
[d1bafbf] | 43 | #include "dev.h"
|
---|
[041b026] | 44 | #include "devman.h"
|
---|
| 45 | #include "driver.h"
|
---|
| 46 |
|
---|
| 47 | /**
|
---|
| 48 | * Initialize the list of device driver's.
|
---|
| 49 | *
|
---|
| 50 | * @param drv_list the list of device driver's.
|
---|
| 51 | *
|
---|
| 52 | */
|
---|
| 53 | void init_driver_list(driver_list_t *drv_list)
|
---|
| 54 | {
|
---|
| 55 | assert(drv_list != NULL);
|
---|
| 56 |
|
---|
| 57 | list_initialize(&drv_list->drivers);
|
---|
| 58 | fibril_mutex_initialize(&drv_list->drivers_mutex);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /** Allocate and initialize a new driver structure.
|
---|
| 62 | *
|
---|
| 63 | * @return Driver structure.
|
---|
| 64 | */
|
---|
| 65 | driver_t *create_driver(void)
|
---|
| 66 | {
|
---|
| 67 | driver_t *res = malloc(sizeof(driver_t));
|
---|
| 68 | if (res != NULL)
|
---|
| 69 | init_driver(res);
|
---|
| 70 | return res;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | /** Add a driver to the list of drivers.
|
---|
| 74 | *
|
---|
| 75 | * @param drivers_list List of drivers.
|
---|
| 76 | * @param drv Driver structure.
|
---|
| 77 | */
|
---|
| 78 | void add_driver(driver_list_t *drivers_list, driver_t *drv)
|
---|
| 79 | {
|
---|
| 80 | fibril_mutex_lock(&drivers_list->drivers_mutex);
|
---|
| 81 | list_prepend(&drv->drivers, &drivers_list->drivers);
|
---|
| 82 | fibril_mutex_unlock(&drivers_list->drivers_mutex);
|
---|
| 83 |
|
---|
| 84 | log_msg(LOG_DEFAULT, LVL_NOTE, "Driver `%s' was added to the list of available "
|
---|
| 85 | "drivers.", drv->name);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | /**
|
---|
| 89 | * Get information about a driver.
|
---|
| 90 | *
|
---|
| 91 | * Each driver has its own directory in the base directory.
|
---|
| 92 | * The name of the driver's directory is the same as the name of the driver.
|
---|
| 93 | * The driver's directory contains driver's binary (named as the driver without
|
---|
| 94 | * extension) and the configuration file with match ids for device-to-driver
|
---|
| 95 | * matching (named as the driver with a special extension).
|
---|
| 96 | *
|
---|
| 97 | * This function searches for the driver's directory and containing
|
---|
| 98 | * configuration files. If all the files needed are found, they are parsed and
|
---|
| 99 | * the information about the driver is stored in the driver's structure.
|
---|
| 100 | *
|
---|
| 101 | * @param base_path The base directory, in which we look for driver's
|
---|
| 102 | * subdirectory.
|
---|
| 103 | * @param name The name of the driver.
|
---|
| 104 | * @param drv The driver structure to fill information in.
|
---|
| 105 | *
|
---|
| 106 | * @return True on success, false otherwise.
|
---|
| 107 | */
|
---|
| 108 | bool get_driver_info(const char *base_path, const char *name, driver_t *drv)
|
---|
| 109 | {
|
---|
| 110 | log_msg(LOG_DEFAULT, LVL_DEBUG, "get_driver_info(base_path=\"%s\", name=\"%s\")",
|
---|
| 111 | base_path, name);
|
---|
| 112 |
|
---|
| 113 | assert(base_path != NULL && name != NULL && drv != NULL);
|
---|
| 114 |
|
---|
| 115 | bool suc = false;
|
---|
| 116 | char *match_path = NULL;
|
---|
| 117 | size_t name_size = 0;
|
---|
| 118 |
|
---|
| 119 | /* Read the list of match ids from the driver's configuration file. */
|
---|
| 120 | match_path = get_abs_path(base_path, name, MATCH_EXT);
|
---|
| 121 | if (match_path == NULL)
|
---|
| 122 | goto cleanup;
|
---|
| 123 |
|
---|
| 124 | if (!read_match_ids(match_path, &drv->match_ids))
|
---|
| 125 | goto cleanup;
|
---|
| 126 |
|
---|
| 127 | /* Allocate and fill driver's name. */
|
---|
| 128 | name_size = str_size(name) + 1;
|
---|
| 129 | drv->name = malloc(name_size);
|
---|
| 130 | if (drv->name == NULL)
|
---|
| 131 | goto cleanup;
|
---|
| 132 | str_cpy(drv->name, name_size, name);
|
---|
| 133 |
|
---|
| 134 | /* Initialize path with driver's binary. */
|
---|
| 135 | drv->binary_path = get_abs_path(base_path, name, "");
|
---|
| 136 | if (drv->binary_path == NULL)
|
---|
| 137 | goto cleanup;
|
---|
| 138 |
|
---|
| 139 | /* Check whether the driver's binary exists. */
|
---|
| 140 | struct stat s;
|
---|
| 141 | if (stat(drv->binary_path, &s) == ENOENT) { /* FIXME!! */
|
---|
| 142 | log_msg(LOG_DEFAULT, LVL_ERROR, "Driver not found at path `%s'.",
|
---|
| 143 | drv->binary_path);
|
---|
| 144 | goto cleanup;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | suc = true;
|
---|
| 148 |
|
---|
| 149 | cleanup:
|
---|
| 150 | if (!suc) {
|
---|
| 151 | free(drv->binary_path);
|
---|
| 152 | free(drv->name);
|
---|
| 153 | /* Set the driver structure to the default state. */
|
---|
| 154 | init_driver(drv);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | free(match_path);
|
---|
| 158 |
|
---|
| 159 | return suc;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | /** Lookup drivers in the directory.
|
---|
| 163 | *
|
---|
| 164 | * @param drivers_list The list of available drivers.
|
---|
| 165 | * @param dir_path The path to the directory where we search for drivers.
|
---|
| 166 | * @return Number of drivers which were found.
|
---|
| 167 | */
|
---|
| 168 | int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path)
|
---|
| 169 | {
|
---|
| 170 | log_msg(LOG_DEFAULT, LVL_DEBUG, "lookup_available_drivers(dir=\"%s\")", dir_path);
|
---|
| 171 |
|
---|
| 172 | int drv_cnt = 0;
|
---|
| 173 | DIR *dir = NULL;
|
---|
| 174 | struct dirent *diren;
|
---|
| 175 |
|
---|
| 176 | dir = opendir(dir_path);
|
---|
| 177 |
|
---|
| 178 | if (dir != NULL) {
|
---|
| 179 | driver_t *drv = create_driver();
|
---|
| 180 | while ((diren = readdir(dir))) {
|
---|
| 181 | if (get_driver_info(dir_path, diren->d_name, drv)) {
|
---|
| 182 | add_driver(drivers_list, drv);
|
---|
| 183 | drv_cnt++;
|
---|
| 184 | drv = create_driver();
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 | delete_driver(drv);
|
---|
| 188 | closedir(dir);
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | return drv_cnt;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | /** Lookup the best matching driver for the specified device in the list of
|
---|
| 195 | * drivers.
|
---|
| 196 | *
|
---|
| 197 | * A match between a device and a driver is found if one of the driver's match
|
---|
| 198 | * ids match one of the device's match ids. The score of the match is the
|
---|
| 199 | * product of the driver's and device's score associated with the matching id.
|
---|
| 200 | * The best matching driver for a device is the driver with the highest score
|
---|
| 201 | * of the match between the device and the driver.
|
---|
| 202 | *
|
---|
| 203 | * @param drivers_list The list of drivers, where we look for the driver
|
---|
| 204 | * suitable for handling the device.
|
---|
| 205 | * @param node The device node structure of the device.
|
---|
| 206 | * @return The best matching driver or NULL if no matching driver
|
---|
| 207 | * is found.
|
---|
| 208 | */
|
---|
| 209 | driver_t *find_best_match_driver(driver_list_t *drivers_list, dev_node_t *node)
|
---|
| 210 | {
|
---|
| 211 | driver_t *best_drv = NULL, *drv = NULL;
|
---|
| 212 | int best_score = 0, score = 0;
|
---|
| 213 |
|
---|
| 214 | fibril_mutex_lock(&drivers_list->drivers_mutex);
|
---|
| 215 |
|
---|
| 216 | list_foreach(drivers_list->drivers, link) {
|
---|
| 217 | drv = list_get_instance(link, driver_t, drivers);
|
---|
| 218 | score = get_match_score(drv, node);
|
---|
| 219 | if (score > best_score) {
|
---|
| 220 | best_score = score;
|
---|
| 221 | best_drv = drv;
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | fibril_mutex_unlock(&drivers_list->drivers_mutex);
|
---|
| 226 |
|
---|
| 227 | return best_drv;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | /** Assign a driver to a device.
|
---|
| 231 | *
|
---|
| 232 | * @param tree Device tree
|
---|
| 233 | * @param node The device's node in the device tree.
|
---|
| 234 | * @param drv The driver.
|
---|
| 235 | */
|
---|
| 236 | void attach_driver(dev_tree_t *tree, dev_node_t *dev, driver_t *drv)
|
---|
| 237 | {
|
---|
| 238 | log_msg(LOG_DEFAULT, LVL_DEBUG, "attach_driver(dev=\"%s\",drv=\"%s\")",
|
---|
| 239 | dev->pfun->pathname, drv->name);
|
---|
| 240 |
|
---|
| 241 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
| 242 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
| 243 |
|
---|
| 244 | dev->drv = drv;
|
---|
| 245 | list_append(&dev->driver_devices, &drv->devices);
|
---|
| 246 |
|
---|
| 247 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
| 248 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | /** Detach driver from device.
|
---|
| 252 | *
|
---|
| 253 | * @param tree Device tree
|
---|
| 254 | * @param node The device's node in the device tree.
|
---|
| 255 | * @param drv The driver.
|
---|
| 256 | */
|
---|
| 257 | void detach_driver(dev_tree_t *tree, dev_node_t *dev)
|
---|
| 258 | {
|
---|
| 259 | driver_t *drv = dev->drv;
|
---|
| 260 |
|
---|
| 261 | assert(drv != NULL);
|
---|
| 262 |
|
---|
| 263 | log_msg(LOG_DEFAULT, LVL_DEBUG, "detach_driver(dev=\"%s\",drv=\"%s\")",
|
---|
| 264 | dev->pfun->pathname, drv->name);
|
---|
| 265 |
|
---|
| 266 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
| 267 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
| 268 |
|
---|
| 269 | dev->drv = NULL;
|
---|
| 270 | list_remove(&dev->driver_devices);
|
---|
| 271 |
|
---|
| 272 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
| 273 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | /** Start a driver
|
---|
| 277 | *
|
---|
| 278 | * @param drv The driver's structure.
|
---|
| 279 | * @return True if the driver's task is successfully spawned, false
|
---|
| 280 | * otherwise.
|
---|
| 281 | */
|
---|
| 282 | bool start_driver(driver_t *drv)
|
---|
| 283 | {
|
---|
| 284 | int rc;
|
---|
| 285 |
|
---|
| 286 | assert(fibril_mutex_is_locked(&drv->driver_mutex));
|
---|
| 287 |
|
---|
| 288 | log_msg(LOG_DEFAULT, LVL_DEBUG, "start_driver(drv=\"%s\")", drv->name);
|
---|
| 289 |
|
---|
| 290 | rc = task_spawnl(NULL, drv->binary_path, drv->binary_path, NULL);
|
---|
| 291 | if (rc != EOK) {
|
---|
| 292 | log_msg(LOG_DEFAULT, LVL_ERROR, "Spawning driver `%s' (%s) failed: %s.",
|
---|
| 293 | drv->name, drv->binary_path, str_error(rc));
|
---|
| 294 | return false;
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | drv->state = DRIVER_STARTING;
|
---|
| 298 | return true;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | /** Find device driver in the list of device drivers.
|
---|
| 302 | *
|
---|
| 303 | * @param drv_list The list of device drivers.
|
---|
| 304 | * @param drv_name The name of the device driver which is searched.
|
---|
| 305 | * @return The device driver of the specified name, if it is in the
|
---|
| 306 | * list, NULL otherwise.
|
---|
| 307 | */
|
---|
| 308 | driver_t *find_driver(driver_list_t *drv_list, const char *drv_name)
|
---|
| 309 | {
|
---|
| 310 | driver_t *res = NULL;
|
---|
| 311 | driver_t *drv = NULL;
|
---|
| 312 |
|
---|
| 313 | fibril_mutex_lock(&drv_list->drivers_mutex);
|
---|
| 314 |
|
---|
| 315 | list_foreach(drv_list->drivers, link) {
|
---|
| 316 | drv = list_get_instance(link, driver_t, drivers);
|
---|
| 317 | if (str_cmp(drv->name, drv_name) == 0) {
|
---|
| 318 | res = drv;
|
---|
| 319 | break;
|
---|
| 320 | }
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | fibril_mutex_unlock(&drv_list->drivers_mutex);
|
---|
| 324 |
|
---|
| 325 | return res;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | /** Notify driver about the devices to which it was assigned.
|
---|
| 329 | *
|
---|
| 330 | * @param driver The driver to which the devices are passed.
|
---|
| 331 | */
|
---|
| 332 | static void pass_devices_to_driver(driver_t *driver, dev_tree_t *tree)
|
---|
| 333 | {
|
---|
| 334 | dev_node_t *dev;
|
---|
| 335 | link_t *link;
|
---|
| 336 |
|
---|
| 337 | log_msg(LOG_DEFAULT, LVL_DEBUG, "pass_devices_to_driver(driver=\"%s\")",
|
---|
| 338 | driver->name);
|
---|
| 339 |
|
---|
| 340 | fibril_mutex_lock(&driver->driver_mutex);
|
---|
| 341 |
|
---|
| 342 | /*
|
---|
| 343 | * Go through devices list as long as there is some device
|
---|
| 344 | * that has not been passed to the driver.
|
---|
| 345 | */
|
---|
| 346 | link = driver->devices.head.next;
|
---|
| 347 | while (link != &driver->devices.head) {
|
---|
| 348 | dev = list_get_instance(link, dev_node_t, driver_devices);
|
---|
| 349 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
| 350 |
|
---|
| 351 | if (dev->passed_to_driver) {
|
---|
| 352 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
| 353 | link = link->next;
|
---|
| 354 | continue;
|
---|
| 355 | }
|
---|
| 356 |
|
---|
| 357 | log_msg(LOG_DEFAULT, LVL_DEBUG, "pass_devices_to_driver: dev->refcnt=%d\n",
|
---|
| 358 | (int)atomic_get(&dev->refcnt));
|
---|
| 359 | dev_add_ref(dev);
|
---|
| 360 |
|
---|
| 361 | /*
|
---|
| 362 | * Unlock to avoid deadlock when adding device
|
---|
| 363 | * handled by itself.
|
---|
| 364 | */
|
---|
| 365 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
| 366 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
| 367 |
|
---|
| 368 | add_device(driver, dev, tree);
|
---|
| 369 |
|
---|
| 370 | dev_del_ref(dev);
|
---|
| 371 |
|
---|
| 372 | /*
|
---|
| 373 | * Lock again as we will work with driver's
|
---|
| 374 | * structure.
|
---|
| 375 | */
|
---|
| 376 | fibril_mutex_lock(&driver->driver_mutex);
|
---|
| 377 |
|
---|
| 378 | /*
|
---|
| 379 | * Restart the cycle to go through all devices again.
|
---|
| 380 | */
|
---|
| 381 | link = driver->devices.head.next;
|
---|
| 382 | }
|
---|
| 383 |
|
---|
| 384 | /*
|
---|
| 385 | * Once we passed all devices to the driver, we need to mark the
|
---|
| 386 | * driver as running.
|
---|
| 387 | * It is vital to do it here and inside critical section.
|
---|
| 388 | *
|
---|
| 389 | * If we would change the state earlier, other devices added to
|
---|
| 390 | * the driver would be added to the device list and started
|
---|
| 391 | * immediately and possibly started here as well.
|
---|
| 392 | */
|
---|
| 393 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Driver `%s' enters running state.", driver->name);
|
---|
| 394 | driver->state = DRIVER_RUNNING;
|
---|
| 395 |
|
---|
| 396 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | /** Finish the initialization of a driver after it has succesfully started
|
---|
| 400 | * and after it has registered itself by the device manager.
|
---|
| 401 | *
|
---|
| 402 | * Pass devices formerly matched to the driver to the driver and remember the
|
---|
| 403 | * driver is running and fully functional now.
|
---|
| 404 | *
|
---|
| 405 | * @param driver The driver which registered itself as running by the
|
---|
| 406 | * device manager.
|
---|
| 407 | */
|
---|
| 408 | void initialize_running_driver(driver_t *driver, dev_tree_t *tree)
|
---|
| 409 | {
|
---|
| 410 | log_msg(LOG_DEFAULT, LVL_DEBUG, "initialize_running_driver(driver=\"%s\")",
|
---|
| 411 | driver->name);
|
---|
| 412 |
|
---|
| 413 | /*
|
---|
| 414 | * Pass devices which have been already assigned to the driver to the
|
---|
| 415 | * driver.
|
---|
| 416 | */
|
---|
| 417 | pass_devices_to_driver(driver, tree);
|
---|
| 418 | }
|
---|
| 419 |
|
---|
| 420 | /** Initialize device driver structure.
|
---|
| 421 | *
|
---|
| 422 | * @param drv The device driver structure.
|
---|
| 423 | */
|
---|
| 424 | void init_driver(driver_t *drv)
|
---|
| 425 | {
|
---|
| 426 | assert(drv != NULL);
|
---|
| 427 |
|
---|
| 428 | memset(drv, 0, sizeof(driver_t));
|
---|
| 429 | list_initialize(&drv->match_ids.ids);
|
---|
| 430 | list_initialize(&drv->devices);
|
---|
| 431 | fibril_mutex_initialize(&drv->driver_mutex);
|
---|
| 432 | drv->sess = NULL;
|
---|
| 433 | }
|
---|
| 434 |
|
---|
| 435 | /** Device driver structure clean-up.
|
---|
| 436 | *
|
---|
| 437 | * @param drv The device driver structure.
|
---|
| 438 | */
|
---|
| 439 | void clean_driver(driver_t *drv)
|
---|
| 440 | {
|
---|
| 441 | assert(drv != NULL);
|
---|
| 442 |
|
---|
| 443 | free(drv->name);
|
---|
| 444 | free(drv->binary_path);
|
---|
| 445 |
|
---|
| 446 | clean_match_ids(&drv->match_ids);
|
---|
| 447 |
|
---|
| 448 | init_driver(drv);
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | /** Delete device driver structure.
|
---|
| 452 | *
|
---|
| 453 | * @param drv The device driver structure.
|
---|
| 454 | */
|
---|
| 455 | void delete_driver(driver_t *drv)
|
---|
| 456 | {
|
---|
| 457 | assert(drv != NULL);
|
---|
| 458 |
|
---|
| 459 | clean_driver(drv);
|
---|
| 460 | free(drv);
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | /** Find suitable driver for a device and assign the driver to it.
|
---|
| 464 | *
|
---|
| 465 | * @param node The device node of the device in the device tree.
|
---|
| 466 | * @param drivers_list The list of available drivers.
|
---|
| 467 | * @return True if the suitable driver is found and
|
---|
| 468 | * successfully assigned to the device, false otherwise.
|
---|
| 469 | */
|
---|
| 470 | bool assign_driver(dev_node_t *dev, driver_list_t *drivers_list,
|
---|
| 471 | dev_tree_t *tree)
|
---|
| 472 | {
|
---|
| 473 | assert(dev != NULL);
|
---|
| 474 | assert(drivers_list != NULL);
|
---|
| 475 | assert(tree != NULL);
|
---|
| 476 |
|
---|
| 477 | /*
|
---|
| 478 | * Find the driver which is the most suitable for handling this device.
|
---|
| 479 | */
|
---|
| 480 | driver_t *drv = find_best_match_driver(drivers_list, dev);
|
---|
| 481 | if (drv == NULL) {
|
---|
| 482 | log_msg(LOG_DEFAULT, LVL_ERROR, "No driver found for device `%s'.",
|
---|
| 483 | dev->pfun->pathname);
|
---|
| 484 | return false;
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 | /* Attach the driver to the device. */
|
---|
| 488 | attach_driver(tree, dev, drv);
|
---|
| 489 |
|
---|
| 490 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
| 491 | if (drv->state == DRIVER_NOT_STARTED) {
|
---|
| 492 | /* Start the driver. */
|
---|
| 493 | start_driver(drv);
|
---|
| 494 | }
|
---|
| 495 | bool is_running = drv->state == DRIVER_RUNNING;
|
---|
| 496 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
| 497 |
|
---|
| 498 | /* Notify the driver about the new device. */
|
---|
| 499 | if (is_running)
|
---|
| 500 | add_device(drv, dev, tree);
|
---|
| 501 |
|
---|
| 502 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
| 503 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
| 504 |
|
---|
| 505 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
| 506 | if (dev->pfun != NULL) {
|
---|
| 507 | dev->pfun->state = FUN_ON_LINE;
|
---|
| 508 | }
|
---|
| 509 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
| 510 | return true;
|
---|
| 511 | }
|
---|
| 512 |
|
---|
| 513 | int driver_dev_remove(dev_tree_t *tree, dev_node_t *dev)
|
---|
| 514 | {
|
---|
| 515 | async_exch_t *exch;
|
---|
| 516 | sysarg_t retval;
|
---|
| 517 | driver_t *drv;
|
---|
| 518 | devman_handle_t handle;
|
---|
| 519 |
|
---|
| 520 | assert(dev != NULL);
|
---|
| 521 |
|
---|
| 522 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_remove(%p)", dev);
|
---|
| 523 |
|
---|
| 524 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
| 525 | drv = dev->drv;
|
---|
| 526 | handle = dev->handle;
|
---|
| 527 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
| 528 |
|
---|
| 529 | exch = async_exchange_begin(drv->sess);
|
---|
| 530 | retval = async_req_1_0(exch, DRIVER_DEV_REMOVE, handle);
|
---|
| 531 | async_exchange_end(exch);
|
---|
| 532 |
|
---|
| 533 | return retval;
|
---|
| 534 | }
|
---|
| 535 |
|
---|
| 536 | int driver_dev_gone(dev_tree_t *tree, dev_node_t *dev)
|
---|
| 537 | {
|
---|
| 538 | async_exch_t *exch;
|
---|
| 539 | sysarg_t retval;
|
---|
| 540 | driver_t *drv;
|
---|
| 541 | devman_handle_t handle;
|
---|
| 542 |
|
---|
| 543 | assert(dev != NULL);
|
---|
| 544 |
|
---|
| 545 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_gone(%p)", dev);
|
---|
| 546 |
|
---|
| 547 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
| 548 | drv = dev->drv;
|
---|
| 549 | handle = dev->handle;
|
---|
| 550 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
| 551 |
|
---|
| 552 | exch = async_exchange_begin(drv->sess);
|
---|
| 553 | retval = async_req_1_0(exch, DRIVER_DEV_GONE, handle);
|
---|
| 554 | async_exchange_end(exch);
|
---|
| 555 |
|
---|
| 556 | return retval;
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 | int driver_fun_online(dev_tree_t *tree, fun_node_t *fun)
|
---|
| 560 | {
|
---|
| 561 | async_exch_t *exch;
|
---|
| 562 | sysarg_t retval;
|
---|
| 563 | driver_t *drv;
|
---|
| 564 | devman_handle_t handle;
|
---|
| 565 |
|
---|
| 566 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_fun_online(%p)", fun);
|
---|
| 567 |
|
---|
| 568 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
| 569 |
|
---|
| 570 | if (fun->dev == NULL) {
|
---|
| 571 | /* XXX root function? */
|
---|
| 572 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
| 573 | return EINVAL;
|
---|
| 574 | }
|
---|
| 575 |
|
---|
| 576 | drv = fun->dev->drv;
|
---|
| 577 | handle = fun->handle;
|
---|
| 578 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
| 579 |
|
---|
| 580 | exch = async_exchange_begin(drv->sess);
|
---|
| 581 | retval = async_req_1_0(exch, DRIVER_FUN_ONLINE, handle);
|
---|
| 582 | loc_exchange_end(exch);
|
---|
| 583 |
|
---|
| 584 | return retval;
|
---|
| 585 | }
|
---|
| 586 |
|
---|
| 587 | int driver_fun_offline(dev_tree_t *tree, fun_node_t *fun)
|
---|
| 588 | {
|
---|
| 589 | async_exch_t *exch;
|
---|
| 590 | sysarg_t retval;
|
---|
| 591 | driver_t *drv;
|
---|
| 592 | devman_handle_t handle;
|
---|
| 593 |
|
---|
| 594 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_fun_offline(%p)", fun);
|
---|
| 595 |
|
---|
| 596 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
| 597 | if (fun->dev == NULL) {
|
---|
| 598 | /* XXX root function? */
|
---|
| 599 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
| 600 | return EINVAL;
|
---|
| 601 | }
|
---|
| 602 |
|
---|
| 603 | drv = fun->dev->drv;
|
---|
| 604 | handle = fun->handle;
|
---|
| 605 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
| 606 |
|
---|
| 607 | exch = async_exchange_begin(drv->sess);
|
---|
| 608 | retval = async_req_1_0(exch, DRIVER_FUN_OFFLINE, handle);
|
---|
| 609 | loc_exchange_end(exch);
|
---|
| 610 |
|
---|
| 611 | return retval;
|
---|
| 612 |
|
---|
| 613 | }
|
---|
| 614 |
|
---|
| 615 | /** @}
|
---|
| 616 | */
|
---|