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

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

Merge mainline changes (DDF refactoring)

This merge includes DDF refactoring that brought multifunctional devices
(i.e. ddf_dev_t and ddf_fun_t). Please, see ticket #295 at HelenOS
upstream Trac.

The conflicts themselves were easy to solve (merely several renamings).

Changes to USB subsystem:

  • drivers uses ddf_dev_t and ddf_fun_t
  • different signatures of many library functions
  • several hacks around communication with parent device (now the communication is clearer and somehow what we have now is hack about other hacks)
    • will repair and clean later
  • maybe added some extra debugging messages (the diff has about 240K, and I admit I have no energy to double check that)

WARNING:

  • the diff is VERY long, recommended is viewing partial diffs of the merge (i.e. merges in mainline branch that lead to the parent one)
  • merging with your branches might involve huge renamings, sorry, no other way is possible

BUGS:

  • hub driver will not work (no function created)

GOOD NEWS:

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