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

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

Check state for removed devices and functions.

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