source: mainline/uspace/srv/devman/main.c@ 8a637a4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8a637a4 was f9b2cb4c, checked in by Martin Decky <martin@…>, 10 years ago

unify interface API

  • introduce new interfaces
  • unify location service clients to always expect service ID as the second argument
  • remove obsolete methods that take explicit exchange management arguments (first phase)
  • use interfaces in device drivers, devman, location service, logger, inet
  • Property mode set to 100644
File size: 9.9 KB
Line 
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 <stdbool.h>
45#include <fibril_synch.h>
46#include <stdlib.h>
47#include <str.h>
48#include <ctype.h>
49#include <io/log.h>
50#include <ipc/devman.h>
51#include <loc.h>
52
53#include "client_conn.h"
54#include "dev.h"
55#include "devman.h"
56#include "devtree.h"
57#include "drv_conn.h"
58#include "driver.h"
59#include "fun.h"
60#include "loc.h"
61
62#define DRIVER_DEFAULT_STORE "/drv"
63
64driver_list_t drivers_list;
65dev_tree_t device_tree;
66
67static void devman_connection_device(ipc_callid_t iid, ipc_call_t *icall,
68 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(iid, 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(iid, 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(iid, 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(iid, 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(iid, exch, INTERFACE_DDF_CLIENT, handle, 0, IPC_FF_NONE);
140 async_exchange_end(exch);
141
142cleanup:
143 if (dev != NULL)
144 dev_del_ref(dev);
145
146 if (fun != NULL)
147 fun_del_ref(fun);
148}
149
150static void devman_connection_parent(ipc_callid_t iid, ipc_call_t *icall,
151 void *arg)
152{
153 devman_handle_t handle = IPC_GET_ARG2(*icall);
154 dev_node_t *dev = NULL;
155
156 fun_node_t *fun = find_fun_node(&device_tree, handle);
157 if (fun == NULL) {
158 dev = find_dev_node(&device_tree, handle);
159 } else {
160 fibril_rwlock_read_lock(&device_tree.rwlock);
161
162 dev = fun->dev;
163 if (dev != NULL)
164 dev_add_ref(dev);
165
166 fibril_rwlock_read_unlock(&device_tree.rwlock);
167 }
168
169 /*
170 * For a valid function to connect to we need a device. The root
171 * function, for example, has no device and cannot be connected to.
172 * This means @c dev needs to be valid regardless whether we are
173 * connecting to a device or to a function.
174 */
175 if (dev == NULL) {
176 log_msg(LOG_DEFAULT, LVL_ERROR, "IPC forwarding failed - no device or "
177 "function with handle %" PRIun " was found.", handle);
178 async_answer_0(iid, ENOENT);
179 goto cleanup;
180 }
181
182 driver_t *driver = NULL;
183
184 fibril_rwlock_read_lock(&device_tree.rwlock);
185
186 /* Connect to parent function of a device (or device function). */
187 if (dev->pfun->dev != NULL)
188 driver = dev->pfun->dev->drv;
189
190 devman_handle_t fun_handle = dev->pfun->handle;
191
192 fibril_rwlock_read_unlock(&device_tree.rwlock);
193
194 if (driver == NULL) {
195 log_msg(LOG_DEFAULT, LVL_ERROR, "IPC forwarding refused - " \
196 "the device %" PRIun " is not in usable state.", handle);
197 async_answer_0(iid, ENOENT);
198 goto cleanup;
199 }
200
201 if (!driver->sess) {
202 log_msg(LOG_DEFAULT, LVL_ERROR,
203 "Could not forward to driver `%s'.", driver->name);
204 async_answer_0(iid, EINVAL);
205 goto cleanup;
206 }
207
208 if (fun != NULL) {
209 log_msg(LOG_DEFAULT, LVL_DEBUG,
210 "Forwarding request for `%s' function to driver `%s'.",
211 fun->pathname, driver->name);
212 } else {
213 log_msg(LOG_DEFAULT, LVL_DEBUG,
214 "Forwarding request for `%s' device to driver `%s'.",
215 dev->pfun->pathname, driver->name);
216 }
217
218 async_exch_t *exch = async_exchange_begin(driver->sess);
219 async_forward_fast(iid, exch, INTERFACE_DDF_DRIVER, fun_handle, 0, IPC_FF_NONE);
220 async_exchange_end(exch);
221
222cleanup:
223 if (dev != NULL)
224 dev_del_ref(dev);
225
226 if (fun != NULL)
227 fun_del_ref(fun);
228}
229
230static void devman_forward(ipc_callid_t iid, ipc_call_t *icall, void *arg)
231{
232 iface_t iface = IPC_GET_ARG1(*icall);
233 service_id_t service_id = IPC_GET_ARG2(*icall);
234
235 fun_node_t *fun = find_loc_tree_function(&device_tree, service_id);
236
237 fibril_rwlock_read_lock(&device_tree.rwlock);
238
239 if ((fun == NULL) || (fun->dev == NULL) || (fun->dev->drv == NULL)) {
240 log_msg(LOG_DEFAULT, LVL_WARN, "devman_forward(): function "
241 "not found.\n");
242 fibril_rwlock_read_unlock(&device_tree.rwlock);
243 async_answer_0(iid, ENOENT);
244 return;
245 }
246
247 dev_node_t *dev = fun->dev;
248 driver_t *driver = dev->drv;
249 devman_handle_t handle = fun->handle;
250
251 fibril_rwlock_read_unlock(&device_tree.rwlock);
252
253 async_exch_t *exch = async_exchange_begin(driver->sess);
254 async_forward_fast(iid, exch, iface, handle, 0, IPC_FF_NONE);
255 async_exchange_end(exch);
256
257 log_msg(LOG_DEFAULT, LVL_DEBUG,
258 "Forwarding service request for `%s' function to driver `%s'.",
259 fun->pathname, driver->name);
260
261 fun_del_ref(fun);
262}
263
264static void *devman_client_data_create(void)
265{
266 client_t *client;
267
268 client = calloc(1, sizeof(client_t));
269 if (client == NULL)
270 return NULL;
271
272 fibril_mutex_initialize(&client->mutex);
273 return client;
274}
275
276static void devman_client_data_destroy(void *data)
277{
278 free(data);
279}
280
281/** Initialize device manager internal structures. */
282static bool devman_init(void)
283{
284 log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_init - looking for available drivers.");
285
286 /* Initialize list of available drivers. */
287 init_driver_list(&drivers_list);
288 if (lookup_available_drivers(&drivers_list,
289 DRIVER_DEFAULT_STORE) == 0) {
290 log_msg(LOG_DEFAULT, LVL_FATAL, "No drivers found.");
291 return false;
292 }
293
294 log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_init - list of drivers has been initialized.");
295
296 /* Create root device node. */
297 if (!init_device_tree(&device_tree, &drivers_list)) {
298 log_msg(LOG_DEFAULT, LVL_FATAL, "Failed to initialize device tree.");
299 return false;
300 }
301
302 /*
303 * Caution: As the device manager is not a real loc
304 * driver (it uses a completely different IPC protocol
305 * than an ordinary loc driver), forwarding a connection
306 * from client to the devman by location service will
307 * not work.
308 */
309 loc_server_register(NAME);
310
311 return true;
312}
313
314int main(int argc, char *argv[])
315{
316 printf("%s: HelenOS Device Manager\n", NAME);
317
318 int rc = log_init(NAME);
319 if (rc != EOK) {
320 printf("%s: Error initializing logging subsystem.\n", NAME);
321 return rc;
322 }
323
324 /* Set handlers for incoming connections. */
325 async_set_client_data_constructor(devman_client_data_create);
326 async_set_client_data_destructor(devman_client_data_destroy);
327
328 port_id_t port;
329 rc = async_create_port(INTERFACE_DDF_DRIVER,
330 devman_connection_driver, NULL, &port);
331 if (rc != EOK)
332 return rc;
333
334 rc = async_create_port(INTERFACE_DDF_CLIENT,
335 devman_connection_client, NULL, &port);
336 if (rc != EOK)
337 return rc;
338
339 rc = async_create_port(INTERFACE_DEVMAN_DEVICE,
340 devman_connection_device, NULL, &port);
341 if (rc != EOK)
342 return rc;
343
344 rc = async_create_port(INTERFACE_DEVMAN_PARENT,
345 devman_connection_parent, NULL, &port);
346 if (rc != EOK)
347 return rc;
348
349 async_set_fallback_port_handler(devman_forward, NULL);
350
351 if (!devman_init()) {
352 log_msg(LOG_DEFAULT, LVL_ERROR, "Error while initializing service.");
353 return -1;
354 }
355
356 /* Register device manager at naming service. */
357 rc = service_register(SERVICE_DEVMAN);
358 if (rc != EOK) {
359 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering as a service.");
360 return rc;
361 }
362
363 printf("%s: Accepting connections.\n", NAME);
364 task_retval(0);
365 async_manager();
366
367 /* Never reached. */
368 return 0;
369}
370
371/** @}
372 */
Note: See TracBrowser for help on using the repository browser.