source: mainline/uspace/srv/devman/main.c@ 8e3498b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8e3498b was f9b2cb4c, checked in by Martin Decky <martin@…>, 10 years ago

unify interface API

  • introduce new interfaces
  • unify location service clients to always expect service ID as the second argument
  • remove obsolete methods that take explicit exchange management arguments (first phase)
  • use interfaces in device drivers, devman, location service, logger, inet
  • Property mode set to 100644
File size: 9.9 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>
[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
[f9b2cb4c]67static void devman_connection_device(ipc_callid_t iid, ipc_call_t *icall,
68 void *arg)
[38b3baf]69{
[0b5a4131]70 devman_handle_t handle = IPC_GET_ARG2(*icall);
[8b1e15ac]71 dev_node_t *dev = NULL;
[9a66bc2e]72
[f9b2cb4c]73 fun_node_t *fun = find_fun_node(&device_tree, handle);
74 if (fun == NULL) {
[8b1e15ac]75 dev = find_dev_node(&device_tree, handle);
[f9b2cb4c]76 } else {
[58cbb0c8]77 fibril_rwlock_read_lock(&device_tree.rwlock);
[f9b2cb4c]78
[8b1e15ac]79 dev = fun->dev;
[58cbb0c8]80 if (dev != NULL)
81 dev_add_ref(dev);
[f9b2cb4c]82
[58cbb0c8]83 fibril_rwlock_read_unlock(&device_tree.rwlock);
84 }
[f9b2cb4c]85
[0418050]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) {
[a1a101d]93 log_msg(LOG_DEFAULT, LVL_ERROR, "IPC forwarding failed - no device or "
[ebcb05a]94 "function with handle %" PRIun " was found.", handle);
[ffa2c8ef]95 async_answer_0(iid, ENOENT);
[58cbb0c8]96 goto cleanup;
[5cd136ab]97 }
[f9b2cb4c]98
99 if (fun == NULL) {
[a1a101d]100 log_msg(LOG_DEFAULT, LVL_ERROR, NAME ": devman_forward error - cannot "
[ebcb05a]101 "connect to handle %" PRIun ", refers to a device.",
[9b415c9]102 handle);
[ffa2c8ef]103 async_answer_0(iid, ENOENT);
[58cbb0c8]104 goto cleanup;
[5cd136ab]105 }
106
[e2b9b341]107 fibril_rwlock_read_lock(&device_tree.rwlock);
108
[f9b2cb4c]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(iid, 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(iid, 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(iid, 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_callid_t iid, ipc_call_t *icall,
151 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);
[005ac3c]159 } else {
[f9b2cb4c]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);
[5cd136ab]167 }
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);
178 async_answer_0(iid, 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
[e2b9b341]192 fibril_rwlock_read_unlock(&device_tree.rwlock);
193
[c6c389ed]194 if (driver == NULL) {
[a1a101d]195 log_msg(LOG_DEFAULT, LVL_ERROR, "IPC forwarding refused - " \
[79ae36dd]196 "the device %" PRIun " is not in usable state.", handle);
[ffa2c8ef]197 async_answer_0(iid, ENOENT);
[58cbb0c8]198 goto cleanup;
[5cd136ab]199 }
200
[79ae36dd]201 if (!driver->sess) {
[a1a101d]202 log_msg(LOG_DEFAULT, LVL_ERROR,
[79ae36dd]203 "Could not forward to driver `%s'.", driver->name);
[ffa2c8ef]204 async_answer_0(iid, EINVAL);
[58cbb0c8]205 goto cleanup;
[9a66bc2e]206 }
[f9b2cb4c]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 }
[79ae36dd]217
218 async_exch_t *exch = async_exchange_begin(driver->sess);
[f9b2cb4c]219 async_forward_fast(iid, exch, INTERFACE_DDF_DRIVER, fun_handle, 0, IPC_FF_NONE);
[79ae36dd]220 async_exchange_end(exch);
[50ad3f3]221
[58cbb0c8]222cleanup:
223 if (dev != NULL)
224 dev_del_ref(dev);
[50ad3f3]225
[58cbb0c8]226 if (fun != NULL)
227 fun_del_ref(fun);
[5cd136ab]228}
229
[f9b2cb4c]230static void devman_forward(ipc_callid_t iid, ipc_call_t *icall, void *arg)
[ce89036b]231{
[f9b2cb4c]232 iface_t iface = IPC_GET_ARG1(*icall);
[15f3c3f]233 service_id_t service_id = IPC_GET_ARG2(*icall);
[f9b2cb4c]234
235 fun_node_t *fun = find_loc_tree_function(&device_tree, service_id);
[ce89036b]236
[e2b9b341]237 fibril_rwlock_read_lock(&device_tree.rwlock);
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);
[ffa2c8ef]243 async_answer_0(iid, ENOENT);
[ce89036b]244 return;
245 }
246
[f9b2cb4c]247 dev_node_t *dev = fun->dev;
248 driver_t *driver = dev->drv;
249 devman_handle_t handle = fun->handle;
[e2b9b341]250
251 fibril_rwlock_read_unlock(&device_tree.rwlock);
[ba38f72c]252
[e2b9b341]253 async_exch_t *exch = async_exchange_begin(driver->sess);
[f9b2cb4c]254 async_forward_fast(iid, exch, iface, handle, 0, IPC_FF_NONE);
[79ae36dd]255 async_exchange_end(exch);
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);
[f9b2cb4c]260
[e2b9b341]261 fun_del_ref(fun);
[ce89036b]262}
263
[422722e]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
[38b3baf]281/** Initialize device manager internal structures. */
282static bool devman_init(void)
[e2b9a993]283{
[a1a101d]284 log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_init - looking for available drivers.");
[08d9c4e6]285
[38b3baf]286 /* Initialize list of available drivers. */
[0c3666d]287 init_driver_list(&drivers_list);
[c6c389ed]288 if (lookup_available_drivers(&drivers_list,
289 DRIVER_DEFAULT_STORE) == 0) {
[a1a101d]290 log_msg(LOG_DEFAULT, LVL_FATAL, "No drivers found.");
[e2b9a993]291 return false;
292 }
[50ad3f3]293
[a1a101d]294 log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_init - list of drivers has been initialized.");
[50ad3f3]295
[38b3baf]296 /* Create root device node. */
[e2b9a993]297 if (!init_device_tree(&device_tree, &drivers_list)) {
[a1a101d]298 log_msg(LOG_DEFAULT, LVL_FATAL, "Failed to initialize device tree.");
[38b3baf]299 return false;
[e2b9a993]300 }
[50ad3f3]301
[38b3baf]302 /*
[f302586]303 * Caution: As the device manager is not a real loc
304 * driver (it uses a completely different IPC protocol
305 * than an ordinary loc driver), forwarding a connection
306 * from client to the devman by location service will
307 * not work.
[38b3baf]308 */
[f302586]309 loc_server_register(NAME);
[ce89036b]310
[e2b9a993]311 return true;
312}
313
314int main(int argc, char *argv[])
315{
[50ad3f3]316 printf("%s: HelenOS Device Manager\n", NAME);
317
[267f235]318 int rc = log_init(NAME);
[50ad3f3]319 if (rc != EOK) {
320 printf("%s: Error initializing logging subsystem.\n", NAME);
321 return rc;
[9b415c9]322 }
[e2b9a993]323
[422722e]324 /* Set handlers for incoming connections. */
325 async_set_client_data_constructor(devman_client_data_create);
326 async_set_client_data_destructor(devman_client_data_destroy);
[f9b2cb4c]327
328 port_id_t port;
329 rc = async_create_port(INTERFACE_DDF_DRIVER,
330 devman_connection_driver, NULL, &port);
331 if (rc != EOK)
332 return rc;
333
334 rc = async_create_port(INTERFACE_DDF_CLIENT,
335 devman_connection_client, NULL, &port);
336 if (rc != EOK)
337 return rc;
338
339 rc = async_create_port(INTERFACE_DEVMAN_DEVICE,
340 devman_connection_device, NULL, &port);
341 if (rc != EOK)
342 return rc;
343
344 rc = async_create_port(INTERFACE_DEVMAN_PARENT,
345 devman_connection_parent, NULL, &port);
346 if (rc != EOK)
347 return rc;
348
349 async_set_fallback_port_handler(devman_forward, NULL);
[50ad3f3]350
[f302586]351 if (!devman_init()) {
[a1a101d]352 log_msg(LOG_DEFAULT, LVL_ERROR, "Error while initializing service.");
[f302586]353 return -1;
354 }
[50ad3f3]355
[38b3baf]356 /* Register device manager at naming service. */
[50ad3f3]357 rc = service_register(SERVICE_DEVMAN);
358 if (rc != EOK) {
[a1a101d]359 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering as a service.");
[50ad3f3]360 return rc;
[9b415c9]361 }
[50ad3f3]362
363 printf("%s: Accepting connections.\n", NAME);
[79ae36dd]364 task_retval(0);
[e2b9a993]365 async_manager();
[50ad3f3]366
[38b3baf]367 /* Never reached. */
[e2b9a993]368 return 0;
369}
[c16cf62]370
371/** @}
[38b3baf]372 */
Note: See TracBrowser for help on using the repository browser.