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.
29 /** @addtogroup devman
32 /** @file Device Manager
35 * (1) driver_t.driver_mutex
36 * (2) dev_tree_t.rwlock
39 * - device_tree.rwlock protects:
40 * - tree root, complete tree topology
41 * - complete contents of device and function nodes
42 * - dev_node_t.refcnt, fun_node_t.refcnt prevent nodes from
44 * - find_xxx() functions increase reference count of returned object
45 * - find_xxx_no_lock() do not increase reference count
48 * - Track all steady and transient device/function states
49 * - Check states, wait for steady state on certain operations
56 #include <ipc/driver.h>
57 #include <ipc/devman.h>
59 #include <str_error.h>
64 static fun_node_t
*find_node_child(dev_tree_t
*, fun_node_t
*, const char *);
66 /* hash table operations */
68 static hash_index_t
devices_hash(unsigned long key
[])
70 return key
[0] % DEVICE_BUCKETS
;
73 static int devman_devices_compare(unsigned long key
[], hash_count_t keys
,
76 dev_node_t
*dev
= hash_table_get_instance(item
, dev_node_t
, devman_dev
);
77 return (dev
->handle
== (devman_handle_t
) key
[0]);
80 static int devman_functions_compare(unsigned long key
[], hash_count_t keys
,
83 fun_node_t
*fun
= hash_table_get_instance(item
, fun_node_t
, devman_fun
);
84 return (fun
->handle
== (devman_handle_t
) key
[0]);
87 static int loc_functions_compare(unsigned long key
[], hash_count_t keys
,
90 fun_node_t
*fun
= hash_table_get_instance(item
, fun_node_t
, loc_fun
);
91 return (fun
->service_id
== (service_id_t
) key
[0]);
94 static void devices_remove_callback(link_t
*item
)
98 static hash_table_operations_t devman_devices_ops
= {
100 .compare
= devman_devices_compare
,
101 .remove_callback
= devices_remove_callback
104 static hash_table_operations_t devman_functions_ops
= {
105 .hash
= devices_hash
,
106 .compare
= devman_functions_compare
,
107 .remove_callback
= devices_remove_callback
110 static hash_table_operations_t loc_devices_ops
= {
111 .hash
= devices_hash
,
112 .compare
= loc_functions_compare
,
113 .remove_callback
= devices_remove_callback
117 * Initialize the list of device driver's.
119 * @param drv_list the list of device driver's.
122 void init_driver_list(driver_list_t
*drv_list
)
124 assert(drv_list
!= NULL
);
126 list_initialize(&drv_list
->drivers
);
127 fibril_mutex_initialize(&drv_list
->drivers_mutex
);
130 /** Allocate and initialize a new driver structure.
132 * @return Driver structure.
134 driver_t
*create_driver(void)
136 driver_t
*res
= malloc(sizeof(driver_t
));
142 /** Add a driver to the list of drivers.
144 * @param drivers_list List of drivers.
145 * @param drv Driver structure.
147 void add_driver(driver_list_t
*drivers_list
, driver_t
*drv
)
149 fibril_mutex_lock(&drivers_list
->drivers_mutex
);
150 list_prepend(&drv
->drivers
, &drivers_list
->drivers
);
151 fibril_mutex_unlock(&drivers_list
->drivers_mutex
);
153 log_msg(LOG_DEFAULT
, LVL_NOTE
, "Driver `%s' was added to the list of available "
154 "drivers.", drv
->name
);
157 /** Read match id at the specified position of a string and set the position in
158 * the string to the first character following the id.
160 * @param buf The position in the input string.
161 * @return The match id.
163 char *read_match_id(char **buf
)
166 size_t len
= get_nonspace_len(*buf
);
169 res
= malloc(len
+ 1);
171 str_ncpy(res
, len
+ 1, *buf
, len
);
180 * Read match ids and associated match scores from a string.
182 * Each match score in the string is followed by its match id.
183 * The match ids and match scores are separated by whitespaces.
184 * Neither match ids nor match scores can contain whitespaces.
186 * @param buf The string from which the match ids are read.
187 * @param ids The list of match ids into which the match ids and
189 * @return True if at least one match id and associated match score
190 * was successfully read, false otherwise.
192 bool parse_match_ids(char *buf
, match_id_list_t
*ids
)
200 if (!skip_spaces(&buf
))
204 score
= strtoul(buf
, &buf
, 10);
207 if (!skip_spaces(&buf
))
211 id
= read_match_id(&buf
);
215 /* create new match_id structure */
216 match_id_t
*mid
= create_match_id();
220 /* add it to the list */
221 add_match_id(ids
, mid
);
230 * Read match ids and associated match scores from a file.
232 * Each match score in the file is followed by its match id.
233 * The match ids and match scores are separated by whitespaces.
234 * Neither match ids nor match scores can contain whitespaces.
236 * @param buf The path to the file from which the match ids are read.
237 * @param ids The list of match ids into which the match ids and
239 * @return True if at least one match id and associated match score
240 * was successfully read, false otherwise.
242 bool read_match_ids(const char *conf_path
, match_id_list_t
*ids
)
244 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "read_match_ids(conf_path=\"%s\")", conf_path
);
252 fd
= open(conf_path
, O_RDONLY
);
254 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Unable to open `%s' for reading: %s.",
255 conf_path
, str_error(fd
));
260 len
= lseek(fd
, 0, SEEK_END
);
261 lseek(fd
, 0, SEEK_SET
);
263 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Configuration file '%s' is empty.",
268 buf
= malloc(len
+ 1);
270 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Memory allocation failed when parsing file "
275 ssize_t read_bytes
= read_all(fd
, buf
, len
);
276 if (read_bytes
<= 0) {
277 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Unable to read file '%s' (%zd).", conf_path
,
283 suc
= parse_match_ids(buf
, ids
);
295 * Get information about a driver.
297 * Each driver has its own directory in the base directory.
298 * The name of the driver's directory is the same as the name of the driver.
299 * The driver's directory contains driver's binary (named as the driver without
300 * extension) and the configuration file with match ids for device-to-driver
301 * matching (named as the driver with a special extension).
303 * This function searches for the driver's directory and containing
304 * configuration files. If all the files needed are found, they are parsed and
305 * the information about the driver is stored in the driver's structure.
307 * @param base_path The base directory, in which we look for driver's
309 * @param name The name of the driver.
310 * @param drv The driver structure to fill information in.
312 * @return True on success, false otherwise.
314 bool get_driver_info(const char *base_path
, const char *name
, driver_t
*drv
)
316 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "get_driver_info(base_path=\"%s\", name=\"%s\")",
319 assert(base_path
!= NULL
&& name
!= NULL
&& drv
!= NULL
);
322 char *match_path
= NULL
;
323 size_t name_size
= 0;
325 /* Read the list of match ids from the driver's configuration file. */
326 match_path
= get_abs_path(base_path
, name
, MATCH_EXT
);
327 if (match_path
== NULL
)
330 if (!read_match_ids(match_path
, &drv
->match_ids
))
333 /* Allocate and fill driver's name. */
334 name_size
= str_size(name
) + 1;
335 drv
->name
= malloc(name_size
);
336 if (drv
->name
== NULL
)
338 str_cpy(drv
->name
, name_size
, name
);
340 /* Initialize path with driver's binary. */
341 drv
->binary_path
= get_abs_path(base_path
, name
, "");
342 if (drv
->binary_path
== NULL
)
345 /* Check whether the driver's binary exists. */
347 if (stat(drv
->binary_path
, &s
) == ENOENT
) { /* FIXME!! */
348 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Driver not found at path `%s'.",
357 free(drv
->binary_path
);
359 /* Set the driver structure to the default state. */
368 /** Lookup drivers in the directory.
370 * @param drivers_list The list of available drivers.
371 * @param dir_path The path to the directory where we search for drivers.
372 * @return Number of drivers which were found.
374 int lookup_available_drivers(driver_list_t
*drivers_list
, const char *dir_path
)
376 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "lookup_available_drivers(dir=\"%s\")", dir_path
);
380 struct dirent
*diren
;
382 dir
= opendir(dir_path
);
385 driver_t
*drv
= create_driver();
386 while ((diren
= readdir(dir
))) {
387 if (get_driver_info(dir_path
, diren
->d_name
, drv
)) {
388 add_driver(drivers_list
, drv
);
390 drv
= create_driver();
400 /** Create root device and function node in the device tree.
402 * @param tree The device tree.
403 * @return True on success, false otherwise.
405 bool create_root_nodes(dev_tree_t
*tree
)
410 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "create_root_nodes()");
412 fibril_rwlock_write_lock(&tree
->rwlock
);
415 * Create root function. This is a pseudo function to which
416 * the root device node is attached. It allows us to match
417 * the root device driver in a standard manner, i.e. against
418 * the parent function.
421 fun
= create_fun_node();
423 fibril_rwlock_write_unlock(&tree
->rwlock
);
428 insert_fun_node(tree
, fun
, str_dup(""), NULL
);
430 match_id_t
*id
= create_match_id();
431 id
->id
= str_dup("root");
433 add_match_id(&fun
->match_ids
, id
);
434 tree
->root_node
= fun
;
437 * Create root device node.
439 dev
= create_dev_node();
441 fibril_rwlock_write_unlock(&tree
->rwlock
);
446 insert_dev_node(tree
, dev
, fun
);
448 fibril_rwlock_write_unlock(&tree
->rwlock
);
453 /** Lookup the best matching driver for the specified device in the list of
456 * A match between a device and a driver is found if one of the driver's match
457 * ids match one of the device's match ids. The score of the match is the
458 * product of the driver's and device's score associated with the matching id.
459 * The best matching driver for a device is the driver with the highest score
460 * of the match between the device and the driver.
462 * @param drivers_list The list of drivers, where we look for the driver
463 * suitable for handling the device.
464 * @param node The device node structure of the device.
465 * @return The best matching driver or NULL if no matching driver
468 driver_t
*find_best_match_driver(driver_list_t
*drivers_list
, dev_node_t
*node
)
470 driver_t
*best_drv
= NULL
, *drv
= NULL
;
471 int best_score
= 0, score
= 0;
473 fibril_mutex_lock(&drivers_list
->drivers_mutex
);
475 list_foreach(drivers_list
->drivers
, link
) {
476 drv
= list_get_instance(link
, driver_t
, drivers
);
477 score
= get_match_score(drv
, node
);
478 if (score
> best_score
) {
484 fibril_mutex_unlock(&drivers_list
->drivers_mutex
);
489 /** Assign a driver to a device.
491 * @param tree Device tree
492 * @param node The device's node in the device tree.
493 * @param drv The driver.
495 void attach_driver(dev_tree_t
*tree
, dev_node_t
*dev
, driver_t
*drv
)
497 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "attach_driver(dev=\"%s\",drv=\"%s\")",
498 dev
->pfun
->pathname
, drv
->name
);
500 fibril_mutex_lock(&drv
->driver_mutex
);
501 fibril_rwlock_write_lock(&tree
->rwlock
);
504 list_append(&dev
->driver_devices
, &drv
->devices
);
506 fibril_rwlock_write_unlock(&tree
->rwlock
);
507 fibril_mutex_unlock(&drv
->driver_mutex
);
510 /** Detach driver from device.
512 * @param tree Device tree
513 * @param node The device's node in the device tree.
514 * @param drv The driver.
516 void detach_driver(dev_tree_t
*tree
, dev_node_t
*dev
)
518 driver_t
*drv
= dev
->drv
;
522 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "detach_driver(dev=\"%s\",drv=\"%s\")",
523 dev
->pfun
->pathname
, drv
->name
);
525 fibril_mutex_lock(&drv
->driver_mutex
);
526 fibril_rwlock_write_lock(&tree
->rwlock
);
529 list_remove(&dev
->driver_devices
);
531 fibril_rwlock_write_unlock(&tree
->rwlock
);
532 fibril_mutex_unlock(&drv
->driver_mutex
);
537 * @param drv The driver's structure.
538 * @return True if the driver's task is successfully spawned, false
541 bool start_driver(driver_t
*drv
)
545 assert(fibril_mutex_is_locked(&drv
->driver_mutex
));
547 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "start_driver(drv=\"%s\")", drv
->name
);
549 rc
= task_spawnl(NULL
, drv
->binary_path
, drv
->binary_path
, NULL
);
551 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Spawning driver `%s' (%s) failed: %s.",
552 drv
->name
, drv
->binary_path
, str_error(rc
));
556 drv
->state
= DRIVER_STARTING
;
560 /** Find device driver in the list of device drivers.
562 * @param drv_list The list of device drivers.
563 * @param drv_name The name of the device driver which is searched.
564 * @return The device driver of the specified name, if it is in the
565 * list, NULL otherwise.
567 driver_t
*find_driver(driver_list_t
*drv_list
, const char *drv_name
)
569 driver_t
*res
= NULL
;
570 driver_t
*drv
= NULL
;
572 fibril_mutex_lock(&drv_list
->drivers_mutex
);
574 list_foreach(drv_list
->drivers
, link
) {
575 drv
= list_get_instance(link
, driver_t
, drivers
);
576 if (str_cmp(drv
->name
, drv_name
) == 0) {
582 fibril_mutex_unlock(&drv_list
->drivers_mutex
);
587 /** Notify driver about the devices to which it was assigned.
589 * @param driver The driver to which the devices are passed.
591 static void pass_devices_to_driver(driver_t
*driver
, dev_tree_t
*tree
)
596 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "pass_devices_to_driver(driver=\"%s\")",
599 fibril_mutex_lock(&driver
->driver_mutex
);
602 * Go through devices list as long as there is some device
603 * that has not been passed to the driver.
605 link
= driver
->devices
.head
.next
;
606 while (link
!= &driver
->devices
.head
) {
607 dev
= list_get_instance(link
, dev_node_t
, driver_devices
);
608 fibril_rwlock_write_lock(&tree
->rwlock
);
610 if (dev
->passed_to_driver
) {
611 fibril_rwlock_write_unlock(&tree
->rwlock
);
616 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "pass_devices_to_driver: dev->refcnt=%d\n",
617 (int)atomic_get(&dev
->refcnt
));
621 * Unlock to avoid deadlock when adding device
624 fibril_mutex_unlock(&driver
->driver_mutex
);
625 fibril_rwlock_write_unlock(&tree
->rwlock
);
627 add_device(driver
, dev
, tree
);
632 * Lock again as we will work with driver's
635 fibril_mutex_lock(&driver
->driver_mutex
);
638 * Restart the cycle to go through all devices again.
640 link
= driver
->devices
.head
.next
;
644 * Once we passed all devices to the driver, we need to mark the
646 * It is vital to do it here and inside critical section.
648 * If we would change the state earlier, other devices added to
649 * the driver would be added to the device list and started
650 * immediately and possibly started here as well.
652 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "Driver `%s' enters running state.", driver
->name
);
653 driver
->state
= DRIVER_RUNNING
;
655 fibril_mutex_unlock(&driver
->driver_mutex
);
658 /** Finish the initialization of a driver after it has succesfully started
659 * and after it has registered itself by the device manager.
661 * Pass devices formerly matched to the driver to the driver and remember the
662 * driver is running and fully functional now.
664 * @param driver The driver which registered itself as running by the
667 void initialize_running_driver(driver_t
*driver
, dev_tree_t
*tree
)
669 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "initialize_running_driver(driver=\"%s\")",
673 * Pass devices which have been already assigned to the driver to the
676 pass_devices_to_driver(driver
, tree
);
679 /** Initialize device driver structure.
681 * @param drv The device driver structure.
683 void init_driver(driver_t
*drv
)
687 memset(drv
, 0, sizeof(driver_t
));
688 list_initialize(&drv
->match_ids
.ids
);
689 list_initialize(&drv
->devices
);
690 fibril_mutex_initialize(&drv
->driver_mutex
);
694 /** Device driver structure clean-up.
696 * @param drv The device driver structure.
698 void clean_driver(driver_t
*drv
)
702 free_not_null(drv
->name
);
703 free_not_null(drv
->binary_path
);
705 clean_match_ids(&drv
->match_ids
);
710 /** Delete device driver structure.
712 * @param drv The device driver structure.
714 void delete_driver(driver_t
*drv
)
722 /** Create loc path and name for the function. */
723 void loc_register_tree_function(fun_node_t
*fun
, dev_tree_t
*tree
)
725 char *loc_pathname
= NULL
;
726 char *loc_name
= NULL
;
728 assert(fibril_rwlock_is_locked(&tree
->rwlock
));
730 asprintf(&loc_name
, "%s", fun
->pathname
);
731 if (loc_name
== NULL
)
734 replace_char(loc_name
, '/', LOC_SEPARATOR
);
736 asprintf(&loc_pathname
, "%s/%s", LOC_DEVICE_NAMESPACE
,
738 if (loc_pathname
== NULL
) {
743 loc_service_register_with_iface(loc_pathname
,
744 &fun
->service_id
, DEVMAN_CONNECT_FROM_LOC
);
746 tree_add_loc_function(tree
, fun
);
752 /** Pass a device to running driver.
754 * @param drv The driver's structure.
755 * @param node The device's node in the device tree.
757 void add_device(driver_t
*drv
, dev_node_t
*dev
, dev_tree_t
*tree
)
760 * We do not expect to have driver's mutex locked as we do not
761 * access any structures that would affect driver_t.
763 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "add_device(drv=\"%s\", dev=\"%s\")",
764 drv
->name
, dev
->pfun
->name
);
766 /* Send the device to the driver. */
767 devman_handle_t parent_handle
;
769 parent_handle
= dev
->pfun
->handle
;
774 async_exch_t
*exch
= async_exchange_begin(drv
->sess
);
777 aid_t req
= async_send_2(exch
, DRIVER_DEV_ADD
, dev
->handle
,
778 parent_handle
, &answer
);
780 /* Send the device name to the driver. */
781 sysarg_t rc
= async_data_write_start(exch
, dev
->pfun
->name
,
782 str_size(dev
->pfun
->name
) + 1);
784 async_exchange_end(exch
);
787 /* TODO handle error */
790 /* Wait for answer from the driver. */
791 async_wait_for(req
, &rc
);
795 dev
->state
= DEVICE_USABLE
;
798 dev
->state
= DEVICE_NOT_PRESENT
;
801 dev
->state
= DEVICE_INVALID
;
805 dev
->passed_to_driver
= true;
810 /** Find suitable driver for a device and assign the driver to it.
812 * @param node The device node of the device in the device tree.
813 * @param drivers_list The list of available drivers.
814 * @return True if the suitable driver is found and
815 * successfully assigned to the device, false otherwise.
817 bool assign_driver(dev_node_t
*dev
, driver_list_t
*drivers_list
,
821 assert(drivers_list
!= NULL
);
822 assert(tree
!= NULL
);
825 * Find the driver which is the most suitable for handling this device.
827 driver_t
*drv
= find_best_match_driver(drivers_list
, dev
);
829 log_msg(LOG_DEFAULT
, LVL_ERROR
, "No driver found for device `%s'.",
830 dev
->pfun
->pathname
);
834 /* Attach the driver to the device. */
835 attach_driver(tree
, dev
, drv
);
837 fibril_mutex_lock(&drv
->driver_mutex
);
838 if (drv
->state
== DRIVER_NOT_STARTED
) {
839 /* Start the driver. */
842 bool is_running
= drv
->state
== DRIVER_RUNNING
;
843 fibril_mutex_unlock(&drv
->driver_mutex
);
845 /* Notify the driver about the new device. */
847 add_device(drv
, dev
, tree
);
849 fibril_mutex_lock(&drv
->driver_mutex
);
850 fibril_mutex_unlock(&drv
->driver_mutex
);
852 fibril_rwlock_write_lock(&tree
->rwlock
);
853 if (dev
->pfun
!= NULL
) {
854 dev
->pfun
->state
= FUN_ON_LINE
;
856 fibril_rwlock_write_unlock(&tree
->rwlock
);
860 int driver_dev_remove(dev_tree_t
*tree
, dev_node_t
*dev
)
865 devman_handle_t handle
;
869 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "driver_dev_remove(%p)", dev
);
871 fibril_rwlock_read_lock(&tree
->rwlock
);
873 handle
= dev
->handle
;
874 fibril_rwlock_read_unlock(&tree
->rwlock
);
876 exch
= async_exchange_begin(drv
->sess
);
877 retval
= async_req_1_0(exch
, DRIVER_DEV_REMOVE
, handle
);
878 async_exchange_end(exch
);
883 int driver_dev_gone(dev_tree_t
*tree
, dev_node_t
*dev
)
888 devman_handle_t handle
;
892 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "driver_dev_gone(%p)", dev
);
894 fibril_rwlock_read_lock(&tree
->rwlock
);
896 handle
= dev
->handle
;
897 fibril_rwlock_read_unlock(&tree
->rwlock
);
899 exch
= async_exchange_begin(drv
->sess
);
900 retval
= async_req_1_0(exch
, DRIVER_DEV_GONE
, handle
);
901 async_exchange_end(exch
);
906 int driver_fun_online(dev_tree_t
*tree
, fun_node_t
*fun
)
911 devman_handle_t handle
;
913 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "driver_fun_online(%p)", fun
);
915 fibril_rwlock_read_lock(&tree
->rwlock
);
917 if (fun
->dev
== NULL
) {
918 /* XXX root function? */
919 fibril_rwlock_read_unlock(&tree
->rwlock
);
924 handle
= fun
->handle
;
925 fibril_rwlock_read_unlock(&tree
->rwlock
);
927 exch
= async_exchange_begin(drv
->sess
);
928 retval
= async_req_1_0(exch
, DRIVER_FUN_ONLINE
, handle
);
929 loc_exchange_end(exch
);
934 int driver_fun_offline(dev_tree_t
*tree
, fun_node_t
*fun
)
939 devman_handle_t handle
;
941 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "driver_fun_offline(%p)", fun
);
943 fibril_rwlock_read_lock(&tree
->rwlock
);
944 if (fun
->dev
== NULL
) {
945 /* XXX root function? */
946 fibril_rwlock_read_unlock(&tree
->rwlock
);
951 handle
= fun
->handle
;
952 fibril_rwlock_read_unlock(&tree
->rwlock
);
954 exch
= async_exchange_begin(drv
->sess
);
955 retval
= async_req_1_0(exch
, DRIVER_FUN_OFFLINE
, handle
);
956 loc_exchange_end(exch
);
962 /** Initialize the device tree.
964 * Create root device node of the tree and assign driver to it.
966 * @param tree The device tree.
967 * @param drivers_list the list of available drivers.
968 * @return True on success, false otherwise.
970 bool init_device_tree(dev_tree_t
*tree
, driver_list_t
*drivers_list
)
972 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "init_device_tree()");
974 tree
->current_handle
= 0;
976 hash_table_create(&tree
->devman_devices
, DEVICE_BUCKETS
, 1,
977 &devman_devices_ops
);
978 hash_table_create(&tree
->devman_functions
, DEVICE_BUCKETS
, 1,
979 &devman_functions_ops
);
980 hash_table_create(&tree
->loc_functions
, DEVICE_BUCKETS
, 1,
983 fibril_rwlock_initialize(&tree
->rwlock
);
985 /* Create root function and root device and add them to the device tree. */
986 if (!create_root_nodes(tree
))
989 /* Find suitable driver and start it. */
990 dev_node_t
*rdev
= tree
->root_node
->child
;
992 int rc
= assign_driver(rdev
, drivers_list
, tree
);
1000 /** Create a new device node.
1002 * @return A device node structure.
1004 dev_node_t
*create_dev_node(void)
1008 dev
= calloc(1, sizeof(dev_node_t
));
1012 atomic_set(&dev
->refcnt
, 0);
1013 list_initialize(&dev
->functions
);
1014 link_initialize(&dev
->driver_devices
);
1015 link_initialize(&dev
->devman_dev
);
1020 /** Delete a device node.
1022 * @param node The device node structure.
1024 void delete_dev_node(dev_node_t
*dev
)
1026 assert(list_empty(&dev
->functions
));
1027 assert(dev
->pfun
== NULL
);
1028 assert(dev
->drv
== NULL
);
1033 /** Increase device node reference count.
1035 * @param dev Device node
1037 void dev_add_ref(dev_node_t
*dev
)
1039 atomic_inc(&dev
->refcnt
);
1042 /** Decrease device node reference count.
1044 * When the count drops to zero the device node is freed.
1046 * @param dev Device node
1048 void dev_del_ref(dev_node_t
*dev
)
1050 if (atomic_predec(&dev
->refcnt
) == 0)
1051 delete_dev_node(dev
);
1054 /** Find the device node structure of the device witch has the specified handle.
1056 * @param tree The device tree where we look for the device node.
1057 * @param handle The handle of the device.
1058 * @return The device node.
1060 dev_node_t
*find_dev_node_no_lock(dev_tree_t
*tree
, devman_handle_t handle
)
1062 unsigned long key
= handle
;
1065 assert(fibril_rwlock_is_locked(&tree
->rwlock
));
1067 link
= hash_table_find(&tree
->devman_devices
, &key
);
1071 return hash_table_get_instance(link
, dev_node_t
, devman_dev
);
1074 /** Find the device node structure of the device witch has the specified handle.
1076 * @param tree The device tree where we look for the device node.
1077 * @param handle The handle of the device.
1078 * @return The device node.
1080 dev_node_t
*find_dev_node(dev_tree_t
*tree
, devman_handle_t handle
)
1082 dev_node_t
*dev
= NULL
;
1084 fibril_rwlock_read_lock(&tree
->rwlock
);
1085 dev
= find_dev_node_no_lock(tree
, handle
);
1089 fibril_rwlock_read_unlock(&tree
->rwlock
);
1094 /** Get list of device functions. */
1095 int dev_get_functions(dev_tree_t
*tree
, dev_node_t
*dev
,
1096 devman_handle_t
*hdl_buf
, size_t buf_size
, size_t *act_size
)
1101 assert(fibril_rwlock_is_locked(&tree
->rwlock
));
1103 buf_cnt
= buf_size
/ sizeof(devman_handle_t
);
1105 act_cnt
= list_count(&dev
->functions
);
1106 *act_size
= act_cnt
* sizeof(devman_handle_t
);
1108 if (buf_size
% sizeof(devman_handle_t
) != 0)
1112 list_foreach(dev
->functions
, item
) {
1114 list_get_instance(item
, fun_node_t
, dev_functions
);
1116 if (pos
< buf_cnt
) {
1117 hdl_buf
[pos
] = fun
->handle
;
1127 /* Function nodes */
1129 /** Create a new function node.
1131 * @return A function node structure.
1133 fun_node_t
*create_fun_node(void)
1137 fun
= calloc(1, sizeof(fun_node_t
));
1141 fun
->state
= FUN_INIT
;
1142 atomic_set(&fun
->refcnt
, 0);
1143 fibril_mutex_initialize(&fun
->busy_lock
);
1144 link_initialize(&fun
->dev_functions
);
1145 list_initialize(&fun
->match_ids
.ids
);
1146 link_initialize(&fun
->devman_fun
);
1147 link_initialize(&fun
->loc_fun
);
1152 /** Delete a function node.
1154 * @param fun The device node structure.
1156 void delete_fun_node(fun_node_t
*fun
)
1158 assert(fun
->dev
== NULL
);
1159 assert(fun
->child
== NULL
);
1161 clean_match_ids(&fun
->match_ids
);
1162 free_not_null(fun
->name
);
1163 free_not_null(fun
->pathname
);
1167 /** Increase function node reference count.
1169 * @param fun Function node
1171 void fun_add_ref(fun_node_t
*fun
)
1173 atomic_inc(&fun
->refcnt
);
1176 /** Decrease function node reference count.
1178 * When the count drops to zero the function node is freed.
1180 * @param fun Function node
1182 void fun_del_ref(fun_node_t
*fun
)
1184 if (atomic_predec(&fun
->refcnt
) == 0)
1185 delete_fun_node(fun
);
1188 /** Make function busy for reconfiguration operations. */
1189 void fun_busy_lock(fun_node_t
*fun
)
1191 fibril_mutex_lock(&fun
->busy_lock
);
1194 /** Mark end of reconfiguration operation. */
1195 void fun_busy_unlock(fun_node_t
*fun
)
1197 fibril_mutex_unlock(&fun
->busy_lock
);
1200 /** Find the function node with the specified handle.
1202 * @param tree The device tree where we look for the device node.
1203 * @param handle The handle of the function.
1204 * @return The function node.
1206 fun_node_t
*find_fun_node_no_lock(dev_tree_t
*tree
, devman_handle_t handle
)
1208 unsigned long key
= handle
;
1212 assert(fibril_rwlock_is_locked(&tree
->rwlock
));
1214 link
= hash_table_find(&tree
->devman_functions
, &key
);
1218 fun
= hash_table_get_instance(link
, fun_node_t
, devman_fun
);
1223 /** Find the function node with the specified handle.
1225 * @param tree The device tree where we look for the device node.
1226 * @param handle The handle of the function.
1227 * @return The function node.
1229 fun_node_t
*find_fun_node(dev_tree_t
*tree
, devman_handle_t handle
)
1231 fun_node_t
*fun
= NULL
;
1233 fibril_rwlock_read_lock(&tree
->rwlock
);
1235 fun
= find_fun_node_no_lock(tree
, handle
);
1239 fibril_rwlock_read_unlock(&tree
->rwlock
);
1244 /** Create and set device's full path in device tree.
1246 * @param tree Device tree
1247 * @param node The device's device node.
1248 * @param parent The parent device node.
1249 * @return True on success, false otherwise (insufficient
1252 static bool set_fun_path(dev_tree_t
*tree
, fun_node_t
*fun
, fun_node_t
*parent
)
1254 assert(fibril_rwlock_is_write_locked(&tree
->rwlock
));
1255 assert(fun
->name
!= NULL
);
1257 size_t pathsize
= (str_size(fun
->name
) + 1);
1259 pathsize
+= str_size(parent
->pathname
) + 1;
1261 fun
->pathname
= (char *) malloc(pathsize
);
1262 if (fun
->pathname
== NULL
) {
1263 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Failed to allocate device path.");
1267 if (parent
!= NULL
) {
1268 str_cpy(fun
->pathname
, pathsize
, parent
->pathname
);
1269 str_append(fun
->pathname
, pathsize
, "/");
1270 str_append(fun
->pathname
, pathsize
, fun
->name
);
1272 str_cpy(fun
->pathname
, pathsize
, fun
->name
);
1278 /** Insert new device into device tree.
1280 * @param tree The device tree.
1281 * @param dev The newly added device node.
1282 * @param pfun The parent function node.
1284 * @return True on success, false otherwise (insufficient resources
1287 bool insert_dev_node(dev_tree_t
*tree
, dev_node_t
*dev
, fun_node_t
*pfun
)
1289 assert(fibril_rwlock_is_write_locked(&tree
->rwlock
));
1291 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "insert_dev_node(dev=%p, pfun=%p [\"%s\"])",
1292 dev
, pfun
, pfun
->pathname
);
1294 /* Add the node to the handle-to-node map. */
1295 dev
->handle
= ++tree
->current_handle
;
1296 unsigned long key
= dev
->handle
;
1297 hash_table_insert(&tree
->devman_devices
, &key
, &dev
->devman_dev
);
1299 /* Add the node to the list of its parent's children. */
1306 /** Remove device from device tree.
1308 * @param tree Device tree
1309 * @param dev Device node
1311 void remove_dev_node(dev_tree_t
*tree
, dev_node_t
*dev
)
1313 assert(fibril_rwlock_is_write_locked(&tree
->rwlock
));
1315 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "remove_dev_node(dev=%p)", dev
);
1317 /* Remove node from the handle-to-node map. */
1318 unsigned long key
= dev
->handle
;
1319 hash_table_remove(&tree
->devman_devices
, &key
, 1);
1321 /* Unlink from parent function. */
1322 dev
->pfun
->child
= NULL
;
1325 dev
->state
= DEVICE_REMOVED
;
1329 /** Insert new function into device tree.
1331 * @param tree The device tree.
1332 * @param fun The newly added function node.
1333 * @param fun_name The name of the newly added function.
1334 * @param dev Owning device node.
1336 * @return True on success, false otherwise (insufficient resources
1339 bool insert_fun_node(dev_tree_t
*tree
, fun_node_t
*fun
, char *fun_name
,
1344 assert(fun_name
!= NULL
);
1345 assert(fibril_rwlock_is_write_locked(&tree
->rwlock
));
1348 * The root function is a special case, it does not belong to any
1349 * device so for the root function dev == NULL.
1351 pfun
= (dev
!= NULL
) ? dev
->pfun
: NULL
;
1353 fun
->name
= fun_name
;
1354 if (!set_fun_path(tree
, fun
, pfun
)) {
1358 /* Add the node to the handle-to-node map. */
1359 fun
->handle
= ++tree
->current_handle
;
1360 unsigned long key
= fun
->handle
;
1361 hash_table_insert(&tree
->devman_functions
, &key
, &fun
->devman_fun
);
1363 /* Add the node to the list of its parent's children. */
1366 list_append(&fun
->dev_functions
, &dev
->functions
);
1371 /** Remove function from device tree.
1373 * @param tree Device tree
1374 * @param node Function node to remove
1376 void remove_fun_node(dev_tree_t
*tree
, fun_node_t
*fun
)
1378 assert(fibril_rwlock_is_write_locked(&tree
->rwlock
));
1380 /* Remove the node from the handle-to-node map. */
1381 unsigned long key
= fun
->handle
;
1382 hash_table_remove(&tree
->devman_functions
, &key
, 1);
1384 /* Remove the node from the list of its parent's children. */
1385 if (fun
->dev
!= NULL
)
1386 list_remove(&fun
->dev_functions
);
1389 fun
->state
= FUN_REMOVED
;
1392 /** Find function node with a specified path in the device tree.
1394 * @param path The path of the function node in the device tree.
1395 * @param tree The device tree.
1396 * @return The function node if it is present in the tree, NULL
1399 fun_node_t
*find_fun_node_by_path(dev_tree_t
*tree
, char *path
)
1401 assert(path
!= NULL
);
1403 bool is_absolute
= path
[0] == '/';
1408 fibril_rwlock_read_lock(&tree
->rwlock
);
1410 fun_node_t
*fun
= tree
->root_node
;
1413 * Relative path to the function from its parent (but with '/' at the
1416 char *rel_path
= path
;
1417 char *next_path_elem
= NULL
;
1418 bool cont
= (rel_path
[1] != '\0');
1420 while (cont
&& fun
!= NULL
) {
1421 next_path_elem
= get_path_elem_end(rel_path
+ 1);
1422 if (next_path_elem
[0] == '/') {
1424 next_path_elem
[0] = 0;
1429 fun_node_t
*cfun
= find_node_child(tree
, fun
, rel_path
+ 1);
1434 /* Restore the original path. */
1435 next_path_elem
[0] = '/';
1437 rel_path
= next_path_elem
;
1440 fibril_rwlock_read_unlock(&tree
->rwlock
);
1445 /** Find function with a specified name belonging to given device.
1447 * Device tree rwlock should be held at least for reading.
1449 * @param tree Device tree
1450 * @param dev Device the function belongs to.
1451 * @param name Function name (not path).
1452 * @return Function node.
1453 * @retval NULL No function with given name.
1455 fun_node_t
*find_fun_node_in_device(dev_tree_t
*tree
, dev_node_t
*dev
,
1458 assert(name
!= NULL
);
1459 assert(fibril_rwlock_is_locked(&tree
->rwlock
));
1463 list_foreach(dev
->functions
, link
) {
1464 fun
= list_get_instance(link
, fun_node_t
, dev_functions
);
1466 if (str_cmp(name
, fun
->name
) == 0) {
1475 /** Find child function node with a specified name.
1477 * Device tree rwlock should be held at least for reading.
1479 * @param tree Device tree
1480 * @param parent The parent function node.
1481 * @param name The name of the child function.
1482 * @return The child function node.
1484 static fun_node_t
*find_node_child(dev_tree_t
*tree
, fun_node_t
*pfun
,
1487 return find_fun_node_in_device(tree
, pfun
->child
, name
);
1492 fun_node_t
*find_loc_tree_function(dev_tree_t
*tree
, service_id_t service_id
)
1494 fun_node_t
*fun
= NULL
;
1496 unsigned long key
= (unsigned long) service_id
;
1498 fibril_rwlock_read_lock(&tree
->rwlock
);
1499 link
= hash_table_find(&tree
->loc_functions
, &key
);
1501 fun
= hash_table_get_instance(link
, fun_node_t
, loc_fun
);
1504 fibril_rwlock_read_unlock(&tree
->rwlock
);
1509 void tree_add_loc_function(dev_tree_t
*tree
, fun_node_t
*fun
)
1511 assert(fibril_rwlock_is_write_locked(&tree
->rwlock
));
1513 unsigned long key
= (unsigned long) fun
->service_id
;
1514 hash_table_insert(&tree
->loc_functions
, &key
, &fun
->loc_fun
);