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