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