source: mainline/uspace/srv/devman/main.c@ 5e801dc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5e801dc was fafb8e5, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 6 years ago

Mechanically lowercase IPC_SET_*/IPC_GET_*

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