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 <assert.h>
|
---|
39 | #include <ipc/services.h>
|
---|
40 | #include <ns.h>
|
---|
41 | #include <async.h>
|
---|
42 | #include <stdio.h>
|
---|
43 | #include <errno.h>
|
---|
44 | #include <str_error.h>
|
---|
45 | #include <stdbool.h>
|
---|
46 | #include <fibril_synch.h>
|
---|
47 | #include <stdlib.h>
|
---|
48 | #include <str.h>
|
---|
49 | #include <ctype.h>
|
---|
50 | #include <io/log.h>
|
---|
51 | #include <ipc/devman.h>
|
---|
52 | #include <loc.h>
|
---|
53 |
|
---|
54 | #include "client_conn.h"
|
---|
55 | #include "dev.h"
|
---|
56 | #include "devman.h"
|
---|
57 | #include "devtree.h"
|
---|
58 | #include "drv_conn.h"
|
---|
59 | #include "driver.h"
|
---|
60 | #include "fun.h"
|
---|
61 | #include "loc.h"
|
---|
62 |
|
---|
63 | #define DRIVER_DEFAULT_STORE "/drv"
|
---|
64 |
|
---|
65 | driver_list_t drivers_list;
|
---|
66 | dev_tree_t device_tree;
|
---|
67 |
|
---|
68 | static void devman_connection_device(ipc_call_t *icall, void *arg)
|
---|
69 | {
|
---|
70 | devman_handle_t handle = IPC_GET_ARG2(*icall);
|
---|
71 | dev_node_t *dev = NULL;
|
---|
72 |
|
---|
73 | fun_node_t *fun = find_fun_node(&device_tree, handle);
|
---|
74 | if (fun == NULL) {
|
---|
75 | dev = find_dev_node(&device_tree, handle);
|
---|
76 | } else {
|
---|
77 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
78 |
|
---|
79 | dev = fun->dev;
|
---|
80 | if (dev != NULL)
|
---|
81 | dev_add_ref(dev);
|
---|
82 |
|
---|
83 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /*
|
---|
87 | * For a valid function to connect to we need a device. The root
|
---|
88 | * function, for example, has no device and cannot be connected to.
|
---|
89 | * This means @c dev needs to be valid regardless whether we are
|
---|
90 | * connecting to a device or to a function.
|
---|
91 | */
|
---|
92 | if (dev == NULL) {
|
---|
93 | log_msg(LOG_DEFAULT, LVL_ERROR, "IPC forwarding failed - no device or "
|
---|
94 | "function with handle %" PRIun " was found.", handle);
|
---|
95 | async_answer_0(icall, ENOENT);
|
---|
96 | goto cleanup;
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (fun == NULL) {
|
---|
100 | log_msg(LOG_DEFAULT, LVL_ERROR, NAME ": devman_forward error - cannot "
|
---|
101 | "connect to handle %" PRIun ", refers to a device.",
|
---|
102 | handle);
|
---|
103 | async_answer_0(icall, ENOENT);
|
---|
104 | goto cleanup;
|
---|
105 | }
|
---|
106 |
|
---|
107 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
108 |
|
---|
109 | /* Connect to the specified function */
|
---|
110 | driver_t *driver = dev->drv;
|
---|
111 |
|
---|
112 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
113 |
|
---|
114 | if (driver == NULL) {
|
---|
115 | log_msg(LOG_DEFAULT, LVL_ERROR, "IPC forwarding refused - "
|
---|
116 | "the device %" PRIun " is not in usable state.", handle);
|
---|
117 | async_answer_0(icall, ENOENT);
|
---|
118 | goto cleanup;
|
---|
119 | }
|
---|
120 |
|
---|
121 | if (!driver->sess) {
|
---|
122 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
123 | "Could not forward to driver `%s'.", driver->name);
|
---|
124 | async_answer_0(icall, EINVAL);
|
---|
125 | goto cleanup;
|
---|
126 | }
|
---|
127 |
|
---|
128 | if (fun != NULL) {
|
---|
129 | log_msg(LOG_DEFAULT, LVL_DEBUG,
|
---|
130 | "Forwarding request for `%s' function to driver `%s'.",
|
---|
131 | fun->pathname, driver->name);
|
---|
132 | } else {
|
---|
133 | log_msg(LOG_DEFAULT, LVL_DEBUG,
|
---|
134 | "Forwarding request for `%s' device to driver `%s'.",
|
---|
135 | dev->pfun->pathname, driver->name);
|
---|
136 | }
|
---|
137 |
|
---|
138 | async_exch_t *exch = async_exchange_begin(driver->sess);
|
---|
139 | async_forward_fast(icall, exch, INTERFACE_DDF_CLIENT, handle, 0, IPC_FF_NONE);
|
---|
140 | async_exchange_end(exch);
|
---|
141 |
|
---|
142 | cleanup:
|
---|
143 | if (dev != NULL)
|
---|
144 | dev_del_ref(dev);
|
---|
145 |
|
---|
146 | if (fun != NULL)
|
---|
147 | fun_del_ref(fun);
|
---|
148 | }
|
---|
149 |
|
---|
150 | static void devman_connection_parent(ipc_call_t *icall, void *arg)
|
---|
151 | {
|
---|
152 | devman_handle_t handle = IPC_GET_ARG2(*icall);
|
---|
153 | dev_node_t *dev = NULL;
|
---|
154 |
|
---|
155 | fun_node_t *fun = find_fun_node(&device_tree, handle);
|
---|
156 | if (fun == NULL) {
|
---|
157 | dev = find_dev_node(&device_tree, handle);
|
---|
158 | } else {
|
---|
159 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
160 |
|
---|
161 | dev = fun->dev;
|
---|
162 | if (dev != NULL)
|
---|
163 | dev_add_ref(dev);
|
---|
164 |
|
---|
165 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
166 | }
|
---|
167 |
|
---|
168 | /*
|
---|
169 | * For a valid function to connect to we need a device. The root
|
---|
170 | * function, for example, has no device and cannot be connected to.
|
---|
171 | * This means @c dev needs to be valid regardless whether we are
|
---|
172 | * connecting to a device or to a function.
|
---|
173 | */
|
---|
174 | if (dev == NULL) {
|
---|
175 | log_msg(LOG_DEFAULT, LVL_ERROR, "IPC forwarding failed - no device or "
|
---|
176 | "function with handle %" PRIun " was found.", handle);
|
---|
177 | async_answer_0(icall, ENOENT);
|
---|
178 | goto cleanup;
|
---|
179 | }
|
---|
180 |
|
---|
181 | driver_t *driver = NULL;
|
---|
182 |
|
---|
183 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
184 |
|
---|
185 | /* Connect to parent function of a device (or device function). */
|
---|
186 | if (dev->pfun->dev != NULL)
|
---|
187 | driver = dev->pfun->dev->drv;
|
---|
188 |
|
---|
189 | devman_handle_t fun_handle = dev->pfun->handle;
|
---|
190 |
|
---|
191 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
192 |
|
---|
193 | if (driver == NULL) {
|
---|
194 | log_msg(LOG_DEFAULT, LVL_ERROR, "IPC forwarding refused - "
|
---|
195 | "the device %" PRIun " is not in usable state.", handle);
|
---|
196 | async_answer_0(icall, ENOENT);
|
---|
197 | goto cleanup;
|
---|
198 | }
|
---|
199 |
|
---|
200 | if (!driver->sess) {
|
---|
201 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
202 | "Could not forward to driver `%s'.", driver->name);
|
---|
203 | async_answer_0(icall, EINVAL);
|
---|
204 | goto cleanup;
|
---|
205 | }
|
---|
206 |
|
---|
207 | if (fun != NULL) {
|
---|
208 | log_msg(LOG_DEFAULT, LVL_DEBUG,
|
---|
209 | "Forwarding request for `%s' function to driver `%s'.",
|
---|
210 | fun->pathname, driver->name);
|
---|
211 | } else {
|
---|
212 | log_msg(LOG_DEFAULT, LVL_DEBUG,
|
---|
213 | "Forwarding request for `%s' device to driver `%s'.",
|
---|
214 | dev->pfun->pathname, driver->name);
|
---|
215 | }
|
---|
216 |
|
---|
217 | async_exch_t *exch = async_exchange_begin(driver->sess);
|
---|
218 | async_forward_fast(icall, exch, INTERFACE_DDF_DRIVER, fun_handle, 0, IPC_FF_NONE);
|
---|
219 | async_exchange_end(exch);
|
---|
220 |
|
---|
221 | cleanup:
|
---|
222 | if (dev != NULL)
|
---|
223 | dev_del_ref(dev);
|
---|
224 |
|
---|
225 | if (fun != NULL)
|
---|
226 | fun_del_ref(fun);
|
---|
227 | }
|
---|
228 |
|
---|
229 | static void devman_forward(ipc_call_t *icall, void *arg)
|
---|
230 | {
|
---|
231 | iface_t iface = IPC_GET_ARG1(*icall);
|
---|
232 | service_id_t service_id = IPC_GET_ARG2(*icall);
|
---|
233 |
|
---|
234 | fun_node_t *fun = find_loc_tree_function(&device_tree, service_id);
|
---|
235 |
|
---|
236 | fibril_rwlock_read_lock(&device_tree.rwlock);
|
---|
237 |
|
---|
238 | if ((fun == NULL) || (fun->dev == NULL) || (fun->dev->drv == NULL)) {
|
---|
239 | log_msg(LOG_DEFAULT, LVL_WARN, "devman_forward(): function "
|
---|
240 | "not found.\n");
|
---|
241 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
242 | async_answer_0(icall, ENOENT);
|
---|
243 | return;
|
---|
244 | }
|
---|
245 |
|
---|
246 | dev_node_t *dev = fun->dev;
|
---|
247 | driver_t *driver = dev->drv;
|
---|
248 | devman_handle_t handle = fun->handle;
|
---|
249 |
|
---|
250 | fibril_rwlock_read_unlock(&device_tree.rwlock);
|
---|
251 |
|
---|
252 | async_exch_t *exch = async_exchange_begin(driver->sess);
|
---|
253 | async_forward_fast(icall, exch, iface, handle, 0, IPC_FF_NONE);
|
---|
254 | async_exchange_end(exch);
|
---|
255 |
|
---|
256 | log_msg(LOG_DEFAULT, LVL_DEBUG,
|
---|
257 | "Forwarding service request for `%s' function to driver `%s'.",
|
---|
258 | fun->pathname, driver->name);
|
---|
259 |
|
---|
260 | fun_del_ref(fun);
|
---|
261 | }
|
---|
262 |
|
---|
263 | static void *devman_client_data_create(void)
|
---|
264 | {
|
---|
265 | client_t *client;
|
---|
266 |
|
---|
267 | client = calloc(1, sizeof(client_t));
|
---|
268 | if (client == NULL)
|
---|
269 | return NULL;
|
---|
270 |
|
---|
271 | fibril_mutex_initialize(&client->mutex);
|
---|
272 | return client;
|
---|
273 | }
|
---|
274 |
|
---|
275 | static void devman_client_data_destroy(void *data)
|
---|
276 | {
|
---|
277 | free(data);
|
---|
278 | }
|
---|
279 |
|
---|
280 | /** Initialize device manager internal structures. */
|
---|
281 | static bool devman_init(void)
|
---|
282 | {
|
---|
283 | log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_init - looking for available drivers.");
|
---|
284 |
|
---|
285 | /* Initialize list of available drivers. */
|
---|
286 | init_driver_list(&drivers_list);
|
---|
287 | if (lookup_available_drivers(&drivers_list,
|
---|
288 | DRIVER_DEFAULT_STORE) == 0) {
|
---|
289 | log_msg(LOG_DEFAULT, LVL_FATAL, "No drivers found.");
|
---|
290 | return false;
|
---|
291 | }
|
---|
292 |
|
---|
293 | log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_init - list of drivers has been initialized.");
|
---|
294 |
|
---|
295 | /* Create root device node. */
|
---|
296 | if (!init_device_tree(&device_tree, &drivers_list)) {
|
---|
297 | log_msg(LOG_DEFAULT, LVL_FATAL, "Failed to initialize device tree.");
|
---|
298 | return false;
|
---|
299 | }
|
---|
300 |
|
---|
301 | /*
|
---|
302 | * Caution: As the device manager is not a real loc
|
---|
303 | * driver (it uses a completely different IPC protocol
|
---|
304 | * than an ordinary loc driver), forwarding a connection
|
---|
305 | * from client to the devman by location service will
|
---|
306 | * not work.
|
---|
307 | */
|
---|
308 | loc_server_register(NAME);
|
---|
309 |
|
---|
310 | return true;
|
---|
311 | }
|
---|
312 |
|
---|
313 | int main(int argc, char *argv[])
|
---|
314 | {
|
---|
315 | printf("%s: HelenOS Device Manager\n", NAME);
|
---|
316 |
|
---|
317 | errno_t rc = log_init(NAME);
|
---|
318 | if (rc != EOK) {
|
---|
319 | printf("%s: Error initializing logging subsystem: %s\n", NAME, str_error(rc));
|
---|
320 | return rc;
|
---|
321 | }
|
---|
322 |
|
---|
323 | /* Set handlers for incoming connections. */
|
---|
324 | async_set_client_data_constructor(devman_client_data_create);
|
---|
325 | async_set_client_data_destructor(devman_client_data_destroy);
|
---|
326 |
|
---|
327 | port_id_t port;
|
---|
328 | rc = async_create_port(INTERFACE_DDF_DRIVER,
|
---|
329 | devman_connection_driver, NULL, &port);
|
---|
330 | if (rc != EOK) {
|
---|
331 | printf("%s: Error creating DDF driver port: %s\n", NAME, str_error(rc));
|
---|
332 | return rc;
|
---|
333 | }
|
---|
334 |
|
---|
335 | rc = async_create_port(INTERFACE_DDF_CLIENT,
|
---|
336 | devman_connection_client, NULL, &port);
|
---|
337 | if (rc != EOK) {
|
---|
338 | printf("%s: Error creating DDF client port: %s\n", NAME, str_error(rc));
|
---|
339 | return rc;
|
---|
340 | }
|
---|
341 |
|
---|
342 | rc = async_create_port(INTERFACE_DEVMAN_DEVICE,
|
---|
343 | devman_connection_device, NULL, &port);
|
---|
344 | if (rc != EOK) {
|
---|
345 | printf("%s: Error creating devman device port: %s\n", NAME, str_error(rc));
|
---|
346 | return rc;
|
---|
347 | }
|
---|
348 |
|
---|
349 | rc = async_create_port(INTERFACE_DEVMAN_PARENT,
|
---|
350 | devman_connection_parent, NULL, &port);
|
---|
351 | if (rc != EOK) {
|
---|
352 | printf("%s: Error creating devman parent port: %s\n", NAME, str_error(rc));
|
---|
353 | return rc;
|
---|
354 | }
|
---|
355 |
|
---|
356 | async_set_fallback_port_handler(devman_forward, NULL);
|
---|
357 |
|
---|
358 | if (!devman_init()) {
|
---|
359 | log_msg(LOG_DEFAULT, LVL_ERROR, "Error while initializing service.");
|
---|
360 | return -1;
|
---|
361 | }
|
---|
362 |
|
---|
363 | /* Register device manager at naming service. */
|
---|
364 | rc = service_register(SERVICE_DEVMAN);
|
---|
365 | if (rc != EOK) {
|
---|
366 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering as a service: %s", str_error(rc));
|
---|
367 | return rc;
|
---|
368 | }
|
---|
369 |
|
---|
370 | printf("%s: Accepting connections.\n", NAME);
|
---|
371 | task_retval(0);
|
---|
372 | async_manager();
|
---|
373 |
|
---|
374 | /* Never reached. */
|
---|
375 | return 0;
|
---|
376 | }
|
---|
377 |
|
---|
378 | /** @}
|
---|
379 | */
|
---|