source: mainline/uspace/srv/devman/devman.c@ 58cbb0c8

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

Reference counting of device and function nodes in devman.

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