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

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

Separate list_t typedef from link_t (user-space part).

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