[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>
|
---|
| 40 | #include <ipc/ns.h>
|
---|
| 41 | #include <async.h>
|
---|
| 42 | #include <stdio.h>
|
---|
| 43 | #include <errno.h>
|
---|
| 44 | #include <bool.h>
|
---|
| 45 | #include <fibril_synch.h>
|
---|
| 46 | #include <stdlib.h>
|
---|
| 47 | #include <string.h>
|
---|
| 48 | #include <dirent.h>
|
---|
| 49 | #include <fcntl.h>
|
---|
| 50 | #include <sys/stat.h>
|
---|
| 51 | #include <ctype.h>
|
---|
| 52 | #include <ipc/devman.h>
|
---|
[5cd136ab] | 53 | #include <ipc/driver.h>
|
---|
[d347b53] | 54 | #include <thread.h>
|
---|
[e2b9a993] | 55 |
|
---|
| 56 | #include "devman.h"
|
---|
| 57 |
|
---|
| 58 | #define DRIVER_DEFAULT_STORE "/srv/drivers"
|
---|
| 59 |
|
---|
[0c3666d] | 60 | static driver_list_t drivers_list;
|
---|
[e2b9a993] | 61 | static dev_tree_t device_tree;
|
---|
| 62 |
|
---|
[729fa2d6] | 63 | /**
|
---|
| 64 | * Register running driver.
|
---|
| 65 | */
|
---|
| 66 | static driver_t * devman_driver_register(void)
|
---|
| 67 | {
|
---|
| 68 | printf(NAME ": devman_driver_register \n");
|
---|
| 69 |
|
---|
| 70 | ipc_call_t icall;
|
---|
| 71 | ipc_callid_t iid = async_get_call(&icall);
|
---|
| 72 | driver_t *driver = NULL;
|
---|
| 73 |
|
---|
| 74 | if (IPC_GET_METHOD(icall) != DEVMAN_DRIVER_REGISTER) {
|
---|
| 75 | ipc_answer_0(iid, EREFUSED);
|
---|
| 76 | return NULL;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[92413de] | 79 | char *drv_name = NULL;
|
---|
[729fa2d6] | 80 |
|
---|
| 81 | // Get driver name
|
---|
[92413de] | 82 | int rc = async_string_receive(&drv_name, DEVMAN_NAME_MAXLEN, NULL);
|
---|
[729fa2d6] | 83 | if (rc != EOK) {
|
---|
| 84 | ipc_answer_0(iid, rc);
|
---|
| 85 | return NULL;
|
---|
| 86 | }
|
---|
| 87 | printf(NAME ": the %s driver is trying to register by the service.\n", drv_name);
|
---|
| 88 |
|
---|
| 89 | // Find driver structure
|
---|
| 90 | driver = find_driver(&drivers_list, drv_name);
|
---|
[92413de] | 91 |
|
---|
| 92 | free(drv_name);
|
---|
| 93 | drv_name = NULL;
|
---|
| 94 |
|
---|
[729fa2d6] | 95 | if (NULL == driver) {
|
---|
| 96 | printf(NAME ": no driver named %s was found.\n", drv_name);
|
---|
| 97 | ipc_answer_0(iid, ENOENT);
|
---|
| 98 | return NULL;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | // Create connection to the driver
|
---|
| 102 | printf(NAME ": creating connection to the %s driver.\n", driver->name);
|
---|
| 103 | ipc_call_t call;
|
---|
| 104 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 105 | if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
|
---|
| 106 | ipc_answer_0(callid, ENOTSUP);
|
---|
| 107 | ipc_answer_0(iid, ENOTSUP);
|
---|
| 108 | return NULL;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[c16cf62] | 111 | // remember driver's phone
|
---|
| 112 | set_driver_phone(driver, IPC_GET_ARG5(call));
|
---|
[729fa2d6] | 113 |
|
---|
| 114 | printf(NAME ": the %s driver was successfully registered as running.\n", driver->name);
|
---|
| 115 |
|
---|
| 116 | ipc_answer_0(callid, EOK);
|
---|
| 117 |
|
---|
| 118 | ipc_answer_0(iid, EOK);
|
---|
| 119 |
|
---|
| 120 | return driver;
|
---|
| 121 | }
|
---|
[e2b9a993] | 122 |
|
---|
[66babbd] | 123 | static int devman_receive_match_id(match_id_list_t *match_ids) {
|
---|
| 124 |
|
---|
| 125 | match_id_t *match_id = create_match_id();
|
---|
| 126 | ipc_callid_t callid;
|
---|
| 127 | ipc_call_t call;
|
---|
| 128 | int rc = 0;
|
---|
| 129 |
|
---|
| 130 | callid = async_get_call(&call);
|
---|
| 131 | if (DEVMAN_ADD_MATCH_ID != IPC_GET_METHOD(call)) {
|
---|
| 132 | printf(NAME ": ERROR: devman_receive_match_id - invalid protocol.\n");
|
---|
| 133 | ipc_answer_0(callid, EINVAL);
|
---|
| 134 | delete_match_id(match_id);
|
---|
| 135 | return EINVAL;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | if (NULL == match_id) {
|
---|
| 139 | printf(NAME ": ERROR: devman_receive_match_id - failed to allocate match id.\n");
|
---|
| 140 | ipc_answer_0(callid, ENOMEM);
|
---|
| 141 | return ENOMEM;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[2480e19] | 144 | ipc_answer_0(callid, EOK);
|
---|
| 145 |
|
---|
[66babbd] | 146 | match_id->score = IPC_GET_ARG1(call);
|
---|
| 147 |
|
---|
| 148 | rc = async_string_receive(&match_id->id, DEVMAN_NAME_MAXLEN, NULL);
|
---|
| 149 | if (EOK != rc) {
|
---|
| 150 | delete_match_id(match_id);
|
---|
[2480e19] | 151 | printf(NAME ": devman_receive_match_id - failed to receive match id string.\n");
|
---|
[66babbd] | 152 | return rc;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | list_append(&match_id->link, &match_ids->ids);
|
---|
[a087f2e] | 156 |
|
---|
| 157 | printf(NAME ": received match id '%s', score = %d \n", match_id->id, match_id->score);
|
---|
[66babbd] | 158 | return rc;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | static int devman_receive_match_ids(ipcarg_t match_count, match_id_list_t *match_ids)
|
---|
[2480e19] | 162 | {
|
---|
[66babbd] | 163 | int ret = EOK;
|
---|
[5cd136ab] | 164 | size_t i;
|
---|
[66babbd] | 165 | for (i = 0; i < match_count; i++) {
|
---|
| 166 | if (EOK != (ret = devman_receive_match_id(match_ids))) {
|
---|
| 167 | return ret;
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 | return ret;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[bda60d9] | 173 | static void devman_add_child(ipc_callid_t callid, ipc_call_t *call, driver_t *driver)
|
---|
| 174 | {
|
---|
[2480e19] | 175 | // printf(NAME ": devman_add_child\n");
|
---|
[66babbd] | 176 |
|
---|
[d347b53] | 177 | device_handle_t parent_handle = IPC_GET_ARG1(*call);
|
---|
[66babbd] | 178 | ipcarg_t match_count = IPC_GET_ARG2(*call);
|
---|
| 179 |
|
---|
[d347b53] | 180 | node_t *parent = find_dev_node(&device_tree, parent_handle);
|
---|
| 181 |
|
---|
| 182 | if (NULL == parent) {
|
---|
| 183 | ipc_answer_0(callid, ENOENT);
|
---|
| 184 | return;
|
---|
[66babbd] | 185 | }
|
---|
[d347b53] | 186 |
|
---|
| 187 | char *dev_name = NULL;
|
---|
| 188 | int rc = async_string_receive(&dev_name, DEVMAN_NAME_MAXLEN, NULL);
|
---|
[66babbd] | 189 | if (EOK != rc) {
|
---|
[d347b53] | 190 | ipc_answer_0(callid, rc);
|
---|
| 191 | return;
|
---|
| 192 | }
|
---|
[2480e19] | 193 | // printf(NAME ": newly added child device's name is '%s'.\n", dev_name);
|
---|
[d347b53] | 194 |
|
---|
| 195 | node_t *node = create_dev_node();
|
---|
| 196 | if (!insert_dev_node(&device_tree, node, dev_name, parent)) {
|
---|
| 197 | delete_dev_node(node);
|
---|
| 198 | ipc_answer_0(callid, ENOMEM);
|
---|
| 199 | return;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[2480e19] | 202 | printf(NAME ": devman_add_child %s\n", node->pathname);
|
---|
| 203 |
|
---|
[66babbd] | 204 | devman_receive_match_ids(match_count, &node->match_ids);
|
---|
[bda60d9] | 205 |
|
---|
[d347b53] | 206 | // return device handle to parent's driver
|
---|
| 207 | ipc_answer_1(callid, EOK, node->handle);
|
---|
[bda60d9] | 208 |
|
---|
[d347b53] | 209 | // try to find suitable driver and assign it to the device
|
---|
| 210 | assign_driver(node, &drivers_list);
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | static int init_running_drv(void *drv)
|
---|
| 214 | {
|
---|
| 215 | driver_t *driver = (driver_t *)drv;
|
---|
| 216 | initialize_running_driver(driver);
|
---|
| 217 | printf(NAME ": the %s driver was successfully initialized. \n", driver->name);
|
---|
| 218 | return 0;
|
---|
[bda60d9] | 219 | }
|
---|
| 220 |
|
---|
[e2b9a993] | 221 | /** Function for handling connections to device manager.
|
---|
| 222 | *
|
---|
| 223 | */
|
---|
[924c75e1] | 224 | static void devman_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
|
---|
[729fa2d6] | 225 | {
|
---|
[e2b9a993] | 226 | /* Accept the connection */
|
---|
| 227 | ipc_answer_0(iid, EOK);
|
---|
| 228 |
|
---|
[729fa2d6] | 229 | driver_t *driver = devman_driver_register();
|
---|
[d347b53] | 230 | if (NULL == driver)
|
---|
[729fa2d6] | 231 | return;
|
---|
[d347b53] | 232 |
|
---|
| 233 | fid_t fid = fibril_create(init_running_drv, driver);
|
---|
| 234 | if (fid == 0) {
|
---|
| 235 | printf(NAME ": Error creating fibril for the initialization of the newly registered running driver.\n");
|
---|
| 236 | exit(1);
|
---|
| 237 | }
|
---|
| 238 | fibril_add_ready(fid);
|
---|
| 239 |
|
---|
| 240 | /*thread_id_t tid;
|
---|
| 241 | if (0 != thread_create(init_running_drv, driver, "init_running_drv", &tid)) {
|
---|
| 242 | printf(NAME ": failed to start the initialization of the newly registered running driver.\n");
|
---|
| 243 | }*/
|
---|
[729fa2d6] | 244 |
|
---|
| 245 | ipc_callid_t callid;
|
---|
| 246 | ipc_call_t call;
|
---|
| 247 | bool cont = true;
|
---|
| 248 | while (cont) {
|
---|
[e2b9a993] | 249 | callid = async_get_call(&call);
|
---|
| 250 |
|
---|
| 251 | switch (IPC_GET_METHOD(call)) {
|
---|
[729fa2d6] | 252 | case IPC_M_PHONE_HUNGUP:
|
---|
| 253 | cont = false;
|
---|
| 254 | continue;
|
---|
[e2b9a993] | 255 | case DEVMAN_ADD_CHILD_DEVICE:
|
---|
[bda60d9] | 256 | devman_add_child(callid, &call, driver);
|
---|
[e2b9a993] | 257 | break;
|
---|
| 258 | default:
|
---|
| 259 | ipc_answer_0(callid, EINVAL);
|
---|
| 260 | break;
|
---|
| 261 | }
|
---|
| 262 | }
|
---|
| 263 | }
|
---|
| 264 |
|
---|
[9a66bc2e] | 265 | static void devman_forward(ipc_callid_t iid, ipc_call_t *icall, bool drv_to_parent) {
|
---|
| 266 |
|
---|
| 267 | device_handle_t handle = IPC_GET_ARG2(*icall);
|
---|
[2480e19] | 268 | // printf(NAME ": devman_forward - trying to forward connection to device with handle %x.\n", handle);
|
---|
[9a66bc2e] | 269 |
|
---|
[5cd136ab] | 270 | node_t *dev = find_dev_node(&device_tree, handle);
|
---|
| 271 | if (NULL == dev) {
|
---|
[9a66bc2e] | 272 | printf(NAME ": devman_forward error - no device with handle %x was found.\n", handle);
|
---|
[5cd136ab] | 273 | ipc_answer_0(iid, ENOENT);
|
---|
| 274 | return;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | driver_t *driver = NULL;
|
---|
| 278 |
|
---|
| 279 | if (drv_to_parent) {
|
---|
[9a66bc2e] | 280 | if (NULL != dev->parent) {
|
---|
| 281 | driver = dev->parent->drv;
|
---|
| 282 | }
|
---|
[5cd136ab] | 283 | } else {
|
---|
| 284 | driver = dev->drv;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
[9a66bc2e] | 287 | if (NULL == driver) {
|
---|
| 288 | printf(NAME ": devman_forward error - no driver to connect to.\n", handle);
|
---|
[5cd136ab] | 289 | ipc_answer_0(iid, ENOENT);
|
---|
| 290 | return;
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | int method;
|
---|
| 294 | if (drv_to_parent) {
|
---|
| 295 | method = DRIVER_DRIVER;
|
---|
| 296 | } else {
|
---|
| 297 | method = DRIVER_CLIENT;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
[9a66bc2e] | 300 | if (driver->phone <= 0) {
|
---|
| 301 | printf(NAME ": devman_forward: cound not forward to driver %s (the driver's phone is %x).\n", driver->name, driver->phone);
|
---|
| 302 | return;
|
---|
| 303 | }
|
---|
[2480e19] | 304 | printf(NAME ": devman_forward: forward connection to device %s to driver %s with phone %d.\n",
|
---|
| 305 | dev->pathname, driver->name, driver->phone);
|
---|
[5cd136ab] | 306 | ipc_forward_fast(iid, driver->phone, method, dev->handle, 0, IPC_FF_NONE);
|
---|
| 307 | }
|
---|
| 308 |
|
---|
[924c75e1] | 309 | /** Function for handling connections to device manager.
|
---|
| 310 | *
|
---|
| 311 | */
|
---|
| 312 | static void devman_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 313 | {
|
---|
| 314 | // Select interface
|
---|
| 315 | switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
|
---|
| 316 | case DEVMAN_DRIVER:
|
---|
| 317 | devman_connection_driver(iid, icall);
|
---|
| 318 | break;
|
---|
| 319 | /*case DEVMAN_CLIENT:
|
---|
| 320 | devmap_connection_client(iid, icall);
|
---|
[5cd136ab] | 321 | break;*/
|
---|
[924c75e1] | 322 | case DEVMAN_CONNECT_TO_DEVICE:
|
---|
| 323 | // Connect client to selected device
|
---|
[5cd136ab] | 324 | devman_forward(iid, icall, false);
|
---|
| 325 | break;
|
---|
| 326 | case DEVMAN_CONNECT_TO_PARENTS_DEVICE:
|
---|
| 327 | // Connect client to selected device
|
---|
| 328 | devman_forward(iid, icall, true);
|
---|
| 329 | break;
|
---|
[924c75e1] | 330 | default:
|
---|
| 331 | /* No such interface */
|
---|
| 332 | ipc_answer_0(iid, ENOENT);
|
---|
| 333 | }
|
---|
| 334 | }
|
---|
| 335 |
|
---|
[e2b9a993] | 336 | /** Initialize device manager internal structures.
|
---|
| 337 | */
|
---|
| 338 | static bool devman_init()
|
---|
| 339 | {
|
---|
[0c3666d] | 340 | printf(NAME ": devman_init - looking for available drivers. \n");
|
---|
[08d9c4e6] | 341 |
|
---|
[e2b9a993] | 342 | // initialize list of available drivers
|
---|
[0c3666d] | 343 | init_driver_list(&drivers_list);
|
---|
[e2b9a993] | 344 | if (0 == lookup_available_drivers(&drivers_list, DRIVER_DEFAULT_STORE)) {
|
---|
| 345 | printf(NAME " no drivers found.");
|
---|
| 346 | return false;
|
---|
| 347 | }
|
---|
[08d9c4e6] | 348 | printf(NAME ": devman_init - list of drivers has been initialized. \n");
|
---|
[e2b9a993] | 349 |
|
---|
| 350 | // create root device node
|
---|
| 351 | if (!init_device_tree(&device_tree, &drivers_list)) {
|
---|
| 352 | printf(NAME " failed to initialize device tree.");
|
---|
| 353 | return false;
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 | return true;
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | int main(int argc, char *argv[])
|
---|
| 360 | {
|
---|
| 361 | printf(NAME ": HelenOS Device Manager\n");
|
---|
| 362 |
|
---|
| 363 | if (!devman_init()) {
|
---|
| 364 | printf(NAME ": Error while initializing service\n");
|
---|
| 365 | return -1;
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | // Set a handler of incomming connections
|
---|
| 369 | async_set_client_connection(devman_connection);
|
---|
| 370 |
|
---|
| 371 | // Register device manager at naming service
|
---|
| 372 | ipcarg_t phonead;
|
---|
| 373 | if (ipc_connect_to_me(PHONE_NS, SERVICE_DEVMAN, 0, 0, &phonead) != 0)
|
---|
| 374 | return -1;
|
---|
| 375 |
|
---|
| 376 | printf(NAME ": Accepting connections\n");
|
---|
| 377 | async_manager();
|
---|
| 378 |
|
---|
| 379 | // Never reached
|
---|
| 380 | return 0;
|
---|
| 381 | }
|
---|
[c16cf62] | 382 |
|
---|
| 383 | /** @}
|
---|
| 384 | */ |
---|