source: mainline/uspace/srv/devman/main.c@ c3d9aaf5

Last change on this file since c3d9aaf5 was c3d9aaf5, checked in by Jiri Svoboda <jiri@…>, 5 months ago

Determine when device (sub)tree is stable.

Devman will only return value when the entire device tree is stable.

  • Property mode set to 100644
File size: 10.4 KB
Line 
1/*
2 * Copyright (c) 2025 Jiri Svoboda
3 * Copyright (c) 2010 Lenka Trochtova
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/**
31 * @addtogroup devman
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;
67loc_srv_t *devman_srv;
68
69static void devman_connection_device(ipc_call_t *icall, void *arg)
70{
71 devman_handle_t handle = ipc_get_arg2(icall);
72 dev_node_t *dev = NULL;
73
74 fun_node_t *fun = find_fun_node(&device_tree, handle);
75 if (fun == NULL) {
76 dev = find_dev_node(&device_tree, handle);
77 } else {
78 fibril_rwlock_read_lock(&device_tree.rwlock);
79
80 dev = fun->dev;
81 if (dev != NULL)
82 dev_add_ref(dev);
83
84 fibril_rwlock_read_unlock(&device_tree.rwlock);
85 }
86
87 /*
88 * For a valid function to connect to we need a device. The root
89 * function, for example, has no device and cannot be connected to.
90 * This means @c dev needs to be valid regardless whether we are
91 * connecting to a device or to a function.
92 */
93 if (dev == NULL) {
94 log_msg(LOG_DEFAULT, LVL_ERROR, "IPC forwarding failed - no device or "
95 "function with handle %" PRIun " was found.", handle);
96 async_answer_0(icall, ENOENT);
97 goto cleanup;
98 }
99
100 if (fun == NULL) {
101 log_msg(LOG_DEFAULT, LVL_ERROR, NAME ": devman_forward error - cannot "
102 "connect to handle %" PRIun ", refers to a device.",
103 handle);
104 async_answer_0(icall, ENOENT);
105 goto cleanup;
106 }
107
108 fibril_rwlock_read_lock(&device_tree.rwlock);
109
110 /* Connect to the specified function */
111 driver_t *driver = dev->drv;
112
113 fibril_rwlock_read_unlock(&device_tree.rwlock);
114
115 if (driver == NULL) {
116 log_msg(LOG_DEFAULT, LVL_ERROR, "IPC forwarding refused - "
117 "the device %" PRIun " is not in usable state.", handle);
118 async_answer_0(icall, ENOENT);
119 goto cleanup;
120 }
121
122 if (!driver->sess) {
123 log_msg(LOG_DEFAULT, LVL_ERROR,
124 "Could not forward to driver `%s'.", driver->name);
125 async_answer_0(icall, EINVAL);
126 goto cleanup;
127 }
128
129 if (fun != NULL) {
130 log_msg(LOG_DEFAULT, LVL_DEBUG,
131 "Forwarding request for `%s' function to driver `%s'.",
132 fun->pathname, driver->name);
133 } else {
134 log_msg(LOG_DEFAULT, LVL_DEBUG,
135 "Forwarding request for `%s' device to driver `%s'.",
136 dev->pfun->pathname, driver->name);
137 }
138
139 async_exch_t *exch = async_exchange_begin(driver->sess);
140 async_forward_1(icall, exch, INTERFACE_DDF_CLIENT, handle, IPC_FF_NONE);
141 async_exchange_end(exch);
142
143cleanup:
144 if (dev != NULL)
145 dev_del_ref(dev);
146
147 if (fun != NULL)
148 fun_del_ref(fun);
149}
150
151static void devman_connection_parent(ipc_call_t *icall, 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(icall, 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(icall, 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(icall, 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_1(icall, exch, INTERFACE_DDF_DRIVER, fun_handle, 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_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(icall, 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_1(icall, exch, iface, handle, 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 errno_t rc;
285
286 log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_init - looking for available drivers.");
287
288 /* Initialize list of available drivers. */
289 init_driver_list(&drivers_list);
290 if (lookup_available_drivers(&drivers_list,
291 DRIVER_DEFAULT_STORE) == 0) {
292 log_msg(LOG_DEFAULT, LVL_FATAL, "No drivers found.");
293 return false;
294 }
295
296 log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_init - list of drivers has been initialized.");
297
298 /* Create root device node. */
299 if (!init_device_tree(&device_tree, &drivers_list)) {
300 log_msg(LOG_DEFAULT, LVL_FATAL, "Failed to initialize device tree.");
301 return false;
302 }
303
304 /*
305 * Caution: As the device manager is not a real loc
306 * driver (it uses a completely different IPC protocol
307 * than an ordinary loc driver), forwarding a connection
308 * from client to the devman by location service will
309 * not work.
310 */
311 rc = loc_server_register(NAME, &devman_srv);
312 if (rc != EOK) {
313 log_msg(LOG_DEFAULT, LVL_FATAL, "Error registering devman server.");
314 return false;
315 }
316
317 return true;
318}
319
320int main(int argc, char *argv[])
321{
322 printf("%s: HelenOS Device Manager\n", NAME);
323
324 errno_t rc = log_init(NAME);
325 if (rc != EOK) {
326 printf("%s: Error initializing logging subsystem: %s\n", NAME, str_error(rc));
327 return rc;
328 }
329
330 /* Set handlers for incoming connections. */
331 async_set_client_data_constructor(devman_client_data_create);
332 async_set_client_data_destructor(devman_client_data_destroy);
333
334 async_set_fallback_port_handler(devman_forward, NULL);
335
336 if (!devman_init()) {
337 log_msg(LOG_DEFAULT, LVL_ERROR, "Error while initializing service.");
338 return -1;
339 }
340
341 /* Register device manager at naming service. */
342 rc = service_register(SERVICE_DEVMAN, INTERFACE_DDF_DRIVER,
343 devman_connection_driver, NULL);
344 if (rc != EOK) {
345 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering driver port: %s", str_error(rc));
346 return rc;
347 }
348
349 rc = service_register(SERVICE_DEVMAN, INTERFACE_DDF_CLIENT,
350 devman_connection_client, NULL);
351 if (rc != EOK) {
352 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering client port: %s", str_error(rc));
353 return rc;
354 }
355
356 rc = service_register(SERVICE_DEVMAN, INTERFACE_DEVMAN_DEVICE,
357 devman_connection_device, NULL);
358 if (rc != EOK) {
359 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering device port: %s", str_error(rc));
360 return rc;
361 }
362
363 rc = service_register(SERVICE_DEVMAN, INTERFACE_DEVMAN_PARENT,
364 devman_connection_parent, NULL);
365 if (rc != EOK) {
366 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering parent port: %s", str_error(rc));
367 return rc;
368 }
369
370 printf("%s: Accepting connections.\n", NAME);
371 log_msg(LOG_DEFAULT, LVL_NOTE, "Wait for device tree to stabilize.");
372 dev_tree_wait_stable(&device_tree);
373 log_msg(LOG_DEFAULT, LVL_NOTE, "Device tree stable.");
374 task_retval(0);
375 async_manager();
376
377 /* Never reached. */
378 return 0;
379}
380
381/** @}
382 */
Note: See TracBrowser for help on using the repository browser.