source: mainline/uspace/srv/devman/devman.c@ 3ad7b1c

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

Make devman robust against somebody manually starting a driver. Allow it
if the driver is not running yet. Prevent it if the driver is already
running. Fix phoneid check (zero is valid).

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