[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>
|
---|
| 53 |
|
---|
| 54 | #include "devman.h"
|
---|
| 55 |
|
---|
| 56 | #define DRIVER_DEFAULT_STORE "/srv/drivers"
|
---|
| 57 |
|
---|
[0c3666d] | 58 | static driver_list_t drivers_list;
|
---|
[e2b9a993] | 59 | static dev_tree_t device_tree;
|
---|
| 60 |
|
---|
[729fa2d6] | 61 | /**
|
---|
| 62 | * Register running driver.
|
---|
| 63 | */
|
---|
| 64 | static driver_t * devman_driver_register(void)
|
---|
| 65 | {
|
---|
| 66 | printf(NAME ": devman_driver_register \n");
|
---|
| 67 |
|
---|
| 68 | ipc_call_t icall;
|
---|
| 69 | ipc_callid_t iid = async_get_call(&icall);
|
---|
| 70 | driver_t *driver = NULL;
|
---|
| 71 |
|
---|
| 72 | if (IPC_GET_METHOD(icall) != DEVMAN_DRIVER_REGISTER) {
|
---|
| 73 | ipc_answer_0(iid, EREFUSED);
|
---|
| 74 | return NULL;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[92413de] | 77 | char *drv_name = NULL;
|
---|
[729fa2d6] | 78 |
|
---|
| 79 | // Get driver name
|
---|
[92413de] | 80 | int rc = async_string_receive(&drv_name, DEVMAN_NAME_MAXLEN, NULL);
|
---|
[729fa2d6] | 81 | if (rc != EOK) {
|
---|
| 82 | ipc_answer_0(iid, rc);
|
---|
| 83 | return NULL;
|
---|
| 84 | }
|
---|
| 85 | printf(NAME ": the %s driver is trying to register by the service.\n", drv_name);
|
---|
| 86 |
|
---|
| 87 | // Find driver structure
|
---|
| 88 | driver = find_driver(&drivers_list, drv_name);
|
---|
[92413de] | 89 |
|
---|
| 90 | free(drv_name);
|
---|
| 91 | drv_name = NULL;
|
---|
| 92 |
|
---|
[729fa2d6] | 93 | if (NULL == driver) {
|
---|
| 94 | printf(NAME ": no driver named %s was found.\n", drv_name);
|
---|
| 95 | ipc_answer_0(iid, ENOENT);
|
---|
| 96 | return NULL;
|
---|
| 97 | }
|
---|
| 98 | printf(NAME ": registering the running instance of the %s driver.\n", driver->name);
|
---|
| 99 |
|
---|
| 100 | // Create connection to the driver
|
---|
| 101 | printf(NAME ": creating connection to the %s driver.\n", driver->name);
|
---|
| 102 | ipc_call_t call;
|
---|
| 103 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 104 | if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
|
---|
| 105 | ipc_answer_0(callid, ENOTSUP);
|
---|
| 106 | ipc_answer_0(iid, ENOTSUP);
|
---|
| 107 | return NULL;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[c16cf62] | 110 | // remember driver's phone
|
---|
| 111 | set_driver_phone(driver, IPC_GET_ARG5(call));
|
---|
[729fa2d6] | 112 |
|
---|
| 113 | printf(NAME ": the %s driver was successfully registered as running.\n", driver->name);
|
---|
| 114 |
|
---|
| 115 | ipc_answer_0(callid, EOK);
|
---|
| 116 |
|
---|
| 117 | ipc_answer_0(iid, EOK);
|
---|
| 118 |
|
---|
| 119 | return driver;
|
---|
| 120 | }
|
---|
[e2b9a993] | 121 |
|
---|
| 122 | /** Function for handling connections to device manager.
|
---|
| 123 | *
|
---|
| 124 | */
|
---|
[924c75e1] | 125 | static void devman_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
|
---|
[729fa2d6] | 126 | {
|
---|
[e2b9a993] | 127 | /* Accept the connection */
|
---|
| 128 | ipc_answer_0(iid, EOK);
|
---|
| 129 |
|
---|
[729fa2d6] | 130 | driver_t *driver = devman_driver_register();
|
---|
| 131 | if (driver == NULL)
|
---|
| 132 | return;
|
---|
| 133 |
|
---|
[c16cf62] | 134 | initialize_running_driver(driver);
|
---|
[729fa2d6] | 135 |
|
---|
| 136 | ipc_callid_t callid;
|
---|
| 137 | ipc_call_t call;
|
---|
| 138 | bool cont = true;
|
---|
| 139 | while (cont) {
|
---|
[e2b9a993] | 140 | callid = async_get_call(&call);
|
---|
| 141 |
|
---|
| 142 | switch (IPC_GET_METHOD(call)) {
|
---|
[729fa2d6] | 143 | case IPC_M_PHONE_HUNGUP:
|
---|
| 144 | cont = false;
|
---|
| 145 | continue;
|
---|
[e2b9a993] | 146 | case DEVMAN_ADD_CHILD_DEVICE:
|
---|
| 147 | // TODO add new device node to the device tree
|
---|
| 148 | break;
|
---|
| 149 | default:
|
---|
| 150 | ipc_answer_0(callid, EINVAL);
|
---|
| 151 | break;
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[924c75e1] | 156 | /** Function for handling connections to device manager.
|
---|
| 157 | *
|
---|
| 158 | */
|
---|
| 159 | static void devman_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 160 | {
|
---|
| 161 | // Select interface
|
---|
| 162 | switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
|
---|
| 163 | case DEVMAN_DRIVER:
|
---|
| 164 | devman_connection_driver(iid, icall);
|
---|
| 165 | break;
|
---|
| 166 | /*case DEVMAN_CLIENT:
|
---|
| 167 | devmap_connection_client(iid, icall);
|
---|
| 168 | break;
|
---|
| 169 | case DEVMAN_CONNECT_TO_DEVICE:
|
---|
| 170 | // Connect client to selected device
|
---|
| 171 | devmap_forward(iid, icall);
|
---|
| 172 | break;*/
|
---|
| 173 | default:
|
---|
| 174 | /* No such interface */
|
---|
| 175 | ipc_answer_0(iid, ENOENT);
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[e2b9a993] | 179 | /** Initialize device manager internal structures.
|
---|
| 180 | */
|
---|
| 181 | static bool devman_init()
|
---|
| 182 | {
|
---|
[0c3666d] | 183 | printf(NAME ": devman_init - looking for available drivers. \n");
|
---|
[08d9c4e6] | 184 |
|
---|
[e2b9a993] | 185 | // initialize list of available drivers
|
---|
[0c3666d] | 186 | init_driver_list(&drivers_list);
|
---|
[e2b9a993] | 187 | if (0 == lookup_available_drivers(&drivers_list, DRIVER_DEFAULT_STORE)) {
|
---|
| 188 | printf(NAME " no drivers found.");
|
---|
| 189 | return false;
|
---|
| 190 | }
|
---|
[08d9c4e6] | 191 | printf(NAME ": devman_init - list of drivers has been initialized. \n");
|
---|
[e2b9a993] | 192 |
|
---|
| 193 | // create root device node
|
---|
| 194 | if (!init_device_tree(&device_tree, &drivers_list)) {
|
---|
| 195 | printf(NAME " failed to initialize device tree.");
|
---|
| 196 | return false;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | return true;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | int main(int argc, char *argv[])
|
---|
| 203 | {
|
---|
| 204 | printf(NAME ": HelenOS Device Manager\n");
|
---|
| 205 |
|
---|
| 206 | if (!devman_init()) {
|
---|
| 207 | printf(NAME ": Error while initializing service\n");
|
---|
| 208 | return -1;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | // Set a handler of incomming connections
|
---|
| 212 | async_set_client_connection(devman_connection);
|
---|
| 213 |
|
---|
| 214 | // Register device manager at naming service
|
---|
| 215 | ipcarg_t phonead;
|
---|
| 216 | if (ipc_connect_to_me(PHONE_NS, SERVICE_DEVMAN, 0, 0, &phonead) != 0)
|
---|
| 217 | return -1;
|
---|
| 218 |
|
---|
| 219 | printf(NAME ": Accepting connections\n");
|
---|
| 220 | async_manager();
|
---|
| 221 |
|
---|
| 222 | // Never reached
|
---|
| 223 | return 0;
|
---|
| 224 | }
|
---|
[c16cf62] | 225 |
|
---|
| 226 | /** @}
|
---|
| 227 | */ |
---|