source: mainline/uspace/srv/devman/devman.c@ 557c7d0

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

Merge mainline changes

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