source: mainline/uspace/srv/devman/devman.c@ 729fa2d6

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

parts of root device driver

  • 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 printf(NAME ": create_driver\n");
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 printf(NAME ": get_driver_info - path to match id list = %s.\n", match_path);
236
237 if (!read_match_ids(match_path, &drv->match_ids)) {
238 goto cleanup;
239 }
240
241 // allocate and fill driver's name
242 name_size = str_size(name)+1;
243 drv->name = malloc(name_size);
244 if (!drv->name) {
245 goto cleanup;
246 }
247 str_cpy(drv->name, name_size, name);
248
249 // initialize path with driver's binary
250 if (NULL == (drv->binary_path = get_abs_path(base_path, name, ""))) {
251 goto cleanup;
252 }
253
254 // check whether the driver's binary exists
255 struct stat s;
256 if (stat(drv->binary_path, &s) == ENOENT) {
257 printf(NAME ": driver not found at path %s.", drv->binary_path);
258 goto cleanup;
259 }
260
261 suc = true;
262
263cleanup:
264
265 if (!suc) {
266 free(drv->binary_path);
267 free(drv->name);
268 // set the driver structure to the default state
269 init_driver(drv);
270 }
271
272 free(match_path);
273
274 return suc;
275}
276
277/** Lookup drivers in the directory.
278 *
279 * @param drivers_list the list of available drivers.
280 * @param dir_path the path to the directory where we search for drivers.
281 *
282 * @return number of drivers which were found.
283 */
284int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path)
285{
286 printf(NAME ": lookup_available_drivers \n");
287
288 int drv_cnt = 0;
289 DIR *dir = NULL;
290 struct dirent *diren;
291
292 dir = opendir(dir_path);
293 printf(NAME ": lookup_available_drivers has opened directory %s for driver search.\n", dir_path);
294
295 if (dir != NULL) {
296 driver_t *drv = create_driver();
297 printf(NAME ": lookup_available_drivers has created driver structure.\n");
298 while ((diren = readdir(dir))) {
299 if (get_driver_info(dir_path, diren->d_name, drv)) {
300 add_driver(drivers_list, drv);
301 drv_cnt++;
302 drv = create_driver();
303 }
304 }
305 delete_driver(drv);
306 closedir(dir);
307 }
308
309 return drv_cnt;
310}
311
312/** Create root device node of the device tree.
313 *
314 * @return root device node.
315 */
316node_t * create_root_node()
317{
318 printf(NAME ": create_root_node\n");
319 node_t *node = create_dev_node();
320 if (node) {
321 init_dev_node(node, NULL);
322 match_id_t *id = create_match_id();
323 id->id = "root";
324 id->score = 100;
325 add_match_id(&node->match_ids, id);
326 }
327 return node;
328}
329
330/** Lookup the best matching driver for the specified device in the list of drivers.
331 *
332 * A match between a device and a driver is found
333 * if one of the driver's match ids match one of the device's match ids.
334 * The score of the match is the product of the driver's and device's score associated with the matching id.
335 * The best matching driver for a device is the driver
336 * with the highest score of the match between the device and the driver.
337 *
338 * @param drivers_list the list of drivers, where we look for the driver suitable for handling the device.
339 * @param node the device node structure of the device.
340 *
341 * @return the best matching driver or NULL if no matching driver is found.
342 */
343driver_t * find_best_match_driver(driver_list_t *drivers_list, node_t *node)
344{
345 printf(NAME ": find_best_match_driver\n");
346 driver_t *best_drv = NULL, *drv = NULL;
347 int best_score = 0, score = 0;
348
349 fibril_mutex_lock(&drivers_list->drivers_mutex);
350
351 link_t *link = drivers_list->drivers.next;
352 while (link != &drivers_list->drivers) {
353 drv = list_get_instance(link, driver_t, drivers);
354 score = get_match_score(drv, node);
355 if (score > best_score) {
356 best_score = score;
357 best_drv = drv;
358 }
359 link = link->next;
360 }
361
362 fibril_mutex_unlock(&drivers_list->drivers_mutex);
363
364 return best_drv;
365}
366
367/**
368 * Assign a driver to a device.
369 *
370 * @param node the device's node in the device tree.
371 * @param drv the driver.
372 */
373void attach_driver(node_t *node, driver_t *drv)
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\n");
393
394 char *argv[2];
395
396 printf(NAME ": spawning driver %s\n", drv->name);
397
398 argv[0] = drv->name;
399 argv[1] = NULL;
400
401 if (!task_spawn(drv->binary_path, argv)) {
402 printf(NAME ": error spawning %s\n", drv->name);
403 return false;
404 }
405
406 drv->state = DRIVER_STARTING;
407 return true;
408}
409
410driver_t * find_driver(driver_list_t *drv_list, const char *drv_name)
411{
412 driver_t *res = NULL;
413
414 fibril_mutex_lock(&drv_list->drivers_mutex);
415
416 driver_t *drv = NULL;
417 link_t *link = drv_list->drivers.next;
418 while (link != &drv_list->drivers) {
419 drv = list_get_instance(link, driver_t, drivers);
420 if (0 == str_cmp(drv->name, drv_name)) {
421 res = drv;
422 break;
423 }
424 link = link->next;
425 }
426
427 fibril_mutex_unlock(&drv_list->drivers_mutex);
428
429 return res;
430}
431
432/** Pass a device to running driver.
433 *
434 * @param drv the driver's structure.
435 * @param node the device's node in the device tree.
436 *
437 * @return true on success, false otherwise.
438 */
439bool add_device(driver_t *drv, node_t *node)
440{
441 printf(NAME ": add_device\n");
442
443 // TODO
444
445 // pass a new device to the running driver, which was previously assigned to it
446 // send the phone of the parent's driver and device's handle within the parent's driver to the driver
447 // let the driver to probe the device and specify whether the device is actually present
448 // if the device is present, remember its handle within the driver
449
450 return true;
451}
452
453/**
454 * Find suitable driver for a device and assign the driver to it.
455 *
456 * @param node the device node of the device in the device tree.
457 * @param drivers_list the list of available drivers.
458 *
459 * @return true if the suitable driver is found and successfully assigned to the device, false otherwise.
460 */
461bool assign_driver(node_t *node, driver_list_t *drivers_list)
462{
463 printf(NAME ": assign_driver\n");
464
465 // find the driver which is the most suitable for handling this device
466 driver_t *drv = find_best_match_driver(drivers_list, node);
467 if (NULL == drv) {
468 printf(NAME ": no driver found for device.\n");
469 return false;
470 }
471
472 // attach the driver to the device
473 attach_driver(node, drv);
474
475 if (DRIVER_NOT_STARTED == drv->state) {
476 // start driver
477 start_driver(drv);
478 }
479
480 if (DRIVER_RUNNING == drv->state) {
481 // notify driver about new device
482 add_device(drv, node);
483 }
484
485 return true;
486}
487
488/**
489 * Initialize the device tree.
490 *
491 * Create root device node of the tree and assign driver to it.
492 *
493 * @param tree the device tree.
494 * @param the list of available drivers.
495 * @return true on success, false otherwise.
496 */
497bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list)
498{
499 printf(NAME ": init_device_tree.\n");
500
501 // create root node and add it to the device tree
502 if (NULL == (tree->root_node = create_root_node())) {
503 return false;
504 }
505
506 // find suitable driver and start it
507 return assign_driver(tree->root_node, drivers_list);
508}
509
Note: See TracBrowser for help on using the repository browser.