source: mainline/uspace/srv/devman/main.c@ 2988aec7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2988aec7 was 33fc3ae, checked in by Jakub Jermar <jakub@…>, 13 years ago

Do not leak a fun_node_t reference in devman_add_function()
when trying to add the same function for the second time.

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