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