Remove stricmp()
[helenos.git] / uspace / srv / devman / driver.c
blob6bf44cee489803075ec22215e36a2fda35a81102
1 /*
2 * Copyright (c) 2010 Lenka Trochtova
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
30 * @{
33 #include <dirent.h>
34 #include <errno.h>
35 #include <io/log.h>
36 #include <vfs/vfs.h>
37 #include <loc.h>
38 #include <str_error.h>
39 #include <stdio.h>
40 #include <task.h>
42 #include "dev.h"
43 #include "devman.h"
44 #include "driver.h"
45 #include "match.h"
47 /**
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));
69 if (res != NULL)
70 init_driver(res);
71 return res;
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);
90 /**
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
104 * subdirectory.
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\")",
113 base_path, name);
115 assert(base_path != NULL && name != NULL && drv != NULL);
117 bool suc = false;
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)
124 goto cleanup;
126 if (!read_match_ids(match_path, &drv->match_ids))
127 goto cleanup;
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)
133 goto cleanup;
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)
139 goto cleanup;
141 /* Check whether the driver's binary exists. */
142 struct stat s;
143 if (vfs_stat_path(drv->binary_path, &s) != EOK) {
144 log_msg(LOG_DEFAULT, LVL_ERROR, "Driver not found at path `%s'.",
145 drv->binary_path);
146 goto cleanup;
149 suc = true;
151 cleanup:
152 if (!suc) {
153 free(drv->binary_path);
154 free(drv->name);
155 /* Set the driver structure to the default state. */
156 init_driver(drv);
159 free(match_path);
161 return suc;
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);
174 int drv_cnt = 0;
175 DIR *dir = NULL;
176 struct dirent *diren;
178 dir = opendir(dir_path);
180 if (dir != NULL) {
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);
185 drv_cnt++;
186 drv = create_driver();
189 delete_driver(drv);
190 closedir(dir);
193 return drv_cnt;
196 /** Lookup the best matching driver for the specified device in the list of
197 * drivers.
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
209 * is found.
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) {
221 best_score = score;
222 best_drv = drv;
226 fibril_mutex_unlock(&drivers_list->drivers_mutex);
228 return best_drv;
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);
245 dev->drv = drv;
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;
262 assert(drv != NULL);
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);
270 dev->drv = NULL;
271 list_remove(&dev->driver_devices);
273 fibril_rwlock_write_unlock(&tree->rwlock);
274 fibril_mutex_unlock(&drv->driver_mutex);
277 /** Start a driver
279 * @param drv The driver's structure.
280 * @return True if the driver's task is successfully spawned, false
281 * otherwise.
283 bool start_driver(driver_t *drv)
285 int rc;
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);
292 if (rc != EOK) {
293 log_msg(LOG_DEFAULT, LVL_ERROR, "Spawning driver `%s' (%s) failed: %s.",
294 drv->name, drv->binary_path, str_error(rc));
295 return false;
298 drv->state = DRIVER_STARTING;
299 return true;
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,
307 * NULL otherwise.
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) {
317 res = drv;
318 break;
322 fibril_mutex_unlock(&drv_list->drivers_mutex);
324 return res;
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) {
343 res = drv;
344 break;
348 fibril_mutex_unlock(&drv_list->drivers_mutex);
350 return res;
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)
359 dev_node_t *dev;
360 link_t *link;
362 log_msg(LOG_DEFAULT, LVL_DEBUG, "pass_devices_to_driver(driver=\"%s\")",
363 driver->name);
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);
378 link = link->next;
379 continue;
382 log_msg(LOG_DEFAULT, LVL_DEBUG, "pass_devices_to_driver: dev->refcnt=%d\n",
383 (int)atomic_get(&dev->refcnt));
384 dev_add_ref(dev);
387 * Unlock to avoid deadlock when adding device
388 * handled by itself.
390 fibril_mutex_unlock(&driver->driver_mutex);
391 fibril_rwlock_write_unlock(&tree->rwlock);
393 add_device(driver, dev, tree);
395 dev_del_ref(dev);
398 * Lock again as we will work with driver's
399 * structure.
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
411 * driver as running.
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
431 * device manager.
433 void initialize_running_driver(driver_t *driver, dev_tree_t *tree)
435 log_msg(LOG_DEFAULT, LVL_DEBUG, "initialize_running_driver(driver=\"%s\")",
436 driver->name);
439 * Pass devices which have been already assigned to the driver to the
440 * driver.
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)
451 assert(drv != NULL);
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);
457 drv->sess = NULL;
460 /** Device driver structure clean-up.
462 * @param drv The device driver structure.
464 void clean_driver(driver_t *drv)
466 assert(drv != NULL);
468 free(drv->name);
469 free(drv->binary_path);
471 clean_match_ids(&drv->match_ids);
473 init_driver(drv);
476 /** Delete device driver structure.
478 * @param drv The device driver structure.
480 void delete_driver(driver_t *drv)
482 assert(drv != NULL);
484 clean_driver(drv);
485 free(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,
496 dev_tree_t *tree)
498 assert(dev != NULL);
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);
506 if (drv == NULL) {
507 log_msg(LOG_DEFAULT, LVL_ERROR, "No driver found for device `%s'.",
508 dev->pfun->pathname);
509 return false;
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. */
518 start_driver(drv);
520 bool is_running = drv->state == DRIVER_RUNNING;
521 fibril_mutex_unlock(&drv->driver_mutex);
523 /* Notify the driver about the new device. */
524 if (is_running)
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);
535 return true;
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;
554 if (dev->pfun) {
555 parent_handle = dev->pfun->handle;
556 } else {
557 parent_handle = 0;
560 async_exch_t *exch = async_exchange_begin(drv->sess);
562 ipc_call_t answer;
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);
572 if (rc != EOK) {
573 async_forget(req);
574 } else {
575 /* Wait for answer from the driver. */
576 async_wait_for(req, &rc);
579 switch (rc) {
580 case EOK:
581 dev->state = DEVICE_USABLE;
582 break;
583 case ENOENT:
584 dev->state = DEVICE_NOT_PRESENT;
585 break;
586 default:
587 dev->state = DEVICE_INVALID;
588 break;
591 dev->passed_to_driver = true;
594 int driver_dev_remove(dev_tree_t *tree, dev_node_t *dev)
596 async_exch_t *exch;
597 sysarg_t retval;
598 driver_t *drv;
599 devman_handle_t handle;
601 assert(dev != NULL);
603 log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_remove(%p)", dev);
605 fibril_rwlock_read_lock(&tree->rwlock);
606 drv = dev->drv;
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);
614 return retval;
617 int driver_dev_gone(dev_tree_t *tree, dev_node_t *dev)
619 async_exch_t *exch;
620 sysarg_t retval;
621 driver_t *drv;
622 devman_handle_t handle;
624 assert(dev != NULL);
626 log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_gone(%p)", dev);
628 fibril_rwlock_read_lock(&tree->rwlock);
629 drv = dev->drv;
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);
637 return retval;
640 int driver_fun_online(dev_tree_t *tree, fun_node_t *fun)
642 async_exch_t *exch;
643 sysarg_t retval;
644 driver_t *drv;
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);
654 return EINVAL;
657 drv = fun->dev->drv;
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);
665 return retval;
668 int driver_fun_offline(dev_tree_t *tree, fun_node_t *fun)
670 async_exch_t *exch;
671 sysarg_t retval;
672 driver_t *drv;
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);
681 return EINVAL;
684 drv = fun->dev->drv;
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);
692 return retval;
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)
700 size_t act_cnt;
701 size_t buf_cnt;
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);
712 return EINVAL;
715 size_t pos = 0;
716 list_foreach(driver_list->drivers, drivers, driver_t, drv) {
717 if (pos < buf_cnt) {
718 hdl_buf[pos] = drv->handle;
721 pos++;
724 fibril_mutex_unlock(&driver_list->drivers_mutex);
725 return EOK;
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)
732 size_t act_cnt;
733 size_t buf_cnt;
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);
744 return EINVAL;
747 size_t pos = 0;
748 list_foreach(driver->devices, driver_devices, dev_node_t, dev) {
749 if (pos < buf_cnt) {
750 hdl_buf[pos] = dev->handle;
753 pos++;
756 fibril_mutex_unlock(&driver->driver_mutex);
757 return EOK;
760 /** @}