source: mainline/uspace/srv/devman/driver.c@ 38e52c92

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

Move dev-node related devman functionality to a separate module.

  • Property mode set to 100644
File size: 15.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 <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 <ipc/devman.h>
40#include <loc.h>
41#include <str_error.h>
42#include <stdio.h>
43
44#include "dev.h"
45#include "devman.h"
46#include "driver.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
514int driver_dev_remove(dev_tree_t *tree, dev_node_t *dev)
515{
516 async_exch_t *exch;
517 sysarg_t retval;
518 driver_t *drv;
519 devman_handle_t handle;
520
521 assert(dev != NULL);
522
523 log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_remove(%p)", dev);
524
525 fibril_rwlock_read_lock(&tree->rwlock);
526 drv = dev->drv;
527 handle = dev->handle;
528 fibril_rwlock_read_unlock(&tree->rwlock);
529
530 exch = async_exchange_begin(drv->sess);
531 retval = async_req_1_0(exch, DRIVER_DEV_REMOVE, handle);
532 async_exchange_end(exch);
533
534 return retval;
535}
536
537int driver_dev_gone(dev_tree_t *tree, dev_node_t *dev)
538{
539 async_exch_t *exch;
540 sysarg_t retval;
541 driver_t *drv;
542 devman_handle_t handle;
543
544 assert(dev != NULL);
545
546 log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_gone(%p)", dev);
547
548 fibril_rwlock_read_lock(&tree->rwlock);
549 drv = dev->drv;
550 handle = dev->handle;
551 fibril_rwlock_read_unlock(&tree->rwlock);
552
553 exch = async_exchange_begin(drv->sess);
554 retval = async_req_1_0(exch, DRIVER_DEV_GONE, handle);
555 async_exchange_end(exch);
556
557 return retval;
558}
559
560int driver_fun_online(dev_tree_t *tree, fun_node_t *fun)
561{
562 async_exch_t *exch;
563 sysarg_t retval;
564 driver_t *drv;
565 devman_handle_t handle;
566
567 log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_fun_online(%p)", fun);
568
569 fibril_rwlock_read_lock(&tree->rwlock);
570
571 if (fun->dev == NULL) {
572 /* XXX root function? */
573 fibril_rwlock_read_unlock(&tree->rwlock);
574 return EINVAL;
575 }
576
577 drv = fun->dev->drv;
578 handle = fun->handle;
579 fibril_rwlock_read_unlock(&tree->rwlock);
580
581 exch = async_exchange_begin(drv->sess);
582 retval = async_req_1_0(exch, DRIVER_FUN_ONLINE, handle);
583 loc_exchange_end(exch);
584
585 return retval;
586}
587
588int driver_fun_offline(dev_tree_t *tree, fun_node_t *fun)
589{
590 async_exch_t *exch;
591 sysarg_t retval;
592 driver_t *drv;
593 devman_handle_t handle;
594
595 log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_fun_offline(%p)", fun);
596
597 fibril_rwlock_read_lock(&tree->rwlock);
598 if (fun->dev == NULL) {
599 /* XXX root function? */
600 fibril_rwlock_read_unlock(&tree->rwlock);
601 return EINVAL;
602 }
603
604 drv = fun->dev->drv;
605 handle = fun->handle;
606 fibril_rwlock_read_unlock(&tree->rwlock);
607
608 exch = async_exchange_begin(drv->sess);
609 retval = async_req_1_0(exch, DRIVER_FUN_OFFLINE, handle);
610 loc_exchange_end(exch);
611
612 return retval;
613
614}
615
616/** @}
617 */
Note: See TracBrowser for help on using the repository browser.