source: mainline/uspace/srv/devman/devman.c@ 58b833c

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

More cstyle in devman.

  • Property mode set to 100644
File size: 24.3 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
40#include "devman.h"
41
42/* hash table operations */
43
44static hash_index_t devices_hash(unsigned long key[])
45{
46 return key[0] % DEVICE_BUCKETS;
47}
48
49static int devman_devices_compare(unsigned long key[], hash_count_t keys,
50 link_t *item)
51{
52 node_t *dev = hash_table_get_instance(item, node_t, devman_link);
53 return (dev->handle == (device_handle_t) key[0]);
54}
55
56static int devmap_devices_compare(unsigned long key[], hash_count_t keys,
57 link_t *item)
58{
59 node_t *dev = hash_table_get_instance(item, node_t, devmap_link);
60 return (dev->devmap_handle == (dev_handle_t) key[0]);
61}
62
63static void devices_remove_callback(link_t *item)
64{
65}
66
67static hash_table_operations_t devman_devices_ops = {
68 .hash = devices_hash,
69 .compare = devman_devices_compare,
70 .remove_callback = devices_remove_callback
71};
72
73static hash_table_operations_t devmap_devices_ops = {
74 .hash = devices_hash,
75 .compare = devmap_devices_compare,
76 .remove_callback = devices_remove_callback
77};
78
79/** Allocate and initialize a new driver structure.
80 *
81 * @return Driver structure.
82 */
83driver_t *create_driver(void)
84{
85 driver_t *res = malloc(sizeof(driver_t));
86 if (res != NULL)
87 init_driver(res);
88 return res;
89}
90
91/** Add a driver to the list of drivers.
92 *
93 * @param drivers_list List of drivers.
94 * @param drv Driver structure.
95 */
96void add_driver(driver_list_t *drivers_list, driver_t *drv)
97{
98 fibril_mutex_lock(&drivers_list->drivers_mutex);
99 list_prepend(&drv->drivers, &drivers_list->drivers);
100 fibril_mutex_unlock(&drivers_list->drivers_mutex);
101
102 printf(NAME": the '%s' driver was added to the list of available "
103 "drivers.\n", drv->name);
104}
105
106/** Read match id at the specified position of a string and set the position in
107 * the string to the first character following the id.
108 *
109 * @param buf The position in the input string.
110 * @return The match id.
111 */
112char *read_match_id(char **buf)
113{
114 char *res = NULL;
115 size_t len = get_nonspace_len(*buf);
116
117 if (len > 0) {
118 res = malloc(len + 1);
119 if (res != NULL) {
120 str_ncpy(res, len + 1, *buf, len);
121 *buf += len;
122 }
123 }
124
125 return res;
126}
127
128/**
129 * Read match ids and associated match scores from a string.
130 *
131 * Each match score in the string is followed by its match id.
132 * The match ids and match scores are separated by whitespaces.
133 * Neither match ids nor match scores can contain whitespaces.
134 *
135 * @param buf The string from which the match ids are read.
136 * @param ids The list of match ids into which the match ids and
137 * scores are added.
138 * @return True if at least one match id and associated match score
139 * was successfully read, false otherwise.
140 */
141bool parse_match_ids(char *buf, match_id_list_t *ids)
142{
143 int score = 0;
144 char *id = NULL;
145 int ids_read = 0;
146
147 while (true) {
148 /* skip spaces */
149 if (!skip_spaces(&buf))
150 break;
151
152 /* read score */
153 score = strtoul(buf, &buf, 10);
154
155 /* skip spaces */
156 if (!skip_spaces(&buf))
157 break;
158
159 /* read id */
160 id = read_match_id(&buf);
161 if (NULL == id)
162 break;
163
164 /* create new match_id structure */
165 match_id_t *mid = create_match_id();
166 mid->id = id;
167 mid->score = score;
168
169 /* add it to the list */
170 add_match_id(ids, mid);
171
172 ids_read++;
173 }
174
175 return ids_read > 0;
176}
177
178/**
179 * Read match ids and associated match scores from a file.
180 *
181 * Each match score in the file is followed by its match id.
182 * The match ids and match scores are separated by whitespaces.
183 * Neither match ids nor match scores can contain whitespaces.
184 *
185 * @param buf The path to the file from which the match ids are read.
186 * @param ids The list of match ids into which the match ids and
187 * scores are added.
188 * @return True if at least one match id and associated match score
189 * was successfully read, false otherwise.
190 */
191bool read_match_ids(const char *conf_path, match_id_list_t *ids)
192{
193 printf(NAME ": read_match_ids conf_path = %s.\n", conf_path);
194
195 bool suc = false;
196 char *buf = NULL;
197 bool opened = false;
198 int fd;
199 size_t len = 0;
200
201 fd = open(conf_path, O_RDONLY);
202 if (fd < 0) {
203 printf(NAME ": unable to open %s\n", conf_path);
204 goto cleanup;
205 }
206 opened = true;
207
208 len = lseek(fd, 0, SEEK_END);
209 lseek(fd, 0, SEEK_SET);
210 if (len == 0) {
211 printf(NAME ": configuration file '%s' is empty.\n", conf_path);
212 goto cleanup;
213 }
214
215 buf = malloc(len + 1);
216 if (buf == NULL) {
217 printf(NAME ": memory allocation failed when parsing file "
218 "'%s'.\n", conf_path);
219 goto cleanup;
220 }
221
222 if (read(fd, buf, len) <= 0) {
223 printf(NAME ": unable to read file '%s'.\n", conf_path);
224 goto cleanup;
225 }
226 buf[len] = 0;
227
228 suc = parse_match_ids(buf, ids);
229
230cleanup:
231 free(buf);
232
233 if (opened)
234 close(fd);
235
236 return suc;
237}
238
239/**
240 * Get information about a driver.
241 *
242 * Each driver has its own directory in the base directory.
243 * The name of the driver's directory is the same as the name of the driver.
244 * The driver's directory contains driver's binary (named as the driver without
245 * extension) and the configuration file with match ids for device-to-driver
246 * matching (named as the driver with a special extension).
247 *
248 * This function searches for the driver's directory and containing
249 * configuration files. If all the files needed are found, they are parsed and
250 * the information about the driver is stored in the driver's structure.
251 *
252 * @param base_path The base directory, in which we look for driver's
253 * subdirectory.
254 * @param name The name of the driver.
255 * @param drv The driver structure to fill information in.
256 *
257 * @return True on success, false otherwise.
258 */
259bool get_driver_info(const char *base_path, const char *name, driver_t *drv)
260{
261 printf(NAME ": get_driver_info base_path = %s, name = %s.\n",
262 base_path, name);
263
264 assert(base_path != NULL && name != NULL && drv != NULL);
265
266 bool suc = false;
267 char *match_path = NULL;
268 size_t name_size = 0;
269
270 /* Read the list of match ids from the driver's configuration file. */
271 match_path = get_abs_path(base_path, name, MATCH_EXT);
272 if (match_path == NULL)
273 goto cleanup;
274
275 if (!read_match_ids(match_path, &drv->match_ids))
276 goto cleanup;
277
278 /* Allocate and fill driver's name. */
279 name_size = str_size(name) + 1;
280 drv->name = malloc(name_size);
281 if (drv->name == NULL)
282 goto cleanup;
283 str_cpy(drv->name, name_size, name);
284
285 /* Initialize path with driver's binary. */
286 drv->binary_path = get_abs_path(base_path, name, "");
287 if (drv->binary_path == NULL)
288 goto cleanup;
289
290 /* Check whether the driver's binary exists. */
291 struct stat s;
292 if (stat(drv->binary_path, &s) == ENOENT) { /* FIXME!! */
293 printf(NAME ": driver not found at path %s.", drv->binary_path);
294 goto cleanup;
295 }
296
297 suc = true;
298
299cleanup:
300 if (!suc) {
301 free(drv->binary_path);
302 free(drv->name);
303 /* Set the driver structure to the default state. */
304 init_driver(drv);
305 }
306
307 free(match_path);
308
309 return suc;
310}
311
312/** Lookup drivers in the directory.
313 *
314 * @param drivers_list The list of available drivers.
315 * @param dir_path The path to the directory where we search for drivers.
316 * @return Number of drivers which were found.
317 */
318int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path)
319{
320 printf(NAME ": lookup_available_drivers, dir = %s \n", dir_path);
321
322 int drv_cnt = 0;
323 DIR *dir = NULL;
324 struct dirent *diren;
325
326 dir = opendir(dir_path);
327
328 if (dir != NULL) {
329 driver_t *drv = create_driver();
330 while ((diren = readdir(dir))) {
331 if (get_driver_info(dir_path, diren->d_name, drv)) {
332 add_driver(drivers_list, drv);
333 drv_cnt++;
334 drv = create_driver();
335 }
336 }
337 delete_driver(drv);
338 closedir(dir);
339 }
340
341 return drv_cnt;
342}
343
344/** Create root device node in the device tree.
345 *
346 * @param tree The device tree.
347 * @return True on success, false otherwise.
348 */
349bool create_root_node(dev_tree_t *tree)
350{
351 node_t *node;
352
353 printf(NAME ": create_root_node\n");
354
355 node = create_dev_node();
356 if (node != NULL) {
357 insert_dev_node(tree, node, clone_string(""), NULL);
358 match_id_t *id = create_match_id();
359 id->id = clone_string("root");
360 id->score = 100;
361 add_match_id(&node->match_ids, id);
362 tree->root_node = node;
363 }
364
365 return node != NULL;
366}
367
368/** Lookup the best matching driver for the specified device in the list of
369 * drivers.
370 *
371 * A match between a device and a driver is found if one of the driver's match
372 * ids match one of the device's match ids. The score of the match is the
373 * product of the driver's and device's score associated with the matching id.
374 * The best matching driver for a device is the driver with the highest score
375 * of the match between the device and the driver.
376 *
377 * @param drivers_list The list of drivers, where we look for the driver
378 * suitable for handling the device.
379 * @param node The device node structure of the device.
380 * @return The best matching driver or NULL if no matching driver
381 * is found.
382 */
383driver_t *find_best_match_driver(driver_list_t *drivers_list, node_t *node)
384{
385 driver_t *best_drv = NULL, *drv = NULL;
386 int best_score = 0, score = 0;
387
388 fibril_mutex_lock(&drivers_list->drivers_mutex);
389
390 link_t *link = drivers_list->drivers.next;
391 while (link != &drivers_list->drivers) {
392 drv = list_get_instance(link, driver_t, drivers);
393 score = get_match_score(drv, node);
394 if (score > best_score) {
395 best_score = score;
396 best_drv = drv;
397 }
398 link = link->next;
399 }
400
401 fibril_mutex_unlock(&drivers_list->drivers_mutex);
402
403 return best_drv;
404}
405
406/** Assign a driver to a device.
407 *
408 * @param node The device's node in the device tree.
409 * @param drv The driver.
410 */
411void attach_driver(node_t *node, driver_t *drv)
412{
413 printf(NAME ": attach_driver %s to device %s\n",
414 drv->name, node->pathname);
415
416 fibril_mutex_lock(&drv->driver_mutex);
417
418 node->drv = drv;
419 list_append(&node->driver_devices, &drv->devices);
420
421 fibril_mutex_unlock(&drv->driver_mutex);
422}
423
424/** Start a driver
425 *
426 * The driver's mutex is assumed to be locked.
427 *
428 * @param drv The driver's structure.
429 * @return True if the driver's task is successfully spawned, false
430 * otherwise.
431 */
432bool start_driver(driver_t *drv)
433{
434 printf(NAME ": start_driver '%s'\n", drv->name);
435
436 const char *argv[2];
437
438 argv[0] = drv->name;
439 argv[1] = NULL;
440
441 int err;
442 if (task_spawn(drv->binary_path, argv, &err) == 0) {
443 printf(NAME ": error spawning %s, errno = %d\n",
444 drv->name, err);
445 return false;
446 }
447
448 drv->state = DRIVER_STARTING;
449 return true;
450}
451
452/** Find device driver in the list of device drivers.
453 *
454 * @param drv_list The list of device drivers.
455 * @param drv_name The name of the device driver which is searched.
456 * @return The device driver of the specified name, if it is in the
457 * list, NULL otherwise.
458 */
459driver_t *find_driver(driver_list_t *drv_list, const char *drv_name)
460{
461 driver_t *res = NULL;
462 driver_t *drv = NULL;
463 link_t *link;
464
465 fibril_mutex_lock(&drv_list->drivers_mutex);
466
467 link = drv_list->drivers.next;
468 while (link != &drv_list->drivers) {
469 drv = list_get_instance(link, driver_t, drivers);
470 if (str_cmp(drv->name, drv_name) == 0) {
471 res = drv;
472 break;
473 }
474
475 link = link->next;
476 }
477
478 fibril_mutex_unlock(&drv_list->drivers_mutex);
479
480 return res;
481}
482
483/** Remember the driver's phone.
484 *
485 * @param driver The driver.
486 * @param phone The phone to the driver.
487 */
488void set_driver_phone(driver_t *driver, ipcarg_t phone)
489{
490 fibril_mutex_lock(&driver->driver_mutex);
491 assert(driver->state == DRIVER_STARTING);
492 driver->phone = phone;
493 fibril_mutex_unlock(&driver->driver_mutex);
494}
495
496/** Notify driver about the devices to which it was assigned.
497 *
498 * The driver's mutex must be locked.
499 *
500 * @param driver The driver to which the devices are passed.
501 */
502static void pass_devices_to_driver(driver_t *driver, dev_tree_t *tree)
503{
504 node_t *dev;
505 link_t *link;
506 int phone;
507
508 printf(NAME ": pass_devices_to_driver\n");
509
510 phone = ipc_connect_me_to(driver->phone, DRIVER_DEVMAN, 0, 0);
511 if (phone > 0) {
512
513 link = driver->devices.next;
514 while (link != &driver->devices) {
515 dev = list_get_instance(link, node_t, driver_devices);
516 add_device(phone, driver, dev, tree);
517 link = link->next;
518 }
519
520 ipc_hangup(phone);
521 }
522}
523
524/** Finish the initialization of a driver after it has succesfully started
525 * and after it has registered itself by the device manager.
526 *
527 * Pass devices formerly matched to the driver to the driver and remember the
528 * driver is running and fully functional now.
529 *
530 * @param driver The driver which registered itself as running by the
531 * device manager.
532 */
533void initialize_running_driver(driver_t *driver, dev_tree_t *tree)
534{
535 printf(NAME ": initialize_running_driver\n");
536 fibril_mutex_lock(&driver->driver_mutex);
537
538 /*
539 * Pass devices which have been already assigned to the driver to the
540 * driver.
541 */
542 pass_devices_to_driver(driver, tree);
543
544 /* Change driver's state to running. */
545 driver->state = DRIVER_RUNNING;
546
547 fibril_mutex_unlock(&driver->driver_mutex);
548}
549
550
551/** Create devmap path and name for the device. */
552static void devmap_register_tree_device(node_t *node, dev_tree_t *tree)
553{
554 char *devmap_pathname = NULL;
555 char *devmap_name = NULL;
556
557 asprintf(&devmap_name, "%s", node->pathname);
558 if (devmap_name == NULL)
559 return;
560
561 replace_char(devmap_name, '/', DEVMAP_SEPARATOR);
562
563 asprintf(&devmap_pathname, "%s/%s", DEVMAP_DEVICE_NAMESPACE,
564 devmap_name);
565 if (devmap_pathname == NULL) {
566 free(devmap_name);
567 return;
568 }
569
570 devmap_device_register(devmap_pathname, &node->devmap_handle);
571
572 tree_add_devmap_device(tree, node);
573
574 free(devmap_name);
575 free(devmap_pathname);
576}
577
578
579/** Pass a device to running driver.
580 *
581 * @param drv The driver's structure.
582 * @param node The device's node in the device tree.
583 */
584void add_device(int phone, driver_t *drv, node_t *node, dev_tree_t *tree)
585{
586 printf(NAME ": add_device\n");
587
588 ipcarg_t rc;
589 ipc_call_t answer;
590
591 /* Send the device to the driver. */
592 aid_t req = async_send_1(phone, DRIVER_ADD_DEVICE, node->handle,
593 &answer);
594
595 /* Send the device's name to the driver. */
596 rc = async_data_write_start(phone, node->name,
597 str_size(node->name) + 1);
598 if (rc != EOK) {
599 /* TODO handle error */
600 }
601
602 /* Wait for answer from the driver. */
603 async_wait_for(req, &rc);
604 switch(rc) {
605 case EOK:
606 node->state = DEVICE_USABLE;
607 devmap_register_tree_device(node, tree);
608 break;
609 case ENOENT:
610 node->state = DEVICE_NOT_PRESENT;
611 break;
612 default:
613 node->state = DEVICE_INVALID;
614 }
615
616 return;
617}
618
619/** Find suitable driver for a device and assign the driver to it.
620 *
621 * @param node The device node of the device in the device tree.
622 * @param drivers_list The list of available drivers.
623 * @return True if the suitable driver is found and
624 * successfully assigned to the device, false otherwise.
625 */
626bool assign_driver(node_t *node, driver_list_t *drivers_list, dev_tree_t *tree)
627{
628 /*
629 * Find the driver which is the most suitable for handling this device.
630 */
631 driver_t *drv = find_best_match_driver(drivers_list, node);
632 if (drv == NULL) {
633 printf(NAME ": no driver found for device '%s'.\n",
634 node->pathname);
635 return false;
636 }
637
638 /* Attach the driver to the device. */
639 attach_driver(node, drv);
640
641 if (drv->state == DRIVER_NOT_STARTED) {
642 /* Start the driver. */
643 start_driver(drv);
644 }
645
646 if (drv->state == DRIVER_RUNNING) {
647 /* Notify the driver about the new device. */
648 int phone = ipc_connect_me_to(drv->phone, DRIVER_DEVMAN, 0, 0);
649 if (phone > 0) {
650 add_device(phone, drv, node, tree);
651 ipc_hangup(phone);
652 }
653 }
654
655 return true;
656}
657
658/** Initialize the device tree.
659 *
660 * Create root device node of the tree and assign driver to it.
661 *
662 * @param tree The device tree.
663 * @param drivers_list the list of available drivers.
664 * @return True on success, false otherwise.
665 */
666bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list)
667{
668 printf(NAME ": init_device_tree.\n");
669
670 tree->current_handle = 0;
671
672 hash_table_create(&tree->devman_devices, DEVICE_BUCKETS, 1,
673 &devman_devices_ops);
674 hash_table_create(&tree->devmap_devices, DEVICE_BUCKETS, 1,
675 &devmap_devices_ops);
676
677 fibril_rwlock_initialize(&tree->rwlock);
678
679 /* Create root node and add it to the device tree. */
680 if (!create_root_node(tree))
681 return false;
682
683 /* Find suitable driver and start it. */
684 return assign_driver(tree->root_node, drivers_list, tree);
685}
686
687/** Create and set device's full path in device tree.
688 *
689 * @param node The device's device node.
690 * @param parent The parent device node.
691 * @return True on success, false otherwise (insufficient
692 * resources etc.).
693 */
694static bool set_dev_path(node_t *node, node_t *parent)
695{
696 assert(node->name != NULL);
697
698 size_t pathsize = (str_size(node->name) + 1);
699 if (parent != NULL)
700 pathsize += str_size(parent->pathname) + 1;
701
702 node->pathname = (char *) malloc(pathsize);
703 if (node->pathname == NULL) {
704 printf(NAME ": failed to allocate device path.\n");
705 return false;
706 }
707
708 if (parent != NULL) {
709 str_cpy(node->pathname, pathsize, parent->pathname);
710 str_append(node->pathname, pathsize, "/");
711 str_append(node->pathname, pathsize, node->name);
712 } else {
713 str_cpy(node->pathname, pathsize, node->name);
714 }
715
716 return true;
717}
718
719/** Insert new device into device tree.
720 *
721 * The device tree's rwlock should be already held exclusively when calling this
722 * function.
723 *
724 * @param tree The device tree.
725 * @param node The newly added device node.
726 * @param dev_name The name of the newly added device.
727 * @param parent The parent device node.
728 *
729 * @return True on success, false otherwise (insufficient resources
730 * etc.).
731 */
732bool insert_dev_node(dev_tree_t *tree, node_t *node, char *dev_name,
733 node_t *parent)
734{
735 assert(node != NULL);
736 assert(tree != NULL);
737 assert(dev_name != NULL);
738
739 node->name = dev_name;
740 if (!set_dev_path(node, parent)) {
741 fibril_rwlock_write_unlock(&tree->rwlock);
742 return false;
743 }
744
745 /* Add the node to the handle-to-node map. */
746 node->handle = ++tree->current_handle;
747 unsigned long key = node->handle;
748 hash_table_insert(&tree->devman_devices, &key, &node->devman_link);
749
750 /* Add the node to the list of its parent's children. */
751 node->parent = parent;
752 if (parent != NULL)
753 list_append(&node->sibling, &parent->children);
754
755 return true;
756}
757
758/** Find device node with a specified path in the device tree.
759 *
760 * @param path The path of the device node in the device tree.
761 * @param tree The device tree.
762 * @return The device node if it is present in the tree, NULL
763 * otherwise.
764 */
765node_t *find_dev_node_by_path(dev_tree_t *tree, char *path)
766{
767 fibril_rwlock_read_lock(&tree->rwlock);
768
769 node_t *dev = tree->root_node;
770 /*
771 * Relative path to the device from its parent (but with '/' at the
772 * beginning)
773 */
774 char *rel_path = path;
775 char *next_path_elem = NULL;
776 bool cont = (rel_path[0] == '/');
777
778 while (cont && dev != NULL) {
779 next_path_elem = get_path_elem_end(rel_path + 1);
780 if (next_path_elem[0] == '/') {
781 cont = true;
782 next_path_elem[0] = 0;
783 } else {
784 cont = false;
785 }
786
787 dev = find_node_child(dev, rel_path + 1);
788
789 if (cont) {
790 /* Restore the original path. */
791 next_path_elem[0] = '/';
792 }
793 rel_path = next_path_elem;
794 }
795
796 fibril_rwlock_read_unlock(&tree->rwlock);
797
798 return dev;
799}
800
801/** Find child device node with a specified name.
802 *
803 * Device tree rwlock should be held at least for reading.
804 *
805 * @param parent The parent device node.
806 * @param name The name of the child device node.
807 * @return The child device node.
808 */
809node_t *find_node_child(node_t *parent, const char *name)
810{
811 node_t *dev;
812 link_t *link;
813
814 link = parent->children.next;
815
816 while (link != &parent->children) {
817 dev = list_get_instance(link, node_t, sibling);
818
819 if (str_cmp(name, dev->name) == 0)
820 return dev;
821
822 link = link->next;
823 }
824
825 return NULL;
826}
827
828/** Create unique device name within the class.
829 *
830 * @param cl The class.
831 * @param base_dev_name Contains the base name for the device if it was
832 * specified by the driver when it registered the device by
833 * the class; NULL if driver specified no base name.
834 * @return The unique name for the device within the class.
835 */
836char *create_dev_name_for_class(dev_class_t *cl, const char *base_dev_name)
837{
838 char *dev_name;
839 const char *base_name;
840
841 if (base_dev_name != NULL)
842 base_name = base_dev_name;
843 else
844 base_name = cl->base_dev_name;
845
846 size_t idx = get_new_class_dev_idx(cl);
847 asprintf(&dev_name, "%s%d", base_name, idx);
848
849 return dev_name;
850}
851
852/** Add the device to the class.
853 *
854 * The device may be added to multiple classes and a class may contain multiple
855 * devices. The class and the device are associated with each other by the
856 * dev_class_info_t structure.
857 *
858 * @param dev The device.
859 * @param class The class.
860 * @param base_dev_name The base name of the device within the class if
861 * specified by the driver, NULL otherwise.
862 * @return dev_class_info_t structure which associates the device
863 * with the class.
864 */
865dev_class_info_t *add_device_to_class(node_t *dev, dev_class_t *cl,
866 const char *base_dev_name)
867{
868 dev_class_info_t *info = create_dev_class_info();
869
870 if (info != NULL) {
871 info->dev_class = cl;
872 info->dev = dev;
873
874 /* Add the device to the class. */
875 fibril_mutex_lock(&cl->mutex);
876 list_append(&info->link, &cl->devices);
877 fibril_mutex_unlock(&cl->mutex);
878
879 /* Add the class to the device. */
880 list_append(&info->dev_classes, &dev->classes);
881
882 /* Create unique name for the device within the class. */
883 info->dev_name = create_dev_name_for_class(cl, base_dev_name);
884 }
885
886 return info;
887}
888
889dev_class_t *get_dev_class(class_list_t *class_list, char *class_name)
890{
891 dev_class_t *cl;
892
893 fibril_rwlock_write_lock(&class_list->rwlock);
894 cl = find_dev_class_no_lock(class_list, class_name);
895 if (cl == NULL) {
896 cl = create_dev_class();
897 if (cl != NULL) {
898 cl->name = class_name;
899 cl->base_dev_name = "";
900 add_dev_class_no_lock(class_list, cl);
901 }
902 }
903
904 fibril_rwlock_write_unlock(&class_list->rwlock);
905 return cl;
906}
907
908dev_class_t *find_dev_class_no_lock(class_list_t *class_list,
909 const char *class_name)
910{
911 dev_class_t *cl;
912 link_t *link = class_list->classes.next;
913
914 while (link != &class_list->classes) {
915 cl = list_get_instance(link, dev_class_t, link);
916 if (str_cmp(cl->name, class_name) == 0)
917 return cl;
918 }
919
920 return NULL;
921}
922
923void init_class_list(class_list_t *class_list)
924{
925 list_initialize(&class_list->classes);
926 fibril_rwlock_initialize(&class_list->rwlock);
927 hash_table_create(&class_list->devmap_devices, DEVICE_BUCKETS, 1,
928 &devmap_devices_ops);
929}
930
931
932/* devmap devices */
933
934node_t *find_devmap_tree_device(dev_tree_t *tree, dev_handle_t devmap_handle)
935{
936 node_t *dev = NULL;
937 link_t *link;
938 unsigned long key = (unsigned long) devmap_handle;
939
940 fibril_rwlock_read_lock(&tree->rwlock);
941 link = hash_table_find(&tree->devmap_devices, &key);
942 if (link != NULL)
943 dev = hash_table_get_instance(link, node_t, devmap_link);
944 fibril_rwlock_read_unlock(&tree->rwlock);
945
946 return dev;
947}
948
949node_t *find_devmap_class_device(class_list_t *classes,
950 dev_handle_t devmap_handle)
951{
952 node_t *dev = NULL;
953 dev_class_info_t *cli;
954 link_t *link;
955 unsigned long key = (unsigned long)devmap_handle;
956
957 fibril_rwlock_read_lock(&classes->rwlock);
958 link = hash_table_find(&classes->devmap_devices, &key);
959 if (link != NULL) {
960 cli = hash_table_get_instance(link, dev_class_info_t,
961 devmap_link);
962 dev = cli->dev;
963 }
964 fibril_rwlock_read_unlock(&classes->rwlock);
965
966 return dev;
967}
968
969/** @}
970 */
Note: See TracBrowser for help on using the repository browser.