source: mainline/uspace/srv/devman/main.c@ 46f7b64

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 46f7b64 was 9b1baac, checked in by Martin Decky <martin@…>, 7 years ago

ns: register service interfaces individually

Each service interface is now registered individually with the naming
service. This adds a degree of type safety, potentially allows the
individual interfaces to be implemented by independent tasks and moves
the code slightly closer to the full-fledged ports design.

Broker services (e.g. the location service) can still register a
fallback port for receiving connections to all interface types
explicitly using service_register_broker().

  • Property mode set to 100644
File size: 10.1 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 <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
65driver_list_t drivers_list;
66dev_tree_t device_tree;
67
68static 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
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_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
221cleanup:
222 if (dev != NULL)
223 dev_del_ref(dev);
224
225 if (fun != NULL)
226 fun_del_ref(fun);
227}
228
229static 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
263static 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
275static void devman_client_data_destroy(void *data)
276{
277 free(data);
278}
279
280/** Initialize device manager internal structures. */
281static 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
313int 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 async_set_fallback_port_handler(devman_forward, NULL);
328
329 if (!devman_init()) {
330 log_msg(LOG_DEFAULT, LVL_ERROR, "Error while initializing service.");
331 return -1;
332 }
333
334 /* Register device manager at naming service. */
335 rc = service_register(SERVICE_DEVMAN, INTERFACE_DDF_DRIVER,
336 devman_connection_driver, NULL);
337 if (rc != EOK) {
338 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering driver port: %s", str_error(rc));
339 return rc;
340 }
341
342 rc = service_register(SERVICE_DEVMAN, INTERFACE_DDF_CLIENT,
343 devman_connection_client, NULL);
344 if (rc != EOK) {
345 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering client port: %s", str_error(rc));
346 return rc;
347 }
348
349 rc = service_register(SERVICE_DEVMAN, INTERFACE_DEVMAN_DEVICE,
350 devman_connection_device, NULL);
351 if (rc != EOK) {
352 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering device port: %s", str_error(rc));
353 return rc;
354 }
355
356 rc = service_register(SERVICE_DEVMAN, INTERFACE_DEVMAN_PARENT,
357 devman_connection_parent, NULL);
358 if (rc != EOK) {
359 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering parent port: %s", str_error(rc));
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.