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

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

Merge mainline changes.

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