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