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 <ipc/driver.h>
|
---|
54 | #include <thread.h>
|
---|
55 |
|
---|
56 | #include "devman.h"
|
---|
57 |
|
---|
58 | #define DRIVER_DEFAULT_STORE "/srv/drivers"
|
---|
59 |
|
---|
60 | static driver_list_t drivers_list;
|
---|
61 | static dev_tree_t device_tree;
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Register running driver.
|
---|
65 | */
|
---|
66 | static driver_t * devman_driver_register(void)
|
---|
67 | {
|
---|
68 | printf(NAME ": devman_driver_register \n");
|
---|
69 |
|
---|
70 | ipc_call_t icall;
|
---|
71 | ipc_callid_t iid = async_get_call(&icall);
|
---|
72 | driver_t *driver = NULL;
|
---|
73 |
|
---|
74 | if (IPC_GET_METHOD(icall) != DEVMAN_DRIVER_REGISTER) {
|
---|
75 | ipc_answer_0(iid, EREFUSED);
|
---|
76 | return NULL;
|
---|
77 | }
|
---|
78 |
|
---|
79 | char *drv_name = NULL;
|
---|
80 |
|
---|
81 | // Get driver name
|
---|
82 | int rc = async_string_receive(&drv_name, DEVMAN_NAME_MAXLEN, NULL);
|
---|
83 | if (rc != EOK) {
|
---|
84 | ipc_answer_0(iid, rc);
|
---|
85 | return NULL;
|
---|
86 | }
|
---|
87 | printf(NAME ": the %s driver is trying to register by the service.\n", drv_name);
|
---|
88 |
|
---|
89 | // Find driver structure
|
---|
90 | driver = find_driver(&drivers_list, drv_name);
|
---|
91 |
|
---|
92 | free(drv_name);
|
---|
93 | drv_name = NULL;
|
---|
94 |
|
---|
95 | if (NULL == driver) {
|
---|
96 | printf(NAME ": no driver named %s was found.\n", drv_name);
|
---|
97 | ipc_answer_0(iid, ENOENT);
|
---|
98 | return NULL;
|
---|
99 | }
|
---|
100 |
|
---|
101 | // Create connection to the driver
|
---|
102 | printf(NAME ": creating connection to the %s driver.\n", driver->name);
|
---|
103 | ipc_call_t call;
|
---|
104 | ipc_callid_t callid = async_get_call(&call);
|
---|
105 | if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
|
---|
106 | ipc_answer_0(callid, ENOTSUP);
|
---|
107 | ipc_answer_0(iid, ENOTSUP);
|
---|
108 | return NULL;
|
---|
109 | }
|
---|
110 |
|
---|
111 | // remember driver's phone
|
---|
112 | set_driver_phone(driver, IPC_GET_ARG5(call));
|
---|
113 |
|
---|
114 | printf(NAME ": the %s driver was successfully registered as running.\n", driver->name);
|
---|
115 |
|
---|
116 | ipc_answer_0(callid, EOK);
|
---|
117 |
|
---|
118 | ipc_answer_0(iid, EOK);
|
---|
119 |
|
---|
120 | return driver;
|
---|
121 | }
|
---|
122 |
|
---|
123 | static int devman_receive_match_id(match_id_list_t *match_ids) {
|
---|
124 |
|
---|
125 | match_id_t *match_id = create_match_id();
|
---|
126 | ipc_callid_t callid;
|
---|
127 | ipc_call_t call;
|
---|
128 | int rc = 0;
|
---|
129 |
|
---|
130 | callid = async_get_call(&call);
|
---|
131 | if (DEVMAN_ADD_MATCH_ID != IPC_GET_METHOD(call)) {
|
---|
132 | printf(NAME ": ERROR: devman_receive_match_id - invalid protocol.\n");
|
---|
133 | ipc_answer_0(callid, EINVAL);
|
---|
134 | delete_match_id(match_id);
|
---|
135 | return EINVAL;
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (NULL == match_id) {
|
---|
139 | printf(NAME ": ERROR: devman_receive_match_id - failed to allocate match id.\n");
|
---|
140 | ipc_answer_0(callid, ENOMEM);
|
---|
141 | return ENOMEM;
|
---|
142 | }
|
---|
143 |
|
---|
144 | ipc_answer_0(callid, EOK);
|
---|
145 |
|
---|
146 | match_id->score = IPC_GET_ARG1(call);
|
---|
147 |
|
---|
148 | rc = async_string_receive(&match_id->id, DEVMAN_NAME_MAXLEN, NULL);
|
---|
149 | if (EOK != rc) {
|
---|
150 | delete_match_id(match_id);
|
---|
151 | printf(NAME ": devman_receive_match_id - failed to receive match id string.\n");
|
---|
152 | return rc;
|
---|
153 | }
|
---|
154 |
|
---|
155 | list_append(&match_id->link, &match_ids->ids);
|
---|
156 |
|
---|
157 | printf(NAME ": received match id '%s', score = %d \n", match_id->id, match_id->score);
|
---|
158 | return rc;
|
---|
159 | }
|
---|
160 |
|
---|
161 | static int devman_receive_match_ids(ipcarg_t match_count, match_id_list_t *match_ids)
|
---|
162 | {
|
---|
163 | int ret = EOK;
|
---|
164 | size_t i;
|
---|
165 | for (i = 0; i < match_count; i++) {
|
---|
166 | if (EOK != (ret = devman_receive_match_id(match_ids))) {
|
---|
167 | return ret;
|
---|
168 | }
|
---|
169 | }
|
---|
170 | return ret;
|
---|
171 | }
|
---|
172 |
|
---|
173 | static void devman_add_child(ipc_callid_t callid, ipc_call_t *call, driver_t *driver)
|
---|
174 | {
|
---|
175 | // printf(NAME ": devman_add_child\n");
|
---|
176 |
|
---|
177 | device_handle_t parent_handle = IPC_GET_ARG1(*call);
|
---|
178 | ipcarg_t match_count = IPC_GET_ARG2(*call);
|
---|
179 |
|
---|
180 | node_t *parent = find_dev_node(&device_tree, parent_handle);
|
---|
181 |
|
---|
182 | if (NULL == parent) {
|
---|
183 | ipc_answer_0(callid, ENOENT);
|
---|
184 | return;
|
---|
185 | }
|
---|
186 |
|
---|
187 | char *dev_name = NULL;
|
---|
188 | int rc = async_string_receive(&dev_name, DEVMAN_NAME_MAXLEN, NULL);
|
---|
189 | if (EOK != rc) {
|
---|
190 | ipc_answer_0(callid, rc);
|
---|
191 | return;
|
---|
192 | }
|
---|
193 | // printf(NAME ": newly added child device's name is '%s'.\n", dev_name);
|
---|
194 |
|
---|
195 | node_t *node = create_dev_node();
|
---|
196 | if (!insert_dev_node(&device_tree, node, dev_name, parent)) {
|
---|
197 | delete_dev_node(node);
|
---|
198 | ipc_answer_0(callid, ENOMEM);
|
---|
199 | return;
|
---|
200 | }
|
---|
201 |
|
---|
202 | printf(NAME ": devman_add_child %s\n", node->pathname);
|
---|
203 |
|
---|
204 | devman_receive_match_ids(match_count, &node->match_ids);
|
---|
205 |
|
---|
206 | // return device handle to parent's driver
|
---|
207 | ipc_answer_1(callid, EOK, node->handle);
|
---|
208 |
|
---|
209 | // try to find suitable driver and assign it to the device
|
---|
210 | assign_driver(node, &drivers_list);
|
---|
211 | }
|
---|
212 |
|
---|
213 | static int init_running_drv(void *drv)
|
---|
214 | {
|
---|
215 | driver_t *driver = (driver_t *)drv;
|
---|
216 | initialize_running_driver(driver);
|
---|
217 | printf(NAME ": the %s driver was successfully initialized. \n", driver->name);
|
---|
218 | return 0;
|
---|
219 | }
|
---|
220 |
|
---|
221 | /** Function for handling connections to device manager.
|
---|
222 | *
|
---|
223 | */
|
---|
224 | static void devman_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
|
---|
225 | {
|
---|
226 | /* Accept the connection */
|
---|
227 | ipc_answer_0(iid, EOK);
|
---|
228 |
|
---|
229 | driver_t *driver = devman_driver_register();
|
---|
230 | if (NULL == driver)
|
---|
231 | return;
|
---|
232 |
|
---|
233 | fid_t fid = fibril_create(init_running_drv, driver);
|
---|
234 | if (fid == 0) {
|
---|
235 | printf(NAME ": Error creating fibril for the initialization of the newly registered running driver.\n");
|
---|
236 | exit(1);
|
---|
237 | }
|
---|
238 | fibril_add_ready(fid);
|
---|
239 |
|
---|
240 | /*thread_id_t tid;
|
---|
241 | if (0 != thread_create(init_running_drv, driver, "init_running_drv", &tid)) {
|
---|
242 | printf(NAME ": failed to start the initialization of the newly registered running driver.\n");
|
---|
243 | }*/
|
---|
244 |
|
---|
245 | ipc_callid_t callid;
|
---|
246 | ipc_call_t call;
|
---|
247 | bool cont = true;
|
---|
248 | while (cont) {
|
---|
249 | callid = async_get_call(&call);
|
---|
250 |
|
---|
251 | switch (IPC_GET_METHOD(call)) {
|
---|
252 | case IPC_M_PHONE_HUNGUP:
|
---|
253 | cont = false;
|
---|
254 | continue;
|
---|
255 | case DEVMAN_ADD_CHILD_DEVICE:
|
---|
256 | devman_add_child(callid, &call, driver);
|
---|
257 | break;
|
---|
258 | default:
|
---|
259 | ipc_answer_0(callid, EINVAL);
|
---|
260 | break;
|
---|
261 | }
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 | static void devman_forward(ipc_callid_t iid, ipc_call_t *icall, bool drv_to_parent) {
|
---|
266 |
|
---|
267 | device_handle_t handle = IPC_GET_ARG2(*icall);
|
---|
268 | // printf(NAME ": devman_forward - trying to forward connection to device with handle %x.\n", handle);
|
---|
269 |
|
---|
270 | node_t *dev = find_dev_node(&device_tree, handle);
|
---|
271 | if (NULL == dev) {
|
---|
272 | printf(NAME ": devman_forward error - no device with handle %x was found.\n", handle);
|
---|
273 | ipc_answer_0(iid, ENOENT);
|
---|
274 | return;
|
---|
275 | }
|
---|
276 |
|
---|
277 | driver_t *driver = NULL;
|
---|
278 |
|
---|
279 | if (drv_to_parent) {
|
---|
280 | if (NULL != dev->parent) {
|
---|
281 | driver = dev->parent->drv;
|
---|
282 | }
|
---|
283 | } else {
|
---|
284 | driver = dev->drv;
|
---|
285 | }
|
---|
286 |
|
---|
287 | if (NULL == driver) {
|
---|
288 | printf(NAME ": devman_forward error - no driver to connect to.\n", handle);
|
---|
289 | ipc_answer_0(iid, ENOENT);
|
---|
290 | return;
|
---|
291 | }
|
---|
292 |
|
---|
293 | int method;
|
---|
294 | if (drv_to_parent) {
|
---|
295 | method = DRIVER_DRIVER;
|
---|
296 | } else {
|
---|
297 | method = DRIVER_CLIENT;
|
---|
298 | }
|
---|
299 |
|
---|
300 | if (driver->phone <= 0) {
|
---|
301 | printf(NAME ": devman_forward: cound not forward to driver %s (the driver's phone is %x).\n", driver->name, driver->phone);
|
---|
302 | return;
|
---|
303 | }
|
---|
304 | printf(NAME ": devman_forward: forward connection to device %s to driver %s with phone %d.\n",
|
---|
305 | dev->pathname, driver->name, driver->phone);
|
---|
306 | ipc_forward_fast(iid, driver->phone, method, dev->handle, 0, IPC_FF_NONE);
|
---|
307 | }
|
---|
308 |
|
---|
309 | /** Function for handling connections to device manager.
|
---|
310 | *
|
---|
311 | */
|
---|
312 | static void devman_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
313 | {
|
---|
314 | // Select interface
|
---|
315 | switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
|
---|
316 | case DEVMAN_DRIVER:
|
---|
317 | devman_connection_driver(iid, icall);
|
---|
318 | break;
|
---|
319 | /*case DEVMAN_CLIENT:
|
---|
320 | devmap_connection_client(iid, icall);
|
---|
321 | break;*/
|
---|
322 | case DEVMAN_CONNECT_TO_DEVICE:
|
---|
323 | // Connect client to selected device
|
---|
324 | devman_forward(iid, icall, false);
|
---|
325 | break;
|
---|
326 | case DEVMAN_CONNECT_TO_PARENTS_DEVICE:
|
---|
327 | // Connect client to selected device
|
---|
328 | devman_forward(iid, icall, true);
|
---|
329 | break;
|
---|
330 | default:
|
---|
331 | /* No such interface */
|
---|
332 | ipc_answer_0(iid, ENOENT);
|
---|
333 | }
|
---|
334 | }
|
---|
335 |
|
---|
336 | /** Initialize device manager internal structures.
|
---|
337 | */
|
---|
338 | static bool devman_init()
|
---|
339 | {
|
---|
340 | printf(NAME ": devman_init - looking for available drivers. \n");
|
---|
341 |
|
---|
342 | // initialize list of available drivers
|
---|
343 | init_driver_list(&drivers_list);
|
---|
344 | if (0 == lookup_available_drivers(&drivers_list, DRIVER_DEFAULT_STORE)) {
|
---|
345 | printf(NAME " no drivers found.");
|
---|
346 | return false;
|
---|
347 | }
|
---|
348 | printf(NAME ": devman_init - list of drivers has been initialized. \n");
|
---|
349 |
|
---|
350 | // create root device node
|
---|
351 | if (!init_device_tree(&device_tree, &drivers_list)) {
|
---|
352 | printf(NAME " failed to initialize device tree.");
|
---|
353 | return false;
|
---|
354 | }
|
---|
355 |
|
---|
356 | return true;
|
---|
357 | }
|
---|
358 |
|
---|
359 | int main(int argc, char *argv[])
|
---|
360 | {
|
---|
361 | printf(NAME ": HelenOS Device Manager\n");
|
---|
362 |
|
---|
363 | if (!devman_init()) {
|
---|
364 | printf(NAME ": Error while initializing service\n");
|
---|
365 | return -1;
|
---|
366 | }
|
---|
367 |
|
---|
368 | // Set a handler of incomming connections
|
---|
369 | async_set_client_connection(devman_connection);
|
---|
370 |
|
---|
371 | // Register device manager at naming service
|
---|
372 | ipcarg_t phonead;
|
---|
373 | if (ipc_connect_to_me(PHONE_NS, SERVICE_DEVMAN, 0, 0, &phonead) != 0)
|
---|
374 | return -1;
|
---|
375 |
|
---|
376 | printf(NAME ": Accepting connections\n");
|
---|
377 | async_manager();
|
---|
378 |
|
---|
379 | // Never reached
|
---|
380 | return 0;
|
---|
381 | }
|
---|
382 |
|
---|
383 | /** @}
|
---|
384 | */ |
---|