source: mainline/uspace/srv/devman/main.c@ 422722e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 422722e was 422722e, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Devman-driver and driver-devman parallel exchange support. Also fix
service_connect_blocking().

  • Property mode set to 100644
File size: 24.0 KB
RevLine 
[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
[7e752b2]38#include <inttypes.h>
[e2b9a993]39#include <assert.h>
40#include <ipc/services.h>
[79ae36dd]41#include <ns.h>
[e2b9a993]42#include <async.h>
43#include <stdio.h>
44#include <errno.h>
[9b415c9]45#include <str_error.h>
[e2b9a993]46#include <bool.h>
47#include <fibril_synch.h>
48#include <stdlib.h>
[c47e1a8]49#include <str.h>
[e2b9a993]50#include <dirent.h>
51#include <fcntl.h>
52#include <sys/stat.h>
53#include <ctype.h>
[9b415c9]54#include <io/log.h>
[e2b9a993]55#include <ipc/devman.h>
[5cd136ab]56#include <ipc/driver.h>
[d347b53]57#include <thread.h>
[15f3c3f]58#include <loc.h>
[e2b9a993]59
60#include "devman.h"
61
[a79d88d]62#define DRIVER_DEFAULT_STORE "/drv"
[e2b9a993]63
[0c3666d]64static driver_list_t drivers_list;
[e2b9a993]65static dev_tree_t device_tree;
66
[422722e]67static int init_running_drv(void *drv);
68
[38b3baf]69/** Register running driver. */
[422722e]70static driver_t *devman_driver_register(ipc_callid_t callid, ipc_call_t *call)
[38b3baf]71{
[729fa2d6]72 driver_t *driver = NULL;
[422722e]73 char *drv_name = NULL;
[c6c389ed]74
[ebcb05a]75 log_msg(LVL_DEBUG, "devman_driver_register");
[729fa2d6]76
[38b3baf]77 /* Get driver name. */
78 int rc = async_data_write_accept((void **) &drv_name, true, 0, 0, 0, 0);
[729fa2d6]79 if (rc != EOK) {
[422722e]80 async_answer_0(callid, rc);
[729fa2d6]81 return NULL;
82 }
[c6c389ed]83
[ebcb05a]84 log_msg(LVL_DEBUG, "The `%s' driver is trying to register.",
[38b3baf]85 drv_name);
[729fa2d6]86
[38b3baf]87 /* Find driver structure. */
[729fa2d6]88 driver = find_driver(&drivers_list, drv_name);
[c6c389ed]89 if (driver == NULL) {
[ebcb05a]90 log_msg(LVL_ERROR, "No driver named `%s' was found.", drv_name);
[04c7003f]91 free(drv_name);
92 drv_name = NULL;
[422722e]93 async_answer_0(callid, ENOENT);
[729fa2d6]94 return NULL;
95 }
96
[04c7003f]97 free(drv_name);
98 drv_name = NULL;
99
[3ad7b1c]100 fibril_mutex_lock(&driver->driver_mutex);
101
[79ae36dd]102 if (driver->sess) {
[3ad7b1c]103 /* We already have a connection to the driver. */
104 log_msg(LVL_ERROR, "Driver '%s' already started.\n",
105 driver->name);
106 fibril_mutex_unlock(&driver->driver_mutex);
[422722e]107 async_answer_0(callid, EEXISTS);
[3ad7b1c]108 return NULL;
109 }
110
111 switch (driver->state) {
112 case DRIVER_NOT_STARTED:
113 /* Somebody started the driver manually. */
114 log_msg(LVL_NOTE, "Driver '%s' started manually.\n",
115 driver->name);
116 driver->state = DRIVER_STARTING;
117 break;
118 case DRIVER_STARTING:
119 /* The expected case */
120 break;
121 case DRIVER_RUNNING:
[79ae36dd]122 /* Should not happen since we do not have a connected session */
[3ad7b1c]123 assert(false);
124 }
125
[38b3baf]126 /* Create connection to the driver. */
[ebcb05a]127 log_msg(LVL_DEBUG, "Creating connection to the `%s' driver.",
[9b415c9]128 driver->name);
[422722e]129 driver->sess = async_callback_receive(EXCHANGE_PARALLEL);
[79ae36dd]130 if (!driver->sess) {
[3ad7b1c]131 fibril_mutex_unlock(&driver->driver_mutex);
[422722e]132 async_answer_0(callid, ENOTSUP);
[729fa2d6]133 return NULL;
134 }
[422722e]135 /* FIXME: Work around problem with callback sessions */
136 async_sess_args_set(driver->sess, DRIVER_DEVMAN, 0, 0);
[729fa2d6]137
[79ae36dd]138 log_msg(LVL_NOTE,
[ebcb05a]139 "The `%s' driver was successfully registered as running.",
[38b3baf]140 driver->name);
[729fa2d6]141
[422722e]142 /*
143 * Initialize the driver as running (e.g. pass assigned devices to it)
144 * in a separate fibril; the separate fibril is used to enable the
145 * driver to use devman service during the driver's initialization.
146 */
147 fid_t fid = fibril_create(init_running_drv, driver);
148 if (fid == 0) {
149 log_msg(LVL_ERROR, "Failed to create initialization fibril " \
150 "for driver `%s'.", driver->name);
151 fibril_mutex_unlock(&driver->driver_mutex);
152 async_answer_0(callid, ENOMEM);
153 return NULL;
154 }
[729fa2d6]155
[422722e]156 fibril_add_ready(fid);
157 fibril_mutex_unlock(&driver->driver_mutex);
158
159 async_answer_0(callid, EOK);
[729fa2d6]160 return driver;
161}
[e2b9a993]162
[38b3baf]163/** Receive device match ID from the device's parent driver and add it to the
164 * list of devices match ids.
165 *
166 * @param match_ids The list of the device's match ids.
167 * @return Zero on success, negative error code otherwise.
[3843ecb]168 */
[38b3baf]169static int devman_receive_match_id(match_id_list_t *match_ids)
170{
[66babbd]171 match_id_t *match_id = create_match_id();
172 ipc_callid_t callid;
173 ipc_call_t call;
174 int rc = 0;
175
176 callid = async_get_call(&call);
[228e490]177 if (DEVMAN_ADD_MATCH_ID != IPC_GET_IMETHOD(call)) {
[9b415c9]178 log_msg(LVL_ERROR,
[ebcb05a]179 "Invalid protocol when trying to receive match id.");
[ffa2c8ef]180 async_answer_0(callid, EINVAL);
[66babbd]181 delete_match_id(match_id);
182 return EINVAL;
183 }
184
[c6c389ed]185 if (match_id == NULL) {
[ebcb05a]186 log_msg(LVL_ERROR, "Failed to allocate match id.");
[ffa2c8ef]187 async_answer_0(callid, ENOMEM);
[66babbd]188 return ENOMEM;
189 }
190
[ffa2c8ef]191 async_answer_0(callid, EOK);
[2480e19]192
[66babbd]193 match_id->score = IPC_GET_ARG1(call);
194
[c47e1a8]195 char *match_id_str;
[38b3baf]196 rc = async_data_write_accept((void **) &match_id_str, true, 0, 0, 0, 0);
[c47e1a8]197 match_id->id = match_id_str;
[c6c389ed]198 if (rc != EOK) {
[66babbd]199 delete_match_id(match_id);
[ebcb05a]200 log_msg(LVL_ERROR, "Failed to receive match id string: %s.",
[9b415c9]201 str_error(rc));
[66babbd]202 return rc;
203 }
204
205 list_append(&match_id->link, &match_ids->ids);
[a087f2e]206
[ebcb05a]207 log_msg(LVL_DEBUG, "Received match id `%s', score %d.",
[38b3baf]208 match_id->id, match_id->score);
[66babbd]209 return rc;
210}
211
[38b3baf]212/** Receive device match IDs from the device's parent driver and add them to the
213 * list of devices match ids.
214 *
215 * @param match_count The number of device's match ids to be received.
216 * @param match_ids The list of the device's match ids.
217 * @return Zero on success, negative error code otherwise.
[3843ecb]218 */
[96b02eb9]219static int devman_receive_match_ids(sysarg_t match_count,
[c6c389ed]220 match_id_list_t *match_ids)
[38b3baf]221{
[66babbd]222 int ret = EOK;
[5cd136ab]223 size_t i;
[38b3baf]224
[66babbd]225 for (i = 0; i < match_count; i++) {
[38b3baf]226 if (EOK != (ret = devman_receive_match_id(match_ids)))
[66babbd]227 return ret;
228 }
229 return ret;
230}
231
[398c4d7]232static int assign_driver_fibril(void *arg)
233{
[ba38f72c]234 dev_node_t *dev_node = (dev_node_t *) arg;
235 assign_driver(dev_node, &drivers_list, &device_tree);
[398c4d7]236 return EOK;
237}
238
[8b1e15ac]239/** Handle function registration.
[38b3baf]240 *
[3843ecb]241 * Child devices are registered by their parent's device driver.
242 */
[8b1e15ac]243static void devman_add_function(ipc_callid_t callid, ipc_call_t *call)
[bda60d9]244{
[8b1e15ac]245 fun_type_t ftype = (fun_type_t) IPC_GET_ARG1(*call);
246 devman_handle_t dev_handle = IPC_GET_ARG2(*call);
247 sysarg_t match_count = IPC_GET_ARG3(*call);
[957cfa58]248 dev_tree_t *tree = &device_tree;
[66babbd]249
[957cfa58]250 fibril_rwlock_write_lock(&tree->rwlock);
[8b1e15ac]251
252 dev_node_t *dev = NULL;
253 dev_node_t *pdev = find_dev_node_no_lock(&device_tree, dev_handle);
[d347b53]254
[ba38f72c]255 if (pdev == NULL) {
[957cfa58]256 fibril_rwlock_write_unlock(&tree->rwlock);
[ffa2c8ef]257 async_answer_0(callid, ENOENT);
[d347b53]258 return;
[c6c389ed]259 }
[d347b53]260
[8b1e15ac]261 if (ftype != fun_inner && ftype != fun_exposed) {
262 /* Unknown function type */
[9b415c9]263 log_msg(LVL_ERROR,
[ebcb05a]264 "Unknown function type %d provided by driver.",
[9b415c9]265 (int) ftype);
[8b1e15ac]266
267 fibril_rwlock_write_unlock(&tree->rwlock);
268 async_answer_0(callid, EINVAL);
269 return;
270 }
271
[ba38f72c]272 char *fun_name = NULL;
273 int rc = async_data_write_accept((void **)&fun_name, true, 0, 0, 0, 0);
[c6c389ed]274 if (rc != EOK) {
[957cfa58]275 fibril_rwlock_write_unlock(&tree->rwlock);
[ffa2c8ef]276 async_answer_0(callid, rc);
[d347b53]277 return;
278 }
279
[0876062]280 /* Check that function with same name is not there already. */
281 if (find_fun_node_in_device(pdev, fun_name) != NULL) {
282 fibril_rwlock_write_unlock(&tree->rwlock);
283 async_answer_0(callid, EEXISTS);
284 printf(NAME ": Warning, driver tried to register `%s' twice.\n",
285 fun_name);
286 free(fun_name);
287 return;
288 }
[d0dd7b5]289
[ba38f72c]290 fun_node_t *fun = create_fun_node();
[d0dd7b5]291 fun->ftype = ftype;
292
[ba38f72c]293 if (!insert_fun_node(&device_tree, fun, fun_name, pdev)) {
[957cfa58]294 fibril_rwlock_write_unlock(&tree->rwlock);
[ba38f72c]295 delete_fun_node(fun);
[ffa2c8ef]296 async_answer_0(callid, ENOMEM);
[d347b53]297 return;
[c6c389ed]298 }
299
[8b1e15ac]300 if (ftype == fun_inner) {
301 dev = create_dev_node();
302 if (dev == NULL) {
303 fibril_rwlock_write_unlock(&tree->rwlock);
304 delete_fun_node(fun);
305 async_answer_0(callid, ENOMEM);
306 return;
307 }
[ba38f72c]308
[8b1e15ac]309 insert_dev_node(tree, dev, fun);
[ba38f72c]310 }
311
[957cfa58]312 fibril_rwlock_write_unlock(&tree->rwlock);
[d347b53]313
[ebcb05a]314 log_msg(LVL_DEBUG, "devman_add_function(fun=\"%s\")", fun->pathname);
[2480e19]315
[ba38f72c]316 devman_receive_match_ids(match_count, &fun->match_ids);
[398c4d7]317
[8b1e15ac]318 if (ftype == fun_inner) {
319 assert(dev != NULL);
[398c4d7]320 /*
[8b1e15ac]321 * Try to find a suitable driver and assign it to the device. We do
322 * not want to block the current fibril that is used for processing
323 * incoming calls: we will launch a separate fibril to handle the
324 * driver assigning. That is because assign_driver can actually include
325 * task spawning which could take some time.
[398c4d7]326 */
[8b1e15ac]327 fid_t assign_fibril = fibril_create(assign_driver_fibril, dev);
328 if (assign_fibril == 0) {
329 /*
330 * Fallback in case we are out of memory.
331 * Probably not needed as we will die soon anyway ;-).
332 */
333 (void) assign_driver_fibril(fun);
334 } else {
335 fibril_add_ready(assign_fibril);
336 }
[398c4d7]337 } else {
[15f3c3f]338 loc_register_tree_function(fun, tree);
[398c4d7]339 }
[8b1e15ac]340
[38b3baf]341 /* Return device handle to parent's driver. */
[ba38f72c]342 async_answer_1(callid, EOK, fun->handle);
[d347b53]343}
344
[1dc4a5e]345static void devman_add_function_to_cat(ipc_callid_t callid, ipc_call_t *call)
[ce89036b]346{
[0b5a4131]347 devman_handle_t handle = IPC_GET_ARG1(*call);
[cc574511]348 category_id_t cat_id;
349 int rc;
[692c40cb]350
[1dc4a5e]351 /* Get category name. */
352 char *cat_name;
353 rc = async_data_write_accept((void **) &cat_name, true,
[38b3baf]354 0, 0, 0, 0);
[692c40cb]355 if (rc != EOK) {
[ffa2c8ef]356 async_answer_0(callid, rc);
[692c40cb]357 return;
358 }
359
[ba38f72c]360 fun_node_t *fun = find_fun_node(&device_tree, handle);
361 if (fun == NULL) {
[ffa2c8ef]362 async_answer_0(callid, ENOENT);
[692c40cb]363 return;
364 }
365
[1dc4a5e]366 rc = loc_category_get_id(cat_name, &cat_id, IPC_FLAG_BLOCKING);
[cc574511]367 if (rc == EOK) {
368 loc_service_add_to_cat(fun->service_id, cat_id);
369 } else {
370 log_msg(LVL_ERROR, "Failed adding function `%s' to category "
[1dc4a5e]371 "`%s'.", fun->pathname, cat_name);
[cc574511]372 }
373
[1dc4a5e]374 log_msg(LVL_NOTE, "Function `%s' added to category `%s'.",
375 fun->pathname, cat_name);
[398c4d7]376
[ffa2c8ef]377 async_answer_0(callid, EOK);
[692c40cb]378}
379
[d0dd7b5]380/** Remove function. */
381static void devman_remove_function(ipc_callid_t callid, ipc_call_t *call)
382{
383 devman_handle_t fun_handle = IPC_GET_ARG1(*call);
384 dev_tree_t *tree = &device_tree;
385 int rc;
386
387 fibril_rwlock_write_lock(&tree->rwlock);
388
389 fun_node_t *fun = find_fun_node_no_lock(&device_tree, fun_handle);
390 if (fun == NULL) {
391 fibril_rwlock_write_unlock(&tree->rwlock);
392 async_answer_0(callid, ENOENT);
393 return;
394 }
395
396 log_msg(LVL_DEBUG, "devman_remove_function(fun='%s')", fun->pathname);
397
398 if (fun->ftype == fun_inner) {
399 /* Handle possible descendants */
400 /* TODO */
401 log_msg(LVL_WARN, "devman_remove_function(): not handling "
402 "descendants\n");
403 } else {
404 /* Unregister from location service */
405 rc = loc_service_unregister(fun->service_id);
406 if (rc != EOK) {
407 log_msg(LVL_ERROR, "Failed unregistering tree service.");
408 fibril_rwlock_write_unlock(&tree->rwlock);
409 async_answer_0(callid, EIO);
410 return;
411 }
412 }
413
414 remove_fun_node(&device_tree, fun);
415 fibril_rwlock_write_unlock(&tree->rwlock);
416 delete_fun_node(fun);
417
418 log_msg(LVL_DEBUG, "devman_remove_function() succeeded.");
419 async_answer_0(callid, EOK);
420}
421
[38b3baf]422/** Initialize driver which has registered itself as running and ready.
423 *
424 * The initialization is done in a separate fibril to avoid deadlocks (if the
425 * driver needed to be served by devman during the driver's initialization).
[3843ecb]426 */
[d347b53]427static int init_running_drv(void *drv)
428{
[38b3baf]429 driver_t *driver = (driver_t *) drv;
430
431 initialize_running_driver(driver, &device_tree);
[ebcb05a]432 log_msg(LVL_DEBUG, "The `%s` driver was successfully initialized.",
[38b3baf]433 driver->name);
[d347b53]434 return 0;
[bda60d9]435}
436
[38b3baf]437/** Function for handling connections from a driver to the device manager. */
[924c75e1]438static void devman_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
[38b3baf]439{
[422722e]440 client_t *client;
441 driver_t *driver;
442
[38b3baf]443 /* Accept the connection. */
[ffa2c8ef]444 async_answer_0(iid, EOK);
[e2b9a993]445
[422722e]446 client = async_get_client_data();
447 if (client == NULL) {
448 log_msg(LVL_ERROR, "Failed to allocate client data.");
[3843ecb]449 return;
[d347b53]450 }
451
[79ae36dd]452 while (true) {
453 ipc_call_t call;
454 ipc_callid_t callid = async_get_call(&call);
455
456 if (!IPC_GET_IMETHOD(call))
457 break;
[e2b9a993]458
[422722e]459 if (IPC_GET_IMETHOD(call) != DEVMAN_DRIVER_REGISTER) {
460 fibril_mutex_lock(&client->mutex);
461 driver = client->driver;
462 fibril_mutex_unlock(&client->mutex);
463 if (driver == NULL) {
464 /* First call must be to DEVMAN_DRIVER_REGISTER */
465 async_answer_0(callid, ENOTSUP);
466 continue;
467 }
468 }
469
[228e490]470 switch (IPC_GET_IMETHOD(call)) {
[422722e]471 case DEVMAN_DRIVER_REGISTER:
472 fibril_mutex_lock(&client->mutex);
473 if (client->driver != NULL) {
474 fibril_mutex_unlock(&client->mutex);
475 async_answer_0(callid, EINVAL);
476 continue;
477 }
478 client->driver = devman_driver_register(callid, &call);
479 fibril_mutex_unlock(&client->mutex);
480 break;
[8b1e15ac]481 case DEVMAN_ADD_FUNCTION:
482 devman_add_function(callid, &call);
[e2b9a993]483 break;
[1dc4a5e]484 case DEVMAN_ADD_DEVICE_TO_CATEGORY:
485 devman_add_function_to_cat(callid, &call);
[692c40cb]486 break;
[d0dd7b5]487 case DEVMAN_REMOVE_FUNCTION:
488 devman_remove_function(callid, &call);
489 break;
[e2b9a993]490 default:
[ffa2c8ef]491 async_answer_0(callid, EINVAL);
[e2b9a993]492 break;
493 }
494 }
495}
496
[38b3baf]497/** Find handle for the device instance identified by the device's path in the
498 * device tree. */
[ba38f72c]499static void devman_function_get_handle(ipc_callid_t iid, ipc_call_t *icall)
[f658458]500{
[38b3baf]501 char *pathname;
502
503 int rc = async_data_write_accept((void **) &pathname, true, 0, 0, 0, 0);
[f658458]504 if (rc != EOK) {
[ffa2c8ef]505 async_answer_0(iid, rc);
[f658458]506 return;
507 }
508
[8b1e15ac]509 fun_node_t *fun = find_fun_node_by_path(&device_tree, pathname);
[f658458]510
511 free(pathname);
512
[ba38f72c]513 if (fun == NULL) {
[ffa2c8ef]514 async_answer_0(iid, ENOENT);
[f658458]515 return;
516 }
[8b1e15ac]517
[ba38f72c]518 async_answer_1(iid, EOK, fun->handle);
[f658458]519}
520
[7beb220]521/** Get device name. */
522static void devman_fun_get_name(ipc_callid_t iid, ipc_call_t *icall)
523{
524 devman_handle_t handle = IPC_GET_ARG1(*icall);
525
526 fun_node_t *fun = find_fun_node(&device_tree, handle);
527 if (fun == NULL) {
528 async_answer_0(iid, ENOMEM);
529 return;
530 }
531
532 ipc_callid_t data_callid;
533 size_t data_len;
534 if (!async_data_read_receive(&data_callid, &data_len)) {
535 async_answer_0(iid, EINVAL);
536 return;
537 }
538
539 void *buffer = malloc(data_len);
540 if (buffer == NULL) {
541 async_answer_0(data_callid, ENOMEM);
542 async_answer_0(iid, ENOMEM);
543 return;
544 }
545
546 size_t sent_length = str_size(fun->name);
547 if (sent_length > data_len) {
548 sent_length = data_len;
549 }
550
551 async_data_read_finalize(data_callid, fun->name, sent_length);
552 async_answer_0(iid, EOK);
553
554 free(buffer);
555}
556
557
558/** Get device path. */
559static void devman_fun_get_path(ipc_callid_t iid, ipc_call_t *icall)
[431d6d6]560{
561 devman_handle_t handle = IPC_GET_ARG1(*icall);
562
563 fun_node_t *fun = find_fun_node(&device_tree, handle);
564 if (fun == NULL) {
565 async_answer_0(iid, ENOMEM);
566 return;
567 }
568
569 ipc_callid_t data_callid;
570 size_t data_len;
571 if (!async_data_read_receive(&data_callid, &data_len)) {
572 async_answer_0(iid, EINVAL);
573 return;
574 }
575
576 void *buffer = malloc(data_len);
577 if (buffer == NULL) {
578 async_answer_0(data_callid, ENOMEM);
579 async_answer_0(iid, ENOMEM);
580 return;
581 }
582
583 size_t sent_length = str_size(fun->pathname);
584 if (sent_length > data_len) {
585 sent_length = data_len;
586 }
587
588 async_data_read_finalize(data_callid, fun->pathname, sent_length);
589 async_answer_0(iid, EOK);
590
591 free(buffer);
592}
593
[7beb220]594static void devman_dev_get_functions(ipc_callid_t iid, ipc_call_t *icall)
595{
596 ipc_callid_t callid;
597 size_t size;
598 size_t act_size;
599 int rc;
600
601 if (!async_data_read_receive(&callid, &size)) {
602 async_answer_0(callid, EREFUSED);
603 async_answer_0(iid, EREFUSED);
604 return;
605 }
606
607 fibril_rwlock_read_lock(&device_tree.rwlock);
608
609 dev_node_t *dev = find_dev_node_no_lock(&device_tree,
610 IPC_GET_ARG1(*icall));
611 if (dev == NULL) {
612 fibril_rwlock_read_unlock(&device_tree.rwlock);
613 async_answer_0(callid, ENOENT);
614 async_answer_0(iid, ENOENT);
615 return;
616 }
617
618 devman_handle_t *hdl_buf = (devman_handle_t *) malloc(size);
619 if (hdl_buf == NULL) {
620 fibril_rwlock_read_unlock(&device_tree.rwlock);
621 async_answer_0(callid, ENOMEM);
622 async_answer_0(iid, ENOMEM);
623 return;
624 }
625
626 rc = dev_get_functions(&device_tree, dev, hdl_buf, size, &act_size);
627 if (rc != EOK) {
628 fibril_rwlock_read_unlock(&device_tree.rwlock);
629 async_answer_0(callid, rc);
630 async_answer_0(iid, rc);
631 return;
632 }
633
634 fibril_rwlock_read_unlock(&device_tree.rwlock);
635
636 sysarg_t retval = async_data_read_finalize(callid, hdl_buf, size);
637 free(hdl_buf);
638
639 async_answer_1(iid, retval, act_size);
640}
641
642
643/** Get handle for child device of a function. */
644static void devman_fun_get_child(ipc_callid_t iid, ipc_call_t *icall)
645{
646 fun_node_t *fun;
647
648 fibril_rwlock_read_lock(&device_tree.rwlock);
649
650 fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));
651 if (fun == NULL) {
652 fibril_rwlock_read_unlock(&device_tree.rwlock);
653 async_answer_0(iid, ENOENT);
654 return;
655 }
656
657 if (fun->child == NULL) {
658 fibril_rwlock_read_unlock(&device_tree.rwlock);
659 async_answer_0(iid, ENOENT);
660 return;
661 }
662
663 async_answer_1(iid, EOK, fun->child->handle);
664
665 fibril_rwlock_read_unlock(&device_tree.rwlock);
666}
667
[e280857]668/** Find handle for the function instance identified by its service ID. */
669static void devman_fun_sid_to_handle(ipc_callid_t iid, ipc_call_t *icall)
670{
671 fun_node_t *fun;
672
673 fun = find_loc_tree_function(&device_tree, IPC_GET_ARG1(*icall));
674
675 if (fun == NULL) {
676 async_answer_0(iid, ENOENT);
677 return;
678 }
679
680 async_answer_1(iid, EOK, fun->handle);
681}
[f658458]682
[38b3baf]683/** Function for handling connections from a client to the device manager. */
[f658458]684static void devman_connection_client(ipc_callid_t iid, ipc_call_t *icall)
685{
[38b3baf]686 /* Accept connection. */
[ffa2c8ef]687 async_answer_0(iid, EOK);
[f658458]688
[79ae36dd]689 while (true) {
[f658458]690 ipc_call_t call;
691 ipc_callid_t callid = async_get_call(&call);
692
[79ae36dd]693 if (!IPC_GET_IMETHOD(call))
694 break;
695
[228e490]696 switch (IPC_GET_IMETHOD(call)) {
[f658458]697 case DEVMAN_DEVICE_GET_HANDLE:
[ba38f72c]698 devman_function_get_handle(callid, &call);
[f658458]699 break;
[7beb220]700 case DEVMAN_DEV_GET_FUNCTIONS:
701 devman_dev_get_functions(callid, &call);
702 break;
703 case DEVMAN_FUN_GET_CHILD:
704 devman_fun_get_child(callid, &call);
705 break;
706 case DEVMAN_FUN_GET_NAME:
707 devman_fun_get_name(callid, &call);
708 break;
709 case DEVMAN_FUN_GET_PATH:
710 devman_fun_get_path(callid, &call);
[431d6d6]711 break;
[e280857]712 case DEVMAN_FUN_SID_TO_HANDLE:
713 devman_fun_sid_to_handle(callid, &call);
714 break;
[f658458]715 default:
[ffa2c8ef]716 async_answer_0(callid, ENOENT);
[f658458]717 }
[c6c389ed]718 }
[f658458]719}
720
[c6c389ed]721static void devman_forward(ipc_callid_t iid, ipc_call_t *icall,
722 bool drv_to_parent)
[38b3baf]723{
[0b5a4131]724 devman_handle_t handle = IPC_GET_ARG2(*icall);
[8b1e15ac]725 devman_handle_t fwd_h;
726 fun_node_t *fun = NULL;
727 dev_node_t *dev = NULL;
[9a66bc2e]728
[8b1e15ac]729 fun = find_fun_node(&device_tree, handle);
730 if (fun == NULL)
731 dev = find_dev_node(&device_tree, handle);
732 else
733 dev = fun->dev;
734
[0418050]735 /*
736 * For a valid function to connect to we need a device. The root
737 * function, for example, has no device and cannot be connected to.
738 * This means @c dev needs to be valid regardless whether we are
739 * connecting to a device or to a function.
740 */
741 if (dev == NULL) {
[9b415c9]742 log_msg(LVL_ERROR, "IPC forwarding failed - no device or "
[ebcb05a]743 "function with handle %" PRIun " was found.", handle);
[ffa2c8ef]744 async_answer_0(iid, ENOENT);
[5cd136ab]745 return;
746 }
[8b1e15ac]747
748 if (fun == NULL && !drv_to_parent) {
[9b415c9]749 log_msg(LVL_ERROR, NAME ": devman_forward error - cannot "
[ebcb05a]750 "connect to handle %" PRIun ", refers to a device.",
[9b415c9]751 handle);
[ffa2c8ef]752 async_answer_0(iid, ENOENT);
[5cd136ab]753 return;
754 }
755
756 driver_t *driver = NULL;
757
758 if (drv_to_parent) {
[8b1e15ac]759 /* Connect to parent function of a device (or device function). */
760 if (dev->pfun->dev != NULL)
761 driver = dev->pfun->dev->drv;
762 fwd_h = dev->pfun->handle;
[c6c389ed]763 } else if (dev->state == DEVICE_USABLE) {
[8b1e15ac]764 /* Connect to the specified function */
[38b3baf]765 driver = dev->drv;
[c6c389ed]766 assert(driver != NULL);
[8b1e15ac]767
768 fwd_h = handle;
[5cd136ab]769 }
770
[c6c389ed]771 if (driver == NULL) {
[9b415c9]772 log_msg(LVL_ERROR, "IPC forwarding refused - " \
[79ae36dd]773 "the device %" PRIun " is not in usable state.", handle);
[ffa2c8ef]774 async_answer_0(iid, ENOENT);
[38b3baf]775 return;
[5cd136ab]776 }
777
[38b3baf]778 int method;
779 if (drv_to_parent)
[5cd136ab]780 method = DRIVER_DRIVER;
[38b3baf]781 else
[5cd136ab]782 method = DRIVER_CLIENT;
783
[79ae36dd]784 if (!driver->sess) {
785 log_msg(LVL_ERROR,
786 "Could not forward to driver `%s'.", driver->name);
[ffa2c8ef]787 async_answer_0(iid, EINVAL);
[9a66bc2e]788 return;
789 }
[c6c389ed]790
[8b1e15ac]791 if (fun != NULL) {
[9b415c9]792 log_msg(LVL_DEBUG,
[ebcb05a]793 "Forwarding request for `%s' function to driver `%s'.",
[9b415c9]794 fun->pathname, driver->name);
[8b1e15ac]795 } else {
[9b415c9]796 log_msg(LVL_DEBUG,
[ebcb05a]797 "Forwarding request for `%s' device to driver `%s'.",
[9b415c9]798 dev->pfun->pathname, driver->name);
[8b1e15ac]799 }
[79ae36dd]800
801 async_exch_t *exch = async_exchange_begin(driver->sess);
802 async_forward_fast(iid, exch, method, fwd_h, 0, IPC_FF_NONE);
803 async_exchange_end(exch);
[5cd136ab]804}
805
[15f3c3f]806/** Function for handling connections from a client forwarded by the location
807 * service to the device manager. */
808static void devman_connection_loc(ipc_callid_t iid, ipc_call_t *icall)
[ce89036b]809{
[15f3c3f]810 service_id_t service_id = IPC_GET_ARG2(*icall);
[ba38f72c]811 fun_node_t *fun;
812 dev_node_t *dev;
[c6c389ed]813
[15f3c3f]814 fun = find_loc_tree_function(&device_tree, service_id);
[ce89036b]815
[ba38f72c]816 if (fun == NULL || fun->dev->drv == NULL) {
[12f9f0d0]817 log_msg(LVL_WARN, "devman_connection_loc(): function "
818 "not found.\n");
[ffa2c8ef]819 async_answer_0(iid, ENOENT);
[ce89036b]820 return;
821 }
822
[ba38f72c]823 dev = fun->dev;
824
[79ae36dd]825 async_exch_t *exch = async_exchange_begin(dev->drv->sess);
826 async_forward_fast(iid, exch, DRIVER_CLIENT, fun->handle, 0,
[38b3baf]827 IPC_FF_NONE);
[79ae36dd]828 async_exchange_end(exch);
829
830 log_msg(LVL_DEBUG,
[15f3c3f]831 "Forwarding loc service request for `%s' function to driver `%s'.",
[9b415c9]832 fun->pathname, dev->drv->name);
[ce89036b]833}
834
[38b3baf]835/** Function for handling connections to device manager. */
[9934f7d]836static void devman_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
[38b3baf]837{
[422722e]838 /* Select port. */
[96b02eb9]839 switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
[924c75e1]840 case DEVMAN_DRIVER:
841 devman_connection_driver(iid, icall);
842 break;
[f658458]843 case DEVMAN_CLIENT:
844 devman_connection_client(iid, icall);
845 break;
[924c75e1]846 case DEVMAN_CONNECT_TO_DEVICE:
[38b3baf]847 /* Connect client to selected device. */
[5cd136ab]848 devman_forward(iid, icall, false);
849 break;
[15f3c3f]850 case DEVMAN_CONNECT_FROM_LOC:
851 /* Someone connected through loc node. */
852 devman_connection_loc(iid, icall);
[47a7174f]853 break;
[5cd136ab]854 case DEVMAN_CONNECT_TO_PARENTS_DEVICE:
[38b3baf]855 /* Connect client to selected device. */
[5cd136ab]856 devman_forward(iid, icall, true);
[38b3baf]857 break;
[924c75e1]858 default:
859 /* No such interface */
[ffa2c8ef]860 async_answer_0(iid, ENOENT);
[924c75e1]861 }
862}
863
[422722e]864static void *devman_client_data_create(void)
865{
866 client_t *client;
867
868 client = calloc(1, sizeof(client_t));
869 if (client == NULL)
870 return NULL;
871
872 fibril_mutex_initialize(&client->mutex);
873 return client;
874}
875
876static void devman_client_data_destroy(void *data)
877{
878 free(data);
879}
880
[38b3baf]881/** Initialize device manager internal structures. */
882static bool devman_init(void)
[e2b9a993]883{
[ebcb05a]884 log_msg(LVL_DEBUG, "devman_init - looking for available drivers.");
[08d9c4e6]885
[38b3baf]886 /* Initialize list of available drivers. */
[0c3666d]887 init_driver_list(&drivers_list);
[c6c389ed]888 if (lookup_available_drivers(&drivers_list,
889 DRIVER_DEFAULT_STORE) == 0) {
[ebcb05a]890 log_msg(LVL_FATAL, "No drivers found.");
[e2b9a993]891 return false;
892 }
[c6c389ed]893
[ebcb05a]894 log_msg(LVL_DEBUG, "devman_init - list of drivers has been initialized.");
[e2b9a993]895
[38b3baf]896 /* Create root device node. */
[e2b9a993]897 if (!init_device_tree(&device_tree, &drivers_list)) {
[9b415c9]898 log_msg(LVL_FATAL, "Failed to initialize device tree.");
[38b3baf]899 return false;
[e2b9a993]900 }
901
[38b3baf]902 /*
[15f3c3f]903 * !!! devman_connection ... as the device manager is not a real loc
[38b3baf]904 * driver (it uses a completely different ipc protocol than an ordinary
[15f3c3f]905 * loc driver) forwarding a connection from client to the devman by
906 * location service would not work.
[38b3baf]907 */
[15f3c3f]908 loc_server_register(NAME, devman_connection);
[ce89036b]909
[e2b9a993]910 return true;
911}
912
913int main(int argc, char *argv[])
914{
915 printf(NAME ": HelenOS Device Manager\n");
916
[cc574511]917 if (log_init(NAME, LVL_WARN) != EOK) {
[9b415c9]918 printf(NAME ": Error initializing logging subsystem.\n");
919 return -1;
920 }
921
[e2b9a993]922 if (!devman_init()) {
[ebcb05a]923 log_msg(LVL_ERROR, "Error while initializing service.");
[e2b9a993]924 return -1;
925 }
926
[422722e]927 /* Set handlers for incoming connections. */
928 async_set_client_data_constructor(devman_client_data_create);
929 async_set_client_data_destructor(devman_client_data_destroy);
[e2b9a993]930 async_set_client_connection(devman_connection);
931
[38b3baf]932 /* Register device manager at naming service. */
[9b415c9]933 if (service_register(SERVICE_DEVMAN) != EOK) {
[ebcb05a]934 log_msg(LVL_ERROR, "Failed registering as a service.");
[e2b9a993]935 return -1;
[9b415c9]936 }
[e2b9a993]937
[9b415c9]938 printf(NAME ": Accepting connections.\n");
[79ae36dd]939 task_retval(0);
[e2b9a993]940 async_manager();
941
[38b3baf]942 /* Never reached. */
[e2b9a993]943 return 0;
944}
[c16cf62]945
946/** @}
[38b3baf]947 */
Note: See TracBrowser for help on using the repository browser.