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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a32defa was a32defa, checked in by Lenka Trochtova <trochtova.lenka@…>, 15 years ago

Register devices in the device tree by the device mapper so the device hierarchy can be seen from devfs.

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