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 <io/log.h>
|
---|
36 | #include <vfs/vfs.h>
|
---|
37 | #include <loc.h>
|
---|
38 | #include <str_error.h>
|
---|
39 | #include <stdio.h>
|
---|
40 | #include <task.h>
|
---|
41 |
|
---|
42 | #include "dev.h"
|
---|
43 | #include "devman.h"
|
---|
44 | #include "driver.h"
|
---|
45 | #include "match.h"
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Initialize the list of device driver's.
|
---|
49 | *
|
---|
50 | * @param drv_list the list of device driver's.
|
---|
51 | *
|
---|
52 | */
|
---|
53 | void init_driver_list(driver_list_t *drv_list)
|
---|
54 | {
|
---|
55 | assert(drv_list != NULL);
|
---|
56 |
|
---|
57 | list_initialize(&drv_list->drivers);
|
---|
58 | fibril_mutex_initialize(&drv_list->drivers_mutex);
|
---|
59 | drv_list->next_handle = 1;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /** Allocate and initialize a new driver structure.
|
---|
63 | *
|
---|
64 | * @return Driver structure.
|
---|
65 | */
|
---|
66 | driver_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 | */
|
---|
79 | void add_driver(driver_list_t *drivers_list, driver_t *drv)
|
---|
80 | {
|
---|
81 | fibril_mutex_lock(&drivers_list->drivers_mutex);
|
---|
82 | list_append(&drv->drivers, &drivers_list->drivers);
|
---|
83 | drv->handle = drivers_list->next_handle++;
|
---|
84 | fibril_mutex_unlock(&drivers_list->drivers_mutex);
|
---|
85 |
|
---|
86 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Driver `%s' was added to the list of available "
|
---|
87 | "drivers.", drv->name);
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Get information about a driver.
|
---|
92 | *
|
---|
93 | * Each driver has its own directory in the base directory.
|
---|
94 | * The name of the driver's directory is the same as the name of the driver.
|
---|
95 | * The driver's directory contains driver's binary (named as the driver without
|
---|
96 | * extension) and the configuration file with match ids for device-to-driver
|
---|
97 | * matching (named as the driver with a special extension).
|
---|
98 | *
|
---|
99 | * This function searches for the driver's directory and containing
|
---|
100 | * configuration files. If all the files needed are found, they are parsed and
|
---|
101 | * the information about the driver is stored in the driver's structure.
|
---|
102 | *
|
---|
103 | * @param base_path The base directory, in which we look for driver's
|
---|
104 | * subdirectory.
|
---|
105 | * @param name The name of the driver.
|
---|
106 | * @param drv The driver structure to fill information in.
|
---|
107 | *
|
---|
108 | * @return True on success, false otherwise.
|
---|
109 | */
|
---|
110 | bool get_driver_info(const char *base_path, const char *name, driver_t *drv)
|
---|
111 | {
|
---|
112 | log_msg(LOG_DEFAULT, LVL_DEBUG, "get_driver_info(base_path=\"%s\", name=\"%s\")",
|
---|
113 | base_path, name);
|
---|
114 |
|
---|
115 | assert(base_path != NULL && name != NULL && drv != NULL);
|
---|
116 |
|
---|
117 | bool suc = false;
|
---|
118 | char *match_path = NULL;
|
---|
119 | size_t name_size = 0;
|
---|
120 |
|
---|
121 | /* Read the list of match ids from the driver's configuration file. */
|
---|
122 | match_path = get_abs_path(base_path, name, MATCH_EXT);
|
---|
123 | if (match_path == NULL)
|
---|
124 | goto cleanup;
|
---|
125 |
|
---|
126 | if (!read_match_ids(match_path, &drv->match_ids))
|
---|
127 | goto cleanup;
|
---|
128 |
|
---|
129 | /* Allocate and fill driver's name. */
|
---|
130 | name_size = str_size(name) + 1;
|
---|
131 | drv->name = malloc(name_size);
|
---|
132 | if (drv->name == NULL)
|
---|
133 | goto cleanup;
|
---|
134 | str_cpy(drv->name, name_size, name);
|
---|
135 |
|
---|
136 | /* Initialize path with driver's binary. */
|
---|
137 | drv->binary_path = get_abs_path(base_path, name, "");
|
---|
138 | if (drv->binary_path == NULL)
|
---|
139 | goto cleanup;
|
---|
140 |
|
---|
141 | /* Check whether the driver's binary exists. */
|
---|
142 | struct stat s;
|
---|
143 | if (vfs_stat_path(drv->binary_path, &s) != EOK) {
|
---|
144 | log_msg(LOG_DEFAULT, LVL_ERROR, "Driver not found at path `%s'.",
|
---|
145 | drv->binary_path);
|
---|
146 | goto cleanup;
|
---|
147 | }
|
---|
148 |
|
---|
149 | suc = true;
|
---|
150 |
|
---|
151 | cleanup:
|
---|
152 | if (!suc) {
|
---|
153 | free(drv->binary_path);
|
---|
154 | free(drv->name);
|
---|
155 | /* Set the driver structure to the default state. */
|
---|
156 | init_driver(drv);
|
---|
157 | }
|
---|
158 |
|
---|
159 | free(match_path);
|
---|
160 |
|
---|
161 | return suc;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /** Lookup drivers in the directory.
|
---|
165 | *
|
---|
166 | * @param drivers_list The list of available drivers.
|
---|
167 | * @param dir_path The path to the directory where we search for drivers.
|
---|
168 | * @return Number of drivers which were found.
|
---|
169 | */
|
---|
170 | int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path)
|
---|
171 | {
|
---|
172 | log_msg(LOG_DEFAULT, LVL_DEBUG, "lookup_available_drivers(dir=\"%s\")", dir_path);
|
---|
173 |
|
---|
174 | int drv_cnt = 0;
|
---|
175 | DIR *dir = NULL;
|
---|
176 | struct dirent *diren;
|
---|
177 |
|
---|
178 | dir = opendir(dir_path);
|
---|
179 |
|
---|
180 | if (dir != NULL) {
|
---|
181 | driver_t *drv = create_driver();
|
---|
182 | while ((diren = readdir(dir))) {
|
---|
183 | if (get_driver_info(dir_path, diren->d_name, drv)) {
|
---|
184 | add_driver(drivers_list, drv);
|
---|
185 | drv_cnt++;
|
---|
186 | drv = create_driver();
|
---|
187 | }
|
---|
188 | }
|
---|
189 | delete_driver(drv);
|
---|
190 | closedir(dir);
|
---|
191 | }
|
---|
192 |
|
---|
193 | return drv_cnt;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /** Lookup the best matching driver for the specified device in the list of
|
---|
197 | * drivers.
|
---|
198 | *
|
---|
199 | * A match between a device and a driver is found if one of the driver's match
|
---|
200 | * ids match one of the device's match ids. The score of the match is the
|
---|
201 | * product of the driver's and device's score associated with the matching id.
|
---|
202 | * The best matching driver for a device is the driver with the highest score
|
---|
203 | * of the match between the device and the driver.
|
---|
204 | *
|
---|
205 | * @param drivers_list The list of drivers, where we look for the driver
|
---|
206 | * suitable for handling the device.
|
---|
207 | * @param node The device node structure of the device.
|
---|
208 | * @return The best matching driver or NULL if no matching driver
|
---|
209 | * is found.
|
---|
210 | */
|
---|
211 | driver_t *find_best_match_driver(driver_list_t *drivers_list, dev_node_t *node)
|
---|
212 | {
|
---|
213 | driver_t *best_drv = NULL;
|
---|
214 | int best_score = 0, score = 0;
|
---|
215 |
|
---|
216 | fibril_mutex_lock(&drivers_list->drivers_mutex);
|
---|
217 |
|
---|
218 | list_foreach(drivers_list->drivers, drivers, driver_t, drv) {
|
---|
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 | */
|
---|
237 | void 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 | */
|
---|
258 | void 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 | */
|
---|
283 | bool 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, 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 by handle.
|
---|
303 | *
|
---|
304 | * @param drv_list The list of device drivers
|
---|
305 | * @param handle Driver handle
|
---|
306 | * @return The device driver, if it is in the list,
|
---|
307 | * NULL otherwise.
|
---|
308 | */
|
---|
309 | driver_t *driver_find(driver_list_t *drv_list, devman_handle_t handle)
|
---|
310 | {
|
---|
311 | driver_t *res = NULL;
|
---|
312 |
|
---|
313 | fibril_mutex_lock(&drv_list->drivers_mutex);
|
---|
314 |
|
---|
315 | list_foreach(drv_list->drivers, drivers, driver_t, drv) {
|
---|
316 | if (drv->handle == handle) {
|
---|
317 | res = drv;
|
---|
318 | break;
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | fibril_mutex_unlock(&drv_list->drivers_mutex);
|
---|
323 |
|
---|
324 | return res;
|
---|
325 | }
|
---|
326 |
|
---|
327 |
|
---|
328 | /** Find device driver by name.
|
---|
329 | *
|
---|
330 | * @param drv_list The list of device drivers.
|
---|
331 | * @param drv_name The name of the device driver which is searched.
|
---|
332 | * @return The device driver of the specified name, if it is in the
|
---|
333 | * list, NULL otherwise.
|
---|
334 | */
|
---|
335 | driver_t *driver_find_by_name(driver_list_t *drv_list, const char *drv_name)
|
---|
336 | {
|
---|
337 | driver_t *res = NULL;
|
---|
338 |
|
---|
339 | fibril_mutex_lock(&drv_list->drivers_mutex);
|
---|
340 |
|
---|
341 | list_foreach(drv_list->drivers, drivers, driver_t, drv) {
|
---|
342 | if (str_cmp(drv->name, drv_name) == 0) {
|
---|
343 | res = drv;
|
---|
344 | break;
|
---|
345 | }
|
---|
346 | }
|
---|
347 |
|
---|
348 | fibril_mutex_unlock(&drv_list->drivers_mutex);
|
---|
349 |
|
---|
350 | return res;
|
---|
351 | }
|
---|
352 |
|
---|
353 | /** Notify driver about the devices to which it was assigned.
|
---|
354 | *
|
---|
355 | * @param driver The driver to which the devices are passed.
|
---|
356 | */
|
---|
357 | static void pass_devices_to_driver(driver_t *driver, dev_tree_t *tree)
|
---|
358 | {
|
---|
359 | dev_node_t *dev;
|
---|
360 | link_t *link;
|
---|
361 |
|
---|
362 | log_msg(LOG_DEFAULT, LVL_DEBUG, "pass_devices_to_driver(driver=\"%s\")",
|
---|
363 | driver->name);
|
---|
364 |
|
---|
365 | fibril_mutex_lock(&driver->driver_mutex);
|
---|
366 |
|
---|
367 | /*
|
---|
368 | * Go through devices list as long as there is some device
|
---|
369 | * that has not been passed to the driver.
|
---|
370 | */
|
---|
371 | link = driver->devices.head.next;
|
---|
372 | while (link != &driver->devices.head) {
|
---|
373 | dev = list_get_instance(link, dev_node_t, driver_devices);
|
---|
374 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
375 |
|
---|
376 | if (dev->passed_to_driver) {
|
---|
377 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
378 | link = link->next;
|
---|
379 | continue;
|
---|
380 | }
|
---|
381 |
|
---|
382 | log_msg(LOG_DEFAULT, LVL_DEBUG, "pass_devices_to_driver: dev->refcnt=%d\n",
|
---|
383 | (int)atomic_get(&dev->refcnt));
|
---|
384 | dev_add_ref(dev);
|
---|
385 |
|
---|
386 | /*
|
---|
387 | * Unlock to avoid deadlock when adding device
|
---|
388 | * handled by itself.
|
---|
389 | */
|
---|
390 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
391 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
392 |
|
---|
393 | add_device(driver, dev, tree);
|
---|
394 |
|
---|
395 | dev_del_ref(dev);
|
---|
396 |
|
---|
397 | /*
|
---|
398 | * Lock again as we will work with driver's
|
---|
399 | * structure.
|
---|
400 | */
|
---|
401 | fibril_mutex_lock(&driver->driver_mutex);
|
---|
402 |
|
---|
403 | /*
|
---|
404 | * Restart the cycle to go through all devices again.
|
---|
405 | */
|
---|
406 | link = driver->devices.head.next;
|
---|
407 | }
|
---|
408 |
|
---|
409 | /*
|
---|
410 | * Once we passed all devices to the driver, we need to mark the
|
---|
411 | * driver as running.
|
---|
412 | * It is vital to do it here and inside critical section.
|
---|
413 | *
|
---|
414 | * If we would change the state earlier, other devices added to
|
---|
415 | * the driver would be added to the device list and started
|
---|
416 | * immediately and possibly started here as well.
|
---|
417 | */
|
---|
418 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Driver `%s' enters running state.", driver->name);
|
---|
419 | driver->state = DRIVER_RUNNING;
|
---|
420 |
|
---|
421 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
422 | }
|
---|
423 |
|
---|
424 | /** Finish the initialization of a driver after it has succesfully started
|
---|
425 | * and after it has registered itself by the device manager.
|
---|
426 | *
|
---|
427 | * Pass devices formerly matched to the driver to the driver and remember the
|
---|
428 | * driver is running and fully functional now.
|
---|
429 | *
|
---|
430 | * @param driver The driver which registered itself as running by the
|
---|
431 | * device manager.
|
---|
432 | */
|
---|
433 | void initialize_running_driver(driver_t *driver, dev_tree_t *tree)
|
---|
434 | {
|
---|
435 | log_msg(LOG_DEFAULT, LVL_DEBUG, "initialize_running_driver(driver=\"%s\")",
|
---|
436 | driver->name);
|
---|
437 |
|
---|
438 | /*
|
---|
439 | * Pass devices which have been already assigned to the driver to the
|
---|
440 | * driver.
|
---|
441 | */
|
---|
442 | pass_devices_to_driver(driver, tree);
|
---|
443 | }
|
---|
444 |
|
---|
445 | /** Initialize device driver structure.
|
---|
446 | *
|
---|
447 | * @param drv The device driver structure.
|
---|
448 | */
|
---|
449 | void init_driver(driver_t *drv)
|
---|
450 | {
|
---|
451 | assert(drv != NULL);
|
---|
452 |
|
---|
453 | memset(drv, 0, sizeof(driver_t));
|
---|
454 | list_initialize(&drv->match_ids.ids);
|
---|
455 | list_initialize(&drv->devices);
|
---|
456 | fibril_mutex_initialize(&drv->driver_mutex);
|
---|
457 | drv->sess = NULL;
|
---|
458 | }
|
---|
459 |
|
---|
460 | /** Device driver structure clean-up.
|
---|
461 | *
|
---|
462 | * @param drv The device driver structure.
|
---|
463 | */
|
---|
464 | void clean_driver(driver_t *drv)
|
---|
465 | {
|
---|
466 | assert(drv != NULL);
|
---|
467 |
|
---|
468 | free(drv->name);
|
---|
469 | free(drv->binary_path);
|
---|
470 |
|
---|
471 | clean_match_ids(&drv->match_ids);
|
---|
472 |
|
---|
473 | init_driver(drv);
|
---|
474 | }
|
---|
475 |
|
---|
476 | /** Delete device driver structure.
|
---|
477 | *
|
---|
478 | * @param drv The device driver structure.
|
---|
479 | */
|
---|
480 | void delete_driver(driver_t *drv)
|
---|
481 | {
|
---|
482 | assert(drv != NULL);
|
---|
483 |
|
---|
484 | clean_driver(drv);
|
---|
485 | free(drv);
|
---|
486 | }
|
---|
487 |
|
---|
488 | /** Find suitable driver for a device and assign the driver to it.
|
---|
489 | *
|
---|
490 | * @param node The device node of the device in the device tree.
|
---|
491 | * @param drivers_list The list of available drivers.
|
---|
492 | * @return True if the suitable driver is found and
|
---|
493 | * successfully assigned to the device, false otherwise.
|
---|
494 | */
|
---|
495 | bool assign_driver(dev_node_t *dev, driver_list_t *drivers_list,
|
---|
496 | dev_tree_t *tree)
|
---|
497 | {
|
---|
498 | assert(dev != NULL);
|
---|
499 | assert(drivers_list != NULL);
|
---|
500 | assert(tree != NULL);
|
---|
501 |
|
---|
502 | /*
|
---|
503 | * Find the driver which is the most suitable for handling this device.
|
---|
504 | */
|
---|
505 | driver_t *drv = find_best_match_driver(drivers_list, dev);
|
---|
506 | if (drv == NULL) {
|
---|
507 | log_msg(LOG_DEFAULT, LVL_ERROR, "No driver found for device `%s'.",
|
---|
508 | dev->pfun->pathname);
|
---|
509 | return false;
|
---|
510 | }
|
---|
511 |
|
---|
512 | /* Attach the driver to the device. */
|
---|
513 | attach_driver(tree, dev, drv);
|
---|
514 |
|
---|
515 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
516 | if (drv->state == DRIVER_NOT_STARTED) {
|
---|
517 | /* Start the driver. */
|
---|
518 | start_driver(drv);
|
---|
519 | }
|
---|
520 | bool is_running = drv->state == DRIVER_RUNNING;
|
---|
521 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
522 |
|
---|
523 | /* Notify the driver about the new device. */
|
---|
524 | if (is_running)
|
---|
525 | add_device(drv, dev, tree);
|
---|
526 |
|
---|
527 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
528 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
529 |
|
---|
530 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
531 | if (dev->pfun != NULL) {
|
---|
532 | dev->pfun->state = FUN_ON_LINE;
|
---|
533 | }
|
---|
534 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
535 | return true;
|
---|
536 | }
|
---|
537 |
|
---|
538 | /** Pass a device to running driver.
|
---|
539 | *
|
---|
540 | * @param drv The driver's structure.
|
---|
541 | * @param node The device's node in the device tree.
|
---|
542 | */
|
---|
543 | void add_device(driver_t *drv, dev_node_t *dev, dev_tree_t *tree)
|
---|
544 | {
|
---|
545 | /*
|
---|
546 | * We do not expect to have driver's mutex locked as we do not
|
---|
547 | * access any structures that would affect driver_t.
|
---|
548 | */
|
---|
549 | log_msg(LOG_DEFAULT, LVL_DEBUG, "add_device(drv=\"%s\", dev=\"%s\")",
|
---|
550 | drv->name, dev->pfun->name);
|
---|
551 |
|
---|
552 | /* Send the device to the driver. */
|
---|
553 | devman_handle_t parent_handle;
|
---|
554 | if (dev->pfun) {
|
---|
555 | parent_handle = dev->pfun->handle;
|
---|
556 | } else {
|
---|
557 | parent_handle = 0;
|
---|
558 | }
|
---|
559 |
|
---|
560 | async_exch_t *exch = async_exchange_begin(drv->sess);
|
---|
561 |
|
---|
562 | ipc_call_t answer;
|
---|
563 | aid_t req = async_send_2(exch, DRIVER_DEV_ADD, dev->handle,
|
---|
564 | parent_handle, &answer);
|
---|
565 |
|
---|
566 | /* Send the device name to the driver. */
|
---|
567 | sysarg_t rc = async_data_write_start(exch, dev->pfun->name,
|
---|
568 | str_size(dev->pfun->name) + 1);
|
---|
569 |
|
---|
570 | async_exchange_end(exch);
|
---|
571 |
|
---|
572 | if (rc != EOK) {
|
---|
573 | async_forget(req);
|
---|
574 | } else {
|
---|
575 | /* Wait for answer from the driver. */
|
---|
576 | async_wait_for(req, &rc);
|
---|
577 | }
|
---|
578 |
|
---|
579 | switch (rc) {
|
---|
580 | case EOK:
|
---|
581 | dev->state = DEVICE_USABLE;
|
---|
582 | break;
|
---|
583 | case ENOENT:
|
---|
584 | dev->state = DEVICE_NOT_PRESENT;
|
---|
585 | break;
|
---|
586 | default:
|
---|
587 | dev->state = DEVICE_INVALID;
|
---|
588 | break;
|
---|
589 | }
|
---|
590 |
|
---|
591 | dev->passed_to_driver = true;
|
---|
592 | }
|
---|
593 |
|
---|
594 | int driver_dev_remove(dev_tree_t *tree, dev_node_t *dev)
|
---|
595 | {
|
---|
596 | async_exch_t *exch;
|
---|
597 | sysarg_t retval;
|
---|
598 | driver_t *drv;
|
---|
599 | devman_handle_t handle;
|
---|
600 |
|
---|
601 | assert(dev != NULL);
|
---|
602 |
|
---|
603 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_remove(%p)", dev);
|
---|
604 |
|
---|
605 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
606 | drv = dev->drv;
|
---|
607 | handle = dev->handle;
|
---|
608 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
609 |
|
---|
610 | exch = async_exchange_begin(drv->sess);
|
---|
611 | retval = async_req_1_0(exch, DRIVER_DEV_REMOVE, handle);
|
---|
612 | async_exchange_end(exch);
|
---|
613 |
|
---|
614 | return retval;
|
---|
615 | }
|
---|
616 |
|
---|
617 | int driver_dev_gone(dev_tree_t *tree, dev_node_t *dev)
|
---|
618 | {
|
---|
619 | async_exch_t *exch;
|
---|
620 | sysarg_t retval;
|
---|
621 | driver_t *drv;
|
---|
622 | devman_handle_t handle;
|
---|
623 |
|
---|
624 | assert(dev != NULL);
|
---|
625 |
|
---|
626 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_gone(%p)", dev);
|
---|
627 |
|
---|
628 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
629 | drv = dev->drv;
|
---|
630 | handle = dev->handle;
|
---|
631 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
632 |
|
---|
633 | exch = async_exchange_begin(drv->sess);
|
---|
634 | retval = async_req_1_0(exch, DRIVER_DEV_GONE, handle);
|
---|
635 | async_exchange_end(exch);
|
---|
636 |
|
---|
637 | return retval;
|
---|
638 | }
|
---|
639 |
|
---|
640 | int driver_fun_online(dev_tree_t *tree, fun_node_t *fun)
|
---|
641 | {
|
---|
642 | async_exch_t *exch;
|
---|
643 | sysarg_t retval;
|
---|
644 | driver_t *drv;
|
---|
645 | devman_handle_t handle;
|
---|
646 |
|
---|
647 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_fun_online(%p)", fun);
|
---|
648 |
|
---|
649 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
650 |
|
---|
651 | if (fun->dev == NULL) {
|
---|
652 | /* XXX root function? */
|
---|
653 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
654 | return EINVAL;
|
---|
655 | }
|
---|
656 |
|
---|
657 | drv = fun->dev->drv;
|
---|
658 | handle = fun->handle;
|
---|
659 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
660 |
|
---|
661 | exch = async_exchange_begin(drv->sess);
|
---|
662 | retval = async_req_1_0(exch, DRIVER_FUN_ONLINE, handle);
|
---|
663 | loc_exchange_end(exch);
|
---|
664 |
|
---|
665 | return retval;
|
---|
666 | }
|
---|
667 |
|
---|
668 | int driver_fun_offline(dev_tree_t *tree, fun_node_t *fun)
|
---|
669 | {
|
---|
670 | async_exch_t *exch;
|
---|
671 | sysarg_t retval;
|
---|
672 | driver_t *drv;
|
---|
673 | devman_handle_t handle;
|
---|
674 |
|
---|
675 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_fun_offline(%p)", fun);
|
---|
676 |
|
---|
677 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
678 | if (fun->dev == NULL) {
|
---|
679 | /* XXX root function? */
|
---|
680 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
681 | return EINVAL;
|
---|
682 | }
|
---|
683 |
|
---|
684 | drv = fun->dev->drv;
|
---|
685 | handle = fun->handle;
|
---|
686 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
687 |
|
---|
688 | exch = async_exchange_begin(drv->sess);
|
---|
689 | retval = async_req_1_0(exch, DRIVER_FUN_OFFLINE, handle);
|
---|
690 | loc_exchange_end(exch);
|
---|
691 |
|
---|
692 | return retval;
|
---|
693 |
|
---|
694 | }
|
---|
695 |
|
---|
696 | /** Get list of registered drivers. */
|
---|
697 | int driver_get_list(driver_list_t *driver_list, devman_handle_t *hdl_buf,
|
---|
698 | size_t buf_size, size_t *act_size)
|
---|
699 | {
|
---|
700 | size_t act_cnt;
|
---|
701 | size_t buf_cnt;
|
---|
702 |
|
---|
703 | fibril_mutex_lock(&driver_list->drivers_mutex);
|
---|
704 |
|
---|
705 | buf_cnt = buf_size / sizeof(devman_handle_t);
|
---|
706 |
|
---|
707 | act_cnt = list_count(&driver_list->drivers);
|
---|
708 | *act_size = act_cnt * sizeof(devman_handle_t);
|
---|
709 |
|
---|
710 | if (buf_size % sizeof(devman_handle_t) != 0) {
|
---|
711 | fibril_mutex_unlock(&driver_list->drivers_mutex);
|
---|
712 | return EINVAL;
|
---|
713 | }
|
---|
714 |
|
---|
715 | size_t pos = 0;
|
---|
716 | list_foreach(driver_list->drivers, drivers, driver_t, drv) {
|
---|
717 | if (pos < buf_cnt) {
|
---|
718 | hdl_buf[pos] = drv->handle;
|
---|
719 | }
|
---|
720 |
|
---|
721 | pos++;
|
---|
722 | }
|
---|
723 |
|
---|
724 | fibril_mutex_unlock(&driver_list->drivers_mutex);
|
---|
725 | return EOK;
|
---|
726 | }
|
---|
727 |
|
---|
728 | /** Get list of device functions. */
|
---|
729 | int driver_get_devices(driver_t *driver, devman_handle_t *hdl_buf,
|
---|
730 | size_t buf_size, size_t *act_size)
|
---|
731 | {
|
---|
732 | size_t act_cnt;
|
---|
733 | size_t buf_cnt;
|
---|
734 |
|
---|
735 | fibril_mutex_lock(&driver->driver_mutex);
|
---|
736 |
|
---|
737 | buf_cnt = buf_size / sizeof(devman_handle_t);
|
---|
738 |
|
---|
739 | act_cnt = list_count(&driver->devices);
|
---|
740 | *act_size = act_cnt * sizeof(devman_handle_t);
|
---|
741 |
|
---|
742 | if (buf_size % sizeof(devman_handle_t) != 0) {
|
---|
743 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
744 | return EINVAL;
|
---|
745 | }
|
---|
746 |
|
---|
747 | size_t pos = 0;
|
---|
748 | list_foreach(driver->devices, driver_devices, dev_node_t, dev) {
|
---|
749 | if (pos < buf_cnt) {
|
---|
750 | hdl_buf[pos] = dev->handle;
|
---|
751 | }
|
---|
752 |
|
---|
753 | pos++;
|
---|
754 | }
|
---|
755 |
|
---|
756 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
757 | return EOK;
|
---|
758 | }
|
---|
759 |
|
---|
760 | /** @}
|
---|
761 | */
|
---|