source: mainline/uspace/srv/devman/main.c@ fb4c877

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

Do not trust driver to remove all functions.

  • Property mode set to 100644
File size: 32.1 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);
[c1a0488]236
237 /* Delete one reference we got from the caller. */
238 dev_del_ref(dev_node);
[398c4d7]239 return EOK;
240}
241
[1a5b252]242static int online_function(fun_node_t *fun)
243{
244 dev_node_t *dev;
245
246 fibril_rwlock_write_lock(&device_tree.rwlock);
[58cbb0c8]247
[c1a0488]248 if (fun->state == FUN_ON_LINE) {
249 fibril_rwlock_write_unlock(&device_tree.rwlock);
250 log_msg(LVL_WARN, "Function %s is already on line.",
251 fun->pathname);
252 return EOK;
253 }
254
[1a5b252]255 if (fun->ftype == fun_inner) {
256 dev = create_dev_node();
257 if (dev == NULL) {
258 fibril_rwlock_write_unlock(&device_tree.rwlock);
259 return ENOMEM;
260 }
261
262 insert_dev_node(&device_tree, dev, fun);
[58cbb0c8]263 dev_add_ref(dev);
[1a5b252]264 }
265
266 log_msg(LVL_DEBUG, "devman_add_function(fun=\"%s\")", fun->pathname);
267
268 if (fun->ftype == fun_inner) {
269 dev = fun->child;
270 assert(dev != NULL);
271
[c1a0488]272 /* Give one reference over to assign_driver_fibril(). */
273 dev_add_ref(dev);
[1a5b252]274 /*
275 * Try to find a suitable driver and assign it to the device. We do
276 * not want to block the current fibril that is used for processing
277 * incoming calls: we will launch a separate fibril to handle the
278 * driver assigning. That is because assign_driver can actually include
279 * task spawning which could take some time.
280 */
281 fid_t assign_fibril = fibril_create(assign_driver_fibril, dev);
282 if (assign_fibril == 0) {
[c1a0488]283 log_msg(LVL_ERROR, "Failed to create fibril for "
284 "assigning driver.");
285 /* XXX Cleanup */
286 fibril_rwlock_write_unlock(&device_tree.rwlock);
287 return ENOMEM;
[1a5b252]288 }
[c1a0488]289 fibril_add_ready(assign_fibril);
[1a5b252]290 } else {
291 loc_register_tree_function(fun, &device_tree);
292 }
293
[58cbb0c8]294 fibril_rwlock_write_unlock(&device_tree.rwlock);
295
[1a5b252]296 return EOK;
297}
298
299static int offline_function(fun_node_t *fun)
300{
301 int rc;
302
[58cbb0c8]303 fibril_rwlock_write_lock(&device_tree.rwlock);
304
[c1a0488]305 if (fun->state == FUN_OFF_LINE) {
306 fibril_rwlock_write_unlock(&device_tree.rwlock);
307 log_msg(LVL_WARN, "Function %s is already off line.",
308 fun->pathname);
309 return EOK;
310 }
311
[1a5b252]312 if (fun->ftype == fun_inner) {
[224c0e7]313 log_msg(LVL_DEBUG, "Offlining inner function %s.",
314 fun->pathname);
315
[1a5b252]316 if (fun->child != NULL) {
317 dev_node_t *dev = fun->child;
318
[58cbb0c8]319 dev_add_ref(dev);
320 fibril_rwlock_write_unlock(&device_tree.rwlock);
321
322 rc = driver_dev_remove(&device_tree, dev);
[1a5b252]323 if (rc != EOK) {
[58cbb0c8]324 dev_del_ref(dev);
[1a5b252]325 return ENOTSUP;
326 }
[58cbb0c8]327
[fb4c877]328 /* Verify that driver removed all functions */
329 fibril_rwlock_read_lock(&device_tree.rwlock);
330 if (!list_empty(&dev->functions)) {
331 fibril_rwlock_read_unlock(&device_tree.rwlock);
332 return EIO;
333 }
334 fibril_rwlock_read_unlock(&device_tree.rwlock);
335
[58cbb0c8]336 detach_driver(&device_tree, dev);
337
[1a5b252]338 fibril_rwlock_write_lock(&device_tree.rwlock);
339 remove_dev_node(&device_tree, dev);
[58cbb0c8]340
341 /* Delete ref created when node was inserted */
342 dev_del_ref(dev);
343 /* Delete ref created by dev_add_ref(dev) above */
344 dev_del_ref(dev);
[1a5b252]345 }
346 } else {
347 /* Unregister from location service */
348 rc = loc_service_unregister(fun->service_id);
349 if (rc != EOK) {
[58cbb0c8]350 fibril_rwlock_write_unlock(&device_tree.rwlock);
[1a5b252]351 log_msg(LVL_ERROR, "Failed unregistering tree service.");
352 return EIO;
353 }
354
355 fun->service_id = 0;
356 }
357
[c1a0488]358 fun->state = FUN_OFF_LINE;
[58cbb0c8]359 fibril_rwlock_write_unlock(&device_tree.rwlock);
360
[1a5b252]361 return EOK;
362}
363
[8b1e15ac]364/** Handle function registration.
[38b3baf]365 *
[3843ecb]366 * Child devices are registered by their parent's device driver.
367 */
[8b1e15ac]368static void devman_add_function(ipc_callid_t callid, ipc_call_t *call)
[bda60d9]369{
[8b1e15ac]370 fun_type_t ftype = (fun_type_t) IPC_GET_ARG1(*call);
371 devman_handle_t dev_handle = IPC_GET_ARG2(*call);
372 sysarg_t match_count = IPC_GET_ARG3(*call);
[957cfa58]373 dev_tree_t *tree = &device_tree;
[66babbd]374
[58cbb0c8]375 dev_node_t *pdev = find_dev_node(&device_tree, dev_handle);
[ba38f72c]376 if (pdev == NULL) {
[ffa2c8ef]377 async_answer_0(callid, ENOENT);
[d347b53]378 return;
[c6c389ed]379 }
[d347b53]380
[8b1e15ac]381 if (ftype != fun_inner && ftype != fun_exposed) {
382 /* Unknown function type */
[9b415c9]383 log_msg(LVL_ERROR,
[ebcb05a]384 "Unknown function type %d provided by driver.",
[9b415c9]385 (int) ftype);
[8b1e15ac]386
[58cbb0c8]387 dev_del_ref(pdev);
[8b1e15ac]388 async_answer_0(callid, EINVAL);
389 return;
390 }
391
[ba38f72c]392 char *fun_name = NULL;
393 int rc = async_data_write_accept((void **)&fun_name, true, 0, 0, 0, 0);
[c6c389ed]394 if (rc != EOK) {
[58cbb0c8]395 dev_del_ref(pdev);
[ffa2c8ef]396 async_answer_0(callid, rc);
[d347b53]397 return;
398 }
399
[58cbb0c8]400 fibril_rwlock_write_lock(&tree->rwlock);
401
[e2b9b341]402 /* Check device state */
403 if (pdev->state == DEVICE_REMOVED) {
404 fibril_rwlock_write_unlock(&tree->rwlock);
405 dev_del_ref(pdev);
406 async_answer_0(callid, ENOENT);
407 return;
408 }
409
[0876062]410 /* Check that function with same name is not there already. */
[58cbb0c8]411 if (find_fun_node_in_device(tree, pdev, fun_name) != NULL) {
[0876062]412 fibril_rwlock_write_unlock(&tree->rwlock);
[58cbb0c8]413 dev_del_ref(pdev);
[0876062]414 async_answer_0(callid, EEXISTS);
415 printf(NAME ": Warning, driver tried to register `%s' twice.\n",
416 fun_name);
417 free(fun_name);
418 return;
419 }
[d0dd7b5]420
[ba38f72c]421 fun_node_t *fun = create_fun_node();
[58cbb0c8]422 fun_add_ref(fun);
[d0dd7b5]423 fun->ftype = ftype;
424
[ba38f72c]425 if (!insert_fun_node(&device_tree, fun, fun_name, pdev)) {
[957cfa58]426 fibril_rwlock_write_unlock(&tree->rwlock);
[58cbb0c8]427 dev_del_ref(pdev);
[ba38f72c]428 delete_fun_node(fun);
[ffa2c8ef]429 async_answer_0(callid, ENOMEM);
[d347b53]430 return;
[c6c389ed]431 }
[d347b53]432
[1a5b252]433 fibril_rwlock_write_unlock(&tree->rwlock);
[58cbb0c8]434 dev_del_ref(pdev);
[2480e19]435
[ba38f72c]436 devman_receive_match_ids(match_count, &fun->match_ids);
[1a5b252]437
438 rc = online_function(fun);
439 if (rc != EOK) {
440 /* XXX clean up */
441 async_answer_0(callid, rc);
442 return;
[398c4d7]443 }
[8b1e15ac]444
[38b3baf]445 /* Return device handle to parent's driver. */
[ba38f72c]446 async_answer_1(callid, EOK, fun->handle);
[d347b53]447}
448
[1dc4a5e]449static void devman_add_function_to_cat(ipc_callid_t callid, ipc_call_t *call)
[ce89036b]450{
[0b5a4131]451 devman_handle_t handle = IPC_GET_ARG1(*call);
[cc574511]452 category_id_t cat_id;
453 int rc;
[692c40cb]454
[1dc4a5e]455 /* Get category name. */
456 char *cat_name;
457 rc = async_data_write_accept((void **) &cat_name, true,
[38b3baf]458 0, 0, 0, 0);
[692c40cb]459 if (rc != EOK) {
[ffa2c8ef]460 async_answer_0(callid, rc);
[692c40cb]461 return;
[58cbb0c8]462 }
[692c40cb]463
[ba38f72c]464 fun_node_t *fun = find_fun_node(&device_tree, handle);
465 if (fun == NULL) {
[ffa2c8ef]466 async_answer_0(callid, ENOENT);
[692c40cb]467 return;
468 }
469
[58cbb0c8]470 fibril_rwlock_read_lock(&device_tree.rwlock);
471
[e2b9b341]472 /* Check function state */
473 if (fun->state == FUN_REMOVED) {
474 fibril_rwlock_read_unlock(&device_tree.rwlock);
475 async_answer_0(callid, ENOENT);
476 return;
477 }
478
[1dc4a5e]479 rc = loc_category_get_id(cat_name, &cat_id, IPC_FLAG_BLOCKING);
[cc574511]480 if (rc == EOK) {
481 loc_service_add_to_cat(fun->service_id, cat_id);
482 } else {
483 log_msg(LVL_ERROR, "Failed adding function `%s' to category "
[1dc4a5e]484 "`%s'.", fun->pathname, cat_name);
[cc574511]485 }
486
[1dc4a5e]487 log_msg(LVL_NOTE, "Function `%s' added to category `%s'.",
488 fun->pathname, cat_name);
[398c4d7]489
[58cbb0c8]490 fibril_rwlock_read_unlock(&device_tree.rwlock);
491 fun_del_ref(fun);
492
[ffa2c8ef]493 async_answer_0(callid, EOK);
[692c40cb]494}
495
[1a5b252]496/** Online function by driver request.
497 *
498 */
499static void devman_drv_fun_online(ipc_callid_t iid, ipc_call_t *icall,
500 driver_t *drv)
501{
502 fun_node_t *fun;
503 int rc;
504
505 printf("devman_drv_fun_online()\n");
[58cbb0c8]506 fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));
507 if (fun == NULL) {
508 async_answer_0(iid, ENOENT);
509 return;
510 }
[1a5b252]511
[58cbb0c8]512 fibril_rwlock_read_lock(&device_tree.rwlock);
513 if (fun->dev == NULL || fun->dev->drv != drv) {
514 fibril_rwlock_read_unlock(&device_tree.rwlock);
515 fun_del_ref(fun);
[1a5b252]516 async_answer_0(iid, ENOENT);
517 return;
518 }
[58cbb0c8]519 fibril_rwlock_read_unlock(&device_tree.rwlock);
[1a5b252]520
521 rc = online_function(fun);
522 if (rc != EOK) {
523 printf("devman_drv_fun_online() online_fun->ERROR\n");
[58cbb0c8]524 fun_del_ref(fun);
[1a5b252]525 async_answer_0(iid, (sysarg_t) rc);
526 return;
527 }
[58cbb0c8]528
529 fun_del_ref(fun);
[1a5b252]530 printf("devman_drv_fun_online() online_fun->OK\n");
531
532 async_answer_0(iid, (sysarg_t) EOK);
533}
534
535
536/** Offline function by driver request.
537 *
538 */
539static void devman_drv_fun_offline(ipc_callid_t iid, ipc_call_t *icall,
540 driver_t *drv)
541{
542 fun_node_t *fun;
543 int rc;
544
[58cbb0c8]545 fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));
546 if (fun == NULL) {
547 async_answer_0(iid, ENOENT);
548 return;
549 }
[1a5b252]550
[58cbb0c8]551 fibril_rwlock_write_lock(&device_tree.rwlock);
552 if (fun->dev == NULL || fun->dev->drv != drv) {
553 fun_del_ref(fun);
[1a5b252]554 async_answer_0(iid, ENOENT);
555 return;
556 }
[58cbb0c8]557 fibril_rwlock_write_unlock(&device_tree.rwlock);
[1a5b252]558
559 rc = offline_function(fun);
560 if (rc != EOK) {
[58cbb0c8]561 fun_del_ref(fun);
[1a5b252]562 async_answer_0(iid, (sysarg_t) rc);
563 return;
564 }
565
[58cbb0c8]566 fun_del_ref(fun);
[1a5b252]567 async_answer_0(iid, (sysarg_t) EOK);
568}
569
[d0dd7b5]570/** Remove function. */
571static void devman_remove_function(ipc_callid_t callid, ipc_call_t *call)
572{
573 devman_handle_t fun_handle = IPC_GET_ARG1(*call);
574 dev_tree_t *tree = &device_tree;
575 int rc;
576
577
[58cbb0c8]578 fun_node_t *fun = find_fun_node(&device_tree, fun_handle);
[d0dd7b5]579 if (fun == NULL) {
580 async_answer_0(callid, ENOENT);
581 return;
582 }
583
[58cbb0c8]584 fibril_rwlock_write_lock(&tree->rwlock);
585
[d0dd7b5]586 log_msg(LVL_DEBUG, "devman_remove_function(fun='%s')", fun->pathname);
587
[e2b9b341]588 /* Check function state */
589 if (fun->state == FUN_REMOVED) {
590 fibril_rwlock_write_unlock(&tree->rwlock);
591 async_answer_0(callid, ENOENT);
592 return;
593 }
594
[d0dd7b5]595 if (fun->ftype == fun_inner) {
596 /* Handle possible descendants */
[58cbb0c8]597 /* TODO - This is a surprise removal */
[1a5b252]598 if (fun->child != NULL) {
599 log_msg(LVL_WARN, "devman_remove_function(): not handling "
600 "descendants\n");
601 }
[d0dd7b5]602 } else {
[1a5b252]603 if (fun->service_id != 0) {
604 /* Unregister from location service */
605 rc = loc_service_unregister(fun->service_id);
606 if (rc != EOK) {
607 log_msg(LVL_ERROR, "Failed unregistering tree "
608 "service.");
609 fibril_rwlock_write_unlock(&tree->rwlock);
[58cbb0c8]610 fun_del_ref(fun);
[1a5b252]611 async_answer_0(callid, EIO);
612 return;
613 }
[d0dd7b5]614 }
615 }
616
617 remove_fun_node(&device_tree, fun);
618 fibril_rwlock_write_unlock(&tree->rwlock);
[58cbb0c8]619
620 /* Delete ref added when inserting function into tree */
621 fun_del_ref(fun);
622 /* Delete ref added above when looking up function */
623 fun_del_ref(fun);
[d0dd7b5]624
625 log_msg(LVL_DEBUG, "devman_remove_function() succeeded.");
626 async_answer_0(callid, EOK);
627}
628
[38b3baf]629/** Initialize driver which has registered itself as running and ready.
630 *
631 * The initialization is done in a separate fibril to avoid deadlocks (if the
632 * driver needed to be served by devman during the driver's initialization).
[3843ecb]633 */
[d347b53]634static int init_running_drv(void *drv)
635{
[38b3baf]636 driver_t *driver = (driver_t *) drv;
637
638 initialize_running_driver(driver, &device_tree);
[ebcb05a]639 log_msg(LVL_DEBUG, "The `%s` driver was successfully initialized.",
[38b3baf]640 driver->name);
[d347b53]641 return 0;
[bda60d9]642}
643
[38b3baf]644/** Function for handling connections from a driver to the device manager. */
[924c75e1]645static void devman_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
[38b3baf]646{
[422722e]647 client_t *client;
648 driver_t *driver;
649
[38b3baf]650 /* Accept the connection. */
[ffa2c8ef]651 async_answer_0(iid, EOK);
[e2b9a993]652
[422722e]653 client = async_get_client_data();
654 if (client == NULL) {
655 log_msg(LVL_ERROR, "Failed to allocate client data.");
[3843ecb]656 return;
[d347b53]657 }
658
[79ae36dd]659 while (true) {
660 ipc_call_t call;
661 ipc_callid_t callid = async_get_call(&call);
662
663 if (!IPC_GET_IMETHOD(call))
664 break;
[e2b9a993]665
[422722e]666 if (IPC_GET_IMETHOD(call) != DEVMAN_DRIVER_REGISTER) {
667 fibril_mutex_lock(&client->mutex);
668 driver = client->driver;
669 fibril_mutex_unlock(&client->mutex);
670 if (driver == NULL) {
671 /* First call must be to DEVMAN_DRIVER_REGISTER */
672 async_answer_0(callid, ENOTSUP);
673 continue;
674 }
675 }
676
[228e490]677 switch (IPC_GET_IMETHOD(call)) {
[422722e]678 case DEVMAN_DRIVER_REGISTER:
679 fibril_mutex_lock(&client->mutex);
680 if (client->driver != NULL) {
681 fibril_mutex_unlock(&client->mutex);
682 async_answer_0(callid, EINVAL);
683 continue;
684 }
685 client->driver = devman_driver_register(callid, &call);
686 fibril_mutex_unlock(&client->mutex);
687 break;
[8b1e15ac]688 case DEVMAN_ADD_FUNCTION:
689 devman_add_function(callid, &call);
[e2b9a993]690 break;
[1dc4a5e]691 case DEVMAN_ADD_DEVICE_TO_CATEGORY:
692 devman_add_function_to_cat(callid, &call);
[692c40cb]693 break;
[1a5b252]694 case DEVMAN_DRV_FUN_ONLINE:
695 devman_drv_fun_online(callid, &call, driver);
696 break;
697 case DEVMAN_DRV_FUN_OFFLINE:
698 devman_drv_fun_offline(callid, &call, driver);
699 break;
[d0dd7b5]700 case DEVMAN_REMOVE_FUNCTION:
701 devman_remove_function(callid, &call);
702 break;
[e2b9a993]703 default:
[1a5b252]704 async_answer_0(callid, EINVAL);
[e2b9a993]705 break;
706 }
707 }
708}
709
[38b3baf]710/** Find handle for the device instance identified by the device's path in the
711 * device tree. */
[ba38f72c]712static void devman_function_get_handle(ipc_callid_t iid, ipc_call_t *icall)
[f658458]713{
[38b3baf]714 char *pathname;
[58cbb0c8]715 devman_handle_t handle;
[38b3baf]716
717 int rc = async_data_write_accept((void **) &pathname, true, 0, 0, 0, 0);
[f658458]718 if (rc != EOK) {
[ffa2c8ef]719 async_answer_0(iid, rc);
[f658458]720 return;
721 }
722
[8b1e15ac]723 fun_node_t *fun = find_fun_node_by_path(&device_tree, pathname);
[f658458]724
725 free(pathname);
726
[ba38f72c]727 if (fun == NULL) {
[ffa2c8ef]728 async_answer_0(iid, ENOENT);
[f658458]729 return;
730 }
[8b1e15ac]731
[58cbb0c8]732 fibril_rwlock_read_lock(&device_tree.rwlock);
[e2b9b341]733
734 /* Check function state */
735 if (fun->state == FUN_REMOVED) {
736 fibril_rwlock_read_unlock(&device_tree.rwlock);
737 async_answer_0(iid, ENOENT);
738 return;
739 }
[58cbb0c8]740 handle = fun->handle;
[e2b9b341]741
[58cbb0c8]742 fibril_rwlock_read_unlock(&device_tree.rwlock);
743
744 /* Delete reference created above by find_fun_node_by_path() */
745 fun_del_ref(fun);
746
747 async_answer_1(iid, EOK, handle);
[f658458]748}
749
[7beb220]750/** Get device name. */
751static void devman_fun_get_name(ipc_callid_t iid, ipc_call_t *icall)
752{
753 devman_handle_t handle = IPC_GET_ARG1(*icall);
754
755 fun_node_t *fun = find_fun_node(&device_tree, handle);
756 if (fun == NULL) {
757 async_answer_0(iid, ENOMEM);
758 return;
759 }
760
761 ipc_callid_t data_callid;
762 size_t data_len;
763 if (!async_data_read_receive(&data_callid, &data_len)) {
764 async_answer_0(iid, EINVAL);
[58cbb0c8]765 fun_del_ref(fun);
[7beb220]766 return;
767 }
768
769 void *buffer = malloc(data_len);
770 if (buffer == NULL) {
771 async_answer_0(data_callid, ENOMEM);
772 async_answer_0(iid, ENOMEM);
[58cbb0c8]773 fun_del_ref(fun);
[7beb220]774 return;
775 }
776
[58cbb0c8]777 fibril_rwlock_read_lock(&device_tree.rwlock);
778
[e2b9b341]779 /* Check function state */
780 if (fun->state == FUN_REMOVED) {
781 fibril_rwlock_read_unlock(&device_tree.rwlock);
782 free(buffer);
783
784 async_answer_0(data_callid, ENOENT);
785 async_answer_0(iid, ENOENT);
786 fun_del_ref(fun);
787 return;
788 }
789
[7beb220]790 size_t sent_length = str_size(fun->name);
791 if (sent_length > data_len) {
792 sent_length = data_len;
793 }
794
795 async_data_read_finalize(data_callid, fun->name, sent_length);
796 async_answer_0(iid, EOK);
797
[58cbb0c8]798 fibril_rwlock_read_unlock(&device_tree.rwlock);
799 fun_del_ref(fun);
[7beb220]800 free(buffer);
801}
802
803
804/** Get device path. */
805static void devman_fun_get_path(ipc_callid_t iid, ipc_call_t *icall)
[431d6d6]806{
807 devman_handle_t handle = IPC_GET_ARG1(*icall);
808
809 fun_node_t *fun = find_fun_node(&device_tree, handle);
810 if (fun == NULL) {
811 async_answer_0(iid, ENOMEM);
812 return;
813 }
814
815 ipc_callid_t data_callid;
816 size_t data_len;
817 if (!async_data_read_receive(&data_callid, &data_len)) {
818 async_answer_0(iid, EINVAL);
[58cbb0c8]819 fun_del_ref(fun);
[431d6d6]820 return;
821 }
822
823 void *buffer = malloc(data_len);
824 if (buffer == NULL) {
825 async_answer_0(data_callid, ENOMEM);
826 async_answer_0(iid, ENOMEM);
[58cbb0c8]827 fun_del_ref(fun);
[431d6d6]828 return;
829 }
[58cbb0c8]830
831 fibril_rwlock_read_lock(&device_tree.rwlock);
832
[e2b9b341]833 /* Check function state */
834 if (fun->state == FUN_REMOVED) {
835 fibril_rwlock_read_unlock(&device_tree.rwlock);
836 free(buffer);
837
838 async_answer_0(data_callid, ENOENT);
839 async_answer_0(iid, ENOENT);
840 fun_del_ref(fun);
841 return;
842 }
843
[431d6d6]844 size_t sent_length = str_size(fun->pathname);
845 if (sent_length > data_len) {
846 sent_length = data_len;
847 }
848
849 async_data_read_finalize(data_callid, fun->pathname, sent_length);
850 async_answer_0(iid, EOK);
851
[58cbb0c8]852 fibril_rwlock_read_unlock(&device_tree.rwlock);
853 fun_del_ref(fun);
[431d6d6]854 free(buffer);
855}
856
[7beb220]857static void devman_dev_get_functions(ipc_callid_t iid, ipc_call_t *icall)
858{
859 ipc_callid_t callid;
860 size_t size;
861 size_t act_size;
862 int rc;
863
864 if (!async_data_read_receive(&callid, &size)) {
865 async_answer_0(callid, EREFUSED);
866 async_answer_0(iid, EREFUSED);
867 return;
868 }
869
870 fibril_rwlock_read_lock(&device_tree.rwlock);
871
872 dev_node_t *dev = find_dev_node_no_lock(&device_tree,
873 IPC_GET_ARG1(*icall));
[e2b9b341]874 if (dev == NULL || dev->state == DEVICE_REMOVED) {
[7beb220]875 fibril_rwlock_read_unlock(&device_tree.rwlock);
876 async_answer_0(callid, ENOENT);
877 async_answer_0(iid, ENOENT);
878 return;
879 }
880
881 devman_handle_t *hdl_buf = (devman_handle_t *) malloc(size);
882 if (hdl_buf == NULL) {
883 fibril_rwlock_read_unlock(&device_tree.rwlock);
884 async_answer_0(callid, ENOMEM);
885 async_answer_0(iid, ENOMEM);
886 return;
887 }
888
889 rc = dev_get_functions(&device_tree, dev, hdl_buf, size, &act_size);
890 if (rc != EOK) {
891 fibril_rwlock_read_unlock(&device_tree.rwlock);
892 async_answer_0(callid, rc);
893 async_answer_0(iid, rc);
894 return;
895 }
896
897 fibril_rwlock_read_unlock(&device_tree.rwlock);
898
899 sysarg_t retval = async_data_read_finalize(callid, hdl_buf, size);
900 free(hdl_buf);
901
902 async_answer_1(iid, retval, act_size);
903}
904
905
906/** Get handle for child device of a function. */
907static void devman_fun_get_child(ipc_callid_t iid, ipc_call_t *icall)
908{
909 fun_node_t *fun;
910
911 fibril_rwlock_read_lock(&device_tree.rwlock);
912
[e2b9b341]913 fun = find_fun_node_no_lock(&device_tree, IPC_GET_ARG1(*icall));
914 if (fun == NULL || fun->state == FUN_REMOVED) {
[7beb220]915 fibril_rwlock_read_unlock(&device_tree.rwlock);
916 async_answer_0(iid, ENOENT);
917 return;
918 }
919
920 if (fun->child == NULL) {
921 fibril_rwlock_read_unlock(&device_tree.rwlock);
922 async_answer_0(iid, ENOENT);
923 return;
924 }
925
926 async_answer_1(iid, EOK, fun->child->handle);
927
928 fibril_rwlock_read_unlock(&device_tree.rwlock);
929}
930
[1a5b252]931/** Online function.
932 *
933 * Send a request to online a function to the responsible driver.
934 * The driver may offline other functions if necessary (i.e. if the state
935 * of this function is linked to state of another function somehow).
936 */
937static void devman_fun_online(ipc_callid_t iid, ipc_call_t *icall)
938{
939 fun_node_t *fun;
940 int rc;
941
[58cbb0c8]942 fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));
[1a5b252]943 if (fun == NULL) {
944 async_answer_0(iid, ENOENT);
945 return;
946 }
947
[58cbb0c8]948 rc = driver_fun_online(&device_tree, fun);
949 fun_del_ref(fun);
[1a5b252]950
951 async_answer_0(iid, (sysarg_t) rc);
952}
953
954/** Offline function.
955 *
956 * Send a request to offline a function to the responsible driver. As
957 * a result the subtree rooted at that function should be cleanly
958 * detatched. The driver may offline other functions if necessary
959 * (i.e. if the state of this function is linked to state of another
960 * function somehow).
961 */
962static void devman_fun_offline(ipc_callid_t iid, ipc_call_t *icall)
963{
964 fun_node_t *fun;
965 int rc;
966
[58cbb0c8]967 fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));
[1a5b252]968 if (fun == NULL) {
969 async_answer_0(iid, ENOENT);
970 return;
971 }
972
[58cbb0c8]973 rc = driver_fun_offline(&device_tree, fun);
974 fun_del_ref(fun);
[1a5b252]975
976 async_answer_0(iid, (sysarg_t) rc);
977}
978
[e280857]979/** Find handle for the function instance identified by its service ID. */
980static void devman_fun_sid_to_handle(ipc_callid_t iid, ipc_call_t *icall)
981{
982 fun_node_t *fun;
983
984 fun = find_loc_tree_function(&device_tree, IPC_GET_ARG1(*icall));
985
986 if (fun == NULL) {
987 async_answer_0(iid, ENOENT);
988 return;
989 }
990
[58cbb0c8]991 fibril_rwlock_read_lock(&device_tree.rwlock);
[e2b9b341]992
993 /* Check function state */
994 if (fun->state == FUN_REMOVED) {
995 fibril_rwlock_read_unlock(&device_tree.rwlock);
996 async_answer_0(iid, ENOENT);
997 return;
998 }
999
[e280857]1000 async_answer_1(iid, EOK, fun->handle);
[58cbb0c8]1001 fibril_rwlock_read_unlock(&device_tree.rwlock);
1002 fun_del_ref(fun);
[e280857]1003}
[f658458]1004
[38b3baf]1005/** Function for handling connections from a client to the device manager. */
[f658458]1006static void devman_connection_client(ipc_callid_t iid, ipc_call_t *icall)
1007{
[38b3baf]1008 /* Accept connection. */
[ffa2c8ef]1009 async_answer_0(iid, EOK);
[f658458]1010
[79ae36dd]1011 while (true) {
[f658458]1012 ipc_call_t call;
1013 ipc_callid_t callid = async_get_call(&call);
1014
[79ae36dd]1015 if (!IPC_GET_IMETHOD(call))
1016 break;
1017
[228e490]1018 switch (IPC_GET_IMETHOD(call)) {
[f658458]1019 case DEVMAN_DEVICE_GET_HANDLE:
[ba38f72c]1020 devman_function_get_handle(callid, &call);
[f658458]1021 break;
[7beb220]1022 case DEVMAN_DEV_GET_FUNCTIONS:
1023 devman_dev_get_functions(callid, &call);
1024 break;
1025 case DEVMAN_FUN_GET_CHILD:
1026 devman_fun_get_child(callid, &call);
1027 break;
1028 case DEVMAN_FUN_GET_NAME:
1029 devman_fun_get_name(callid, &call);
1030 break;
1031 case DEVMAN_FUN_GET_PATH:
1032 devman_fun_get_path(callid, &call);
[431d6d6]1033 break;
[1a5b252]1034 case DEVMAN_FUN_ONLINE:
1035 devman_fun_online(callid, &call);
1036 break;
1037 case DEVMAN_FUN_OFFLINE:
1038 devman_fun_offline(callid, &call);
1039 break;
[e280857]1040 case DEVMAN_FUN_SID_TO_HANDLE:
1041 devman_fun_sid_to_handle(callid, &call);
1042 break;
[f658458]1043 default:
[ffa2c8ef]1044 async_answer_0(callid, ENOENT);
[f658458]1045 }
[c6c389ed]1046 }
[f658458]1047}
1048
[c6c389ed]1049static void devman_forward(ipc_callid_t iid, ipc_call_t *icall,
1050 bool drv_to_parent)
[38b3baf]1051{
[0b5a4131]1052 devman_handle_t handle = IPC_GET_ARG2(*icall);
[8b1e15ac]1053 devman_handle_t fwd_h;
1054 fun_node_t *fun = NULL;
1055 dev_node_t *dev = NULL;
[9a66bc2e]1056
[8b1e15ac]1057 fun = find_fun_node(&device_tree, handle);
1058 if (fun == NULL)
1059 dev = find_dev_node(&device_tree, handle);
[58cbb0c8]1060 else {
1061 fibril_rwlock_read_lock(&device_tree.rwlock);
[8b1e15ac]1062 dev = fun->dev;
[58cbb0c8]1063 if (dev != NULL)
1064 dev_add_ref(dev);
1065 fibril_rwlock_read_unlock(&device_tree.rwlock);
1066 }
[8b1e15ac]1067
[0418050]1068 /*
1069 * For a valid function to connect to we need a device. The root
1070 * function, for example, has no device and cannot be connected to.
1071 * This means @c dev needs to be valid regardless whether we are
1072 * connecting to a device or to a function.
1073 */
1074 if (dev == NULL) {
[9b415c9]1075 log_msg(LVL_ERROR, "IPC forwarding failed - no device or "
[ebcb05a]1076 "function with handle %" PRIun " was found.", handle);
[ffa2c8ef]1077 async_answer_0(iid, ENOENT);
[58cbb0c8]1078 goto cleanup;
[5cd136ab]1079 }
[8b1e15ac]1080
1081 if (fun == NULL && !drv_to_parent) {
[9b415c9]1082 log_msg(LVL_ERROR, NAME ": devman_forward error - cannot "
[ebcb05a]1083 "connect to handle %" PRIun ", refers to a device.",
[9b415c9]1084 handle);
[ffa2c8ef]1085 async_answer_0(iid, ENOENT);
[58cbb0c8]1086 goto cleanup;
[5cd136ab]1087 }
1088
1089 driver_t *driver = NULL;
1090
[e2b9b341]1091 fibril_rwlock_read_lock(&device_tree.rwlock);
1092
[5cd136ab]1093 if (drv_to_parent) {
[8b1e15ac]1094 /* Connect to parent function of a device (or device function). */
1095 if (dev->pfun->dev != NULL)
1096 driver = dev->pfun->dev->drv;
1097 fwd_h = dev->pfun->handle;
[c6c389ed]1098 } else if (dev->state == DEVICE_USABLE) {
[8b1e15ac]1099 /* Connect to the specified function */
[38b3baf]1100 driver = dev->drv;
[c6c389ed]1101 assert(driver != NULL);
[8b1e15ac]1102
1103 fwd_h = handle;
[5cd136ab]1104 }
1105
[e2b9b341]1106 fibril_rwlock_read_unlock(&device_tree.rwlock);
1107
[c6c389ed]1108 if (driver == NULL) {
[9b415c9]1109 log_msg(LVL_ERROR, "IPC forwarding refused - " \
[79ae36dd]1110 "the device %" PRIun " is not in usable state.", handle);
[ffa2c8ef]1111 async_answer_0(iid, ENOENT);
[58cbb0c8]1112 goto cleanup;
[5cd136ab]1113 }
1114
[38b3baf]1115 int method;
1116 if (drv_to_parent)
[5cd136ab]1117 method = DRIVER_DRIVER;
[38b3baf]1118 else
[5cd136ab]1119 method = DRIVER_CLIENT;
1120
[79ae36dd]1121 if (!driver->sess) {
1122 log_msg(LVL_ERROR,
1123 "Could not forward to driver `%s'.", driver->name);
[ffa2c8ef]1124 async_answer_0(iid, EINVAL);
[58cbb0c8]1125 goto cleanup;
[9a66bc2e]1126 }
[c6c389ed]1127
[8b1e15ac]1128 if (fun != NULL) {
[9b415c9]1129 log_msg(LVL_DEBUG,
[ebcb05a]1130 "Forwarding request for `%s' function to driver `%s'.",
[9b415c9]1131 fun->pathname, driver->name);
[8b1e15ac]1132 } else {
[9b415c9]1133 log_msg(LVL_DEBUG,
[ebcb05a]1134 "Forwarding request for `%s' device to driver `%s'.",
[9b415c9]1135 dev->pfun->pathname, driver->name);
[8b1e15ac]1136 }
[79ae36dd]1137
1138 async_exch_t *exch = async_exchange_begin(driver->sess);
1139 async_forward_fast(iid, exch, method, fwd_h, 0, IPC_FF_NONE);
1140 async_exchange_end(exch);
[58cbb0c8]1141
1142cleanup:
1143 if (dev != NULL)
1144 dev_del_ref(dev);
1145 if (fun != NULL)
1146 fun_del_ref(fun);
[5cd136ab]1147}
1148
[15f3c3f]1149/** Function for handling connections from a client forwarded by the location
1150 * service to the device manager. */
1151static void devman_connection_loc(ipc_callid_t iid, ipc_call_t *icall)
[ce89036b]1152{
[15f3c3f]1153 service_id_t service_id = IPC_GET_ARG2(*icall);
[ba38f72c]1154 fun_node_t *fun;
1155 dev_node_t *dev;
[e2b9b341]1156 devman_handle_t handle;
1157 driver_t *driver;
[c6c389ed]1158
[15f3c3f]1159 fun = find_loc_tree_function(&device_tree, service_id);
[ce89036b]1160
[e2b9b341]1161 fibril_rwlock_read_lock(&device_tree.rwlock);
1162
1163 if (fun == NULL || fun->dev == NULL || fun->dev->drv == NULL) {
[12f9f0d0]1164 log_msg(LVL_WARN, "devman_connection_loc(): function "
1165 "not found.\n");
[e2b9b341]1166 fibril_rwlock_read_unlock(&device_tree.rwlock);
[ffa2c8ef]1167 async_answer_0(iid, ENOENT);
[ce89036b]1168 return;
1169 }
1170
[ba38f72c]1171 dev = fun->dev;
[e2b9b341]1172 driver = dev->drv;
1173 handle = fun->handle;
1174
1175 fibril_rwlock_read_unlock(&device_tree.rwlock);
[ba38f72c]1176
[e2b9b341]1177 async_exch_t *exch = async_exchange_begin(driver->sess);
1178 async_forward_fast(iid, exch, DRIVER_CLIENT, handle, 0,
[38b3baf]1179 IPC_FF_NONE);
[79ae36dd]1180 async_exchange_end(exch);
1181
1182 log_msg(LVL_DEBUG,
[15f3c3f]1183 "Forwarding loc service request for `%s' function to driver `%s'.",
[e2b9b341]1184 fun->pathname, driver->name);
1185
1186 fun_del_ref(fun);
[ce89036b]1187}
1188
[38b3baf]1189/** Function for handling connections to device manager. */
[9934f7d]1190static void devman_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
[38b3baf]1191{
[422722e]1192 /* Select port. */
[96b02eb9]1193 switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
[924c75e1]1194 case DEVMAN_DRIVER:
1195 devman_connection_driver(iid, icall);
1196 break;
[f658458]1197 case DEVMAN_CLIENT:
1198 devman_connection_client(iid, icall);
1199 break;
[924c75e1]1200 case DEVMAN_CONNECT_TO_DEVICE:
[38b3baf]1201 /* Connect client to selected device. */
[5cd136ab]1202 devman_forward(iid, icall, false);
1203 break;
[15f3c3f]1204 case DEVMAN_CONNECT_FROM_LOC:
1205 /* Someone connected through loc node. */
1206 devman_connection_loc(iid, icall);
[47a7174f]1207 break;
[5cd136ab]1208 case DEVMAN_CONNECT_TO_PARENTS_DEVICE:
[38b3baf]1209 /* Connect client to selected device. */
[5cd136ab]1210 devman_forward(iid, icall, true);
[38b3baf]1211 break;
[924c75e1]1212 default:
1213 /* No such interface */
[ffa2c8ef]1214 async_answer_0(iid, ENOENT);
[924c75e1]1215 }
1216}
1217
[422722e]1218static void *devman_client_data_create(void)
1219{
1220 client_t *client;
1221
1222 client = calloc(1, sizeof(client_t));
1223 if (client == NULL)
1224 return NULL;
1225
1226 fibril_mutex_initialize(&client->mutex);
1227 return client;
1228}
1229
1230static void devman_client_data_destroy(void *data)
1231{
1232 free(data);
1233}
1234
[38b3baf]1235/** Initialize device manager internal structures. */
1236static bool devman_init(void)
[e2b9a993]1237{
[ebcb05a]1238 log_msg(LVL_DEBUG, "devman_init - looking for available drivers.");
[08d9c4e6]1239
[38b3baf]1240 /* Initialize list of available drivers. */
[0c3666d]1241 init_driver_list(&drivers_list);
[c6c389ed]1242 if (lookup_available_drivers(&drivers_list,
1243 DRIVER_DEFAULT_STORE) == 0) {
[ebcb05a]1244 log_msg(LVL_FATAL, "No drivers found.");
[e2b9a993]1245 return false;
1246 }
[c6c389ed]1247
[ebcb05a]1248 log_msg(LVL_DEBUG, "devman_init - list of drivers has been initialized.");
[e2b9a993]1249
[38b3baf]1250 /* Create root device node. */
[e2b9a993]1251 if (!init_device_tree(&device_tree, &drivers_list)) {
[9b415c9]1252 log_msg(LVL_FATAL, "Failed to initialize device tree.");
[38b3baf]1253 return false;
[e2b9a993]1254 }
1255
[38b3baf]1256 /*
[15f3c3f]1257 * !!! devman_connection ... as the device manager is not a real loc
[38b3baf]1258 * driver (it uses a completely different ipc protocol than an ordinary
[15f3c3f]1259 * loc driver) forwarding a connection from client to the devman by
1260 * location service would not work.
[38b3baf]1261 */
[15f3c3f]1262 loc_server_register(NAME, devman_connection);
[ce89036b]1263
[e2b9a993]1264 return true;
1265}
1266
1267int main(int argc, char *argv[])
1268{
1269 printf(NAME ": HelenOS Device Manager\n");
1270
[cc574511]1271 if (log_init(NAME, LVL_WARN) != EOK) {
[9b415c9]1272 printf(NAME ": Error initializing logging subsystem.\n");
1273 return -1;
1274 }
1275
[e2b9a993]1276 if (!devman_init()) {
[ebcb05a]1277 log_msg(LVL_ERROR, "Error while initializing service.");
[e2b9a993]1278 return -1;
1279 }
1280
[422722e]1281 /* Set handlers for incoming connections. */
1282 async_set_client_data_constructor(devman_client_data_create);
1283 async_set_client_data_destructor(devman_client_data_destroy);
[e2b9a993]1284 async_set_client_connection(devman_connection);
1285
[38b3baf]1286 /* Register device manager at naming service. */
[9b415c9]1287 if (service_register(SERVICE_DEVMAN) != EOK) {
[ebcb05a]1288 log_msg(LVL_ERROR, "Failed registering as a service.");
[e2b9a993]1289 return -1;
[9b415c9]1290 }
[e2b9a993]1291
[9b415c9]1292 printf(NAME ": Accepting connections.\n");
[79ae36dd]1293 task_retval(0);
[e2b9a993]1294 async_manager();
1295
[38b3baf]1296 /* Never reached. */
[e2b9a993]1297 return 0;
1298}
[c16cf62]1299
1300/** @}
[38b3baf]1301 */
Note: See TracBrowser for help on using the repository browser.