2 * Copyright (c) 2010 Lenka Trochtova
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 * @defgroup devman Device manager.
31 * @brief HelenOS device manager.
39 #include <ipc/services.h>
44 #include <str_error.h>
46 #include <fibril_synch.h>
50 #include <ipc/devman.h>
53 #include "client_conn.h"
63 static int init_running_drv(void *drv
);
65 /** Register running driver. */
66 static driver_t
*devman_driver_register(ipc_callid_t callid
, ipc_call_t
*call
)
68 driver_t
*driver
= NULL
;
69 char *drv_name
= NULL
;
71 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "devman_driver_register");
73 /* Get driver name. */
74 int rc
= async_data_write_accept((void **) &drv_name
, true, 0, 0, 0, 0);
76 async_answer_0(callid
, rc
);
80 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "The `%s' driver is trying to register.",
83 /* Find driver structure. */
84 driver
= driver_find_by_name(&drivers_list
, drv_name
);
86 log_msg(LOG_DEFAULT
, LVL_ERROR
, "No driver named `%s' was found.", drv_name
);
89 async_answer_0(callid
, ENOENT
);
96 fibril_mutex_lock(&driver
->driver_mutex
);
99 /* We already have a connection to the driver. */
100 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Driver '%s' already started.\n",
102 fibril_mutex_unlock(&driver
->driver_mutex
);
103 async_answer_0(callid
, EEXIST
);
107 switch (driver
->state
) {
108 case DRIVER_NOT_STARTED
:
109 /* Somebody started the driver manually. */
110 log_msg(LOG_DEFAULT
, LVL_NOTE
, "Driver '%s' started manually.\n",
112 driver
->state
= DRIVER_STARTING
;
114 case DRIVER_STARTING
:
115 /* The expected case */
118 /* Should not happen since we do not have a connected session */
122 /* Create connection to the driver. */
123 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "Creating connection to the `%s' driver.",
125 driver
->sess
= async_callback_receive(EXCHANGE_PARALLEL
);
127 fibril_mutex_unlock(&driver
->driver_mutex
);
128 async_answer_0(callid
, ENOTSUP
);
131 /* FIXME: Work around problem with callback sessions */
132 async_sess_args_set(driver
->sess
, INTERFACE_DDF_DEVMAN
, 0, 0);
134 log_msg(LOG_DEFAULT
, LVL_NOTE
,
135 "The `%s' driver was successfully registered as running.",
139 * Initialize the driver as running (e.g. pass assigned devices to it)
140 * in a separate fibril; the separate fibril is used to enable the
141 * driver to use devman service during the driver's initialization.
143 fid_t fid
= fibril_create(init_running_drv
, driver
);
145 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Failed to create initialization fibril " \
146 "for driver `%s'.", driver
->name
);
147 fibril_mutex_unlock(&driver
->driver_mutex
);
148 async_answer_0(callid
, ENOMEM
);
152 fibril_add_ready(fid
);
153 fibril_mutex_unlock(&driver
->driver_mutex
);
155 async_answer_0(callid
, EOK
);
159 /** Receive device match ID from the device's parent driver and add it to the
160 * list of devices match ids.
162 * @param match_ids The list of the device's match ids.
163 * @return Zero on success, negative error code otherwise.
165 static int devman_receive_match_id(match_id_list_t
*match_ids
)
167 match_id_t
*match_id
= create_match_id();
172 callid
= async_get_call(&call
);
173 if (DEVMAN_ADD_MATCH_ID
!= IPC_GET_IMETHOD(call
)) {
174 log_msg(LOG_DEFAULT
, LVL_ERROR
,
175 "Invalid protocol when trying to receive match id.");
176 async_answer_0(callid
, EINVAL
);
177 delete_match_id(match_id
);
181 if (match_id
== NULL
) {
182 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Failed to allocate match id.");
183 async_answer_0(callid
, ENOMEM
);
187 async_answer_0(callid
, EOK
);
189 match_id
->score
= IPC_GET_ARG1(call
);
192 rc
= async_data_write_accept((void **) &match_id_str
, true, 0, 0, 0, 0);
193 match_id
->id
= match_id_str
;
195 delete_match_id(match_id
);
196 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Failed to receive match id string: %s.",
201 list_append(&match_id
->link
, &match_ids
->ids
);
203 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "Received match id `%s', score %d.",
204 match_id
->id
, match_id
->score
);
208 /** Receive device match IDs from the device's parent driver and add them to the
209 * list of devices match ids.
211 * @param match_count The number of device's match ids to be received.
212 * @param match_ids The list of the device's match ids.
213 * @return Zero on success, negative error code otherwise.
215 static int devman_receive_match_ids(sysarg_t match_count
,
216 match_id_list_t
*match_ids
)
221 for (i
= 0; i
< match_count
; i
++) {
222 if (EOK
!= (ret
= devman_receive_match_id(match_ids
)))
228 /** Handle function registration.
230 * Child devices are registered by their parent's device driver.
232 static void devman_add_function(ipc_callid_t callid
, ipc_call_t
*call
)
234 fun_type_t ftype
= (fun_type_t
) IPC_GET_ARG1(*call
);
235 devman_handle_t dev_handle
= IPC_GET_ARG2(*call
);
236 sysarg_t match_count
= IPC_GET_ARG3(*call
);
237 dev_tree_t
*tree
= &device_tree
;
239 dev_node_t
*pdev
= find_dev_node(&device_tree
, dev_handle
);
241 async_answer_0(callid
, ENOENT
);
245 if (ftype
!= fun_inner
&& ftype
!= fun_exposed
) {
246 /* Unknown function type */
247 log_msg(LOG_DEFAULT
, LVL_ERROR
,
248 "Unknown function type %d provided by driver.",
252 async_answer_0(callid
, EINVAL
);
256 char *fun_name
= NULL
;
257 int rc
= async_data_write_accept((void **)&fun_name
, true, 0, 0, 0, 0);
260 async_answer_0(callid
, rc
);
264 fibril_rwlock_write_lock(&tree
->rwlock
);
266 /* Check device state */
267 if (pdev
->state
== DEVICE_REMOVED
) {
268 fibril_rwlock_write_unlock(&tree
->rwlock
);
270 async_answer_0(callid
, ENOENT
);
274 /* Check that function with same name is not there already. */
275 fun_node_t
*tfun
= find_fun_node_in_device(tree
, pdev
, fun_name
);
277 fun_del_ref(tfun
); /* drop the new unwanted reference */
278 fibril_rwlock_write_unlock(&tree
->rwlock
);
280 async_answer_0(callid
, EEXIST
);
281 printf(NAME
": Warning, driver tried to register `%s' twice.\n",
287 fun_node_t
*fun
= create_fun_node();
288 /* One reference for creation, one for us */
294 * We can lock the function here even when holding the tree because
295 * we know it cannot be held by anyone else yet.
299 if (!insert_fun_node(&device_tree
, fun
, fun_name
, pdev
)) {
300 fibril_rwlock_write_unlock(&tree
->rwlock
);
302 fun_busy_unlock(fun
);
304 delete_fun_node(fun
);
305 async_answer_0(callid
, ENOMEM
);
309 fibril_rwlock_write_unlock(&tree
->rwlock
);
312 devman_receive_match_ids(match_count
, &fun
->match_ids
);
314 rc
= fun_online(fun
);
316 /* XXX Set some failed state? */
317 fun_busy_unlock(fun
);
319 async_answer_0(callid
, rc
);
323 fun_busy_unlock(fun
);
326 /* Return device handle to parent's driver. */
327 async_answer_1(callid
, EOK
, fun
->handle
);
330 static void devman_add_function_to_cat(ipc_callid_t callid
, ipc_call_t
*call
)
332 devman_handle_t handle
= IPC_GET_ARG1(*call
);
333 category_id_t cat_id
;
336 /* Get category name. */
338 rc
= async_data_write_accept((void **) &cat_name
, true,
341 async_answer_0(callid
, rc
);
345 fun_node_t
*fun
= find_fun_node(&device_tree
, handle
);
347 async_answer_0(callid
, ENOENT
);
351 fibril_rwlock_read_lock(&device_tree
.rwlock
);
353 /* Check function state */
354 if (fun
->state
== FUN_REMOVED
) {
355 fibril_rwlock_read_unlock(&device_tree
.rwlock
);
356 async_answer_0(callid
, ENOENT
);
360 rc
= loc_category_get_id(cat_name
, &cat_id
, IPC_FLAG_BLOCKING
);
362 loc_service_add_to_cat(fun
->service_id
, cat_id
);
363 log_msg(LOG_DEFAULT
, LVL_NOTE
, "Function `%s' added to category `%s'.",
364 fun
->pathname
, cat_name
);
366 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Failed adding function `%s' to category "
367 "`%s'.", fun
->pathname
, cat_name
);
370 fibril_rwlock_read_unlock(&device_tree
.rwlock
);
373 async_answer_0(callid
, rc
);
376 /** Online function by driver request.
379 static void devman_drv_fun_online(ipc_callid_t iid
, ipc_call_t
*icall
,
385 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "devman_drv_fun_online()");
387 fun
= find_fun_node(&device_tree
, IPC_GET_ARG1(*icall
));
389 async_answer_0(iid
, ENOENT
);
395 fibril_rwlock_read_lock(&device_tree
.rwlock
);
396 if (fun
->dev
== NULL
|| fun
->dev
->drv
!= drv
) {
397 fibril_rwlock_read_unlock(&device_tree
.rwlock
);
398 fun_busy_unlock(fun
);
400 async_answer_0(iid
, ENOENT
);
403 fibril_rwlock_read_unlock(&device_tree
.rwlock
);
405 rc
= fun_online(fun
);
407 fun_busy_unlock(fun
);
409 async_answer_0(iid
, (sysarg_t
) rc
);
413 fun_busy_unlock(fun
);
416 async_answer_0(iid
, (sysarg_t
) EOK
);
420 /** Offline function by driver request.
423 static void devman_drv_fun_offline(ipc_callid_t iid
, ipc_call_t
*icall
,
429 fun
= find_fun_node(&device_tree
, IPC_GET_ARG1(*icall
));
431 async_answer_0(iid
, ENOENT
);
437 fibril_rwlock_write_lock(&device_tree
.rwlock
);
438 if (fun
->dev
== NULL
|| fun
->dev
->drv
!= drv
) {
439 fun_busy_unlock(fun
);
441 async_answer_0(iid
, ENOENT
);
444 fibril_rwlock_write_unlock(&device_tree
.rwlock
);
446 rc
= fun_offline(fun
);
448 fun_busy_unlock(fun
);
450 async_answer_0(iid
, (sysarg_t
) rc
);
454 fun_busy_unlock(fun
);
456 async_answer_0(iid
, (sysarg_t
) EOK
);
459 /** Remove function. */
460 static void devman_remove_function(ipc_callid_t callid
, ipc_call_t
*call
)
462 devman_handle_t fun_handle
= IPC_GET_ARG1(*call
);
463 dev_tree_t
*tree
= &device_tree
;
466 fun_node_t
*fun
= find_fun_node(&device_tree
, fun_handle
);
468 async_answer_0(callid
, ENOENT
);
474 fibril_rwlock_write_lock(&tree
->rwlock
);
476 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "devman_remove_function(fun='%s')", fun
->pathname
);
478 /* Check function state */
479 if (fun
->state
== FUN_REMOVED
) {
480 fibril_rwlock_write_unlock(&tree
->rwlock
);
481 fun_busy_unlock(fun
);
483 async_answer_0(callid
, ENOENT
);
487 if (fun
->ftype
== fun_inner
) {
488 /* This is a surprise removal. Handle possible descendants */
489 if (fun
->child
!= NULL
) {
490 dev_node_t
*dev
= fun
->child
;
491 device_state_t dev_state
;
495 dev_state
= dev
->state
;
497 fibril_rwlock_write_unlock(&device_tree
.rwlock
);
499 /* If device is owned by driver, inform driver it is gone. */
500 if (dev_state
== DEVICE_USABLE
)
501 gone_rc
= driver_dev_gone(&device_tree
, dev
);
505 fibril_rwlock_read_lock(&device_tree
.rwlock
);
507 /* Verify that driver succeeded and removed all functions */
508 if (gone_rc
!= EOK
|| !list_empty(&dev
->functions
)) {
509 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Driver did not remove "
510 "functions for device that is gone. "
511 "Device node is now defunct.");
514 * Not much we can do but mark the device
515 * node as having invalid state. This
518 dev
->state
= DEVICE_INVALID
;
519 fibril_rwlock_read_unlock(&device_tree
.rwlock
);
523 fun_busy_unlock(fun
);
525 async_answer_0(callid
, gone_rc
);
529 driver_t
*driver
= dev
->drv
;
530 fibril_rwlock_read_unlock(&device_tree
.rwlock
);
533 detach_driver(&device_tree
, dev
);
535 fibril_rwlock_write_lock(&device_tree
.rwlock
);
536 remove_dev_node(&device_tree
, dev
);
538 /* Delete ref created when node was inserted */
540 /* Delete ref created by dev_add_ref(dev) above */
544 if (fun
->service_id
!= 0) {
545 /* Unregister from location service */
546 rc
= loc_unregister_tree_function(fun
, &device_tree
);
548 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Failed unregistering tree "
550 fibril_rwlock_write_unlock(&tree
->rwlock
);
551 fun_busy_unlock(fun
);
553 async_answer_0(callid
, EIO
);
559 remove_fun_node(&device_tree
, fun
);
560 fibril_rwlock_write_unlock(&tree
->rwlock
);
561 fun_busy_unlock(fun
);
563 /* Delete ref added when inserting function into tree */
565 /* Delete ref added above when looking up function */
568 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "devman_remove_function() succeeded.");
569 async_answer_0(callid
, EOK
);
572 /** Initialize driver which has registered itself as running and ready.
574 * The initialization is done in a separate fibril to avoid deadlocks (if the
575 * driver needed to be served by devman during the driver's initialization).
577 static int init_running_drv(void *drv
)
579 driver_t
*driver
= (driver_t
*) drv
;
581 initialize_running_driver(driver
, &device_tree
);
582 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "The `%s` driver was successfully initialized.",
587 /** Function for handling connections from a driver to the device manager. */
588 void devman_connection_driver(ipc_callid_t iid
, ipc_call_t
*icall
, void *arg
)
591 driver_t
*driver
= NULL
;
593 /* Accept the connection. */
594 async_answer_0(iid
, EOK
);
596 client
= async_get_client_data();
597 if (client
== NULL
) {
598 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Failed to allocate client data.");
604 ipc_callid_t callid
= async_get_call(&call
);
606 if (!IPC_GET_IMETHOD(call
))
609 if (IPC_GET_IMETHOD(call
) != DEVMAN_DRIVER_REGISTER
) {
610 fibril_mutex_lock(&client
->mutex
);
611 driver
= client
->driver
;
612 fibril_mutex_unlock(&client
->mutex
);
613 if (driver
== NULL
) {
614 /* First call must be to DEVMAN_DRIVER_REGISTER */
615 async_answer_0(callid
, ENOTSUP
);
620 switch (IPC_GET_IMETHOD(call
)) {
621 case DEVMAN_DRIVER_REGISTER
:
622 fibril_mutex_lock(&client
->mutex
);
623 if (client
->driver
!= NULL
) {
624 fibril_mutex_unlock(&client
->mutex
);
625 async_answer_0(callid
, EINVAL
);
628 client
->driver
= devman_driver_register(callid
, &call
);
629 fibril_mutex_unlock(&client
->mutex
);
631 case DEVMAN_ADD_FUNCTION
:
632 devman_add_function(callid
, &call
);
634 case DEVMAN_ADD_DEVICE_TO_CATEGORY
:
635 devman_add_function_to_cat(callid
, &call
);
637 case DEVMAN_DRV_FUN_ONLINE
:
638 devman_drv_fun_online(callid
, &call
, driver
);
640 case DEVMAN_DRV_FUN_OFFLINE
:
641 devman_drv_fun_offline(callid
, &call
, driver
);
643 case DEVMAN_REMOVE_FUNCTION
:
644 devman_remove_function(callid
, &call
);
647 async_answer_0(callid
, EINVAL
);