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