source: mainline/uspace/srv/devman/devman.c@ 92413de

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

fixed a bug (devman)

  • Property mode set to 100644
File size: 13.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
37#include "devman.h"
38#include "util.h"
39
40/** Allocate and initialize a new driver structure.
41 *
42 * @return driver structure.
43 */
44driver_t * create_driver()
45{
46 driver_t *res = malloc(sizeof(driver_t));
47 if(res != NULL) {
48 init_driver(res);
49 }
50 return res;
51}
52
53/** Add a driver to the list of drivers.
54 *
55 * @param drivers_list the list of drivers.
56 * @param drv the driver's structure.
57 */
58void add_driver(driver_list_t *drivers_list, driver_t *drv)
59{
60 fibril_mutex_lock(&drivers_list->drivers_mutex);
61 list_prepend(&drv->drivers, &drivers_list->drivers);
62 fibril_mutex_unlock(&drivers_list->drivers_mutex);
63
64 printf(NAME": the '%s' driver was added to the list of available drivers.\n", drv->name);
65}
66
67/** Read match id at the specified position of a string and set
68 * the position in the string to the first character following the id.
69 *
70 * @param buf the position in the input string.
71 *
72 * @return the match id.
73 */
74char * read_match_id(const char **buf)
75{
76 char *res = NULL;
77 size_t len = get_nonspace_len(*buf);
78 if (len > 0) {
79 res = malloc(len + 1);
80 if (res != NULL) {
81 str_ncpy(res, len + 1, *buf, len);
82 *buf += len;
83 }
84 }
85 return res;
86}
87
88/**
89 * Read match ids and associated match scores from a string.
90 *
91 * Each match score in the string is followed by its match id.
92 * The match ids and match scores are separated by whitespaces.
93 * Neither match ids nor match scores can contain whitespaces.
94 *
95 * @param buf the string from which the match ids are read.
96 * @param ids the list of match ids into which the match ids and scores are added.
97 *
98 * @return true if at least one match id and associated match score was successfully read, false otherwise.
99 */
100bool parse_match_ids(const char *buf, match_id_list_t *ids)
101{
102 int score = 0;
103 char *id = NULL;
104 int ids_read = 0;
105
106 while (true) {
107 // skip spaces
108 if (!skip_spaces(&buf)) {
109 break;
110 }
111 // read score
112 score = strtoul(buf, &buf, 10);
113
114 // skip spaces
115 if (!skip_spaces(&buf)) {
116 break;
117 }
118
119 // read id
120 if (NULL == (id = read_match_id(&buf))) {
121 break;
122 }
123
124 // create new match_id structure
125 match_id_t *mid = create_match_id();
126 mid->id = id;
127 mid->score = score;
128
129 /// add it to the list
130 add_match_id(ids, mid);
131
132 ids_read++;
133 }
134
135 return ids_read > 0;
136}
137
138/**
139 * Read match ids and associated match scores from a file.
140 *
141 * Each match score in the file is followed by its match id.
142 * The match ids and match scores are separated by whitespaces.
143 * Neither match ids nor match scores can contain whitespaces.
144 *
145 * @param buf the path to the file from which the match ids are read.
146 * @param ids the list of match ids into which the match ids and scores are added.
147 *
148 * @return true if at least one match id and associated match score was successfully read, false otherwise.
149 */
150bool read_match_ids(const char *conf_path, match_id_list_t *ids)
151{
152 printf(NAME ": read_match_ids conf_path = %s.\n", conf_path);
153
154 bool suc = false;
155 char *buf = NULL;
156 bool opened = false;
157 int fd;
158 off_t len = 0;
159
160 fd = open(conf_path, O_RDONLY);
161 if (fd < 0) {
162 printf(NAME ": unable to open %s\n", conf_path);
163 goto cleanup;
164 }
165 opened = true;
166
167 len = lseek(fd, 0, SEEK_END);
168 lseek(fd, 0, SEEK_SET);
169 if (len == 0) {
170 printf(NAME ": configuration file '%s' is empty.\n", conf_path);
171 goto cleanup;
172 }
173
174 buf = malloc(len + 1);
175 if (buf == NULL) {
176 printf(NAME ": memory allocation failed when parsing file '%s'.\n", conf_path);
177 goto cleanup;
178 }
179
180 if (0 >= read(fd, buf, len)) {
181 printf(NAME ": unable to read file '%s'.\n", conf_path);
182 goto cleanup;
183 }
184 buf[len] = 0;
185
186 suc = parse_match_ids(buf, ids);
187
188cleanup:
189
190 free(buf);
191
192 if(opened) {
193 close(fd);
194 }
195
196 return suc;
197}
198
199/**
200 * Get information about a driver.
201 *
202 * Each driver has its own directory in the base directory.
203 * The name of the driver's directory is the same as the name of the driver.
204 * The driver's directory contains driver's binary (named as the driver without extension)
205 * and the configuration file with match ids for device-to-driver matching
206 * (named as the driver with a special extension).
207 *
208 * This function searches for the driver's directory and containing configuration files.
209 * If all the files needed are found, they are parsed and
210 * the information about the driver is stored to the driver's structure.
211 *
212 * @param base_path the base directory, in which we look for driver's subdirectory.
213 * @param name the name of the driver.
214 * @param drv the driver structure to fill information in.
215 *
216 * @return true on success, false otherwise.
217 */
218bool get_driver_info(const char *base_path, const char *name, driver_t *drv)
219{
220 printf(NAME ": get_driver_info base_path = %s, name = %s.\n", base_path, name);
221
222 assert(base_path != NULL && name != NULL && drv != NULL);
223
224 bool suc = false;
225 char *match_path = NULL;
226 size_t name_size = 0;
227
228 // read the list of match ids from the driver's configuration file
229 if (NULL == (match_path = get_abs_path(base_path, name, MATCH_EXT))) {
230 goto cleanup;
231 }
232
233 printf(NAME ": get_driver_info - path to match id list = %s.\n", match_path);
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 \n");
285
286 int drv_cnt = 0;
287 DIR *dir = NULL;
288 struct dirent *diren;
289
290 dir = opendir(dir_path);
291 printf(NAME ": lookup_available_drivers has opened directory %s for driver search.\n", dir_path);
292
293 if (dir != NULL) {
294 driver_t *drv = create_driver();
295 printf(NAME ": lookup_available_drivers has created driver structure.\n");
296 while ((diren = readdir(dir))) {
297 if (get_driver_info(dir_path, diren->d_name, drv)) {
298 add_driver(drivers_list, drv);
299 drv_cnt++;
300 drv = create_driver();
301 }
302 }
303 delete_driver(drv);
304 closedir(dir);
305 }
306
307 return drv_cnt;
308}
309
310/** Create root device node of the device tree.
311 *
312 * @return root device node.
313 */
314node_t * create_root_node()
315{
316 printf(NAME ": create_root_node\n");
317 node_t *node = create_dev_node();
318 if (node) {
319 init_dev_node(node, NULL);
320 match_id_t *id = create_match_id();
321 id->id = "root";
322 id->score = 100;
323 add_match_id(&node->match_ids, id);
324 }
325 return node;
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\n");
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 fibril_mutex_lock(&drv->driver_mutex);
374
375 node->drv = drv;
376 list_append(&node->driver_devices, &drv->devices);
377
378 fibril_mutex_unlock(&drv->driver_mutex);
379}
380
381/** Start a driver.
382 *
383 * The driver's mutex is assumed to be locked.
384 *
385 * @param drv the driver's structure.
386 * @return true if the driver's task is successfully spawned, false otherwise.
387 */
388bool start_driver(driver_t *drv)
389{
390 printf(NAME ": start_driver\n");
391
392 char *argv[2];
393
394 printf(NAME ": spawning driver %s\n", drv->name);
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
408driver_t * find_driver(driver_list_t *drv_list, const char *drv_name)
409{
410 driver_t *res = NULL;
411
412 fibril_mutex_lock(&drv_list->drivers_mutex);
413
414 driver_t *drv = NULL;
415 link_t *link = drv_list->drivers.next;
416 while (link != &drv_list->drivers) {
417 drv = list_get_instance(link, driver_t, drivers);
418 if (0 == str_cmp(drv->name, drv_name)) {
419 res = drv;
420 break;
421 }
422 link = link->next;
423 }
424
425 fibril_mutex_unlock(&drv_list->drivers_mutex);
426
427 return res;
428}
429
430/** Pass a device to running driver.
431 *
432 * @param drv the driver's structure.
433 * @param node the device's node in the device tree.
434 *
435 * @return true on success, false otherwise.
436 */
437bool add_device(driver_t *drv, node_t *node)
438{
439 printf(NAME ": add_device\n");
440
441 // TODO
442
443 // pass a new device to the running driver, which was previously assigned to it
444 // send the phone of the parent's driver and device's handle within the parent's driver to the driver
445 // let the driver to probe the device and specify whether the device is actually present
446 // if the device is present, remember its handle within the driver
447
448 return true;
449}
450
451/**
452 * Find suitable driver for a device and assign the driver to it.
453 *
454 * @param node the device node of the device in the device tree.
455 * @param drivers_list the list of available drivers.
456 *
457 * @return true if the suitable driver is found and successfully assigned to the device, false otherwise.
458 */
459bool assign_driver(node_t *node, driver_list_t *drivers_list)
460{
461 printf(NAME ": assign_driver\n");
462
463 // find the driver which is the most suitable for handling this device
464 driver_t *drv = find_best_match_driver(drivers_list, node);
465 if (NULL == drv) {
466 printf(NAME ": no driver found for device.\n");
467 return false;
468 }
469
470 // attach the driver to the device
471 attach_driver(node, drv);
472
473 if (DRIVER_NOT_STARTED == drv->state) {
474 // start driver
475 start_driver(drv);
476 }
477
478 if (DRIVER_RUNNING == drv->state) {
479 // notify driver about new device
480 add_device(drv, node);
481 }
482
483 return true;
484}
485
486/**
487 * Initialize the device tree.
488 *
489 * Create root device node of the tree and assign driver to it.
490 *
491 * @param tree the device tree.
492 * @param the list of available drivers.
493 * @return true on success, false otherwise.
494 */
495bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list)
496{
497 printf(NAME ": init_device_tree.\n");
498
499 // create root node and add it to the device tree
500 if (NULL == (tree->root_node = create_root_node())) {
501 return false;
502 }
503
504 // find suitable driver and start it
505 return assign_driver(tree->root_node, drivers_list);
506}
507
Note: See TracBrowser for help on using the repository browser.