1 | /*
|
---|
2 | * Copyright (c) 2018 Jiri Svoboda
|
---|
3 | * Copyright (c) 2010 Lenka Trochtova
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup devman
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 |
|
---|
34 | #include <dirent.h>
|
---|
35 | #include <errno.h>
|
---|
36 | #include <fcntl.h>
|
---|
37 | #include <io/log.h>
|
---|
38 | #include <vfs/vfs.h>
|
---|
39 | #include <loc.h>
|
---|
40 | #include <stdio.h>
|
---|
41 | #include <str_error.h>
|
---|
42 | #include <sys/stat.h>
|
---|
43 | #include <sysman/ctl.h>
|
---|
44 | #include <task.h>
|
---|
45 |
|
---|
46 | #include "dev.h"
|
---|
47 | #include "devman.h"
|
---|
48 | #include "driver.h"
|
---|
49 | #include "fun.h"
|
---|
50 | #include "match.h"
|
---|
51 | #include "main.h"
|
---|
52 |
|
---|
53 | static errno_t driver_reassign_fibril(void *);
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Initialize the list of device driver's.
|
---|
57 | *
|
---|
58 | * @param drv_list the list of device driver's.
|
---|
59 | *
|
---|
60 | */
|
---|
61 | void init_driver_list(driver_list_t *drv_list)
|
---|
62 | {
|
---|
63 | assert(drv_list != NULL);
|
---|
64 |
|
---|
65 | list_initialize(&drv_list->drivers);
|
---|
66 | fibril_mutex_initialize(&drv_list->drivers_mutex);
|
---|
67 | drv_list->next_handle = 1;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /** Allocate and initialize a new driver structure.
|
---|
71 | *
|
---|
72 | * @return Driver structure.
|
---|
73 | */
|
---|
74 | driver_t *create_driver(void)
|
---|
75 | {
|
---|
76 | driver_t *res = malloc(sizeof(driver_t));
|
---|
77 | if (res != NULL)
|
---|
78 | init_driver(res);
|
---|
79 | return res;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /** Add a driver to the list of drivers.
|
---|
83 | *
|
---|
84 | * @param drivers_list List of drivers.
|
---|
85 | * @param drv Driver structure.
|
---|
86 | */
|
---|
87 | void add_driver(driver_list_t *drivers_list, driver_t *drv)
|
---|
88 | {
|
---|
89 | fibril_mutex_lock(&drivers_list->drivers_mutex);
|
---|
90 | list_append(&drv->drivers, &drivers_list->drivers);
|
---|
91 | drv->handle = drivers_list->next_handle++;
|
---|
92 | fibril_mutex_unlock(&drivers_list->drivers_mutex);
|
---|
93 |
|
---|
94 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Driver `%s' was added to the list of available "
|
---|
95 | "drivers.", drv->name);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Get information about a driver.
|
---|
100 | *
|
---|
101 | * Each driver has its own directory in the base directory.
|
---|
102 | * The name of the driver's directory is the same as the name of the driver.
|
---|
103 | * The driver's directory contains driver's binary (named as the driver without
|
---|
104 | * extension) and the configuration file with match ids for device-to-driver
|
---|
105 | * matching (named as the driver with a special extension).
|
---|
106 | *
|
---|
107 | * This function searches for the driver's directory and containing
|
---|
108 | * configuration files. If all the files needed are found, they are parsed and
|
---|
109 | * the information about the driver is stored in the driver's structure.
|
---|
110 | *
|
---|
111 | * @param base_path The base directory, in which we look for driver's
|
---|
112 | * subdirectory.
|
---|
113 | * @param name The name of the driver.
|
---|
114 | * @param drv The driver structure to fill information in.
|
---|
115 | *
|
---|
116 | * @return True on success, false otherwise.
|
---|
117 | */
|
---|
118 | bool get_driver_info(const char *base_path, const char *name, driver_t *drv)
|
---|
119 | {
|
---|
120 | log_msg(LOG_DEFAULT, LVL_DEBUG, "get_driver_info(base_path=\"%s\", name=\"%s\")",
|
---|
121 | base_path, name);
|
---|
122 |
|
---|
123 | assert(base_path != NULL && name != NULL && drv != NULL);
|
---|
124 |
|
---|
125 | bool suc = false;
|
---|
126 | char *match_path = NULL;
|
---|
127 | size_t name_size = 0;
|
---|
128 |
|
---|
129 | /* Read the list of match ids from the driver's configuration file. */
|
---|
130 | match_path = get_abs_path(base_path, name, MATCH_EXT);
|
---|
131 | if (match_path == NULL)
|
---|
132 | goto cleanup;
|
---|
133 |
|
---|
134 | if (!read_match_ids(match_path, &drv->match_ids))
|
---|
135 | goto cleanup;
|
---|
136 |
|
---|
137 | /* Allocate and fill driver's name. */
|
---|
138 | name_size = str_size(name) + 1;
|
---|
139 | drv->name = malloc(name_size);
|
---|
140 | if (drv->name == NULL)
|
---|
141 | goto cleanup;
|
---|
142 | str_cpy(drv->name, name_size, name);
|
---|
143 |
|
---|
144 | suc = true;
|
---|
145 |
|
---|
146 | cleanup:
|
---|
147 | if (!suc) {
|
---|
148 | free(drv->name);
|
---|
149 | /* Set the driver structure to the default state. */
|
---|
150 | init_driver(drv);
|
---|
151 | }
|
---|
152 |
|
---|
153 | free(match_path);
|
---|
154 |
|
---|
155 | return suc;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /** Lookup drivers in the directory.
|
---|
159 | *
|
---|
160 | * @param drivers_list The list of available drivers.
|
---|
161 | * @param dir_path The path to the directory where we search for drivers.
|
---|
162 | * @return Number of drivers which were found.
|
---|
163 | */
|
---|
164 | int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path)
|
---|
165 | {
|
---|
166 | log_msg(LOG_DEFAULT, LVL_DEBUG, "lookup_available_drivers(dir=\"%s\")", dir_path);
|
---|
167 |
|
---|
168 | int drv_cnt = 0;
|
---|
169 | DIR *dir = NULL;
|
---|
170 | struct dirent *diren;
|
---|
171 |
|
---|
172 | dir = opendir(dir_path);
|
---|
173 |
|
---|
174 | if (dir != NULL) {
|
---|
175 | driver_t *drv = create_driver();
|
---|
176 | while ((diren = readdir(dir))) {
|
---|
177 | if (get_driver_info(dir_path, diren->d_name, drv)) {
|
---|
178 | add_driver(drivers_list, drv);
|
---|
179 | drv_cnt++;
|
---|
180 | drv = create_driver();
|
---|
181 | }
|
---|
182 | }
|
---|
183 | delete_driver(drv);
|
---|
184 | closedir(dir);
|
---|
185 | }
|
---|
186 |
|
---|
187 | return drv_cnt;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /** Get name of unit that represents the driver
|
---|
191 | *
|
---|
192 | * @param[in] drv
|
---|
193 | * @param[out] unit_name_ptr should be free'd after use, not touched on fail
|
---|
194 | *
|
---|
195 | * @return EOK on success
|
---|
196 | * @return ENOMEM
|
---|
197 | */
|
---|
198 | errno_t driver_unit_name(driver_t *drv, char **unit_name_ptr)
|
---|
199 | {
|
---|
200 | char *unit_name = NULL;
|
---|
201 | asprintf(&unit_name, "%s%c%s", drv->name, UNIT_NAME_SEPARATOR,
|
---|
202 | UNIT_SVC_TYPE_NAME);
|
---|
203 |
|
---|
204 | if (unit_name == NULL) {
|
---|
205 | return ENOMEM;
|
---|
206 | } else {
|
---|
207 | *unit_name_ptr = unit_name;
|
---|
208 | return EOK;
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | /** Lookup the next best matching driver for a device.
|
---|
213 | *
|
---|
214 | * A match between a device and a driver is found if one of the driver's match
|
---|
215 | * ids match one of the device's match ids. The score of the match is the
|
---|
216 | * product of the driver's and device's score associated with the matching id.
|
---|
217 | * The best matching driver for a device is the driver with the highest score
|
---|
218 | * of the match between the device and the driver.
|
---|
219 | *
|
---|
220 | * If a driver is already assigned to the device (node->drv != NULL),
|
---|
221 | * we look for the next best driver. That is either the next driver with the
|
---|
222 | * same score in the list of drivers, or a driver with the next best score
|
---|
223 | * (greater than zero).
|
---|
224 | *
|
---|
225 | * @param drivers_list The list of drivers, where we look for the driver
|
---|
226 | * suitable for handling the device.
|
---|
227 | * @param node The device node structure of the device.
|
---|
228 | * @return The best matching driver or NULL if no matching driver
|
---|
229 | * is found.
|
---|
230 | */
|
---|
231 | driver_t *find_best_match_driver(driver_list_t *drivers_list, dev_node_t *node)
|
---|
232 | {
|
---|
233 | driver_t *best_drv = NULL;
|
---|
234 | int best_score = 0, score = 0;
|
---|
235 | int cur_score;
|
---|
236 | link_t *link;
|
---|
237 |
|
---|
238 | fibril_mutex_lock(&drivers_list->drivers_mutex);
|
---|
239 |
|
---|
240 | if (node->drv != NULL) {
|
---|
241 | cur_score = get_match_score(node->drv, node);
|
---|
242 |
|
---|
243 | link = list_next(&node->drv->drivers, &drivers_list->drivers);
|
---|
244 |
|
---|
245 | /*
|
---|
246 | * Find next driver with score equal to the current.
|
---|
247 | */
|
---|
248 | while (link != NULL) {
|
---|
249 | driver_t *drv = list_get_instance(link, driver_t,
|
---|
250 | drivers);
|
---|
251 | score = get_match_score(drv, node);
|
---|
252 | if (score == cur_score) {
|
---|
253 | /* Found it */
|
---|
254 | fibril_mutex_unlock(&drivers_list->drivers_mutex);
|
---|
255 | return drv;
|
---|
256 | }
|
---|
257 |
|
---|
258 | link = list_next(link, &drivers_list->drivers);
|
---|
259 | }
|
---|
260 |
|
---|
261 | /* There is no driver with the same score */
|
---|
262 | } else {
|
---|
263 | cur_score = INT_MAX;
|
---|
264 | }
|
---|
265 |
|
---|
266 | /*
|
---|
267 | * Find driver with the next best score
|
---|
268 | */
|
---|
269 | list_foreach(drivers_list->drivers, drivers, driver_t, drv) {
|
---|
270 | score = get_match_score(drv, node);
|
---|
271 | if (score > best_score && score < cur_score) {
|
---|
272 | best_score = score;
|
---|
273 | best_drv = drv;
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | fibril_mutex_unlock(&drivers_list->drivers_mutex);
|
---|
278 | return best_drv;
|
---|
279 | }
|
---|
280 |
|
---|
281 | /** Assign a driver to a device.
|
---|
282 | *
|
---|
283 | * @param tree Device tree
|
---|
284 | * @param dev The device's node in the device tree.
|
---|
285 | * @param drv The driver.
|
---|
286 | */
|
---|
287 | void attach_driver(dev_tree_t *tree, dev_node_t *dev, driver_t *drv)
|
---|
288 | {
|
---|
289 | log_msg(LOG_DEFAULT, LVL_DEBUG, "attach_driver(dev=\"%s\",drv=\"%s\")",
|
---|
290 | dev->pfun->pathname, drv->name);
|
---|
291 |
|
---|
292 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
293 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
294 |
|
---|
295 | dev->drv = drv;
|
---|
296 | dev->passed_to_driver = false;
|
---|
297 | dev->state = DEVICE_NOT_INITIALIZED;
|
---|
298 | list_append(&dev->driver_devices, &drv->devices);
|
---|
299 |
|
---|
300 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
301 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
302 | }
|
---|
303 |
|
---|
304 | /** Detach driver from device.
|
---|
305 | *
|
---|
306 | * @param tree Device tree
|
---|
307 | * @param node The device's node in the device tree.
|
---|
308 | * @param drv The driver.
|
---|
309 | */
|
---|
310 | void detach_driver(dev_tree_t *tree, dev_node_t *dev)
|
---|
311 | {
|
---|
312 | driver_t *drv = dev->drv;
|
---|
313 |
|
---|
314 | assert(drv != NULL);
|
---|
315 |
|
---|
316 | log_msg(LOG_DEFAULT, LVL_DEBUG, "detach_driver(dev=\"%s\",drv=\"%s\")",
|
---|
317 | dev->pfun->pathname, drv->name);
|
---|
318 |
|
---|
319 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
320 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
321 |
|
---|
322 | dev->drv = NULL;
|
---|
323 | list_remove(&dev->driver_devices);
|
---|
324 |
|
---|
325 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
326 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
327 | }
|
---|
328 |
|
---|
329 | /** Start a driver
|
---|
330 | *
|
---|
331 | * @param drv The driver's structure.
|
---|
332 | * @return True if the driver's task is successfully spawned, false
|
---|
333 | * otherwise.
|
---|
334 | */
|
---|
335 | bool start_driver(driver_t *drv)
|
---|
336 | {
|
---|
337 | errno_t rc;
|
---|
338 |
|
---|
339 | assert(fibril_mutex_is_locked(&drv->driver_mutex));
|
---|
340 |
|
---|
341 | log_msg(LOG_DEFAULT, LVL_DEBUG, "start_driver(drv=\"%s\")", drv->name);
|
---|
342 |
|
---|
343 | char *unit_name = NULL;
|
---|
344 | if (driver_unit_name(drv, &unit_name) != EOK) {
|
---|
345 | return false;
|
---|
346 | }
|
---|
347 |
|
---|
348 | /*
|
---|
349 | * Non-blocking asynchronous request to start a driver
|
---|
350 | * FIXME See note about sysman_unit_start non-blocking (asynchronous)
|
---|
351 | * API
|
---|
352 | */
|
---|
353 | int flags = 0;
|
---|
354 | rc = sysman_unit_start(unit_name, flags);
|
---|
355 | free(unit_name);
|
---|
356 |
|
---|
357 | if (rc != EOK) {
|
---|
358 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
359 | "Request to start driver `%s' failed: %s.",
|
---|
360 | drv->name, str_error(rc));
|
---|
361 | return false;
|
---|
362 | }
|
---|
363 |
|
---|
364 | drv->state = DRIVER_STARTING;
|
---|
365 | return true;
|
---|
366 | }
|
---|
367 |
|
---|
368 | /** Stop a driver
|
---|
369 | *
|
---|
370 | * @param drv The driver's structure.
|
---|
371 | * @return True if the driver's task is successfully spawned, false
|
---|
372 | * otherwise.
|
---|
373 | */
|
---|
374 | errno_t stop_driver(driver_t *drv)
|
---|
375 | {
|
---|
376 | async_exch_t *exch;
|
---|
377 | errno_t retval;
|
---|
378 |
|
---|
379 | log_msg(LOG_DEFAULT, LVL_DEBUG, "stop_driver(drv=\"%s\")", drv->name);
|
---|
380 |
|
---|
381 | exch = async_exchange_begin(drv->sess);
|
---|
382 | retval = async_req_0_0(exch, DRIVER_STOP);
|
---|
383 | loc_exchange_end(exch);
|
---|
384 |
|
---|
385 | if (retval != EOK)
|
---|
386 | return retval;
|
---|
387 |
|
---|
388 | drv->state = DRIVER_NOT_STARTED;
|
---|
389 | async_hangup(drv->sess);
|
---|
390 | drv->sess = NULL;
|
---|
391 | return EOK;
|
---|
392 | }
|
---|
393 |
|
---|
394 | /** Find device driver by handle.
|
---|
395 | *
|
---|
396 | * @param drv_list The list of device drivers
|
---|
397 | * @param handle Driver handle
|
---|
398 | * @return The device driver, if it is in the list,
|
---|
399 | * NULL otherwise.
|
---|
400 | */
|
---|
401 | driver_t *driver_find(driver_list_t *drv_list, devman_handle_t handle)
|
---|
402 | {
|
---|
403 | driver_t *res = NULL;
|
---|
404 |
|
---|
405 | fibril_mutex_lock(&drv_list->drivers_mutex);
|
---|
406 |
|
---|
407 | list_foreach(drv_list->drivers, drivers, driver_t, drv) {
|
---|
408 | if (drv->handle == handle) {
|
---|
409 | res = drv;
|
---|
410 | break;
|
---|
411 | }
|
---|
412 | }
|
---|
413 |
|
---|
414 | fibril_mutex_unlock(&drv_list->drivers_mutex);
|
---|
415 |
|
---|
416 | return res;
|
---|
417 | }
|
---|
418 |
|
---|
419 | /** Find device driver by name.
|
---|
420 | *
|
---|
421 | * @param drv_list The list of device drivers.
|
---|
422 | * @param drv_name The name of the device driver which is searched.
|
---|
423 | * @return The device driver of the specified name, if it is in the
|
---|
424 | * list, NULL otherwise.
|
---|
425 | */
|
---|
426 | driver_t *driver_find_by_name(driver_list_t *drv_list, const char *drv_name)
|
---|
427 | {
|
---|
428 | driver_t *res = NULL;
|
---|
429 |
|
---|
430 | fibril_mutex_lock(&drv_list->drivers_mutex);
|
---|
431 |
|
---|
432 | list_foreach(drv_list->drivers, drivers, driver_t, drv) {
|
---|
433 | if (str_cmp(drv->name, drv_name) == 0) {
|
---|
434 | res = drv;
|
---|
435 | break;
|
---|
436 | }
|
---|
437 | }
|
---|
438 |
|
---|
439 | fibril_mutex_unlock(&drv_list->drivers_mutex);
|
---|
440 |
|
---|
441 | return res;
|
---|
442 | }
|
---|
443 |
|
---|
444 | /** Notify driver about the devices to which it was assigned.
|
---|
445 | *
|
---|
446 | * @param driver The driver to which the devices are passed.
|
---|
447 | */
|
---|
448 | static void pass_devices_to_driver(driver_t *driver, dev_tree_t *tree)
|
---|
449 | {
|
---|
450 | dev_node_t *dev;
|
---|
451 | link_t *link;
|
---|
452 |
|
---|
453 | log_msg(LOG_DEFAULT, LVL_DEBUG, "pass_devices_to_driver(driver=\"%s\")",
|
---|
454 | driver->name);
|
---|
455 |
|
---|
456 | fibril_mutex_lock(&driver->driver_mutex);
|
---|
457 |
|
---|
458 | /*
|
---|
459 | * Go through devices list as long as there is some device
|
---|
460 | * that has not been passed to the driver.
|
---|
461 | */
|
---|
462 | link = driver->devices.head.next;
|
---|
463 | while (link != &driver->devices.head) {
|
---|
464 | dev = list_get_instance(link, dev_node_t, driver_devices);
|
---|
465 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
466 |
|
---|
467 | if (dev->passed_to_driver) {
|
---|
468 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
469 | link = link->next;
|
---|
470 | continue;
|
---|
471 | }
|
---|
472 |
|
---|
473 | dev_add_ref(dev);
|
---|
474 |
|
---|
475 | /*
|
---|
476 | * Unlock to avoid deadlock when adding device
|
---|
477 | * handled by itself.
|
---|
478 | */
|
---|
479 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
480 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
481 |
|
---|
482 | add_device(driver, dev, tree);
|
---|
483 |
|
---|
484 | /* Device probe failed, need to try next best driver */
|
---|
485 | if (dev->state == DEVICE_NOT_PRESENT) {
|
---|
486 | fibril_mutex_lock(&driver->driver_mutex);
|
---|
487 | list_remove(&dev->driver_devices);
|
---|
488 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
489 | /* Give an extra reference to driver_reassign_fibril */
|
---|
490 | dev_add_ref(dev);
|
---|
491 | fid_t fid = fibril_create(driver_reassign_fibril, dev);
|
---|
492 | if (fid == 0) {
|
---|
493 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
494 | "Error creating fibril to assign driver.");
|
---|
495 | dev_del_ref(dev);
|
---|
496 | }
|
---|
497 | fibril_add_ready(fid);
|
---|
498 | }
|
---|
499 |
|
---|
500 | dev_del_ref(dev);
|
---|
501 |
|
---|
502 | /*
|
---|
503 | * Lock again as we will work with driver's
|
---|
504 | * structure.
|
---|
505 | */
|
---|
506 | fibril_mutex_lock(&driver->driver_mutex);
|
---|
507 |
|
---|
508 | /*
|
---|
509 | * Restart the cycle to go through all devices again.
|
---|
510 | */
|
---|
511 | link = driver->devices.head.next;
|
---|
512 | }
|
---|
513 |
|
---|
514 | /*
|
---|
515 | * Once we passed all devices to the driver, we need to mark the
|
---|
516 | * driver as running.
|
---|
517 | * It is vital to do it here and inside critical section.
|
---|
518 | *
|
---|
519 | * If we would change the state earlier, other devices added to
|
---|
520 | * the driver would be added to the device list and started
|
---|
521 | * immediately and possibly started here as well.
|
---|
522 | */
|
---|
523 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Driver `%s' enters running state.", driver->name);
|
---|
524 | driver->state = DRIVER_RUNNING;
|
---|
525 |
|
---|
526 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
527 | }
|
---|
528 |
|
---|
529 | /** Finish the initialization of a driver after it has succesfully started
|
---|
530 | * and after it has registered itself by the device manager.
|
---|
531 | *
|
---|
532 | * Pass devices formerly matched to the driver to the driver and remember the
|
---|
533 | * driver is running and fully functional now.
|
---|
534 | *
|
---|
535 | * @param driver The driver which registered itself as running by the
|
---|
536 | * device manager.
|
---|
537 | */
|
---|
538 | void initialize_running_driver(driver_t *driver, dev_tree_t *tree)
|
---|
539 | {
|
---|
540 | log_msg(LOG_DEFAULT, LVL_DEBUG, "initialize_running_driver(driver=\"%s\")",
|
---|
541 | driver->name);
|
---|
542 |
|
---|
543 | /*
|
---|
544 | * Pass devices which have been already assigned to the driver to the
|
---|
545 | * driver.
|
---|
546 | */
|
---|
547 | pass_devices_to_driver(driver, tree);
|
---|
548 | }
|
---|
549 |
|
---|
550 | /** Initialize device driver structure.
|
---|
551 | *
|
---|
552 | * @param drv The device driver structure.
|
---|
553 | */
|
---|
554 | void init_driver(driver_t *drv)
|
---|
555 | {
|
---|
556 | assert(drv != NULL);
|
---|
557 |
|
---|
558 | memset(drv, 0, sizeof(driver_t));
|
---|
559 | list_initialize(&drv->match_ids.ids);
|
---|
560 | list_initialize(&drv->devices);
|
---|
561 | fibril_mutex_initialize(&drv->driver_mutex);
|
---|
562 | drv->sess = NULL;
|
---|
563 | }
|
---|
564 |
|
---|
565 | /** Device driver structure clean-up.
|
---|
566 | *
|
---|
567 | * @param drv The device driver structure.
|
---|
568 | */
|
---|
569 | void clean_driver(driver_t *drv)
|
---|
570 | {
|
---|
571 | assert(drv != NULL);
|
---|
572 |
|
---|
573 | free(drv->name);
|
---|
574 |
|
---|
575 | clean_match_ids(&drv->match_ids);
|
---|
576 |
|
---|
577 | init_driver(drv);
|
---|
578 | }
|
---|
579 |
|
---|
580 | /** Delete device driver structure.
|
---|
581 | *
|
---|
582 | * @param drv The device driver structure.
|
---|
583 | */
|
---|
584 | void delete_driver(driver_t *drv)
|
---|
585 | {
|
---|
586 | assert(drv != NULL);
|
---|
587 |
|
---|
588 | clean_driver(drv);
|
---|
589 | free(drv);
|
---|
590 | }
|
---|
591 |
|
---|
592 | /** Find suitable driver for a device and assign the driver to it.
|
---|
593 | *
|
---|
594 | * @param dev The device node of the device in the device tree.
|
---|
595 | * @param drivers_list The list of available drivers.
|
---|
596 | * @return True if the suitable driver is found and
|
---|
597 | * successfully assigned to the device, false otherwise.
|
---|
598 | */
|
---|
599 | bool assign_driver(dev_node_t *dev, driver_list_t *drivers_list,
|
---|
600 | dev_tree_t *tree)
|
---|
601 | {
|
---|
602 | driver_t *drv;
|
---|
603 |
|
---|
604 | assert(dev != NULL);
|
---|
605 | assert(drivers_list != NULL);
|
---|
606 | assert(tree != NULL);
|
---|
607 |
|
---|
608 | /*
|
---|
609 | * Find the next best driver for this device.
|
---|
610 | */
|
---|
611 | again:
|
---|
612 | drv = find_best_match_driver(drivers_list, dev);
|
---|
613 | if (drv == NULL) {
|
---|
614 | log_msg(LOG_DEFAULT, LVL_ERROR, "No driver found for device `%s'.",
|
---|
615 | dev->pfun->pathname);
|
---|
616 | return false;
|
---|
617 | }
|
---|
618 |
|
---|
619 | /* Attach the driver to the device. */
|
---|
620 | attach_driver(tree, dev, drv);
|
---|
621 |
|
---|
622 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
623 | if (drv->state == DRIVER_NOT_STARTED) {
|
---|
624 | /* Start the driver. */
|
---|
625 | start_driver(drv);
|
---|
626 | }
|
---|
627 | bool is_running = drv->state == DRIVER_RUNNING;
|
---|
628 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
629 |
|
---|
630 | /* Notify the driver about the new device. */
|
---|
631 | if (is_running) {
|
---|
632 | add_device(drv, dev, tree);
|
---|
633 |
|
---|
634 | /* If the device probe failed, need to try next available driver */
|
---|
635 | if (dev->state == DEVICE_NOT_PRESENT)
|
---|
636 | goto again;
|
---|
637 | }
|
---|
638 |
|
---|
639 | fibril_mutex_lock(&drv->driver_mutex);
|
---|
640 | fibril_mutex_unlock(&drv->driver_mutex);
|
---|
641 |
|
---|
642 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
643 | if (dev->pfun != NULL) {
|
---|
644 | dev->pfun->state = FUN_ON_LINE;
|
---|
645 | }
|
---|
646 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
647 | return true;
|
---|
648 | }
|
---|
649 |
|
---|
650 | /** Pass a device to running driver.
|
---|
651 | *
|
---|
652 | * @param drv The driver's structure.
|
---|
653 | * @param dev The device's node in the device tree.
|
---|
654 | */
|
---|
655 | void add_device(driver_t *drv, dev_node_t *dev, dev_tree_t *tree)
|
---|
656 | {
|
---|
657 | /*
|
---|
658 | * We do not expect to have driver's mutex locked as we do not
|
---|
659 | * access any structures that would affect driver_t.
|
---|
660 | */
|
---|
661 | log_msg(LOG_DEFAULT, LVL_DEBUG, "add_device(drv=\"%s\", dev=\"%s\")",
|
---|
662 | drv->name, dev->pfun->name);
|
---|
663 |
|
---|
664 | /* Send the device to the driver. */
|
---|
665 | devman_handle_t parent_handle;
|
---|
666 | if (dev->pfun) {
|
---|
667 | parent_handle = dev->pfun->handle;
|
---|
668 | } else {
|
---|
669 | parent_handle = 0;
|
---|
670 | }
|
---|
671 |
|
---|
672 | async_exch_t *exch = async_exchange_begin(drv->sess);
|
---|
673 |
|
---|
674 | ipc_call_t answer;
|
---|
675 | aid_t req = async_send_2(exch, DRIVER_DEV_ADD, dev->handle,
|
---|
676 | parent_handle, &answer);
|
---|
677 |
|
---|
678 | /* Send the device name to the driver. */
|
---|
679 | errno_t rc = async_data_write_start(exch, dev->pfun->name,
|
---|
680 | str_size(dev->pfun->name) + 1);
|
---|
681 |
|
---|
682 | async_exchange_end(exch);
|
---|
683 |
|
---|
684 | if (rc != EOK) {
|
---|
685 | async_forget(req);
|
---|
686 | } else {
|
---|
687 | /* Wait for answer from the driver. */
|
---|
688 | async_wait_for(req, &rc);
|
---|
689 | }
|
---|
690 |
|
---|
691 | switch (rc) {
|
---|
692 | case EOK:
|
---|
693 | dev->state = DEVICE_USABLE;
|
---|
694 | break;
|
---|
695 | case ENOENT:
|
---|
696 | dev->state = DEVICE_NOT_PRESENT;
|
---|
697 | break;
|
---|
698 | default:
|
---|
699 | dev->state = DEVICE_INVALID;
|
---|
700 | break;
|
---|
701 | }
|
---|
702 |
|
---|
703 | dev->passed_to_driver = true;
|
---|
704 | }
|
---|
705 |
|
---|
706 | errno_t driver_dev_remove(dev_tree_t *tree, dev_node_t *dev)
|
---|
707 | {
|
---|
708 | async_exch_t *exch;
|
---|
709 | errno_t retval;
|
---|
710 | driver_t *drv;
|
---|
711 | devman_handle_t handle;
|
---|
712 |
|
---|
713 | assert(dev != NULL);
|
---|
714 |
|
---|
715 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_remove(%p)", dev);
|
---|
716 |
|
---|
717 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
718 | drv = dev->drv;
|
---|
719 | handle = dev->handle;
|
---|
720 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
721 |
|
---|
722 | exch = async_exchange_begin(drv->sess);
|
---|
723 | retval = async_req_1_0(exch, DRIVER_DEV_REMOVE, handle);
|
---|
724 | async_exchange_end(exch);
|
---|
725 |
|
---|
726 | return retval;
|
---|
727 | }
|
---|
728 |
|
---|
729 | errno_t driver_dev_gone(dev_tree_t *tree, dev_node_t *dev)
|
---|
730 | {
|
---|
731 | async_exch_t *exch;
|
---|
732 | errno_t retval;
|
---|
733 | driver_t *drv;
|
---|
734 | devman_handle_t handle;
|
---|
735 |
|
---|
736 | assert(dev != NULL);
|
---|
737 |
|
---|
738 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_dev_gone(%p)", dev);
|
---|
739 |
|
---|
740 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
741 | drv = dev->drv;
|
---|
742 | handle = dev->handle;
|
---|
743 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
744 |
|
---|
745 | exch = async_exchange_begin(drv->sess);
|
---|
746 | retval = async_req_1_0(exch, DRIVER_DEV_GONE, handle);
|
---|
747 | async_exchange_end(exch);
|
---|
748 |
|
---|
749 | return retval;
|
---|
750 | }
|
---|
751 |
|
---|
752 | errno_t driver_fun_online(dev_tree_t *tree, fun_node_t *fun)
|
---|
753 | {
|
---|
754 | async_exch_t *exch;
|
---|
755 | errno_t retval;
|
---|
756 | driver_t *drv;
|
---|
757 | devman_handle_t handle;
|
---|
758 |
|
---|
759 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_fun_online(%p)", fun);
|
---|
760 |
|
---|
761 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
762 |
|
---|
763 | if (fun->dev == NULL) {
|
---|
764 | /* XXX root function? */
|
---|
765 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
766 | return EINVAL;
|
---|
767 | }
|
---|
768 |
|
---|
769 | drv = fun->dev->drv;
|
---|
770 | handle = fun->handle;
|
---|
771 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
772 |
|
---|
773 | exch = async_exchange_begin(drv->sess);
|
---|
774 | retval = async_req_1_0(exch, DRIVER_FUN_ONLINE, handle);
|
---|
775 | loc_exchange_end(exch);
|
---|
776 |
|
---|
777 | return retval;
|
---|
778 | }
|
---|
779 |
|
---|
780 | errno_t driver_fun_offline(dev_tree_t *tree, fun_node_t *fun)
|
---|
781 | {
|
---|
782 | async_exch_t *exch;
|
---|
783 | errno_t retval;
|
---|
784 | driver_t *drv;
|
---|
785 | devman_handle_t handle;
|
---|
786 |
|
---|
787 | log_msg(LOG_DEFAULT, LVL_DEBUG, "driver_fun_offline(%p)", fun);
|
---|
788 |
|
---|
789 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
790 | if (fun->dev == NULL) {
|
---|
791 | /* XXX root function? */
|
---|
792 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
793 | return EINVAL;
|
---|
794 | }
|
---|
795 |
|
---|
796 | drv = fun->dev->drv;
|
---|
797 | handle = fun->handle;
|
---|
798 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
799 |
|
---|
800 | exch = async_exchange_begin(drv->sess);
|
---|
801 | retval = async_req_1_0(exch, DRIVER_FUN_OFFLINE, handle);
|
---|
802 | loc_exchange_end(exch);
|
---|
803 |
|
---|
804 | return retval;
|
---|
805 |
|
---|
806 | }
|
---|
807 |
|
---|
808 | /** Get list of registered drivers. */
|
---|
809 | errno_t driver_get_list(driver_list_t *driver_list, devman_handle_t *hdl_buf,
|
---|
810 | size_t buf_size, size_t *act_size)
|
---|
811 | {
|
---|
812 | size_t act_cnt;
|
---|
813 | size_t buf_cnt;
|
---|
814 |
|
---|
815 | fibril_mutex_lock(&driver_list->drivers_mutex);
|
---|
816 |
|
---|
817 | buf_cnt = buf_size / sizeof(devman_handle_t);
|
---|
818 |
|
---|
819 | act_cnt = list_count(&driver_list->drivers);
|
---|
820 | *act_size = act_cnt * sizeof(devman_handle_t);
|
---|
821 |
|
---|
822 | if (buf_size % sizeof(devman_handle_t) != 0) {
|
---|
823 | fibril_mutex_unlock(&driver_list->drivers_mutex);
|
---|
824 | return EINVAL;
|
---|
825 | }
|
---|
826 |
|
---|
827 | size_t pos = 0;
|
---|
828 | list_foreach(driver_list->drivers, drivers, driver_t, drv) {
|
---|
829 | if (pos < buf_cnt) {
|
---|
830 | hdl_buf[pos] = drv->handle;
|
---|
831 | }
|
---|
832 |
|
---|
833 | pos++;
|
---|
834 | }
|
---|
835 |
|
---|
836 | fibril_mutex_unlock(&driver_list->drivers_mutex);
|
---|
837 | return EOK;
|
---|
838 | }
|
---|
839 |
|
---|
840 | /** Get list of device functions. */
|
---|
841 | errno_t driver_get_devices(driver_t *driver, devman_handle_t *hdl_buf,
|
---|
842 | size_t buf_size, size_t *act_size)
|
---|
843 | {
|
---|
844 | size_t act_cnt;
|
---|
845 | size_t buf_cnt;
|
---|
846 |
|
---|
847 | fibril_mutex_lock(&driver->driver_mutex);
|
---|
848 |
|
---|
849 | buf_cnt = buf_size / sizeof(devman_handle_t);
|
---|
850 |
|
---|
851 | act_cnt = list_count(&driver->devices);
|
---|
852 | *act_size = act_cnt * sizeof(devman_handle_t);
|
---|
853 |
|
---|
854 | if (buf_size % sizeof(devman_handle_t) != 0) {
|
---|
855 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
856 | return EINVAL;
|
---|
857 | }
|
---|
858 |
|
---|
859 | size_t pos = 0;
|
---|
860 | list_foreach(driver->devices, driver_devices, dev_node_t, dev) {
|
---|
861 | if (pos < buf_cnt) {
|
---|
862 | hdl_buf[pos] = dev->handle;
|
---|
863 | }
|
---|
864 |
|
---|
865 | pos++;
|
---|
866 | }
|
---|
867 |
|
---|
868 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
869 | return EOK;
|
---|
870 | }
|
---|
871 |
|
---|
872 | /** Try to find next available driver in a separate fibril.
|
---|
873 | *
|
---|
874 | * @param arg Device node (dev_node_t)
|
---|
875 | */
|
---|
876 | static errno_t driver_reassign_fibril(void *arg)
|
---|
877 | {
|
---|
878 | dev_node_t *dev_node = (dev_node_t *) arg;
|
---|
879 | assign_driver(dev_node, &drivers_list, &device_tree);
|
---|
880 |
|
---|
881 | /* Delete one reference we got from the caller. */
|
---|
882 | dev_del_ref(dev_node);
|
---|
883 | return EOK;
|
---|
884 | }
|
---|
885 |
|
---|
886 | /** @}
|
---|
887 | */
|
---|