source: mainline/uspace/srv/devman/main.c@ 224c0e7

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

Supress debugging messages.

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