source: mainline/uspace/srv/devman/main.c@ 9e195e2c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9e195e2c was 431d6d6, checked in by Vojtech Horky <vojtechhorky@…>, 15 years ago

Devman can tell device path by its handle

This patch might go into mainline rightaway.

  • Property mode set to 100644
File size: 20.3 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>
41#include <ipc/ns.h>
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>
[ce89036b]58#include <devmap.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;
[692c40cb]66static class_list_t class_list;
[e2b9a993]67
[38b3baf]68/** Register running driver. */
69static driver_t *devman_driver_register(void)
70{
[729fa2d6]71 ipc_call_t icall;
[c6c389ed]72 ipc_callid_t iid;
[729fa2d6]73 driver_t *driver = NULL;
[c6c389ed]74
[ebcb05a]75 log_msg(LVL_DEBUG, "devman_driver_register");
[729fa2d6]76
[c6c389ed]77 iid = async_get_call(&icall);
[228e490]78 if (IPC_GET_IMETHOD(icall) != DEVMAN_DRIVER_REGISTER) {
[ffa2c8ef]79 async_answer_0(iid, EREFUSED);
[729fa2d6]80 return NULL;
81 }
82
[92413de]83 char *drv_name = NULL;
[729fa2d6]84
[38b3baf]85 /* Get driver name. */
86 int rc = async_data_write_accept((void **) &drv_name, true, 0, 0, 0, 0);
[729fa2d6]87 if (rc != EOK) {
[ffa2c8ef]88 async_answer_0(iid, rc);
[729fa2d6]89 return NULL;
90 }
[c6c389ed]91
[ebcb05a]92 log_msg(LVL_DEBUG, "The `%s' driver is trying to register.",
[38b3baf]93 drv_name);
[729fa2d6]94
[38b3baf]95 /* Find driver structure. */
[729fa2d6]96 driver = find_driver(&drivers_list, drv_name);
[c6c389ed]97 if (driver == NULL) {
[ebcb05a]98 log_msg(LVL_ERROR, "No driver named `%s' was found.", drv_name);
[04c7003f]99 free(drv_name);
100 drv_name = NULL;
[ffa2c8ef]101 async_answer_0(iid, ENOENT);
[729fa2d6]102 return NULL;
103 }
104
[04c7003f]105 free(drv_name);
106 drv_name = NULL;
107
[3ad7b1c]108 fibril_mutex_lock(&driver->driver_mutex);
109
110 if (driver->phone >= 0) {
111 /* We already have a connection to the driver. */
112 log_msg(LVL_ERROR, "Driver '%s' already started.\n",
113 driver->name);
114 fibril_mutex_unlock(&driver->driver_mutex);
115 async_answer_0(iid, EEXISTS);
116 return NULL;
117 }
118
119 switch (driver->state) {
120 case DRIVER_NOT_STARTED:
121 /* Somebody started the driver manually. */
122 log_msg(LVL_NOTE, "Driver '%s' started manually.\n",
123 driver->name);
124 driver->state = DRIVER_STARTING;
125 break;
126 case DRIVER_STARTING:
127 /* The expected case */
128 break;
129 case DRIVER_RUNNING:
130 /* Should not happen since we do not have a connected phone */
131 assert(false);
132 }
133
[38b3baf]134 /* Create connection to the driver. */
[ebcb05a]135 log_msg(LVL_DEBUG, "Creating connection to the `%s' driver.",
[9b415c9]136 driver->name);
[729fa2d6]137 ipc_call_t call;
[38b3baf]138 ipc_callid_t callid = async_get_call(&call);
[228e490]139 if (IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) {
[3ad7b1c]140 fibril_mutex_unlock(&driver->driver_mutex);
[ffa2c8ef]141 async_answer_0(callid, ENOTSUP);
142 async_answer_0(iid, ENOTSUP);
[729fa2d6]143 return NULL;
144 }
145
[38b3baf]146 /* Remember driver's phone. */
[3ad7b1c]147 driver->phone = IPC_GET_ARG5(call);
148
149 fibril_mutex_unlock(&driver->driver_mutex);
[729fa2d6]150
[9b415c9]151 log_msg(LVL_NOTE,
[ebcb05a]152 "The `%s' driver was successfully registered as running.",
[38b3baf]153 driver->name);
[729fa2d6]154
[ffa2c8ef]155 async_answer_0(callid, EOK);
156 async_answer_0(iid, EOK);
[729fa2d6]157
158 return driver;
159}
[e2b9a993]160
[38b3baf]161/** Receive device match ID from the device's parent driver and add it to the
162 * list of devices match ids.
163 *
164 * @param match_ids The list of the device's match ids.
165 * @return Zero on success, negative error code otherwise.
[3843ecb]166 */
[38b3baf]167static int devman_receive_match_id(match_id_list_t *match_ids)
168{
[66babbd]169 match_id_t *match_id = create_match_id();
170 ipc_callid_t callid;
171 ipc_call_t call;
172 int rc = 0;
173
174 callid = async_get_call(&call);
[228e490]175 if (DEVMAN_ADD_MATCH_ID != IPC_GET_IMETHOD(call)) {
[9b415c9]176 log_msg(LVL_ERROR,
[ebcb05a]177 "Invalid protocol when trying to receive match id.");
[ffa2c8ef]178 async_answer_0(callid, EINVAL);
[66babbd]179 delete_match_id(match_id);
180 return EINVAL;
181 }
182
[c6c389ed]183 if (match_id == NULL) {
[ebcb05a]184 log_msg(LVL_ERROR, "Failed to allocate match id.");
[ffa2c8ef]185 async_answer_0(callid, ENOMEM);
[66babbd]186 return ENOMEM;
187 }
188
[ffa2c8ef]189 async_answer_0(callid, EOK);
[2480e19]190
[66babbd]191 match_id->score = IPC_GET_ARG1(call);
192
[c47e1a8]193 char *match_id_str;
[38b3baf]194 rc = async_data_write_accept((void **) &match_id_str, true, 0, 0, 0, 0);
[c47e1a8]195 match_id->id = match_id_str;
[c6c389ed]196 if (rc != EOK) {
[66babbd]197 delete_match_id(match_id);
[ebcb05a]198 log_msg(LVL_ERROR, "Failed to receive match id string: %s.",
[9b415c9]199 str_error(rc));
[66babbd]200 return rc;
201 }
202
203 list_append(&match_id->link, &match_ids->ids);
[a087f2e]204
[ebcb05a]205 log_msg(LVL_DEBUG, "Received match id `%s', score %d.",
[38b3baf]206 match_id->id, match_id->score);
[66babbd]207 return rc;
208}
209
[38b3baf]210/** Receive device match IDs from the device's parent driver and add them to the
211 * list of devices match ids.
212 *
213 * @param match_count The number of device's match ids to be received.
214 * @param match_ids The list of the device's match ids.
215 * @return Zero on success, negative error code otherwise.
[3843ecb]216 */
[96b02eb9]217static int devman_receive_match_ids(sysarg_t match_count,
[c6c389ed]218 match_id_list_t *match_ids)
[38b3baf]219{
[66babbd]220 int ret = EOK;
[5cd136ab]221 size_t i;
[38b3baf]222
[66babbd]223 for (i = 0; i < match_count; i++) {
[38b3baf]224 if (EOK != (ret = devman_receive_match_id(match_ids)))
[66babbd]225 return ret;
226 }
227 return ret;
228}
229
[398c4d7]230static int assign_driver_fibril(void *arg)
231{
[ba38f72c]232 dev_node_t *dev_node = (dev_node_t *) arg;
233 assign_driver(dev_node, &drivers_list, &device_tree);
[398c4d7]234 return EOK;
235}
236
[8b1e15ac]237/** Handle function registration.
[38b3baf]238 *
[3843ecb]239 * Child devices are registered by their parent's device driver.
240 */
[8b1e15ac]241static void devman_add_function(ipc_callid_t callid, ipc_call_t *call)
[bda60d9]242{
[8b1e15ac]243 fun_type_t ftype = (fun_type_t) IPC_GET_ARG1(*call);
244 devman_handle_t dev_handle = IPC_GET_ARG2(*call);
245 sysarg_t match_count = IPC_GET_ARG3(*call);
[957cfa58]246 dev_tree_t *tree = &device_tree;
[66babbd]247
[957cfa58]248 fibril_rwlock_write_lock(&tree->rwlock);
[8b1e15ac]249
250 dev_node_t *dev = NULL;
251 dev_node_t *pdev = find_dev_node_no_lock(&device_tree, dev_handle);
[d347b53]252
[ba38f72c]253 if (pdev == NULL) {
[957cfa58]254 fibril_rwlock_write_unlock(&tree->rwlock);
[ffa2c8ef]255 async_answer_0(callid, ENOENT);
[d347b53]256 return;
[c6c389ed]257 }
[d347b53]258
[8b1e15ac]259 if (ftype != fun_inner && ftype != fun_exposed) {
260 /* Unknown function type */
[9b415c9]261 log_msg(LVL_ERROR,
[ebcb05a]262 "Unknown function type %d provided by driver.",
[9b415c9]263 (int) ftype);
[8b1e15ac]264
265 fibril_rwlock_write_unlock(&tree->rwlock);
266 async_answer_0(callid, EINVAL);
267 return;
268 }
269
[ba38f72c]270 char *fun_name = NULL;
271 int rc = async_data_write_accept((void **)&fun_name, true, 0, 0, 0, 0);
[c6c389ed]272 if (rc != EOK) {
[957cfa58]273 fibril_rwlock_write_unlock(&tree->rwlock);
[ffa2c8ef]274 async_answer_0(callid, rc);
[d347b53]275 return;
276 }
277
[0876062]278 /* Check that function with same name is not there already. */
279 if (find_fun_node_in_device(pdev, fun_name) != NULL) {
280 fibril_rwlock_write_unlock(&tree->rwlock);
281 async_answer_0(callid, EEXISTS);
282 printf(NAME ": Warning, driver tried to register `%s' twice.\n",
283 fun_name);
284 free(fun_name);
285 return;
286 }
287
[ba38f72c]288 fun_node_t *fun = create_fun_node();
289 if (!insert_fun_node(&device_tree, fun, fun_name, pdev)) {
[957cfa58]290 fibril_rwlock_write_unlock(&tree->rwlock);
[ba38f72c]291 delete_fun_node(fun);
[ffa2c8ef]292 async_answer_0(callid, ENOMEM);
[d347b53]293 return;
[c6c389ed]294 }
295
[8b1e15ac]296 if (ftype == fun_inner) {
297 dev = create_dev_node();
298 if (dev == NULL) {
299 fibril_rwlock_write_unlock(&tree->rwlock);
300 delete_fun_node(fun);
301 async_answer_0(callid, ENOMEM);
302 return;
303 }
[ba38f72c]304
[8b1e15ac]305 insert_dev_node(tree, dev, fun);
[ba38f72c]306 }
307
[957cfa58]308 fibril_rwlock_write_unlock(&tree->rwlock);
[d347b53]309
[ebcb05a]310 log_msg(LVL_DEBUG, "devman_add_function(fun=\"%s\")", fun->pathname);
[2480e19]311
[ba38f72c]312 devman_receive_match_ids(match_count, &fun->match_ids);
[398c4d7]313
[8b1e15ac]314 if (ftype == fun_inner) {
315 assert(dev != NULL);
[398c4d7]316 /*
[8b1e15ac]317 * Try to find a suitable driver and assign it to the device. We do
318 * not want to block the current fibril that is used for processing
319 * incoming calls: we will launch a separate fibril to handle the
320 * driver assigning. That is because assign_driver can actually include
321 * task spawning which could take some time.
[398c4d7]322 */
[8b1e15ac]323 fid_t assign_fibril = fibril_create(assign_driver_fibril, dev);
324 if (assign_fibril == 0) {
325 /*
326 * Fallback in case we are out of memory.
327 * Probably not needed as we will die soon anyway ;-).
328 */
329 (void) assign_driver_fibril(fun);
330 } else {
331 fibril_add_ready(assign_fibril);
332 }
[398c4d7]333 } else {
[8b1e15ac]334 devmap_register_tree_function(fun, tree);
[398c4d7]335 }
[8b1e15ac]336
[38b3baf]337 /* Return device handle to parent's driver. */
[ba38f72c]338 async_answer_1(callid, EOK, fun->handle);
[d347b53]339}
340
[ce89036b]341static void devmap_register_class_dev(dev_class_info_t *cli)
342{
[38b3baf]343 /* Create devmap path and name for the device. */
[ce89036b]344 char *devmap_pathname = NULL;
[c6c389ed]345
[38b3baf]346 asprintf(&devmap_pathname, "%s/%s%c%s", DEVMAP_CLASS_NAMESPACE,
347 cli->dev_class->name, DEVMAP_SEPARATOR, cli->dev_name);
[c6c389ed]348 if (devmap_pathname == NULL)
[ce89036b]349 return;
350
[38b3baf]351 /*
352 * Register the device by the device mapper and remember its devmap
353 * handle.
354 */
[6e50466]355 devmap_device_register_with_iface(devmap_pathname,
356 &cli->devmap_handle, DEVMAN_CONNECT_FROM_DEVMAP);
[ce89036b]357
[38b3baf]358 /*
359 * Add device to the hash map of class devices registered by device
360 * mapper.
361 */
[ba38f72c]362 class_add_devmap_function(&class_list, cli);
[a32defa]363
[38b3baf]364 free(devmap_pathname);
[ce89036b]365}
366
[ba38f72c]367static void devman_add_function_to_class(ipc_callid_t callid, ipc_call_t *call)
[ce89036b]368{
[0b5a4131]369 devman_handle_t handle = IPC_GET_ARG1(*call);
[692c40cb]370
[38b3baf]371 /* Get class name. */
[692c40cb]372 char *class_name;
[38b3baf]373 int rc = async_data_write_accept((void **) &class_name, true,
374 0, 0, 0, 0);
[692c40cb]375 if (rc != EOK) {
[ffa2c8ef]376 async_answer_0(callid, rc);
[692c40cb]377 return;
378 }
379
[ba38f72c]380 fun_node_t *fun = find_fun_node(&device_tree, handle);
381 if (fun == NULL) {
[ffa2c8ef]382 async_answer_0(callid, ENOENT);
[692c40cb]383 return;
384 }
385
386 dev_class_t *cl = get_dev_class(&class_list, class_name);
[ba38f72c]387 dev_class_info_t *class_info = add_function_to_class(fun, cl, NULL);
[692c40cb]388
[38b3baf]389 /* Register the device's class alias by devmapper. */
[ce89036b]390 devmap_register_class_dev(class_info);
[692c40cb]391
[ebcb05a]392 log_msg(LVL_NOTE, "Function `%s' added to class `%s' as `%s'.",
[9b415c9]393 fun->pathname, class_name, class_info->dev_name);
[398c4d7]394
[ffa2c8ef]395 async_answer_0(callid, EOK);
[692c40cb]396}
397
[38b3baf]398/** Initialize driver which has registered itself as running and ready.
399 *
400 * The initialization is done in a separate fibril to avoid deadlocks (if the
401 * driver needed to be served by devman during the driver's initialization).
[3843ecb]402 */
[d347b53]403static int init_running_drv(void *drv)
404{
[38b3baf]405 driver_t *driver = (driver_t *) drv;
406
407 initialize_running_driver(driver, &device_tree);
[ebcb05a]408 log_msg(LVL_DEBUG, "The `%s` driver was successfully initialized.",
[38b3baf]409 driver->name);
[d347b53]410 return 0;
[bda60d9]411}
412
[38b3baf]413/** Function for handling connections from a driver to the device manager. */
[924c75e1]414static void devman_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
[38b3baf]415{
416 /* Accept the connection. */
[ffa2c8ef]417 async_answer_0(iid, EOK);
[e2b9a993]418
[729fa2d6]419 driver_t *driver = devman_driver_register();
[c6c389ed]420 if (driver == NULL)
[729fa2d6]421 return;
[d347b53]422
[38b3baf]423 /*
424 * Initialize the driver as running (e.g. pass assigned devices to it)
425 * in a separate fibril; the separate fibril is used to enable the
426 * driver to use devman service during the driver's initialization.
427 */
[d347b53]428 fid_t fid = fibril_create(init_running_drv, driver);
429 if (fid == 0) {
[9b415c9]430 log_msg(LVL_ERROR, "Failed to create initialization fibril " \
[ebcb05a]431 "for driver `%s'.", driver->name);
[3843ecb]432 return;
[d347b53]433 }
434 fibril_add_ready(fid);
435
[729fa2d6]436 ipc_callid_t callid;
437 ipc_call_t call;
438 bool cont = true;
439 while (cont) {
[e2b9a993]440 callid = async_get_call(&call);
441
[228e490]442 switch (IPC_GET_IMETHOD(call)) {
[729fa2d6]443 case IPC_M_PHONE_HUNGUP:
444 cont = false;
445 continue;
[8b1e15ac]446 case DEVMAN_ADD_FUNCTION:
447 devman_add_function(callid, &call);
[e2b9a993]448 break;
[692c40cb]449 case DEVMAN_ADD_DEVICE_TO_CLASS:
[ba38f72c]450 devman_add_function_to_class(callid, &call);
[692c40cb]451 break;
[e2b9a993]452 default:
[ffa2c8ef]453 async_answer_0(callid, EINVAL);
[e2b9a993]454 break;
455 }
456 }
457}
458
[38b3baf]459/** Find handle for the device instance identified by the device's path in the
460 * device tree. */
[ba38f72c]461static void devman_function_get_handle(ipc_callid_t iid, ipc_call_t *icall)
[f658458]462{
[38b3baf]463 char *pathname;
464
465 int rc = async_data_write_accept((void **) &pathname, true, 0, 0, 0, 0);
[f658458]466 if (rc != EOK) {
[ffa2c8ef]467 async_answer_0(iid, rc);
[f658458]468 return;
469 }
470
[8b1e15ac]471 fun_node_t *fun = find_fun_node_by_path(&device_tree, pathname);
[f658458]472
473 free(pathname);
474
[ba38f72c]475 if (fun == NULL) {
[ffa2c8ef]476 async_answer_0(iid, ENOENT);
[f658458]477 return;
478 }
[8b1e15ac]479
[ba38f72c]480 async_answer_1(iid, EOK, fun->handle);
[f658458]481}
482
[fc8c2b6]483/** Find handle for the device instance identified by device class name. */
484static void devman_function_get_handle_by_class(ipc_callid_t iid,
485 ipc_call_t *icall)
486{
487 char *classname;
488 char *devname;
489
490 int rc = async_data_write_accept((void **) &classname, true, 0, 0, 0, 0);
491 if (rc != EOK) {
492 async_answer_0(iid, rc);
493 return;
494 }
495 rc = async_data_write_accept((void **) &devname, true, 0, 0, 0, 0);
496 if (rc != EOK) {
497 free(classname);
498 async_answer_0(iid, rc);
499 return;
500 }
501
502
503 fun_node_t *fun = find_fun_node_by_class(&class_list,
504 classname, devname);
505
506 free(classname);
507 free(devname);
508
509 if (fun == NULL) {
510 async_answer_0(iid, ENOENT);
511 return;
512 }
513
514 async_answer_1(iid, EOK, fun->handle);
515}
516
[431d6d6]517/** Find device path by its handle. */
518static void devman_get_device_path_by_handle(ipc_callid_t iid,
519 ipc_call_t *icall)
520{
521 devman_handle_t handle = IPC_GET_ARG1(*icall);
522
523 fun_node_t *fun = find_fun_node(&device_tree, handle);
524 if (fun == NULL) {
525 async_answer_0(iid, ENOMEM);
526 return;
527 }
528
529 ipc_callid_t data_callid;
530 size_t data_len;
531 if (!async_data_read_receive(&data_callid, &data_len)) {
532 async_answer_0(iid, EINVAL);
533 return;
534 }
535
536 void *buffer = malloc(data_len);
537 if (buffer == NULL) {
538 async_answer_0(data_callid, ENOMEM);
539 async_answer_0(iid, ENOMEM);
540 return;
541 }
542
543 size_t sent_length = str_size(fun->pathname);
544 if (sent_length > data_len) {
545 sent_length = data_len;
546 }
547
548 async_data_read_finalize(data_callid, fun->pathname, sent_length);
549 async_answer_0(iid, EOK);
550
551 free(buffer);
552}
553
[f658458]554
[38b3baf]555/** Function for handling connections from a client to the device manager. */
[f658458]556static void devman_connection_client(ipc_callid_t iid, ipc_call_t *icall)
557{
[38b3baf]558 /* Accept connection. */
[ffa2c8ef]559 async_answer_0(iid, EOK);
[f658458]560
561 bool cont = true;
562 while (cont) {
563 ipc_call_t call;
564 ipc_callid_t callid = async_get_call(&call);
565
[228e490]566 switch (IPC_GET_IMETHOD(call)) {
[f658458]567 case IPC_M_PHONE_HUNGUP:
568 cont = false;
569 continue;
570 case DEVMAN_DEVICE_GET_HANDLE:
[ba38f72c]571 devman_function_get_handle(callid, &call);
[f658458]572 break;
[fc8c2b6]573 case DEVMAN_DEVICE_GET_HANDLE_BY_CLASS:
574 devman_function_get_handle_by_class(callid, &call);
575 break;
[431d6d6]576 case DEVMAN_DEVICE_GET_DEVICE_PATH:
577 devman_get_device_path_by_handle(callid, &call);
578 break;
[f658458]579 default:
[ffa2c8ef]580 async_answer_0(callid, ENOENT);
[f658458]581 }
[c6c389ed]582 }
[f658458]583}
584
[c6c389ed]585static void devman_forward(ipc_callid_t iid, ipc_call_t *icall,
586 bool drv_to_parent)
[38b3baf]587{
[0b5a4131]588 devman_handle_t handle = IPC_GET_ARG2(*icall);
[8b1e15ac]589 devman_handle_t fwd_h;
590 fun_node_t *fun = NULL;
591 dev_node_t *dev = NULL;
[9a66bc2e]592
[8b1e15ac]593 fun = find_fun_node(&device_tree, handle);
594 if (fun == NULL)
595 dev = find_dev_node(&device_tree, handle);
596 else
597 dev = fun->dev;
598
[0418050]599 /*
600 * For a valid function to connect to we need a device. The root
601 * function, for example, has no device and cannot be connected to.
602 * This means @c dev needs to be valid regardless whether we are
603 * connecting to a device or to a function.
604 */
605 if (dev == NULL) {
[9b415c9]606 log_msg(LVL_ERROR, "IPC forwarding failed - no device or "
[ebcb05a]607 "function with handle %" PRIun " was found.", handle);
[ffa2c8ef]608 async_answer_0(iid, ENOENT);
[5cd136ab]609 return;
610 }
[8b1e15ac]611
612 if (fun == NULL && !drv_to_parent) {
[9b415c9]613 log_msg(LVL_ERROR, NAME ": devman_forward error - cannot "
[ebcb05a]614 "connect to handle %" PRIun ", refers to a device.",
[9b415c9]615 handle);
[ffa2c8ef]616 async_answer_0(iid, ENOENT);
[5cd136ab]617 return;
618 }
619
620 driver_t *driver = NULL;
621
622 if (drv_to_parent) {
[8b1e15ac]623 /* Connect to parent function of a device (or device function). */
624 if (dev->pfun->dev != NULL)
625 driver = dev->pfun->dev->drv;
626 fwd_h = dev->pfun->handle;
[c6c389ed]627 } else if (dev->state == DEVICE_USABLE) {
[8b1e15ac]628 /* Connect to the specified function */
[38b3baf]629 driver = dev->drv;
[c6c389ed]630 assert(driver != NULL);
[8b1e15ac]631
632 fwd_h = handle;
[5cd136ab]633 }
634
[c6c389ed]635 if (driver == NULL) {
[9b415c9]636 log_msg(LVL_ERROR, "IPC forwarding refused - " \
[969585f]637 "the device %" PRIun "(%s) is not in usable state.",
[eb1a2f4]638 handle, dev->pfun->pathname);
[ffa2c8ef]639 async_answer_0(iid, ENOENT);
[38b3baf]640 return;
[5cd136ab]641 }
642
[38b3baf]643 int method;
644 if (drv_to_parent)
[5cd136ab]645 method = DRIVER_DRIVER;
[38b3baf]646 else
[5cd136ab]647 method = DRIVER_CLIENT;
648
[3ad7b1c]649 if (driver->phone < 0) {
[9b415c9]650 log_msg(LVL_ERROR,
[ebcb05a]651 "Could not forward to driver `%s' (phone is %d).",
[9b415c9]652 driver->name, (int) driver->phone);
[ffa2c8ef]653 async_answer_0(iid, EINVAL);
[9a66bc2e]654 return;
655 }
[c6c389ed]656
[8b1e15ac]657 if (fun != NULL) {
[9b415c9]658 log_msg(LVL_DEBUG,
[ebcb05a]659 "Forwarding request for `%s' function to driver `%s'.",
[9b415c9]660 fun->pathname, driver->name);
[8b1e15ac]661 } else {
[9b415c9]662 log_msg(LVL_DEBUG,
[ebcb05a]663 "Forwarding request for `%s' device to driver `%s'.",
[9b415c9]664 dev->pfun->pathname, driver->name);
[8b1e15ac]665 }
666
667 async_forward_fast(iid, driver->phone, method, fwd_h, 0, IPC_FF_NONE);
[5cd136ab]668}
669
[38b3baf]670/** Function for handling connections from a client forwarded by the device
671 * mapper to the device manager. */
[ce89036b]672static void devman_connection_devmapper(ipc_callid_t iid, ipc_call_t *icall)
673{
[47a7174f]674 devmap_handle_t devmap_handle = IPC_GET_ARG2(*icall);
[ba38f72c]675 fun_node_t *fun;
676 dev_node_t *dev;
[c6c389ed]677
[ba38f72c]678 fun = find_devmap_tree_function(&device_tree, devmap_handle);
679 if (fun == NULL)
680 fun = find_devmap_class_function(&class_list, devmap_handle);
[ce89036b]681
[ba38f72c]682 if (fun == NULL || fun->dev->drv == NULL) {
[ffa2c8ef]683 async_answer_0(iid, ENOENT);
[ce89036b]684 return;
685 }
686
[ba38f72c]687 dev = fun->dev;
688
[3ad7b1c]689 if (dev->state != DEVICE_USABLE || dev->drv->phone < 0) {
[ffa2c8ef]690 async_answer_0(iid, EINVAL);
[ce89036b]691 return;
692 }
693
[b927375]694 async_forward_fast(iid, dev->drv->phone, DRIVER_CLIENT, fun->handle, 0,
[38b3baf]695 IPC_FF_NONE);
[9b415c9]696 log_msg(LVL_DEBUG,
[ebcb05a]697 "Forwarding devmapper request for `%s' function to driver `%s'.",
[9b415c9]698 fun->pathname, dev->drv->name);
[ce89036b]699}
700
[38b3baf]701/** Function for handling connections to device manager. */
[924c75e1]702static void devman_connection(ipc_callid_t iid, ipc_call_t *icall)
[38b3baf]703{
704 /* Select interface. */
[96b02eb9]705 switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
[924c75e1]706 case DEVMAN_DRIVER:
707 devman_connection_driver(iid, icall);
708 break;
[f658458]709 case DEVMAN_CLIENT:
710 devman_connection_client(iid, icall);
711 break;
[924c75e1]712 case DEVMAN_CONNECT_TO_DEVICE:
[38b3baf]713 /* Connect client to selected device. */
[5cd136ab]714 devman_forward(iid, icall, false);
715 break;
[47a7174f]716 case DEVMAN_CONNECT_FROM_DEVMAP:
717 /* Someone connected through devmap node. */
718 devman_connection_devmapper(iid, icall);
719 break;
[5cd136ab]720 case DEVMAN_CONNECT_TO_PARENTS_DEVICE:
[38b3baf]721 /* Connect client to selected device. */
[5cd136ab]722 devman_forward(iid, icall, true);
[38b3baf]723 break;
[924c75e1]724 default:
725 /* No such interface */
[ffa2c8ef]726 async_answer_0(iid, ENOENT);
[924c75e1]727 }
728}
729
[38b3baf]730/** Initialize device manager internal structures. */
731static bool devman_init(void)
[e2b9a993]732{
[ebcb05a]733 log_msg(LVL_DEBUG, "devman_init - looking for available drivers.");
[08d9c4e6]734
[38b3baf]735 /* Initialize list of available drivers. */
[0c3666d]736 init_driver_list(&drivers_list);
[c6c389ed]737 if (lookup_available_drivers(&drivers_list,
738 DRIVER_DEFAULT_STORE) == 0) {
[ebcb05a]739 log_msg(LVL_FATAL, "No drivers found.");
[e2b9a993]740 return false;
741 }
[c6c389ed]742
[ebcb05a]743 log_msg(LVL_DEBUG, "devman_init - list of drivers has been initialized.");
[e2b9a993]744
[38b3baf]745 /* Create root device node. */
[e2b9a993]746 if (!init_device_tree(&device_tree, &drivers_list)) {
[9b415c9]747 log_msg(LVL_FATAL, "Failed to initialize device tree.");
[38b3baf]748 return false;
[e2b9a993]749 }
750
[692c40cb]751 init_class_list(&class_list);
752
[38b3baf]753 /*
754 * !!! devman_connection ... as the device manager is not a real devmap
755 * driver (it uses a completely different ipc protocol than an ordinary
756 * devmap driver) forwarding a connection from client to the devman by
757 * devmapper would not work.
758 */
759 devmap_driver_register(NAME, devman_connection);
[ce89036b]760
[e2b9a993]761 return true;
762}
763
764int main(int argc, char *argv[])
765{
766 printf(NAME ": HelenOS Device Manager\n");
767
[9b415c9]768 if (log_init(NAME, LVL_ERROR) != EOK) {
769 printf(NAME ": Error initializing logging subsystem.\n");
770 return -1;
771 }
772
[e2b9a993]773 if (!devman_init()) {
[ebcb05a]774 log_msg(LVL_ERROR, "Error while initializing service.");
[e2b9a993]775 return -1;
776 }
777
[38b3baf]778 /* Set a handler of incomming connections. */
[e2b9a993]779 async_set_client_connection(devman_connection);
780
[38b3baf]781 /* Register device manager at naming service. */
[9b415c9]782 if (service_register(SERVICE_DEVMAN) != EOK) {
[ebcb05a]783 log_msg(LVL_ERROR, "Failed registering as a service.");
[e2b9a993]784 return -1;
[9b415c9]785 }
[e2b9a993]786
[9b415c9]787 printf(NAME ": Accepting connections.\n");
[e2b9a993]788 async_manager();
789
[38b3baf]790 /* Never reached. */
[e2b9a993]791 return 0;
792}
[c16cf62]793
794/** @}
[38b3baf]795 */
Note: See TracBrowser for help on using the repository browser.