source: mainline/uspace/srv/devman/driver.c@ a60e90b

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

Move rest of functionality from devman.c to other modules.

  • Property mode set to 100644
File size: 17.1 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 <dirent.h>
34#include <errno.h>
35#include <fcntl.h>
36#include <sys/stat.h>
37#include <io/log.h>
38#include <ipc/driver.h>
39#include <loc.h>
40#include <str_error.h>
41#include <stdio.h>
42
43#include "dev.h"
44#include "devman.h"
45#include "driver.h"
46#include "match.h"
47
48/**
49 * Initialize the list of device driver's.
50 *
51 * @param drv_list the list of device driver's.
52 *
53 */
54void init_driver_list(driver_list_t *drv_list)
55{
56 assert(drv_list != NULL);
57
58 list_initialize(&drv_list->drivers);
59 fibril_mutex_initialize(&drv_list->drivers_mutex);
60}
61
62/** Allocate and initialize a new driver structure.
63 *
64 * @return Driver structure.
65 */
66driver_t *create_driver(void)
67{
68 driver_t *res = malloc(sizeof(driver_t));
69 if (res != NULL)
70 init_driver(res);
71 return res;
72}
73
74/** Add a driver to the list of drivers.
75 *
76 * @param drivers_list List of drivers.
77 * @param drv Driver structure.
78 */
79void add_driver(driver_list_t *drivers_list, driver_t *drv)
80{
81 fibril_mutex_lock(&drivers_list->drivers_mutex);
82 list_prepend(&drv->drivers, &drivers_list->drivers);
83 fibril_mutex_unlock(&drivers_list->drivers_mutex);
84
85 log_msg(LOG_DEFAULT, LVL_NOTE, "Driver `%s' was added to the list of available "
86 "drivers.", drv->name);
87}
88
89/**
90 * Get information about a driver.
91 *
92 * Each driver has its own directory in the base directory.
93 * The name of the driver's directory is the same as the name of the driver.
94 * The driver's directory contains driver's binary (named as the driver without
95 * extension) and the configuration file with match ids for device-to-driver
96 * matching (named as the driver with a special extension).
97 *
98 * This function searches for the driver's directory and containing
99 * configuration files. If all the files needed are found, they are parsed and
100 * the information about the driver is stored in the driver's structure.
101 *
102 * @param base_path The base directory, in which we look for driver's
103 * subdirectory.
104 * @param name The name of the driver.
105 * @param drv The driver structure to fill information in.
106 *
107 * @return True on success, false otherwise.
108 */
109bool get_driver_info(const char *base_path, const char *name, driver_t *drv)
110{
111 log_msg(LOG_DEFAULT, LVL_DEBUG, "get_driver_info(base_path=\"%s\", name=\"%s\")",
112 base_path, name);
113
114 assert(base_path != NULL && name != NULL && drv != NULL);
115
116 bool suc = false;
117 char *match_path = NULL;
118 size_t name_size = 0;
119
120 /* Read the list of match ids from the driver's configuration file. */
121 match_path = get_abs_path(base_path, name, MATCH_EXT);
122 if (match_path == NULL)
123 goto cleanup;
124
125 if (!read_match_ids(match_path, &drv->match_ids))
126 goto cleanup;
127
128 /* Allocate and fill driver's name. */
129 name_size = str_size(name) + 1;
130 drv->name = malloc(name_size);
131 if (drv->name == NULL)
132 goto cleanup;
133 str_cpy(drv->name, name_size, name);
134
135 /* Initialize path with driver's binary. */
136 drv->binary_path = get_abs_path(base_path, name, "");
137 if (drv->binary_path == NULL)
138 goto cleanup;
139
140 /* Check whether the driver's binary exists. */
141 struct stat s;
142 if (stat(drv->binary_path, &s) == ENOENT) { /* FIXME!! */
143 log_msg(LOG_DEFAULT, LVL_ERROR, "Driver not found at path `%s'.",
144 drv->binary_path);
145 goto cleanup;
146 }
147
148 suc = true;
149
150cleanup:
151 if (!suc) {
152 free(drv->binary_path);
153 free(drv->name);
154 /* Set the driver structure to the default state. */
155 init_driver(drv);
156 }
157
158 free(match_path);
159
160 return suc;
161}
162
163/** Lookup drivers in the directory.
164 *
165 * @param drivers_list The list of available drivers.
166 * @param dir_path The path to the directory where we search for drivers.
167 * @return Number of drivers which were found.
168 */
169int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path)
170{
171 log_msg(LOG_DEFAULT, LVL_DEBUG, "lookup_available_drivers(dir=\"%s\")", dir_path);
172
173 int drv_cnt = 0;
174 DIR *dir = NULL;
175 struct dirent *diren;
176
177 dir = opendir(dir_path);
178
179 if (dir != NULL) {
180 driver_t *drv = create_driver();
181 while ((diren = readdir(dir))) {
182 if (get_driver_info(dir_path, diren->d_name, drv)) {
183 add_driver(drivers_list, drv);
184 drv_cnt++;
185 drv = create_driver();
186 }
187 }
188 delete_driver(drv);
189 closedir(dir);
190 }
191
192 return drv_cnt;
193}
194
195/** Lookup the best matching driver for the specified device in the list of
196 * drivers.
197 *
198 * A match between a device and a driver is found if one of the driver's match
199 * ids match one of the device's match ids. The score of the match is the
200 * product of the driver's and device's score associated with the matching id.
201 * The best matching driver for a device is the driver with the highest score
202 * of the match between the device and the driver.
203 *
204 * @param drivers_list The list of drivers, where we look for the driver
205 * suitable for handling the device.
206 * @param node The device node structure of the device.
207 * @return The best matching driver or NULL if no matching driver
208 * is found.
209 */
210driver_t *find_best_match_driver(driver_list_t *drivers_list, dev_node_t *node)
211{
212 driver_t *best_drv = NULL, *drv = NULL;
213 int best_score = 0, score = 0;
214
215 fibril_mutex_lock(&drivers_list->drivers_mutex);
216
217 list_foreach(drivers_list->drivers, link) {
218 drv = list_get_instance(link, driver_t, drivers);
219 score = get_match_score(drv, node);
220 if (score > best_score) {
221 best_score = score;
222 best_drv = drv;
223 }
224 }
225
226 fibril_mutex_unlock(&drivers_list->drivers_mutex);
227
228 return best_drv;
229}
230
231/** Assign a driver to a device.
232 *
233 * @param tree Device tree
234 * @param node The device's node in the device tree.
235 * @param drv The driver.
236 */
237void attach_driver(dev_tree_t *tree, dev_node_t *dev, driver_t *drv)
238{
239 log_msg(LOG_DEFAULT, LVL_DEBUG, "attach_driver(dev=\"%s\",drv=\"%s\")",
240 dev->pfun->pathname, drv->name);
241
242 fibril_mutex_lock(&drv->driver_mutex);
243 fibril_rwlock_write_lock(&tree->rwlock);
244
245 dev->drv = drv;
246 list_append(&dev->driver_devices, &drv->devices);
247
248 fibril_rwlock_write_unlock(&tree->rwlock);
249 fibril_mutex_unlock(&drv->driver_mutex);
250}
251
252/** Detach driver from device.
253 *
254 * @param tree Device tree
255 * @param node The device's node in the device tree.
256 * @param drv The driver.
257 */
258void detach_driver(dev_tree_t *tree, dev_node_t *dev)
259{
260 driver_t *drv = dev->drv;
261
262 assert(drv != NULL);
263
264 log_msg(LOG_DEFAULT, LVL_DEBUG, "detach_driver(dev=\"%s\",drv=\"%s\")",
265 dev->pfun->pathname, drv->name);
266
267 fibril_mutex_lock(&drv->driver_mutex);
268 fibril_rwlock_write_lock(&tree->rwlock);
269
270 dev->drv = NULL;
271 list_remove(&dev->driver_devices);
272
273 fibril_rwlock_write_unlock(&tree->rwlock);
274 fibril_mutex_unlock(&drv->driver_mutex);
275}
276
277/** Start a driver
278 *
279 * @param drv The driver's structure.
280 * @return True if the driver's task is successfully spawned, false
281 * otherwise.
282 */
283bool start_driver(driver_t *drv)
284{
285 int rc;
286
287 assert(fibril_mutex_is_locked(&drv->driver_mutex));
288
289 log_msg(LOG_DEFAULT, LVL_DEBUG, "start_driver(drv=\"%s\")", drv->name);
290
291 rc = task_spawnl(NULL, drv->binary_path, drv->binary_path, NULL);
292 if (rc != EOK) {
293 log_msg(LOG_DEFAULT, LVL_ERROR, "Spawning driver `%s' (%s) failed: %s.",
294 drv->name, drv->binary_path, str_error(rc));
295 return false;
296 }
297
298 drv->state = DRIVER_STARTING;
299 return true;
300}
301
302/** Find device driver in the list of device drivers.
303 *
304 * @param drv_list The list of device drivers.
305 * @param drv_name The name of the device driver which is searched.
306 * @return The device driver of the specified name, if it is in the
307 * list, NULL otherwise.
308 */
309driver_t *find_driver(driver_list_t *drv_list, const char *drv_name)
310{
311 driver_t *res = NULL;
312 driver_t *drv = NULL;
313
314 fibril_mutex_lock(&drv_list->drivers_mutex);
315
316 list_foreach(drv_list->drivers, link) {
317 drv = list_get_instance(link, driver_t, drivers);
318 if (str_cmp(drv->name, drv_name) == 0) {
319 res = drv;
320 break;
321 }
322 }
323
324 fibril_mutex_unlock(&drv_list->drivers_mutex);
325
326 return res;
327}
328
329/** Notify driver about the devices to which it was assigned.
330 *
331 * @param driver The driver to which the devices are passed.
332 */
333static void pass_devices_to_driver(driver_t *driver, dev_tree_t *tree)
334{
335 dev_node_t *dev;
336 link_t *link;
337
338 log_msg(LOG_DEFAULT, LVL_DEBUG, "pass_devices_to_driver(driver=\"%s\")",
339 driver->name);
340
341 fibril_mutex_lock(&driver->driver_mutex);
342
343 /*
344 * Go through devices list as long as there is some device
345 * that has not been passed to the driver.
346 */
347 link = driver->devices.head.next;
348 while (link != &driver->devices.head) {
349 dev = list_get_instance(link, dev_node_t, driver_devices);
350 fibril_rwlock_write_lock(&tree->rwlock);
351
352 if (dev->passed_to_driver) {
353 fibril_rwlock_write_unlock(&tree->rwlock);
354 link = link->next;
355 continue;
356 }
357
358 log_msg(LOG_DEFAULT, LVL_DEBUG, "pass_devices_to_driver: dev->refcnt=%d\n",
359 (int)atomic_get(&dev->refcnt));
360 dev_add_ref(dev);
361
362 /*
363 * Unlock to avoid deadlock when adding device
364 * handled by itself.
365 */
366 fibril_mutex_unlock(&driver->driver_mutex);
367 fibril_rwlock_write_unlock(&tree->rwlock);
368
369 add_device(driver, dev, tree);
370
371 dev_del_ref(dev);
372
373 /*
374 * Lock again as we will work with driver's
375 * structure.
376 */
377 fibril_mutex_lock(&driver->driver_mutex);
378
379 /*
380 * Restart the cycle to go through all devices again.
381 */
382 link = driver->devices.head.next;
383 }
384
385 /*
386 * Once we passed all devices to the driver, we need to mark the
387 * driver as running.
388 * It is vital to do it here and inside critical section.
389 *
390 * If we would change the state earlier, other devices added to
391 * the driver would be added to the device list and started
392 * immediately and possibly started here as well.
393 */
394 log_msg(LOG_DEFAULT, LVL_DEBUG, "Driver `%s' enters running state.", driver->name);
395 driver->state = DRIVER_RUNNING;
396
397 fibril_mutex_unlock(&driver->driver_mutex);
398}
399
400/** Finish the initialization of a driver after it has succesfully started
401 * and after it has registered itself by the device manager.
402 *
403 * Pass devices formerly matched to the driver to the driver and remember the
404 * driver is running and fully functional now.
405 *
406 * @param driver The driver which registered itself as running by the
407 * device manager.
408 */
409void initialize_running_driver(driver_t *driver, dev_tree_t *tree)
410{
411 log_msg(LOG_DEFAULT, LVL_DEBUG, "initialize_running_driver(driver=\"%s\")",
412 driver->name);
413
414 /*
415 * Pass devices which have been already assigned to the driver to the
416 * driver.
417 */
418 pass_devices_to_driver(driver, tree);
419}
420
421/** Initialize device driver structure.
422 *
423 * @param drv The device driver structure.
424 */
425void init_driver(driver_t *drv)
426{
427 assert(drv != NULL);
428
429 memset(drv, 0, sizeof(driver_t));
430 list_initialize(&drv->match_ids.ids);
431 list_initialize(&drv->devices);
432 fibril_mutex_initialize(&drv->driver_mutex);
433 drv->sess = NULL;
434}
435
436/** Device driver structure clean-up.
437 *
438 * @param drv The device driver structure.
439 */
440void clean_driver(driver_t *drv)
441{
442 assert(drv != NULL);
443
444 free(drv->name);
445 free(drv->binary_path);
446
447 clean_match_ids(&drv->match_ids);
448
449 init_driver(drv);
450}
451
452/** Delete device driver structure.
453 *
454 * @param drv The device driver structure.
455 */
456void delete_driver(driver_t *drv)
457{
458 assert(drv != NULL);
459
460 clean_driver(drv);
461 free(drv);
462}
463
464/** Find suitable driver for a device and assign the driver to it.
465 *
466 * @param node The device node of the device in the device tree.
467 * @param drivers_list The list of available drivers.
468 * @return True if the suitable driver is found and
469 * successfully assigned to the device, false otherwise.
470 */
471bool assign_driver(dev_node_t *dev, driver_list_t *drivers_list,
472 dev_tree_t *tree)
473{
474 assert(dev != NULL);
475 assert(drivers_list != NULL);
476 assert(tree != NULL);
477
478 /*
479 * Find the driver which is the most suitable for handling this device.
480 */
481 driver_t *drv = find_best_match_driver(drivers_list, dev);
482 if (drv == NULL) {
483 log_msg(LOG_DEFAULT, LVL_ERROR, "No driver found for device `%s'.",
484 dev->pfun->pathname);
485 return false;
486 }
487
488 /* Attach the driver to the device. */
489 attach_driver(tree, dev, drv);
490
491 fibril_mutex_lock(&drv->driver_mutex);
492 if (drv->state == DRIVER_NOT_STARTED) {
493 /* Start the driver. */
494 start_driver(drv);
495 }
496 bool is_running = drv->state == DRIVER_RUNNING;
497 fibril_mutex_unlock(&drv->driver_mutex);
498
499 /* Notify the driver about the new device. */
500 if (is_running)
501 add_device(drv, dev, tree);
502
503 fibril_mutex_lock(&drv->driver_mutex);
504 fibril_mutex_unlock(&drv->driver_mutex);
505
506 fibril_rwlock_write_lock(&tree->rwlock);
507 if (dev->pfun != NULL) {
508 dev->pfun->state = FUN_ON_LINE;
509 }
510 fibril_rwlock_write_unlock(&tree->rwlock);
511 return true;
512}
513
514/** Pass a device to running driver.
515 *
516 * @param drv The driver's structure.
517 * @param node The device's node in the device tree.
518 */
519void add_device(driver_t *drv, dev_node_t *dev, dev_tree_t *tree)
520{
521 /*
522 * We do not expect to have driver's mutex locked as we do not
523 * access any structures that would affect driver_t.
524 */
525 log_msg(LOG_DEFAULT, LVL_DEBUG, "add_device(drv=\"%s\", dev=\"%s\")",
526 drv->name, dev->pfun->name);
527
528 /* Send the device to the driver. */
529 devman_handle_t parent_handle;
530 if (dev->pfun) {
531 parent_handle = dev->pfun->handle;
532 } else {
533 parent_handle = 0;
534 }
535
536 async_exch_t *exch = async_exchange_begin(drv->sess);
537
538 ipc_call_t answer;
539 aid_t req = async_send_2(exch, DRIVER_DEV_ADD, dev->handle,
540 parent_handle, &answer);
541
542 /* Send the device name to the driver. */
543 sysarg_t rc = async_data_write_start(exch, dev->pfun->name,
544 str_size(dev->pfun->name) + 1);
545
546 async_exchange_end(exch);
547
548 if (rc != EOK) {
549 /* TODO handle error */
550 }
551
552 /* Wait for answer from the driver. */
553 async_wait_for(req, &rc);
554
555 switch(rc) {
556 case EOK:
557 dev->state = DEVICE_USABLE;
558 break;
559 case ENOENT:
560 dev->state = DEVICE_NOT_PRESENT;
561 break;
562 default:
563 dev->state = DEVICE_INVALID;
564 break;
565 }
566
567 dev->passed_to_driver = true;
568
569 return;
570}
571
572int driver_dev_remove(dev_tree_t *tree, dev_node_t *dev)
573{
574 async_exch_t *exch;
575 sysarg_t retval;
576 driver_t *drv;
577 devman_handle_t handle;
578
579 assert(dev != NULL);
580
581 log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_remove(%p)", dev);
582
583 fibril_rwlock_read_lock(&tree->rwlock);
584 drv = dev->drv;
585 handle = dev->handle;
586 fibril_rwlock_read_unlock(&tree->rwlock);
587
588 exch = async_exchange_begin(drv->sess);
589 retval = async_req_1_0(exch, DRIVER_DEV_REMOVE, handle);
590 async_exchange_end(exch);
591
592 return retval;
593}
594
595int driver_dev_gone(dev_tree_t *tree, dev_node_t *dev)
596{
597 async_exch_t *exch;
598 sysarg_t retval;
599 driver_t *drv;
600 devman_handle_t handle;
601
602 assert(dev != NULL);
603
604 log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_gone(%p)", dev);
605
606 fibril_rwlock_read_lock(&tree->rwlock);
607 drv = dev->drv;
608 handle = dev->handle;
609 fibril_rwlock_read_unlock(&tree->rwlock);
610
611 exch = async_exchange_begin(drv->sess);
612 retval = async_req_1_0(exch, DRIVER_DEV_GONE, handle);
613 async_exchange_end(exch);
614
615 return retval;
616}
617
618int driver_fun_online(dev_tree_t *tree, fun_node_t *fun)
619{
620 async_exch_t *exch;
621 sysarg_t retval;
622 driver_t *drv;
623 devman_handle_t handle;
624
625 log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_fun_online(%p)", fun);
626
627 fibril_rwlock_read_lock(&tree->rwlock);
628
629 if (fun->dev == NULL) {
630 /* XXX root function? */
631 fibril_rwlock_read_unlock(&tree->rwlock);
632 return EINVAL;
633 }
634
635 drv = fun->dev->drv;
636 handle = fun->handle;
637 fibril_rwlock_read_unlock(&tree->rwlock);
638
639 exch = async_exchange_begin(drv->sess);
640 retval = async_req_1_0(exch, DRIVER_FUN_ONLINE, handle);
641 loc_exchange_end(exch);
642
643 return retval;
644}
645
646int driver_fun_offline(dev_tree_t *tree, fun_node_t *fun)
647{
648 async_exch_t *exch;
649 sysarg_t retval;
650 driver_t *drv;
651 devman_handle_t handle;
652
653 log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_fun_offline(%p)", fun);
654
655 fibril_rwlock_read_lock(&tree->rwlock);
656 if (fun->dev == NULL) {
657 /* XXX root function? */
658 fibril_rwlock_read_unlock(&tree->rwlock);
659 return EINVAL;
660 }
661
662 drv = fun->dev->drv;
663 handle = fun->handle;
664 fibril_rwlock_read_unlock(&tree->rwlock);
665
666 exch = async_exchange_begin(drv->sess);
667 retval = async_req_1_0(exch, DRIVER_FUN_OFFLINE, handle);
668 loc_exchange_end(exch);
669
670 return retval;
671
672}
673
674/** @}
675 */
Note: See TracBrowser for help on using the repository browser.