| [181c32f] | 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 | /**
|
|---|
| [4122410] | 30 | * @addtogroup devman
|
|---|
| [181c32f] | 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 |
|
|---|
| 34 | /** @file
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <assert.h>
|
|---|
| 38 | #include <ipc/services.h>
|
|---|
| 39 | #include <ns.h>
|
|---|
| 40 | #include <async.h>
|
|---|
| 41 | #include <stdio.h>
|
|---|
| 42 | #include <errno.h>
|
|---|
| 43 | #include <str_error.h>
|
|---|
| 44 | #include <stdbool.h>
|
|---|
| 45 | #include <fibril_synch.h>
|
|---|
| 46 | #include <stdlib.h>
|
|---|
| 47 | #include <str.h>
|
|---|
| 48 | #include <io/log.h>
|
|---|
| 49 | #include <ipc/devman.h>
|
|---|
| 50 | #include <loc.h>
|
|---|
| 51 |
|
|---|
| 52 | #include "client_conn.h"
|
|---|
| 53 | #include "dev.h"
|
|---|
| 54 | #include "devman.h"
|
|---|
| 55 | #include "devtree.h"
|
|---|
| 56 | #include "driver.h"
|
|---|
| 57 | #include "drv_conn.h"
|
|---|
| 58 | #include "fun.h"
|
|---|
| 59 | #include "loc.h"
|
|---|
| 60 | #include "main.h"
|
|---|
| 61 |
|
|---|
| [b7fd2a0] | 62 | static errno_t init_running_drv(void *drv);
|
|---|
| [181c32f] | 63 |
|
|---|
| 64 | /** Register running driver. */
|
|---|
| [984a9ba] | 65 | static driver_t *devman_driver_register(ipc_call_t *call)
|
|---|
| [181c32f] | 66 | {
|
|---|
| 67 | driver_t *driver = NULL;
|
|---|
| 68 | char *drv_name = NULL;
|
|---|
| 69 |
|
|---|
| 70 | log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_driver_register");
|
|---|
| [a35b458] | 71 |
|
|---|
| [181c32f] | 72 | /* Get driver name. */
|
|---|
| [b7fd2a0] | 73 | errno_t rc = async_data_write_accept((void **) &drv_name, true, 0, 0, 0, 0);
|
|---|
| [181c32f] | 74 | if (rc != EOK) {
|
|---|
| [984a9ba] | 75 | async_answer_0(call, rc);
|
|---|
| [181c32f] | 76 | return NULL;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | log_msg(LOG_DEFAULT, LVL_DEBUG, "The `%s' driver is trying to register.",
|
|---|
| 80 | drv_name);
|
|---|
| [a35b458] | 81 |
|
|---|
| [181c32f] | 82 | /* Find driver structure. */
|
|---|
| [0511549] | 83 | driver = driver_find_by_name(&drivers_list, drv_name);
|
|---|
| [181c32f] | 84 | if (driver == NULL) {
|
|---|
| 85 | log_msg(LOG_DEFAULT, LVL_ERROR, "No driver named `%s' was found.", drv_name);
|
|---|
| 86 | free(drv_name);
|
|---|
| 87 | drv_name = NULL;
|
|---|
| [984a9ba] | 88 | async_answer_0(call, ENOENT);
|
|---|
| [181c32f] | 89 | return NULL;
|
|---|
| 90 | }
|
|---|
| [a35b458] | 91 |
|
|---|
| [181c32f] | 92 | free(drv_name);
|
|---|
| 93 | drv_name = NULL;
|
|---|
| [a35b458] | 94 |
|
|---|
| [181c32f] | 95 | fibril_mutex_lock(&driver->driver_mutex);
|
|---|
| [a35b458] | 96 |
|
|---|
| [181c32f] | 97 | if (driver->sess) {
|
|---|
| 98 | /* We already have a connection to the driver. */
|
|---|
| 99 | log_msg(LOG_DEFAULT, LVL_ERROR, "Driver '%s' already started.\n",
|
|---|
| 100 | driver->name);
|
|---|
| 101 | fibril_mutex_unlock(&driver->driver_mutex);
|
|---|
| [984a9ba] | 102 | async_answer_0(call, EEXIST);
|
|---|
| [181c32f] | 103 | return NULL;
|
|---|
| 104 | }
|
|---|
| [a35b458] | 105 |
|
|---|
| [181c32f] | 106 | switch (driver->state) {
|
|---|
| 107 | case DRIVER_NOT_STARTED:
|
|---|
| 108 | /* Somebody started the driver manually. */
|
|---|
| 109 | log_msg(LOG_DEFAULT, LVL_NOTE, "Driver '%s' started manually.\n",
|
|---|
| 110 | driver->name);
|
|---|
| 111 | driver->state = DRIVER_STARTING;
|
|---|
| 112 | break;
|
|---|
| 113 | case DRIVER_STARTING:
|
|---|
| 114 | /* The expected case */
|
|---|
| 115 | break;
|
|---|
| 116 | case DRIVER_RUNNING:
|
|---|
| 117 | /* Should not happen since we do not have a connected session */
|
|---|
| 118 | assert(false);
|
|---|
| 119 | }
|
|---|
| [a35b458] | 120 |
|
|---|
| [181c32f] | 121 | /* Create connection to the driver. */
|
|---|
| 122 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Creating connection to the `%s' driver.",
|
|---|
| 123 | driver->name);
|
|---|
| 124 | driver->sess = async_callback_receive(EXCHANGE_PARALLEL);
|
|---|
| 125 | if (!driver->sess) {
|
|---|
| 126 | fibril_mutex_unlock(&driver->driver_mutex);
|
|---|
| [984a9ba] | 127 | async_answer_0(call, ENOTSUP);
|
|---|
| [181c32f] | 128 | return NULL;
|
|---|
| 129 | }
|
|---|
| 130 | /* FIXME: Work around problem with callback sessions */
|
|---|
| [f9b2cb4c] | 131 | async_sess_args_set(driver->sess, INTERFACE_DDF_DEVMAN, 0, 0);
|
|---|
| [a35b458] | 132 |
|
|---|
| [181c32f] | 133 | log_msg(LOG_DEFAULT, LVL_NOTE,
|
|---|
| 134 | "The `%s' driver was successfully registered as running.",
|
|---|
| 135 | driver->name);
|
|---|
| [a35b458] | 136 |
|
|---|
| [181c32f] | 137 | /*
|
|---|
| 138 | * Initialize the driver as running (e.g. pass assigned devices to it)
|
|---|
| 139 | * in a separate fibril; the separate fibril is used to enable the
|
|---|
| 140 | * driver to use devman service during the driver's initialization.
|
|---|
| 141 | */
|
|---|
| 142 | fid_t fid = fibril_create(init_running_drv, driver);
|
|---|
| 143 | if (fid == 0) {
|
|---|
| [5ef16903] | 144 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to create initialization fibril "
|
|---|
| [181c32f] | 145 | "for driver `%s'.", driver->name);
|
|---|
| 146 | fibril_mutex_unlock(&driver->driver_mutex);
|
|---|
| [984a9ba] | 147 | async_answer_0(call, ENOMEM);
|
|---|
| [181c32f] | 148 | return NULL;
|
|---|
| 149 | }
|
|---|
| [a35b458] | 150 |
|
|---|
| [181c32f] | 151 | fibril_add_ready(fid);
|
|---|
| 152 | fibril_mutex_unlock(&driver->driver_mutex);
|
|---|
| [a35b458] | 153 |
|
|---|
| [984a9ba] | 154 | async_answer_0(call, EOK);
|
|---|
| [181c32f] | 155 | return driver;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | /** Receive device match ID from the device's parent driver and add it to the
|
|---|
| 159 | * list of devices match ids.
|
|---|
| 160 | *
|
|---|
| 161 | * @param match_ids The list of the device's match ids.
|
|---|
| [cde999a] | 162 | * @return Zero on success, error code otherwise.
|
|---|
| [181c32f] | 163 | */
|
|---|
| [b7fd2a0] | 164 | static errno_t devman_receive_match_id(match_id_list_t *match_ids)
|
|---|
| [181c32f] | 165 | {
|
|---|
| 166 | match_id_t *match_id = create_match_id();
|
|---|
| 167 | ipc_call_t call;
|
|---|
| [b7fd2a0] | 168 | errno_t rc = 0;
|
|---|
| [a35b458] | 169 |
|
|---|
| [984a9ba] | 170 | async_get_call(&call);
|
|---|
| [181c32f] | 171 | if (DEVMAN_ADD_MATCH_ID != IPC_GET_IMETHOD(call)) {
|
|---|
| [acb8766e] | 172 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
|---|
| [181c32f] | 173 | "Invalid protocol when trying to receive match id.");
|
|---|
| [984a9ba] | 174 | async_answer_0(&call, EINVAL);
|
|---|
| [181c32f] | 175 | delete_match_id(match_id);
|
|---|
| 176 | return EINVAL;
|
|---|
| 177 | }
|
|---|
| [a35b458] | 178 |
|
|---|
| [181c32f] | 179 | if (match_id == NULL) {
|
|---|
| 180 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to allocate match id.");
|
|---|
| [984a9ba] | 181 | async_answer_0(&call, ENOMEM);
|
|---|
| [181c32f] | 182 | return ENOMEM;
|
|---|
| 183 | }
|
|---|
| [a35b458] | 184 |
|
|---|
| [984a9ba] | 185 | async_answer_0(&call, EOK);
|
|---|
| [a35b458] | 186 |
|
|---|
| [181c32f] | 187 | match_id->score = IPC_GET_ARG1(call);
|
|---|
| [a35b458] | 188 |
|
|---|
| [181c32f] | 189 | char *match_id_str;
|
|---|
| 190 | rc = async_data_write_accept((void **) &match_id_str, true, 0, 0, 0, 0);
|
|---|
| 191 | match_id->id = match_id_str;
|
|---|
| 192 | if (rc != EOK) {
|
|---|
| 193 | delete_match_id(match_id);
|
|---|
| 194 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to receive match id string: %s.",
|
|---|
| 195 | str_error(rc));
|
|---|
| 196 | return rc;
|
|---|
| 197 | }
|
|---|
| [a35b458] | 198 |
|
|---|
| [181c32f] | 199 | list_append(&match_id->link, &match_ids->ids);
|
|---|
| [a35b458] | 200 |
|
|---|
| [181c32f] | 201 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Received match id `%s', score %d.",
|
|---|
| 202 | match_id->id, match_id->score);
|
|---|
| 203 | return rc;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | /** Receive device match IDs from the device's parent driver and add them to the
|
|---|
| 207 | * list of devices match ids.
|
|---|
| 208 | *
|
|---|
| 209 | * @param match_count The number of device's match ids to be received.
|
|---|
| 210 | * @param match_ids The list of the device's match ids.
|
|---|
| [cde999a] | 211 | * @return Zero on success, error code otherwise.
|
|---|
| [181c32f] | 212 | */
|
|---|
| [b7fd2a0] | 213 | static errno_t devman_receive_match_ids(sysarg_t match_count,
|
|---|
| [181c32f] | 214 | match_id_list_t *match_ids)
|
|---|
| 215 | {
|
|---|
| [b7fd2a0] | 216 | errno_t ret = EOK;
|
|---|
| [181c32f] | 217 | size_t i;
|
|---|
| [a35b458] | 218 |
|
|---|
| [181c32f] | 219 | for (i = 0; i < match_count; i++) {
|
|---|
| 220 | if (EOK != (ret = devman_receive_match_id(match_ids)))
|
|---|
| 221 | return ret;
|
|---|
| 222 | }
|
|---|
| 223 | return ret;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | /** Handle function registration.
|
|---|
| 227 | *
|
|---|
| 228 | * Child devices are registered by their parent's device driver.
|
|---|
| 229 | */
|
|---|
| [984a9ba] | 230 | static void devman_add_function(ipc_call_t *call)
|
|---|
| [181c32f] | 231 | {
|
|---|
| 232 | fun_type_t ftype = (fun_type_t) IPC_GET_ARG1(*call);
|
|---|
| 233 | devman_handle_t dev_handle = IPC_GET_ARG2(*call);
|
|---|
| 234 | sysarg_t match_count = IPC_GET_ARG3(*call);
|
|---|
| 235 | dev_tree_t *tree = &device_tree;
|
|---|
| [a35b458] | 236 |
|
|---|
| [181c32f] | 237 | dev_node_t *pdev = find_dev_node(&device_tree, dev_handle);
|
|---|
| 238 | if (pdev == NULL) {
|
|---|
| [984a9ba] | 239 | async_answer_0(call, ENOENT);
|
|---|
| [181c32f] | 240 | return;
|
|---|
| 241 | }
|
|---|
| [a35b458] | 242 |
|
|---|
| [181c32f] | 243 | if (ftype != fun_inner && ftype != fun_exposed) {
|
|---|
| 244 | /* Unknown function type */
|
|---|
| [acb8766e] | 245 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
|---|
| [181c32f] | 246 | "Unknown function type %d provided by driver.",
|
|---|
| 247 | (int) ftype);
|
|---|
| 248 |
|
|---|
| 249 | dev_del_ref(pdev);
|
|---|
| [984a9ba] | 250 | async_answer_0(call, EINVAL);
|
|---|
| [181c32f] | 251 | return;
|
|---|
| 252 | }
|
|---|
| [a35b458] | 253 |
|
|---|
| [181c32f] | 254 | char *fun_name = NULL;
|
|---|
| [984a9ba] | 255 | errno_t rc = async_data_write_accept((void **) &fun_name, true, 0, 0, 0, 0);
|
|---|
| [181c32f] | 256 | if (rc != EOK) {
|
|---|
| 257 | dev_del_ref(pdev);
|
|---|
| [984a9ba] | 258 | async_answer_0(call, rc);
|
|---|
| [181c32f] | 259 | return;
|
|---|
| 260 | }
|
|---|
| [a35b458] | 261 |
|
|---|
| [181c32f] | 262 | fibril_rwlock_write_lock(&tree->rwlock);
|
|---|
| [a35b458] | 263 |
|
|---|
| [181c32f] | 264 | /* Check device state */
|
|---|
| 265 | if (pdev->state == DEVICE_REMOVED) {
|
|---|
| 266 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 267 | dev_del_ref(pdev);
|
|---|
| [984a9ba] | 268 | async_answer_0(call, ENOENT);
|
|---|
| [181c32f] | 269 | return;
|
|---|
| 270 | }
|
|---|
| [a35b458] | 271 |
|
|---|
| [181c32f] | 272 | /* Check that function with same name is not there already. */
|
|---|
| 273 | fun_node_t *tfun = find_fun_node_in_device(tree, pdev, fun_name);
|
|---|
| 274 | if (tfun) {
|
|---|
| 275 | fun_del_ref(tfun); /* drop the new unwanted reference */
|
|---|
| 276 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 277 | dev_del_ref(pdev);
|
|---|
| [984a9ba] | 278 | async_answer_0(call, EEXIST);
|
|---|
| [181c32f] | 279 | printf(NAME ": Warning, driver tried to register `%s' twice.\n",
|
|---|
| 280 | fun_name);
|
|---|
| 281 | free(fun_name);
|
|---|
| 282 | return;
|
|---|
| 283 | }
|
|---|
| [a35b458] | 284 |
|
|---|
| [181c32f] | 285 | fun_node_t *fun = create_fun_node();
|
|---|
| 286 | /* One reference for creation, one for us */
|
|---|
| 287 | fun_add_ref(fun);
|
|---|
| 288 | fun_add_ref(fun);
|
|---|
| 289 | fun->ftype = ftype;
|
|---|
| [a35b458] | 290 |
|
|---|
| [181c32f] | 291 | /*
|
|---|
| 292 | * We can lock the function here even when holding the tree because
|
|---|
| 293 | * we know it cannot be held by anyone else yet.
|
|---|
| 294 | */
|
|---|
| 295 | fun_busy_lock(fun);
|
|---|
| [a35b458] | 296 |
|
|---|
| [181c32f] | 297 | if (!insert_fun_node(&device_tree, fun, fun_name, pdev)) {
|
|---|
| 298 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 299 | dev_del_ref(pdev);
|
|---|
| 300 | fun_busy_unlock(fun);
|
|---|
| 301 | fun_del_ref(fun);
|
|---|
| 302 | delete_fun_node(fun);
|
|---|
| [984a9ba] | 303 | async_answer_0(call, ENOMEM);
|
|---|
| [181c32f] | 304 | return;
|
|---|
| 305 | }
|
|---|
| [a35b458] | 306 |
|
|---|
| [181c32f] | 307 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 308 | dev_del_ref(pdev);
|
|---|
| [a35b458] | 309 |
|
|---|
| [181c32f] | 310 | devman_receive_match_ids(match_count, &fun->match_ids);
|
|---|
| [a35b458] | 311 |
|
|---|
| [02e5e34] | 312 | rc = fun_online(fun);
|
|---|
| [181c32f] | 313 | if (rc != EOK) {
|
|---|
| 314 | /* XXX Set some failed state? */
|
|---|
| 315 | fun_busy_unlock(fun);
|
|---|
| 316 | fun_del_ref(fun);
|
|---|
| [984a9ba] | 317 | async_answer_0(call, rc);
|
|---|
| [181c32f] | 318 | return;
|
|---|
| 319 | }
|
|---|
| [a35b458] | 320 |
|
|---|
| [181c32f] | 321 | fun_busy_unlock(fun);
|
|---|
| 322 | fun_del_ref(fun);
|
|---|
| [a35b458] | 323 |
|
|---|
| [181c32f] | 324 | /* Return device handle to parent's driver. */
|
|---|
| [984a9ba] | 325 | async_answer_1(call, EOK, fun->handle);
|
|---|
| [181c32f] | 326 | }
|
|---|
| 327 |
|
|---|
| [984a9ba] | 328 | static void devman_add_function_to_cat(ipc_call_t *call)
|
|---|
| [181c32f] | 329 | {
|
|---|
| 330 | devman_handle_t handle = IPC_GET_ARG1(*call);
|
|---|
| 331 | category_id_t cat_id;
|
|---|
| [b7fd2a0] | 332 | errno_t rc;
|
|---|
| [a35b458] | 333 |
|
|---|
| [181c32f] | 334 | /* Get category name. */
|
|---|
| 335 | char *cat_name;
|
|---|
| 336 | rc = async_data_write_accept((void **) &cat_name, true,
|
|---|
| 337 | 0, 0, 0, 0);
|
|---|
| 338 | if (rc != EOK) {
|
|---|
| [984a9ba] | 339 | async_answer_0(call, rc);
|
|---|
| [181c32f] | 340 | return;
|
|---|
| 341 | }
|
|---|
| [a35b458] | 342 |
|
|---|
| [181c32f] | 343 | fun_node_t *fun = find_fun_node(&device_tree, handle);
|
|---|
| 344 | if (fun == NULL) {
|
|---|
| [984a9ba] | 345 | async_answer_0(call, ENOENT);
|
|---|
| [181c32f] | 346 | return;
|
|---|
| 347 | }
|
|---|
| [a35b458] | 348 |
|
|---|
| [181c32f] | 349 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
|---|
| [a35b458] | 350 |
|
|---|
| [181c32f] | 351 | /* Check function state */
|
|---|
| 352 | if (fun->state == FUN_REMOVED) {
|
|---|
| 353 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| [984a9ba] | 354 | async_answer_0(call, ENOENT);
|
|---|
| [181c32f] | 355 | return;
|
|---|
| 356 | }
|
|---|
| [a35b458] | 357 |
|
|---|
| [181c32f] | 358 | rc = loc_category_get_id(cat_name, &cat_id, IPC_FLAG_BLOCKING);
|
|---|
| 359 | if (rc == EOK) {
|
|---|
| 360 | loc_service_add_to_cat(fun->service_id, cat_id);
|
|---|
| 361 | log_msg(LOG_DEFAULT, LVL_NOTE, "Function `%s' added to category `%s'.",
|
|---|
| 362 | fun->pathname, cat_name);
|
|---|
| 363 | } else {
|
|---|
| 364 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding function `%s' to category "
|
|---|
| 365 | "`%s'.", fun->pathname, cat_name);
|
|---|
| 366 | }
|
|---|
| [a35b458] | 367 |
|
|---|
| [181c32f] | 368 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 369 | fun_del_ref(fun);
|
|---|
| [a35b458] | 370 |
|
|---|
| [984a9ba] | 371 | async_answer_0(call, rc);
|
|---|
| [181c32f] | 372 | }
|
|---|
| 373 |
|
|---|
| 374 | /** Online function by driver request.
|
|---|
| 375 | *
|
|---|
| 376 | */
|
|---|
| [984a9ba] | 377 | static void devman_drv_fun_online(ipc_call_t *icall, driver_t *drv)
|
|---|
| [181c32f] | 378 | {
|
|---|
| 379 | fun_node_t *fun;
|
|---|
| [b7fd2a0] | 380 | errno_t rc;
|
|---|
| [a35b458] | 381 |
|
|---|
| [181c32f] | 382 | log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_drv_fun_online()");
|
|---|
| [a35b458] | 383 |
|
|---|
| [181c32f] | 384 | fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));
|
|---|
| 385 | if (fun == NULL) {
|
|---|
| [984a9ba] | 386 | async_answer_0(icall, ENOENT);
|
|---|
| [181c32f] | 387 | return;
|
|---|
| 388 | }
|
|---|
| [a35b458] | 389 |
|
|---|
| [181c32f] | 390 | fun_busy_lock(fun);
|
|---|
| [a35b458] | 391 |
|
|---|
| [181c32f] | 392 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
|---|
| 393 | if (fun->dev == NULL || fun->dev->drv != drv) {
|
|---|
| 394 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 395 | fun_busy_unlock(fun);
|
|---|
| 396 | fun_del_ref(fun);
|
|---|
| [984a9ba] | 397 | async_answer_0(icall, ENOENT);
|
|---|
| [181c32f] | 398 | return;
|
|---|
| 399 | }
|
|---|
| 400 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| [a35b458] | 401 |
|
|---|
| [acb8766e] | 402 | rc = fun_online(fun);
|
|---|
| [181c32f] | 403 | if (rc != EOK) {
|
|---|
| 404 | fun_busy_unlock(fun);
|
|---|
| 405 | fun_del_ref(fun);
|
|---|
| [984a9ba] | 406 | async_answer_0(icall, rc);
|
|---|
| [181c32f] | 407 | return;
|
|---|
| 408 | }
|
|---|
| [a35b458] | 409 |
|
|---|
| [181c32f] | 410 | fun_busy_unlock(fun);
|
|---|
| 411 | fun_del_ref(fun);
|
|---|
| [a35b458] | 412 |
|
|---|
| [984a9ba] | 413 | async_answer_0(icall, EOK);
|
|---|
| [181c32f] | 414 | }
|
|---|
| 415 |
|
|---|
| 416 | /** Offline function by driver request.
|
|---|
| 417 | *
|
|---|
| 418 | */
|
|---|
| [984a9ba] | 419 | static void devman_drv_fun_offline(ipc_call_t *icall, driver_t *drv)
|
|---|
| [181c32f] | 420 | {
|
|---|
| 421 | fun_node_t *fun;
|
|---|
| [b7fd2a0] | 422 | errno_t rc;
|
|---|
| [181c32f] | 423 |
|
|---|
| 424 | fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));
|
|---|
| 425 | if (fun == NULL) {
|
|---|
| [984a9ba] | 426 | async_answer_0(icall, ENOENT);
|
|---|
| [181c32f] | 427 | return;
|
|---|
| 428 | }
|
|---|
| [a35b458] | 429 |
|
|---|
| [181c32f] | 430 | fun_busy_lock(fun);
|
|---|
| [a35b458] | 431 |
|
|---|
| [181c32f] | 432 | fibril_rwlock_write_lock(&device_tree.rwlock);
|
|---|
| 433 | if (fun->dev == NULL || fun->dev->drv != drv) {
|
|---|
| 434 | fun_busy_unlock(fun);
|
|---|
| 435 | fun_del_ref(fun);
|
|---|
| [984a9ba] | 436 | async_answer_0(icall, ENOENT);
|
|---|
| [181c32f] | 437 | return;
|
|---|
| 438 | }
|
|---|
| 439 | fibril_rwlock_write_unlock(&device_tree.rwlock);
|
|---|
| [a35b458] | 440 |
|
|---|
| [02e5e34] | 441 | rc = fun_offline(fun);
|
|---|
| [181c32f] | 442 | if (rc != EOK) {
|
|---|
| 443 | fun_busy_unlock(fun);
|
|---|
| 444 | fun_del_ref(fun);
|
|---|
| [984a9ba] | 445 | async_answer_0(icall, rc);
|
|---|
| [181c32f] | 446 | return;
|
|---|
| 447 | }
|
|---|
| [a35b458] | 448 |
|
|---|
| [181c32f] | 449 | fun_busy_unlock(fun);
|
|---|
| 450 | fun_del_ref(fun);
|
|---|
| [984a9ba] | 451 | async_answer_0(icall, EOK);
|
|---|
| [181c32f] | 452 | }
|
|---|
| 453 |
|
|---|
| 454 | /** Remove function. */
|
|---|
| [984a9ba] | 455 | static void devman_remove_function(ipc_call_t *call)
|
|---|
| [181c32f] | 456 | {
|
|---|
| 457 | devman_handle_t fun_handle = IPC_GET_ARG1(*call);
|
|---|
| 458 | dev_tree_t *tree = &device_tree;
|
|---|
| [b7fd2a0] | 459 | errno_t rc;
|
|---|
| [a35b458] | 460 |
|
|---|
| [181c32f] | 461 | fun_node_t *fun = find_fun_node(&device_tree, fun_handle);
|
|---|
| 462 | if (fun == NULL) {
|
|---|
| [984a9ba] | 463 | async_answer_0(call, ENOENT);
|
|---|
| [181c32f] | 464 | return;
|
|---|
| 465 | }
|
|---|
| [a35b458] | 466 |
|
|---|
| [181c32f] | 467 | fun_busy_lock(fun);
|
|---|
| [a35b458] | 468 |
|
|---|
| [181c32f] | 469 | fibril_rwlock_write_lock(&tree->rwlock);
|
|---|
| [a35b458] | 470 |
|
|---|
| [181c32f] | 471 | log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_remove_function(fun='%s')", fun->pathname);
|
|---|
| [a35b458] | 472 |
|
|---|
| [181c32f] | 473 | /* Check function state */
|
|---|
| 474 | if (fun->state == FUN_REMOVED) {
|
|---|
| 475 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 476 | fun_busy_unlock(fun);
|
|---|
| 477 | fun_del_ref(fun);
|
|---|
| [984a9ba] | 478 | async_answer_0(call, ENOENT);
|
|---|
| [181c32f] | 479 | return;
|
|---|
| 480 | }
|
|---|
| [a35b458] | 481 |
|
|---|
| [181c32f] | 482 | if (fun->ftype == fun_inner) {
|
|---|
| 483 | /* This is a surprise removal. Handle possible descendants */
|
|---|
| 484 | if (fun->child != NULL) {
|
|---|
| 485 | dev_node_t *dev = fun->child;
|
|---|
| 486 | device_state_t dev_state;
|
|---|
| [b7fd2a0] | 487 | errno_t gone_rc;
|
|---|
| [a35b458] | 488 |
|
|---|
| [181c32f] | 489 | dev_add_ref(dev);
|
|---|
| 490 | dev_state = dev->state;
|
|---|
| [a35b458] | 491 |
|
|---|
| [181c32f] | 492 | fibril_rwlock_write_unlock(&device_tree.rwlock);
|
|---|
| [a35b458] | 493 |
|
|---|
| [181c32f] | 494 | /* If device is owned by driver, inform driver it is gone. */
|
|---|
| 495 | if (dev_state == DEVICE_USABLE)
|
|---|
| 496 | gone_rc = driver_dev_gone(&device_tree, dev);
|
|---|
| 497 | else
|
|---|
| 498 | gone_rc = EOK;
|
|---|
| [a35b458] | 499 |
|
|---|
| [181c32f] | 500 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
|---|
| [a35b458] | 501 |
|
|---|
| [181c32f] | 502 | /* Verify that driver succeeded and removed all functions */
|
|---|
| 503 | if (gone_rc != EOK || !list_empty(&dev->functions)) {
|
|---|
| 504 | log_msg(LOG_DEFAULT, LVL_ERROR, "Driver did not remove "
|
|---|
| 505 | "functions for device that is gone. "
|
|---|
| 506 | "Device node is now defunct.");
|
|---|
| [a35b458] | 507 |
|
|---|
| [181c32f] | 508 | /*
|
|---|
| 509 | * Not much we can do but mark the device
|
|---|
| 510 | * node as having invalid state. This
|
|---|
| 511 | * is a driver bug.
|
|---|
| 512 | */
|
|---|
| 513 | dev->state = DEVICE_INVALID;
|
|---|
| 514 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| 515 | dev_del_ref(dev);
|
|---|
| 516 | if (gone_rc == EOK)
|
|---|
| 517 | gone_rc = ENOTSUP;
|
|---|
| 518 | fun_busy_unlock(fun);
|
|---|
| 519 | fun_del_ref(fun);
|
|---|
| [984a9ba] | 520 | async_answer_0(call, gone_rc);
|
|---|
| [181c32f] | 521 | return;
|
|---|
| 522 | }
|
|---|
| [a35b458] | 523 |
|
|---|
| [181c32f] | 524 | driver_t *driver = dev->drv;
|
|---|
| 525 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
|---|
| [a35b458] | 526 |
|
|---|
| [181c32f] | 527 | if (driver)
|
|---|
| 528 | detach_driver(&device_tree, dev);
|
|---|
| [a35b458] | 529 |
|
|---|
| [181c32f] | 530 | fibril_rwlock_write_lock(&device_tree.rwlock);
|
|---|
| 531 | remove_dev_node(&device_tree, dev);
|
|---|
| [a35b458] | 532 |
|
|---|
| [181c32f] | 533 | /* Delete ref created when node was inserted */
|
|---|
| 534 | dev_del_ref(dev);
|
|---|
| 535 | /* Delete ref created by dev_add_ref(dev) above */
|
|---|
| 536 | dev_del_ref(dev);
|
|---|
| 537 | }
|
|---|
| 538 | } else {
|
|---|
| 539 | if (fun->service_id != 0) {
|
|---|
| 540 | /* Unregister from location service */
|
|---|
| [96ef672] | 541 | rc = loc_unregister_tree_function(fun, &device_tree);
|
|---|
| [181c32f] | 542 | if (rc != EOK) {
|
|---|
| 543 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed unregistering tree "
|
|---|
| 544 | "service.");
|
|---|
| 545 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 546 | fun_busy_unlock(fun);
|
|---|
| 547 | fun_del_ref(fun);
|
|---|
| [984a9ba] | 548 | async_answer_0(call, EIO);
|
|---|
| [181c32f] | 549 | return;
|
|---|
| 550 | }
|
|---|
| 551 | }
|
|---|
| 552 | }
|
|---|
| [a35b458] | 553 |
|
|---|
| [181c32f] | 554 | remove_fun_node(&device_tree, fun);
|
|---|
| 555 | fibril_rwlock_write_unlock(&tree->rwlock);
|
|---|
| 556 | fun_busy_unlock(fun);
|
|---|
| [a35b458] | 557 |
|
|---|
| [181c32f] | 558 | /* Delete ref added when inserting function into tree */
|
|---|
| 559 | fun_del_ref(fun);
|
|---|
| 560 | /* Delete ref added above when looking up function */
|
|---|
| 561 | fun_del_ref(fun);
|
|---|
| [a35b458] | 562 |
|
|---|
| [181c32f] | 563 | log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_remove_function() succeeded.");
|
|---|
| [984a9ba] | 564 | async_answer_0(call, EOK);
|
|---|
| [181c32f] | 565 | }
|
|---|
| 566 |
|
|---|
| 567 | /** Initialize driver which has registered itself as running and ready.
|
|---|
| 568 | *
|
|---|
| 569 | * The initialization is done in a separate fibril to avoid deadlocks (if the
|
|---|
| 570 | * driver needed to be served by devman during the driver's initialization).
|
|---|
| 571 | */
|
|---|
| [b7fd2a0] | 572 | static errno_t init_running_drv(void *drv)
|
|---|
| [181c32f] | 573 | {
|
|---|
| 574 | driver_t *driver = (driver_t *) drv;
|
|---|
| [a35b458] | 575 |
|
|---|
| [181c32f] | 576 | initialize_running_driver(driver, &device_tree);
|
|---|
| 577 | log_msg(LOG_DEFAULT, LVL_DEBUG, "The `%s` driver was successfully initialized.",
|
|---|
| 578 | driver->name);
|
|---|
| 579 | return 0;
|
|---|
| 580 | }
|
|---|
| 581 |
|
|---|
| 582 | /** Function for handling connections from a driver to the device manager. */
|
|---|
| [984a9ba] | 583 | void devman_connection_driver(ipc_call_t *icall, void *arg)
|
|---|
| [181c32f] | 584 | {
|
|---|
| 585 | client_t *client;
|
|---|
| 586 | driver_t *driver = NULL;
|
|---|
| [a35b458] | 587 |
|
|---|
| [181c32f] | 588 | /* Accept the connection. */
|
|---|
| [984a9ba] | 589 | async_answer_0(icall, EOK);
|
|---|
| [a35b458] | 590 |
|
|---|
| [181c32f] | 591 | client = async_get_client_data();
|
|---|
| 592 | if (client == NULL) {
|
|---|
| 593 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to allocate client data.");
|
|---|
| 594 | return;
|
|---|
| 595 | }
|
|---|
| [a35b458] | 596 |
|
|---|
| [181c32f] | 597 | while (true) {
|
|---|
| 598 | ipc_call_t call;
|
|---|
| [984a9ba] | 599 | async_get_call(&call);
|
|---|
| [a35b458] | 600 |
|
|---|
| [181c32f] | 601 | if (!IPC_GET_IMETHOD(call))
|
|---|
| 602 | break;
|
|---|
| [a35b458] | 603 |
|
|---|
| [181c32f] | 604 | if (IPC_GET_IMETHOD(call) != DEVMAN_DRIVER_REGISTER) {
|
|---|
| 605 | fibril_mutex_lock(&client->mutex);
|
|---|
| 606 | driver = client->driver;
|
|---|
| 607 | fibril_mutex_unlock(&client->mutex);
|
|---|
| 608 | if (driver == NULL) {
|
|---|
| 609 | /* First call must be to DEVMAN_DRIVER_REGISTER */
|
|---|
| [984a9ba] | 610 | async_answer_0(&call, ENOTSUP);
|
|---|
| [181c32f] | 611 | continue;
|
|---|
| 612 | }
|
|---|
| 613 | }
|
|---|
| [a35b458] | 614 |
|
|---|
| [181c32f] | 615 | switch (IPC_GET_IMETHOD(call)) {
|
|---|
| 616 | case DEVMAN_DRIVER_REGISTER:
|
|---|
| 617 | fibril_mutex_lock(&client->mutex);
|
|---|
| 618 | if (client->driver != NULL) {
|
|---|
| 619 | fibril_mutex_unlock(&client->mutex);
|
|---|
| [984a9ba] | 620 | async_answer_0(&call, EINVAL);
|
|---|
| [181c32f] | 621 | continue;
|
|---|
| 622 | }
|
|---|
| [984a9ba] | 623 | client->driver = devman_driver_register(&call);
|
|---|
| [181c32f] | 624 | fibril_mutex_unlock(&client->mutex);
|
|---|
| 625 | break;
|
|---|
| 626 | case DEVMAN_ADD_FUNCTION:
|
|---|
| [984a9ba] | 627 | devman_add_function(&call);
|
|---|
| [181c32f] | 628 | break;
|
|---|
| 629 | case DEVMAN_ADD_DEVICE_TO_CATEGORY:
|
|---|
| [984a9ba] | 630 | devman_add_function_to_cat(&call);
|
|---|
| [181c32f] | 631 | break;
|
|---|
| 632 | case DEVMAN_DRV_FUN_ONLINE:
|
|---|
| [984a9ba] | 633 | devman_drv_fun_online(&call, driver);
|
|---|
| [181c32f] | 634 | break;
|
|---|
| 635 | case DEVMAN_DRV_FUN_OFFLINE:
|
|---|
| [984a9ba] | 636 | devman_drv_fun_offline(&call, driver);
|
|---|
| [181c32f] | 637 | break;
|
|---|
| 638 | case DEVMAN_REMOVE_FUNCTION:
|
|---|
| [984a9ba] | 639 | devman_remove_function(&call);
|
|---|
| [181c32f] | 640 | break;
|
|---|
| 641 | default:
|
|---|
| [984a9ba] | 642 | async_answer_0(&call, EINVAL);
|
|---|
| [181c32f] | 643 | break;
|
|---|
| 644 | }
|
|---|
| 645 | }
|
|---|
| 646 | }
|
|---|
| 647 |
|
|---|
| 648 | /** @}
|
|---|
| 649 | */
|
|---|