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

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

devman, libdrv: replace async_string_receive by async_data_write_accept

  • Property mode set to 100644
File size: 12.4 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 <str.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
60static driver_list_t drivers_list;
61static dev_tree_t device_tree;
62
63
64/**
65 * Register running driver.
66 */
67static driver_t * devman_driver_register(void)
68{
69 printf(NAME ": devman_driver_register \n");
70
71 ipc_call_t icall;
72 ipc_callid_t iid = async_get_call(&icall);
73 driver_t *driver = NULL;
74
75 if (IPC_GET_METHOD(icall) != DEVMAN_DRIVER_REGISTER) {
76 ipc_answer_0(iid, EREFUSED);
77 return NULL;
78 }
79
80 char *drv_name = NULL;
81
82 // Get driver name
83 int rc = async_data_write_accept((void **)&drv_name, true, 0, 0, 0, 0);
84 if (rc != EOK) {
85 ipc_answer_0(iid, rc);
86 return NULL;
87 }
88 printf(NAME ": the %s driver is trying to register by the service.\n", drv_name);
89
90 // Find driver structure
91 driver = find_driver(&drivers_list, drv_name);
92
93 if (NULL == driver) {
94 printf(NAME ": no driver named %s was found.\n", drv_name);
95 free(drv_name);
96 drv_name = NULL;
97 ipc_answer_0(iid, ENOENT);
98 return NULL;
99 }
100
101 free(drv_name);
102 drv_name = NULL;
103
104 // Create connection to the driver
105 printf(NAME ": creating connection to the %s driver.\n", driver->name);
106 ipc_call_t call;
107 ipc_callid_t callid = async_get_call(&call);
108 if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
109 ipc_answer_0(callid, ENOTSUP);
110 ipc_answer_0(iid, ENOTSUP);
111 return NULL;
112 }
113
114 // remember driver's phone
115 set_driver_phone(driver, IPC_GET_ARG5(call));
116
117 printf(NAME ": the %s driver was successfully registered as running.\n", driver->name);
118
119 ipc_answer_0(callid, EOK);
120
121 ipc_answer_0(iid, EOK);
122
123 return driver;
124}
125
126/**
127 * Receive device match ID from the device's parent driver and add it to the list of devices match ids.
128 *
129 * @param match_ids the list of the device's match ids.
130 *
131 * @return 0 on success, negative error code otherwise.
132 */
133static int devman_receive_match_id(match_id_list_t *match_ids) {
134
135 match_id_t *match_id = create_match_id();
136 ipc_callid_t callid;
137 ipc_call_t call;
138 int rc = 0;
139
140 callid = async_get_call(&call);
141 if (DEVMAN_ADD_MATCH_ID != IPC_GET_METHOD(call)) {
142 printf(NAME ": ERROR: devman_receive_match_id - invalid protocol.\n");
143 ipc_answer_0(callid, EINVAL);
144 delete_match_id(match_id);
145 return EINVAL;
146 }
147
148 if (NULL == match_id) {
149 printf(NAME ": ERROR: devman_receive_match_id - failed to allocate match id.\n");
150 ipc_answer_0(callid, ENOMEM);
151 return ENOMEM;
152 }
153
154 ipc_answer_0(callid, EOK);
155
156 match_id->score = IPC_GET_ARG1(call);
157
158 char *match_id_str;
159 rc = async_data_write_accept((void **)&match_id_str, true, 0, 0, 0, 0);
160 match_id->id = match_id_str;
161 if (EOK != rc) {
162 delete_match_id(match_id);
163 printf(NAME ": devman_receive_match_id - failed to receive match id string.\n");
164 return rc;
165 }
166
167 list_append(&match_id->link, &match_ids->ids);
168
169 printf(NAME ": received match id '%s', score = %d \n", match_id->id, match_id->score);
170 return rc;
171}
172
173/**
174 * Receive device match IDs from the device's parent driver
175 * and add them to the list of devices match ids.
176 *
177 * @param match_count the number of device's match ids to be received.
178 * @param match_ids the list of the device's match ids.
179 *
180 * @return 0 on success, negative error code otherwise.
181 */
182static int devman_receive_match_ids(ipcarg_t match_count, match_id_list_t *match_ids)
183{
184 int ret = EOK;
185 size_t i;
186 for (i = 0; i < match_count; i++) {
187 if (EOK != (ret = devman_receive_match_id(match_ids))) {
188 return ret;
189 }
190 }
191 return ret;
192}
193
194/** Handle child device registration.
195 *
196 * Child devices are registered by their parent's device driver.
197 */
198static void devman_add_child(ipc_callid_t callid, ipc_call_t *call)
199{
200 //printf(NAME ": devman_add_child\n");
201
202 device_handle_t parent_handle = IPC_GET_ARG1(*call);
203 ipcarg_t match_count = IPC_GET_ARG2(*call);
204 dev_tree_t *tree = &device_tree;
205
206 fibril_rwlock_write_lock(&tree->rwlock);
207 node_t *parent = find_dev_node_no_lock(&device_tree, parent_handle);
208
209 if (NULL == parent) {
210 fibril_rwlock_write_unlock(&tree->rwlock);
211 ipc_answer_0(callid, ENOENT);
212 return;
213 }
214
215 char *dev_name = NULL;
216 int rc = async_data_write_accept((void **)&dev_name, true, 0, 0, 0, 0);
217 if (EOK != rc) {
218 fibril_rwlock_write_unlock(&tree->rwlock);
219 ipc_answer_0(callid, rc);
220 return;
221 }
222 //printf(NAME ": newly added child device's name is '%s'.\n", dev_name);
223
224 node_t *node = create_dev_node();
225 if (!insert_dev_node(&device_tree, node, dev_name, parent)) {
226 fibril_rwlock_write_unlock(&tree->rwlock);
227 delete_dev_node(node);
228 ipc_answer_0(callid, ENOMEM);
229 return;
230 }
231 fibril_rwlock_write_unlock(&tree->rwlock);
232
233 printf(NAME ": devman_add_child %s\n", node->pathname);
234
235 devman_receive_match_ids(match_count, &node->match_ids);
236
237 // return device handle to parent's driver
238 ipc_answer_1(callid, EOK, node->handle);
239
240 // try to find suitable driver and assign it to the device
241 assign_driver(node, &drivers_list);
242}
243
244/**
245 * Initialize driver which has registered itself as running and ready.
246 *
247 * The initialization is done in a separate fibril to avoid deadlocks
248 * (if the driver needed to be served by devman during the driver's initialization).
249 */
250static int init_running_drv(void *drv)
251{
252 driver_t *driver = (driver_t *)drv;
253 initialize_running_driver(driver);
254 printf(NAME ": the %s driver was successfully initialized. \n", driver->name);
255 return 0;
256}
257
258/** Function for handling connections from a driver to the device manager.
259 */
260static void devman_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
261{
262 /* Accept the connection */
263 ipc_answer_0(iid, EOK);
264
265 driver_t *driver = devman_driver_register();
266 if (NULL == driver)
267 return;
268
269 // Initialize the driver as running (e.g. pass assigned devices to it) in a separate fibril;
270 // the separate fibril is used to enable the driver
271 // to use devman service during the driver's initialization.
272 fid_t fid = fibril_create(init_running_drv, driver);
273 if (fid == 0) {
274 printf(NAME ": Error creating fibril for the initialization of the newly registered running driver.\n");
275 return;
276 }
277 fibril_add_ready(fid);
278
279 /*thread_id_t tid;
280 if (0 != thread_create(init_running_drv, driver, "init_running_drv", &tid)) {
281 printf(NAME ": failed to start the initialization of the newly registered running driver.\n");
282 }*/
283
284 ipc_callid_t callid;
285 ipc_call_t call;
286 bool cont = true;
287 while (cont) {
288 callid = async_get_call(&call);
289
290 switch (IPC_GET_METHOD(call)) {
291 case IPC_M_PHONE_HUNGUP:
292 cont = false;
293 continue;
294 case DEVMAN_ADD_CHILD_DEVICE:
295 devman_add_child(callid, &call);
296 break;
297 default:
298 ipc_answer_0(callid, EINVAL);
299 break;
300 }
301 }
302}
303
304/** Find handle for the device instance identified by the device's path in the device tree.
305 */
306static void devman_device_get_handle(ipc_callid_t iid, ipc_call_t *icall)
307{
308 char *pathname;
309 int rc = async_data_write_accept((void **)&pathname, true, 0, 0, 0, 0);
310 if (rc != EOK) {
311 ipc_answer_0(iid, rc);
312 return;
313 }
314
315 node_t * dev = find_dev_node_by_path(&device_tree, pathname);
316
317 free(pathname);
318
319 if (NULL == dev) {
320 ipc_answer_0(iid, ENOENT);
321 return;
322 }
323
324 ipc_answer_1(iid, EOK, dev->handle);
325}
326
327
328/** Function for handling connections from a client to the device manager.
329 */
330static void devman_connection_client(ipc_callid_t iid, ipc_call_t *icall)
331{
332 /* Accept connection */
333 ipc_answer_0(iid, EOK);
334
335 bool cont = true;
336 while (cont) {
337 ipc_call_t call;
338 ipc_callid_t callid = async_get_call(&call);
339
340 switch (IPC_GET_METHOD(call)) {
341 case IPC_M_PHONE_HUNGUP:
342 cont = false;
343 continue;
344 case DEVMAN_DEVICE_GET_HANDLE:
345 devman_device_get_handle(callid, &call);
346 break;
347 default:
348 if (!(callid & IPC_CALLID_NOTIFICATION))
349 ipc_answer_0(callid, ENOENT);
350 }
351 }
352}
353
354static void devman_forward(ipc_callid_t iid, ipc_call_t *icall, bool drv_to_parent) {
355
356 device_handle_t handle = IPC_GET_ARG2(*icall);
357 // printf(NAME ": devman_forward - trying to forward connection to device with handle %x.\n", handle);
358
359 node_t *dev = find_dev_node(&device_tree, handle);
360 if (NULL == dev) {
361 printf(NAME ": devman_forward error - no device with handle %x was found.\n", handle);
362 ipc_answer_0(iid, ENOENT);
363 return;
364 }
365
366 driver_t *driver = NULL;
367
368 if (drv_to_parent) {
369 if (NULL != dev->parent) {
370 driver = dev->parent->drv;
371 }
372 } else if (DEVICE_USABLE == dev->state) {
373 driver = dev->drv;
374 assert(NULL != driver);
375 }
376
377 if (NULL == driver) {
378 printf(NAME ": devman_forward error - the device is not in usable state.\n", handle);
379 ipc_answer_0(iid, ENOENT);
380 return;
381 }
382
383 int method;
384 if (drv_to_parent) {
385 method = DRIVER_DRIVER;
386 } else {
387 method = DRIVER_CLIENT;
388 }
389
390 if (driver->phone <= 0) {
391 printf(NAME ": devman_forward: cound not forward to driver %s ", driver->name);
392 printf("the driver's phone is %x).\n", driver->phone);
393 return;
394 }
395 printf(NAME ": devman_forward: forward connection to device %s to driver %s.\n", dev->pathname, driver->name);
396 ipc_forward_fast(iid, driver->phone, method, dev->handle, 0, IPC_FF_NONE);
397}
398
399/** Function for handling connections to device manager.
400 *
401 */
402static void devman_connection(ipc_callid_t iid, ipc_call_t *icall)
403{
404 // Select interface
405 switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
406 case DEVMAN_DRIVER:
407 devman_connection_driver(iid, icall);
408 break;
409 case DEVMAN_CLIENT:
410 devman_connection_client(iid, icall);
411 break;
412 case DEVMAN_CONNECT_TO_DEVICE:
413 // Connect client to selected device
414 devman_forward(iid, icall, false);
415 break;
416 case DEVMAN_CONNECT_TO_PARENTS_DEVICE:
417 // Connect client to selected device
418 devman_forward(iid, icall, true);
419 break;
420 default:
421 /* No such interface */
422 ipc_answer_0(iid, ENOENT);
423 }
424}
425
426/** Initialize device manager internal structures.
427 */
428static bool devman_init()
429{
430 printf(NAME ": devman_init - looking for available drivers. \n");
431
432 // initialize list of available drivers
433 init_driver_list(&drivers_list);
434 if (0 == lookup_available_drivers(&drivers_list, DRIVER_DEFAULT_STORE)) {
435 printf(NAME " no drivers found.");
436 return false;
437 }
438 printf(NAME ": devman_init - list of drivers has been initialized. \n");
439
440 // create root device node
441 if (!init_device_tree(&device_tree, &drivers_list)) {
442 printf(NAME " failed to initialize device tree.");
443 return false;
444 }
445
446 return true;
447}
448
449int main(int argc, char *argv[])
450{
451 printf(NAME ": HelenOS Device Manager\n");
452
453 if (!devman_init()) {
454 printf(NAME ": Error while initializing service\n");
455 return -1;
456 }
457
458 // Set a handler of incomming connections
459 async_set_client_connection(devman_connection);
460
461 // Register device manager at naming service
462 ipcarg_t phonead;
463 if (ipc_connect_to_me(PHONE_NS, SERVICE_DEVMAN, 0, 0, &phonead) != 0)
464 return -1;
465
466 printf(NAME ": Accepting connections\n");
467 async_manager();
468
469 // Never reached
470 return 0;
471}
472
473/** @}
474 */
Note: See TracBrowser for help on using the repository browser.