source: mainline/uspace/srv/devman/main.c@ 832cbe7

Last change on this file since 832cbe7 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
RevLine 
[e2b9a993]1/*
[c3d9aaf5]2 * Copyright (c) 2025 Jiri Svoboda
[e2b9a993]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/**
[4122410]31 * @addtogroup devman
[e2b9a993]32 * @{
33 */
34
35/** @file
36 */
37
38#include <assert.h>
39#include <ipc/services.h>
[79ae36dd]40#include <ns.h>
[e2b9a993]41#include <async.h>
42#include <stdio.h>
43#include <errno.h>
[c1694b6b]44#include <str_error.h>
[3e6a98c5]45#include <stdbool.h>
[e2b9a993]46#include <fibril_synch.h>
47#include <stdlib.h>
[c47e1a8]48#include <str.h>
[e2b9a993]49#include <ctype.h>
[9b415c9]50#include <io/log.h>
[e2b9a993]51#include <ipc/devman.h>
[15f3c3f]52#include <loc.h>
[e2b9a993]53
[d80d7a8]54#include "client_conn.h"
[d1bafbf]55#include "dev.h"
[e2b9a993]56#include "devman.h"
[59dc181]57#include "devtree.h"
[181c32f]58#include "drv_conn.h"
[041b026]59#include "driver.h"
[38e52c92]60#include "fun.h"
[a60e90b]61#include "loc.h"
[e2b9a993]62
[a79d88d]63#define DRIVER_DEFAULT_STORE "/drv"
[e2b9a993]64
[181c32f]65driver_list_t drivers_list;
[d80d7a8]66dev_tree_t device_tree;
[4c6fd56]67loc_srv_t *devman_srv;
[e2b9a993]68
[984a9ba]69static void devman_connection_device(ipc_call_t *icall, void *arg)
[38b3baf]70{
[fafb8e5]71 devman_handle_t handle = ipc_get_arg2(icall);
[8b1e15ac]72 dev_node_t *dev = NULL;
[a35b458]73
[f9b2cb4c]74 fun_node_t *fun = find_fun_node(&device_tree, handle);
75 if (fun == NULL) {
[8b1e15ac]76 dev = find_dev_node(&device_tree, handle);
[f9b2cb4c]77 } else {
[58cbb0c8]78 fibril_rwlock_read_lock(&device_tree.rwlock);
[a35b458]79
[8b1e15ac]80 dev = fun->dev;
[58cbb0c8]81 if (dev != NULL)
82 dev_add_ref(dev);
[a35b458]83
[58cbb0c8]84 fibril_rwlock_read_unlock(&device_tree.rwlock);
85 }
[a35b458]86
[0418050]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) {
[a1a101d]94 log_msg(LOG_DEFAULT, LVL_ERROR, "IPC forwarding failed - no device or "
[ebcb05a]95 "function with handle %" PRIun " was found.", handle);
[984a9ba]96 async_answer_0(icall, ENOENT);
[58cbb0c8]97 goto cleanup;
[5cd136ab]98 }
[a35b458]99
[f9b2cb4c]100 if (fun == NULL) {
[a1a101d]101 log_msg(LOG_DEFAULT, LVL_ERROR, NAME ": devman_forward error - cannot "
[ebcb05a]102 "connect to handle %" PRIun ", refers to a device.",
[9b415c9]103 handle);
[984a9ba]104 async_answer_0(icall, ENOENT);
[58cbb0c8]105 goto cleanup;
[5cd136ab]106 }
[a35b458]107
[e2b9b341]108 fibril_rwlock_read_lock(&device_tree.rwlock);
[a35b458]109
[f9b2cb4c]110 /* Connect to the specified function */
111 driver_t *driver = dev->drv;
[a35b458]112
[f9b2cb4c]113 fibril_rwlock_read_unlock(&device_tree.rwlock);
[a35b458]114
[f9b2cb4c]115 if (driver == NULL) {
[5ef16903]116 log_msg(LOG_DEFAULT, LVL_ERROR, "IPC forwarding refused - "
[f9b2cb4c]117 "the device %" PRIun " is not in usable state.", handle);
[984a9ba]118 async_answer_0(icall, ENOENT);
[f9b2cb4c]119 goto cleanup;
120 }
[a35b458]121
[f9b2cb4c]122 if (!driver->sess) {
123 log_msg(LOG_DEFAULT, LVL_ERROR,
124 "Could not forward to driver `%s'.", driver->name);
[984a9ba]125 async_answer_0(icall, EINVAL);
[f9b2cb4c]126 goto cleanup;
127 }
[a35b458]128
[f9b2cb4c]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 }
[a35b458]138
[f9b2cb4c]139 async_exch_t *exch = async_exchange_begin(driver->sess);
[4f13e19]140 async_forward_1(icall, exch, INTERFACE_DDF_CLIENT, handle, IPC_FF_NONE);
[f9b2cb4c]141 async_exchange_end(exch);
[a35b458]142
[f9b2cb4c]143cleanup:
144 if (dev != NULL)
145 dev_del_ref(dev);
[a35b458]146
[f9b2cb4c]147 if (fun != NULL)
148 fun_del_ref(fun);
149}
150
[984a9ba]151static void devman_connection_parent(ipc_call_t *icall, void *arg)
[f9b2cb4c]152{
[fafb8e5]153 devman_handle_t handle = ipc_get_arg2(icall);
[f9b2cb4c]154 dev_node_t *dev = NULL;
[a35b458]155
[f9b2cb4c]156 fun_node_t *fun = find_fun_node(&device_tree, handle);
157 if (fun == NULL) {
158 dev = find_dev_node(&device_tree, handle);
[005ac3c]159 } else {
[f9b2cb4c]160 fibril_rwlock_read_lock(&device_tree.rwlock);
[a35b458]161
[f9b2cb4c]162 dev = fun->dev;
163 if (dev != NULL)
164 dev_add_ref(dev);
[a35b458]165
[f9b2cb4c]166 fibril_rwlock_read_unlock(&device_tree.rwlock);
[5cd136ab]167 }
[a35b458]168
[f9b2cb4c]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);
[984a9ba]178 async_answer_0(icall, ENOENT);
[f9b2cb4c]179 goto cleanup;
180 }
[a35b458]181
[f9b2cb4c]182 driver_t *driver = NULL;
[a35b458]183
[f9b2cb4c]184 fibril_rwlock_read_lock(&device_tree.rwlock);
[a35b458]185
[f9b2cb4c]186 /* Connect to parent function of a device (or device function). */
187 if (dev->pfun->dev != NULL)
188 driver = dev->pfun->dev->drv;
[a35b458]189
[f9b2cb4c]190 devman_handle_t fun_handle = dev->pfun->handle;
[a35b458]191
[e2b9b341]192 fibril_rwlock_read_unlock(&device_tree.rwlock);
[a35b458]193
[c6c389ed]194 if (driver == NULL) {
[5ef16903]195 log_msg(LOG_DEFAULT, LVL_ERROR, "IPC forwarding refused - "
[79ae36dd]196 "the device %" PRIun " is not in usable state.", handle);
[984a9ba]197 async_answer_0(icall, ENOENT);
[58cbb0c8]198 goto cleanup;
[5cd136ab]199 }
[a35b458]200
[79ae36dd]201 if (!driver->sess) {
[a1a101d]202 log_msg(LOG_DEFAULT, LVL_ERROR,
[79ae36dd]203 "Could not forward to driver `%s'.", driver->name);
[984a9ba]204 async_answer_0(icall, EINVAL);
[58cbb0c8]205 goto cleanup;
[9a66bc2e]206 }
[a35b458]207
[8b1e15ac]208 if (fun != NULL) {
[a1a101d]209 log_msg(LOG_DEFAULT, LVL_DEBUG,
[ebcb05a]210 "Forwarding request for `%s' function to driver `%s'.",
[9b415c9]211 fun->pathname, driver->name);
[8b1e15ac]212 } else {
[a1a101d]213 log_msg(LOG_DEFAULT, LVL_DEBUG,
[ebcb05a]214 "Forwarding request for `%s' device to driver `%s'.",
[9b415c9]215 dev->pfun->pathname, driver->name);
[8b1e15ac]216 }
[a35b458]217
[79ae36dd]218 async_exch_t *exch = async_exchange_begin(driver->sess);
[4f13e19]219 async_forward_1(icall, exch, INTERFACE_DDF_DRIVER, fun_handle, IPC_FF_NONE);
[79ae36dd]220 async_exchange_end(exch);
[a35b458]221
[58cbb0c8]222cleanup:
223 if (dev != NULL)
224 dev_del_ref(dev);
[a35b458]225
[58cbb0c8]226 if (fun != NULL)
227 fun_del_ref(fun);
[5cd136ab]228}
229
[984a9ba]230static void devman_forward(ipc_call_t *icall, void *arg)
[ce89036b]231{
[fafb8e5]232 iface_t iface = ipc_get_arg1(icall);
233 service_id_t service_id = ipc_get_arg2(icall);
[a35b458]234
[f9b2cb4c]235 fun_node_t *fun = find_loc_tree_function(&device_tree, service_id);
[a35b458]236
[e2b9b341]237 fibril_rwlock_read_lock(&device_tree.rwlock);
[a35b458]238
[f9b2cb4c]239 if ((fun == NULL) || (fun->dev == NULL) || (fun->dev->drv == NULL)) {
240 log_msg(LOG_DEFAULT, LVL_WARN, "devman_forward(): function "
[12f9f0d0]241 "not found.\n");
[e2b9b341]242 fibril_rwlock_read_unlock(&device_tree.rwlock);
[984a9ba]243 async_answer_0(icall, ENOENT);
[ce89036b]244 return;
245 }
[a35b458]246
[f9b2cb4c]247 dev_node_t *dev = fun->dev;
248 driver_t *driver = dev->drv;
249 devman_handle_t handle = fun->handle;
[a35b458]250
[e2b9b341]251 fibril_rwlock_read_unlock(&device_tree.rwlock);
[a35b458]252
[e2b9b341]253 async_exch_t *exch = async_exchange_begin(driver->sess);
[4f13e19]254 async_forward_1(icall, exch, iface, handle, IPC_FF_NONE);
[79ae36dd]255 async_exchange_end(exch);
[a35b458]256
[a1a101d]257 log_msg(LOG_DEFAULT, LVL_DEBUG,
[f9b2cb4c]258 "Forwarding service request for `%s' function to driver `%s'.",
[e2b9b341]259 fun->pathname, driver->name);
[a35b458]260
[e2b9b341]261 fun_del_ref(fun);
[ce89036b]262}
263
[422722e]264static void *devman_client_data_create(void)
265{
266 client_t *client;
[a35b458]267
[422722e]268 client = calloc(1, sizeof(client_t));
269 if (client == NULL)
270 return NULL;
[a35b458]271
[422722e]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
[38b3baf]281/** Initialize device manager internal structures. */
282static bool devman_init(void)
[e2b9a993]283{
[4c6fd56]284 errno_t rc;
285
[a1a101d]286 log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_init - looking for available drivers.");
[a35b458]287
[38b3baf]288 /* Initialize list of available drivers. */
[0c3666d]289 init_driver_list(&drivers_list);
[c6c389ed]290 if (lookup_available_drivers(&drivers_list,
291 DRIVER_DEFAULT_STORE) == 0) {
[a1a101d]292 log_msg(LOG_DEFAULT, LVL_FATAL, "No drivers found.");
[e2b9a993]293 return false;
294 }
[a35b458]295
[a1a101d]296 log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_init - list of drivers has been initialized.");
[a35b458]297
[38b3baf]298 /* Create root device node. */
[e2b9a993]299 if (!init_device_tree(&device_tree, &drivers_list)) {
[a1a101d]300 log_msg(LOG_DEFAULT, LVL_FATAL, "Failed to initialize device tree.");
[38b3baf]301 return false;
[e2b9a993]302 }
[a35b458]303
[38b3baf]304 /*
[f302586]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.
[38b3baf]310 */
[4c6fd56]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 }
[a35b458]316
[e2b9a993]317 return true;
318}
319
320int main(int argc, char *argv[])
321{
[50ad3f3]322 printf("%s: HelenOS Device Manager\n", NAME);
[a35b458]323
[b7fd2a0]324 errno_t rc = log_init(NAME);
[50ad3f3]325 if (rc != EOK) {
[c1694b6b]326 printf("%s: Error initializing logging subsystem: %s\n", NAME, str_error(rc));
[50ad3f3]327 return rc;
[9b415c9]328 }
[a35b458]329
[422722e]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);
[a35b458]333
[9b1baac]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;
[c1694b6b]339 }
[a35b458]340
[9b1baac]341 /* Register device manager at naming service. */
342 rc = service_register(SERVICE_DEVMAN, INTERFACE_DDF_DRIVER,
343 devman_connection_driver, NULL);
[c1694b6b]344 if (rc != EOK) {
[9b1baac]345 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering driver port: %s", str_error(rc));
[f9b2cb4c]346 return rc;
[c1694b6b]347 }
[a35b458]348
[9b1baac]349 rc = service_register(SERVICE_DEVMAN, INTERFACE_DDF_CLIENT,
350 devman_connection_client, NULL);
[c1694b6b]351 if (rc != EOK) {
[9b1baac]352 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering client port: %s", str_error(rc));
[f9b2cb4c]353 return rc;
[c1694b6b]354 }
[a35b458]355
[9b1baac]356 rc = service_register(SERVICE_DEVMAN, INTERFACE_DEVMAN_DEVICE,
357 devman_connection_device, NULL);
[c1694b6b]358 if (rc != EOK) {
[9b1baac]359 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering device port: %s", str_error(rc));
[f9b2cb4c]360 return rc;
[c1694b6b]361 }
[a35b458]362
[9b1baac]363 rc = service_register(SERVICE_DEVMAN, INTERFACE_DEVMAN_PARENT,
364 devman_connection_parent, NULL);
[50ad3f3]365 if (rc != EOK) {
[9b1baac]366 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering parent port: %s", str_error(rc));
[50ad3f3]367 return rc;
[9b415c9]368 }
[a35b458]369
[50ad3f3]370 printf("%s: Accepting connections.\n", NAME);
[c3d9aaf5]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.");
[79ae36dd]374 task_retval(0);
[e2b9a993]375 async_manager();
[a35b458]376
[38b3baf]377 /* Never reached. */
[e2b9a993]378 return 0;
379}
[c16cf62]380
381/** @}
[38b3baf]382 */
Note: See TracBrowser for help on using the repository browser.