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");
|
---|
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.",
|
---|
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.", 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.",
|
---|
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.",
|
---|
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.");
|
---|
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.");
|
---|
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.",
|
---|
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.",
|
---|
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.",
|
---|
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 | /* Check that function with same name is not there already. */
|
---|
251 | if (find_fun_node_in_device(pdev, fun_name) != NULL) {
|
---|
252 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
253 | async_answer_0(callid, EEXISTS);
|
---|
254 | printf(NAME ": Warning, driver tried to register `%s' twice.\n",
|
---|
255 | fun_name);
|
---|
256 | free(fun_name);
|
---|
257 | return;
|
---|
258 | }
|
---|
259 |
|
---|
260 | fun_node_t *fun = create_fun_node();
|
---|
261 | if (!insert_fun_node(&device_tree, fun, fun_name, pdev)) {
|
---|
262 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
263 | delete_fun_node(fun);
|
---|
264 | async_answer_0(callid, ENOMEM);
|
---|
265 | return;
|
---|
266 | }
|
---|
267 |
|
---|
268 | if (ftype == fun_inner) {
|
---|
269 | dev = create_dev_node();
|
---|
270 | if (dev == NULL) {
|
---|
271 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
272 | delete_fun_node(fun);
|
---|
273 | async_answer_0(callid, ENOMEM);
|
---|
274 | return;
|
---|
275 | }
|
---|
276 |
|
---|
277 | insert_dev_node(tree, dev, fun);
|
---|
278 | }
|
---|
279 |
|
---|
280 | fibril_rwlock_write_unlock(&tree->rwlock);
|
---|
281 |
|
---|
282 | log_msg(LVL_DEBUG, "devman_add_function(fun=\"%s\")", fun->pathname);
|
---|
283 |
|
---|
284 | devman_receive_match_ids(match_count, &fun->match_ids);
|
---|
285 |
|
---|
286 | if (ftype == fun_inner) {
|
---|
287 | assert(dev != NULL);
|
---|
288 | /*
|
---|
289 | * Try to find a suitable driver and assign it to the device. We do
|
---|
290 | * not want to block the current fibril that is used for processing
|
---|
291 | * incoming calls: we will launch a separate fibril to handle the
|
---|
292 | * driver assigning. That is because assign_driver can actually include
|
---|
293 | * task spawning which could take some time.
|
---|
294 | */
|
---|
295 | fid_t assign_fibril = fibril_create(assign_driver_fibril, dev);
|
---|
296 | if (assign_fibril == 0) {
|
---|
297 | /*
|
---|
298 | * Fallback in case we are out of memory.
|
---|
299 | * Probably not needed as we will die soon anyway ;-).
|
---|
300 | */
|
---|
301 | (void) assign_driver_fibril(fun);
|
---|
302 | } else {
|
---|
303 | fibril_add_ready(assign_fibril);
|
---|
304 | }
|
---|
305 | } else {
|
---|
306 | devmap_register_tree_function(fun, tree);
|
---|
307 | }
|
---|
308 |
|
---|
309 | /* Return device handle to parent's driver. */
|
---|
310 | async_answer_1(callid, EOK, fun->handle);
|
---|
311 | }
|
---|
312 |
|
---|
313 | static void devmap_register_class_dev(dev_class_info_t *cli)
|
---|
314 | {
|
---|
315 | /* Create devmap path and name for the device. */
|
---|
316 | char *devmap_pathname = NULL;
|
---|
317 |
|
---|
318 | asprintf(&devmap_pathname, "%s/%s%c%s", DEVMAP_CLASS_NAMESPACE,
|
---|
319 | cli->dev_class->name, DEVMAP_SEPARATOR, cli->dev_name);
|
---|
320 | if (devmap_pathname == NULL)
|
---|
321 | return;
|
---|
322 |
|
---|
323 | /*
|
---|
324 | * Register the device by the device mapper and remember its devmap
|
---|
325 | * handle.
|
---|
326 | */
|
---|
327 | devmap_device_register_with_iface(devmap_pathname,
|
---|
328 | &cli->devmap_handle, DEVMAN_CONNECT_FROM_DEVMAP);
|
---|
329 |
|
---|
330 | /*
|
---|
331 | * Add device to the hash map of class devices registered by device
|
---|
332 | * mapper.
|
---|
333 | */
|
---|
334 | class_add_devmap_function(&class_list, cli);
|
---|
335 |
|
---|
336 | free(devmap_pathname);
|
---|
337 | }
|
---|
338 |
|
---|
339 | static void devman_add_function_to_class(ipc_callid_t callid, ipc_call_t *call)
|
---|
340 | {
|
---|
341 | devman_handle_t handle = IPC_GET_ARG1(*call);
|
---|
342 |
|
---|
343 | /* Get class name. */
|
---|
344 | char *class_name;
|
---|
345 | int rc = async_data_write_accept((void **) &class_name, true,
|
---|
346 | 0, 0, 0, 0);
|
---|
347 | if (rc != EOK) {
|
---|
348 | async_answer_0(callid, rc);
|
---|
349 | return;
|
---|
350 | }
|
---|
351 |
|
---|
352 | fun_node_t *fun = find_fun_node(&device_tree, handle);
|
---|
353 | if (fun == NULL) {
|
---|
354 | async_answer_0(callid, ENOENT);
|
---|
355 | return;
|
---|
356 | }
|
---|
357 |
|
---|
358 | dev_class_t *cl = get_dev_class(&class_list, class_name);
|
---|
359 | dev_class_info_t *class_info = add_function_to_class(fun, cl, NULL);
|
---|
360 |
|
---|
361 | /* Register the device's class alias by devmapper. */
|
---|
362 | devmap_register_class_dev(class_info);
|
---|
363 |
|
---|
364 | log_msg(LVL_NOTE, "Function `%s' added to class `%s' as `%s'.",
|
---|
365 | fun->pathname, class_name, class_info->dev_name);
|
---|
366 |
|
---|
367 | async_answer_0(callid, EOK);
|
---|
368 | }
|
---|
369 |
|
---|
370 | /** Initialize driver which has registered itself as running and ready.
|
---|
371 | *
|
---|
372 | * The initialization is done in a separate fibril to avoid deadlocks (if the
|
---|
373 | * driver needed to be served by devman during the driver's initialization).
|
---|
374 | */
|
---|
375 | static int init_running_drv(void *drv)
|
---|
376 | {
|
---|
377 | driver_t *driver = (driver_t *) drv;
|
---|
378 |
|
---|
379 | initialize_running_driver(driver, &device_tree);
|
---|
380 | log_msg(LVL_DEBUG, "The `%s` driver was successfully initialized.",
|
---|
381 | driver->name);
|
---|
382 | return 0;
|
---|
383 | }
|
---|
384 |
|
---|
385 | /** Function for handling connections from a driver to the device manager. */
|
---|
386 | static void devman_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
|
---|
387 | {
|
---|
388 | /* Accept the connection. */
|
---|
389 | async_answer_0(iid, EOK);
|
---|
390 |
|
---|
391 | driver_t *driver = devman_driver_register();
|
---|
392 | if (driver == NULL)
|
---|
393 | return;
|
---|
394 |
|
---|
395 | /*
|
---|
396 | * Initialize the driver as running (e.g. pass assigned devices to it)
|
---|
397 | * in a separate fibril; the separate fibril is used to enable the
|
---|
398 | * driver to use devman service during the driver's initialization.
|
---|
399 | */
|
---|
400 | fid_t fid = fibril_create(init_running_drv, driver);
|
---|
401 | if (fid == 0) {
|
---|
402 | log_msg(LVL_ERROR, "Failed to create initialization fibril " \
|
---|
403 | "for driver `%s'.", driver->name);
|
---|
404 | return;
|
---|
405 | }
|
---|
406 | fibril_add_ready(fid);
|
---|
407 |
|
---|
408 | ipc_callid_t callid;
|
---|
409 | ipc_call_t call;
|
---|
410 | bool cont = true;
|
---|
411 | while (cont) {
|
---|
412 | callid = async_get_call(&call);
|
---|
413 |
|
---|
414 | switch (IPC_GET_IMETHOD(call)) {
|
---|
415 | case IPC_M_PHONE_HUNGUP:
|
---|
416 | cont = false;
|
---|
417 | continue;
|
---|
418 | case DEVMAN_ADD_FUNCTION:
|
---|
419 | devman_add_function(callid, &call);
|
---|
420 | break;
|
---|
421 | case DEVMAN_ADD_DEVICE_TO_CLASS:
|
---|
422 | devman_add_function_to_class(callid, &call);
|
---|
423 | break;
|
---|
424 | default:
|
---|
425 | async_answer_0(callid, EINVAL);
|
---|
426 | break;
|
---|
427 | }
|
---|
428 | }
|
---|
429 | }
|
---|
430 |
|
---|
431 | /** Find handle for the device instance identified by the device's path in the
|
---|
432 | * device tree. */
|
---|
433 | static void devman_function_get_handle(ipc_callid_t iid, ipc_call_t *icall)
|
---|
434 | {
|
---|
435 | char *pathname;
|
---|
436 |
|
---|
437 | int rc = async_data_write_accept((void **) &pathname, true, 0, 0, 0, 0);
|
---|
438 | if (rc != EOK) {
|
---|
439 | async_answer_0(iid, rc);
|
---|
440 | return;
|
---|
441 | }
|
---|
442 |
|
---|
443 | fun_node_t *fun = find_fun_node_by_path(&device_tree, pathname);
|
---|
444 |
|
---|
445 | free(pathname);
|
---|
446 |
|
---|
447 | if (fun == NULL) {
|
---|
448 | async_answer_0(iid, ENOENT);
|
---|
449 | return;
|
---|
450 | }
|
---|
451 |
|
---|
452 | async_answer_1(iid, EOK, fun->handle);
|
---|
453 | }
|
---|
454 |
|
---|
455 | /** Find handle for the device instance identified by device class name. */
|
---|
456 | static void devman_function_get_handle_by_class(ipc_callid_t iid,
|
---|
457 | ipc_call_t *icall)
|
---|
458 | {
|
---|
459 | char *classname;
|
---|
460 | char *devname;
|
---|
461 |
|
---|
462 | int rc = async_data_write_accept((void **) &classname, true, 0, 0, 0, 0);
|
---|
463 | if (rc != EOK) {
|
---|
464 | async_answer_0(iid, rc);
|
---|
465 | return;
|
---|
466 | }
|
---|
467 | rc = async_data_write_accept((void **) &devname, true, 0, 0, 0, 0);
|
---|
468 | if (rc != EOK) {
|
---|
469 | free(classname);
|
---|
470 | async_answer_0(iid, rc);
|
---|
471 | return;
|
---|
472 | }
|
---|
473 |
|
---|
474 |
|
---|
475 | fun_node_t *fun = find_fun_node_by_class(&class_list,
|
---|
476 | classname, devname);
|
---|
477 |
|
---|
478 | free(classname);
|
---|
479 | free(devname);
|
---|
480 |
|
---|
481 | if (fun == NULL) {
|
---|
482 | async_answer_0(iid, ENOENT);
|
---|
483 | return;
|
---|
484 | }
|
---|
485 |
|
---|
486 | async_answer_1(iid, EOK, fun->handle);
|
---|
487 | }
|
---|
488 |
|
---|
489 |
|
---|
490 | /** Function for handling connections from a client to the device manager. */
|
---|
491 | static void devman_connection_client(ipc_callid_t iid, ipc_call_t *icall)
|
---|
492 | {
|
---|
493 | /* Accept connection. */
|
---|
494 | async_answer_0(iid, EOK);
|
---|
495 |
|
---|
496 | bool cont = true;
|
---|
497 | while (cont) {
|
---|
498 | ipc_call_t call;
|
---|
499 | ipc_callid_t callid = async_get_call(&call);
|
---|
500 |
|
---|
501 | switch (IPC_GET_IMETHOD(call)) {
|
---|
502 | case IPC_M_PHONE_HUNGUP:
|
---|
503 | cont = false;
|
---|
504 | continue;
|
---|
505 | case DEVMAN_DEVICE_GET_HANDLE:
|
---|
506 | devman_function_get_handle(callid, &call);
|
---|
507 | break;
|
---|
508 | case DEVMAN_DEVICE_GET_HANDLE_BY_CLASS:
|
---|
509 | devman_function_get_handle_by_class(callid, &call);
|
---|
510 | break;
|
---|
511 | default:
|
---|
512 | async_answer_0(callid, ENOENT);
|
---|
513 | }
|
---|
514 | }
|
---|
515 | }
|
---|
516 |
|
---|
517 | static void devman_forward(ipc_callid_t iid, ipc_call_t *icall,
|
---|
518 | bool drv_to_parent)
|
---|
519 | {
|
---|
520 | devman_handle_t handle = IPC_GET_ARG2(*icall);
|
---|
521 | devman_handle_t fwd_h;
|
---|
522 | fun_node_t *fun = NULL;
|
---|
523 | dev_node_t *dev = NULL;
|
---|
524 |
|
---|
525 | fun = find_fun_node(&device_tree, handle);
|
---|
526 | if (fun == NULL)
|
---|
527 | dev = find_dev_node(&device_tree, handle);
|
---|
528 | else
|
---|
529 | dev = fun->dev;
|
---|
530 |
|
---|
531 | /*
|
---|
532 | * For a valid function to connect to we need a device. The root
|
---|
533 | * function, for example, has no device and cannot be connected to.
|
---|
534 | * This means @c dev needs to be valid regardless whether we are
|
---|
535 | * connecting to a device or to a function.
|
---|
536 | */
|
---|
537 | if (dev == NULL) {
|
---|
538 | log_msg(LVL_ERROR, "IPC forwarding failed - no device or "
|
---|
539 | "function with handle %" PRIun " was found.", handle);
|
---|
540 | async_answer_0(iid, ENOENT);
|
---|
541 | return;
|
---|
542 | }
|
---|
543 |
|
---|
544 | if (fun == NULL && !drv_to_parent) {
|
---|
545 | log_msg(LVL_ERROR, NAME ": devman_forward error - cannot "
|
---|
546 | "connect to handle %" PRIun ", refers to a device.",
|
---|
547 | handle);
|
---|
548 | async_answer_0(iid, ENOENT);
|
---|
549 | return;
|
---|
550 | }
|
---|
551 |
|
---|
552 | driver_t *driver = NULL;
|
---|
553 |
|
---|
554 | if (drv_to_parent) {
|
---|
555 | /* Connect to parent function of a device (or device function). */
|
---|
556 | if (dev->pfun->dev != NULL)
|
---|
557 | driver = dev->pfun->dev->drv;
|
---|
558 | fwd_h = dev->pfun->handle;
|
---|
559 | } else if (dev->state == DEVICE_USABLE) {
|
---|
560 | /* Connect to the specified function */
|
---|
561 | driver = dev->drv;
|
---|
562 | assert(driver != NULL);
|
---|
563 |
|
---|
564 | fwd_h = handle;
|
---|
565 | }
|
---|
566 |
|
---|
567 | if (driver == NULL) {
|
---|
568 | log_msg(LVL_ERROR, "IPC forwarding refused - " \
|
---|
569 | "the device %" PRIun "(%s) is not in usable state.",
|
---|
570 | handle, dev->pfun->pathname);
|
---|
571 | async_answer_0(iid, ENOENT);
|
---|
572 | return;
|
---|
573 | }
|
---|
574 |
|
---|
575 | int method;
|
---|
576 | if (drv_to_parent)
|
---|
577 | method = DRIVER_DRIVER;
|
---|
578 | else
|
---|
579 | method = DRIVER_CLIENT;
|
---|
580 |
|
---|
581 | if (driver->phone <= 0) {
|
---|
582 | log_msg(LVL_ERROR,
|
---|
583 | "Could not forward to driver `%s' (phone is %d).",
|
---|
584 | driver->name, (int) driver->phone);
|
---|
585 | async_answer_0(iid, EINVAL);
|
---|
586 | return;
|
---|
587 | }
|
---|
588 |
|
---|
589 | if (fun != NULL) {
|
---|
590 | log_msg(LVL_DEBUG,
|
---|
591 | "Forwarding request for `%s' function to driver `%s'.",
|
---|
592 | fun->pathname, driver->name);
|
---|
593 | } else {
|
---|
594 | log_msg(LVL_DEBUG,
|
---|
595 | "Forwarding request for `%s' device to driver `%s'.",
|
---|
596 | dev->pfun->pathname, driver->name);
|
---|
597 | }
|
---|
598 |
|
---|
599 | async_forward_fast(iid, driver->phone, method, fwd_h, 0, IPC_FF_NONE);
|
---|
600 | }
|
---|
601 |
|
---|
602 | /** Function for handling connections from a client forwarded by the device
|
---|
603 | * mapper to the device manager. */
|
---|
604 | static void devman_connection_devmapper(ipc_callid_t iid, ipc_call_t *icall)
|
---|
605 | {
|
---|
606 | devmap_handle_t devmap_handle = IPC_GET_ARG2(*icall);
|
---|
607 | fun_node_t *fun;
|
---|
608 | dev_node_t *dev;
|
---|
609 |
|
---|
610 | fun = find_devmap_tree_function(&device_tree, devmap_handle);
|
---|
611 | if (fun == NULL)
|
---|
612 | fun = find_devmap_class_function(&class_list, devmap_handle);
|
---|
613 |
|
---|
614 | if (fun == NULL || fun->dev->drv == NULL) {
|
---|
615 | async_answer_0(iid, ENOENT);
|
---|
616 | return;
|
---|
617 | }
|
---|
618 |
|
---|
619 | dev = fun->dev;
|
---|
620 |
|
---|
621 | if (dev->state != DEVICE_USABLE || dev->drv->phone <= 0) {
|
---|
622 | async_answer_0(iid, EINVAL);
|
---|
623 | return;
|
---|
624 | }
|
---|
625 |
|
---|
626 | async_forward_fast(iid, dev->drv->phone, DRIVER_CLIENT, fun->handle, 0,
|
---|
627 | IPC_FF_NONE);
|
---|
628 | log_msg(LVL_DEBUG,
|
---|
629 | "Forwarding devmapper request for `%s' function to driver `%s'.",
|
---|
630 | fun->pathname, dev->drv->name);
|
---|
631 | }
|
---|
632 |
|
---|
633 | /** Function for handling connections to device manager. */
|
---|
634 | static void devman_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
635 | {
|
---|
636 | /* Select interface. */
|
---|
637 | switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
|
---|
638 | case DEVMAN_DRIVER:
|
---|
639 | devman_connection_driver(iid, icall);
|
---|
640 | break;
|
---|
641 | case DEVMAN_CLIENT:
|
---|
642 | devman_connection_client(iid, icall);
|
---|
643 | break;
|
---|
644 | case DEVMAN_CONNECT_TO_DEVICE:
|
---|
645 | /* Connect client to selected device. */
|
---|
646 | devman_forward(iid, icall, false);
|
---|
647 | break;
|
---|
648 | case DEVMAN_CONNECT_FROM_DEVMAP:
|
---|
649 | /* Someone connected through devmap node. */
|
---|
650 | devman_connection_devmapper(iid, icall);
|
---|
651 | break;
|
---|
652 | case DEVMAN_CONNECT_TO_PARENTS_DEVICE:
|
---|
653 | /* Connect client to selected device. */
|
---|
654 | devman_forward(iid, icall, true);
|
---|
655 | break;
|
---|
656 | default:
|
---|
657 | /* No such interface */
|
---|
658 | async_answer_0(iid, ENOENT);
|
---|
659 | }
|
---|
660 | }
|
---|
661 |
|
---|
662 | /** Initialize device manager internal structures. */
|
---|
663 | static bool devman_init(void)
|
---|
664 | {
|
---|
665 | log_msg(LVL_DEBUG, "devman_init - looking for available drivers.");
|
---|
666 |
|
---|
667 | /* Initialize list of available drivers. */
|
---|
668 | init_driver_list(&drivers_list);
|
---|
669 | if (lookup_available_drivers(&drivers_list,
|
---|
670 | DRIVER_DEFAULT_STORE) == 0) {
|
---|
671 | log_msg(LVL_FATAL, "No drivers found.");
|
---|
672 | return false;
|
---|
673 | }
|
---|
674 |
|
---|
675 | log_msg(LVL_DEBUG, "devman_init - list of drivers has been initialized.");
|
---|
676 |
|
---|
677 | /* Create root device node. */
|
---|
678 | if (!init_device_tree(&device_tree, &drivers_list)) {
|
---|
679 | log_msg(LVL_FATAL, "Failed to initialize device tree.");
|
---|
680 | return false;
|
---|
681 | }
|
---|
682 |
|
---|
683 | init_class_list(&class_list);
|
---|
684 |
|
---|
685 | /*
|
---|
686 | * !!! devman_connection ... as the device manager is not a real devmap
|
---|
687 | * driver (it uses a completely different ipc protocol than an ordinary
|
---|
688 | * devmap driver) forwarding a connection from client to the devman by
|
---|
689 | * devmapper would not work.
|
---|
690 | */
|
---|
691 | devmap_driver_register(NAME, devman_connection);
|
---|
692 |
|
---|
693 | return true;
|
---|
694 | }
|
---|
695 |
|
---|
696 | int main(int argc, char *argv[])
|
---|
697 | {
|
---|
698 | printf(NAME ": HelenOS Device Manager\n");
|
---|
699 |
|
---|
700 | if (log_init(NAME, LVL_ERROR) != EOK) {
|
---|
701 | printf(NAME ": Error initializing logging subsystem.\n");
|
---|
702 | return -1;
|
---|
703 | }
|
---|
704 |
|
---|
705 | if (!devman_init()) {
|
---|
706 | log_msg(LVL_ERROR, "Error while initializing service.");
|
---|
707 | return -1;
|
---|
708 | }
|
---|
709 |
|
---|
710 | /* Set a handler of incomming connections. */
|
---|
711 | async_set_client_connection(devman_connection);
|
---|
712 |
|
---|
713 | /* Register device manager at naming service. */
|
---|
714 | if (service_register(SERVICE_DEVMAN) != EOK) {
|
---|
715 | log_msg(LVL_ERROR, "Failed registering as a service.");
|
---|
716 | return -1;
|
---|
717 | }
|
---|
718 |
|
---|
719 | printf(NAME ": Accepting connections.\n");
|
---|
720 | async_manager();
|
---|
721 |
|
---|
722 | /* Never reached. */
|
---|
723 | return 0;
|
---|
724 | }
|
---|
725 |
|
---|
726 | /** @}
|
---|
727 | */
|
---|