source: mainline/uspace/srv/devman/devman.c@ dc87f3fd

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

Devman must be robust against short reads.

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