[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 |
|
---|
| 38 | #include <assert.h>
|
---|
| 39 | #include <ipc/services.h>
|
---|
[79ae36dd] | 40 | #include <ns.h>
|
---|
[e2b9a993] | 41 | #include <async.h>
|
---|
| 42 | #include <stdio.h>
|
---|
| 43 | #include <errno.h>
|
---|
[3e6a98c5] | 44 | #include <stdbool.h>
|
---|
[e2b9a993] | 45 | #include <fibril_synch.h>
|
---|
| 46 | #include <stdlib.h>
|
---|
[c47e1a8] | 47 | #include <str.h>
|
---|
[e2b9a993] | 48 | #include <ctype.h>
|
---|
[9b415c9] | 49 | #include <io/log.h>
|
---|
[e2b9a993] | 50 | #include <ipc/devman.h>
|
---|
[5cd136ab] | 51 | #include <ipc/driver.h>
|
---|
[15f3c3f] | 52 | #include <loc.h>
|
---|
[e2b9a993] | 53 |
|
---|
[d80d7a8] | 54 | #include "client_conn.h"
|
---|
[d1bafbf] | 55 | #include "dev.h"
|
---|
[e2b9a993] | 56 | #include "devman.h"
|
---|
[59dc181] | 57 | #include "devtree.h"
|
---|
[181c32f] | 58 | #include "drv_conn.h"
|
---|
[041b026] | 59 | #include "driver.h"
|
---|
[38e52c92] | 60 | #include "fun.h"
|
---|
[a60e90b] | 61 | #include "loc.h"
|
---|
[e2b9a993] | 62 |
|
---|
[a79d88d] | 63 | #define DRIVER_DEFAULT_STORE "/drv"
|
---|
[e2b9a993] | 64 |
|
---|
[181c32f] | 65 | driver_list_t drivers_list;
|
---|
[d80d7a8] | 66 | dev_tree_t device_tree;
|
---|
[e2b9a993] | 67 |
|
---|
[c6c389ed] | 68 | static void devman_forward(ipc_callid_t iid, ipc_call_t *icall,
|
---|
| 69 | bool drv_to_parent)
|
---|
[38b3baf] | 70 | {
|
---|
[0b5a4131] | 71 | devman_handle_t handle = IPC_GET_ARG2(*icall);
|
---|
[8b1e15ac] | 72 | devman_handle_t fwd_h;
|
---|
| 73 | fun_node_t *fun = NULL;
|
---|
| 74 | dev_node_t *dev = NULL;
|
---|
[9a66bc2e] | 75 |
|
---|
[8b1e15ac] | 76 | fun = find_fun_node(&device_tree, handle);
|
---|
| 77 | if (fun == NULL)
|
---|
| 78 | dev = find_dev_node(&device_tree, handle);
|
---|
[58cbb0c8] | 79 | else {
|
---|
| 80 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
[8b1e15ac] | 81 | dev = fun->dev;
|
---|
[58cbb0c8] | 82 | if (dev != NULL)
|
---|
| 83 | dev_add_ref(dev);
|
---|
| 84 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
| 85 | }
|
---|
[8b1e15ac] | 86 |
|
---|
[0418050] | 87 | /*
|
---|
| 88 | * For a valid function to connect to we need a device. The root
|
---|
| 89 | * function, for example, has no device and cannot be connected to.
|
---|
| 90 | * This means @c dev needs to be valid regardless whether we are
|
---|
| 91 | * connecting to a device or to a function.
|
---|
| 92 | */
|
---|
| 93 | if (dev == NULL) {
|
---|
[a1a101d] | 94 | log_msg(LOG_DEFAULT, LVL_ERROR, "IPC forwarding failed - no device or "
|
---|
[ebcb05a] | 95 | "function with handle %" PRIun " was found.", handle);
|
---|
[ffa2c8ef] | 96 | async_answer_0(iid, ENOENT);
|
---|
[58cbb0c8] | 97 | goto cleanup;
|
---|
[5cd136ab] | 98 | }
|
---|
[8b1e15ac] | 99 |
|
---|
| 100 | if (fun == NULL && !drv_to_parent) {
|
---|
[a1a101d] | 101 | log_msg(LOG_DEFAULT, LVL_ERROR, NAME ": devman_forward error - cannot "
|
---|
[ebcb05a] | 102 | "connect to handle %" PRIun ", refers to a device.",
|
---|
[9b415c9] | 103 | handle);
|
---|
[ffa2c8ef] | 104 | async_answer_0(iid, ENOENT);
|
---|
[58cbb0c8] | 105 | goto cleanup;
|
---|
[5cd136ab] | 106 | }
|
---|
| 107 |
|
---|
| 108 | driver_t *driver = NULL;
|
---|
| 109 |
|
---|
[e2b9b341] | 110 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
| 111 |
|
---|
[5cd136ab] | 112 | if (drv_to_parent) {
|
---|
[8b1e15ac] | 113 | /* Connect to parent function of a device (or device function). */
|
---|
| 114 | if (dev->pfun->dev != NULL)
|
---|
| 115 | driver = dev->pfun->dev->drv;
|
---|
[50ad3f3] | 116 |
|
---|
[8b1e15ac] | 117 | fwd_h = dev->pfun->handle;
|
---|
[005ac3c] | 118 | } else {
|
---|
[8b1e15ac] | 119 | /* Connect to the specified function */
|
---|
[38b3baf] | 120 | driver = dev->drv;
|
---|
[8b1e15ac] | 121 | fwd_h = handle;
|
---|
[5cd136ab] | 122 | }
|
---|
| 123 |
|
---|
[e2b9b341] | 124 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
| 125 |
|
---|
[c6c389ed] | 126 | if (driver == NULL) {
|
---|
[a1a101d] | 127 | log_msg(LOG_DEFAULT, LVL_ERROR, "IPC forwarding refused - " \
|
---|
[79ae36dd] | 128 | "the device %" PRIun " is not in usable state.", handle);
|
---|
[ffa2c8ef] | 129 | async_answer_0(iid, ENOENT);
|
---|
[58cbb0c8] | 130 | goto cleanup;
|
---|
[5cd136ab] | 131 | }
|
---|
| 132 |
|
---|
[38b3baf] | 133 | int method;
|
---|
| 134 | if (drv_to_parent)
|
---|
[5cd136ab] | 135 | method = DRIVER_DRIVER;
|
---|
[38b3baf] | 136 | else
|
---|
[5cd136ab] | 137 | method = DRIVER_CLIENT;
|
---|
| 138 |
|
---|
[79ae36dd] | 139 | if (!driver->sess) {
|
---|
[a1a101d] | 140 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
[79ae36dd] | 141 | "Could not forward to driver `%s'.", driver->name);
|
---|
[ffa2c8ef] | 142 | async_answer_0(iid, EINVAL);
|
---|
[58cbb0c8] | 143 | goto cleanup;
|
---|
[9a66bc2e] | 144 | }
|
---|
[c6c389ed] | 145 |
|
---|
[8b1e15ac] | 146 | if (fun != NULL) {
|
---|
[a1a101d] | 147 | log_msg(LOG_DEFAULT, LVL_DEBUG,
|
---|
[ebcb05a] | 148 | "Forwarding request for `%s' function to driver `%s'.",
|
---|
[9b415c9] | 149 | fun->pathname, driver->name);
|
---|
[8b1e15ac] | 150 | } else {
|
---|
[a1a101d] | 151 | log_msg(LOG_DEFAULT, LVL_DEBUG,
|
---|
[ebcb05a] | 152 | "Forwarding request for `%s' device to driver `%s'.",
|
---|
[9b415c9] | 153 | dev->pfun->pathname, driver->name);
|
---|
[8b1e15ac] | 154 | }
|
---|
[79ae36dd] | 155 |
|
---|
| 156 | async_exch_t *exch = async_exchange_begin(driver->sess);
|
---|
| 157 | async_forward_fast(iid, exch, method, fwd_h, 0, IPC_FF_NONE);
|
---|
| 158 | async_exchange_end(exch);
|
---|
[50ad3f3] | 159 |
|
---|
[58cbb0c8] | 160 | cleanup:
|
---|
| 161 | if (dev != NULL)
|
---|
| 162 | dev_del_ref(dev);
|
---|
[50ad3f3] | 163 |
|
---|
[58cbb0c8] | 164 | if (fun != NULL)
|
---|
| 165 | fun_del_ref(fun);
|
---|
[5cd136ab] | 166 | }
|
---|
| 167 |
|
---|
[15f3c3f] | 168 | /** Function for handling connections from a client forwarded by the location
|
---|
| 169 | * service to the device manager. */
|
---|
| 170 | static void devman_connection_loc(ipc_callid_t iid, ipc_call_t *icall)
|
---|
[ce89036b] | 171 | {
|
---|
[15f3c3f] | 172 | service_id_t service_id = IPC_GET_ARG2(*icall);
|
---|
[ba38f72c] | 173 | fun_node_t *fun;
|
---|
| 174 | dev_node_t *dev;
|
---|
[e2b9b341] | 175 | devman_handle_t handle;
|
---|
| 176 | driver_t *driver;
|
---|
[c6c389ed] | 177 |
|
---|
[15f3c3f] | 178 | fun = find_loc_tree_function(&device_tree, service_id);
|
---|
[ce89036b] | 179 |
|
---|
[e2b9b341] | 180 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
| 181 |
|
---|
| 182 | if (fun == NULL || fun->dev == NULL || fun->dev->drv == NULL) {
|
---|
[a1a101d] | 183 | log_msg(LOG_DEFAULT, LVL_WARN, "devman_connection_loc(): function "
|
---|
[12f9f0d0] | 184 | "not found.\n");
|
---|
[e2b9b341] | 185 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
[ffa2c8ef] | 186 | async_answer_0(iid, ENOENT);
|
---|
[ce89036b] | 187 | return;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[ba38f72c] | 190 | dev = fun->dev;
|
---|
[e2b9b341] | 191 | driver = dev->drv;
|
---|
| 192 | handle = fun->handle;
|
---|
| 193 |
|
---|
| 194 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
[ba38f72c] | 195 |
|
---|
[e2b9b341] | 196 | async_exch_t *exch = async_exchange_begin(driver->sess);
|
---|
| 197 | async_forward_fast(iid, exch, DRIVER_CLIENT, handle, 0,
|
---|
[38b3baf] | 198 | IPC_FF_NONE);
|
---|
[79ae36dd] | 199 | async_exchange_end(exch);
|
---|
| 200 |
|
---|
[a1a101d] | 201 | log_msg(LOG_DEFAULT, LVL_DEBUG,
|
---|
[15f3c3f] | 202 | "Forwarding loc service request for `%s' function to driver `%s'.",
|
---|
[e2b9b341] | 203 | fun->pathname, driver->name);
|
---|
| 204 |
|
---|
| 205 | fun_del_ref(fun);
|
---|
[ce89036b] | 206 | }
|
---|
| 207 |
|
---|
[38b3baf] | 208 | /** Function for handling connections to device manager. */
|
---|
[9934f7d] | 209 | static void devman_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
[38b3baf] | 210 | {
|
---|
[422722e] | 211 | /* Select port. */
|
---|
[96b02eb9] | 212 | switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
|
---|
[924c75e1] | 213 | case DEVMAN_DRIVER:
|
---|
| 214 | devman_connection_driver(iid, icall);
|
---|
| 215 | break;
|
---|
[f658458] | 216 | case DEVMAN_CLIENT:
|
---|
| 217 | devman_connection_client(iid, icall);
|
---|
| 218 | break;
|
---|
[924c75e1] | 219 | case DEVMAN_CONNECT_TO_DEVICE:
|
---|
[38b3baf] | 220 | /* Connect client to selected device. */
|
---|
[5cd136ab] | 221 | devman_forward(iid, icall, false);
|
---|
| 222 | break;
|
---|
[15f3c3f] | 223 | case DEVMAN_CONNECT_FROM_LOC:
|
---|
| 224 | /* Someone connected through loc node. */
|
---|
| 225 | devman_connection_loc(iid, icall);
|
---|
[47a7174f] | 226 | break;
|
---|
[5cd136ab] | 227 | case DEVMAN_CONNECT_TO_PARENTS_DEVICE:
|
---|
[38b3baf] | 228 | /* Connect client to selected device. */
|
---|
[5cd136ab] | 229 | devman_forward(iid, icall, true);
|
---|
[38b3baf] | 230 | break;
|
---|
[924c75e1] | 231 | default:
|
---|
| 232 | /* No such interface */
|
---|
[ffa2c8ef] | 233 | async_answer_0(iid, ENOENT);
|
---|
[924c75e1] | 234 | }
|
---|
| 235 | }
|
---|
| 236 |
|
---|
[422722e] | 237 | static void *devman_client_data_create(void)
|
---|
| 238 | {
|
---|
| 239 | client_t *client;
|
---|
| 240 |
|
---|
| 241 | client = calloc(1, sizeof(client_t));
|
---|
| 242 | if (client == NULL)
|
---|
| 243 | return NULL;
|
---|
| 244 |
|
---|
| 245 | fibril_mutex_initialize(&client->mutex);
|
---|
| 246 | return client;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | static void devman_client_data_destroy(void *data)
|
---|
| 250 | {
|
---|
| 251 | free(data);
|
---|
| 252 | }
|
---|
| 253 |
|
---|
[38b3baf] | 254 | /** Initialize device manager internal structures. */
|
---|
| 255 | static bool devman_init(void)
|
---|
[e2b9a993] | 256 | {
|
---|
[a1a101d] | 257 | log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_init - looking for available drivers.");
|
---|
[08d9c4e6] | 258 |
|
---|
[38b3baf] | 259 | /* Initialize list of available drivers. */
|
---|
[0c3666d] | 260 | init_driver_list(&drivers_list);
|
---|
[c6c389ed] | 261 | if (lookup_available_drivers(&drivers_list,
|
---|
| 262 | DRIVER_DEFAULT_STORE) == 0) {
|
---|
[a1a101d] | 263 | log_msg(LOG_DEFAULT, LVL_FATAL, "No drivers found.");
|
---|
[e2b9a993] | 264 | return false;
|
---|
| 265 | }
|
---|
[50ad3f3] | 266 |
|
---|
[a1a101d] | 267 | log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_init - list of drivers has been initialized.");
|
---|
[50ad3f3] | 268 |
|
---|
[38b3baf] | 269 | /* Create root device node. */
|
---|
[e2b9a993] | 270 | if (!init_device_tree(&device_tree, &drivers_list)) {
|
---|
[a1a101d] | 271 | log_msg(LOG_DEFAULT, LVL_FATAL, "Failed to initialize device tree.");
|
---|
[38b3baf] | 272 | return false;
|
---|
[e2b9a993] | 273 | }
|
---|
[50ad3f3] | 274 |
|
---|
[38b3baf] | 275 | /*
|
---|
[f302586] | 276 | * Caution: As the device manager is not a real loc
|
---|
| 277 | * driver (it uses a completely different IPC protocol
|
---|
| 278 | * than an ordinary loc driver), forwarding a connection
|
---|
| 279 | * from client to the devman by location service will
|
---|
| 280 | * not work.
|
---|
[38b3baf] | 281 | */
|
---|
[f302586] | 282 | loc_server_register(NAME);
|
---|
[ce89036b] | 283 |
|
---|
[e2b9a993] | 284 | return true;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | int main(int argc, char *argv[])
|
---|
| 288 | {
|
---|
[50ad3f3] | 289 | printf("%s: HelenOS Device Manager\n", NAME);
|
---|
| 290 |
|
---|
[267f235] | 291 | int rc = log_init(NAME);
|
---|
[50ad3f3] | 292 | if (rc != EOK) {
|
---|
| 293 | printf("%s: Error initializing logging subsystem.\n", NAME);
|
---|
| 294 | return rc;
|
---|
[9b415c9] | 295 | }
|
---|
[e2b9a993] | 296 |
|
---|
[422722e] | 297 | /* Set handlers for incoming connections. */
|
---|
| 298 | async_set_client_data_constructor(devman_client_data_create);
|
---|
| 299 | async_set_client_data_destructor(devman_client_data_destroy);
|
---|
[e2b9a993] | 300 | async_set_client_connection(devman_connection);
|
---|
[50ad3f3] | 301 |
|
---|
[f302586] | 302 | if (!devman_init()) {
|
---|
[a1a101d] | 303 | log_msg(LOG_DEFAULT, LVL_ERROR, "Error while initializing service.");
|
---|
[f302586] | 304 | return -1;
|
---|
| 305 | }
|
---|
[50ad3f3] | 306 |
|
---|
[38b3baf] | 307 | /* Register device manager at naming service. */
|
---|
[50ad3f3] | 308 | rc = service_register(SERVICE_DEVMAN);
|
---|
| 309 | if (rc != EOK) {
|
---|
[a1a101d] | 310 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering as a service.");
|
---|
[50ad3f3] | 311 | return rc;
|
---|
[9b415c9] | 312 | }
|
---|
[50ad3f3] | 313 |
|
---|
| 314 | printf("%s: Accepting connections.\n", NAME);
|
---|
[79ae36dd] | 315 | task_retval(0);
|
---|
[e2b9a993] | 316 | async_manager();
|
---|
[50ad3f3] | 317 |
|
---|
[38b3baf] | 318 | /* Never reached. */
|
---|
[e2b9a993] | 319 | return 0;
|
---|
| 320 | }
|
---|
[c16cf62] | 321 |
|
---|
| 322 | /** @}
|
---|
[38b3baf] | 323 | */
|
---|