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