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
38 #include <str_error.h>
48 * Initialize the list of device driver's.
50 * @param drv_list the list of device driver's.
53 void init_driver_list(driver_list_t
*drv_list
)
55 assert(drv_list
!= NULL
);
57 list_initialize(&drv_list
->drivers
);
58 fibril_mutex_initialize(&drv_list
->drivers_mutex
);
59 drv_list
->next_handle
= 1;
62 /** Allocate and initialize a new driver structure.
64 * @return Driver structure.
66 driver_t
*create_driver(void)
68 driver_t
*res
= malloc(sizeof(driver_t
));
74 /** Add a driver to the list of drivers.
76 * @param drivers_list List of drivers.
77 * @param drv Driver structure.
79 void add_driver(driver_list_t
*drivers_list
, driver_t
*drv
)
81 fibril_mutex_lock(&drivers_list
->drivers_mutex
);
82 list_append(&drv
->drivers
, &drivers_list
->drivers
);
83 drv
->handle
= drivers_list
->next_handle
++;
84 fibril_mutex_unlock(&drivers_list
->drivers_mutex
);
86 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "Driver `%s' was added to the list of available "
87 "drivers.", drv
->name
);
91 * Get information about a driver.
93 * Each driver has its own directory in the base directory.
94 * The name of the driver's directory is the same as the name of the driver.
95 * The driver's directory contains driver's binary (named as the driver without
96 * extension) and the configuration file with match ids for device-to-driver
97 * matching (named as the driver with a special extension).
99 * This function searches for the driver's directory and containing
100 * configuration files. If all the files needed are found, they are parsed and
101 * the information about the driver is stored in the driver's structure.
103 * @param base_path The base directory, in which we look for driver's
105 * @param name The name of the driver.
106 * @param drv The driver structure to fill information in.
108 * @return True on success, false otherwise.
110 bool get_driver_info(const char *base_path
, const char *name
, driver_t
*drv
)
112 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "get_driver_info(base_path=\"%s\", name=\"%s\")",
115 assert(base_path
!= NULL
&& name
!= NULL
&& drv
!= NULL
);
118 char *match_path
= NULL
;
119 size_t name_size
= 0;
121 /* Read the list of match ids from the driver's configuration file. */
122 match_path
= get_abs_path(base_path
, name
, MATCH_EXT
);
123 if (match_path
== NULL
)
126 if (!read_match_ids(match_path
, &drv
->match_ids
))
129 /* Allocate and fill driver's name. */
130 name_size
= str_size(name
) + 1;
131 drv
->name
= malloc(name_size
);
132 if (drv
->name
== NULL
)
134 str_cpy(drv
->name
, name_size
, name
);
136 /* Initialize path with driver's binary. */
137 drv
->binary_path
= get_abs_path(base_path
, name
, "");
138 if (drv
->binary_path
== NULL
)
141 /* Check whether the driver's binary exists. */
143 if (vfs_stat_path(drv
->binary_path
, &s
) != EOK
) {
144 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Driver not found at path `%s'.",
153 free(drv
->binary_path
);
155 /* Set the driver structure to the default state. */
164 /** Lookup drivers in the directory.
166 * @param drivers_list The list of available drivers.
167 * @param dir_path The path to the directory where we search for drivers.
168 * @return Number of drivers which were found.
170 int lookup_available_drivers(driver_list_t
*drivers_list
, const char *dir_path
)
172 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "lookup_available_drivers(dir=\"%s\")", dir_path
);
176 struct dirent
*diren
;
178 dir
= opendir(dir_path
);
181 driver_t
*drv
= create_driver();
182 while ((diren
= readdir(dir
))) {
183 if (get_driver_info(dir_path
, diren
->d_name
, drv
)) {
184 add_driver(drivers_list
, drv
);
186 drv
= create_driver();
196 /** Lookup the best matching driver for the specified device in the list of
199 * A match between a device and a driver is found if one of the driver's match
200 * ids match one of the device's match ids. The score of the match is the
201 * product of the driver's and device's score associated with the matching id.
202 * The best matching driver for a device is the driver with the highest score
203 * of the match between the device and the driver.
205 * @param drivers_list The list of drivers, where we look for the driver
206 * suitable for handling the device.
207 * @param node The device node structure of the device.
208 * @return The best matching driver or NULL if no matching driver
211 driver_t
*find_best_match_driver(driver_list_t
*drivers_list
, dev_node_t
*node
)
213 driver_t
*best_drv
= NULL
;
214 int best_score
= 0, score
= 0;
216 fibril_mutex_lock(&drivers_list
->drivers_mutex
);
218 list_foreach(drivers_list
->drivers
, drivers
, driver_t
, drv
) {
219 score
= get_match_score(drv
, node
);
220 if (score
> best_score
) {
226 fibril_mutex_unlock(&drivers_list
->drivers_mutex
);
231 /** Assign a driver to a device.
233 * @param tree Device tree
234 * @param node The device's node in the device tree.
235 * @param drv The driver.
237 void attach_driver(dev_tree_t
*tree
, dev_node_t
*dev
, driver_t
*drv
)
239 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "attach_driver(dev=\"%s\",drv=\"%s\")",
240 dev
->pfun
->pathname
, drv
->name
);
242 fibril_mutex_lock(&drv
->driver_mutex
);
243 fibril_rwlock_write_lock(&tree
->rwlock
);
246 list_append(&dev
->driver_devices
, &drv
->devices
);
248 fibril_rwlock_write_unlock(&tree
->rwlock
);
249 fibril_mutex_unlock(&drv
->driver_mutex
);
252 /** Detach driver from device.
254 * @param tree Device tree
255 * @param node The device's node in the device tree.
256 * @param drv The driver.
258 void detach_driver(dev_tree_t
*tree
, dev_node_t
*dev
)
260 driver_t
*drv
= dev
->drv
;
264 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "detach_driver(dev=\"%s\",drv=\"%s\")",
265 dev
->pfun
->pathname
, drv
->name
);
267 fibril_mutex_lock(&drv
->driver_mutex
);
268 fibril_rwlock_write_lock(&tree
->rwlock
);
271 list_remove(&dev
->driver_devices
);
273 fibril_rwlock_write_unlock(&tree
->rwlock
);
274 fibril_mutex_unlock(&drv
->driver_mutex
);
279 * @param drv The driver's structure.
280 * @return True if the driver's task is successfully spawned, false
283 bool start_driver(driver_t
*drv
)
287 assert(fibril_mutex_is_locked(&drv
->driver_mutex
));
289 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "start_driver(drv=\"%s\")", drv
->name
);
291 rc
= task_spawnl(NULL
, NULL
, drv
->binary_path
, drv
->binary_path
, NULL
);
293 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Spawning driver `%s' (%s) failed: %s.",
294 drv
->name
, drv
->binary_path
, str_error(rc
));
298 drv
->state
= DRIVER_STARTING
;
302 /** Find device driver by handle.
304 * @param drv_list The list of device drivers
305 * @param handle Driver handle
306 * @return The device driver, if it is in the list,
309 driver_t
*driver_find(driver_list_t
*drv_list
, devman_handle_t handle
)
311 driver_t
*res
= NULL
;
313 fibril_mutex_lock(&drv_list
->drivers_mutex
);
315 list_foreach(drv_list
->drivers
, drivers
, driver_t
, drv
) {
316 if (drv
->handle
== handle
) {
322 fibril_mutex_unlock(&drv_list
->drivers_mutex
);
328 /** Find device driver by name.
330 * @param drv_list The list of device drivers.
331 * @param drv_name The name of the device driver which is searched.
332 * @return The device driver of the specified name, if it is in the
333 * list, NULL otherwise.
335 driver_t
*driver_find_by_name(driver_list_t
*drv_list
, const char *drv_name
)
337 driver_t
*res
= NULL
;
339 fibril_mutex_lock(&drv_list
->drivers_mutex
);
341 list_foreach(drv_list
->drivers
, drivers
, driver_t
, drv
) {
342 if (str_cmp(drv
->name
, drv_name
) == 0) {
348 fibril_mutex_unlock(&drv_list
->drivers_mutex
);
353 /** Notify driver about the devices to which it was assigned.
355 * @param driver The driver to which the devices are passed.
357 static void pass_devices_to_driver(driver_t
*driver
, dev_tree_t
*tree
)
362 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "pass_devices_to_driver(driver=\"%s\")",
365 fibril_mutex_lock(&driver
->driver_mutex
);
368 * Go through devices list as long as there is some device
369 * that has not been passed to the driver.
371 link
= driver
->devices
.head
.next
;
372 while (link
!= &driver
->devices
.head
) {
373 dev
= list_get_instance(link
, dev_node_t
, driver_devices
);
374 fibril_rwlock_write_lock(&tree
->rwlock
);
376 if (dev
->passed_to_driver
) {
377 fibril_rwlock_write_unlock(&tree
->rwlock
);
382 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "pass_devices_to_driver: dev->refcnt=%d\n",
383 (int)atomic_get(&dev
->refcnt
));
387 * Unlock to avoid deadlock when adding device
390 fibril_mutex_unlock(&driver
->driver_mutex
);
391 fibril_rwlock_write_unlock(&tree
->rwlock
);
393 add_device(driver
, dev
, tree
);
398 * Lock again as we will work with driver's
401 fibril_mutex_lock(&driver
->driver_mutex
);
404 * Restart the cycle to go through all devices again.
406 link
= driver
->devices
.head
.next
;
410 * Once we passed all devices to the driver, we need to mark the
412 * It is vital to do it here and inside critical section.
414 * If we would change the state earlier, other devices added to
415 * the driver would be added to the device list and started
416 * immediately and possibly started here as well.
418 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "Driver `%s' enters running state.", driver
->name
);
419 driver
->state
= DRIVER_RUNNING
;
421 fibril_mutex_unlock(&driver
->driver_mutex
);
424 /** Finish the initialization of a driver after it has succesfully started
425 * and after it has registered itself by the device manager.
427 * Pass devices formerly matched to the driver to the driver and remember the
428 * driver is running and fully functional now.
430 * @param driver The driver which registered itself as running by the
433 void initialize_running_driver(driver_t
*driver
, dev_tree_t
*tree
)
435 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "initialize_running_driver(driver=\"%s\")",
439 * Pass devices which have been already assigned to the driver to the
442 pass_devices_to_driver(driver
, tree
);
445 /** Initialize device driver structure.
447 * @param drv The device driver structure.
449 void init_driver(driver_t
*drv
)
453 memset(drv
, 0, sizeof(driver_t
));
454 list_initialize(&drv
->match_ids
.ids
);
455 list_initialize(&drv
->devices
);
456 fibril_mutex_initialize(&drv
->driver_mutex
);
460 /** Device driver structure clean-up.
462 * @param drv The device driver structure.
464 void clean_driver(driver_t
*drv
)
469 free(drv
->binary_path
);
471 clean_match_ids(&drv
->match_ids
);
476 /** Delete device driver structure.
478 * @param drv The device driver structure.
480 void delete_driver(driver_t
*drv
)
488 /** Find suitable driver for a device and assign the driver to it.
490 * @param node The device node of the device in the device tree.
491 * @param drivers_list The list of available drivers.
492 * @return True if the suitable driver is found and
493 * successfully assigned to the device, false otherwise.
495 bool assign_driver(dev_node_t
*dev
, driver_list_t
*drivers_list
,
499 assert(drivers_list
!= NULL
);
500 assert(tree
!= NULL
);
503 * Find the driver which is the most suitable for handling this device.
505 driver_t
*drv
= find_best_match_driver(drivers_list
, dev
);
507 log_msg(LOG_DEFAULT
, LVL_ERROR
, "No driver found for device `%s'.",
508 dev
->pfun
->pathname
);
512 /* Attach the driver to the device. */
513 attach_driver(tree
, dev
, drv
);
515 fibril_mutex_lock(&drv
->driver_mutex
);
516 if (drv
->state
== DRIVER_NOT_STARTED
) {
517 /* Start the driver. */
520 bool is_running
= drv
->state
== DRIVER_RUNNING
;
521 fibril_mutex_unlock(&drv
->driver_mutex
);
523 /* Notify the driver about the new device. */
525 add_device(drv
, dev
, tree
);
527 fibril_mutex_lock(&drv
->driver_mutex
);
528 fibril_mutex_unlock(&drv
->driver_mutex
);
530 fibril_rwlock_write_lock(&tree
->rwlock
);
531 if (dev
->pfun
!= NULL
) {
532 dev
->pfun
->state
= FUN_ON_LINE
;
534 fibril_rwlock_write_unlock(&tree
->rwlock
);
538 /** Pass a device to running driver.
540 * @param drv The driver's structure.
541 * @param node The device's node in the device tree.
543 void add_device(driver_t
*drv
, dev_node_t
*dev
, dev_tree_t
*tree
)
546 * We do not expect to have driver's mutex locked as we do not
547 * access any structures that would affect driver_t.
549 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "add_device(drv=\"%s\", dev=\"%s\")",
550 drv
->name
, dev
->pfun
->name
);
552 /* Send the device to the driver. */
553 devman_handle_t parent_handle
;
555 parent_handle
= dev
->pfun
->handle
;
560 async_exch_t
*exch
= async_exchange_begin(drv
->sess
);
563 aid_t req
= async_send_2(exch
, DRIVER_DEV_ADD
, dev
->handle
,
564 parent_handle
, &answer
);
566 /* Send the device name to the driver. */
567 sysarg_t rc
= async_data_write_start(exch
, dev
->pfun
->name
,
568 str_size(dev
->pfun
->name
) + 1);
570 async_exchange_end(exch
);
575 /* Wait for answer from the driver. */
576 async_wait_for(req
, &rc
);
581 dev
->state
= DEVICE_USABLE
;
584 dev
->state
= DEVICE_NOT_PRESENT
;
587 dev
->state
= DEVICE_INVALID
;
591 dev
->passed_to_driver
= true;
594 int driver_dev_remove(dev_tree_t
*tree
, dev_node_t
*dev
)
599 devman_handle_t handle
;
603 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "driver_dev_remove(%p)", dev
);
605 fibril_rwlock_read_lock(&tree
->rwlock
);
607 handle
= dev
->handle
;
608 fibril_rwlock_read_unlock(&tree
->rwlock
);
610 exch
= async_exchange_begin(drv
->sess
);
611 retval
= async_req_1_0(exch
, DRIVER_DEV_REMOVE
, handle
);
612 async_exchange_end(exch
);
617 int driver_dev_gone(dev_tree_t
*tree
, dev_node_t
*dev
)
622 devman_handle_t handle
;
626 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "driver_dev_gone(%p)", dev
);
628 fibril_rwlock_read_lock(&tree
->rwlock
);
630 handle
= dev
->handle
;
631 fibril_rwlock_read_unlock(&tree
->rwlock
);
633 exch
= async_exchange_begin(drv
->sess
);
634 retval
= async_req_1_0(exch
, DRIVER_DEV_GONE
, handle
);
635 async_exchange_end(exch
);
640 int driver_fun_online(dev_tree_t
*tree
, fun_node_t
*fun
)
645 devman_handle_t handle
;
647 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "driver_fun_online(%p)", fun
);
649 fibril_rwlock_read_lock(&tree
->rwlock
);
651 if (fun
->dev
== NULL
) {
652 /* XXX root function? */
653 fibril_rwlock_read_unlock(&tree
->rwlock
);
658 handle
= fun
->handle
;
659 fibril_rwlock_read_unlock(&tree
->rwlock
);
661 exch
= async_exchange_begin(drv
->sess
);
662 retval
= async_req_1_0(exch
, DRIVER_FUN_ONLINE
, handle
);
663 loc_exchange_end(exch
);
668 int driver_fun_offline(dev_tree_t
*tree
, fun_node_t
*fun
)
673 devman_handle_t handle
;
675 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "driver_fun_offline(%p)", fun
);
677 fibril_rwlock_read_lock(&tree
->rwlock
);
678 if (fun
->dev
== NULL
) {
679 /* XXX root function? */
680 fibril_rwlock_read_unlock(&tree
->rwlock
);
685 handle
= fun
->handle
;
686 fibril_rwlock_read_unlock(&tree
->rwlock
);
688 exch
= async_exchange_begin(drv
->sess
);
689 retval
= async_req_1_0(exch
, DRIVER_FUN_OFFLINE
, handle
);
690 loc_exchange_end(exch
);
696 /** Get list of registered drivers. */
697 int driver_get_list(driver_list_t
*driver_list
, devman_handle_t
*hdl_buf
,
698 size_t buf_size
, size_t *act_size
)
703 fibril_mutex_lock(&driver_list
->drivers_mutex
);
705 buf_cnt
= buf_size
/ sizeof(devman_handle_t
);
707 act_cnt
= list_count(&driver_list
->drivers
);
708 *act_size
= act_cnt
* sizeof(devman_handle_t
);
710 if (buf_size
% sizeof(devman_handle_t
) != 0) {
711 fibril_mutex_unlock(&driver_list
->drivers_mutex
);
716 list_foreach(driver_list
->drivers
, drivers
, driver_t
, drv
) {
718 hdl_buf
[pos
] = drv
->handle
;
724 fibril_mutex_unlock(&driver_list
->drivers_mutex
);
728 /** Get list of device functions. */
729 int driver_get_devices(driver_t
*driver
, devman_handle_t
*hdl_buf
,
730 size_t buf_size
, size_t *act_size
)
735 fibril_mutex_lock(&driver
->driver_mutex
);
737 buf_cnt
= buf_size
/ sizeof(devman_handle_t
);
739 act_cnt
= list_count(&driver
->devices
);
740 *act_size
= act_cnt
* sizeof(devman_handle_t
);
742 if (buf_size
% sizeof(devman_handle_t
) != 0) {
743 fibril_mutex_unlock(&driver
->driver_mutex
);
748 list_foreach(driver
->devices
, driver_devices
, dev_node_t
, dev
) {
750 hdl_buf
[pos
] = dev
->handle
;
756 fibril_mutex_unlock(&driver
->driver_mutex
);