| 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 | /**
|
|---|
| 30 | * @addtogroup devman
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 |
|
|---|
| 34 | /** @file
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <assert.h>
|
|---|
| 38 | #include <ipc/services.h>
|
|---|
| 39 | #include <ns.h>
|
|---|
| 40 | #include <async.h>
|
|---|
| 41 | #include <stdio.h>
|
|---|
| 42 | #include <errno.h>
|
|---|
| 43 | #include <str_error.h>
|
|---|
| 44 | #include <stdbool.h>
|
|---|
| 45 | #include <fibril_synch.h>
|
|---|
| 46 | #include <stdlib.h>
|
|---|
| 47 | #include <str.h>
|
|---|
| 48 | #include <io/log.h>
|
|---|
| 49 | #include <ipc/devman.h>
|
|---|
| 50 | #include <loc.h>
|
|---|
| 51 |
|
|---|
| 52 | #include "client_conn.h"
|
|---|
| 53 | #include "dev.h"
|
|---|
| 54 | #include "devman.h"
|
|---|
| 55 | #include "devtree.h"
|
|---|
| 56 | #include "driver.h"
|
|---|
| 57 | #include "drv_conn.h"
|
|---|
| 58 | #include "fun.h"
|
|---|
| 59 | #include "loc.h"
|
|---|
| 60 | #include "main.h"
|
|---|
| 61 |
|
|---|
| 62 | static errno_t init_running_drv(void *drv);
|
|---|
| 63 |
|
|---|
| 64 | /** Register running driver. */
|
|---|
| 65 | static driver_t *devman_driver_register(ipc_call_t *call)
|
|---|
| 66 | {
|
|---|
| 67 | driver_t *driver = NULL;
|
|---|
| 68 | char *drv_name = NULL;
|
|---|
| 69 |
|
|---|
| 70 | log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_driver_register");
|
|---|
| 71 |
|
|---|
| 72 | /* Get driver name. */
|
|---|
| 73 | errno_t rc = async_data_write_accept((void **) &drv_name, true, 0, 0, 0, 0);
|
|---|
| 74 | if (rc != EOK) {
|
|---|
| 75 | async_answer_0(call, rc);
|
|---|
| 76 | return NULL;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | log_msg(LOG_DEFAULT, LVL_DEBUG, "The `%s' driver is trying to register.",
|
|---|
| 80 | drv_name);
|
|---|
| 81 |
|
|---|
| 82 | /* Find driver structure. */
|
|---|
| 83 | driver = driver_find_by_name(&drivers_list, drv_name);
|
|---|
| 84 | if (driver == NULL) {
|
|---|
| 85 | log_msg(LOG_DEFAULT, LVL_ERROR, "No driver named `%s' was found.", drv_name);
|
|---|
| 86 | free(drv_name);
|
|---|
| 87 | drv_name = NULL;
|
|---|
| 88 | async_answer_0(call, ENOENT);
|
|---|
| 89 | return NULL;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | free(drv_name);
|
|---|
| 93 | drv_name = NULL;
|
|---|
| 94 |
|
|---|
| 95 | fibril_mutex_lock(&driver->driver_mutex);
|
|---|
| 96 |
|
|---|
| 97 | if (driver->sess) {
|
|---|
| 98 | /* We already have a connection to the driver. */
|
|---|
| 99 | log_msg(LOG_DEFAULT, LVL_ERROR, "Driver '%s' already started.\n",
|
|---|
| 100 | driver->name);
|
|---|
| 101 | fibril_mutex_unlock(&driver->driver_mutex);
|
|---|
| 102 | async_answer_0(call, EEXIST);
|
|---|
| 103 | return NULL;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | switch (driver->state) {
|
|---|
| 107 | case DRIVER_NOT_STARTED:
|
|---|
| 108 | /* Somebody started the driver manually. */
|
|---|
| 109 | log_msg(LOG_DEFAULT, LVL_NOTE, "Driver '%s' started manually.\n",
|
|---|
| 110 | driver->name);
|
|---|
| 111 | driver->state = DRIVER_STARTING;
|
|---|
| 112 | break;
|
|---|
| 113 | case DRIVER_STARTING:
|
|---|
| 114 | /* The expected case */
|
|---|
| 115 | break;
|
|---|
| 116 | case DRIVER_RUNNING:
|
|---|
| 117 | /* Should not happen since we do not have a connected session */
|
|---|
| 118 | assert(false);
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | /* Create connection to the driver. */
|
|---|
| 122 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Creating connection to the `%s' driver.",
|
|---|
| 123 | driver->name);
|
|---|
| 124 | driver->sess = async_callback_receive(EXCHANGE_PARALLEL);
|
|---|
| 125 | if (!driver->sess) {
|
|---|
| 126 | fibril_mutex_unlock(&driver->driver_mutex);
|
|---|
| 127 | async_answer_0(call, ENOTSUP);
|
|---|
| 128 | return NULL;
|
|---|
| 129 | }
|
|---|
| 130 | /* FIXME: Work around problem with callback sessions */
|
|---|
| 131 | async_sess_args_set(driver->sess, INTERFACE_DDF_DEVMAN, 0, 0);
|
|---|
| 132 |
|
|---|
| 133 | log_msg(LOG_DEFAULT, LVL_NOTE,
|
|---|
| 134 | "The `%s' driver was successfully registered as running.",
|
|---|
| 135 | driver->name);
|
|---|
| 136 |
|
|---|
| 137 | /*
|
|---|
| 138 | * Initialize the driver as running (e.g. pass assigned devices to it)
|
|---|
| 139 | * in a separate fibril; the separate fibril is used to enable the
|
|---|
| 140 | * driver to use devman service during the driver's initialization.
|
|---|
| 141 | */
|
|---|
| 142 | fid_t fid = fibril_create(init_running_drv, driver);
|
|---|
| 143 | if (fid == 0) {
|
|---|
| 144 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to create initialization fibril "
|
|---|
| 145 | "for driver `%s'.", driver->name);
|
|---|
| 146 | fibril_mutex_unlock(&driver->driver_mutex);
|
|---|
| 147 | async_answer_0(call, ENOMEM);
|
|---|
| 148 | return NULL;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | fibril_add_ready(fid);
|
|---|
| 152 | fibril_mutex_unlock(&driver->driver_mutex);
|
|---|
| 153 |
|
|---|
| 154 | async_answer_0(call, EOK);
|
|---|
| 155 | return driver;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | /** Receive device match ID from the device's parent driver and add it to the
|
|---|
| 159 | * list of devices match ids.
|
|---|
| 160 | *
|
|---|
| 161 | * @param match_ids The list of the device's match ids.
|
|---|
| 162 | * @return Zero on success, error code otherwise.
|
|---|
| 163 | */
|
|---|
| 164 | static errno_t devman_receive_match_id(match_id_list_t *match_ids)
|
|---|
| 165 | {
|
|---|
| 166 | match_id_t *match_id = create_match_id();
|
|---|
| 167 | ipc_call_t call;
|
|---|
| 168 | errno_t rc = 0;
|
|---|
| 169 |
|
|---|
| 170 | async_get_call(&call);
|
|---|
| 171 | if (DEVMAN_ADD_MATCH_ID != ipc_get_imethod(&call)) {
|
|---|
| 172 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
|---|
| 173 | "Invalid protocol when trying to receive match id.");
|
|---|
| 174 | async_answer_0(&call, EINVAL);
|
|---|
| 175 | delete_match_id(match_id);
|
|---|
| 176 | return EINVAL;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | if (match_id == NULL) {
|
|---|
| 180 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to allocate match id.");
|
|---|
| 181 | async_answer_0(&call, ENOMEM);
|
|---|
| 182 | return ENOMEM;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | async_answer_0(&call, EOK);
|
|---|
| 186 |
|
|---|
| 187 | match_id->score = ipc_get_arg1(&call);
|
|---|
| 188 |
|
|---|
| 189 | char *match_id_str;
|
|---|
| 190 | rc = async_data_write_accept((void **) &match_id_str, true, 0, 0, 0, 0);
|
|---|
| 191 | match_id->id = match_id_str;
|
|---|
| 192 | if (rc != EOK) {
|
|---|
| 193 | delete_match_id(match_id);
|
|---|
| 194 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to receive match id string: %s.",
|
|---|
| 195 | str_error(rc));
|
|---|
| 196 | return rc;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | list_append(&match_id->link, &match_ids->ids);
|
|---|
| 200 |
|
|---|
| 201 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Received match id `%s', score %d.",
|
|---|
| 202 | match_id->id, match_id->score);
|
|---|
| 203 | return rc;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | /** Receive device match IDs from the device's parent driver and add them to the
|
|---|
| 207 | * list of devices match ids.
|
|---|
| 208 | *
|
|---|
| 209 | * @param match_count The number of device's match ids to be received.
|
|---|
| 210 | * @param match_ids The list of the device's match ids.
|
|---|
| 211 | * @return Zero on success, error code otherwise.
|
|---|
| 212 | */
|
|---|
| 213 | static errno_t devman_receive_match_ids(sysarg_t match_count,
|
|---|
| 214 | match_id_list_t *match_ids)
|
|---|
| 215 | {
|
|---|
| 216 | errno_t ret = EOK;
|
|---|
| 217 | size_t i;
|
|---|
| 218 |
|
|---|
| 219 | for (i = 0; i < match_count; i++) {
|
|---|
| 220 | if (EOK != (ret = devman_receive_match_id(match_ids)))
|
|---|
| 221 | return ret;
|
|---|
| 222 | }
|
|---|
| 223 | return ret;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | /** Handle function registration.
|
|---|
| 227 | *
|
|---|
| 228 | * Child devices are registered by their parent's device driver.
|
|---|
| 229 | */
|
|---|
| 230 | static void devman_add_function(ipc_call_t *call)
|
|---|
| 231 | {
|
|---|
| 232 | fun_type_t ftype = (fun_type_t) ipc_get_arg1(call);
|
|---|
| 233 | devman_handle_t dev_handle = ipc_get_arg2(call);
|
|---|
| 234 | sysarg_t match_count = ipc_get_arg3(call);
|
|---|
| 235 | dev_tree_t *tree = &device_tree;
|
|---|
| 236 |
|
|---|
| 237 | dev_node_t *pdev = find_dev_node(&device_tree, dev_handle);
|
|---|
| 238 | if (pdev == NULL) {
|
|---|
| 239 | async_answer_0(call, ENOENT);
|
|---|
| 240 | return;
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | if (ftype != fun_inner && ftype != fun_exposed) {
|
|---|
| 244 | /* Unknown function type */
|
|---|
| 245 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
|---|
| 246 | "Unknown function type %d provided by driver.",
|
|---|
| 247 | (int) ftype);
|
|---|
| 248 |
|
|---|
| 249 | dev_del_ref(pdev);
|
|---|
| 250 | async_answer_0(call, EINVAL);
|
|---|
| 251 | return;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | char *fun_name = NULL;
|
|---|
| 255 | errno_t rc = async_data_write_accept((void **) &fun_name, true, 0, 0, 0, 0);
|
|---|
| 256 | if (rc != EOK) {
|
|---|
| 257 | dev_del_ref(pdev);
|
|---|
| 258 | async_answer_0(call, rc);
|
|---|
| 259 | return;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | fibril_rwlock_write_lock(&tree->rwlock);
|
|---|
| 263 |
|
|---|
| 264 | /* Check device state */
|
|---|
| 265 | if (pdev->state == DEVICE_REMOVED) {
|
|---|
| 266 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 267 | dev_del_ref(pdev);
|
|---|
| 268 | async_answer_0(call, ENOENT);
|
|---|
| 269 | return;
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | /* Check that function with same name is not there already. */
|
|---|
| 273 | fun_node_t *tfun = find_fun_node_in_device(tree, pdev, fun_name);
|
|---|
| 274 | if (tfun) {
|
|---|
| 275 | fun_del_ref(tfun); /* drop the new unwanted reference */
|
|---|
| 276 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 277 | dev_del_ref(pdev);
|
|---|
| 278 | async_answer_0(call, EEXIST);
|
|---|
| 279 | printf(NAME ": Warning, driver tried to register `%s' twice.\n",
|
|---|
| 280 | fun_name);
|
|---|
| 281 | free(fun_name);
|
|---|
| 282 | return;
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | fun_node_t *fun = create_fun_node();
|
|---|
| 286 | /*
|
|---|
| 287 | * Hold a temporary reference while we work with fun. The reference from
|
|---|
| 288 | * create_fun_node() moves to the device tree.
|
|---|
| 289 | */
|
|---|
| 290 | fun_add_ref(fun);
|
|---|
| 291 | fun->ftype = ftype;
|
|---|
| 292 |
|
|---|
| 293 | /*
|
|---|
| 294 | * We can lock the function here even when holding the tree because
|
|---|
| 295 | * we know it cannot be held by anyone else yet.
|
|---|
| 296 | */
|
|---|
| 297 | fun_busy_lock(fun);
|
|---|
| 298 |
|
|---|
| 299 | if (!insert_fun_node(&device_tree, fun, fun_name, pdev)) {
|
|---|
| 300 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 301 | dev_del_ref(pdev);
|
|---|
| 302 | fun_busy_unlock(fun);
|
|---|
| 303 | fun_del_ref(fun);
|
|---|
| 304 | fun_del_ref(fun); /* fun is destroyed */
|
|---|
| 305 | async_answer_0(call, ENOMEM);
|
|---|
| 306 | return;
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 310 | dev_del_ref(pdev);
|
|---|
| 311 |
|
|---|
| 312 | devman_receive_match_ids(match_count, &fun->match_ids);
|
|---|
| 313 |
|
|---|
| 314 | rc = fun_online(fun);
|
|---|
| 315 | if (rc != EOK) {
|
|---|
| 316 | /* XXX Set some failed state? */
|
|---|
| 317 | fun_busy_unlock(fun);
|
|---|
| 318 | fun_del_ref(fun);
|
|---|
| 319 | async_answer_0(call, rc);
|
|---|
| 320 | return;
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | fun_busy_unlock(fun);
|
|---|
| 324 | fun_del_ref(fun);
|
|---|
| 325 |
|
|---|
| 326 | /* Return device handle to parent's driver. */
|
|---|
| 327 | async_answer_1(call, EOK, fun->handle);
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 330 | static void devman_add_function_to_cat(ipc_call_t *call)
|
|---|
| 331 | {
|
|---|
| 332 | devman_handle_t handle = ipc_get_arg1(call);
|
|---|
| 333 | category_id_t cat_id;
|
|---|
| 334 | errno_t rc;
|
|---|
| 335 |
|
|---|
| 336 | /* Get category name. */
|
|---|
| 337 | char *cat_name;
|
|---|
| 338 | rc = async_data_write_accept((void **) &cat_name, true,
|
|---|
| 339 | 0, 0, 0, 0);
|
|---|
| 340 | if (rc != EOK) {
|
|---|
| 341 | async_answer_0(call, rc);
|
|---|
| 342 | return;
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 | fun_node_t *fun = find_fun_node(&device_tree, handle);
|
|---|
| 346 | if (fun == NULL) {
|
|---|
| 347 | async_answer_0(call, ENOENT);
|
|---|
| 348 | return;
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
|---|
| 352 |
|
|---|
| 353 | /* Check function state */
|
|---|
| 354 | if (fun->state == FUN_REMOVED) {
|
|---|
| 355 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 356 | async_answer_0(call, ENOENT);
|
|---|
| 357 | return;
|
|---|
| 358 | }
|
|---|
| 359 |
|
|---|
| 360 | rc = loc_category_get_id(cat_name, &cat_id, IPC_FLAG_BLOCKING);
|
|---|
| 361 | if (rc == EOK)
|
|---|
| 362 | rc = loc_service_add_to_cat(fun->service_id, cat_id);
|
|---|
| 363 | if (rc == EOK) {
|
|---|
| 364 | log_msg(LOG_DEFAULT, LVL_NOTE, "Function `%s' added to category `%s'.",
|
|---|
| 365 | fun->pathname, cat_name);
|
|---|
| 366 | } else {
|
|---|
| 367 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding function `%s' to category "
|
|---|
| 368 | "`%s'.", fun->pathname, cat_name);
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 372 | fun_del_ref(fun);
|
|---|
| 373 |
|
|---|
| 374 | async_answer_0(call, rc);
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | /** Online function by driver request.
|
|---|
| 378 | *
|
|---|
| 379 | */
|
|---|
| 380 | static void devman_drv_fun_online(ipc_call_t *icall, driver_t *drv)
|
|---|
| 381 | {
|
|---|
| 382 | fun_node_t *fun;
|
|---|
| 383 | errno_t rc;
|
|---|
| 384 |
|
|---|
| 385 | log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_drv_fun_online()");
|
|---|
| 386 |
|
|---|
| 387 | fun = find_fun_node(&device_tree, ipc_get_arg1(icall));
|
|---|
| 388 | if (fun == NULL) {
|
|---|
| 389 | async_answer_0(icall, ENOENT);
|
|---|
| 390 | return;
|
|---|
| 391 | }
|
|---|
| 392 |
|
|---|
| 393 | fun_busy_lock(fun);
|
|---|
| 394 |
|
|---|
| 395 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
|---|
| 396 | if (fun->dev == NULL || fun->dev->drv != drv) {
|
|---|
| 397 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 398 | fun_busy_unlock(fun);
|
|---|
| 399 | fun_del_ref(fun);
|
|---|
| 400 | async_answer_0(icall, ENOENT);
|
|---|
| 401 | return;
|
|---|
| 402 | }
|
|---|
| 403 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 404 |
|
|---|
| 405 | rc = fun_online(fun);
|
|---|
| 406 | if (rc != EOK) {
|
|---|
| 407 | fun_busy_unlock(fun);
|
|---|
| 408 | fun_del_ref(fun);
|
|---|
| 409 | async_answer_0(icall, rc);
|
|---|
| 410 | return;
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | fun_busy_unlock(fun);
|
|---|
| 414 | fun_del_ref(fun);
|
|---|
| 415 |
|
|---|
| 416 | async_answer_0(icall, EOK);
|
|---|
| 417 | }
|
|---|
| 418 |
|
|---|
| 419 | /** Offline function by driver request.
|
|---|
| 420 | *
|
|---|
| 421 | */
|
|---|
| 422 | static void devman_drv_fun_offline(ipc_call_t *icall, driver_t *drv)
|
|---|
| 423 | {
|
|---|
| 424 | fun_node_t *fun;
|
|---|
| 425 | errno_t rc;
|
|---|
| 426 |
|
|---|
| 427 | fun = find_fun_node(&device_tree, ipc_get_arg1(icall));
|
|---|
| 428 | if (fun == NULL) {
|
|---|
| 429 | async_answer_0(icall, ENOENT);
|
|---|
| 430 | return;
|
|---|
| 431 | }
|
|---|
| 432 |
|
|---|
| 433 | fun_busy_lock(fun);
|
|---|
| 434 |
|
|---|
| 435 | fibril_rwlock_write_lock(&device_tree.rwlock);
|
|---|
| 436 | if (fun->dev == NULL || fun->dev->drv != drv) {
|
|---|
| 437 | fun_busy_unlock(fun);
|
|---|
| 438 | fun_del_ref(fun);
|
|---|
| 439 | async_answer_0(icall, ENOENT);
|
|---|
| 440 | return;
|
|---|
| 441 | }
|
|---|
| 442 | fibril_rwlock_write_unlock(&device_tree.rwlock);
|
|---|
| 443 |
|
|---|
| 444 | rc = fun_offline(fun);
|
|---|
| 445 | if (rc != EOK) {
|
|---|
| 446 | fun_busy_unlock(fun);
|
|---|
| 447 | fun_del_ref(fun);
|
|---|
| 448 | async_answer_0(icall, rc);
|
|---|
| 449 | return;
|
|---|
| 450 | }
|
|---|
| 451 |
|
|---|
| 452 | fun_busy_unlock(fun);
|
|---|
| 453 | fun_del_ref(fun);
|
|---|
| 454 | async_answer_0(icall, EOK);
|
|---|
| 455 | }
|
|---|
| 456 |
|
|---|
| 457 | /** Remove function. */
|
|---|
| 458 | static void devman_remove_function(ipc_call_t *call)
|
|---|
| 459 | {
|
|---|
| 460 | devman_handle_t fun_handle = ipc_get_arg1(call);
|
|---|
| 461 | dev_tree_t *tree = &device_tree;
|
|---|
| 462 | errno_t rc;
|
|---|
| 463 |
|
|---|
| 464 | fun_node_t *fun = find_fun_node(&device_tree, fun_handle);
|
|---|
| 465 | if (fun == NULL) {
|
|---|
| 466 | async_answer_0(call, ENOENT);
|
|---|
| 467 | return;
|
|---|
| 468 | }
|
|---|
| 469 |
|
|---|
| 470 | fun_busy_lock(fun);
|
|---|
| 471 |
|
|---|
| 472 | fibril_rwlock_write_lock(&tree->rwlock);
|
|---|
| 473 |
|
|---|
| 474 | log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_remove_function(fun='%s')", fun->pathname);
|
|---|
| 475 |
|
|---|
| 476 | /* Check function state */
|
|---|
| 477 | if (fun->state == FUN_REMOVED) {
|
|---|
| 478 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 479 | fun_busy_unlock(fun);
|
|---|
| 480 | fun_del_ref(fun);
|
|---|
| 481 | async_answer_0(call, ENOENT);
|
|---|
| 482 | return;
|
|---|
| 483 | }
|
|---|
| 484 |
|
|---|
| 485 | if (fun->ftype == fun_inner) {
|
|---|
| 486 | /* This is a surprise removal. Handle possible descendants */
|
|---|
| 487 | if (fun->child != NULL) {
|
|---|
| 488 | dev_node_t *dev = fun->child;
|
|---|
| 489 | device_state_t dev_state;
|
|---|
| 490 | errno_t gone_rc;
|
|---|
| 491 |
|
|---|
| 492 | dev_add_ref(dev);
|
|---|
| 493 | dev_state = dev->state;
|
|---|
| 494 |
|
|---|
| 495 | fibril_rwlock_write_unlock(&device_tree.rwlock);
|
|---|
| 496 |
|
|---|
| 497 | /* If device is owned by driver, inform driver it is gone. */
|
|---|
| 498 | if (dev_state == DEVICE_USABLE)
|
|---|
| 499 | gone_rc = driver_dev_gone(&device_tree, dev);
|
|---|
| 500 | else
|
|---|
| 501 | gone_rc = EOK;
|
|---|
| 502 |
|
|---|
| 503 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
|---|
| 504 |
|
|---|
| 505 | /* Verify that driver succeeded and removed all functions */
|
|---|
| 506 | if (gone_rc != EOK || !list_empty(&dev->functions)) {
|
|---|
| 507 | log_msg(LOG_DEFAULT, LVL_ERROR, "Driver did not remove "
|
|---|
| 508 | "functions for device that is gone. "
|
|---|
| 509 | "Device node is now defunct.");
|
|---|
| 510 |
|
|---|
| 511 | /*
|
|---|
| 512 | * Not much we can do but mark the device
|
|---|
| 513 | * node as having invalid state. This
|
|---|
| 514 | * is a driver bug.
|
|---|
| 515 | */
|
|---|
| 516 | dev->state = DEVICE_INVALID;
|
|---|
| 517 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 518 | dev_del_ref(dev);
|
|---|
| 519 | if (gone_rc == EOK)
|
|---|
| 520 | gone_rc = ENOTSUP;
|
|---|
| 521 | fun_busy_unlock(fun);
|
|---|
| 522 | fun_del_ref(fun);
|
|---|
| 523 | async_answer_0(call, gone_rc);
|
|---|
| 524 | return;
|
|---|
| 525 | }
|
|---|
| 526 |
|
|---|
| 527 | driver_t *driver = dev->drv;
|
|---|
| 528 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 529 |
|
|---|
| 530 | if (driver)
|
|---|
| 531 | detach_driver(&device_tree, dev);
|
|---|
| 532 |
|
|---|
| 533 | fibril_rwlock_write_lock(&device_tree.rwlock);
|
|---|
| 534 | remove_dev_node(&device_tree, dev);
|
|---|
| 535 |
|
|---|
| 536 | /* Delete ref created when node was inserted */
|
|---|
| 537 | dev_del_ref(dev);
|
|---|
| 538 | /* Delete ref created by dev_add_ref(dev) above */
|
|---|
| 539 | dev_del_ref(dev);
|
|---|
| 540 | }
|
|---|
| 541 | } else {
|
|---|
| 542 | if (fun->service_id != 0) {
|
|---|
| 543 | /* Unregister from location service */
|
|---|
| 544 | rc = loc_unregister_tree_function(fun, &device_tree);
|
|---|
| 545 | if (rc != EOK) {
|
|---|
| 546 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed unregistering tree "
|
|---|
| 547 | "service.");
|
|---|
| 548 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 549 | fun_busy_unlock(fun);
|
|---|
| 550 | fun_del_ref(fun);
|
|---|
| 551 | async_answer_0(call, EIO);
|
|---|
| 552 | return;
|
|---|
| 553 | }
|
|---|
| 554 | }
|
|---|
| 555 | }
|
|---|
| 556 |
|
|---|
| 557 | remove_fun_node(&device_tree, fun);
|
|---|
| 558 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 559 | fun_busy_unlock(fun);
|
|---|
| 560 |
|
|---|
| 561 | /* Delete ref added when inserting function into tree */
|
|---|
| 562 | fun_del_ref(fun);
|
|---|
| 563 | /* Delete ref added above when looking up function */
|
|---|
| 564 | fun_del_ref(fun);
|
|---|
| 565 |
|
|---|
| 566 | log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_remove_function() succeeded.");
|
|---|
| 567 | async_answer_0(call, EOK);
|
|---|
| 568 | }
|
|---|
| 569 |
|
|---|
| 570 | /** Initialize driver which has registered itself as running and ready.
|
|---|
| 571 | *
|
|---|
| 572 | * The initialization is done in a separate fibril to avoid deadlocks (if the
|
|---|
| 573 | * driver needed to be served by devman during the driver's initialization).
|
|---|
| 574 | */
|
|---|
| 575 | static errno_t init_running_drv(void *drv)
|
|---|
| 576 | {
|
|---|
| 577 | driver_t *driver = (driver_t *) drv;
|
|---|
| 578 |
|
|---|
| 579 | initialize_running_driver(driver, &device_tree);
|
|---|
| 580 | log_msg(LOG_DEFAULT, LVL_DEBUG, "The `%s` driver was successfully initialized.",
|
|---|
| 581 | driver->name);
|
|---|
| 582 | return 0;
|
|---|
| 583 | }
|
|---|
| 584 |
|
|---|
| 585 | /** Function for handling connections from a driver to the device manager. */
|
|---|
| 586 | void devman_connection_driver(ipc_call_t *icall, void *arg)
|
|---|
| 587 | {
|
|---|
| 588 | client_t *client;
|
|---|
| 589 | driver_t *driver = NULL;
|
|---|
| 590 |
|
|---|
| 591 | /* Accept the connection. */
|
|---|
| 592 | async_accept_0(icall);
|
|---|
| 593 |
|
|---|
| 594 | client = async_get_client_data();
|
|---|
| 595 | if (client == NULL) {
|
|---|
| 596 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to allocate client data.");
|
|---|
| 597 | return;
|
|---|
| 598 | }
|
|---|
| 599 |
|
|---|
| 600 | while (true) {
|
|---|
| 601 | ipc_call_t call;
|
|---|
| 602 | async_get_call(&call);
|
|---|
| 603 |
|
|---|
| 604 | if (!ipc_get_imethod(&call)) {
|
|---|
| 605 | async_answer_0(&call, EOK);
|
|---|
| 606 | break;
|
|---|
| 607 | }
|
|---|
| 608 |
|
|---|
| 609 | if (ipc_get_imethod(&call) != DEVMAN_DRIVER_REGISTER) {
|
|---|
| 610 | fibril_mutex_lock(&client->mutex);
|
|---|
| 611 | driver = client->driver;
|
|---|
| 612 | fibril_mutex_unlock(&client->mutex);
|
|---|
| 613 | if (driver == NULL) {
|
|---|
| 614 | /* First call must be to DEVMAN_DRIVER_REGISTER */
|
|---|
| 615 | async_answer_0(&call, ENOTSUP);
|
|---|
| 616 | continue;
|
|---|
| 617 | }
|
|---|
| 618 | }
|
|---|
| 619 |
|
|---|
| 620 | switch (ipc_get_imethod(&call)) {
|
|---|
| 621 | case DEVMAN_DRIVER_REGISTER:
|
|---|
| 622 | fibril_mutex_lock(&client->mutex);
|
|---|
| 623 | if (client->driver != NULL) {
|
|---|
| 624 | fibril_mutex_unlock(&client->mutex);
|
|---|
| 625 | async_answer_0(&call, EINVAL);
|
|---|
| 626 | continue;
|
|---|
| 627 | }
|
|---|
| 628 | client->driver = devman_driver_register(&call);
|
|---|
| 629 | fibril_mutex_unlock(&client->mutex);
|
|---|
| 630 | break;
|
|---|
| 631 | case DEVMAN_ADD_FUNCTION:
|
|---|
| 632 | devman_add_function(&call);
|
|---|
| 633 | break;
|
|---|
| 634 | case DEVMAN_ADD_DEVICE_TO_CATEGORY:
|
|---|
| 635 | devman_add_function_to_cat(&call);
|
|---|
| 636 | break;
|
|---|
| 637 | case DEVMAN_DRV_FUN_ONLINE:
|
|---|
| 638 | devman_drv_fun_online(&call, driver);
|
|---|
| 639 | break;
|
|---|
| 640 | case DEVMAN_DRV_FUN_OFFLINE:
|
|---|
| 641 | devman_drv_fun_offline(&call, driver);
|
|---|
| 642 | break;
|
|---|
| 643 | case DEVMAN_REMOVE_FUNCTION:
|
|---|
| 644 | devman_remove_function(&call);
|
|---|
| 645 | break;
|
|---|
| 646 | default:
|
|---|
| 647 | async_answer_0(&call, EINVAL);
|
|---|
| 648 | break;
|
|---|
| 649 | }
|
|---|
| 650 | }
|
|---|
| 651 | }
|
|---|
| 652 |
|
|---|
| 653 | /** @}
|
|---|
| 654 | */
|
|---|