source: mainline/uspace/srv/devman/devman.c@ 0c968a17

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

Merge DDF and drivers refactoring work. Major points:

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