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 | return EOK;
|
---|
237 | }
|
---|
238 |
|
---|
239 | /** Handle function registration.
|
---|
240 | *
|
---|
241 | * Child devices are registered by their parent's device driver.
|
---|
242 | */
|
---|
243 | static void devman_add_function(ipc_callid_t callid, ipc_call_t *call)
|
---|
244 | {
|
---|
245 | fun_type_t ftype = (fun_type_t) IPC_GET_ARG1(*call);
|
---|
246 | devman_handle_t dev_handle = IPC_GET_ARG2(*call);
|
---|
247 | sysarg_t match_count = IPC_GET_ARG3(*call);
|
---|
248 | dev_tree_t *tree = &device_tree;
|
---|
249 |
|
---|
250 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
251 |
|
---|
252 | dev_node_t *dev = NULL;
|
---|
253 | dev_node_t *pdev = find_dev_node_no_lock(&device_tree, dev_handle);
|
---|
254 |
|
---|
255 | if (pdev == NULL) {
|
---|
256 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
257 | async_answer_0(callid, ENOENT);
|
---|
258 | return;
|
---|
259 | }
|
---|
260 |
|
---|
261 | if (ftype != fun_inner && ftype != fun_exposed) {
|
---|
262 | /* Unknown function type */
|
---|
263 | log_msg(LVL_ERROR,
|
---|
264 | "Unknown function type %d provided by driver.",
|
---|
265 | (int) ftype);
|
---|
266 |
|
---|
267 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
268 | async_answer_0(callid, EINVAL);
|
---|
269 | return;
|
---|
270 | }
|
---|
271 |
|
---|
272 | char *fun_name = NULL;
|
---|
273 | int rc = async_data_write_accept((void **)&fun_name, true, 0, 0, 0, 0);
|
---|
274 | if (rc != EOK) {
|
---|
275 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
276 | async_answer_0(callid, rc);
|
---|
277 | return;
|
---|
278 | }
|
---|
279 |
|
---|
280 | /* Check that function with same name is not there already. */
|
---|
281 | if (find_fun_node_in_device(pdev, fun_name) != NULL) {
|
---|
282 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
283 | async_answer_0(callid, EEXISTS);
|
---|
284 | printf(NAME ": Warning, driver tried to register `%s' twice.\n",
|
---|
285 | fun_name);
|
---|
286 | free(fun_name);
|
---|
287 | return;
|
---|
288 | }
|
---|
289 |
|
---|
290 | fun_node_t *fun = create_fun_node();
|
---|
291 | fun->ftype = ftype;
|
---|
292 |
|
---|
293 | if (!insert_fun_node(&device_tree, fun, fun_name, pdev)) {
|
---|
294 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
295 | delete_fun_node(fun);
|
---|
296 | async_answer_0(callid, ENOMEM);
|
---|
297 | return;
|
---|
298 | }
|
---|
299 |
|
---|
300 | if (ftype == fun_inner) {
|
---|
301 | dev = create_dev_node();
|
---|
302 | if (dev == NULL) {
|
---|
303 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
304 | delete_fun_node(fun);
|
---|
305 | async_answer_0(callid, ENOMEM);
|
---|
306 | return;
|
---|
307 | }
|
---|
308 |
|
---|
309 | insert_dev_node(tree, dev, fun);
|
---|
310 | }
|
---|
311 |
|
---|
312 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
313 |
|
---|
314 | log_msg(LVL_DEBUG, "devman_add_function(fun=\"%s\")", fun->pathname);
|
---|
315 |
|
---|
316 | devman_receive_match_ids(match_count, &fun->match_ids);
|
---|
317 |
|
---|
318 | if (ftype == fun_inner) {
|
---|
319 | assert(dev != NULL);
|
---|
320 | /*
|
---|
321 | * Try to find a suitable driver and assign it to the device. We do
|
---|
322 | * not want to block the current fibril that is used for processing
|
---|
323 | * incoming calls: we will launch a separate fibril to handle the
|
---|
324 | * driver assigning. That is because assign_driver can actually include
|
---|
325 | * task spawning which could take some time.
|
---|
326 | */
|
---|
327 | fid_t assign_fibril = fibril_create(assign_driver_fibril, dev);
|
---|
328 | if (assign_fibril == 0) {
|
---|
329 | /*
|
---|
330 | * Fallback in case we are out of memory.
|
---|
331 | * Probably not needed as we will die soon anyway ;-).
|
---|
332 | */
|
---|
333 | (void) assign_driver_fibril(fun);
|
---|
334 | } else {
|
---|
335 | fibril_add_ready(assign_fibril);
|
---|
336 | }
|
---|
337 | } else {
|
---|
338 | loc_register_tree_function(fun, tree);
|
---|
339 | }
|
---|
340 |
|
---|
341 | /* Return device handle to parent's driver. */
|
---|
342 | async_answer_1(callid, EOK, fun->handle);
|
---|
343 | }
|
---|
344 |
|
---|
345 | static void devman_add_function_to_cat(ipc_callid_t callid, ipc_call_t *call)
|
---|
346 | {
|
---|
347 | devman_handle_t handle = IPC_GET_ARG1(*call);
|
---|
348 | category_id_t cat_id;
|
---|
349 | int rc;
|
---|
350 |
|
---|
351 | /* Get category name. */
|
---|
352 | char *cat_name;
|
---|
353 | rc = async_data_write_accept((void **) &cat_name, true,
|
---|
354 | 0, 0, 0, 0);
|
---|
355 | if (rc != EOK) {
|
---|
356 | async_answer_0(callid, rc);
|
---|
357 | return;
|
---|
358 | }
|
---|
359 |
|
---|
360 | fun_node_t *fun = find_fun_node(&device_tree, handle);
|
---|
361 | if (fun == NULL) {
|
---|
362 | async_answer_0(callid, ENOENT);
|
---|
363 | return;
|
---|
364 | }
|
---|
365 |
|
---|
366 | rc = loc_category_get_id(cat_name, &cat_id, IPC_FLAG_BLOCKING);
|
---|
367 | if (rc == EOK) {
|
---|
368 | loc_service_add_to_cat(fun->service_id, cat_id);
|
---|
369 | } else {
|
---|
370 | log_msg(LVL_ERROR, "Failed adding function `%s' to category "
|
---|
371 | "`%s'.", fun->pathname, cat_name);
|
---|
372 | }
|
---|
373 |
|
---|
374 | log_msg(LVL_NOTE, "Function `%s' added to category `%s'.",
|
---|
375 | fun->pathname, cat_name);
|
---|
376 |
|
---|
377 | async_answer_0(callid, EOK);
|
---|
378 | }
|
---|
379 |
|
---|
380 | /** Remove function. */
|
---|
381 | static void devman_remove_function(ipc_callid_t callid, ipc_call_t *call)
|
---|
382 | {
|
---|
383 | devman_handle_t fun_handle = IPC_GET_ARG1(*call);
|
---|
384 | dev_tree_t *tree = &device_tree;
|
---|
385 | int rc;
|
---|
386 |
|
---|
387 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
388 |
|
---|
389 | fun_node_t *fun = find_fun_node_no_lock(&device_tree, fun_handle);
|
---|
390 | if (fun == NULL) {
|
---|
391 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
392 | async_answer_0(callid, ENOENT);
|
---|
393 | return;
|
---|
394 | }
|
---|
395 |
|
---|
396 | log_msg(LVL_DEBUG, "devman_remove_function(fun='%s')", fun->pathname);
|
---|
397 |
|
---|
398 | if (fun->ftype == fun_inner) {
|
---|
399 | /* Handle possible descendants */
|
---|
400 | /* TODO */
|
---|
401 | log_msg(LVL_WARN, "devman_remove_function(): not handling "
|
---|
402 | "descendants\n");
|
---|
403 | } else {
|
---|
404 | /* Unregister from location service */
|
---|
405 | rc = loc_service_unregister(fun->service_id);
|
---|
406 | if (rc != EOK) {
|
---|
407 | log_msg(LVL_ERROR, "Failed unregistering tree service.");
|
---|
408 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
409 | async_answer_0(callid, EIO);
|
---|
410 | return;
|
---|
411 | }
|
---|
412 | }
|
---|
413 |
|
---|
414 | remove_fun_node(&device_tree, fun);
|
---|
415 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
416 | delete_fun_node(fun);
|
---|
417 |
|
---|
418 | log_msg(LVL_DEBUG, "devman_remove_function() succeeded.");
|
---|
419 | async_answer_0(callid, EOK);
|
---|
420 | }
|
---|
421 |
|
---|
422 | /** Initialize driver which has registered itself as running and ready.
|
---|
423 | *
|
---|
424 | * The initialization is done in a separate fibril to avoid deadlocks (if the
|
---|
425 | * driver needed to be served by devman during the driver's initialization).
|
---|
426 | */
|
---|
427 | static int init_running_drv(void *drv)
|
---|
428 | {
|
---|
429 | driver_t *driver = (driver_t *) drv;
|
---|
430 |
|
---|
431 | initialize_running_driver(driver, &device_tree);
|
---|
432 | log_msg(LVL_DEBUG, "The `%s` driver was successfully initialized.",
|
---|
433 | driver->name);
|
---|
434 | return 0;
|
---|
435 | }
|
---|
436 |
|
---|
437 | /** Function for handling connections from a driver to the device manager. */
|
---|
438 | static void devman_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
|
---|
439 | {
|
---|
440 | client_t *client;
|
---|
441 | driver_t *driver;
|
---|
442 |
|
---|
443 | /* Accept the connection. */
|
---|
444 | async_answer_0(iid, EOK);
|
---|
445 |
|
---|
446 | client = async_get_client_data();
|
---|
447 | if (client == NULL) {
|
---|
448 | log_msg(LVL_ERROR, "Failed to allocate client data.");
|
---|
449 | return;
|
---|
450 | }
|
---|
451 |
|
---|
452 | while (true) {
|
---|
453 | ipc_call_t call;
|
---|
454 | ipc_callid_t callid = async_get_call(&call);
|
---|
455 |
|
---|
456 | if (!IPC_GET_IMETHOD(call))
|
---|
457 | break;
|
---|
458 |
|
---|
459 | if (IPC_GET_IMETHOD(call) != DEVMAN_DRIVER_REGISTER) {
|
---|
460 | fibril_mutex_lock(&client->mutex);
|
---|
461 | driver = client->driver;
|
---|
462 | fibril_mutex_unlock(&client->mutex);
|
---|
463 | if (driver == NULL) {
|
---|
464 | /* First call must be to DEVMAN_DRIVER_REGISTER */
|
---|
465 | async_answer_0(callid, ENOTSUP);
|
---|
466 | continue;
|
---|
467 | }
|
---|
468 | }
|
---|
469 |
|
---|
470 | switch (IPC_GET_IMETHOD(call)) {
|
---|
471 | case DEVMAN_DRIVER_REGISTER:
|
---|
472 | fibril_mutex_lock(&client->mutex);
|
---|
473 | if (client->driver != NULL) {
|
---|
474 | fibril_mutex_unlock(&client->mutex);
|
---|
475 | async_answer_0(callid, EINVAL);
|
---|
476 | continue;
|
---|
477 | }
|
---|
478 | client->driver = devman_driver_register(callid, &call);
|
---|
479 | fibril_mutex_unlock(&client->mutex);
|
---|
480 | break;
|
---|
481 | case DEVMAN_ADD_FUNCTION:
|
---|
482 | devman_add_function(callid, &call);
|
---|
483 | break;
|
---|
484 | case DEVMAN_ADD_DEVICE_TO_CATEGORY:
|
---|
485 | devman_add_function_to_cat(callid, &call);
|
---|
486 | break;
|
---|
487 | case DEVMAN_REMOVE_FUNCTION:
|
---|
488 | devman_remove_function(callid, &call);
|
---|
489 | break;
|
---|
490 | default:
|
---|
491 | async_answer_0(callid, EINVAL);
|
---|
492 | break;
|
---|
493 | }
|
---|
494 | }
|
---|
495 | }
|
---|
496 |
|
---|
497 | /** Find handle for the device instance identified by the device's path in the
|
---|
498 | * device tree. */
|
---|
499 | static void devman_function_get_handle(ipc_callid_t iid, ipc_call_t *icall)
|
---|
500 | {
|
---|
501 | char *pathname;
|
---|
502 |
|
---|
503 | int rc = async_data_write_accept((void **) &pathname, true, 0, 0, 0, 0);
|
---|
504 | if (rc != EOK) {
|
---|
505 | async_answer_0(iid, rc);
|
---|
506 | return;
|
---|
507 | }
|
---|
508 |
|
---|
509 | fun_node_t *fun = find_fun_node_by_path(&device_tree, pathname);
|
---|
510 |
|
---|
511 | free(pathname);
|
---|
512 |
|
---|
513 | if (fun == NULL) {
|
---|
514 | async_answer_0(iid, ENOENT);
|
---|
515 | return;
|
---|
516 | }
|
---|
517 |
|
---|
518 | async_answer_1(iid, EOK, fun->handle);
|
---|
519 | }
|
---|
520 |
|
---|
521 | /** Get device name. */
|
---|
522 | static void devman_fun_get_name(ipc_callid_t iid, ipc_call_t *icall)
|
---|
523 | {
|
---|
524 | devman_handle_t handle = IPC_GET_ARG1(*icall);
|
---|
525 |
|
---|
526 | fun_node_t *fun = find_fun_node(&device_tree, handle);
|
---|
527 | if (fun == NULL) {
|
---|
528 | async_answer_0(iid, ENOMEM);
|
---|
529 | return;
|
---|
530 | }
|
---|
531 |
|
---|
532 | ipc_callid_t data_callid;
|
---|
533 | size_t data_len;
|
---|
534 | if (!async_data_read_receive(&data_callid, &data_len)) {
|
---|
535 | async_answer_0(iid, EINVAL);
|
---|
536 | return;
|
---|
537 | }
|
---|
538 |
|
---|
539 | void *buffer = malloc(data_len);
|
---|
540 | if (buffer == NULL) {
|
---|
541 | async_answer_0(data_callid, ENOMEM);
|
---|
542 | async_answer_0(iid, ENOMEM);
|
---|
543 | return;
|
---|
544 | }
|
---|
545 |
|
---|
546 | size_t sent_length = str_size(fun->name);
|
---|
547 | if (sent_length > data_len) {
|
---|
548 | sent_length = data_len;
|
---|
549 | }
|
---|
550 |
|
---|
551 | async_data_read_finalize(data_callid, fun->name, sent_length);
|
---|
552 | async_answer_0(iid, EOK);
|
---|
553 |
|
---|
554 | free(buffer);
|
---|
555 | }
|
---|
556 |
|
---|
557 |
|
---|
558 | /** Get device path. */
|
---|
559 | static void devman_fun_get_path(ipc_callid_t iid, ipc_call_t *icall)
|
---|
560 | {
|
---|
561 | devman_handle_t handle = IPC_GET_ARG1(*icall);
|
---|
562 |
|
---|
563 | fun_node_t *fun = find_fun_node(&device_tree, handle);
|
---|
564 | if (fun == NULL) {
|
---|
565 | async_answer_0(iid, ENOMEM);
|
---|
566 | return;
|
---|
567 | }
|
---|
568 |
|
---|
569 | ipc_callid_t data_callid;
|
---|
570 | size_t data_len;
|
---|
571 | if (!async_data_read_receive(&data_callid, &data_len)) {
|
---|
572 | async_answer_0(iid, EINVAL);
|
---|
573 | return;
|
---|
574 | }
|
---|
575 |
|
---|
576 | void *buffer = malloc(data_len);
|
---|
577 | if (buffer == NULL) {
|
---|
578 | async_answer_0(data_callid, ENOMEM);
|
---|
579 | async_answer_0(iid, ENOMEM);
|
---|
580 | return;
|
---|
581 | }
|
---|
582 |
|
---|
583 | size_t sent_length = str_size(fun->pathname);
|
---|
584 | if (sent_length > data_len) {
|
---|
585 | sent_length = data_len;
|
---|
586 | }
|
---|
587 |
|
---|
588 | async_data_read_finalize(data_callid, fun->pathname, sent_length);
|
---|
589 | async_answer_0(iid, EOK);
|
---|
590 |
|
---|
591 | free(buffer);
|
---|
592 | }
|
---|
593 |
|
---|
594 | static void devman_dev_get_functions(ipc_callid_t iid, ipc_call_t *icall)
|
---|
595 | {
|
---|
596 | ipc_callid_t callid;
|
---|
597 | size_t size;
|
---|
598 | size_t act_size;
|
---|
599 | int rc;
|
---|
600 |
|
---|
601 | if (!async_data_read_receive(&callid, &size)) {
|
---|
602 | async_answer_0(callid, EREFUSED);
|
---|
603 | async_answer_0(iid, EREFUSED);
|
---|
604 | return;
|
---|
605 | }
|
---|
606 |
|
---|
607 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
608 |
|
---|
609 | dev_node_t *dev = find_dev_node_no_lock(&device_tree,
|
---|
610 | IPC_GET_ARG1(*icall));
|
---|
611 | if (dev == NULL) {
|
---|
612 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
613 | async_answer_0(callid, ENOENT);
|
---|
614 | async_answer_0(iid, ENOENT);
|
---|
615 | return;
|
---|
616 | }
|
---|
617 |
|
---|
618 | devman_handle_t *hdl_buf = (devman_handle_t *) malloc(size);
|
---|
619 | if (hdl_buf == NULL) {
|
---|
620 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
621 | async_answer_0(callid, ENOMEM);
|
---|
622 | async_answer_0(iid, ENOMEM);
|
---|
623 | return;
|
---|
624 | }
|
---|
625 |
|
---|
626 | rc = dev_get_functions(&device_tree, dev, hdl_buf, size, &act_size);
|
---|
627 | if (rc != EOK) {
|
---|
628 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
629 | async_answer_0(callid, rc);
|
---|
630 | async_answer_0(iid, rc);
|
---|
631 | return;
|
---|
632 | }
|
---|
633 |
|
---|
634 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
635 |
|
---|
636 | sysarg_t retval = async_data_read_finalize(callid, hdl_buf, size);
|
---|
637 | free(hdl_buf);
|
---|
638 |
|
---|
639 | async_answer_1(iid, retval, act_size);
|
---|
640 | }
|
---|
641 |
|
---|
642 |
|
---|
643 | /** Get handle for child device of a function. */
|
---|
644 | static void devman_fun_get_child(ipc_callid_t iid, ipc_call_t *icall)
|
---|
645 | {
|
---|
646 | fun_node_t *fun;
|
---|
647 |
|
---|
648 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
649 |
|
---|
650 | fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));
|
---|
651 | if (fun == NULL) {
|
---|
652 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
653 | async_answer_0(iid, ENOENT);
|
---|
654 | return;
|
---|
655 | }
|
---|
656 |
|
---|
657 | if (fun->child == NULL) {
|
---|
658 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
659 | async_answer_0(iid, ENOENT);
|
---|
660 | return;
|
---|
661 | }
|
---|
662 |
|
---|
663 | async_answer_1(iid, EOK, fun->child->handle);
|
---|
664 |
|
---|
665 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
666 | }
|
---|
667 |
|
---|
668 | /** Find handle for the function instance identified by its service ID. */
|
---|
669 | static void devman_fun_sid_to_handle(ipc_callid_t iid, ipc_call_t *icall)
|
---|
670 | {
|
---|
671 | fun_node_t *fun;
|
---|
672 |
|
---|
673 | fun = find_loc_tree_function(&device_tree, IPC_GET_ARG1(*icall));
|
---|
674 |
|
---|
675 | if (fun == NULL) {
|
---|
676 | async_answer_0(iid, ENOENT);
|
---|
677 | return;
|
---|
678 | }
|
---|
679 |
|
---|
680 | async_answer_1(iid, EOK, fun->handle);
|
---|
681 | }
|
---|
682 |
|
---|
683 | /** Function for handling connections from a client to the device manager. */
|
---|
684 | static void devman_connection_client(ipc_callid_t iid, ipc_call_t *icall)
|
---|
685 | {
|
---|
686 | /* Accept connection. */
|
---|
687 | async_answer_0(iid, EOK);
|
---|
688 |
|
---|
689 | while (true) {
|
---|
690 | ipc_call_t call;
|
---|
691 | ipc_callid_t callid = async_get_call(&call);
|
---|
692 |
|
---|
693 | if (!IPC_GET_IMETHOD(call))
|
---|
694 | break;
|
---|
695 |
|
---|
696 | switch (IPC_GET_IMETHOD(call)) {
|
---|
697 | case DEVMAN_DEVICE_GET_HANDLE:
|
---|
698 | devman_function_get_handle(callid, &call);
|
---|
699 | break;
|
---|
700 | case DEVMAN_DEV_GET_FUNCTIONS:
|
---|
701 | devman_dev_get_functions(callid, &call);
|
---|
702 | break;
|
---|
703 | case DEVMAN_FUN_GET_CHILD:
|
---|
704 | devman_fun_get_child(callid, &call);
|
---|
705 | break;
|
---|
706 | case DEVMAN_FUN_GET_NAME:
|
---|
707 | devman_fun_get_name(callid, &call);
|
---|
708 | break;
|
---|
709 | case DEVMAN_FUN_GET_PATH:
|
---|
710 | devman_fun_get_path(callid, &call);
|
---|
711 | break;
|
---|
712 | case DEVMAN_FUN_SID_TO_HANDLE:
|
---|
713 | devman_fun_sid_to_handle(callid, &call);
|
---|
714 | break;
|
---|
715 | default:
|
---|
716 | async_answer_0(callid, ENOENT);
|
---|
717 | }
|
---|
718 | }
|
---|
719 | }
|
---|
720 |
|
---|
721 | static void devman_forward(ipc_callid_t iid, ipc_call_t *icall,
|
---|
722 | bool drv_to_parent)
|
---|
723 | {
|
---|
724 | devman_handle_t handle = IPC_GET_ARG2(*icall);
|
---|
725 | devman_handle_t fwd_h;
|
---|
726 | fun_node_t *fun = NULL;
|
---|
727 | dev_node_t *dev = NULL;
|
---|
728 |
|
---|
729 | fun = find_fun_node(&device_tree, handle);
|
---|
730 | if (fun == NULL)
|
---|
731 | dev = find_dev_node(&device_tree, handle);
|
---|
732 | else
|
---|
733 | dev = fun->dev;
|
---|
734 |
|
---|
735 | /*
|
---|
736 | * For a valid function to connect to we need a device. The root
|
---|
737 | * function, for example, has no device and cannot be connected to.
|
---|
738 | * This means @c dev needs to be valid regardless whether we are
|
---|
739 | * connecting to a device or to a function.
|
---|
740 | */
|
---|
741 | if (dev == NULL) {
|
---|
742 | log_msg(LVL_ERROR, "IPC forwarding failed - no device or "
|
---|
743 | "function with handle %" PRIun " was found.", handle);
|
---|
744 | async_answer_0(iid, ENOENT);
|
---|
745 | return;
|
---|
746 | }
|
---|
747 |
|
---|
748 | if (fun == NULL && !drv_to_parent) {
|
---|
749 | log_msg(LVL_ERROR, NAME ": devman_forward error - cannot "
|
---|
750 | "connect to handle %" PRIun ", refers to a device.",
|
---|
751 | handle);
|
---|
752 | async_answer_0(iid, ENOENT);
|
---|
753 | return;
|
---|
754 | }
|
---|
755 |
|
---|
756 | driver_t *driver = NULL;
|
---|
757 |
|
---|
758 | if (drv_to_parent) {
|
---|
759 | /* Connect to parent function of a device (or device function). */
|
---|
760 | if (dev->pfun->dev != NULL)
|
---|
761 | driver = dev->pfun->dev->drv;
|
---|
762 | fwd_h = dev->pfun->handle;
|
---|
763 | } else if (dev->state == DEVICE_USABLE) {
|
---|
764 | /* Connect to the specified function */
|
---|
765 | driver = dev->drv;
|
---|
766 | assert(driver != NULL);
|
---|
767 |
|
---|
768 | fwd_h = handle;
|
---|
769 | }
|
---|
770 |
|
---|
771 | if (driver == NULL) {
|
---|
772 | log_msg(LVL_ERROR, "IPC forwarding refused - " \
|
---|
773 | "the device %" PRIun " is not in usable state.", handle);
|
---|
774 | async_answer_0(iid, ENOENT);
|
---|
775 | return;
|
---|
776 | }
|
---|
777 |
|
---|
778 | int method;
|
---|
779 | if (drv_to_parent)
|
---|
780 | method = DRIVER_DRIVER;
|
---|
781 | else
|
---|
782 | method = DRIVER_CLIENT;
|
---|
783 |
|
---|
784 | if (!driver->sess) {
|
---|
785 | log_msg(LVL_ERROR,
|
---|
786 | "Could not forward to driver `%s'.", driver->name);
|
---|
787 | async_answer_0(iid, EINVAL);
|
---|
788 | return;
|
---|
789 | }
|
---|
790 |
|
---|
791 | if (fun != NULL) {
|
---|
792 | log_msg(LVL_DEBUG,
|
---|
793 | "Forwarding request for `%s' function to driver `%s'.",
|
---|
794 | fun->pathname, driver->name);
|
---|
795 | } else {
|
---|
796 | log_msg(LVL_DEBUG,
|
---|
797 | "Forwarding request for `%s' device to driver `%s'.",
|
---|
798 | dev->pfun->pathname, driver->name);
|
---|
799 | }
|
---|
800 |
|
---|
801 | async_exch_t *exch = async_exchange_begin(driver->sess);
|
---|
802 | async_forward_fast(iid, exch, method, fwd_h, 0, IPC_FF_NONE);
|
---|
803 | async_exchange_end(exch);
|
---|
804 | }
|
---|
805 |
|
---|
806 | /** Function for handling connections from a client forwarded by the location
|
---|
807 | * service to the device manager. */
|
---|
808 | static void devman_connection_loc(ipc_callid_t iid, ipc_call_t *icall)
|
---|
809 | {
|
---|
810 | service_id_t service_id = IPC_GET_ARG2(*icall);
|
---|
811 | fun_node_t *fun;
|
---|
812 | dev_node_t *dev;
|
---|
813 |
|
---|
814 | fun = find_loc_tree_function(&device_tree, service_id);
|
---|
815 |
|
---|
816 | if (fun == NULL || fun->dev->drv == NULL) {
|
---|
817 | log_msg(LVL_WARN, "devman_connection_loc(): function "
|
---|
818 | "not found.\n");
|
---|
819 | async_answer_0(iid, ENOENT);
|
---|
820 | return;
|
---|
821 | }
|
---|
822 |
|
---|
823 | dev = fun->dev;
|
---|
824 |
|
---|
825 | async_exch_t *exch = async_exchange_begin(dev->drv->sess);
|
---|
826 | async_forward_fast(iid, exch, DRIVER_CLIENT, fun->handle, 0,
|
---|
827 | IPC_FF_NONE);
|
---|
828 | async_exchange_end(exch);
|
---|
829 |
|
---|
830 | log_msg(LVL_DEBUG,
|
---|
831 | "Forwarding loc service request for `%s' function to driver `%s'.",
|
---|
832 | fun->pathname, dev->drv->name);
|
---|
833 | }
|
---|
834 |
|
---|
835 | /** Function for handling connections to device manager. */
|
---|
836 | static void devman_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
837 | {
|
---|
838 | /* Select port. */
|
---|
839 | switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
|
---|
840 | case DEVMAN_DRIVER:
|
---|
841 | devman_connection_driver(iid, icall);
|
---|
842 | break;
|
---|
843 | case DEVMAN_CLIENT:
|
---|
844 | devman_connection_client(iid, icall);
|
---|
845 | break;
|
---|
846 | case DEVMAN_CONNECT_TO_DEVICE:
|
---|
847 | /* Connect client to selected device. */
|
---|
848 | devman_forward(iid, icall, false);
|
---|
849 | break;
|
---|
850 | case DEVMAN_CONNECT_FROM_LOC:
|
---|
851 | /* Someone connected through loc node. */
|
---|
852 | devman_connection_loc(iid, icall);
|
---|
853 | break;
|
---|
854 | case DEVMAN_CONNECT_TO_PARENTS_DEVICE:
|
---|
855 | /* Connect client to selected device. */
|
---|
856 | devman_forward(iid, icall, true);
|
---|
857 | break;
|
---|
858 | default:
|
---|
859 | /* No such interface */
|
---|
860 | async_answer_0(iid, ENOENT);
|
---|
861 | }
|
---|
862 | }
|
---|
863 |
|
---|
864 | static void *devman_client_data_create(void)
|
---|
865 | {
|
---|
866 | client_t *client;
|
---|
867 |
|
---|
868 | client = calloc(1, sizeof(client_t));
|
---|
869 | if (client == NULL)
|
---|
870 | return NULL;
|
---|
871 |
|
---|
872 | fibril_mutex_initialize(&client->mutex);
|
---|
873 | return client;
|
---|
874 | }
|
---|
875 |
|
---|
876 | static void devman_client_data_destroy(void *data)
|
---|
877 | {
|
---|
878 | free(data);
|
---|
879 | }
|
---|
880 |
|
---|
881 | /** Initialize device manager internal structures. */
|
---|
882 | static bool devman_init(void)
|
---|
883 | {
|
---|
884 | log_msg(LVL_DEBUG, "devman_init - looking for available drivers.");
|
---|
885 |
|
---|
886 | /* Initialize list of available drivers. */
|
---|
887 | init_driver_list(&drivers_list);
|
---|
888 | if (lookup_available_drivers(&drivers_list,
|
---|
889 | DRIVER_DEFAULT_STORE) == 0) {
|
---|
890 | log_msg(LVL_FATAL, "No drivers found.");
|
---|
891 | return false;
|
---|
892 | }
|
---|
893 |
|
---|
894 | log_msg(LVL_DEBUG, "devman_init - list of drivers has been initialized.");
|
---|
895 |
|
---|
896 | /* Create root device node. */
|
---|
897 | if (!init_device_tree(&device_tree, &drivers_list)) {
|
---|
898 | log_msg(LVL_FATAL, "Failed to initialize device tree.");
|
---|
899 | return false;
|
---|
900 | }
|
---|
901 |
|
---|
902 | /*
|
---|
903 | * !!! devman_connection ... as the device manager is not a real loc
|
---|
904 | * driver (it uses a completely different ipc protocol than an ordinary
|
---|
905 | * loc driver) forwarding a connection from client to the devman by
|
---|
906 | * location service would not work.
|
---|
907 | */
|
---|
908 | loc_server_register(NAME, devman_connection);
|
---|
909 |
|
---|
910 | return true;
|
---|
911 | }
|
---|
912 |
|
---|
913 | int main(int argc, char *argv[])
|
---|
914 | {
|
---|
915 | printf(NAME ": HelenOS Device Manager\n");
|
---|
916 |
|
---|
917 | if (log_init(NAME, LVL_WARN) != EOK) {
|
---|
918 | printf(NAME ": Error initializing logging subsystem.\n");
|
---|
919 | return -1;
|
---|
920 | }
|
---|
921 |
|
---|
922 | if (!devman_init()) {
|
---|
923 | log_msg(LVL_ERROR, "Error while initializing service.");
|
---|
924 | return -1;
|
---|
925 | }
|
---|
926 |
|
---|
927 | /* Set handlers for incoming connections. */
|
---|
928 | async_set_client_data_constructor(devman_client_data_create);
|
---|
929 | async_set_client_data_destructor(devman_client_data_destroy);
|
---|
930 | async_set_client_connection(devman_connection);
|
---|
931 |
|
---|
932 | /* Register device manager at naming service. */
|
---|
933 | if (service_register(SERVICE_DEVMAN) != EOK) {
|
---|
934 | log_msg(LVL_ERROR, "Failed registering as a service.");
|
---|
935 | return -1;
|
---|
936 | }
|
---|
937 |
|
---|
938 | printf(NAME ": Accepting connections.\n");
|
---|
939 | task_retval(0);
|
---|
940 | async_manager();
|
---|
941 |
|
---|
942 | /* Never reached. */
|
---|
943 | return 0;
|
---|
944 | }
|
---|
945 |
|
---|
946 | /** @}
|
---|
947 | */
|
---|