source: mainline/uspace/srv/devman/devman.c@ 1a5b252

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1a5b252 was 1a5b252, checked in by Jiri Svoboda <jiri@…>, 14 years ago

DDF support for function offlining and onlining. This allows
(anticipated) hot removal — support needs to be added in individual
drivers, currently there is support in test1 and partially in rootvirt.
Surprise removal is not supported. TODO synchronization.

  • Property mode set to 100644
File size: 32.9 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/** @addtogroup devman
30 * @{
31 */
32
33#include <errno.h>
34#include <fcntl.h>
35#include <sys/stat.h>
36#include <io/log.h>
37#include <ipc/driver.h>
38#include <ipc/devman.h>
39#include <loc.h>
40#include <str_error.h>
41#include <stdio.h>
42
43#include "devman.h"
44
45fun_node_t *find_node_child(fun_node_t *parent, const char *name);
46
47/* hash table operations */
48
49static hash_index_t devices_hash(unsigned long key[])
50{
51 return key[0] % DEVICE_BUCKETS;
52}
53
54static int devman_devices_compare(unsigned long key[], hash_count_t keys,
55 link_t *item)
56{
57 dev_node_t *dev = hash_table_get_instance(item, dev_node_t, devman_dev);
58 return (dev->handle == (devman_handle_t) key[0]);
59}
60
61static int devman_functions_compare(unsigned long key[], hash_count_t keys,
62 link_t *item)
63{
64 fun_node_t *fun = hash_table_get_instance(item, fun_node_t, devman_fun);
65 return (fun->handle == (devman_handle_t) key[0]);
66}
67
68static int loc_functions_compare(unsigned long key[], hash_count_t keys,
69 link_t *item)
70{
71 fun_node_t *fun = hash_table_get_instance(item, fun_node_t, loc_fun);
72 return (fun->service_id == (service_id_t) key[0]);
73}
74
75static void devices_remove_callback(link_t *item)
76{
77}
78
79static hash_table_operations_t devman_devices_ops = {
80 .hash = devices_hash,
81 .compare = devman_devices_compare,
82 .remove_callback = devices_remove_callback
83};
84
85static hash_table_operations_t devman_functions_ops = {
86 .hash = devices_hash,
87 .compare = devman_functions_compare,
88 .remove_callback = devices_remove_callback
89};
90
91static hash_table_operations_t loc_devices_ops = {
92 .hash = devices_hash,
93 .compare = loc_functions_compare,
94 .remove_callback = devices_remove_callback
95};
96
97/**
98 * Initialize the list of device driver's.
99 *
100 * @param drv_list the list of device driver's.
101 *
102 */
103void init_driver_list(driver_list_t *drv_list)
104{
105 assert(drv_list != NULL);
106
107 list_initialize(&drv_list->drivers);
108 fibril_mutex_initialize(&drv_list->drivers_mutex);
109}
110
111/** Allocate and initialize a new driver structure.
112 *
113 * @return Driver structure.
114 */
115driver_t *create_driver(void)
116{
117 driver_t *res = malloc(sizeof(driver_t));
118 if (res != NULL)
119 init_driver(res);
120 return res;
121}
122
123/** Add a driver to the list of drivers.
124 *
125 * @param drivers_list List of drivers.
126 * @param drv Driver structure.
127 */
128void add_driver(driver_list_t *drivers_list, driver_t *drv)
129{
130 fibril_mutex_lock(&drivers_list->drivers_mutex);
131 list_prepend(&drv->drivers, &drivers_list->drivers);
132 fibril_mutex_unlock(&drivers_list->drivers_mutex);
133
134 log_msg(LVL_NOTE, "Driver `%s' was added to the list of available "
135 "drivers.", drv->name);
136}
137
138/** Read match id at the specified position of a string and set the position in
139 * the string to the first character following the id.
140 *
141 * @param buf The position in the input string.
142 * @return The match id.
143 */
144char *read_match_id(char **buf)
145{
146 char *res = NULL;
147 size_t len = get_nonspace_len(*buf);
148
149 if (len > 0) {
150 res = malloc(len + 1);
151 if (res != NULL) {
152 str_ncpy(res, len + 1, *buf, len);
153 *buf += len;
154 }
155 }
156
157 return res;
158}
159
160/**
161 * Read match ids and associated match scores from a string.
162 *
163 * Each match score in the string is followed by its match id.
164 * The match ids and match scores are separated by whitespaces.
165 * Neither match ids nor match scores can contain whitespaces.
166 *
167 * @param buf The string from which the match ids are read.
168 * @param ids The list of match ids into which the match ids and
169 * scores are added.
170 * @return True if at least one match id and associated match score
171 * was successfully read, false otherwise.
172 */
173bool parse_match_ids(char *buf, match_id_list_t *ids)
174{
175 int score = 0;
176 char *id = NULL;
177 int ids_read = 0;
178
179 while (true) {
180 /* skip spaces */
181 if (!skip_spaces(&buf))
182 break;
183
184 /* read score */
185 score = strtoul(buf, &buf, 10);
186
187 /* skip spaces */
188 if (!skip_spaces(&buf))
189 break;
190
191 /* read id */
192 id = read_match_id(&buf);
193 if (NULL == id)
194 break;
195
196 /* create new match_id structure */
197 match_id_t *mid = create_match_id();
198 mid->id = id;
199 mid->score = score;
200
201 /* add it to the list */
202 add_match_id(ids, mid);
203
204 ids_read++;
205 }
206
207 return ids_read > 0;
208}
209
210/**
211 * Read match ids and associated match scores from a file.
212 *
213 * Each match score in the file is followed by its match id.
214 * The match ids and match scores are separated by whitespaces.
215 * Neither match ids nor match scores can contain whitespaces.
216 *
217 * @param buf The path to the file from which the match ids are read.
218 * @param ids The list of match ids into which the match ids and
219 * scores are added.
220 * @return True if at least one match id and associated match score
221 * was successfully read, false otherwise.
222 */
223bool read_match_ids(const char *conf_path, match_id_list_t *ids)
224{
225 log_msg(LVL_DEBUG, "read_match_ids(conf_path=\"%s\")", conf_path);
226
227 bool suc = false;
228 char *buf = NULL;
229 bool opened = false;
230 int fd;
231 size_t len = 0;
232
233 fd = open(conf_path, O_RDONLY);
234 if (fd < 0) {
235 log_msg(LVL_ERROR, "Unable to open `%s' for reading: %s.",
236 conf_path, str_error(fd));
237 goto cleanup;
238 }
239 opened = true;
240
241 len = lseek(fd, 0, SEEK_END);
242 lseek(fd, 0, SEEK_SET);
243 if (len == 0) {
244 log_msg(LVL_ERROR, "Configuration file '%s' is empty.",
245 conf_path);
246 goto cleanup;
247 }
248
249 buf = malloc(len + 1);
250 if (buf == NULL) {
251 log_msg(LVL_ERROR, "Memory allocation failed when parsing file "
252 "'%s'.", conf_path);
253 goto cleanup;
254 }
255
256 ssize_t read_bytes = read_all(fd, buf, len);
257 if (read_bytes <= 0) {
258 log_msg(LVL_ERROR, "Unable to read file '%s' (%zd).", conf_path,
259 read_bytes);
260 goto cleanup;
261 }
262 buf[read_bytes] = 0;
263
264 suc = parse_match_ids(buf, ids);
265
266cleanup:
267 free(buf);
268
269 if (opened)
270 close(fd);
271
272 return suc;
273}
274
275/**
276 * Get information about a driver.
277 *
278 * Each driver has its own directory in the base directory.
279 * The name of the driver's directory is the same as the name of the driver.
280 * The driver's directory contains driver's binary (named as the driver without
281 * extension) and the configuration file with match ids for device-to-driver
282 * matching (named as the driver with a special extension).
283 *
284 * This function searches for the driver's directory and containing
285 * configuration files. If all the files needed are found, they are parsed and
286 * the information about the driver is stored in the driver's structure.
287 *
288 * @param base_path The base directory, in which we look for driver's
289 * subdirectory.
290 * @param name The name of the driver.
291 * @param drv The driver structure to fill information in.
292 *
293 * @return True on success, false otherwise.
294 */
295bool get_driver_info(const char *base_path, const char *name, driver_t *drv)
296{
297 log_msg(LVL_DEBUG, "get_driver_info(base_path=\"%s\", name=\"%s\")",
298 base_path, name);
299
300 assert(base_path != NULL && name != NULL && drv != NULL);
301
302 bool suc = false;
303 char *match_path = NULL;
304 size_t name_size = 0;
305
306 /* Read the list of match ids from the driver's configuration file. */
307 match_path = get_abs_path(base_path, name, MATCH_EXT);
308 if (match_path == NULL)
309 goto cleanup;
310
311 if (!read_match_ids(match_path, &drv->match_ids))
312 goto cleanup;
313
314 /* Allocate and fill driver's name. */
315 name_size = str_size(name) + 1;
316 drv->name = malloc(name_size);
317 if (drv->name == NULL)
318 goto cleanup;
319 str_cpy(drv->name, name_size, name);
320
321 /* Initialize path with driver's binary. */
322 drv->binary_path = get_abs_path(base_path, name, "");
323 if (drv->binary_path == NULL)
324 goto cleanup;
325
326 /* Check whether the driver's binary exists. */
327 struct stat s;
328 if (stat(drv->binary_path, &s) == ENOENT) { /* FIXME!! */
329 log_msg(LVL_ERROR, "Driver not found at path `%s'.",
330 drv->binary_path);
331 goto cleanup;
332 }
333
334 suc = true;
335
336cleanup:
337 if (!suc) {
338 free(drv->binary_path);
339 free(drv->name);
340 /* Set the driver structure to the default state. */
341 init_driver(drv);
342 }
343
344 free(match_path);
345
346 return suc;
347}
348
349/** Lookup drivers in the directory.
350 *
351 * @param drivers_list The list of available drivers.
352 * @param dir_path The path to the directory where we search for drivers.
353 * @return Number of drivers which were found.
354 */
355int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path)
356{
357 log_msg(LVL_DEBUG, "lookup_available_drivers(dir=\"%s\")", dir_path);
358
359 int drv_cnt = 0;
360 DIR *dir = NULL;
361 struct dirent *diren;
362
363 dir = opendir(dir_path);
364
365 if (dir != NULL) {
366 driver_t *drv = create_driver();
367 while ((diren = readdir(dir))) {
368 if (get_driver_info(dir_path, diren->d_name, drv)) {
369 add_driver(drivers_list, drv);
370 drv_cnt++;
371 drv = create_driver();
372 }
373 }
374 delete_driver(drv);
375 closedir(dir);
376 }
377
378 return drv_cnt;
379}
380
381/** Create root device and function node in the device tree.
382 *
383 * @param tree The device tree.
384 * @return True on success, false otherwise.
385 */
386bool create_root_nodes(dev_tree_t *tree)
387{
388 fun_node_t *fun;
389 dev_node_t *dev;
390
391 log_msg(LVL_DEBUG, "create_root_nodes()");
392
393 fibril_rwlock_write_lock(&tree->rwlock);
394
395 /*
396 * Create root function. This is a pseudo function to which
397 * the root device node is attached. It allows us to match
398 * the root device driver in a standard manner, i.e. against
399 * the parent function.
400 */
401
402 fun = create_fun_node();
403 if (fun == NULL) {
404 fibril_rwlock_write_unlock(&tree->rwlock);
405 return false;
406 }
407
408 insert_fun_node(tree, fun, str_dup(""), NULL);
409 match_id_t *id = create_match_id();
410 id->id = str_dup("root");
411 id->score = 100;
412 add_match_id(&fun->match_ids, id);
413 tree->root_node = fun;
414
415 /*
416 * Create root device node.
417 */
418 dev = create_dev_node();
419 if (dev == NULL) {
420 fibril_rwlock_write_unlock(&tree->rwlock);
421 return false;
422 }
423
424 insert_dev_node(tree, dev, fun);
425
426 fibril_rwlock_write_unlock(&tree->rwlock);
427
428 return dev != NULL;
429}
430
431/** Lookup the best matching driver for the specified device in the list of
432 * drivers.
433 *
434 * A match between a device and a driver is found if one of the driver's match
435 * ids match one of the device's match ids. The score of the match is the
436 * product of the driver's and device's score associated with the matching id.
437 * The best matching driver for a device is the driver with the highest score
438 * of the match between the device and the driver.
439 *
440 * @param drivers_list The list of drivers, where we look for the driver
441 * suitable for handling the device.
442 * @param node The device node structure of the device.
443 * @return The best matching driver or NULL if no matching driver
444 * is found.
445 */
446driver_t *find_best_match_driver(driver_list_t *drivers_list, dev_node_t *node)
447{
448 driver_t *best_drv = NULL, *drv = NULL;
449 int best_score = 0, score = 0;
450
451 fibril_mutex_lock(&drivers_list->drivers_mutex);
452
453 list_foreach(drivers_list->drivers, link) {
454 drv = list_get_instance(link, driver_t, drivers);
455 score = get_match_score(drv, node);
456 if (score > best_score) {
457 best_score = score;
458 best_drv = drv;
459 }
460 }
461
462 fibril_mutex_unlock(&drivers_list->drivers_mutex);
463
464 return best_drv;
465}
466
467/** Assign a driver to a device.
468 *
469 * @param node The device's node in the device tree.
470 * @param drv The driver.
471 */
472void attach_driver(dev_node_t *dev, driver_t *drv)
473{
474 log_msg(LVL_DEBUG, "attach_driver(dev=\"%s\",drv=\"%s\")",
475 dev->pfun->pathname, drv->name);
476
477 fibril_mutex_lock(&drv->driver_mutex);
478
479 dev->drv = drv;
480 list_append(&dev->driver_devices, &drv->devices);
481
482 fibril_mutex_unlock(&drv->driver_mutex);
483}
484
485/** Detach driver from device.
486 *
487 * @param node The device's node in the device tree.
488 * @param drv The driver.
489 */
490void detach_driver(dev_node_t *dev)
491{
492 /* XXX need lock on dev */
493 driver_t *drv = dev->drv;
494
495 assert(drv != NULL);
496 log_msg(LVL_DEBUG, "detach_driver(dev=\"%s\",drv=\"%s\")",
497 dev->pfun->pathname, drv->name);
498
499 fibril_mutex_lock(&drv->driver_mutex);
500
501 dev->drv = NULL;
502 list_remove(&dev->driver_devices);
503
504 fibril_mutex_unlock(&drv->driver_mutex);
505}
506
507/** Start a driver
508 *
509 * @param drv The driver's structure.
510 * @return True if the driver's task is successfully spawned, false
511 * otherwise.
512 */
513bool start_driver(driver_t *drv)
514{
515 int rc;
516
517 assert(fibril_mutex_is_locked(&drv->driver_mutex));
518
519 log_msg(LVL_DEBUG, "start_driver(drv=\"%s\")", drv->name);
520
521 rc = task_spawnl(NULL, drv->binary_path, drv->binary_path, NULL);
522 if (rc != EOK) {
523 log_msg(LVL_ERROR, "Spawning driver `%s' (%s) failed: %s.",
524 drv->name, drv->binary_path, str_error(rc));
525 return false;
526 }
527
528 drv->state = DRIVER_STARTING;
529 return true;
530}
531
532/** Find device driver in the list of device drivers.
533 *
534 * @param drv_list The list of device drivers.
535 * @param drv_name The name of the device driver which is searched.
536 * @return The device driver of the specified name, if it is in the
537 * list, NULL otherwise.
538 */
539driver_t *find_driver(driver_list_t *drv_list, const char *drv_name)
540{
541 driver_t *res = NULL;
542 driver_t *drv = NULL;
543
544 fibril_mutex_lock(&drv_list->drivers_mutex);
545
546 list_foreach(drv_list->drivers, link) {
547 drv = list_get_instance(link, driver_t, drivers);
548 if (str_cmp(drv->name, drv_name) == 0) {
549 res = drv;
550 break;
551 }
552 }
553
554 fibril_mutex_unlock(&drv_list->drivers_mutex);
555
556 return res;
557}
558
559/** Notify driver about the devices to which it was assigned.
560 *
561 * @param driver The driver to which the devices are passed.
562 */
563static void pass_devices_to_driver(driver_t *driver, dev_tree_t *tree)
564{
565 dev_node_t *dev;
566 link_t *link;
567
568 log_msg(LVL_DEBUG, "pass_devices_to_driver(driver=\"%s\")",
569 driver->name);
570
571 fibril_mutex_lock(&driver->driver_mutex);
572
573 /*
574 * Go through devices list as long as there is some device
575 * that has not been passed to the driver.
576 */
577 link = driver->devices.head.next;
578 while (link != &driver->devices.head) {
579 dev = list_get_instance(link, dev_node_t, driver_devices);
580 if (dev->passed_to_driver) {
581 link = link->next;
582 continue;
583 }
584
585 /*
586 * We remove the device from the list to allow safe adding
587 * of new devices (no one will touch our item this way).
588 */
589 list_remove(link);
590
591 /*
592 * Unlock to avoid deadlock when adding device
593 * handled by itself.
594 */
595 fibril_mutex_unlock(&driver->driver_mutex);
596
597 add_device(driver, dev, tree);
598
599 /*
600 * Lock again as we will work with driver's
601 * structure.
602 */
603 fibril_mutex_lock(&driver->driver_mutex);
604
605 /*
606 * Insert the device back.
607 * The order is not relevant here so no harm is done
608 * (actually, the order would be preserved in most cases).
609 */
610 list_append(link, &driver->devices);
611
612 /*
613 * Restart the cycle to go through all devices again.
614 */
615 link = driver->devices.head.next;
616 }
617
618 /*
619 * Once we passed all devices to the driver, we need to mark the
620 * driver as running.
621 * It is vital to do it here and inside critical section.
622 *
623 * If we would change the state earlier, other devices added to
624 * the driver would be added to the device list and started
625 * immediately and possibly started here as well.
626 */
627 log_msg(LVL_DEBUG, "Driver `%s' enters running state.", driver->name);
628 driver->state = DRIVER_RUNNING;
629
630 fibril_mutex_unlock(&driver->driver_mutex);
631}
632
633/** Finish the initialization of a driver after it has succesfully started
634 * and after it has registered itself by the device manager.
635 *
636 * Pass devices formerly matched to the driver to the driver and remember the
637 * driver is running and fully functional now.
638 *
639 * @param driver The driver which registered itself as running by the
640 * device manager.
641 */
642void initialize_running_driver(driver_t *driver, dev_tree_t *tree)
643{
644 log_msg(LVL_DEBUG, "initialize_running_driver(driver=\"%s\")",
645 driver->name);
646
647 /*
648 * Pass devices which have been already assigned to the driver to the
649 * driver.
650 */
651 pass_devices_to_driver(driver, tree);
652}
653
654/** Initialize device driver structure.
655 *
656 * @param drv The device driver structure.
657 */
658void init_driver(driver_t *drv)
659{
660 assert(drv != NULL);
661
662 memset(drv, 0, sizeof(driver_t));
663 list_initialize(&drv->match_ids.ids);
664 list_initialize(&drv->devices);
665 fibril_mutex_initialize(&drv->driver_mutex);
666 drv->sess = NULL;
667}
668
669/** Device driver structure clean-up.
670 *
671 * @param drv The device driver structure.
672 */
673void clean_driver(driver_t *drv)
674{
675 assert(drv != NULL);
676
677 free_not_null(drv->name);
678 free_not_null(drv->binary_path);
679
680 clean_match_ids(&drv->match_ids);
681
682 init_driver(drv);
683}
684
685/** Delete device driver structure.
686 *
687 * @param drv The device driver structure.
688 */
689void delete_driver(driver_t *drv)
690{
691 assert(drv != NULL);
692
693 clean_driver(drv);
694 free(drv);
695}
696
697/** Create loc path and name for the function. */
698void loc_register_tree_function(fun_node_t *fun, dev_tree_t *tree)
699{
700 char *loc_pathname = NULL;
701 char *loc_name = NULL;
702
703 asprintf(&loc_name, "%s", fun->pathname);
704 if (loc_name == NULL)
705 return;
706
707 replace_char(loc_name, '/', LOC_SEPARATOR);
708
709 asprintf(&loc_pathname, "%s/%s", LOC_DEVICE_NAMESPACE,
710 loc_name);
711 if (loc_pathname == NULL) {
712 free(loc_name);
713 return;
714 }
715
716 loc_service_register_with_iface(loc_pathname,
717 &fun->service_id, DEVMAN_CONNECT_FROM_LOC);
718
719 tree_add_loc_function(tree, fun);
720
721 free(loc_name);
722 free(loc_pathname);
723}
724
725/** Pass a device to running driver.
726 *
727 * @param drv The driver's structure.
728 * @param node The device's node in the device tree.
729 */
730void add_device(driver_t *drv, dev_node_t *dev, dev_tree_t *tree)
731{
732 /*
733 * We do not expect to have driver's mutex locked as we do not
734 * access any structures that would affect driver_t.
735 */
736 log_msg(LVL_DEBUG, "add_device(drv=\"%s\", dev=\"%s\")",
737 drv->name, dev->pfun->name);
738
739 /* Send the device to the driver. */
740 devman_handle_t parent_handle;
741 if (dev->pfun) {
742 parent_handle = dev->pfun->handle;
743 } else {
744 parent_handle = 0;
745 }
746
747 async_exch_t *exch = async_exchange_begin(drv->sess);
748
749 ipc_call_t answer;
750 aid_t req = async_send_2(exch, DRIVER_DEV_ADD, dev->handle,
751 parent_handle, &answer);
752
753 /* Send the device name to the driver. */
754 sysarg_t rc = async_data_write_start(exch, dev->pfun->name,
755 str_size(dev->pfun->name) + 1);
756
757 async_exchange_end(exch);
758
759 if (rc != EOK) {
760 /* TODO handle error */
761 }
762
763 /* Wait for answer from the driver. */
764 async_wait_for(req, &rc);
765
766 switch(rc) {
767 case EOK:
768 dev->state = DEVICE_USABLE;
769 break;
770 case ENOENT:
771 dev->state = DEVICE_NOT_PRESENT;
772 break;
773 default:
774 dev->state = DEVICE_INVALID;
775 }
776
777 dev->passed_to_driver = true;
778
779 return;
780}
781
782/** Find suitable driver for a device and assign the driver to it.
783 *
784 * @param node The device node of the device in the device tree.
785 * @param drivers_list The list of available drivers.
786 * @return True if the suitable driver is found and
787 * successfully assigned to the device, false otherwise.
788 */
789bool assign_driver(dev_node_t *dev, driver_list_t *drivers_list,
790 dev_tree_t *tree)
791{
792 assert(dev != NULL);
793 assert(drivers_list != NULL);
794 assert(tree != NULL);
795
796 /*
797 * Find the driver which is the most suitable for handling this device.
798 */
799 driver_t *drv = find_best_match_driver(drivers_list, dev);
800 if (drv == NULL) {
801 log_msg(LVL_ERROR, "No driver found for device `%s'.",
802 dev->pfun->pathname);
803 return false;
804 }
805
806 /* Attach the driver to the device. */
807 attach_driver(dev, drv);
808
809 fibril_mutex_lock(&drv->driver_mutex);
810 if (drv->state == DRIVER_NOT_STARTED) {
811 /* Start the driver. */
812 start_driver(drv);
813 }
814 bool is_running = drv->state == DRIVER_RUNNING;
815 fibril_mutex_unlock(&drv->driver_mutex);
816
817 /* Notify the driver about the new device. */
818 if (is_running)
819 add_device(drv, dev, tree);
820
821 return true;
822}
823
824int driver_dev_remove(dev_node_t *dev)
825{
826 async_exch_t *exch;
827 sysarg_t retval;
828 driver_t *drv;
829
830 assert(dev != NULL);
831 log_msg(LVL_DEBUG, "driver_dev_remove(%p)", dev);
832 drv = dev->drv;
833
834 exch = async_exchange_begin(drv->sess);
835 retval = async_req_1_0(exch, DRIVER_DEV_REMOVE, dev->handle);
836 async_exchange_end(exch);
837
838 return retval;
839
840}
841
842int driver_fun_online(fun_node_t *fun)
843{
844 async_exch_t *exch;
845 sysarg_t retval;
846 driver_t *drv;
847
848 log_msg(LVL_DEBUG, "driver_fun_online(%p)", fun);
849 if (fun->dev == NULL) {
850 /* XXX root function? */
851 return EINVAL;
852 }
853
854 drv = fun->dev->drv;
855
856 exch = async_exchange_begin(drv->sess);
857 retval = async_req_1_0(exch, DRIVER_FUN_ONLINE, fun->handle);
858 loc_exchange_end(exch);
859
860 return retval;
861}
862
863int driver_fun_offline(fun_node_t *fun)
864{
865 async_exch_t *exch;
866 sysarg_t retval;
867 driver_t *drv;
868
869 log_msg(LVL_DEBUG, "driver_fun_offline(%p)", fun);
870 if (fun->dev == NULL) {
871 /* XXX root function? */
872 return EINVAL;
873 }
874
875 drv = fun->dev->drv;
876
877 exch = async_exchange_begin(drv->sess);
878 retval = async_req_1_0(exch, DRIVER_FUN_OFFLINE, fun->handle);
879 loc_exchange_end(exch);
880
881 return retval;
882
883}
884
885/** Initialize the device tree.
886 *
887 * Create root device node of the tree and assign driver to it.
888 *
889 * @param tree The device tree.
890 * @param drivers_list the list of available drivers.
891 * @return True on success, false otherwise.
892 */
893bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list)
894{
895 log_msg(LVL_DEBUG, "init_device_tree()");
896
897 tree->current_handle = 0;
898
899 hash_table_create(&tree->devman_devices, DEVICE_BUCKETS, 1,
900 &devman_devices_ops);
901 hash_table_create(&tree->devman_functions, DEVICE_BUCKETS, 1,
902 &devman_functions_ops);
903 hash_table_create(&tree->loc_functions, DEVICE_BUCKETS, 1,
904 &loc_devices_ops);
905
906 fibril_rwlock_initialize(&tree->rwlock);
907
908 /* Create root function and root device and add them to the device tree. */
909 if (!create_root_nodes(tree))
910 return false;
911
912 /* Find suitable driver and start it. */
913 return assign_driver(tree->root_node->child, drivers_list, tree);
914}
915
916/* Device nodes */
917
918/** Create a new device node.
919 *
920 * @return A device node structure.
921 */
922dev_node_t *create_dev_node(void)
923{
924 dev_node_t *res = malloc(sizeof(dev_node_t));
925
926 if (res != NULL) {
927 memset(res, 0, sizeof(dev_node_t));
928 list_initialize(&res->functions);
929 link_initialize(&res->driver_devices);
930 link_initialize(&res->devman_dev);
931 }
932
933 return res;
934}
935
936/** Delete a device node.
937 *
938 * @param node The device node structure.
939 */
940void delete_dev_node(dev_node_t *dev)
941{
942 assert(list_empty(&dev->functions));
943 assert(dev->pfun == NULL);
944 assert(dev->drv == NULL);
945
946 free(dev);
947}
948
949/** Find the device node structure of the device witch has the specified handle.
950 *
951 * @param tree The device tree where we look for the device node.
952 * @param handle The handle of the device.
953 * @return The device node.
954 */
955dev_node_t *find_dev_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
956{
957 unsigned long key = handle;
958 link_t *link;
959
960 assert(fibril_rwlock_is_locked(&tree->rwlock));
961
962 link = hash_table_find(&tree->devman_devices, &key);
963 return hash_table_get_instance(link, dev_node_t, devman_dev);
964}
965
966/** Find the device node structure of the device witch has the specified handle.
967 *
968 * @param tree The device tree where we look for the device node.
969 * @param handle The handle of the device.
970 * @return The device node.
971 */
972dev_node_t *find_dev_node(dev_tree_t *tree, devman_handle_t handle)
973{
974 dev_node_t *dev = NULL;
975
976 fibril_rwlock_read_lock(&tree->rwlock);
977 dev = find_dev_node_no_lock(tree, handle);
978 fibril_rwlock_read_unlock(&tree->rwlock);
979
980 return dev;
981}
982
983/** Get list of device functions. */
984int dev_get_functions(dev_tree_t *tree, dev_node_t *dev,
985 devman_handle_t *hdl_buf, size_t buf_size, size_t *act_size)
986{
987 size_t act_cnt;
988 size_t buf_cnt;
989
990 assert(fibril_rwlock_is_locked(&tree->rwlock));
991
992 buf_cnt = buf_size / sizeof(devman_handle_t);
993
994 act_cnt = list_count(&dev->functions);
995 *act_size = act_cnt * sizeof(devman_handle_t);
996
997 if (buf_size % sizeof(devman_handle_t) != 0)
998 return EINVAL;
999
1000 size_t pos = 0;
1001 list_foreach(dev->functions, item) {
1002 fun_node_t *fun =
1003 list_get_instance(item, fun_node_t, dev_functions);
1004
1005 if (pos < buf_cnt)
1006 hdl_buf[pos] = fun->handle;
1007 pos++;
1008 }
1009
1010 return EOK;
1011}
1012
1013
1014/* Function nodes */
1015
1016/** Create a new function node.
1017 *
1018 * @return A function node structure.
1019 */
1020fun_node_t *create_fun_node(void)
1021{
1022 fun_node_t *res = malloc(sizeof(fun_node_t));
1023
1024 if (res != NULL) {
1025 memset(res, 0, sizeof(fun_node_t));
1026 link_initialize(&res->dev_functions);
1027 list_initialize(&res->match_ids.ids);
1028 link_initialize(&res->devman_fun);
1029 link_initialize(&res->loc_fun);
1030 }
1031
1032 return res;
1033}
1034
1035/** Delete a function node.
1036 *
1037 * @param fun The device node structure.
1038 */
1039void delete_fun_node(fun_node_t *fun)
1040{
1041 assert(fun->dev == NULL);
1042 assert(fun->child == NULL);
1043
1044 clean_match_ids(&fun->match_ids);
1045 free_not_null(fun->name);
1046 free_not_null(fun->pathname);
1047 free(fun);
1048}
1049
1050/** Find the function node with the specified handle.
1051 *
1052 * @param tree The device tree where we look for the device node.
1053 * @param handle The handle of the function.
1054 * @return The function node.
1055 */
1056fun_node_t *find_fun_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
1057{
1058 unsigned long key = handle;
1059 link_t *link;
1060
1061 assert(fibril_rwlock_is_locked(&tree->rwlock));
1062
1063 link = hash_table_find(&tree->devman_functions, &key);
1064 if (link == NULL)
1065 return NULL;
1066
1067 return hash_table_get_instance(link, fun_node_t, devman_fun);
1068}
1069
1070/** Find the function node with the specified handle.
1071 *
1072 * @param tree The device tree where we look for the device node.
1073 * @param handle The handle of the function.
1074 * @return The function node.
1075 */
1076fun_node_t *find_fun_node(dev_tree_t *tree, devman_handle_t handle)
1077{
1078 fun_node_t *fun = NULL;
1079
1080 fibril_rwlock_read_lock(&tree->rwlock);
1081 fun = find_fun_node_no_lock(tree, handle);
1082 fibril_rwlock_read_unlock(&tree->rwlock);
1083
1084 return fun;
1085}
1086
1087/** Create and set device's full path in device tree.
1088 *
1089 * @param node The device's device node.
1090 * @param parent The parent device node.
1091 * @return True on success, false otherwise (insufficient
1092 * resources etc.).
1093 */
1094static bool set_fun_path(fun_node_t *fun, fun_node_t *parent)
1095{
1096 assert(fun->name != NULL);
1097
1098 size_t pathsize = (str_size(fun->name) + 1);
1099 if (parent != NULL)
1100 pathsize += str_size(parent->pathname) + 1;
1101
1102 fun->pathname = (char *) malloc(pathsize);
1103 if (fun->pathname == NULL) {
1104 log_msg(LVL_ERROR, "Failed to allocate device path.");
1105 return false;
1106 }
1107
1108 if (parent != NULL) {
1109 str_cpy(fun->pathname, pathsize, parent->pathname);
1110 str_append(fun->pathname, pathsize, "/");
1111 str_append(fun->pathname, pathsize, fun->name);
1112 } else {
1113 str_cpy(fun->pathname, pathsize, fun->name);
1114 }
1115
1116 return true;
1117}
1118
1119/** Insert new device into device tree.
1120 *
1121 * @param tree The device tree.
1122 * @param node The newly added device node.
1123 * @param dev_name The name of the newly added device.
1124 * @param parent The parent device node.
1125 *
1126 * @return True on success, false otherwise (insufficient resources
1127 * etc.).
1128 */
1129bool insert_dev_node(dev_tree_t *tree, dev_node_t *dev, fun_node_t *pfun)
1130{
1131 assert(dev != NULL);
1132 assert(tree != NULL);
1133 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
1134
1135 log_msg(LVL_DEBUG, "insert_dev_node(dev=%p, pfun=%p [\"%s\"])",
1136 dev, pfun, pfun->pathname);
1137
1138 /* Add the node to the handle-to-node map. */
1139 dev->handle = ++tree->current_handle;
1140 unsigned long key = dev->handle;
1141 hash_table_insert(&tree->devman_devices, &key, &dev->devman_dev);
1142
1143 /* Add the node to the list of its parent's children. */
1144 dev->pfun = pfun;
1145 pfun->child = dev;
1146
1147 return true;
1148}
1149
1150/** Remove device from device tree.
1151 *
1152 * @param tree Device tree
1153 * @param dev Device node
1154 */
1155void remove_dev_node(dev_tree_t *tree, dev_node_t *dev)
1156{
1157 assert(tree != NULL);
1158 assert(dev != NULL);
1159 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
1160
1161 log_msg(LVL_DEBUG, "remove_dev_node(dev=%p)", dev);
1162
1163 /* Remove node from the handle-to-node map. */
1164 unsigned long key = dev->handle;
1165 hash_table_remove(&tree->devman_devices, &key, 1);
1166
1167 /* Unlink from parent function. */
1168 dev->pfun->child = NULL;
1169 dev->pfun = NULL;
1170}
1171
1172
1173/** Insert new function into device tree.
1174 *
1175 * @param tree The device tree.
1176 * @param node The newly added function node.
1177 * @param dev_name The name of the newly added function.
1178 * @param parent Owning device node.
1179 *
1180 * @return True on success, false otherwise (insufficient resources
1181 * etc.).
1182 */
1183bool insert_fun_node(dev_tree_t *tree, fun_node_t *fun, char *fun_name,
1184 dev_node_t *dev)
1185{
1186 fun_node_t *pfun;
1187
1188 assert(fun != NULL);
1189 assert(tree != NULL);
1190 assert(fun_name != NULL);
1191 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
1192
1193 /*
1194 * The root function is a special case, it does not belong to any
1195 * device so for the root function dev == NULL.
1196 */
1197 pfun = (dev != NULL) ? dev->pfun : NULL;
1198
1199 fun->name = fun_name;
1200 if (!set_fun_path(fun, pfun)) {
1201 return false;
1202 }
1203
1204 /* Add the node to the handle-to-node map. */
1205 fun->handle = ++tree->current_handle;
1206 unsigned long key = fun->handle;
1207 hash_table_insert(&tree->devman_functions, &key, &fun->devman_fun);
1208
1209 /* Add the node to the list of its parent's children. */
1210 fun->dev = dev;
1211 if (dev != NULL)
1212 list_append(&fun->dev_functions, &dev->functions);
1213
1214 return true;
1215}
1216
1217/** Remove function from device tree.
1218 *
1219 * @param tree Device tree
1220 * @param node Function node to remove
1221 */
1222void remove_fun_node(dev_tree_t *tree, fun_node_t *fun)
1223{
1224 assert(tree != NULL);
1225 assert(fun != NULL);
1226 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
1227
1228 /* Remove the node from the handle-to-node map. */
1229 unsigned long key = fun->handle;
1230 hash_table_remove(&tree->devman_functions, &key, 1);
1231
1232 /* Remove the node from the list of its parent's children. */
1233 if (fun->dev != NULL)
1234 list_remove(&fun->dev_functions);
1235
1236 fun->dev = NULL;
1237}
1238
1239/** Find function node with a specified path in the device tree.
1240 *
1241 * @param path The path of the function node in the device tree.
1242 * @param tree The device tree.
1243 * @return The function node if it is present in the tree, NULL
1244 * otherwise.
1245 */
1246fun_node_t *find_fun_node_by_path(dev_tree_t *tree, char *path)
1247{
1248 assert(path != NULL);
1249
1250 bool is_absolute = path[0] == '/';
1251 if (!is_absolute) {
1252 return NULL;
1253 }
1254
1255 fibril_rwlock_read_lock(&tree->rwlock);
1256
1257 fun_node_t *fun = tree->root_node;
1258 /*
1259 * Relative path to the function from its parent (but with '/' at the
1260 * beginning)
1261 */
1262 char *rel_path = path;
1263 char *next_path_elem = NULL;
1264 bool cont = (rel_path[1] != '\0');
1265
1266 while (cont && fun != NULL) {
1267 next_path_elem = get_path_elem_end(rel_path + 1);
1268 if (next_path_elem[0] == '/') {
1269 cont = true;
1270 next_path_elem[0] = 0;
1271 } else {
1272 cont = false;
1273 }
1274
1275 fun = find_node_child(fun, rel_path + 1);
1276
1277 if (cont) {
1278 /* Restore the original path. */
1279 next_path_elem[0] = '/';
1280 }
1281 rel_path = next_path_elem;
1282 }
1283
1284 fibril_rwlock_read_unlock(&tree->rwlock);
1285
1286 return fun;
1287}
1288
1289/** Find function with a specified name belonging to given device.
1290 *
1291 * Device tree rwlock should be held at least for reading.
1292 *
1293 * @param dev Device the function belongs to.
1294 * @param name Function name (not path).
1295 * @return Function node.
1296 * @retval NULL No function with given name.
1297 */
1298fun_node_t *find_fun_node_in_device(dev_node_t *dev, const char *name)
1299{
1300 assert(dev != NULL);
1301 assert(name != NULL);
1302
1303 fun_node_t *fun;
1304
1305 list_foreach(dev->functions, link) {
1306 fun = list_get_instance(link, fun_node_t, dev_functions);
1307
1308 if (str_cmp(name, fun->name) == 0)
1309 return fun;
1310 }
1311
1312 return NULL;
1313}
1314
1315/** Find child function node with a specified name.
1316 *
1317 * Device tree rwlock should be held at least for reading.
1318 *
1319 * @param parent The parent function node.
1320 * @param name The name of the child function.
1321 * @return The child function node.
1322 */
1323fun_node_t *find_node_child(fun_node_t *pfun, const char *name)
1324{
1325 return find_fun_node_in_device(pfun->child, name);
1326}
1327
1328/* loc devices */
1329
1330fun_node_t *find_loc_tree_function(dev_tree_t *tree, service_id_t service_id)
1331{
1332 fun_node_t *fun = NULL;
1333 link_t *link;
1334 unsigned long key = (unsigned long) service_id;
1335
1336 fibril_rwlock_read_lock(&tree->rwlock);
1337 link = hash_table_find(&tree->loc_functions, &key);
1338 if (link != NULL)
1339 fun = hash_table_get_instance(link, fun_node_t, loc_fun);
1340 fibril_rwlock_read_unlock(&tree->rwlock);
1341
1342 return fun;
1343}
1344
1345void tree_add_loc_function(dev_tree_t *tree, fun_node_t *fun)
1346{
1347 unsigned long key = (unsigned long) fun->service_id;
1348 fibril_rwlock_write_lock(&tree->rwlock);
1349 hash_table_insert(&tree->loc_functions, &key, &fun->loc_fun);
1350 fibril_rwlock_write_unlock(&tree->rwlock);
1351}
1352
1353/** @}
1354 */
Note: See TracBrowser for help on using the repository browser.