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 <ipc/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 <devmap.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 | static class_list_t class_list;
|
---|
67 |
|
---|
68 | /** Register running driver. */
|
---|
69 | static driver_t *devman_driver_register(void)
|
---|
70 | {
|
---|
71 | ipc_call_t icall;
|
---|
72 | ipc_callid_t iid;
|
---|
73 | driver_t *driver = NULL;
|
---|
74 |
|
---|
75 | log_msg(LVL_DEBUG, "devman_driver_register\n");
|
---|
76 |
|
---|
77 | iid = async_get_call(&icall);
|
---|
78 | if (IPC_GET_IMETHOD(icall) != DEVMAN_DRIVER_REGISTER) {
|
---|
79 | async_answer_0(iid, EREFUSED);
|
---|
80 | return NULL;
|
---|
81 | }
|
---|
82 |
|
---|
83 | char *drv_name = NULL;
|
---|
84 |
|
---|
85 | /* Get driver name. */
|
---|
86 | int rc = async_data_write_accept((void **) &drv_name, true, 0, 0, 0, 0);
|
---|
87 | if (rc != EOK) {
|
---|
88 | async_answer_0(iid, rc);
|
---|
89 | return NULL;
|
---|
90 | }
|
---|
91 |
|
---|
92 | log_msg(LVL_DEBUG, "The `%s' driver is trying to register.\n",
|
---|
93 | drv_name);
|
---|
94 |
|
---|
95 | /* Find driver structure. */
|
---|
96 | driver = find_driver(&drivers_list, drv_name);
|
---|
97 |
|
---|
98 | if (driver == NULL) {
|
---|
99 | log_msg(LVL_ERROR, "No driver named `%s' was found.\n", drv_name);
|
---|
100 | free(drv_name);
|
---|
101 | drv_name = NULL;
|
---|
102 | async_answer_0(iid, ENOENT);
|
---|
103 | return NULL;
|
---|
104 | }
|
---|
105 |
|
---|
106 | free(drv_name);
|
---|
107 | drv_name = NULL;
|
---|
108 |
|
---|
109 | /* Create connection to the driver. */
|
---|
110 | log_msg(LVL_DEBUG, "Creating connection to the `%s' driver.\n",
|
---|
111 | driver->name);
|
---|
112 | ipc_call_t call;
|
---|
113 | ipc_callid_t callid = async_get_call(&call);
|
---|
114 | if (IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) {
|
---|
115 | async_answer_0(callid, ENOTSUP);
|
---|
116 | async_answer_0(iid, ENOTSUP);
|
---|
117 | return NULL;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /* Remember driver's phone. */
|
---|
121 | set_driver_phone(driver, IPC_GET_ARG5(call));
|
---|
122 |
|
---|
123 | log_msg(LVL_NOTE,
|
---|
124 | "The `%s' driver was successfully registered as running.\n",
|
---|
125 | driver->name);
|
---|
126 |
|
---|
127 | async_answer_0(callid, EOK);
|
---|
128 | async_answer_0(iid, EOK);
|
---|
129 |
|
---|
130 | return driver;
|
---|
131 | }
|
---|
132 |
|
---|
133 | /** Receive device match ID from the device's parent driver and add it to the
|
---|
134 | * list of devices match ids.
|
---|
135 | *
|
---|
136 | * @param match_ids The list of the device's match ids.
|
---|
137 | * @return Zero on success, negative error code otherwise.
|
---|
138 | */
|
---|
139 | static int devman_receive_match_id(match_id_list_t *match_ids)
|
---|
140 | {
|
---|
141 | match_id_t *match_id = create_match_id();
|
---|
142 | ipc_callid_t callid;
|
---|
143 | ipc_call_t call;
|
---|
144 | int rc = 0;
|
---|
145 |
|
---|
146 | callid = async_get_call(&call);
|
---|
147 | if (DEVMAN_ADD_MATCH_ID != IPC_GET_IMETHOD(call)) {
|
---|
148 | log_msg(LVL_ERROR,
|
---|
149 | "Invalid protocol when trying to receive match id.\n");
|
---|
150 | async_answer_0(callid, EINVAL);
|
---|
151 | delete_match_id(match_id);
|
---|
152 | return EINVAL;
|
---|
153 | }
|
---|
154 |
|
---|
155 | if (match_id == NULL) {
|
---|
156 | log_msg(LVL_ERROR, "Failed to allocate match id.\n");
|
---|
157 | async_answer_0(callid, ENOMEM);
|
---|
158 | return ENOMEM;
|
---|
159 | }
|
---|
160 |
|
---|
161 | async_answer_0(callid, EOK);
|
---|
162 |
|
---|
163 | match_id->score = IPC_GET_ARG1(call);
|
---|
164 |
|
---|
165 | char *match_id_str;
|
---|
166 | rc = async_data_write_accept((void **) &match_id_str, true, 0, 0, 0, 0);
|
---|
167 | match_id->id = match_id_str;
|
---|
168 | if (rc != EOK) {
|
---|
169 | delete_match_id(match_id);
|
---|
170 | log_msg(LVL_ERROR, "Failed to receive match id string: %s.\n",
|
---|
171 | str_error(rc));
|
---|
172 | return rc;
|
---|
173 | }
|
---|
174 |
|
---|
175 | list_append(&match_id->link, &match_ids->ids);
|
---|
176 |
|
---|
177 | log_msg(LVL_DEBUG, "Received match id `%s', score %d.\n",
|
---|
178 | match_id->id, match_id->score);
|
---|
179 | return rc;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /** Receive device match IDs from the device's parent driver and add them to the
|
---|
183 | * list of devices match ids.
|
---|
184 | *
|
---|
185 | * @param match_count The number of device's match ids to be received.
|
---|
186 | * @param match_ids The list of the device's match ids.
|
---|
187 | * @return Zero on success, negative error code otherwise.
|
---|
188 | */
|
---|
189 | static int devman_receive_match_ids(sysarg_t match_count,
|
---|
190 | match_id_list_t *match_ids)
|
---|
191 | {
|
---|
192 | int ret = EOK;
|
---|
193 | size_t i;
|
---|
194 |
|
---|
195 | for (i = 0; i < match_count; i++) {
|
---|
196 | if (EOK != (ret = devman_receive_match_id(match_ids)))
|
---|
197 | return ret;
|
---|
198 | }
|
---|
199 | return ret;
|
---|
200 | }
|
---|
201 |
|
---|
202 | static int assign_driver_fibril(void *arg)
|
---|
203 | {
|
---|
204 | dev_node_t *dev_node = (dev_node_t *) arg;
|
---|
205 | assign_driver(dev_node, &drivers_list, &device_tree);
|
---|
206 | return EOK;
|
---|
207 | }
|
---|
208 |
|
---|
209 | /** Handle function registration.
|
---|
210 | *
|
---|
211 | * Child devices are registered by their parent's device driver.
|
---|
212 | */
|
---|
213 | static void devman_add_function(ipc_callid_t callid, ipc_call_t *call)
|
---|
214 | {
|
---|
215 | fun_type_t ftype = (fun_type_t) IPC_GET_ARG1(*call);
|
---|
216 | devman_handle_t dev_handle = IPC_GET_ARG2(*call);
|
---|
217 | sysarg_t match_count = IPC_GET_ARG3(*call);
|
---|
218 | dev_tree_t *tree = &device_tree;
|
---|
219 |
|
---|
220 | fibril_rwlock_write_lock(&tree->rwlock);
|
---|
221 |
|
---|
222 | dev_node_t *dev = NULL;
|
---|
223 | dev_node_t *pdev = find_dev_node_no_lock(&device_tree, dev_handle);
|
---|
224 |
|
---|
225 | if (pdev == NULL) {
|
---|
226 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
227 | async_answer_0(callid, ENOENT);
|
---|
228 | return;
|
---|
229 | }
|
---|
230 |
|
---|
231 | if (ftype != fun_inner && ftype != fun_exposed) {
|
---|
232 | /* Unknown function type */
|
---|
233 | log_msg(LVL_ERROR,
|
---|
234 | "Unknown function type %d provided by driver.\n",
|
---|
235 | (int) ftype);
|
---|
236 |
|
---|
237 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
238 | async_answer_0(callid, EINVAL);
|
---|
239 | return;
|
---|
240 | }
|
---|
241 |
|
---|
242 | char *fun_name = NULL;
|
---|
243 | int rc = async_data_write_accept((void **)&fun_name, true, 0, 0, 0, 0);
|
---|
244 | if (rc != EOK) {
|
---|
245 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
246 | async_answer_0(callid, rc);
|
---|
247 | return;
|
---|
248 | }
|
---|
249 |
|
---|
250 | fun_node_t *fun = create_fun_node();
|
---|
251 | if (!insert_fun_node(&device_tree, fun, fun_name, pdev)) {
|
---|
252 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
253 | delete_fun_node(fun);
|
---|
254 | async_answer_0(callid, ENOMEM);
|
---|
255 | return;
|
---|
256 | }
|
---|
257 |
|
---|
258 | if (ftype == fun_inner) {
|
---|
259 | dev = create_dev_node();
|
---|
260 | if (dev == NULL) {
|
---|
261 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
262 | delete_fun_node(fun);
|
---|
263 | async_answer_0(callid, ENOMEM);
|
---|
264 | return;
|
---|
265 | }
|
---|
266 |
|
---|
267 | insert_dev_node(tree, dev, fun);
|
---|
268 | }
|
---|
269 |
|
---|
270 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
271 |
|
---|
272 | log_msg(LVL_DEBUG, "devman_add_function(fun=\"%s\")\n", fun->pathname);
|
---|
273 |
|
---|
274 | devman_receive_match_ids(match_count, &fun->match_ids);
|
---|
275 |
|
---|
276 | if (ftype == fun_inner) {
|
---|
277 | assert(dev != NULL);
|
---|
278 | /*
|
---|
279 | * Try to find a suitable driver and assign it to the device. We do
|
---|
280 | * not want to block the current fibril that is used for processing
|
---|
281 | * incoming calls: we will launch a separate fibril to handle the
|
---|
282 | * driver assigning. That is because assign_driver can actually include
|
---|
283 | * task spawning which could take some time.
|
---|
284 | */
|
---|
285 | fid_t assign_fibril = fibril_create(assign_driver_fibril, dev);
|
---|
286 | if (assign_fibril == 0) {
|
---|
287 | /*
|
---|
288 | * Fallback in case we are out of memory.
|
---|
289 | * Probably not needed as we will die soon anyway ;-).
|
---|
290 | */
|
---|
291 | (void) assign_driver_fibril(fun);
|
---|
292 | } else {
|
---|
293 | fibril_add_ready(assign_fibril);
|
---|
294 | }
|
---|
295 | } else {
|
---|
296 | devmap_register_tree_function(fun, tree);
|
---|
297 | }
|
---|
298 |
|
---|
299 | /* Return device handle to parent's driver. */
|
---|
300 | async_answer_1(callid, EOK, fun->handle);
|
---|
301 | }
|
---|
302 |
|
---|
303 | static void devmap_register_class_dev(dev_class_info_t *cli)
|
---|
304 | {
|
---|
305 | /* Create devmap path and name for the device. */
|
---|
306 | char *devmap_pathname = NULL;
|
---|
307 |
|
---|
308 | asprintf(&devmap_pathname, "%s/%s%c%s", DEVMAP_CLASS_NAMESPACE,
|
---|
309 | cli->dev_class->name, DEVMAP_SEPARATOR, cli->dev_name);
|
---|
310 | if (devmap_pathname == NULL)
|
---|
311 | return;
|
---|
312 |
|
---|
313 | /*
|
---|
314 | * Register the device by the device mapper and remember its devmap
|
---|
315 | * handle.
|
---|
316 | */
|
---|
317 | devmap_device_register_with_iface(devmap_pathname,
|
---|
318 | &cli->devmap_handle, DEVMAN_CONNECT_FROM_DEVMAP);
|
---|
319 |
|
---|
320 | /*
|
---|
321 | * Add device to the hash map of class devices registered by device
|
---|
322 | * mapper.
|
---|
323 | */
|
---|
324 | class_add_devmap_function(&class_list, cli);
|
---|
325 |
|
---|
326 | free(devmap_pathname);
|
---|
327 | }
|
---|
328 |
|
---|
329 | static void devman_add_function_to_class(ipc_callid_t callid, ipc_call_t *call)
|
---|
330 | {
|
---|
331 | devman_handle_t handle = IPC_GET_ARG1(*call);
|
---|
332 |
|
---|
333 | /* Get class name. */
|
---|
334 | char *class_name;
|
---|
335 | int rc = async_data_write_accept((void **) &class_name, true,
|
---|
336 | 0, 0, 0, 0);
|
---|
337 | if (rc != EOK) {
|
---|
338 | async_answer_0(callid, rc);
|
---|
339 | return;
|
---|
340 | }
|
---|
341 |
|
---|
342 | fun_node_t *fun = find_fun_node(&device_tree, handle);
|
---|
343 | if (fun == NULL) {
|
---|
344 | async_answer_0(callid, ENOENT);
|
---|
345 | return;
|
---|
346 | }
|
---|
347 |
|
---|
348 | dev_class_t *cl = get_dev_class(&class_list, class_name);
|
---|
349 | dev_class_info_t *class_info = add_function_to_class(fun, cl, NULL);
|
---|
350 |
|
---|
351 | /* Register the device's class alias by devmapper. */
|
---|
352 | devmap_register_class_dev(class_info);
|
---|
353 |
|
---|
354 | log_msg(LVL_NOTE, "Function `%s' added to class `%s' as `%s'.\n",
|
---|
355 | fun->pathname, class_name, class_info->dev_name);
|
---|
356 |
|
---|
357 | async_answer_0(callid, EOK);
|
---|
358 | }
|
---|
359 |
|
---|
360 | /** Initialize driver which has registered itself as running and ready.
|
---|
361 | *
|
---|
362 | * The initialization is done in a separate fibril to avoid deadlocks (if the
|
---|
363 | * driver needed to be served by devman during the driver's initialization).
|
---|
364 | */
|
---|
365 | static int init_running_drv(void *drv)
|
---|
366 | {
|
---|
367 | driver_t *driver = (driver_t *) drv;
|
---|
368 |
|
---|
369 | initialize_running_driver(driver, &device_tree);
|
---|
370 | log_msg(LVL_DEBUG, "The `%s` driver was successfully initialized.\n",
|
---|
371 | driver->name);
|
---|
372 | return 0;
|
---|
373 | }
|
---|
374 |
|
---|
375 | /** Function for handling connections from a driver to the device manager. */
|
---|
376 | static void devman_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
|
---|
377 | {
|
---|
378 | /* Accept the connection. */
|
---|
379 | async_answer_0(iid, EOK);
|
---|
380 |
|
---|
381 | driver_t *driver = devman_driver_register();
|
---|
382 | if (driver == NULL)
|
---|
383 | return;
|
---|
384 |
|
---|
385 | /*
|
---|
386 | * Initialize the driver as running (e.g. pass assigned devices to it)
|
---|
387 | * in a separate fibril; the separate fibril is used to enable the
|
---|
388 | * driver to use devman service during the driver's initialization.
|
---|
389 | */
|
---|
390 | fid_t fid = fibril_create(init_running_drv, driver);
|
---|
391 | if (fid == 0) {
|
---|
392 | log_msg(LVL_ERROR, "Failed to create initialization fibril " \
|
---|
393 | "for driver `%s' .\n", driver->name);
|
---|
394 | return;
|
---|
395 | }
|
---|
396 | fibril_add_ready(fid);
|
---|
397 |
|
---|
398 | ipc_callid_t callid;
|
---|
399 | ipc_call_t call;
|
---|
400 | bool cont = true;
|
---|
401 | while (cont) {
|
---|
402 | callid = async_get_call(&call);
|
---|
403 |
|
---|
404 | switch (IPC_GET_IMETHOD(call)) {
|
---|
405 | case IPC_M_PHONE_HUNGUP:
|
---|
406 | cont = false;
|
---|
407 | continue;
|
---|
408 | case DEVMAN_ADD_FUNCTION:
|
---|
409 | devman_add_function(callid, &call);
|
---|
410 | break;
|
---|
411 | case DEVMAN_ADD_DEVICE_TO_CLASS:
|
---|
412 | devman_add_function_to_class(callid, &call);
|
---|
413 | break;
|
---|
414 | default:
|
---|
415 | async_answer_0(callid, EINVAL);
|
---|
416 | break;
|
---|
417 | }
|
---|
418 | }
|
---|
419 | }
|
---|
420 |
|
---|
421 | /** Find handle for the device instance identified by the device's path in the
|
---|
422 | * device tree. */
|
---|
423 | static void devman_function_get_handle(ipc_callid_t iid, ipc_call_t *icall)
|
---|
424 | {
|
---|
425 | char *pathname;
|
---|
426 |
|
---|
427 | int rc = async_data_write_accept((void **) &pathname, true, 0, 0, 0, 0);
|
---|
428 | if (rc != EOK) {
|
---|
429 | async_answer_0(iid, rc);
|
---|
430 | return;
|
---|
431 | }
|
---|
432 |
|
---|
433 | fun_node_t *fun = find_fun_node_by_path(&device_tree, pathname);
|
---|
434 |
|
---|
435 | free(pathname);
|
---|
436 |
|
---|
437 | if (fun == NULL) {
|
---|
438 | async_answer_0(iid, ENOENT);
|
---|
439 | return;
|
---|
440 | }
|
---|
441 |
|
---|
442 | async_answer_1(iid, EOK, fun->handle);
|
---|
443 | }
|
---|
444 |
|
---|
445 |
|
---|
446 | /** Function for handling connections from a client to the device manager. */
|
---|
447 | static void devman_connection_client(ipc_callid_t iid, ipc_call_t *icall)
|
---|
448 | {
|
---|
449 | /* Accept connection. */
|
---|
450 | async_answer_0(iid, EOK);
|
---|
451 |
|
---|
452 | bool cont = true;
|
---|
453 | while (cont) {
|
---|
454 | ipc_call_t call;
|
---|
455 | ipc_callid_t callid = async_get_call(&call);
|
---|
456 |
|
---|
457 | switch (IPC_GET_IMETHOD(call)) {
|
---|
458 | case IPC_M_PHONE_HUNGUP:
|
---|
459 | cont = false;
|
---|
460 | continue;
|
---|
461 | case DEVMAN_DEVICE_GET_HANDLE:
|
---|
462 | devman_function_get_handle(callid, &call);
|
---|
463 | break;
|
---|
464 | default:
|
---|
465 | async_answer_0(callid, ENOENT);
|
---|
466 | }
|
---|
467 | }
|
---|
468 | }
|
---|
469 |
|
---|
470 | static void devman_forward(ipc_callid_t iid, ipc_call_t *icall,
|
---|
471 | bool drv_to_parent)
|
---|
472 | {
|
---|
473 | devman_handle_t handle = IPC_GET_ARG2(*icall);
|
---|
474 | devman_handle_t fwd_h;
|
---|
475 | fun_node_t *fun = NULL;
|
---|
476 | dev_node_t *dev = NULL;
|
---|
477 |
|
---|
478 | fun = find_fun_node(&device_tree, handle);
|
---|
479 | if (fun == NULL)
|
---|
480 | dev = find_dev_node(&device_tree, handle);
|
---|
481 | else
|
---|
482 | dev = fun->dev;
|
---|
483 |
|
---|
484 | /*
|
---|
485 | * For a valid function to connect to we need a device. The root
|
---|
486 | * function, for example, has no device and cannot be connected to.
|
---|
487 | * This means @c dev needs to be valid regardless whether we are
|
---|
488 | * connecting to a device or to a function.
|
---|
489 | */
|
---|
490 | if (dev == NULL) {
|
---|
491 | log_msg(LVL_ERROR, "IPC forwarding failed - no device or "
|
---|
492 | "function with handle %" PRIun " was found.\n", handle);
|
---|
493 | async_answer_0(iid, ENOENT);
|
---|
494 | return;
|
---|
495 | }
|
---|
496 |
|
---|
497 | if (fun == NULL && !drv_to_parent) {
|
---|
498 | log_msg(LVL_ERROR, NAME ": devman_forward error - cannot "
|
---|
499 | "connect to handle %" PRIun ", refers to a device.\n",
|
---|
500 | handle);
|
---|
501 | async_answer_0(iid, ENOENT);
|
---|
502 | return;
|
---|
503 | }
|
---|
504 |
|
---|
505 | driver_t *driver = NULL;
|
---|
506 |
|
---|
507 | if (drv_to_parent) {
|
---|
508 | /* Connect to parent function of a device (or device function). */
|
---|
509 | if (dev->pfun->dev != NULL)
|
---|
510 | driver = dev->pfun->dev->drv;
|
---|
511 | fwd_h = dev->pfun->handle;
|
---|
512 | } else if (dev->state == DEVICE_USABLE) {
|
---|
513 | /* Connect to the specified function */
|
---|
514 | driver = dev->drv;
|
---|
515 | assert(driver != NULL);
|
---|
516 |
|
---|
517 | fwd_h = handle;
|
---|
518 | }
|
---|
519 |
|
---|
520 | if (driver == NULL) {
|
---|
521 | log_msg(LVL_ERROR, "IPC forwarding refused - " \
|
---|
522 | "the device %" PRIun " is not in usable state.\n", handle);
|
---|
523 | async_answer_0(iid, ENOENT);
|
---|
524 | return;
|
---|
525 | }
|
---|
526 |
|
---|
527 | int method;
|
---|
528 | if (drv_to_parent)
|
---|
529 | method = DRIVER_DRIVER;
|
---|
530 | else
|
---|
531 | method = DRIVER_CLIENT;
|
---|
532 |
|
---|
533 | if (driver->phone <= 0) {
|
---|
534 | log_msg(LVL_ERROR,
|
---|
535 | "Could not forward to driver `%s' (phone is %d).\n",
|
---|
536 | driver->name, (int) driver->phone);
|
---|
537 | async_answer_0(iid, EINVAL);
|
---|
538 | return;
|
---|
539 | }
|
---|
540 |
|
---|
541 | if (fun != NULL) {
|
---|
542 | log_msg(LVL_DEBUG,
|
---|
543 | "Forwarding request for `%s' function to driver `%s'.\n",
|
---|
544 | fun->pathname, driver->name);
|
---|
545 | } else {
|
---|
546 | log_msg(LVL_DEBUG,
|
---|
547 | "Forwarding request for `%s' device to driver `%s'.\n",
|
---|
548 | dev->pfun->pathname, driver->name);
|
---|
549 | }
|
---|
550 |
|
---|
551 | async_forward_fast(iid, driver->phone, method, fwd_h, 0, IPC_FF_NONE);
|
---|
552 | }
|
---|
553 |
|
---|
554 | /** Function for handling connections from a client forwarded by the device
|
---|
555 | * mapper to the device manager. */
|
---|
556 | static void devman_connection_devmapper(ipc_callid_t iid, ipc_call_t *icall)
|
---|
557 | {
|
---|
558 | devmap_handle_t devmap_handle = IPC_GET_ARG2(*icall);
|
---|
559 | fun_node_t *fun;
|
---|
560 | dev_node_t *dev;
|
---|
561 |
|
---|
562 | fun = find_devmap_tree_function(&device_tree, devmap_handle);
|
---|
563 | if (fun == NULL)
|
---|
564 | fun = find_devmap_class_function(&class_list, devmap_handle);
|
---|
565 |
|
---|
566 | if (fun == NULL || fun->dev->drv == NULL) {
|
---|
567 | async_answer_0(iid, ENOENT);
|
---|
568 | return;
|
---|
569 | }
|
---|
570 |
|
---|
571 | dev = fun->dev;
|
---|
572 |
|
---|
573 | if (dev->state != DEVICE_USABLE || dev->drv->phone <= 0) {
|
---|
574 | async_answer_0(iid, EINVAL);
|
---|
575 | return;
|
---|
576 | }
|
---|
577 |
|
---|
578 | async_forward_fast(iid, dev->drv->phone, DRIVER_CLIENT, fun->handle, 0,
|
---|
579 | IPC_FF_NONE);
|
---|
580 | log_msg(LVL_DEBUG,
|
---|
581 | "Forwarding devmapper request for `%s' function to driver `%s'.\n",
|
---|
582 | fun->pathname, dev->drv->name);
|
---|
583 | }
|
---|
584 |
|
---|
585 | /** Function for handling connections to device manager. */
|
---|
586 | static void devman_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
587 | {
|
---|
588 | /* Select interface. */
|
---|
589 | switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
|
---|
590 | case DEVMAN_DRIVER:
|
---|
591 | devman_connection_driver(iid, icall);
|
---|
592 | break;
|
---|
593 | case DEVMAN_CLIENT:
|
---|
594 | devman_connection_client(iid, icall);
|
---|
595 | break;
|
---|
596 | case DEVMAN_CONNECT_TO_DEVICE:
|
---|
597 | /* Connect client to selected device. */
|
---|
598 | devman_forward(iid, icall, false);
|
---|
599 | break;
|
---|
600 | case DEVMAN_CONNECT_FROM_DEVMAP:
|
---|
601 | /* Someone connected through devmap node. */
|
---|
602 | devman_connection_devmapper(iid, icall);
|
---|
603 | break;
|
---|
604 | case DEVMAN_CONNECT_TO_PARENTS_DEVICE:
|
---|
605 | /* Connect client to selected device. */
|
---|
606 | devman_forward(iid, icall, true);
|
---|
607 | break;
|
---|
608 | default:
|
---|
609 | /* No such interface */
|
---|
610 | async_answer_0(iid, ENOENT);
|
---|
611 | }
|
---|
612 | }
|
---|
613 |
|
---|
614 | /** Initialize device manager internal structures. */
|
---|
615 | static bool devman_init(void)
|
---|
616 | {
|
---|
617 | log_msg(LVL_DEBUG, "devman_init - looking for available drivers.\n");
|
---|
618 |
|
---|
619 | /* Initialize list of available drivers. */
|
---|
620 | init_driver_list(&drivers_list);
|
---|
621 | if (lookup_available_drivers(&drivers_list,
|
---|
622 | DRIVER_DEFAULT_STORE) == 0) {
|
---|
623 | log_msg(LVL_FATAL, "no drivers found.");
|
---|
624 | return false;
|
---|
625 | }
|
---|
626 |
|
---|
627 | log_msg(LVL_DEBUG, "devman_init - list of drivers has been initialized.\n");
|
---|
628 |
|
---|
629 | /* Create root device node. */
|
---|
630 | if (!init_device_tree(&device_tree, &drivers_list)) {
|
---|
631 | log_msg(LVL_FATAL, "Failed to initialize device tree.");
|
---|
632 | return false;
|
---|
633 | }
|
---|
634 |
|
---|
635 | init_class_list(&class_list);
|
---|
636 |
|
---|
637 | /*
|
---|
638 | * !!! devman_connection ... as the device manager is not a real devmap
|
---|
639 | * driver (it uses a completely different ipc protocol than an ordinary
|
---|
640 | * devmap driver) forwarding a connection from client to the devman by
|
---|
641 | * devmapper would not work.
|
---|
642 | */
|
---|
643 | devmap_driver_register(NAME, devman_connection);
|
---|
644 |
|
---|
645 | return true;
|
---|
646 | }
|
---|
647 |
|
---|
648 | int main(int argc, char *argv[])
|
---|
649 | {
|
---|
650 | printf(NAME ": HelenOS Device Manager\n");
|
---|
651 |
|
---|
652 | if (log_init(NAME, LVL_ERROR) != EOK) {
|
---|
653 | printf(NAME ": Error initializing logging subsystem.\n");
|
---|
654 | return -1;
|
---|
655 | }
|
---|
656 |
|
---|
657 | if (!devman_init()) {
|
---|
658 | log_msg(LVL_ERROR, "Error while initializing service.\n");
|
---|
659 | return -1;
|
---|
660 | }
|
---|
661 |
|
---|
662 | /* Set a handler of incomming connections. */
|
---|
663 | async_set_client_connection(devman_connection);
|
---|
664 |
|
---|
665 | /* Register device manager at naming service. */
|
---|
666 | if (service_register(SERVICE_DEVMAN) != EOK) {
|
---|
667 | log_msg(LVL_ERROR, "Failed registering as a service.\n");
|
---|
668 | return -1;
|
---|
669 | }
|
---|
670 |
|
---|
671 | printf(NAME ": Accepting connections.\n");
|
---|
672 | async_manager();
|
---|
673 |
|
---|
674 | /* Never reached. */
|
---|
675 | return 0;
|
---|
676 | }
|
---|
677 |
|
---|
678 | /** @}
|
---|
679 | */
|
---|