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

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

child device registration - parts of code

  • Property mode set to 100644
File size: 7.1 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#include <thread.h>
54
55#include "devman.h"
56
57#define DRIVER_DEFAULT_STORE "/srv/drivers"
58
59static driver_list_t drivers_list;
60static dev_tree_t device_tree;
61
62/**
63 * Register running driver.
64 */
65static driver_t * devman_driver_register(void)
66{
67 printf(NAME ": devman_driver_register \n");
68
69 ipc_call_t icall;
70 ipc_callid_t iid = async_get_call(&icall);
71 driver_t *driver = NULL;
72
73 if (IPC_GET_METHOD(icall) != DEVMAN_DRIVER_REGISTER) {
74 ipc_answer_0(iid, EREFUSED);
75 return NULL;
76 }
77
78 char *drv_name = NULL;
79
80 // Get driver name
81 int rc = async_string_receive(&drv_name, DEVMAN_NAME_MAXLEN, NULL);
82 if (rc != EOK) {
83 ipc_answer_0(iid, rc);
84 return NULL;
85 }
86 printf(NAME ": the %s driver is trying to register by the service.\n", drv_name);
87
88 // Find driver structure
89 driver = find_driver(&drivers_list, drv_name);
90
91 free(drv_name);
92 drv_name = NULL;
93
94 if (NULL == driver) {
95 printf(NAME ": no driver named %s was found.\n", drv_name);
96 ipc_answer_0(iid, ENOENT);
97 return NULL;
98 }
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
122static void devman_add_child(ipc_callid_t callid, ipc_call_t *call, driver_t *driver)
123{
124 printf(NAME ": devman_add_child\n");
125
126 device_handle_t parent_handle = IPC_GET_ARG1(*call);
127 node_t *parent = find_dev_node(&device_tree, parent_handle);
128
129 if (NULL == parent) {
130 ipc_answer_0(callid, ENOENT);
131 return;
132 }
133
134 char *dev_name = NULL;
135 int rc = async_string_receive(&dev_name, DEVMAN_NAME_MAXLEN, NULL);
136 if (rc != EOK) {
137 ipc_answer_0(callid, rc);
138 return;
139 }
140 printf(NAME ": newly added child device's name is '%s'.\n", dev_name);
141
142 node_t *node = create_dev_node();
143 if (!insert_dev_node(&device_tree, node, dev_name, parent)) {
144 delete_dev_node(node);
145 ipc_answer_0(callid, ENOMEM);
146 return;
147 }
148
149 // TODO match ids
150
151 // return device handle to parent's driver
152 ipc_answer_1(callid, EOK, node->handle);
153
154 // try to find suitable driver and assign it to the device
155 assign_driver(node, &drivers_list);
156}
157
158static int init_running_drv(void *drv)
159{
160 driver_t *driver = (driver_t *)drv;
161 initialize_running_driver(driver);
162 printf(NAME ": the %s driver was successfully initialized. \n", driver->name);
163 return 0;
164}
165
166/** Function for handling connections to device manager.
167 *
168 */
169static void devman_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
170{
171 /* Accept the connection */
172 ipc_answer_0(iid, EOK);
173
174 driver_t *driver = devman_driver_register();
175 if (NULL == driver)
176 return;
177
178 fid_t fid = fibril_create(init_running_drv, driver);
179 if (fid == 0) {
180 printf(NAME ": Error creating fibril for the initialization of the newly registered running driver.\n");
181 exit(1);
182 }
183 fibril_add_ready(fid);
184
185 /*thread_id_t tid;
186 if (0 != thread_create(init_running_drv, driver, "init_running_drv", &tid)) {
187 printf(NAME ": failed to start the initialization of the newly registered running driver.\n");
188 }*/
189
190 ipc_callid_t callid;
191 ipc_call_t call;
192 bool cont = true;
193 while (cont) {
194 callid = async_get_call(&call);
195
196 switch (IPC_GET_METHOD(call)) {
197 case IPC_M_PHONE_HUNGUP:
198 cont = false;
199 continue;
200 case DEVMAN_ADD_CHILD_DEVICE:
201 devman_add_child(callid, &call, driver);
202 break;
203 default:
204 ipc_answer_0(callid, EINVAL);
205 break;
206 }
207 }
208}
209
210/** Function for handling connections to device manager.
211 *
212 */
213static void devman_connection(ipc_callid_t iid, ipc_call_t *icall)
214{
215 // Select interface
216 switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
217 case DEVMAN_DRIVER:
218 devman_connection_driver(iid, icall);
219 break;
220 /*case DEVMAN_CLIENT:
221 devmap_connection_client(iid, icall);
222 break;
223 case DEVMAN_CONNECT_TO_DEVICE:
224 // Connect client to selected device
225 devmap_forward(iid, icall);
226 break;*/
227 default:
228 /* No such interface */
229 ipc_answer_0(iid, ENOENT);
230 }
231}
232
233/** Initialize device manager internal structures.
234 */
235static bool devman_init()
236{
237 printf(NAME ": devman_init - looking for available drivers. \n");
238
239 // initialize list of available drivers
240 init_driver_list(&drivers_list);
241 if (0 == lookup_available_drivers(&drivers_list, DRIVER_DEFAULT_STORE)) {
242 printf(NAME " no drivers found.");
243 return false;
244 }
245 printf(NAME ": devman_init - list of drivers has been initialized. \n");
246
247 // create root device node
248 if (!init_device_tree(&device_tree, &drivers_list)) {
249 printf(NAME " failed to initialize device tree.");
250 return false;
251 }
252
253 return true;
254}
255
256int main(int argc, char *argv[])
257{
258 printf(NAME ": HelenOS Device Manager\n");
259
260 if (!devman_init()) {
261 printf(NAME ": Error while initializing service\n");
262 return -1;
263 }
264
265 // Set a handler of incomming connections
266 async_set_client_connection(devman_connection);
267
268 // Register device manager at naming service
269 ipcarg_t phonead;
270 if (ipc_connect_to_me(PHONE_NS, SERVICE_DEVMAN, 0, 0, &phonead) != 0)
271 return -1;
272
273 printf(NAME ": Accepting connections\n");
274 async_manager();
275
276 // Never reached
277 return 0;
278}
279
280/** @}
281 */
Note: See TracBrowser for help on using the repository browser.