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