source: mainline/uspace/srv/devman/devman.c@ 3375bd4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3375bd4 was c7bbf029, checked in by Martin Decky <martin@…>, 14 years ago

improve stack traces and assertions
reduce header files pollution

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