source: mainline/uspace/srv/devman/devman.c@ 27bdfa5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 27bdfa5 was 27bdfa5, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

Merge mainline changes

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