| 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 <inttypes.h>
|
|---|
| 39 | #include <assert.h>
|
|---|
| 40 | #include <ipc/services.h>
|
|---|
| 41 | #include <ns.h>
|
|---|
| 42 | #include <async.h>
|
|---|
| 43 | #include <stdio.h>
|
|---|
| 44 | #include <errno.h>
|
|---|
| 45 | #include <str_error.h>
|
|---|
| 46 | #include <stdbool.h>
|
|---|
| 47 | #include <fibril_synch.h>
|
|---|
| 48 | #include <stdlib.h>
|
|---|
| 49 | #include <str.h>
|
|---|
| 50 | #include <dirent.h>
|
|---|
| 51 | #include <fcntl.h>
|
|---|
| 52 | #include <sys/stat.h>
|
|---|
| 53 | #include <ctype.h>
|
|---|
| 54 | #include <io/log.h>
|
|---|
| 55 | #include <ipc/devman.h>
|
|---|
| 56 | #include <ipc/driver.h>
|
|---|
| 57 | #include <thread.h>
|
|---|
| 58 | #include <loc.h>
|
|---|
| 59 |
|
|---|
| 60 | #include "dev.h"
|
|---|
| 61 | #include "devman.h"
|
|---|
| 62 | #include "devtree.h"
|
|---|
| 63 | #include "driver.h"
|
|---|
| 64 | #include "fun.h"
|
|---|
| 65 | #include "loc.h"
|
|---|
| 66 |
|
|---|
| 67 | #define DRIVER_DEFAULT_STORE "/drv"
|
|---|
| 68 |
|
|---|
| 69 | static driver_list_t drivers_list;
|
|---|
| 70 | static dev_tree_t device_tree;
|
|---|
| 71 |
|
|---|
| 72 | static int init_running_drv(void *drv);
|
|---|
| 73 |
|
|---|
| 74 | /** Register running driver. */
|
|---|
| 75 | static driver_t *devman_driver_register(ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 76 | {
|
|---|
| 77 | driver_t *driver = NULL;
|
|---|
| 78 | char *drv_name = NULL;
|
|---|
| 79 |
|
|---|
| 80 | log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_driver_register");
|
|---|
| 81 |
|
|---|
| 82 | /* Get driver name. */
|
|---|
| 83 | int rc = async_data_write_accept((void **) &drv_name, true, 0, 0, 0, 0);
|
|---|
| 84 | if (rc != EOK) {
|
|---|
| 85 | async_answer_0(callid, rc);
|
|---|
| 86 | return NULL;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | log_msg(LOG_DEFAULT, LVL_DEBUG, "The `%s' driver is trying to register.",
|
|---|
| 90 | drv_name);
|
|---|
| 91 |
|
|---|
| 92 | /* Find driver structure. */
|
|---|
| 93 | driver = find_driver(&drivers_list, drv_name);
|
|---|
| 94 | if (driver == NULL) {
|
|---|
| 95 | log_msg(LOG_DEFAULT, LVL_ERROR, "No driver named `%s' was found.", drv_name);
|
|---|
| 96 | free(drv_name);
|
|---|
| 97 | drv_name = NULL;
|
|---|
| 98 | async_answer_0(callid, ENOENT);
|
|---|
| 99 | return NULL;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | free(drv_name);
|
|---|
| 103 | drv_name = NULL;
|
|---|
| 104 |
|
|---|
| 105 | fibril_mutex_lock(&driver->driver_mutex);
|
|---|
| 106 |
|
|---|
| 107 | if (driver->sess) {
|
|---|
| 108 | /* We already have a connection to the driver. */
|
|---|
| 109 | log_msg(LOG_DEFAULT, LVL_ERROR, "Driver '%s' already started.\n",
|
|---|
| 110 | driver->name);
|
|---|
| 111 | fibril_mutex_unlock(&driver->driver_mutex);
|
|---|
| 112 | async_answer_0(callid, EEXISTS);
|
|---|
| 113 | return NULL;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | switch (driver->state) {
|
|---|
| 117 | case DRIVER_NOT_STARTED:
|
|---|
| 118 | /* Somebody started the driver manually. */
|
|---|
| 119 | log_msg(LOG_DEFAULT, LVL_NOTE, "Driver '%s' started manually.\n",
|
|---|
| 120 | driver->name);
|
|---|
| 121 | driver->state = DRIVER_STARTING;
|
|---|
| 122 | break;
|
|---|
| 123 | case DRIVER_STARTING:
|
|---|
| 124 | /* The expected case */
|
|---|
| 125 | break;
|
|---|
| 126 | case DRIVER_RUNNING:
|
|---|
| 127 | /* Should not happen since we do not have a connected session */
|
|---|
| 128 | assert(false);
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /* Create connection to the driver. */
|
|---|
| 132 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Creating connection to the `%s' driver.",
|
|---|
| 133 | driver->name);
|
|---|
| 134 | driver->sess = async_callback_receive(EXCHANGE_PARALLEL);
|
|---|
| 135 | if (!driver->sess) {
|
|---|
| 136 | fibril_mutex_unlock(&driver->driver_mutex);
|
|---|
| 137 | async_answer_0(callid, ENOTSUP);
|
|---|
| 138 | return NULL;
|
|---|
| 139 | }
|
|---|
| 140 | /* FIXME: Work around problem with callback sessions */
|
|---|
| 141 | async_sess_args_set(driver->sess, DRIVER_DEVMAN, 0, 0);
|
|---|
| 142 |
|
|---|
| 143 | log_msg(LOG_DEFAULT, LVL_NOTE,
|
|---|
| 144 | "The `%s' driver was successfully registered as running.",
|
|---|
| 145 | driver->name);
|
|---|
| 146 |
|
|---|
| 147 | /*
|
|---|
| 148 | * Initialize the driver as running (e.g. pass assigned devices to it)
|
|---|
| 149 | * in a separate fibril; the separate fibril is used to enable the
|
|---|
| 150 | * driver to use devman service during the driver's initialization.
|
|---|
| 151 | */
|
|---|
| 152 | fid_t fid = fibril_create(init_running_drv, driver);
|
|---|
| 153 | if (fid == 0) {
|
|---|
| 154 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to create initialization fibril " \
|
|---|
| 155 | "for driver `%s'.", driver->name);
|
|---|
| 156 | fibril_mutex_unlock(&driver->driver_mutex);
|
|---|
| 157 | async_answer_0(callid, ENOMEM);
|
|---|
| 158 | return NULL;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | fibril_add_ready(fid);
|
|---|
| 162 | fibril_mutex_unlock(&driver->driver_mutex);
|
|---|
| 163 |
|
|---|
| 164 | async_answer_0(callid, EOK);
|
|---|
| 165 | return driver;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | /** Receive device match ID from the device's parent driver and add it to the
|
|---|
| 169 | * list of devices match ids.
|
|---|
| 170 | *
|
|---|
| 171 | * @param match_ids The list of the device's match ids.
|
|---|
| 172 | * @return Zero on success, negative error code otherwise.
|
|---|
| 173 | */
|
|---|
| 174 | static int devman_receive_match_id(match_id_list_t *match_ids)
|
|---|
| 175 | {
|
|---|
| 176 | match_id_t *match_id = create_match_id();
|
|---|
| 177 | ipc_callid_t callid;
|
|---|
| 178 | ipc_call_t call;
|
|---|
| 179 | int rc = 0;
|
|---|
| 180 |
|
|---|
| 181 | callid = async_get_call(&call);
|
|---|
| 182 | if (DEVMAN_ADD_MATCH_ID != IPC_GET_IMETHOD(call)) {
|
|---|
| 183 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
|---|
| 184 | "Invalid protocol when trying to receive match id.");
|
|---|
| 185 | async_answer_0(callid, EINVAL);
|
|---|
| 186 | delete_match_id(match_id);
|
|---|
| 187 | return EINVAL;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | if (match_id == NULL) {
|
|---|
| 191 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to allocate match id.");
|
|---|
| 192 | async_answer_0(callid, ENOMEM);
|
|---|
| 193 | return ENOMEM;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | async_answer_0(callid, EOK);
|
|---|
| 197 |
|
|---|
| 198 | match_id->score = IPC_GET_ARG1(call);
|
|---|
| 199 |
|
|---|
| 200 | char *match_id_str;
|
|---|
| 201 | rc = async_data_write_accept((void **) &match_id_str, true, 0, 0, 0, 0);
|
|---|
| 202 | match_id->id = match_id_str;
|
|---|
| 203 | if (rc != EOK) {
|
|---|
| 204 | delete_match_id(match_id);
|
|---|
| 205 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to receive match id string: %s.",
|
|---|
| 206 | str_error(rc));
|
|---|
| 207 | return rc;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | list_append(&match_id->link, &match_ids->ids);
|
|---|
| 211 |
|
|---|
| 212 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Received match id `%s', score %d.",
|
|---|
| 213 | match_id->id, match_id->score);
|
|---|
| 214 | return rc;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | /** Receive device match IDs from the device's parent driver and add them to the
|
|---|
| 218 | * list of devices match ids.
|
|---|
| 219 | *
|
|---|
| 220 | * @param match_count The number of device's match ids to be received.
|
|---|
| 221 | * @param match_ids The list of the device's match ids.
|
|---|
| 222 | * @return Zero on success, negative error code otherwise.
|
|---|
| 223 | */
|
|---|
| 224 | static int devman_receive_match_ids(sysarg_t match_count,
|
|---|
| 225 | match_id_list_t *match_ids)
|
|---|
| 226 | {
|
|---|
| 227 | int ret = EOK;
|
|---|
| 228 | size_t i;
|
|---|
| 229 |
|
|---|
| 230 | for (i = 0; i < match_count; i++) {
|
|---|
| 231 | if (EOK != (ret = devman_receive_match_id(match_ids)))
|
|---|
| 232 | return ret;
|
|---|
| 233 | }
|
|---|
| 234 | return ret;
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | static int assign_driver_fibril(void *arg)
|
|---|
| 238 | {
|
|---|
| 239 | dev_node_t *dev_node = (dev_node_t *) arg;
|
|---|
| 240 | assign_driver(dev_node, &drivers_list, &device_tree);
|
|---|
| 241 |
|
|---|
| 242 | /* Delete one reference we got from the caller. */
|
|---|
| 243 | dev_del_ref(dev_node);
|
|---|
| 244 | return EOK;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | static int online_function(fun_node_t *fun)
|
|---|
| 248 | {
|
|---|
| 249 | dev_node_t *dev;
|
|---|
| 250 |
|
|---|
| 251 | fibril_rwlock_write_lock(&device_tree.rwlock);
|
|---|
| 252 |
|
|---|
| 253 | if (fun->state == FUN_ON_LINE) {
|
|---|
| 254 | fibril_rwlock_write_unlock(&device_tree.rwlock);
|
|---|
| 255 | log_msg(LOG_DEFAULT, LVL_WARN, "Function %s is already on line.",
|
|---|
| 256 | fun->pathname);
|
|---|
| 257 | return EOK;
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | if (fun->ftype == fun_inner) {
|
|---|
| 261 | dev = create_dev_node();
|
|---|
| 262 | if (dev == NULL) {
|
|---|
| 263 | fibril_rwlock_write_unlock(&device_tree.rwlock);
|
|---|
| 264 | return ENOMEM;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | insert_dev_node(&device_tree, dev, fun);
|
|---|
| 268 | dev_add_ref(dev);
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_add_function(fun=\"%s\")", fun->pathname);
|
|---|
| 272 |
|
|---|
| 273 | if (fun->ftype == fun_inner) {
|
|---|
| 274 | dev = fun->child;
|
|---|
| 275 | assert(dev != NULL);
|
|---|
| 276 |
|
|---|
| 277 | /* Give one reference over to assign_driver_fibril(). */
|
|---|
| 278 | dev_add_ref(dev);
|
|---|
| 279 |
|
|---|
| 280 | /*
|
|---|
| 281 | * Try to find a suitable driver and assign it to the device. We do
|
|---|
| 282 | * not want to block the current fibril that is used for processing
|
|---|
| 283 | * incoming calls: we will launch a separate fibril to handle the
|
|---|
| 284 | * driver assigning. That is because assign_driver can actually include
|
|---|
| 285 | * task spawning which could take some time.
|
|---|
| 286 | */
|
|---|
| 287 | fid_t assign_fibril = fibril_create(assign_driver_fibril, dev);
|
|---|
| 288 | if (assign_fibril == 0) {
|
|---|
| 289 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to create fibril for "
|
|---|
| 290 | "assigning driver.");
|
|---|
| 291 | /* XXX Cleanup */
|
|---|
| 292 | fibril_rwlock_write_unlock(&device_tree.rwlock);
|
|---|
| 293 | return ENOMEM;
|
|---|
| 294 | }
|
|---|
| 295 | fibril_add_ready(assign_fibril);
|
|---|
| 296 | } else
|
|---|
| 297 | loc_register_tree_function(fun, &device_tree);
|
|---|
| 298 |
|
|---|
| 299 | fibril_rwlock_write_unlock(&device_tree.rwlock);
|
|---|
| 300 |
|
|---|
| 301 | return EOK;
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | static int offline_function(fun_node_t *fun)
|
|---|
| 305 | {
|
|---|
| 306 | int rc;
|
|---|
| 307 |
|
|---|
| 308 | fibril_rwlock_write_lock(&device_tree.rwlock);
|
|---|
| 309 |
|
|---|
| 310 | if (fun->state == FUN_OFF_LINE) {
|
|---|
| 311 | fibril_rwlock_write_unlock(&device_tree.rwlock);
|
|---|
| 312 | log_msg(LOG_DEFAULT, LVL_WARN, "Function %s is already off line.",
|
|---|
| 313 | fun->pathname);
|
|---|
| 314 | return EOK;
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | if (fun->ftype == fun_inner) {
|
|---|
| 318 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Offlining inner function %s.",
|
|---|
| 319 | fun->pathname);
|
|---|
| 320 |
|
|---|
| 321 | if (fun->child != NULL) {
|
|---|
| 322 | dev_node_t *dev = fun->child;
|
|---|
| 323 | device_state_t dev_state;
|
|---|
| 324 |
|
|---|
| 325 | dev_add_ref(dev);
|
|---|
| 326 | dev_state = dev->state;
|
|---|
| 327 |
|
|---|
| 328 | fibril_rwlock_write_unlock(&device_tree.rwlock);
|
|---|
| 329 |
|
|---|
| 330 | /* If device is owned by driver, ask driver to give it up. */
|
|---|
| 331 | if (dev_state == DEVICE_USABLE) {
|
|---|
| 332 | rc = driver_dev_remove(&device_tree, dev);
|
|---|
| 333 | if (rc != EOK) {
|
|---|
| 334 | dev_del_ref(dev);
|
|---|
| 335 | return ENOTSUP;
|
|---|
| 336 | }
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | /* Verify that driver removed all functions */
|
|---|
| 340 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
|---|
| 341 | if (!list_empty(&dev->functions)) {
|
|---|
| 342 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 343 | dev_del_ref(dev);
|
|---|
| 344 | return EIO;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | driver_t *driver = dev->drv;
|
|---|
| 348 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 349 |
|
|---|
| 350 | if (driver)
|
|---|
| 351 | detach_driver(&device_tree, dev);
|
|---|
| 352 |
|
|---|
| 353 | fibril_rwlock_write_lock(&device_tree.rwlock);
|
|---|
| 354 | remove_dev_node(&device_tree, dev);
|
|---|
| 355 |
|
|---|
| 356 | /* Delete ref created when node was inserted */
|
|---|
| 357 | dev_del_ref(dev);
|
|---|
| 358 | /* Delete ref created by dev_add_ref(dev) above */
|
|---|
| 359 | dev_del_ref(dev);
|
|---|
| 360 | }
|
|---|
| 361 | } else {
|
|---|
| 362 | /* Unregister from location service */
|
|---|
| 363 | rc = loc_service_unregister(fun->service_id);
|
|---|
| 364 | if (rc != EOK) {
|
|---|
| 365 | fibril_rwlock_write_unlock(&device_tree.rwlock);
|
|---|
| 366 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed unregistering tree service.");
|
|---|
| 367 | return EIO;
|
|---|
| 368 | }
|
|---|
| 369 |
|
|---|
| 370 | fun->service_id = 0;
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | fun->state = FUN_OFF_LINE;
|
|---|
| 374 | fibril_rwlock_write_unlock(&device_tree.rwlock);
|
|---|
| 375 |
|
|---|
| 376 | return EOK;
|
|---|
| 377 | }
|
|---|
| 378 |
|
|---|
| 379 | /** Handle function registration.
|
|---|
| 380 | *
|
|---|
| 381 | * Child devices are registered by their parent's device driver.
|
|---|
| 382 | */
|
|---|
| 383 | static void devman_add_function(ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 384 | {
|
|---|
| 385 | fun_type_t ftype = (fun_type_t) IPC_GET_ARG1(*call);
|
|---|
| 386 | devman_handle_t dev_handle = IPC_GET_ARG2(*call);
|
|---|
| 387 | sysarg_t match_count = IPC_GET_ARG3(*call);
|
|---|
| 388 | dev_tree_t *tree = &device_tree;
|
|---|
| 389 |
|
|---|
| 390 | dev_node_t *pdev = find_dev_node(&device_tree, dev_handle);
|
|---|
| 391 | if (pdev == NULL) {
|
|---|
| 392 | async_answer_0(callid, ENOENT);
|
|---|
| 393 | return;
|
|---|
| 394 | }
|
|---|
| 395 |
|
|---|
| 396 | if (ftype != fun_inner && ftype != fun_exposed) {
|
|---|
| 397 | /* Unknown function type */
|
|---|
| 398 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
|---|
| 399 | "Unknown function type %d provided by driver.",
|
|---|
| 400 | (int) ftype);
|
|---|
| 401 |
|
|---|
| 402 | dev_del_ref(pdev);
|
|---|
| 403 | async_answer_0(callid, EINVAL);
|
|---|
| 404 | return;
|
|---|
| 405 | }
|
|---|
| 406 |
|
|---|
| 407 | char *fun_name = NULL;
|
|---|
| 408 | int rc = async_data_write_accept((void **)&fun_name, true, 0, 0, 0, 0);
|
|---|
| 409 | if (rc != EOK) {
|
|---|
| 410 | dev_del_ref(pdev);
|
|---|
| 411 | async_answer_0(callid, rc);
|
|---|
| 412 | return;
|
|---|
| 413 | }
|
|---|
| 414 |
|
|---|
| 415 | fibril_rwlock_write_lock(&tree->rwlock);
|
|---|
| 416 |
|
|---|
| 417 | /* Check device state */
|
|---|
| 418 | if (pdev->state == DEVICE_REMOVED) {
|
|---|
| 419 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 420 | dev_del_ref(pdev);
|
|---|
| 421 | async_answer_0(callid, ENOENT);
|
|---|
| 422 | return;
|
|---|
| 423 | }
|
|---|
| 424 |
|
|---|
| 425 | /* Check that function with same name is not there already. */
|
|---|
| 426 | fun_node_t *tfun = find_fun_node_in_device(tree, pdev, fun_name);
|
|---|
| 427 | if (tfun) {
|
|---|
| 428 | fun_del_ref(tfun); /* drop the new unwanted reference */
|
|---|
| 429 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 430 | dev_del_ref(pdev);
|
|---|
| 431 | async_answer_0(callid, EEXISTS);
|
|---|
| 432 | printf(NAME ": Warning, driver tried to register `%s' twice.\n",
|
|---|
| 433 | fun_name);
|
|---|
| 434 | free(fun_name);
|
|---|
| 435 | return;
|
|---|
| 436 | }
|
|---|
| 437 |
|
|---|
| 438 | fun_node_t *fun = create_fun_node();
|
|---|
| 439 | /* One reference for creation, one for us */
|
|---|
| 440 | fun_add_ref(fun);
|
|---|
| 441 | fun_add_ref(fun);
|
|---|
| 442 | fun->ftype = ftype;
|
|---|
| 443 |
|
|---|
| 444 | /*
|
|---|
| 445 | * We can lock the function here even when holding the tree because
|
|---|
| 446 | * we know it cannot be held by anyone else yet.
|
|---|
| 447 | */
|
|---|
| 448 | fun_busy_lock(fun);
|
|---|
| 449 |
|
|---|
| 450 | if (!insert_fun_node(&device_tree, fun, fun_name, pdev)) {
|
|---|
| 451 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 452 | dev_del_ref(pdev);
|
|---|
| 453 | fun_busy_unlock(fun);
|
|---|
| 454 | fun_del_ref(fun);
|
|---|
| 455 | delete_fun_node(fun);
|
|---|
| 456 | async_answer_0(callid, ENOMEM);
|
|---|
| 457 | return;
|
|---|
| 458 | }
|
|---|
| 459 |
|
|---|
| 460 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 461 | dev_del_ref(pdev);
|
|---|
| 462 |
|
|---|
| 463 | devman_receive_match_ids(match_count, &fun->match_ids);
|
|---|
| 464 |
|
|---|
| 465 | rc = online_function(fun);
|
|---|
| 466 | if (rc != EOK) {
|
|---|
| 467 | /* XXX Set some failed state? */
|
|---|
| 468 | fun_busy_unlock(fun);
|
|---|
| 469 | fun_del_ref(fun);
|
|---|
| 470 | async_answer_0(callid, rc);
|
|---|
| 471 | return;
|
|---|
| 472 | }
|
|---|
| 473 |
|
|---|
| 474 | fun_busy_unlock(fun);
|
|---|
| 475 | fun_del_ref(fun);
|
|---|
| 476 |
|
|---|
| 477 | /* Return device handle to parent's driver. */
|
|---|
| 478 | async_answer_1(callid, EOK, fun->handle);
|
|---|
| 479 | }
|
|---|
| 480 |
|
|---|
| 481 | static void devman_add_function_to_cat(ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 482 | {
|
|---|
| 483 | devman_handle_t handle = IPC_GET_ARG1(*call);
|
|---|
| 484 | category_id_t cat_id;
|
|---|
| 485 | int rc;
|
|---|
| 486 |
|
|---|
| 487 | /* Get category name. */
|
|---|
| 488 | char *cat_name;
|
|---|
| 489 | rc = async_data_write_accept((void **) &cat_name, true,
|
|---|
| 490 | 0, 0, 0, 0);
|
|---|
| 491 | if (rc != EOK) {
|
|---|
| 492 | async_answer_0(callid, rc);
|
|---|
| 493 | return;
|
|---|
| 494 | }
|
|---|
| 495 |
|
|---|
| 496 | fun_node_t *fun = find_fun_node(&device_tree, handle);
|
|---|
| 497 | if (fun == NULL) {
|
|---|
| 498 | async_answer_0(callid, ENOENT);
|
|---|
| 499 | return;
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
|---|
| 503 |
|
|---|
| 504 | /* Check function state */
|
|---|
| 505 | if (fun->state == FUN_REMOVED) {
|
|---|
| 506 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 507 | async_answer_0(callid, ENOENT);
|
|---|
| 508 | return;
|
|---|
| 509 | }
|
|---|
| 510 |
|
|---|
| 511 | rc = loc_category_get_id(cat_name, &cat_id, IPC_FLAG_BLOCKING);
|
|---|
| 512 | if (rc == EOK) {
|
|---|
| 513 | loc_service_add_to_cat(fun->service_id, cat_id);
|
|---|
| 514 | log_msg(LOG_DEFAULT, LVL_NOTE, "Function `%s' added to category `%s'.",
|
|---|
| 515 | fun->pathname, cat_name);
|
|---|
| 516 | } else {
|
|---|
| 517 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding function `%s' to category "
|
|---|
| 518 | "`%s'.", fun->pathname, cat_name);
|
|---|
| 519 | }
|
|---|
| 520 |
|
|---|
| 521 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 522 | fun_del_ref(fun);
|
|---|
| 523 |
|
|---|
| 524 | async_answer_0(callid, rc);
|
|---|
| 525 | }
|
|---|
| 526 |
|
|---|
| 527 | /** Online function by driver request.
|
|---|
| 528 | *
|
|---|
| 529 | */
|
|---|
| 530 | static void devman_drv_fun_online(ipc_callid_t iid, ipc_call_t *icall,
|
|---|
| 531 | driver_t *drv)
|
|---|
| 532 | {
|
|---|
| 533 | fun_node_t *fun;
|
|---|
| 534 | int rc;
|
|---|
| 535 |
|
|---|
| 536 | log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_drv_fun_online()");
|
|---|
| 537 |
|
|---|
| 538 | fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));
|
|---|
| 539 | if (fun == NULL) {
|
|---|
| 540 | async_answer_0(iid, ENOENT);
|
|---|
| 541 | return;
|
|---|
| 542 | }
|
|---|
| 543 |
|
|---|
| 544 | fun_busy_lock(fun);
|
|---|
| 545 |
|
|---|
| 546 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
|---|
| 547 | if (fun->dev == NULL || fun->dev->drv != drv) {
|
|---|
| 548 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 549 | fun_busy_unlock(fun);
|
|---|
| 550 | fun_del_ref(fun);
|
|---|
| 551 | async_answer_0(iid, ENOENT);
|
|---|
| 552 | return;
|
|---|
| 553 | }
|
|---|
| 554 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 555 |
|
|---|
| 556 | rc = online_function(fun);
|
|---|
| 557 | if (rc != EOK) {
|
|---|
| 558 | fun_busy_unlock(fun);
|
|---|
| 559 | fun_del_ref(fun);
|
|---|
| 560 | async_answer_0(iid, (sysarg_t) rc);
|
|---|
| 561 | return;
|
|---|
| 562 | }
|
|---|
| 563 |
|
|---|
| 564 | fun_busy_unlock(fun);
|
|---|
| 565 | fun_del_ref(fun);
|
|---|
| 566 |
|
|---|
| 567 | async_answer_0(iid, (sysarg_t) EOK);
|
|---|
| 568 | }
|
|---|
| 569 |
|
|---|
| 570 |
|
|---|
| 571 | /** Offline function by driver request.
|
|---|
| 572 | *
|
|---|
| 573 | */
|
|---|
| 574 | static void devman_drv_fun_offline(ipc_callid_t iid, ipc_call_t *icall,
|
|---|
| 575 | driver_t *drv)
|
|---|
| 576 | {
|
|---|
| 577 | fun_node_t *fun;
|
|---|
| 578 | int rc;
|
|---|
| 579 |
|
|---|
| 580 | fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));
|
|---|
| 581 | if (fun == NULL) {
|
|---|
| 582 | async_answer_0(iid, ENOENT);
|
|---|
| 583 | return;
|
|---|
| 584 | }
|
|---|
| 585 |
|
|---|
| 586 | fun_busy_lock(fun);
|
|---|
| 587 |
|
|---|
| 588 | fibril_rwlock_write_lock(&device_tree.rwlock);
|
|---|
| 589 | if (fun->dev == NULL || fun->dev->drv != drv) {
|
|---|
| 590 | fun_busy_unlock(fun);
|
|---|
| 591 | fun_del_ref(fun);
|
|---|
| 592 | async_answer_0(iid, ENOENT);
|
|---|
| 593 | return;
|
|---|
| 594 | }
|
|---|
| 595 | fibril_rwlock_write_unlock(&device_tree.rwlock);
|
|---|
| 596 |
|
|---|
| 597 | rc = offline_function(fun);
|
|---|
| 598 | if (rc != EOK) {
|
|---|
| 599 | fun_busy_unlock(fun);
|
|---|
| 600 | fun_del_ref(fun);
|
|---|
| 601 | async_answer_0(iid, (sysarg_t) rc);
|
|---|
| 602 | return;
|
|---|
| 603 | }
|
|---|
| 604 |
|
|---|
| 605 | fun_busy_unlock(fun);
|
|---|
| 606 | fun_del_ref(fun);
|
|---|
| 607 | async_answer_0(iid, (sysarg_t) EOK);
|
|---|
| 608 | }
|
|---|
| 609 |
|
|---|
| 610 | /** Remove function. */
|
|---|
| 611 | static void devman_remove_function(ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 612 | {
|
|---|
| 613 | devman_handle_t fun_handle = IPC_GET_ARG1(*call);
|
|---|
| 614 | dev_tree_t *tree = &device_tree;
|
|---|
| 615 | int rc;
|
|---|
| 616 |
|
|---|
| 617 | fun_node_t *fun = find_fun_node(&device_tree, fun_handle);
|
|---|
| 618 | if (fun == NULL) {
|
|---|
| 619 | async_answer_0(callid, ENOENT);
|
|---|
| 620 | return;
|
|---|
| 621 | }
|
|---|
| 622 |
|
|---|
| 623 | fun_busy_lock(fun);
|
|---|
| 624 |
|
|---|
| 625 | fibril_rwlock_write_lock(&tree->rwlock);
|
|---|
| 626 |
|
|---|
| 627 | log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_remove_function(fun='%s')", fun->pathname);
|
|---|
| 628 |
|
|---|
| 629 | /* Check function state */
|
|---|
| 630 | if (fun->state == FUN_REMOVED) {
|
|---|
| 631 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 632 | fun_busy_unlock(fun);
|
|---|
| 633 | fun_del_ref(fun);
|
|---|
| 634 | async_answer_0(callid, ENOENT);
|
|---|
| 635 | return;
|
|---|
| 636 | }
|
|---|
| 637 |
|
|---|
| 638 | if (fun->ftype == fun_inner) {
|
|---|
| 639 | /* This is a surprise removal. Handle possible descendants */
|
|---|
| 640 | if (fun->child != NULL) {
|
|---|
| 641 | dev_node_t *dev = fun->child;
|
|---|
| 642 | device_state_t dev_state;
|
|---|
| 643 | int gone_rc;
|
|---|
| 644 |
|
|---|
| 645 | dev_add_ref(dev);
|
|---|
| 646 | dev_state = dev->state;
|
|---|
| 647 |
|
|---|
| 648 | fibril_rwlock_write_unlock(&device_tree.rwlock);
|
|---|
| 649 |
|
|---|
| 650 | /* If device is owned by driver, inform driver it is gone. */
|
|---|
| 651 | if (dev_state == DEVICE_USABLE)
|
|---|
| 652 | gone_rc = driver_dev_gone(&device_tree, dev);
|
|---|
| 653 | else
|
|---|
| 654 | gone_rc = EOK;
|
|---|
| 655 |
|
|---|
| 656 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
|---|
| 657 |
|
|---|
| 658 | /* Verify that driver succeeded and removed all functions */
|
|---|
| 659 | if (gone_rc != EOK || !list_empty(&dev->functions)) {
|
|---|
| 660 | log_msg(LOG_DEFAULT, LVL_ERROR, "Driver did not remove "
|
|---|
| 661 | "functions for device that is gone. "
|
|---|
| 662 | "Device node is now defunct.");
|
|---|
| 663 |
|
|---|
| 664 | /*
|
|---|
| 665 | * Not much we can do but mark the device
|
|---|
| 666 | * node as having invalid state. This
|
|---|
| 667 | * is a driver bug.
|
|---|
| 668 | */
|
|---|
| 669 | dev->state = DEVICE_INVALID;
|
|---|
| 670 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 671 | dev_del_ref(dev);
|
|---|
| 672 | if (gone_rc == EOK)
|
|---|
| 673 | gone_rc = ENOTSUP;
|
|---|
| 674 | fun_busy_unlock(fun);
|
|---|
| 675 | fun_del_ref(fun);
|
|---|
| 676 | async_answer_0(callid, gone_rc);
|
|---|
| 677 | return;
|
|---|
| 678 | }
|
|---|
| 679 |
|
|---|
| 680 | driver_t *driver = dev->drv;
|
|---|
| 681 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 682 |
|
|---|
| 683 | if (driver)
|
|---|
| 684 | detach_driver(&device_tree, dev);
|
|---|
| 685 |
|
|---|
| 686 | fibril_rwlock_write_lock(&device_tree.rwlock);
|
|---|
| 687 | remove_dev_node(&device_tree, dev);
|
|---|
| 688 |
|
|---|
| 689 | /* Delete ref created when node was inserted */
|
|---|
| 690 | dev_del_ref(dev);
|
|---|
| 691 | /* Delete ref created by dev_add_ref(dev) above */
|
|---|
| 692 | dev_del_ref(dev);
|
|---|
| 693 | }
|
|---|
| 694 | } else {
|
|---|
| 695 | if (fun->service_id != 0) {
|
|---|
| 696 | /* Unregister from location service */
|
|---|
| 697 | rc = loc_service_unregister(fun->service_id);
|
|---|
| 698 | if (rc != EOK) {
|
|---|
| 699 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed unregistering tree "
|
|---|
| 700 | "service.");
|
|---|
| 701 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 702 | fun_busy_unlock(fun);
|
|---|
| 703 | fun_del_ref(fun);
|
|---|
| 704 | async_answer_0(callid, EIO);
|
|---|
| 705 | return;
|
|---|
| 706 | }
|
|---|
| 707 | }
|
|---|
| 708 | }
|
|---|
| 709 |
|
|---|
| 710 | remove_fun_node(&device_tree, fun);
|
|---|
| 711 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 712 | fun_busy_unlock(fun);
|
|---|
| 713 |
|
|---|
| 714 | /* Delete ref added when inserting function into tree */
|
|---|
| 715 | fun_del_ref(fun);
|
|---|
| 716 | /* Delete ref added above when looking up function */
|
|---|
| 717 | fun_del_ref(fun);
|
|---|
| 718 |
|
|---|
| 719 | log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_remove_function() succeeded.");
|
|---|
| 720 | async_answer_0(callid, EOK);
|
|---|
| 721 | }
|
|---|
| 722 |
|
|---|
| 723 | /** Initialize driver which has registered itself as running and ready.
|
|---|
| 724 | *
|
|---|
| 725 | * The initialization is done in a separate fibril to avoid deadlocks (if the
|
|---|
| 726 | * driver needed to be served by devman during the driver's initialization).
|
|---|
| 727 | */
|
|---|
| 728 | static int init_running_drv(void *drv)
|
|---|
| 729 | {
|
|---|
| 730 | driver_t *driver = (driver_t *) drv;
|
|---|
| 731 |
|
|---|
| 732 | initialize_running_driver(driver, &device_tree);
|
|---|
| 733 | log_msg(LOG_DEFAULT, LVL_DEBUG, "The `%s` driver was successfully initialized.",
|
|---|
| 734 | driver->name);
|
|---|
| 735 | return 0;
|
|---|
| 736 | }
|
|---|
| 737 |
|
|---|
| 738 | /** Function for handling connections from a driver to the device manager. */
|
|---|
| 739 | static void devman_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 740 | {
|
|---|
| 741 | client_t *client;
|
|---|
| 742 | driver_t *driver = NULL;
|
|---|
| 743 |
|
|---|
| 744 | /* Accept the connection. */
|
|---|
| 745 | async_answer_0(iid, EOK);
|
|---|
| 746 |
|
|---|
| 747 | client = async_get_client_data();
|
|---|
| 748 | if (client == NULL) {
|
|---|
| 749 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to allocate client data.");
|
|---|
| 750 | return;
|
|---|
| 751 | }
|
|---|
| 752 |
|
|---|
| 753 | while (true) {
|
|---|
| 754 | ipc_call_t call;
|
|---|
| 755 | ipc_callid_t callid = async_get_call(&call);
|
|---|
| 756 |
|
|---|
| 757 | if (!IPC_GET_IMETHOD(call))
|
|---|
| 758 | break;
|
|---|
| 759 |
|
|---|
| 760 | if (IPC_GET_IMETHOD(call) != DEVMAN_DRIVER_REGISTER) {
|
|---|
| 761 | fibril_mutex_lock(&client->mutex);
|
|---|
| 762 | driver = client->driver;
|
|---|
| 763 | fibril_mutex_unlock(&client->mutex);
|
|---|
| 764 | if (driver == NULL) {
|
|---|
| 765 | /* First call must be to DEVMAN_DRIVER_REGISTER */
|
|---|
| 766 | async_answer_0(callid, ENOTSUP);
|
|---|
| 767 | continue;
|
|---|
| 768 | }
|
|---|
| 769 | }
|
|---|
| 770 |
|
|---|
| 771 | switch (IPC_GET_IMETHOD(call)) {
|
|---|
| 772 | case DEVMAN_DRIVER_REGISTER:
|
|---|
| 773 | fibril_mutex_lock(&client->mutex);
|
|---|
| 774 | if (client->driver != NULL) {
|
|---|
| 775 | fibril_mutex_unlock(&client->mutex);
|
|---|
| 776 | async_answer_0(callid, EINVAL);
|
|---|
| 777 | continue;
|
|---|
| 778 | }
|
|---|
| 779 | client->driver = devman_driver_register(callid, &call);
|
|---|
| 780 | fibril_mutex_unlock(&client->mutex);
|
|---|
| 781 | break;
|
|---|
| 782 | case DEVMAN_ADD_FUNCTION:
|
|---|
| 783 | devman_add_function(callid, &call);
|
|---|
| 784 | break;
|
|---|
| 785 | case DEVMAN_ADD_DEVICE_TO_CATEGORY:
|
|---|
| 786 | devman_add_function_to_cat(callid, &call);
|
|---|
| 787 | break;
|
|---|
| 788 | case DEVMAN_DRV_FUN_ONLINE:
|
|---|
| 789 | devman_drv_fun_online(callid, &call, driver);
|
|---|
| 790 | break;
|
|---|
| 791 | case DEVMAN_DRV_FUN_OFFLINE:
|
|---|
| 792 | devman_drv_fun_offline(callid, &call, driver);
|
|---|
| 793 | break;
|
|---|
| 794 | case DEVMAN_REMOVE_FUNCTION:
|
|---|
| 795 | devman_remove_function(callid, &call);
|
|---|
| 796 | break;
|
|---|
| 797 | default:
|
|---|
| 798 | async_answer_0(callid, EINVAL);
|
|---|
| 799 | break;
|
|---|
| 800 | }
|
|---|
| 801 | }
|
|---|
| 802 | }
|
|---|
| 803 |
|
|---|
| 804 | /** Find handle for the device instance identified by the device's path in the
|
|---|
| 805 | * device tree. */
|
|---|
| 806 | static void devman_function_get_handle(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 807 | {
|
|---|
| 808 | char *pathname;
|
|---|
| 809 | devman_handle_t handle;
|
|---|
| 810 |
|
|---|
| 811 | int rc = async_data_write_accept((void **) &pathname, true, 0, 0, 0, 0);
|
|---|
| 812 | if (rc != EOK) {
|
|---|
| 813 | async_answer_0(iid, rc);
|
|---|
| 814 | return;
|
|---|
| 815 | }
|
|---|
| 816 |
|
|---|
| 817 | fun_node_t *fun = find_fun_node_by_path(&device_tree, pathname);
|
|---|
| 818 |
|
|---|
| 819 | free(pathname);
|
|---|
| 820 |
|
|---|
| 821 | if (fun == NULL) {
|
|---|
| 822 | async_answer_0(iid, ENOENT);
|
|---|
| 823 | return;
|
|---|
| 824 | }
|
|---|
| 825 |
|
|---|
| 826 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
|---|
| 827 |
|
|---|
| 828 | /* Check function state */
|
|---|
| 829 | if (fun->state == FUN_REMOVED) {
|
|---|
| 830 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 831 | async_answer_0(iid, ENOENT);
|
|---|
| 832 | return;
|
|---|
| 833 | }
|
|---|
| 834 | handle = fun->handle;
|
|---|
| 835 |
|
|---|
| 836 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 837 |
|
|---|
| 838 | /* Delete reference created above by find_fun_node_by_path() */
|
|---|
| 839 | fun_del_ref(fun);
|
|---|
| 840 |
|
|---|
| 841 | async_answer_1(iid, EOK, handle);
|
|---|
| 842 | }
|
|---|
| 843 |
|
|---|
| 844 | /** Get device name. */
|
|---|
| 845 | static void devman_fun_get_name(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 846 | {
|
|---|
| 847 | devman_handle_t handle = IPC_GET_ARG1(*icall);
|
|---|
| 848 |
|
|---|
| 849 | fun_node_t *fun = find_fun_node(&device_tree, handle);
|
|---|
| 850 | if (fun == NULL) {
|
|---|
| 851 | async_answer_0(iid, ENOMEM);
|
|---|
| 852 | return;
|
|---|
| 853 | }
|
|---|
| 854 |
|
|---|
| 855 | ipc_callid_t data_callid;
|
|---|
| 856 | size_t data_len;
|
|---|
| 857 | if (!async_data_read_receive(&data_callid, &data_len)) {
|
|---|
| 858 | async_answer_0(iid, EINVAL);
|
|---|
| 859 | fun_del_ref(fun);
|
|---|
| 860 | return;
|
|---|
| 861 | }
|
|---|
| 862 |
|
|---|
| 863 | void *buffer = malloc(data_len);
|
|---|
| 864 | if (buffer == NULL) {
|
|---|
| 865 | async_answer_0(data_callid, ENOMEM);
|
|---|
| 866 | async_answer_0(iid, ENOMEM);
|
|---|
| 867 | fun_del_ref(fun);
|
|---|
| 868 | return;
|
|---|
| 869 | }
|
|---|
| 870 |
|
|---|
| 871 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
|---|
| 872 |
|
|---|
| 873 | /* Check function state */
|
|---|
| 874 | if (fun->state == FUN_REMOVED) {
|
|---|
| 875 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 876 | free(buffer);
|
|---|
| 877 |
|
|---|
| 878 | async_answer_0(data_callid, ENOENT);
|
|---|
| 879 | async_answer_0(iid, ENOENT);
|
|---|
| 880 | fun_del_ref(fun);
|
|---|
| 881 | return;
|
|---|
| 882 | }
|
|---|
| 883 |
|
|---|
| 884 | size_t sent_length = str_size(fun->name);
|
|---|
| 885 | if (sent_length > data_len) {
|
|---|
| 886 | sent_length = data_len;
|
|---|
| 887 | }
|
|---|
| 888 |
|
|---|
| 889 | async_data_read_finalize(data_callid, fun->name, sent_length);
|
|---|
| 890 | async_answer_0(iid, EOK);
|
|---|
| 891 |
|
|---|
| 892 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 893 | fun_del_ref(fun);
|
|---|
| 894 | free(buffer);
|
|---|
| 895 | }
|
|---|
| 896 |
|
|---|
| 897 | /** Get function driver name. */
|
|---|
| 898 | static void devman_fun_get_driver_name(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 899 | {
|
|---|
| 900 | devman_handle_t handle = IPC_GET_ARG1(*icall);
|
|---|
| 901 |
|
|---|
| 902 | fun_node_t *fun = find_fun_node(&device_tree, handle);
|
|---|
| 903 | if (fun == NULL) {
|
|---|
| 904 | async_answer_0(iid, ENOMEM);
|
|---|
| 905 | return;
|
|---|
| 906 | }
|
|---|
| 907 |
|
|---|
| 908 | ipc_callid_t data_callid;
|
|---|
| 909 | size_t data_len;
|
|---|
| 910 | if (!async_data_read_receive(&data_callid, &data_len)) {
|
|---|
| 911 | async_answer_0(iid, EINVAL);
|
|---|
| 912 | fun_del_ref(fun);
|
|---|
| 913 | return;
|
|---|
| 914 | }
|
|---|
| 915 |
|
|---|
| 916 | void *buffer = malloc(data_len);
|
|---|
| 917 | if (buffer == NULL) {
|
|---|
| 918 | async_answer_0(data_callid, ENOMEM);
|
|---|
| 919 | async_answer_0(iid, ENOMEM);
|
|---|
| 920 | fun_del_ref(fun);
|
|---|
| 921 | return;
|
|---|
| 922 | }
|
|---|
| 923 |
|
|---|
| 924 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
|---|
| 925 |
|
|---|
| 926 | /* Check function state */
|
|---|
| 927 | if (fun->state == FUN_REMOVED) {
|
|---|
| 928 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 929 | free(buffer);
|
|---|
| 930 |
|
|---|
| 931 | async_answer_0(data_callid, ENOENT);
|
|---|
| 932 | async_answer_0(iid, ENOENT);
|
|---|
| 933 | fun_del_ref(fun);
|
|---|
| 934 | return;
|
|---|
| 935 | }
|
|---|
| 936 |
|
|---|
| 937 | /* Check whether function has a driver */
|
|---|
| 938 | if (fun->child == NULL || fun->child->drv == NULL) {
|
|---|
| 939 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 940 | free(buffer);
|
|---|
| 941 |
|
|---|
| 942 | async_answer_0(data_callid, EINVAL);
|
|---|
| 943 | async_answer_0(iid, EINVAL);
|
|---|
| 944 | fun_del_ref(fun);
|
|---|
| 945 | return;
|
|---|
| 946 | }
|
|---|
| 947 |
|
|---|
| 948 | size_t sent_length = str_size(fun->child->drv->name);
|
|---|
| 949 | if (sent_length > data_len) {
|
|---|
| 950 | sent_length = data_len;
|
|---|
| 951 | }
|
|---|
| 952 |
|
|---|
| 953 | async_data_read_finalize(data_callid, fun->child->drv->name,
|
|---|
| 954 | sent_length);
|
|---|
| 955 | async_answer_0(iid, EOK);
|
|---|
| 956 |
|
|---|
| 957 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 958 | fun_del_ref(fun);
|
|---|
| 959 | free(buffer);
|
|---|
| 960 | }
|
|---|
| 961 |
|
|---|
| 962 | /** Get device path. */
|
|---|
| 963 | static void devman_fun_get_path(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 964 | {
|
|---|
| 965 | devman_handle_t handle = IPC_GET_ARG1(*icall);
|
|---|
| 966 |
|
|---|
| 967 | fun_node_t *fun = find_fun_node(&device_tree, handle);
|
|---|
| 968 | if (fun == NULL) {
|
|---|
| 969 | async_answer_0(iid, ENOMEM);
|
|---|
| 970 | return;
|
|---|
| 971 | }
|
|---|
| 972 |
|
|---|
| 973 | ipc_callid_t data_callid;
|
|---|
| 974 | size_t data_len;
|
|---|
| 975 | if (!async_data_read_receive(&data_callid, &data_len)) {
|
|---|
| 976 | async_answer_0(iid, EINVAL);
|
|---|
| 977 | fun_del_ref(fun);
|
|---|
| 978 | return;
|
|---|
| 979 | }
|
|---|
| 980 |
|
|---|
| 981 | void *buffer = malloc(data_len);
|
|---|
| 982 | if (buffer == NULL) {
|
|---|
| 983 | async_answer_0(data_callid, ENOMEM);
|
|---|
| 984 | async_answer_0(iid, ENOMEM);
|
|---|
| 985 | fun_del_ref(fun);
|
|---|
| 986 | return;
|
|---|
| 987 | }
|
|---|
| 988 |
|
|---|
| 989 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
|---|
| 990 |
|
|---|
| 991 | /* Check function state */
|
|---|
| 992 | if (fun->state == FUN_REMOVED) {
|
|---|
| 993 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 994 | free(buffer);
|
|---|
| 995 |
|
|---|
| 996 | async_answer_0(data_callid, ENOENT);
|
|---|
| 997 | async_answer_0(iid, ENOENT);
|
|---|
| 998 | fun_del_ref(fun);
|
|---|
| 999 | return;
|
|---|
| 1000 | }
|
|---|
| 1001 |
|
|---|
| 1002 | size_t sent_length = str_size(fun->pathname);
|
|---|
| 1003 | if (sent_length > data_len) {
|
|---|
| 1004 | sent_length = data_len;
|
|---|
| 1005 | }
|
|---|
| 1006 |
|
|---|
| 1007 | async_data_read_finalize(data_callid, fun->pathname, sent_length);
|
|---|
| 1008 | async_answer_0(iid, EOK);
|
|---|
| 1009 |
|
|---|
| 1010 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 1011 | fun_del_ref(fun);
|
|---|
| 1012 | free(buffer);
|
|---|
| 1013 | }
|
|---|
| 1014 |
|
|---|
| 1015 | static void devman_dev_get_functions(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 1016 | {
|
|---|
| 1017 | ipc_callid_t callid;
|
|---|
| 1018 | size_t size;
|
|---|
| 1019 | size_t act_size;
|
|---|
| 1020 | int rc;
|
|---|
| 1021 |
|
|---|
| 1022 | if (!async_data_read_receive(&callid, &size)) {
|
|---|
| 1023 | async_answer_0(callid, EREFUSED);
|
|---|
| 1024 | async_answer_0(iid, EREFUSED);
|
|---|
| 1025 | return;
|
|---|
| 1026 | }
|
|---|
| 1027 |
|
|---|
| 1028 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
|---|
| 1029 |
|
|---|
| 1030 | dev_node_t *dev = find_dev_node_no_lock(&device_tree,
|
|---|
| 1031 | IPC_GET_ARG1(*icall));
|
|---|
| 1032 | if (dev == NULL || dev->state == DEVICE_REMOVED) {
|
|---|
| 1033 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 1034 | async_answer_0(callid, ENOENT);
|
|---|
| 1035 | async_answer_0(iid, ENOENT);
|
|---|
| 1036 | return;
|
|---|
| 1037 | }
|
|---|
| 1038 |
|
|---|
| 1039 | devman_handle_t *hdl_buf = (devman_handle_t *) malloc(size);
|
|---|
| 1040 | if (hdl_buf == NULL) {
|
|---|
| 1041 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 1042 | async_answer_0(callid, ENOMEM);
|
|---|
| 1043 | async_answer_0(iid, ENOMEM);
|
|---|
| 1044 | return;
|
|---|
| 1045 | }
|
|---|
| 1046 |
|
|---|
| 1047 | rc = dev_get_functions(&device_tree, dev, hdl_buf, size, &act_size);
|
|---|
| 1048 | if (rc != EOK) {
|
|---|
| 1049 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 1050 | async_answer_0(callid, rc);
|
|---|
| 1051 | async_answer_0(iid, rc);
|
|---|
| 1052 | return;
|
|---|
| 1053 | }
|
|---|
| 1054 |
|
|---|
| 1055 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 1056 |
|
|---|
| 1057 | sysarg_t retval = async_data_read_finalize(callid, hdl_buf, size);
|
|---|
| 1058 | free(hdl_buf);
|
|---|
| 1059 |
|
|---|
| 1060 | async_answer_1(iid, retval, act_size);
|
|---|
| 1061 | }
|
|---|
| 1062 |
|
|---|
| 1063 |
|
|---|
| 1064 | /** Get handle for child device of a function. */
|
|---|
| 1065 | static void devman_fun_get_child(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 1066 | {
|
|---|
| 1067 | fun_node_t *fun;
|
|---|
| 1068 |
|
|---|
| 1069 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
|---|
| 1070 |
|
|---|
| 1071 | fun = find_fun_node_no_lock(&device_tree, IPC_GET_ARG1(*icall));
|
|---|
| 1072 | if (fun == NULL || fun->state == FUN_REMOVED) {
|
|---|
| 1073 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 1074 | async_answer_0(iid, ENOENT);
|
|---|
| 1075 | return;
|
|---|
| 1076 | }
|
|---|
| 1077 |
|
|---|
| 1078 | if (fun->child == NULL) {
|
|---|
| 1079 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 1080 | async_answer_0(iid, ENOENT);
|
|---|
| 1081 | return;
|
|---|
| 1082 | }
|
|---|
| 1083 |
|
|---|
| 1084 | async_answer_1(iid, EOK, fun->child->handle);
|
|---|
| 1085 |
|
|---|
| 1086 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 1087 | }
|
|---|
| 1088 |
|
|---|
| 1089 | /** Online function.
|
|---|
| 1090 | *
|
|---|
| 1091 | * Send a request to online a function to the responsible driver.
|
|---|
| 1092 | * The driver may offline other functions if necessary (i.e. if the state
|
|---|
| 1093 | * of this function is linked to state of another function somehow).
|
|---|
| 1094 | */
|
|---|
| 1095 | static void devman_fun_online(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 1096 | {
|
|---|
| 1097 | fun_node_t *fun;
|
|---|
| 1098 | int rc;
|
|---|
| 1099 |
|
|---|
| 1100 | fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));
|
|---|
| 1101 | if (fun == NULL) {
|
|---|
| 1102 | async_answer_0(iid, ENOENT);
|
|---|
| 1103 | return;
|
|---|
| 1104 | }
|
|---|
| 1105 |
|
|---|
| 1106 | rc = driver_fun_online(&device_tree, fun);
|
|---|
| 1107 | fun_del_ref(fun);
|
|---|
| 1108 |
|
|---|
| 1109 | async_answer_0(iid, (sysarg_t) rc);
|
|---|
| 1110 | }
|
|---|
| 1111 |
|
|---|
| 1112 | /** Offline function.
|
|---|
| 1113 | *
|
|---|
| 1114 | * Send a request to offline a function to the responsible driver. As
|
|---|
| 1115 | * a result the subtree rooted at that function should be cleanly
|
|---|
| 1116 | * detatched. The driver may offline other functions if necessary
|
|---|
| 1117 | * (i.e. if the state of this function is linked to state of another
|
|---|
| 1118 | * function somehow).
|
|---|
| 1119 | */
|
|---|
| 1120 | static void devman_fun_offline(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 1121 | {
|
|---|
| 1122 | fun_node_t *fun;
|
|---|
| 1123 | int rc;
|
|---|
| 1124 |
|
|---|
| 1125 | fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));
|
|---|
| 1126 | if (fun == NULL) {
|
|---|
| 1127 | async_answer_0(iid, ENOENT);
|
|---|
| 1128 | return;
|
|---|
| 1129 | }
|
|---|
| 1130 |
|
|---|
| 1131 | rc = driver_fun_offline(&device_tree, fun);
|
|---|
| 1132 | fun_del_ref(fun);
|
|---|
| 1133 |
|
|---|
| 1134 | async_answer_0(iid, (sysarg_t) rc);
|
|---|
| 1135 | }
|
|---|
| 1136 |
|
|---|
| 1137 | /** Find handle for the function instance identified by its service ID. */
|
|---|
| 1138 | static void devman_fun_sid_to_handle(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 1139 | {
|
|---|
| 1140 | fun_node_t *fun;
|
|---|
| 1141 |
|
|---|
| 1142 | fun = find_loc_tree_function(&device_tree, IPC_GET_ARG1(*icall));
|
|---|
| 1143 |
|
|---|
| 1144 | if (fun == NULL) {
|
|---|
| 1145 | async_answer_0(iid, ENOENT);
|
|---|
| 1146 | return;
|
|---|
| 1147 | }
|
|---|
| 1148 |
|
|---|
| 1149 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
|---|
| 1150 |
|
|---|
| 1151 | /* Check function state */
|
|---|
| 1152 | if (fun->state == FUN_REMOVED) {
|
|---|
| 1153 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 1154 | async_answer_0(iid, ENOENT);
|
|---|
| 1155 | return;
|
|---|
| 1156 | }
|
|---|
| 1157 |
|
|---|
| 1158 | async_answer_1(iid, EOK, fun->handle);
|
|---|
| 1159 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 1160 | fun_del_ref(fun);
|
|---|
| 1161 | }
|
|---|
| 1162 |
|
|---|
| 1163 | /** Function for handling connections from a client to the device manager. */
|
|---|
| 1164 | static void devman_connection_client(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 1165 | {
|
|---|
| 1166 | /* Accept connection. */
|
|---|
| 1167 | async_answer_0(iid, EOK);
|
|---|
| 1168 |
|
|---|
| 1169 | while (true) {
|
|---|
| 1170 | ipc_call_t call;
|
|---|
| 1171 | ipc_callid_t callid = async_get_call(&call);
|
|---|
| 1172 |
|
|---|
| 1173 | if (!IPC_GET_IMETHOD(call))
|
|---|
| 1174 | break;
|
|---|
| 1175 |
|
|---|
| 1176 | switch (IPC_GET_IMETHOD(call)) {
|
|---|
| 1177 | case DEVMAN_DEVICE_GET_HANDLE:
|
|---|
| 1178 | devman_function_get_handle(callid, &call);
|
|---|
| 1179 | break;
|
|---|
| 1180 | case DEVMAN_DEV_GET_FUNCTIONS:
|
|---|
| 1181 | devman_dev_get_functions(callid, &call);
|
|---|
| 1182 | break;
|
|---|
| 1183 | case DEVMAN_FUN_GET_CHILD:
|
|---|
| 1184 | devman_fun_get_child(callid, &call);
|
|---|
| 1185 | break;
|
|---|
| 1186 | case DEVMAN_FUN_GET_NAME:
|
|---|
| 1187 | devman_fun_get_name(callid, &call);
|
|---|
| 1188 | break;
|
|---|
| 1189 | case DEVMAN_FUN_GET_DRIVER_NAME:
|
|---|
| 1190 | devman_fun_get_driver_name(callid, &call);
|
|---|
| 1191 | break;
|
|---|
| 1192 | case DEVMAN_FUN_GET_PATH:
|
|---|
| 1193 | devman_fun_get_path(callid, &call);
|
|---|
| 1194 | break;
|
|---|
| 1195 | case DEVMAN_FUN_ONLINE:
|
|---|
| 1196 | devman_fun_online(callid, &call);
|
|---|
| 1197 | break;
|
|---|
| 1198 | case DEVMAN_FUN_OFFLINE:
|
|---|
| 1199 | devman_fun_offline(callid, &call);
|
|---|
| 1200 | break;
|
|---|
| 1201 | case DEVMAN_FUN_SID_TO_HANDLE:
|
|---|
| 1202 | devman_fun_sid_to_handle(callid, &call);
|
|---|
| 1203 | break;
|
|---|
| 1204 | default:
|
|---|
| 1205 | async_answer_0(callid, ENOENT);
|
|---|
| 1206 | }
|
|---|
| 1207 | }
|
|---|
| 1208 | }
|
|---|
| 1209 |
|
|---|
| 1210 | static void devman_forward(ipc_callid_t iid, ipc_call_t *icall,
|
|---|
| 1211 | bool drv_to_parent)
|
|---|
| 1212 | {
|
|---|
| 1213 | devman_handle_t handle = IPC_GET_ARG2(*icall);
|
|---|
| 1214 | devman_handle_t fwd_h;
|
|---|
| 1215 | fun_node_t *fun = NULL;
|
|---|
| 1216 | dev_node_t *dev = NULL;
|
|---|
| 1217 |
|
|---|
| 1218 | fun = find_fun_node(&device_tree, handle);
|
|---|
| 1219 | if (fun == NULL)
|
|---|
| 1220 | dev = find_dev_node(&device_tree, handle);
|
|---|
| 1221 | else {
|
|---|
| 1222 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
|---|
| 1223 | dev = fun->dev;
|
|---|
| 1224 | if (dev != NULL)
|
|---|
| 1225 | dev_add_ref(dev);
|
|---|
| 1226 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 1227 | }
|
|---|
| 1228 |
|
|---|
| 1229 | /*
|
|---|
| 1230 | * For a valid function to connect to we need a device. The root
|
|---|
| 1231 | * function, for example, has no device and cannot be connected to.
|
|---|
| 1232 | * This means @c dev needs to be valid regardless whether we are
|
|---|
| 1233 | * connecting to a device or to a function.
|
|---|
| 1234 | */
|
|---|
| 1235 | if (dev == NULL) {
|
|---|
| 1236 | log_msg(LOG_DEFAULT, LVL_ERROR, "IPC forwarding failed - no device or "
|
|---|
| 1237 | "function with handle %" PRIun " was found.", handle);
|
|---|
| 1238 | async_answer_0(iid, ENOENT);
|
|---|
| 1239 | goto cleanup;
|
|---|
| 1240 | }
|
|---|
| 1241 |
|
|---|
| 1242 | if (fun == NULL && !drv_to_parent) {
|
|---|
| 1243 | log_msg(LOG_DEFAULT, LVL_ERROR, NAME ": devman_forward error - cannot "
|
|---|
| 1244 | "connect to handle %" PRIun ", refers to a device.",
|
|---|
| 1245 | handle);
|
|---|
| 1246 | async_answer_0(iid, ENOENT);
|
|---|
| 1247 | goto cleanup;
|
|---|
| 1248 | }
|
|---|
| 1249 |
|
|---|
| 1250 | driver_t *driver = NULL;
|
|---|
| 1251 |
|
|---|
| 1252 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
|---|
| 1253 |
|
|---|
| 1254 | if (drv_to_parent) {
|
|---|
| 1255 | /* Connect to parent function of a device (or device function). */
|
|---|
| 1256 | if (dev->pfun->dev != NULL)
|
|---|
| 1257 | driver = dev->pfun->dev->drv;
|
|---|
| 1258 |
|
|---|
| 1259 | fwd_h = dev->pfun->handle;
|
|---|
| 1260 | } else if (dev->state == DEVICE_USABLE) {
|
|---|
| 1261 | /* Connect to the specified function */
|
|---|
| 1262 | driver = dev->drv;
|
|---|
| 1263 | assert(driver != NULL);
|
|---|
| 1264 |
|
|---|
| 1265 | fwd_h = handle;
|
|---|
| 1266 | }
|
|---|
| 1267 |
|
|---|
| 1268 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 1269 |
|
|---|
| 1270 | if (driver == NULL) {
|
|---|
| 1271 | log_msg(LOG_DEFAULT, LVL_ERROR, "IPC forwarding refused - " \
|
|---|
| 1272 | "the device %" PRIun " is not in usable state.", handle);
|
|---|
| 1273 | async_answer_0(iid, ENOENT);
|
|---|
| 1274 | goto cleanup;
|
|---|
| 1275 | }
|
|---|
| 1276 |
|
|---|
| 1277 | int method;
|
|---|
| 1278 | if (drv_to_parent)
|
|---|
| 1279 | method = DRIVER_DRIVER;
|
|---|
| 1280 | else
|
|---|
| 1281 | method = DRIVER_CLIENT;
|
|---|
| 1282 |
|
|---|
| 1283 | if (!driver->sess) {
|
|---|
| 1284 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
|---|
| 1285 | "Could not forward to driver `%s'.", driver->name);
|
|---|
| 1286 | async_answer_0(iid, EINVAL);
|
|---|
| 1287 | goto cleanup;
|
|---|
| 1288 | }
|
|---|
| 1289 |
|
|---|
| 1290 | if (fun != NULL) {
|
|---|
| 1291 | log_msg(LOG_DEFAULT, LVL_DEBUG,
|
|---|
| 1292 | "Forwarding request for `%s' function to driver `%s'.",
|
|---|
| 1293 | fun->pathname, driver->name);
|
|---|
| 1294 | } else {
|
|---|
| 1295 | log_msg(LOG_DEFAULT, LVL_DEBUG,
|
|---|
| 1296 | "Forwarding request for `%s' device to driver `%s'.",
|
|---|
| 1297 | dev->pfun->pathname, driver->name);
|
|---|
| 1298 | }
|
|---|
| 1299 |
|
|---|
| 1300 | async_exch_t *exch = async_exchange_begin(driver->sess);
|
|---|
| 1301 | async_forward_fast(iid, exch, method, fwd_h, 0, IPC_FF_NONE);
|
|---|
| 1302 | async_exchange_end(exch);
|
|---|
| 1303 |
|
|---|
| 1304 | cleanup:
|
|---|
| 1305 | if (dev != NULL)
|
|---|
| 1306 | dev_del_ref(dev);
|
|---|
| 1307 |
|
|---|
| 1308 | if (fun != NULL)
|
|---|
| 1309 | fun_del_ref(fun);
|
|---|
| 1310 | }
|
|---|
| 1311 |
|
|---|
| 1312 | /** Function for handling connections from a client forwarded by the location
|
|---|
| 1313 | * service to the device manager. */
|
|---|
| 1314 | static void devman_connection_loc(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 1315 | {
|
|---|
| 1316 | service_id_t service_id = IPC_GET_ARG2(*icall);
|
|---|
| 1317 | fun_node_t *fun;
|
|---|
| 1318 | dev_node_t *dev;
|
|---|
| 1319 | devman_handle_t handle;
|
|---|
| 1320 | driver_t *driver;
|
|---|
| 1321 |
|
|---|
| 1322 | fun = find_loc_tree_function(&device_tree, service_id);
|
|---|
| 1323 |
|
|---|
| 1324 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
|---|
| 1325 |
|
|---|
| 1326 | if (fun == NULL || fun->dev == NULL || fun->dev->drv == NULL) {
|
|---|
| 1327 | log_msg(LOG_DEFAULT, LVL_WARN, "devman_connection_loc(): function "
|
|---|
| 1328 | "not found.\n");
|
|---|
| 1329 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 1330 | async_answer_0(iid, ENOENT);
|
|---|
| 1331 | return;
|
|---|
| 1332 | }
|
|---|
| 1333 |
|
|---|
| 1334 | dev = fun->dev;
|
|---|
| 1335 | driver = dev->drv;
|
|---|
| 1336 | handle = fun->handle;
|
|---|
| 1337 |
|
|---|
| 1338 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 1339 |
|
|---|
| 1340 | async_exch_t *exch = async_exchange_begin(driver->sess);
|
|---|
| 1341 | async_forward_fast(iid, exch, DRIVER_CLIENT, handle, 0,
|
|---|
| 1342 | IPC_FF_NONE);
|
|---|
| 1343 | async_exchange_end(exch);
|
|---|
| 1344 |
|
|---|
| 1345 | log_msg(LOG_DEFAULT, LVL_DEBUG,
|
|---|
| 1346 | "Forwarding loc service request for `%s' function to driver `%s'.",
|
|---|
| 1347 | fun->pathname, driver->name);
|
|---|
| 1348 |
|
|---|
| 1349 | fun_del_ref(fun);
|
|---|
| 1350 | }
|
|---|
| 1351 |
|
|---|
| 1352 | /** Function for handling connections to device manager. */
|
|---|
| 1353 | static void devman_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
|---|
| 1354 | {
|
|---|
| 1355 | /* Select port. */
|
|---|
| 1356 | switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
|
|---|
| 1357 | case DEVMAN_DRIVER:
|
|---|
| 1358 | devman_connection_driver(iid, icall);
|
|---|
| 1359 | break;
|
|---|
| 1360 | case DEVMAN_CLIENT:
|
|---|
| 1361 | devman_connection_client(iid, icall);
|
|---|
| 1362 | break;
|
|---|
| 1363 | case DEVMAN_CONNECT_TO_DEVICE:
|
|---|
| 1364 | /* Connect client to selected device. */
|
|---|
| 1365 | devman_forward(iid, icall, false);
|
|---|
| 1366 | break;
|
|---|
| 1367 | case DEVMAN_CONNECT_FROM_LOC:
|
|---|
| 1368 | /* Someone connected through loc node. */
|
|---|
| 1369 | devman_connection_loc(iid, icall);
|
|---|
| 1370 | break;
|
|---|
| 1371 | case DEVMAN_CONNECT_TO_PARENTS_DEVICE:
|
|---|
| 1372 | /* Connect client to selected device. */
|
|---|
| 1373 | devman_forward(iid, icall, true);
|
|---|
| 1374 | break;
|
|---|
| 1375 | default:
|
|---|
| 1376 | /* No such interface */
|
|---|
| 1377 | async_answer_0(iid, ENOENT);
|
|---|
| 1378 | }
|
|---|
| 1379 | }
|
|---|
| 1380 |
|
|---|
| 1381 | static void *devman_client_data_create(void)
|
|---|
| 1382 | {
|
|---|
| 1383 | client_t *client;
|
|---|
| 1384 |
|
|---|
| 1385 | client = calloc(1, sizeof(client_t));
|
|---|
| 1386 | if (client == NULL)
|
|---|
| 1387 | return NULL;
|
|---|
| 1388 |
|
|---|
| 1389 | fibril_mutex_initialize(&client->mutex);
|
|---|
| 1390 | return client;
|
|---|
| 1391 | }
|
|---|
| 1392 |
|
|---|
| 1393 | static void devman_client_data_destroy(void *data)
|
|---|
| 1394 | {
|
|---|
| 1395 | free(data);
|
|---|
| 1396 | }
|
|---|
| 1397 |
|
|---|
| 1398 | /** Initialize device manager internal structures. */
|
|---|
| 1399 | static bool devman_init(void)
|
|---|
| 1400 | {
|
|---|
| 1401 | log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_init - looking for available drivers.");
|
|---|
| 1402 |
|
|---|
| 1403 | /* Initialize list of available drivers. */
|
|---|
| 1404 | init_driver_list(&drivers_list);
|
|---|
| 1405 | if (lookup_available_drivers(&drivers_list,
|
|---|
| 1406 | DRIVER_DEFAULT_STORE) == 0) {
|
|---|
| 1407 | log_msg(LOG_DEFAULT, LVL_FATAL, "No drivers found.");
|
|---|
| 1408 | return false;
|
|---|
| 1409 | }
|
|---|
| 1410 |
|
|---|
| 1411 | log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_init - list of drivers has been initialized.");
|
|---|
| 1412 |
|
|---|
| 1413 | /* Create root device node. */
|
|---|
| 1414 | if (!init_device_tree(&device_tree, &drivers_list)) {
|
|---|
| 1415 | log_msg(LOG_DEFAULT, LVL_FATAL, "Failed to initialize device tree.");
|
|---|
| 1416 | return false;
|
|---|
| 1417 | }
|
|---|
| 1418 |
|
|---|
| 1419 | /*
|
|---|
| 1420 | * Caution: As the device manager is not a real loc
|
|---|
| 1421 | * driver (it uses a completely different IPC protocol
|
|---|
| 1422 | * than an ordinary loc driver), forwarding a connection
|
|---|
| 1423 | * from client to the devman by location service will
|
|---|
| 1424 | * not work.
|
|---|
| 1425 | */
|
|---|
| 1426 | loc_server_register(NAME);
|
|---|
| 1427 |
|
|---|
| 1428 | return true;
|
|---|
| 1429 | }
|
|---|
| 1430 |
|
|---|
| 1431 | int main(int argc, char *argv[])
|
|---|
| 1432 | {
|
|---|
| 1433 | printf("%s: HelenOS Device Manager\n", NAME);
|
|---|
| 1434 |
|
|---|
| 1435 | int rc = log_init(NAME);
|
|---|
| 1436 | if (rc != EOK) {
|
|---|
| 1437 | printf("%s: Error initializing logging subsystem.\n", NAME);
|
|---|
| 1438 | return rc;
|
|---|
| 1439 | }
|
|---|
| 1440 |
|
|---|
| 1441 | /* Set handlers for incoming connections. */
|
|---|
| 1442 | async_set_client_data_constructor(devman_client_data_create);
|
|---|
| 1443 | async_set_client_data_destructor(devman_client_data_destroy);
|
|---|
| 1444 | async_set_client_connection(devman_connection);
|
|---|
| 1445 |
|
|---|
| 1446 | if (!devman_init()) {
|
|---|
| 1447 | log_msg(LOG_DEFAULT, LVL_ERROR, "Error while initializing service.");
|
|---|
| 1448 | return -1;
|
|---|
| 1449 | }
|
|---|
| 1450 |
|
|---|
| 1451 | /* Register device manager at naming service. */
|
|---|
| 1452 | rc = service_register(SERVICE_DEVMAN);
|
|---|
| 1453 | if (rc != EOK) {
|
|---|
| 1454 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering as a service.");
|
|---|
| 1455 | return rc;
|
|---|
| 1456 | }
|
|---|
| 1457 |
|
|---|
| 1458 | printf("%s: Accepting connections.\n", NAME);
|
|---|
| 1459 | task_retval(0);
|
|---|
| 1460 | async_manager();
|
|---|
| 1461 |
|
|---|
| 1462 | /* Never reached. */
|
|---|
| 1463 | return 0;
|
|---|
| 1464 | }
|
|---|
| 1465 |
|
|---|
| 1466 | /** @}
|
|---|
| 1467 | */
|
|---|