source: mainline/uspace/srv/devman/main.c@ 3e242d2

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3e242d2 was 3be9d10, checked in by Jakub Jermar <jakub@…>, 7 years ago

Get rid of ipc_callid_t

  • Property mode set to 100644
File size: 10.2 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/**
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>
[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;
[e2b9a993]67
[3be9d10]68static void devman_connection_device(cap_call_handle_t iid, ipc_call_t *icall,
[f9b2cb4c]69 void *arg)
[38b3baf]70{
[0b5a4131]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);
[ffa2c8ef]96 async_answer_0(iid, 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);
[ffa2c8ef]104 async_answer_0(iid, 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);
118 async_answer_0(iid, ENOENT);
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);
125 async_answer_0(iid, EINVAL);
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);
140 async_forward_fast(iid, exch, INTERFACE_DDF_CLIENT, handle, 0, IPC_FF_NONE);
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
[3be9d10]151static void devman_connection_parent(cap_call_handle_t iid, ipc_call_t *icall,
[f9b2cb4c]152 void *arg)
153{
154 devman_handle_t handle = IPC_GET_ARG2(*icall);
155 dev_node_t *dev = NULL;
[a35b458]156
[f9b2cb4c]157 fun_node_t *fun = find_fun_node(&device_tree, handle);
158 if (fun == NULL) {
159 dev = find_dev_node(&device_tree, handle);
[005ac3c]160 } else {
[f9b2cb4c]161 fibril_rwlock_read_lock(&device_tree.rwlock);
[a35b458]162
[f9b2cb4c]163 dev = fun->dev;
164 if (dev != NULL)
165 dev_add_ref(dev);
[a35b458]166
[f9b2cb4c]167 fibril_rwlock_read_unlock(&device_tree.rwlock);
[5cd136ab]168 }
[a35b458]169
[f9b2cb4c]170 /*
171 * For a valid function to connect to we need a device. The root
172 * function, for example, has no device and cannot be connected to.
173 * This means @c dev needs to be valid regardless whether we are
174 * connecting to a device or to a function.
175 */
176 if (dev == NULL) {
177 log_msg(LOG_DEFAULT, LVL_ERROR, "IPC forwarding failed - no device or "
178 "function with handle %" PRIun " was found.", handle);
179 async_answer_0(iid, ENOENT);
180 goto cleanup;
181 }
[a35b458]182
[f9b2cb4c]183 driver_t *driver = NULL;
[a35b458]184
[f9b2cb4c]185 fibril_rwlock_read_lock(&device_tree.rwlock);
[a35b458]186
[f9b2cb4c]187 /* Connect to parent function of a device (or device function). */
188 if (dev->pfun->dev != NULL)
189 driver = dev->pfun->dev->drv;
[a35b458]190
[f9b2cb4c]191 devman_handle_t fun_handle = dev->pfun->handle;
[a35b458]192
[e2b9b341]193 fibril_rwlock_read_unlock(&device_tree.rwlock);
[a35b458]194
[c6c389ed]195 if (driver == NULL) {
[5ef16903]196 log_msg(LOG_DEFAULT, LVL_ERROR, "IPC forwarding refused - "
[79ae36dd]197 "the device %" PRIun " is not in usable state.", handle);
[ffa2c8ef]198 async_answer_0(iid, ENOENT);
[58cbb0c8]199 goto cleanup;
[5cd136ab]200 }
[a35b458]201
[79ae36dd]202 if (!driver->sess) {
[a1a101d]203 log_msg(LOG_DEFAULT, LVL_ERROR,
[79ae36dd]204 "Could not forward to driver `%s'.", driver->name);
[ffa2c8ef]205 async_answer_0(iid, EINVAL);
[58cbb0c8]206 goto cleanup;
[9a66bc2e]207 }
[a35b458]208
[8b1e15ac]209 if (fun != NULL) {
[a1a101d]210 log_msg(LOG_DEFAULT, LVL_DEBUG,
[ebcb05a]211 "Forwarding request for `%s' function to driver `%s'.",
[9b415c9]212 fun->pathname, driver->name);
[8b1e15ac]213 } else {
[a1a101d]214 log_msg(LOG_DEFAULT, LVL_DEBUG,
[ebcb05a]215 "Forwarding request for `%s' device to driver `%s'.",
[9b415c9]216 dev->pfun->pathname, driver->name);
[8b1e15ac]217 }
[a35b458]218
[79ae36dd]219 async_exch_t *exch = async_exchange_begin(driver->sess);
[f9b2cb4c]220 async_forward_fast(iid, exch, INTERFACE_DDF_DRIVER, fun_handle, 0, IPC_FF_NONE);
[79ae36dd]221 async_exchange_end(exch);
[a35b458]222
[58cbb0c8]223cleanup:
224 if (dev != NULL)
225 dev_del_ref(dev);
[a35b458]226
[58cbb0c8]227 if (fun != NULL)
228 fun_del_ref(fun);
[5cd136ab]229}
230
[3be9d10]231static void devman_forward(cap_call_handle_t iid, ipc_call_t *icall, void *arg)
[ce89036b]232{
[f9b2cb4c]233 iface_t iface = IPC_GET_ARG1(*icall);
[15f3c3f]234 service_id_t service_id = IPC_GET_ARG2(*icall);
[a35b458]235
[f9b2cb4c]236 fun_node_t *fun = find_loc_tree_function(&device_tree, service_id);
[a35b458]237
[e2b9b341]238 fibril_rwlock_read_lock(&device_tree.rwlock);
[a35b458]239
[f9b2cb4c]240 if ((fun == NULL) || (fun->dev == NULL) || (fun->dev->drv == NULL)) {
241 log_msg(LOG_DEFAULT, LVL_WARN, "devman_forward(): function "
[12f9f0d0]242 "not found.\n");
[e2b9b341]243 fibril_rwlock_read_unlock(&device_tree.rwlock);
[ffa2c8ef]244 async_answer_0(iid, ENOENT);
[ce89036b]245 return;
246 }
[a35b458]247
[f9b2cb4c]248 dev_node_t *dev = fun->dev;
249 driver_t *driver = dev->drv;
250 devman_handle_t handle = fun->handle;
[a35b458]251
[e2b9b341]252 fibril_rwlock_read_unlock(&device_tree.rwlock);
[a35b458]253
[e2b9b341]254 async_exch_t *exch = async_exchange_begin(driver->sess);
[f9b2cb4c]255 async_forward_fast(iid, exch, iface, handle, 0, IPC_FF_NONE);
[79ae36dd]256 async_exchange_end(exch);
[a35b458]257
[a1a101d]258 log_msg(LOG_DEFAULT, LVL_DEBUG,
[f9b2cb4c]259 "Forwarding service request for `%s' function to driver `%s'.",
[e2b9b341]260 fun->pathname, driver->name);
[a35b458]261
[e2b9b341]262 fun_del_ref(fun);
[ce89036b]263}
264
[422722e]265static void *devman_client_data_create(void)
266{
267 client_t *client;
[a35b458]268
[422722e]269 client = calloc(1, sizeof(client_t));
270 if (client == NULL)
271 return NULL;
[a35b458]272
[422722e]273 fibril_mutex_initialize(&client->mutex);
274 return client;
275}
276
277static void devman_client_data_destroy(void *data)
278{
279 free(data);
280}
281
[38b3baf]282/** Initialize device manager internal structures. */
283static bool devman_init(void)
[e2b9a993]284{
[a1a101d]285 log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_init - looking for available drivers.");
[a35b458]286
[38b3baf]287 /* Initialize list of available drivers. */
[0c3666d]288 init_driver_list(&drivers_list);
[c6c389ed]289 if (lookup_available_drivers(&drivers_list,
290 DRIVER_DEFAULT_STORE) == 0) {
[a1a101d]291 log_msg(LOG_DEFAULT, LVL_FATAL, "No drivers found.");
[e2b9a993]292 return false;
293 }
[a35b458]294
[a1a101d]295 log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_init - list of drivers has been initialized.");
[a35b458]296
[38b3baf]297 /* Create root device node. */
[e2b9a993]298 if (!init_device_tree(&device_tree, &drivers_list)) {
[a1a101d]299 log_msg(LOG_DEFAULT, LVL_FATAL, "Failed to initialize device tree.");
[38b3baf]300 return false;
[e2b9a993]301 }
[a35b458]302
[38b3baf]303 /*
[f302586]304 * Caution: As the device manager is not a real loc
305 * driver (it uses a completely different IPC protocol
306 * than an ordinary loc driver), forwarding a connection
307 * from client to the devman by location service will
308 * not work.
[38b3baf]309 */
[f302586]310 loc_server_register(NAME);
[a35b458]311
[e2b9a993]312 return true;
313}
314
315int main(int argc, char *argv[])
316{
[50ad3f3]317 printf("%s: HelenOS Device Manager\n", NAME);
[a35b458]318
[b7fd2a0]319 errno_t rc = log_init(NAME);
[50ad3f3]320 if (rc != EOK) {
[c1694b6b]321 printf("%s: Error initializing logging subsystem: %s\n", NAME, str_error(rc));
[50ad3f3]322 return rc;
[9b415c9]323 }
[a35b458]324
[422722e]325 /* Set handlers for incoming connections. */
326 async_set_client_data_constructor(devman_client_data_create);
327 async_set_client_data_destructor(devman_client_data_destroy);
[a35b458]328
[f9b2cb4c]329 port_id_t port;
330 rc = async_create_port(INTERFACE_DDF_DRIVER,
331 devman_connection_driver, NULL, &port);
[c1694b6b]332 if (rc != EOK) {
333 printf("%s: Error creating DDF driver port: %s\n", NAME, str_error(rc));
[f9b2cb4c]334 return rc;
[c1694b6b]335 }
[a35b458]336
[f9b2cb4c]337 rc = async_create_port(INTERFACE_DDF_CLIENT,
338 devman_connection_client, NULL, &port);
[c1694b6b]339 if (rc != EOK) {
340 printf("%s: Error creating DDF client port: %s\n", NAME, str_error(rc));
[f9b2cb4c]341 return rc;
[c1694b6b]342 }
[a35b458]343
[f9b2cb4c]344 rc = async_create_port(INTERFACE_DEVMAN_DEVICE,
345 devman_connection_device, NULL, &port);
[c1694b6b]346 if (rc != EOK) {
347 printf("%s: Error creating devman device port: %s\n", NAME, str_error(rc));
[f9b2cb4c]348 return rc;
[c1694b6b]349 }
[a35b458]350
[f9b2cb4c]351 rc = async_create_port(INTERFACE_DEVMAN_PARENT,
352 devman_connection_parent, NULL, &port);
[c1694b6b]353 if (rc != EOK) {
354 printf("%s: Error creating devman parent port: %s\n", NAME, str_error(rc));
[f9b2cb4c]355 return rc;
[c1694b6b]356 }
[a35b458]357
[f9b2cb4c]358 async_set_fallback_port_handler(devman_forward, NULL);
[a35b458]359
[f302586]360 if (!devman_init()) {
[a1a101d]361 log_msg(LOG_DEFAULT, LVL_ERROR, "Error while initializing service.");
[f302586]362 return -1;
363 }
[a35b458]364
[38b3baf]365 /* Register device manager at naming service. */
[50ad3f3]366 rc = service_register(SERVICE_DEVMAN);
367 if (rc != EOK) {
[c1694b6b]368 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering as a service: %s", str_error(rc));
[50ad3f3]369 return rc;
[9b415c9]370 }
[a35b458]371
[50ad3f3]372 printf("%s: Accepting connections.\n", NAME);
[79ae36dd]373 task_retval(0);
[e2b9a993]374 async_manager();
[a35b458]375
[38b3baf]376 /* Never reached. */
[e2b9a993]377 return 0;
378}
[c16cf62]379
380/** @}
[38b3baf]381 */
Note: See TracBrowser for help on using the repository browser.