source: mainline/uspace/srv/devman/devman.c@ 7beb220

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

Skeleton of devctl utility. Currently prints device tree.

  • Property mode set to 100644
File size: 31.3 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/** Start a driver
486 *
487 * @param drv The driver's structure.
488 * @return True if the driver's task is successfully spawned, false
489 * otherwise.
490 */
491bool start_driver(driver_t *drv)
492{
493 int rc;
494
495 assert(fibril_mutex_is_locked(&drv->driver_mutex));
496
497 log_msg(LVL_DEBUG, "start_driver(drv=\"%s\")", drv->name);
498
499 rc = task_spawnl(NULL, drv->binary_path, drv->binary_path, NULL);
500 if (rc != EOK) {
501 log_msg(LVL_ERROR, "Spawning driver `%s' (%s) failed: %s.",
502 drv->name, drv->binary_path, str_error(rc));
503 return false;
504 }
505
506 drv->state = DRIVER_STARTING;
507 return true;
508}
509
510/** Find device driver in the list of device drivers.
511 *
512 * @param drv_list The list of device drivers.
513 * @param drv_name The name of the device driver which is searched.
514 * @return The device driver of the specified name, if it is in the
515 * list, NULL otherwise.
516 */
517driver_t *find_driver(driver_list_t *drv_list, const char *drv_name)
518{
519 driver_t *res = NULL;
520 driver_t *drv = NULL;
521
522 fibril_mutex_lock(&drv_list->drivers_mutex);
523
524 list_foreach(drv_list->drivers, link) {
525 drv = list_get_instance(link, driver_t, drivers);
526 if (str_cmp(drv->name, drv_name) == 0) {
527 res = drv;
528 break;
529 }
530 }
531
532 fibril_mutex_unlock(&drv_list->drivers_mutex);
533
534 return res;
535}
536
537/** Notify driver about the devices to which it was assigned.
538 *
539 * @param driver The driver to which the devices are passed.
540 */
541static void pass_devices_to_driver(driver_t *driver, dev_tree_t *tree)
542{
543 dev_node_t *dev;
544 link_t *link;
545
546 log_msg(LVL_DEBUG, "pass_devices_to_driver(driver=\"%s\")",
547 driver->name);
548
549 fibril_mutex_lock(&driver->driver_mutex);
550
551 async_exch_t *exch = async_exchange_begin(driver->sess);
552 async_sess_t *sess = async_connect_me_to(EXCHANGE_SERIALIZE, exch,
553 DRIVER_DEVMAN, 0, 0);
554 async_exchange_end(exch);
555
556 if (!sess) {
557 fibril_mutex_unlock(&driver->driver_mutex);
558 return;
559 }
560
561 /*
562 * Go through devices list as long as there is some device
563 * that has not been passed to the driver.
564 */
565 link = driver->devices.head.next;
566 while (link != &driver->devices.head) {
567 dev = list_get_instance(link, dev_node_t, driver_devices);
568 if (dev->passed_to_driver) {
569 link = link->next;
570 continue;
571 }
572
573 /*
574 * We remove the device from the list to allow safe adding
575 * of new devices (no one will touch our item this way).
576 */
577 list_remove(link);
578
579 /*
580 * Unlock to avoid deadlock when adding device
581 * handled by itself.
582 */
583 fibril_mutex_unlock(&driver->driver_mutex);
584
585 add_device(sess, driver, dev, tree);
586
587 /*
588 * Lock again as we will work with driver's
589 * structure.
590 */
591 fibril_mutex_lock(&driver->driver_mutex);
592
593 /*
594 * Insert the device back.
595 * The order is not relevant here so no harm is done
596 * (actually, the order would be preserved in most cases).
597 */
598 list_append(link, &driver->devices);
599
600 /*
601 * Restart the cycle to go through all devices again.
602 */
603 link = driver->devices.head.next;
604 }
605
606 async_hangup(sess);
607
608 /*
609 * Once we passed all devices to the driver, we need to mark the
610 * driver as running.
611 * It is vital to do it here and inside critical section.
612 *
613 * If we would change the state earlier, other devices added to
614 * the driver would be added to the device list and started
615 * immediately and possibly started here as well.
616 */
617 log_msg(LVL_DEBUG, "Driver `%s' enters running state.", driver->name);
618 driver->state = DRIVER_RUNNING;
619
620 fibril_mutex_unlock(&driver->driver_mutex);
621}
622
623/** Finish the initialization of a driver after it has succesfully started
624 * and after it has registered itself by the device manager.
625 *
626 * Pass devices formerly matched to the driver to the driver and remember the
627 * driver is running and fully functional now.
628 *
629 * @param driver The driver which registered itself as running by the
630 * device manager.
631 */
632void initialize_running_driver(driver_t *driver, dev_tree_t *tree)
633{
634 log_msg(LVL_DEBUG, "initialize_running_driver(driver=\"%s\")",
635 driver->name);
636
637 /*
638 * Pass devices which have been already assigned to the driver to the
639 * driver.
640 */
641 pass_devices_to_driver(driver, tree);
642}
643
644/** Initialize device driver structure.
645 *
646 * @param drv The device driver structure.
647 */
648void init_driver(driver_t *drv)
649{
650 assert(drv != NULL);
651
652 memset(drv, 0, sizeof(driver_t));
653 list_initialize(&drv->match_ids.ids);
654 list_initialize(&drv->devices);
655 fibril_mutex_initialize(&drv->driver_mutex);
656 drv->sess = NULL;
657}
658
659/** Device driver structure clean-up.
660 *
661 * @param drv The device driver structure.
662 */
663void clean_driver(driver_t *drv)
664{
665 assert(drv != NULL);
666
667 free_not_null(drv->name);
668 free_not_null(drv->binary_path);
669
670 clean_match_ids(&drv->match_ids);
671
672 init_driver(drv);
673}
674
675/** Delete device driver structure.
676 *
677 * @param drv The device driver structure.
678 */
679void delete_driver(driver_t *drv)
680{
681 assert(drv != NULL);
682
683 clean_driver(drv);
684 free(drv);
685}
686
687/** Create loc path and name for the function. */
688void loc_register_tree_function(fun_node_t *fun, dev_tree_t *tree)
689{
690 char *loc_pathname = NULL;
691 char *loc_name = NULL;
692
693 asprintf(&loc_name, "%s", fun->pathname);
694 if (loc_name == NULL)
695 return;
696
697 replace_char(loc_name, '/', LOC_SEPARATOR);
698
699 asprintf(&loc_pathname, "%s/%s", LOC_DEVICE_NAMESPACE,
700 loc_name);
701 if (loc_pathname == NULL) {
702 free(loc_name);
703 return;
704 }
705
706 loc_service_register_with_iface(loc_pathname,
707 &fun->service_id, DEVMAN_CONNECT_FROM_LOC);
708
709 tree_add_loc_function(tree, fun);
710
711 free(loc_name);
712 free(loc_pathname);
713}
714
715/** Pass a device to running driver.
716 *
717 * @param drv The driver's structure.
718 * @param node The device's node in the device tree.
719 */
720void add_device(async_sess_t *sess, driver_t *drv, dev_node_t *dev,
721 dev_tree_t *tree)
722{
723 /*
724 * We do not expect to have driver's mutex locked as we do not
725 * access any structures that would affect driver_t.
726 */
727 log_msg(LVL_DEBUG, "add_device(drv=\"%s\", dev=\"%s\")",
728 drv->name, dev->pfun->name);
729
730 /* Send the device to the driver. */
731 devman_handle_t parent_handle;
732 if (dev->pfun) {
733 parent_handle = dev->pfun->handle;
734 } else {
735 parent_handle = 0;
736 }
737
738 async_exch_t *exch = async_exchange_begin(sess);
739
740 ipc_call_t answer;
741 aid_t req = async_send_2(exch, DRIVER_ADD_DEVICE, dev->handle,
742 parent_handle, &answer);
743
744 /* Send the device name to the driver. */
745 sysarg_t rc = async_data_write_start(exch, dev->pfun->name,
746 str_size(dev->pfun->name) + 1);
747
748 async_exchange_end(exch);
749
750 if (rc != EOK) {
751 /* TODO handle error */
752 }
753
754 /* Wait for answer from the driver. */
755 async_wait_for(req, &rc);
756
757 switch(rc) {
758 case EOK:
759 dev->state = DEVICE_USABLE;
760 break;
761 case ENOENT:
762 dev->state = DEVICE_NOT_PRESENT;
763 break;
764 default:
765 dev->state = DEVICE_INVALID;
766 }
767
768 dev->passed_to_driver = true;
769
770 return;
771}
772
773/** Find suitable driver for a device and assign the driver to it.
774 *
775 * @param node The device node of the device in the device tree.
776 * @param drivers_list The list of available drivers.
777 * @return True if the suitable driver is found and
778 * successfully assigned to the device, false otherwise.
779 */
780bool assign_driver(dev_node_t *dev, driver_list_t *drivers_list,
781 dev_tree_t *tree)
782{
783 assert(dev != NULL);
784 assert(drivers_list != NULL);
785 assert(tree != NULL);
786
787 /*
788 * Find the driver which is the most suitable for handling this device.
789 */
790 driver_t *drv = find_best_match_driver(drivers_list, dev);
791 if (drv == NULL) {
792 log_msg(LVL_ERROR, "No driver found for device `%s'.",
793 dev->pfun->pathname);
794 return false;
795 }
796
797 /* Attach the driver to the device. */
798 attach_driver(dev, drv);
799
800 fibril_mutex_lock(&drv->driver_mutex);
801 if (drv->state == DRIVER_NOT_STARTED) {
802 /* Start the driver. */
803 start_driver(drv);
804 }
805 bool is_running = drv->state == DRIVER_RUNNING;
806 fibril_mutex_unlock(&drv->driver_mutex);
807
808 if (is_running) {
809 /* Notify the driver about the new device. */
810 async_exch_t *exch = async_exchange_begin(drv->sess);
811 async_sess_t *sess = async_connect_me_to(EXCHANGE_SERIALIZE, exch,
812 DRIVER_DEVMAN, 0, 0);
813 async_exchange_end(exch);
814
815 if (sess) {
816 add_device(sess, drv, dev, tree);
817 async_hangup(sess);
818 }
819 }
820
821 return true;
822}
823
824/** Initialize the device tree.
825 *
826 * Create root device node of the tree and assign driver to it.
827 *
828 * @param tree The device tree.
829 * @param drivers_list the list of available drivers.
830 * @return True on success, false otherwise.
831 */
832bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list)
833{
834 log_msg(LVL_DEBUG, "init_device_tree()");
835
836 tree->current_handle = 0;
837
838 hash_table_create(&tree->devman_devices, DEVICE_BUCKETS, 1,
839 &devman_devices_ops);
840 hash_table_create(&tree->devman_functions, DEVICE_BUCKETS, 1,
841 &devman_functions_ops);
842 hash_table_create(&tree->loc_functions, DEVICE_BUCKETS, 1,
843 &loc_devices_ops);
844
845 fibril_rwlock_initialize(&tree->rwlock);
846
847 /* Create root function and root device and add them to the device tree. */
848 if (!create_root_nodes(tree))
849 return false;
850
851 /* Find suitable driver and start it. */
852 return assign_driver(tree->root_node->child, drivers_list, tree);
853}
854
855/* Device nodes */
856
857/** Create a new device node.
858 *
859 * @return A device node structure.
860 */
861dev_node_t *create_dev_node(void)
862{
863 dev_node_t *res = malloc(sizeof(dev_node_t));
864
865 if (res != NULL) {
866 memset(res, 0, sizeof(dev_node_t));
867 list_initialize(&res->functions);
868 link_initialize(&res->driver_devices);
869 link_initialize(&res->devman_dev);
870 }
871
872 return res;
873}
874
875/** Delete a device node.
876 *
877 * @param node The device node structure.
878 */
879void delete_dev_node(dev_node_t *dev)
880{
881 assert(list_empty(&dev->functions));
882 assert(dev->pfun == NULL);
883 assert(dev->drv == NULL);
884
885 free(dev);
886}
887
888/** Find the device node structure of the device witch has the specified handle.
889 *
890 * @param tree The device tree where we look for the device node.
891 * @param handle The handle of the device.
892 * @return The device node.
893 */
894dev_node_t *find_dev_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
895{
896 unsigned long key = handle;
897 link_t *link;
898
899 assert(fibril_rwlock_is_locked(&tree->rwlock));
900
901 link = hash_table_find(&tree->devman_devices, &key);
902 return hash_table_get_instance(link, dev_node_t, devman_dev);
903}
904
905/** Find the device node structure of the device witch has the specified handle.
906 *
907 * @param tree The device tree where we look for the device node.
908 * @param handle The handle of the device.
909 * @return The device node.
910 */
911dev_node_t *find_dev_node(dev_tree_t *tree, devman_handle_t handle)
912{
913 dev_node_t *dev = NULL;
914
915 fibril_rwlock_read_lock(&tree->rwlock);
916 dev = find_dev_node_no_lock(tree, handle);
917 fibril_rwlock_read_unlock(&tree->rwlock);
918
919 return dev;
920}
921
922/** Get list of device functions. */
923int dev_get_functions(dev_tree_t *tree, dev_node_t *dev,
924 devman_handle_t *hdl_buf, size_t buf_size, size_t *act_size)
925{
926 size_t act_cnt;
927 size_t buf_cnt;
928
929 assert(fibril_rwlock_is_locked(&tree->rwlock));
930
931 buf_cnt = buf_size / sizeof(devman_handle_t);
932
933 act_cnt = list_count(&dev->functions);
934 *act_size = act_cnt * sizeof(devman_handle_t);
935
936 if (buf_size % sizeof(devman_handle_t) != 0)
937 return EINVAL;
938
939 size_t pos = 0;
940 list_foreach(dev->functions, item) {
941 fun_node_t *fun =
942 list_get_instance(item, fun_node_t, dev_functions);
943
944 if (pos < buf_cnt)
945 hdl_buf[pos] = fun->handle;
946 pos++;
947 }
948
949 return EOK;
950}
951
952
953/* Function nodes */
954
955/** Create a new function node.
956 *
957 * @return A function node structure.
958 */
959fun_node_t *create_fun_node(void)
960{
961 fun_node_t *res = malloc(sizeof(fun_node_t));
962
963 if (res != NULL) {
964 memset(res, 0, sizeof(fun_node_t));
965 link_initialize(&res->dev_functions);
966 list_initialize(&res->match_ids.ids);
967 link_initialize(&res->devman_fun);
968 link_initialize(&res->loc_fun);
969 }
970
971 return res;
972}
973
974/** Delete a function node.
975 *
976 * @param fun The device node structure.
977 */
978void delete_fun_node(fun_node_t *fun)
979{
980 assert(fun->dev == NULL);
981 assert(fun->child == NULL);
982
983 clean_match_ids(&fun->match_ids);
984 free_not_null(fun->name);
985 free_not_null(fun->pathname);
986 free(fun);
987}
988
989/** Find the function node with the specified handle.
990 *
991 * @param tree The device tree where we look for the device node.
992 * @param handle The handle of the function.
993 * @return The function node.
994 */
995fun_node_t *find_fun_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
996{
997 unsigned long key = handle;
998 link_t *link;
999
1000 assert(fibril_rwlock_is_locked(&tree->rwlock));
1001
1002 link = hash_table_find(&tree->devman_functions, &key);
1003 if (link == NULL)
1004 return NULL;
1005
1006 return hash_table_get_instance(link, fun_node_t, devman_fun);
1007}
1008
1009/** Find the function node with the specified handle.
1010 *
1011 * @param tree The device tree where we look for the device node.
1012 * @param handle The handle of the function.
1013 * @return The function node.
1014 */
1015fun_node_t *find_fun_node(dev_tree_t *tree, devman_handle_t handle)
1016{
1017 fun_node_t *fun = NULL;
1018
1019 fibril_rwlock_read_lock(&tree->rwlock);
1020 fun = find_fun_node_no_lock(tree, handle);
1021 fibril_rwlock_read_unlock(&tree->rwlock);
1022
1023 return fun;
1024}
1025
1026/** Create and set device's full path in device tree.
1027 *
1028 * @param node The device's device node.
1029 * @param parent The parent device node.
1030 * @return True on success, false otherwise (insufficient
1031 * resources etc.).
1032 */
1033static bool set_fun_path(fun_node_t *fun, fun_node_t *parent)
1034{
1035 assert(fun->name != NULL);
1036
1037 size_t pathsize = (str_size(fun->name) + 1);
1038 if (parent != NULL)
1039 pathsize += str_size(parent->pathname) + 1;
1040
1041 fun->pathname = (char *) malloc(pathsize);
1042 if (fun->pathname == NULL) {
1043 log_msg(LVL_ERROR, "Failed to allocate device path.");
1044 return false;
1045 }
1046
1047 if (parent != NULL) {
1048 str_cpy(fun->pathname, pathsize, parent->pathname);
1049 str_append(fun->pathname, pathsize, "/");
1050 str_append(fun->pathname, pathsize, fun->name);
1051 } else {
1052 str_cpy(fun->pathname, pathsize, fun->name);
1053 }
1054
1055 return true;
1056}
1057
1058/** Insert new device into device tree.
1059 *
1060 * @param tree The device tree.
1061 * @param node The newly added device node.
1062 * @param dev_name The name of the newly added device.
1063 * @param parent The parent device node.
1064 *
1065 * @return True on success, false otherwise (insufficient resources
1066 * etc.).
1067 */
1068bool insert_dev_node(dev_tree_t *tree, dev_node_t *dev, fun_node_t *pfun)
1069{
1070 assert(dev != NULL);
1071 assert(tree != NULL);
1072 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
1073
1074 log_msg(LVL_DEBUG, "insert_dev_node(dev=%p, pfun=%p [\"%s\"])",
1075 dev, pfun, pfun->pathname);
1076
1077 /* Add the node to the handle-to-node map. */
1078 dev->handle = ++tree->current_handle;
1079 unsigned long key = dev->handle;
1080 hash_table_insert(&tree->devman_devices, &key, &dev->devman_dev);
1081
1082 /* Add the node to the list of its parent's children. */
1083 dev->pfun = pfun;
1084 pfun->child = dev;
1085
1086 return true;
1087}
1088
1089/** Insert new function into device tree.
1090 *
1091 * @param tree The device tree.
1092 * @param node The newly added function node.
1093 * @param dev_name The name of the newly added function.
1094 * @param parent Owning device node.
1095 *
1096 * @return True on success, false otherwise (insufficient resources
1097 * etc.).
1098 */
1099bool insert_fun_node(dev_tree_t *tree, fun_node_t *fun, char *fun_name,
1100 dev_node_t *dev)
1101{
1102 fun_node_t *pfun;
1103
1104 assert(fun != NULL);
1105 assert(tree != NULL);
1106 assert(fun_name != NULL);
1107 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
1108
1109 /*
1110 * The root function is a special case, it does not belong to any
1111 * device so for the root function dev == NULL.
1112 */
1113 pfun = (dev != NULL) ? dev->pfun : NULL;
1114
1115 fun->name = fun_name;
1116 if (!set_fun_path(fun, pfun)) {
1117 return false;
1118 }
1119
1120 /* Add the node to the handle-to-node map. */
1121 fun->handle = ++tree->current_handle;
1122 unsigned long key = fun->handle;
1123 hash_table_insert(&tree->devman_functions, &key, &fun->devman_fun);
1124
1125 /* Add the node to the list of its parent's children. */
1126 fun->dev = dev;
1127 if (dev != NULL)
1128 list_append(&fun->dev_functions, &dev->functions);
1129
1130 return true;
1131}
1132
1133/** Remove function from device tree.
1134 *
1135 * @param tree Device tree
1136 * @param node Function node to remove
1137 */
1138void remove_fun_node(dev_tree_t *tree, fun_node_t *fun)
1139{
1140 assert(tree != NULL);
1141 assert(fun != NULL);
1142 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
1143
1144 /* Remove the node from the handle-to-node map. */
1145 unsigned long key = fun->handle;
1146 hash_table_remove(&tree->devman_functions, &key, 1);
1147
1148 /* Remove the node from the list of its parent's children. */
1149 if (fun->dev != NULL)
1150 list_remove(&fun->dev_functions);
1151}
1152
1153/** Find function node with a specified path in the device tree.
1154 *
1155 * @param path The path of the function node in the device tree.
1156 * @param tree The device tree.
1157 * @return The function node if it is present in the tree, NULL
1158 * otherwise.
1159 */
1160fun_node_t *find_fun_node_by_path(dev_tree_t *tree, char *path)
1161{
1162 assert(path != NULL);
1163
1164 bool is_absolute = path[0] == '/';
1165 if (!is_absolute) {
1166 return NULL;
1167 }
1168
1169 fibril_rwlock_read_lock(&tree->rwlock);
1170
1171 fun_node_t *fun = tree->root_node;
1172 /*
1173 * Relative path to the function from its parent (but with '/' at the
1174 * beginning)
1175 */
1176 char *rel_path = path;
1177 char *next_path_elem = NULL;
1178 bool cont = (rel_path[1] != '\0');
1179
1180 while (cont && fun != NULL) {
1181 next_path_elem = get_path_elem_end(rel_path + 1);
1182 if (next_path_elem[0] == '/') {
1183 cont = true;
1184 next_path_elem[0] = 0;
1185 } else {
1186 cont = false;
1187 }
1188
1189 fun = find_node_child(fun, rel_path + 1);
1190
1191 if (cont) {
1192 /* Restore the original path. */
1193 next_path_elem[0] = '/';
1194 }
1195 rel_path = next_path_elem;
1196 }
1197
1198 fibril_rwlock_read_unlock(&tree->rwlock);
1199
1200 return fun;
1201}
1202
1203/** Find function with a specified name belonging to given device.
1204 *
1205 * Device tree rwlock should be held at least for reading.
1206 *
1207 * @param dev Device the function belongs to.
1208 * @param name Function name (not path).
1209 * @return Function node.
1210 * @retval NULL No function with given name.
1211 */
1212fun_node_t *find_fun_node_in_device(dev_node_t *dev, const char *name)
1213{
1214 assert(dev != NULL);
1215 assert(name != NULL);
1216
1217 fun_node_t *fun;
1218
1219 list_foreach(dev->functions, link) {
1220 fun = list_get_instance(link, fun_node_t, dev_functions);
1221
1222 if (str_cmp(name, fun->name) == 0)
1223 return fun;
1224 }
1225
1226 return NULL;
1227}
1228
1229/** Find child function node with a specified name.
1230 *
1231 * Device tree rwlock should be held at least for reading.
1232 *
1233 * @param parent The parent function node.
1234 * @param name The name of the child function.
1235 * @return The child function node.
1236 */
1237fun_node_t *find_node_child(fun_node_t *pfun, const char *name)
1238{
1239 return find_fun_node_in_device(pfun->child, name);
1240}
1241
1242/* loc devices */
1243
1244fun_node_t *find_loc_tree_function(dev_tree_t *tree, service_id_t service_id)
1245{
1246 fun_node_t *fun = NULL;
1247 link_t *link;
1248 unsigned long key = (unsigned long) service_id;
1249
1250 fibril_rwlock_read_lock(&tree->rwlock);
1251 link = hash_table_find(&tree->loc_functions, &key);
1252 if (link != NULL)
1253 fun = hash_table_get_instance(link, fun_node_t, loc_fun);
1254 fibril_rwlock_read_unlock(&tree->rwlock);
1255
1256 return fun;
1257}
1258
1259void tree_add_loc_function(dev_tree_t *tree, fun_node_t *fun)
1260{
1261 unsigned long key = (unsigned long) fun->service_id;
1262 fibril_rwlock_write_lock(&tree->rwlock);
1263 hash_table_insert(&tree->loc_functions, &key, &fun->loc_fun);
1264 fibril_rwlock_write_unlock(&tree->rwlock);
1265}
1266
1267/** @}
1268 */
Note: See TracBrowser for help on using the repository browser.