| 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 <ipc/ns.h>
|
|---|
| 42 | #include <async.h>
|
|---|
| 43 | #include <stdio.h>
|
|---|
| 44 | #include <errno.h>
|
|---|
| 45 | #include <str_error.h>
|
|---|
| 46 | #include <bool.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 <devmap.h>
|
|---|
| 59 |
|
|---|
| 60 | #include "devman.h"
|
|---|
| 61 |
|
|---|
| 62 | #define DRIVER_DEFAULT_STORE "/drv"
|
|---|
| 63 |
|
|---|
| 64 | static driver_list_t drivers_list;
|
|---|
| 65 | static dev_tree_t device_tree;
|
|---|
| 66 | static class_list_t class_list;
|
|---|
| 67 |
|
|---|
| 68 | /** Register running driver. */
|
|---|
| 69 | static driver_t *devman_driver_register(void)
|
|---|
| 70 | {
|
|---|
| 71 | ipc_call_t icall;
|
|---|
| 72 | ipc_callid_t iid;
|
|---|
| 73 | driver_t *driver = NULL;
|
|---|
| 74 |
|
|---|
| 75 | log_msg(LVL_DEBUG, "devman_driver_register");
|
|---|
| 76 |
|
|---|
| 77 | iid = async_get_call(&icall);
|
|---|
| 78 | if (IPC_GET_IMETHOD(icall) != DEVMAN_DRIVER_REGISTER) {
|
|---|
| 79 | async_answer_0(iid, EREFUSED);
|
|---|
| 80 | return NULL;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | char *drv_name = NULL;
|
|---|
| 84 |
|
|---|
| 85 | /* Get driver name. */
|
|---|
| 86 | int rc = async_data_write_accept((void **) &drv_name, true, 0, 0, 0, 0);
|
|---|
| 87 | if (rc != EOK) {
|
|---|
| 88 | async_answer_0(iid, rc);
|
|---|
| 89 | return NULL;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | log_msg(LVL_DEBUG, "The `%s' driver is trying to register.",
|
|---|
| 93 | drv_name);
|
|---|
| 94 |
|
|---|
| 95 | /* Find driver structure. */
|
|---|
| 96 | driver = find_driver(&drivers_list, drv_name);
|
|---|
| 97 | if (driver == NULL) {
|
|---|
| 98 | log_msg(LVL_ERROR, "No driver named `%s' was found.", drv_name);
|
|---|
| 99 | free(drv_name);
|
|---|
| 100 | drv_name = NULL;
|
|---|
| 101 | async_answer_0(iid, ENOENT);
|
|---|
| 102 | return NULL;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | free(drv_name);
|
|---|
| 106 | drv_name = NULL;
|
|---|
| 107 |
|
|---|
| 108 | fibril_mutex_lock(&driver->driver_mutex);
|
|---|
| 109 |
|
|---|
| 110 | if (driver->phone >= 0) {
|
|---|
| 111 | /* We already have a connection to the driver. */
|
|---|
| 112 | log_msg(LVL_ERROR, "Driver '%s' already started.\n",
|
|---|
| 113 | driver->name);
|
|---|
| 114 | fibril_mutex_unlock(&driver->driver_mutex);
|
|---|
| 115 | async_answer_0(iid, EEXISTS);
|
|---|
| 116 | return NULL;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | switch (driver->state) {
|
|---|
| 120 | case DRIVER_NOT_STARTED:
|
|---|
| 121 | /* Somebody started the driver manually. */
|
|---|
| 122 | log_msg(LVL_NOTE, "Driver '%s' started manually.\n",
|
|---|
| 123 | driver->name);
|
|---|
| 124 | driver->state = DRIVER_STARTING;
|
|---|
| 125 | break;
|
|---|
| 126 | case DRIVER_STARTING:
|
|---|
| 127 | /* The expected case */
|
|---|
| 128 | break;
|
|---|
| 129 | case DRIVER_RUNNING:
|
|---|
| 130 | /* Should not happen since we do not have a connected phone */
|
|---|
| 131 | assert(false);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /* Create connection to the driver. */
|
|---|
| 135 | log_msg(LVL_DEBUG, "Creating connection to the `%s' driver.",
|
|---|
| 136 | driver->name);
|
|---|
| 137 | ipc_call_t call;
|
|---|
| 138 | ipc_callid_t callid = async_get_call(&call);
|
|---|
| 139 | if (IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) {
|
|---|
| 140 | fibril_mutex_unlock(&driver->driver_mutex);
|
|---|
| 141 | async_answer_0(callid, ENOTSUP);
|
|---|
| 142 | async_answer_0(iid, ENOTSUP);
|
|---|
| 143 | return NULL;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | /* Remember driver's phone. */
|
|---|
| 147 | driver->phone = IPC_GET_ARG5(call);
|
|---|
| 148 |
|
|---|
| 149 | fibril_mutex_unlock(&driver->driver_mutex);
|
|---|
| 150 |
|
|---|
| 151 | log_msg(LVL_NOTE,
|
|---|
| 152 | "The `%s' driver was successfully registered as running.",
|
|---|
| 153 | driver->name);
|
|---|
| 154 |
|
|---|
| 155 | async_answer_0(callid, EOK);
|
|---|
| 156 | async_answer_0(iid, EOK);
|
|---|
| 157 |
|
|---|
| 158 | return driver;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /** Receive device match ID from the device's parent driver and add it to the
|
|---|
| 162 | * list of devices match ids.
|
|---|
| 163 | *
|
|---|
| 164 | * @param match_ids The list of the device's match ids.
|
|---|
| 165 | * @return Zero on success, negative error code otherwise.
|
|---|
| 166 | */
|
|---|
| 167 | static int devman_receive_match_id(match_id_list_t *match_ids)
|
|---|
| 168 | {
|
|---|
| 169 | match_id_t *match_id = create_match_id();
|
|---|
| 170 | ipc_callid_t callid;
|
|---|
| 171 | ipc_call_t call;
|
|---|
| 172 | int rc = 0;
|
|---|
| 173 |
|
|---|
| 174 | callid = async_get_call(&call);
|
|---|
| 175 | if (DEVMAN_ADD_MATCH_ID != IPC_GET_IMETHOD(call)) {
|
|---|
| 176 | log_msg(LVL_ERROR,
|
|---|
| 177 | "Invalid protocol when trying to receive match id.");
|
|---|
| 178 | async_answer_0(callid, EINVAL);
|
|---|
| 179 | delete_match_id(match_id);
|
|---|
| 180 | return EINVAL;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | if (match_id == NULL) {
|
|---|
| 184 | log_msg(LVL_ERROR, "Failed to allocate match id.");
|
|---|
| 185 | async_answer_0(callid, ENOMEM);
|
|---|
| 186 | return ENOMEM;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | async_answer_0(callid, EOK);
|
|---|
| 190 |
|
|---|
| 191 | match_id->score = IPC_GET_ARG1(call);
|
|---|
| 192 |
|
|---|
| 193 | char *match_id_str;
|
|---|
| 194 | rc = async_data_write_accept((void **) &match_id_str, true, 0, 0, 0, 0);
|
|---|
| 195 | match_id->id = match_id_str;
|
|---|
| 196 | if (rc != EOK) {
|
|---|
| 197 | delete_match_id(match_id);
|
|---|
| 198 | log_msg(LVL_ERROR, "Failed to receive match id string: %s.",
|
|---|
| 199 | str_error(rc));
|
|---|
| 200 | return rc;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | list_append(&match_id->link, &match_ids->ids);
|
|---|
| 204 |
|
|---|
| 205 | log_msg(LVL_DEBUG, "Received match id `%s', score %d.",
|
|---|
| 206 | match_id->id, match_id->score);
|
|---|
| 207 | return rc;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | /** Receive device match IDs from the device's parent driver and add them to the
|
|---|
| 211 | * list of devices match ids.
|
|---|
| 212 | *
|
|---|
| 213 | * @param match_count The number of device's match ids to be received.
|
|---|
| 214 | * @param match_ids The list of the device's match ids.
|
|---|
| 215 | * @return Zero on success, negative error code otherwise.
|
|---|
| 216 | */
|
|---|
| 217 | static int devman_receive_match_ids(sysarg_t match_count,
|
|---|
| 218 | match_id_list_t *match_ids)
|
|---|
| 219 | {
|
|---|
| 220 | int ret = EOK;
|
|---|
| 221 | size_t i;
|
|---|
| 222 |
|
|---|
| 223 | for (i = 0; i < match_count; i++) {
|
|---|
| 224 | if (EOK != (ret = devman_receive_match_id(match_ids)))
|
|---|
| 225 | return ret;
|
|---|
| 226 | }
|
|---|
| 227 | return ret;
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | static int assign_driver_fibril(void *arg)
|
|---|
| 231 | {
|
|---|
| 232 | dev_node_t *dev_node = (dev_node_t *) arg;
|
|---|
| 233 | assign_driver(dev_node, &drivers_list, &device_tree);
|
|---|
| 234 | return EOK;
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | /** Handle function registration.
|
|---|
| 238 | *
|
|---|
| 239 | * Child devices are registered by their parent's device driver.
|
|---|
| 240 | */
|
|---|
| 241 | static void devman_add_function(ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 242 | {
|
|---|
| 243 | fun_type_t ftype = (fun_type_t) IPC_GET_ARG1(*call);
|
|---|
| 244 | devman_handle_t dev_handle = IPC_GET_ARG2(*call);
|
|---|
| 245 | sysarg_t match_count = IPC_GET_ARG3(*call);
|
|---|
| 246 | dev_tree_t *tree = &device_tree;
|
|---|
| 247 |
|
|---|
| 248 | fibril_rwlock_write_lock(&tree->rwlock);
|
|---|
| 249 |
|
|---|
| 250 | dev_node_t *dev = NULL;
|
|---|
| 251 | dev_node_t *pdev = find_dev_node_no_lock(&device_tree, dev_handle);
|
|---|
| 252 |
|
|---|
| 253 | if (pdev == NULL) {
|
|---|
| 254 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 255 | async_answer_0(callid, ENOENT);
|
|---|
| 256 | return;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | if (ftype != fun_inner && ftype != fun_exposed) {
|
|---|
| 260 | /* Unknown function type */
|
|---|
| 261 | log_msg(LVL_ERROR,
|
|---|
| 262 | "Unknown function type %d provided by driver.",
|
|---|
| 263 | (int) ftype);
|
|---|
| 264 |
|
|---|
| 265 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 266 | async_answer_0(callid, EINVAL);
|
|---|
| 267 | return;
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | char *fun_name = NULL;
|
|---|
| 271 | int rc = async_data_write_accept((void **)&fun_name, true, 0, 0, 0, 0);
|
|---|
| 272 | if (rc != EOK) {
|
|---|
| 273 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 274 | async_answer_0(callid, rc);
|
|---|
| 275 | return;
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | /* Check that function with same name is not there already. */
|
|---|
| 279 | if (find_fun_node_in_device(pdev, fun_name) != NULL) {
|
|---|
| 280 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 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 | if (!insert_fun_node(&device_tree, fun, fun_name, pdev)) {
|
|---|
| 290 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 291 | delete_fun_node(fun);
|
|---|
| 292 | async_answer_0(callid, ENOMEM);
|
|---|
| 293 | return;
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | if (ftype == fun_inner) {
|
|---|
| 297 | dev = create_dev_node();
|
|---|
| 298 | if (dev == NULL) {
|
|---|
| 299 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 300 | delete_fun_node(fun);
|
|---|
| 301 | async_answer_0(callid, ENOMEM);
|
|---|
| 302 | return;
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | insert_dev_node(tree, dev, fun);
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 309 |
|
|---|
| 310 | log_msg(LVL_DEBUG, "devman_add_function(fun=\"%s\")", fun->pathname);
|
|---|
| 311 |
|
|---|
| 312 | devman_receive_match_ids(match_count, &fun->match_ids);
|
|---|
| 313 |
|
|---|
| 314 | if (ftype == fun_inner) {
|
|---|
| 315 | assert(dev != NULL);
|
|---|
| 316 | /*
|
|---|
| 317 | * Try to find a suitable driver and assign it to the device. We do
|
|---|
| 318 | * not want to block the current fibril that is used for processing
|
|---|
| 319 | * incoming calls: we will launch a separate fibril to handle the
|
|---|
| 320 | * driver assigning. That is because assign_driver can actually include
|
|---|
| 321 | * task spawning which could take some time.
|
|---|
| 322 | */
|
|---|
| 323 | fid_t assign_fibril = fibril_create(assign_driver_fibril, dev);
|
|---|
| 324 | if (assign_fibril == 0) {
|
|---|
| 325 | /*
|
|---|
| 326 | * Fallback in case we are out of memory.
|
|---|
| 327 | * Probably not needed as we will die soon anyway ;-).
|
|---|
| 328 | */
|
|---|
| 329 | (void) assign_driver_fibril(fun);
|
|---|
| 330 | } else {
|
|---|
| 331 | fibril_add_ready(assign_fibril);
|
|---|
| 332 | }
|
|---|
| 333 | } else {
|
|---|
| 334 | devmap_register_tree_function(fun, tree);
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | /* Return device handle to parent's driver. */
|
|---|
| 338 | async_answer_1(callid, EOK, fun->handle);
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | static void devmap_register_class_dev(dev_class_info_t *cli)
|
|---|
| 342 | {
|
|---|
| 343 | /* Create devmap path and name for the device. */
|
|---|
| 344 | char *devmap_pathname = NULL;
|
|---|
| 345 |
|
|---|
| 346 | asprintf(&devmap_pathname, "%s/%s%c%s", DEVMAP_CLASS_NAMESPACE,
|
|---|
| 347 | cli->dev_class->name, DEVMAP_SEPARATOR, cli->dev_name);
|
|---|
| 348 | if (devmap_pathname == NULL)
|
|---|
| 349 | return;
|
|---|
| 350 |
|
|---|
| 351 | /*
|
|---|
| 352 | * Register the device by the device mapper and remember its devmap
|
|---|
| 353 | * handle.
|
|---|
| 354 | */
|
|---|
| 355 | devmap_device_register_with_iface(devmap_pathname,
|
|---|
| 356 | &cli->devmap_handle, DEVMAN_CONNECT_FROM_DEVMAP);
|
|---|
| 357 |
|
|---|
| 358 | /*
|
|---|
| 359 | * Add device to the hash map of class devices registered by device
|
|---|
| 360 | * mapper.
|
|---|
| 361 | */
|
|---|
| 362 | class_add_devmap_function(&class_list, cli);
|
|---|
| 363 |
|
|---|
| 364 | free(devmap_pathname);
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | static void devman_add_function_to_class(ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 368 | {
|
|---|
| 369 | devman_handle_t handle = IPC_GET_ARG1(*call);
|
|---|
| 370 |
|
|---|
| 371 | /* Get class name. */
|
|---|
| 372 | char *class_name;
|
|---|
| 373 | int rc = async_data_write_accept((void **) &class_name, true,
|
|---|
| 374 | 0, 0, 0, 0);
|
|---|
| 375 | if (rc != EOK) {
|
|---|
| 376 | async_answer_0(callid, rc);
|
|---|
| 377 | return;
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | fun_node_t *fun = find_fun_node(&device_tree, handle);
|
|---|
| 381 | if (fun == NULL) {
|
|---|
| 382 | async_answer_0(callid, ENOENT);
|
|---|
| 383 | return;
|
|---|
| 384 | }
|
|---|
| 385 |
|
|---|
| 386 | dev_class_t *cl = get_dev_class(&class_list, class_name);
|
|---|
| 387 | dev_class_info_t *class_info = add_function_to_class(fun, cl, NULL);
|
|---|
| 388 |
|
|---|
| 389 | /* Register the device's class alias by devmapper. */
|
|---|
| 390 | devmap_register_class_dev(class_info);
|
|---|
| 391 |
|
|---|
| 392 | log_msg(LVL_NOTE, "Function `%s' added to class `%s' as `%s'.",
|
|---|
| 393 | fun->pathname, class_name, class_info->dev_name);
|
|---|
| 394 |
|
|---|
| 395 | async_answer_0(callid, EOK);
|
|---|
| 396 | }
|
|---|
| 397 |
|
|---|
| 398 | /** Initialize driver which has registered itself as running and ready.
|
|---|
| 399 | *
|
|---|
| 400 | * The initialization is done in a separate fibril to avoid deadlocks (if the
|
|---|
| 401 | * driver needed to be served by devman during the driver's initialization).
|
|---|
| 402 | */
|
|---|
| 403 | static int init_running_drv(void *drv)
|
|---|
| 404 | {
|
|---|
| 405 | driver_t *driver = (driver_t *) drv;
|
|---|
| 406 |
|
|---|
| 407 | initialize_running_driver(driver, &device_tree);
|
|---|
| 408 | log_msg(LVL_DEBUG, "The `%s` driver was successfully initialized.",
|
|---|
| 409 | driver->name);
|
|---|
| 410 | return 0;
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | /** Function for handling connections from a driver to the device manager. */
|
|---|
| 414 | static void devman_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 415 | {
|
|---|
| 416 | /* Accept the connection. */
|
|---|
| 417 | async_answer_0(iid, EOK);
|
|---|
| 418 |
|
|---|
| 419 | driver_t *driver = devman_driver_register();
|
|---|
| 420 | if (driver == NULL)
|
|---|
| 421 | return;
|
|---|
| 422 |
|
|---|
| 423 | /*
|
|---|
| 424 | * Initialize the driver as running (e.g. pass assigned devices to it)
|
|---|
| 425 | * in a separate fibril; the separate fibril is used to enable the
|
|---|
| 426 | * driver to use devman service during the driver's initialization.
|
|---|
| 427 | */
|
|---|
| 428 | fid_t fid = fibril_create(init_running_drv, driver);
|
|---|
| 429 | if (fid == 0) {
|
|---|
| 430 | log_msg(LVL_ERROR, "Failed to create initialization fibril " \
|
|---|
| 431 | "for driver `%s'.", driver->name);
|
|---|
| 432 | return;
|
|---|
| 433 | }
|
|---|
| 434 | fibril_add_ready(fid);
|
|---|
| 435 |
|
|---|
| 436 | ipc_callid_t callid;
|
|---|
| 437 | ipc_call_t call;
|
|---|
| 438 | bool cont = true;
|
|---|
| 439 | while (cont) {
|
|---|
| 440 | callid = async_get_call(&call);
|
|---|
| 441 |
|
|---|
| 442 | switch (IPC_GET_IMETHOD(call)) {
|
|---|
| 443 | case IPC_M_PHONE_HUNGUP:
|
|---|
| 444 | cont = false;
|
|---|
| 445 | continue;
|
|---|
| 446 | case DEVMAN_ADD_FUNCTION:
|
|---|
| 447 | devman_add_function(callid, &call);
|
|---|
| 448 | break;
|
|---|
| 449 | case DEVMAN_ADD_DEVICE_TO_CLASS:
|
|---|
| 450 | devman_add_function_to_class(callid, &call);
|
|---|
| 451 | break;
|
|---|
| 452 | default:
|
|---|
| 453 | async_answer_0(callid, EINVAL);
|
|---|
| 454 | break;
|
|---|
| 455 | }
|
|---|
| 456 | }
|
|---|
| 457 | }
|
|---|
| 458 |
|
|---|
| 459 | /** Find handle for the device instance identified by the device's path in the
|
|---|
| 460 | * device tree. */
|
|---|
| 461 | static void devman_function_get_handle(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 462 | {
|
|---|
| 463 | char *pathname;
|
|---|
| 464 |
|
|---|
| 465 | int rc = async_data_write_accept((void **) &pathname, true, 0, 0, 0, 0);
|
|---|
| 466 | if (rc != EOK) {
|
|---|
| 467 | async_answer_0(iid, rc);
|
|---|
| 468 | return;
|
|---|
| 469 | }
|
|---|
| 470 |
|
|---|
| 471 | fun_node_t *fun = find_fun_node_by_path(&device_tree, pathname);
|
|---|
| 472 |
|
|---|
| 473 | free(pathname);
|
|---|
| 474 |
|
|---|
| 475 | if (fun == NULL) {
|
|---|
| 476 | async_answer_0(iid, ENOENT);
|
|---|
| 477 | return;
|
|---|
| 478 | }
|
|---|
| 479 |
|
|---|
| 480 | async_answer_1(iid, EOK, fun->handle);
|
|---|
| 481 | }
|
|---|
| 482 |
|
|---|
| 483 | /** Find handle for the device instance identified by device class name. */
|
|---|
| 484 | static void devman_function_get_handle_by_class(ipc_callid_t iid,
|
|---|
| 485 | ipc_call_t *icall)
|
|---|
| 486 | {
|
|---|
| 487 | char *classname;
|
|---|
| 488 | char *devname;
|
|---|
| 489 |
|
|---|
| 490 | int rc = async_data_write_accept((void **) &classname, true, 0, 0, 0, 0);
|
|---|
| 491 | if (rc != EOK) {
|
|---|
| 492 | async_answer_0(iid, rc);
|
|---|
| 493 | return;
|
|---|
| 494 | }
|
|---|
| 495 | rc = async_data_write_accept((void **) &devname, true, 0, 0, 0, 0);
|
|---|
| 496 | if (rc != EOK) {
|
|---|
| 497 | free(classname);
|
|---|
| 498 | async_answer_0(iid, rc);
|
|---|
| 499 | return;
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 |
|
|---|
| 503 | fun_node_t *fun = find_fun_node_by_class(&class_list,
|
|---|
| 504 | classname, devname);
|
|---|
| 505 |
|
|---|
| 506 | free(classname);
|
|---|
| 507 | free(devname);
|
|---|
| 508 |
|
|---|
| 509 | if (fun == NULL) {
|
|---|
| 510 | async_answer_0(iid, ENOENT);
|
|---|
| 511 | return;
|
|---|
| 512 | }
|
|---|
| 513 |
|
|---|
| 514 | async_answer_1(iid, EOK, fun->handle);
|
|---|
| 515 | }
|
|---|
| 516 |
|
|---|
| 517 |
|
|---|
| 518 | /** Function for handling connections from a client to the device manager. */
|
|---|
| 519 | static void devman_connection_client(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 520 | {
|
|---|
| 521 | /* Accept connection. */
|
|---|
| 522 | async_answer_0(iid, EOK);
|
|---|
| 523 |
|
|---|
| 524 | bool cont = true;
|
|---|
| 525 | while (cont) {
|
|---|
| 526 | ipc_call_t call;
|
|---|
| 527 | ipc_callid_t callid = async_get_call(&call);
|
|---|
| 528 |
|
|---|
| 529 | switch (IPC_GET_IMETHOD(call)) {
|
|---|
| 530 | case IPC_M_PHONE_HUNGUP:
|
|---|
| 531 | cont = false;
|
|---|
| 532 | continue;
|
|---|
| 533 | case DEVMAN_DEVICE_GET_HANDLE:
|
|---|
| 534 | devman_function_get_handle(callid, &call);
|
|---|
| 535 | break;
|
|---|
| 536 | case DEVMAN_DEVICE_GET_HANDLE_BY_CLASS:
|
|---|
| 537 | devman_function_get_handle_by_class(callid, &call);
|
|---|
| 538 | break;
|
|---|
| 539 | default:
|
|---|
| 540 | async_answer_0(callid, ENOENT);
|
|---|
| 541 | }
|
|---|
| 542 | }
|
|---|
| 543 | }
|
|---|
| 544 |
|
|---|
| 545 | static void devman_forward(ipc_callid_t iid, ipc_call_t *icall,
|
|---|
| 546 | bool drv_to_parent)
|
|---|
| 547 | {
|
|---|
| 548 | devman_handle_t handle = IPC_GET_ARG2(*icall);
|
|---|
| 549 | devman_handle_t fwd_h;
|
|---|
| 550 | fun_node_t *fun = NULL;
|
|---|
| 551 | dev_node_t *dev = NULL;
|
|---|
| 552 |
|
|---|
| 553 | fun = find_fun_node(&device_tree, handle);
|
|---|
| 554 | if (fun == NULL)
|
|---|
| 555 | dev = find_dev_node(&device_tree, handle);
|
|---|
| 556 | else
|
|---|
| 557 | dev = fun->dev;
|
|---|
| 558 |
|
|---|
| 559 | /*
|
|---|
| 560 | * For a valid function to connect to we need a device. The root
|
|---|
| 561 | * function, for example, has no device and cannot be connected to.
|
|---|
| 562 | * This means @c dev needs to be valid regardless whether we are
|
|---|
| 563 | * connecting to a device or to a function.
|
|---|
| 564 | */
|
|---|
| 565 | if (dev == NULL) {
|
|---|
| 566 | log_msg(LVL_ERROR, "IPC forwarding failed - no device or "
|
|---|
| 567 | "function with handle %" PRIun " was found.", handle);
|
|---|
| 568 | async_answer_0(iid, ENOENT);
|
|---|
| 569 | return;
|
|---|
| 570 | }
|
|---|
| 571 |
|
|---|
| 572 | if (fun == NULL && !drv_to_parent) {
|
|---|
| 573 | log_msg(LVL_ERROR, NAME ": devman_forward error - cannot "
|
|---|
| 574 | "connect to handle %" PRIun ", refers to a device.",
|
|---|
| 575 | handle);
|
|---|
| 576 | async_answer_0(iid, ENOENT);
|
|---|
| 577 | return;
|
|---|
| 578 | }
|
|---|
| 579 |
|
|---|
| 580 | driver_t *driver = NULL;
|
|---|
| 581 |
|
|---|
| 582 | if (drv_to_parent) {
|
|---|
| 583 | /* Connect to parent function of a device (or device function). */
|
|---|
| 584 | if (dev->pfun->dev != NULL)
|
|---|
| 585 | driver = dev->pfun->dev->drv;
|
|---|
| 586 | fwd_h = dev->pfun->handle;
|
|---|
| 587 | } else if (dev->state == DEVICE_USABLE) {
|
|---|
| 588 | /* Connect to the specified function */
|
|---|
| 589 | driver = dev->drv;
|
|---|
| 590 | assert(driver != NULL);
|
|---|
| 591 |
|
|---|
| 592 | fwd_h = handle;
|
|---|
| 593 | }
|
|---|
| 594 |
|
|---|
| 595 | if (driver == NULL) {
|
|---|
| 596 | log_msg(LVL_ERROR, "IPC forwarding refused - " \
|
|---|
| 597 | "the device %" PRIun " is not in usable state.", handle);
|
|---|
| 598 | async_answer_0(iid, ENOENT);
|
|---|
| 599 | return;
|
|---|
| 600 | }
|
|---|
| 601 |
|
|---|
| 602 | int method;
|
|---|
| 603 | if (drv_to_parent)
|
|---|
| 604 | method = DRIVER_DRIVER;
|
|---|
| 605 | else
|
|---|
| 606 | method = DRIVER_CLIENT;
|
|---|
| 607 |
|
|---|
| 608 | if (driver->phone < 0) {
|
|---|
| 609 | log_msg(LVL_ERROR,
|
|---|
| 610 | "Could not forward to driver `%s' (phone is %d).",
|
|---|
| 611 | driver->name, (int) driver->phone);
|
|---|
| 612 | async_answer_0(iid, EINVAL);
|
|---|
| 613 | return;
|
|---|
| 614 | }
|
|---|
| 615 |
|
|---|
| 616 | if (fun != NULL) {
|
|---|
| 617 | log_msg(LVL_DEBUG,
|
|---|
| 618 | "Forwarding request for `%s' function to driver `%s'.",
|
|---|
| 619 | fun->pathname, driver->name);
|
|---|
| 620 | } else {
|
|---|
| 621 | log_msg(LVL_DEBUG,
|
|---|
| 622 | "Forwarding request for `%s' device to driver `%s'.",
|
|---|
| 623 | dev->pfun->pathname, driver->name);
|
|---|
| 624 | }
|
|---|
| 625 |
|
|---|
| 626 | async_forward_fast(iid, driver->phone, method, fwd_h, 0, IPC_FF_NONE);
|
|---|
| 627 | }
|
|---|
| 628 |
|
|---|
| 629 | /** Function for handling connections from a client forwarded by the device
|
|---|
| 630 | * mapper to the device manager. */
|
|---|
| 631 | static void devman_connection_devmapper(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 632 | {
|
|---|
| 633 | devmap_handle_t devmap_handle = IPC_GET_ARG2(*icall);
|
|---|
| 634 | fun_node_t *fun;
|
|---|
| 635 | dev_node_t *dev;
|
|---|
| 636 |
|
|---|
| 637 | fun = find_devmap_tree_function(&device_tree, devmap_handle);
|
|---|
| 638 | if (fun == NULL)
|
|---|
| 639 | fun = find_devmap_class_function(&class_list, devmap_handle);
|
|---|
| 640 |
|
|---|
| 641 | if (fun == NULL || fun->dev->drv == NULL) {
|
|---|
| 642 | async_answer_0(iid, ENOENT);
|
|---|
| 643 | return;
|
|---|
| 644 | }
|
|---|
| 645 |
|
|---|
| 646 | dev = fun->dev;
|
|---|
| 647 |
|
|---|
| 648 | if (dev->state != DEVICE_USABLE || dev->drv->phone < 0) {
|
|---|
| 649 | async_answer_0(iid, EINVAL);
|
|---|
| 650 | return;
|
|---|
| 651 | }
|
|---|
| 652 |
|
|---|
| 653 | async_forward_fast(iid, dev->drv->phone, DRIVER_CLIENT, fun->handle, 0,
|
|---|
| 654 | IPC_FF_NONE);
|
|---|
| 655 | log_msg(LVL_DEBUG,
|
|---|
| 656 | "Forwarding devmapper request for `%s' function to driver `%s'.",
|
|---|
| 657 | fun->pathname, dev->drv->name);
|
|---|
| 658 | }
|
|---|
| 659 |
|
|---|
| 660 | /** Function for handling connections to device manager. */
|
|---|
| 661 | static void devman_connection(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 662 | {
|
|---|
| 663 | /* Select interface. */
|
|---|
| 664 | switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
|
|---|
| 665 | case DEVMAN_DRIVER:
|
|---|
| 666 | devman_connection_driver(iid, icall);
|
|---|
| 667 | break;
|
|---|
| 668 | case DEVMAN_CLIENT:
|
|---|
| 669 | devman_connection_client(iid, icall);
|
|---|
| 670 | break;
|
|---|
| 671 | case DEVMAN_CONNECT_TO_DEVICE:
|
|---|
| 672 | /* Connect client to selected device. */
|
|---|
| 673 | devman_forward(iid, icall, false);
|
|---|
| 674 | break;
|
|---|
| 675 | case DEVMAN_CONNECT_FROM_DEVMAP:
|
|---|
| 676 | /* Someone connected through devmap node. */
|
|---|
| 677 | devman_connection_devmapper(iid, icall);
|
|---|
| 678 | break;
|
|---|
| 679 | case DEVMAN_CONNECT_TO_PARENTS_DEVICE:
|
|---|
| 680 | /* Connect client to selected device. */
|
|---|
| 681 | devman_forward(iid, icall, true);
|
|---|
| 682 | break;
|
|---|
| 683 | default:
|
|---|
| 684 | /* No such interface */
|
|---|
| 685 | async_answer_0(iid, ENOENT);
|
|---|
| 686 | }
|
|---|
| 687 | }
|
|---|
| 688 |
|
|---|
| 689 | /** Initialize device manager internal structures. */
|
|---|
| 690 | static bool devman_init(void)
|
|---|
| 691 | {
|
|---|
| 692 | log_msg(LVL_DEBUG, "devman_init - looking for available drivers.");
|
|---|
| 693 |
|
|---|
| 694 | /* Initialize list of available drivers. */
|
|---|
| 695 | init_driver_list(&drivers_list);
|
|---|
| 696 | if (lookup_available_drivers(&drivers_list,
|
|---|
| 697 | DRIVER_DEFAULT_STORE) == 0) {
|
|---|
| 698 | log_msg(LVL_FATAL, "No drivers found.");
|
|---|
| 699 | return false;
|
|---|
| 700 | }
|
|---|
| 701 |
|
|---|
| 702 | log_msg(LVL_DEBUG, "devman_init - list of drivers has been initialized.");
|
|---|
| 703 |
|
|---|
| 704 | /* Create root device node. */
|
|---|
| 705 | if (!init_device_tree(&device_tree, &drivers_list)) {
|
|---|
| 706 | log_msg(LVL_FATAL, "Failed to initialize device tree.");
|
|---|
| 707 | return false;
|
|---|
| 708 | }
|
|---|
| 709 |
|
|---|
| 710 | init_class_list(&class_list);
|
|---|
| 711 |
|
|---|
| 712 | /*
|
|---|
| 713 | * !!! devman_connection ... as the device manager is not a real devmap
|
|---|
| 714 | * driver (it uses a completely different ipc protocol than an ordinary
|
|---|
| 715 | * devmap driver) forwarding a connection from client to the devman by
|
|---|
| 716 | * devmapper would not work.
|
|---|
| 717 | */
|
|---|
| 718 | devmap_driver_register(NAME, devman_connection);
|
|---|
| 719 |
|
|---|
| 720 | return true;
|
|---|
| 721 | }
|
|---|
| 722 |
|
|---|
| 723 | int main(int argc, char *argv[])
|
|---|
| 724 | {
|
|---|
| 725 | printf(NAME ": HelenOS Device Manager\n");
|
|---|
| 726 |
|
|---|
| 727 | if (log_init(NAME, LVL_ERROR) != EOK) {
|
|---|
| 728 | printf(NAME ": Error initializing logging subsystem.\n");
|
|---|
| 729 | return -1;
|
|---|
| 730 | }
|
|---|
| 731 |
|
|---|
| 732 | if (!devman_init()) {
|
|---|
| 733 | log_msg(LVL_ERROR, "Error while initializing service.");
|
|---|
| 734 | return -1;
|
|---|
| 735 | }
|
|---|
| 736 |
|
|---|
| 737 | /* Set a handler of incomming connections. */
|
|---|
| 738 | async_set_client_connection(devman_connection);
|
|---|
| 739 |
|
|---|
| 740 | /* Register device manager at naming service. */
|
|---|
| 741 | if (service_register(SERVICE_DEVMAN) != EOK) {
|
|---|
| 742 | log_msg(LVL_ERROR, "Failed registering as a service.");
|
|---|
| 743 | return -1;
|
|---|
| 744 | }
|
|---|
| 745 |
|
|---|
| 746 | printf(NAME ": Accepting connections.\n");
|
|---|
| 747 | async_manager();
|
|---|
| 748 |
|
|---|
| 749 | /* Never reached. */
|
|---|
| 750 | return 0;
|
|---|
| 751 | }
|
|---|
| 752 |
|
|---|
| 753 | /** @}
|
|---|
| 754 | */
|
|---|