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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c16cf62 was c16cf62, checked in by Lenka Trochtova <trochtova.lenka@…>, 15 years ago

backup (unstable)

  • Property mode set to 100644
File size: 5.7 KB
Line 
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>
40#include <ipc/ns.h>
41#include <async.h>
42#include <stdio.h>
43#include <errno.h>
44#include <bool.h>
45#include <fibril_synch.h>
46#include <stdlib.h>
47#include <string.h>
48#include <dirent.h>
49#include <fcntl.h>
50#include <sys/stat.h>
51#include <ctype.h>
52#include <ipc/devman.h>
53
54#include "devman.h"
55
56#define DRIVER_DEFAULT_STORE "/srv/drivers"
57
58static driver_list_t drivers_list;
59static dev_tree_t device_tree;
60
61/**
62 * Register running driver.
63 */
64static driver_t * devman_driver_register(void)
65{
66 printf(NAME ": devman_driver_register \n");
67
68 ipc_call_t icall;
69 ipc_callid_t iid = async_get_call(&icall);
70 driver_t *driver = NULL;
71
72 if (IPC_GET_METHOD(icall) != DEVMAN_DRIVER_REGISTER) {
73 ipc_answer_0(iid, EREFUSED);
74 return NULL;
75 }
76
77 char *drv_name = NULL;
78
79 // Get driver name
80 int rc = async_string_receive(&drv_name, DEVMAN_NAME_MAXLEN, NULL);
81 if (rc != EOK) {
82 ipc_answer_0(iid, rc);
83 return NULL;
84 }
85 printf(NAME ": the %s driver is trying to register by the service.\n", drv_name);
86
87 // Find driver structure
88 driver = find_driver(&drivers_list, drv_name);
89
90 free(drv_name);
91 drv_name = NULL;
92
93 if (NULL == driver) {
94 printf(NAME ": no driver named %s was found.\n", drv_name);
95 ipc_answer_0(iid, ENOENT);
96 return NULL;
97 }
98 printf(NAME ": registering the running instance of the %s driver.\n", driver->name);
99
100 // Create connection to the driver
101 printf(NAME ": creating connection to the %s driver.\n", driver->name);
102 ipc_call_t call;
103 ipc_callid_t callid = async_get_call(&call);
104 if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
105 ipc_answer_0(callid, ENOTSUP);
106 ipc_answer_0(iid, ENOTSUP);
107 return NULL;
108 }
109
110 // remember driver's phone
111 set_driver_phone(driver, IPC_GET_ARG5(call));
112
113 printf(NAME ": the %s driver was successfully registered as running.\n", driver->name);
114
115 ipc_answer_0(callid, EOK);
116
117 ipc_answer_0(iid, EOK);
118
119 return driver;
120}
121
122/** Function for handling connections to device manager.
123 *
124 */
125static void devman_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
126{
127 /* Accept the connection */
128 ipc_answer_0(iid, EOK);
129
130 driver_t *driver = devman_driver_register();
131 if (driver == NULL)
132 return;
133
134 initialize_running_driver(driver);
135
136 ipc_callid_t callid;
137 ipc_call_t call;
138 bool cont = true;
139 while (cont) {
140 callid = async_get_call(&call);
141
142 switch (IPC_GET_METHOD(call)) {
143 case IPC_M_PHONE_HUNGUP:
144 cont = false;
145 continue;
146 case DEVMAN_ADD_CHILD_DEVICE:
147 // TODO add new device node to the device tree
148 break;
149 default:
150 ipc_answer_0(callid, EINVAL);
151 break;
152 }
153 }
154}
155
156/** Function for handling connections to device manager.
157 *
158 */
159static void devman_connection(ipc_callid_t iid, ipc_call_t *icall)
160{
161 // Select interface
162 switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
163 case DEVMAN_DRIVER:
164 devman_connection_driver(iid, icall);
165 break;
166 /*case DEVMAN_CLIENT:
167 devmap_connection_client(iid, icall);
168 break;
169 case DEVMAN_CONNECT_TO_DEVICE:
170 // Connect client to selected device
171 devmap_forward(iid, icall);
172 break;*/
173 default:
174 /* No such interface */
175 ipc_answer_0(iid, ENOENT);
176 }
177}
178
179/** Initialize device manager internal structures.
180 */
181static bool devman_init()
182{
183 printf(NAME ": devman_init - looking for available drivers. \n");
184
185 // initialize list of available drivers
186 init_driver_list(&drivers_list);
187 if (0 == lookup_available_drivers(&drivers_list, DRIVER_DEFAULT_STORE)) {
188 printf(NAME " no drivers found.");
189 return false;
190 }
191 printf(NAME ": devman_init - list of drivers has been initialized. \n");
192
193 // create root device node
194 if (!init_device_tree(&device_tree, &drivers_list)) {
195 printf(NAME " failed to initialize device tree.");
196 return false;
197 }
198
199 return true;
200}
201
202int main(int argc, char *argv[])
203{
204 printf(NAME ": HelenOS Device Manager\n");
205
206 if (!devman_init()) {
207 printf(NAME ": Error while initializing service\n");
208 return -1;
209 }
210
211 // Set a handler of incomming connections
212 async_set_client_connection(devman_connection);
213
214 // Register device manager at naming service
215 ipcarg_t phonead;
216 if (ipc_connect_to_me(PHONE_NS, SERVICE_DEVMAN, 0, 0, &phonead) != 0)
217 return -1;
218
219 printf(NAME ": Accepting connections\n");
220 async_manager();
221
222 // Never reached
223 return 0;
224}
225
226/** @}
227 */
Note: See TracBrowser for help on using the repository browser.