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