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

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

Bugfix in hash table usage in devman

The hash table with device class must use different comparing due to
different backend structures.

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