[d80d7a8] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Lenka Trochtova
|
---|
[1db5669] | 3 | * Copyright (c) 2013 Jiri Svoboda
|
---|
[d80d7a8] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * @defgroup devman Device manager.
|
---|
| 32 | * @brief HelenOS device manager.
|
---|
| 33 | * @{
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | /** @file
|
---|
| 37 | */
|
---|
| 38 |
|
---|
| 39 | #include <inttypes.h>
|
---|
| 40 | #include <assert.h>
|
---|
| 41 | #include <ns.h>
|
---|
| 42 | #include <async.h>
|
---|
| 43 | #include <stdio.h>
|
---|
| 44 | #include <errno.h>
|
---|
| 45 | #include <str_error.h>
|
---|
| 46 | #include <stdbool.h>
|
---|
| 47 | #include <fibril_synch.h>
|
---|
| 48 | #include <stdlib.h>
|
---|
| 49 | #include <str.h>
|
---|
| 50 | #include <ctype.h>
|
---|
| 51 | #include <ipc/devman.h>
|
---|
| 52 |
|
---|
| 53 | #include "client_conn.h"
|
---|
| 54 | #include "dev.h"
|
---|
| 55 | #include "devman.h"
|
---|
| 56 | #include "driver.h"
|
---|
| 57 | #include "fun.h"
|
---|
| 58 | #include "loc.h"
|
---|
| 59 | #include "main.h"
|
---|
| 60 |
|
---|
| 61 | /** Find handle for the device instance identified by the device's path in the
|
---|
[7c3fb9b] | 62 | * device tree.
|
---|
| 63 | */
|
---|
[984a9ba] | 64 | static void devman_function_get_handle(ipc_call_t *icall)
|
---|
[d80d7a8] | 65 | {
|
---|
| 66 | char *pathname;
|
---|
| 67 | devman_handle_t handle;
|
---|
[a35b458] | 68 |
|
---|
[b7fd2a0] | 69 | errno_t rc = async_data_write_accept((void **) &pathname, true, 0, 0, 0, 0);
|
---|
[d80d7a8] | 70 | if (rc != EOK) {
|
---|
[984a9ba] | 71 | async_answer_0(icall, rc);
|
---|
[d80d7a8] | 72 | return;
|
---|
| 73 | }
|
---|
[a35b458] | 74 |
|
---|
[d80d7a8] | 75 | fun_node_t *fun = find_fun_node_by_path(&device_tree, pathname);
|
---|
[a35b458] | 76 |
|
---|
[d80d7a8] | 77 | free(pathname);
|
---|
| 78 |
|
---|
| 79 | if (fun == NULL) {
|
---|
[984a9ba] | 80 | async_answer_0(icall, ENOENT);
|
---|
[d80d7a8] | 81 | return;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
| 85 |
|
---|
| 86 | /* Check function state */
|
---|
| 87 | if (fun->state == FUN_REMOVED) {
|
---|
| 88 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
[984a9ba] | 89 | async_answer_0(icall, ENOENT);
|
---|
[d80d7a8] | 90 | return;
|
---|
| 91 | }
|
---|
| 92 | handle = fun->handle;
|
---|
| 93 |
|
---|
| 94 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
| 95 |
|
---|
| 96 | /* Delete reference created above by find_fun_node_by_path() */
|
---|
| 97 | fun_del_ref(fun);
|
---|
| 98 |
|
---|
[984a9ba] | 99 | async_answer_1(icall, EOK, handle);
|
---|
[d80d7a8] | 100 | }
|
---|
| 101 |
|
---|
[4c9b28a] | 102 | /** Get device match ID. */
|
---|
[984a9ba] | 103 | static void devman_fun_get_match_id(ipc_call_t *icall)
|
---|
[4c9b28a] | 104 | {
|
---|
| 105 | devman_handle_t handle = IPC_GET_ARG1(*icall);
|
---|
| 106 | size_t index = IPC_GET_ARG2(*icall);
|
---|
| 107 | void *buffer = NULL;
|
---|
| 108 |
|
---|
| 109 | fun_node_t *fun = find_fun_node(&device_tree, handle);
|
---|
| 110 | if (fun == NULL) {
|
---|
[984a9ba] | 111 | async_answer_0(icall, ENOMEM);
|
---|
[4c9b28a] | 112 | return;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[984a9ba] | 115 | ipc_call_t data;
|
---|
[4c9b28a] | 116 | size_t data_len;
|
---|
[984a9ba] | 117 | if (!async_data_read_receive(&data, &data_len)) {
|
---|
| 118 | async_answer_0(icall, EINVAL);
|
---|
[4c9b28a] | 119 | fun_del_ref(fun);
|
---|
| 120 | return;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | buffer = malloc(data_len);
|
---|
| 124 | if (buffer == NULL) {
|
---|
[984a9ba] | 125 | async_answer_0(&data, ENOMEM);
|
---|
| 126 | async_answer_0(icall, ENOMEM);
|
---|
[4c9b28a] | 127 | fun_del_ref(fun);
|
---|
| 128 | return;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
| 132 |
|
---|
| 133 | /* Check function state */
|
---|
| 134 | if (fun->state == FUN_REMOVED)
|
---|
| 135 | goto error;
|
---|
| 136 |
|
---|
| 137 | link_t *link = list_nth(&fun->match_ids.ids, index);
|
---|
| 138 | if (link == NULL)
|
---|
| 139 | goto error;
|
---|
| 140 |
|
---|
| 141 | match_id_t *mid = list_get_instance(link, match_id_t, link);
|
---|
| 142 |
|
---|
| 143 | size_t sent_length = str_size(mid->id);
|
---|
| 144 | if (sent_length > data_len) {
|
---|
| 145 | sent_length = data_len;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[984a9ba] | 148 | async_data_read_finalize(&data, mid->id, sent_length);
|
---|
| 149 | async_answer_1(icall, EOK, mid->score);
|
---|
[4c9b28a] | 150 |
|
---|
| 151 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
| 152 | fun_del_ref(fun);
|
---|
| 153 | free(buffer);
|
---|
| 154 |
|
---|
| 155 | return;
|
---|
| 156 | error:
|
---|
| 157 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
| 158 | free(buffer);
|
---|
| 159 |
|
---|
[984a9ba] | 160 | async_answer_0(&data, ENOENT);
|
---|
| 161 | async_answer_0(icall, ENOENT);
|
---|
[4c9b28a] | 162 | fun_del_ref(fun);
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[d80d7a8] | 165 | /** Get device name. */
|
---|
[984a9ba] | 166 | static void devman_fun_get_name(ipc_call_t *icall)
|
---|
[d80d7a8] | 167 | {
|
---|
| 168 | devman_handle_t handle = IPC_GET_ARG1(*icall);
|
---|
| 169 |
|
---|
| 170 | fun_node_t *fun = find_fun_node(&device_tree, handle);
|
---|
| 171 | if (fun == NULL) {
|
---|
[984a9ba] | 172 | async_answer_0(icall, ENOMEM);
|
---|
[d80d7a8] | 173 | return;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
[984a9ba] | 176 | ipc_call_t data;
|
---|
[d80d7a8] | 177 | size_t data_len;
|
---|
[984a9ba] | 178 | if (!async_data_read_receive(&data, &data_len)) {
|
---|
| 179 | async_answer_0(icall, EINVAL);
|
---|
[d80d7a8] | 180 | fun_del_ref(fun);
|
---|
| 181 | return;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | void *buffer = malloc(data_len);
|
---|
| 185 | if (buffer == NULL) {
|
---|
[984a9ba] | 186 | async_answer_0(&data, ENOMEM);
|
---|
| 187 | async_answer_0(icall, ENOMEM);
|
---|
[d80d7a8] | 188 | fun_del_ref(fun);
|
---|
| 189 | return;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
| 193 |
|
---|
| 194 | /* Check function state */
|
---|
| 195 | if (fun->state == FUN_REMOVED) {
|
---|
| 196 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
| 197 | free(buffer);
|
---|
| 198 |
|
---|
[984a9ba] | 199 | async_answer_0(&data, ENOENT);
|
---|
| 200 | async_answer_0(icall, ENOENT);
|
---|
[d80d7a8] | 201 | fun_del_ref(fun);
|
---|
| 202 | return;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | size_t sent_length = str_size(fun->name);
|
---|
| 206 | if (sent_length > data_len) {
|
---|
| 207 | sent_length = data_len;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[984a9ba] | 210 | async_data_read_finalize(&data, fun->name, sent_length);
|
---|
| 211 | async_answer_0(icall, EOK);
|
---|
[d80d7a8] | 212 |
|
---|
| 213 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
| 214 | fun_del_ref(fun);
|
---|
| 215 | free(buffer);
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | /** Get function driver name. */
|
---|
[984a9ba] | 219 | static void devman_fun_get_driver_name(ipc_call_t *icall)
|
---|
[d80d7a8] | 220 | {
|
---|
| 221 | devman_handle_t handle = IPC_GET_ARG1(*icall);
|
---|
| 222 |
|
---|
| 223 | fun_node_t *fun = find_fun_node(&device_tree, handle);
|
---|
| 224 | if (fun == NULL) {
|
---|
[984a9ba] | 225 | async_answer_0(icall, ENOMEM);
|
---|
[d80d7a8] | 226 | return;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[984a9ba] | 229 | ipc_call_t data;
|
---|
[d80d7a8] | 230 | size_t data_len;
|
---|
[984a9ba] | 231 | if (!async_data_read_receive(&data, &data_len)) {
|
---|
| 232 | async_answer_0(icall, EINVAL);
|
---|
[d80d7a8] | 233 | fun_del_ref(fun);
|
---|
| 234 | return;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | void *buffer = malloc(data_len);
|
---|
| 238 | if (buffer == NULL) {
|
---|
[984a9ba] | 239 | async_answer_0(&data, ENOMEM);
|
---|
| 240 | async_answer_0(icall, ENOMEM);
|
---|
[d80d7a8] | 241 | fun_del_ref(fun);
|
---|
| 242 | return;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
| 246 |
|
---|
| 247 | /* Check function state */
|
---|
| 248 | if (fun->state == FUN_REMOVED) {
|
---|
| 249 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
| 250 | free(buffer);
|
---|
| 251 |
|
---|
[984a9ba] | 252 | async_answer_0(&data, ENOENT);
|
---|
| 253 | async_answer_0(icall, ENOENT);
|
---|
[d80d7a8] | 254 | fun_del_ref(fun);
|
---|
| 255 | return;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | /* Check whether function has a driver */
|
---|
| 259 | if (fun->child == NULL || fun->child->drv == NULL) {
|
---|
| 260 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
| 261 | free(buffer);
|
---|
| 262 |
|
---|
[984a9ba] | 263 | async_answer_0(&data, EINVAL);
|
---|
| 264 | async_answer_0(icall, EINVAL);
|
---|
[d80d7a8] | 265 | fun_del_ref(fun);
|
---|
| 266 | return;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | size_t sent_length = str_size(fun->child->drv->name);
|
---|
| 270 | if (sent_length > data_len) {
|
---|
| 271 | sent_length = data_len;
|
---|
| 272 | }
|
---|
| 273 |
|
---|
[984a9ba] | 274 | async_data_read_finalize(&data, fun->child->drv->name,
|
---|
[d80d7a8] | 275 | sent_length);
|
---|
[984a9ba] | 276 | async_answer_0(icall, EOK);
|
---|
[d80d7a8] | 277 |
|
---|
| 278 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
| 279 | fun_del_ref(fun);
|
---|
| 280 | free(buffer);
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | /** Get device path. */
|
---|
[984a9ba] | 284 | static void devman_fun_get_path(ipc_call_t *icall)
|
---|
[d80d7a8] | 285 | {
|
---|
| 286 | devman_handle_t handle = IPC_GET_ARG1(*icall);
|
---|
| 287 |
|
---|
| 288 | fun_node_t *fun = find_fun_node(&device_tree, handle);
|
---|
| 289 | if (fun == NULL) {
|
---|
[984a9ba] | 290 | async_answer_0(icall, ENOMEM);
|
---|
[d80d7a8] | 291 | return;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
[984a9ba] | 294 | ipc_call_t data;
|
---|
[d80d7a8] | 295 | size_t data_len;
|
---|
[984a9ba] | 296 | if (!async_data_read_receive(&data, &data_len)) {
|
---|
| 297 | async_answer_0(icall, EINVAL);
|
---|
[d80d7a8] | 298 | fun_del_ref(fun);
|
---|
| 299 | return;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | void *buffer = malloc(data_len);
|
---|
| 303 | if (buffer == NULL) {
|
---|
[984a9ba] | 304 | async_answer_0(&data, ENOMEM);
|
---|
| 305 | async_answer_0(icall, ENOMEM);
|
---|
[d80d7a8] | 306 | fun_del_ref(fun);
|
---|
| 307 | return;
|
---|
| 308 | }
|
---|
[a35b458] | 309 |
|
---|
[d80d7a8] | 310 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
[a35b458] | 311 |
|
---|
[d80d7a8] | 312 | /* Check function state */
|
---|
| 313 | if (fun->state == FUN_REMOVED) {
|
---|
| 314 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
| 315 | free(buffer);
|
---|
| 316 |
|
---|
[984a9ba] | 317 | async_answer_0(&data, ENOENT);
|
---|
| 318 | async_answer_0(icall, ENOENT);
|
---|
[d80d7a8] | 319 | fun_del_ref(fun);
|
---|
| 320 | return;
|
---|
| 321 | }
|
---|
[a35b458] | 322 |
|
---|
[d80d7a8] | 323 | size_t sent_length = str_size(fun->pathname);
|
---|
| 324 | if (sent_length > data_len) {
|
---|
| 325 | sent_length = data_len;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
[984a9ba] | 328 | async_data_read_finalize(&data, fun->pathname, sent_length);
|
---|
| 329 | async_answer_0(icall, EOK);
|
---|
[d80d7a8] | 330 |
|
---|
| 331 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
| 332 | fun_del_ref(fun);
|
---|
| 333 | free(buffer);
|
---|
| 334 | }
|
---|
| 335 |
|
---|
[1db5669] | 336 | /** Get handle for parent function of a device. */
|
---|
[984a9ba] | 337 | static void devman_dev_get_parent(ipc_call_t *icall)
|
---|
[1db5669] | 338 | {
|
---|
| 339 | dev_node_t *dev;
|
---|
[a35b458] | 340 |
|
---|
[1db5669] | 341 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
[a35b458] | 342 |
|
---|
[1db5669] | 343 | dev = find_dev_node_no_lock(&device_tree, IPC_GET_ARG1(*icall));
|
---|
| 344 | if (dev == NULL || dev->state == DEVICE_REMOVED) {
|
---|
| 345 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
[984a9ba] | 346 | async_answer_0(icall, ENOENT);
|
---|
[1db5669] | 347 | return;
|
---|
| 348 | }
|
---|
[a35b458] | 349 |
|
---|
[1db5669] | 350 | if (dev->pfun == NULL) {
|
---|
| 351 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
[984a9ba] | 352 | async_answer_0(icall, ENOENT);
|
---|
[1db5669] | 353 | return;
|
---|
| 354 | }
|
---|
[a35b458] | 355 |
|
---|
[984a9ba] | 356 | async_answer_1(icall, EOK, dev->pfun->handle);
|
---|
[a35b458] | 357 |
|
---|
[1db5669] | 358 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
| 359 | }
|
---|
| 360 |
|
---|
[984a9ba] | 361 | static void devman_dev_get_functions(ipc_call_t *icall)
|
---|
[d80d7a8] | 362 | {
|
---|
[984a9ba] | 363 | ipc_call_t call;
|
---|
[d80d7a8] | 364 | size_t size;
|
---|
| 365 | size_t act_size;
|
---|
[b7fd2a0] | 366 | errno_t rc;
|
---|
[a35b458] | 367 |
|
---|
[984a9ba] | 368 | if (!async_data_read_receive(&call, &size)) {
|
---|
| 369 | async_answer_0(&call, EREFUSED);
|
---|
| 370 | async_answer_0(icall, EREFUSED);
|
---|
[d80d7a8] | 371 | return;
|
---|
| 372 | }
|
---|
[a35b458] | 373 |
|
---|
[d80d7a8] | 374 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
[a35b458] | 375 |
|
---|
[d80d7a8] | 376 | dev_node_t *dev = find_dev_node_no_lock(&device_tree,
|
---|
| 377 | IPC_GET_ARG1(*icall));
|
---|
| 378 | if (dev == NULL || dev->state == DEVICE_REMOVED) {
|
---|
| 379 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
[984a9ba] | 380 | async_answer_0(&call, ENOENT);
|
---|
| 381 | async_answer_0(icall, ENOENT);
|
---|
[d80d7a8] | 382 | return;
|
---|
| 383 | }
|
---|
[a35b458] | 384 |
|
---|
[d80d7a8] | 385 | devman_handle_t *hdl_buf = (devman_handle_t *) malloc(size);
|
---|
| 386 | if (hdl_buf == NULL) {
|
---|
| 387 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
[984a9ba] | 388 | async_answer_0(&call, ENOMEM);
|
---|
| 389 | async_answer_0(icall, ENOMEM);
|
---|
[d80d7a8] | 390 | return;
|
---|
| 391 | }
|
---|
[a35b458] | 392 |
|
---|
[d80d7a8] | 393 | rc = dev_get_functions(&device_tree, dev, hdl_buf, size, &act_size);
|
---|
| 394 | if (rc != EOK) {
|
---|
| 395 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
[984a9ba] | 396 | async_answer_0(&call, rc);
|
---|
| 397 | async_answer_0(icall, rc);
|
---|
[d80d7a8] | 398 | return;
|
---|
| 399 | }
|
---|
[a35b458] | 400 |
|
---|
[d80d7a8] | 401 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
[a35b458] | 402 |
|
---|
[984a9ba] | 403 | errno_t retval = async_data_read_finalize(&call, hdl_buf, size);
|
---|
[d80d7a8] | 404 | free(hdl_buf);
|
---|
[a35b458] | 405 |
|
---|
[984a9ba] | 406 | async_answer_1(icall, retval, act_size);
|
---|
[d80d7a8] | 407 | }
|
---|
| 408 |
|
---|
| 409 | /** Get handle for child device of a function. */
|
---|
[984a9ba] | 410 | static void devman_fun_get_child(ipc_call_t *icall)
|
---|
[d80d7a8] | 411 | {
|
---|
| 412 | fun_node_t *fun;
|
---|
[a35b458] | 413 |
|
---|
[d80d7a8] | 414 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
[a35b458] | 415 |
|
---|
[d80d7a8] | 416 | fun = find_fun_node_no_lock(&device_tree, IPC_GET_ARG1(*icall));
|
---|
| 417 | if (fun == NULL || fun->state == FUN_REMOVED) {
|
---|
| 418 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
[984a9ba] | 419 | async_answer_0(icall, ENOENT);
|
---|
[d80d7a8] | 420 | return;
|
---|
| 421 | }
|
---|
[a35b458] | 422 |
|
---|
[d80d7a8] | 423 | if (fun->child == NULL) {
|
---|
| 424 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
[984a9ba] | 425 | async_answer_0(icall, ENOENT);
|
---|
[d80d7a8] | 426 | return;
|
---|
| 427 | }
|
---|
[a35b458] | 428 |
|
---|
[984a9ba] | 429 | async_answer_1(icall, EOK, fun->child->handle);
|
---|
[a35b458] | 430 |
|
---|
[d80d7a8] | 431 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
| 432 | }
|
---|
| 433 |
|
---|
| 434 | /** Online function.
|
---|
| 435 | *
|
---|
| 436 | * Send a request to online a function to the responsible driver.
|
---|
| 437 | * The driver may offline other functions if necessary (i.e. if the state
|
---|
| 438 | * of this function is linked to state of another function somehow).
|
---|
| 439 | */
|
---|
[984a9ba] | 440 | static void devman_fun_online(ipc_call_t *icall)
|
---|
[d80d7a8] | 441 | {
|
---|
| 442 | fun_node_t *fun;
|
---|
[b7fd2a0] | 443 | errno_t rc;
|
---|
[d80d7a8] | 444 |
|
---|
| 445 | fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));
|
---|
| 446 | if (fun == NULL) {
|
---|
[984a9ba] | 447 | async_answer_0(icall, ENOENT);
|
---|
[d80d7a8] | 448 | return;
|
---|
| 449 | }
|
---|
[a35b458] | 450 |
|
---|
[d80d7a8] | 451 | rc = driver_fun_online(&device_tree, fun);
|
---|
| 452 | fun_del_ref(fun);
|
---|
[a35b458] | 453 |
|
---|
[984a9ba] | 454 | async_answer_0(icall, rc);
|
---|
[d80d7a8] | 455 | }
|
---|
| 456 |
|
---|
| 457 | /** Offline function.
|
---|
| 458 | *
|
---|
| 459 | * Send a request to offline a function to the responsible driver. As
|
---|
| 460 | * a result the subtree rooted at that function should be cleanly
|
---|
| 461 | * detatched. The driver may offline other functions if necessary
|
---|
| 462 | * (i.e. if the state of this function is linked to state of another
|
---|
| 463 | * function somehow).
|
---|
| 464 | */
|
---|
[984a9ba] | 465 | static void devman_fun_offline(ipc_call_t *icall)
|
---|
[d80d7a8] | 466 | {
|
---|
| 467 | fun_node_t *fun;
|
---|
[b7fd2a0] | 468 | errno_t rc;
|
---|
[d80d7a8] | 469 |
|
---|
| 470 | fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));
|
---|
| 471 | if (fun == NULL) {
|
---|
[984a9ba] | 472 | async_answer_0(icall, ENOENT);
|
---|
[d80d7a8] | 473 | return;
|
---|
| 474 | }
|
---|
[a35b458] | 475 |
|
---|
[d80d7a8] | 476 | rc = driver_fun_offline(&device_tree, fun);
|
---|
| 477 | fun_del_ref(fun);
|
---|
[a35b458] | 478 |
|
---|
[984a9ba] | 479 | async_answer_0(icall, rc);
|
---|
[d80d7a8] | 480 | }
|
---|
| 481 |
|
---|
| 482 | /** Find handle for the function instance identified by its service ID. */
|
---|
[984a9ba] | 483 | static void devman_fun_sid_to_handle(ipc_call_t *icall)
|
---|
[d80d7a8] | 484 | {
|
---|
| 485 | fun_node_t *fun;
|
---|
| 486 |
|
---|
| 487 | fun = find_loc_tree_function(&device_tree, IPC_GET_ARG1(*icall));
|
---|
[a35b458] | 488 |
|
---|
[d80d7a8] | 489 | if (fun == NULL) {
|
---|
[984a9ba] | 490 | async_answer_0(icall, ENOENT);
|
---|
[d80d7a8] | 491 | return;
|
---|
| 492 | }
|
---|
| 493 |
|
---|
| 494 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
| 495 |
|
---|
| 496 | /* Check function state */
|
---|
| 497 | if (fun->state == FUN_REMOVED) {
|
---|
| 498 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
[984a9ba] | 499 | async_answer_0(icall, ENOENT);
|
---|
[d80d7a8] | 500 | return;
|
---|
| 501 | }
|
---|
| 502 |
|
---|
[984a9ba] | 503 | async_answer_1(icall, EOK, fun->handle);
|
---|
[d80d7a8] | 504 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
| 505 | fun_del_ref(fun);
|
---|
| 506 | }
|
---|
| 507 |
|
---|
[0511549] | 508 | /** Get list of all registered drivers. */
|
---|
[984a9ba] | 509 | static void devman_get_drivers(ipc_call_t *icall)
|
---|
[0511549] | 510 | {
|
---|
[984a9ba] | 511 | ipc_call_t call;
|
---|
[0511549] | 512 | size_t size;
|
---|
| 513 | size_t act_size;
|
---|
[b7fd2a0] | 514 | errno_t rc;
|
---|
[a35b458] | 515 |
|
---|
[984a9ba] | 516 | if (!async_data_read_receive(&call, &size)) {
|
---|
| 517 | async_answer_0(icall, EREFUSED);
|
---|
[0511549] | 518 | return;
|
---|
| 519 | }
|
---|
[a35b458] | 520 |
|
---|
[0511549] | 521 | devman_handle_t *hdl_buf = (devman_handle_t *) malloc(size);
|
---|
| 522 | if (hdl_buf == NULL) {
|
---|
[984a9ba] | 523 | async_answer_0(&call, ENOMEM);
|
---|
| 524 | async_answer_0(icall, ENOMEM);
|
---|
[0511549] | 525 | return;
|
---|
| 526 | }
|
---|
[a35b458] | 527 |
|
---|
[0511549] | 528 | rc = driver_get_list(&drivers_list, hdl_buf, size, &act_size);
|
---|
| 529 | if (rc != EOK) {
|
---|
[984a9ba] | 530 | async_answer_0(&call, rc);
|
---|
| 531 | async_answer_0(icall, rc);
|
---|
[0511549] | 532 | return;
|
---|
| 533 | }
|
---|
[a35b458] | 534 |
|
---|
[984a9ba] | 535 | errno_t retval = async_data_read_finalize(&call, hdl_buf, size);
|
---|
[0511549] | 536 | free(hdl_buf);
|
---|
[a35b458] | 537 |
|
---|
[984a9ba] | 538 | async_answer_1(icall, retval, act_size);
|
---|
[0511549] | 539 | }
|
---|
| 540 |
|
---|
[984a9ba] | 541 | static void devman_driver_get_devices(ipc_call_t *icall)
|
---|
[1db5669] | 542 | {
|
---|
[984a9ba] | 543 | ipc_call_t call;
|
---|
[1db5669] | 544 | size_t size;
|
---|
| 545 | size_t act_size;
|
---|
[b7fd2a0] | 546 | errno_t rc;
|
---|
[a35b458] | 547 |
|
---|
[984a9ba] | 548 | if (!async_data_read_receive(&call, &size)) {
|
---|
| 549 | async_answer_0(icall, EREFUSED);
|
---|
[1db5669] | 550 | return;
|
---|
| 551 | }
|
---|
[a35b458] | 552 |
|
---|
[1db5669] | 553 | driver_t *drv = driver_find(&drivers_list, IPC_GET_ARG1(*icall));
|
---|
| 554 | if (drv == NULL) {
|
---|
[984a9ba] | 555 | async_answer_0(&call, ENOENT);
|
---|
| 556 | async_answer_0(icall, ENOENT);
|
---|
[1db5669] | 557 | return;
|
---|
| 558 | }
|
---|
[a35b458] | 559 |
|
---|
[1db5669] | 560 | devman_handle_t *hdl_buf = (devman_handle_t *) malloc(size);
|
---|
| 561 | if (hdl_buf == NULL) {
|
---|
[984a9ba] | 562 | async_answer_0(&call, ENOMEM);
|
---|
| 563 | async_answer_0(icall, ENOMEM);
|
---|
[1db5669] | 564 | return;
|
---|
| 565 | }
|
---|
[a35b458] | 566 |
|
---|
[1db5669] | 567 | rc = driver_get_devices(drv, hdl_buf, size, &act_size);
|
---|
| 568 | if (rc != EOK) {
|
---|
| 569 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
[984a9ba] | 570 | async_answer_0(&call, rc);
|
---|
| 571 | async_answer_0(icall, rc);
|
---|
[1db5669] | 572 | return;
|
---|
| 573 | }
|
---|
[a35b458] | 574 |
|
---|
[984a9ba] | 575 | errno_t retval = async_data_read_finalize(&call, hdl_buf, size);
|
---|
[1db5669] | 576 | free(hdl_buf);
|
---|
[a35b458] | 577 |
|
---|
[984a9ba] | 578 | async_answer_1(icall, retval, act_size);
|
---|
[1db5669] | 579 | }
|
---|
| 580 |
|
---|
| 581 |
|
---|
[7969087] | 582 | /** Find driver by name. */
|
---|
[984a9ba] | 583 | static void devman_driver_get_handle(ipc_call_t *icall)
|
---|
[7969087] | 584 | {
|
---|
| 585 | char *drvname;
|
---|
[a35b458] | 586 |
|
---|
[b7fd2a0] | 587 | errno_t rc = async_data_write_accept((void **) &drvname, true, 0, 0, 0, 0);
|
---|
[7969087] | 588 | if (rc != EOK) {
|
---|
[984a9ba] | 589 | async_answer_0(icall, rc);
|
---|
[7969087] | 590 | return;
|
---|
| 591 | }
|
---|
[a35b458] | 592 |
|
---|
[7969087] | 593 | driver_t *driver = driver_find_by_name(&drivers_list, drvname);
|
---|
[a35b458] | 594 |
|
---|
[7969087] | 595 | free(drvname);
|
---|
[a35b458] | 596 |
|
---|
[7969087] | 597 | if (driver == NULL) {
|
---|
[984a9ba] | 598 | async_answer_0(icall, ENOENT);
|
---|
[7969087] | 599 | return;
|
---|
| 600 | }
|
---|
[a35b458] | 601 |
|
---|
[984a9ba] | 602 | async_answer_1(icall, EOK, driver->handle);
|
---|
[7969087] | 603 | }
|
---|
| 604 |
|
---|
[4c9b28a] | 605 | /** Get driver match ID. */
|
---|
[984a9ba] | 606 | static void devman_driver_get_match_id(ipc_call_t *icall)
|
---|
[4c9b28a] | 607 | {
|
---|
| 608 | devman_handle_t handle = IPC_GET_ARG1(*icall);
|
---|
| 609 | size_t index = IPC_GET_ARG2(*icall);
|
---|
| 610 |
|
---|
| 611 | driver_t *drv = driver_find(&drivers_list, handle);
|
---|
| 612 | if (drv == NULL) {
|
---|
[984a9ba] | 613 | async_answer_0(icall, ENOMEM);
|
---|
[4c9b28a] | 614 | return;
|
---|
| 615 | }
|
---|
| 616 |
|
---|
[984a9ba] | 617 | ipc_call_t data;
|
---|
[4c9b28a] | 618 | size_t data_len;
|
---|
[984a9ba] | 619 | if (!async_data_read_receive(&data, &data_len)) {
|
---|
| 620 | async_answer_0(icall, EINVAL);
|
---|
[4c9b28a] | 621 | return;
|
---|
| 622 | }
|
---|
| 623 |
|
---|
| 624 | void *buffer = malloc(data_len);
|
---|
| 625 | if (buffer == NULL) {
|
---|
[984a9ba] | 626 | async_answer_0(&data, ENOMEM);
|
---|
| 627 | async_answer_0(icall, ENOMEM);
|
---|
[4c9b28a] | 628 | return;
|
---|
| 629 | }
|
---|
| 630 |
|
---|
| 631 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
| 632 | link_t *link = list_nth(&drv->match_ids.ids, index);
|
---|
| 633 | if (link == NULL) {
|
---|
| 634 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
[5b18137] | 635 | free(buffer);
|
---|
[984a9ba] | 636 | async_answer_0(&data, ENOMEM);
|
---|
| 637 | async_answer_0(icall, ENOMEM);
|
---|
[4c9b28a] | 638 | return;
|
---|
| 639 | }
|
---|
| 640 |
|
---|
| 641 | match_id_t *mid = list_get_instance(link, match_id_t, link);
|
---|
| 642 |
|
---|
| 643 | size_t sent_length = str_size(mid->id);
|
---|
| 644 | if (sent_length > data_len) {
|
---|
| 645 | sent_length = data_len;
|
---|
| 646 | }
|
---|
| 647 |
|
---|
[984a9ba] | 648 | async_data_read_finalize(&data, mid->id, sent_length);
|
---|
| 649 | async_answer_1(icall, EOK, mid->score);
|
---|
[4c9b28a] | 650 |
|
---|
| 651 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
| 652 |
|
---|
| 653 | free(buffer);
|
---|
| 654 | }
|
---|
| 655 |
|
---|
[0511549] | 656 | /** Get driver name. */
|
---|
[984a9ba] | 657 | static void devman_driver_get_name(ipc_call_t *icall)
|
---|
[0511549] | 658 | {
|
---|
| 659 | devman_handle_t handle = IPC_GET_ARG1(*icall);
|
---|
| 660 |
|
---|
| 661 | driver_t *drv = driver_find(&drivers_list, handle);
|
---|
| 662 | if (drv == NULL) {
|
---|
[984a9ba] | 663 | async_answer_0(icall, ENOMEM);
|
---|
[0511549] | 664 | return;
|
---|
| 665 | }
|
---|
| 666 |
|
---|
[984a9ba] | 667 | ipc_call_t data;
|
---|
[0511549] | 668 | size_t data_len;
|
---|
[984a9ba] | 669 | if (!async_data_read_receive(&data, &data_len)) {
|
---|
| 670 | async_answer_0(icall, EINVAL);
|
---|
[0511549] | 671 | return;
|
---|
| 672 | }
|
---|
| 673 |
|
---|
| 674 | void *buffer = malloc(data_len);
|
---|
| 675 | if (buffer == NULL) {
|
---|
[984a9ba] | 676 | async_answer_0(&data, ENOMEM);
|
---|
| 677 | async_answer_0(icall, ENOMEM);
|
---|
[0511549] | 678 | return;
|
---|
| 679 | }
|
---|
| 680 |
|
---|
| 681 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
| 682 |
|
---|
| 683 | size_t sent_length = str_size(drv->name);
|
---|
| 684 | if (sent_length > data_len) {
|
---|
| 685 | sent_length = data_len;
|
---|
| 686 | }
|
---|
| 687 |
|
---|
[984a9ba] | 688 | async_data_read_finalize(&data, drv->name, sent_length);
|
---|
| 689 | async_answer_0(icall, EOK);
|
---|
[0511549] | 690 |
|
---|
| 691 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
| 692 |
|
---|
| 693 | free(buffer);
|
---|
| 694 | }
|
---|
| 695 |
|
---|
[e5556e4a] | 696 | /** Get driver state. */
|
---|
[984a9ba] | 697 | static void devman_driver_get_state(ipc_call_t *icall)
|
---|
[e5556e4a] | 698 | {
|
---|
| 699 | driver_t *drv;
|
---|
[a35b458] | 700 |
|
---|
[e5556e4a] | 701 | drv = driver_find(&drivers_list, IPC_GET_ARG1(*icall));
|
---|
| 702 | if (drv == NULL) {
|
---|
[984a9ba] | 703 | async_answer_0(icall, ENOENT);
|
---|
[e5556e4a] | 704 | return;
|
---|
| 705 | }
|
---|
[a35b458] | 706 |
|
---|
[984a9ba] | 707 | async_answer_1(icall, EOK, (sysarg_t) drv->state);
|
---|
[e5556e4a] | 708 | }
|
---|
| 709 |
|
---|
[7969087] | 710 | /** Forcibly load a driver. */
|
---|
[984a9ba] | 711 | static void devman_driver_load(ipc_call_t *icall)
|
---|
[7969087] | 712 | {
|
---|
| 713 | driver_t *drv;
|
---|
[b7fd2a0] | 714 | errno_t rc;
|
---|
[a35b458] | 715 |
|
---|
[7969087] | 716 | drv = driver_find(&drivers_list, IPC_GET_ARG1(*icall));
|
---|
| 717 | if (drv == NULL) {
|
---|
[984a9ba] | 718 | async_answer_0(icall, ENOENT);
|
---|
[7969087] | 719 | return;
|
---|
| 720 | }
|
---|
[a35b458] | 721 |
|
---|
[7969087] | 722 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
| 723 | rc = start_driver(drv) ? EOK : EIO;
|
---|
| 724 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
| 725 |
|
---|
[984a9ba] | 726 | async_answer_0(icall, rc);
|
---|
[7969087] | 727 | }
|
---|
| 728 |
|
---|
[81685dd9] | 729 | /** Unload a driver by user request. */
|
---|
[984a9ba] | 730 | static void devman_driver_unload(ipc_call_t *icall)
|
---|
[81685dd9] | 731 | {
|
---|
| 732 | driver_t *drv;
|
---|
[b7fd2a0] | 733 | errno_t rc;
|
---|
[a35b458] | 734 |
|
---|
[81685dd9] | 735 | drv = driver_find(&drivers_list, IPC_GET_ARG1(*icall));
|
---|
| 736 | if (drv == NULL) {
|
---|
[984a9ba] | 737 | async_answer_0(icall, ENOENT);
|
---|
[81685dd9] | 738 | return;
|
---|
| 739 | }
|
---|
[a35b458] | 740 |
|
---|
[81685dd9] | 741 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
| 742 | rc = stop_driver(drv);
|
---|
| 743 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
| 744 |
|
---|
[984a9ba] | 745 | async_answer_0(icall, rc);
|
---|
[81685dd9] | 746 | }
|
---|
| 747 |
|
---|
[d80d7a8] | 748 | /** Function for handling connections from a client to the device manager. */
|
---|
[984a9ba] | 749 | void devman_connection_client(ipc_call_t *icall, void *arg)
|
---|
[d80d7a8] | 750 | {
|
---|
| 751 | /* Accept connection. */
|
---|
[984a9ba] | 752 | async_answer_0(icall, EOK);
|
---|
[a35b458] | 753 |
|
---|
[d80d7a8] | 754 | while (true) {
|
---|
| 755 | ipc_call_t call;
|
---|
[984a9ba] | 756 | async_get_call(&call);
|
---|
[a35b458] | 757 |
|
---|
[d80d7a8] | 758 | if (!IPC_GET_IMETHOD(call))
|
---|
| 759 | break;
|
---|
[a35b458] | 760 |
|
---|
[d80d7a8] | 761 | switch (IPC_GET_IMETHOD(call)) {
|
---|
| 762 | case DEVMAN_DEVICE_GET_HANDLE:
|
---|
[984a9ba] | 763 | devman_function_get_handle(&call);
|
---|
[d80d7a8] | 764 | break;
|
---|
[1db5669] | 765 | case DEVMAN_DEV_GET_PARENT:
|
---|
[984a9ba] | 766 | devman_dev_get_parent(&call);
|
---|
[1db5669] | 767 | break;
|
---|
[d80d7a8] | 768 | case DEVMAN_DEV_GET_FUNCTIONS:
|
---|
[984a9ba] | 769 | devman_dev_get_functions(&call);
|
---|
[d80d7a8] | 770 | break;
|
---|
| 771 | case DEVMAN_FUN_GET_CHILD:
|
---|
[984a9ba] | 772 | devman_fun_get_child(&call);
|
---|
[d80d7a8] | 773 | break;
|
---|
[4c9b28a] | 774 | case DEVMAN_FUN_GET_MATCH_ID:
|
---|
[984a9ba] | 775 | devman_fun_get_match_id(&call);
|
---|
[4c9b28a] | 776 | break;
|
---|
[d80d7a8] | 777 | case DEVMAN_FUN_GET_NAME:
|
---|
[984a9ba] | 778 | devman_fun_get_name(&call);
|
---|
[d80d7a8] | 779 | break;
|
---|
| 780 | case DEVMAN_FUN_GET_DRIVER_NAME:
|
---|
[984a9ba] | 781 | devman_fun_get_driver_name(&call);
|
---|
[d80d7a8] | 782 | break;
|
---|
| 783 | case DEVMAN_FUN_GET_PATH:
|
---|
[984a9ba] | 784 | devman_fun_get_path(&call);
|
---|
[d80d7a8] | 785 | break;
|
---|
| 786 | case DEVMAN_FUN_ONLINE:
|
---|
[984a9ba] | 787 | devman_fun_online(&call);
|
---|
[d80d7a8] | 788 | break;
|
---|
| 789 | case DEVMAN_FUN_OFFLINE:
|
---|
[984a9ba] | 790 | devman_fun_offline(&call);
|
---|
[d80d7a8] | 791 | break;
|
---|
| 792 | case DEVMAN_FUN_SID_TO_HANDLE:
|
---|
[984a9ba] | 793 | devman_fun_sid_to_handle(&call);
|
---|
[d80d7a8] | 794 | break;
|
---|
[0511549] | 795 | case DEVMAN_GET_DRIVERS:
|
---|
[984a9ba] | 796 | devman_get_drivers(&call);
|
---|
[0511549] | 797 | break;
|
---|
[1db5669] | 798 | case DEVMAN_DRIVER_GET_DEVICES:
|
---|
[984a9ba] | 799 | devman_driver_get_devices(&call);
|
---|
[1db5669] | 800 | break;
|
---|
[7969087] | 801 | case DEVMAN_DRIVER_GET_HANDLE:
|
---|
[984a9ba] | 802 | devman_driver_get_handle(&call);
|
---|
[7969087] | 803 | break;
|
---|
[4c9b28a] | 804 | case DEVMAN_DRIVER_GET_MATCH_ID:
|
---|
[984a9ba] | 805 | devman_driver_get_match_id(&call);
|
---|
[4c9b28a] | 806 | break;
|
---|
[0511549] | 807 | case DEVMAN_DRIVER_GET_NAME:
|
---|
[984a9ba] | 808 | devman_driver_get_name(&call);
|
---|
[0511549] | 809 | break;
|
---|
[e5556e4a] | 810 | case DEVMAN_DRIVER_GET_STATE:
|
---|
[984a9ba] | 811 | devman_driver_get_state(&call);
|
---|
[e5556e4a] | 812 | break;
|
---|
[7969087] | 813 | case DEVMAN_DRIVER_LOAD:
|
---|
[984a9ba] | 814 | devman_driver_load(&call);
|
---|
[7969087] | 815 | break;
|
---|
[81685dd9] | 816 | case DEVMAN_DRIVER_UNLOAD:
|
---|
[984a9ba] | 817 | devman_driver_unload(&call);
|
---|
[81685dd9] | 818 | break;
|
---|
[d80d7a8] | 819 | default:
|
---|
[984a9ba] | 820 | async_answer_0(&call, ENOENT);
|
---|
[d80d7a8] | 821 | }
|
---|
| 822 | }
|
---|
| 823 | }
|
---|
| 824 |
|
---|
| 825 | /** @}
|
---|
| 826 | */
|
---|