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

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

Send device's name to a driver when the device is passed to it.

  • Property mode set to 100644
File size: 18.9 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
39#include "devman.h"
40#include "util.h"
41
42/** Allocate and initialize a new driver structure.
43 *
44 * @return driver structure.
45 */
46driver_t * create_driver()
47{
48 driver_t *res = malloc(sizeof(driver_t));
49 if(res != NULL) {
50 init_driver(res);
51 }
52 return res;
53}
54
55/** Add a driver to the list of drivers.
56 *
57 * @param drivers_list the list of drivers.
58 * @param drv the driver's structure.
59 */
60void add_driver(driver_list_t *drivers_list, driver_t *drv)
61{
62 fibril_mutex_lock(&drivers_list->drivers_mutex);
63 list_prepend(&drv->drivers, &drivers_list->drivers);
64 fibril_mutex_unlock(&drivers_list->drivers_mutex);
65
66 printf(NAME": the '%s' driver was added to the list of available drivers.\n", drv->name);
67}
68
69/** Read match id at the specified position of a string and set
70 * the position in the string to the first character following the id.
71 *
72 * @param buf the position in the input string.
73 *
74 * @return the match id.
75 */
76char * read_match_id(const char **buf)
77{
78 char *res = NULL;
79 size_t len = get_nonspace_len(*buf);
80 if (len > 0) {
81 res = malloc(len + 1);
82 if (res != NULL) {
83 str_ncpy(res, len + 1, *buf, len);
84 *buf += len;
85 }
86 }
87 return res;
88}
89
90/**
91 * Read match ids and associated match scores from a string.
92 *
93 * Each match score in the string is followed by its match id.
94 * The match ids and match scores are separated by whitespaces.
95 * Neither match ids nor match scores can contain whitespaces.
96 *
97 * @param buf the string from which the match ids are read.
98 * @param ids the list of match ids into which the match ids and scores are added.
99 *
100 * @return true if at least one match id and associated match score was successfully read, false otherwise.
101 */
102bool parse_match_ids(const char *buf, match_id_list_t *ids)
103{
104 int score = 0;
105 char *id = NULL;
106 int ids_read = 0;
107
108 while (true) {
109 // skip spaces
110 if (!skip_spaces(&buf)) {
111 break;
112 }
113 // read score
114 score = strtoul(buf, &buf, 10);
115
116 // skip spaces
117 if (!skip_spaces(&buf)) {
118 break;
119 }
120
121 // read id
122 if (NULL == (id = read_match_id(&buf))) {
123 break;
124 }
125
126 // create new match_id structure
127 match_id_t *mid = create_match_id();
128 mid->id = id;
129 mid->score = score;
130
131 /// add it to the list
132 add_match_id(ids, mid);
133
134 ids_read++;
135 }
136
137 return ids_read > 0;
138}
139
140/**
141 * Read match ids and associated match scores from a file.
142 *
143 * Each match score in the file is followed by its match id.
144 * The match ids and match scores are separated by whitespaces.
145 * Neither match ids nor match scores can contain whitespaces.
146 *
147 * @param buf the path to the file from which the match ids are read.
148 * @param ids the list of match ids into which the match ids and scores are added.
149 *
150 * @return true if at least one match id and associated match score was successfully read, false otherwise.
151 */
152bool read_match_ids(const char *conf_path, match_id_list_t *ids)
153{
154 printf(NAME ": read_match_ids conf_path = %s.\n", conf_path);
155
156 bool suc = false;
157 char *buf = NULL;
158 bool opened = false;
159 int fd;
160 off_t len = 0;
161
162 fd = open(conf_path, O_RDONLY);
163 if (fd < 0) {
164 printf(NAME ": unable to open %s\n", conf_path);
165 goto cleanup;
166 }
167 opened = true;
168
169 len = lseek(fd, 0, SEEK_END);
170 lseek(fd, 0, SEEK_SET);
171 if (len == 0) {
172 printf(NAME ": configuration file '%s' is empty.\n", conf_path);
173 goto cleanup;
174 }
175
176 buf = malloc(len + 1);
177 if (buf == NULL) {
178 printf(NAME ": memory allocation failed when parsing file '%s'.\n", conf_path);
179 goto cleanup;
180 }
181
182 if (0 >= read(fd, buf, len)) {
183 printf(NAME ": unable to read file '%s'.\n", conf_path);
184 goto cleanup;
185 }
186 buf[len] = 0;
187
188 suc = parse_match_ids(buf, ids);
189
190cleanup:
191
192 free(buf);
193
194 if(opened) {
195 close(fd);
196 }
197
198 return suc;
199}
200
201/**
202 * Get information about a driver.
203 *
204 * Each driver has its own directory in the base directory.
205 * The name of the driver's directory is the same as the name of the driver.
206 * The driver's directory contains driver's binary (named as the driver without extension)
207 * and the configuration file with match ids for device-to-driver matching
208 * (named as the driver with a special extension).
209 *
210 * This function searches for the driver's directory and containing configuration files.
211 * If all the files needed are found, they are parsed and
212 * the information about the driver is stored to the driver's structure.
213 *
214 * @param base_path the base directory, in which we look for driver's subdirectory.
215 * @param name the name of the driver.
216 * @param drv the driver structure to fill information in.
217 *
218 * @return true on success, false otherwise.
219 */
220bool get_driver_info(const char *base_path, const char *name, driver_t *drv)
221{
222 printf(NAME ": get_driver_info base_path = %s, name = %s.\n", base_path, name);
223
224 assert(base_path != NULL && name != NULL && drv != NULL);
225
226 bool suc = false;
227 char *match_path = NULL;
228 size_t name_size = 0;
229
230 // read the list of match ids from the driver's configuration file
231 if (NULL == (match_path = get_abs_path(base_path, name, MATCH_EXT))) {
232 goto cleanup;
233 }
234
235 if (!read_match_ids(match_path, &drv->match_ids)) {
236 goto cleanup;
237 }
238
239 // allocate and fill driver's name
240 name_size = str_size(name)+1;
241 drv->name = malloc(name_size);
242 if (!drv->name) {
243 goto cleanup;
244 }
245 str_cpy(drv->name, name_size, name);
246
247 // initialize path with driver's binary
248 if (NULL == (drv->binary_path = get_abs_path(base_path, name, ""))) {
249 goto cleanup;
250 }
251
252 // check whether the driver's binary exists
253 struct stat s;
254 if (stat(drv->binary_path, &s) == ENOENT) {
255 printf(NAME ": driver not found at path %s.", drv->binary_path);
256 goto cleanup;
257 }
258
259 suc = true;
260
261cleanup:
262
263 if (!suc) {
264 free(drv->binary_path);
265 free(drv->name);
266 // set the driver structure to the default state
267 init_driver(drv);
268 }
269
270 free(match_path);
271
272 return suc;
273}
274
275/** Lookup drivers in the directory.
276 *
277 * @param drivers_list the list of available drivers.
278 * @param dir_path the path to the directory where we search for drivers.
279 *
280 * @return number of drivers which were found.
281 */
282int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path)
283{
284 printf(NAME ": lookup_available_drivers, dir = %s \n", dir_path);
285
286 int drv_cnt = 0;
287 DIR *dir = NULL;
288 struct dirent *diren;
289
290 dir = opendir(dir_path);
291
292 if (dir != NULL) {
293 driver_t *drv = create_driver();
294 while ((diren = readdir(dir))) {
295 if (get_driver_info(dir_path, diren->d_name, drv)) {
296 add_driver(drivers_list, drv);
297 drv_cnt++;
298 drv = create_driver();
299 }
300 }
301 delete_driver(drv);
302 closedir(dir);
303 }
304
305 return drv_cnt;
306}
307
308/** Create root device node in the device tree.
309 *
310 * @param tree the device tree.
311 * @return true on success, false otherwise.
312 */
313bool create_root_node(dev_tree_t *tree)
314{
315 printf(NAME ": create_root_node\n");
316 node_t *node = create_dev_node();
317 if (node) {
318 insert_dev_node(tree, node, "", NULL);
319 match_id_t *id = create_match_id();
320 id->id = "root";
321 id->score = 100;
322 add_match_id(&node->match_ids, id);
323 tree->root_node = node;
324 }
325 return node != NULL;
326}
327
328/** Lookup the best matching driver for the specified device in the list of drivers.
329 *
330 * A match between a device and a driver is found
331 * if one of the driver's match ids match one of the device's match ids.
332 * The score of the match is the product of the driver's and device's score associated with the matching id.
333 * The best matching driver for a device is the driver
334 * with the highest score of the match between the device and the driver.
335 *
336 * @param drivers_list the list of drivers, where we look for the driver suitable for handling the device.
337 * @param node the device node structure of the device.
338 *
339 * @return the best matching driver or NULL if no matching driver is found.
340 */
341driver_t * find_best_match_driver(driver_list_t *drivers_list, node_t *node)
342{
343 //printf(NAME ": find_best_match_driver for device '%s' \n", node->pathname);
344 driver_t *best_drv = NULL, *drv = NULL;
345 int best_score = 0, score = 0;
346
347 fibril_mutex_lock(&drivers_list->drivers_mutex);
348
349 link_t *link = drivers_list->drivers.next;
350 while (link != &drivers_list->drivers) {
351 drv = list_get_instance(link, driver_t, drivers);
352 score = get_match_score(drv, node);
353 if (score > best_score) {
354 best_score = score;
355 best_drv = drv;
356 }
357 link = link->next;
358 }
359
360 fibril_mutex_unlock(&drivers_list->drivers_mutex);
361
362 return best_drv;
363}
364
365/**
366 * Assign a driver to a device.
367 *
368 * @param node the device's node in the device tree.
369 * @param drv the driver.
370 */
371void attach_driver(node_t *node, driver_t *drv)
372{
373 printf(NAME ": attach_driver %s to device %s\n", drv->name, node->pathname);
374
375 fibril_mutex_lock(&drv->driver_mutex);
376
377 node->drv = drv;
378 list_append(&node->driver_devices, &drv->devices);
379
380 fibril_mutex_unlock(&drv->driver_mutex);
381}
382
383/** Start a driver.
384 *
385 * The driver's mutex is assumed to be locked.
386 *
387 * @param drv the driver's structure.
388 * @return true if the driver's task is successfully spawned, false otherwise.
389 */
390bool start_driver(driver_t *drv)
391{
392 printf(NAME ": start_driver '%s'\n", drv->name);
393
394 char *argv[2];
395
396 argv[0] = drv->name;
397 argv[1] = NULL;
398
399 if (!task_spawn(drv->binary_path, argv)) {
400 printf(NAME ": error spawning %s\n", drv->name);
401 return false;
402 }
403
404 drv->state = DRIVER_STARTING;
405 return true;
406}
407
408/** Find device driver in the list of device drivers.
409 *
410 * @param drv_list the list of device drivers.
411 * @param drv_name the name of the device driver which is searched.
412 * @return the device driver of the specified name, if it is in the list, NULL otherwise.
413 */
414driver_t * find_driver(driver_list_t *drv_list, const char *drv_name)
415{
416 driver_t *res = NULL;
417
418 fibril_mutex_lock(&drv_list->drivers_mutex);
419
420 driver_t *drv = NULL;
421 link_t *link = drv_list->drivers.next;
422 while (link != &drv_list->drivers) {
423 drv = list_get_instance(link, driver_t, drivers);
424 if (0 == str_cmp(drv->name, drv_name)) {
425 res = drv;
426 break;
427 }
428 link = link->next;
429 }
430
431 fibril_mutex_unlock(&drv_list->drivers_mutex);
432
433 return res;
434}
435
436/** Remember the driver's phone.
437 * @param driver the driver.
438 * @param phone the phone to the driver.
439 */
440void set_driver_phone(driver_t *driver, ipcarg_t phone)
441{
442 fibril_mutex_lock(&driver->driver_mutex);
443 assert(DRIVER_STARTING == driver->state);
444 driver->phone = phone;
445 fibril_mutex_unlock(&driver->driver_mutex);
446}
447
448/**
449 * Notify driver about the devices to which it was assigned.
450 *
451 * The driver's mutex must be locked.
452 *
453 * @param driver the driver to which the devices are passed.
454 */
455static void pass_devices_to_driver(driver_t *driver)
456{
457 printf(NAME ": pass_devices_to_driver\n");
458 node_t *dev;
459 link_t *link;
460
461 int phone = ipc_connect_me_to(driver->phone, DRIVER_DEVMAN, 0, 0);
462
463 if (0 < phone) {
464
465 link = driver->devices.next;
466 while (link != &driver->devices) {
467 dev = list_get_instance(link, node_t, driver_devices);
468 add_device(phone, driver, dev);
469 link = link->next;
470 }
471
472 ipc_hangup(phone);
473 }
474}
475
476/** Finish the initialization of a driver after it has succesfully started
477 * and after it has registered itself by the device manager.
478 *
479 * Pass devices formerly matched to the driver to the driver and remember the driver is running and fully functional now.
480 *
481 * @param driver the driver which registered itself as running by the device manager.
482 */
483void initialize_running_driver(driver_t *driver)
484{
485 printf(NAME ": initialize_running_driver\n");
486 fibril_mutex_lock(&driver->driver_mutex);
487
488 // pass devices which have been already assigned to the driver to the driver
489 pass_devices_to_driver(driver);
490
491 // change driver's state to running
492 driver->state = DRIVER_RUNNING;
493
494 fibril_mutex_unlock(&driver->driver_mutex);
495}
496
497/** Pass a device to running driver.
498 *
499 * @param drv the driver's structure.
500 * @param node the device's node in the device tree.
501 */
502void add_device(int phone, driver_t *drv, node_t *node)
503{
504 printf(NAME ": add_device\n");
505
506 ipcarg_t rc;
507 ipc_call_t answer;
508
509 // send the device to the driver
510 aid_t req = async_send_1(phone, DRIVER_ADD_DEVICE, node->handle, &answer);
511
512 // send the device's name to the driver
513 rc = async_data_write_start(phone, node->name, str_size(node->name) + 1);
514 if (rc != EOK) {
515 // TODO handle error
516 }
517
518 // wait for answer from the driver
519 async_wait_for(req, &rc);
520 switch(rc) {
521 // TODO inspect return value to find out whether the device was successfully probed and added
522 case EOK:
523 case ENOENT:
524
525 break;
526
527 }
528
529 return;
530}
531
532/**
533 * Find suitable driver for a device and assign the driver to it.
534 *
535 * @param node the device node of the device in the device tree.
536 * @param drivers_list the list of available drivers.
537 *
538 * @return true if the suitable driver is found and successfully assigned to the device, false otherwise.
539 */
540bool assign_driver(node_t *node, driver_list_t *drivers_list)
541{
542 //printf(NAME ": assign_driver\n");
543
544 // find the driver which is the most suitable for handling this device
545 driver_t *drv = find_best_match_driver(drivers_list, node);
546 if (NULL == drv) {
547 printf(NAME ": no driver found for device '%s'.\n", node->pathname);
548 return false;
549 }
550
551 // attach the driver to the device
552 attach_driver(node, drv);
553
554 if (DRIVER_NOT_STARTED == drv->state) {
555 // start driver
556 start_driver(drv);
557 }
558
559 if (DRIVER_RUNNING == drv->state) {
560 // notify driver about new device
561 int phone = ipc_connect_me_to(drv->phone, DRIVER_DEVMAN, 0, 0);
562 if (phone > 0) {
563 add_device(phone, drv, node);
564 ipc_hangup(phone);
565 }
566 }
567
568 return true;
569}
570
571/**
572 * Initialize the device tree.
573 *
574 * Create root device node of the tree and assign driver to it.
575 *
576 * @param tree the device tree.
577 * @param the list of available drivers.
578 * @return true on success, false otherwise.
579 */
580bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list)
581{
582 printf(NAME ": init_device_tree.\n");
583
584 memset(tree->node_map, 0, MAX_DEV * sizeof(node_t *));
585
586 atomic_set(&tree->current_handle, 0);
587
588 // create root node and add it to the device tree
589 if (!create_root_node(tree)) {
590 return false;
591 }
592
593 // find suitable driver and start it
594 return assign_driver(tree->root_node, drivers_list);
595}
596
597/** Create and set device's full path in device tree.
598 *
599 * @param node the device's device node.
600 * @param parent the parent device node.
601 * @return true on success, false otherwise (insufficient resources etc.).
602 */
603static bool set_dev_path(node_t *node, node_t *parent)
604{
605 assert(NULL != node->name);
606
607 size_t pathsize = (str_size(node->name) + 1);
608 if (NULL != parent) {
609 pathsize += str_size(parent->pathname) + 1;
610 }
611
612 if (NULL == (node->pathname = (char *)malloc(pathsize))) {
613 printf(NAME ": failed to allocate device path.\n");
614 return false;
615 }
616
617 if (NULL != parent) {
618 str_cpy(node->pathname, pathsize, parent->pathname);
619 str_append(node->pathname, pathsize, "/");
620 str_append(node->pathname, pathsize, node->name);
621 } else {
622 str_cpy(node->pathname, pathsize, node->name);
623 }
624
625 return true;
626}
627
628/** Insert new device into device tree.
629 *
630 * @param tree the device tree.
631 * @param node the newly added device node.
632 * @param dev_name the name of the newly added device.
633 * @param parent the parent device node.
634 * @return true on success, false otherwise (insufficient resources etc.).
635 */
636bool insert_dev_node(dev_tree_t *tree, node_t *node, char *dev_name, node_t *parent)
637{
638 // printf(NAME ": insert_dev_node\n");
639
640 assert(NULL != node && NULL != tree && NULL != dev_name);
641
642 node->name = dev_name;
643 if (!set_dev_path(node, parent)) {
644 return false;
645 }
646
647 // add the node to the handle-to-node map
648 node->handle = atomic_postinc(&tree->current_handle);
649 if (node->handle >= MAX_DEV) {
650 printf(NAME ": failed to add device to device tree, because maximum number of devices was reached.\n");
651 free(node->pathname);
652 node->pathname = NULL;
653 atomic_postdec(&tree->current_handle);
654 return false;
655 }
656 tree->node_map[node->handle] = node;
657
658 // add the node to the list of its parent's children
659 node->parent = parent;
660 if (NULL != parent) {
661 fibril_mutex_lock(&parent->children_mutex);
662 list_append(&node->sibling, &parent->children);
663 fibril_mutex_unlock(&parent->children_mutex);
664 }
665 return true;
666}
667
668/**
669 * Find device node with a specified path in the device tree.
670 *
671 * @param path the path of the device node in the device tree.
672 * @param tree the device tree.
673 *
674 * @return the device node if it is present in the tree, NULL otherwise.
675 */
676node_t * find_dev_node_by_path(dev_tree_t *tree, char *path)
677{
678 node_t *dev = tree->root_node;
679 char *rel_path = path;
680 char *next_path_elem = NULL;
681 size_t elem_size = 0;
682 bool cont = '/' == rel_path[0];
683
684 while (cont && NULL != dev) {
685 next_path_elem = get_path_elem_end(rel_path+1);
686 if ('/' == next_path_elem[0]) {
687 cont = true;
688 next_path_elem[0] = 0;
689 } else {
690 cont = false;
691 }
692
693 dev = find_node_child(dev, rel_path);
694
695 if (cont) {
696 next_path_elem[0] = '/';
697 }
698 rel_path = next_path_elem;
699 }
700
701 return dev;
702}
703
704/**
705 * Find child device node with a specified name.
706 *
707 * @param parent the parent device node.
708 * @param name the name of the child device node.
709 *
710 * @return the child device node.
711 */
712node_t *find_node_child(node_t *parent, const char *name)
713{
714 node_t *dev;
715 link_t *link;
716
717 fibril_mutex_lock(&parent->children_mutex);
718 link = parent->children.next;
719
720 while (link != &parent->children) {
721 dev = list_get_instance(link, node_t, sibling);
722
723 if (0 == str_cmp(name, dev->name)) {
724 fibril_mutex_unlock(&parent->children_mutex);
725 return dev;
726 }
727
728 link = link->next;
729 }
730
731 fibril_mutex_unlock(&parent->children_mutex);
732 return NULL;
733}
734
735/** @}
736 */
Note: See TracBrowser for help on using the repository browser.