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 | */
|
---|
54 | void 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 | */
|
---|
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_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 | */
|
---|
109 | bool 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 |
|
---|
150 | cleanup:
|
---|
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 | */
|
---|
169 | int 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 | */
|
---|
210 | driver_t *find_best_match_driver(driver_list_t *drivers_list, dev_node_t *node)
|
---|
211 | {
|
---|
212 | driver_t *best_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, drivers, driver_t, drv) {
|
---|
218 | score = get_match_score(drv, node);
|
---|
219 | if (score > best_score) {
|
---|
220 | best_score = score;
|
---|
221 | best_drv = drv;
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | fibril_mutex_unlock(&drivers_list->drivers_mutex);
|
---|
226 |
|
---|
227 | return best_drv;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /** Assign a driver to a device.
|
---|
231 | *
|
---|
232 | * @param tree Device tree
|
---|
233 | * @param node The device's node in the device tree.
|
---|
234 | * @param drv The driver.
|
---|
235 | */
|
---|
236 | void attach_driver(dev_tree_t *tree, dev_node_t *dev, driver_t *drv)
|
---|
237 | {
|
---|
238 | log_msg(LOG_DEFAULT, LVL_DEBUG, "attach_driver(dev=\"%s\",drv=\"%s\")",
|
---|
239 | dev->pfun->pathname, drv->name);
|
---|
240 |
|
---|
241 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
242 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
243 |
|
---|
244 | dev->drv = drv;
|
---|
245 | list_append(&dev->driver_devices, &drv->devices);
|
---|
246 |
|
---|
247 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
248 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
249 | }
|
---|
250 |
|
---|
251 | /** Detach driver from device.
|
---|
252 | *
|
---|
253 | * @param tree Device tree
|
---|
254 | * @param node The device's node in the device tree.
|
---|
255 | * @param drv The driver.
|
---|
256 | */
|
---|
257 | void detach_driver(dev_tree_t *tree, dev_node_t *dev)
|
---|
258 | {
|
---|
259 | driver_t *drv = dev->drv;
|
---|
260 |
|
---|
261 | assert(drv != NULL);
|
---|
262 |
|
---|
263 | log_msg(LOG_DEFAULT, LVL_DEBUG, "detach_driver(dev=\"%s\",drv=\"%s\")",
|
---|
264 | dev->pfun->pathname, drv->name);
|
---|
265 |
|
---|
266 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
267 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
268 |
|
---|
269 | dev->drv = NULL;
|
---|
270 | list_remove(&dev->driver_devices);
|
---|
271 |
|
---|
272 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
273 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
274 | }
|
---|
275 |
|
---|
276 | /** Start a driver
|
---|
277 | *
|
---|
278 | * @param drv The driver's structure.
|
---|
279 | * @return True if the driver's task is successfully spawned, false
|
---|
280 | * otherwise.
|
---|
281 | */
|
---|
282 | bool start_driver(driver_t *drv)
|
---|
283 | {
|
---|
284 | int rc;
|
---|
285 |
|
---|
286 | assert(fibril_mutex_is_locked(&drv->driver_mutex));
|
---|
287 |
|
---|
288 | log_msg(LOG_DEFAULT, LVL_DEBUG, "start_driver(drv=\"%s\")", drv->name);
|
---|
289 |
|
---|
290 | rc = task_spawnl(NULL, drv->binary_path, drv->binary_path, NULL);
|
---|
291 | if (rc != EOK) {
|
---|
292 | log_msg(LOG_DEFAULT, LVL_ERROR, "Spawning driver `%s' (%s) failed: %s.",
|
---|
293 | drv->name, drv->binary_path, str_error(rc));
|
---|
294 | return false;
|
---|
295 | }
|
---|
296 |
|
---|
297 | drv->state = DRIVER_STARTING;
|
---|
298 | return true;
|
---|
299 | }
|
---|
300 |
|
---|
301 | /** Find device driver in the list of device drivers.
|
---|
302 | *
|
---|
303 | * @param drv_list The list of device drivers.
|
---|
304 | * @param drv_name The name of the device driver which is searched.
|
---|
305 | * @return The device driver of the specified name, if it is in the
|
---|
306 | * list, NULL otherwise.
|
---|
307 | */
|
---|
308 | driver_t *find_driver(driver_list_t *drv_list, const char *drv_name)
|
---|
309 | {
|
---|
310 | driver_t *res = NULL;
|
---|
311 |
|
---|
312 | fibril_mutex_lock(&drv_list->drivers_mutex);
|
---|
313 |
|
---|
314 | list_foreach(drv_list->drivers, drivers, driver_t, drv) {
|
---|
315 | if (str_cmp(drv->name, drv_name) == 0) {
|
---|
316 | res = drv;
|
---|
317 | break;
|
---|
318 | }
|
---|
319 | }
|
---|
320 |
|
---|
321 | fibril_mutex_unlock(&drv_list->drivers_mutex);
|
---|
322 |
|
---|
323 | return res;
|
---|
324 | }
|
---|
325 |
|
---|
326 | /** Notify driver about the devices to which it was assigned.
|
---|
327 | *
|
---|
328 | * @param driver The driver to which the devices are passed.
|
---|
329 | */
|
---|
330 | static void pass_devices_to_driver(driver_t *driver, dev_tree_t *tree)
|
---|
331 | {
|
---|
332 | dev_node_t *dev;
|
---|
333 | link_t *link;
|
---|
334 |
|
---|
335 | log_msg(LOG_DEFAULT, LVL_DEBUG, "pass_devices_to_driver(driver=\"%s\")",
|
---|
336 | driver->name);
|
---|
337 |
|
---|
338 | fibril_mutex_lock(&driver->driver_mutex);
|
---|
339 |
|
---|
340 | /*
|
---|
341 | * Go through devices list as long as there is some device
|
---|
342 | * that has not been passed to the driver.
|
---|
343 | */
|
---|
344 | link = driver->devices.head.next;
|
---|
345 | while (link != &driver->devices.head) {
|
---|
346 | dev = list_get_instance(link, dev_node_t, driver_devices);
|
---|
347 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
348 |
|
---|
349 | if (dev->passed_to_driver) {
|
---|
350 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
351 | link = link->next;
|
---|
352 | continue;
|
---|
353 | }
|
---|
354 |
|
---|
355 | log_msg(LOG_DEFAULT, LVL_DEBUG, "pass_devices_to_driver: dev->refcnt=%d\n",
|
---|
356 | (int)atomic_get(&dev->refcnt));
|
---|
357 | dev_add_ref(dev);
|
---|
358 |
|
---|
359 | /*
|
---|
360 | * Unlock to avoid deadlock when adding device
|
---|
361 | * handled by itself.
|
---|
362 | */
|
---|
363 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
364 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
365 |
|
---|
366 | add_device(driver, dev, tree);
|
---|
367 |
|
---|
368 | dev_del_ref(dev);
|
---|
369 |
|
---|
370 | /*
|
---|
371 | * Lock again as we will work with driver's
|
---|
372 | * structure.
|
---|
373 | */
|
---|
374 | fibril_mutex_lock(&driver->driver_mutex);
|
---|
375 |
|
---|
376 | /*
|
---|
377 | * Restart the cycle to go through all devices again.
|
---|
378 | */
|
---|
379 | link = driver->devices.head.next;
|
---|
380 | }
|
---|
381 |
|
---|
382 | /*
|
---|
383 | * Once we passed all devices to the driver, we need to mark the
|
---|
384 | * driver as running.
|
---|
385 | * It is vital to do it here and inside critical section.
|
---|
386 | *
|
---|
387 | * If we would change the state earlier, other devices added to
|
---|
388 | * the driver would be added to the device list and started
|
---|
389 | * immediately and possibly started here as well.
|
---|
390 | */
|
---|
391 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Driver `%s' enters running state.", driver->name);
|
---|
392 | driver->state = DRIVER_RUNNING;
|
---|
393 |
|
---|
394 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
395 | }
|
---|
396 |
|
---|
397 | /** Finish the initialization of a driver after it has succesfully started
|
---|
398 | * and after it has registered itself by the device manager.
|
---|
399 | *
|
---|
400 | * Pass devices formerly matched to the driver to the driver and remember the
|
---|
401 | * driver is running and fully functional now.
|
---|
402 | *
|
---|
403 | * @param driver The driver which registered itself as running by the
|
---|
404 | * device manager.
|
---|
405 | */
|
---|
406 | void initialize_running_driver(driver_t *driver, dev_tree_t *tree)
|
---|
407 | {
|
---|
408 | log_msg(LOG_DEFAULT, LVL_DEBUG, "initialize_running_driver(driver=\"%s\")",
|
---|
409 | driver->name);
|
---|
410 |
|
---|
411 | /*
|
---|
412 | * Pass devices which have been already assigned to the driver to the
|
---|
413 | * driver.
|
---|
414 | */
|
---|
415 | pass_devices_to_driver(driver, tree);
|
---|
416 | }
|
---|
417 |
|
---|
418 | /** Initialize device driver structure.
|
---|
419 | *
|
---|
420 | * @param drv The device driver structure.
|
---|
421 | */
|
---|
422 | void init_driver(driver_t *drv)
|
---|
423 | {
|
---|
424 | assert(drv != NULL);
|
---|
425 |
|
---|
426 | memset(drv, 0, sizeof(driver_t));
|
---|
427 | list_initialize(&drv->match_ids.ids);
|
---|
428 | list_initialize(&drv->devices);
|
---|
429 | fibril_mutex_initialize(&drv->driver_mutex);
|
---|
430 | drv->sess = NULL;
|
---|
431 | }
|
---|
432 |
|
---|
433 | /** Device driver structure clean-up.
|
---|
434 | *
|
---|
435 | * @param drv The device driver structure.
|
---|
436 | */
|
---|
437 | void clean_driver(driver_t *drv)
|
---|
438 | {
|
---|
439 | assert(drv != NULL);
|
---|
440 |
|
---|
441 | free(drv->name);
|
---|
442 | free(drv->binary_path);
|
---|
443 |
|
---|
444 | clean_match_ids(&drv->match_ids);
|
---|
445 |
|
---|
446 | init_driver(drv);
|
---|
447 | }
|
---|
448 |
|
---|
449 | /** Delete device driver structure.
|
---|
450 | *
|
---|
451 | * @param drv The device driver structure.
|
---|
452 | */
|
---|
453 | void delete_driver(driver_t *drv)
|
---|
454 | {
|
---|
455 | assert(drv != NULL);
|
---|
456 |
|
---|
457 | clean_driver(drv);
|
---|
458 | free(drv);
|
---|
459 | }
|
---|
460 |
|
---|
461 | /** Find suitable driver for a device and assign the driver to it.
|
---|
462 | *
|
---|
463 | * @param node The device node of the device in the device tree.
|
---|
464 | * @param drivers_list The list of available drivers.
|
---|
465 | * @return True if the suitable driver is found and
|
---|
466 | * successfully assigned to the device, false otherwise.
|
---|
467 | */
|
---|
468 | bool assign_driver(dev_node_t *dev, driver_list_t *drivers_list,
|
---|
469 | dev_tree_t *tree)
|
---|
470 | {
|
---|
471 | assert(dev != NULL);
|
---|
472 | assert(drivers_list != NULL);
|
---|
473 | assert(tree != NULL);
|
---|
474 |
|
---|
475 | /*
|
---|
476 | * Find the driver which is the most suitable for handling this device.
|
---|
477 | */
|
---|
478 | driver_t *drv = find_best_match_driver(drivers_list, dev);
|
---|
479 | if (drv == NULL) {
|
---|
480 | log_msg(LOG_DEFAULT, LVL_ERROR, "No driver found for device `%s'.",
|
---|
481 | dev->pfun->pathname);
|
---|
482 | return false;
|
---|
483 | }
|
---|
484 |
|
---|
485 | /* Attach the driver to the device. */
|
---|
486 | attach_driver(tree, dev, drv);
|
---|
487 |
|
---|
488 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
489 | if (drv->state == DRIVER_NOT_STARTED) {
|
---|
490 | /* Start the driver. */
|
---|
491 | start_driver(drv);
|
---|
492 | }
|
---|
493 | bool is_running = drv->state == DRIVER_RUNNING;
|
---|
494 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
495 |
|
---|
496 | /* Notify the driver about the new device. */
|
---|
497 | if (is_running)
|
---|
498 | add_device(drv, dev, tree);
|
---|
499 |
|
---|
500 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
501 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
502 |
|
---|
503 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
504 | if (dev->pfun != NULL) {
|
---|
505 | dev->pfun->state = FUN_ON_LINE;
|
---|
506 | }
|
---|
507 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
508 | return true;
|
---|
509 | }
|
---|
510 |
|
---|
511 | /** Pass a device to running driver.
|
---|
512 | *
|
---|
513 | * @param drv The driver's structure.
|
---|
514 | * @param node The device's node in the device tree.
|
---|
515 | */
|
---|
516 | void add_device(driver_t *drv, dev_node_t *dev, dev_tree_t *tree)
|
---|
517 | {
|
---|
518 | /*
|
---|
519 | * We do not expect to have driver's mutex locked as we do not
|
---|
520 | * access any structures that would affect driver_t.
|
---|
521 | */
|
---|
522 | log_msg(LOG_DEFAULT, LVL_DEBUG, "add_device(drv=\"%s\", dev=\"%s\")",
|
---|
523 | drv->name, dev->pfun->name);
|
---|
524 |
|
---|
525 | /* Send the device to the driver. */
|
---|
526 | devman_handle_t parent_handle;
|
---|
527 | if (dev->pfun) {
|
---|
528 | parent_handle = dev->pfun->handle;
|
---|
529 | } else {
|
---|
530 | parent_handle = 0;
|
---|
531 | }
|
---|
532 |
|
---|
533 | async_exch_t *exch = async_exchange_begin(drv->sess);
|
---|
534 |
|
---|
535 | ipc_call_t answer;
|
---|
536 | aid_t req = async_send_2(exch, DRIVER_DEV_ADD, dev->handle,
|
---|
537 | parent_handle, &answer);
|
---|
538 |
|
---|
539 | /* Send the device name to the driver. */
|
---|
540 | sysarg_t rc = async_data_write_start(exch, dev->pfun->name,
|
---|
541 | str_size(dev->pfun->name) + 1);
|
---|
542 |
|
---|
543 | async_exchange_end(exch);
|
---|
544 |
|
---|
545 | if (rc != EOK) {
|
---|
546 | /* TODO handle error */
|
---|
547 | }
|
---|
548 |
|
---|
549 | /* Wait for answer from the driver. */
|
---|
550 | async_wait_for(req, &rc);
|
---|
551 |
|
---|
552 | switch(rc) {
|
---|
553 | case EOK:
|
---|
554 | dev->state = DEVICE_USABLE;
|
---|
555 | break;
|
---|
556 | case ENOENT:
|
---|
557 | dev->state = DEVICE_NOT_PRESENT;
|
---|
558 | break;
|
---|
559 | default:
|
---|
560 | dev->state = DEVICE_INVALID;
|
---|
561 | break;
|
---|
562 | }
|
---|
563 |
|
---|
564 | dev->passed_to_driver = true;
|
---|
565 |
|
---|
566 | return;
|
---|
567 | }
|
---|
568 |
|
---|
569 | int driver_dev_remove(dev_tree_t *tree, dev_node_t *dev)
|
---|
570 | {
|
---|
571 | async_exch_t *exch;
|
---|
572 | sysarg_t retval;
|
---|
573 | driver_t *drv;
|
---|
574 | devman_handle_t handle;
|
---|
575 |
|
---|
576 | assert(dev != NULL);
|
---|
577 |
|
---|
578 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_remove(%p)", dev);
|
---|
579 |
|
---|
580 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
581 | drv = dev->drv;
|
---|
582 | handle = dev->handle;
|
---|
583 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
584 |
|
---|
585 | exch = async_exchange_begin(drv->sess);
|
---|
586 | retval = async_req_1_0(exch, DRIVER_DEV_REMOVE, handle);
|
---|
587 | async_exchange_end(exch);
|
---|
588 |
|
---|
589 | return retval;
|
---|
590 | }
|
---|
591 |
|
---|
592 | int driver_dev_gone(dev_tree_t *tree, dev_node_t *dev)
|
---|
593 | {
|
---|
594 | async_exch_t *exch;
|
---|
595 | sysarg_t retval;
|
---|
596 | driver_t *drv;
|
---|
597 | devman_handle_t handle;
|
---|
598 |
|
---|
599 | assert(dev != NULL);
|
---|
600 |
|
---|
601 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_gone(%p)", dev);
|
---|
602 |
|
---|
603 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
604 | drv = dev->drv;
|
---|
605 | handle = dev->handle;
|
---|
606 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
607 |
|
---|
608 | exch = async_exchange_begin(drv->sess);
|
---|
609 | retval = async_req_1_0(exch, DRIVER_DEV_GONE, handle);
|
---|
610 | async_exchange_end(exch);
|
---|
611 |
|
---|
612 | return retval;
|
---|
613 | }
|
---|
614 |
|
---|
615 | int driver_fun_online(dev_tree_t *tree, fun_node_t *fun)
|
---|
616 | {
|
---|
617 | async_exch_t *exch;
|
---|
618 | sysarg_t retval;
|
---|
619 | driver_t *drv;
|
---|
620 | devman_handle_t handle;
|
---|
621 |
|
---|
622 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_fun_online(%p)", fun);
|
---|
623 |
|
---|
624 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
625 |
|
---|
626 | if (fun->dev == NULL) {
|
---|
627 | /* XXX root function? */
|
---|
628 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
629 | return EINVAL;
|
---|
630 | }
|
---|
631 |
|
---|
632 | drv = fun->dev->drv;
|
---|
633 | handle = fun->handle;
|
---|
634 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
635 |
|
---|
636 | exch = async_exchange_begin(drv->sess);
|
---|
637 | retval = async_req_1_0(exch, DRIVER_FUN_ONLINE, handle);
|
---|
638 | loc_exchange_end(exch);
|
---|
639 |
|
---|
640 | return retval;
|
---|
641 | }
|
---|
642 |
|
---|
643 | int driver_fun_offline(dev_tree_t *tree, fun_node_t *fun)
|
---|
644 | {
|
---|
645 | async_exch_t *exch;
|
---|
646 | sysarg_t retval;
|
---|
647 | driver_t *drv;
|
---|
648 | devman_handle_t handle;
|
---|
649 |
|
---|
650 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_fun_offline(%p)", fun);
|
---|
651 |
|
---|
652 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
653 | if (fun->dev == NULL) {
|
---|
654 | /* XXX root function? */
|
---|
655 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
656 | return EINVAL;
|
---|
657 | }
|
---|
658 |
|
---|
659 | drv = fun->dev->drv;
|
---|
660 | handle = fun->handle;
|
---|
661 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
662 |
|
---|
663 | exch = async_exchange_begin(drv->sess);
|
---|
664 | retval = async_req_1_0(exch, DRIVER_FUN_OFFLINE, handle);
|
---|
665 | loc_exchange_end(exch);
|
---|
666 |
|
---|
667 | return retval;
|
---|
668 |
|
---|
669 | }
|
---|
670 |
|
---|
671 | /** @}
|
---|
672 | */
|
---|