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 | /**
|
---|
30 | * @defgroup devman Device manager.
|
---|
31 | * @brief HelenOS device manager.
|
---|
32 | * @{
|
---|
33 | */
|
---|
34 |
|
---|
35 | /** @file
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <inttypes.h>
|
---|
39 | #include <assert.h>
|
---|
40 | #include <ipc/services.h>
|
---|
41 | #include <ns.h>
|
---|
42 | #include <async.h>
|
---|
43 | #include <stdio.h>
|
---|
44 | #include <errno.h>
|
---|
45 | #include <str_error.h>
|
---|
46 | #include <bool.h>
|
---|
47 | #include <fibril_synch.h>
|
---|
48 | #include <stdlib.h>
|
---|
49 | #include <str.h>
|
---|
50 | #include <dirent.h>
|
---|
51 | #include <fcntl.h>
|
---|
52 | #include <sys/stat.h>
|
---|
53 | #include <ctype.h>
|
---|
54 | #include <io/log.h>
|
---|
55 | #include <ipc/devman.h>
|
---|
56 | #include <ipc/driver.h>
|
---|
57 | #include <thread.h>
|
---|
58 | #include <loc.h>
|
---|
59 |
|
---|
60 | #include "devman.h"
|
---|
61 |
|
---|
62 | #define DRIVER_DEFAULT_STORE "/drv"
|
---|
63 |
|
---|
64 | static driver_list_t drivers_list;
|
---|
65 | static dev_tree_t device_tree;
|
---|
66 |
|
---|
67 | static int init_running_drv(void *drv);
|
---|
68 |
|
---|
69 | /** Register running driver. */
|
---|
70 | static driver_t *devman_driver_register(ipc_callid_t callid, ipc_call_t *call)
|
---|
71 | {
|
---|
72 | driver_t *driver = NULL;
|
---|
73 | char *drv_name = NULL;
|
---|
74 |
|
---|
75 | log_msg(LVL_DEBUG, "devman_driver_register");
|
---|
76 |
|
---|
77 | /* Get driver name. */
|
---|
78 | int rc = async_data_write_accept((void **) &drv_name, true, 0, 0, 0, 0);
|
---|
79 | if (rc != EOK) {
|
---|
80 | async_answer_0(callid, rc);
|
---|
81 | return NULL;
|
---|
82 | }
|
---|
83 |
|
---|
84 | log_msg(LVL_DEBUG, "The `%s' driver is trying to register.",
|
---|
85 | drv_name);
|
---|
86 |
|
---|
87 | /* Find driver structure. */
|
---|
88 | driver = find_driver(&drivers_list, drv_name);
|
---|
89 | if (driver == NULL) {
|
---|
90 | log_msg(LVL_ERROR, "No driver named `%s' was found.", drv_name);
|
---|
91 | free(drv_name);
|
---|
92 | drv_name = NULL;
|
---|
93 | async_answer_0(callid, ENOENT);
|
---|
94 | return NULL;
|
---|
95 | }
|
---|
96 |
|
---|
97 | free(drv_name);
|
---|
98 | drv_name = NULL;
|
---|
99 |
|
---|
100 | fibril_mutex_lock(&driver->driver_mutex);
|
---|
101 |
|
---|
102 | if (driver->sess) {
|
---|
103 | /* We already have a connection to the driver. */
|
---|
104 | log_msg(LVL_ERROR, "Driver '%s' already started.\n",
|
---|
105 | driver->name);
|
---|
106 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
107 | async_answer_0(callid, EEXISTS);
|
---|
108 | return NULL;
|
---|
109 | }
|
---|
110 |
|
---|
111 | switch (driver->state) {
|
---|
112 | case DRIVER_NOT_STARTED:
|
---|
113 | /* Somebody started the driver manually. */
|
---|
114 | log_msg(LVL_NOTE, "Driver '%s' started manually.\n",
|
---|
115 | driver->name);
|
---|
116 | driver->state = DRIVER_STARTING;
|
---|
117 | break;
|
---|
118 | case DRIVER_STARTING:
|
---|
119 | /* The expected case */
|
---|
120 | break;
|
---|
121 | case DRIVER_RUNNING:
|
---|
122 | /* Should not happen since we do not have a connected session */
|
---|
123 | assert(false);
|
---|
124 | }
|
---|
125 |
|
---|
126 | /* Create connection to the driver. */
|
---|
127 | log_msg(LVL_DEBUG, "Creating connection to the `%s' driver.",
|
---|
128 | driver->name);
|
---|
129 | driver->sess = async_callback_receive(EXCHANGE_PARALLEL);
|
---|
130 | if (!driver->sess) {
|
---|
131 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
132 | async_answer_0(callid, ENOTSUP);
|
---|
133 | return NULL;
|
---|
134 | }
|
---|
135 | /* FIXME: Work around problem with callback sessions */
|
---|
136 | async_sess_args_set(driver->sess, DRIVER_DEVMAN, 0, 0);
|
---|
137 |
|
---|
138 | log_msg(LVL_NOTE,
|
---|
139 | "The `%s' driver was successfully registered as running.",
|
---|
140 | driver->name);
|
---|
141 |
|
---|
142 | /*
|
---|
143 | * Initialize the driver as running (e.g. pass assigned devices to it)
|
---|
144 | * in a separate fibril; the separate fibril is used to enable the
|
---|
145 | * driver to use devman service during the driver's initialization.
|
---|
146 | */
|
---|
147 | fid_t fid = fibril_create(init_running_drv, driver);
|
---|
148 | if (fid == 0) {
|
---|
149 | log_msg(LVL_ERROR, "Failed to create initialization fibril " \
|
---|
150 | "for driver `%s'.", driver->name);
|
---|
151 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
152 | async_answer_0(callid, ENOMEM);
|
---|
153 | return NULL;
|
---|
154 | }
|
---|
155 |
|
---|
156 | fibril_add_ready(fid);
|
---|
157 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
158 |
|
---|
159 | async_answer_0(callid, EOK);
|
---|
160 | return driver;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /** Receive device match ID from the device's parent driver and add it to the
|
---|
164 | * list of devices match ids.
|
---|
165 | *
|
---|
166 | * @param match_ids The list of the device's match ids.
|
---|
167 | * @return Zero on success, negative error code otherwise.
|
---|
168 | */
|
---|
169 | static int devman_receive_match_id(match_id_list_t *match_ids)
|
---|
170 | {
|
---|
171 | match_id_t *match_id = create_match_id();
|
---|
172 | ipc_callid_t callid;
|
---|
173 | ipc_call_t call;
|
---|
174 | int rc = 0;
|
---|
175 |
|
---|
176 | callid = async_get_call(&call);
|
---|
177 | if (DEVMAN_ADD_MATCH_ID != IPC_GET_IMETHOD(call)) {
|
---|
178 | log_msg(LVL_ERROR,
|
---|
179 | "Invalid protocol when trying to receive match id.");
|
---|
180 | async_answer_0(callid, EINVAL);
|
---|
181 | delete_match_id(match_id);
|
---|
182 | return EINVAL;
|
---|
183 | }
|
---|
184 |
|
---|
185 | if (match_id == NULL) {
|
---|
186 | log_msg(LVL_ERROR, "Failed to allocate match id.");
|
---|
187 | async_answer_0(callid, ENOMEM);
|
---|
188 | return ENOMEM;
|
---|
189 | }
|
---|
190 |
|
---|
191 | async_answer_0(callid, EOK);
|
---|
192 |
|
---|
193 | match_id->score = IPC_GET_ARG1(call);
|
---|
194 |
|
---|
195 | char *match_id_str;
|
---|
196 | rc = async_data_write_accept((void **) &match_id_str, true, 0, 0, 0, 0);
|
---|
197 | match_id->id = match_id_str;
|
---|
198 | if (rc != EOK) {
|
---|
199 | delete_match_id(match_id);
|
---|
200 | log_msg(LVL_ERROR, "Failed to receive match id string: %s.",
|
---|
201 | str_error(rc));
|
---|
202 | return rc;
|
---|
203 | }
|
---|
204 |
|
---|
205 | list_append(&match_id->link, &match_ids->ids);
|
---|
206 |
|
---|
207 | log_msg(LVL_DEBUG, "Received match id `%s', score %d.",
|
---|
208 | match_id->id, match_id->score);
|
---|
209 | return rc;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /** Receive device match IDs from the device's parent driver and add them to the
|
---|
213 | * list of devices match ids.
|
---|
214 | *
|
---|
215 | * @param match_count The number of device's match ids to be received.
|
---|
216 | * @param match_ids The list of the device's match ids.
|
---|
217 | * @return Zero on success, negative error code otherwise.
|
---|
218 | */
|
---|
219 | static int devman_receive_match_ids(sysarg_t match_count,
|
---|
220 | match_id_list_t *match_ids)
|
---|
221 | {
|
---|
222 | int ret = EOK;
|
---|
223 | size_t i;
|
---|
224 |
|
---|
225 | for (i = 0; i < match_count; i++) {
|
---|
226 | if (EOK != (ret = devman_receive_match_id(match_ids)))
|
---|
227 | return ret;
|
---|
228 | }
|
---|
229 | return ret;
|
---|
230 | }
|
---|
231 |
|
---|
232 | static int assign_driver_fibril(void *arg)
|
---|
233 | {
|
---|
234 | dev_node_t *dev_node = (dev_node_t *) arg;
|
---|
235 | assign_driver(dev_node, &drivers_list, &device_tree);
|
---|
236 |
|
---|
237 | /* Delete one reference we got from the caller. */
|
---|
238 | dev_del_ref(dev_node);
|
---|
239 | return EOK;
|
---|
240 | }
|
---|
241 |
|
---|
242 | static int online_function(fun_node_t *fun)
|
---|
243 | {
|
---|
244 | dev_node_t *dev;
|
---|
245 |
|
---|
246 | fibril_rwlock_write_lock(&device_tree.rwlock);
|
---|
247 |
|
---|
248 | if (fun->state == FUN_ON_LINE) {
|
---|
249 | fibril_rwlock_write_unlock(&device_tree.rwlock);
|
---|
250 | log_msg(LVL_WARN, "Function %s is already on line.",
|
---|
251 | fun->pathname);
|
---|
252 | return EOK;
|
---|
253 | }
|
---|
254 |
|
---|
255 | if (fun->ftype == fun_inner) {
|
---|
256 | dev = create_dev_node();
|
---|
257 | if (dev == NULL) {
|
---|
258 | fibril_rwlock_write_unlock(&device_tree.rwlock);
|
---|
259 | return ENOMEM;
|
---|
260 | }
|
---|
261 |
|
---|
262 | insert_dev_node(&device_tree, dev, fun);
|
---|
263 | dev_add_ref(dev);
|
---|
264 | }
|
---|
265 |
|
---|
266 | log_msg(LVL_DEBUG, "devman_add_function(fun=\"%s\")", fun->pathname);
|
---|
267 |
|
---|
268 | if (fun->ftype == fun_inner) {
|
---|
269 | dev = fun->child;
|
---|
270 | assert(dev != NULL);
|
---|
271 |
|
---|
272 | /* Give one reference over to assign_driver_fibril(). */
|
---|
273 | dev_add_ref(dev);
|
---|
274 | /*
|
---|
275 | * Try to find a suitable driver and assign it to the device. We do
|
---|
276 | * not want to block the current fibril that is used for processing
|
---|
277 | * incoming calls: we will launch a separate fibril to handle the
|
---|
278 | * driver assigning. That is because assign_driver can actually include
|
---|
279 | * task spawning which could take some time.
|
---|
280 | */
|
---|
281 | fid_t assign_fibril = fibril_create(assign_driver_fibril, dev);
|
---|
282 | if (assign_fibril == 0) {
|
---|
283 | log_msg(LVL_ERROR, "Failed to create fibril for "
|
---|
284 | "assigning driver.");
|
---|
285 | /* XXX Cleanup */
|
---|
286 | fibril_rwlock_write_unlock(&device_tree.rwlock);
|
---|
287 | return ENOMEM;
|
---|
288 | }
|
---|
289 | fibril_add_ready(assign_fibril);
|
---|
290 | } else {
|
---|
291 | loc_register_tree_function(fun, &device_tree);
|
---|
292 | }
|
---|
293 |
|
---|
294 | fibril_rwlock_write_unlock(&device_tree.rwlock);
|
---|
295 |
|
---|
296 | return EOK;
|
---|
297 | }
|
---|
298 |
|
---|
299 | static int offline_function(fun_node_t *fun)
|
---|
300 | {
|
---|
301 | int rc;
|
---|
302 |
|
---|
303 | fibril_rwlock_write_lock(&device_tree.rwlock);
|
---|
304 |
|
---|
305 | if (fun->state == FUN_OFF_LINE) {
|
---|
306 | fibril_rwlock_write_unlock(&device_tree.rwlock);
|
---|
307 | log_msg(LVL_WARN, "Function %s is already off line.",
|
---|
308 | fun->pathname);
|
---|
309 | return EOK;
|
---|
310 | }
|
---|
311 |
|
---|
312 | if (fun->ftype == fun_inner) {
|
---|
313 | log_msg(LVL_DEBUG, "Offlining inner function %s.",
|
---|
314 | fun->pathname);
|
---|
315 |
|
---|
316 | if (fun->child != NULL) {
|
---|
317 | dev_node_t *dev = fun->child;
|
---|
318 |
|
---|
319 | dev_add_ref(dev);
|
---|
320 | fibril_rwlock_write_unlock(&device_tree.rwlock);
|
---|
321 |
|
---|
322 | /* If device is owned by driver, ask driver to give it up. */
|
---|
323 | if (dev->state == DEVICE_USABLE) {
|
---|
324 | rc = driver_dev_remove(&device_tree, dev);
|
---|
325 | if (rc != EOK) {
|
---|
326 | dev_del_ref(dev);
|
---|
327 | return ENOTSUP;
|
---|
328 | }
|
---|
329 | }
|
---|
330 |
|
---|
331 | /* Verify that driver removed all functions */
|
---|
332 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
333 | if (!list_empty(&dev->functions)) {
|
---|
334 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
335 | return EIO;
|
---|
336 | }
|
---|
337 | driver_t *driver = dev->drv;
|
---|
338 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
339 |
|
---|
340 | if (driver)
|
---|
341 | detach_driver(&device_tree, dev);
|
---|
342 |
|
---|
343 | fibril_rwlock_write_lock(&device_tree.rwlock);
|
---|
344 | remove_dev_node(&device_tree, dev);
|
---|
345 |
|
---|
346 | /* Delete ref created when node was inserted */
|
---|
347 | dev_del_ref(dev);
|
---|
348 | /* Delete ref created by dev_add_ref(dev) above */
|
---|
349 | dev_del_ref(dev);
|
---|
350 | }
|
---|
351 | } else {
|
---|
352 | /* Unregister from location service */
|
---|
353 | rc = loc_service_unregister(fun->service_id);
|
---|
354 | if (rc != EOK) {
|
---|
355 | fibril_rwlock_write_unlock(&device_tree.rwlock);
|
---|
356 | log_msg(LVL_ERROR, "Failed unregistering tree service.");
|
---|
357 | return EIO;
|
---|
358 | }
|
---|
359 |
|
---|
360 | fun->service_id = 0;
|
---|
361 | }
|
---|
362 |
|
---|
363 | fun->state = FUN_OFF_LINE;
|
---|
364 | fibril_rwlock_write_unlock(&device_tree.rwlock);
|
---|
365 |
|
---|
366 | return EOK;
|
---|
367 | }
|
---|
368 |
|
---|
369 | /** Handle function registration.
|
---|
370 | *
|
---|
371 | * Child devices are registered by their parent's device driver.
|
---|
372 | */
|
---|
373 | static void devman_add_function(ipc_callid_t callid, ipc_call_t *call)
|
---|
374 | {
|
---|
375 | fun_type_t ftype = (fun_type_t) IPC_GET_ARG1(*call);
|
---|
376 | devman_handle_t dev_handle = IPC_GET_ARG2(*call);
|
---|
377 | sysarg_t match_count = IPC_GET_ARG3(*call);
|
---|
378 | dev_tree_t *tree = &device_tree;
|
---|
379 |
|
---|
380 | dev_node_t *pdev = find_dev_node(&device_tree, dev_handle);
|
---|
381 | if (pdev == NULL) {
|
---|
382 | async_answer_0(callid, ENOENT);
|
---|
383 | return;
|
---|
384 | }
|
---|
385 |
|
---|
386 | if (ftype != fun_inner && ftype != fun_exposed) {
|
---|
387 | /* Unknown function type */
|
---|
388 | log_msg(LVL_ERROR,
|
---|
389 | "Unknown function type %d provided by driver.",
|
---|
390 | (int) ftype);
|
---|
391 |
|
---|
392 | dev_del_ref(pdev);
|
---|
393 | async_answer_0(callid, EINVAL);
|
---|
394 | return;
|
---|
395 | }
|
---|
396 |
|
---|
397 | char *fun_name = NULL;
|
---|
398 | int rc = async_data_write_accept((void **)&fun_name, true, 0, 0, 0, 0);
|
---|
399 | if (rc != EOK) {
|
---|
400 | dev_del_ref(pdev);
|
---|
401 | async_answer_0(callid, rc);
|
---|
402 | return;
|
---|
403 | }
|
---|
404 |
|
---|
405 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
406 |
|
---|
407 | /* Check device state */
|
---|
408 | if (pdev->state == DEVICE_REMOVED) {
|
---|
409 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
410 | dev_del_ref(pdev);
|
---|
411 | async_answer_0(callid, ENOENT);
|
---|
412 | return;
|
---|
413 | }
|
---|
414 |
|
---|
415 | /* Check that function with same name is not there already. */
|
---|
416 | if (find_fun_node_in_device(tree, pdev, fun_name) != NULL) {
|
---|
417 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
418 | dev_del_ref(pdev);
|
---|
419 | async_answer_0(callid, EEXISTS);
|
---|
420 | printf(NAME ": Warning, driver tried to register `%s' twice.\n",
|
---|
421 | fun_name);
|
---|
422 | free(fun_name);
|
---|
423 | return;
|
---|
424 | }
|
---|
425 |
|
---|
426 | fun_node_t *fun = create_fun_node();
|
---|
427 | fun_add_ref(fun);
|
---|
428 | fun->ftype = ftype;
|
---|
429 |
|
---|
430 | if (!insert_fun_node(&device_tree, fun, fun_name, pdev)) {
|
---|
431 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
432 | dev_del_ref(pdev);
|
---|
433 | delete_fun_node(fun);
|
---|
434 | async_answer_0(callid, ENOMEM);
|
---|
435 | return;
|
---|
436 | }
|
---|
437 |
|
---|
438 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
439 | dev_del_ref(pdev);
|
---|
440 |
|
---|
441 | devman_receive_match_ids(match_count, &fun->match_ids);
|
---|
442 |
|
---|
443 | rc = online_function(fun);
|
---|
444 | if (rc != EOK) {
|
---|
445 | /* XXX clean up */
|
---|
446 | async_answer_0(callid, rc);
|
---|
447 | return;
|
---|
448 | }
|
---|
449 |
|
---|
450 | /* Return device handle to parent's driver. */
|
---|
451 | async_answer_1(callid, EOK, fun->handle);
|
---|
452 | }
|
---|
453 |
|
---|
454 | static void devman_add_function_to_cat(ipc_callid_t callid, ipc_call_t *call)
|
---|
455 | {
|
---|
456 | devman_handle_t handle = IPC_GET_ARG1(*call);
|
---|
457 | category_id_t cat_id;
|
---|
458 | int rc;
|
---|
459 |
|
---|
460 | /* Get category name. */
|
---|
461 | char *cat_name;
|
---|
462 | rc = async_data_write_accept((void **) &cat_name, true,
|
---|
463 | 0, 0, 0, 0);
|
---|
464 | if (rc != EOK) {
|
---|
465 | async_answer_0(callid, rc);
|
---|
466 | return;
|
---|
467 | }
|
---|
468 |
|
---|
469 | fun_node_t *fun = find_fun_node(&device_tree, handle);
|
---|
470 | if (fun == NULL) {
|
---|
471 | async_answer_0(callid, ENOENT);
|
---|
472 | return;
|
---|
473 | }
|
---|
474 |
|
---|
475 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
476 |
|
---|
477 | /* Check function state */
|
---|
478 | if (fun->state == FUN_REMOVED) {
|
---|
479 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
480 | async_answer_0(callid, ENOENT);
|
---|
481 | return;
|
---|
482 | }
|
---|
483 |
|
---|
484 | rc = loc_category_get_id(cat_name, &cat_id, IPC_FLAG_BLOCKING);
|
---|
485 | if (rc == EOK) {
|
---|
486 | loc_service_add_to_cat(fun->service_id, cat_id);
|
---|
487 | } else {
|
---|
488 | log_msg(LVL_ERROR, "Failed adding function `%s' to category "
|
---|
489 | "`%s'.", fun->pathname, cat_name);
|
---|
490 | }
|
---|
491 |
|
---|
492 | log_msg(LVL_NOTE, "Function `%s' added to category `%s'.",
|
---|
493 | fun->pathname, cat_name);
|
---|
494 |
|
---|
495 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
496 | fun_del_ref(fun);
|
---|
497 |
|
---|
498 | async_answer_0(callid, EOK);
|
---|
499 | }
|
---|
500 |
|
---|
501 | /** Online function by driver request.
|
---|
502 | *
|
---|
503 | */
|
---|
504 | static void devman_drv_fun_online(ipc_callid_t iid, ipc_call_t *icall,
|
---|
505 | driver_t *drv)
|
---|
506 | {
|
---|
507 | fun_node_t *fun;
|
---|
508 | int rc;
|
---|
509 |
|
---|
510 | log_msg(LVL_DEBUG, "devman_drv_fun_online()");
|
---|
511 |
|
---|
512 | fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));
|
---|
513 | if (fun == NULL) {
|
---|
514 | async_answer_0(iid, ENOENT);
|
---|
515 | return;
|
---|
516 | }
|
---|
517 |
|
---|
518 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
519 | if (fun->dev == NULL || fun->dev->drv != drv) {
|
---|
520 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
521 | fun_del_ref(fun);
|
---|
522 | async_answer_0(iid, ENOENT);
|
---|
523 | return;
|
---|
524 | }
|
---|
525 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
526 |
|
---|
527 | rc = online_function(fun);
|
---|
528 | if (rc != EOK) {
|
---|
529 | fun_del_ref(fun);
|
---|
530 | async_answer_0(iid, (sysarg_t) rc);
|
---|
531 | return;
|
---|
532 | }
|
---|
533 |
|
---|
534 | fun_del_ref(fun);
|
---|
535 |
|
---|
536 | async_answer_0(iid, (sysarg_t) EOK);
|
---|
537 | }
|
---|
538 |
|
---|
539 |
|
---|
540 | /** Offline function by driver request.
|
---|
541 | *
|
---|
542 | */
|
---|
543 | static void devman_drv_fun_offline(ipc_callid_t iid, ipc_call_t *icall,
|
---|
544 | driver_t *drv)
|
---|
545 | {
|
---|
546 | fun_node_t *fun;
|
---|
547 | int rc;
|
---|
548 |
|
---|
549 | fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));
|
---|
550 | if (fun == NULL) {
|
---|
551 | async_answer_0(iid, ENOENT);
|
---|
552 | return;
|
---|
553 | }
|
---|
554 |
|
---|
555 | fibril_rwlock_write_lock(&device_tree.rwlock);
|
---|
556 | if (fun->dev == NULL || fun->dev->drv != drv) {
|
---|
557 | fun_del_ref(fun);
|
---|
558 | async_answer_0(iid, ENOENT);
|
---|
559 | return;
|
---|
560 | }
|
---|
561 | fibril_rwlock_write_unlock(&device_tree.rwlock);
|
---|
562 |
|
---|
563 | rc = offline_function(fun);
|
---|
564 | if (rc != EOK) {
|
---|
565 | fun_del_ref(fun);
|
---|
566 | async_answer_0(iid, (sysarg_t) rc);
|
---|
567 | return;
|
---|
568 | }
|
---|
569 |
|
---|
570 | fun_del_ref(fun);
|
---|
571 | async_answer_0(iid, (sysarg_t) EOK);
|
---|
572 | }
|
---|
573 |
|
---|
574 | /** Remove function. */
|
---|
575 | static void devman_remove_function(ipc_callid_t callid, ipc_call_t *call)
|
---|
576 | {
|
---|
577 | devman_handle_t fun_handle = IPC_GET_ARG1(*call);
|
---|
578 | dev_tree_t *tree = &device_tree;
|
---|
579 | int rc;
|
---|
580 |
|
---|
581 |
|
---|
582 | fun_node_t *fun = find_fun_node(&device_tree, fun_handle);
|
---|
583 | if (fun == NULL) {
|
---|
584 | async_answer_0(callid, ENOENT);
|
---|
585 | return;
|
---|
586 | }
|
---|
587 |
|
---|
588 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
589 |
|
---|
590 | log_msg(LVL_DEBUG, "devman_remove_function(fun='%s')", fun->pathname);
|
---|
591 |
|
---|
592 | /* Check function state */
|
---|
593 | if (fun->state == FUN_REMOVED) {
|
---|
594 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
595 | async_answer_0(callid, ENOENT);
|
---|
596 | return;
|
---|
597 | }
|
---|
598 |
|
---|
599 | if (fun->ftype == fun_inner) {
|
---|
600 | /* Handle possible descendants */
|
---|
601 | /* TODO - This is a surprise removal */
|
---|
602 | if (fun->child != NULL) {
|
---|
603 | log_msg(LVL_WARN, "devman_remove_function(): not handling "
|
---|
604 | "descendants\n");
|
---|
605 | }
|
---|
606 | } else {
|
---|
607 | if (fun->service_id != 0) {
|
---|
608 | /* Unregister from location service */
|
---|
609 | rc = loc_service_unregister(fun->service_id);
|
---|
610 | if (rc != EOK) {
|
---|
611 | log_msg(LVL_ERROR, "Failed unregistering tree "
|
---|
612 | "service.");
|
---|
613 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
614 | fun_del_ref(fun);
|
---|
615 | async_answer_0(callid, EIO);
|
---|
616 | return;
|
---|
617 | }
|
---|
618 | }
|
---|
619 | }
|
---|
620 |
|
---|
621 | remove_fun_node(&device_tree, fun);
|
---|
622 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
623 |
|
---|
624 | /* Delete ref added when inserting function into tree */
|
---|
625 | fun_del_ref(fun);
|
---|
626 | /* Delete ref added above when looking up function */
|
---|
627 | fun_del_ref(fun);
|
---|
628 |
|
---|
629 | log_msg(LVL_DEBUG, "devman_remove_function() succeeded.");
|
---|
630 | async_answer_0(callid, EOK);
|
---|
631 | }
|
---|
632 |
|
---|
633 | /** Initialize driver which has registered itself as running and ready.
|
---|
634 | *
|
---|
635 | * The initialization is done in a separate fibril to avoid deadlocks (if the
|
---|
636 | * driver needed to be served by devman during the driver's initialization).
|
---|
637 | */
|
---|
638 | static int init_running_drv(void *drv)
|
---|
639 | {
|
---|
640 | driver_t *driver = (driver_t *) drv;
|
---|
641 |
|
---|
642 | initialize_running_driver(driver, &device_tree);
|
---|
643 | log_msg(LVL_DEBUG, "The `%s` driver was successfully initialized.",
|
---|
644 | driver->name);
|
---|
645 | return 0;
|
---|
646 | }
|
---|
647 |
|
---|
648 | /** Function for handling connections from a driver to the device manager. */
|
---|
649 | static void devman_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
|
---|
650 | {
|
---|
651 | client_t *client;
|
---|
652 | driver_t *driver;
|
---|
653 |
|
---|
654 | /* Accept the connection. */
|
---|
655 | async_answer_0(iid, EOK);
|
---|
656 |
|
---|
657 | client = async_get_client_data();
|
---|
658 | if (client == NULL) {
|
---|
659 | log_msg(LVL_ERROR, "Failed to allocate client data.");
|
---|
660 | return;
|
---|
661 | }
|
---|
662 |
|
---|
663 | while (true) {
|
---|
664 | ipc_call_t call;
|
---|
665 | ipc_callid_t callid = async_get_call(&call);
|
---|
666 |
|
---|
667 | if (!IPC_GET_IMETHOD(call))
|
---|
668 | break;
|
---|
669 |
|
---|
670 | if (IPC_GET_IMETHOD(call) != DEVMAN_DRIVER_REGISTER) {
|
---|
671 | fibril_mutex_lock(&client->mutex);
|
---|
672 | driver = client->driver;
|
---|
673 | fibril_mutex_unlock(&client->mutex);
|
---|
674 | if (driver == NULL) {
|
---|
675 | /* First call must be to DEVMAN_DRIVER_REGISTER */
|
---|
676 | async_answer_0(callid, ENOTSUP);
|
---|
677 | continue;
|
---|
678 | }
|
---|
679 | }
|
---|
680 |
|
---|
681 | switch (IPC_GET_IMETHOD(call)) {
|
---|
682 | case DEVMAN_DRIVER_REGISTER:
|
---|
683 | fibril_mutex_lock(&client->mutex);
|
---|
684 | if (client->driver != NULL) {
|
---|
685 | fibril_mutex_unlock(&client->mutex);
|
---|
686 | async_answer_0(callid, EINVAL);
|
---|
687 | continue;
|
---|
688 | }
|
---|
689 | client->driver = devman_driver_register(callid, &call);
|
---|
690 | fibril_mutex_unlock(&client->mutex);
|
---|
691 | break;
|
---|
692 | case DEVMAN_ADD_FUNCTION:
|
---|
693 | devman_add_function(callid, &call);
|
---|
694 | break;
|
---|
695 | case DEVMAN_ADD_DEVICE_TO_CATEGORY:
|
---|
696 | devman_add_function_to_cat(callid, &call);
|
---|
697 | break;
|
---|
698 | case DEVMAN_DRV_FUN_ONLINE:
|
---|
699 | devman_drv_fun_online(callid, &call, driver);
|
---|
700 | break;
|
---|
701 | case DEVMAN_DRV_FUN_OFFLINE:
|
---|
702 | devman_drv_fun_offline(callid, &call, driver);
|
---|
703 | break;
|
---|
704 | case DEVMAN_REMOVE_FUNCTION:
|
---|
705 | devman_remove_function(callid, &call);
|
---|
706 | break;
|
---|
707 | default:
|
---|
708 | async_answer_0(callid, EINVAL);
|
---|
709 | break;
|
---|
710 | }
|
---|
711 | }
|
---|
712 | }
|
---|
713 |
|
---|
714 | /** Find handle for the device instance identified by the device's path in the
|
---|
715 | * device tree. */
|
---|
716 | static void devman_function_get_handle(ipc_callid_t iid, ipc_call_t *icall)
|
---|
717 | {
|
---|
718 | char *pathname;
|
---|
719 | devman_handle_t handle;
|
---|
720 |
|
---|
721 | int rc = async_data_write_accept((void **) &pathname, true, 0, 0, 0, 0);
|
---|
722 | if (rc != EOK) {
|
---|
723 | async_answer_0(iid, rc);
|
---|
724 | return;
|
---|
725 | }
|
---|
726 |
|
---|
727 | fun_node_t *fun = find_fun_node_by_path(&device_tree, pathname);
|
---|
728 |
|
---|
729 | free(pathname);
|
---|
730 |
|
---|
731 | if (fun == NULL) {
|
---|
732 | async_answer_0(iid, ENOENT);
|
---|
733 | return;
|
---|
734 | }
|
---|
735 |
|
---|
736 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
737 |
|
---|
738 | /* Check function state */
|
---|
739 | if (fun->state == FUN_REMOVED) {
|
---|
740 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
741 | async_answer_0(iid, ENOENT);
|
---|
742 | return;
|
---|
743 | }
|
---|
744 | handle = fun->handle;
|
---|
745 |
|
---|
746 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
747 |
|
---|
748 | /* Delete reference created above by find_fun_node_by_path() */
|
---|
749 | fun_del_ref(fun);
|
---|
750 |
|
---|
751 | async_answer_1(iid, EOK, handle);
|
---|
752 | }
|
---|
753 |
|
---|
754 | /** Get device name. */
|
---|
755 | static void devman_fun_get_name(ipc_callid_t iid, ipc_call_t *icall)
|
---|
756 | {
|
---|
757 | devman_handle_t handle = IPC_GET_ARG1(*icall);
|
---|
758 |
|
---|
759 | fun_node_t *fun = find_fun_node(&device_tree, handle);
|
---|
760 | if (fun == NULL) {
|
---|
761 | async_answer_0(iid, ENOMEM);
|
---|
762 | return;
|
---|
763 | }
|
---|
764 |
|
---|
765 | ipc_callid_t data_callid;
|
---|
766 | size_t data_len;
|
---|
767 | if (!async_data_read_receive(&data_callid, &data_len)) {
|
---|
768 | async_answer_0(iid, EINVAL);
|
---|
769 | fun_del_ref(fun);
|
---|
770 | return;
|
---|
771 | }
|
---|
772 |
|
---|
773 | void *buffer = malloc(data_len);
|
---|
774 | if (buffer == NULL) {
|
---|
775 | async_answer_0(data_callid, ENOMEM);
|
---|
776 | async_answer_0(iid, ENOMEM);
|
---|
777 | fun_del_ref(fun);
|
---|
778 | return;
|
---|
779 | }
|
---|
780 |
|
---|
781 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
782 |
|
---|
783 | /* Check function state */
|
---|
784 | if (fun->state == FUN_REMOVED) {
|
---|
785 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
786 | free(buffer);
|
---|
787 |
|
---|
788 | async_answer_0(data_callid, ENOENT);
|
---|
789 | async_answer_0(iid, ENOENT);
|
---|
790 | fun_del_ref(fun);
|
---|
791 | return;
|
---|
792 | }
|
---|
793 |
|
---|
794 | size_t sent_length = str_size(fun->name);
|
---|
795 | if (sent_length > data_len) {
|
---|
796 | sent_length = data_len;
|
---|
797 | }
|
---|
798 |
|
---|
799 | async_data_read_finalize(data_callid, fun->name, sent_length);
|
---|
800 | async_answer_0(iid, EOK);
|
---|
801 |
|
---|
802 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
803 | fun_del_ref(fun);
|
---|
804 | free(buffer);
|
---|
805 | }
|
---|
806 |
|
---|
807 |
|
---|
808 | /** Get device path. */
|
---|
809 | static void devman_fun_get_path(ipc_callid_t iid, ipc_call_t *icall)
|
---|
810 | {
|
---|
811 | devman_handle_t handle = IPC_GET_ARG1(*icall);
|
---|
812 |
|
---|
813 | fun_node_t *fun = find_fun_node(&device_tree, handle);
|
---|
814 | if (fun == NULL) {
|
---|
815 | async_answer_0(iid, ENOMEM);
|
---|
816 | return;
|
---|
817 | }
|
---|
818 |
|
---|
819 | ipc_callid_t data_callid;
|
---|
820 | size_t data_len;
|
---|
821 | if (!async_data_read_receive(&data_callid, &data_len)) {
|
---|
822 | async_answer_0(iid, EINVAL);
|
---|
823 | fun_del_ref(fun);
|
---|
824 | return;
|
---|
825 | }
|
---|
826 |
|
---|
827 | void *buffer = malloc(data_len);
|
---|
828 | if (buffer == NULL) {
|
---|
829 | async_answer_0(data_callid, ENOMEM);
|
---|
830 | async_answer_0(iid, ENOMEM);
|
---|
831 | fun_del_ref(fun);
|
---|
832 | return;
|
---|
833 | }
|
---|
834 |
|
---|
835 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
836 |
|
---|
837 | /* Check function state */
|
---|
838 | if (fun->state == FUN_REMOVED) {
|
---|
839 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
840 | free(buffer);
|
---|
841 |
|
---|
842 | async_answer_0(data_callid, ENOENT);
|
---|
843 | async_answer_0(iid, ENOENT);
|
---|
844 | fun_del_ref(fun);
|
---|
845 | return;
|
---|
846 | }
|
---|
847 |
|
---|
848 | size_t sent_length = str_size(fun->pathname);
|
---|
849 | if (sent_length > data_len) {
|
---|
850 | sent_length = data_len;
|
---|
851 | }
|
---|
852 |
|
---|
853 | async_data_read_finalize(data_callid, fun->pathname, sent_length);
|
---|
854 | async_answer_0(iid, EOK);
|
---|
855 |
|
---|
856 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
857 | fun_del_ref(fun);
|
---|
858 | free(buffer);
|
---|
859 | }
|
---|
860 |
|
---|
861 | static void devman_dev_get_functions(ipc_callid_t iid, ipc_call_t *icall)
|
---|
862 | {
|
---|
863 | ipc_callid_t callid;
|
---|
864 | size_t size;
|
---|
865 | size_t act_size;
|
---|
866 | int rc;
|
---|
867 |
|
---|
868 | if (!async_data_read_receive(&callid, &size)) {
|
---|
869 | async_answer_0(callid, EREFUSED);
|
---|
870 | async_answer_0(iid, EREFUSED);
|
---|
871 | return;
|
---|
872 | }
|
---|
873 |
|
---|
874 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
875 |
|
---|
876 | dev_node_t *dev = find_dev_node_no_lock(&device_tree,
|
---|
877 | IPC_GET_ARG1(*icall));
|
---|
878 | if (dev == NULL || dev->state == DEVICE_REMOVED) {
|
---|
879 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
880 | async_answer_0(callid, ENOENT);
|
---|
881 | async_answer_0(iid, ENOENT);
|
---|
882 | return;
|
---|
883 | }
|
---|
884 |
|
---|
885 | devman_handle_t *hdl_buf = (devman_handle_t *) malloc(size);
|
---|
886 | if (hdl_buf == NULL) {
|
---|
887 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
888 | async_answer_0(callid, ENOMEM);
|
---|
889 | async_answer_0(iid, ENOMEM);
|
---|
890 | return;
|
---|
891 | }
|
---|
892 |
|
---|
893 | rc = dev_get_functions(&device_tree, dev, hdl_buf, size, &act_size);
|
---|
894 | if (rc != EOK) {
|
---|
895 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
896 | async_answer_0(callid, rc);
|
---|
897 | async_answer_0(iid, rc);
|
---|
898 | return;
|
---|
899 | }
|
---|
900 |
|
---|
901 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
902 |
|
---|
903 | sysarg_t retval = async_data_read_finalize(callid, hdl_buf, size);
|
---|
904 | free(hdl_buf);
|
---|
905 |
|
---|
906 | async_answer_1(iid, retval, act_size);
|
---|
907 | }
|
---|
908 |
|
---|
909 |
|
---|
910 | /** Get handle for child device of a function. */
|
---|
911 | static void devman_fun_get_child(ipc_callid_t iid, ipc_call_t *icall)
|
---|
912 | {
|
---|
913 | fun_node_t *fun;
|
---|
914 |
|
---|
915 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
916 |
|
---|
917 | fun = find_fun_node_no_lock(&device_tree, IPC_GET_ARG1(*icall));
|
---|
918 | if (fun == NULL || fun->state == FUN_REMOVED) {
|
---|
919 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
920 | async_answer_0(iid, ENOENT);
|
---|
921 | return;
|
---|
922 | }
|
---|
923 |
|
---|
924 | if (fun->child == NULL) {
|
---|
925 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
926 | async_answer_0(iid, ENOENT);
|
---|
927 | return;
|
---|
928 | }
|
---|
929 |
|
---|
930 | async_answer_1(iid, EOK, fun->child->handle);
|
---|
931 |
|
---|
932 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
933 | }
|
---|
934 |
|
---|
935 | /** Online function.
|
---|
936 | *
|
---|
937 | * Send a request to online a function to the responsible driver.
|
---|
938 | * The driver may offline other functions if necessary (i.e. if the state
|
---|
939 | * of this function is linked to state of another function somehow).
|
---|
940 | */
|
---|
941 | static void devman_fun_online(ipc_callid_t iid, ipc_call_t *icall)
|
---|
942 | {
|
---|
943 | fun_node_t *fun;
|
---|
944 | int rc;
|
---|
945 |
|
---|
946 | fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));
|
---|
947 | if (fun == NULL) {
|
---|
948 | async_answer_0(iid, ENOENT);
|
---|
949 | return;
|
---|
950 | }
|
---|
951 |
|
---|
952 | rc = driver_fun_online(&device_tree, fun);
|
---|
953 | fun_del_ref(fun);
|
---|
954 |
|
---|
955 | async_answer_0(iid, (sysarg_t) rc);
|
---|
956 | }
|
---|
957 |
|
---|
958 | /** Offline function.
|
---|
959 | *
|
---|
960 | * Send a request to offline a function to the responsible driver. As
|
---|
961 | * a result the subtree rooted at that function should be cleanly
|
---|
962 | * detatched. The driver may offline other functions if necessary
|
---|
963 | * (i.e. if the state of this function is linked to state of another
|
---|
964 | * function somehow).
|
---|
965 | */
|
---|
966 | static void devman_fun_offline(ipc_callid_t iid, ipc_call_t *icall)
|
---|
967 | {
|
---|
968 | fun_node_t *fun;
|
---|
969 | int rc;
|
---|
970 |
|
---|
971 | fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));
|
---|
972 | if (fun == NULL) {
|
---|
973 | async_answer_0(iid, ENOENT);
|
---|
974 | return;
|
---|
975 | }
|
---|
976 |
|
---|
977 | rc = driver_fun_offline(&device_tree, fun);
|
---|
978 | fun_del_ref(fun);
|
---|
979 |
|
---|
980 | async_answer_0(iid, (sysarg_t) rc);
|
---|
981 | }
|
---|
982 |
|
---|
983 | /** Find handle for the function instance identified by its service ID. */
|
---|
984 | static void devman_fun_sid_to_handle(ipc_callid_t iid, ipc_call_t *icall)
|
---|
985 | {
|
---|
986 | fun_node_t *fun;
|
---|
987 |
|
---|
988 | fun = find_loc_tree_function(&device_tree, IPC_GET_ARG1(*icall));
|
---|
989 |
|
---|
990 | if (fun == NULL) {
|
---|
991 | async_answer_0(iid, ENOENT);
|
---|
992 | return;
|
---|
993 | }
|
---|
994 |
|
---|
995 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
996 |
|
---|
997 | /* Check function state */
|
---|
998 | if (fun->state == FUN_REMOVED) {
|
---|
999 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
1000 | async_answer_0(iid, ENOENT);
|
---|
1001 | return;
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | async_answer_1(iid, EOK, fun->handle);
|
---|
1005 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
1006 | fun_del_ref(fun);
|
---|
1007 | }
|
---|
1008 |
|
---|
1009 | /** Function for handling connections from a client to the device manager. */
|
---|
1010 | static void devman_connection_client(ipc_callid_t iid, ipc_call_t *icall)
|
---|
1011 | {
|
---|
1012 | /* Accept connection. */
|
---|
1013 | async_answer_0(iid, EOK);
|
---|
1014 |
|
---|
1015 | while (true) {
|
---|
1016 | ipc_call_t call;
|
---|
1017 | ipc_callid_t callid = async_get_call(&call);
|
---|
1018 |
|
---|
1019 | if (!IPC_GET_IMETHOD(call))
|
---|
1020 | break;
|
---|
1021 |
|
---|
1022 | switch (IPC_GET_IMETHOD(call)) {
|
---|
1023 | case DEVMAN_DEVICE_GET_HANDLE:
|
---|
1024 | devman_function_get_handle(callid, &call);
|
---|
1025 | break;
|
---|
1026 | case DEVMAN_DEV_GET_FUNCTIONS:
|
---|
1027 | devman_dev_get_functions(callid, &call);
|
---|
1028 | break;
|
---|
1029 | case DEVMAN_FUN_GET_CHILD:
|
---|
1030 | devman_fun_get_child(callid, &call);
|
---|
1031 | break;
|
---|
1032 | case DEVMAN_FUN_GET_NAME:
|
---|
1033 | devman_fun_get_name(callid, &call);
|
---|
1034 | break;
|
---|
1035 | case DEVMAN_FUN_GET_PATH:
|
---|
1036 | devman_fun_get_path(callid, &call);
|
---|
1037 | break;
|
---|
1038 | case DEVMAN_FUN_ONLINE:
|
---|
1039 | devman_fun_online(callid, &call);
|
---|
1040 | break;
|
---|
1041 | case DEVMAN_FUN_OFFLINE:
|
---|
1042 | devman_fun_offline(callid, &call);
|
---|
1043 | break;
|
---|
1044 | case DEVMAN_FUN_SID_TO_HANDLE:
|
---|
1045 | devman_fun_sid_to_handle(callid, &call);
|
---|
1046 | break;
|
---|
1047 | default:
|
---|
1048 | async_answer_0(callid, ENOENT);
|
---|
1049 | }
|
---|
1050 | }
|
---|
1051 | }
|
---|
1052 |
|
---|
1053 | static void devman_forward(ipc_callid_t iid, ipc_call_t *icall,
|
---|
1054 | bool drv_to_parent)
|
---|
1055 | {
|
---|
1056 | devman_handle_t handle = IPC_GET_ARG2(*icall);
|
---|
1057 | devman_handle_t fwd_h;
|
---|
1058 | fun_node_t *fun = NULL;
|
---|
1059 | dev_node_t *dev = NULL;
|
---|
1060 |
|
---|
1061 | fun = find_fun_node(&device_tree, handle);
|
---|
1062 | if (fun == NULL)
|
---|
1063 | dev = find_dev_node(&device_tree, handle);
|
---|
1064 | else {
|
---|
1065 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
1066 | dev = fun->dev;
|
---|
1067 | if (dev != NULL)
|
---|
1068 | dev_add_ref(dev);
|
---|
1069 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 | /*
|
---|
1073 | * For a valid function to connect to we need a device. The root
|
---|
1074 | * function, for example, has no device and cannot be connected to.
|
---|
1075 | * This means @c dev needs to be valid regardless whether we are
|
---|
1076 | * connecting to a device or to a function.
|
---|
1077 | */
|
---|
1078 | if (dev == NULL) {
|
---|
1079 | log_msg(LVL_ERROR, "IPC forwarding failed - no device or "
|
---|
1080 | "function with handle %" PRIun " was found.", handle);
|
---|
1081 | async_answer_0(iid, ENOENT);
|
---|
1082 | goto cleanup;
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | if (fun == NULL && !drv_to_parent) {
|
---|
1086 | log_msg(LVL_ERROR, NAME ": devman_forward error - cannot "
|
---|
1087 | "connect to handle %" PRIun ", refers to a device.",
|
---|
1088 | handle);
|
---|
1089 | async_answer_0(iid, ENOENT);
|
---|
1090 | goto cleanup;
|
---|
1091 | }
|
---|
1092 |
|
---|
1093 | driver_t *driver = NULL;
|
---|
1094 |
|
---|
1095 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
1096 |
|
---|
1097 | if (drv_to_parent) {
|
---|
1098 | /* Connect to parent function of a device (or device function). */
|
---|
1099 | if (dev->pfun->dev != NULL)
|
---|
1100 | driver = dev->pfun->dev->drv;
|
---|
1101 | fwd_h = dev->pfun->handle;
|
---|
1102 | } else if (dev->state == DEVICE_USABLE) {
|
---|
1103 | /* Connect to the specified function */
|
---|
1104 | driver = dev->drv;
|
---|
1105 | assert(driver != NULL);
|
---|
1106 |
|
---|
1107 | fwd_h = handle;
|
---|
1108 | }
|
---|
1109 |
|
---|
1110 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
1111 |
|
---|
1112 | if (driver == NULL) {
|
---|
1113 | log_msg(LVL_ERROR, "IPC forwarding refused - " \
|
---|
1114 | "the device %" PRIun " is not in usable state.", handle);
|
---|
1115 | async_answer_0(iid, ENOENT);
|
---|
1116 | goto cleanup;
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 | int method;
|
---|
1120 | if (drv_to_parent)
|
---|
1121 | method = DRIVER_DRIVER;
|
---|
1122 | else
|
---|
1123 | method = DRIVER_CLIENT;
|
---|
1124 |
|
---|
1125 | if (!driver->sess) {
|
---|
1126 | log_msg(LVL_ERROR,
|
---|
1127 | "Could not forward to driver `%s'.", driver->name);
|
---|
1128 | async_answer_0(iid, EINVAL);
|
---|
1129 | goto cleanup;
|
---|
1130 | }
|
---|
1131 |
|
---|
1132 | if (fun != NULL) {
|
---|
1133 | log_msg(LVL_DEBUG,
|
---|
1134 | "Forwarding request for `%s' function to driver `%s'.",
|
---|
1135 | fun->pathname, driver->name);
|
---|
1136 | } else {
|
---|
1137 | log_msg(LVL_DEBUG,
|
---|
1138 | "Forwarding request for `%s' device to driver `%s'.",
|
---|
1139 | dev->pfun->pathname, driver->name);
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | async_exch_t *exch = async_exchange_begin(driver->sess);
|
---|
1143 | async_forward_fast(iid, exch, method, fwd_h, 0, IPC_FF_NONE);
|
---|
1144 | async_exchange_end(exch);
|
---|
1145 |
|
---|
1146 | cleanup:
|
---|
1147 | if (dev != NULL)
|
---|
1148 | dev_del_ref(dev);
|
---|
1149 | if (fun != NULL)
|
---|
1150 | fun_del_ref(fun);
|
---|
1151 | }
|
---|
1152 |
|
---|
1153 | /** Function for handling connections from a client forwarded by the location
|
---|
1154 | * service to the device manager. */
|
---|
1155 | static void devman_connection_loc(ipc_callid_t iid, ipc_call_t *icall)
|
---|
1156 | {
|
---|
1157 | service_id_t service_id = IPC_GET_ARG2(*icall);
|
---|
1158 | fun_node_t *fun;
|
---|
1159 | dev_node_t *dev;
|
---|
1160 | devman_handle_t handle;
|
---|
1161 | driver_t *driver;
|
---|
1162 |
|
---|
1163 | fun = find_loc_tree_function(&device_tree, service_id);
|
---|
1164 |
|
---|
1165 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
1166 |
|
---|
1167 | if (fun == NULL || fun->dev == NULL || fun->dev->drv == NULL) {
|
---|
1168 | log_msg(LVL_WARN, "devman_connection_loc(): function "
|
---|
1169 | "not found.\n");
|
---|
1170 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
1171 | async_answer_0(iid, ENOENT);
|
---|
1172 | return;
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | dev = fun->dev;
|
---|
1176 | driver = dev->drv;
|
---|
1177 | handle = fun->handle;
|
---|
1178 |
|
---|
1179 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
1180 |
|
---|
1181 | async_exch_t *exch = async_exchange_begin(driver->sess);
|
---|
1182 | async_forward_fast(iid, exch, DRIVER_CLIENT, handle, 0,
|
---|
1183 | IPC_FF_NONE);
|
---|
1184 | async_exchange_end(exch);
|
---|
1185 |
|
---|
1186 | log_msg(LVL_DEBUG,
|
---|
1187 | "Forwarding loc service request for `%s' function to driver `%s'.",
|
---|
1188 | fun->pathname, driver->name);
|
---|
1189 |
|
---|
1190 | fun_del_ref(fun);
|
---|
1191 | }
|
---|
1192 |
|
---|
1193 | /** Function for handling connections to device manager. */
|
---|
1194 | static void devman_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
1195 | {
|
---|
1196 | /* Select port. */
|
---|
1197 | switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
|
---|
1198 | case DEVMAN_DRIVER:
|
---|
1199 | devman_connection_driver(iid, icall);
|
---|
1200 | break;
|
---|
1201 | case DEVMAN_CLIENT:
|
---|
1202 | devman_connection_client(iid, icall);
|
---|
1203 | break;
|
---|
1204 | case DEVMAN_CONNECT_TO_DEVICE:
|
---|
1205 | /* Connect client to selected device. */
|
---|
1206 | devman_forward(iid, icall, false);
|
---|
1207 | break;
|
---|
1208 | case DEVMAN_CONNECT_FROM_LOC:
|
---|
1209 | /* Someone connected through loc node. */
|
---|
1210 | devman_connection_loc(iid, icall);
|
---|
1211 | break;
|
---|
1212 | case DEVMAN_CONNECT_TO_PARENTS_DEVICE:
|
---|
1213 | /* Connect client to selected device. */
|
---|
1214 | devman_forward(iid, icall, true);
|
---|
1215 | break;
|
---|
1216 | default:
|
---|
1217 | /* No such interface */
|
---|
1218 | async_answer_0(iid, ENOENT);
|
---|
1219 | }
|
---|
1220 | }
|
---|
1221 |
|
---|
1222 | static void *devman_client_data_create(void)
|
---|
1223 | {
|
---|
1224 | client_t *client;
|
---|
1225 |
|
---|
1226 | client = calloc(1, sizeof(client_t));
|
---|
1227 | if (client == NULL)
|
---|
1228 | return NULL;
|
---|
1229 |
|
---|
1230 | fibril_mutex_initialize(&client->mutex);
|
---|
1231 | return client;
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | static void devman_client_data_destroy(void *data)
|
---|
1235 | {
|
---|
1236 | free(data);
|
---|
1237 | }
|
---|
1238 |
|
---|
1239 | /** Initialize device manager internal structures. */
|
---|
1240 | static bool devman_init(void)
|
---|
1241 | {
|
---|
1242 | log_msg(LVL_DEBUG, "devman_init - looking for available drivers.");
|
---|
1243 |
|
---|
1244 | /* Initialize list of available drivers. */
|
---|
1245 | init_driver_list(&drivers_list);
|
---|
1246 | if (lookup_available_drivers(&drivers_list,
|
---|
1247 | DRIVER_DEFAULT_STORE) == 0) {
|
---|
1248 | log_msg(LVL_FATAL, "No drivers found.");
|
---|
1249 | return false;
|
---|
1250 | }
|
---|
1251 |
|
---|
1252 | log_msg(LVL_DEBUG, "devman_init - list of drivers has been initialized.");
|
---|
1253 |
|
---|
1254 | /* Create root device node. */
|
---|
1255 | if (!init_device_tree(&device_tree, &drivers_list)) {
|
---|
1256 | log_msg(LVL_FATAL, "Failed to initialize device tree.");
|
---|
1257 | return false;
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 | /*
|
---|
1261 | * !!! devman_connection ... as the device manager is not a real loc
|
---|
1262 | * driver (it uses a completely different ipc protocol than an ordinary
|
---|
1263 | * loc driver) forwarding a connection from client to the devman by
|
---|
1264 | * location service would not work.
|
---|
1265 | */
|
---|
1266 | loc_server_register(NAME, devman_connection);
|
---|
1267 |
|
---|
1268 | return true;
|
---|
1269 | }
|
---|
1270 |
|
---|
1271 | int main(int argc, char *argv[])
|
---|
1272 | {
|
---|
1273 | printf(NAME ": HelenOS Device Manager\n");
|
---|
1274 |
|
---|
1275 | if (log_init(NAME, LVL_WARN) != EOK) {
|
---|
1276 | printf(NAME ": Error initializing logging subsystem.\n");
|
---|
1277 | return -1;
|
---|
1278 | }
|
---|
1279 |
|
---|
1280 | if (!devman_init()) {
|
---|
1281 | log_msg(LVL_ERROR, "Error while initializing service.");
|
---|
1282 | return -1;
|
---|
1283 | }
|
---|
1284 |
|
---|
1285 | /* Set handlers for incoming connections. */
|
---|
1286 | async_set_client_data_constructor(devman_client_data_create);
|
---|
1287 | async_set_client_data_destructor(devman_client_data_destroy);
|
---|
1288 | async_set_client_connection(devman_connection);
|
---|
1289 |
|
---|
1290 | /* Register device manager at naming service. */
|
---|
1291 | if (service_register(SERVICE_DEVMAN) != EOK) {
|
---|
1292 | log_msg(LVL_ERROR, "Failed registering as a service.");
|
---|
1293 | return -1;
|
---|
1294 | }
|
---|
1295 |
|
---|
1296 | printf(NAME ": Accepting connections.\n");
|
---|
1297 | task_retval(0);
|
---|
1298 | async_manager();
|
---|
1299 |
|
---|
1300 | /* Never reached. */
|
---|
1301 | return 0;
|
---|
1302 | }
|
---|
1303 |
|
---|
1304 | /** @}
|
---|
1305 | */
|
---|